KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode (3)

Status: moderation: reported on 2026/11/20 00:04
Subsystems: mm
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+5e144c78a31a24f3faf6@syzkaller.appspotmail.com
First crash: 2d21h, last: 15h50m
✨ AI-generated root cause summary:

shmem_recalc_inode() reads inode->i_mapping->nrpages using READ_ONCE() while holding SHMEM_I(inode)->lock, whereas shmem_add_to_page_cache() updates mapping->nrpages += nr with a plain (unmarked) read-modify-write while holding mapping->i_pages.xa_lock. Because these two functions hold different locks, KCSAN reports a data race between the marked read at mm/shmem.c:454 and the unmarked write at mm/shmem.c:1151. On 64-bit systems this race is benign: nrpages is an aligned unsigned long serialized among writers by xa_lock, and shmem_recalc_inode() handles transient mismatches before the caller updates info->alloced or info->swapped. Missing WRITE_ONCE() annotations on mapping->nrpages updates (also present in shmem_delete_from_page_cache(), shmem_charge(), and mm/filemap.c helpers) trigger the KCSAN report.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 4405c18c95d2:

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

  1. 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.

  2. 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.

▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
d5511d9f assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode (3) 2026/11/19 21:09 2026/11/19 21:09 2026/11/19 21:11 229865fb
ac038607 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode (3) 2026/11/19 21:07 2026/11/19 21:07 2026/11/19 21:09 229865fb
▶ ▼ Similar bugs (2)
Kernel Title Rank 🛈 Repro Cause bisect Fix bisect Count Last Reported Patched Status
upstream KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode (2) mm 6 6 1198d 1215d 0/29 auto-obsoleted due to no activity on 2023/09/13 03:46
upstream KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode mm 6 113 2352d 2486d 0/29 auto-closed as invalid on 2020/07/16 16:35

Sample crash report:
==================================================================
BUG: KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode

read (marked) to 0xffff888118c3cfd0 of 8 bytes by task 6760 on cpu 1:
 shmem_recalc_inode+0x6e/0x1f0 mm/shmem.c:454
 shmem_writeout+0x46e/0x9b0 mm/shmem.c:1936
 pageout mm/vmscan.c:730 [inline]
 shrink_folio_list+0x1fb5/0x2d70 mm/vmscan.c:1505
 evict_folios+0x1fa4/0x2990 mm/vmscan.c:5097
 try_to_shrink_lruvec+0x9fb/0xd60 mm/vmscan.c:5271
 lru_gen_shrink_lruvec mm/vmscan.c:5422 [inline]
 shrink_lruvec mm/vmscan.c:6199 [inline]
 shrink_node_memcgs mm/vmscan.c:6438 [inline]
 shrink_node+0x97e/0x4600 mm/vmscan.c:6482
 shrink_zones mm/vmscan.c:6701 [inline]
 do_try_to_free_pages+0x310/0xb80 mm/vmscan.c:6763
 try_to_free_mem_cgroup_pages+0x201/0x450 mm/vmscan.c:7044
 mem_cgroup_force_empty mm/memcontrol-v1.c:1502 [inline]
 mem_cgroup_force_empty_write+0x6b/0x160 mm/memcontrol-v1.c:1518
 cgroup_file_write+0x197/0x350 kernel/cgroup/cgroup.c:4426
 kernfs_fop_write_iter+0x1d2/0x2e0 fs/kernfs/file.c:345
 new_sync_write fs/read_write.c:575 [inline]
 vfs_write+0x57a/0x9b0 fs/read_write.c:667
 ksys_write+0xdc/0x1a0 fs/read_write.c:719
 __do_sys_write fs/read_write.c:730 [inline]
 __se_sys_write fs/read_write.c:727 [inline]
 __x64_sys_write+0x40/0x50 fs/read_write.c:727
 x64_sys_call+0x154e/0x2550 arch/x86/include/generated/asm/syscalls_64.h:2
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x11e/0x3a0 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read-write to 0xffff888118c3cfd0 of 8 bytes by task 6656 on cpu 0:
 shmem_add_to_page_cache+0x53a/0x650 mm/shmem.c:1151
 shmem_alloc_and_add_folio mm/shmem.c:2252 [inline]
 shmem_get_folio_gfp+0x4f0/0xcb0 mm/shmem.c:2792
 shmem_get_folio mm/shmem.c:2891 [inline]
 shmem_write_begin+0xfc/0x210 mm/shmem.c:3526
 generic_perform_write+0x198/0x510 mm/filemap.c:4407
 shmem_file_write_iter+0xc5/0xf0 mm/shmem.c:3701
 __kernel_write_iter+0x30d/0x5a0 fs/read_write.c:601
 dump_write_iter fs/coredump.c:1360 [inline]
 dump_emit_page fs/coredump.c:1533 [inline]
 dump_user_range+0x78e/0x950 fs/coredump.c:1601
 elf_core_dump+0x2183/0x2310 fs/binfmt_elf.c:2106
 coredump_write fs/coredump.c:1150 [inline]
 do_coredump+0x3abb/0x4040 fs/coredump.c:1237
 vfs_coredump+0x757/0x990 fs/coredump.c:1312
 get_signal+0xb41/0xd30 kernel/signal.c:3098
 arch_do_signal_or_restart+0x96/0x480 arch/x86/kernel/signal.c:337
 __exit_to_user_mode_loop kernel/entry/common.c:66 [inline]
 exit_to_user_mode_loop kernel/entry/common.c:101 [inline]
 __exit_to_user_mode_prepare include/linux/irq-entry-common.h:207 [inline]
 irqentry_exit_to_user_mode_prepare include/linux/irq-entry-common.h:244 [inline]
 irqentry_exit_to_user_mode include/linux/irq-entry-common.h:315 [inline]
 irqentry_exit+0x15f/0x640 kernel/entry/common.c:152
 asm_exc_page_fault+0x26/0x30 arch/x86/include/asm/idtentry.h:595

Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 6656 Comm: syz.0.622 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================
batman_adv: batadv0: Interface deactivated: syz_tun
batman_adv: batadv0: Removing interface: syz_tun

Crashes (5):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/19 08:53 upstream 4405c18c95d2 f138241c .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode
2026/11/18 22:13 upstream 4405c18c95d2 2006bdcc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode
2026/11/18 07:58 upstream 20eef29b7c84 eb4895d7 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode
2026/11/17 11:58 upstream 666539eecdf4 43af6cac .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode
2026/11/17 02:47 upstream 666539eecdf4 a3e64cfc .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in shmem_add_to_page_cache / shmem_recalc_inode
* Struck through repros no longer work on HEAD.