perf sched stats: Fix memory leaks
Namhyung Kim
maintainercover perf sched stats: Fix memory leaks
Hello,
This is a small series to fix memory leaks in the perf sched stats. I've tested the changes with the address sanitizer.
Thanks,
Namhyung
Namhyung Kim (3):
perf sched: Add missing perf_session__delete() perf sched: Fix memory leaks in perf sched stats report perf sched: Free subcommand string after perf sched stats
tools/perf/builtin-sched.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Namhyung Kim
maintainerPATCH 1/3 perf sched: Add missing perf_session__delete()
The perf sched stats record missed to release the session and ASAN reported a leak.
--- tools/perf/builtin-sched.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/builtin-sched.c +1 -0
@@ -4027,6 +4027,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,else fprintf (stderr,"[ perf sched stats: Failed !! ]\n" ); +perf_session__delete (session);evlist__put (evlist);close (fd);return err;
Signature · 1 line
2.55.0.795.g602f6c329a-goog
[email protected]
AI reviewRe: [PATCH v1 1/3] perf sched: Add missing perf_session__delete()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The patch introduces a double close of the file descriptor `fd` because…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch introduces a double close of the file descriptor fd because perf_session__delete(session) inherently closes it before the explicit close(fd). --
--- Patch [1]: [PATCH v1 1/3] perf sched: Add missing perf_session__delete() --- commit 2da9bd6a7ce8e17e442d8be81b1b57837aaee6e1
Author: Namhyung Kim <[email protected]>
perf sched: Add missing perf_session__delete()
This patch addresses an ASAN-reported memory leak in the perf sched stats record command by explicitly deleting the session before returning.
tools/perf/builtin-sched.c static int perf_sched__schedstat_record(struct perf_sched *sched, · line 4032
Quoted 11 lines
tools/perf/builtin-sched.c +1
-0@@ -4027,6 +4027,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,else fprintf (stderr,"[ perf sched stats: Failed !! ]\n" ); +perf_session__delete (session);evlist__put (evlist);close (fd);
[Severity: Medium]
Does adding perf_session__delete() result in closing the file descriptor twice?
Since perf_session__delete() internally closes the file descriptor associated with the session data, the subsequent explicit close(fd) will attempt to close it again. This could return EBADF or erroneously close an unrelated file descriptor if another thread opens a file in the meantime.
Quoted 2 lines
tools/perf/builtin-sched.c +0
-0return err;}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Namhyung Kim
maintainerRe: [PATCH v1 1/3] perf sched: Add missing perf_session__delete()
Quoted 28 lines · On Mon, Jul 13, 2026 at 08:59:07PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The patch introduces a double close of the file descriptorfdbecauseperf_session__delete(session)inherently closes it before the explicitclose(fd). ----- Patch [1]: [PATCH v1 1/3] perf sched: Add missing
perf_session__delete()--- commit2da9bd6a7ce8e17e442d8be81b1b57837aaee6e1
Author: Namhyung Kim <[email protected]>perf sched: Add missing
perf_session__delete()This patch addresses an ASAN-reported memory leak in the perf sched stats record command by explicitly deleting the session before returning.
tools/perf/builtin-sched.c static int perf_sched__schedstat_record(struct perf_sched *sched, · line 4032
Quoted 11 lines
tools/perf/builtin-sched.c +1
-0@@ -4027,6 +4027,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,else fprintf (stderr,"[ perf sched stats: Failed !! ]\n" ); +perf_session__delete (session);evlist__put (evlist);close (fd);[Severity: Medium]
Does addingperf_session__delete()result in closing the file descriptor twice?
Quoted 4 lines
Since
perf_session__delete()internally closes the file descriptor associated with the session data, the subsequent explicit close(fd) will attempt to close it again. This could return EBADF or erroneously close an unrelated file descriptor if another thread opens a file in the meantime.
Hmm.. looks right. I think I can just remove the close().
Thanks,
Namhyung
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v1 1/3] perf sched: Add missing perf_session__delete()
Quoted 14 lines · Namhyung Kim
Quoted 13 lines · On Mon, Jul 13, 2026 at 08:59:07PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
tools/perf/builtin-sched.c static int perf_sched__schedstat_record(struct perf_sched *sched, · line 4032
Quoted 8 lines
+++
b/tools/perf/builtin-sched.ctools/perf/builtin-sched.c +1
-0@@ -4027,6 +4027,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,else fprintf (stderr,"[ perf sched stats: Failed !! ]\n" ); +perf_session__delete (session);evlist__put (evlist);close (fd);[Severity: Medium]
Does addingperf_session__delete()result in closing the file descriptor twice?
Quoted 6 lines
Quoted 4 lines
Since
perf_session__delete()internally closes the file descriptor associated with the session data, the subsequent explicit close(fd) will attempt to close it again. This could return EBADF or erroneously close an unrelated file descriptor if another thread opens a file in the meantime.Hmm.. looks right. I think I can just remove the
close().
Ok, waiting for v2 then.
- Arnaldo
Namhyung Kim
maintainerPATCH 2/3 perf sched: Fix memory leaks in perf sched stats report
In reply to Namhyung KimThe second pass data is not saved in the list and only used to calculate delta from the first pass. Let's free the data after use.
--- tools/perf/builtin-sched.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/perf/builtin-sched.c +4 -0
@@ -4632,6 +4632,8 @@ static int perf_sched__process_schedstat(const struct perf_tool *tool __maybe_un domain_second_pass =list_first_entry (&cpu_second_pass->domain_head, struct schedstat_domain, domain_list);store_schedstat_cpu_diff (temp); +free (temp->cpu_data); +free (temp); } }else if (event->header.type ==PERF_RECORD_SCHEDSTAT_DOMAIN ) {struct schedstat_cpu *cpu_tail;
@@ -4652,6 +4654,8 @@ static int perf_sched__process_schedstat(const struct perf_tool *tool __maybe_un }else {store_schedstat_domain_diff (temp); domain_second_pass =list_next_entry (domain_second_pass, domain_list); +free (temp->domain_data); +free (temp); } }
Signature · 1 line
2.55.0.795.g602f6c329a-goog
Namhyung Kim
maintainerPATCH 3/3 perf sched: Free subcommand string after perf sched stats
In reply to Namhyung KimThe first entry of the sched_usage is dynamically allocated in parse_options_subcommand() so it should be released at the end. Do not return from a subcommand directly.
--- tools/perf/builtin-sched.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
tools/perf/builtin-sched.c +5 -4
@@ -5260,19 +5260,20 @@ int cmd_sched(int argc, const char **argv)if (argc) argc =parse_options (argc, argv, stats_options, stats_usage,0 ); -return perf_sched__schedstat_record (&sched, argc, argv); + ret =perf_sched__schedstat_record (&sched, argc, argv); }else if (argv[0 ] && !strcmp (argv[0 ],"report" )) {if (argc) argc =parse_options (argc, argv, stats_options, stats_usage,0 ); -return perf_sched__schedstat_report (&sched); + ret =perf_sched__schedstat_report (&sched); }else if (argv[0 ] && !strcmp (argv[0 ],"diff" )) {if (argc) argc =parse_options (argc, argv, stats_options, stats_usage,0 ); -return perf_sched__schedstat_diff (&sched, argc, argv); + ret =perf_sched__schedstat_diff (&sched, argc, argv); + }else { + ret =perf_sched__schedstat_live (&sched, argc, argv); } -return perf_sched__schedstat_live (&sched, argc, argv); }else {usage_with_options (sched_usage, sched_options); }
Signature · 1 line
2.55.0.795.g602f6c329a-goog