KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages (4)

Status: moderation: reported on 2026/11/05 01:31
Subsystems: fs mm
Labels: race:benign prio:low
[Documentation on labels]
Reported-by: syzbot+5d380c3d6727a9581fd4@syzkaller.appspotmail.com
First crash: 11d, last: 2d20h
✨ AI-generated root cause summary:

A KCSAN data race occurs between __filemap_add_folio() modifying mapping->nrpages and nr_blockdev_pages() reading inode->i_mapping->nrpages. In struct address_space, nrpages is protected by the i_pages XArray spinlock (xa_lock), which __filemap_add_folio() holds during modification. nr_blockdev_pages() iterates over blockdev_superblock->s_inodes holding only s_inode_list_lock and performs an unannotated, lockless plain read of inode->i_mapping->nrpages without holding xa_lock or using data_race() / READ_ONCE(). Because nr_blockdev_pages() only computes an approximate bufferram statistic for si_meminfo(), this unannotated lockless read triggers KCSAN.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 05441d697f7f:

Root Cause Analysis

1. Involved Components and Data Structures

  • Source Files:
    • block/bdev.c: Contains nr_blockdev_pages(), which calculates the total page count across all block devices.
    • mm/filemap.c: Contains __filemap_add_folio(), which adds newly allocated folios to an address space's page cache.
    • include/linux/fs.h: Defines struct address_space.
  • Structures and Fields:
    • struct address_space::nrpages: Tracks the total number of page entries present in the address space page cache. As documented in include/linux/fs.h, this field is protected by the i_pages lock (xa_lock).
    • struct super_block::s_inode_list_lock: Protects the list of inodes associated with blockdev_superblock.

2. Synchronization Mechanism and Invariant Violation

According to the synchronization rules of the page cache (struct address_space), modifications to mapping->nrpages must be performed while holding the i_pages.xa_lock:

/* mm/filemap.c: __filemap_add_folio() */
xas_lock_irq(&xas);
...
xas_store(&xas, folio);
if (xas_error(&xas))
    goto unlock;

mapping->nrpages += nr;  /* line 919 */

unlock:
xas_unlock_irq(&xas);

In nr_blockdev_pages(), the kernel calculates the total number of block device buffer pages (val->bufferram) for si_meminfo() by iterating through all block device inodes on blockdev_superblock->s_inodes:

/* block/bdev.c: nr_blockdev_pages() */
long nr_blockdev_pages(void)
{
    struct inode *inode;
    long ret = 0;

    spin_lock(&blockdev_superblock->s_inode_list_lock);
    list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
        ret += inode->i_mapping->nrpages;  /* line 568 */
    spin_unlock(&blockdev_superblock->s_inode_list_lock);

    return ret;
}

While nr_blockdev_pages() holds blockdev_superblock->s_inode_list_lock to protect the list traversal of block device inodes, it intentionally does not acquire each individual block device's inode->i_mapping->i_pages.xa_lock when reading nrpages, avoiding global spinlock contention with block device I/O and readahead paths.

However, the read ret += inode->i_mapping->nrpages; is an unannotated plain read. When a concurrent thread (such as task 8211 executing __filemap_add_folio()) concurrently executes mapping->nrpages += nr;, KCSAN detects a data race between the concurrent read-write and the unannotated read on mapping->nrpages.

3. Resolution

To inform concurrency tools and the compiler that lockless, approximate reads of mapping->nrpages in statistical accounting functions like nr_blockdev_pages() are intentional, the read should be annotated using data_race(...) or READ_ONCE(...) (e.g., ret += data_race(inode->i_mapping->nrpages);).

