KCSAN: data-race in lo_release / shmem_write_begin

Status: moderation: reported on 2026/11/18 00:04
Subsystems: block
Labels: prio:low race:benign
[Documentation on labels]
Reported-by: syzbot+52b746fa7af8b234e4b0@syzkaller.appspotmail.com
First crash: 2h28m, last: 2h28m
✨ AI cause bisection: introduced by (AI job) :
commit 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2
Author: Linus Torvalds <torvalds@ppc970.osdl.org>
Date: Sat Apr 16 22:20:36 2005 +0000

  Linux-2.6.12-rc2

  
✨ AI-generated root cause summary:

In drivers/block/loop.c, loop_assign_backing_file() and __loop_clr_fd() dynamically modify and restore the address_space::gfp_mask of the underlying backing file (stripping and re-adding __GFP_IO | __GFP_FS) using mapping_set_gfp_mask(). However, mapping_set_gfp_mask() is non-atomic and is only intended to be called before an address_space is activated and exposed to concurrent access. Because userspace can hold open file descriptors to the backing file and perform concurrent I/O while the loop device is released or cleared, tasks writing to the backing file (e.g., in shmem_write_begin() -> shmem_get_folio()) read mapping->gfp_mask without synchronization against __loop_clr_fd(), causing a KCSAN data race.

▶ ▼ ✨ Full AI root cause analysis
Based on kernel commit 63fdb511c27a:

Root Cause Analysis

Involved Functions, Structs, and Files


Technical Mechanism

  1. GFP Mask Manipulation in the Loop Driver: When configuring a backing file for a loop device in loop_assign_backing_file() (drivers/block/loop.c:518), the loop driver records the backing file's original mapping GFP mask and clears __GFP_IO | __GFP_FS using mapping_set_gfp_mask():

    lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
    mapping_set_gfp_mask(file->f_mapping,
                         lo->old_gfp_mask & ~(__GFP_IO | __GFP_FS));
    

    When the loop device is cleared (via LOOP_CLR_FD or auto-clear on final close in lo_release() -> __loop_clr_fd(), drivers/block/loop.c:1069), it restores the original GFP mask:

    mapping_set_gfp_mask(filp->f_mapping, gfp);
    
  2. Invariant Violation on struct address_space::gfp_mask: The helper mapping_set_gfp_mask() in include/linux/pagemap.h:360 is explicitly documented as non-atomic and restricted to initialization:

    /*
     * This is non-atomic. Only to be used before the mapping is activated.
     * Probably needs a barrier...
     */
    static inline void mapping_set_gfp_mask(struct address_space *m, gfp_t mask)
    {
        m->gfp_mask = mask;
    }
    

    In normal pagecache usage, mapping_set_gfp_mask() is called only during inode or GEM object creation before the mapping is active and reachable by other threads.

  3. Triggering the Race: When a loop device is configured with a backing file (e.g., a shmem/tmpfs file), the userspace process or other processes retain open file descriptors to the backing file or can open it independently.

    While Task 6007 closes the loop device fd (lo_release() -> __loop_clr_fd()), executing:

    mapping_set_gfp_mask(filp->f_mapping, gfp);  // writes 0x00100cca (__GFP_IO | __GFP_FS restored)
    

    Task 6008 concurrently performs a write() system call on the same backing file descriptor, invoking shmem_file_write_iter() -> generic_perform_write() -> shmem_write_begin() -> shmem_get_folio(), which reads:

    mapping_gfp_mask(inode->i_mapping);          // reads 0x00100c0a
    

    Because address_space::gfp_mask is not protected by any lock shared between the loop driver and generic file operations, and neither accessor uses atomic or marked (READ_ONCE() / WRITE_ONCE()) semantics, KCSAN detects the unsynchronized write/read data race.

▶ ▼ ✨ AI Jobs (3)
ID Workflow Result Correct Bug Created Started Finished Revision
0446e1f4 assessment-kcsan Benign: ✅ ❓ KCSAN: data-race in lo_release / shmem_write_begin 2026/11/17 22:35 2026/11/17 22:35 2026/11/17 23:04 ca622473
db109280 bisect-cause ❓ KCSAN: data-race in lo_release / shmem_write_begin 2026/11/17 22:34 2026/11/17 22:34 2026/11/17 23:07 ca622473
67dc1e0a assessment-security DenialOfService: ❌ Exploitable: ❌ FilesystemTrigger: ❌ FuzzingArtifact: ❌ MemoryCorruption: ❌ NetworkNeedsSetup: ❌ NetworkTrigger: ❌ PeripheralTrigger: ❌ RemoteTrigger: ❌ Unprivileged: ❌ UserNamespace: ❌ VMGuestTrigger: ❌ VMHostTrigger: ❌ ❓ KCSAN: data-race in lo_release / shmem_write_begin 2026/11/17 22:29 2026/11/17 22:29 2026/11/17 23:03 ca622473

Sample crash report:
==================================================================
BUG: KCSAN: data-race in lo_release / shmem_write_begin

write to 0xffff88811b7d0d00 of 4 bytes by task 6007 on cpu 1:
 mapping_set_gfp_mask include/linux/pagemap.h:365 [inline]
 __loop_clr_fd drivers/block/loop.c:1069 [inline]
 lo_release+0x2d5/0x410 drivers/block/loop.c:1673
 bdev_release+0x3e3/0x440 block/bdev.c:-1
 blkdev_release+0x15/0x20 block/fops.c:690
 __fput+0x292/0x660 fs/file_table.c:512
 fput_close_sync+0xd5/0x1d0 fs/file_table.c:618
 filp_close_sync+0x27/0x40 fs/open.c:1574
 __range_close fs/file.c:919 [inline]
 __do_sys_close_range fs/file.c:1003 [inline]
 __se_sys_close_range+0x765/0x880 fs/file.c:954
 __x64_sys_close_range+0x43/0x50 fs/file.c:954
 x64_sys_call+0x2198/0x2550 arch/x86/include/generated/asm/syscalls_64.h:437
 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 to 0xffff88811b7d0d00 of 4 bytes by task 6008 on cpu 0:
 mapping_gfp_mask include/linux/pagemap.h:349 [inline]
 shmem_get_folio mm/shmem.c:2880 [inline]
 shmem_write_begin+0xdc/0x210 mm/shmem.c:3514
 generic_perform_write+0x198/0x510 mm/filemap.c:4400
 shmem_file_write_iter+0xc5/0xf0 mm/shmem.c:3689
 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

value changed: 0x00100c0a -> 0x00100cca

Reported by Kernel Concurrency Sanitizer on:
CPU: 0 UID: 0 PID: 6008 Comm: syz.4.691 Not tainted syzkaller #0 PREEMPT(lazy) 
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/06/2026
==================================================================

Crashes (1):
Time Kernel Commit Syzkaller Config Log Report Syz repro C repro VM info Assets (help?) Manager Title
2026/11/17 22:24 upstream 63fdb511c27a 122f94d5 .config console log report info [disk image] [vmlinux] [kernel image] ci2-upstream-kcsan-gce KCSAN: data-race in lo_release / shmem_write_begin
* Struck through repros no longer work on HEAD.