Technical Breakdown and Root Cause Analysis
1. Functions, Structs, and Fields Involved
2. Triggering Mechanism and Root Cause
In commit 3d252160b818 ("fs/pipe: Read pipe->{head,tail} atomically outside pipe->mutex"), union pipe_index was introduced with the unsigned long head_tail field so that lockless callers (pipe_poll(), pipe_readable(), pipe_writable()) can inspect both head and tail in a single atomic 64-bit read without acquiring pipe->mutex.
In pipe_poll():
idx.head_tail = READ_ONCE(pipe->head_tail);
Task 3279 executes an intentional, marked 8-byte load of pipe->head_tail without holding pipe->mutex.
Concurrently, Task 3293 in anon_pipe_write():
pipe->head = head + 1;
executes a plain, unmarked 4-byte store to pipe->head (the lower 4 bytes of pipe->head_tail).
Under the Linux Kernel Memory Model (LKMM) and KCSAN rules, when an unmarked write access races with a marked read on overlapping memory bytes (0xffff8881003b1848), KCSAN flags it as a data race because the compiler is not strictly prevented from inventing stores or tearing plain writes. Neither pipe->head nor pipe->tail are updated with WRITE_ONCE() (or annotated as __data_racy in union pipe_index).
3. Invariant and Lifecycle Verification
- Buffer Initialization Timing: In
anon_pipe_write() (fs/pipe.c:563-575), pipe->head = head + 1 is updated prior to populating buf->page, buf->ops, and buf->len. However, anon_pipe_write() holds pipe->mutex throughout this entire block (and normal write disallows watch-queue pipes via pipe_has_watch_queue(pipe)). Any reader attempting to read or dereference pipe->bufs (such as anon_pipe_read()) must acquire pipe->mutex first, serializing strictly after the writer finishes initializing the buffer. Lockless callers (pipe_poll(), pipe_readable(), pipe_writable()) only check pipe_empty() / pipe_full() against indices and do not inspect or dereference pipe->bufs.
- Resolution: Marking the write using
WRITE_ONCE(pipe->head, head + 1) (along with other index updates like pipe_update_tail()), or qualifying the union pipe_index members with __data_racy, eliminates the data race.