perf data convert json: Fix trace_seq memory leak in process_sample_event()
PATCH 1/1 perf data convert json: Fix trace_seq memory leak in process_sample_event()
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
--- tools/perf/util/data-convert-json.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/data-convert-json.c +1 -0
@@ -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++; }
Signature · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The process_sample_event() function leaks reference counts and memory associated with struct addr_location when a sample is skipped due to time filtering. --
commit de6e4710063f2942dd58ebe436d7d992872132b3
Author: Tanushree Shah <[email protected]>
perf data convert json: Fix trace_seq memory leak in process_sample_event()
This commit fixes a memory leak by ensuring trace_seq_destroy() is called to pair with trace_seq_init() when formatting tracepoint raw data.
tools/perf/util/data-convert-json.c static int process_sample_event(const struct perf_tool *tool, · line 263
Quoted 12 lines
tools/perf/util/data-convert-json.c +1
-0@@ -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++; }
[Severity: High]
This is a pre-existing issue, but does the early return path for skipped samples higher up in process_sample_event() leak the addr_location reference handles?
tools/perf/util/data-convert-json.c:process_sample_event() {
addr_location__init(&al);
if (machine__resolve(machine, &al, sample) < 0) {
pr_err("Sample resolution failed!\n");
addr_location__exit(&al);
return -1;
}
if (perf_time__ranges_skip_sample(c->ptime_range, c->range_num, sample->time)) {
++c->skipped;
return 0;
}
...
}When machine__resolve() populates the addr_location structure, it acquires reference handles for objects like thread and map. The early return for time filtering bypasses the addr_location__exit() cleanup.
Could this cause reference counts and memory to leak when dropping skipped samples?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Sashiko,
Thank you for the review and for spotting this!
Regarding the addr_location reference leak:
yes its correct. The early return path for time-filtered samples does leak addr_location references.
When machine__resolve() succeeds, it calls thread__find_map(), which acquires reference counts for thread, map, and maps objects and stores them in the addr_location structure. The early return bypasses addr_location__exit(), preventing these references from being released.
Validation with Valgrind:
I've validated the issue with Valgrind using a test case with 2,385 total samples (150 converted, 2,235 skipped by time filter):
Without the fix:
definitely lost: 8,336 bytes in 32 blocks indirectly lost: 1,539,361 bytes in 21,307 blocks possibly lost: 10,325,465 bytes in 146,283 blocks Total leaked: ~11.9 MB
With the fix:
definitely lost: 0 bytes in 0 blocks indirectly lost: 0 bytes in 0 blocks possibly lost: 0 bytes in 0 blocks Total leaked: 0 bytes
I will send a v2 patch series that addresses both memory leaks:
The trace_seq_destroy() fix (original issue) The addr_location__exit() fix for the early return path
Thanks
Tanushree Shah
Quoted 46 lines · On 04/06/26 12:36, [email protected] wrote:
perf data convert json: Fix
trace_seqmemory leak inprocess_sample_event()This commit fixes a memory leak by ensuring
trace_seq_destroy()is called to pair withtrace_seq_init()when formatting tracepoint raw data.tools/perf/util/data-convert-json.c static int process_sample_event(const struct perf_tool *tool, · line 263
Quoted 12 lines
tools/perf/util/data-convert-json.c +1
-0@@ -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++; }[Severity: High]
This is a pre-existing issue, but does the early return path for skipped samples higher up inprocess_sample_event()leak theaddr_locationreference handles?tools/perf/util/data-convert-json.c:process_sample_event() { addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_err("Sample resolution failed!\n"); addr_location__exit(&al); return -1; } if (perf_time__ranges_skip_sample(c->ptime_range, c->range_num, sample->time)) { ++c->skipped; return 0; } ... }When
machine__resolve()populates theaddr_locationstructure, it acquires reference handles for objects like thread and map. The early return for time filtering bypasses theaddr_location__exit()cleanup.Could this cause reference counts and memory to leak when dropping skipped samples?
-- Sashiko AI review · https://sashiko.dev/#/
patchset/[email protected]?part=1
Quoted 9 lines · Tanushree Shah
Unlike the in-kernel
trace_seqwhich uses a statically allocated buffer, the userspace traceevent library'strace_sequses a dynamically allocated one. Therefore, everytrace_seq_init()call must be paired with atrace_seq_destroy(), otherwise it produces a memory leak.In
process_sample_event(), atrace_seqis initialized for each field when formatting tracepointraw_data, but the matchingtrace_seq_destroy()is never called, leaking memory for every field of every sample processed.
Quoted 6 lines
Add the missing
trace_seq_destroy()after using thetrace_seqbuffer 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
Quoted 20 lines
Fixes 9d895e468429 ("perf data: Add tracepoint fields when converting to JSON")Signed-off-by Tanushree Shah <[email protected]>--- tools/perf/util/data-convert-json.c | 1 + 1 file changed, 1 insertion(+)tools/perf/util/data-convert-json.c +1
-0@@ -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++; }Signature · 1 line
2.47.3
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.
Quoted 41 lines · Arnaldo Carvalho de Melo
Quoted 9 lines · Tanushree Shah
Unlike the in-kernel
trace_seqwhich uses a statically allocated buffer, the userspace traceevent library'strace_sequses a dynamically allocated one. Therefore, everytrace_seq_init()call must be paired with atrace_seq_destroy(), otherwise it produces a memory leak.In
process_sample_event(), atrace_seqis initialized for each field when formatting tracepointraw_data, but the matchingtrace_seq_destroy()is never called, leaking memory for every field of every sample processed.Quoted 6 lines
Add the missing
trace_seq_destroy()after using thetrace_seqbuffer 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 blocksThanks, applied to perf-tools-next, for v7.2.
- Arnaldo
Quoted 20 lines
Fixes 9d895e468429 ("perf data: Add tracepoint fields when converting to JSON")Signed-off-by Tanushree Shah <[email protected]>--- tools/perf/util/data-convert-json.c | 1 + 1 file changed, 1 insertion(+)tools/perf/util/data-convert-json.c +1
-0@@ -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++; }Signature · 1 line
2.47.3
Namhyung Kim
maintainerHello,
Quoted 4 lines · Tanushree Shah
Hello Arnaldo
Thanks for checking the patch. I cannot see this patch applied on perf-tools-next yet.
According to the other comment, I guess you're supposed to send v2. No?
Thanks,
Namhyung
Hi Namhyung,
Yes, I was planning to send a v2 based on the review comments to fix the pre-existing issue.
However, Arnaldo mentioned that the original patch had already been merged before I got to send a v2, so I addressed the pre-existing issue in a separate patch instead. The patch is available here:
https://lore.kernel.org/linux-perf-users/[email protected]/
That said, although Arnaldo mentioned that this patch had been merged, I don't see it in perf-tools-next yet either. I'll wait for him to clarify its current status.
Thanks,
Tanushree Shah
Quoted 13 lines · Namhyung Kim
Hello,
Quoted 4 lines · Tanushree Shah
Hello Arnaldo
Thanks for checking the patch. I cannot see this patch applied on perf-tools-next yet.
According to the other comment, I guess you're supposed to send v2. No?
Thanks,
Namhyung
Namhyung Kim
maintainerQuoted 9 lines · Tanushree Shah
Hi Namhyung,
Yes, I was planning to send a v2 based on the review comments to fix the pre-existing issue.
However, Arnaldo mentioned that the original patch had already been merged before I got to send a v2, so I addressed the pre-existing issue in a separate patch instead. The patch is available here:
Quoted 5 lines
https://lore.kernel.org/linux-perf-users/[email protected]/
That said, although Arnaldo mentioned that this patch had been merged, I don't see it in perf-tools-next yet either. I'll wait for him to clarify its current status.
It's not in the perf-tools-next, and no plan to add it. Please send v2.
Thanks,
Namhyung
Hi Namhyung,
The only change I had planned for v2 was the fix for the pre-existing issue, which has now already been merged separately upstream.
https://github.com/torvalds/linux/commit/2857a5dca750ea989c6fb70b4c14e801e4b7b4ad
Please let me know if there is any additional change you would like me to include in v2.
Quoted 12 lines · Namhyung Kim
Quoted 11 lines · Tanushree Shah
Hi Namhyung,
Yes, I was planning to send a v2 based on the review comments to fix the pre-existing issue.
However, Arnaldo mentioned that the original patch had already been merged before I got to send a v2, so I addressed the pre-existing issue in a separate patch instead. The patch is available here:
Quoted 10 lines
Quoted 3 lines
That said, although Arnaldo mentioned that this patch had been merged, I don't see it in perf-tools-next yet either. I'll wait for him to clarify its current status.
It's not in the perf-tools-next, and no plan to add it. Please send v2.
Thanks,
Namhyung
Thanks
Tanushree Shah
Namhyung Kim
maintainerQuoted 8 lines · Tanushree Shah
Hi Namhyung,
The only change I had planned for v2 was the fix for the pre-existing issue, which has now already been merged separately upstream.https://github.com/torvalds/linux/commit/2857a5dca750ea989c6fb70b4c14e801e4b7b4ad
Please let me know if there is any additional change you would like me to include in v2.
Oh, nevermind then. I'll process this.
Thanks,
Namhyung
Quoted 10 lines · Tanushree Shah
Unlike the in-kernel
trace_seqwhich uses a statically allocated buffer, the userspace traceevent library'strace_sequses a dynamically allocated one. Therefore, everytrace_seq_init()call must be paired with atrace_seq_destroy(), otherwise it produces a memory leak.In
process_sample_event(), atrace_seqis initialized for each field when formatting tracepointraw_data, but the matchingtrace_seq_destroy()is never called, leaking memory for every field of every sample processed.[...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung