mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: irogers@google.com,linux-kernel@vger.kernel.org,linux-trace-kernel@vger.kernel.org,linux-arch@vger.kernel.org
Cc: jpoimboe@kernel.org,rostedt@goodmis.org,peterz@infradead.org,mingo@redhat.com,tglx@kernel.org,bp@alien8.de,dave.hansen@linux.intel.com,x86@kernel.org,catalin.marinas@arm.com,will@kernel.org,mark.rutland@arm.com,linux-arm-kernel@lists.infradead.org,pjw@kernel.org,palmer@dabbelt.com,aou@eecs.berkeley.edu,alex@ghiti.fr,linux-riscv@lists.infradead.org,acme@kernel.org,namhyung@kernel.org,linux-perf-users@vger.kernel.org,mhiramat@kernel.org,ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,bpf@vger.kernel.org,nathan@kernel.org,nsc@kernel.org,linux-kbuild@vger.kernel.org,ojeda@kernel.org,rust-for-linux@vger.kernel.org,ardb@kernel.org,linux-efi@vger.kernel.org,maz@kernel.org,oupton@kernel.org,kvmarm@lists.linux.dev,shuah@kernel.org,linux-kselftest@vger.kernel.org,indu.bhagat@oracle.com,jremus@linux.ibm.com,wnliu@google.com,dylanbhatch@google.com,irogers@google.com,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mas
	on@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [RFC PATCH v1 05/11] x86/unwind: Add Minimal DWARF CFI kernel unwinder
Date: Mon, 23 Nov 2026 00:56:41 +0000 (UTC)	[thread overview]
Message-ID: <0b034cf88655a8541a880e294e84f1067a0e1b83681ae3ca3e5d9d3b2a4f86d8@mail.kernel.org> (raw)
In-Reply-To: <20261122235757.3456048-6-irogers@google.com>

[-- Attachment #1: Type: text/plain, Size: 11005 bytes --]

> 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

  reply	other threads:[~2026-11-23  0:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22 23:57 [RFC PATCH v1 00/11] unwind: Minimal DWARF CFI stack unwinding for x86, arm64, and riscv Ian Rogers
2026-11-22 23:57 ` [RFC PATCH v1 01/11] unwind: Add Minimal DWARF CFI bytecode interpreter and KUnit tests Ian Rogers
2026-11-23  0:40   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 02/11] unwind_user: Support Minimal DWARF CFI user-space unwinding Ian Rogers
2026-11-23  0:39   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 03/11] scripts, unwind: Add build-time .eh_frame minifier Ian Rogers
2026-11-23  0:40   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 04/11] objtool/ibt: Ignore .eh_frame relocations during ENDBR validation Ian Rogers
2026-11-22 23:57 ` [RFC PATCH v1 05/11] x86/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-23  0:56   ` bot+bpf-ci [this message]
2026-11-22 23:57 ` [RFC PATCH v1 06/11] arm64: unwind_user: Support Minimal DWARF CFI and FP user unwinding Ian Rogers
2026-11-22 23:57 ` [RFC PATCH v1 07/11] arm64/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-23  0:56   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 08/11] riscv: unwind_user: Support Minimal DWARF CFI and FP user unwinding Ian Rogers
2026-11-23  0:39   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 09/11] riscv/unwind: Add Minimal DWARF CFI kernel unwinder Ian Rogers
2026-11-23  0:56   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 10/11] selftests/perf_events: Add Minimal DWARF CFI deferred user unwind test Ian Rogers
2026-11-23  0:40   ` bot+bpf-ci
2026-11-22 23:57 ` [RFC PATCH v1 11/11] scripts: Add eh_frame_min.py .eh_frame coverage and size analyzer Ian Rogers
2026-11-23  0:39   ` bot+bpf-ci

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0b034cf88655a8541a880e294e84f1067a0e1b83681ae3ca3e5d9d3b2a4f86d8@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=acme@kernel.org \
    --cc=alex@ghiti.fr \
    --cc=andrii@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ardb@kernel.org \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dylanbhatch@google.com \
    --cc=eddyz87@gmail.com \
    --cc=indu.bhagat@oracle.com \
    --cc=irogers@google.com \
    --cc=jpoimboe@kernel.org \
    --cc=jremus@linux.ibm.com \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=martin.lau@kernel.org \
    --cc=maz@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=nathan@kernel.org \
    --cc=nsc@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=oupton@kernel.org \
    --cc=palmer@dabbelt.com \
    --cc=peterz@infradead.org \
    --cc=pjw@kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=tglx@kernel.org \
    --cc=will@kernel.org \
    --cc=wnliu@google.com \
    --cc=x86@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®