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

perf inject: Lazily allocate guest_event event_buf

The event_buf is 64kb (PERF_SAMPLE_SIZE_MAX) and stack allocated in
struct perf_inject. It is used for guest events that may not exist in
a file. Make the array allocation lazy to cut down on the stack usage.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
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>
Link: https://lore.kernel.org/r/20230527034324.2597593-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
892d00fb d3944f0e

+12 -3
+12 -3
tools/perf/builtin-inject.c
··· 47 47 struct guest_event { 48 48 struct perf_sample sample; 49 49 union perf_event *event; 50 - char event_buf[PERF_SAMPLE_MAX_SIZE]; 50 + char *event_buf; 51 51 }; 52 52 53 53 struct guest_id { ··· 1374 1374 1375 1375 static int guest_session__fetch(struct guest_session *gs) 1376 1376 { 1377 - void *buf = gs->ev.event_buf; 1378 - struct perf_event_header *hdr = buf; 1377 + void *buf; 1378 + struct perf_event_header *hdr; 1379 1379 size_t hdr_sz = sizeof(*hdr); 1380 1380 ssize_t ret; 1381 1381 1382 + buf = gs->ev.event_buf; 1383 + if (!buf) { 1384 + buf = malloc(PERF_SAMPLE_MAX_SIZE); 1385 + if (!buf) 1386 + return -ENOMEM; 1387 + gs->ev.event_buf = buf; 1388 + } 1389 + hdr = buf; 1382 1390 ret = readn(gs->tmp_fd, buf, hdr_sz); 1383 1391 if (ret < 0) 1384 1392 return ret; ··· 2409 2401 perf_data__close(&inject.output); 2410 2402 free(inject.itrace_synth_opts.vm_tm_corr_args); 2411 2403 free(inject.event_copy); 2404 + free(inject.guest_session.ev.event_buf); 2412 2405 return ret; 2413 2406 }