[BUG] ntfs: recursive mrec_lock in buffered write to a resident file mmap'd as the write() source (possible deadlock)
From: Guikun Yang
Date: Thu Dec 03 2026 - 01:09:51 EST
Hi Namjae, Hyunchul,
We found a lockdep-detectable recursive acquisition of
ntfs_inode::mrec_lock on a normal NTFS image. It needs no crafted
metadata beyond a resident file, and the trigger is a plain buffered
write whose source buffer is the same file's page cache.
## Reproduction (plain syscalls)
Mount a 2MB NTFS image in which /file1 is a 10-byte resident regular
file, then:
unlink("./file1");
r0 = openat(AT_FDCWD, "./file1", O_RDWR | O_CREAT, 0600);
p = mmap(NULL, 0x3000, PROT_READ, MAP_SHARED, r0, 0);
write(r0, p + 0x80, 1);
The source page is not in the page cache, so the write's copy source is
faulted in through the file's own read path while the write path still
holds mrec_lock.
## The chain (master v7.4-rc5 d89e1d186ec9; v7.3.0 numbers in parentheses)
1. ntfs_file_write_iter() -> iomap_file_buffered_write()
(fs/ntfs/file.c:851/933; v7.3.0: 580/656)
2. iomap_iter() runs ->iomap_begin first:
__ntfs_write_iomap_begin() takes mrec_lock for a resident inode
(fs/ntfs/iomap.c:736/749; v7.3.0: 715/725), stores it in
iomap->private, and releases it only in
ntfs_write_iomap_end_resident() (fs/ntfs/iomap.c:764-771;
v7.3.0: 740-748) after the whole body.
3. Only then does iomap_write_iter() call
fault_in_iov_iter_readable() (fs/iomap/buffered-io.c:1227, same in
v7.3.0).
4. The source page is not cached -> filemap_fault -> ntfs_read_folio ->
iomap_read_folio -> ntfs_read_iomap_begin_resident()
(fs/ntfs/iomap.c:83/94-98; v7.3.0: 83/98) ->
mutex_lock(&base_ni->mrec_lock) again on the same task.
For a resident regular file base_ni == ni, so this is a recursive
acquisition of the same mutex. mutex_lock is not recursive; lockdep
reports "possible recursive locking" at the second acquire.
The two chains, abbreviated:
write: inode_lock -> mrec_lock (iomap begin) -> iomap_write_iter body
-> fault_in_iov_iter_readable
fault: filemap_fault -> ntfs_read_folio -> iomap_read_folio
-> ntfs_read_iomap_begin_resident -> mrec_lock (same ni)
## Probable introduction
d9e00c457d4a ("ntfs: serialize resident iomap reads with mrec_lock")
added the read-side lock to close a KCSAN race. The write side already
held mrec_lock across the iomap body, so that change also made the
read fault path acquire the same lock recursively for this input.
## Suggested fix
On the resident write path, do not hold mrec_lock across the copy: copy
the resident attribute to a local buffer before returning the
IOMAP_INLINE mapping, or do not retain the lock for write-side mappings
and re-acquire it only around the metadata update.
More generally, any filesystem that keeps a private lock across the
whole iomap_iter body for IOMAP_INLINE mappings has the same structure.
## Boundaries
- lockdep reports a "possible" recursive acquire; no actual hang or
deadlock was observed.
- Four independent fuzzing instances reproduced the same lock order.
- /file1 was confirmed to be a 10-byte resident regular file in the
image, freshly mounted.
- No privilege boundary is claimed.
- The 2MB image and a small C reproducer are available; I can send them
if useful.
- Happy to test candidate patches.
Thanks,
Guikun