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

perf test hwmon_pmu: Fix event file location

The temp directory is made and a known fake hwmon PMU created within
it. Prior to this fix the events were being incorrectly written to the
temp directory rather than the fake PMU directory. This didn't impact
the test as the directory fd matched the wrong location, but it
doesn't mirror what a hwmon PMU would actually look like.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@kernel.org>
Link: https://lore.kernel.org/r/20241206042306.1055913-2-irogers@google.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Ian Rogers and committed by
Namhyung Kim
d4e17a32 3f61a12b

+18 -11
+18 -11
tools/perf/tests/hwmon_pmu.c
··· 65 65 { "temp2_label", "test hwmon event2\n", }, 66 66 { "temp2_input", "50000\n", }, 67 67 }; 68 - int dirfd, file; 68 + int hwmon_dirfd = -1, test_dirfd = -1, file; 69 69 struct perf_pmu *hwm = NULL; 70 70 ssize_t len; 71 71 ··· 76 76 dir[0] = '\0'; 77 77 return NULL; 78 78 } 79 - dirfd = open(dir, O_DIRECTORY); 80 - if (dirfd < 0) { 79 + test_dirfd = open(dir, O_PATH|O_DIRECTORY); 80 + if (test_dirfd < 0) { 81 81 pr_err("Failed to open test directory \"%s\"\n", dir); 82 82 goto err_out; 83 83 } 84 84 85 85 /* Create the test hwmon directory and give it a name. */ 86 - if (mkdirat(dirfd, "hwmon1234", 0755) < 0) { 86 + if (mkdirat(test_dirfd, "hwmon1234", 0755) < 0) { 87 87 pr_err("Failed to mkdir hwmon directory\n"); 88 88 goto err_out; 89 89 } 90 - file = openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600); 91 - if (!file) { 90 + hwmon_dirfd = openat(test_dirfd, "hwmon1234", O_DIRECTORY); 91 + if (hwmon_dirfd < 0) { 92 + pr_err("Failed to open test hwmon directory \"%s/hwmon1234\"\n", dir); 93 + goto err_out; 94 + } 95 + file = openat(hwmon_dirfd, "name", O_WRONLY | O_CREAT, 0600); 96 + if (file < 0) { 92 97 pr_err("Failed to open for writing file \"name\"\n"); 93 98 goto err_out; 94 99 } ··· 109 104 for (size_t i = 0; i < ARRAY_SIZE(test_items); i++) { 110 105 const struct test_item *item = &test_items[i]; 111 106 112 - file = openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600); 113 - if (!file) { 107 + file = openat(hwmon_dirfd, item->name, O_WRONLY | O_CREAT, 0600); 108 + if (file < 0) { 114 109 pr_err("Failed to open for writing file \"%s\"\n", item->name); 115 110 goto err_out; 116 111 } ··· 124 119 } 125 120 126 121 /* Make the PMU reading the files created above. */ 127 - hwm = perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name); 122 + hwm = perf_pmus__add_test_hwmon_pmu(hwmon_dirfd, "hwmon1234", test_hwmon_name); 128 123 if (!hwm) 129 124 pr_err("Test hwmon creation failed\n"); 130 125 131 126 err_out: 132 127 if (!hwm) { 133 128 test_pmu_put(dir, hwm); 134 - if (dirfd >= 0) 135 - close(dirfd); 129 + if (hwmon_dirfd >= 0) 130 + close(hwmon_dirfd); 136 131 } 132 + if (test_dirfd >= 0) 133 + close(test_dirfd); 137 134 return hwm; 138 135 } 139 136