syzbot
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: 2h43m, last: 2h43m
✨ 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
Root Cause Analysis
Involved Functions, Structs, and Files
- Source Files:
- Functions:
- Structs and Fields:
Technical Mechanism
-
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);
-
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.
-
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.
Sample crash report:
* Struck through repros no longer work on HEAD.