Skip to contentSkip to conversation

perf hwmon_pmu: Use openat rather than dup to refresh directory

PATCH Applied Ian Rogers 3 participants 6 messages Dec 6, 2024 – Dec 11, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/2 perf hwmon_pmu: Use openat rather than dup to refresh directory

The hwmon PMU test will make a temp directory, open the directory with O_DIRECTORY then fill it with contents. As the open is before the filling the contents the later fdopendir may reflect the initial empty state, meaning no events are seen. Change to re-open the directory, rather than dup the fd, so the latest contents are seen.

Minor tweaks/additions to debug messages.

---
 tools/perf/util/hwmon_pmu.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
tools/perf/util/hwmon_pmu.c +11 -4
index e61429b38ba7..4acb9bb19b84 100644
@@ -258,8 +258,12 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
 	if (pmu->pmu.sysfs_aliases_loaded)
 		return 0;
 
-	/* Use a dup-ed fd as closedir will close it. */
-	dup_fd = dup(pmu->hwmon_dir_fd);
+	/*
+	 * Use a dup-ed fd as closedir will close it. Use openat so that the
+	 * directory contents are refreshed.
+	 */
+	dup_fd = openat(pmu->hwmon_dir_fd, ".", O_DIRECTORY);
+
 	if (dup_fd == -1)
 		return -ENOMEM;
 
@@ -336,6 +340,9 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
 			close(fd);
 		}
 	}
+	if (hashmap__size(&pmu->events) == 0)
+		pr_debug2("hwmon_pmu: %s has no events\n", pmu->pmu.name);
+
 	hashmap__for_each_entry_safe((&pmu->events), cur, tmp, bkt) {
 		union hwmon_pmu_event_key key = {
 			.type_and_num = cur->key,
@@ -343,8 +350,8 @@ static int hwmon_pmu__read_events(struct hwmon_pmu *pmu)
 		struct hwmon_pmu_event_value *value = cur->pvalue;
 
 		if (!test_bit(HWMON_ITEM_INPUT, value->items)) {
-			pr_debug("hwmon_pmu: removing event '%s%d' that has no input file\n",
-				hwmon_type_strs[key.type], key.num);
+			pr_debug("hwmon_pmu: %s removing event '%s%d' that has no input file\n",
+				pmu->pmu.name, hwmon_type_strs[key.type], key.num);
 			hashmap__delete(&pmu->events, key.type_and_num, &key, &value);
 			zfree(&value->label);
 			zfree(&value->name);
Signature · 1 line
2.47.0.338.g60cca15819-goog
author reviewer
#1 lore raw

PATCH 2/2 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.

---
 tools/perf/tests/hwmon_pmu.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
tools/perf/tests/hwmon_pmu.c +18 -11
index f8bcee9660d5..d2b066a2b557 100644
@@ -65,7 +65,7 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 		{ "temp2_label", "test hwmon event2\n", },
 		{ "temp2_input", "50000\n", },
 	};
-	int dirfd, file;
+	int hwmon_dirfd = -1, test_dirfd = -1, file;
 	struct perf_pmu *hwm = NULL;
 	ssize_t len;
 
@@ -76,19 +76,24 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 		dir[0] = '\0';
 		return NULL;
 	}
-	dirfd = open(dir, O_DIRECTORY);
-	if (dirfd < 0) {
+	test_dirfd = open(dir, O_PATH|O_DIRECTORY);
+	if (test_dirfd < 0) {
 		pr_err("Failed to open test directory \"%s\"\n", dir);
 		goto err_out;
 	}
 
 	/* Create the test hwmon directory and give it a name. */
-	if (mkdirat(dirfd, "hwmon1234", 0755) < 0) {
+	if (mkdirat(test_dirfd, "hwmon1234", 0755) < 0) {
 		pr_err("Failed to mkdir hwmon directory\n");
 		goto err_out;
 	}
-	file = openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600);
-	if (!file) {
+	hwmon_dirfd = openat(test_dirfd, "hwmon1234", O_DIRECTORY);
+	if (hwmon_dirfd < 0) {
+		pr_err("Failed to open test hwmon directory \"%s/hwmon1234\"\n", dir);
+		goto err_out;
+	}
+	file = openat(hwmon_dirfd, "name", O_WRONLY | O_CREAT, 0600);
+	if (file < 0) {
 		pr_err("Failed to open for writing file \"name\"\n");
 		goto err_out;
 	}
@@ -104,8 +109,8 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 	for (size_t i = 0; i < ARRAY_SIZE(test_items); i++) {
 		const struct test_item *item = &test_items[i];
 
-		file = openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600);
-		if (!file) {
+		file = openat(hwmon_dirfd, item->name, O_WRONLY | O_CREAT, 0600);
+		if (file < 0) {
 			pr_err("Failed to open for writing file \"%s\"\n", item->name);
 			goto err_out;
 		}
@@ -119,16 +124,18 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 	}
 
 	/* Make the PMU reading the files created above. */
-	hwm = perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name);
+	hwm = perf_pmus__add_test_hwmon_pmu(hwmon_dirfd, "hwmon1234", test_hwmon_name);
 	if (!hwm)
 		pr_err("Test hwmon creation failed\n");
 
 err_out:
 	if (!hwm) {
 		test_pmu_put(dir, hwm);
-		if (dirfd >= 0)
-			close(dirfd);
+		if (hwmon_dirfd >= 0)
+			close(hwmon_dirfd);
 	}
+	if (test_dirfd >= 0)
+		close(test_dirfd);
 	return hwm;
 }
 
Signature · 1 line
2.47.0.338.g60cca15819-goog
maintainer Applied
#2 lore raw

Re: [PATCH v1 2/2] perf test hwmon_pmu: Fix event file location

Quoted 5 lines · Ian Rogers

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.

With these two files the 'perf test 11' for hwmon is successfully completed on my system where I previously reported failures.

Thanks, applied,

- Arnaldo

Quoted 83 lines
---
 tools/perf/tests/hwmon_pmu.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
tools/perf/tests/hwmon_pmu.c +18 -11
index f8bcee9660d5..d2b066a2b557 100644
@@ -65,7 +65,7 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 		{ "temp2_label", "test hwmon event2\n", },
 		{ "temp2_input", "50000\n", },
 	};
