[PATCH v5 17/25] perf kmem: Don't pass evsel with sample

From: Ian Rogers

Date: Fri Apr 03 2026 - 16:42:24 EST


The sample contains the evsel and so it is unnecessary to pass the
evsel as well. Fix memory leaks on error paths spotted during code
review.

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-kmem.c | 44 ++++++++++++++++++++++-----------------
1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index b04f5d578e7a..cafa0eabfbc5 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -171,7 +171,7 @@ static int insert_caller_stat(unsigned long call_site,
return 0;
}

-static int evsel__process_alloc_event(struct evsel *evsel, struct perf_sample *sample)
+static int evsel__process_alloc_event(struct perf_sample *sample)
{
unsigned long ptr = perf_sample__intval(sample, "ptr"),
call_site = perf_sample__intval(sample, "call_site");
@@ -198,7 +198,7 @@ static int evsel__process_alloc_event(struct evsel *evsel, struct perf_sample *s
* If the tracepoint contains the field "node" the tool stats the
* cross allocation.
*/
- if (evsel__field(evsel, "node")) {
+ if (evsel__field(sample->evsel, "node")) {
int node1, node2;

node1 = cpu__get_node((struct perf_cpu){.cpu = sample->cpu});
@@ -243,7 +243,7 @@ static struct alloc_stat *search_alloc_stat(unsigned long ptr,
return NULL;
}

-static int evsel__process_free_event(struct evsel *evsel __maybe_unused, struct perf_sample *sample)
+static int evsel__process_free_event(struct perf_sample *sample)
{
unsigned long ptr = perf_sample__intval(sample, "ptr");
struct alloc_stat *s_alloc, *s_caller;
@@ -751,8 +751,7 @@ static char *compact_gfp_string(unsigned long gfp_flags)
return NULL;
}

-static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,
- unsigned int gfp_flags)
+static int parse_gfp_flags(struct perf_sample *sample, unsigned int gfp_flags)
{
struct tep_record record = {
.cpu = sample->cpu,
@@ -773,7 +772,7 @@ static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,
}

trace_seq_init(&seq);
- tp_format = evsel__tp_format(evsel);
+ tp_format = evsel__tp_format(sample->evsel);
if (tp_format)
tep_print_event(tp_format->tep, &seq, &record, "%s", TEP_PRINT_INFO);

@@ -784,17 +783,21 @@ static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,

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);
}

@@ -803,9 +806,12 @@ static int parse_gfp_flags(struct evsel *evsel, struct perf_sample *sample,

trace_seq_destroy(&seq);
return 0;
+err_out:
+ trace_seq_destroy(&seq);
+ return -ENOMEM;
}

-static int evsel__process_page_alloc_event(struct evsel *evsel, struct perf_sample *sample)
+static int evsel__process_page_alloc_event(struct perf_sample *sample)
{
u64 page;
unsigned int order = perf_sample__intval(sample, "order");
@@ -845,7 +851,7 @@ static int evsel__process_page_alloc_event(struct evsel *evsel, struct perf_samp
return 0;
}

- if (parse_gfp_flags(evsel, sample, gfp_flags) < 0)
+ if (parse_gfp_flags(sample, gfp_flags) < 0)
return -1;

callsite = find_callsite(sample);
@@ -886,8 +892,7 @@ static int evsel__process_page_alloc_event(struct evsel *evsel, struct perf_samp
return 0;
}

-static int evsel__process_page_free_event(struct evsel *evsel __maybe_unused,
- struct perf_sample *sample)
+static int evsel__process_page_free_event(struct perf_sample *sample)
{
u64 page;
unsigned int order = perf_sample__intval(sample, "order");
@@ -969,8 +974,7 @@ static bool perf_kmem__skip_sample(struct perf_sample *sample)
return false;
}

-typedef int (*tracepoint_handler)(struct evsel *evsel,
- struct perf_sample *sample);
+typedef int (*tracepoint_handler)(struct perf_sample *sample);

static int process_sample_event(const struct perf_tool *tool __maybe_unused,
union perf_event *event,
@@ -988,14 +992,16 @@ static int process_sample_event(const struct perf_tool *tool __maybe_unused,
return -1;
}

- if (perf_kmem__skip_sample(sample))
+ if (perf_kmem__skip_sample(sample)) {
+ thread__put(thread);
return 0;
+ }

dump_printf(" ... thread: %s:%d\n", thread__comm_str(thread), thread__tid(thread));

if (evsel->handler != NULL) {
tracepoint_handler f = evsel->handler;
- err = f(evsel, sample);
+ err = f(sample);
}

thread__put(thread);
--
2.53.0.1213.gd9a14994de-goog