Skip to contentSkip to conversation

perf bpf_counter: Fix opening of "any"(-1) CPU events

PATCH Superseded Ian Rogers 2 participants 3 messages Oct 7, 2025 – Oct 8, 2025 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf bpf_counter: Fix opening of "any"(-1) CPU events

The bperf BPF counter code doesn't handle "any"(-1) CPU events, always wanting to aggregate a count against a CPU, which avoids the need for atomics. Force evsels used for BPF counters to require a CPU when not in system-wide mode so that the "any"(-1) value isn't used during map propagation and evsel's CPU map matches that of the PMU.

Fixes b91917c0c6fa ("perf bpf_counter: Fix handling of cpumap fixing hybrid")
---
 tools/perf/builtin-stat.c     | 12 ++++++++++++
 tools/perf/util/bpf_counter.c |  1 +
 2 files changed, 13 insertions(+)
tools/perf/builtin-stat.c +12 -0
index 7006f848f87a..7fdc7f273a48 100644
@@ -2797,6 +2797,18 @@ int cmd_stat(int argc, const char **argv)
 
 	evlist__warn_user_requested_cpus(evsel_list, target.cpu_list);
 
+	if (target.use_bpf && !target.system_wide) {
+		/*
+		 * Setup BPF counters to require CPUs as any(-1) isn't
+		 * supported. evlist__create_maps below will propagate this
+		 * information to the evsels.
+		 */
+		struct evsel *counter;
+
+		evlist__for_each_entry(evsel_list, counter)
+			counter->core.requires_cpu = true;
+	}
+
 	if (evlist__create_maps(evsel_list, &target) < 0) {
 		if (target__has_task(&target)) {
 			pr_err("Problems finding threads of monitor\n");
tools/perf/util/bpf_counter.c +1 -0
index ca5d01b9017d..d3e5933b171b 100644
@@ -495,6 +495,7 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd,
 	 * following evsel__open_per_cpu call
 	 */
 	evsel->leader_skel = skel;
+	assert(!perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus));
 	evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
 
 out:
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
maintainer
#1 lore raw

Hi Ian,

Quoted 18 lines · Ian Rogers

The bperf BPF counter code doesn't handle "any"(-1) CPU events, always wanting to aggregate a count against a CPU, which avoids the need for atomics. Force evsels used for BPF counters to require a CPU when not in system-wide mode so that the "any"(-1) value isn't used during map propagation and evsel's CPU map matches that of the PMU.

Fixes b91917c0c6fa ("perf bpf_counter: Fix handling of cpumap fixing hybrid")
---
 tools/perf/builtin-stat.c     | 12 ++++++++++++
 tools/perf/util/bpf_counter.c |  1 +
 2 files changed, 13 insertions(+)
tools/perf/builtin-stat.c +0 -0
index 7006f848f87a..7fdc7f273a48 100644

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 2800

Quoted 4 lines
tools/perf/builtin-stat.c +1 -0
@@ -2797,6 +2797,18 @@ int cmd_stat(int argc, const char **argv)
 
 	evlist__warn_user_requested_cpus(evsel_list, target.cpu_list);
 
+	if (target.use_bpf && !target.system_wide) {

The target.use_bpf only checks the --bpf-counters option. But IIUC it's possible to use BPF only for selected events with ':b' modifier. I think you need to check each evsel with evsel__is_bperf().

Also system_wide might not be set for -C/--cpu option. Probably you want target__has_cpu() instead of target.system_wide.

Thanks,
Namhyung

Quoted 29 lines
tools/perf/builtin-stat.c +11 -0
+		/*
+		 * Setup BPF counters to require CPUs as any(-1) isn't
+		 * supported. evlist__create_maps below will propagate this
+		 * information to the evsels.
+		 */
+		struct evsel *counter;
+
+		evlist__for_each_entry(evsel_list, counter)
+			counter->core.requires_cpu = true;
+	}
+
 	if (evlist__create_maps(evsel_list, &target) < 0) {
 		if (target__has_task(&target)) {
 			pr_err("Problems finding threads of monitor\n");
tools/perf/util/bpf_counter.c +1 -0
index ca5d01b9017d..d3e5933b171b 100644
@@ -495,6 +495,7 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd,
 	 * following evsel__open_per_cpu call
 	 */
 	evsel->leader_skel = skel;
+	assert(!perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus));
 	evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
 
 out:
Signature · 1 line
2.51.0.710.ga91ca5db03-goog
author reviewer
#2 lore raw
Quoted 25 lines · Namhyung Kim

Hi Ian,

Quoted 21 lines · Ian Rogers

The bperf BPF counter code doesn't handle "any"(-1) CPU events, always wanting to aggregate a count against a CPU, which avoids the need for atomics. Force evsels used for BPF counters to require a CPU when not in system-wide mode so that the "any"(-1) value isn't used during map propagation and evsel's CPU map matches that of the PMU.

Fixes b91917c0c6fa ("perf bpf_counter: Fix handling of cpumap fixing hybrid")
---
 tools/perf/builtin-stat.c     | 12 ++++++++++++
 tools/perf/util/bpf_counter.c |  1 +
 2 files changed, 13 insertions(+)
tools/perf/builtin-stat.c +0 -0
index 7006f848f87a..7fdc7f273a48 100644
@@ -2797,6 +2797,18 @@ int cmd_stat(int argc, const char **argv)
 
      evlist__warn_user_requested_cpus(evsel_list, target.cpu_list);
 
Quoted 8 lines

tools/perf/builtin-stat.c int cmd_stat(int argc, const char **argv) · line 2800

Quoted 1 line
tools/perf/builtin-stat.c +1 -0
+     if (target.use_bpf && !target.system_wide) {

The target.use_bpf only checks the --bpf-counters option. But IIUC it's possible to use BPF only for selected events with ':b' modifier. I think you need to check each evsel with evsel__is_bperf().

Also system_wide might not be set for -C/--cpu option. Probably you want target__has_cpu() instead of target.system_wide.

Thanks Namhyung, I'll fix in v2.

Ian

Quoted 33 lines

Thanks,
Namhyung

Quoted 29 lines
tools/perf/builtin-stat.c +11 -0
+             /*
+              * Setup BPF counters to require CPUs as any(-1) isn't
+              * supported. evlist__create_maps below will propagate this
+              * information to the evsels.
+              */
+             struct evsel *counter;
+
+             evlist__for_each_entry(evsel_list, counter)
+                     counter->core.requires_cpu = true;
+     }
+
      if (evlist__create_maps(evsel_list, &target) < 0) {
              if (target__has_task(&target)) {
                      pr_err("Problems finding threads of monitor\n");
tools/perf/util/bpf_counter.c +1 -1
index ca5d01b9017d..d3e5933b171b 100644
@@ -495,6 +495,7 @@ static int bperf_reload_leader_program(struct evsel *evsel, int attr_map_fd,
       * following evsel__open_per_cpu call
       */
      evsel->leader_skel = skel;
+     assert(!perf_cpu_map__has_any_cpu_or_is_empty(evsel->core.cpus));
      evsel__open(evsel, evsel->core.cpus, evsel->core.threads);
 
 out:
--

2.51.0.710.ga91ca5db03-goog