From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=220.197.31.4 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796368803; b=uC63KUYdz+KudTbJcxHCJVQDV5kGa7WGJ+SY1S+I3FBBET4p9qydrlDmbiTAwhVcu7id 9TgAKnhaQEHm3yZ9eV3B0/Byg0hFZfNL0w3VtWZ0pHK6n9hHRnjQqlsRosUvhoEXlHysv RlKuHzilwGVN8fd+IbsNozeT6/nR324MwGFpoPK87njijtnVFpBxSbq56iAPBrRjN+I8X Fzu5x1Y6b074cHjhAcp/4MkoXB9w99/xGaQYUr8JGYHNRU6JjePOlePJe5Xkc8PtVnqAp VIiHVXCGPLZYCX2TFzgXPbbKTIhY1YgL1vHviID11Ff5bIWrj3+18O8SSDMvdrfnGFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796368803; h=DMARC-Filter:DKIM-Signature:Received: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-Originating-IP:X-CM-SenderInfo; bh=QH/5I4ZpoB6pBhN6zPqvMropJCwGqLe3LbgZK3SzStM=; b=Joibr6LpkrFTkiYDW55qzY3joETz6rt5NwPetavkkOPLryttctypFJfy4O7dhOnCyu+/ yfjUs2/xd3Nvf5L/U0GtWbo8JVwh0zfDl5Veg/zjtTu1f59VHnCU9lBXicpLZWBse0jq5 eGSyiKm1hqOR4iyR2z8CyA7lm5kDxyGuiFR+W0rn4wCjI6pKOx/i1g6YAt52bjEiSib4E xAE7XIGtHcfSTP3mCrWWQfzl4EnWdSD1dmTxvQtkafGlryDAJrJlb/Gy/ICxp6TNZfIi3 am5y1hPPVLhDY4pacVS2XNlvZ9F9Z03IDGCSQ/JqsFjt0Msadug2Zkg8zmmyIOWQwuQ== 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=ivdd6OYd; arc=none smtp.remote-ip=220.197.31.4 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.4]) (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 04E73606A322; Fri, 4 Dec 2026 07:20:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 04E73606A322 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=ivdd6OYd 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=QH/5I4ZpoB6pBhN6zPqvMropJCwGqLe3LbgZK3SzStM=; b=ivdd6OYdjAPfladJb7CrBQ6xu1vx/soiXa0sEPEDMleE/2TSe7o4eOnvRdbC8F NCTb7Cz+Usk/w4SaPjd8sTuwfRul/FsEuO8BhEzxt8c8Oe8LdW9g9W7jgv0XysPa oXmnC/YxMsBdwXslZ3KSoCgxgIyDeI51TFwPQ5Czfj4u0= Received: from [IPV6:2409:8900:1fc7:c434:7504:5cc7:aed1:e845] (unknown []) by gzga-smtp-mtada-g0-2 (Coremail) with SMTP id _____wCXcyB3aRJrR2BdMQ--.56383S2; Fri, 04 Dec 2026 15:19:21 +0800 (CST) Message-ID: Date: Fri, 4 Dec 2026 15:19:19 +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 v3] 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: <20261204035543.2835520-1-zhanxusheng1024@gmail.com> Content-Language: en-US From: liubaolin In-Reply-To: <20261204035543.2835520-1-zhanxusheng1024@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wCXcyB3aRJrR2BdMQ--.56383S2 X-Coremail-Antispam: 1Uf129KBjvJXoW3Jw13CFW5KFWxZryDJFWDJwb_yoWxJr1Upr Zag3ZxGrZ2q3yUCwnIg3ZYqF1F9w1fGF4UCry5C3W7ZwnYkrnYgFWDKFs5Wr4ktrsYv3WU tF40v3y7GF1UZrJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07U3xhJUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6RvXomsSaXveZQAA3X 在 2026/12/4 11:55, 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, and > write the zeroes back before the initialized size covers them, or an > O_DIRECT read that has already flushed the page cache reads the gap > from disk. 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@vger.kernel.org > Signed-off-by: Zhan Xusheng > --- > v3: > - Write the zeroes back before the initialized size covers them; a > direct read that flushed the page cache before the zeroing read the > gap from disk (Namjae). > - Fail the fault on a shut-down volume before zeroing (Sashiko). > - Not changed: truncate_pagecache_range() in > __ntfs_extend_initialized_size() drops the faulting folio, so the > fault is retried once (Sashiko). The retry reads the page again and > finds the initialized size at the page, so it does not extend again. > - Not changed either: a direct read of the faulting page itself can > still see disk contents, as iomap_page_mkwrite() moves the > initialized size to the end of the page in ->iomap_begin() before it > dirties the folio. This is the same without the patch. > - Baolin's Reviewed-by on v2 is dropped, as the code changed. > > 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 660cb7022966 with 64K clusters, all free > space filled with a known pattern first. Bytes of old data read: > > ntfs-next v3 > the example above, [0, 64K) after remount 61440 0 > direct read of [4K, 64K) racing the store 61440 0 > direct read of [2M, 2M + 4K) racing it 4096 4096 > > The races use a test-only 300ms sleep in ->iomap_begin() of the direct > read, 3 runs each; v2 reads the same 61440 bytes as ntfs-next from > [4K, 64K). 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 | 17 +++++++++++++++++ > fs/ntfs/inode.c | 19 +++++++++++++++++-- > 2 files changed, 34 insertions(+), 2 deletions(-) > > diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c > index 31ff35176e05..1289fc9a18e2 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,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); > + if (err) > + goto out_error; > + } > + > /* > * Serialize against truncate/fallocate which hold the lock > * exclusively while invalidating pagecache and changing extents. > diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c > index 3aac1d7d310f..209f340f6dfb 100644 > --- a/fs/ntfs/inode.c > +++ b/fs/ntfs/inode.c > @@ -2634,6 +2634,16 @@ static int __ntfs_extend_initialized_size(struct inode *vi, > &ntfs_iomap_folio_ops, NULL); > if (err) > goto out; > + /* > + * A direct read that has already flushed the page cache > + * would read the gap from disk once the initialized size > + * covers it: write the zeroes back first. > + */ > + err = filemap_write_and_wait_range(vi->i_mapping, > + old_init_size, > + offset - 1); > + if (err) > + goto out; Hi, Thanks for the update. This looks like it addresses the gap race discussed on v2 when writeback succeeds. Thanks, Baolin. > } > } > > @@ -2647,10 +2657,15 @@ 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) > { > - 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); > filemap_invalidate_unlock(vi->i_mapping); > return err; > } > > base-commit: 660cb70229665c241b4ea5537491d7cd01c25ad8