* [PATCH] perf bpf_counter: Say why the perf_event_attr map can't be used
@ 2026-11-20 6:49 Hang Zhengyang via B4 Relay
2026-11-20 7:02 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Hang Zhengyang via B4 Relay @ 2026-11-20 6:49 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, James Clark
Cc: Song Liu, linux-perf-users, linux-kernel, bpf, Hang Zhengyang
From: Hang Zhengyang <hang.zhengyang1010@gmail.com>
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
Link: https://lore.kernel.org/all/YFneUHDiAt8Rg0cc@kernel.org/
Signed-off-by: Hang Zhengyang <hang.zhengyang1010@gmail.com>
---
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(-)
diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c
index 9362e45e17ce..9a463eb9d51d 100644
--- a/tools/perf/util/bpf_counter.c
+++ b/tools/perf/util/bpf_counter.c
@@ -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,
--
Hang Zhengyang <hang.zhengyang1010@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] perf bpf_counter: Say why the perf_event_attr map can't be used
2026-11-20 6:49 [PATCH] perf bpf_counter: Say why the perf_event_attr map can't be used Hang Zhengyang via B4 Relay
@ 2026-11-20 7:02 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20 7:02 UTC (permalink / raw)
To: Hang Zhengyang; +Cc: linux-perf-users, bpf
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 <hang.zhengyang1010@gmail.com>
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.
Link: https://lore.kernel.org/all/YFneUHDiAt8Rg0cc@kernel.org/
> diff --git a/tools/perf/util/bpf_counter.c b/tools/perf/util/bpf_counter.c
> index 9362e45e17ceb..9a463eb9d51d9 100644
> --- a/tools/perf/util/bpf_counter.c
> +++ b/tools/perf/util/bpf_counter.c
> @@ -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;
[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.
> + 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;
[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.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261120-bperf-attr-map-err-v1-1-b12b6b550273@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-20 7:02 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 6:49 [PATCH] perf bpf_counter: Say why the perf_event_attr map can't be used Hang Zhengyang via B4 Relay
2026-11-20 7:02 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).