linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: linux-trace-devel@vger.kernel.org
Cc: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 5/7] kernel-shark: Fix memory leak in "sched events" plugin.
Date: Tue, 27 Apr 2021 19:24:06 +0300	[thread overview]
Message-ID: <20210427162408.134001-6-y.karadz@gmail.com> (raw)
In-Reply-To: <20210427162408.134001-1-y.karadz@gmail.com>

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 | 14 +++++++++++++-
 tests/libkshark-tests.cpp  |  2 +-
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
index 85a0a8c..4c5fa5a 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)						\
@@ -403,7 +403,7 @@ __hidden void __close(int sd)						\
 		__n_streams = -1;					\
 		return;							\
 	}								\
-	free(__context_handler[sd]);					\
+	type_free(__context_handler[sd]);				\
 	__context_handler[sd] = NULL;					\
 }									\
 __hidden type *__get_context(int sd)					\
diff --git a/src/plugins/sched_events.c b/src/plugins/sched_events.c
index 5c82803..a66904a 100644
--- a/src/plugins/sched_events.c
+++ b/src/plugins/sched_events.c
@@ -73,8 +73,20 @@ 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;
+
+	if (plugin_ctx->ss_data)
+		kshark_free_data_container(plugin_ctx->ss_data);
+
+	if (plugin_ctx->sw_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)
 {
-- 
2.27.0


  parent reply	other threads:[~2021-04-27 16:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27 16:24 [PATCH 0/7] More minor modifications and fixes toward KS 2.0 Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 1/7] kernel-shark: Fix the build for 32b systems Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 2/7] kernel-shark: Add "cron" job to workflows Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 3/7] kernel-shark: Fix KS_DEFINE_PLUGIN_CONTEXT macro Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 4/7] kernel-shark: Add cleanup of all plugin contexts Yordan Karadzhov (VMware)
2021-04-27 16:24 ` Yordan Karadzhov (VMware) [this message]
2021-04-27 16:24 ` [PATCH 6/7] kernel-shark: Disable the pop-up offset dialog Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 7/7] kernel-shark: Remove kvm_combo from the list of default plugins Yordan Karadzhov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210427162408.134001-6-y.karadz@gmail.com \
    --to=y.karadz@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).