mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sohaib Mohamed <sohaib.amhmd@gmail.com>
To: jolsa@redhat.com
Cc: acme@kernel.org, irogers@google.com, jolsa@kernel.org,
	linux-kernel@vger.kernel.org, namhyung@kernel.org,
	sohaib.amhmd@gmail.com, songliubraving@fb.com
Subject: [PATCH] Fixup: perf bench: Fix two memory leaks detected with ASan
Date: Sat, 20 Nov 2021 06:08:54 +0200	[thread overview]
Message-ID: <20211120040854.95169-1-sohaib.amhmd@gmail.com> (raw)
In-Reply-To: <YZev7KClb/ud43Lc@krava>

Fixes: 92723ea0f11d ("perf bench: Fix two memory leaks detected with
ASan")

This patch fixes the previous patch which frees the ctx before
the thread finishes the work. This break -t option.

This patch stores all overwritten pointers in a global vars then free
them at the end of the program.

Tests:
$ perf bench sched messaging -g 1 -l 100 -t
$ perf bench sched messaging -g 1 -l 100
$ perf bench sched all

Signed-off-by: Sohaib Mohamed <sohaib.amhmd@gmail.com>
---
 tools/perf/bench/sched-messaging.c | 39 +++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/tools/perf/bench/sched-messaging.c b/tools/perf/bench/sched-messaging.c
index 488f6e6ba1a5..f60dc9c36f4e 100644
--- a/tools/perf/bench/sched-messaging.c
+++ b/tools/perf/bench/sched-messaging.c
@@ -30,11 +30,15 @@
 #include <linux/time64.h>

 #define DATASIZE 100
+#define GROUPS_NUM 10
+#define FD_NUM 20

 static bool use_pipes = false;
 static unsigned int nr_loops = 100;
 static bool thread_mode = false;
-static unsigned int num_groups = 10;
+static unsigned int num_groups = GROUPS_NUM;
+static unsigned int num_fds = FD_NUM;
+static unsigned int total_counter; /* num_groups * num_fds */

 struct sender_context {
 	unsigned int num_fds;
@@ -50,6 +54,9 @@ struct receiver_context {
 	int wakefd;
 };

+struct receiver_context *ctx_arr[FD_NUM*GROUPS_NUM];
+struct sender_context *snd_ctx_arr[FD_NUM*GROUPS_NUM];
+
 static void fdpair(int fds[2])
 {
 	if (use_pipes) {
@@ -190,29 +197,30 @@ static void reap_worker(pthread_t id)

 /* One group of senders and receivers */
 static unsigned int group(pthread_t *pth,
-		unsigned int num_fds,
+		unsigned int num_fd,
 		int ready_out,
 		int wakefd)
 {
 	unsigned int i;
 	struct sender_context *snd_ctx = malloc(sizeof(struct sender_context)
-			+ num_fds * sizeof(int));
+			+ num_fd * sizeof(int));

 	if (!snd_ctx)
 		err(EXIT_FAILURE, "malloc()");

-	for (i = 0; i < num_fds; i++) {
+	for (i = 0; i < num_fd; i++) {
 		int fds[2];
 		struct receiver_context *ctx = malloc(sizeof(*ctx));

 		if (!ctx)
 			err(EXIT_FAILURE, "malloc()");

+		total_counter++;

 		/* Create the pipe between client and server */
 		fdpair(fds);

-		ctx->num_packets = num_fds * nr_loops;
+		ctx->num_packets = num_fd * nr_loops;
 		ctx->in_fds[0] = fds[0];
 		ctx->in_fds[1] = fds[1];
 		ctx->ready_out = ready_out;
@@ -223,24 +231,28 @@ static unsigned int group(pthread_t *pth,
 		snd_ctx->out_fds[i] = fds[1];
 		if (!thread_mode)
 			close(fds[0]);
+
+		ctx_arr[total_counter] = ctx;
 	}

 	/* Now we have all the fds, fork the senders */
-	for (i = 0; i < num_fds; i++) {
+	for (i = 0; i < num_fd; i++) {
 		snd_ctx->ready_out = ready_out;
 		snd_ctx->wakefd = wakefd;
-		snd_ctx->num_fds = num_fds;
+		snd_ctx->num_fds = num_fd;

-		pth[num_fds+i] = create_worker(snd_ctx, (void *)sender);
+		pth[num_fd+i] = create_worker(snd_ctx, (void *)sender);
 	}

 	/* Close the fds we have left */
 	if (!thread_mode)
-		for (i = 0; i < num_fds; i++)
+		for (i = 0; i < num_fd; i++)
 			close(snd_ctx->out_fds[i]);

+	snd_ctx_arr[total_counter] = snd_ctx;
+
 	/* Return number of children to reap */
-	return num_fds * 2;
+	return num_fd * 2;
 }

 static const struct option options[] = {
@@ -262,7 +274,6 @@ int bench_sched_messaging(int argc, const char **argv)
 {
 	unsigned int i, total_children;
 	struct timeval start, stop, diff;
-	unsigned int num_fds = 20;
 	int readyfds[2], wakefds[2];
 	char dummy;
 	pthread_t *pth_tab;
@@ -287,6 +298,7 @@ int bench_sched_messaging(int argc, const char **argv)
 		if (read(readyfds[0], &dummy, 1) != 1)
 			err(EXIT_FAILURE, "Reading for readyfds");

+
 	gettimeofday(&start, NULL);

 	/* Kick them off */
@@ -323,6 +335,11 @@ int bench_sched_messaging(int argc, const char **argv)
 		break;
 	}

+	/* Free malloc's */
+	for (i = 0; i < FD_NUM * GROUPS_NUM; i++) {
+		free(ctx_arr[i]);
+		free(snd_ctx_arr[i]);
+	}
 	free(pth_tab);

 	return 0;
--
2.25.1


  parent reply	other threads:[~2021-11-20  4:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-19 12:28 'perf stat --bpf-counters test' failures Arnaldo Carvalho de Melo
2021-11-19 14:08 ` Jiri Olsa
2021-11-20  0:26   ` Namhyung Kim
2021-11-20  1:06   ` Sohaib Mohamed
2021-11-20  4:08   ` Sohaib Mohamed [this message]
2021-11-20  6:53     ` [PATCH] Fixup: perf bench: Fix two memory leaks detected with ASan Ahmad Fatoum
2021-11-20  7:08       ` Ahmad Fatoum
2021-11-22 13:08   ` 'perf stat --bpf-counters test' failures Arnaldo Carvalho de Melo
2021-11-22 13:16     ` Jiri Olsa

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=20211120040854.95169-1-sohaib.amhmd@gmail.com \
    --to=sohaib.amhmd@gmail.com \
    --cc=acme@kernel.org \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=songliubraving@fb.com \
    /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

all inboxes | Powered by JetHome®