[RFC PATCH v1 05/11] x86/unwind: Add Minimal DWARF CFI kernel unwinder

bot+bpf-ci at kernel.org bot+bpf-ci at kernel.org
Sun Nov 22 16:56:41 PST 2026


> x86/unwind: Add Minimal DWARF CFI kernel unwinder

TL;DR:
- Reliable stacktrace may wrongly succeed on kernel pt_regs inside inline asm,
  so livepatch could miss a sleeping function
- Build may fail with KPROBES=n (rethook symbol)
- KMSAN false reports; ftrace skip counts drop frames

> diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
> --- a/arch/x86/Kconfig
> +++ b/arch/x86/Kconfig
> @@ -288,7 +288,7 @@ config X86
>  	select MMU_GATHER_MERGE_VMAS
>  	select HAVE_POSIX_CPU_TIMERS_TASK_WORK
>  	select HAVE_REGS_AND_STACK_ACCESS_API
> -	select HAVE_RELIABLE_STACKTRACE		if UNWINDER_ORC || STACK_VALIDATION
> +	select HAVE_RELIABLE_STACKTRACE		if UNWINDER_ORC || UNWINDER_DWARF_CFI || STACK_VALIDATION
>  	select HAVE_FUNCTION_ARG_ACCESS_API
>  	select HAVE_SETUP_PER_CPU_AREA
>  	select HAVE_SOFTIRQ_ON_OWN_STACK

Is HAVE_RELIABLE_STACKTRACE justified for UNWINDER_DWARF_CFI?

arch_stack_walk_reliable() only refuses kernel-mode pt_regs when frame
pointers are enabled:

    regs = unwind_get_entry_regs(&state, NULL);
    if (regs) {
        if (user_mode(regs))
            return 0;
        ...
        if (IS_ENABLED(CONFIG_FRAME_POINTER))
            return -EINVAL;
    }

With UNWINDER_DWARF_CFI, FRAME_POINTER is n, so the walk continues.
unwind_next_frame() then reaches the pt_regs through the encoded %rbp, sets
state->ip = regs->ip and state->sp = regs->sp, and evaluates the
compiler-generated .eh_frame row at regs->ip.

