Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces
@ 2026-12-01 23:02 Andrii Nakryiko
  2026-12-01 23:02 ` [PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains Andrii Nakryiko
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2026-12-01 23:02 UTC (permalink / raw)
  To: bpf, linux-arm-kernel
  Cc: catalin.marinas, will, mark.rutland, linux-perf-users, peterz,
	jolsa, rostedt, Andrii Nakryiko

On arm64 the caller is reported twice when the kernel builds a callchain for
the current task from regs filled by perf_fetch_caller_regs(), instead of regs
saved by a real exception. There was a similar problem on x86-64, but it was
since fixed. Now it's time for arm64.

Patch 1 has the fix, patches 2-3 add BPF selftests for this.

I'm targeting bpf-next because of BPF selftests and to have BPF CI test
everything. Patch 1 doesn't have to go through bpf-next, but it's simple enough,
so if arm64 folks are ok with that, please ack and then we'll land everything
in one go into bpf-next.

v2->v3:
  - skip the duplicate leaf for PSR_MODE_EL1t regs in kunwind_stack_walk()
    by stepping to the frame record at x29, instead of dropping it in
    perf_callchain_kernel(); perf_callchain.c is not changed anymore (Alexei);
  - patch 3 commit message adjusted accordingly.

v1->v2:
  - keep __builtin_frame_address(0) in perf_arch_fetch_caller_regs(), mark
    these regs with PSR_MODE_EL1t instead, and have perf_callchain_kernel()
    skip the duplicate leaf regs->pc for them, like x86 does with
    perf_hw_regs() (Will);

v2: https://lore.kernel.org/bpf/20261119000112.2377949-1-andrii@kernel.org/
v1: https://lore.kernel.org/bpf/20261114001846.873499-1-andrii@kernel.org/

Andrii Nakryiko (3):
  arm64: perf: Don't report the caller twice in caller-regs callchains
  selftests/bpf: Add stacktrace_ips test for tracepoint
  selftests/bpf: Run stacktrace_ips raw_tp, tp and fentry tests on arm64

 arch/arm64/include/asm/perf_event.h           |  3 +-
 arch/arm64/kernel/stacktrace.c                |  6 ++
 .../selftests/bpf/prog_tests/stacktrace_ips.c | 67 ++++++++++++++++++-
 .../selftests/bpf/progs/stacktrace_ips.c      | 18 ++++-
 4 files changed, 88 insertions(+), 6 deletions(-)

-- 
2.53.1-Meta


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains
  2026-12-01 23:02 [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces Andrii Nakryiko
@ 2026-12-01 23:02 ` Andrii Nakryiko
  2026-12-01 23:02 ` [PATCH bpf-next v3 2/3] selftests/bpf: Add stacktrace_ips test for tracepoint Andrii Nakryiko
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2026-12-01 23:02 UTC (permalink / raw)
  To: bpf, linux-arm-kernel
  Cc: catalin.marinas, will, mark.rutland, linux-perf-users, peterz,
	jolsa, rostedt, Andrii Nakryiko, Alexei Starovoitov

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@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@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) &regs->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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH bpf-next v3 2/3] selftests/bpf: Add stacktrace_ips test for tracepoint
  2026-12-01 23:02 [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces Andrii Nakryiko
  2026-12-01 23:02 ` [PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains Andrii Nakryiko
@ 2026-12-01 23:02 ` Andrii Nakryiko
  2026-12-01 23:02 ` [PATCH bpf-next v3 3/3] selftests/bpf: Run stacktrace_ips raw_tp, tp and fentry tests on arm64 Andrii Nakryiko
  2026-12-01 23:12 ` [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces sashiko-bot
  3 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2026-12-01 23:02 UTC (permalink / raw)
  To: bpf, linux-arm-kernel
  Cc: catalin.marinas, will, mark.rutland, linux-perf-users, peterz,
	jolsa, rostedt, Andrii Nakryiko

Add a tp subtest that checks the stack captured by bpf_get_stack()
from a tracepoint program, recorded only for the test's own process.
Depending on static calls and inlining, the stack may or may not have
__traceiter_bpf_testmod_test_read in front of bpf_testmod_test_read,
so accept both. Also check that the entry after it differs: x86
kernels without commit 6d08340d1e35 ("Revert "perf/x86: Always store
regs->ip in perf_callchain_kernel()"") report the tracepoint's caller
twice.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../selftests/bpf/prog_tests/stacktrace_ips.c | 54 +++++++++++++++++++
 .../selftests/bpf/progs/stacktrace_ips.c      | 13 +++++
 2 files changed, 67 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c b/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c
index da42b00e3d1f..a03d4c10c469 100644
--- a/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c
+++ b/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c
@@ -137,6 +137,58 @@ static void test_stacktrace_ips_raw_tp(void)
 	stacktrace_ips__destroy(skel);
 }
 
+static void test_stacktrace_ips_tp(void)
+{
+	__u64 traceiter_addr, read_addr, *ips;
+	struct stacktrace_ips *skel;
+	struct ksyms *ksyms = NULL;
+	struct ksym *ksym;
+	int i;
+
+	skel = stacktrace_ips__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load"))
+		return;
+
+	if (!skel->kconfig->CONFIG_UNWINDER_ORC) {
+		test__skip();
+		goto cleanup;
+	}
+
+	skel->bss->my_pid = getpid();
+
+	skel->links.tp_test = bpf_program__attach(skel->progs.tp_test);
+	if (!ASSERT_OK_PTR(skel->links.tp_test, "bpf_program__attach"))
+		goto cleanup;
+
+	trigger_module_test_read(1);
+
+	ksyms = load_kallsyms_local();
+	if (!ASSERT_OK_PTR(ksyms, "load_kallsyms_local"))
+		goto cleanup;
+	traceiter_addr = ksym_get_addr_local(ksyms, "__traceiter_bpf_testmod_test_read");
+	read_addr = ksym_get_addr_local(ksyms, "bpf_testmod_test_read");
+
+	/*
+	 * Depending on static calls and inlining, __traceiter_bpf_testmod_test_read
+	 * may or may not be in front of bpf_testmod_test_read.
+	 */
+	ips = skel->bss->tp_ips;
+	ksym = ksym_search_local(ksyms, ips[0]);
+	if (!ASSERT_OK_PTR(ksym, "ksym_search_local"))
+		goto cleanup;
+	i = ksym->addr == traceiter_addr ? 1 : 0;
+
+	ksym = ksym_search_local(ksyms, ips[i]);
+	if (!ASSERT_OK_PTR(ksym, "ksym_search_local"))
+		goto cleanup;
+	ASSERT_EQ(ksym->addr, read_addr, "stack_cmp");
+	ASSERT_NEQ(ips[i], ips[i + 1], "stack_dup");
+
+cleanup:
+	free_kallsyms_local(ksyms);
+	stacktrace_ips__destroy(skel);
+}
+
 static void test_stacktrace_ips_kprobe(bool retprobe)
 {
 	LIBBPF_OPTS(bpf_kprobe_opts, opts,
@@ -238,6 +290,8 @@ static void __test_stacktrace_ips(void)
 		test_stacktrace_ips_kprobe_multi(true);
 	if (test__start_subtest("raw_tp"))
 		test_stacktrace_ips_raw_tp();
+	if (test__start_subtest("tp"))
+		test_stacktrace_ips_tp();
 	if (test__start_subtest("kprobe"))
 		test_stacktrace_ips_kprobe(false);
 	if (test__start_subtest("kretprobe"))
diff --git a/tools/testing/selftests/bpf/progs/stacktrace_ips.c b/tools/testing/selftests/bpf/progs/stacktrace_ips.c
index 6830f2978613..df10ef3cb14d 100644
--- a/tools/testing/selftests/bpf/progs/stacktrace_ips.c
+++ b/tools/testing/selftests/bpf/progs/stacktrace_ips.c
@@ -53,6 +53,19 @@ int rawtp_test(void *ctx)
 	return 0;
 }
 
+__u64 tp_ips[128];
+int my_pid;
+
+SEC("tp/bpf_testmod/bpf_testmod_test_read")
+int tp_test(void *ctx)
+{
+	if (bpf_get_current_pid_tgid() >> 32 != my_pid)
+		return 0;
+
+	bpf_get_stack(ctx, tp_ips, sizeof(tp_ips), 0);
+	return 0;
+}
+
 SEC("fentry/bpf_testmod_stacktrace_test")
 int fentry_test(struct pt_regs *ctx)
 {
-- 
2.53.1-Meta


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH bpf-next v3 3/3] selftests/bpf: Run stacktrace_ips raw_tp, tp and fentry tests on arm64
  2026-12-01 23:02 [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces Andrii Nakryiko
  2026-12-01 23:02 ` [PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains Andrii Nakryiko
  2026-12-01 23:02 ` [PATCH bpf-next v3 2/3] selftests/bpf: Add stacktrace_ips test for tracepoint Andrii Nakryiko
@ 2026-12-01 23:02 ` Andrii Nakryiko
  2026-12-01 23:12 ` [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces sashiko-bot
  3 siblings, 0 replies; 5+ messages in thread
From: Andrii Nakryiko @ 2026-12-01 23:02 UTC (permalink / raw)
  To: bpf, linux-arm-kernel
  Cc: catalin.marinas, will, mark.rutland, linux-perf-users, peterz,
	jolsa, rostedt, Andrii Nakryiko

The raw_tp, tp and fentry subtests check stacks unwound from
perf_fetch_caller_regs() regs. Now that the arm64 unwinder no longer
reports the caller twice for such regs, run these subtests on arm64 as
well, where kernel stacks are always unwound through frame records.

Leave the kprobe, kretprobe, kprobe_multi, kretprobe_multi and fexit
subtests x86-only: on arm64, kprobes at function entry miss the probed
function's caller, and fexit stacks have an extra entry for the traced
function. The kretprobe subtests pass on arm64, but don't use
perf_fetch_caller_regs() regs.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 .../selftests/bpf/prog_tests/stacktrace_ips.c     | 15 +++++++++++----
 .../testing/selftests/bpf/progs/stacktrace_ips.c  |  5 +++--
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c b/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c
index a03d4c10c469..aee46543f390 100644
--- a/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c
+++ b/tools/testing/selftests/bpf/prog_tests/stacktrace_ips.c
@@ -2,7 +2,7 @@
 #include <test_progs.h>
 #include "stacktrace_ips.skel.h"
 
-#ifdef __x86_64__
+#if defined(__x86_64__) || defined(__aarch64__)
 static int check_stacktrace_ips(int fd, __u32 key, int cnt, ...)
 {
 	__u64 ips[PERF_MAX_STACK_DEPTH];
@@ -47,6 +47,12 @@ static int check_stacktrace_ips(int fd, __u32 key, int cnt, ...)
 	return err;
 }
 
+/* arm64 always unwinds kernel stacks through frame records */
+static bool unwinder_ok(struct stacktrace_ips *skel)
+{
+	return skel->kconfig->CONFIG_UNWINDER_ORC || skel->kconfig->CONFIG_ARM64;
+}
+
 static void test_stacktrace_ips_kprobe_multi(bool retprobe)
 {
 	LIBBPF_OPTS(bpf_kprobe_multi_opts, opts,
@@ -106,7 +112,7 @@ static void test_stacktrace_ips_raw_tp(void)
 	if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load"))
 		return;
 
-	if (!skel->kconfig->CONFIG_UNWINDER_ORC) {
+	if (!unwinder_ok(skel)) {
 		test__skip();
 		goto cleanup;
 	}
@@ -149,7 +155,7 @@ static void test_stacktrace_ips_tp(void)
 	if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load"))
 		return;
 
-	if (!skel->kconfig->CONFIG_UNWINDER_ORC) {
+	if (!unwinder_ok(skel)) {
 		test__skip();
 		goto cleanup;
 	}
@@ -244,7 +250,8 @@ static void test_stacktrace_ips_trampoline(bool retprobe)
 	if (!ASSERT_OK_PTR(skel, "stacktrace_ips__open_and_load"))
 		return;
 
-	if (!skel->kconfig->CONFIG_UNWINDER_ORC) {
+	/* on arm64, fexit stacks also have a frame for the traced function */
+	if ((retprobe && !skel->kconfig->CONFIG_UNWINDER_ORC) || !unwinder_ok(skel)) {
 		test__skip();
 		goto cleanup;
 	}
diff --git a/tools/testing/selftests/bpf/progs/stacktrace_ips.c b/tools/testing/selftests/bpf/progs/stacktrace_ips.c
index df10ef3cb14d..11dfebd4ddb2 100644
--- a/tools/testing/selftests/bpf/progs/stacktrace_ips.c
+++ b/tools/testing/selftests/bpf/progs/stacktrace_ips.c
@@ -19,14 +19,15 @@ struct {
 } stackmap SEC(".maps");
 
 extern bool CONFIG_UNWINDER_ORC __kconfig __weak;
+extern bool CONFIG_ARM64 __kconfig __weak;
 
 /*
- * This function is here to have CONFIG_UNWINDER_ORC
+ * This function is here to have CONFIG_UNWINDER_ORC and CONFIG_ARM64
  * used and added to object BTF.
  */
 int unused(void)
 {
-	return CONFIG_UNWINDER_ORC ? 0 : 1;
+	return CONFIG_UNWINDER_ORC || CONFIG_ARM64 ? 0 : 1;
 }
 
 __u32 stack_key;
-- 
2.53.1-Meta


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces
  2026-12-01 23:02 [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces Andrii Nakryiko
                   ` (2 preceding siblings ...)
  2026-12-01 23:02 ` [PATCH bpf-next v3 3/3] selftests/bpf: Run stacktrace_ips raw_tp, tp and fentry tests on arm64 Andrii Nakryiko
@ 2026-12-01 23:12 ` sashiko-bot
  3 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-01 23:12 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: linux-perf-users

Sashiko has reviewed this 3-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201230247.73877-1-andrii@kernel.org

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-01 23:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 23:02 [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces Andrii Nakryiko
2026-12-01 23:02 ` [PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains Andrii Nakryiko
2026-12-01 23:02 ` [PATCH bpf-next v3 2/3] selftests/bpf: Add stacktrace_ips test for tracepoint Andrii Nakryiko
2026-12-01 23:02 ` [PATCH bpf-next v3 3/3] selftests/bpf: Run stacktrace_ips raw_tp, tp and fentry tests on arm64 Andrii Nakryiko
2026-12-01 23:12 ` [PATCH bpf-next v3 0/3] Fix duplicate caller frame in arm64 stack traces sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox