Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v3] perf sched stats: Fix segmentation faults, memory leaks, and stale pointers in diff mode
Date: Wed, 06 May 2026 00:28:58 +0000	[thread overview]
Message-ID: <20260506002858.E7F58C2BCB4@smtp.kernel.org> (raw)
In-Reply-To: <20260506000018.3113599-1-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Resource and memory leak of the second perf session when the first session's data is empty.
- [Medium] Stranded elements in the global `cpu_head` list are leaked when `perf_session__process_events()` fails.
--

commit 2f5196e9db7b207b2b2f6bf8c143c68c7b431e3b
Author: Ian Rogers <irogers@google.com>

perf sched stats: Fix segmentation faults, memory leaks, and stale pointers in diff mode

This commit addresses multiple crash vectors and memory leaks in perf sched
stats diff mode by adding bounds checks, NULL checks, proper error
propagation, and robust memory cleanup.

> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c

[ ... ]

> @@ -4632,7 +4704,10 @@ static int perf_sched__schedstat_diff(struct perf_sched *sched,
>  	err = perf_session__process_events(session[0]);
>  	if (err)
>  		goto out_delete_ses0;

Is this code leaking the events added to the global cpu_head list?

The schedstat callbacks dynamically allocate and add parsed elements to the
global cpu_head list during perf_session__process_events(). However, the
transfer to cpu_head_ses0 via list_replace_init() hasn't happened yet at
this point. Since out_delete_ses0 only frees cpu_head_ses0, it looks like
any items added to cpu_head before the error occurred would be stranded.

The same pattern appears to exist for session[1] and cpu_head_ses1.

[ ... ]

>  	if (list_empty(&cpu_head_ses1)) {
>  		pr_err("Data is not available\n");
>  		ret = -1;
>  		goto out_delete_ses1;
>  	}
> 
>  	if (list_empty(&cpu_head_ses0)) {
>  		pr_err("Data is not available\n");
>  		ret = -1;
>  		goto out_delete_ses0;
>  	}

Does jumping to out_delete_ses0 here bypass the cleanup for the second
session?

Since session[1] has already been successfully opened, parsed, and its
events transferred into cpu_head_ses1, skipping the out_delete_ses1 block
would mean free_schedstat(&cpu_head_ses1) and
perf_session__delete(session[1]) are never called.

> -	show_schedstat_data(&cpu_head_ses0, cd_map0, &cpu_head_ses1, cd_map1, true);
> +	ret = show_schedstat_data(&cpu_head_ses0, cd_map0, session[0]->header.env.nr_cpus_avail,
> +				  &cpu_head_ses1, cd_map1, session[1]->header.env.nr_cpus_avail, true);
> +	if (ret)
> +		goto out_delete_ses1;
>  
>  out_delete_ses1:
>  	free_schedstat(&cpu_head_ses1);
>  	if (!IS_ERR(session[1]))
>  		perf_session__delete(session[1]);
> 
>  out_delete_ses0:
>  	free_schedstat(&cpu_head_ses0);
>  	if (!IS_ERR(session[0]))
>  		perf_session__delete(session[0]);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260506000018.3113599-1-irogers@google.com?part=1

  reply	other threads:[~2026-05-06  0:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-28  7:08 [PATCH v1] perf sched stats: Fix segmentation faults in diff mode Ian Rogers
2026-04-29 12:30 ` James Clark
2026-04-29 14:01 ` Athira Rajeev
2026-04-29 17:39 ` [PATCH v2] " Ian Rogers
2026-04-29 20:18   ` sashiko-bot
2026-04-30 13:29   ` James Clark
2026-05-01 15:16   ` Athira Rajeev
2026-05-06  0:00   ` [PATCH v3] perf sched stats: Fix segmentation faults, memory leaks, and stale pointers " Ian Rogers
2026-05-06  0:28     ` sashiko-bot [this message]
2026-05-06  4:10     ` [PATCH v4] " Ian Rogers
2026-05-06  6:45       ` Ian Rogers
2026-05-06 13:03         ` Arnaldo Carvalho de Melo
2026-05-11 16:20           ` Ian Rogers
2026-05-06  9:07       ` James Clark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260506002858.E7F58C2BCB4@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox