Skip to content

Commit c44000b

Browse files
ij-intelshuahkh
authored andcommitted
selftests/resctrl: Fix closing IMC fds on error and open-code R+W instead of loops
The imc perf fd close() calls are missing from all error paths. In addition, get_mem_bw_imc() handles fds in a for loop but close() is based on two fixed indexes READ and WRITE. Open code inner for loops to READ+WRITE entries for clarity and add a function to close() IMC fds properly in all cases. Fixes: 7f4d257 ("selftests/resctrl: Add callback to start a benchmark") Suggested-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Tested-by: Babu Moger <babu.moger@amd.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent b47619a commit c44000b

1 file changed

Lines changed: 36 additions & 18 deletions

File tree

tools/testing/selftests/resctrl/resctrl_val.c

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ static int initialize_mem_bw_imc(void)
293293
return 0;
294294
}
295295

296+
static void perf_close_imc_mem_bw(void)
297+
{
298+
int mc;
299+
300+
for (mc = 0; mc < imcs; mc++) {
301+
if (imc_counters_config[mc][READ].fd != -1)
302+
close(imc_counters_config[mc][READ].fd);
303+
if (imc_counters_config[mc][WRITE].fd != -1)
304+
close(imc_counters_config[mc][WRITE].fd);
305+
}
306+
}
307+
296308
/*
297309
* get_mem_bw_imc: Memory band width as reported by iMC counters
298310
* @cpu_no: CPU number that the benchmark PID is binded to
@@ -306,26 +318,33 @@ static int initialize_mem_bw_imc(void)
306318
static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc)
307319
{
308320
float reads, writes, of_mul_read, of_mul_write;
309-
int imc, j, ret;
321+
int imc, ret;
322+
323+
for (imc = 0; imc < imcs; imc++) {
324+
imc_counters_config[imc][READ].fd = -1;
325+
imc_counters_config[imc][WRITE].fd = -1;
326+
}
310327

311328
/* Start all iMC counters to log values (both read and write) */
312329
reads = 0, writes = 0, of_mul_read = 1, of_mul_write = 1;
313330
for (imc = 0; imc < imcs; imc++) {
314-
for (j = 0; j < 2; j++) {
315-
ret = open_perf_event(imc, cpu_no, j);
316-
if (ret)
317-
return -1;
318-
}
319-
for (j = 0; j < 2; j++)
320-
membw_ioctl_perf_event_ioc_reset_enable(imc, j);
331+
ret = open_perf_event(imc, cpu_no, READ);
332+
if (ret)
333+
goto close_fds;
334+
ret = open_perf_event(imc, cpu_no, WRITE);
335+
if (ret)
336+
goto close_fds;
337+
338+
membw_ioctl_perf_event_ioc_reset_enable(imc, READ);
339+
membw_ioctl_perf_event_ioc_reset_enable(imc, WRITE);
321340
}
322341

323342
sleep(1);
324343

325344
/* Stop counters after a second to get results (both read and write) */
326345
for (imc = 0; imc < imcs; imc++) {
327-
for (j = 0; j < 2; j++)
328-
membw_ioctl_perf_event_ioc_disable(imc, j);
346+
membw_ioctl_perf_event_ioc_disable(imc, READ);
347+
membw_ioctl_perf_event_ioc_disable(imc, WRITE);
329348
}
330349

331350
/*
@@ -341,15 +360,13 @@ static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc)
341360
if (read(r->fd, &r->return_value,
342361
sizeof(struct membw_read_format)) == -1) {
343362
ksft_perror("Couldn't get read b/w through iMC");
344-
345-
return -1;
363+
goto close_fds;
346364
}
347365

348366
if (read(w->fd, &w->return_value,
349367
sizeof(struct membw_read_format)) == -1) {
350368
ksft_perror("Couldn't get write bw through iMC");
351-
352-
return -1;
369+
goto close_fds;
353370
}
354371

355372
__u64 r_time_enabled = r->return_value.time_enabled;
@@ -369,10 +386,7 @@ static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc)
369386
writes += w->return_value.value * of_mul_write * SCALE;
370387
}
371388

372-
for (imc = 0; imc < imcs; imc++) {
373-
close(imc_counters_config[imc][READ].fd);
374-
close(imc_counters_config[imc][WRITE].fd);
375-
}
389+
perf_close_imc_mem_bw();
376390

377391
if (strcmp(bw_report, "reads") == 0) {
378392
*bw_imc = reads;
@@ -386,6 +400,10 @@ static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc)
386400

387401
*bw_imc = reads + writes;
388402
return 0;
403+
404+
close_fds:
405+
perf_close_imc_mem_bw();
406+
return -1;
389407
}
390408

391409
void set_mbm_path(const char *ctrlgrp, const char *mongrp, int domain_id)

0 commit comments

Comments
 (0)