From nobody Tue Oct 7 19:50:51 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13955221FC4; Tue, 8 Jul 2025 01:24:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937840; cv=none; b=uTeSqSfe/tDvTFAzOyDOZeXx70xJBR2Jt5LZw9zvkGZrhIvUDphDrQR0LTkHwB2XihpSmVdNx+RlXX1ReZNeu9ZEZJR3ADSIazY1TzdknE14RjI4hvza91x623fHrsST8wc+6Jy9VTCySoycThDJmESgwovMijWGAt+y2W+M2yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751937840; c=relaxed/simple; bh=OTmEBmEDwwr+5i50GFjKWiFV3nOMPMB1tKCDHYcguyM=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=CpotaNiH1ihIVN/XWKu1MZW1dwBeWWm1DcXQl7v6tv/u3A0cEs37bBGsggvddVCIl//jBpTNQ+5zHM6ZgKcvnJiQUy8J3C4QUpTZ0ryjzt/acITqe+/Y/41GPBIVFil2X4/3n1tK+pUduKhujVKaOXBa5B72vhzrOBFVgm1CRD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j0lUt9uB; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="j0lUt9uB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4DC6C4CEF6; Tue, 8 Jul 2025 01:23:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751937840; bh=OTmEBmEDwwr+5i50GFjKWiFV3nOMPMB1tKCDHYcguyM=; h=Date:From:To:Cc:Subject:References:From; b=j0lUt9uBCmNalZZgXZn6WUMBYzGKP9DZ90y8WUfzcQ4pA+oQY/jQghRiohCMVKb3k NUjXRT7HRxdlw5ZhIi6KYvsVRd5huvOrY3EXTvQa8pMmv6NsxvKHCRGIeWLrbQ3MS6 bthybsKKnI0m+XCdULIPyohXCbRIT4Rq2KjN4wmM8pc7udhUMDpoGEE8xpIsJlTF5g jy9hjHEV+KfnrN2ksOF7RrI1Wgk4cJe3sh/fH9fikCNg+p7Pr+s1L83qbJywEQuW1+ SVuhg97ikp43TNmAlOM2hZtp/ZV3AeN1Hfpy9ephNxzwWlcOQ2tjXVH22DDyRvIJJs +ho2Yv50vsVcA== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uYx3w-00000000Bvx-0ivC; Mon, 07 Jul 2025 21:24:00 -0400 Message-ID: <20250708012400.023290498@kernel.org> User-Agent: quilt/0.68 Date: Mon, 07 Jul 2025 21:22:53 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, x86@kernel.org Cc: Masami Hiramatsu , Mathieu Desnoyers , Josh Poimboeuf , Peter Zijlstra , Ingo Molnar , Jiri Olsa , Namhyung Kim , Thomas Gleixner , Andrii Nakryiko , Indu Bhagat , "Jose E. Marchesi" , Beau Belgrave , Jens Remus , Linus Torvalds , Andrew Morton , Jens Axboe , Florian Weimer , Sam James Subject: [PATCH v13 14/14] unwind_user/x86: Enable compat mode frame pointer unwinding on x86 References: <20250708012239.268642741@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Josh Poimboeuf Use ARCH_INIT_USER_COMPAT_FP_FRAME to describe how frame pointers are unwound on x86, and implement the hooks needed to add the segment base addresses. Enable HAVE_UNWIND_USER_COMPAT_FP if the system has compat mode compiled in. Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- arch/x86/Kconfig | 1 + arch/x86/include/asm/unwind_user.h | 31 ++++++++++++++++++++++++ arch/x86/include/asm/unwind_user_types.h | 17 +++++++++++++ arch/x86/kernel/stacktrace.c | 28 +++++++++++++++++++++ include/linux/unwind_user.h | 20 +++++++++++++++ kernel/unwind/user.c | 4 +++ 6 files changed, 101 insertions(+) create mode 100644 arch/x86/include/asm/unwind_user_types.h diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 5862433c81e1..17d4094c821b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -302,6 +302,7 @@ config X86 select HAVE_SYSCALL_TRACEPOINTS select HAVE_UACCESS_VALIDATION if HAVE_OBJTOOL select HAVE_UNSTABLE_SCHED_CLOCK + select HAVE_UNWIND_USER_COMPAT_FP if IA32_EMULATION select HAVE_UNWIND_USER_FP if X86_64 select HAVE_USER_RETURN_NOTIFIER select HAVE_GENERIC_VDSO diff --git a/arch/x86/include/asm/unwind_user.h b/arch/x86/include/asm/unwi= nd_user.h index 8597857bf896..19634a73612d 100644 --- a/arch/x86/include/asm/unwind_user.h +++ b/arch/x86/include/asm/unwind_user.h @@ -2,10 +2,41 @@ #ifndef _ASM_X86_UNWIND_USER_H #define _ASM_X86_UNWIND_USER_H =20 +#include + #define ARCH_INIT_USER_FP_FRAME \ .cfa_off =3D (s32)sizeof(long) * 2, \ .ra_off =3D (s32)sizeof(long) * -1, \ .fp_off =3D (s32)sizeof(long) * -2, \ .use_fp =3D true, =20 +#ifdef CONFIG_IA32_EMULATION + +#define ARCH_INIT_USER_COMPAT_FP_FRAME \ + .cfa_off =3D (s32)sizeof(u32) * 2, \ + .ra_off =3D (s32)sizeof(u32) * -1, \ + .fp_off =3D (s32)sizeof(u32) * -2, \ + .use_fp =3D true, + +#define in_compat_mode(regs) !user_64bit_mode(regs) + +void arch_unwind_user_init(struct unwind_user_state *state, + struct pt_regs *regs); + +static inline void arch_unwind_user_next(struct unwind_user_state *state) +{ + if (state->type !=3D UNWIND_USER_TYPE_COMPAT_FP) + return; + + state->ip +=3D state->arch.cs_base; + state->fp +=3D state->arch.ss_base; +} + +#define arch_unwind_user_init arch_unwind_user_init +#define arch_unwind_user_next arch_unwind_user_next + +#endif /* CONFIG_IA32_EMULATION */ + +#include + #endif /* _ASM_X86_UNWIND_USER_H */ diff --git a/arch/x86/include/asm/unwind_user_types.h b/arch/x86/include/as= m/unwind_user_types.h new file mode 100644 index 000000000000..f93d535f900e --- /dev/null +++ b/arch/x86/include/asm/unwind_user_types.h @@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_UNWIND_USER_TYPES_H +#define _ASM_X86_UNWIND_USER_TYPES_H + +#ifdef CONFIG_IA32_EMULATION + +struct arch_unwind_user_state { + unsigned long ss_base; + unsigned long cs_base; +}; +#define arch_unwind_user_state arch_unwind_user_state + +#endif /* CONFIG_IA32_EMULATION */ + +#include + +#endif /* _ASM_UNWIND_USER_TYPES_H */ diff --git a/arch/x86/kernel/stacktrace.c b/arch/x86/kernel/stacktrace.c index ee117fcf46ed..8ef9d8c71df9 100644 --- a/arch/x86/kernel/stacktrace.c +++ b/arch/x86/kernel/stacktrace.c @@ -9,7 +9,10 @@ #include #include #include +#include #include +#include +#include #include =20 void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, @@ -128,3 +131,28 @@ void arch_stack_walk_user(stack_trace_consume_fn consu= me_entry, void *cookie, } } =20 +#ifdef CONFIG_IA32_EMULATION +void arch_unwind_user_init(struct unwind_user_state *state, + struct pt_regs *regs) +{ + unsigned long cs_base, ss_base; + + if (state->type !=3D UNWIND_USER_TYPE_COMPAT_FP) + return; + + cs_base =3D insn_get_seg_base(regs, INAT_SEG_REG_CS); + ss_base =3D insn_get_seg_base(regs, INAT_SEG_REG_SS); + + if (cs_base =3D=3D -1) + cs_base =3D 0; + if (ss_base =3D=3D -1) + ss_base =3D 0; + + state->arch.cs_base =3D cs_base; + state->arch.ss_base =3D ss_base; + + state->ip +=3D cs_base; + state->sp +=3D ss_base; + state->fp +=3D ss_base; +} +#endif /* CONFIG_IA32_EMULATION */ diff --git a/include/linux/unwind_user.h b/include/linux/unwind_user.h index 834b643afd3a..8a4af0214ecb 100644 --- a/include/linux/unwind_user.h +++ b/include/linux/unwind_user.h @@ -14,6 +14,26 @@ #define in_compat_mode(regs) false #endif =20 +/* + * If an architecture needs to initialize the state for a specific + * reason, for example, it may need to do something different + * in compat mode, it can define a macro named arch_unwind_user_init + * with the name of the function that will perform this initialization. + */ +#ifndef arch_unwind_user_init +static inline void arch_unwind_user_init(struct unwind_user_state *state, = struct pt_regs *reg) {} +#endif + +/* + * If an architecture requires some more updates to the state between + * stack frames, it can define a macro named arch_unwind_user_next + * with the name of the function that will update the state between + * reading stack frames during the user space stack walk. + */ +#ifndef arch_unwind_user_next +static inline void arch_unwind_user_next(struct unwind_user_state *state) = {} +#endif + int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries); =20 #endif /* _LINUX_UNWIND_USER_H */ diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c index 03775191447c..249d9e32fad7 100644 --- a/kernel/unwind/user.c +++ b/kernel/unwind/user.c @@ -89,6 +89,8 @@ static int unwind_user_next(struct unwind_user_state *sta= te) if (frame->fp_off) state->fp =3D fp; =20 + arch_unwind_user_next(state); + return 0; =20 done: @@ -118,6 +120,8 @@ static int unwind_user_start(struct unwind_user_state *= state) state->sp =3D user_stack_pointer(regs); state->fp =3D frame_pointer(regs); =20 + arch_unwind_user_init(state, regs); + return 0; } =20 --=20 2.47.2