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

sashiko-bot
2026-04-13T04:52:19Z

Sashiko AI review found 1 potential issue(s):
- [Medium] The commit overloads the `NULL` return value of `get_callstack()` to mean both 'missing callchain' and 'out of memory', causing the caller to silently suppress genuine memory allocation failures.
--

commit 2eb0a20f68e00ddc33ffe858af31a1ce762d66da
Author: Ian Rogers <irogers@google.com>
Subject: perf lock: Avoid segv if event is missing a callchain

This commit avoids a segmentation fault when a sample is missing a callchain
by checking for the missing callchain and returning NULL. The caller is
updated to skip the event instead of failing the entire process.

> 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;

Does this overload the NULL return value of get_callstack() to mean both
"missing callchain" and "out of memory"?

Since this now returns 0 when the callstack is NULL, genuine out-of-memory
errors from calloc() will be silently skipped instead of returning -ENOMEM
and safely aborting the process.

Under memory pressure, could this result in silently dropping events and
producing incomplete statistics without warning the user?

--
Sashiko AI review · https://sashiko.dev/#/patchset/20260413041143.1736055-1-irogers@google.com?part=24