Root Cause
The data race is caused by missing fpregs_lock() synchronization (and premature FPU state modification before TIF_NEED_FPU_LOAD is set) in fpu_flush_thread() (arch/x86/kernel/fpu/core.c:880-884).
During execve() (begin_new_exec() -> flush_thread() -> fpu_flush_thread()):
void fpu_flush_thread(void)
{
fpstate_reset(x86_task_fpu(current));
fpu_reset_fpstate_regs();
}
-
fpstate_reset() runs without fpregs_lock() and while TIF_NEED_FPU_LOAD may still be clear.
- As documented on
fpregs_lock() (arch/x86/include/asm/fpu/api.h:51-72), modifying current's fpu->fpstate requires fpregs_lock() (which disables preemption and softirqs on !PREEMPT_RT kernels) because a context switch or softirq can otherwise observe !TIF_NEED_FPU_LOAD and save CPU FPU registers to fpu->fpstate.
fpu_flush_thread() calls fpstate_reset() (arch/x86/kernel/fpu/core.c:616-634) before fpu_reset_fpstate_regs() (arch/x86/kernel/fpu/core.c:820-842), which is the function that acquires fpregs_lock(), invalidates FPU state (__fpu_invalidate_fpregs_state()), resets fpu->fpstate->regs, and sets TIF_NEED_FPU_LOAD.
- Inside
fpstate_reset(), fpu->fpstate is set to &fpu->__fpstate and __fpstate_reset() (arch/x86/kernel/fpu/core.c:590-614) writes fpstate->size, fpstate->xfeatures, fpstate->xfd (arch/x86/kernel/fpu/core.c:609, offset 0x18 within struct fpstate, matching 0xffff888103fc3058), fpstate->user_size, and fpstate->user_xfeatures.
-
Concurrent access via context switch (switch_fpu()):
- If
current is preempted during fpstate_reset() (or between fpstate_reset() and fpu_reset_fpstate_regs()), __switch_to() (arch/x86/kernel/process_64.c:619) calls switch_fpu(prev_p, cpu) (arch/x86/include/asm/fpu/sched.h:34-53).
- Because
TIF_NEED_FPU_LOAD has not been set yet, switch_fpu() calls update_fpu_state_and_flag() (arch/x86/kernel/fpu/core.c:223-227) -> save_fpregs_to_fpstate() (arch/x86/kernel/fpu/core.c:141-160) -> os_xsave(fpu->fpstate) (arch/x86/kernel/fpu/xstate.h:216-230).
- With
CONFIG_X86_DEBUG_FPU=y, os_xsave() calls xfd_validate_state(fpstate, mask, false) (arch/x86/kernel/fpu/xstate.c:1581-1584) -> xstate_op_valid() (arch/x86/kernel/fpu/xstate.c:1535-1579), which reads fpstate->xfd at line 1539 (if (fpstate->xfd == xfd)) concurrently with __fpstate_reset() writing fpstate->xfd at arch/x86/kernel/fpu/core.c:609 (and os_xsave() likewise reads fpstate->xfeatures concurrently with line 608).
- If the outgoing task had dynamic XFD features active (e.g., AMX, with per-CPU
xfd_state differing from init_fpstate.xfd), resetting fpstate->xfd before invalidating FPU state and setting TIF_NEED_FPU_LOAD can also trip WARN_ON_ONCE(!xstate_op_valid(...)).
Why the KCSAN Report Shows the Same Task/CPU, Empty Read Stack, and skb_copy_bits
- When KCSAN sets up the watchpoint in
__fpstate_reset(), kcsan_setup_watchpoint() -> delay_tsc() (arch/x86/lib/delay.c:75) calls preempt_enable(), allowing task 15366 (dhcpcd-run-hook) to be preempted on CPU 0.
- In
__switch_to_asm (arch/x86/entry/entry_64.S:192), %rsp is switched to the incoming (next) task's kernel stack before jumping to __switch_to().
- Inside
__switch_to() (arch/x86/kernel/process_64.c:619), switch_fpu(prev_p, cpu) executes before raw_cpu_write(current_task, next_p) at line 670. Thus, when os_xsave() hits the watchpoint, current is still task 15366 (in_task() is true, cpu_id == 0), but %rsp already points to next's stack.
- Consequently,
stack_trace_save() in prepare_report_producer() (kernel/kcsan/report.c:598) calls arch_stack_walk(..., current, NULL) -> get_stack_info(state->sp, current, ...), which fails because state->sp is not on current's (prev_p's) stack, returning num_stack_entries = 0.
- In
print_report() (kernel/kcsan/report.c:413-416, 455-457), other_skipnr is 0, print_stack_trace() prints zero frames for the read side, and other_frame = other_info->stack_entries[other_skipnr] reads a stale stack_entries[0] (skb_copy_bits) left behind in other_infos[watchpoint_idx] from an earlier unrelated KCSAN report.
Fix Direction
Acquire fpregs_lock() across both fpstate_reset() and fpu_reset_fpstate_regs() in fpu_flush_thread() (or otherwise invalidate/set TIF_NEED_FPU_LOAD under fpregs_lock() before calling fpstate_reset() so switch_fpu() / kernel_fpu_begin() cannot observe !TIF_NEED_FPU_LOAD while fpu->fpstate is being reset).