-	int dirfd, file;
+	int hwmon_dirfd = -1, test_dirfd = -1, file;
 	struct perf_pmu *hwm = NULL;
 	ssize_t len;
 
@@ -76,19 +76,24 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 		dir[0] = '\0';
 		return NULL;
 	}
-	dirfd = open(dir, O_DIRECTORY);
-	if (dirfd < 0) {
+	test_dirfd = open(dir, O_PATH|O_DIRECTORY);
+	if (test_dirfd < 0) {
 		pr_err("Failed to open test directory \"%s\"\n", dir);
 		goto err_out;
 	}
 
 	/* Create the test hwmon directory and give it a name. */
-	if (mkdirat(dirfd, "hwmon1234", 0755) < 0) {
+	if (mkdirat(test_dirfd, "hwmon1234", 0755) < 0) {
 		pr_err("Failed to mkdir hwmon directory\n");
 		goto err_out;
 	}
-	file = openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600);
-	if (!file) {
+	hwmon_dirfd = openat(test_dirfd, "hwmon1234", O_DIRECTORY);
+	if (hwmon_dirfd < 0) {
+		pr_err("Failed to open test hwmon directory \"%s/hwmon1234\"\n", dir);
+		goto err_out;
+	}
+	file = openat(hwmon_dirfd, "name", O_WRONLY | O_CREAT, 0600);
+	if (file < 0) {
 		pr_err("Failed to open for writing file \"name\"\n");
 		goto err_out;
 	}
@@ -104,8 +109,8 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 	for (size_t i = 0; i < ARRAY_SIZE(test_items); i++) {
 		const struct test_item *item = &test_items[i];
 
-		file = openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600);
-		if (!file) {
+		file = openat(hwmon_dirfd, item->name, O_WRONLY | O_CREAT, 0600);
+		if (file < 0) {
 			pr_err("Failed to open for writing file \"%s\"\n", item->name);
 			goto err_out;
 		}
@@ -119,16 +124,18 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
 	}
 
 	/* Make the PMU reading the files created above. */
-	hwm = perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name);
+	hwm = perf_pmus__add_test_hwmon_pmu(hwmon_dirfd, "hwmon1234", test_hwmon_name);
 	if (!hwm)
 		pr_err("Test hwmon creation failed\n");
 
 err_out:
 	if (!hwm) {
 		test_pmu_put(dir, hwm);
-		if (dirfd >= 0)
-			close(dirfd);
+		if (hwmon_dirfd >= 0)
+			close(hwmon_dirfd);
 	}
+	if (test_dirfd >= 0)
+		close(test_dirfd);
 	return hwm;
 }
 
