mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] fs/ntfs3: serialize size changes with mapping->invalidate_lock
@ 2026-11-26 22:12 Mostafa Amin via B4 Relay
  2026-12-03  6:58 ` [syzbot ci] " syzbot ci
  0 siblings, 1 reply; 2+ messages in thread
From: Mostafa Amin via B4 Relay @ 2026-11-26 22:12 UTC (permalink / raw)
  To: Konstantin Komarov
  Cc: ntfs3, syzbot+079e16bc964fb91acb4a, linux-fsdevel, linux-kernel,
	stable, Mostafa Amin

From: Mostafa Amin <mostafa.amin@windriver.com>

syzbot reported a WARNING in iomap_read_inline_data() ("size >
iomap->length") triggered by concurrent ftruncate() and mmap() page
faults on a resident NTFS3 file:

  CPU0 (filemap_fault)               CPU1 (ntfs_setattr / write_iter)
  --------------------               --------------------------------
  filemap_invalidate_lock_shared()
  ntfs_read_folio()
    ntfs_iomap_begin()
      attr_data_get_block()
        iomap->length = 5
                                     truncate_setsize(inode, 20)
                                       i_size_write(inode, 20)
                                     ntfs_set_size(inode, 20)
    iomap_read_inline_data()
      size = i_size_read(inode) = 20
      WARN_ON_ONCE(20 > 5) -> -EIO

iomap_read_inline_data() copies i_size_read(inode) - iomap->offset bytes
out of iomap->inline_data and warns with -EIO if that exceeds
iomap->length.  In ntfs3, attr_data_get_block_locked() snapshots a
resident $DATA attribute into an allocated page under ni_lock and sets
iomap->length to res.data_size, dropping ni_lock before returning from
ntfs_iomap_begin().

Meanwhile, ntfs_setattr() updates inode->i_size via truncate_setsize()
before ntfs_set_size() grows res.data_size and without holding
mapping->invalidate_lock.  Extending writes in ntfs_file_write_iter()
and preallocation in ntfs_fallocate() likewise resize the attribute and
inode->i_size via ntfs_set_size() without mapping->invalidate_lock,
allowing a concurrent page fault or buffered read to observe the new
inode->i_size against an inline iomap built from the old resident size.

Serialize regular-file size and resident/non-resident layout changes
against pagecache readers and faults with filemap_invalidate_lock():

  - In ntfs_setattr(), hold mapping->invalidate_lock across ATTR_SIZE
    updates; when extending, truncate any stray pagecache beyond oldsize
    and run ntfs_set_size() before pagecache_isize_extended() so i_size
    never leads res.data_size or transiently changes on error.
  - In ntfs_file_write_iter(), reject extending IOCB_NOWAIT writes with
    -EAGAIN and hold mapping->invalidate_lock around ntfs_set_size().
  - In ntfs_fallocate(), hold mapping->invalidate_lock across all modes,
    including default preallocation which converts resident files to
    non-resident and extends i_size.

Fixes: 099ef9ab9203 ("fs/ntfs3: implement iomap-based file operations")
Reported-by: syzbot+079e16bc964fb91acb4a@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=079e16bc964fb91acb4a
Cc: stable@vger.kernel.org
Signed-off-by: Mostafa Amin <mostafa.amin@windriver.com>
---
Tested on v7.4-rc4 (x86_64 QEMU/KVM, 4 vCPUs, CONFIG_KASAN=y,
CONFIG_PROVE_LOCKING=y, CONFIG_UBSAN=y, CONFIG_DEBUG_ATOMIC_SLEEP=y,
CONFIG_DEBUG_KMEMLEAK=y, panic_on_warn=1):
  - syzbot C reproducer (ftruncate vs mmap + MADV_DONTNEED): 3/3
    crashes before fix, 0/10 crashes after fix, 3/3 crashes with fix
    reverted.
  - Extending pwrite + fsync + POSIX_FADV_DONTNEED race and
    resident-to-nonresident data-integrity suite: 1/1 crash before
    fix, 0/3 after fix.
  - W=1 build of fs/ntfs3/ on debug (CONFIG_NTFS3_FS=y) and
    allmodconfig (CONFIG_NTFS3_FS=m): 0 warnings.

Not tested: sparse/smatch/coccinelle, non-x86_64 runtime, full xfstests.
---
 fs/ntfs3/file.c | 34 ++++++++++++++++++++++++----------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/fs/ntfs3/file.c b/fs/ntfs3/file.c
index 6c84a3e1c7fe..33fb6dd91503 100644
--- a/fs/ntfs3/file.c
+++ b/fs/ntfs3/file.c
@@ -518,12 +518,9 @@ static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
 		goto out;
 	}
 
-	if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
-		    FALLOC_FL_INSERT_RANGE)) {
-		inode_dio_wait(inode);
-		filemap_invalidate_lock(mapping);
-		map_locked = true;
-	}
+	inode_dio_wait(inode);
+	filemap_invalidate_lock(mapping);
+	map_locked = true;
 
 	if (mode & FALLOC_FL_PUNCH_HOLE) {
 		u32 frame_size;
@@ -736,15 +733,25 @@ int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 			goto out;
 		}
 		inode_dio_wait(inode);
+		filemap_invalidate_lock(inode->i_mapping);
 		oldsize = i_size_read(inode);
 		newsize = attr->ia_size;
 
 		if (newsize != oldsize) {
-			truncate_setsize(inode, newsize);
-
-			err = ntfs_set_size(inode, newsize);
+			if (newsize > oldsize) {
+				truncate_pagecache(inode, oldsize);
+				err = ntfs_set_size(inode, newsize);
+				if (!err)
+					pagecache_isize_extended(inode, oldsize,
+								 newsize);
+			} else {
+				truncate_setsize(inode, newsize);
+				err = ntfs_set_size(inode, newsize);
+				if (err)
+					i_size_write(inode, oldsize);
+			}
 			if (err) {
-				i_size_write(inode, oldsize);
+				filemap_invalidate_unlock(inode->i_mapping);
 				goto out;
 			}
 
@@ -753,6 +760,7 @@ int ntfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 			inode_set_mtime_to_ts(inode,
 					      inode_set_ctime_current(inode));
 		}
+		filemap_invalidate_unlock(inode->i_mapping);
 	}
 
 	setattr_copy(idmap, inode, attr);
@@ -1288,11 +1296,17 @@ static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	endbyte = vbo + ret;
 
 	if (endbyte > inode->i_size) {
+		if (iocb->ki_flags & IOCB_NOWAIT) {
+			ret = -EAGAIN;
+			goto out;
+		}
 		/*
 		 * Normal files: increase file size, allocate space.
 		 * Sparse/Compressed: increase file size. No space allocated.
 		 */
+		filemap_invalidate_lock(inode->i_mapping);
 		ret = ntfs_set_size(inode, endbyte);
+		filemap_invalidate_unlock(inode->i_mapping);
 		if (ret)
 			goto out;
 	}

---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261126-fix-krn-ntfs3-inline-read-936f502cc415

Best regards,
-- 
Mostafa Amin <mostafa.amin@windriver.com>



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [syzbot ci] Re: fs/ntfs3: serialize size changes with mapping->invalidate_lock
  2026-11-26 22:12 [PATCH] fs/ntfs3: serialize size changes with mapping->invalidate_lock Mostafa Amin via B4 Relay
@ 2026-12-03  6:58 ` syzbot ci
  0 siblings, 0 replies; 2+ messages in thread
