[PATCH v14 23/32] perf lock: Avoid segv if event is missing a callchain
From: Ian Rogers
Date: Wed May 20 2026 - 15:26:04 EST
Avoid a potential segmentation fault if a parsed sample unexpectedly lacks a
valid callchain pointer.
Add a check for a NULL sample->callchain pointer in get_callstack(). If the
callchain is missing, return NULL to safely skip the event and log a debug
warning rather than causing a tool segfault.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
Acked-by: Namhyung Kim <namhyung@xxxxxxxxxx>
---
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.54.0.746.g67dd491aae-goog