Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH] perf evsel: Skip group members without fds when walking a group
@ 2026-10-23  8:56 TAKANO Mitsuhiro
  2026-10-23  9:20 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: TAKANO Mitsuhiro @ 2026-10-23  8:56 UTC (permalink / raw)
  To: linux-perf-users, Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Ian Rogers, TAKANO Mitsuhiro

A group led by a userspace PMU event and containing kernel events is
opened with group_fd = -1 for the members, because the kernel cannot
group a userspace PMU with a kernel one.  The members are therefore
enabled from userspace when the leader is enabled, but their fd array is
not set up in that case, so perf_evsel__enable_cpu() dereferences a NULL
pointer:

  $ perf stat -a -e '{user_time,cycles}' -- sleep 0.05
  Segmentation fault

  $ perf stat -a -e '{system_time,cycles}' -- sleep 0.05
  Segmentation fault

Only the leaders of such a group are opened with an fd in this path.
Skip the members that have no fd, the same way __evlist__enable() skips
leaders without one, in the four loops that walk the members of a group.

Verified on an Ampere Altra (arm64, Neoverse-N1) with a perf built from
this tree: the two commands above segfault before the change and print
user_time/system_time after it, while plain groups such as
'{cycles,instructions}', 'perf stat' with no event and '-r 2' are
unchanged.

Fixes: 0b18bced5444 ("perf tool_pmu: Make tool PMU events respect enable/disable")
Assisted-by: LLM
Signed-off-by: TAKANO Mitsuhiro <takano32@gmail.com>
---
 tools/perf/util/evsel.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index d4cb455f4a7d..32c54cd4acf0 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1882,6 +1882,8 @@ int evsel__enable_cpu(struct evsel *evsel, int cpu_map_idx)
 		struct evsel *member;
 
 		for_each_group_member(member, evsel) {
+			if (!member->core.fd)
+				continue;
 			if (evsel__is_non_perf_event_open_pmu(evsel) ||
 			    evsel__is_non_perf_event_open_pmu(member)) {
 				/*
@@ -1917,6 +1919,8 @@ int evsel__enable(struct evsel *evsel)
 		struct evsel *member;
 
 		for_each_group_member(member, evsel) {
+			if (!member->core.fd)
+				continue;
 			if (evsel__is_non_perf_event_open_pmu(evsel) ||
 			    evsel__is_non_perf_event_open_pmu(member)) {
 				/*
@@ -1952,6 +1956,8 @@ int evsel__disable_cpu(struct evsel *evsel, int cpu_map_idx)
 		struct evsel *member;
 
 		for_each_group_member(member, evsel) {
+			if (!member->core.fd)
+				continue;
 			if (evsel__is_non_perf_event_open_pmu(evsel) ||
 			    evsel__is_non_perf_event_open_pmu(member)) {
 				/*
@@ -1992,6 +1998,8 @@ int evsel__disable(struct evsel *evsel)
 		struct evsel *member;
 
 		for_each_group_member(member, evsel) {
+			if (!member->core.fd)
+				continue;
 			if (evsel__is_non_perf_event_open_pmu(evsel) ||
 			    evsel__is_non_perf_event_open_pmu(member)) {
 				/*

base-commit: f47d223627f80cb7a1eb14c585fe6cb35de5e984
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-10-23 16:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-23  8:56 [PATCH] perf evsel: Skip group members without fds when walking a group TAKANO Mitsuhiro
2026-10-23  9:20 ` sashiko-bot
2026-10-23 16:24   ` TAKANO Mitsuhiro

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox