[PATCH v2 3/7] perf stat: Release the initial evlist before opening the session
From: Leo Yan
Date: Fri Dec 04 2026 - 06:24:01 EST
If perf_session__new() fails, __cmd_report() returns before the put.
cmd_stat() returns the result of __cmd_report() directly, bypassing
cleanup and leaking the initial evlist. Move the put before session
creation to cover this error path.
Fixes: 2b87be183bca ("perf stat: Avoid evlist leak")
Link: https://sashiko.dev/#/bug/linux-b77574e3-cc57-4338-8810-73a0cfa6fff7
Signed-off-by: Leo Yan <leo.yan@xxxxxxx>
---
tools/perf/builtin-stat.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 92892581ccdcb28f20737f94b72406432b67e316..7b2025c37e87cff4ddcf7da8c643db008bdaed54 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -2408,13 +2408,19 @@ static int __cmd_report(int argc, const char **argv)
perf_stat.tool.stat = perf_event__process_stat_event;
perf_stat.tool.stat_round = process_stat_round_event;
+ /*
+ * The session creates its own evlist. If perf_session__new() fails,
+ * cmd_stat() returns without releasing the initial evlist, so drop
+ * it here.
+ */
+ evlist__put(evsel_list);
+
session = perf_session__new(&perf_stat.data, &perf_stat.tool);
if (IS_ERR(session))
return PTR_ERR(session);
perf_stat.session = session;
stat_config.output = stderr;
- evlist__put(evsel_list);
evsel_list = session->evlist;
ret = perf_session__process_events(session);
--
2.34.1