From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.5 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796517476; b=psi7CLapWs1SfBXuEUBiyaPxOpvFVaKBGmZrauuMJesxJu98+Rb4Cl1J3VR+v1TVE1A2 rnkhpSCjaVIyeN2sSM7fKfkMedhwfz3H4+QxoCT/1hqEK+BKtNZaFZsABdRyqdQPF7C64 M+kIBX2Hdeh6C96XTZnAXMX1w/JgFClZvvSYaAfdOkbhzCPr8JP4iAm5eRU0MhT4HA27f hO4aTuP+VsvZFY9hVWkYlAIPeA3ZsJc0EDrwoXdANZwPTYhep3VEOsj6pRXwDlOFBwUHT OEGVHnB763c8OT9u8KXxc8KxjiNfWOkC5OtQs0k16mnf15dmZfJL8fJwugox0Nr2iuA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796517476; h=DMARC-Filter:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding:X-CM-TRANSID: X-Coremail-Antispam:X-CM-SenderInfo; bh=J/i+mzjUhqk2pCZKUZG8M0ScnIwQQ1nIRqpoYakmSZY=; b=xfQ8/tDv1O0JaYJurRsXbeWsyUiyAKd2fMMMWa1PcYjvnM78eil5drW5zITRfFl2S9JW dquldACx132FdothfIbBN7uWuRkPGIyBS4uvso0SSF8cunjM+rFDz34Brd0VHjXOT/Th2 owEmtGsUHGD2JCwK4ijFFpCA639mbtXwUS54AqTde/4HqQpW+LrO+NDQuZKilEuGB+hQf kW/F6T2b44fqwCdlwF1tXU/C24TJI11qx0rLIFNqS6+areD5ndfimt5OLNQ9BRfy0z6Lh XPk40mPK6EWl9X7oo0Nj8Z6D7C/zS8bqLBuHg2LGmOiKeLYcuaXPnFX9Xi0X2lBGrig== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=lP1cIOQI; arc=none smtp.remote-ip=220.197.31.5 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BE64A606A31F; Sun, 6 Dec 2026 00:37:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BE64A606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=lP1cIOQI DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Message-ID:Date:MIME-Version:Subject:To:From: Content-Type; bh=J/i+mzjUhqk2pCZKUZG8M0ScnIwQQ1nIRqpoYakmSZY=; b=lP1cIOQI1OqPTQBsPRinkKQzr59YSyIfQxoihP+ldTsEne9p8z4Qpp5q3H74/F d3mmbV9sYSI0kAhPNYKj3XR1aeOlFm+lhM2nQOeLqiDqj9YFrncH81M3RyGjOrSI lX0i/d46ZgSp4EvUSh6sDMiwa4Z1TDQrm2qldqveer3r4= Message-ID: Date: Sun, 6 Dec 2026 08:37:26 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4] ntfs: zero up to a page that a shared mapping makes writable To: Zhan Xusheng , linkinjeon@kernel.org, hyc.lee@gmail.com Cc: ernest.borowski.git@proton.me, ntfs@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org, zhanxusheng@xiaomi.com References: <20261205092448.3416486-1-zhanxusheng1024@gmail.com> Content-Language: en-US From: liubaolin In-Reply-To: <20261205092448.3416486-1-zhanxusheng1024@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wA3FRFGrhRre7CnMg--.53691S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3ZF13JFWUCryDtF4kXrWxtFb_yoWDAFyrpr ZFgFn8GrZag34jkwnaq3WkXa1Fgw18JF4UCryrCw129393Krn0gFWUtrWrWFZ7trZayw1Y qF4q93yUGr4UZrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UsJ5wUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6gmdaGsUrkmBdAAA37 在 2026/12/5 17:24, Zhan Xusheng 写道: > From: Zhan Xusheng > > 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 > --- > 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; > + } 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//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