From: Ian Rogers Date: Wed, 20 May 2026 19:05:31 +0000 (-0700) Subject: perf kmem: Fix memory leaks on error path and when skipping X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b3ab668761786f9df9e19dd2805e657af2f19c41;p=thirdparty%2Fkernel%2Flinux.git perf kmem: Fix memory leaks on error path and when skipping Fix memory leaks on the error paths and skipped sample handling paths in the perf kmem tool. Ensure that all allocated GFP flags and thread references are properly freed and released via thread__put() when skipping samples or encountering parsing failures, preventing long-term memory usage leaks during large trace analyses. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim Cc: Adrian Hunter Cc: Albert Ou Cc: Alexander Shishkin Cc: Alexandre Ghiti Cc: Andi Kleen Cc: Andrew Jones Cc: Anup Patel Cc: Athira Rajeev Cc: Blake Jones Cc: Chen Ni Cc: Chun-Tse Shao Cc: Dapeng Mi Cc: Derek Foreman Cc: Dmitriy Vyukov Cc: Dr. David Alan Gilbert Cc: Howard Chu Cc: Hrishikesh Suresh Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Krzysztof Ɓopatowski Cc: Leo Yan Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Peter Zijlstra Cc: Quan Zhou Cc: Ravi Bangoria Cc: Swapnil Sapkal Cc: Thomas Falcon Cc: Tianyou Li Cc: Yujie Liu Cc: tanze Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c index 68a39f56204db..daf2272c73372 100644 --- a/tools/perf/builtin-kmem.c +++ b/tools/perf/builtin-kmem.c @@ -783,17 +783,21 @@ static int parse_gfp_flags(struct perf_sample *sample, unsigned int gfp_flags) new = realloc(gfps, (nr_gfps + 1) * sizeof(*gfps)); if (new == NULL) - return -ENOMEM; + goto err_out; gfps = new; - new += nr_gfps++; + new += nr_gfps; new->flags = gfp_flags; new->human_readable = strdup(str + 10); + if (!new->human_readable) + goto err_out; new->compact_str = compact_gfp_flags(str + 10); - if (!new->human_readable || !new->compact_str) - return -ENOMEM; - + if (!new->compact_str) { + free(new->human_readable); + goto err_out; + } + nr_gfps++; qsort(gfps, nr_gfps, sizeof(*gfps), gfpcmp); } @@ -802,6 +806,9 @@ static int parse_gfp_flags(struct perf_sample *sample, unsigned int gfp_flags) trace_seq_destroy(&seq); return 0; +err_out: + trace_seq_destroy(&seq); + return -ENOMEM; } static int evsel__process_page_alloc_event(struct perf_sample *sample) @@ -971,6 +978,7 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused, } if (perf_kmem__skip_sample(sample)) { + thread__put(thread); return 0; }