[PATCH v14 25/32] perf kmem: Fix memory leaks on error path and when skipping
From: Ian Rogers
Date: Wed May 20 2026 - 15:26:24 EST
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 <irogers@xxxxxxxxxx>
Acked-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
tools/perf/builtin-kmem.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 68a39f56204d..daf2272c7337 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;
}
--
2.54.0.746.g67dd491aae-goog