Skip to contentSkip to conversation

Improvements to unwind user

RFC PATCH New Mathieu Desnoyers 1 participant 4 messages Jul 10, 2025

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

v2 cover Improvements to unwind user

Here are a few improvements to unwind user for feedback.

Thanks,

Mathieu

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]>

Mathieu Desnoyers (3):
unwind_user: Fix userspace unwind iterator 32-bit compat handling
unwind: Export unwind_user symbol to GPL modules unwind deferred: Introduce unwind_user_trace_cached

 arch/x86/include/asm/unwind_user.h |   6 +-
 arch/x86/kernel/stacktrace.c       |   2 +-
 include/asm-generic/unwind_user.h  |   4 +
 include/linux/unwind_deferred.h    |   2 +
 include/linux/unwind_user.h        |   9 --
 include/linux/unwind_user_types.h  |  25 ++++--
 kernel/unwind/deferred.c           |  31 +++++++
 kernel/unwind/user.c               | 140 +++++++++++++++++------------
 8 files changed, 144 insertions(+), 75 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH v2 1/3 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 to 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.

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.

Solution
========

Split this type field into two 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.

Remove the "UNWIND_USER_TYPE_COMPAT_FP" label from the unwind types, since it is now implicitly taken into account by the unwind_compat_mode() check.

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

Rename in_compat_mode to unwind_compat_mode to remove confusion between the pre-existing in_compat_syscall() macro (which returns true for both ia32 and x32) and unwind_compat_mode() which only returns true for ia32.

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/x86/include/asm/unwind_user.h |   6 +-
 arch/x86/kernel/stacktrace.c       |   2 +-
 include/asm-generic/unwind_user.h  |   4 +
 include/linux/unwind_user.h        |   9 --
 include/linux/unwind_user_types.h  |  25 ++++--
 kernel/unwind/user.c               | 139 +++++++++++++++++------------
 6 files changed, 110 insertions(+), 75 deletions(-)
arch/x86/include/asm/unwind_user.h +3 -3
index 19634a73612d..1b32491f28a5 100644
@@ -10,6 +10,8 @@
 	.fp_off		= (s32)sizeof(long) * -2,				\
 	.use_fp		= true,
 
+#define unwind_compat_mode(regs) (!user_64bit_mode(regs))
+
 #ifdef CONFIG_IA32_EMULATION
 
 #define ARCH_INIT_USER_COMPAT_FP_FRAME						\
@@ -18,14 +20,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 (!unwind_compat_mode(task_pt_regs(current)))
 		return;
 
 	state->ip += state->arch.cs_base;
arch/x86/kernel/stacktrace.c +1 -1
index 8ef9d8c71df9..3e970162fc0f 100644
@@ -137,7 +137,7 @@ void arch_unwind_user_init(struct unwind_user_state *state,
 {
 	unsigned long cs_base, ss_base;
 
-	if (state->type != UNWIND_USER_TYPE_COMPAT_FP)
+	if (!unwind_compat_mode(regs))
 		return;
 
 	cs_base = insn_get_seg_base(regs, INAT_SEG_REG_CS);
include/asm-generic/unwind_user.h +4 -0
index b8882b909944..f9d7603670cd 100644
@@ -2,4 +2,8 @@
 #ifndef _ASM_GENERIC_UNWIND_USER_H
 #define _ASM_GENERIC_UNWIND_USER_H
 
+#ifndef unwind_compat_mode
+#define unwind_compat_mode(regs)	false
+#endif
+
 #endif /* _ASM_GENERIC_UNWIND_USER_H */
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..41ea44c5ce5c 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 +82 -57

196 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

RFC PATCH v2 2/3 unwind: Export unwind_user symbol to GPL modules

In reply to Mathieu Desnoyers

Allow the unwind_user symbol to be used by GPL modules, for instance LTTng.

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]>
---
 kernel/unwind/user.c | 1 +
 1 file changed, 1 insertion(+)
kernel/unwind/user.c +1 -0
index 18d83efc876e..7d7dfb8178d0 100644
@@ -189,3 +189,4 @@ int unwind_user(struct unwind_stacktrace *trace, unsigned int max_entries)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(unwind_user);
Signature · 1 line
2.43.0
#3 lore raw

RFC PATCH v2 3/3 unwind deferred: Introduce unwind_user_trace_cached

In reply to Mathieu Desnoyers

Introduce unwind_user_trace_cached which copies the stack trace if it was previously stored into the cache since the last reset of the cache.

The expected use-case is sampling a stack trace from a faultable context at system call entry (coping it into the cache), and then copying the stack trace from the cache from non-faultable context to a ring buffer.

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]>
---
 include/linux/unwind_deferred.h |  2 ++
 kernel/unwind/deferred.c        | 31 +++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)
include/linux/unwind_deferred.h +2 -0
index a9d5b100d6b2..ddc985ba982a 100644
@@ -34,6 +34,7 @@ void unwind_task_init(struct task_struct *task);
 void unwind_task_free(struct task_struct *task);
 
 int unwind_user_faultable(struct unwind_stacktrace *trace);
+int unwind_user_trace_cached(struct unwind_stacktrace *trace);
 
 int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func);
 int unwind_deferred_request(struct unwind_work *work, u64 *cookie);
@@ -67,6 +68,7 @@ static inline void unwind_task_init(struct task_struct *task) {}
 static inline void unwind_task_free(struct task_struct *task) {}
 
 static inline int unwind_user_faultable(struct unwind_stacktrace *trace) { return -ENOSYS; }
+static inline int unwind_user_trace_cached(struct unwind_stacktrace *trace) { return -ENOSYS; }
 static inline int unwind_deferred_init(struct unwind_work *work, unwind_callback_t func) { return -ENOSYS; }
 static inline int unwind_deferred_request(struct unwind_work *work, u64 *timestamp) { return -ENOSYS; }
 static inline void unwind_deferred_cancel(struct unwind_work *work) {}
kernel/unwind/deferred.c +31 -0
index 039e12700d49..02ab1d2afc21 100644
@@ -145,6 +145,37 @@ int unwind_user_faultable(struct unwind_stacktrace *trace)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(unwind_user_faultable);
+
+/**
+ * unwind_user_trace_cached - Copy user stack trace from cache
+ * @trace: The descriptor that will store the user stacktrace
+ *
+ * Copy user stack trace from cache if the cache was populated by
+ * unwind_user_faultable prior to this call. The returned trace
+ * entries are only valid until the cache is reset.
+ *
+ * Return: 0 on success and negative on error
+ *         On success @trace will contain the user space stacktrace
+ */
+int unwind_user_trace_cached(struct unwind_stacktrace *trace)
+{
+	struct unwind_task_info *info = &current->unwind_info;
+	struct unwind_cache *cache;
+
+	if (!current->mm)
+		return -EINVAL;
+
+	cache = info->cache;
+	if (!cache || !cache->nr_entries)
+		return -ENOENT;
+
+	trace->nr = cache->nr_entries;
+	trace->entries = cache->entries;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(unwind_user_trace_cached);
 
 static void process_unwind_deferred(struct task_struct *task)
 {
Signature · 1 line
2.43.0