Skip to contentSkip to conversation

perf bpf_counter: Say why the perf_event_attr map can't be used

PATCH Applied Hang Zhengyang 3 participants 3 messages Nov 20 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf bpf_counter: Say why the perf_event_attr map can't be used

When 'perf stat --bpf-counters' can't set up the pinned map it uses to share counters, all it prints is:

Failed to lock perf_event_attr map

Arnaldo asked for better wording when bperf was added. The map can fail to be created, pinned, opened, checked or locked, and the message says neither which step failed nor why.

Print the path and the error at each step, keeping the pin error across the "pinned in parallel" retry. When the pin fails because the directory isn't on bpffs, say so, as libbpf's check_path() does. The caller's message is now redundant, so remove it.

As a normal user:

Failed to create perf_event_attr map: Operation not permitted

As root, with bpffs not mounted:

Failed to pin perf_event_attr map at /sys/fs/bpf/perf_attr_map: No such file or directory bpffs is not mounted at /sys/fs/bpf, try 'mount -t bpf bpf /sys/fs/bpf' or use --bpf-attr-map

---
Tested on perf-tools-next (bb27ddbaee56): each failure path as root in a KVM guest and as a normal user, 8 concurrent runs with bpffs mounted (no new output), and the bperf perf tests (no change).

The patch, this text and the tests were written and run with Claude Code; I reproduced the issue and reviewed the change.

---
 tools/perf/util/bpf_counter.c | 45 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 39 insertions(+), 6 deletions(-)
tools/perf/util/bpf_counter.c +39 -6
index 9362e45e17ce..9a463eb9d51d 100644
@@ -3,11 +3,13 @@
 /* Copyright (c) 2019 Facebook */
 
 #include <assert.h>
+#include <libgen.h>
 #include <limits.h>
 #include <unistd.h>
 #include <sys/file.h>
 #include <sys/resource.h>
 #include <sys/time.h>
+#include <sys/vfs.h>
 #include <linux/err.h>
 #include <linux/list.h>
 #include <linux/zalloc.h>
@@ -32,6 +34,10 @@
 #include "bpf_skel/bperf_leader.skel.h"
 #include "bpf_skel/bperf_follower.skel.h"
 
+#ifndef BPF_FS_MAGIC
+#define BPF_FS_MAGIC 0xcafe4a11
+#endif
+
 struct bpf_counter {
 	void *skel;
 	struct list_head list;
@@ -376,6 +382,24 @@ static bool bperf_attr_map_compatible(int attr_map_fd)
 		(map_info.value_size == sizeof(struct perf_event_attr_map_entry));
 }
 
+/* bpf_obj_pin() only works on bpffs: say so if that is why it failed. */
+static void bperf_pin_hint(struct target *target, const char *path)
+{
+	char buf[PATH_MAX];
+	struct statfs st;
+	const char *dir;
+
+	scnprintf(buf, sizeof(buf), "%s", path);
+	dir = dirname(buf);
+	if (statfs(dir, &st) || st.f_type == BPF_FS_MAGIC)
+		return;
+	if (target->attr_map)
+		pr_err("--bpf-attr-map: %s is not on bpffs\n", dir);
+	else
+		pr_err("bpffs is not mounted at %s, try 'mount -t bpf bpf %s' or use --bpf-attr-map\n",
+		       dir, dir);
+}
+
 static int bperf_lock_attr_map(struct target *target)
 {
 	char path[PATH_MAX];
@@ -393,30 +417,41 @@ static int bperf_lock_attr_map(struct target *target)
 					sizeof(struct perf_event_attr),
 					sizeof(struct perf_event_attr_map_entry),
 					ATTR_MAP_SIZE, NULL);
-		if (map_fd < 0)
+		if (map_fd < 0) {
+			pr_err("Failed to create perf_event_attr map: %m\n");
 			return -1;
+		}
 
 		err = bpf_obj_pin(map_fd, path);
 		if (err) {
 			/* someone pinned the map in parallel? */
 			close(map_fd);
 			map_fd = bpf_obj_get(path);
-			if (map_fd < 0)
+			if (map_fd < 0) {
+				/* report the pin error, not the bpf_obj_get() one */
+				errno = -err;
+				pr_err("Failed to pin perf_event_attr map at %s: %m\n", path);
+				bperf_pin_hint(target, path);
 				return -1;
+			}
 		}
 	} else {
 		map_fd = bpf_obj_get(path);
-		if (map_fd < 0)
+		if (map_fd < 0) {
+			pr_err("Failed to open perf_event_attr map at %s: %m\n", path);
 			return -1;
+		}
 	}
 
 	if (!bperf_attr_map_compatible(map_fd)) {
+		pr_err("%s is not a perf_event_attr map this perf can use\n", path);
 		close(map_fd);
 		return -1;
 
 	}
 	err = flock(map_fd, LOCK_EX);
 	if (err) {
+		pr_err("Failed to lock %s: %m\n", path);
 		close(map_fd);
 		return -1;
 	}
@@ -550,10 +585,8 @@ static int bperf__load(struct evsel *evsel, struct target *target)
 	 * map.
 	 */
 	attr_map_fd = bperf_lock_attr_map(target);