▶ ▼ ✨ AI Jobs (2)
ID Workflow Result Correct Bug Created Started Finished Revision
b91f9fa8 assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ✅ UserNamespace: ✅ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages (4) 2026/11/05 01:01 2026/11/05 01:01 2026/11/05 01:23 73438ed0
9b6621cd assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages (4) 2026/11/05 00:51 2026/11/05 00:51 2026/11/05 01:12 73438ed0
▶ ▼ Discussions (1)
Title Replies (including bot) Last reply
[PATCH] block: annotate the approximate block device page count 1 (1) 2026/11/11 06:47
▶ ▼ Similar bugs (3)
Kernel Title Rank 🛈 Repro Cause bisect Fix bisect Count Last Reported Patched Status
upstream KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages ext4 6 45 1725d 1831d 0/29 auto-closed as invalid on 2022/04/01 15:15
upstream KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages (2) ext4 6 63 1180d 1685d 0/29 auto-obsoleted due to no activity on 2023/09/28 20:31
upstream KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages (3) prio:low mm 6 180 92d 1072d 0/29 auto-obsoleted due to no activity on 2026/10/11 08:46

Sample crash report:
==================================================================
BUG: KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages

read-write to 0xffff8881004c4178 of 8 bytes by task 10048 on cpu 1:
 __filemap_add_folio+0x6c4/0x930 mm/filemap.c:918
 filemap_add_folio+0x1cd/0x380 mm/filemap.c:970
 __filemap_get_folio_mpol+0x309/0x830 mm/filemap.c:2057
 __filemap_get_folio include/linux/pagemap.h:758 [inline]
 grow_dev_folio fs/buffer.c:868 [inline]
 grow_buffers fs/buffer.c:934 [inline]
 __getblk_slow fs/buffer.c:952 [inline]
 bdev_getblk+0x127/0x370 fs/buffer.c:1362
 getblk_unmovable include/linux/buffer_head.h:425 [inline]
 ext4_getblk+0x1d8/0x540 fs/ext4/inode.c:1127
 ext4_bread_batch+0x5c/0x320 fs/ext4/inode.c:1196
 __ext4_find_entry+0x869/0xdf0 fs/ext4/namei.c:1603
 ext4_lookup_entry fs/ext4/namei.c:1705 [inline]
 ext4_lookup+0xcd/0x3a0 fs/ext4/namei.c:1771
 lookup_open+0x6aa/0x1050 fs/namei.c:4536
 open_last_lookups fs/namei.c:4774 [inline]
 path_openat+0x492/0x11c0 fs/namei.c:5013
 do_file_open+0x16c/0x290 fs/namei.c:5045
 do_sys_openat2+0xa1/0x140 fs/open.c:1443
 do_sys_open fs/open.c:1449 [inline]
 __do_sys_openat fs/open.c:1465 [inline]
 __se_sys_openat fs/open.c:1460 [inline]
 __x64_sys_openat+0xf2/0x120 fs/open.c:1460
 x64_sys_call+0x1f72/0x2550 arch/x86/include/generated/asm/syscalls_64.h:258
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x116/0x380 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

read to 0xffff8881004c4178 of 8 bytes by task 316 on cpu 0:
 nr_blockdev_pages+0x7e/0xd0 block/bdev.c:568
 si_meminfo+0x87/0xd0 mm/show_mem.c:81
 update_defense_level+0x44/0x5c0 net/netfilter/ipvs/ip_vs_ctl.c:102
 defense_work_handler+0x1f/0x80 net/netfilter/ipvs/ip_vs_ctl.c:235
 process_one_work kernel/workqueue.c:3495 [inline]
 process_scheduled_works+0x53f/0xa20 kernel/workqueue.c:3578
 worker_thread+0x56c/0x770 kernel/workqueue.c:3659
 kthread+0x223/0x270 kernel/kthread.c:436
 ret_from_fork+0x146/0x330 arch/x86/kernel/process.c:158
 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245

value changed: 0x0000000000000005 -> 0x0000000000000006

Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 316 Comm: kworker/u8:5 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
Workqueue: events_dfl_long defense_work_handler
==================================================================

Crashes (2):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/14 01:54 upstream 97d73c825122 819ebe34 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages
2026/11/05 00:48 upstream 05441d697f7f 73438ed0 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in __filemap_add_folio / nr_blockdev_pages
* Struck through repros no longer work on HEAD.