diff mbox series

[04/87] trace-cmd library: Fixed a memory leak on input handler close

Message ID 20210728133250.234140-5-tz.stoyanov@gmail.com (mailing list archive)
State Superseded
Headers show
Series Trace file version 7 | expand

Commit Message

Tzvetomir Stoyanov (VMware) July 28, 2021, 1:31 p.m. UTC
When an input hanlder to a trace file is closed with tracecmd_close(),
the list with buffers is not freed. This leads to a memory leak. Added
logic to free that list.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 lib/trace-cmd/trace-input.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index d5ffafd7..f94a0073 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -3482,7 +3482,7 @@  void tracecmd_ref(struct tracecmd_input *handle)
  */
 void tracecmd_close(struct tracecmd_input *handle)
 {
-	int cpu;
+	int i;
 
 	if (!handle)
 		return;
@@ -3495,21 +3495,21 @@  void tracecmd_close(struct tracecmd_input *handle)
 	if (--handle->ref)
 		return;
 
-	for (cpu = 0; cpu < handle->cpus; cpu++) {
+	for (i = 0; i < handle->cpus; i++) {
 		/* The tracecmd_peek_data may have cached a record */
-		free_next(handle, cpu);
-		free_page(handle, cpu);
-		if (handle->cpu_data && 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);
-
-			if (handle->cpu_data[cpu].page_cnt)
+		free_next(handle, i);
+		free_page(handle, i);
+		if (handle->cpu_data && handle->cpu_data[i].kbuf) {
+			kbuffer_free(handle->cpu_data[i].kbuf);
+			if (handle->cpu_data[i].page_map)
+				free_page_map(handle->cpu_data[i].page_map);
+
+			if (handle->cpu_data[i].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);
+						 handle->cpu_data[i].page_cnt, i,
+						 show_records(handle->cpu_data[i].pages,
+							      handle->cpu_data[i].nr_pages));
+			free(handle->cpu_data[i].pages);
 		}
 	}
 
@@ -3520,6 +3520,11 @@  void tracecmd_close(struct tracecmd_input *handle)
 	free(handle->version);
 	close(handle->fd);
 
+	for (i = 0; i < handle->nr_buffers; i++)
+		free(handle->buffers[i].name);
+
+	free(handle->buffers);
+
 	tracecmd_free_hooks(handle->hooks);
 	handle->hooks = NULL;