From: Zhan Xusheng <zhanxusheng1024@gmail.com>
To: linkinjeon@kernel.org, hyc.lee@gmail.com
Cc: liubaolin12138@163.com, ernest.borowski.git@proton.me,
ntfs@lists.linux.dev, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, zhanxusheng@xiaomi.com
Subject: [PATCH v4] ntfs: zero up to a page that a shared mapping makes writable
Date: Sat, 5 Dec 2026 17:24:48 +0800 [thread overview]
Message-ID: <20261205092448.3416486-1-zhanxusheng1024@gmail.com> (raw)
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. 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@vger.kernel.org
Signed-off-by: Zhan Xusheng <zhanxusheng@xiaomi.com>
---
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@gmail.com/
v2: https://lore.kernel.org/all/20261201125126.1651071-1-zhanxusheng1024@gmail.com/
v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@gmail.com/
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;
+ }
}
}
@@ -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
--
2.43.0
next reply other threads:[~2026-12-05 9:24 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-05 9:24 Zhan Xusheng [this message]
2026-12-06 0:37 ` liubaolin
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=20261205092448.3416486-1-zhanxusheng1024@gmail.com \
--to=zhanxusheng1024@gmail.com \
--cc=ernest.borowski.git@proton.me \
--cc=hyc.lee@gmail.com \
--cc=linkinjeon@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liubaolin12138@163.com \
--cc=ntfs@lists.linux.dev \
--cc=stable@vger.kernel.org \
--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®