* [PATCH] ntfs: zero up to a page that a shared mapping makes writable
@ 2026-11-30 3:02 Zhan Xusheng
2026-11-30 9:40 ` liubaolin
0 siblings, 1 reply; 2+ messages in thread
From: Zhan Xusheng @ 2026-11-30 3:02 UTC (permalink / raw)
To: linkinjeon, hyc.lee
Cc: liubaolin12138, ernest.borowski.git, ntfs, linux-kernel,
zhanxusheng, stable
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);
+ if (err)
+ goto out_error;
+ }
+
/*
* Serialize against truncate/fallocate which hold the lock
* exclusively while invalidating pagecache and changing extents.
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: [PATCH] ntfs: zero up to a page that a shared mapping makes writable
2026-11-30 3:02 [PATCH] ntfs: zero up to a page that a shared mapping makes writable Zhan Xusheng
@ 2026-11-30 9:40 ` liubaolin
0 siblings, 0 replies; 2+ messages in thread
From: liubaolin @ 2026-11-30 9:40 UTC (permalink / raw)
To: Zhan Xusheng, linkinjeon, hyc.lee
Cc: ernest.borowski.git, ntfs, linux-kernel, zhanxusheng, stable
在 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.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-30 9:41 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30 3:02 [PATCH] ntfs: zero up to a page that a shared mapping makes writable Zhan Xusheng
2026-11-30 9:40 ` liubaolin
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®