Technical Mechanism and Root Cause
1. Functions, Structs, and Files Involved
2. Triggering Sequence and Invariant Violation
-
Unannotated Stack Scan:
During task termination in do_exit() (kernel/exit.c:1015), check_stack_usage() calls stack_not_used(current) while preemption and interrupts are still enabled (preempt_disable() is only called on line 1016).
On architectures where the stack grows downward (!CONFIG_STACK_GROWSUP), stack_not_used() iterates over stack addresses:
unsigned long *n = end_of_stack(p);
do { /* Skip over canary */
n++;
} while (!*n);
Because *n is a plain, unannotated C pointer dereference rather than marked with READ_ONCE_NOCHECK(*n), data_race(*n), or defined with __no_kcsan, KCSAN treats accesses to *n as regular shared-memory reads and instruments them.
-
KCSAN Watchpoint Setup and Delay:
While scanning an unused, zeroed stack word at 0xffffc9000448fc20, KCSAN sets up a watchpoint covering [0xffffc9000448fc20, 0xffffc9000448fc28) via kcsan_setup_watchpoint() and pauses the CPU in delay_access() -> udelay() -> delay_tsc() -> native_pause().
-
Asynchronous Interrupt on the Same Task and CPU:
While paused in native_pause(), an asynchronous APIC timer interrupt (sysvec_apic_timer_interrupt) is delivered to CPU 1.
Under DEFINE_IDTENTRY_SYSVEC (arch/x86/include/asm/idtentry.h:256-265), irqentry_enter() creates a trap frame (struct pt_regs) on the interrupted task's stack. Although the main interrupt handler runs on the per-CPU interrupt stack via run_sysvec_on_irqstack_cond(), control switches back to the task stack before calling irqentry_exit(regs, state).
-
Task Stack Growth into the Watched Memory:
On the task stack, irqentry_exit() -> irqentry_exit_to_kernel_mode() -> irqentry_exit_to_kernel_mode_after_preempt() -> irqentry_hrtimer_rearm_deferred() calls __hrtimer_rearm_deferred() (kernel/time/hrtimer.c:2193), which acquires a raw spinlock via guard(raw_spinlock)(&cpu_base->lock).
In queued_spin_lock() (include/asm-generic/qspinlock.h:108-116):
static __always_inline void queued_spin_lock(struct qspinlock *lock)
{
int val = 0;
if (likely(atomic_try_cmpxchg_acquire(&lock->val, &val, _Q_LOCKED_VAL)))
return;
...
The local variable int val = 0 is allocated on the stack and has its address passed to atomic_try_cmpxchg_acquire(). Because its address escapes to an atomic helper, the compiler emits an instrumented store for val = 0 at address 0xffffc9000448fc24 (4 bytes).
This address falls directly inside the watchpoint window [0xffffc9000448fc20, 0xffffc9000448fc28) previously registered by stack_not_used(). When the interrupt unwinds, return addresses (e.g. 0xffffffff815205b9) are left in that stack region, changing the memory from 0x0000000000000000 to non-zero and tripping KCSAN's watchpoint detection.
3. Root Cause Summary
The root cause is that stack_not_used() reads its argument's kernel stack memoryβwhich can legally be modified asynchronously by interrupt handlers executing on the same task stack or concurrently by another task when queried from sched_show_task()βusing a plain, unannotated memory dereference (while (!*n)). Because compiler instrumentation is active, KCSAN detects the concurrent stack write from the nested interrupt handler as a data race. Marking the read as a known diagnostic race (e.g., using READ_ONCE_NOCHECK(*n) or data_race(*n)) or annotating stack_not_used() with __no_kcsan prevents KCSAN from setting watchpoints on volatile stack regions.