Skip to contentSkip to conversation

unwind_user: Fix userspace unwind iterator 32-bit compat handling

RFC PATCH New Mathieu Desnoyers 1 participant 1 message Jul 9, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 unwind_user: Fix userspace unwind iterator 32-bit compat handling

Refactor the stack unwind iterator to clean the overall design and fix 32-bit compat corner-cases that arise due to confusion over the semantic of the struct unwind_user_state type field.

Observed Issue
==============

Before this refactor, the struct unwind_user_state type field identifies:

- Whether sframe is available in the current process, for at least portions of the address space,
- Whether frame pointers should be used for stack walk,
- Whether frame pointers should be used for stack walk in a 32-bit compat process.

It would be natural for the unwind iterator caller to expect the "state->type" to contain the type of stack walk operation performed for the most recent unwind iteration (unwind_user_next()), but it actually acts as an internal cache and is unrelated to the current iteration, which makes this field confusing.

It is not only confusing to the caller, but also the the implementation of unwind_user_next(). Is has the following drawbacks:

- A 32-bit compat process will favor using frame pointers rather than sframe for its next unwind iterator, which is an unexpected priority order because sframe is more accurate than frame pointers when available.
- Because the "UNWIND_USER_TYPE_SFRAME" state type is unaware of 32-bit compat, handling of sframe would be broken on 32-bit compat because compat_fp_state() would return false, thus expecting 64-bit unsigned long types.

Moreover, only IA32 compat emulation is supported. x32 support appears to be missing.

Cause
=====

Associating a semantic to a single enumeration field that consists of:

- many orthogonal properties, without listing all those properties (sframe vs frame pointer, compat vs non-compat) within each of the enumeration labels,
- an implicit priority,

leads to confusion in the unwind iterator implementation.

Using ifdef based on CONFIG_IA32_EMULATION and re-implementing a custom "in_compat_mode" rather than using the pre-existing in_compat_syscall() contributes to mishandling of x32 binaries.

Solution
========

Split this type field into three fields:

- A "current_type" field, which can be queried by the iterator caller and by architecture code to learn which method is used for the current unwind iteration,
- An "available_types" field, which is a bitmask of available user types. It is used internally by the iterator implementation.
- A "compat" field to track whether the unwind targets a 32-bit compat process.

This allows removing the "UNWIND_USER_TYPE_COMPAT_FP" label from the unwind types, since it is now implicitly taken into account by the "compat" field.

This approach will also make it easier to introduce new unwind types in the future, such as an unwinder for JIT code.

Also use CONFIG_COMPAT ifdef rather than CONFIG_IA32_EMULATION, and remove the in_compat_mode() custom macro in favor of in_compat_syscall(). This aims to fix x32 support.

Known Drawbacks
===============

None.

[ This is based on linux-trace unwind/sframe ]

Fixes 1abc29eeca39 ("unwind_user/sframe: Wire up unwind_user to sframe")
Fixes ce8d69a1a578 ("unwind_user/x86: Enable compat mode frame pointer unwinding on x86")
Cc Steven Rostedt (Google) <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Masami Hiramatsu <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andrii Nakryiko <[email protected]>
Cc Indu Bhagat <[email protected]>
Cc "Jose E. Marchesi" <[email protected]>
Cc Beau Belgrave <[email protected]>
Cc Jens Remus <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Andrew Morton <[email protected]>
---
 arch/Kconfig                             |   4 -
 arch/x86/Kconfig                         |   1 -
 arch/x86/include/asm/unwind_user.h       |   9 +-
 arch/x86/include/asm/unwind_user_types.h |   4 +-
 arch/x86/kernel/stacktrace.c             |   6 +-
 include/linux/unwind_user.h              |   9 --
 include/linux/unwind_user_types.h        |  25 ++++-
 kernel/unwind/user.c                     | 137 +++++++++++++----------
 8 files changed, 109 insertions(+), 86 deletions(-)
arch/Kconfig +0 -4
index 86eec85cb898..c2f12135db92 100644
@@ -442,10 +442,6 @@ config HAVE_UNWIND_USER_FP
 	bool
 	select UNWIND_USER
 
-config HAVE_UNWIND_USER_COMPAT_FP
-	bool
-	depends on HAVE_UNWIND_USER_FP
-
 config HAVE_UNWIND_USER_SFRAME
 	bool
 	select UNWIND_USER
arch/x86/Kconfig +0 -1
index 8a382a6b9be3..05dbfa3eb8ea 100644
@@ -302,7 +302,6 @@ 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_UNWIND_USER_SFRAME		if X86_64
 	select HAVE_USER_RETURN_NOTIFIER
arch/x86/include/asm/unwind_user.h +4 -5
index 19634a73612d..77f7af37aa82 100644
@@ -3,6 +3,7 @@
 #define _ASM_X86_UNWIND_USER_H
 
 #include <linux/unwind_user_types.h>
+#include <linux/compat.h>
 
 #define ARCH_INIT_USER_FP_FRAME							\
 	.cfa_off	= (s32)sizeof(long) *  2,				\
@@ -10,7 +11,7 @@
 	.fp_off		= (s32)sizeof(long) * -2,				\
 	.use_fp		= true,
 
