trace-cmd library: Fix memory leak in tracecmd_close()
cover trace-cmd library: Fix memory leak in tracecmd_close()
From: "Steven Rostedt (Google)" <[email protected]>
While writing the trace-cmd analyze command, I ran valgrind on it and found that tracecmd_close() does not clean up the page_maps that were created, at least not when reading a v6 file.
Steven Rostedt (Google) (2):
trace-cmd library: Use cpu_data variable to shorten commands trace-cmd library: Fix memory leak of page_maps
lib/trace-cmd/trace-input.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-)
Signature · 1 line
2.35.1
PATCH 1/2 trace-cmd library: Use cpu_data variable to shorten commands
From: "Steven Rostedt (Google)" <[email protected]>
Instead of using handle->cpu_data[cpu] for all the references in tracecmd_close(), just assign it to a variable cpu_data and use that. It makes the code much more readable.
--- lib/trace-cmd/trace-input.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-)
lib/trace-cmd/trace-input.c +17 -15
@@ -4446,6 +4446,7 @@ void tracecmd_close(struct tracecmd_input *handle) {struct zchunk_cache *cache;struct file_section *del_sec; +struct cpu_data *cpu_data;int cpu;int i;
@@ -4465,30 +4466,31 @@ void tracecmd_close(struct tracecmd_input *handle)free_next (handle, cpu);free_page (handle, cpu);if (handle->cpu_data) { -if (handle->cpu_data[cpu].kbuf) { -kbuffer_free (handle->cpu_data[cpu].kbuf); -if (handle->cpu_data[cpu].page_map) -free_page_map (handle->cpu_data[cpu].page_map); + cpu_data = &handle->cpu_data[cpu]; +if (cpu_data->kbuf) { +kbuffer_free (cpu_data->kbuf); +if (cpu_data->page_map) +free_page_map (cpu_data->page_map); -if (handle->cpu_data[cpu].page_cnt) +if (cpu_data->page_cnt)tracecmd_warning ("%d pages still allocated on cpu %d%s" , - handle->cpu_data[cpu].page_cnt, cpu, -show_records (handle->cpu_data[cpu].pages, - handle->cpu_data[cpu].nr_pages)); -free (handle->cpu_data[cpu].pages); + cpu_data->page_cnt, cpu, +show_records (cpu_data->pages, + cpu_data->nr_pages)); +free (cpu_data->pages); } -if (handle->cpu_data[cpu].compress.fd >=0 ) { -close (handle->cpu_data[cpu].compress.fd); -unlink (handle->cpu_data[cpu].compress.file); +if (cpu_data->compress.fd >=0 ) { +close (cpu_data->compress.fd); +unlink (cpu_data->compress.file); } -while (!list_empty (&handle->cpu_data[cpu].compress.cache)) { - cache =container_of (handle->cpu_data[cpu].compress.cache.next, +while (!list_empty (&cpu_data->compress.cache)) { + cache =container_of (cpu_data->compress.cache.next, struct zchunk_cache, list);list_del (&cache->list);free (cache->map);free (cache); } -free (handle->cpu_data[cpu].compress.chunks); +free (cpu_data->compress.chunks); } }
Signature · 1 line
2.35.1
PATCH 2/2 trace-cmd library: Fix memory leak of page_maps
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
tracecmd_close() does not free up the page_maps for when it reads v6 files. This was found via valgrind.
--- lib/trace-cmd/trace-input.c | 5 +++++ 1 file changed, 5 insertions(+)
lib/trace-cmd/trace-input.c +5 -0
@@ -4447,6 +4447,7 @@ void tracecmd_close(struct tracecmd_input *handle)struct zchunk_cache *cache;struct file_section *del_sec;struct cpu_data *cpu_data; +struct page_map *page_map, *n;int cpu;int i;
@@ -4491,6 +4492,10 @@ void tracecmd_close(struct tracecmd_input *handle)free (cache); }free (cpu_data->compress.chunks); +list_for_each_entry_safe (page_map, n, &cpu_data->page_maps, list) { +list_del (&page_map->list); +free (page_map); + } } }
Signature · 1 line
2.35.1