[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAP-5=fU3F4uhqzhvmpjB69O3zs-qz5_n8=riLz3JoCqKn4156g@mail.gmail.com>
Date: Wed, 2 Jul 2025 20:06:59 -0700
From: Ian Rogers <irogers@...gle.com>
To: Namhyung Kim <namhyung@...nel.org>
Cc: Arnaldo Carvalho de Melo <acme@...nel.org>, Kan Liang <kan.liang@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>, Adrian Hunter <adrian.hunter@...el.com>,
Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...nel.org>,
LKML <linux-kernel@...r.kernel.org>, linux-perf-users@...r.kernel.org
Subject: Re: [PATCH 3/8] perf sched: Fix memory leaks in 'perf sched map'
On Wed, Jul 2, 2025 at 6:49 PM Namhyung Kim <namhyung@...nel.org> wrote:
>
> It maintains per-cpu pointers for the current thread but it doesn't
> release the refcounts.
>
> Signed-off-by: Namhyung Kim <namhyung@...nel.org>
Reviewed-by: Ian Rogers <irogers@...gle.com>
Thanks,
Ian
> ---
> tools/perf/builtin-sched.c | 31 ++++++++++++++++++++-----------
> 1 file changed, 20 insertions(+), 11 deletions(-)
>
> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
> index fa4052e040201105..b73989fb6acef8d6 100644
> --- a/tools/perf/builtin-sched.c
> +++ b/tools/perf/builtin-sched.c
> @@ -1634,6 +1634,7 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
> const char *color = PERF_COLOR_NORMAL;
> char stimestamp[32];
> const char *str;
> + int ret = -1;
>
> BUG_ON(this_cpu.cpu >= MAX_CPUS || this_cpu.cpu < 0);
>
> @@ -1664,17 +1665,20 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
> sched_in = map__findnew_thread(sched, machine, -1, next_pid);
> sched_out = map__findnew_thread(sched, machine, -1, prev_pid);
> if (sched_in == NULL || sched_out == NULL)
> - return -1;
> + goto out;
>
> tr = thread__get_runtime(sched_in);
> - if (tr == NULL) {
> - thread__put(sched_in);
> - return -1;
> - }
> + if (tr == NULL)
> + goto out;
> +
> + thread__put(sched->curr_thread[this_cpu.cpu]);
> + thread__put(sched->curr_out_thread[this_cpu.cpu]);
>
> sched->curr_thread[this_cpu.cpu] = thread__get(sched_in);
> sched->curr_out_thread[this_cpu.cpu] = thread__get(sched_out);
>
> + ret = 0;
> +
> str = thread__comm_str(sched_in);
> new_shortname = 0;
> if (!tr->shortname[0]) {
> @@ -1769,12 +1773,10 @@ static int map_switch_event(struct perf_sched *sched, struct evsel *evsel,
> color_fprintf(stdout, color, "\n");
>
> out:
> - if (sched->map.task_name)
> - thread__put(sched_out);
> -
> + thread__put(sched_out);
> thread__put(sched_in);
>
> - return 0;
> + return ret;
> }
>
> static int process_sched_switch_event(const struct perf_tool *tool,
> @@ -3556,10 +3558,10 @@ static int perf_sched__map(struct perf_sched *sched)
>
> sched->curr_out_thread = calloc(MAX_CPUS, sizeof(*(sched->curr_out_thread)));
> if (!sched->curr_out_thread)
> - return rc;
> + goto out_free_curr_thread;
>
> if (setup_cpus_switch_event(sched))
> - goto out_free_curr_thread;
> + goto out_free_curr_out_thread;
>
> if (setup_map_cpus(sched))
> goto out_free_cpus_switch_event;
> @@ -3590,7 +3592,14 @@ static int perf_sched__map(struct perf_sched *sched)
> out_free_cpus_switch_event:
> free_cpus_switch_event(sched);
>
> +out_free_curr_out_thread:
> + for (int i = 0; i < MAX_CPUS; i++)
> + thread__put(sched->curr_out_thread[i]);
> + zfree(&sched->curr_out_thread);
> +
> out_free_curr_thread:
> + for (int i = 0; i < MAX_CPUS; i++)
> + thread__put(sched->curr_thread[i]);
> zfree(&sched->curr_thread);
> return rc;
> }
> --
> 2.50.0.727.gbf7dc18ff4-goog
>
Powered by blists - more mailing lists