selftests/resctrl: Fix checking for < 0 for unsigned values

Dan reported following static checker warnings

tools/testing/selftests/resctrl/resctrl_val.c:545 measure_vals()
warn: 'bw_imc' unsigned <= 0

tools/testing/selftests/resctrl/resctrl_val.c:549 measure_vals()
warn: 'bw_resc_end' unsigned <= 0

These warnings are reported because
1. measure_vals() declares 'bw_imc' and 'bw_resc_end' as unsigned long
   variables
2. Return value of get_mem_bw_imc() and get_mem_bw_resctrl() are assigned
   to 'bw_imc' and 'bw_resc_end' respectively
3. The returned values are checked for <= 0 to see if the calls failed

Checking for < 0 for an unsigned value doesn't make any sense.

Fix this issue by changing the implementation of get_mem_bw_imc() and
get_mem_bw_resctrl() such that they now accept reference to a variable
and set the variable appropriately upon success and return 0, else return
< 0 on error.

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Tested-by: Babu Moger <babu.moger@amd.com>
Signed-off-by: Fenghua Yu <fenghua.yu@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
Fenghua Yu 2021-03-17 02:22:54 +00:00 коммит произвёл Shuah Khan
Родитель d81343b5ee
Коммит 1205b688c9
1 изменённых файлов: 23 добавлений и 18 удалений

Просмотреть файл

@ -300,9 +300,9 @@ static int initialize_mem_bw_imc(void)
* Memory B/W utilized by a process on a socket can be calculated using * Memory B/W utilized by a process on a socket can be calculated using
* iMC counters. Perf events are used to read these counters. * iMC counters. Perf events are used to read these counters.
* *
* Return: >= 0 on success. < 0 on failure. * Return: = 0 on success. < 0 on failure.
*/ */
static float get_mem_bw_imc(int cpu_no, char *bw_report) static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc)
{ {
float reads, writes, of_mul_read, of_mul_write; float reads, writes, of_mul_read, of_mul_write;
int imc, j, ret; int imc, j, ret;
@ -373,13 +373,18 @@ static float get_mem_bw_imc(int cpu_no, char *bw_report)
close(imc_counters_config[imc][WRITE].fd); close(imc_counters_config[imc][WRITE].fd);
} }
if (strcmp(bw_report, "reads") == 0) if (strcmp(bw_report, "reads") == 0) {
return reads; *bw_imc = reads;
return 0;
}
if (strcmp(bw_report, "writes") == 0) if (strcmp(bw_report, "writes") == 0) {
return writes; *bw_imc = writes;
return 0;
}
return (reads + writes); *bw_imc = reads + writes;
return 0;
} }
void set_mbm_path(const char *ctrlgrp, const char *mongrp, int resource_id) void set_mbm_path(const char *ctrlgrp, const char *mongrp, int resource_id)
@ -438,9 +443,8 @@ static void initialize_mem_bw_resctrl(const char *ctrlgrp, const char *mongrp,
* 1. If con_mon grp is given, then read from it * 1. If con_mon grp is given, then read from it
* 2. If con_mon grp is not given, then read from root con_mon grp * 2. If con_mon grp is not given, then read from root con_mon grp
*/ */
static unsigned long get_mem_bw_resctrl(void) static int get_mem_bw_resctrl(unsigned long *mbm_total)
{ {
unsigned long mbm_total = 0;
FILE *fp; FILE *fp;
fp = fopen(mbm_total_path, "r"); fp = fopen(mbm_total_path, "r");
@ -449,7 +453,7 @@ static unsigned long get_mem_bw_resctrl(void)
return -1; return -1;
} }
if (fscanf(fp, "%lu", &mbm_total) <= 0) { if (fscanf(fp, "%lu", mbm_total) <= 0) {
perror("Could not get mbm local bytes"); perror("Could not get mbm local bytes");
fclose(fp); fclose(fp);
@ -457,7 +461,7 @@ static unsigned long get_mem_bw_resctrl(void)
} }
fclose(fp); fclose(fp);
return mbm_total; return 0;
} }
pid_t bm_pid, ppid; pid_t bm_pid, ppid;
@ -549,7 +553,8 @@ static void initialize_llc_occu_resctrl(const char *ctrlgrp, const char *mongrp,
static int static int
measure_vals(struct resctrl_val_param *param, unsigned long *bw_resc_start) measure_vals(struct resctrl_val_param *param, unsigned long *bw_resc_start)
{ {
unsigned long bw_imc, bw_resc, bw_resc_end; unsigned long bw_resc, bw_resc_end;
float bw_imc;
int ret; int ret;
/* /*
@ -559,13 +564,13 @@ measure_vals(struct resctrl_val_param *param, unsigned long *bw_resc_start)
* Compare the two values to validate resctrl value. * Compare the two values to validate resctrl value.
* It takes 1sec to measure the data. * It takes 1sec to measure the data.
*/ */
bw_imc = get_mem_bw_imc(param->cpu_no, param->bw_report); ret = get_mem_bw_imc(param->cpu_no, param->bw_report, &bw_imc);
if (bw_imc <= 0) if (ret < 0)
return bw_imc; return ret;
bw_resc_end = get_mem_bw_resctrl(); ret = get_mem_bw_resctrl(&bw_resc_end);
if (bw_resc_end <= 0) if (ret < 0)
return bw_resc_end; return ret;
bw_resc = (bw_resc_end - *bw_resc_start) / MB; bw_resc = (bw_resc_end - *bw_resc_start) / MB;
ret = print_results_bw(param->filename, bm_pid, bw_imc, bw_resc); ret = print_results_bw(param->filename, bm_pid, bw_imc, bw_resc);