Re: [PATCH v2] ntfs: zero up to a page that a shared mapping makes writable

From: Namjae Jeon

Date: Thu Dec 03 2026 - 06:59:46 EST


On Tue, Dec 1, 2026 at 9:51 PM Zhan Xusheng <zhanxusheng1024@xxxxxxxxx> wrote:
>
> 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. A
> truncate can shrink the file between the fault and the zeroing, so have
> ntfs_extend_initialized_size() check the file size under that 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
> ---
> v2: check the file size under the invalidate lock, as a truncate
> between the fault and the zeroing left the initialized size past the
> end of the file (Baolin). The check is in
> ntfs_extend_initialized_size(), so it also covers
> ntfs_file_mmap_prepare(), which reads the file size without the lock as
> well; that path is not tested.
>
> v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@xxxxxxxxx/
>
> Tested in a VM on ntfs-next 329758f9d73b, with all free space filled
> with a known pattern first. The example above, bytes of old data in
> [0, 64K) after remount:
>
> ntfs-next v2
> 4K clusters 0 0
> 64K clusters 61440 0
>
> With a test-only msleep() before the zeroing, the store at 2M waiting
> there and the file shrunk to 64K meanwhile, the store gets SIGBUS in
> both versions. After remount, v1 leaves "initialized size 2097152,
> data size 65536" and the inode cannot be read; v2 leaves 65536 for
> both.
>
> Shared mappings made writable with mprotect(), the killed and stalled
> writer cases, fsx (buffered and O_DIRECT), insert/collapse range and
> the partial O_DIRECT cases give the same results with and without the
> patch. No lockdep or KASAN reports.
>
> fs/ntfs/file.c | 15 +++++++++++++++
> fs/ntfs/inode.c | 9 +++++++--
> 2 files changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
> index 31ff35176e05..213a867dd2a4 100644
> --- a/fs/ntfs/file.c
> +++ b/fs/ntfs/file.c
> @@ -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,20 @@ 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 = ntfs_extend_initialized_size(inode, pos, pos);
> + if (err)
> + goto out_error;
> + }
Can this race with an O_DIRECT read? iomap_zero_range() zeroes the gap
in the page cache. __ntfs_extend_initialized_size() then advances
initialized_size before those zeroes have been written back. The
direct read holds i_rwsem shared, and this fault uses invalidate_lock.
The read can therefore complete its kiocb_write_and_wait() check
before the gap is dirtied, then see the new initialized_size at iomap
lookup time. That can map the still unwritten gap as IOMAP_MAPPED and
return stale disk contents.

And please check Sashiko's review comments:
https://sashiko.dev/#/patchset/20261201125126.1651071-1-zhanxusheng1024%40gmail.com
Thanks.