syzbot
KCSAN: data-race in shmem_recalc_inode / shmem_writeout
Status:
moderation: reported on 2026/11/20 00:01
Subsystems:
mm
Labels:
prio:low
race:benign
[Documentation on labels]
Reported-by: syzbot+d982dbed29a35e0e6571@syzkaller.appspotmail.com
First crash: 2d21h, last: 3h15m
✨ AI-generated root cause summary:
In mm/shmem.c, shmem_recalc_inode() (invoked from shmem_writeout()) reads inode->i_mapping->nrpages using READ_ONCE() while holding only the shmem inode info->lock, without holding the mapping->i_pages lock. Concurrently, writeout of another folio for the same inode in shmem_delete_from_page_cache() decrements mapping->nrpages under xa_lock_irq(&mapping->i_pages) using an unmarked read-modify-write operation (mapping->nrpages -= nr). Because the write operation is unmarked and executed without a common lock shared with the reader, KCSAN flags this access pattern as a data race between a marked read and an unmarked write under the Linux Kernel Memory Model.
▶
▼
✨ Full AI root cause analysis
Root Cause Analysis
1. Functions, Data Structures, and Invariants Involved
- Source File:
mm/shmem.c (also referencing definitions in include/linux/fs.h).
- Data Structures and Fields:
struct address_space (include/linux/fs.h): field unsigned long nrpages, documented as protected by &mapping->i_pages XArray lock.
struct shmem_inode_info (mm/shmem.h): fields alloced, swapped, and spinlock_t lock.
- Functions:
shmem_recalc_inode() (mm/shmem.c:444): called during shmem swap writeout (shmem_writeout() at mm/shmem.c:1936) to update info->alloced and info->swapped. It acquires spin_lock(&info->lock) without acquiring mapping->i_pages XArray lock.
shmem_delete_from_page_cache() (mm/shmem.c:1168): called during writeout (shmem_writeout() at mm/shmem.c:1962) to replace the folio entry with a swap entry in the mapping's XArray and decrement mapping->nrpages. It acquires xa_lock_irq(&mapping->i_pages) without acquiring info->lock.
2. Triggering Mechanism & Race Condition
-
Concurrent Pageout / Writeout on the Same Inode:
Under memory pressure (e.g. shrink_folio_list() or mem_cgroup_force_empty), two tasks concurrently reclaim different folios belonging to the same tmpfs/shmem inode:
- Task 1 (CPU 0) is at the start of
shmem_writeout() (mm/shmem.c:1936) executing shmem_recalc_inode(inode, 0, nr_pages).
- Task 2 (CPU 1) is further along in
shmem_writeout() (mm/shmem.c:1962) executing shmem_delete_from_page_cache().
-
Locking Decoupling and Missing LKMM Annotation:
- In
shmem_recalc_inode(), info->lock is held to update the inode's block allocation accounting. To avoid deadlocks, high lock contention, and sleep-inside-atomic issues during block unaccounting, shmem_recalc_inode() intentionally avoids acquiring xa_lock_irq(&mapping->i_pages).
- Instead,
shmem_recalc_inode() inspects nrpages using READ_ONCE(inode->i_mapping->nrpages) at mm/shmem.c:454:
freed = info->alloced - info->swapped -
READ_ONCE(inode->i_mapping->nrpages);
- Meanwhile, Task 2 executes
shmem_delete_from_page_cache() under xa_lock_irq(&mapping->i_pages):
xa_lock_irq(&mapping->i_pages);
error = shmem_replace_entry(mapping, folio->index, folio, radswap);
folio->mapping = NULL;
mapping->nrpages -= nr;
shmem_update_stats(folio, -nr);
xa_unlock_irq(&mapping->i_pages);
- At line 1177,
mapping->nrpages -= nr; is a plain, unmarked read-modify-write operation.
- Because Task 1 reads
mapping->nrpages via READ_ONCE() while Task 2 performs an unmarked write to mapping->nrpages without holding a shared lock, this violates LKMM rules regarding data races between concurrent plain writes and marked reads, triggering the KCSAN report.
Sample crash report:
* Struck through repros no longer work on HEAD.