[PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains
Andrii Nakryiko
andrii at kernel.org
Tue Dec 1 15:02:45 PST 2026
perf_arch_fetch_caller_regs() sets regs->pc to the return address of
the function it is used in (CALLER_ADDR0) and points regs->regs[29] at
that function's own frame record, whose saved LR is that same return
address. The arm64 unwinder reports regs->pc and then unwinds from the
frame record at x29, so the caller shows up twice at the top of every
kernel callchain built from perf_fetch_caller_regs(): tracepoint
events, software events emitted by __perf_sw_event_sched()
(context-switches, cgroup-switches, cpu-migrations), and
bpf_get_stack() and bpf_get_stackid() in tracepoint, raw tracepoint,
tp_btf and fentry/fexit programs. E.g., for a tracepoint program
attached to sched_process_fork:
__traceiter_sched_process_fork+0x44
__traceiter_sched_process_fork+0x44
kernel_clone+0x250
...
x86 tells such regs apart in perf_callchain_kernel() with
perf_hw_regs(): perf_arch_fetch_caller_regs() sets regs->flags to 0,
while hardware-saved RFLAGS always has X86_EFLAGS_FIXED set, and
regs->ip is not reported for regs that fail the check.
Mark these regs on arm64 by setting pstate to PSR_MODE_EL1t, which
exceptions taken from the kernel never have (the kernel runs in EL1h,
or EL2h with VHE). For regs with that mode, kunwind_stack_walk() steps
to the frame record at x29 before reporting anything, so the unwind
starts from the LR saved there and regs->pc is not reported.
regs->pstate of software events emitted by __perf_sw_event_sched(),
which BPF perf_event programs see in bpf_perf_event_data::regs, changes
from PSR_MODE_EL1h to PSR_MODE_EL1t.
x86 had the same duplicate entry, fixed by commit 6d08340d1e35
("Revert "perf/x86: Always store regs->ip in perf_callchain_kernel()"").
Fixes: 5b09a094f2fb ("arm64: perf: Fix callchain parse error with kernel tracepoint events")
Suggested-by: Alexei Starovoitov <ast at kernel.org>
Signed-off-by: Andrii Nakryiko <andrii at kernel.org>
---
arch/arm64/include/asm/perf_event.h | 3 ++-
arch/arm64/kernel/stacktrace.c | 6 ++++++
2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/include/asm/perf_event.h b/arch/arm64/include/asm/perf_event.h
index ee45b4e77347..7c5ab65d0590 100644
--- a/arch/arm64/include/asm/perf_event.h
+++ b/arch/arm64/include/asm/perf_event.h
@@ -13,11 +13,12 @@
#define perf_arch_bpf_user_pt_regs(regs) ®s->user_regs
#endif
+/* PSR_MODE_EL1t marks these regs for kunwind_stack_walk() */
#define perf_arch_fetch_caller_regs(regs, __ip) { \
(regs)->pc = (__ip); \
(regs)->regs[29] = (unsigned long) __builtin_frame_address(0); \
(regs)->sp = current_stack_pointer; \
- (regs)->pstate = PSR_MODE_EL1h; \
+ (regs)->pstate = PSR_MODE_EL1t; \
}
#endif
diff --git a/arch/arm64/kernel/stacktrace.c b/arch/arm64/kernel/stacktrace.c
index 3ebcf8c53fb0..8c79de126116 100644
--- a/arch/arm64/kernel/stacktrace.c
+++ b/arch/arm64/kernel/stacktrace.c
@@ -357,6 +357,12 @@ kunwind_stack_walk(kunwind_consume_fn consume_state,
if (task != current)
return -EINVAL;
kunwind_init_from_regs(&state, regs);
+ /*
+ * With perf_arch_fetch_caller_regs() the PC is a copy of the
+ * LR in the frame record at FP. Start from that record.
+ */
+ if (processor_mode(regs) == PSR_MODE_EL1t && kunwind_next(&state))
+ return -EINVAL;
} else if (task == current) {
kunwind_init_from_caller(&state);
} else {
--
2.53.1-Meta
More information about the linux-arm-kernel
mailing list