Signature · 1 line
2.47.0.338.g60cca15819-goog
maintainer
#3 lore raw

Re: [PATCH v1 2/2] perf test hwmon_pmu: Fix event file location

Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers

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.

Quoted 4 lines

With these two files the 'perf test 11' for hwmon is successfully completed on my system where I previously reported failures.

Thanks, applied,

I think it can go to perf-tools.

Thanks,
Namhyung

Quoted 84 lines
Quoted 84 lines
---
 tools/perf/tests/hwmon_pmu.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
tools/perf/tests/hwmon_pmu.c +18 -12
index f8bcee9660d5..d2b066a2b557 100644
@@ -65,7 +65,7 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
              { "temp2_label", "test hwmon event2\n", },
              { "temp2_input", "50000\n", },
      };
-     int dirfd, file;
+     int hwmon_dirfd = -1, test_dirfd = -1, file;
      struct perf_pmu *hwm = NULL;
      ssize_t len;
 
@@ -76,19 +76,24 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
              dir[0] = '\0';
              return NULL;
      }
-     dirfd = open(dir, O_DIRECTORY);
-     if (dirfd < 0) {
+     test_dirfd = open(dir, O_PATH|O_DIRECTORY);
+     if (test_dirfd < 0) {
              pr_err("Failed to open test directory \"%s\"\n", dir);
              goto err_out;
      }
 
      /* Create the test hwmon directory and give it a name. */
-     if (mkdirat(dirfd, "hwmon1234", 0755) < 0) {
+     if (mkdirat(test_dirfd, "hwmon1234", 0755) < 0) {
              pr_err("Failed to mkdir hwmon directory\n");
              goto err_out;
      }
-     file = openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600);
-     if (!file) {
+     hwmon_dirfd = openat(test_dirfd, "hwmon1234", O_DIRECTORY);
+     if (hwmon_dirfd < 0) {
+             pr_err("Failed to open test hwmon directory \"%s/hwmon1234\"\n", dir);
+             goto err_out;
+     }
+     file = openat(hwmon_dirfd, "name", O_WRONLY | O_CREAT, 0600);
+     if (file < 0) {
              pr_err("Failed to open for writing file \"name\"\n");
              goto err_out;
      }
@@ -104,8 +109,8 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
      for (size_t i = 0; i < ARRAY_SIZE(test_items); i++) {
              const struct test_item *item = &test_items[i];
 
-             file = openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600);
-             if (!file) {
+             file = openat(hwmon_dirfd, item->name, O_WRONLY | O_CREAT, 0600);
+             if (file < 0) {
                      pr_err("Failed to open for writing file \"%s\"\n", item->name);
                      goto err_out;
              }
@@ -119,16 +124,18 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
      }
 
      /* Make the PMU reading the files created above. */
-     hwm = perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name);
+     hwm = perf_pmus__add_test_hwmon_pmu(hwmon_dirfd, "hwmon1234", test_hwmon_name);
      if (!hwm)
              pr_err("Test hwmon creation failed\n");
 
 err_out:
      if (!hwm) {
              test_pmu_put(dir, hwm);
-             if (dirfd >= 0)
-                     close(dirfd);
+             if (hwmon_dirfd >= 0)
+                     close(hwmon_dirfd);
      }
+     if (test_dirfd >= 0)
+             close(test_dirfd);
      return hwm;
 }
 
--

2.47.0.338.g60cca15819-goog

maintainer Tested-by
#4 lore raw

Re: [PATCH v1 2/2] perf test hwmon_pmu: Fix event file location

Quoted 9 lines · Namhyung Kim
Quoted 8 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers

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.

Quoted 6 lines
Quoted 4 lines