-#ifdef CONFIG_IA32_EMULATION
+#ifdef CONFIG_COMPAT
 
 #define ARCH_INIT_USER_COMPAT_FP_FRAME						\
 	.cfa_off	= (s32)sizeof(u32)  *  2,				\
@@ -18,14 +19,12 @@
 	.fp_off		= (s32)sizeof(u32)  * -2,				\
 	.use_fp		= 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 != UNWIND_USER_TYPE_COMPAT_FP)
+	if (!in_compat_syscall())
 		return;
 
 	state->ip += state->arch.cs_base;
@@ -35,7 +34,7 @@ static inline void arch_unwind_user_next(struct unwind_user_state *state)
 #define arch_unwind_user_init arch_unwind_user_init
 #define arch_unwind_user_next arch_unwind_user_next
 
-#endif /* CONFIG_IA32_EMULATION */
+#endif /* CONFIG_COMPAT */
 
 #include <asm-generic/unwind_user.h>
 
arch/x86/include/asm/unwind_user_types.h +2 -2
index f93d535f900e..7b940520659e 100644
@@ -2,7 +2,7 @@
 #ifndef _ASM_X86_UNWIND_USER_TYPES_H
 #define _ASM_X86_UNWIND_USER_TYPES_H
 
-#ifdef CONFIG_IA32_EMULATION
+#ifdef CONFIG_COMPAT
 
 struct arch_unwind_user_state {
 	unsigned long ss_base;
@@ -10,7 +10,7 @@ struct arch_unwind_user_state {
 };
 #define arch_unwind_user_state arch_unwind_user_state
 
-#endif /* CONFIG_IA32_EMULATION */
+#endif /* CONFIG_COMPAT */
 
 #include <asm-generic/unwind_user_types.h>
 
arch/x86/kernel/stacktrace.c +3 -3
index 8ef9d8c71df9..396897851b00 100644
@@ -131,13 +131,13 @@ void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
 	}
 }
 
-#ifdef CONFIG_IA32_EMULATION
+#ifdef CONFIG_COMPAT
 void arch_unwind_user_init(struct unwind_user_state *state,
 			   struct pt_regs *regs)
 {
 	unsigned long cs_base, ss_base;
 
-	if (state->type != UNWIND_USER_TYPE_COMPAT_FP)
+	if (user_64bit_mode(regs))
 		return;
 
 	cs_base = insn_get_seg_base(regs, INAT_SEG_REG_CS);
@@ -155,4 +155,4 @@ void arch_unwind_user_init(struct unwind_user_state *state,
 	state->sp += ss_base;
 	state->fp += ss_base;
 }
-#endif /* CONFIG_IA32_EMULATION */
+#endif /* CONFIG_COMPAT */
include/linux/unwind_user.h +0 -9
index 8a4af0214ecb..0308adb349fc 100644
@@ -5,15 +5,6 @@
 #include <linux/unwind_user_types.h>
 #include <asm/unwind_user.h>
 
-#ifndef ARCH_INIT_USER_FP_FRAME
- #define ARCH_INIT_USER_FP_FRAME
-#endif
-
-#ifndef ARCH_INIT_USER_COMPAT_FP_FRAME
- #define ARCH_INIT_USER_COMPAT_FP_FRAME
- #define in_compat_mode(regs) false
-#endif
-
 /*
  * If an architecture needs to initialize the state for a specific
  * reason, for example, it may need to do something different
include/linux/unwind_user_types.h +20 -5
index 4d50476e950e..fc440ae3a29b 100644
@@ -9,11 +9,23 @@
 struct arch_unwind_user_state {};
 #endif
 
+
+/*
+ * unwind types, listed in priority order: lower numbers are attempted first if
+ * available.
+ */
+enum unwind_user_type_bits {
+	UNWIND_USER_TYPE_SFRAME_BIT =		0,
+	UNWIND_USER_TYPE_FP_BIT =		1,
+
+	_NR_UNWIND_USER_TYPE_BITS,
+};
+
 enum unwind_user_type {
-	UNWIND_USER_TYPE_NONE,
-	UNWIND_USER_TYPE_FP,
-	UNWIND_USER_TYPE_COMPAT_FP,
-	UNWIND_USER_TYPE_SFRAME,
+	/* Type "none" for the start of stack walk iteration. */
+	UNWIND_USER_TYPE_NONE =			0,
+	UNWIND_USER_TYPE_SFRAME =		(1U << UNWIND_USER_TYPE_SFRAME_BIT),
+	UNWIND_USER_TYPE_FP =			(1U << UNWIND_USER_TYPE_FP_BIT),
 };
 
 struct unwind_stacktrace {
@@ -33,7 +45,10 @@ struct unwind_user_state {
 	unsigned long sp;
 	unsigned long fp;
 	struct arch_unwind_user_state arch;
-	enum unwind_user_type type;
+	/* Unwind time used for the most recent unwind traversal iteration. */
+	enum unwind_user_type current_type;
+	/* Unwind types available in the current context. Bitmask of enum unwind_user_type. */
+	unsigned int available_types;
 	bool done;
 };
 
kernel/unwind/user.c +80 -57

199 lines not on this page · raw message

Signature · 1 line
2.43.0