-	if (attr_map_fd < 0) {
-		pr_err("Failed to lock perf_event_attr map\n");
+	if (attr_map_fd < 0)
 		return -1;
-	}
 
 	err = bpf_map_lookup_elem(attr_map_fd, &evsel->core.attr, &entry);
 	if (err) {

---
base-commit: bb27ddbaee56536fb8df0a24f07b526766dfe131
change-id: 20261120-bperf-attr-map-err-ec624e923718

Best regards,

Signature · 1 line
Hang Zhengyang <[email protected]>
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] perf bpf_counter: -Werror=sign-compare in bperf_pin_hint() on 32-bit glibc - [Low] perf…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] perf bpf_counter: -Werror=sign-compare in bperf_pin_hint() on 32-bit glibc
- [Low] perf bpf_counter: missing errno.h include for errno in bperf_lock_attr_map()
--

commit ef2748ac810907c5c84a28e7a31cf730bd68ad09
Author: Hang Zhengyang <[email protected]>

perf bpf_counter: Say why the perf_event_attr map can't be used

This commit reports specific errors and a bpffs mount hint when creating, pinning, opening, checking, or locking the bperf perf_event_attr map fails.

Quoted 34 lines
tools/perf/util/bpf_counter.c +7 -0
index 9362e45e17ceb..9a463eb9d51d9 100644
@@ -3,11 +3,13 @@
 /* Copyright (c) 2019 Facebook */
 
 #include <assert.h>
+#include <libgen.h>
 #include <limits.h>
 #include <unistd.h>
 #include <sys/file.h>
 #include <sys/resource.h>
 #include <sys/time.h>
+#include <sys/vfs.h>
 #include <linux/err.h>
 #include <linux/list.h>
 #include <linux/zalloc.h>
@@ -32,6 +34,10 @@
 #include "bpf_skel/bperf_leader.skel.h"
 #include "bpf_skel/bperf_follower.skel.h"
 
+#ifndef BPF_FS_MAGIC
+#define BPF_FS_MAGIC 0xcafe4a11
+#endif
+
 struct bpf_counter {
 	void *skel;
 	struct list_head list;
@@ -376,6 +382,24 @@ static bool bperf_attr_map_compatible(int attr_map_fd)
 		(map_info.value_size == sizeof(struct perf_event_attr_map_entry));
 }
 
+/* bpf_obj_pin() only works on bpffs: say so if that is why it failed. */

tools/perf/util/bpf_counter.c static bool bperf_attr_map_compatible(int attr_map_fd) · line 395

Quoted 10 lines
tools/perf/util/bpf_counter.c +10 -0
+static void bperf_pin_hint(struct target *target, const char *path)
+{
+	char buf[PATH_MAX];
+	struct statfs st;
+	const char *dir;
+
+	scnprintf(buf, sizeof(buf), "%s", path);
+	dir = dirname(buf);
+	if (statfs(dir, &st) || st.f_type == BPF_FS_MAGIC)
+		return;

[Severity: Medium]
Does comparing st.f_type against BPF_FS_MAGIC without a cast fail to build with -Werror=sign-compare on 32-bit glibc?

0xcafe4a11 exceeds INT_MAX, so BPF_FS_MAGIC has type unsigned int, while struct statfs f_type (__fsword_t) is a 32-bit signed integer on 32-bit glibc targets. Casting st.f_type to unsigned long, as is_bpffs() in tools/bpf/bpftool/common.c does, avoids the warning.

Quoted 19 lines
tools/perf/util/bpf_counter.c +10 -1
+	if (target->attr_map)
+		pr_err("--bpf-attr-map: %s is not on bpffs\n", dir);
+	else
+		pr_err("bpffs is not mounted at %s, try 'mount -t bpf bpf %s' or use --bpf-attr-map\n",
+		       dir, dir);
+}
+
 static int bperf_lock_attr_map(struct target *target)
 {
 	char path[PATH_MAX];
@@ -393,30 +417,41 @@ static int bperf_lock_attr_map(struct target *target)
 					sizeof(struct perf_event_attr),
 					sizeof(struct perf_event_attr_map_entry),
 					ATTR_MAP_SIZE, NULL);
-		if (map_fd < 0)
+		if (map_fd < 0) {
+			pr_err("Failed to create perf_event_attr map: %m\n");
 			return -1;
+		}

tools/perf/util/bpf_counter.c static int bperf_lock_attr_map(struct target *target) · line 432

Quoted 10 lines
tools/perf/util/bpf_counter.c +3 -1
 
 		err = bpf_obj_pin(map_fd, path);
 		if (err) {
 			/* someone pinned the map in parallel? */
 			close(map_fd);
 			map_fd = bpf_obj_get(path);
-			if (map_fd < 0)
+			if (map_fd < 0) {
+				/* report the pin error, not the bpf_obj_get() one */
+				errno = -err;

[Severity: Low]
This isn't a bug right now because the generated BPF skeleton headers pull it in transitively, but tools/perf/util/bpf_counter.c should explicitly include <errno.h> at the top of the file for errno.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw
In reply to Hang Zhengyang
Quoted 10 lines · Hang Zhengyang

When 'perf stat --bpf-counters' can't set up the pinned map it uses to share counters, all it prints is:

Failed to lock perf_event_attr map

Arnaldo asked for better wording when bperf was added. The map can fail to be created, pinned, opened, checked or locked, and the message says neither which step failed nor why.

[...]

Applied to perf-tools-next, thanks!

Best regards
Namhyung