Root Cause Analysis
The reported KCSAN bug is a data race between prb_first_seq() (via inline desc_read()) and prb_reserve() in kernel/printk/printk_ringbuffer.c.
1. Background: Lockless Ringbuffer State Machine
In kernel/printk/printk_ringbuffer.c, log records consist of descriptors (struct prb_desc) in desc_ring.descs and associated metadata records (struct printk_info) in desc_ring.infos. Synchronization between writers and lockless readers is governed by an atomic state variable (desc->state_var), which packs both the descriptor ID and the descriptor state (desc_reserved, desc_committed, desc_finalized, or desc_reusable).
To read a descriptor, desc_read() uses an optimistic read-then-validate protocol:
state_val = atomic_long_read(state_var); (desc_read:A)
- Validates that the state is not
desc_miss or desc_reserved.
- Executes a read memory barrier:
smp_rmb(); (desc_read:B).
- Copies the descriptor and info data:
if (seq_out)
*seq_out = info->seq; /* desc_read:C, kernel/printk/printk_ringbuffer.c:499 */
- Executes another read memory barrier:
smp_rmb(); (desc_read:D, line 541).
- Re-reads the descriptor state:
state_val = atomic_long_read(state_var); /* desc_read:E, line 547 */
d_state = get_desc_state(id, state_val);
If the descriptor was reused or modified concurrently, d_state will return desc_miss (because DESC_ID(state_val) != id) or desc_reserved.
2. Race Condition Trigger
The race occurs when a descriptor slot wraps around and is recycled while another CPU is reading the tail descriptor:
-
Reader Context (CPU 1, Task 31):
Task 31 in prb_first_seq() reads tail_id (e.g., id = 0x170ee) and invokes desc_read(desc_ring, 0x170ee, NULL, &seq, NULL) (kernel/printk/printk_ringbuffer.c:2012). It checks desc->state_var (0x170ee), observes a consistent finalized or reusable state, executes smp_rmb(), and prepares to read info->seq (line 499).
-
Writer Context (CPU 0, Interrupt Context):
While the reader is between steps 3 and 5, CPU 0 triggers a printk from lapbeth_xmit() -> vprintk_store() -> prb_reserve(). The ring buffer wraps around (0x170ee + DESCS_COUNT (0x2000) = 0x190ee), targeting the exact same descriptor index.
desc_reserve() pushes the tail and advances the head, executing:
atomic_long_try_cmpxchg(&desc->state_var, &prev_state_val, DESC_SV(0x190ee, desc_reserved)); /* desc_reserve:F */
Immediately following this, prb_reserve() executes:
seq = info->seq;
memset(info, 0, sizeof(*info)); /* line 1689 */
info->seq = seq + DESCS_COUNT(desc_ring); /* line 1712 */
-
Conflict:
CPU 0 performs an 88-byte plain write (memset) starting at info (&info->seq), followed by assigning info->seq = 0x190ee. Simultaneously, CPU 1 executes a plain 8-byte load from info->seq at line 499 (0x170ee -> 0x190ee).
Although the design of printk_ringbuffer guarantees that CPU 1's subsequent check of desc->state_var (desc_read:E) will observe id = 0x190ee != 0x170ee and return desc_miss (causing prb_first_seq() to discard seq and loop again), the memory access at line 499 is a plain unannotated C load that races against a plain write (memset). Under the Linux Kernel Memory Model (LKMM) and C concurrency rules, this constitutes an unannotated data race that is flagged by KCSAN.