[PATCH v12 24/33] perf lock: Avoid segv if event is missing a callchain

Ian Rogers
2026-04-13T04:12:53Z

Sashiko recognized a potential segv if a sample is missing a
callchain. Return NULL for this case just as a memory allocation
failure does. In the verbose pr_debug output describe the error and
rather than fail the entire file just skip the event (return 0 rather
than -ENOMEM).

Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-lock.c | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index 89a40d385b27..064b3aa4bad7 100644
--- a/tools/perf/builtin-lock.c
+++ b/tools/perf/builtin-lock.c
@@ -938,9 +938,16 @@ static u64 *get_callstack(struct perf_sample *sample, int max_stack)
u64 i;
int c;

+ if (!sample->callchain) {
+ pr_debug("Sample unexpectedly missing callchain\n");
+ return NULL;
+ }
+
callstack = calloc(max_stack, sizeof(*callstack));
- if (callstack == NULL)
+ if (callstack == NULL) {
+ pr_debug("Failed to allocate callstack\n");
return NULL;
+ }

for (i = 0, c = 0; i < sample->callchain->nr && c < max_stack; i++) {
u64 ip = sample->callchain->ips[i];
@@ -1059,7 +1066,7 @@ static int report_lock_contention_begin_event(struct perf_sample *sample)
if (needs_callstack()) {
u64 *callstack = get_callstack(sample, max_stack_depth);
if (callstack == NULL)
- return -ENOMEM;
+ return 0;

if (!match_callstack_filter(machine, callstack, max_stack_depth)) {
free(callstack);
--
2.53.0.1213.gd9a14994de-goog