Re: [PATCH v8 29/29] perf kwork: Fix memory management of kwork_work

sashiko-bot
2026-04-11T07:40:42Z

Sashiko AI review found 1 potential issue(s):
- [Medium] Memory leak of `tmp.name` in `add_work()` in `tools/perf/util/bpf_kwork.c` due to a change in ownership semantics.
--

commit 2e5446a0c1c5c4b87f2d9f4c0b6711cbb57d82a7
Author: Ian Rogers <irogers@google.com>
perf kwork: Fix memory management of kwork_work

This commit addresses several memory management issues in builtin-kwork.c
by implementing a global cleanup function and ensuring name fields in
struct kwork_work are properly managed with strdup and zfree.

> diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
> index 8430a79a02ba8..1b8d4e216ae74 100644
> --- a/tools/perf/builtin-kwork.c
> +++ b/tools/perf/builtin-kwork.c
[ ... ]
> @@ -371,11 +371,58 @@ static struct kwork_work *work_new(struct kwork_work *key)
>
> work->id = key->id;
> work->cpu = key->cpu;
> - work->name = key->name;
> + work->name = key->name ? strdup(key->name) : NULL;
> work->class = key->class;
> return work;
> }

Does this change introduce a memory leak of tmp.name in add_work()?

Looking at add_work() in tools/perf/util/bpf_kwork.c, it allocates memory
for tmp.name via get_work_name_from_map() which internally calls strdup().
It then passes tmp to kwork->add_work().

Before this patch, work_new() took direct ownership of the string pointer.
However, since work_new() now always duplicates the string, the original
tmp.name allocated in add_work() appears to be leaked for every BPF trace
record processed.

While work_push_atom() and work_pop_atom() were updated to free the
temporary key.name via work_exit(), add_work() seems to have been missed.

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260411065718.372240-1-irogers@google.com?part=29