Re: [PATCH v4] ntfs: zero up to a page that a shared mapping makes writable
From: liubaolin
Date: Sat Dec 05 2026 - 19:38:04 EST
在 2026/12/5 17:24, Zhan Xusheng 写道:
From: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
A page fault that makes a page of a shared mapping writable moves the
initialized size to the end of that page. If the page starts past the
initialized size, nothing zeroes the range in between, and the
allocated tail of the cluster holding the old initialized size becomes
readable with whatever the disk held before. This needs clusters larger
than a page.
For example, with 64K clusters, write 4K to a new file, map it 4M
MAP_SHARED, grow it to 4M with ftruncate() and store 8 bytes at 2M
through the mapping. After msync() and a remount, [4K, 64K) reads back
the old contents of the cluster.
Zero the gap with ntfs_extend_initialized_size() before the fault takes
the invalidate lock, as a write past the initialized size does. Unlike
a write, the fault does not hold i_rwsem, so an O_DIRECT read that has
already flushed the page cache would read the gap from disk once the
initialized size covers it: have the fault write the zeroes back first.
mmap() of a shared writable range extends the initialized size without
i_rwsem as well, so do the same there.
A truncate can shrink the file between the fault and the zeroing, so
have ntfs_extend_initialized_size() check the file size under the
invalidate lock, or the initialized size ends up past the end of the
file.
Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Zhan Xusheng <zhanxusheng@xxxxxxxxxx>
---
v4:
- Write the zeroes back only from the fault and mmap(). Writes hold
i_rwsem exclusively, which already keeps direct reads out, and with
v3, 200 one-byte writes 256K apart each waited for a write of zeroes:
199 write I/Os where ntfs-next does none (Sashiko).
- mmap() had the same race with a direct read, also without this
patch; it now writes the zeroes back too.
- If the write-back fails, the initialized size stays below the gap
and the fault gets SIGBUS (Baolin).
- Not changed: the unlocked read of the initialized size can tear on
32-bit (Sashiko). ntfs_attr_set_initialized_size() stores it without
size_lock, so the other unlocked readers have the same problem; that
wants its own change.
- Not changed: dropping the faulting folio (Sashiko), as explained for
v3. It only happens to a fault past the initialized size; faults in
order find the initialized size at their page.
v3: https://lore.kernel.org/all/20261204035543.2835520-1-zhanxusheng1024@xxxxxxxxx/
v2: https://lore.kernel.org/all/20261201125126.1651071-1-zhanxusheng1024@xxxxxxxxx/
v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@xxxxxxxxx/
Tested in a VM on ntfs-next 94ebffe9b5d7 with 64K clusters, all free
space filled with a known pattern first. Bytes of old data read:
ntfs-next v4
the example above, [0, 64K) after remount 61440 0
direct read of [4K, 64K) racing the store 61440 0
direct read of [4K, 64K) racing mmap() 61440 0
direct read of [2M, 2M + 4K) racing the 4096 4096
store (see v3 notes)
The races use a test-only 300ms sleep in ->iomap_begin() of the direct
read, 3 runs each. With 4K clusters the example reads 0 in both. The
truncate race of v2 still leaves 65536 for both sizes. mmap, fsx
(buffered and O_DIRECT), insert/collapse range, aio, partial O_DIRECT
and the short write cases give the same results with and without the
patch. No lockdep or KASAN reports.
fs/ntfs/file.c | 21 +++++++++++++++++++--
fs/ntfs/inode.c | 29 ++++++++++++++++++++++++-----
fs/ntfs/inode.h | 2 +-
3 files changed, 44 insertions(+), 8 deletions(-)
diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
index 31ff35176e05..4183e8cbf543 100644
--- a/fs/ntfs/file.c
+++ b/fs/ntfs/file.c
@@ -1135,7 +1135,7 @@ ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
if (NInoNonResident(ni) && !NInoCompressed(ni) &&
end > old_init_size) {
- ret = ntfs_extend_initialized_size(vi, pos, end);
+ ret = ntfs_extend_initialized_size(vi, pos, end, false);
if (ret < 0)
goto out;
}
@@ -1177,6 +1177,7 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
struct inode *inode = file_inode(vmf->vma->vm_file);
struct address_space *mapping = inode->i_mapping;
bool stream = ntfs_inode_is_named_stream(NTFS_I(inode));
+ loff_t pos;
int err;
vm_fault_t ret;
@@ -1195,6 +1196,22 @@ static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
file_update_time(vmf->vma->vm_file);
}
+ /*
+ * Making the page writable moves the initialized size up to its end.
+ * If the page starts past the initialized size, zero what lies between
+ * first, as a write there would, or the tail of the last initialized
+ * cluster becomes readable with whatever the disk held.
+ */
+ pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
+ if (NInoNonResident(NTFS_I(inode)) &&
+ pos > NTFS_I(inode)->initialized_size) {
+ err = -EIO;
+ if (!NVolShutdown(NTFS_I(inode)->vol))
+ err = ntfs_extend_initialized_size(inode, pos, pos, true);
+ if (err)
+ goto out_error;
+ }
+
/*
* Serialize against truncate/fallocate which hold the lock
* exclusively while invalidating pagecache and changing extents.
@@ -1237,7 +1254,7 @@ int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
from + desc->end - desc->start);
if (NTFS_I(inode)->initialized_size < to) {
- err = ntfs_extend_initialized_size(inode, to, to);
+ err = ntfs_extend_initialized_size(inode, to, to, true);
if (err)
return err;
}
diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c
index 3aac1d7d310f..4a99d1c56168 100644
--- a/fs/ntfs/inode.c
+++ b/fs/ntfs/inode.c
@@ -2595,7 +2595,8 @@ int ntfs_show_options(struct seq_file *sf, struct dentry *root)
*/
static int __ntfs_extend_initialized_size(struct inode *vi,
const loff_t offset,
- const loff_t new_size)
+ const loff_t new_size,
+ bool flush)
{
struct ntfs_inode *ni = NTFS_I(vi);
struct ntfs_inode *mrec_ni = ntfs_base_inode(ni);
@@ -2634,6 +2635,18 @@ static int __ntfs_extend_initialized_size(struct inode *vi,
&ntfs_iomap_folio_ops, NULL);
if (err)
goto out;
+ /*
+ * A direct read that is not excluded by i_rwsem and has
+ * already flushed the page cache would read the gap from
+ * disk once the initialized size covers it.
+ */
+ if (flush) {
+ err = filemap_write_and_wait_range(vi->i_mapping,
+ old_init_size,
+ offset - 1);
+ if (err)
+ goto out;
+ }
Hi Xusheng,
I found a problem here and reproduced it locally with the v4 patch
applied: a direct read consumes the writeback error before we see it in
this call.
filemap_write_and_wait_range() checks AS_EIO with filemap_check_errors(),
which clears the flag. A blocking direct read uses the same function through kiocb_write_and_wait(), and page_mkwrite doesn't hold i_rwsem to
keep it out.
When the zero write fails and a direct reader checks and clears
AS_EIO first, the reader gets -EIO, this call returns 0, and we advance
initialized_size anyway. iomap doesn't redirty the folio, so the old
data in the gap stays on disk and can be read back.
I reproduced it in a VM with 64K clusters, with all free space filled
with a known pattern first. The file was written with 4K and mmapped
MAP_SHARED for 48 MiB, then grown to 48 MiB over allocated clusters
(fallocate KEEP_SIZE, then ftruncate), so the fault has a large gap to
zero. One thread stored at 32 MiB. While the fault was zeroing
[4K, 32M), a second thread kept doing O_DIRECT pread() of the first 64K
with /proc/thread-self/fail-nth set to 1 each time, and the disk
flagged with /sys/block/<dev>/make-it-fail. The flush of the zeroed
folios is then the write that fails.
The store went through instead of getting SIGBUS. fiemap showed
[4K, 32M) as data, and the [4K, 64K) part of a direct read of the
first 64K was 61440 bytes of the old pattern. A buffered read after a
remount saw them too. It hit 12 out of 12 times. The error is gone
long before the fault reaches this call.
This is not the test itself going wrong: with the same failed write
seen by the faulting thread, the fault gets SIGBUS and initialized_size
stays at 4K.
Could you take a look when you get a chance?
Looking forward to your feedback.
Thanks,
Baolin.
}
}
@@ -2645,12 +2658,18 @@ static int __ntfs_extend_initialized_size(struct inode *vi,
}
int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
- const loff_t new_size)
+ const loff_t new_size, bool flush)
{
- int err;
+ int err = 0;
filemap_invalidate_lock(vi->i_mapping);
- err = __ntfs_extend_initialized_size(vi, offset, new_size);
+ /*
+ * A page fault or mmap() looks at the file size without the lock, so
+ * a truncate may have shrunk the file since: don't initialize past it.
+ */
+ if (new_size <= i_size_read(vi))
+ err = __ntfs_extend_initialized_size(vi, offset, new_size,
+ flush);
filemap_invalidate_unlock(vi->i_mapping);
return err;
}
@@ -4176,7 +4195,7 @@ s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, bool s
* invalidate lock.
*/
ret = __ntfs_extend_initialized_size(vi, pos + count,
- pos + count);
+ pos + count, false);
modified |= ni->initialized_size != old_init_size;
if (!ret)
ret = ntfs_attr_fallocate(ni, pos, count, true);
diff --git a/fs/ntfs/inode.h b/fs/ntfs/inode.h
index 8c1929bebe44..622c89a021ee 100644
--- a/fs/ntfs/inode.h
+++ b/fs/ntfs/inode.h
@@ -381,7 +381,7 @@ static inline void ntfs_commit_inode(struct inode *vi)
int ntfs_inode_sync_filename(struct ntfs_inode *ni);
int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset,
- const loff_t new_size);
+ const loff_t new_size, bool flush);
void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev);
struct folio *ntfs_get_locked_folio(struct address_space *mapping,
pgoff_t index, pgoff_t end_index, struct file_ra_state *ra);
base-commit: 94ebffe9b5d71d008bff5ff3c202d4980a6f2a55