With these two files the 'perf test 11' for hwmon is successfully completed on my system where I previously reported failures.

Thanks, applied,

I think it can go to perf-tools.

Please do so and also add:

- Arnaldo

Quoted 87 lines

Thanks,
Namhyung

Quoted 84 lines
Quoted 84 lines
---
 tools/perf/tests/hwmon_pmu.c | 29 ++++++++++++++++++-----------
 1 file changed, 18 insertions(+), 11 deletions(-)
tools/perf/tests/hwmon_pmu.c +18 -12
index f8bcee9660d5..d2b066a2b557 100644
@@ -65,7 +65,7 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
              { "temp2_label", "test hwmon event2\n", },
              { "temp2_input", "50000\n", },
      };
-     int dirfd, file;
+     int hwmon_dirfd = -1, test_dirfd = -1, file;
      struct perf_pmu *hwm = NULL;
      ssize_t len;
 
@@ -76,19 +76,24 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
              dir[0] = '\0';
              return NULL;
      }
-     dirfd = open(dir, O_DIRECTORY);
-     if (dirfd < 0) {
+     test_dirfd = open(dir, O_PATH|O_DIRECTORY);
+     if (test_dirfd < 0) {
              pr_err("Failed to open test directory \"%s\"\n", dir);
              goto err_out;
      }
 
      /* Create the test hwmon directory and give it a name. */
-     if (mkdirat(dirfd, "hwmon1234", 0755) < 0) {
+     if (mkdirat(test_dirfd, "hwmon1234", 0755) < 0) {
              pr_err("Failed to mkdir hwmon directory\n");
              goto err_out;
      }
-     file = openat(dirfd, "hwmon1234/name", O_WRONLY | O_CREAT, 0600);
-     if (!file) {
+     hwmon_dirfd = openat(test_dirfd, "hwmon1234", O_DIRECTORY);
+     if (hwmon_dirfd < 0) {
+             pr_err("Failed to open test hwmon directory \"%s/hwmon1234\"\n", dir);
+             goto err_out;
+     }
+     file = openat(hwmon_dirfd, "name", O_WRONLY | O_CREAT, 0600);
+     if (file < 0) {
              pr_err("Failed to open for writing file \"name\"\n");
              goto err_out;
      }
@@ -104,8 +109,8 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
      for (size_t i = 0; i < ARRAY_SIZE(test_items); i++) {
              const struct test_item *item = &test_items[i];
 
-             file = openat(dirfd, item->name, O_WRONLY | O_CREAT, 0600);
-             if (!file) {
+             file = openat(hwmon_dirfd, item->name, O_WRONLY | O_CREAT, 0600);
+             if (file < 0) {
                      pr_err("Failed to open for writing file \"%s\"\n", item->name);
                      goto err_out;
              }
@@ -119,16 +124,18 @@ static struct perf_pmu *test_pmu_get(char *dir, size_t sz)
      }
 
      /* Make the PMU reading the files created above. */
-     hwm = perf_pmus__add_test_hwmon_pmu(dirfd, "hwmon1234", test_hwmon_name);
+     hwm = perf_pmus__add_test_hwmon_pmu(hwmon_dirfd, "hwmon1234", test_hwmon_name);
      if (!hwm)
              pr_err("Test hwmon creation failed\n");
 
 err_out:
      if (!hwm) {
              test_pmu_put(dir, hwm);
-             if (dirfd >= 0)
-                     close(dirfd);
+             if (hwmon_dirfd >= 0)
+                     close(hwmon_dirfd);
      }
+     if (test_dirfd >= 0)
+             close(test_dirfd);
      return hwm;
 }
 
--

2.47.0.338.g60cca15819-goog

maintainer Applied
#5 lore raw
In reply to Ian Rogers
Quoted 9 lines · Ian Rogers

The hwmon PMU test will make a temp directory, open the directory with O_DIRECTORY then fill it with contents. As the open is before the filling the contents the later fdopendir may reflect the initial empty state, meaning no events are seen. Change to re-open the directory, rather than dup the fd, so the latest contents are seen.

Minor tweaks/additions to debug messages.

[...]

Applied to perf-tools, thanks!

Best regards,
Namhyung