Re: [PATCH bpf-next v8 2/3] perf: Refactor get_perf_callchain

From: Tao Chen

Date: Tue Jan 27 2026 - 21:44:21 EST


在 2026/1/28 05:07, Andrii Nakryiko 写道:
On Sun, Jan 25, 2026 at 11:45 PM Tao Chen <chen.dylane@xxxxxxxxx> wrote:

From BPF stack map, we want to ensure that the callchain buffer
will not be overwritten by other preemptive tasks and we also aim
to reduce the preempt disable interval, Based on the suggestions from Peter
and Andrrii, export new API __get_perf_callchain and the usage scenarios
are as follows from BPF side:

preempt_disable()
entry = get_callchain_entry()
preempt_enable()
__get_perf_callchain(entry)
put_callchain_entry(entry)

Suggested-by: Andrii Nakryiko <andrii@xxxxxxxxxx>
Signed-off-by: Tao Chen <chen.dylane@xxxxxxxxx>
---
include/linux/perf_event.h | 5 +++++
kernel/events/callchain.c | 34 ++++++++++++++++++++++------------
2 files changed, 27 insertions(+), 12 deletions(-)

[...]

+struct perf_callchain_entry *
+get_perf_callchain(struct pt_regs *regs, bool kernel, bool user,
+ u32 max_stack, bool crosstask, bool add_mark, u64 defer_cookie)
+{
+ struct perf_callchain_entry *entry;
+ int ret;
+
+ entry = get_callchain_entry();
+ if (!entry)
+ return NULL;

-exit_put:
+ ret = __get_perf_callchain(entry, regs, kernel, user, max_stack, crosstask, add_mark,
+ defer_cookie);
put_callchain_entry(entry);
+ if (ret)
+ entry = NULL;


purely stylistical nit, so this can be ignored if you disagree, but I
find code that modifies some variable before returning it slightly
less preferable to more explicit:


if (__get_perf_callchain(...)) {
put_callchain_entry(entry);
return NULL;
}

return entry;

return entry;
}


agree, will change it in v9, thanks.

--
2.48.1



--
Best Regards
Tao Chen