diff mbox series

[v2,5/9] kernel-shark: Fix memory leak in "sched events" plugin.

Message ID 20210428134730.187533-6-y.karadz@gmail.com (mailing list archive)
State Accepted
Commit 5f8a9bf3ab4bba71aa8705786c771f2f7ae107c1
Headers show
Series (Not so) Minor fixes toward KS 2.0 | expand

Commit Message

Yordan Karadzhov April 28, 2021, 1:47 p.m. UTC
The memory used by the Data container object must be freed. The fix
includes improvement of the KS_DEFINE_PLUGIN_CONTEXT macro.

Fixing: b39499d (kernel-shark: Speed-up the sched_events plugin)
Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/libkshark-plugin.h     |  4 ++--
 src/plugins/sched_events.c | 11 ++++++++++-
 tests/libkshark-tests.cpp  |  2 +-
 3 files changed, 13 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
index b259b24..10e5f1b 100644
--- a/src/libkshark-plugin.h
+++ b/src/libkshark-plugin.h
@@ -373,7 +373,7 @@  int kshark_handle_all_dpis(struct kshark_data_stream *stream,
  * General purpose macro defining methods for adding plugin context.
  * Do not use this macro in header files.
  */
-#define KS_DEFINE_PLUGIN_CONTEXT(type)					\
+#define KS_DEFINE_PLUGIN_CONTEXT(type, type_free)			\
 static type **__context_handler;					\
 static ssize_t __n_streams = -1;					\
 __hidden type *__init(int sd)						\
@@ -412,7 +412,7 @@  __hidden void __close(int sd)						\
 	}								\
 	obj = __get_context(sd);					\
 	if (obj) {							\
-		free(__context_handler[sd]);				\
+		type_free(__context_handler[sd]);			\
 		__context_handler[sd] = NULL;				\
 	}								\
 }									\
diff --git a/src/plugins/sched_events.c b/src/plugins/sched_events.c
index f3c2c23..e151a70 100644
--- a/src/plugins/sched_events.c
+++ b/src/plugins/sched_events.c
@@ -73,8 +73,17 @@  int plugin_sched_get_prev_state(ks_num_field_t field)
 	return (field & mask) >> PREV_STATE_SHIFT;
 }
 
+static void sched_free_context(struct plugin_sched_context *plugin_ctx)
+{
+	if (!plugin_ctx)
+		return;
+
+	kshark_free_data_container(plugin_ctx->ss_data);
+	kshark_free_data_container(plugin_ctx->sw_data);
+}
+
 /** A general purpose macro is used to define plugin context. */
-KS_DEFINE_PLUGIN_CONTEXT(struct plugin_sched_context);
+KS_DEFINE_PLUGIN_CONTEXT(struct plugin_sched_context, sched_free_context);
 
 static bool plugin_sched_init_context(struct kshark_data_stream *stream,
 				      struct plugin_sched_context *plugin_ctx)
diff --git a/tests/libkshark-tests.cpp b/tests/libkshark-tests.cpp
index 5f7c88a..3a5b0c3 100644
--- a/tests/libkshark-tests.cpp
+++ b/tests/libkshark-tests.cpp
@@ -148,7 +148,7 @@  struct test_context {
 	char b;
 };
 
-KS_DEFINE_PLUGIN_CONTEXT(struct test_context);
+KS_DEFINE_PLUGIN_CONTEXT(struct test_context, free);
 
 BOOST_AUTO_TEST_CASE(init_close_plugin)
 {