Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf bench sched messaging: Free contexts on exit

Place sender and receiver contexts onto lists so that they may be
freed on exit. Add missing pthread_attr_destroy. Fixes memory leaks
reported by leak sanitizer.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: André Almeida <andrealmeid@igalia.com>
Cc: Darren Hart <dvhart@infradead.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20230611233610.953456-5-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
e57d7393 8351498d

+17 -1
+17 -1
tools/perf/bench/sched-messaging.c
··· 27 27 #include <poll.h> 28 28 #include <limits.h> 29 29 #include <err.h> 30 + #include <linux/list.h> 30 31 #include <linux/time64.h> 31 32 32 33 #define DATASIZE 100 ··· 36 35 static unsigned int nr_loops = 100; 37 36 static bool thread_mode = false; 38 37 static unsigned int num_groups = 10; 38 + static struct list_head sender_contexts = LIST_HEAD_INIT(sender_contexts); 39 + static struct list_head receiver_contexts = LIST_HEAD_INIT(receiver_contexts); 39 40 40 41 struct sender_context { 42 + struct list_head list; 41 43 unsigned int num_fds; 42 44 int ready_out; 43 45 int wakefd; ··· 48 44 }; 49 45 50 46 struct receiver_context { 47 + struct list_head list; 51 48 unsigned int num_packets; 52 49 int in_fds[2]; 53 50 int ready_out; ··· 175 170 if (ret != 0) 176 171 err(EXIT_FAILURE, "pthread_create failed"); 177 172 173 + pthread_attr_destroy(&attr); 178 174 return childid; 179 175 } 180 176 ··· 207 201 if (!snd_ctx) 208 202 err(EXIT_FAILURE, "malloc()"); 209 203 204 + list_add(&snd_ctx->list, &sender_contexts); 210 205 for (i = 0; i < num_fds; i++) { 211 206 int fds[2]; 212 207 struct receiver_context *ctx = malloc(sizeof(*ctx)); ··· 215 208 if (!ctx) 216 209 err(EXIT_FAILURE, "malloc()"); 217 210 211 + list_add(&ctx->list, &receiver_contexts); 218 212 219 213 /* Create the pipe between client and server */ 220 214 fdpair(fds); ··· 274 266 int readyfds[2], wakefds[2]; 275 267 char dummy; 276 268 pthread_t *pth_tab; 269 + struct sender_context *pos, *n; 277 270 278 271 argc = parse_options(argc, argv, options, 279 272 bench_sched_message_usage, 0); ··· 333 324 } 334 325 335 326 free(pth_tab); 336 - 327 + list_for_each_entry_safe(pos, n, &sender_contexts, list) { 328 + list_del_init(&pos->list); 329 + free(pos); 330 + } 331 + list_for_each_entry_safe(pos, n, &receiver_contexts, list) { 332 + list_del_init(&pos->list); 333 + free(pos); 334 + } 337 335 return 0; 338 336 }