Root Cause Analysis
KCSAN reports a data race on struct address_space::nrpages (8-byte unsigned long at offset 80 of struct address_space) between shmem_recalc_inode() and shmem_add_to_page_cache() in mm/shmem.c.
Conflicting Accesses
-
Marked read (mm/shmem.c:454) in shmem_recalc_inode() (called from shmem_writeout() during cgroup reclaim):
spin_lock(&info->lock);
info->alloced += alloced;
info->swapped += swapped;
freed = info->alloced - info->swapped -
READ_ONCE(inode->i_mapping->nrpages);
Here info is struct shmem_inode_info * (SHMEM_I(inode)). shmem_recalc_inode() holds info->lock, not mapping->i_pages.xa_lock, and uses READ_ONCE() to read nrpages locklessly.
-
Unmarked read-modify-write (mm/shmem.c:1151) in shmem_add_to_page_cache() (called from shmem_alloc_and_add_folio() during a write to a shmem file):
xas_lock_irq(&xas);
...
xas_store(&xas, folio);
if (xas_error(&xas))
goto unlock;
shmem_update_stats(folio, nr);
mapping->nrpages += nr;
unlock:
xas_unlock_irq(&xas);
shmem_add_to_page_cache() holds mapping->i_pages.xa_lock (xas_lock_irq(&xas)), not info->lock, and updates mapping->nrpages with a plain += nr without WRITE_ONCE().
Why the Race Occurs and Invariant Handling
- Locking mismatch:
struct address_space::nrpages is documented (include/linux/fs.h:465) as protected by the i_pages lock (xa_lock), so all writers (shmem_add_to_page_cache(), shmem_delete_from_page_cache(), shmem_charge(), and generic mm/filemap.c helpers) serialize updates under xa_lock. However, shmem_recalc_inode() and the error path in shmem_alloc_and_add_folio() (mm/shmem.c:2271) read nrpages under info->lock without acquiring xa_lock, using READ_ONCE() on the reader side without a matching WRITE_ONCE() on the writer side.
- Trigger: Task 6656 (
shmem_file_write_iter() -> shmem_alloc_and_add_folio() -> shmem_add_to_page_cache()) inserts a folio into mapping->i_pages and increments mapping->nrpages, concurrently with Task 6760 (mem_cgroup_force_empty_write() -> shmem_writeout() -> shmem_recalc_inode()) writing out another folio of the same shmem inode and reading inode->i_mapping->nrpages.
- Lifecycle / accounting correctness: Seeing the incremented
nrpages before shmem_alloc_and_add_folio() reaches shmem_recalc_inode(inode, pages, 0) at mm/shmem.c:2284 temporarily makes freed = info->alloced - info->swapped - nrpages smaller or negative (skipping shmem_inode_unacct_blocks()), and the caller's follow-up shmem_recalc_inode() call reconciles info->alloced and unaccounts any freed blocks.
Related Unmarked Writes
The same missing WRITE_ONCE() pattern exists in other xa_lock-protected updates to mapping->nrpages, including shmem_delete_from_page_cache() (mm/shmem.c:1177), shmem_charge() (mm/shmem.c:486), page_cache_delete() (mm/filemap.c:147), page_cache_delete_batch() (mm/filemap.c:309), __filemap_add_folio() (mm/filemap.c:918), and collapse_file() (mm/khugepaged.c:2650). Using WRITE_ONCE(mapping->nrpages, mapping->nrpages + nr) (or corresponding helpers) resolves the KCSAN report.