From: syzbot ci @ 2026-12-03  6:58 UTC (permalink / raw)
  To: almaz.alexandrovich, devnull, linux-fsdevel, linux-kernel,
	mostafa.amin, ntfs3, stable, syzbot
  Cc: syzbot, syzkaller-bugs

syzbot ci has tested the following series

[v1] fs/ntfs3: serialize size changes with mapping->invalidate_lock
https://lore.kernel.org/all/20261126-fix-krn-ntfs3-inline-read-v1-1-30d32e60a4f5@windriver.com
* [PATCH] fs/ntfs3: serialize size changes with mapping->invalidate_lock

and found the following issue:
WARNING in pagecache_isize_extended

Full report is available here:
https://ci.syzbot.org/series/c16040ba-3e2a-4031-91a3-340e5cd61789

***

WARNING in pagecache_isize_extended

tree:      axboe
URL:       https://kernel.googlesource.com/pub/scm/linux/kernel/git/axboe/linux.git
base:      b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
arch:      amd64
compiler:  Debian clang version 22.1.8 (1~deb13u5), Debian LLD 22.1.8
config:    https://ci.syzbot.org/builds/fe45e83c-56f0-4dc1-8f7a-8ff7cc57d73a/config
syz repro: https://ci.syzbot.org/findings/ca80e5b5-60f4-449e-97dc-a1df732fb72e/syz_repro

