riscv: Add reliable stack unwinding for livepatch
Subsystems (7)
v6 cover riscv: Add reliable stack unwinding for livepatch
Hi,
This is v6 of the RISC-V reliable stack unwinding series for livepatch. Wang Han has left our team, so I am taking over this series from now on.
The series adds explicit frame-record metadata, preserves the frame pointer through dynamic ftrace, and replaces the open-coded stack walker with a conservative frame-pointer based unwinder. It then enables the RISC-V livepatch capability and adds the syscall-wrapper selftest prefix.
Changes since v5:
* Rebase onto the current riscv/for-next; no functional changes.
* Fold in the tags collected from the v5 thread:
- Reviewed-by: Shuai Xue on 2/7, 3/7, 4/7 and 6/7
- Acked-by: Miroslav Benes on 7/7
* Add my sign-off as the new sender.
Since the patches are otherwise identical to v5, the v5 validation results still apply. For reference, from the v5 cover letter:
* Built Linux 7.2.0-rc6 with riscv64-unknown-linux-gnu-gcc 15.2.0 and binutils 2.46.
* Standard livepatch configuration built successfully, including Image, vmlinux, modules, compile_commands.json and GDB scripts.
* CONFIG_IRQ_STACKS=n variant linked successfully with GCC.
* QEMU 8.2.2, 1 CPU: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* QEMU 8.2.2, 4 CPUs: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* No BUG, WARNING, Oops, panic, unreliable stack or Call Trace found by the validation log scans. Expected negative livepatch errors are part of the selftests.
v5 was posted in early August and has been quiet since then. I would appreciate it if the series could be considered for the upcoming merge window. Happy to address any remaining feedback.
Base:
riscv/for-next 77ae27fd98f3 ("Merge tag 'printk-for-7.3' of git://git.kernel.org/pub/scm/linux/kernel/git/printk/linux")
Previous versions:
v5: https://lore.kernel.org/r/[email protected]
v4 RESEND: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
Wang Han (7):
riscv: stacktrace: Add frame record metadata
riscv: stacktrace: disable KASAN and KCOV instrumentation for
stacktrace.o
riscv: ftrace: always preserve s0 in dynamic ftrace register frame
riscv: stacktrace: introduce stack-bound tracking helpers
riscv: stacktrace: switch to frame-pointer based unwinder
riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH selftests/livepatch: Add RISC-V syscall wrapper prefix
arch/riscv/Kconfig | 4 + arch/riscv/include/asm/ptrace.h | 9 + arch/riscv/include/asm/stacktrace.h | 67 ++- arch/riscv/include/asm/stacktrace/common.h | 159 +++++ arch/riscv/include/asm/stacktrace/frame.h | 53 ++ arch/riscv/kernel/Makefile | 6 + arch/riscv/kernel/asm-offsets.c | 6 + arch/riscv/kernel/entry.S | 39 +- arch/riscv/kernel/ftrace.c | 6 +- arch/riscv/kernel/head.S | 23 + arch/riscv/kernel/mcount-dyn.S | 4 - arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/process.c | 33 +- arch/riscv/kernel/stacktrace.c | 561 +++++++++++++++--- .../livepatch/test_modules/test_klp_syscall.c | 2 + 15 files changed, 868 insertions(+), 106 deletions(-) create mode 100644 arch/riscv/include/asm/stacktrace/common.h create mode 100644 arch/riscv/include/asm/stacktrace/frame.h
base-commit: 77ae27fd98f3b9ab376239051ec7f9ee34c2c072
Signature · 1 line
2.39.3
PATCH v6 1/7 riscv: stacktrace: Add frame record metadata
From: Wang Han <[email protected]>
Reliable frame-pointer unwinding needs an explicit way to identify exception boundaries and the final entry frame. The existing unwinder infers those boundaries from return addresses, which is too loose for a future reliable unwinder.
Add a small metadata frame record to pt_regs and initialize it on exception entry, kernel stack overflow, kernel thread fork, user fork, and early idle task setup. The record uses a zero {fp, ra} sentinel plus a type field so a later unwinder can distinguish a final user-to-kernel boundary from a nested kernel pt_regs boundary.
This follows the arm64 metadata frame-record model, adapted to the RISC-V {fp, ra} frame record convention.
The metadata is established at the RISC-V entry boundaries that need an explicit unwind marker:
* exception entry clears the metadata {fp, ra} pair and uses SPP (or MPP in M-mode) to record whether the pt_regs frame is the final user-to-kernel boundary or a nested kernel boundary;
* the kernel stack overflow path builds a nested pt_regs metadata record on the overflow stack so an unwinder can resume from the pre-overflow s0 saved in PT_S0;
* _start_kernel builds the init task's final metadata record, while the secondary CPU path sets up s0 before smp_callin() so idle-task unwinding does not inherit an undefined caller frame;
* copy_thread creates matching final metadata records for new kernel and user tasks, and keeps s0 available for the frame-pointer chain.
Keep the embedded metadata-record field offsets distinct from the s0-relative STACKFRAME_* offsets used by call_on_irq_stack(), because the latter describe a frame record relative to s0 rather than to the record base.
These changes keep s0 reserved for the frame-pointer chain at task and exception boundaries.
--- arch/riscv/include/asm/ptrace.h | 9 ++++ arch/riscv/include/asm/stacktrace/frame.h | 53 +++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 6 +++ arch/riscv/kernel/entry.S | 39 ++++++++++++++++- arch/riscv/kernel/head.S | 23 ++++++++++ arch/riscv/kernel/process.c | 33 +++++++++++++- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 arch/riscv/include/asm/stacktrace/frame.h
arch/riscv/include/asm/ptrace.h +9 -0
@@ -8,6 +8,7 @@#include <uapi/asm/ptrace.h> #include <asm/csr.h> +#include <asm/stacktrace/frame.h> #include <linux/compiler.h> #ifndef __ASSEMBLER__
@@ -53,6 +54,14 @@ struct pt_regs {unsigned long cause;/* a0 value before the syscall */ unsigned long orig_a0; + +/* +* This frame record is entirely zeroed on exception entry, allowing the +* unwinder to identify exception boundaries. The type field encodes +* whether the exception was taken from user (FINAL) or kernel (PT_REGS) +* mode. +*/ +struct frame_record_meta stackframe; };#define PTRACE_SYSEMU 0x1f
arch/riscv/include/asm/stacktrace/frame.h new file +53 -0
53 lines not on this page · raw message
arch/riscv/kernel/asm-offsets.c +6 -0
@@ -131,6 +131,9 @@ void asm_offsets(void)OFFSET (PT_BADADDR , pt_regs, badaddr);OFFSET (PT_CAUSE , pt_regs, cause); +DEFINE (S_STACKFRAME , offsetof(struct pt_regs , stackframe)); + DEFINE(S_STACKFRAME_TYPE, offsetof(struct pt_regs, stackframe.type)); +OFFSET (SUSPEND_CONTEXT_REGS , suspend_context, regs);OFFSET (HIBERN_PBE_ADDR , pbe, address);
@@ -503,6 +506,9 @@ void asm_offsets(void)DEFINE (STACKFRAME_SIZE_ON_STACK ,ALIGN (sizeof (struct stackframe ),STACK_ALIGN ));DEFINE (STACKFRAME_FP , offsetof(struct stackframe , fp) -sizeof (struct stackframe ));DEFINE (STACKFRAME_RA , offsetof(struct stackframe , ra) -sizeof (struct stackframe )); +DEFINE (STACKFRAME_RECORD_SIZE ,sizeof (struct frame_record )); +OFFSET (FRAME_RECORD_FP , frame_record, fp); +OFFSET (FRAME_RECORD_RA , frame_record, ra);#ifdef CONFIG_FUNCTION_TRACER DEFINE (FTRACE_OPS_FUNC , offsetof(struct ftrace_ops , func));#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
arch/riscv/kernel/entry.S +37 -2
@@ -11,6 +11,7 @@ #include <asm/asm.h> #include <asm/csr.h> #include <asm/scs.h> +#include <asm/stacktrace/frame.h> #include <asm/unistd.h> #include <asm/page.h> #include <asm/thread_info.h>
@@ -198,6 +199,27 @@ SYM_CODE_START(handle_exception) REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + /* + * Create a metadata frame record. The unwinder will use this to + * identify and unwind exception boundaries. + */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) /* stackframe.record.fp = 0 */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) /* stackframe.record.ra = 0 */ +#ifdef CONFIG_RISCV_M_MODE + li t0, SR_MPP + and t0, s1, t0 +#else + andi t0, s1, SR_SPP +#endif + bnez t0, 1f + li t0, FRAME_META_TYPE_FINAL + j 2f +1: + li t0, FRAME_META_TYPE_PT_REGS +2: + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + /* * Set the scratch register to 0, so that if a recursive exception * occurs, the exception vector knows it came from the kernel
@@ -354,6 +376,19 @@ SYM_CODE_START_LOCAL(handle_kernel_stack_overflow) REG_S s3, PT_BADADDR(sp) REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + + /* + * Create a metadata frame record for the overflow pt_regs. The + * overflow path is entered from kernel context, so this is a nested + * pt_regs boundary and the unwinder can resume from the pre-overflow + * frame pointer saved in PT_S0. + */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) + li t0, FRAME_META_TYPE_PT_REGS + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + move a0, sp tail handle_bad_stack SYM_CODE_END(handle_kernel_stack_overflow)
@@ -362,8 +397,8 @@ ASM_NOKPROBE(handle_kernel_stack_overflow) SYM_CODE_START(ret_from_fork_kernel_asm) call schedule_tail - move a0, s1 /* fn_arg */ - move a1, s0 /* fn */ + move a0, s3 /* fn_arg */ + move a1, s2 /* fn */ move a2, sp /* pt_regs */ call ret_from_fork_kernel j ret_from_exception
arch/riscv/kernel/head.S +23 -0
@@ -14,6 +14,7 @@ #include <asm/hwcap.h> #include <asm/image.h> #include <asm/scs.h> +#include <asm/stacktrace/frame.h> #include <asm/usercfi.h> #include "efi-header.S"
@@ -177,6 +178,14 @@ secondary_start_sbi: REG_S a0, (a1) 1: #endif + + /* + * Set up the frame pointer for the secondary idle task so reliable + * stack unwinding terminates at the metadata frame in task_pt_regs(). + * Without this, the first frame records can inherit an undefined caller + * fp and unwind past smp_callin() into .Lsecondary_park. + */ + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE scs_load_current call smp_callin #endif /* CONFIG_SMP */
@@ -305,6 +314,20 @@ SYM_CODE_START(_start_kernel) la tp, init_task la sp, init_thread_union + THREAD_SIZE addi sp, sp, -PT_SIZE_ON_STACK + + /* + * Set up a metadata frame record for the init task so that + * the unwinder can identify the outermost frame by its + * {fp, ra} = {0, 0} sentinel at the bottom of pt_regs. + * fp/s0 points above the metadata record (RISC-V + * convention). + */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) + li t0, FRAME_META_TYPE_FINAL + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + #if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI) li a7, SBI_EXT_FWFT li a6, SBI_EXT_FWFT_SET
arch/riscv/kernel/process.c +31 -2
@@ -258,8 +258,23 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)/* Supervisor/Machine, irqs on: */ childregs->status =SR_PP |SR_PIE ; - p->thread.s[0 ] = (unsigned long )args->fn; - p->thread.s[1 ] = (unsigned long )args->fn_arg; +/* +* Set up a metadata frame record at the bottom of the +* stack for the unwinder. Use FRAME_META_TYPE_FINAL +* since this is the outermost kernel entry for the new +* task. The frame_record::{fp,ra} are already zero from +* memset(). +* +* fp/s0 points above the metadata record (RISC-V +* convention). fn and fn_arg are passed via s2/s3, +* keeping s0 available for the frame pointer chain. +*/ + childregs->stackframe.type =FRAME_META_TYPE_FINAL ; + + p->thread.s[0 ] = (unsigned long )(&childregs->stackframe) + +sizeof (struct frame_record ); + p->thread.s[2 ] = (unsigned long )args->fn; + p->thread.s[3 ] = (unsigned long )args->fn_arg; p->thread.ra = (unsigned long )ret_from_fork_kernel_asm; }else {/* allocate new shadow stack if needed. In case of CLONE_VM we have to */
@@ -278,6 +293,20 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)if (clone_flags &CLONE_SETTLS ) childregs->tp = tls; childregs->a0 =0 ;/* Return value of fork() */ + +/* +* Set up the unwind boundary: ensure the metadata +* frame record has its {fp,ra} sentinel zeroed and +* point fp/s0 above the metadata record. Mark it as +* FINAL since this is the outermost kernel entry for +* the new task. +*/ + childregs->stackframe.record.fp =0 ; + childregs->stackframe.record.ra =0 ; + childregs->stackframe.type =FRAME_META_TYPE_FINAL ; + p->thread.s[0 ] = (unsigned long )(&childregs->stackframe) + +sizeof (struct frame_record ); + p->thread.ra = (unsigned long )ret_from_fork_user_asm; } p->thread.riscv_v_flags =0 ;
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 1/7] riscv: stacktrace: Add frame record metadata
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
From: Wang Han <[email protected]>
Reliable frame-pointer unwinding needs an explicit way to identify exception boundaries and the final entry frame. The existing unwinder infers those boundaries from return addresses, which is too loose for a future reliable unwinder.
Add a small metadata frame record to
pt_regsand initialize it on exception entry, kernel stack overflow, kernel thread fork, user fork, and early idle task setup. The record uses a zero {fp, ra} sentinel plus a type field so a later unwinder can distinguish a final user-to-kernel boundary from a nested kernelpt_regsboundary.
Quoted 8 lines
This follows the arm64 metadata frame-record model, adapted to the RISC-V {fp, ra} frame record convention.
[ ... ]Reviewed-by Shuai Xue <[email protected]>Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v6 2/7 riscv: stacktrace: disable KASAN and KCOV instrumentation for stacktrace.o
In reply to Shuai XueFrom: Wang Han <[email protected]>
KASAN records stack traces for every alloc/free, which means it walks the unwinder very frequently. Instrumenting the stack trace collection code itself adds substantial overhead and makes the traces themselves noisier.
KCOV instruments every basic-block edge. The unwinder is a hot path, especially with KASAN enabled, so KCOV instrumentation has the same kind of cost and noise problem here.
Mark stacktrace.o as not KASAN- or KCOV-instrumented, matching the x86 treatment of its stack unwinding code. RISC-V keeps the relevant unwinder code in stacktrace.o, so a single translation-unit annotation covers the equivalent scope. This is a prerequisite preference for the upcoming reliable unwinder, but the change is valid on its own.
--- arch/riscv/kernel/Makefile | 6 ++++++ 1 file changed, 6 insertions(+)
arch/riscv/kernel/Makefile +6 -0
@@ -44,6 +44,12 @@ CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi_ecall.o = $(CC_FLAGS_FTRACE) endif +# When KASAN is enabled, a stack trace is recorded for every alloc/free, which +# can significantly impact performance. Avoid instrumenting the stack trace +# collection code to minimize this impact. +KASAN_SANITIZE_stacktrace.o := n +KCOV_INSTRUMENT_stacktrace.o := n + always-$(KBUILD_BUILTIN) += vmlinux.lds obj-y += head.o
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 2/7] riscv: stacktrace: disable KASAN and KCOV instrumentation for stacktrace.o
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
From: Wang Han <[email protected]>
KASAN records stack traces for every alloc/free, which means it walks the unwinder very frequently. Instrumenting the stack trace collection code itself adds substantial overhead and makes the traces themselves noisier.
KCOV instruments every basic-block edge. The unwinder is a hot path, especially with KASAN enabled, so KCOV instrumentation has the same kind of cost and noise problem here.
Mark stacktrace.o as not KASAN- or KCOV-instrumented, matching the x86 treatment of its stack unwinding code. RISC-V keeps the relevant unwinder code in stacktrace.o, so a single translation-unit annotation covers the equivalent scope. This is a prerequisite preference for the upcoming reliable unwinder, but the change is valid on its own.
Quoted 4 lines
Reviewed-by Shuai Xue <[email protected]>Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v6 3/7 riscv: ftrace: always preserve s0 in dynamic ftrace register frame
In reply to Shuai XueFrom: Wang Han <[email protected]>
struct __arch_ftrace_regs declares s0 unconditionally, and both ftrace_regs_get_frame_pointer() and ftrace_partial_regs() read it unconditionally. But the SAVE_ABI_REGS / RESTORE_ABI_REGS macros in mcount-dyn.S only stored s0 under HAVE_FUNCTION_GRAPH_FP_TEST (CONFIG_FUNCTION_GRAPH_TRACER && CONFIG_FRAME_POINTER). With CONFIG_FRAME_POINTER=n the slot held whatever was on the stack before, so any callback going through ftrace_partial_regs() saw a garbage regs->s0. RISC-V kernels default to FRAME_POINTER=y, which is why this has not bitten in practice.
Save and restore s0 unconditionally in the dynamic ftrace ABI register frame. This fixes the latent garbage-s0 case, brings the dynamic ftrace path in line with the static _mcount path (mcount.S SAVE_ABI_STATE already saves s0 unconditionally), and matches the frame layout already documented in the comment above SAVE_ABI_REGS. It is also a prerequisite for the upcoming reliable unwinder, which reads ftrace_regs_get_frame_pointer(fregs) directly.
The cost is one extra REG_S/REG_L pair per traced call, negligible compared to the overall ftrace cost; the existing FREGS_SIZE_ON_STACK already reserved the slot, so no extra stack space is used.
--- arch/riscv/kernel/mcount-dyn.S | 4 ---- 1 file changed, 4 deletions(-)
arch/riscv/kernel/mcount-dyn.S +0 -4
@@ -85,9 +85,7 @@ addi sp, sp, -FREGS_SIZE_ON_STACK REG_S t0, FREGS_EPC(sp) REG_S x1, FREGS_RA(sp) -#ifdef HAVE_FUNCTION_GRAPH_FP_TEST REG_S x8, FREGS_S0(sp) -#endif REG_S x6, FREGS_T1(sp) #ifdef CONFIG_CC_IS_CLANG REG_S x7, FREGS_T2(sp)
@@ -113,9 +111,7 @@ .macro RESTORE_ABI_REGS REG_L t0, FREGS_EPC(sp) REG_L x1, FREGS_RA(sp) -#ifdef HAVE_FUNCTION_GRAPH_FP_TEST REG_L x8, FREGS_S0(sp) -#endif REG_L x6, FREGS_T1(sp) #ifdef CONFIG_CC_IS_CLANG REG_L x7, FREGS_T2(sp)
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 3/7] riscv: ftrace: always preserve s0 in dynamic ftrace register frame
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines
From: Wang Han <[email protected]>
struct
__arch_ftrace_regsdeclares s0 unconditionally, and bothftrace_regs_get_frame_pointer()andftrace_partial_regs()read it unconditionally. But theSAVE_ABI_REGS/RESTORE_ABI_REGSmacros inmcount-dyn.Sonly stored s0 underHAVE_FUNCTION_GRAPH_FP_TEST(CONFIG_FUNCTION_GRAPH_TRACER&&CONFIG_FRAME_POINTER). WithCONFIG_FRAME_POINTER=nthe slot held whatever was on the stack before, so any callback going throughftrace_partial_regs()saw a garbageregs->s0. RISC-V kernels default to FRAME_POINTER=y, which is why this has not bitten in practice.Save and restore s0 unconditionally in the dynamic ftrace ABI register frame. This fixes the latent garbage-s0 case, brings the dynamic ftrace path in line with the static
_mcountpath (mcount.SSAVE_ABI_STATEalready saves s0 unconditionally), and matches the frame layout already documented in the comment aboveSAVE_ABI_REGS. It is also a prerequisite for the upcoming reliable unwinder, which readsftrace_regs_get_frame_pointer(fregs)directly.
Quoted 8 lines
The cost is one extra REG_S/REG_L pair per traced call, negligible compared to the overall ftrace cost; the existing
FREGS_SIZE_ON_STACKalready reserved the slot, so no extra stack space is used.Reviewed-by Shuai Xue <[email protected]>Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v6 4/7 riscv: stacktrace: introduce stack-bound tracking helpers
In reply to Shuai XueFrom: Wang Han <[email protected]>
A reliable unwinder needs to validate that every frame record it reads is fully contained in a known kernel stack, and it needs to refuse to walk back into a stack it has already left. Add the building blocks for that:
* struct stack_info / struct unwind_state in a new asm/stacktrace/common.h, modelled on the arm64 reference implementation.
* stackinfo_get_irq() / stackinfo_get_task() / stackinfo_get_overflow() plus the corresponding on_*_stack() predicates in asm/stacktrace.h, so callers can ask "is this object on stack X?" by stack kind rather than open-coded address arithmetic.
* unwind_init_common(), unwind_find_stack() and unwind_consume_stack() helpers that enforce the forward-progress-only invariant required for reliability.
No existing user is wired up to these helpers in this commit; the unwinder switch comes in a follow-up. The header changes leave on_thread_stack() with the same semantics as before, just expressed in terms of the new helpers.
--- arch/riscv/include/asm/stacktrace.h | 67 ++++++++- arch/riscv/include/asm/stacktrace/common.h | 159 +++++++++++++++++++++ 2 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/stacktrace/common.h
arch/riscv/include/asm/stacktrace.h +65 -2
@@ -3,8 +3,13 @@ #ifndef _ASM_RISCV_STACKTRACE_H #define _ASM_RISCV_STACKTRACE_H +#include <linux/percpu.h> #include <linux/sched.h> +#include <linux/sched/task_stack.h> + +#include <asm/irq_stack.h> #include <asm/ptrace.h> +#include <asm/stacktrace/common.h> struct stackframe { unsigned long fp;
@@ -16,14 +21,72 @@ extern void notrace walk_stackframe(struct task_struct *task, struct pt_regs *reextern void dump_backtrace (struct pt_regs *regs,struct task_struct *task,const char *loglvl); -static inline bool on_thread_stack (void ) +/* +* IRQ stack accessors +*/ +#ifdef CONFIG_IRQ_STACKS +static inline struct stack_info stackinfo_get_irq (void ) +{ +unsigned long low = (unsigned long )raw_cpu_read (irq_stack_ptr); +unsigned long high = low +IRQ_STACK_SIZE ; + +return (struct stack_info ) { + .low = low, + .high = high, + }; +} + +static inline bool on_irq_stack (unsigned long sp,unsigned long size) +{ +struct stack_info info =stackinfo_get_irq (); + +return stackinfo_on_stack (&info, sp, size); +} +#endif /* CONFIG_IRQ_STACKS */ + +/* +* Task stack accessors +*/ +static inline struct stack_info stackinfo_get_task (const struct task_struct *tsk) { -return !(((unsigned long )(current->stack) ^ current_stack_pointer) & ~(THREAD_SIZE -1 )); +unsigned long low = (unsigned long )task_stack_page (tsk); +unsigned long high = low +THREAD_SIZE ; + +return (struct stack_info ) { + .low = low, + .high = high, + }; +} + +static inline bool on_task_stack (const struct task_struct *tsk, +unsigned long sp,unsigned long size) +{ +struct stack_info info =stackinfo_get_task (tsk); + +return stackinfo_on_stack (&info, sp, size); } +/* +* Cast is necessary since current->stack is an opaque ptr. +*/ +#define on_thread_stack () (on_task_stack(current, current_stack_pointer, 1)) +/* +* Overflow stack accessors +*/ #ifdef CONFIG_VMAP_STACK DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); + +static inline struct stack_info stackinfo_get_overflow (void ) +{ +unsigned long low = (unsigned long )raw_cpu_ptr (overflow_stack); +unsigned long high = low +OVERFLOW_STACK_SIZE ; + +return (struct stack_info ) { + .low = low, + .high = high, + }; +}#endif /* CONFIG_VMAP_STACK */ #endif /* _ASM_RISCV_STACKTRACE_H */
arch/riscv/include/asm/stacktrace/common.h new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 4/7] riscv: stacktrace: introduce stack-bound tracking helpers
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 23 lines
From: Wang Han <[email protected]>
A reliable unwinder needs to validate that every frame record it reads is fully contained in a known kernel stack, and it needs to refuse to walk back into a stack it has already left. Add the building blocks for that:
* struct
stack_info/ structunwind_statein a newasm/stacktrace/common.h, modelled on the arm64 reference implementation.
*stackinfo_get_irq()/stackinfo_get_task()/stackinfo_get_overflow()plus the corresponding on_*_stack() predicates inasm/stacktrace.h, so callers can ask "is this object on stack X?" by stack kind rather than open-coded address arithmetic.
*unwind_init_common(),unwind_find_stack()andunwind_consume_stack()helpers that enforce the forward-progress-only invariant required for reliability.No existing user is wired up to these helpers in this commit; the unwinder switch comes in a follow-up. The header changes leave
on_thread_stack()with the same semantics as before, just expressed in terms of the new helpers.
Quoted 4 lines
Reviewed-by Shuai Xue <[email protected]>Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v6 5/7 riscv: stacktrace: switch to frame-pointer based unwinder
In reply to Shuai XueFrom: Wang Han <[email protected]>
Replace the open-coded frame-pointer walker in arch_stack_walk() with a robust kunwind state machine, modelled on arch/arm64/kernel/stacktrace.c and retargeted to the RISC-V {fp, ra} frame record convention. The new walker tracks stack bounds, consumes frame records monotonically, understands the metadata pt_regs records added in the previous frame record metadata patch, and recovers return addresses replaced by function graph tracing and kretprobes.
This commit introduces arch_stack_walk_reliable() but does not yet select HAVE_RELIABLE_STACKTRACE; that is done in a follow-up Kconfig patch so this commit can be reviewed and bisected as a pure unwinder replacement. Until that Kconfig change lands, livepatch is not yet enabled and arch_stack_walk_reliable() has no in-tree caller.
Three related callers are updated to keep the same frame-record assumptions everywhere:
* Function graph tracing: the old RISC-V unwinder matched function graph return-stack entries by the saved return-address slot. That was consistent with the static mcount path, but not with the dynamic ftrace path where the parent slot is ftrace_regs::ra. Use the architectural frame pointer as the function graph return-address cookie, matching the kunwind walker.
* Perf callchains: route kernel callchain collection through arch_stack_walk() so perf sees the same frame-pointer unwind behaviour as dump_stack() and the upcoming livepatch path.
* dump_backtrace() / __get_wchan() / show_stack(): these now go through arch_stack_walk(); the explicit "Call Trace:" header is moved into dump_backtrace() to preserve the original output.
The non-frame-pointer fallback walker is kept untouched for !CONFIG_FRAME_POINTER builds.
--- arch/riscv/kernel/ftrace.c | 6 +- arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/stacktrace.c | 561 ++++++++++++++++++++++++----- 3 files changed, 473 insertions(+), 96 deletions(-)
arch/riscv/kernel/ftrace.c +4 -2
@@ -245,7 +245,8 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,*/ old = *parent; -if (!function_graph_enter (old, self_addr, frame_pointer, parent)) +if (!function_graph_enter (old, self_addr, frame_pointer, + (void *)frame_pointer)) *parent = return_hooker; }
@@ -267,7 +268,8 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,*/ old = *parent; -if (!function_graph_enter_regs (old, ip, frame_pointer, parent, fregs)) +if (!function_graph_enter_regs (old, ip, frame_pointer, + (void *)frame_pointer, fregs)) *parent = return_hooker; }#endif /* CONFIG_DYNAMIC_FTRACE */
arch/riscv/kernel/perf_callchain.c +1 -1
@@ -44,5 +44,5 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,return ; } -walk_stackframe (NULL , regs, fill_callchain, entry); +arch_stack_walk (fill_callchain, entry,NULL , regs); }
arch/riscv/kernel/stacktrace.c +468 -93
602 lines not on this page · raw message
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 5/7] riscv: stacktrace: switch to frame-pointer based unwinder
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 18 lines
From: Wang Han <[email protected]>
Replace the open-coded frame-pointer walker in
arch_stack_walk()with a robust kunwind state machine, modelled onarch/arm64/kernel/stacktrace.cand retargeted to the RISC-V {fp, ra} frame record convention. The new walker tracks stack bounds, consumes frame records monotonically, understands the metadatapt_regsrecords added in the previous frame record metadata patch, and recovers return addresses replaced by function graph tracing and kretprobes.This commit introduces
arch_stack_walk_reliable()but does not yet selectHAVE_RELIABLE_STACKTRACE; that is done in a follow-upKconfigpatch so this commit can be reviewed and bisected as a pure unwinder replacement. Until thatKconfigchange lands, livepatch is not yet enabled andarch_stack_walk_reliable()has no in-tree caller. [ ... ]
!CONFIG_FRAME_POINTER builds.
Quoted 3 lines
Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v6 5/7] riscv: stacktrace: switch to frame-pointer based unwinder
In reply to Shuai Xuearch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 527
Quoted 11 lines · Shuai Xue
arch/riscv/kernel/stacktrace.c +11
-0+noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task, +struct pt_regs *regs) +{ +struct kunwind_consume_entry_data data = { + .consume_entry = consume_entry, + .cookie = cookie, + }; + +kunwind_stack_walk (arch_kunwind_consume_entry, &data, task, regs); +}
Hi Shuai Xue,
With this patch and FRAME_POINTER enabled, return_address() returns the caller one level further up than asked for.
return_address() in arch/riscv/kernel/return_address.c skips level + 3 entries from arch_stack_walk(). The arm64 original skips level + 2. The third entry exists on mainline because the walk starts inside walk_stackframe() and first reports the return into arch_stack_walk(). With 5/7 the walk starts at the caller of arch_stack_walk(), so level + 3 now skips a real caller. CALLER_ADDR1 and up are built on return_address(). The irqsoff tracer and the preemptirq tracepoints use them.
A KUnit test that calls ftrace_return_address() from leaf() <- middle() <- outer() <- check() returns:
af32da41b032 with series with series, level + 2 level 0 middle outer middle level 1 outer check outer
The change to level + 2 has to depend on FRAME_POINTER. Without it the series keeps walk_stackframe() and the result is the same as on mainline. There, level + 2 makes return_address(0) return an address inside return_address().
The same extra entry makes stack_trace_save() start one frame too early on mainline. 5/7 fixes that. I have a small fix for it on mainline, meant for stable kernels, which conflicts with 5/7. I would post it with you in Cc. Tell me if you would rather take the return_address.c change into 5/7 first.
Tested with clang 22 and the series applied on 52318cf0fa6e. The "with series" column is the same on QEMU's virt machine and on a BeagleV Ahead (TH1520). Everything else ran on QEMU only.
Karl
Re: [PATCH v6 5/7] riscv: stacktrace: switch to frame-pointer based unwinder
Quoted 30 lines · Karl Mehltretter
arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 527
Quoted 11 lines · Shuai Xue
arch/riscv/kernel/stacktrace.c +11
-0+noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task, +struct pt_regs *regs) +{ +struct kunwind_consume_entry_data data = { + .consume_entry = consume_entry, + .cookie = cookie, + }; + +kunwind_stack_walk (arch_kunwind_consume_entry, &data, task, regs); +}Hi Shuai Xue,
With this patch and
FRAME_POINTERenabled,return_address()returns the caller one level further up than asked for.
return_address()inarch/riscv/kernel/return_address.cskips level + 3 entries fromarch_stack_walk(). The arm64 original skips level + 2. The third entry exists on mainline because the walk starts insidewalk_stackframe()and first reports the return intoarch_stack_walk(). With 5/7 the walk starts at the caller ofarch_stack_walk(), so level + 3 now skips a real caller.CALLER_ADDR1and up are built onreturn_address(). The irqsoff tracer and the preemptirq tracepoints use them.A KUnit test that calls
ftrace_return_address()fromleaf()<-middle()<-outer()<-check()returns:
Quoted 8 lines
af32da41b032 with series with series, level + 2 level 0 middle outer middle level 1 outer check outerThe change to level + 2 has to depend on
FRAME_POINTER. Without it the series keepswalk_stackframe()and the result is the same as on mainline. There, level + 2 makesreturn_address(0)return an address insidereturn_address().
Hi, Karl,
Nice point. Thank you for the thorough review and the KUnit test. The analysis is spot on, and your table matches my reading of the code exactly.
Quoted 6 lines
The same extra entry makes
stack_trace_save()start one frame too early on mainline. 5/7 fixes that. I have a small fix for it on mainline, meant for stable kernels, which conflicts with 5/7. I would post it with you in Cc. Tell me if you would rather take thereturn_address.cchange into 5/7 first.
Please go ahead with your mainline/stable fix for the stack_trace_save() off-by-one as an independent patch. You are right that this is a separate mainline bug: stack_trace_save() hardcodes .skip = skipnr + 1, i.e. it expects the first entry reported by arch_stack_walk() to be the return into stack_trace_save() itself, and mainline's walk_stackframe() breaks that contract by reporting the return into arch_stack_walk() first. A 7-patch series is not a vehicle for stable, so the minimal fix is the right approach there.
Please keep me in Cc when you post it, I will review and test it.
Quoted 4 lines
Tested with clang 22 and the series applied on
52318cf0fa6e. The "with series" column is the same on QEMU's virt machine and on a BeagleV Ahead (TH1520). Everything else ran on QEMU only.
Thanks again for testing on real hardware (BeagleV Ahead) in addition to QEMU, that is much appreciated.
Best regards,
Shuai Xue
Re: [PATCH v6 5/7] riscv: stacktrace: switch to frame-pointer based unwinder
In reply to Shuai XueThus wrote Shuai Xue ([email protected]):
Quoted 61 lines
From: Wang Han <[email protected]>
Replace the open-coded frame-pointer walker in
arch_stack_walk()with a robust kunwind state machine, modelled onarch/arm64/kernel/stacktrace.cand retargeted to the RISC-V {fp, ra} frame record convention. The new walker tracks stack bounds, consumes frame records monotonically, understands the metadatapt_regsrecords added in the previous frame record metadata patch, and recovers return addresses replaced by function graph tracing and kretprobes.This commit introduces
arch_stack_walk_reliable()but does not yet selectHAVE_RELIABLE_STACKTRACE; that is done in a follow-upKconfigpatch so this commit can be reviewed and bisected as a pure unwinder replacement. Until thatKconfigchange lands, livepatch is not yet enabled andarch_stack_walk_reliable()has no in-tree caller.Three related callers are updated to keep the same frame-record assumptions everywhere:
* Function graph tracing: the old RISC-V unwinder matched function graph return-stack entries by the saved return-address slot. That was consistent with the static mcount path, but not with the dynamic ftrace path where the parent slot is
ftrace_regs::ra. Use the architectural frame pointer as the function graph return-address cookie, matching the kunwind walker.* Perf callchains: route kernel callchain collection through
arch_stack_walk()so perf sees the same frame-pointer unwind behaviour asdump_stack()and the upcoming livepatch path.*
dump_backtrace()/__get_wchan()/show_stack(): these now go througharch_stack_walk(); the explicit "Call Trace:" header is moved intodump_backtrace()to preserve the original output.The non-frame-pointer fallback walker is kept untouched for !CONFIG_FRAME_POINTER builds.
Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- arch/riscv/kernel/ftrace.c | 6 +- arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/stacktrace.c | 561 ++++++++++++++++++++++++----- 3 files changed, 473 insertions(+), 96 deletions(-)arch/riscv/kernel/ftrace.c +2
-1@@ -245,7 +245,8 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,*/ old = *parent; -if (!function_graph_enter (old, self_addr, frame_pointer, parent)) +if (!function_graph_enter (old, self_addr, frame_pointer, + (void *)frame_pointer)) *parent = return_hooker; }
arch/riscv/kernel/ftrace.c void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, · line 274
Quoted 8 lines
arch/riscv/kernel/ftrace.c +2
-1@@ -267,7 +268,8 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,*/ old = *parent; -if (!function_graph_enter_regs (old, ip, frame_pointer, parent, fregs)) +if (!function_graph_enter_regs (old, ip, frame_pointer, + (void *)frame_pointer, fregs)) *parent = return_hooker; }
Are these two changes fixing a bug in the current code (that is independent from the rest of this patch)?
Without your changes, function_graph_enter_regs writes a struct ftrace_ret_stack with retp == the saved ra.
And the only place where retp is fetched and checked is
function_get_true_parent_ip
ftrace_graph_ret_addr(..., (uintptr_t *)ftrace_regs_get_stack_pointer(fregs));
The stored retp is checked against the stack pointer.
Your changes store fp instead of ra, this seems the right thing to do. (And it's in line with other archs.)
Should those two changes be in a separate patch (that can be backported to stable kernels)?
Thanks,
Martin
Quoted 630 lines
arch/riscv/kernel/ftrace.c +0
-0#endif /* CONFIG_DYNAMIC_FTRACE */ arch/riscv/kernel/perf_callchain.c +1
-1@@ -44,5 +44,5 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,return ; } -walk_stackframe (NULL , regs, fill_callchain, entry); +arch_stack_walk (fill_callchain, entry,NULL , regs); }arch/riscv/kernel/stacktrace.c +468
-93@@ -11,98 +11,16 @@#include <linux/sched/task_stack.h> #include <linux/stacktrace.h> #include <linux/ftrace.h> +#include <linux/kprobes.h> +#include <linux/llist.h> #include <asm/stacktrace.h> -#ifdef CONFIG_FRAME_POINTER -/* -* This disables KASAN checking when reading a value from another task's stack, -* since the other task could be running on another CPU and could have poisoned -* the stack in the meantime. +* Non-frame-pointer fallback unwinder. +* Only compiled when CONFIG_FRAME_POINTER is not enabled. */ -#define READ_ONCE_TASK_STACK (task, x) \ -({ \ - unsigned long val; \ - unsigned long addr = x; \ - if ((task) == current) \ - val = READ_ONCE(addr); \ - else \ - val = READ_ONCE_NOCHECK(addr); \ - val; \ -}) - -extern asmlinkage void handle_exception(void); -extern unsigned long ret_from_exception_end; - -static inline int fp_is_valid (unsigned long fp,unsigned long sp) -{ -unsigned long low, high; - - low = sp +sizeof (struct stackframe ); - high =ALIGN (sp,THREAD_SIZE ); - -return !(fp < low || fp > high || fp &0x07 ); -} - -void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, -bool (*fn)(void *,unsigned long ),void *arg) -{ -unsigned long fp, sp, pc; -int graph_idx =0 ; -int level =0 ; - -if (regs) { - fp =frame_pointer (regs); - sp =user_stack_pointer (regs); - pc =instruction_pointer (regs); - }else if (task ==NULL || task == current) { - fp = (unsigned long )__builtin_frame_address (0 ); - sp = current_stack_pointer; - pc = (unsigned long )walk_stackframe; - level =-1 ; - }else { -/* task blocked in __switch_to */ - fp = task->thread.s[0 ]; - sp = task->thread.sp; - pc = task->thread.ra; - } - -for (;;) { -struct stackframe *frame; - -if (unlikely (!__kernel_text_address (pc) || (level++ >=0 && !fn (arg, pc)))) -break ; - -if (unlikely (!fp_is_valid (fp, sp))) -break ; - -/* Unwind stack frame */ - frame = (struct stackframe *)fp -1 ; - sp = fp; -if (regs && (regs->epc == pc) &&fp_is_valid (frame->ra, sp)) { -/* We hit function where ra is not saved on the stack */ - fp = frame->ra; - pc = regs->ra; - }else { - fp =READ_ONCE_TASK_STACK (task, frame->fp); - pc =READ_ONCE_TASK_STACK (task, frame->ra); - pc =ftrace_graph_ret_addr (task, &graph_idx, pc, - &frame->ra); -if (pc >= (unsigned long )handle_exception && - pc < (unsigned long )&ret_from_exception_end) { -if (unlikely (!fn (arg, pc))) -break ; - - pc = ((struct pt_regs *)sp)->epc; - fp = ((struct pt_regs *)sp)->s0; - } - } - - } -} - -#else /* !CONFIG_FRAME_POINTER */ +#ifndef CONFIG_FRAME_POINTER void notrace walk_stackframe(struct task_struct *task,struct pt_regs *regs,bool (*fn)(void *,unsigned long ),void *arg)@@ -133,7 +51,12 @@ void notrace walk_stackframe(struct task_struct *task, } } -#endif /* CONFIG_FRAME_POINTER */ +#endif /* !CONFIG_FRAME_POINTER */ + +/* +* Common trace helpers. +* These are used by both the FP (kunwind) and non-FP (walk_stackframe) paths. +*/ static bool print_trace_address (void *arg,unsigned long pc) {@@ -146,12 +69,12 @@ static bool print_trace_address(void *arg, unsigned long pc) noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task,const char *loglvl) { -walk_stackframe (task, regs, print_trace_address, (void *)loglvl); +printk ("%sCall Trace:\n" , loglvl); +arch_stack_walk (print_trace_address, (void *)loglvl, task, regs); }void show_stack (struct task_struct *task,unsigned long *sp,const char *loglvl) { -pr_cont ("%sCall Trace:\n" , loglvl);dump_backtrace (NULL , task, loglvl); }@@ -171,17 +94,469 @@ unsigned long __get_wchan(struct task_struct *task)if (!try_get_task_stack (task))return 0 ; -walk_stackframe (task,NULL , save_wchan, &pc); +arch_stack_walk (save_wchan, &pc, task,NULL );put_task_stack (task);return pc; } -noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, -struct task_struct *task,struct pt_regs *regs) +/* +* Frame-pointer-based kernel unwind infrastructure. +* Only compiled when CONFIG_FRAME_POINTER is enabled. +* +* See: arch/arm64/kernel/stacktrace.c for the reference implementation. +*/ +#ifdef CONFIG_FRAME_POINTER + +/* +* Per-cpu stacks are only accessible when unwinding the current task in a +* non-preemptible context. +*/ +#define STACKINFO_CPU (task, name) \ + ({ \ + (((task) == current) && !preemptible()) \ + ? stackinfo_get_##name() \ + : stackinfo_get_unknown(); \ + }) + +enum kunwind_source { +KUNWIND_SOURCE_UNKNOWN , +KUNWIND_SOURCE_FRAME , +KUNWIND_SOURCE_CALLER , +KUNWIND_SOURCE_TASK , +KUNWIND_SOURCE_REGS_PC , +}; + +union unwind_flags { +unsigned long all; +struct { +unsigned long fgraph :1 , + kretprobe :1 ; + }; +}; + +/* +* Kernel unwind state +* +* @common: Common unwind state. +* @task: The task being unwound. +* @graph_idx: Used by ftrace_graph_ret_addr() for optimized stack unwinding. +* @kr_cur: When KRETPROBES is selected, holds the kretprobe instance +* associated with the most recently encountered replacement ra +* value. +*/ +struct kunwind_state { +struct unwind_state common; +struct task_struct *task; +int graph_idx; +#ifdef CONFIG_KRETPROBES +struct llist_node *kr_cur; +#endif +enum kunwind_source source; +union unwind_flags flags; +struct pt_regs *regs; +}; + +static __always_inline void +kunwind_init (struct kunwind_state *state, +struct task_struct *task) +{ +unwind_init_common (&state->common); + state->task = task; + state->source =KUNWIND_SOURCE_UNKNOWN ; + state->flags.all =0 ; + state->regs =NULL ; +} + +/* +* Start an unwind from a pt_regs. +* +* The unwind will begin at the PC within the regs. +* +* The regs must be on a stack currently owned by the calling task. +*/ +static __always_inline void +kunwind_init_from_regs (struct kunwind_state *state, +struct pt_regs *regs) +{ +kunwind_init (state, current); + + state->regs = regs; + state->common.fp =frame_pointer (regs); + state->common.pc =instruction_pointer (regs); + state->source =KUNWIND_SOURCE_REGS_PC ; +} + +/* +* Start an unwind from a caller. +* +* The unwind will begin at the caller of whichever function this is inlined +* into. +* +* The function which invokes this must be noinline. +*/ +static __always_inline void +kunwind_init_from_caller (struct kunwind_state *state) +{ +unsigned long fp = (unsigned long )__builtin_frame_address (0 ); +struct frame_record *record = (struct frame_record *)fp -1 ; + +kunwind_init (state, current); + + state->common.fp =READ_ONCE (record->fp); + state->common.pc =READ_ONCE (record->ra); + state->source =KUNWIND_SOURCE_CALLER ; +} + +/* +* Start an unwind from a blocked task. +* +* The unwind will begin at the blocked task's saved PC (i.e. the caller of +* __switch_to). +* +* The caller should ensure the task is blocked in __switch_to for the +* duration of the unwind, or the unwind will be bogus. It is never valid to +* call this for the current task. +*/ +static __always_inline void +kunwind_init_from_task (struct kunwind_state *state, +struct task_struct *task) +{ +kunwind_init (state, task); + + state->common.fp = task->thread.s[0 ]; + state->common.pc = task->thread.ra; + state->source =KUNWIND_SOURCE_TASK ; +} + +static __always_inline int +kunwind_recover_return_address (struct kunwind_state *state) +{ +#ifdef CONFIG_FUNCTION_GRAPH_TRACER +if (state->task->ret_stack && + state->common.pc == (unsigned long )return_to_handler) { +unsigned long orig_pc; + + orig_pc =ftrace_graph_ret_addr (state->task, &state->graph_idx, + state->common.pc, + (void *)state->common.fp); +if (state->common.pc == orig_pc) { +WARN_ON_ONCE (state->task == current); +return -EINVAL ; + } + state->common.pc = orig_pc; + state->flags.fgraph =1 ; + } +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ + +#ifdef CONFIG_KRETPROBES +if (is_kretprobe_trampoline (state->common.pc)) { +unsigned long orig_pc; + + orig_pc =kretprobe_find_ret_addr (state->task, + (void *)state->common.fp, + &state->kr_cur); +if (!orig_pc) +return -EINVAL ; + state->common.pc = orig_pc; + state->flags.kretprobe =1 ; + } +#endif /* CONFIG_KRETPROBES */ + +return 0 ; +} + +/* +* When we reach an exception boundary marked by a metadata frame record, +* extract pt_regs from the stack and continue unwinding from the saved +* context (epc and s0/fp). +* +* On RISC-V, fp points above the metadata record, so the record's +* frame_record portion is at fp - sizeof(struct frame_record). +*/ +static __always_inline int +kunwind_next_regs_pc (struct kunwind_state *state) +{ +struct stack_info *info; +unsigned long fp = state->common.fp; +struct pt_regs *regs; + + regs =container_of ((unsigned long *)(fp -sizeof (struct frame_record )), + struct pt_regs, stackframe.record.fp); + + info =unwind_find_stack (&state->common, (unsigned long )regs, +sizeof (*regs)); +if (!info) +return -EINVAL ; + +unwind_consume_stack (&state->common, info, (unsigned long )regs, +sizeof (*regs)); + + state->regs = regs; + state->common.pc = regs->epc; + state->common.fp =frame_pointer (regs); + state->source =KUNWIND_SOURCE_REGS_PC ; +return 0 ; +} + +/* +* Handle a metadata frame record embedded in pt_regs. +* +* On RISC-V, fp points above the record (fp = metadata + 16), so the +* frame_record_meta starts at fp - sizeof(struct frame_record). +* +* FRAME_META_TYPE_FINAL: This is the outermost exception entry +* (user -> kernel). Unwinding terminates successfully. +* FRAME_META_TYPE_PT_REGS: This is a nested exception entry +* (kernel -> kernel). Continue unwinding from the saved context. +*/ +static __always_inline int +kunwind_next_frame_record_meta (struct kunwind_state *state) +{ +struct task_struct *tsk = state->task; +unsigned long fp = state->common.fp; +unsigned long meta_base = fp -sizeof (struct frame_record ); +struct frame_record_meta *meta; +struct stack_info *info; + + info =unwind_find_stack (&state->common, meta_base,sizeof (*meta)); +if (!info) +return -EINVAL ; + + meta = (struct frame_record_meta *)meta_base; +switch (READ_ONCE (meta->type)) { +case FRAME_META_TYPE_FINAL : +if (meta == &task_pt_regs (tsk)->stackframe) +return -ENOENT ; +WARN_ON_ONCE (tsk == current); +return -EINVAL ; +case FRAME_META_TYPE_PT_REGS : +return kunwind_next_regs_pc (state); +default : +WARN_ON_ONCE (tsk == current); +return -EINVAL ; + } +} + +/* +* Unwind from one frame record to the next. +* +* On RISC-V, the frame record sits at fp - sizeof(struct frame_record), +* immediately below the address pointed to by fp/s0. This applies to both +* normal frame records and metadata frame records (embedded in pt_regs). +* +* A metadata record is identified by both fp and ra being zero in the +* frame_record portion, with a type value following at fp + 16. +*/ +static __always_inline int +kunwind_next_frame_record (struct kunwind_state *state) +{ +unsigned long fp = state->common.fp; +struct frame_record *record; +struct stack_info *info; +unsigned long new_fp, new_pc; +unsigned long record_base; + +if (fp &0x7 ) +return -EINVAL ; + + record_base = fp -sizeof (*record); + + info =unwind_find_stack (&state->common, record_base,sizeof (*record)); +if (!info) +return -EINVAL ; + + record = (struct frame_record *)record_base; + new_fp =READ_ONCE (record->fp); + new_pc =READ_ONCE (record->ra); + +if (!new_fp && !new_pc) +return kunwind_next_frame_record_meta (state); + +unwind_consume_stack (&state->common, info, record_base, +sizeof (*record)); + + state->common.fp = new_fp; + state->common.pc = new_pc; + state->source =KUNWIND_SOURCE_FRAME ; + +return 0 ; +} + +/* +* Unwind from one frame record (A) to the next frame record (B). +* +* We terminate early if the location of B indicates a malformed chain of frame +* records (e.g. a cycle), determined based on the location and fp value of A +* and the location (but not the fp value) of B. +*/ +static __always_inline int +kunwind_next (struct kunwind_state *state) +{ +int err; + + state->flags.all =0 ; + +switch (state->source) { +case KUNWIND_SOURCE_FRAME : +case KUNWIND_SOURCE_CALLER : +case KUNWIND_SOURCE_TASK : +case KUNWIND_SOURCE_REGS_PC : + err =kunwind_next_frame_record (state); +break ; +default : + err = -EINVAL ; + } + +if (err) +return err; + +return kunwind_recover_return_address (state); +} + +typedef bool (*kunwind_consume_fn )(const struct kunwind_state *state,void *cookie); + +static __always_inline int +do_kunwind (struct kunwind_state *state,kunwind_consume_fn consume_state, +void *cookie) +{ +int ret; + + ret =kunwind_recover_return_address (state); +if (ret) +return ret; + +while (1 ) { +if (!consume_state (state, cookie)) +return -EINVAL ; + ret =kunwind_next (state); +if (ret == -ENOENT ) +return 0 ; +if (ret <0 ) +return ret; + } +} + +static __always_inline int +kunwind_stack_walk (kunwind_consume_fn consume_state, +void *cookie,struct task_struct *task, +struct pt_regs *regs) +{ +struct task_struct *tsk = task ?: current; +struct stack_info stacks[] = { +stackinfo_get_task (tsk), +#ifdef CONFIG_IRQ_STACKS +STACKINFO_CPU (tsk, irq), +#endif +#ifdef CONFIG_VMAP_STACK +STACKINFO_CPU (tsk, overflow), +#endif + }; +struct kunwind_state state = { + .common = { + .stacks = stacks, + .nr_stacks =ARRAY_SIZE (stacks), + }, + }; + +if (regs) { +if (tsk != current) +return -EINVAL ; +kunwind_init_from_regs (&state, regs); + }else if (tsk == current) { +kunwind_init_from_caller (&state); + }else { +kunwind_init_from_task (&state, tsk); + } + +return do_kunwind (&state, consume_state, cookie); +} + +struct kunwind_consume_entry_data { +stack_trace_consume_fn consume_entry; +void *cookie; +}; + +static __always_inline bool +arch_kunwind_consume_entry (const struct kunwind_state *state,void *cookie) +{ +struct kunwind_consume_entry_data *data = cookie; + +return data->consume_entry (data->cookie, state->common.pc); +} + +static __always_inline bool +arch_reliable_kunwind_consume_entry (const struct kunwind_state *state,void *cookie) +{ +/* +* At an exception boundary we can reliably consume the saved PC. We do +* not know whether ra was live when the exception was taken, and +* so we cannot perform the next unwind step reliably. +* +* All that matters is whether the *entire* unwind is reliable, so give +* up as soon as we hit an exception boundary. +*/ +if (state->source ==KUNWIND_SOURCE_REGS_PC ) +return false; + +return arch_kunwind_consume_entry (state, cookie); +} + +#endif /* CONFIG_FRAME_POINTER */ + +/* +* arch_stack_walk - dual implementation. +* +* When CONFIG_FRAME_POINTER is enabled, uses the kunwind infrastructure for +* robust frame-pointer-based unwinding, consistent with arch_stack_walk_reliable. +* +* When CONFIG_FRAME_POINTER is disabled, falls back to the simple stack scan +* in walk_stackframe(). +*/ +#ifdef CONFIG_FRAME_POINTER + +noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task, +struct pt_regs *regs) +{ +struct kunwind_consume_entry_data data = { + .consume_entry = consume_entry, + .cookie = cookie, + }; + +kunwind_stack_walk (arch_kunwind_consume_entry, &data, task, regs); +} + +#else + +noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task, +struct pt_regs *regs) {walk_stackframe (task, regs, consume_entry, cookie); } +#endif /* CONFIG_FRAME_POINTER */ + +/* +* Reliable stack walk for livepatch (CONFIG_FRAME_POINTER only). +*/ +#ifdef CONFIG_FRAME_POINTER + +noinline noinstr int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry, +void *cookie, +struct task_struct *task) +{ +struct kunwind_consume_entry_data data = { + .consume_entry = consume_entry, + .cookie = cookie, + }; + +return kunwind_stack_walk (arch_reliable_kunwind_consume_entry, &data, + task,NULL ); +} + +#endif /* CONFIG_FRAME_POINTER */ +/* * Get the return address for a single stackframe and return a pointer to the * next frame tail. Signature · 7 lines
2.39.3 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH v6 5/7] riscv: stacktrace: switch to frame-pointer based unwinder
Quoted 84 lines · Martin Kaiser
Thus wrote Shuai Xue ([email protected]):
Quoted 61 lines
From: Wang Han <[email protected]>
Replace the open-coded frame-pointer walker in
arch_stack_walk()with a robust kunwind state machine, modelled onarch/arm64/kernel/stacktrace.cand retargeted to the RISC-V {fp, ra} frame record convention. The new walker tracks stack bounds, consumes frame records monotonically, understands the metadatapt_regsrecords added in the previous frame record metadata patch, and recovers return addresses replaced by function graph tracing and kretprobes.This commit introduces
arch_stack_walk_reliable()but does not yet selectHAVE_RELIABLE_STACKTRACE; that is done in a follow-upKconfigpatch so this commit can be reviewed and bisected as a pure unwinder replacement. Until thatKconfigchange lands, livepatch is not yet enabled andarch_stack_walk_reliable()has no in-tree caller.Three related callers are updated to keep the same frame-record assumptions everywhere:
* Function graph tracing: the old RISC-V unwinder matched function graph return-stack entries by the saved return-address slot. That was consistent with the static mcount path, but not with the dynamic ftrace path where the parent slot is
ftrace_regs::ra. Use the architectural frame pointer as the function graph return-address cookie, matching the kunwind walker.* Perf callchains: route kernel callchain collection through
arch_stack_walk()so perf sees the same frame-pointer unwind behaviour asdump_stack()and the upcoming livepatch path.*
dump_backtrace()/__get_wchan()/show_stack(): these now go througharch_stack_walk(); the explicit "Call Trace:" header is moved intodump_backtrace()to preserve the original output.The non-frame-pointer fallback walker is kept untouched for !CONFIG_FRAME_POINTER builds.
Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>--- arch/riscv/kernel/ftrace.c | 6 +- arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/stacktrace.c | 561 ++++++++++++++++++++++++----- 3 files changed, 473 insertions(+), 96 deletions(-)arch/riscv/kernel/ftrace.c +2
-1@@ -245,7 +245,8 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,*/ old = *parent; -if (!function_graph_enter (old, self_addr, frame_pointer, parent)) +if (!function_graph_enter (old, self_addr, frame_pointer, + (void *)frame_pointer)) *parent = return_hooker; }arch/riscv/kernel/ftrace.c void ftrace_graph_func(unsigned long ip, unsigned long parent_ip, · line 274
Quoted 8 lines
arch/riscv/kernel/ftrace.c +2
-1@@ -267,7 +268,8 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,*/ old = *parent; -if (!function_graph_enter_regs (old, ip, frame_pointer, parent, fregs)) +if (!function_graph_enter_regs (old, ip, frame_pointer, + (void *)frame_pointer, fregs)) *parent = return_hooker; }Are these two changes fixing a bug in the current code (that is independent from the rest of this patch)?
Without your changes,
function_graph_enter_regswrites a structftrace_ret_stackwith retp == the saved ra.And the only place where retp is fetched and checked is
function_get_true_parent_ipftrace_graph_ret_addr(..., (uintptr_t *)ftrace_regs_get_stack_pointer(fregs));
Quoted 7 lines
The stored retp is checked against the stack pointer.
Your changes store fp instead of ra, this seems the right thing to do. (And it's in line with other archs.)
Should those two changes be in a separate patch (that can be backported to stable kernels)?
Hi, Martin,
Thanks for the sharp question.
You are right that the stored cookie is broken. What ftrace_graph_func() passes as retp is &arch_ftrace_regs(fregs)->ra, i.e. the slot inside the transient ftrace trampoline frame, which is popped as soon as the callback returns. That has been the case since 35e61e8827ee8 ("riscv: ftrace: Make function graph use ftrace directly") introduced ftrace_graph_func() in v6.8. (The code was copied from arm64's ftrace_graph_func(), but arm64 passes (void *)frame_pointer there - see 41705c4262aac for the current shape.)
Since distro kernels only ever use the dynamic path, the net effect is that the shadow stack is never consulted during unwinding:
whenever the function graph tracer is running, dump_stack(), perf callchains and __get_wchan() report return_to_handler instead of the real caller.
I will split these two changes into a separate patch and cc stable.
Thanks.
Shuai
PATCH v6 6/7 riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
In reply to Shuai XueFrom: Wang Han <[email protected]>
Now that the metadata frame records, the kunwind state machine and arch_stack_walk_reliable() are all in place, advertise the capability to the rest of the kernel:
* select HAVE_RELIABLE_STACKTRACE under FRAME_POINTER && 64BIT, so only the configurations with the tested metadata records and FP-based reliable walker enable it.
* select HAVE_LIVEPATCH under the same condition and source kernel/livepatch/Kconfig so the livepatch menu is reachable from the RISC-V configuration.
The 64BIT dependency is conservative scoping rather than a hard technical requirement: the metadata frame record, kunwind state machine and arch_stack_walk_reliable() also build on RV32, and the IRQ-stack frame-record adjustment fixes a latent RV32 issue. However, the syscall livepatch selftest and module relocation path have only been exercised on RV64 QEMU virt so far. The 64BIT gate can be relaxed in a follow-up once RV32 has equivalent coverage.
This is split out from the unwinder change so the policy decision and the implementation can be reviewed and reverted independently.
--- arch/riscv/Kconfig | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/Kconfig +4 -0
@@ -186,6 +186,7 @@ config RISCV select HAVE_KRETPROBES # https://github.com/ClangBuiltLinux/linux/issues/1881 select HAVE_LD_DEAD_CODE_DATA_ELIMINATION if !LD_IS_LLD + select HAVE_LIVEPATCH if FRAME_POINTER && 64BIT select HAVE_MOVE_PMD select HAVE_MOVE_PUD select HAVE_PAGE_SIZE_4KB
@@ -196,6 +197,7 @@ config RISCV select HAVE_POSIX_CPU_TIMERS_TASK_WORK select HAVE_PREEMPT_DYNAMIC_KEY select HAVE_REGS_AND_STACK_ACCESS_API + select HAVE_RELIABLE_STACKTRACE if FRAME_POINTER && 64BIT select HAVE_RETHOOK select HAVE_RSEQ select HAVE_RUST if RUSTC_SUPPORTS_RISCV && CC_IS_CLANG
@@ -1411,3 +1413,5 @@ endmenu # "CPU Power Management" source "arch/riscv/kvm/Kconfig" source "drivers/acpi/Kconfig" + +source "kernel/livepatch/Kconfig"
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 6/7] riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 21 lines
From: Wang Han <[email protected]>
Now that the metadata frame records, the kunwind state machine and
arch_stack_walk_reliable()are all in place, advertise the capability to the rest of the kernel:* select
HAVE_RELIABLE_STACKTRACEunderFRAME_POINTER&& 64BIT, so only the configurations with the tested metadata records and FP-based reliable walker enable it.
* selectHAVE_LIVEPATCHunder the same condition and sourcekernel/livepatch/Kconfigso the livepatch menu is reachable from the RISC-V configuration.The 64BIT dependency is conservative scoping rather than a hard technical requirement: the metadata frame record, kunwind state machine and
arch_stack_walk_reliable()also build on RV32, and the IRQ-stack frame-record adjustment fixes a latent RV32 issue. However, the syscall livepatch selftest and module relocation path have only been exercised on RV64 QEMU virt so far. The 64BIT gate can be relaxed in a follow-up once RV32 has equivalent coverage.
Quoted 7 lines
This is split out from the unwinder change so the policy decision and the implementation can be reviewed and reverted independently.
Reviewed-by Shuai Xue <[email protected]>Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v6 7/7 selftests/livepatch: Add RISC-V syscall wrapper prefix
In reply to Shuai XueFrom: Wang Han <[email protected]>
The syscall livepatch selftest resolves and patches a syscall wrapper symbol. To use that test for RISC-V livepatch validation, add the RISC-V FN_PREFIX definition for ARCH_HAS_SYSCALL_WRAPPER.
Without this macro, the syscall livepatch selftest cannot resolve the RISC-V target symbol, and the syscall-related livepatch test fails on RISC-V.
--- .../testing/selftests/livepatch/test_modules/test_klp_syscall.c | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c +2 -0
@@ -24,6 +24,8 @@#define FN_PREFIX __s390x_#elif defined(__aarch64__)#define FN_PREFIX __arm64_ +#elif defined(__riscv) +#define FN_PREFIX __riscv_#elif defined(__powerpc__)#define FN_PREFIX #else
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v6 7/7] selftests/livepatch: Add RISC-V syscall wrapper prefix
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
From: Wang Han <[email protected]>
The syscall livepatch selftest resolves and patches a syscall wrapper symbol. To use that test for RISC-V livepatch validation, add the RISC-V
FN_PREFIXdefinition forARCH_HAS_SYSCALL_WRAPPER.Without this macro, the syscall livepatch selftest cannot resolve the RISC-V target symbol, and the syscall-related livepatch test fails on RISC-V.
Quoted 6 lines
Reviewed-by Marcos Paulo de Souza <[email protected]>Reviewed-by Shuai Xue <[email protected]>Acked-by Miroslav Benes <[email protected]>Assisted-by Qoder:Qwen-3.8-MAX-PreviewSigned-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Thus wrote Shuai Xue ([email protected]):
Quoted 39 lines
Hi,
This is v6 of the RISC-V reliable stack unwinding series for livepatch. Wang Han has left our team, so I am taking over this series from now on.
The series adds explicit frame-record metadata, preserves the frame pointer through dynamic ftrace, and replaces the open-coded stack walker with a conservative frame-pointer based unwinder. It then enables the RISC-V livepatch capability and adds the syscall-wrapper selftest prefix.
Changes since v5:
* Rebase onto the current riscv/for-next; no functional changes.
* Fold in the tags collected from the v5 thread:
- Reviewed-by: Shuai Xue on 2/7, 3/7, 4/7 and 6/7
- Acked-by: Miroslav Benes on 7/7
* Add my sign-off as the new sender.Since the patches are otherwise identical to v5, the v5 validation results still apply. For reference, from the v5 cover letter:
* Built Linux 7.2.0-rc6 with riscv64-unknown-linux-gnu-gcc 15.2.0 and binutils 2.46.
* Standard livepatch configuration built successfully, including Image, vmlinux, modules, compile_commands.json and GDB scripts.
*CONFIG_IRQ_STACKS=nvariant linked successfully with GCC.
* QEMU 8.2.2, 1 CPU: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* QEMU 8.2.2, 4 CPUs: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* No BUG, WARNING, Oops, panic, unreliable stack or Call Trace found by the validation log scans. Expected negative livepatch errors are part of the selftests.v5 was posted in early August and has been quiet since then. I would appreciate it if the series could be considered for the upcoming merge window. Happy to address any remaining feedback.
Base:
riscv/for-next77ae27fd98f3("Merge tag 'printk-for-7.3' ofgit://git.kernel.org/pub/scm/linux/kernel/git/printk/linux")
Quoted 8 lines
Previous versions:
v5: https://lore.kernel.org/r/[email protected]
v4 RESEND: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]Assisted-by Qoder:Qwen-3.8-MAX-Preview
Thanks for these patches.
I tried them on a standard rv64 qemu and on a rv64 cheri system (cheri-linux.org). The latter needed some minor tweaks, but in both cases, I could get livepatch to work.
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 50 lines · Martin Kaiser
Thus wrote Shuai Xue ([email protected]):
Quoted 39 lines
Hi,
This is v6 of the RISC-V reliable stack unwinding series for livepatch. Wang Han has left our team, so I am taking over this series from now on.
The series adds explicit frame-record metadata, preserves the frame pointer through dynamic ftrace, and replaces the open-coded stack walker with a conservative frame-pointer based unwinder. It then enables the RISC-V livepatch capability and adds the syscall-wrapper selftest prefix.
Changes since v5:
* Rebase onto the current riscv/for-next; no functional changes.
* Fold in the tags collected from the v5 thread:
- Reviewed-by: Shuai Xue on 2/7, 3/7, 4/7 and 6/7
- Acked-by: Miroslav Benes on 7/7
* Add my sign-off as the new sender.Since the patches are otherwise identical to v5, the v5 validation results still apply. For reference, from the v5 cover letter:
* Built Linux 7.2.0-rc6 with riscv64-unknown-linux-gnu-gcc 15.2.0 and binutils 2.46.
* Standard livepatch configuration built successfully, including Image, vmlinux, modules, compile_commands.json and GDB scripts.
*CONFIG_IRQ_STACKS=nvariant linked successfully with GCC.
* QEMU 8.2.2, 1 CPU: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* QEMU 8.2.2, 4 CPUs: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* No BUG, WARNING, Oops, panic, unreliable stack or Call Trace found by the validation log scans. Expected negative livepatch errors are part of the selftests.v5 was posted in early August and has been quiet since then. I would appreciate it if the series could be considered for the upcoming merge window. Happy to address any remaining feedback.
Base:
riscv/for-next77ae27fd98f3("Merge tag 'printk-for-7.3' ofgit://git.kernel.org/pub/scm/linux/kernel/git/printk/linux")Quoted 8 lines
Previous versions:
v5: https://lore.kernel.org/r/[email protected]
v4 RESEND: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]Assisted-by Qoder:Qwen-3.8-MAX-Preview
Quoted 8 lines
Thanks for these patches.
I tried them on a standard rv64 qemu and on a rv64 cheri system (cheri-linux.org). The latter needed some minor tweaks, but in both cases, I could get livepatch to work.
Reviewed-by Martin Kaiser <[email protected]>Tested-by Martin Kaiser <[email protected]>
Hi Martin,
Thank you very much for testing the series and for the tags!
It is great to hear that livepatch works both on a standard rv64 QEMU and on the rv64 CHERI system. Could you share what the minor tweaks for CHERI were? If any of them are generic -- for example frame-record size/alignment or pointer-width assumptions in the unwinder -- it may be worth folding them into this series, so the cheri-linux tree has less to carry downstream. If they are purely CHERI-specific, a pointer to your tree would still be a useful reference.
+Palmer, Paul, gentle ping on this series.
It has been under review since late May (v1 on May 27, now in its sixth revision) and the tags collected so far are:
* Acked-by: Miroslav Benes <[email protected]> (7/7, livepatch)
* Reviewed-by: Marcos Paulo de Souza <[email protected]> (7/7)
* Reviewed-by: Martin Kaiser <[email protected]> (whole series)
* Tested-by: Martin Kaiser <[email protected]> (whole series,
rv64 QEMU and rv64 CHERI, livepatch working on both)
* Reviewed-by: Shuai Xue <[email protected]> (2/7, 3/7, 4/7, 6/7)
v6 still applies cleanly onto the current riscv/for-next (52318cf0fa6e). If it helps, I can send a v7 that folds in Martin's Reviewed-by/Tested-by tags.
Could the series be considered for the upcoming 7.4 merge window?
Thanks,
Shuai Xue
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Hi Shuai,
Thus wrote Shuai Xue ([email protected]):
Quoted 7 lines
It is great to hear that livepatch works both on a standard rv64 QEMU and on the rv64 CHERI system. Could you share what the minor tweaks for CHERI were? If any of them are generic -- for example frame-record size/alignment or pointer-width assumptions in the unwinder -- it may be worth folding them into this series, so the cheri-linux tree has less to carry downstream. If they are purely CHERI-specific, a pointer to your tree would still be a useful reference.
the cheri changes are simple fixups of data types and assembly instructions. This isn't something that can be upstreamed at this point.
Eventually, it'll end up in the cheri linux tree at
https://github.com/CHERI-Alliance/linux
but there's more cleanup to do first.
I just wanted to show my interest in getting your patches merged.
Thanks,
Martin
Quoted 12 lines
+Palmer, Paul, gentle ping on this series.
It has been under review since late May (v1 on May 27, now in its sixth revision) and the tags collected so far are:
* Acked-by: Miroslav Benes <[email protected]> (7/7, livepatch)
* Reviewed-by: Marcos Paulo de Souza <[email protected]> (7/7)
* Reviewed-by: Martin Kaiser <[email protected]> (whole series)
* Tested-by: Martin Kaiser <[email protected]> (whole series,
rv64 QEMU and rv64 CHERI, livepatch working on both)
* Reviewed-by: Shuai Xue <[email protected]> (2/7, 3/7, 4/7, 6/7)
Quoted 8 lines
v6 still applies cleanly onto the current riscv/for-next (
52318cf0fa6e). If it helps, I can send a v7 that folds in Martin's Reviewed-by/Tested-by tags.Could the series be considered for the upcoming 7.4 merge window?
Thanks,
Shuai Xue
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
v7 cover riscv: Add reliable stack unwinding for livepatch
In reply to Shuai XueHi,
This is v7 of the RISC-V reliable stack unwinding series for livepatch.
The series starts with three standalone fixes for the existing stack
walkers: Karl Mehltretter's fix for arch_stack_walk() reporting the walker's own frame, its KUnit test, and a fix for the function-graph return-address cookie that Martin Kaiser asked to be split out. The main part of the series then adds explicit frame-record metadata, preserves the frame pointer through dynamic ftrace, replaces the open-coded stack walker with a conservative frame-pointer based unwinder, enables the RISC-V livepatch capability and adds the syscall-wrapper selftest prefix.
Changes since v6:
* Rebase onto the current riscv/for-next (based on 7.4-rc1).
* Fold in Karl Mehltretter's two patches [1] as 1/10 and 2/10. Patch 1/10 stops arch_stack_walk() from reporting the walker's own frame as the first entry, which corrupts the skip-frame accounting of stack_trace_save() users (page_owner, perf), and adjusts return_address() level accounting accordingly. Patch 2/10 adds a KUnit test for the start frames. Both patches keep their original authorship; I added my Reviewed-by/Tested-by and sign-off. I asked Karl on 3 November whether he would like to see his patches folded into this series; he has not replied so far. Since 8/10 reuses the walker extraction from 1/10 for its !CONFIG_FRAME_POINTER fallback, his patches are included here to keep the series self-contained.
* Split the function-graph return-address cookie change out of the old 5/7, as suggested by Martin Kaiser. It is now a separate patch (3/10) that also fixes the old walker's cookie key, so it stands on its own without the rest of the series and can be picked independently; it is marked Fixes: and Cc: stable.
* Reworked the !CONFIG_FRAME_POINTER fallback of the unwinder switch patch (8/10) to use the __always_inline __walk_stackframe() helper from 1/10, so both walkers report the same start frame.
* Collected tags from the v6 review thread: Reviewed-by and Tested-by:
Martin Kaiser for the patches carried over from v6 (rv64 QEMU and rv64 CHERI builds, livepatch working on both).
The patches carried over from v6 are unchanged, except for the reworked !CONFIG_FRAME_POINTER fallback in 8/10, so the v6 validation results still apply to them. The v7-specific changes were covered by the fresh build and boot runs described below:
* Built with riscv64-unknown-linux-gnu-gcc 15.2.0 and binutils 2.46, including a CONFIG_IRQ_STACKS=n variant.
* QEMU 8.2.2, 1 and 4 CPUs: livepatch selftests PASS: 7, SKIP: 1,
FAIL: 0; ftrace function graph 3 passed, 0 failed, 3 unsupported.
* No BUG, WARNING, Oops, panic, unreliable stack or Call Trace found by the validation log scans.
Patches 1-2/10 were tested by Karl with the KUnit suite from 2/10 on QEMU's virt machine with clang 22 (LLVM=1) and GCC 15.2, and on a BeagleV Ahead (TH1520), where all five test cases pass.
In addition, the combined v7 tree was freshly built and booted:
* Built with riscv64-unknown-linux-gnu-gcc 16.2.0 on top of 7.4-rc1:
a full livepatch configuration (CONFIG_LIVEPATCH=y with dynamic ftrace and function graph tracer) and a !CONFIG_FRAME_POINTER variant both link successfully.
* QEMU 11.1.1, rv64 virt, 4 CPUs: the KUnit suite from 2/10 passes 5/5 on the frame-pointer build; the !CONFIG_FRAME_POINTER build boots and its panic backtrace (no initramfs, by design) unwinds cleanly.
Base:
riscv/for-next 6628e7d4c6b2 ("Linux 7.4-rc1")
Previous versions:
v6: https://lore.kernel.org/r/[email protected]
v5: https://lore.kernel.org/r/[email protected]
v4 RESEND: https://lore.kernel.org/r/[email protected]
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
[1] https://lore.kernel.org/r/[email protected]
Karl Mehltretter (2):
riscv: stacktrace: do not report the walker's own frame from
arch_stack_walk()
riscv: Add KUnit test for stack trace start frames
Shuai Xue (1):
riscv: ftrace: use the frame pointer as the function graph
return-address cookie
Wang Han (7):
riscv: stacktrace: Add frame record metadata
riscv: stacktrace: disable KASAN and KCOV instrumentation for
stacktrace.o
riscv: ftrace: always preserve s0 in dynamic ftrace register frame
riscv: stacktrace: introduce stack-bound tracking helpers
riscv: stacktrace: switch to frame-pointer based unwinder
riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH selftests/livepatch: Add RISC-V syscall wrapper prefix
arch/riscv/Kconfig | 4 + arch/riscv/include/asm/ptrace.h | 9 + arch/riscv/include/asm/stacktrace.h | 67 ++- arch/riscv/include/asm/stacktrace/common.h | 159 +++++ arch/riscv/include/asm/stacktrace/frame.h | 53 ++ arch/riscv/kernel/Makefile | 6 + arch/riscv/kernel/asm-offsets.c | 6 + arch/riscv/kernel/entry.S | 39 +- arch/riscv/kernel/ftrace.c | 6 +- arch/riscv/kernel/head.S | 23 + arch/riscv/kernel/mcount-dyn.S | 4 - arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/process.c | 33 +- arch/riscv/kernel/return_address.c | 2 +- arch/riscv/kernel/stacktrace.c | 583 +++++++++++++++--- arch/riscv/kernel/tests/Kconfig.debug | 12 + arch/riscv/kernel/tests/Makefile | 1 + arch/riscv/kernel/tests/stacktrace_test.c | 142 +++++ .../livepatch/test_modules/test_klp_syscall.c | 2 + 19 files changed, 1043 insertions(+), 110 deletions(-) create mode 100644 arch/riscv/include/asm/stacktrace/common.h create mode 100644 arch/riscv/include/asm/stacktrace/frame.h create mode 100644 arch/riscv/kernel/tests/stacktrace_test.c
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.39.3
PATCH v7 1/10 riscv: stacktrace: do not report the walker's own frame from arch_stack_walk()
From: Karl Mehltretter <[email protected]>
stack_trace_save() uses skipnr + 1 because arch_stack_walk() is expected to first report the return into stack_trace_save(). With FRAME_POINTER, the RISC-V arch_stack_walk() calls walk_stackframe(), which starts from its own frame and first reports the return into arch_stack_walk(). stack_trace_save() therefore starts one frame too early. For example, page_owner records begin at __set_page_owner() instead of the allocation site.
Move the walker into an always-inlined helper and call it directly from arch_stack_walk(). Keep walk_stackframe() as a wrapper for its existing callers.
return_address() skips level + 3 entries, while the arm64 implementation skips level + 2. After removing the extra frame, use level + 2 so ftrace_return_address() and CALLER_ADDR results do not shift by one caller.
--- arch/riscv/kernel/return_address.c | 2 +- arch/riscv/kernel/stacktrace.c | 40 ++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-)
arch/riscv/kernel/return_address.c +1 -1
@@ -33,7 +33,7 @@ noinline void *return_address(unsigned int level) {struct return_address_data data; - data.level = level +3 ; + data.level = level +2 ; data.addr =NULL ;arch_stack_walk (save_return_addr, &data, current,NULL );
arch/riscv/kernel/stacktrace.c +35 -5
@@ -45,8 +45,16 @@ static inline int fp_is_valid(unsigned long fp, unsigned long sp)return !(fp < low || fp > high || fp &0x07 ); } -void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, -bool (*fn)(void *,unsigned long ),void *arg) +/* +* Always inlined so that the frame the walk starts from is the caller's own, +* and the first entry reported for the current task is the caller's return +* address. arch_stack_walk() relies on that to report the same frames as +* the other architectures, see the comment there. +*/ +static __always_inline void __walk_stackframe(struct task_struct *task, +struct pt_regs *regs, +bool (*fn)(void *,unsigned long ), +void *arg) {unsigned long fp, sp, pc;int graph_idx =0 ;
@@ -59,6 +67,7 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, }else if (task ==NULL || task == current) { fp = (unsigned long )__builtin_frame_address (0 ); sp = current_stack_pointer; +/* Placeholder for the frame the walk starts from, never reported. */ pc = (unsigned long )walk_stackframe; level =-1 ; }else {
@@ -102,10 +111,18 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, } } +void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, +bool (*fn)(void *,unsigned long ),void *arg) +{ +__walk_stackframe (task, regs, fn, arg); +} +#else /* !CONFIG_FRAME_POINTER */ -void notrace walk_stackframe(struct task_struct *task, -struct pt_regs *regs,bool (*fn)(void *,unsigned long ),void *arg) +static __always_inline void __walk_stackframe(struct task_struct *task, +struct pt_regs *regs, +bool (*fn)(void *,unsigned long ), +void *arg) {unsigned long sp, pc;unsigned long *ksp;
@@ -133,6 +150,12 @@ void notrace walk_stackframe(struct task_struct *task, } } +void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, +bool (*fn)(void *,unsigned long ),void *arg) +{ +__walk_stackframe (task, regs, fn, arg); +} +#endif /* CONFIG_FRAME_POINTER */ static bool print_trace_address (void *arg,unsigned long pc)
@@ -176,10 +199,17 @@ unsigned long __get_wchan(struct task_struct *task)return pc; } +/* +* The generic code expects the first entry for the current task to be the +* return address of arch_stack_walk() itself, the way x86 and arm64 report +* it, and stack_trace_save() sizes its skip count accordingly. Walking from +* inside a called walk_stackframe() adds that function's own frame on top, +* so inline the walker here and start from this frame instead. +*/ noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,struct task_struct *task,struct pt_regs *regs) { -walk_stackframe (task, regs, consume_entry, cookie); +__walk_stackframe (task, regs, consume_entry, cookie); }/*
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 01/10] riscv: stacktrace: do not report the walker's own frame from arch_stack_walk()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] riscv: stacktrace: off-by-one frame unwind in __walk_stackframe() for !FRAME_POINTER --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] riscv: stacktrace: off-by-one frame unwind in __walk_stackframe() for !FRAME_POINTER
--
commit 0ad8b77869cf04ae7e9c217cd0f43219461f68f7
Author: Karl Mehltretter <[email protected]>
riscv: stacktrace: do not report the walker's own frame from arch_stack_walk()
This commit moves the stack walker into an always-inlined __walk_stackframe() helper called directly from arch_stack_walk() and reduces the skip count in return_address() from level + 3 to level + 2.
Quoted 17 lines
arch/riscv/kernel/return_address.c +1
-1@@ -33,7 +33,7 @@ noinline void *return_address(unsigned int level) {struct return_address_data data; - data.level = level +3 ; + data.level = level +2 ; data.addr =NULL ;arch_stack_walk (save_return_addr, &data, current,NULL );arch/riscv/kernel/stacktrace.c +0
-0
[ ... ]
Quoted 11 lines
arch/riscv/kernel/stacktrace.c +6
-0@@ -102,10 +111,18 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, } } +void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, +bool (*fn)(void *,unsigned long ),void *arg) +{ +__walk_stackframe (task, regs, fn, arg); +} +#else /* !CONFIG_FRAME_POINTER */
arch/riscv/kernel/stacktrace.c void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, · line 128
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +4
-2-void notrace walk_stackframe(struct task_struct *task, -struct pt_regs *regs,bool (*fn)(void *,unsigned long ),void *arg) +static __always_inline void __walk_stackframe(struct task_struct *task, +struct pt_regs *regs, +bool (*fn)(void *,unsigned long ), +void *arg) {unsigned long sp, pc;unsigned long *ksp;
[Severity: Medium]
Does the !CONFIG_FRAME_POINTER version of __walk_stackframe() still report walk_stackframe as its first entry before scanning the stack?
arch/riscv/kernel/stacktrace.c:__walk_stackframe() {
...
} else if (task == NULL || task == current) {
sp = current_stack_pointer;
pc = (unsigned long)walk_stackframe;
} else {
...
while (!kstack_end(ksp)) {
if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
break;
pc = READ_ONCE_NOCHECK(*ksp++);
}
}Unlike the CONFIG_FRAME_POINTER version, which initializes level = -1 so the initial walk_stackframe placeholder is skipped, the !CONFIG_FRAME_POINTER loop passes pc = (unsigned long)walk_stackframe directly to fn() on the first iteration before reading arch_stack_walk()'s saved return address from *ksp++. As a result, when CONFIG_FRAME_POINTER is disabled, stack_trace_save() still starts one frame too early, and reducing data.level from level + 3 to level + 2 in return_address() causes return_address(), ftrace_return_address(), and CALLER_ADDRx to stop one caller too early.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v7 01/10] riscv: stacktrace: do not report the walker's own frame from arch_stack_walk()
Quoted 77 lines · On 11/10/26 9:52 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] riscv: stacktrace: off-by-one frame unwind in__walk_stackframe()for !FRAME_POINTER
--commit
0ad8b77869cf04ae7e9c217cd0f43219461f68f7
Author: Karl Mehltretter <[email protected]>riscv: stacktrace: do not report the walker's own frame from
arch_stack_walk()This commit moves the stack walker into an always-inlined
__walk_stackframe()helper called directly fromarch_stack_walk()and reduces the skip count inreturn_address()from level + 3 to level + 2.Quoted 17 lines
arch/riscv/kernel/return_address.c +1
-1@@ -33,7 +33,7 @@ noinline void *return_address(unsigned int level) {struct return_address_data data; - data.level = level +3 ; + data.level = level +2 ; data.addr =NULL ;arch_stack_walk (save_return_addr, &data, current,NULL );arch/riscv/kernel/stacktrace.c +0
-0[ ... ]
Quoted 11 lines
arch/riscv/kernel/stacktrace.c +6
-0@@ -102,10 +111,18 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, } } +void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, +bool (*fn)(void *,unsigned long ),void *arg) +{ +__walk_stackframe (task, regs, fn, arg); +} +#else /* !CONFIG_FRAME_POINTER */ arch/riscv/kernel/stacktrace.c void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, · line 128
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +4
-2-void notrace walk_stackframe(struct task_struct *task, -struct pt_regs *regs,bool (*fn)(void *,unsigned long ),void *arg) +static __always_inline void __walk_stackframe(struct task_struct *task, +struct pt_regs *regs, +bool (*fn)(void *,unsigned long ), +void *arg) {unsigned long sp, pc;unsigned long *ksp;[Severity: Medium]
Does the !CONFIG_FRAME_POINTER version of__walk_stackframe()still reportwalk_stackframeas its first entry before scanning the stack?arch/riscv/kernel/stacktrace.c:__walk_stackframe() { ... } else if (task == NULL || task == current) { sp = current_stack_pointer; pc = (unsigned long)walk_stackframe; } else { ... while (!kstack_end(ksp)) { if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) break; pc = READ_ONCE_NOCHECK(*ksp++); } }
Quoted 9 lines
Unlike the
CONFIG_FRAME_POINTERversion, which initializes level = -1 so the initialwalk_stackframeplaceholder is skipped, the !CONFIG_FRAME_POINTER loop passes pc = (unsigned long)walk_stackframe directly tofn()on the first iteration before reading arch_stack_walk()'s saved return address from *ksp++. As a result, whenCONFIG_FRAME_POINTERis disabled,stack_trace_save()still starts one frame too early, and reducing data.level from level + 3 to level + 2 inreturn_address()causesreturn_address(),ftrace_return_address(), andCALLER_ADDRxto stop one caller too early.
Yes, you are right.
Will fix it in next version.
Thanks.
Shuai
PATCH v7 2/10 riscv: Add KUnit test for stack trace start frames
In reply to Shuai XueFrom: Karl Mehltretter <[email protected]>
stack_trace_save() and return_address() depend on the first frame reported by arch_stack_walk(), but no test checks their frame numbering.
Add a test that calls both through a chain of three noinline functions and compares the symbol of the result with the function expected for each skip count or level.
Require FRAME_POINTER because the stack-scanning walker cannot provide an exact start frame.
--- arch/riscv/kernel/tests/Kconfig.debug | 12 ++ arch/riscv/kernel/tests/Makefile | 1 + arch/riscv/kernel/tests/stacktrace_test.c | 142 ++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 arch/riscv/kernel/tests/stacktrace_test.c
arch/riscv/kernel/tests/Kconfig.debug +12 -0
@@ -42,6 +42,18 @@ config RISCV_KPROBES_KUNIT If unsure, say N. +config RISCV_STACKTRACE_KUNIT_TEST + bool "KUnit tests for RISC-V stack trace start frames" + depends on KUNIT && STACKTRACE && FRAME_POINTER && KALLSYMS + help + Check that stack_trace_save() starts at its caller and skips exactly + the requested number of frames through a known call chain, and that + ftrace_return_address(), which CALLER_ADDR1 and up are built on, + returns the caller it was asked for. This catches a stack walker + that exposes its own implementation frames to its users. + + If unsure, say N. + endif # RUNTIME_TESTING_MENU endmenu # "arch/riscv/kernel runtime Testing"
arch/riscv/kernel/tests/Makefile +1 -0
@@ -1,2 +1,3 @@ obj-$(CONFIG_RISCV_MODULE_LINKING_KUNIT) += module_test/ obj-$(CONFIG_RISCV_KPROBES_KUNIT) += kprobes/ +obj-$(CONFIG_RISCV_STACKTRACE_KUNIT_TEST) += stacktrace_test.o
arch/riscv/kernel/tests/stacktrace_test.c new file +142 -0
142 lines not on this page · raw message
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 02/10] riscv: Add KUnit test for stack trace start frames
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] riscv: vmlinux link failure when CONFIG_KUNIT=m with RISCV_STACKTRACE_KUNIT_TEST --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] riscv: vmlinux link failure when CONFIG_KUNIT=m with RISCV_STACKTRACE_KUNIT_TEST
--
commit 88e1ac2e959d970cabee9d276d02925cc3d44f40
Author: Karl Mehltretter <[email protected]>
riscv: Add KUnit test for stack trace start frames
This commit adds a KUnit test suite to verify the starting frame and skip counts of stack_trace_save() and ftrace_return_address() on RISC-V.
arch/riscv/kernel/tests/Kconfig.debug config RISCV_KPROBES_KUNIT · line 47
Quoted 11 lines
arch/riscv/kernel/tests/Kconfig.debug +3
-0@@ -42,6 +42,18 @@ config RISCV_KPROBES_KUNIT If unsure, say N. +config RISCV_STACKTRACE_KUNIT_TEST + bool "KUnit tests for RISC-V stack trace start frames" + depends on KUNIT && STACKTRACE && FRAME_POINTER && KALLSYMS
[Severity: Medium]
Does declaring RISCV_STACKTRACE_KUNIT_TEST as bool while depending on tristate KUNIT break the vmlinux link when CONFIG_KUNIT=m and CONFIG_RISCV_STACKTRACE_KUNIT_TEST=y (such as under allmodconfig)?
In that configuration, stacktrace_test.o is built into vmlinux while the KUnit core symbols it calls (__kunit_do_failed_assertion, kunit_log_append, etc.) live in kunit.ko, similar to the issue fixed for RISCV_KPROBES_KUNIT in 5fada1605733 ("riscv: tests: Make RISCV_KPROBES_KUNIT tristate").
Should this depend on KUNIT=y instead (since building stacktrace_test as a module would also cause sprint_symbol_no_offset() in riscv_stacktrace_check_skip() and riscv_return_address_check() to append " [stacktrace_test]" to the symbol names)?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v7 02/10] riscv: Add KUnit test for stack trace start frames
Quoted 38 lines · On 11/10/26 9:50 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] riscv: vmlinux link failure whenCONFIG_KUNIT=mwithRISCV_STACKTRACE_KUNIT_TEST
--commit
88e1ac2e959d970cabee9d276d02925cc3d44f40
Author: Karl Mehltretter <[email protected]>riscv: Add KUnit test for stack trace start frames
This commit adds a KUnit test suite to verify the starting frame and skip counts of
stack_trace_save()andftrace_return_address()on RISC-V.arch/riscv/kernel/tests/Kconfig.debug config RISCV_KPROBES_KUNIT · line 47
Quoted 11 lines
arch/riscv/kernel/tests/Kconfig.debug +3
-0@@ -42,6 +42,18 @@ config RISCV_KPROBES_KUNIT If unsure, say N. +config RISCV_STACKTRACE_KUNIT_TEST + bool "KUnit tests for RISC-V stack trace start frames" + depends on KUNIT && STACKTRACE && FRAME_POINTER && KALLSYMS[Severity: Medium]
Does declaringRISCV_STACKTRACE_KUNIT_TESTas bool while depending on tristate KUNIT break the vmlinux link whenCONFIG_KUNIT=mandCONFIG_RISCV_STACKTRACE_KUNIT_TEST=y(such as under allmodconfig)?In that configuration, stacktrace_test.o is built into vmlinux while the KUnit core symbols it calls (
__kunit_do_failed_assertion,kunit_log_append, etc.) live in kunit.ko, similar to the issue fixed forRISCV_KPROBES_KUNITin5fada1605733("riscv: tests: MakeRISCV_KPROBES_KUNITtristate").
Quoted 5 lines
Should this depend on KUNIT=y instead (since building
stacktrace_testas a module would also causesprint_symbol_no_offset()inriscv_stacktrace_check_skip()andriscv_return_address_check()to append " [stacktrace_test]" to the symbol names)?
Yes. Will fix in next version.
Thanks.
Shuai
PATCH v7 3/10 riscv: ftrace: use the frame pointer as the function graph return-address cookie
In reply to Shuai XueThe function graph tracer stamps every shadow-stack entry with a cookie, ftrace_ret_stack::retp, which stack unwinders later pass to ftrace_graph_ret_addr() to recover a return address that the tracer replaced with return_to_handler. The match is an exact pointer comparison, so the producer and the consumer must agree on what the cookie is. On riscv they don't:
* The dynamic ftrace path stores &arch_ftrace_regs(fregs)->ra, a slot inside the transient ftrace trampoline frame. The frame is popped as soon as the callback returns, so the stored cookie is an address that no consumer can ever reproduce. It has never matched anything since ftrace_graph_func() was introduced in v6.8.
* The frame-pointer walker in walk_stackframe() passes &frame->ra, the ra slot of a live frame record. That key matches the static mcount path, which passes the callee's ra-slot address, but not the dynamic path.
Since distro kernels only ever use the dynamic path, the net effect is that the shadow stack is never consulted during unwinding:
whenever the function graph tracer is running, dump_stack(), perf callchains and __get_wchan() report return_to_handler instead of the real caller.
Switch the cookie to the architectural frame pointer, i.e. the caller's frame-pointer value. Every party can reproduce it from the frame records themselves:
* ftrace.c stores (void *)frame_pointer on both entry paths. At ftrace entry the frame pointer is still the caller's, which is the value the callee later saves in its frame_record::fp.
* walk_stackframe() passes that value, frame->fp, to ftrace_graph_ret_addr() instead of the ra-slot address. The static mcount path has to switch too: its old cookie (the callee's ra-slot address) is exactly what the old walker key matched, so leaving it behind would regress the static path.
This is the convention arm64 already uses: (void *)frame_pointer from ftrace.c, the saved fp from the unwinder.
function_get_true_parent_ip() keeps passing the entry stack pointer as its key; that key is only reproducible on architectures whose return address lives at the entry stack pointer (x86). On riscv, as on arm64, it keeps falling back to the raw parent ip.
--- arch/riscv/kernel/ftrace.c | 6 ++++-- arch/riscv/kernel/stacktrace.c | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-)
arch/riscv/kernel/ftrace.c +4 -2
@@ -245,7 +245,8 @@ void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,*/ old = *parent; -if (!function_graph_enter (old, self_addr, frame_pointer, parent)) +if (!function_graph_enter (old, self_addr, frame_pointer, + (void *)frame_pointer)) *parent = return_hooker; }
@@ -267,7 +268,8 @@ void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,*/ old = *parent; -if (!function_graph_enter_regs (old, ip, frame_pointer, parent, fregs)) +if (!function_graph_enter_regs (old, ip, frame_pointer, + (void *)frame_pointer, fregs)) *parent = return_hooker; }#endif /* CONFIG_DYNAMIC_FTRACE */
arch/riscv/kernel/stacktrace.c +2 -1
@@ -96,8 +96,9 @@ static __always_inline void __walk_stackframe(struct task_struct *task, }else { fp =READ_ONCE_TASK_STACK (task, frame->fp); pc =READ_ONCE_TASK_STACK (task, frame->ra); +/* the cookie is the caller's frame pointer */ pc =ftrace_graph_ret_addr (task, &graph_idx, pc, - &frame->ra); + (void *)fp);if (pc >= (unsigned long )handle_exception && pc < (unsigned long )&ret_from_exception_end) {if (unlikely (!fn (arg, pc)))
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 03/10] riscv: ftrace: use the frame pointer as the function graph return-address cookie
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
The function graph tracer stamps every shadow-stack entry with a cookie,
ftrace_ret_stack::retp, which stack unwinders later pass toftrace_graph_ret_addr()to recover a return address that the tracer replaced withreturn_to_handler. The match is an exact pointer comparison, so the producer and the consumer must agree on what the cookie is. On riscv they don't:* The dynamic ftrace path stores &arch_ftrace_regs(fregs)->ra, a slot inside the transient ftrace trampoline frame. The frame is popped as soon as the callback returns, so the stored cookie is an address that no consumer can ever reproduce. It has never matched anything since
ftrace_graph_func()was introduced in v6.8.
Quoted 7 lines
* The frame-pointer walker in
walk_stackframe()passes&frame->ra,
[ ... ]Fixes 35e61e8827ee8 ("riscv: ftrace: Make function graph use ftrace directly")Suggested-by Martin Kaiser <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v7 4/10 riscv: stacktrace: Add frame record metadata
In reply to Shuai XueFrom: Wang Han <[email protected]>
Reliable frame-pointer unwinding needs an explicit way to identify exception boundaries and the final entry frame. The existing unwinder infers those boundaries from return addresses, which is too loose for a future reliable unwinder.
Add a small metadata frame record to pt_regs and initialize it on exception entry, kernel stack overflow, kernel thread fork, user fork, and early idle task setup. The record uses a zero {fp, ra} sentinel plus a type field so a later unwinder can distinguish a final user-to-kernel boundary from a nested kernel pt_regs boundary.
This follows the arm64 metadata frame-record model, adapted to the RISC-V {fp, ra} frame record convention.
The metadata is established at the RISC-V entry boundaries that need an explicit unwind marker:
* exception entry clears the metadata {fp, ra} pair and uses SPP (or MPP in M-mode) to record whether the pt_regs frame is the final user-to-kernel boundary or a nested kernel boundary;
* the kernel stack overflow path builds a nested pt_regs metadata record on the overflow stack so an unwinder can resume from the pre-overflow s0 saved in PT_S0;
* _start_kernel builds the init task's final metadata record, while the secondary CPU path sets up s0 before smp_callin() so idle-task unwinding does not inherit an undefined caller frame;
* copy_thread creates matching final metadata records for new kernel and user tasks, and keeps s0 available for the frame-pointer chain.
Keep the embedded metadata-record field offsets distinct from the s0-relative STACKFRAME_* offsets used by call_on_irq_stack(), because the latter describe a frame record relative to s0 rather than to the record base.
These changes keep s0 reserved for the frame-pointer chain at task and exception boundaries.
--- arch/riscv/include/asm/ptrace.h | 9 ++++ arch/riscv/include/asm/stacktrace/frame.h | 53 +++++++++++++++++++++++ arch/riscv/kernel/asm-offsets.c | 6 +++ arch/riscv/kernel/entry.S | 39 ++++++++++++++++- arch/riscv/kernel/head.S | 23 ++++++++++ arch/riscv/kernel/process.c | 33 +++++++++++++- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 arch/riscv/include/asm/stacktrace/frame.h
arch/riscv/include/asm/ptrace.h +9 -0
@@ -8,6 +8,7 @@#include <uapi/asm/ptrace.h> #include <asm/csr.h> +#include <asm/stacktrace/frame.h> #include <linux/compiler.h> #ifndef __ASSEMBLER__
@@ -53,6 +54,14 @@ struct pt_regs {unsigned long cause;/* a0 value before the syscall */ unsigned long orig_a0; + +/* +* This frame record is entirely zeroed on exception entry, allowing the +* unwinder to identify exception boundaries. The type field encodes +* whether the exception was taken from user (FINAL) or kernel (PT_REGS) +* mode. +*/ +struct frame_record_meta stackframe; };#define PTRACE_SYSEMU 0x1f
arch/riscv/include/asm/stacktrace/frame.h new file +53 -0
53 lines not on this page · raw message
arch/riscv/kernel/asm-offsets.c +6 -0
@@ -131,6 +131,9 @@ void asm_offsets(void)OFFSET (PT_BADADDR , pt_regs, badaddr);OFFSET (PT_CAUSE , pt_regs, cause); +DEFINE (S_STACKFRAME , offsetof(struct pt_regs , stackframe)); + DEFINE(S_STACKFRAME_TYPE, offsetof(struct pt_regs, stackframe.type)); +OFFSET (SUSPEND_CONTEXT_REGS , suspend_context, regs);OFFSET (HIBERN_PBE_ADDR , pbe, address);
@@ -503,6 +506,9 @@ void asm_offsets(void)DEFINE (STACKFRAME_SIZE_ON_STACK ,ALIGN (sizeof (struct stackframe ),STACK_ALIGN ));DEFINE (STACKFRAME_FP , offsetof(struct stackframe , fp) -sizeof (struct stackframe ));DEFINE (STACKFRAME_RA , offsetof(struct stackframe , ra) -sizeof (struct stackframe )); +DEFINE (STACKFRAME_RECORD_SIZE ,sizeof (struct frame_record )); +OFFSET (FRAME_RECORD_FP , frame_record, fp); +OFFSET (FRAME_RECORD_RA , frame_record, ra);#ifdef CONFIG_FUNCTION_TRACER DEFINE (FTRACE_OPS_FUNC , offsetof(struct ftrace_ops , func));#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
arch/riscv/kernel/entry.S +37 -2
@@ -11,6 +11,7 @@ #include <asm/asm.h> #include <asm/csr.h> #include <asm/scs.h> +#include <asm/stacktrace/frame.h> #include <asm/unistd.h> #include <asm/page.h> #include <asm/thread_info.h>
@@ -198,6 +199,27 @@ SYM_CODE_START(handle_exception) REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + /* + * Create a metadata frame record. The unwinder will use this to + * identify and unwind exception boundaries. + */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) /* stackframe.record.fp = 0 */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) /* stackframe.record.ra = 0 */ +#ifdef CONFIG_RISCV_M_MODE + li t0, SR_MPP + and t0, s1, t0 +#else + andi t0, s1, SR_SPP +#endif + bnez t0, 1f + li t0, FRAME_META_TYPE_FINAL + j 2f +1: + li t0, FRAME_META_TYPE_PT_REGS +2: + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + /* * Set the scratch register to 0, so that if a recursive exception * occurs, the exception vector knows it came from the kernel
@@ -354,6 +376,19 @@ SYM_CODE_START_LOCAL(handle_kernel_stack_overflow) REG_S s3, PT_BADADDR(sp) REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + + /* + * Create a metadata frame record for the overflow pt_regs. The + * overflow path is entered from kernel context, so this is a nested + * pt_regs boundary and the unwinder can resume from the pre-overflow + * frame pointer saved in PT_S0. + */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) + li t0, FRAME_META_TYPE_PT_REGS + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + move a0, sp tail handle_bad_stack SYM_CODE_END(handle_kernel_stack_overflow)
@@ -362,8 +397,8 @@ ASM_NOKPROBE(handle_kernel_stack_overflow) SYM_CODE_START(ret_from_fork_kernel_asm) call schedule_tail - move a0, s1 /* fn_arg */ - move a1, s0 /* fn */ + move a0, s3 /* fn_arg */ + move a1, s2 /* fn */ move a2, sp /* pt_regs */ call ret_from_fork_kernel j ret_from_exception
arch/riscv/kernel/head.S +23 -0
@@ -14,6 +14,7 @@ #include <asm/hwcap.h> #include <asm/image.h> #include <asm/scs.h> +#include <asm/stacktrace/frame.h> #include <asm/usercfi.h> #include <asm/alternative.h> #include <asm/errata_list.h>
@@ -179,6 +180,14 @@ secondary_start_sbi: REG_S a0, (a1) 1: #endif + + /* + * Set up the frame pointer for the secondary idle task so reliable + * stack unwinding terminates at the metadata frame in task_pt_regs(). + * Without this, the first frame records can inherit an undefined caller + * fp and unwind past smp_callin() into .Lsecondary_park. + */ + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE scs_load_current call smp_callin #endif /* CONFIG_SMP */
@@ -307,6 +316,20 @@ SYM_CODE_START(_start_kernel) la tp, init_task la sp, init_thread_union + THREAD_SIZE addi sp, sp, -PT_SIZE_ON_STACK + + /* + * Set up a metadata frame record for the init task so that + * the unwinder can identify the outermost frame by its + * {fp, ra} = {0, 0} sentinel at the bottom of pt_regs. + * fp/s0 points above the metadata record (RISC-V + * convention). + */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) + li t0, FRAME_META_TYPE_FINAL + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + #if defined(CONFIG_RISCV_SBI) && defined(CONFIG_RISCV_USER_CFI) li a7, SBI_EXT_FWFT li a6, SBI_EXT_FWFT_SET
arch/riscv/kernel/process.c +31 -2
@@ -258,8 +258,23 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)/* Supervisor/Machine, irqs on: */ childregs->status =SR_PP |SR_PIE ; - p->thread.s[0 ] = (unsigned long )args->fn; - p->thread.s[1 ] = (unsigned long )args->fn_arg; +/* +* Set up a metadata frame record at the bottom of the +* stack for the unwinder. Use FRAME_META_TYPE_FINAL +* since this is the outermost kernel entry for the new +* task. The frame_record::{fp,ra} are already zero from +* memset(). +* +* fp/s0 points above the metadata record (RISC-V +* convention). fn and fn_arg are passed via s2/s3, +* keeping s0 available for the frame pointer chain. +*/ + childregs->stackframe.type =FRAME_META_TYPE_FINAL ; + + p->thread.s[0 ] = (unsigned long )(&childregs->stackframe) + +sizeof (struct frame_record ); + p->thread.s[2 ] = (unsigned long )args->fn; + p->thread.s[3 ] = (unsigned long )args->fn_arg; p->thread.ra = (unsigned long )ret_from_fork_kernel_asm; }else {/* allocate new shadow stack if needed. In case of CLONE_VM we have to */
@@ -278,6 +293,20 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)if (clone_flags &CLONE_SETTLS ) childregs->tp = tls; childregs->a0 =0 ;/* Return value of fork() */ + +/* +* Set up the unwind boundary: ensure the metadata +* frame record has its {fp,ra} sentinel zeroed and +* point fp/s0 above the metadata record. Mark it as +* FINAL since this is the outermost kernel entry for +* the new task. +*/ + childregs->stackframe.record.fp =0 ; + childregs->stackframe.record.ra =0 ; + childregs->stackframe.type =FRAME_META_TYPE_FINAL ; + p->thread.s[0 ] = (unsigned long )(&childregs->stackframe) + +sizeof (struct frame_record ); + p->thread.ra = (unsigned long )ret_from_fork_user_asm; } p->thread.riscv_v_flags =0 ;
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 04/10] riscv: stacktrace: Add frame record metadata
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] riscv: entry: use call instead of tail for handle_bad_stack() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] riscv: entry: use call instead of tail for handle_bad_stack() --
commit ed05329452fecde9e16a00910af1efec91f2922b
Author: Wang Han <[email protected]>
riscv: stacktrace: Add frame record metadata
This commit adds a metadata frame record to struct pt_regs and initializes it across exception, overflow, boot, and task-creation entry boundaries for frame-pointer stack unwinding.
Quoted 4 lines
arch/riscv/kernel/entry.S +0
-0
[ ... ]
Quoted 10 lines
arch/riscv/kernel/entry.S +6
-0@@ -354,6 +376,19 @@ SYM_CODE_START_LOCAL(handle_kernel_stack_overflow) REG_S s3, PT_BADADDR(sp) REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + + /* + * Create a metadata frame record for the overflow pt_regs. The + * overflow path is entered from kernel context, so this is a nested + * pt_regs boundary and the unwinder can resume from the pre-overflow + * frame pointer saved in PT_S0.
arch/riscv/kernel/entry.S SYM_CODE_START_LOCAL(handle_kernel_stack_overflow) · line 394
Quoted 10 lines
arch/riscv/kernel/entry.S +7
-0+ */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) + li t0, FRAME_META_TYPE_PT_REGS + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + move a0, sp tail handle_bad_stack SYM_CODE_END(handle_kernel_stack_overflow)
[Severity: Medium]
Should this use "call handle_bad_stack" instead of "tail handle_bad_stack"?
Because tail does not update ra, handle_bad_stack()'s prologue saves a frame record on the overflow stack that pairs the new s0 (pointing at the overflow pt_regs metadata record) with the interrupted pre-overflow ra. When panic() unwinds from handle_bad_stack(), the unwinder consumes regs->ra before reaching the pt_regs metadata record, and if that return address was hooked by function_graph or kretprobes, recovering it with the overflow-stack fp fails and aborts the stack trace before reaching the overflowed task stack.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v7 04/10] riscv: stacktrace: Add frame record metadata
Quoted 45 lines · On 11/10/26 9:54 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] riscv: entry: use call instead of tail forhandle_bad_stack()--commit
ed05329452fecde9e16a00910af1efec91f2922b
Author: Wang Han <[email protected]>riscv: stacktrace: Add frame record metadata
This commit adds a metadata frame record to struct
pt_regsand initializes it across exception, overflow, boot, and task-creation entry boundaries for frame-pointer stack unwinding.Quoted 4 lines
arch/riscv/kernel/entry.S +0
-0[ ... ]
Quoted 10 lines
arch/riscv/kernel/entry.S +6
-0@@ -354,6 +376,19 @@ SYM_CODE_START_LOCAL(handle_kernel_stack_overflow) REG_S s3, PT_BADADDR(sp) REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + + /* + * Create a metadata frame record for the overflow pt_regs. The + * overflow path is entered from kernel context, so this is a nested + * pt_regs boundary and the unwinder can resume from the pre-overflow + * frame pointer saved in PT_S0.arch/riscv/kernel/entry.S SYM_CODE_START_LOCAL(handle_kernel_stack_overflow) · line 394
Quoted 10 lines
arch/riscv/kernel/entry.S +7
-0+ */ + REG_S zero, (S_STACKFRAME + FRAME_RECORD_FP)(sp) + REG_S zero, (S_STACKFRAME + FRAME_RECORD_RA)(sp) + li t0, FRAME_META_TYPE_PT_REGS + REG_S t0, S_STACKFRAME_TYPE(sp) + addi s0, sp, S_STACKFRAME + STACKFRAME_RECORD_SIZE + move a0, sp tail handle_bad_stack SYM_CODE_END(handle_kernel_stack_overflow)[Severity: Medium]
Should this use "callhandle_bad_stack" instead of "tailhandle_bad_stack"?
Quoted 9 lines
Because tail does not update ra, handle_bad_stack()'s prologue saves a frame record on the overflow stack that pairs the new s0 (pointing at the overflow
pt_regsmetadata record) with the interrupted pre-overflow ra. Whenpanic()unwinds fromhandle_bad_stack(), the unwinder consumesregs->rabefore reaching thept_regsmetadata record, and if that return address was hooked byfunction_graphor kretprobes, recovering it with the overflow-stack fp fails and aborts the stack trace before reaching the overflowed task stack.
Yes, will use "call handle_bad_stack".
Thanks.
Shuai
PATCH v7 5/10 riscv: stacktrace: disable KASAN and KCOV instrumentation for stacktrace.o
In reply to Shuai XueFrom: Wang Han <[email protected]>
KASAN records stack traces for every alloc/free, which means it walks the unwinder very frequently. Instrumenting the stack trace collection code itself adds substantial overhead and makes the traces themselves noisier.
KCOV instruments every basic-block edge. The unwinder is a hot path, especially with KASAN enabled, so KCOV instrumentation has the same kind of cost and noise problem here.
Mark stacktrace.o as not KASAN- or KCOV-instrumented, matching the x86 treatment of its stack unwinding code. RISC-V keeps the relevant unwinder code in stacktrace.o, so a single translation-unit annotation covers the equivalent scope. This is a prerequisite preference for the upcoming reliable unwinder, but the change is valid on its own.
--- arch/riscv/kernel/Makefile | 6 ++++++ 1 file changed, 6 insertions(+)
arch/riscv/kernel/Makefile +6 -0
@@ -44,6 +44,12 @@ CFLAGS_REMOVE_return_address.o = $(CC_FLAGS_FTRACE) CFLAGS_REMOVE_sbi_ecall.o = $(CC_FLAGS_FTRACE) endif +# When KASAN is enabled, a stack trace is recorded for every alloc/free, which +# can significantly impact performance. Avoid instrumenting the stack trace +# collection code to minimize this impact. +KASAN_SANITIZE_stacktrace.o := n +KCOV_INSTRUMENT_stacktrace.o := n + always-$(KBUILD_BUILTIN) += vmlinux.lds obj-y += head.o
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 05/10] riscv: stacktrace: disable KASAN and KCOV instrumentation for stacktrace.o
Sashiko has reviewed this patch and found no issues.
Quoted 17 lines
From: Wang Han <[email protected]>
KASAN records stack traces for every alloc/free, which means it walks the unwinder very frequently. Instrumenting the stack trace collection code itself adds substantial overhead and makes the traces themselves noisier.
KCOV instruments every basic-block edge. The unwinder is a hot path, especially with KASAN enabled, so KCOV instrumentation has the same kind of cost and noise problem here.
Mark stacktrace.o as not KASAN- or KCOV-instrumented, matching the x86 treatment of its stack unwinding code. RISC-V keeps the relevant unwinder code in stacktrace.o, so a single translation-unit annotation covers the equivalent scope. This is a prerequisite preference for the upcoming reliable unwinder, but the change is valid on its own.
Quoted 5 lines
Reviewed-by Shuai Xue <[email protected]>Reviewed-by Martin Kaiser <[email protected]>Tested-by Martin Kaiser <[email protected]>Signed-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v7 6/10 riscv: ftrace: always preserve s0 in dynamic ftrace register frame
In reply to Shuai XueFrom: Wang Han <[email protected]>
struct __arch_ftrace_regs declares s0 unconditionally, and both ftrace_regs_get_frame_pointer() and ftrace_partial_regs() read it unconditionally. But the SAVE_ABI_REGS / RESTORE_ABI_REGS macros in mcount-dyn.S only stored s0 under HAVE_FUNCTION_GRAPH_FP_TEST (CONFIG_FUNCTION_GRAPH_TRACER && CONFIG_FRAME_POINTER). With CONFIG_FRAME_POINTER=n the slot held whatever was on the stack before, so any callback going through ftrace_partial_regs() saw a garbage regs->s0. RISC-V kernels default to FRAME_POINTER=y, which is why this has not bitten in practice.
Save and restore s0 unconditionally in the dynamic ftrace ABI register frame. This fixes the latent garbage-s0 case, brings the dynamic ftrace path in line with the static _mcount path (mcount.S SAVE_ABI_STATE already saves s0 unconditionally), and matches the frame layout already documented in the comment above SAVE_ABI_REGS. It is also a prerequisite for the upcoming reliable unwinder, which reads ftrace_regs_get_frame_pointer(fregs) directly.
The cost is one extra REG_S/REG_L pair per traced call, negligible compared to the overall ftrace cost; the existing FREGS_SIZE_ON_STACK already reserved the slot, so no extra stack space is used.
--- arch/riscv/kernel/mcount-dyn.S | 4 ---- 1 file changed, 4 deletions(-)
arch/riscv/kernel/mcount-dyn.S +0 -4
@@ -85,9 +85,7 @@ addi sp, sp, -FREGS_SIZE_ON_STACK REG_S t0, FREGS_EPC(sp) REG_S x1, FREGS_RA(sp) -#ifdef HAVE_FUNCTION_GRAPH_FP_TEST REG_S x8, FREGS_S0(sp) -#endif REG_S x6, FREGS_T1(sp) #ifdef CONFIG_CC_IS_CLANG REG_S x7, FREGS_T2(sp)
@@ -113,9 +111,7 @@ .macro RESTORE_ABI_REGS REG_L t0, FREGS_EPC(sp) REG_L x1, FREGS_RA(sp) -#ifdef HAVE_FUNCTION_GRAPH_FP_TEST REG_L x8, FREGS_S0(sp) -#endif REG_L x6, FREGS_T1(sp) #ifdef CONFIG_CC_IS_CLANG REG_L x7, FREGS_T2(sp)
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 06/10] riscv: ftrace: always preserve s0 in dynamic ftrace register frame
Sashiko has reviewed this patch and found no issues.
Quoted 24 lines
From: Wang Han <[email protected]>
struct
__arch_ftrace_regsdeclares s0 unconditionally, and bothftrace_regs_get_frame_pointer()andftrace_partial_regs()read it unconditionally. But theSAVE_ABI_REGS/RESTORE_ABI_REGSmacros inmcount-dyn.Sonly stored s0 underHAVE_FUNCTION_GRAPH_FP_TEST(CONFIG_FUNCTION_GRAPH_TRACER&&CONFIG_FRAME_POINTER). WithCONFIG_FRAME_POINTER=nthe slot held whatever was on the stack before, so any callback going throughftrace_partial_regs()saw a garbageregs->s0. RISC-V kernels default to FRAME_POINTER=y, which is why this has not bitten in practice.Save and restore s0 unconditionally in the dynamic ftrace ABI register frame. This fixes the latent garbage-s0 case, brings the dynamic ftrace path in line with the static
_mcountpath (mcount.SSAVE_ABI_STATEalready saves s0 unconditionally), and matches the frame layout already documented in the comment aboveSAVE_ABI_REGS. It is also a prerequisite for the upcoming reliable unwinder, which readsftrace_regs_get_frame_pointer(fregs)directly.The cost is one extra REG_S/REG_L pair per traced call, negligible compared to the overall ftrace cost; the existing
FREGS_SIZE_ON_STACKalready reserved the slot, so no extra stack space is used.
Quoted 5 lines
Reviewed-by Shuai Xue <[email protected]>Reviewed-by Martin Kaiser <[email protected]>Tested-by Martin Kaiser <[email protected]>Signed-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v7 7/10 riscv: stacktrace: introduce stack-bound tracking helpers
In reply to Shuai XueFrom: Wang Han <[email protected]>
A reliable unwinder needs to validate that every frame record it reads is fully contained in a known kernel stack, and it needs to refuse to walk back into a stack it has already left. Add the building blocks for that:
* struct stack_info / struct unwind_state in a new asm/stacktrace/common.h, modelled on the arm64 reference implementation.
* stackinfo_get_irq() / stackinfo_get_task() / stackinfo_get_overflow() plus the corresponding on_*_stack() predicates in asm/stacktrace.h, so callers can ask "is this object on stack X?" by stack kind rather than open-coded address arithmetic.
* unwind_init_common(), unwind_find_stack() and unwind_consume_stack() helpers that enforce the forward-progress-only invariant required for reliability.
No existing user is wired up to these helpers in this commit; the unwinder switch comes in a follow-up. The header changes leave on_thread_stack() with the same semantics as before, just expressed in terms of the new helpers.
--- arch/riscv/include/asm/stacktrace.h | 67 ++++++++- arch/riscv/include/asm/stacktrace/common.h | 159 +++++++++++++++++++++ 2 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/stacktrace/common.h
arch/riscv/include/asm/stacktrace.h +65 -2
@@ -3,8 +3,13 @@ #ifndef _ASM_RISCV_STACKTRACE_H #define _ASM_RISCV_STACKTRACE_H +#include <linux/percpu.h> #include <linux/sched.h> +#include <linux/sched/task_stack.h> + +#include <asm/irq_stack.h> #include <asm/ptrace.h> +#include <asm/stacktrace/common.h> struct stackframe { unsigned long fp;
@@ -16,14 +21,72 @@ extern void notrace walk_stackframe(struct task_struct *task, struct pt_regs *reextern void dump_backtrace (struct pt_regs *regs,struct task_struct *task,const char *loglvl); -static inline bool on_thread_stack (void ) +/* +* IRQ stack accessors +*/ +#ifdef CONFIG_IRQ_STACKS +static inline struct stack_info stackinfo_get_irq (void ) +{ +unsigned long low = (unsigned long )raw_cpu_read (irq_stack_ptr); +unsigned long high = low +IRQ_STACK_SIZE ; + +return (struct stack_info ) { + .low = low, + .high = high, + }; +} + +static inline bool on_irq_stack (unsigned long sp,unsigned long size) +{ +struct stack_info info =stackinfo_get_irq (); + +return stackinfo_on_stack (&info, sp, size); +} +#endif /* CONFIG_IRQ_STACKS */ + +/* +* Task stack accessors +*/ +static inline struct stack_info stackinfo_get_task (const struct task_struct *tsk) { -return !(((unsigned long )(current->stack) ^ current_stack_pointer) & ~(THREAD_SIZE -1 )); +unsigned long low = (unsigned long )task_stack_page (tsk); +unsigned long high = low +THREAD_SIZE ; + +return (struct stack_info ) { + .low = low, + .high = high, + }; +} + +static inline bool on_task_stack (const struct task_struct *tsk, +unsigned long sp,unsigned long size) +{ +struct stack_info info =stackinfo_get_task (tsk); + +return stackinfo_on_stack (&info, sp, size); } +/* +* Cast is necessary since current->stack is an opaque ptr. +*/ +#define on_thread_stack () (on_task_stack(current, current_stack_pointer, 1)) +/* +* Overflow stack accessors +*/ #ifdef CONFIG_VMAP_STACK DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack); + +static inline struct stack_info stackinfo_get_overflow (void ) +{ +unsigned long low = (unsigned long )raw_cpu_ptr (overflow_stack); +unsigned long high = low +OVERFLOW_STACK_SIZE ; + +return (struct stack_info ) { + .low = low, + .high = high, + }; +}#endif /* CONFIG_VMAP_STACK */ #endif /* _ASM_RISCV_STACKTRACE_H */
arch/riscv/include/asm/stacktrace/common.h new file +159 -0
159 lines not on this page · raw message
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 07/10] riscv: stacktrace: introduce stack-bound tracking helpers
Sashiko has reviewed this patch and found no issues.
Quoted 23 lines
From: Wang Han <[email protected]>
A reliable unwinder needs to validate that every frame record it reads is fully contained in a known kernel stack, and it needs to refuse to walk back into a stack it has already left. Add the building blocks for that:
* struct
stack_info/ structunwind_statein a newasm/stacktrace/common.h, modelled on the arm64 reference implementation.
*stackinfo_get_irq()/stackinfo_get_task()/stackinfo_get_overflow()plus the corresponding on_*_stack() predicates inasm/stacktrace.h, so callers can ask "is this object on stack X?" by stack kind rather than open-coded address arithmetic.
*unwind_init_common(),unwind_find_stack()andunwind_consume_stack()helpers that enforce the forward-progress-only invariant required for reliability.No existing user is wired up to these helpers in this commit; the unwinder switch comes in a follow-up. The header changes leave
on_thread_stack()with the same semantics as before, just expressed in terms of the new helpers.
Quoted 5 lines
Reviewed-by Shuai Xue <[email protected]>Reviewed-by Martin Kaiser <[email protected]>Tested-by Martin Kaiser <[email protected]>Signed-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
PATCH v7 8/10 riscv: stacktrace: switch to frame-pointer based unwinder
In reply to Shuai XueFrom: Wang Han <[email protected]>
Replace the open-coded frame-pointer walker in arch_stack_walk() with a robust kunwind state machine, modelled on arch/arm64/kernel/stacktrace.c and retargeted to the RISC-V {fp, ra} frame record convention. The new walker tracks stack bounds, consumes frame records monotonically, understands the metadata pt_regs records added in the previous frame record metadata patch, and recovers return addresses replaced by function graph tracing and kretprobes. Function graph recovery matches the caller's frame pointer, the convention set up by the preceding ftrace cookie patch.
This commit introduces arch_stack_walk_reliable() but does not yet select HAVE_RELIABLE_STACKTRACE; that is done in a follow-up Kconfig patch so this commit can be reviewed and bisected as a pure unwinder replacement. Until that Kconfig change lands, livepatch is not yet enabled and arch_stack_walk_reliable() has no in-tree caller.
Two related callers are updated to keep the same frame-record assumptions everywhere:
* Perf callchains: route kernel callchain collection through arch_stack_walk() so perf sees the same frame-pointer unwind behaviour as dump_stack() and the upcoming livepatch path.
* dump_backtrace() / __get_wchan() / show_stack(): these now go through arch_stack_walk(); the explicit "Call Trace:" header is moved into dump_backtrace() to preserve the original output.
For !CONFIG_FRAME_POINTER builds the stack-scanning fallback walker follows the approach of the first patch of this series: the walker is extracted into an __always_inline __walk_stackframe() helper that arch_stack_walk() calls directly, so the first reported entry is the return into arch_stack_walk()'s caller. walk_stackframe() remains as a wrapper for its existing callers.
--- arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/stacktrace.c | 588 +++++++++++++++++++++++------ 2 files changed, 475 insertions(+), 115 deletions(-)
arch/riscv/kernel/perf_callchain.c +1 -1
@@ -44,5 +44,5 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,return ; } -walk_stackframe (NULL , regs, fill_callchain, entry); +arch_stack_walk (fill_callchain, entry,NULL , regs); }
arch/riscv/kernel/stacktrace.c +474 -114
637 lines not on this page · raw message
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 08/10] riscv: stacktrace: switch to frame-pointer based unwinder
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] riscv: stacktrace: broken leaf-function unwind in kunwind_next_frame_record() - [High] riscv…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] riscv: stacktrace: broken leaf-function unwind in kunwind_next_frame_record()
- [High] riscv: stacktrace: broken unwind across ftrace_caller and return_to_handler
- [Medium] riscv: stacktrace: bogus walk_stackframe entry in __walk_stackframe()
- [Low] riscv: stacktrace: inaccurate metadata type offset comment --
commit 263985113a916e8cd08cc4485092812e49e28303
Author: Wang Han <[email protected]>
riscv: stacktrace: switch to frame-pointer based unwinder
This commit replaces the open-coded frame-pointer stack walker with a kunwind state machine, adds arch_stack_walk_reliable(), and routes perf_callchain_kernel(), dump_backtrace(), and __get_wchan() through arch_stack_walk().
Quoted 4 lines
arch/riscv/kernel/stacktrace.c +0
-0
[ ... ]
Quoted 10 lines
+/*
+ * Unwind from one frame record to the next.
+ *
+ * On RISC-V, the frame record sits at fp -sizeof(struct frame_record),
+ * immediately below the address pointed to by fp/s0. This applies to both
+ * normal frame records and metadata frame records (embedded inpt_regs).
+ *
+ * A metadata record is identified by both fp and ra being zero in the
+ *frame_recordportion, with a type value following at fp + 16.
+ */
[Severity: Low]
This isn't a bug, but since fp points immediately above struct frame_record on RISC-V (meta_base = fp - sizeof(struct frame_record)), meta->type is located at fp rather than fp + 16.
Quoted 24 lines
arch/riscv/kernel/stacktrace.c +24
-0+static __always_inline int +kunwind_next_frame_record (struct kunwind_state *state) +{ +unsigned long fp = state->common.fp; +struct frame_record *record; +struct stack_info *info; +unsigned long new_fp, new_pc; +unsigned long record_base; + +if (fp &0x7 ) +return -EINVAL ; + + record_base = fp -sizeof (*record); + + info =unwind_find_stack (&state->common, record_base,sizeof (*record)); +if (!info) +return -EINVAL ; + + record = (struct frame_record *)record_base; + new_fp =READ_ONCE (record->fp); + new_pc =READ_ONCE (record->ra); + +if (!new_fp && !new_pc) +return kunwind_next_frame_record_meta (state);
arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 391
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +10
-0+ +unwind_consume_stack (&state->common, info, record_base, +sizeof (*record)); + + state->common.fp = new_fp; + state->common.pc = new_pc; + state->source =KUNWIND_SOURCE_FRAME ; + +return 0 ; +}
[Severity: High]
Does unwinding from KUNWIND_SOURCE_REGS_PC corrupt the unwind and leak kernel stack addresses when an exception or interrupt hits a leaf function?
On RISC-V with CONFIG_FRAME_POINTER=y, leaf functions do not push ra onto the stack: they save only the caller's s0 at fp - sizeof(unsigned long) (which aliases record->ra), leave the return address in regs->ra, and leave fp - 2 * sizeof(unsigned long) (record->fp) as uninitialized stack padding or local storage.
The previous unwinder checked for this case, whereas kunwind_init_from_regs() and kunwind_next_regs_pc() store regs in state->regs without ever reading it back, and kunwind_next_frame_record() unconditionally reads record->ra as new_pc and record->fp as new_fp. Because __kernel_text_address() filtering is also removed and perf_callchain_kernel() now calls arch_stack_walk(), a PMU sample in a leaf function passes the caller's saved kernel stack pointer in record->ra to fill_callchain() -> perf_callchain_store(), exposing it to userspace, while regs->ra is lost and the uninitialized word in record->fp is used as the next frame pointer.
[ ... ]
Quoted 6 lines
arch/riscv/kernel/stacktrace.c +6
-0+static __always_inline bool +arch_reliable_kunwind_consume_entry (const struct kunwind_state *state,void *cookie) +{ +/* +* At an exception boundary we can reliably consume the saved PC. We do +* not know whether ra was live when the exception was taken, and
arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 510
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +10
-0+* so we cannot perform the next unwind step reliably. +* +* All that matters is whether the *entire* unwind is reliable, so give +* up as soon as we hit an exception boundary. +*/ +if (state->source ==KUNWIND_SOURCE_REGS_PC ) +return false; + +return arch_kunwind_consume_entry (state, cookie); +}
[Severity: High]
Can kunwind_next_frame_record() and arch_stack_walk_reliable() unwind across ftrace_caller and return_to_handler?
Unlike arm64, RISC-V's ftrace trampolines do not maintain a valid struct frame_record chain at s0 - sizeof(struct frame_record):
arch/riscv/kernel/mcount-dyn.S:SAVE_ABI_REGS {
addi sp, sp, -FREGS_SIZE_ON_STACK
REG_S t0, FREGS_EPC(sp)
REG_S x1, FREGS_RA(sp)
REG_S x8, FREGS_S0(sp)
...
}arch/riscv/kernel/mcount.S:SAVE_RET_ABI_STATE {
addi sp, sp, -FREGS_SIZE_ON_STACK
REG_S ra, FREGS_RA(sp)
REG_S s0, FREGS_S0(sp)
REG_S a0, FREGS_A0(sp)
REG_S a1, FREGS_A1(sp)
addi s0, sp, FREGS_SIZE_ON_STACK
}In ftrace_caller, SAVE_ABI_REGS saves ftrace_regs without updating s0 or creating a frame record, so a C tracer callback saves the traced function's caller s0 alongside a return address in ftrace_caller. Walking past the callback jumps straight from ftrace_caller to the caller of the traced function, silently skipping both the traced function and its immediate caller while arch_stack_walk_reliable() still succeeds. (Commit "riscv: ftrace: always preserve s0 in dynamic ftrace register frame" noted that the reliable unwinder would read ftrace_regs_get_frame_pointer(fregs) directly, but kunwind has no ftrace_regs handling.)
In return_to_handler, SAVE_RET_ABI_STATE sets s0 to sp + FREGS_SIZE_ON_STACK after saving only ra, s0, a0, and a1. When unwinding past ftrace_return_to_handler(), kunwind_next_frame_record() reads s0 - sizeof(struct frame_record), which sits in the uninitialized upper words of the __arch_ftrace_regs allocation rather than at FREGS_S0(sp).
[ ... ]
Quoted 9 lines
arch/riscv/kernel/stacktrace.c +4
-3+/* * The generic code expects the first entry for the current task to be the * return address of arch_stack_walk() itself, the way x86 and arm64 report -* it, and stack_trace_save() sizes its skip count accordingly. Walking from -* inside a called walk_stackframe() adds that function's own frame on top, -* so inline the walker here and start from this frame instead. +* it, and stack_trace_save() sizes its skip count accordingly. Walking +* from inside a called walk_stackframe() adds that function's own frame +* on top, so call the always-inlined walker here and start from this
arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 552
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +4
-2+* frame instead. */ -noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, -struct task_struct *task,struct pt_regs *regs) +noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task, +struct pt_regs *regs) {__walk_stackframe (task, regs, consume_entry, cookie); }
[Severity: Medium]
On !CONFIG_FRAME_POINTER builds, doesn't __walk_stackframe() still report walk_stackframe as the first entry when unwinding the current task?
arch/riscv/kernel/stacktrace.c:__walk_stackframe() {
...
} else if (task == NULL || task == current) {
sp = current_stack_pointer;
pc = (unsigned long)walk_stackframe;
} else {
...
ksp = (unsigned long *)sp;
while (!kstack_end(ksp)) {
if (__kernel_text_address(pc) && unlikely(!fn(arg, pc)))
break;
pc = READ_ONCE_NOCHECK(*ksp++);
}
}Unlike the removed CONFIG_FRAME_POINTER version (which started at level = -1 to skip the placeholder pc), the !CONFIG_FRAME_POINTER loop calls fn(arg, pc) before reading *ksp++. As a result, arch_stack_walk() reports walk_stackframe before scanning its own stack frame, shifting stack_trace_save() and return_address() off by one and printing a bogus walk_stackframe entry in dump_backtrace().
Also, since all callers of walk_stackframe() are converted to arch_stack_walk() by this patch, walk_stackframe() has no remaining callers and its extern declaration in arch/riscv/include/asm/stacktrace.h is left without a definition when CONFIG_FRAME_POINTER=y.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Re: [PATCH v7 08/10] riscv: stacktrace: switch to frame-pointer based unwinder
Quoted 39 lines · On 11/10/26 10:20 AM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] riscv: stacktrace: broken leaf-function unwind inkunwind_next_frame_record()
- [High] riscv: stacktrace: broken unwind acrossftrace_callerandreturn_to_handler
- [Medium] riscv: stacktrace: boguswalk_stackframeentry in__walk_stackframe()
- [Low] riscv: stacktrace: inaccurate metadata type offset comment --commit
263985113a916e8cd08cc4485092812e49e28303
Author: Wang Han <[email protected]>riscv: stacktrace: switch to frame-pointer based unwinder
This commit replaces the open-coded frame-pointer stack walker with a kunwind state machine, adds
arch_stack_walk_reliable(), and routesperf_callchain_kernel(),dump_backtrace(), and__get_wchan()througharch_stack_walk().Quoted 4 lines
arch/riscv/kernel/stacktrace.c +0
-0[ ... ]
Quoted 10 lines
+/*
+ * Unwind from one frame record to the next.
+ *
+ * On RISC-V, the frame record sits at fp -sizeof(struct frame_record),
+ * immediately below the address pointed to by fp/s0. This applies to both
+ * normal frame records and metadata frame records (embedded inpt_regs).
+ *
+ * A metadata record is identified by both fp and ra being zero in the
+ *frame_recordportion, with a type value following at fp + 16.
+ */
Quoted 4 lines
[Severity: Low]
This isn't a bug, but since fp points immediately above structframe_recordon RISC-V (meta_base= fp -sizeof(struct frame_record)),meta->typeis located at fp rather than fp + 16.
Will fix it.
Quoted 47 lines
Quoted 25 lines
arch/riscv/kernel/stacktrace.c +24
-0+static __always_inline int +kunwind_next_frame_record (struct kunwind_state *state) +{ +unsigned long fp = state->common.fp; +struct frame_record *record; +struct stack_info *info; +unsigned long new_fp, new_pc; +unsigned long record_base; + +if (fp &0x7 ) +return -EINVAL ; + + record_base = fp -sizeof (*record); + + info =unwind_find_stack (&state->common, record_base,sizeof (*record)); +if (!info) +return -EINVAL ; + + record = (struct frame_record *)record_base; + new_fp =READ_ONCE (record->fp); + new_pc =READ_ONCE (record->ra); + +if (!new_fp && !new_pc) +return kunwind_next_frame_record_meta (state);arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 391
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +10
-0+ +unwind_consume_stack (&state->common, info, record_base, +sizeof (*record)); + + state->common.fp = new_fp; + state->common.pc = new_pc; + state->source =KUNWIND_SOURCE_FRAME ; + +return 0 ; +}[Severity: High]
Does unwinding fromKUNWIND_SOURCE_REGS_PCcorrupt the unwind and leak kernel stack addresses when an exception or interrupt hits a leaf function?On RISC-V with
CONFIG_FRAME_POINTER=y, leaf functions do not push ra onto the stack: they save only the caller's s0 at fp - sizeof(unsigned long) (which aliasesrecord->ra), leave the return address inregs->ra, and leave fp - 2 * sizeof(unsigned long) (record->fp) as uninitialized stack padding or local storage.
Quoted 10 lines
The previous unwinder checked for this case, whereas
kunwind_init_from_regs()andkunwind_next_regs_pc()store regs instate->regswithout ever reading it back, andkunwind_next_frame_record()unconditionally readsrecord->raasnew_pcandrecord->fpasnew_fp. Because__kernel_text_address()filtering is also removed andperf_callchain_kernel()now callsarch_stack_walk(), a PMU sample in a leaf function passes the caller's saved kernel stack pointer inrecord->ratofill_callchain()->perf_callchain_store(), exposing it to userspace, whileregs->rais lost and the uninitialized word inrecord->fpis used as the next frame pointer.
Yes. For v8 we will reinstate
the leaf special case in kunwind_next_frame_record() (when source == KUNWIND_SOURCE_REGS_PC and regs->epc == pc, take the next fp from record->ra and the next pc from regs->ra) and restore the __kernel_text_address() check in arch_kunwind_consume_entry().
Quoted 54 lines
[ ... ]
Quoted 6 lines
arch/riscv/kernel/stacktrace.c +6
-0+static __always_inline bool +arch_reliable_kunwind_consume_entry (const struct kunwind_state *state,void *cookie) +{ +/* +* At an exception boundary we can reliably consume the saved PC. We do +* not know whether ra was live when the exception was taken, and arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 510
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +10
-0+* so we cannot perform the next unwind step reliably. +* +* All that matters is whether the *entire* unwind is reliable, so give +* up as soon as we hit an exception boundary. +*/ +if (state->source ==KUNWIND_SOURCE_REGS_PC ) +return false; + +return arch_kunwind_consume_entry (state, cookie); +}[Severity: High]
Cankunwind_next_frame_record()andarch_stack_walk_reliable()unwind acrossftrace_callerandreturn_to_handler?Unlike arm64, RISC-V's ftrace trampolines do not maintain a valid struct
frame_recordchain at s0 -sizeof(struct frame_record):arch/riscv/kernel/mcount-dyn.S:SAVE_ABI_REGS { addi sp, sp, -FREGS_SIZE_ON_STACK REG_S t0, FREGS_EPC(sp) REG_S x1, FREGS_RA(sp) REG_S x8, FREGS_S0(sp) ... }arch/riscv/kernel/mcount.S:SAVE_RET_ABI_STATE { addi sp, sp, -FREGS_SIZE_ON_STACK REG_S ra, FREGS_RA(sp) REG_S s0, FREGS_S0(sp) REG_S a0, FREGS_A0(sp) REG_S a1, FREGS_A1(sp) addi s0, sp, FREGS_SIZE_ON_STACK }In
ftrace_caller,SAVE_ABI_REGSsavesftrace_regswithout updating s0 or creating a frame record, so a C tracer callback saves the traced function's caller s0 alongside a return address inftrace_caller. Walking past the callback jumps straight fromftrace_callerto the caller of the traced function, silently skipping both the traced function and its immediate caller whilearch_stack_walk_reliable()still succeeds. (Commit "riscv: ftrace: always preserve s0 in dynamic ftrace register frame" noted that the reliable unwinder would readftrace_regs_get_frame_pointer(fregs)directly, but kunwind has noftrace_regshandling.)
Quoted 5 lines
In
return_to_handler,SAVE_RET_ABI_STATEsets s0 to sp +FREGS_SIZE_ON_STACKafter saving only ra, s0, a0, and a1. When unwinding pastftrace_return_to_handler(),kunwind_next_frame_record()reads s0 -sizeof(struct frame_record), which sits in the uninitialized upper words of the__arch_ftrace_regsallocation rather than atFREGS_S0(sp).
Yes, a walk crossing ftrace_caller skips the traced function and its immediate caller while arch_stack_walk_reliable() still returns success. >
Quoted 48 lines
[ ... ]
Quoted 9 lines
arch/riscv/kernel/stacktrace.c +4
-3+/* * The generic code expects the first entry for the current task to be the * return address of arch_stack_walk() itself, the way x86 and arm64 report -* it, and stack_trace_save() sizes its skip count accordingly. Walking from -* inside a called walk_stackframe() adds that function's own frame on top, -* so inline the walker here and start from this frame instead. +* it, and stack_trace_save() sizes its skip count accordingly. Walking +* from inside a called walk_stackframe() adds that function's own frame +* on top, so call the always-inlined walker here and start from this arch/riscv/kernel/stacktrace.c unsigned long __get_wchan(struct task_struct *task) · line 552
Quoted 10 lines
arch/riscv/kernel/stacktrace.c +4
-2+* frame instead. */ -noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, -struct task_struct *task,struct pt_regs *regs) +noinline noinstr void arch_stack_walk(stack_trace_consume_fn consume_entry, +void *cookie,struct task_struct *task, +struct pt_regs *regs) {__walk_stackframe (task, regs, consume_entry, cookie); }[Severity: Medium]
On !CONFIG_FRAME_POINTER builds, doesn't__walk_stackframe()still reportwalk_stackframeas the first entry when unwinding the current task?arch/riscv/kernel/stacktrace.c:__walk_stackframe() { ... } else if (task == NULL || task == current) { sp = current_stack_pointer; pc = (unsigned long)walk_stackframe; } else { ... ksp = (unsigned long *)sp; while (!kstack_end(ksp)) { if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) break; pc = READ_ONCE_NOCHECK(*ksp++); } }Unlike the removed
CONFIG_FRAME_POINTERversion (which started at level = -1 to skip the placeholder pc), the !CONFIG_FRAME_POINTER loop callsfn(arg, pc)before reading *ksp++. As a result,arch_stack_walk()reportswalk_stackframebefore scanning its own stack frame, shiftingstack_trace_save()andreturn_address()off by one and printing a boguswalk_stackframeentry indump_backtrace().
Quoted 5 lines
Also, since all callers of
walk_stackframe()are converted toarch_stack_walk()by this patch,walk_stackframe()has no remaining callers and its extern declaration inarch/riscv/include/asm/stacktrace.his left without a definition whenCONFIG_FRAME_POINTER=y.
Same issue like patch 01/10, will fix it.
Thanks.
Shuai
PATCH v7 9/10 riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
In reply to Shuai XueFrom: Wang Han <[email protected]>
Now that the metadata frame records, the kunwind state machine and arch_stack_walk_reliable() are all in place, advertise the capability to the rest of the kernel:
* select HAVE_RELIABLE_STACKTRACE under FRAME_POINTER && 64BIT, so only the configurations with the tested metadata records and FP-based reliable walker enable it.
* select HAVE_LIVEPATCH under the same condition and source kernel/livepatch/Kconfig so the livepatch menu is reachable from the RISC-V configuration.
The 64BIT dependency is conservative scoping rather than a hard technical requirement: the metadata frame record, kunwind state machine and arch_stack_walk_reliable() also build on RV32, and the IRQ-stack frame-record adjustment fixes a latent RV32 issue. However, the syscall livepatch selftest and module relocation path have only been exercised on RV64 QEMU virt so far. The 64BIT gate can be relaxed in a follow-up once RV32 has equivalent coverage.
This is split out from the unwinder change so the policy decision and the implementation can be reviewed and reverted independently.
--- arch/riscv/Kconfig | 4 ++++ 1 file changed, 4 insertions(+)
arch/riscv/Kconfig +4 -0
@@ -186,6 +186,7 @@ config RISCV select HAVE_KRETPROBES # https://github.com/ClangBuiltLinux/linux/issues/1881 select HAVE_LD_DEAD_CODE_DATA_ELIMINATION if !LD_IS_LLD + select HAVE_LIVEPATCH if FRAME_POINTER && 64BIT select HAVE_MOVE_PMD select HAVE_MOVE_PUD select HAVE_PAGE_SIZE_4KB
@@ -195,6 +196,7 @@ config RISCV select HAVE_PERF_USER_STACK_DUMP select HAVE_POSIX_CPU_TIMERS_TASK_WORK select HAVE_REGS_AND_STACK_ACCESS_API + select HAVE_RELIABLE_STACKTRACE if FRAME_POINTER && 64BIT select HAVE_RETHOOK select HAVE_RSEQ select HAVE_RUST if RUSTC_SUPPORTS_RISCV && CC_IS_CLANG
@@ -1415,3 +1417,5 @@ endmenu # "CPU Power Management" source "arch/riscv/kvm/Kconfig" source "drivers/acpi/Kconfig" + +source "kernel/livepatch/Kconfig"
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 09/10] riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] riscv: module: fault and UAF in apply_relocate_add() for klp relocs - [Medium] riscv: stacktrace…
Thank you for your contribution! Sashiko AI review found 4 potential issues to consider:
- [High] riscv: module: fault and UAF in apply_relocate_add() for klp relocs
- [Medium] riscv: stacktrace: ftrace_caller() omits frames in reliable unwind
- [Medium] riscv: stacktrace: uninitialized frame read in return_to_handler()
- [Medium] riscv: stacktrace: arch_rethook_trampoline() omits caller frame
--
commit 57947c1aaf895b44b13bb74dddecd8c57f82bb66
Author: Wang Han <[email protected]>
riscv: Kconfig: enable HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH
This commit selects HAVE_RELIABLE_STACKTRACE and HAVE_LIVEPATCH on 64-bit RISC-V configurations with FRAME_POINTER enabled, and sources kernel/livepatch/Kconfig.
arch/riscv/Kconfig config RISCV · line 189
Quoted 9 lines
arch/riscv/Kconfig +1
-0@@ -186,6 +186,7 @@ config RISCV select HAVE_KRETPROBES # https://github.com/ClangBuiltLinux/linux/issues/1881 select HAVE_LD_DEAD_CODE_DATA_ELIMINATION if !LD_IS_LLD + select HAVE_LIVEPATCH if FRAME_POINTER && 64BIT
[Severity: High]
Can klp_apply_object_relocs() -> apply_relocate_add() trigger a store page fault and use-after-free when applying late .klp.rela.<module>.* relocations?
During load_module(), complete_formation() marks module text and rodata (including .plt, .got, and .got.plt) read-only under CONFIG_STRICT_MODULE_RWX before klp_enable_patch() or klp_module_coming() runs. When apply_relocate_add() is later called on a module with mod->state != MODULE_STATE_UNFORMED, the relocation handlers in arch/riscv/kernel/module.c (such as riscv_insn_rmw()) and module_emit_got_entry() / module_emit_plt_entry() in arch/riscv/kernel/module-sections.c write directly to read-only module memory instead of using text patching helpers and flushing the instruction cache:
arch/riscv/kernel/module.c:riscv_insn_rmw() {
...
parcel[0] = cpu_to_le16(insn);
parcel[1] = cpu_to_le16(insn >> 16);
return 0;
}In addition, module_frob_arch_sections() caches pointers into the temporary info->sechdrs buffer in mod->arch.plt.shdr, mod->arch.got.shdr, and mod->arch.got_plt.shdr, which free_copy() frees via vfree() at the end of load_module():
arch/riscv/kernel/module-sections.c:module_frob_arch_sections() {
...
for (i = 0; i < ehdr->e_shnum; i++) {
if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
mod->arch.plt.shdr = sechdrs + i;
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got"))
mod->arch.got.shdr = sechdrs + i;
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".got.plt"))
mod->arch.got_plt.shdr = sechdrs + i;
}
...
}When late livepatch relocations emit GOT or PLT entries, module_emit_got_entry() and module_emit_plt_entry() dereference these freed shdr pointers (e.g. got_sec->shdr->sh_addr), and re-applying relocations when a target module is reloaded increments num_entries past max_entries and hits BUG_ON().
arch/riscv/Kconfig config RISCV · line 199
Quoted 8 lines
arch/riscv/Kconfig +1
-0select HAVE_MOVE_PMD select HAVE_MOVE_PUD select HAVE_PAGE_SIZE_4KB@@ -195,6 +196,7 @@ config RISCV select HAVE_PERF_USER_STACK_DUMP select HAVE_POSIX_CPU_TIMERS_TASK_WORK select HAVE_REGS_AND_STACK_ACCESS_API + select HAVE_RELIABLE_STACKTRACE if FRAME_POINTER && 64BIT
[Severity: Medium]
Does arch_stack_walk_reliable() silently omit the traced function and its caller when unwinding a task inside an ftrace callback or direct trampoline?
With CONFIG_DYNAMIC_FTRACE_WITH_ARGS, the patchable entry of a traced function jumps to ftrace_caller before the function sets up its stack frame. At that point, t0 holds the traced function's PC, ra holds the return address in its caller, and s0 still holds the caller's frame pointer. In arch/riscv/kernel/mcount-dyn.S, SAVE_ABI_REGS saves t0, x1, and x8 into struct __arch_ftrace_regs without constructing frame records on the stack or updating s0:
arch/riscv/kernel/mcount-dyn.S:SAVE_ABI_REGS {
addi sp, sp, -FREGS_SIZE_ON_STACK
REG_S t0, FREGS_EPC(sp)
REG_S x1, FREGS_RA(sp)
REG_S x8, FREGS_S0(sp)
...
}When ftrace_caller calls op->func, op->func saves the unchanged s0 in its frame record. Unwinding that frame record in kunwind_next_frame_record() advances directly to the caller's caller with state->source == KUNWIND_SOURCE_FRAME, which arch_reliable_kunwind_consume_entry() accepts as reliable. During a livepatch transition, klp_check_stack() can miss that a preempted or blocked task is executing inside a patched function or its caller.
[Severity: Medium]
Can kunwind_next_frame_record() read uninitialized stack memory when unwinding through return_to_handler()?
In arch/riscv/kernel/mcount.S, SAVE_RET_ABI_STATE allocates FREGS_SIZE_ON_STACK bytes, saves ra and s0 at low offsets from sp, and sets s0 to sp + FREGS_SIZE_ON_STACK before return_to_handler calls ftrace_return_to_handler():
arch/riscv/kernel/mcount.S:SAVE_RET_ABI_STATE {
addi sp, sp, -FREGS_SIZE_ON_STACK
REG_S ra, FREGS_RA(sp)
REG_S s0, FREGS_S0(sp)
REG_S a0, FREGS_A0(sp)
REG_S a1, FREGS_A1(sp)
addi s0, sp, FREGS_SIZE_ON_STACK
}The 16 bytes at s0 - 16 (sp + FREGS_SIZE_ON_STACK - 16) are never initialized. When unwinding a task inside ftrace_return_to_handler(), kunwind_next_frame_record() restores fp = sp + FREGS_SIZE_ON_STACK and on the next step reads fp - sizeof(*record) as a struct frame_record, either triggering WARN_ON_ONCE(tsk == current) in kunwind_next_frame_record_meta() if those bytes happen to be zero or following a bogus frame pointer and skipping live frames while returning 0 (reliable).
[Severity: Medium]
Does unwinding from inside arch_rethook_trampoline_callback() omit the caller of the probed function while still reporting a reliable stack trace?
When a kretprobed function returns to arch_rethook_trampoline(), its epilogue has already restored s0 to its caller's frame pointer, while the return address in the caller is held in current->rethooks. In arch/riscv/kernel/probes/rethook_trampoline.S, arch_rethook_trampoline allocates PT_SIZE_ON_STACK without initializing S_STACKFRAME (FRAME_META_TYPE_PT_REGS) or updating s0 before calling arch_rethook_trampoline_callback():
arch/riscv/kernel/probes/rethook_trampoline.S:arch_rethook_trampoline() {
addi sp, sp, -(PT_SIZE_ON_STACK)
save_all_base_regs
move a0, sp /* pt_regs */
call arch_rethook_trampoline_callback
...
}When kunwind unwinds arch_rethook_trampoline_callback()'s frame record, state->common.pc is inside arch_rethook_trampoline (past the entry address checked by is_kretprobe_trampoline()) and state->common.fp is the caller's frame pointer. The next unwind step reads the frame record of the caller's caller, skipping the caller of the probed function and returning 0 (reliable).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
PATCH v7 10/10 selftests/livepatch: Add RISC-V syscall wrapper prefix
In reply to Shuai XueFrom: Wang Han <[email protected]>
The syscall livepatch selftest resolves and patches a syscall wrapper symbol. To use that test for RISC-V livepatch validation, add the RISC-V FN_PREFIX definition for ARCH_HAS_SYSCALL_WRAPPER.
Without this macro, the syscall livepatch selftest cannot resolve the RISC-V target symbol, and the syscall-related livepatch test fails on RISC-V.
--- .../testing/selftests/livepatch/test_modules/test_klp_syscall.c | 2 ++ 1 file changed, 2 insertions(+)
tools/testing/selftests/livepatch/test_modules/test_klp_syscall.c +2 -0
@@ -24,6 +24,8 @@#define FN_PREFIX __s390x_#elif defined(__aarch64__)#define FN_PREFIX __arm64_ +#elif defined(__riscv) +#define FN_PREFIX __riscv_#elif defined(__powerpc__)#define FN_PREFIX #else
Signature · 1 line
2.39.3
[email protected]
AI reviewRe: [PATCH v7 10/10] selftests/livepatch: Add RISC-V syscall wrapper prefix
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
From: Wang Han <[email protected]>
The syscall livepatch selftest resolves and patches a syscall wrapper symbol. To use that test for RISC-V livepatch validation, add the RISC-V
FN_PREFIXdefinition forARCH_HAS_SYSCALL_WRAPPER.Without this macro, the syscall livepatch selftest cannot resolve the RISC-V target symbol, and the syscall-related livepatch test fails on RISC-V.
Quoted 7 lines
Reviewed-by Marcos Paulo de Souza <[email protected]>Reviewed-by Shuai Xue <[email protected]>Acked-by Miroslav Benes <[email protected]>Reviewed-by Martin Kaiser <[email protected]>Tested-by Martin Kaiser <[email protected]>Signed-off-by Wang Han <[email protected]>Signed-off-by Shuai Xue <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Quoted 4 lines · Shuai Xue
Changes since v6:
* Rebase onto the current riscv/for-next (based on 7.4-rc1).
Quoted 12 lines
* Fold in Karl Mehltretter's two patches [1] as 1/10 and 2/10. Patch 1/10 stops
arch_stack_walk()from reporting the walker's own frame as the first entry, which corrupts the skip-frame accounting ofstack_trace_save()users (page_owner, perf), and adjustsreturn_address()level accounting accordingly. Patch 2/10 adds a KUnit test for the start frames. Both patches keep their original authorship; I added my Reviewed-by/Tested-by and sign-off. I asked Karl on 3 November whether he would like to see his patches folded into this series; he has not replied so far. Since 8/10 reuses the walker extraction from 1/10 for its !CONFIG_FRAME_POINTER fallback, his patches are included here to keep the series self-contained.
Thank you for folding those patches in. I'm fine with that.
Regards,
Karl