From: Ian Rogers Date: Wed, 20 May 2026 19:05:29 +0000 (-0700) Subject: perf lock: Avoid segv if event is missing a callchain X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8dca7ad2c2576b721e78244206143832d46a3b83;p=thirdparty%2Flinux.git perf lock: Avoid segv if event is missing a callchain 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 Acked-by: Namhyung Kim Cc: Adrian Hunter Cc: Albert Ou Cc: Alexander Shishkin Cc: Alexandre Ghiti Cc: Andi Kleen Cc: Andrew Jones Cc: Anup Patel Cc: Athira Rajeev Cc: Blake Jones Cc: Chen Ni Cc: Chun-Tse Shao Cc: Dapeng Mi Cc: Derek Foreman Cc: Dmitriy Vyukov Cc: Dr. David Alan Gilbert Cc: Howard Chu Cc: Hrishikesh Suresh Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Krzysztof Ɓopatowski Cc: Leo Yan Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Peter Zijlstra Cc: Quan Zhou Cc: Ravi Bangoria Cc: Swapnil Sapkal Cc: Thomas Falcon Cc: Tianyou Li Cc: Yujie Liu Cc: tanze Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c index 89a40d385b27d..064b3aa4bad75 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);