Technical Explanation of Root Cause
1. Components and Files Involved
- File:
kernel/printk/printk_ringbuffer.c
- Functions:
data_make_reusable(), data_push_tail(), data_alloc()
- Structs and Fields:
struct prb_data_block: contains unsigned long id; and char data[];
struct prb_data_ring: represents the data buffer ring (text_data_ring) with head_lpos and tail_lpos
struct prb_desc_ring: descriptor ring storing metadata and descriptor state
- Interacting Call:
number() (lib/vsprintf.c:568) invoked via vprintk_store() -> printk_sprint() -> vscnprintf() -> vsnprintf()
2. Triggering Mechanism and Race Condition
The printk ringbuffer is a lockless multi-reader multi-writer data structure. It manages message text using text_data_ring as a circular byte array made up of data blocks (struct prb_data_block). Each data block begins with a header field id (an unsigned long identifying the owning descriptor) followed by the message text in data[].
When the ring buffer is full and a writer CPU (Task 9053) allocates space:
- It advances
tail_lpos and head_lpos in data_alloc(), claims a region of text_data_ring that wraps around, and sets r.text_buf to &blk->data[0].
- The writer then formats its message directly into
r.text_buf via printk_sprint() -> vscnprintf() -> vsnprintf() -> number(), performing plain byte stores into the memory area (*buf++ = c).
Concurrently, another writer CPU (Task 3248) is attempting to reserve space and calls data_push_tail(), which executes data_make_reusable() to identify and retire old data blocks:
/* kernel/printk/printk_ringbuffer.c:600-607 */
/*
* Load the block ID from the data block. This is a data race
* against a writer that may have newly reserved this data
* area. If the loaded value matches a valid descriptor ID,
* the blk_lpos of that descriptor will be checked to make
* sure it points back to this data block. If the check fails,
* the data area has been recycled by another writer.
*/
id = blk->id; /* LMM(data_make_reusable:A) */
Because id = blk->id; is an unannotated plain C read, it directly accesses memory that Task 9053 has newly recycled and is concurrently overwriting with formatted text characters in number().
3. Invariants and Sanitizer Violation
- The ringbuffer design deliberately permits this data race: comments at line 600 and in
data_alloc() (lines 1103–1107) explicitly acknowledge that another CPU reading blk->id may read garbage or torn data, and rely on downstream validation (desc_read() state checks, blk_lpos->begin == lpos_begin cross-checks, and atomic descriptor state updates) to reject invalid IDs and reload tail_lpos.
- However, under the Linux kernel concurrency rules, plain memory accesses that intentionally race against concurrent writers must be marked with concurrency annotations such as
data_race() and READ_ONCE() (e.g., id = data_race(READ_ONCE(blk->id));).
- Because
id = blk->id; lacks these annotations, KCSAN detects an unannotated concurrent write (1 byte by number()) and read (8 bytes by data_make_reusable()) at the same memory address (0xffffffff896307f8), resulting in the reported splat.