Re: [PATCH v1 3/4] perf off-cpu: Fix cgroup_filter map sizing
From: James Clark
Date: Wed Oct 28 2026 - 05:36:29 EST
On 27/10/2026 17:04, Ian Rogers wrote:
off_cpu_prepare() assumed that if evlist__first(evlist)->cgrp is set,
the number of cgroups is evlist__nr_entries(evlist) - 1 (excluding the
dummy bpf-output event). This can under-size cgroup_filter when
non-cgroup events precede cgroup events or when no dummy bpf-output
event is added.
Count the number of evsels in evlist that have a cgroup set and use that
count to size and populate cgroup_filter.
Fixes: 685439a7a037 ("perf record: Add cgroup support for off-cpu profiling")
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/bpf_off_cpu.c | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/bpf_off_cpu.c b/tools/perf/util/bpf_off_cpu.c
index 16948761cc45..71933a71c8d1 100644
--- a/tools/perf/util/bpf_off_cpu.c
+++ b/tools/perf/util/bpf_off_cpu.c
@@ -151,9 +151,10 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target,
struct record_opts *opts)
{
int err, fd, i;
- int ncpus = 1, ntasks = 1, ncgrps = 1;
+ int ncpus = 1, ntasks = 1, ncgrps = 0;
struct strlist *pid_slist = NULL;
struct str_node *pos;
+ struct evsel *evsel;
if (off_cpu_config(evlist) < 0) {
pr_err("Failed to config off-cpu BPF event\n");
@@ -208,8 +209,12 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target,
skel->rodata->uses_tgid = 1;
}
- if (evlist__first(evlist)->cgrp) {
- ncgrps = evlist__nr_entries(evlist) - 1; /* excluding a dummy */
+ evlist__for_each_entry(evlist, evsel) {
+ if (evsel->cgrp)
+ ncgrps++;
+ }
+
+ if (ncgrps > 0) {
bpf_map__set_max_entries(skel->maps.cgroup_filter, ncgrps);
if (!cgroup_is_v2("perf_event"))
@@ -274,8 +279,7 @@ int off_cpu_prepare(struct evlist *evlist, struct target *target,
}
}
- if (evlist__first(evlist)->cgrp) {
- struct evsel *evsel;
+ if (ncgrps > 0) {
u8 val = 1;
fd = bpf_map__fd(skel->maps.cgroup_filter);
Reviewed-by: James Clark <james.clark@xxxxxxxxxx>