Technical Mechanism and Root Cause
1. Functions, Structs, and Fields Involved
2. Triggering Mechanism and Race Condition
The bug is detected by Kernel Concurrency Sanitizer (KCSAN) on the same CPU (CPU 1) between task execution context and an asynchronous interrupt handler:
- Task Context Write: A task on CPU 1 enters
__blk_flush_plug(). At line 1285, it executes current->flags &= ~PF_BLOCK_TS;. This is an unmarked C read-modify-write operation on current->flags.
- KCSAN Watchpoint Setup: Prior to / around the access, KCSAN sets up a watchpoint on the 4-byte address of
current->flags and delays execution (delay_access() / udelay()).
- Interrupt Context Read: While the task context is suspended in the delay window, a local APIC timer interrupt (
sysvec_apic_timer_interrupt) triggers on CPU 1. The interrupt handler executes hrtimer_interrupt() -> perf_swevent_hrtimer() -> perf_prepare_sample() -> perf_sample_regs_user(), which invokes is_user_task(current).
- Data Race:
is_user_task() issues an unmarked 32-bit load of current->flags to check the bits PF_KTHREAD (0x00200000) and PF_USER_WORKER (0x00004000). Because one access is a write/read-modify-write and the other is a read to the same 4-byte memory location without synchronization primitives (such as READ_ONCE() / WRITE_ONCE() or atomic bit operations), KCSAN reports a data-race violation under the Linux Kernel Memory Model (LKMM).
3. Invariants and Practical Consequence
- The block plug code introduced
PF_BLOCK_TS (0x20000000) into task->flags as a fast flag to determine whether cached plug timestamps need invalidation upon task switch (finish_task_switch()).
- Under normal kernel conventions,
task->flags is updated non-atomically because only current writes it. However, asynchronous interrupt handlers on the same CPU can run at any instruction boundary and inspect current->flags.
- In practice, this race is benign: the interrupt reader only checks
PF_KTHREAD and PF_USER_WORKER, which are initialized during process/thread creation and never change over the thread's lifetime, while __blk_flush_plug() only modifies bit 29 (PF_BLOCK_TS). Furthermore, the interrupt handler does not write current->flags, preventing lost updates, and aligned 32-bit memory accesses are single-copy atomic on modern architectures (such as x86-64).