The compiler's .eh_frame only describes stack changes the compiler made
itself.  It knows nothing about push/pop inside inline asm, for example
native_save_fl(), which arch_local_irq_save() inlines into every
spin_lock_irqsave():

    asm volatile("# __raw_save_flags\n\t"
                 "pushf ; pop %0"

The paravirt save_fl alternative, KVM's fastop() and iret_to_self() do the
same.  ORC handles these because objtool decodes the instructions, but no
objtool stack validation runs here (STACK_VALIDATION depends on
UNWINDER_FRAME_POINTER).

Consider CONFIG_PREEMPTION=y, IRQs on, preempt_count 0, and a task in
__raw_spin_lock_irqsave().  An IRQ arrives after pushf and before pop, and
irqentry_exit() calls preempt_schedule_irq(), so the task sleeps with
regs->ip at the pop.  Later:

klp_try_switch_task()
  klp_check_stack()
    stack_trace_save_tsk_reliable()

walks the task.  At regs->ip the CFA computed from %rsp is 8 bytes too low,
so the return address is read from the wrong slot.

If that slot holds a pushed flags value, the walk fails safely.  If it holds
a kernel text address, such as a callee-saved function pointer, the walk
records a bogus frame and drops the real caller.  If it then resynchronises
and reaches task_pt_regs(), it returns 0, and livepatch treats a function
the task is sleeping in as absent.

Documentation/livepatch/reliable-stacktrace.rst section 4.3 says:
"Architectures which cannot identify when it is reliable to unwind such
cases ... must reject unwinding across exception boundaries."

Should arch_stack_walk_reliable() also return -EINVAL on kernel-mode regs
for UNWINDER_DWARF_CFI, or should the unwinder otherwise prove that
regs->ip is not inside an inline-asm stack adjustment?

[ ... ]

> diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug
> --- a/arch/x86/Kconfig.debug
> +++ b/arch/x86/Kconfig.debug
> @@ -246,6 +246,19 @@ config UNWINDER_ORC
>  	  Enabling this option will increase the kernel's runtime memory usage
>  	  by roughly 2-4MB, depending on your kernel config.
>  
> +config UNWINDER_DWARF_CFI
> +	bool "Minimal DWARF CFI unwinder"
> +	depends on X86_64
> +	select UNWIND_DWARF_CFI
> +	select UNWIND_TABLES

The new unwinder skips frames the same way unwind_orc.c does, and the
kernel is built without frame pointers, so -fno-optimize-sibling-calls is
dropped just as under ORC.  But the tracing code picks its skip counts by
testing CONFIG_UNWINDER_ORC alone:

kernel/trace/trace.c:__ftrace_trace_stack() {
#ifndef CONFIG_UNWINDER_ORC
    if (!regs)
        skip++;
#endif
}

trace_dump_stack() has the same extra skip++, and:

kernel/trace/trace_functions.c: STACK_SKIP is 3 instead of 2 and
FTRACE_STACK_SKIP is 5 instead of 3
kernel/trace/trace_events_trigger.c: STACK_SKIP is 4 instead of 2

These values were calibrated separately for ORC and frame pointers (commit
2ee5b92a2598).  With UNWINDER_DWARF_CFI=y, the func_stack_trace option, the
stacktrace event trigger, the stacktrace probe command and
trace_dump_stack() take the frame-pointer branch and would drop one or two
real caller frames from their output.

Should these tests be CONFIG_UNWINDER_ORC || CONFIG_UNWINDER_DWARF_CFI, or
be keyed on !CONFIG_FRAME_POINTER?

[ ... ]

> diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
> --- a/arch/x86/kernel/Makefile
> +++ b/arch/x86/kernel/Makefile
> @@ -25,6 +26,7 @@ KASAN_SANITIZE_dumpstack.o				:= n
>  KASAN_SANITIZE_dumpstack_$(BITS).o			:= n
>  KASAN_SANITIZE_stacktrace.o				:= n
>  KASAN_SANITIZE_paravirt.o				:= n
> +KASAN_SANITIZE_unwind_dwarf.o				:= n
>  
>  # With some compiler versions the generated code results in boot hangs, caused
>  # by several compilation units. To be safe, disable all instrumentation.
> @@ -41,6 +43,7 @@ KCOV_INSTRUMENT_stacktrace.o				:= n
>  KCOV_INSTRUMENT_dumpstack.o				:= n
>  KCOV_INSTRUMENT_dumpstack_$(BITS).o			:= n
>  KCOV_INSTRUMENT_unwind_orc.o				:= n
> +KCOV_INSTRUMENT_unwind_dwarf.o				:= n
>  KCOV_INSTRUMENT_unwind_frame.o				:= n
>  KCOV_INSTRUMENT_unwind_guess.o				:= n

With CONFIG_KMSAN=y, will this report false "uninit-value in
unwind_next_frame" on ordinary stack walks?

KASAN and KCOV are turned off for unwind_dwarf.o, but KMSAN stays on, while
the interpreter it calls is built with no KMSAN instrumentation
(kernel/unwind/Makefile has KMSAN_SANITIZE_dwarf_cfi.o := n).

In unwind_next_frame():

    struct dwarf_cfi_row row;
    ...
    err = dwarf_cfi_find(lookup_ip, &row);
    ...
    if (row.ra_rule == DWARF_CFI_RULE_UNDEFINED)

KMSAN poisons row on entry.  dwarf_cfi_find() fills it through
dwarf_cfi_unwind_eh_frame_hdr() or dwarf_cfi_mod_eval(), both out-of-line in
the uninstrumented dwarf_cfi.c, so their stores never clear the shadow.  The
later row.ra_rule, row.cfa_base and row.fp_rule tests then read poisoned
memory.

KMSAN depends only on HAVE_ARCH_KMSAN, clang and a few other options, and
UNWINDER_DWARF_CFI depends only on X86_64, so nothing prevents this
combination.  INIT_STACK_ALL_ZERO and INIT_STACK_ALL_PATTERN depend on
!KMSAN, so row is not pre-initialised.

Callers that would report include stack_trace_save() -> arch_stack_walk(),
arch_stack_walk_reliable(), perf_callchain_kernel() and __get_wchan().

unwind_frame.c marks unwind_next_frame() and update_stack_state()
__no_kmsan_checks for a related reason (commit 37ad4ee83642).

Would marking unwind_next_frame() __no_kmsan_checks, calling
kmsan_unpoison_memory(&row, sizeof(row)) after a successful
dwarf_cfi_find(), or building dwarf_cfi.o with
KMSAN_ENABLE_CHECKS_dwarf_cfi.o := n instead be needed here?  Adding
KMSAN_SANITIZE_unwind_dwarf.o := n alone would leave the callers' struct
unwind_state poisoned.

[ ... ]

> diff --git a/arch/x86/kernel/unwind_dwarf.c b/arch/x86/kernel/unwind_dwarf.c
> --- /dev/null
> +++ b/arch/x86/kernel/unwind_dwarf.c

[ ... ]

> +	lookup_ip = state->signal ? state->ip : state->ip - 1;
> +	err = dwarf_cfi_find(lookup_ip, &row);
> +	if (err) {
> +		/*
> +		 * Handle an interrupt/unwind starting right at the entry of
> +		 * arch_rethook_trampoline() before pushq $arch_rethook_trampoline.
> +		 */
> +		if (state->signal && is_rethook_trampoline(state->ip)) {
> +			state->ip = dwarf_recover_ret_addr(state, state->ip,
> +					(unsigned long *)(state->sp - sizeof(long)));
> +			if (!state->ip || is_rethook_trampoline(state->ip))
> +				goto err;

Can this fail to link when CONFIG_UNWINDER_DWARF_CFI=y and CONFIG_KPROBES=n?

unwind_next_frame() calls is_rethook_trampoline() unconditionally in
several places, and nothing guards those calls with CONFIG_RETHOOK.
is_rethook_trampoline() in include/linux/rethook.h is an unconditional
static inline:

    static inline bool is_rethook_trampoline(unsigned long addr)
    {
        return addr == (unsigned long)dereference_symbol_descriptor(arch_rethook_trampoline);
    }

On x86, arch_rethook_trampoline is defined only in
arch/x86/kernel/rethook.c, which is built only with
obj-$(CONFIG_RETHOOK).  RETHOOK is a hidden bool whose only selector is
KRETPROBE_ON_RETHOOK, which depends on KRETPROBES and therefore KPROBES.
UNWINDER_DWARF_CFI depends only on X86_64 and selects neither, so KPROBES=n
is a valid configuration.  There unwind_dwarf.o references
arch_rethook_trampoline, no object defines it, and vmlinux fails with an
undefined reference.

Other users are guarded: unwind_recover_rethook() in
arch/x86/include/asm/unwind.h and unwind_recover_ret_addr() in
arch/s390/include/asm/unwind.h use #ifdef CONFIG_RETHOOK, and this file
already guards the return_to_handler checks with
#ifdef CONFIG_FUNCTION_GRAPH_TRACER, but the rethook checks next to them
have no matching guard.

Should the rethook branch and comparisons be wrapped in
IS_ENABLED(CONFIG_RETHOOK), or should is_rethook_trampoline() get a stub
returning false when !CONFIG_RETHOOK?  No later patch in the series touches
unwind_dwarf.c, rethook.h or the x86 Kconfig.

[ ... ]

> +	while (!unwind_done(state) &&
> +	       (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
> +		state->sp <= (unsigned long)first_frame))
> +		unwind_next_frame(state);

This is the same skip loop as in unwind_orc.c, and a DWARF build has the
same code generation as an ORC build: CONFIG_FRAME_POINTER is off, so the
top-level Makefile does not add -fno-omit-frame-pointer
-fno-optimize-sibling-calls and sibling calls stay enabled.

The ftrace stack-trace skip counts are chosen only by CONFIG_UNWINDER_ORC
(kernel/trace/trace.c __ftrace_trace_stack() and trace_dump_stack(),
trace_functions.c STACK_SKIP and FTRACE_STACK_SKIP, trace_events_trigger.c
STACK_SKIP), so with CONFIG_UNWINDER_DWARF_CFI=y each takes the
frame-pointer branch.  Going by the ORC calibration, that would drop about
two frames for the function tracer's stacktrace option and trace_dump_stack(),
and about three for the stacktrace event trigger and func-probe command.
The exact count was inferred from the ORC calibration and not measured.

This patch already updates the ORC-keyed check in
arch/x86/net/bpf_jit_comp.c to
IS_ENABLED(CONFIG_UNWINDER_ORC) || IS_ENABLED(CONFIG_UNWINDER_DWARF_CFI),
but not the ones in kernel/trace, and no later commit in the series touches
kernel/trace.

Should these conditionals also cover CONFIG_UNWINDER_DWARF_CFI, or be keyed
on whether frame pointers are used rather than on one unwinder?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/45087188604


More information about the linux-riscv mailing list