Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/resctrl: Clean up resctrl features check

Checking resctrl features call strcmp() to compare feature strings
(e.g. "mba", "cat" etc). The checkings are error prone and don't have
good coding style. Define the constant strings in macros and call
strncmp() to solve the potential issues.

Suggested-by: Shuah Khan <skhan@linuxfoundation.org>
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>

authored by

Fenghua Yu and committed by
Shuah Khan
24286736 896016d2

+41 -35
+4 -4
tools/testing/selftests/resctrl/cache.c
··· 182 182 /* 183 183 * Measure cache miss from perf. 184 184 */ 185 - if (!strcmp(param->resctrl_val, "cat")) { 185 + if (!strncmp(param->resctrl_val, CAT_STR, sizeof(CAT_STR))) { 186 186 ret = get_llc_perf(&llc_perf_miss); 187 187 if (ret < 0) 188 188 return ret; ··· 192 192 /* 193 193 * Measure llc occupancy from resctrl. 194 194 */ 195 - if (!strcmp(param->resctrl_val, "cqm")) { 195 + if (!strncmp(param->resctrl_val, CQM_STR, sizeof(CQM_STR))) { 196 196 ret = get_llc_occu_resctrl(&llc_occu_resc); 197 197 if (ret < 0) 198 198 return ret; ··· 234 234 if (ret) 235 235 return ret; 236 236 237 - if ((strcmp(resctrl_val, "cat") == 0)) { 237 + if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) { 238 238 ret = initialize_llc_perf(); 239 239 if (ret) 240 240 return ret; ··· 242 242 243 243 /* Test runs until the callback setup() tells the test to stop. */ 244 244 while (1) { 245 - if (strcmp(resctrl_val, "cat") == 0) { 245 + if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) { 246 246 ret = param->setup(1, param); 247 247 if (ret) { 248 248 ret = 0;
+1 -1
tools/testing/selftests/resctrl/cat_test.c
··· 164 164 return -1; 165 165 166 166 struct resctrl_val_param param = { 167 - .resctrl_val = "cat", 167 + .resctrl_val = CAT_STR, 168 168 .cpu_no = cpu_no, 169 169 .mum_resctrlfs = 0, 170 170 .setup = cat_setup,
+1 -1
tools/testing/selftests/resctrl/cqm_test.c
··· 145 145 } 146 146 147 147 struct resctrl_val_param param = { 148 - .resctrl_val = "cqm", 148 + .resctrl_val = CQM_STR, 149 149 .ctrlgrp = "c1", 150 150 .mongrp = "m1", 151 151 .cpu_no = cpu_no,
+2 -2
tools/testing/selftests/resctrl/fill_buf.c
··· 115 115 116 116 while (1) { 117 117 ret = fill_one_span_read(start_ptr, end_ptr); 118 - if (!strcmp(resctrl_val, "cat")) 118 + if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) 119 119 break; 120 120 } 121 121 ··· 134 134 { 135 135 while (1) { 136 136 fill_one_span_write(start_ptr, end_ptr); 137 - if (!strcmp(resctrl_val, "cat")) 137 + if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) 138 138 break; 139 139 } 140 140
+1 -1
tools/testing/selftests/resctrl/mba_test.c
··· 141 141 int mba_schemata_change(int cpu_no, char *bw_report, char **benchmark_cmd) 142 142 { 143 143 struct resctrl_val_param param = { 144 - .resctrl_val = "mba", 144 + .resctrl_val = MBA_STR, 145 145 .ctrlgrp = "c1", 146 146 .mongrp = "m1", 147 147 .cpu_no = cpu_no,
+1 -1
tools/testing/selftests/resctrl/mbm_test.c
··· 114 114 int mbm_bw_change(int span, int cpu_no, char *bw_report, char **benchmark_cmd) 115 115 { 116 116 struct resctrl_val_param param = { 117 - .resctrl_val = "mbm", 117 + .resctrl_val = MBM_STR, 118 118 .ctrlgrp = "c1", 119 119 .mongrp = "m1", 120 120 .span = span,
+5
tools/testing/selftests/resctrl/resctrl.h
··· 62 62 int (*setup)(int num, ...); 63 63 }; 64 64 65 + #define MBM_STR "mbm" 66 + #define MBA_STR "mba" 67 + #define CQM_STR "cqm" 68 + #define CAT_STR "cat" 69 + 65 70 extern pid_t bm_pid, ppid; 66 71 extern int tests_run; 67 72
+6 -6
tools/testing/selftests/resctrl/resctrl_tests.c
··· 85 85 cqm_test = false; 86 86 cat_test = false; 87 87 while (token) { 88 - if (!strcmp(token, "mbm")) { 88 + if (!strncmp(token, MBM_STR, sizeof(MBM_STR))) { 89 89 mbm_test = true; 90 - } else if (!strcmp(token, "mba")) { 90 + } else if (!strncmp(token, MBA_STR, sizeof(MBA_STR))) { 91 91 mba_test = true; 92 - } else if (!strcmp(token, "cqm")) { 92 + } else if (!strncmp(token, CQM_STR, sizeof(CQM_STR))) { 93 93 cqm_test = true; 94 - } else if (!strcmp(token, "cat")) { 94 + } else if (!strncmp(token, CAT_STR, sizeof(CAT_STR))) { 95 95 cat_test = true; 96 96 } else { 97 97 printf("invalid argument\n"); ··· 161 161 if (!is_amd && mbm_test) { 162 162 printf("# Starting MBM BW change ...\n"); 163 163 if (!has_ben) 164 - sprintf(benchmark_cmd[5], "%s", "mba"); 164 + sprintf(benchmark_cmd[5], "%s", MBA_STR); 165 165 res = mbm_bw_change(span, cpu_no, bw_report, benchmark_cmd); 166 166 printf("%sok MBM: bw change\n", res ? "not " : ""); 167 167 mbm_test_cleanup(); ··· 181 181 if (cqm_test) { 182 182 printf("# Starting CQM test ...\n"); 183 183 if (!has_ben) 184 - sprintf(benchmark_cmd[5], "%s", "cqm"); 184 + sprintf(benchmark_cmd[5], "%s", CQM_STR); 185 185 res = cqm_resctrl_val(cpu_no, no_of_bits, benchmark_cmd); 186 186 printf("%sok CQM: test\n", res ? "not " : ""); 187 187 cqm_test_cleanup();
+11 -11
tools/testing/selftests/resctrl/resctrl_val.c
··· 397 397 return; 398 398 } 399 399 400 - if (strcmp(resctrl_val, "mbm") == 0) 400 + if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) 401 401 set_mbm_path(ctrlgrp, mongrp, resource_id); 402 402 403 - if ((strcmp(resctrl_val, "mba") == 0)) { 403 + if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) { 404 404 if (ctrlgrp) 405 405 sprintf(mbm_total_path, CON_MBM_LOCAL_BYTES_PATH, 406 406 RESCTRL_PATH, ctrlgrp, resource_id); ··· 524 524 return; 525 525 } 526 526 527 - if (strcmp(resctrl_val, "cqm") == 0) 527 + if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) 528 528 set_cqm_path(ctrlgrp, mongrp, resource_id); 529 529 } 530 530 ··· 579 579 if (strcmp(param->filename, "") == 0) 580 580 sprintf(param->filename, "stdio"); 581 581 582 - if ((strcmp(resctrl_val, "mba")) == 0 || 583 - (strcmp(resctrl_val, "mbm")) == 0) { 582 + if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) || 583 + !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) { 584 584 ret = validate_bw_report_request(param->bw_report); 585 585 if (ret) 586 586 return ret; ··· 674 674 if (ret) 675 675 goto out; 676 676 677 - if ((strcmp(resctrl_val, "mbm") == 0) || 678 - (strcmp(resctrl_val, "mba") == 0)) { 677 + if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) || 678 + !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) { 679 679 ret = initialize_mem_bw_imc(); 680 680 if (ret) 681 681 goto out; 682 682 683 683 initialize_mem_bw_resctrl(param->ctrlgrp, param->mongrp, 684 684 param->cpu_no, resctrl_val); 685 - } else if (strcmp(resctrl_val, "cqm") == 0) 685 + } else if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) 686 686 initialize_llc_occu_resctrl(param->ctrlgrp, param->mongrp, 687 687 param->cpu_no, resctrl_val); 688 688 ··· 710 710 711 711 /* Test runs until the callback setup() tells the test to stop. */ 712 712 while (1) { 713 - if ((strcmp(resctrl_val, "mbm") == 0) || 714 - (strcmp(resctrl_val, "mba") == 0)) { 713 + if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) || 714 + !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) { 715 715 ret = param->setup(1, param); 716 716 if (ret) { 717 717 ret = 0; ··· 721 721 ret = measure_vals(param, &bw_resc_start); 722 722 if (ret) 723 723 break; 724 - } else if (strcmp(resctrl_val, "cqm") == 0) { 724 + } else if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) { 725 725 ret = param->setup(1, param); 726 726 if (ret) { 727 727 ret = 0;
+9 -8
tools/testing/selftests/resctrl/resctrlfs.c
··· 334 334 operation = atoi(benchmark_cmd[4]); 335 335 sprintf(resctrl_val, "%s", benchmark_cmd[5]); 336 336 337 - if (strcmp(resctrl_val, "cqm") != 0) 337 + if (strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) 338 338 buffer_span = span * MB; 339 339 else 340 340 buffer_span = span; ··· 459 459 goto out; 460 460 461 461 /* Create mon grp and write pid into it for "mbm" and "cqm" test */ 462 - if ((strcmp(resctrl_val, "cqm") == 0) || 463 - (strcmp(resctrl_val, "mbm") == 0)) { 462 + if (!strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR)) || 463 + !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR))) { 464 464 if (strlen(mongrp)) { 465 465 sprintf(monitorgroup_p, "%s/mon_groups", controlgroup); 466 466 sprintf(monitorgroup, "%s/%s", monitorgroup_p, mongrp); ··· 505 505 int resource_id, ret = 0; 506 506 FILE *fp; 507 507 508 - if ((strcmp(resctrl_val, "mba") != 0) && 509 - (strcmp(resctrl_val, "cat") != 0) && 510 - (strcmp(resctrl_val, "cqm") != 0)) 508 + if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) && 509 + strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) && 510 + strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) 511 511 return -ENOENT; 512 512 513 513 if (!schemata) { ··· 528 528 else 529 529 sprintf(controlgroup, "%s/schemata", RESCTRL_PATH); 530 530 531 - if (!strcmp(resctrl_val, "cat") || !strcmp(resctrl_val, "cqm")) 531 + if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) || 532 + !strncmp(resctrl_val, CQM_STR, sizeof(CQM_STR))) 532 533 sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata); 533 - if (strcmp(resctrl_val, "mba") == 0) 534 + if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) 534 535 sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata); 535 536 536 537 fp = fopen(controlgroup, "w");