Skip to content

Commit 2789f15

Browse files
leitaogregkh
authored andcommitted
perf stat: Fix crash on arm64
[ Upstream commit b5708a3 ] Perf stat is crashing on arm64 hosts with the following issue: # make -C tools/perf DEBUG=1 # perf stat sleep 1 perf: util/evsel.c:2034: get_group_fd: Assertion `!(!leader->core.fd)' failed. [1] 1220794 IOT instruction (core dumped) ./perf stat The sorting function introduced by commit a745c08 ("perf stat: Sort default events/metrics") compares events based on their individual properties. This can cause events from different groups to be interleaved, resulting in group members appearing before their leaders in the sorted evlist. When the iterator opens events in list order, a group member may be processed before its leader has been opened. For example, CPU_CYCLES (idx=32) with leader STALL_SLOT_BACKEND (idx=37) could be sorted before its leader, causing the crash when CPU_CYCLES tries to get its group fd from the not-yet-opened leader. Fix this by comparing events based on their leader's attributes instead of their own attributes when the events are in different groups. This ensures all members of a group share the same sort key as their leader, keeping groups together and guaranteeing leaders are opened before their members. Fixes: a745c08 ("perf stat: Sort default events/metrics") Reported-by: Denis Yaroshevskiy <dyaroshev@meta.com> Tested-by: Dmitry Ilvokhin <d@ilvokhin.com> Tested-by: Ian Rogers <irogers@google.com> Signed-off-by: Breno Leitao <leitao@debian.org> Signed-off-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b1f267b commit 2789f15

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

tools/perf/builtin-stat.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,25 +1917,33 @@ static int default_evlist_evsel_cmp(void *priv __maybe_unused,
19171917
const struct evsel *lhs = container_of(lhs_core, struct evsel, core);
19181918
const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node);
19191919
const struct evsel *rhs = container_of(rhs_core, struct evsel, core);
1920+
const struct evsel *lhs_leader = evsel__leader(lhs);
1921+
const struct evsel *rhs_leader = evsel__leader(rhs);
19201922

1921-
if (evsel__leader(lhs) == evsel__leader(rhs)) {
1923+
if (lhs_leader == rhs_leader) {
19221924
/* Within the same group, respect the original order. */
19231925
return lhs_core->idx - rhs_core->idx;
19241926
}
19251927

1928+
/*
1929+
* Compare using leader's attributes so that all members of a group
1930+
* stay together. This ensures leaders are opened before their members.
1931+
*/
1932+
19261933
/* Sort default metrics evsels first, and default show events before those. */
1927-
if (lhs->default_metricgroup != rhs->default_metricgroup)
1928-
return lhs->default_metricgroup ? -1 : 1;
1934+
if (lhs_leader->default_metricgroup != rhs_leader->default_metricgroup)
1935+
return lhs_leader->default_metricgroup ? -1 : 1;
19291936

1930-
if (lhs->default_show_events != rhs->default_show_events)
1931-
return lhs->default_show_events ? -1 : 1;
1937+
if (lhs_leader->default_show_events != rhs_leader->default_show_events)
1938+
return lhs_leader->default_show_events ? -1 : 1;
19321939

19331940
/* Sort by PMU type (prefers legacy types first). */
1934-
if (lhs->pmu != rhs->pmu)
1935-
return lhs->pmu->type - rhs->pmu->type;
1941+
if (lhs_leader->pmu != rhs_leader->pmu)
1942+
return lhs_leader->pmu->type - rhs_leader->pmu->type;
19361943

1937-
/* Sort by name. */
1938-
return strcmp(evsel__name((struct evsel *)lhs), evsel__name((struct evsel *)rhs));
1944+
/* Sort by leader's name. */
1945+
return strcmp(evsel__name((struct evsel *)lhs_leader),
1946+
evsel__name((struct evsel *)rhs_leader));
19391947
}
19401948

19411949
/*

0 commit comments

Comments
 (0)