Technical Root Cause Analysis
1. Memory Layout and Union Aliasing
In the Linux kernel's memory descriptor layout (include/linux/mm_types.h), struct page and struct folio use an overlapping union for page/folio metadata at offset 8 (the second 8-byte word on 64-bit systems):
This overlay is explicitly documented and matched via FOLIO_MATCH(compound_info, lru) at line 517 of include/linux/mm_types.h. As a consequence, folio->lru.next and page->compound_info share the exact same 8-byte memory location (e.g. 0xffffea0004218548).
For head pages and single (order-0) folios, bit 0 of this word must remain 0 to avoid false-positive detection of tail pages (PageTail()), as checked by _compound_head() (include/linux/page-flags.h):
static __always_inline unsigned long _compound_head(const struct page *page)
{
unsigned long info = READ_ONCE(page->compound_info);
...
if (!compound_info_has_mask()) {
if (info & 1)
return info - 1;
return (unsigned long)page;
}
mask = (info & 1) - 1;
mask |= info;
return (unsigned long)page & mask;
}
2. Concurrency Window and Trigger Path
The data race occurs due to how folios are queued for swap-in read I/O versus LRU batch draining:
-
Swap-in Allocation and LRU Batching (mm/swap_state.c):
During asynchronous swap-in (e.g., swap_cache_read_folio() or swapin_sync()), a new locked folio is allocated and queued into the local CPU's per-CPU lru_add batch via folio_add_lru() before submitting the asynchronous block I/O via swap_read_folio(). At this point, the read I/O is still in-flight and the folio remains locked.
-
LRU Batch Drain on CPU 0 (mm/folio.c, include/linux/mm_inline.h, include/linux/list.h):
While the swap-read bio is in flight, CPU 0 triggers an LRU drain (e.g. via do_swap_page() -> do_wp_page() -> wp_can_reuse_anon_folio() -> lru_add_drain()). Draining the batch calls folio_batch_move_lru() -> lru_add(), which takes lruvec->lru_lock (without needing the folio lock) and inserts the folio into the LRU list:
lruvec_add_folio(lruvec, folio);
list_add(&folio->lru, &lruvec->lists[lru]);
__list_add(new, head, head->next);
In __list_add() (include/linux/list.h:173):
next->prev = new;
new->next = next; // Plain, unmarked 8-byte store to &folio->lru.next
new->prev = prev;
WRITE_ONCE(prev->next, new);
The write new->next = next is an unmarked store to folio->lru.next at offset 8.
-
Softirq Swap Read Completion on CPU 1 (mm/page_io.c, include/linux/bvec.h):
Concurrently on CPU 1, interrupt/softirq processing finishes the swap bio:
blk_done_softirq() -> ... -> bio_endio() -> swap_bio_read_end_io() -> swap_read_end().
In swap_read_end():
for (p = 0; p < sio->nr_bvecs; p++) {
struct folio *folio = bvec_folio(&sio->bvecs[p]);
...
folio_unlock(folio);
}
bvec_folio() calls page_folio(bv->bv_page) -> _compound_head().
_compound_head() reads page->compound_info using READ_ONCE(page->compound_info).
3. Why KCSAN Flags the Race
Under the Linux Kernel Memory Model (LKMM), a concurrent unmarked plain write (new->next = next) racing against a marked read (READ_ONCE(page->compound_info)) constitutes a data race. Because folio->lru.next and page->compound_info alias the exact same word, KCSAN detects and reports the concurrent read and write.