ntfs3(loop0): Failed to load $Extend (-22).
ntfs3(loop0): Failed to initialize $Extend.
------------[ cut here ]------------
to > inode->i_size
WARNING: mm/truncate.c:902 at pagecache_isize_extended+0x42f/0x500 mm/truncate.c:902, CPU#1: syz.0.17/5880
Modules linked in:
CPU: 1 UID: 0 PID: 5880 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) 
Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014
RIP: 0010:pagecache_isize_extended+0x42f/0x500 mm/truncate.c:902
Code: 5d 41 5e 41 5f 5d e9 20 77 fe ff e8 cb 15 b3 ff 48 83 c4 18 5b 41 5c 41 5d 41 5e 41 5f 5d c3 cc cc cc cc cc e8 b2 15 b3 ff 90 <0f> 0b 90 e9 64 fc ff ff 44 89 e1 80 e1 07 38 c1 0f 8c ff fb ff ff
RSP: 0018:ffffc9000375fad0 EFLAGS: 00010293
RAX: ffffffff82161b7e RBX: 000000000003f000 RCX: ffff8881605e5a00
RDX: 0000000000000000 RSI: 000000000007e000 RDI: 000000000008fff5
RBP: dffffc0000000000 R08: ffffc9000375fa67 R09: 1ffff920006ebf4c
R10: dffffc0000000000 R11: fffff520006ebf4d R12: 000000000000000c
R13: 000000000007e000 R14: 000000000008fff5 R15: ffff8881b8bab3c0
FS:  00007fd5818c86c0(0000) GS:ffff8882a77fc000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000001b2de63fff CR3: 000000010d756000 CR4: 00000000000006f0
Call Trace:
 <TASK>
 ntfs_setattr+0x357/0x660 fs/ntfs3/file.c:745
 notify_change+0xbba/0xea0 fs/attr.c:556
 do_truncate+0x1c2/0x250 fs/open.c:68
 vfs_truncate+0x4b7/0x540 fs/open.c:118
 ksys_truncate+0xf3/0x1c0 fs/open.c:142
 __do_sys_truncate fs/open.c:154 [inline]
 __se_sys_truncate fs/open.c:152 [inline]
 __x64_sys_truncate+0x5b/0x70 fs/open.c:152
 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
 do_syscall_64+0x171/0x540 arch/x86/entry/syscall_64.c:89
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fd5809a9b59
Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fd5818c8028 EFLAGS: 00000246 ORIG_RAX: 000000000000004c
RAX: ffffffffffffffda RBX: 00007fd580c35fa0 RCX: 00007fd5809a9b59
RDX: 0000000000000000 RSI: 000000000008fff5 RDI: 0000200000022400
RBP: 00007fd580a3a18b R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007fd580c36038 R14: 00007fd580c35fa0 R15: 00007fffd021e188
 </TASK>


***

If these findings have caused you to resend the series or submit a
separate fix, please add the following tag to your commit message:
  Tested-by: syzbot@syzkaller.appspotmail.com

---
This report is generated by a bot. It may contain errors.
syzbot ci engineers can be reached at syzkaller@googlegroups.com.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-03  6:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 22:12 [PATCH] fs/ntfs3: serialize size changes with mapping->invalidate_lock Mostafa Amin via B4 Relay
2026-12-03  6:58 ` [syzbot ci] " syzbot ci

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®