From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA8C4606A325 for ; Tue, 1 Dec 2026 23:03:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=JrIKEVlK; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cJDd3+2N DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=HZZcqDbDwKflXm79fcCn40NKDlJKrXzk0y0pRuq2Uws=; b=JrIKEVlKw0Y7KZ WVvsVlJb8NCrN6V77/KV/sU19NBOLoFF4fU5qb4uTxLtiaVVpYbtLpo6oVyi06lkvoGstnOSmv6So PGLg2B8MpwKpTJRqqnPTB6Q/uBRlMSBzNtMcAdKYFt1bhfykASO7Jc9w8m02c895IbG/U7oXpWjg3 ah9mTwu6kGDfYdmp1IvL+KQjJ8pL30fsqNQ/s5aDS0S2524Y5rB0hc7uKL2CPF5MKoambS8vP4unP TNr9sSwKiUKUaVjCMGaClmf2V7LuwWEBPYnF/tWdTZvzlhlTqaneZO3q3XBV16lnKn/h2Mw82IrrR aWlDb/YF3xv9S8x367hg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYWsB-00000009bRA-3ZZF; Tue, 01 Dec 2026 23:02:55 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xYWs9-00000009bQO-2bSj for linux-arm-kernel@lists.infradead.org; Tue, 01 Dec 2026 23:02:53 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id CA0A9600D1; Tue, 1 Dec 2026 23:02:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60B5B1F0089A; Tue, 1 Dec 2026 23:02:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796166172; bh=HZZcqDbDwKflXm79fcCn40NKDlJKrXzk0y0pRuq2Uws=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=cJDd3+2N605HNa4HznjEiPEz+p4FLr+hm+fer9V9uL56A1XUJA8/XHIw1wKSatnOg l08Yx4q5UjDg/7yw8vYIv16aOxwrjfzZigygOTL8nKVtcX3uxV4oKmkkr/B2wzOI59 HYXKMdbJAOfNlOg4DvsOgcsDyFdUnpbmGx/nUr6tXwdxAO05bSWn+Bgr5k+k+wT/Ky C+HjbuRM7bISbUxDj2u+juOccdeaN1ur9BMeeg4ybVFMBdZ5U/1ZgRWdo5ukW+BXp1 PJNDVqxNgjrytkgGHmNNmJZ6MjrxtzHciyH9MXlbrO0ERrHGk1EcGCbOF4jtTz8hbD Vx6q72nZzElsg== From: Andrii Nakryiko 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 , Alexei Starovoitov Subject: [PATCH bpf-next v3 1/3] arm64: perf: Don't report the caller twice in caller-regs callchains Date: Tue, 1 Dec 2026 15:02:45 -0800 Message-ID: <20261201230247.73877-2-andrii@kernel.org> X-Mailer: git-send-email 2.53.1 In-Reply-To: <20261201230247.73877-1-andrii@kernel.org> References: <20261201230247.73877-1-andrii@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 Signed-off-by: Andrii Nakryiko --- 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