riscv: Always inline on_thread_stack()
PATCH 1/1 riscv: Always inline on_thread_stack()
With CONFIG_IRQ_STACKS=y, GCC may outline on_thread_stack(). CONFIG_KCOV_INSTRUMENT_ALL instruments it, and do_irq() calls it before irq_enter_rcu() increments the hardirq count.
Normal KCOV records this interrupt coverage in the task's buffer, making results timing-dependent.
With CONFIG_KCOV_SELFTEST=y, current->kcov_area remains NULL. In QEMU, a timer interrupt called the helper before hardirq accounting. KCOV treated it as task coverage and dereferenced NULL. Fault handling recursed to stack overflow and stopped boot.
On a BeagleV Ahead, the unpatched kernel did not finish the selftest within 120 seconds. Three patched boots completed it and reached init.
Mark the helper __always_inline to keep it in noinstr do_irq(), following commit 1894a4030582 ("x86: Always inline on_thread_stack() and current_top_of_stack()"), which fixed the same out-of-line call from x86 noinstr code.
---
Tested with GCC 15.2.0 and QEMU 10.2.1 on v7.3-rc7. Both images used byte-identical configs and v7.3-rc7 rebases of these seven pending patches (canonical commit IDs shown):
3a25097bf845 kcov: use unsigned int for kcov_start() mode parameter e39a9c820c37 kcov: add kcov_pause()/kcov_resume() helpers ae2662563c3e hrtimer: pause KCOV during deferred rearm 554af946139e sched: pause KCOV in __schedule() 7f608b777204 sched: pause KCOV in try_to_wake_up() 2ba73ec2a246 sched: pause KCOV in wake_up_new_task() 5ee777070fac context_tracking: exclude from KCOV instrumentation
plain inline __always_inline
KCOV selftest started yes yes KCOV selftest finished no yes Reached /init no yes
The plain-inline image stopped after printing "kcov: running self test" and reached the 45-second limit. The patched image reached /init in two 60-second runs. Those runs timed out after the marker because the test /init deliberately loops.
The first common patch is a type prerequisite. The remaining pause and exclusion changes suppress separate KCOV callbacks that otherwise obscure this test. CONFIG_RISCV_ISA_V_PREEMPTIVE was disabled to avoid a separate vector-entry callback. The only A/B source difference was this patch.
The same images were tested on a BeagleV Ahead (TH1520, four C910 CPUs). The plain-inline image did not finish the selftest or reach /init during 120 seconds after the start message. It reported RCU stalls, and CPU 1 did not answer NMI requests. No panic or stack-overflow message was printed. All three patched runs completed the selftest in about 0.301 seconds and reached /init.
arch/riscv/include/asm/stacktrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/stacktrace.h +1 -1
@@ -16,7 +16,7 @@ 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 ) +static __always_inline bool on_thread_stack(void) {return !(((unsigned long )(current->stack) ^ current_stack_pointer) & ~(THREAD_SIZE -1 )); }
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.53.0
Quoted 20 lines · Karl Mehltretter
With
CONFIG_IRQ_STACKS=y, GCC may outlineon_thread_stack().CONFIG_KCOV_INSTRUMENT_ALLinstruments it, anddo_irq()calls it beforeirq_enter_rcu()increments the hardirq count.Normal KCOV records this interrupt coverage in the task's buffer, making results timing-dependent.
With
CONFIG_KCOV_SELFTEST=y,current->kcov_arearemains NULL. In QEMU, a timer interrupt called the helper before hardirq accounting. KCOV treated it as task coverage and dereferenced NULL. Fault handling recursed to stack overflow and stopped boot.On a BeagleV Ahead, the unpatched kernel did not finish the selftest within 120 seconds. Three patched boots completed it and reached init.
Mark the helper
__always_inlineto keep it in noinstrdo_irq(), following commit1894a4030582("x86: Always inlineon_thread_stack()andcurrent_top_of_stack()"), which fixed the same out-of-line call from x86 noinstr code.
Quoted 4 lines
Fixes 163e76cc6ef4 ("riscv: stack: Support HAVE_IRQ_EXIT_ON_IRQ_STACK")Assisted-by LLMSigned-off-by Karl Mehltretter <[email protected]>
Good catch!
Quoted 61 lines
---
Tested with GCC 15.2.0 and QEMU 10.2.1 on v7.3-rc7. Both images used byte-identical configs and v7.3-rc7 rebases of these seven pending patches (canonical commit IDs shown):
3a25097bf845kcov: use unsigned int forkcov_start()mode parametere39a9c820c37kcov: add kcov_pause()/kcov_resume() helpersae2662563c3ehrtimer: pause KCOV during deferred rearm554af946139esched: pause KCOV in__schedule()7f608b777204sched: pause KCOV intry_to_wake_up()2ba73ec2a246sched: pause KCOV inwake_up_new_task()5ee777070faccontext_tracking: exclude from KCOV instrumentationplain inline
__always_inlineKCOV selftest started yes yes KCOV selftest finished no yes Reached /init no yesThe plain-inline image stopped after printing "kcov: running self test" and reached the 45-second limit. The patched image reached /init in two 60-second runs. Those runs timed out after the marker because the test /init deliberately loops.
The first common patch is a type prerequisite. The remaining pause and exclusion changes suppress separate KCOV callbacks that otherwise obscure this test.
CONFIG_RISCV_ISA_V_PREEMPTIVEwas disabled to avoid a separate vector-entry callback. The only A/B source difference was this patch.The same images were tested on a BeagleV Ahead (TH1520, four C910 CPUs). The plain-inline image did not finish the selftest or reach /init during 120 seconds after the start message. It reported RCU stalls, and CPU 1 did not answer NMI requests. No panic or stack-overflow message was printed. All three patched runs completed the selftest in about 0.301 seconds and reached /init.
arch/riscv/include/asm/stacktrace.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/riscv/include/asm/stacktrace.h +1
-1@@ -16,7 +16,7 @@ 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 ) +static __always_inline bool on_thread_stack(void) {return !(((unsigned long )(current->stack) ^ current_stack_pointer) & ~(THREAD_SIZE -1 )); }base-commit:
24a19b953e2a3d0ea5a8c4951a0ab8f32720c907Signature · 7 lines
2.53.0 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv