* [PATCH] perf evlist: Reparent first_wildcard_match when evsels are removed
@ 2026-12-04 1:18 Ian Rogers
2026-12-04 1:36 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Ian Rogers @ 2026-12-04 1:18 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim
Cc: Jiri Olsa, Adrian Hunter, James Clark, linux-perf-users,
linux-kernel, Ian Rogers
When a wildcard event expands across multiple PMUs (such as hybrid core
PMUs cpu_atom/cpu_core or multiple uncore PMU instances), the later
evsels point their first_wildcard_match field to the first matched
evsel. During stat aggregation and display, evsel__merge_aliases() merges
aliases into their first_wildcard_match and print_counter_aggrdata()
skips any evsel with a non-NULL first_wildcard_match (for uncore events
and hybrid events under --hybrid-merge).
If that first matched evsel is subsequently removed from the evlist --
either by perf_evlist__set_maps() / __perf_evlist__propagate_maps() when
its CPU map intersection with -C is empty, or via evlist__remove() when
opening fails -- the remaining aliases keep a stale first_wildcard_match
pointing to the removed evsel. As a result, they are neither merged nor
displayed. For example, on a hybrid x86 machine (cpu_core on CPUs 0-15,
cpu_atom on CPUs 16-27):
$ perf stat --hybrid-merge -C 0 -e 'cycles,duration_time' -- true
cpu_atom/cycles/ is removed due to an empty CPU intersection with -C 0,
while cpu_core/cycles/ keeps first_wildcard_match pointing to the
removed cpu_atom/cycles/ and is silently omitted from the output.
Add evlist__reparent_wildcard_match() to promote the first remaining
alias in the evlist to be the new primary (first_wildcard_match = NULL)
and reparent any subsequent aliases to it without dereferencing the
removed evsel pointer. Call it from evlist__remove() and via
evlist__fix_removed_wildcard_matches() (checking live list membership by
pointer equality via evlist__contains()) after perf_evlist__set_maps() in
evlist__create_maps() and evlist__create_syswide_maps().
Add unit tests in tools/perf/tests/hybrid-merge.c covering both direct
evlist__remove() and empty-CPU map propagation via evlist__create_maps().
Fixes: 137359b7895f ("perf parse-events: Use wildcard processing to set an event to merge into")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/tests/hybrid-merge.c | 96 +++++++++++++++++++++++++++++++++
tools/perf/util/evlist.c | 40 ++++++++++++++
2 files changed, 136 insertions(+)
diff --git a/tools/perf/tests/hybrid-merge.c b/tools/perf/tests/hybrid-merge.c
index 426b1d09618e..7a25eb8f34ee 100644
--- a/tools/perf/tests/hybrid-merge.c
+++ b/tools/perf/tests/hybrid-merge.c
@@ -2,10 +2,12 @@
#include <stdlib.h>
#include <string.h>
#include <linux/perf_event.h>
+#include <perf/cpumap.h>
#include "debug.h"
#include "env.h"
#include "evlist.h"
#include "evsel.h"
+#include "target.h"
#include "tests.h"
/* A hybrid machine with 2 core PMUs, as read from a perf.data file. */
@@ -193,10 +195,104 @@ static int test__hybrid_merge_unmergeable(struct test_suite *test __maybe_unused
return ret;
}
+static int check_reparent_wildcard_remove(struct evlist *evlist)
+{
+ struct evsel *atom_cycles, *core_cycles, *lowpower_cycles;
+
+ atom_cycles = test_evsel__new(evlist, "cpu_atom/cycles/");
+ core_cycles = test_evsel__new(evlist, "cpu_core/cycles/");
+ lowpower_cycles = test_evsel__new(evlist, "cpu_lowpower/cycles/");
+ TEST_ASSERT_VAL("failed to allocate evsels",
+ atom_cycles && core_cycles && lowpower_cycles);
+
+ core_cycles->first_wildcard_match = atom_cycles;
+ lowpower_cycles->first_wildcard_match = atom_cycles;
+
+ evlist__remove(evlist, atom_cycles);
+ evsel__put(atom_cycles);
+ TEST_ASSERT_VAL("second PMU not promoted to first wildcard match",
+ !core_cycles->first_wildcard_match);
+ TEST_ASSERT_VAL("third PMU not reparented to second PMU",
+ lowpower_cycles->first_wildcard_match == core_cycles);
+
+ evlist__remove(evlist, core_cycles);
+ evsel__put(core_cycles);
+ TEST_ASSERT_VAL("third PMU not promoted after second removal",
+ !lowpower_cycles->first_wildcard_match);
+
+ return TEST_OK;
+}
+
+static int check_reparent_wildcard_create_maps(struct evlist *evlist)
+{
+ struct evsel *atom_cycles, *core_cycles, *lowpower_cycles;
+ struct target target = {
+ .cpu_list = "0",
+ };
+
+ atom_cycles = test_evsel__new(evlist, "cpu_atom/cycles/");
+ core_cycles = test_evsel__new(evlist, "cpu_core/cycles/");
+ lowpower_cycles = test_evsel__new(evlist, "cpu_lowpower/cycles/");
+ TEST_ASSERT_VAL("failed to allocate evsels",
+ atom_cycles && core_cycles && lowpower_cycles);
+
+ perf_cpu_map__put(atom_cycles->core.pmu_cpus);
+ perf_cpu_map__put(core_cycles->core.pmu_cpus);
+ perf_cpu_map__put(lowpower_cycles->core.pmu_cpus);
+ atom_cycles->core.pmu_cpus = perf_cpu_map__new("1");
+ core_cycles->core.pmu_cpus = perf_cpu_map__new("0");
+ lowpower_cycles->core.pmu_cpus = perf_cpu_map__new("0");
+ TEST_ASSERT_VAL("failed to allocate cpu maps",
+ atom_cycles->core.pmu_cpus &&
+ core_cycles->core.pmu_cpus &&
+ lowpower_cycles->core.pmu_cpus);
+
+ core_cycles->first_wildcard_match = atom_cycles;
+ lowpower_cycles->first_wildcard_match = atom_cycles;
+
+ TEST_ASSERT_VAL("evlist__create_maps failed",
+ evlist__create_maps(evlist, &target) == 0);
+ TEST_ASSERT_VAL("empty-CPU evsel not removed",
+ evlist__nr_entries(evlist) == 2 &&
+ list_empty(&atom_cycles->core.node));
+ evlist__put(atom_cycles->evlist);
+ atom_cycles->evlist = NULL;
+ evsel__put(atom_cycles);
+
+ TEST_ASSERT_VAL("second PMU not promoted after map propagation",
+ !core_cycles->first_wildcard_match);
+ TEST_ASSERT_VAL("third PMU not reparented after map propagation",
+ lowpower_cycles->first_wildcard_match == core_cycles);
+ return TEST_OK;
+}
+
+static int test__hybrid_merge_reparent_removed(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ struct evlist *evlist = evlist__new();
+ int ret;
+
+ TEST_ASSERT_VAL("failed to allocate evlist", evlist);
+
+ ret = check_reparent_wildcard_remove(evlist);
+ evlist__put(evlist);
+ if (ret != TEST_OK)
+ return ret;
+
+ evlist = evlist__new();
+ TEST_ASSERT_VAL("failed to allocate evlist", evlist);
+
+ ret = check_reparent_wildcard_create_maps(evlist);
+ evlist__put(evlist);
+ return ret;
+}
+
static struct test_case tests__hybrid_merge[] = {
TEST_CASE("Merge events of 2 core PMUs", hybrid_merge_events),
TEST_CASE("Merge events of 3 core PMUs", hybrid_merge_3_core_pmus),
TEST_CASE("Events that shouldn't merge", hybrid_merge_unmergeable),
+ TEST_CASE("Reparent first_wildcard_match on evsel removal",
+ hybrid_merge_reparent_removed),
{ .name = NULL, }
};
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index c2402e4791b6..4b8cba6410c4 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -458,9 +458,47 @@ void evlist__add(struct evlist *evlist, struct evsel *entry)
evlist__set_id_pos(evlist);
}
+static bool evlist__contains(struct evlist *evlist, const struct evsel *evsel)
+{
+ struct evsel *pos;
+
+ evlist__for_each_entry(evlist, pos) {
+ if (pos == evsel)
+ return true;
+ }
+ return false;
+}
+
+static void evlist__reparent_wildcard_match(struct evlist *evlist,
+ const struct evsel *removed)
+{
+ struct evsel *pos, *new_first = NULL;
+
+ evlist__for_each_entry(evlist, pos) {
+ if (pos->first_wildcard_match == removed) {
+ pos->first_wildcard_match = new_first;
+ if (!new_first)
+ new_first = pos;
+ }
+ }
+}
+
+static void evlist__fix_removed_wildcard_matches(struct evlist *evlist)
+{
+ struct evsel *pos;
+
+ evlist__for_each_entry(evlist, pos) {
+ if (pos->first_wildcard_match &&
+ !evlist__contains(evlist, pos->first_wildcard_match))
+ evlist__reparent_wildcard_match(evlist, pos->first_wildcard_match);
+ }
+}
+
void evlist__remove(struct evlist *evlist, struct evsel *evsel)
{
perf_evlist__remove(evlist__core(evlist), &evsel->core);
+ evlist__reparent_wildcard_match(evlist, evsel);
+ evsel->first_wildcard_match = NULL;
evlist__put(evsel->evlist);
evsel->evlist = NULL;
}
@@ -1383,6 +1421,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target)
evlist__core(evlist)->has_user_cpus = !!target->cpu_list;
perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
+ evlist__fix_removed_wildcard_matches(evlist);
/* as evlist now has references, put count here */
perf_cpu_map__put(cpus);
@@ -1731,6 +1770,7 @@ static int evlist__create_syswide_maps(struct evlist *evlist)
}
perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
+ evlist__fix_removed_wildcard_matches(evlist);
perf_thread_map__put(threads);
perf_cpu_map__put(cpus);
return 0;
--
2.56.1.915.g69d5ec7298-goog
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] perf evlist: Reparent first_wildcard_match when evsels are removed
2026-12-04 1:18 [PATCH] perf evlist: Reparent first_wildcard_match when evsels are removed Ian Rogers
@ 2026-12-04 1:36 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 1:36 UTC (permalink / raw)
To: Ian Rogers; +Cc: linux-perf-users
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] perf: evlist__add() map propagation omits wildcard
reparenting
--
commit 21a926f2d87a03affcb864c9eb166a663484742d
Author: Ian Rogers <irogers@google.com>
perf evlist: Reparent first_wildcard_match when evsels are removed
This patch adds helpers to reparent first_wildcard_match when an evsel is
removed from an evlist, calling them from evlist__remove(),
evlist__create_maps(), and evlist__create_syswide_maps(), along with unit
tests in tools/perf/tests/hybrid-merge.c.
> diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
> index c2402e4791b64..4b8cba6410c45 100644
> --- a/tools/perf/util/evlist.c
> +++ b/tools/perf/util/evlist.c
[ ... ]
> @@ -1383,6 +1421,7 @@ int evlist__create_maps(struct evlist *evlist, struct target *target)
> evlist__core(evlist)->has_user_cpus = !!target->cpu_list;
>
> perf_evlist__set_maps(evlist__core(evlist), cpus, threads);
> + evlist__fix_removed_wildcard_matches(evlist);
>
> /* as evlist now has references, put count here */
> perf_cpu_map__put(cpus);
[Severity: Medium]
Can evlist__add() still leave stale first_wildcard_match pointers before
evlist__create_maps() runs, or when it is never called?
evlist__new() calls perf_evlist__set_maps() with NULL maps, which sets
needs_map_propagation on every new evlist. During __parse_events() ->
evlist__splice_list_tail(), evlist__add() runs
__perf_evlist__propagate_maps() and removes an empty-CPU primary evsel.
For an event with a per-event cpu= term (such as cycles/cpu=0/ when cpu_atom
does not cover CPU 0), the later evsels are added with first_wildcard_match
still pointing to the removed evsel.
In perf stat --for-each-cgroup, evlist__expand_cgroup() runs before
evlist__create_maps() and copies pos->first_wildcard_match->priv. Since the
removed evsel was never in the list, priv is NULL, permanently breaking
wildcard merging among the surviving PMUs.
Callers such as pyrf__parse_events() set maps before parse_events() and never
call evlist__create_maps(), leaving dangling first_wildcard_match pointers.
Would calling evlist__fix_removed_wildcard_matches() in
evlist__splice_list_tail() (or reparenting on removal in evlist__add()) cover
these paths?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204011846.1959123-1-irogers@google.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 1:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 1:18 [PATCH] perf evlist: Reparent first_wildcard_match when evsels are removed Ian Rogers
2026-12-04 1:36 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox