From nobody Thu Dec 18 10:35:04 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E48C2C4167B for ; Fri, 8 Dec 2023 10:38:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1573630AbjLHKiN (ORCPT ); Fri, 8 Dec 2023 05:38:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1573654AbjLHKiI (ORCPT ); Fri, 8 Dec 2023 05:38:08 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A5D82114 for ; Fri, 8 Dec 2023 02:29:50 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DD247C433C9; Fri, 8 Dec 2023 10:29:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702031389; bh=qNmjHVAcUhvkoBgfYIyluKuCUP9AzOWE8BbeAF90uB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oOvWvE8TkF6/u8bgZIdhZQWWiUj1p3OGXQYmyUFwxiu0wsbjUyP7i2CzK5diCXNWf PWrUGxmSpeHo7fFG8aszT1HXDNIH/IYwlB8FV19hhwRlazuYsWV95X2miUjdYyrt2n nBQvLquyQZKg9AMydnA5S9a3CidSY9pvfksOSBtAn4TOyMfwHaxJN7lHRS7w5TpwzQ 0dXtC84iM/tkNGVNXGXIopkm1xwWzyWlrOLj1fF40R6PL+fBz9dThGzoCj9IugPDPX 3YSNqplCzX6DuOtWdqJPVLtXB91AVM6OP8pLDi4jvKNtwC9Fqel+j0K7ENoIBdk+mM 4djFLqETUUqMg== From: "Masami Hiramatsu (Google)" To: Alexei Starovoitov , Steven Rostedt , Florent Revest Cc: linux-trace-kernel@vger.kernel.org, LKML , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov , Jiri Olsa , Arnaldo Carvalho de Melo , Daniel Borkmann , Alan Maguire , Mark Rutland , Peter Zijlstra , Thomas Gleixner , Guo Ren Subject: [PATCH v4 27/33] tracing: Add ftrace_fill_perf_regs() for perf event Date: Fri, 8 Dec 2023 19:29:43 +0900 Message-Id: <170203138294.579004.12112201392035747803.stgit@devnote2> X-Mailer: git-send-email 2.34.1 In-Reply-To: <170203105427.579004.8033550792660734570.stgit@devnote2> References: <170203105427.579004.8033550792660734570.stgit@devnote2> User-Agent: StGit/0.19 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Masami Hiramatsu (Google) Add ftrace_fill_perf_regs() which should be compatible with the perf_fetch_caller_regs(). In other words, the pt_regs returned from the ftrace_fill_perf_regs() must satisfy 'user_mode(regs) =3D=3D false' and can= be used for stack tracing. Signed-off-by: Masami Hiramatsu (Google) --- Changes from previous series: NOTHING, just forward ported. --- arch/arm64/include/asm/ftrace.h | 7 +++++++ arch/powerpc/include/asm/ftrace.h | 7 +++++++ arch/s390/include/asm/ftrace.h | 5 +++++ arch/x86/include/asm/ftrace.h | 7 +++++++ include/linux/ftrace.h | 31 +++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+) diff --git a/arch/arm64/include/asm/ftrace.h b/arch/arm64/include/asm/ftrac= e.h index 31051fa2b4d9..c1921bdf760b 100644 --- a/arch/arm64/include/asm/ftrace.h +++ b/arch/arm64/include/asm/ftrace.h @@ -154,6 +154,13 @@ ftrace_partial_regs(const struct ftrace_regs *fregs, s= truct pt_regs *regs) return regs; } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->pc =3D (fregs)->pc; \ + (_regs)->regs[29] =3D (fregs)->fp; \ + (_regs)->sp =3D (fregs)->sp; \ + (_regs)->pstate =3D PSR_MODE_EL1h; \ + } while (0) + int ftrace_regs_query_register_offset(const char *name); =20 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); diff --git a/arch/powerpc/include/asm/ftrace.h b/arch/powerpc/include/asm/f= trace.h index 7e138e0e3baf..8737a794c764 100644 --- a/arch/powerpc/include/asm/ftrace.h +++ b/arch/powerpc/include/asm/ftrace.h @@ -52,6 +52,13 @@ static __always_inline struct pt_regs *arch_ftrace_get_r= egs(struct ftrace_regs * return fregs->regs.msr ? &fregs->regs : NULL; } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->result =3D 0; \ + (_regs)->nip =3D (fregs)->regs.nip; \ + (_regs)->gpr[1] =3D (fregs)->regs.gpr[1]; \ + asm volatile("mfmsr %0" : "=3Dr" ((_regs)->msr)); \ + } while (0) + static __always_inline void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip) diff --git a/arch/s390/include/asm/ftrace.h b/arch/s390/include/asm/ftrace.h index 01e775c98425..c2a269c1617c 100644 --- a/arch/s390/include/asm/ftrace.h +++ b/arch/s390/include/asm/ftrace.h @@ -97,6 +97,11 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *= fregs, #define ftrace_regs_query_register_offset(name) \ regs_query_register_offset(name) =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->psw.addr =3D (fregs)->regs.psw.addr; \ + (_regs)->gprs[15] =3D (fregs)->regs.gprs[15]; \ + } while (0) + #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS /* * When an ftrace registered caller is tracing a function that is diff --git a/arch/x86/include/asm/ftrace.h b/arch/x86/include/asm/ftrace.h index a061f8832b20..2e3de45e9746 100644 --- a/arch/x86/include/asm/ftrace.h +++ b/arch/x86/include/asm/ftrace.h @@ -54,6 +54,13 @@ arch_ftrace_get_regs(struct ftrace_regs *fregs) return &fregs->regs; } =20 +#define arch_ftrace_fill_perf_regs(fregs, _regs) do { \ + (_regs)->ip =3D (fregs)->regs.ip; \ + (_regs)->sp =3D (fregs)->regs.sp; \ + (_regs)->cs =3D __KERNEL_CS; \ + (_regs)->flags =3D 0; \ + } while (0) + #define ftrace_regs_set_instruction_pointer(fregs, _ip) \ do { (fregs)->regs.ip =3D (_ip); } while (0) =20 diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h index 515ec804d605..8150edcf8496 100644 --- a/include/linux/ftrace.h +++ b/include/linux/ftrace.h @@ -190,6 +190,37 @@ ftrace_partial_regs(struct ftrace_regs *fregs, struct = pt_regs *regs) =20 #endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_PT_REGS_TO_= FTRACE_REGS_CAST */ =20 +#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS + +/* + * Please define arch dependent pt_regs which compatible to the + * perf_arch_fetch_caller_regs() but based on ftrace_regs. + * This requires + * - user_mode(_regs) returns false (always kernel mode). + * - able to use the _regs for stack trace. + */ +#ifndef arch_ftrace_fill_perf_regs +/* As same as perf_arch_fetch_caller_regs(), do nothing by default */ +#define arch_ftrace_fill_perf_regs(fregs, _regs) do {} while (0) +#endif + +static __always_inline struct pt_regs * +ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + arch_ftrace_fill_perf_regs(fregs, regs); + return regs; +} + +#else /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */ + +static __always_inline struct pt_regs * +ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs) +{ + return &fregs->regs; +} + +#endif + /* * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs. * Note: this can be true even when ftrace_get_regs() cannot provide a pt_= regs.