* [PATCH] perf data convert json: Fix trace_seq memory leak in process_sample_event()
@ 2026-06-04 6:55 Tanushree Shah
2026-06-04 21:29 ` Arnaldo Carvalho de Melo
0 siblings, 1 reply; 3+ messages in thread
From: Tanushree Shah @ 2026-06-04 6:55 UTC (permalink / raw)
To: acme, jolsa, adrian.hunter, vmolnaro, mpetlan, tmricht, maddy,
irogers, namhyung
Cc: linux-perf-users, linuxppc-dev, atrajeev, hbathini, Tejas.Manhas1,
Tanushree.Shah, Shivani.Nittor, Tanushree Shah
Unlike the in-kernel trace_seq which uses a statically allocated buffer,
the userspace traceevent library's trace_seq uses a dynamically allocated
one. Therefore, every trace_seq_init() call must be paired with a
trace_seq_destroy(), otherwise it produces a memory leak.
In process_sample_event(), a trace_seq is initialized for each field when
formatting tracepoint raw_data, but the matching trace_seq_destroy() is
never called, leaking memory for every field of every sample processed.
Add the missing trace_seq_destroy() after using the trace_seq buffer to
properly free the allocated memory.
Detected with Valgrind on a perf.data file with 2,729 tracepoint samples:
Before: definitely lost: 55,537,664 bytes in 13,559 blocks
After: definitely lost: 0 bytes in 0 blocks
Fixes: 9d895e468429 ("perf data: Add tracepoint fields when converting to JSON")
Signed-off-by: Tanushree Shah <tshah@linux.ibm.com>
---
tools/perf/util/data-convert-json.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
index d526c91312ed..6dd9349ab10f 100644
--- a/tools/perf/util/data-convert-json.c
+++ b/tools/perf/util/data-convert-json.c
@@ -257,6 +257,7 @@ static int process_sample_event(const struct perf_tool *tool,
trace_seq_init(&s);
tep_print_field(&s, sample->raw_data, fields[i]);
output_json_key_string(out, true, 3, fields[i]->name, s.buffer);
+ trace_seq_destroy(&s);
i++;
}
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] perf data convert json: Fix trace_seq memory leak in process_sample_event()
2026-06-04 6:55 [PATCH] perf data convert json: Fix trace_seq memory leak in process_sample_event() Tanushree Shah
@ 2026-06-04 21:29 ` Arnaldo Carvalho de Melo
2026-06-11 11:53 ` Tanushree Shah
0 siblings, 1 reply; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2026-06-04 21:29 UTC (permalink / raw)
To: Tanushree Shah
Cc: jolsa, adrian.hunter, vmolnaro, mpetlan, tmricht, maddy, irogers,
namhyung, linux-perf-users, linuxppc-dev, atrajeev, hbathini,
Tejas.Manhas1, Tanushree.Shah, Shivani.Nittor
On Thu, Jun 04, 2026 at 12:25:19PM +0530, Tanushree Shah wrote:
> Unlike the in-kernel trace_seq which uses a statically allocated buffer,
> the userspace traceevent library's trace_seq uses a dynamically allocated
> one. Therefore, every trace_seq_init() call must be paired with a
> trace_seq_destroy(), otherwise it produces a memory leak.
>
> In process_sample_event(), a trace_seq is initialized for each field when
> formatting tracepoint raw_data, but the matching trace_seq_destroy() is
> never called, leaking memory for every field of every sample processed.
>
> Add the missing trace_seq_destroy() after using the trace_seq buffer to
> properly free the allocated memory.
>
> Detected with Valgrind on a perf.data file with 2,729 tracepoint samples:
> Before: definitely lost: 55,537,664 bytes in 13,559 blocks
> After: definitely lost: 0 bytes in 0 blocks
Thanks, applied to perf-tools-next, for v7.2.
- Arnaldo
> Fixes: 9d895e468429 ("perf data: Add tracepoint fields when converting to JSON")
> Signed-off-by: Tanushree Shah <tshah@linux.ibm.com>
> ---
> tools/perf/util/data-convert-json.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
> index d526c91312ed..6dd9349ab10f 100644
> --- a/tools/perf/util/data-convert-json.c
> +++ b/tools/perf/util/data-convert-json.c
> @@ -257,6 +257,7 @@ static int process_sample_event(const struct perf_tool *tool,
> trace_seq_init(&s);
> tep_print_field(&s, sample->raw_data, fields[i]);
> output_json_key_string(out, true, 3, fields[i]->name, s.buffer);
> + trace_seq_destroy(&s);
>
> i++;
> }
> --
> 2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] perf data convert json: Fix trace_seq memory leak in process_sample_event()
2026-06-04 21:29 ` Arnaldo Carvalho de Melo
@ 2026-06-11 11:53 ` Tanushree Shah
0 siblings, 0 replies; 3+ messages in thread
From: Tanushree Shah @ 2026-06-11 11:53 UTC (permalink / raw)
To: Arnaldo Carvalho de Melo
Cc: jolsa, adrian.hunter, vmolnaro, mpetlan, tmricht, maddy, irogers,
namhyung, linux-perf-users, linuxppc-dev, atrajeev, hbathini,
Tejas.Manhas1, Tanushree.Shah, Shivani.Nittor
Hello Arnaldo
Thanks for checking the patch. I cannot see this patch applied on
perf-tools-next yet.
https://git.kernel.org/pub/scm/linux/kernel/git/perf/perf-tools-next.git/log/?h=perf-tools-next
Thanks.
On 05/06/26 02:59, Arnaldo Carvalho de Melo wrote:
> On Thu, Jun 04, 2026 at 12:25:19PM +0530, Tanushree Shah wrote:
>> Unlike the in-kernel trace_seq which uses a statically allocated buffer,
>> the userspace traceevent library's trace_seq uses a dynamically allocated
>> one. Therefore, every trace_seq_init() call must be paired with a
>> trace_seq_destroy(), otherwise it produces a memory leak.
>>
>> In process_sample_event(), a trace_seq is initialized for each field when
>> formatting tracepoint raw_data, but the matching trace_seq_destroy() is
>> never called, leaking memory for every field of every sample processed.
>>
>> Add the missing trace_seq_destroy() after using the trace_seq buffer to
>> properly free the allocated memory.
>>
>> Detected with Valgrind on a perf.data file with 2,729 tracepoint samples:
>> Before: definitely lost: 55,537,664 bytes in 13,559 blocks
>> After: definitely lost: 0 bytes in 0 blocks
>
> Thanks, applied to perf-tools-next, for v7.2.
>
> - Arnaldo
>
>> Fixes: 9d895e468429 ("perf data: Add tracepoint fields when converting to JSON")
>> Signed-off-by: Tanushree Shah <tshah@linux.ibm.com>
>> ---
>> tools/perf/util/data-convert-json.c | 1 +
>> 1 file changed, 1 insertion(+)
>>
>> diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
>> index d526c91312ed..6dd9349ab10f 100644
>> --- a/tools/perf/util/data-convert-json.c
>> +++ b/tools/perf/util/data-convert-json.c
>> @@ -257,6 +257,7 @@ static int process_sample_event(const struct perf_tool *tool,
>> trace_seq_init(&s);
>> tep_print_field(&s, sample->raw_data, fields[i]);
>> output_json_key_string(out, true, 3, fields[i]->name, s.buffer);
>> + trace_seq_destroy(&s);
>>
>> i++;
>> }
>> --
>> 2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-11 11:54 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-04 6:55 [PATCH] perf data convert json: Fix trace_seq memory leak in process_sample_event() Tanushree Shah
2026-06-04 21:29 ` Arnaldo Carvalho de Melo
2026-06-11 11:53 ` Tanushree Shah
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox