]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf lock: Avoid segv if event is missing a callchain
authorIan Rogers <irogers@google.com>
Wed, 20 May 2026 19:05:29 +0000 (12:05 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 May 2026 19:39:40 +0000 (16:39 -0300)
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@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Jones <ajones@ventanamicro.com>
Cc: Anup Patel <anup@brainfault.org>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Blake Jones <blakejones@google.com>
Cc: Chen Ni <nichen@iscas.ac.cn>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: Derek Foreman <derek.foreman@collabora.com>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Hrishikesh Suresh <hrishikesh123s@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Krzysztof Ɓopatowski <krzysztof.m.lopatowski@gmail.com>
Cc: Leo Yan <leo.yan@arm.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Quan Zhou <zhouquan@iscas.ac.cn>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Swapnil Sapkal <swapnil.sapkal@amd.com>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Tianyou Li <tianyou.li@intel.com>
Cc: Yujie Liu <yujie.liu@intel.com>
Cc: tanze <tanze@kylinos.cn>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-lock.c

index 89a40d385b27d83c164c0c87121388c27a8bb721..064b3aa4bad751468e707e1318cd295e7d512aea 100644 (file)
@@ -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);