mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: liubaolin <liubaolin12138@163.com>
To: Zhan Xusheng <zhanxusheng1024@gmail.com>,
	linkinjeon@kernel.org, hyc.lee@gmail.com
Cc: ernest.borowski.git@proton.me, ntfs@lists.linux.dev,
	linux-kernel@vger.kernel.org, zhanxusheng@xiaomi.com,
	stable@vger.kernel.org
Subject: Re: [PATCH] ntfs: zero up to a page that a shared mapping makes writable
Date: Mon, 30 Nov 2026 17:40:58 +0800	[thread overview]
Message-ID: <a3abc675-fb6e-41c6-b1ee-da60d436751b@163.com> (raw)
In-Reply-To: <20261130030220.1100209-1-zhanxusheng1024@gmail.com>



在 2026/11/30 11:02, Zhan Xusheng 写道:
> From: Zhan Xusheng <zhanxusheng@xiaomi.com>
> 
> 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.
> 
> Fixes: b041ca562526 ("ntfs: update iomap and address space operations")
> Cc: stable@vger.kernel.org
> ---
> Tested in a VM on ntfs-next 8b42a85768e9, with all free space filled
> with a known pattern first.  The example above, bytes of old data in
> [0, 64K) after remount:
> 
>                     ntfs-next  patched
>    4K clusters      0          0
>    64K clusters     61440      0
> 
> The 4K written first and the bytes stored at 2M are kept in both.  A
> pwrite() of one byte at 2M instead of the store gives 0 with and without
> the patch.  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.
> 
> This only runs for a page past the initialized size.
> ntfs_file_mmap_prepare() already moves it to the end of the mapping, so
> that takes a file grown after mmap().
> 
>   fs/ntfs/file.c | 15 +++++++++++++++
>   1 file changed, 15 insertions(+)
> 
> diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c
> index 612498b45fbb..d0da022d0860 100644
> --- a/fs/ntfs/file.c
> +++ b/fs/ntfs/file.c
> @@ -993,6 +993,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;
>   
> @@ -1011,6 +1012,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);

Hi Xusheng,
    There is a race with truncate here.
    A fault at 2 MiB could reach page_mkwrite(), then another thread
  could shrink the file to 64 KiB before ntfs_extend_initialized_size()
  takes the invalidate lock.
    The helper doesn't check the current EOF. Zeroing beyond the
  allocation can be skipped as a hole, so it can still set
  initialized_size to 2 MiB while data_size is only 64 KiB.
iomap_page_mkwrite() checks for truncation later, but rejecting the
folio at that point won't undo the initialized_size update.
    Could we check the fault offset against the current EOF after taking
the invalidate lock, and keep the lock held through the zeroing and
initialized_size update?

Thanks,
Baolin.

> +		if (err)
> +			goto out_error;
> +	}
> +
>   	/*
>   	 * Serialize against truncate/fallocate which hold the lock
>   	 * exclusively while invalidating pagecache and changing extents.


      reply	other threads:[~2026-11-30  9:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  3:02 Zhan Xusheng
2026-11-30  9:40 ` liubaolin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a3abc675-fb6e-41c6-b1ee-da60d436751b@163.com \
    --to=liubaolin12138@163.com \
    --cc=ernest.borowski.git@proton.me \
    --cc=hyc.lee@gmail.com \
    --cc=linkinjeon@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntfs@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=zhanxusheng1024@gmail.com \
    --cc=zhanxusheng@xiaomi.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®