Skip to content

Commit 20d96b2

Browse files
maciejwieczorretmanshuahkh
authored andcommitted
selftests/resctrl: Fix schemata write error check
Writing bitmasks to the schemata can fail when the bitmask doesn't adhere to constraints defined by what a particular CPU supports. Some example of constraints are max length or having contiguous bits. The driver should properly return errors when any rule concerning bitmask format is broken. Resctrl FS returns error codes from fprintf() only when fclose() is called. Current error checking scheme allows invalid bitmasks to be written into schemata file and the selftest doesn't notice because the fclose() error code isn't checked. Substitute fopen(), flose() and fprintf() with open(), close() and write() to avoid error code buffering between fprintf() and fclose(). Remove newline character from the schema string after writing it to the schemata file so it prints correctly before function return. Pass the string generated with strerror() to the "reason" buffer so the error message is more verbose. Extend "reason" buffer so it can hold longer messages. Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@intel.com> Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent ef43c30 commit 20d96b2

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

tools/testing/selftests/resctrl/resctrlfs.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
99
* Fenghua Yu <fenghua.yu@intel.com>
1010
*/
11+
#include <fcntl.h>
1112
#include <limits.h>
1213

1314
#include "resctrl.h"
@@ -490,9 +491,8 @@ int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
490491
*/
491492
int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
492493
{
493-
char controlgroup[1024], schema[1024], reason[64];
494-
int resource_id, ret = 0;
495-
FILE *fp;
494+
char controlgroup[1024], reason[128], schema[1024] = {};
495+
int resource_id, fd, schema_len = -1, ret = 0;
496496

497497
if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
498498
strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
@@ -520,28 +520,39 @@ int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
520520

521521
if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
522522
!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
523-
sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata);
523+
schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n",
524+
"L3:", resource_id, '=', schemata);
524525
if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
525526
!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
526-
sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata);
527-
528-
fp = fopen(controlgroup, "w");
529-
if (!fp) {
530-
sprintf(reason, "Failed to open control group");
527+
schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n",
528+
"MB:", resource_id, '=', schemata);
529+
if (schema_len < 0 || schema_len >= sizeof(schema)) {
530+
snprintf(reason, sizeof(reason),
531+
"snprintf() failed with return value : %d", schema_len);
531532
ret = -1;
532-
533533
goto out;
534534
}
535535

536-
if (fprintf(fp, "%s\n", schema) < 0) {
537-
sprintf(reason, "Failed to write schemata in control group");
538-
fclose(fp);
536+
fd = open(controlgroup, O_WRONLY);
537+
if (fd < 0) {
538+
snprintf(reason, sizeof(reason),
539+
"open() failed : %s", strerror(errno));
539540
ret = -1;
540541

541-
goto out;
542+
goto err_schema_not_empty;
542543
}
543-
fclose(fp);
544+
if (write(fd, schema, schema_len) < 0) {
545+
snprintf(reason, sizeof(reason),
546+
"write() failed : %s", strerror(errno));
547+
close(fd);
548+
ret = -1;
549+
550+
goto err_schema_not_empty;
551+
}
552+
close(fd);
544553

554+
err_schema_not_empty:
555+
schema[schema_len - 1] = 0;
545556
out:
546557
ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
547558
schema, ret ? " # " : "",

0 commit comments

Comments
 (0)