Linux Perf Users
 help / color / mirror / Atom feed
From: Andrii Nakryiko <andrii@kernel.org>
To: bpf@vger.kernel.org, linux-arm-kernel@lists.infradead.org
Cc: catalin.marinas@arm.com, will@kernel.org, mark.rutland@arm.com,
	linux-perf-users@vger.kernel.org, peterz@infradead.org,
	jolsa@kernel.org, rostedt@goodmis.org,
	Andrii Nakryiko <andrii@kernel.org>
Subject: [PATCH bpf-next v2 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains
Date: Wed, 18 Nov 2026 16:01:10 -0800	[thread overview]
Message-ID: <20261119000112.2377949-2-andrii@kernel.org> (raw)
In-Reply-To: <20261119000112.2377949-1-andrii@kernel.org>

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. perf_callchain_kernel() 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.

Do the same on arm64. Set pstate to PSR_MODE_EL1t, which exceptions
taken from the kernel never have (the kernel runs in EL1h, or EL2h
with VHE), and skip the leaf regs->pc in perf_callchain_kernel() for
regs with that mode. The unwind still starts from regs, with the frame
record at x29 walked by the unwinder.

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")
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
 arch/arm64/include/asm/perf_event.h |  2 +-
 arch/arm64/kernel/perf_callchain.c  | 33 ++++++++++++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/perf_event.h b/arch/arm64/include/asm/perf_event.h
index ee45b4e77347..b3e7c24a5aab 100644
--- a/arch/arm64/include/asm/perf_event.h
+++ b/arch/arm64/include/asm/perf_event.h
@@ -17,7 +17,7 @@
 	(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/perf_callchain.c b/arch/arm64/kernel/perf_callchain.c
index 9b7f26b128b5..3e956c4b0798 100644
--- a/arch/arm64/kernel/perf_callchain.c
+++ b/arch/arm64/kernel/perf_callchain.c
@@ -28,6 +28,31 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
 	arch_stack_walk_user(callchain_trace, entry, regs);
 }
 
+/*
+ * Determine whether the regs were taken from an exception handler rather than
+ * from perf_arch_fetch_caller_regs().
+ */
+static bool perf_hw_regs(struct pt_regs *regs)
+{
+	return (regs->pstate & PSR_MODE_MASK) != PSR_MODE_EL1t;
+}
+
+struct caller_regs_callchain {
+	struct perf_callchain_entry_ctx *entry;
+	bool skip_leaf;
+};
+
+static bool caller_regs_callchain_trace(void *data, unsigned long pc)
+{
+	struct caller_regs_callchain *cc = data;
+
+	if (cc->skip_leaf) {
+		cc->skip_leaf = false;
+		return true;
+	}
+	return callchain_trace(cc->entry, pc);
+}
+
 void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
 			   struct pt_regs *regs)
 {
@@ -36,5 +61,11 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
 		return;
 	}
 
-	arch_stack_walk(callchain_trace, entry, current, regs);
+	if (perf_hw_regs(regs)) {
+		arch_stack_walk(callchain_trace, entry, current, regs);
+	} else {
+		struct caller_regs_callchain cc = { .entry = entry, .skip_leaf = true };
+
+		arch_stack_walk(caller_regs_callchain_trace, &cc, current, regs);
+	}
 }
-- 
2.53.1-Meta


  reply	other threads:[~2026-11-19  0:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  0:01 [PATCH bpf-next v2 0/3] Fix duplicate caller frame in arm64 stack traces Andrii Nakryiko
2026-11-19  0:01 ` Andrii Nakryiko [this message]
2026-11-19  0:22   ` [PATCH bpf-next v2 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains sashiko-bot
2026-11-19  1:38   ` Alexei Starovoitov
2026-11-19  0:01 ` [PATCH bpf-next v2 2/3] selftests/bpf: Add stacktrace_ips test for tracepoint Andrii Nakryiko
2026-11-19  0:07   ` sashiko-bot
2026-11-19  0:01 ` [PATCH bpf-next v2 3/3] selftests/bpf: Run stacktrace_ips raw_tp, tp and fentry tests on arm64 Andrii Nakryiko
2026-11-19  0:05   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119000112.2377949-2-andrii@kernel.org \
    --to=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=jolsa@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox