Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Namhyung Kim" <namhyung@kernel.org>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH 2/4] perf timechart: Fix memory leaks during record
Date: Fri, 05 Jun 2026 23:21:30 +0000	[thread overview]
Message-ID: <20260605232130.BEA5A1F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260605231155.2415760-3-namhyung@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] The dynamically allocated `backtrace` string is assigned to multiple `cpu_sample` structs in `sched_switch()`, leading to a double-free during cleanup in `timechart__release()`.

Pre-existing issues:
- [High] The string dynamically allocated by `cat_backtrace()` is leaked for all events whose handlers do not explicitly take ownership of the pointer.
--

commit 3fc0db6fae9ace5123335636ffe90eb603c38998
Author: Namhyung Kim <namhyung@kernel.org>

perf timechart: Fix memory leaks during record

The command line options can be passed without copy as their lifetime is
limited within the same function.

> diff --git a/tools/perf/builtin-timechart.c b/tools/perf/builtin-timechart.c
> index 73a08236ac299..fded4be93de0d 100644
> --- a/tools/perf/builtin-timechart.c
> +++ b/tools/perf/builtin-timechart.c
> @@ -1543,6 +1543,8 @@ static void timechart__release(struct timechart *tchart)
>  			free(c);
>  			c = next_comm;
>  		}
> +
> +		free(p);
>  		p = next_pid;
>  	}

[Severity: High]
Does this code trigger a double-free on the shared backtrace pointers?

In sched_switch(), if both prev_p and p tasks have been previously
observed, the exact same backtrace pointer is passed to pid_put_sample()
twice:

pid_put_sample(tchart, prev_pid, TYPE_RUNNING, cpu,
               prev_p->current->state_since, timestamp, backtrace);
/* ... */
pid_put_sample(tchart, next_pid, p->current->state, cpu,
               p->current->state_since, timestamp, backtrace);

pid_put_sample() stores this pointer in two newly allocated cpu_sample
structs without duplicating it.

Later, when timechart__release() traverses all cpu_sample structs it calls
free((char *)cs->backtrace). Since two samples point to the exact same
memory address, wouldn't this result in a double-free?

[Severity: High]
This is a pre-existing issue, and was not introduced by the patch under
review, but does process_sample_event() leak memory when calling event
handlers?

process_sample_event() unconditionally calls cat_backtrace(), which
dynamically allocates memory using open_memstream():

FILE *f = open_memstream(&p, &p_len);

This pointer is passed directly to the event's tracepoint_handler callback:

return f(tchart, evsel, sample, cat_backtrace(event, sample, machine));

However, numerous event handlers (e.g., process_sample_cpu_idle,
process_enter_read, process_exit_write) either ignore the backtrace
parameter or omit it entirely.

Since neither the caller nor these handlers free the memory, is it leaked
for every such sample?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260605231155.2415760-1-namhyung@kernel.org?part=2

  reply	other threads:[~2026-06-05 23:21 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-05 23:11 [PATCH 0/4] perf timechart: Fix memory leaks Namhyung Kim
2026-06-05 23:11 ` [PATCH 1/4] perf timechart: Release event samples at the end Namhyung Kim
2026-06-05 23:21   ` Arnaldo Carvalho de Melo
2026-06-05 23:31     ` Namhyung Kim
2026-06-05 23:23   ` sashiko-bot
2026-06-05 23:32     ` Namhyung Kim
2026-06-05 23:11 ` [PATCH 2/4] perf timechart: Fix memory leaks during record Namhyung Kim
2026-06-05 23:21   ` sashiko-bot [this message]
2026-06-05 23:23   ` Arnaldo Carvalho de Melo
2026-06-05 23:11 ` [PATCH 3/4] perf timechart: Fix memory leaks in draw_wakeups() Namhyung Kim
2026-06-05 23:17   ` sashiko-bot
2026-06-05 23:20     ` Namhyung Kim
2026-06-05 23:11 ` [PATCH 4/4] perf test: Update perf timechart test Namhyung Kim
2026-06-05 23:19 ` [PATCH 0/4] perf timechart: Fix memory leaks Arnaldo Carvalho de Melo
2026-06-05 23:28   ` Namhyung Kim
2026-06-05 23:41     ` Arnaldo Carvalho de Melo

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=20260605232130.BEA5A1F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=sashiko-reviews@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