[RFC PATCH v2 12/30] x86/unwind: Add Minimal DWARF CFI kernel unwinder
Jens Remus
jremus at linux.ibm.com
Fri Nov 27 07:43:34 PST 2026
Hello Ian,
some early feedback.
On 11/25/2026 7:15 AM, Ian Rogers wrote:
> Add CONFIG_UNWINDER_DWARF_CFI as a selectable x86-64 kernel stack
> unwinder in arch/x86/kernel/unwind_dwarf.c, implementing the asm/unwind.h
> and HAVE_RELIABLE_STACKTRACE interfaces over compiler-generated
> .eh_frame and linker-generated --eh-frame-hdr tables:
> - Build kernel C and Rust objects with asynchronous unwind tables and
> -fomit-frame-pointer while suppressing unwind tables in boot/startup
> and purgatory objects.
> - Place .eh_frame_hdr and .eh_frame inside RO_DATA via
> ARCH_RODATA_SECTIONS, index loadable module .eh_frame sections via
> dwarf_cfi_mod_alloc()/dwarf_cfi_mod_free_rcu(), and enable BPF
> exception stack walks in arch/x86/net/bpf_jit_comp.c.
> - Unwind across task, IRQ, exception, ftrace, fgraph, rethook, BPF, and
> call_on_stack() frames, while rejecting in-kernel pt_regs exception
> boundaries in arch_stack_walk_reliable().
>
> Signed-off-by: Ian Rogers <irogers at google.com>
> diff --git a/arch/x86/kernel/unwind_dwarf.c b/arch/x86/kernel/unwind_dwarf.c
> +/**
> + * unwind_next_frame() - Advance @state to the next outer x86-64 kernel frame
> + * @state: Current x86 unwind state.
> + *
> + * Evaluates Minimal DWARF CFI for @state->ip, handles top-of-stack task
> + * pt_regs, arch_rethook_trampoline entry, %rsp-verified ENCODE_FRAME_POINTER
> + * entry/exception pt_regs transitions, and call_on_stack() IRQ stack pivots.
> + *
> + * Return: true if a valid outer frame was unwound, or false when the bottom of
> + * the stack is reached or an error terminates the walk.
> + */
> +bool unwind_next_frame(struct unwind_state *state)
> +{
...
> + /*
> + * Unencoded entry assembly (pre-ENCODE_FRAME_POINTER or
> + * post-POP_REGS) or an unaligned/encoded %rbp cannot be
> + * unwound via a frame-pointer guess.
> + */
> + if (in_entry_text(state->ip) || (state->bp & (sizeof(long) - 1)))
> + goto err;
> +
> + /* Fallback to frame-pointer guess and mark unreliable */
> + row = bpf_fp_row;
> + state->error = true;
> + }
> +
Maybe add a comment that the UNDEFINED rule indicates that the outermost
frame has been reached?
/* RA undefined indicates outermost frame reached */
> + if (row.ra_rule == DWARF_CFI_RULE_UNDEFINED)
> + goto the_end;
> +
> + if (row.ra_rule != DWARF_CFI_RULE_OFFSET)
> + goto err;
Good: For RA rule only OFFSET is supported. SP and FP rules may need
checking (see below).
> +
> + switch (row.cfa_base) {
> + case DWARF_CFI_BASE_SP: {
> + unsigned long sp_base = state->sp;
> +
> + /*
> + * call_on_stack() stores the pre-switch task-stack %rsp at the
> + * top slot of the per-CPU IRQ stack (info.end - 1) and pivots
> + * %rsp there before invoking the IRQ/softirq handler.
> + */
> + if (state->stack_info.type == STACK_TYPE_IRQ &&
> + state->sp == (unsigned long)(state->stack_info.end - 1)) {
> + if (!deref_stack_reg(state, state->sp, &sp_base))
> + goto err;
> + }
> + cfa = sp_base + row.cfa_off;
> + break;
> + }
> + case DWARF_CFI_BASE_FP:
> + cfa = state->bp + row.cfa_off;
> + break;
> + case DWARF_CFI_BASE_FP_INDIRECT:
> + if (!deref_stack_reg(state, state->bp + row.cfa_off, &cfa))
> + goto err;
> + break;
> + case DWARF_CFI_BASE_REG:
> + if (!get_scratch_reg(state, row.cfa_reg, &cfa))
> + goto err;
> + cfa += row.cfa_off;
> + break;
> + default:
> + goto err;
> + }
> +
> + ip_p = cfa + row.ra_off;
> + if (!deref_stack_reg(state, ip_p, &state->ip))
> + goto err;
> +
> + state->ip = dwarf_recover_ret_addr(state, state->ip,
> + (unsigned long *)ip_p);
> + if (!state->ip || dwarf_is_rethook_trampoline(state->ip))
> + goto err;
> +#ifdef CONFIG_FUNCTION_GRAPH_TRACER
> + if (state->ip == (unsigned long)return_to_handler)
> + goto err;
> +#endif
> +
> + if (row.fp_rule == DWARF_CFI_RULE_OFFSET) {
> + if (!deref_stack_reg(state, cfa + row.fp_off, &state->bp))
> + goto err;
> + } else if (row.fp_rule == DWARF_CFI_RULE_FP_OFFSET) {
> + if (!deref_stack_reg(state, state->bp + row.fp_off, &state->bp))
> + goto err;
> + }
IIUC any other FP rule falls through and is implicitly handled as SAME.
Furthermore row.sp_rule is not checked anywhere, so that any SP rule is
explicitly handled as SP = CFA. The implementations for the other archs
are similar. Maybe I am missing something.
> +
> + state->sp = cfa;
> + state->regs = NULL;
> + state->prev_regs = NULL;
> + state->full_regs = false;
> + state->signal = row.signal;
> +
Regards,
Jens
--
Jens Remus
Linux on Z Development (D3303)
jremus at de.ibm.com / jremus at linux.ibm.com
IBM Deutschland Research & Development GmbH; Vorsitzender des Aufsichtsrats: Wolfgang Wendt; Geschäftsführung: David Faller; Sitz der Gesellschaft: Ehningen; Registergericht: Amtsgericht Stuttgart, HRB 243294
IBM Data Privacy Statement: https://www.ibm.com/privacy/
More information about the linux-riscv
mailing list