From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.3 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796031697; b=P4TGFGxClMncGslIagdYBff+f8GB2On/5hLU/MpX+o2xrwYvvZKhLH20SuCa0Rmv46RZ kTullI+kHTHDN6KoUZp0cZWLXZwJNRbGWpNsI/Z2JvCOkVeqtMSCnG+6+buGRvVlr9sAf VGpAxejptg9ZRjzkT4m+1eqq/XZdMKZZDfWFlB1mmEjSVNPJ3Px5xi6h9PEgdz9GMHPw5 vY6xpms52hjQRqch+fxUayFOnIB7cTyHRz8GtjUl6lJXl3xH6CHdCu8M3pDPwTaYjghee ZiMLTdEHj+3CPqGetdv5NA9AremB4FuB1OSOc2qXtehha1/O/fsoouh8mHzkeAzrvxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796031697; 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=Dw1myTdpZytytBm/zbwdztqPi7V3A6tI4UHdblpamF8=; b=1V6gm8fryYSTplbm3yi28l4YlmXGSA3w3YkwU546tox82r6/eVL/Cb61onsTwH9aO1Lr ox0zt2/9/LFs1cO7pbGSKKpBfNBoi0fNZprSgqhGZvxIE3xWB8ks6/tUjrk5vmWz4UvCr k6zc0ZV7MFgz1oZihBAjSPVsn9wXYrDjlBWaJ7JJu51PEfMFRGtrXtuFGe3p+Jzzlt/jj 3FXQOX4ENZfwxnWhdy2mjpP2LLTOcC6V0aCVoykMPdF3fsQ3FPqTntpAWvBwkZqNk9LDb x23Ld9baTuoIEuAUZsQQclp4QohAniIePjO+mUvrlV9tTQVOXieVB4YjN1shGLiWK3g== 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=GP1ItNZj; arc=none smtp.remote-ip=117.135.210.3 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) (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 F3D2A606A31C; Mon, 30 Nov 2026 09:41:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3D2A606A31C 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=GP1ItNZj 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=Dw1myTdpZytytBm/zbwdztqPi7V3A6tI4UHdblpamF8=; b=GP1ItNZjqUmTGYyYxBAksp5kAX40aBEexnNfZWF+GIK+S1qyfp3mVCiZ5haRhX vIofpIyVS/w2yijY2W6bqx3L3lOl3Z4Ax3DqJUL1pe27n43/dux2oEmnlWg58PIv vIgdfcHqcCtp6gnil+/SnLlIDV8hAjabomtoVmP6Ky3aQ= Received: from [IPV6:2409:8900:6ae:3295:bdf5:7149:7d26:d2ca] (unknown []) by gzsmtp4 (Coremail) with SMTP id PygvCgCnn6WqRA1rFPJAJA--.35626S2; Mon, 30 Nov 2026 17:41:00 +0800 (CST) Message-ID: Date: Mon, 30 Nov 2026 17:40:58 +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] 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, zhanxusheng@xiaomi.com, stable@vger.kernel.org References: <20261130030220.1100209-1-zhanxusheng1024@gmail.com> Content-Language: en-US From: liubaolin In-Reply-To: <20261130030220.1100209-1-zhanxusheng1024@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:PygvCgCnn6WqRA1rFPJAJA--.35626S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZw1fKF17XryUZrykAF4UXFb_yoWrGrWxpr 9xtan8Cws0g3y2qwnxu3WkX3WF9a1rArWUCrW3Jw1xCwnYyrnYqFWUt3ykXF4kGrsYvF1j yF4j93y7W3Z8Z3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jeYLkUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6g1aJWsNRK3cUwAA3Y 在 2026/11/30 11:02, 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. > > 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.