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=1796131462; b=g3MArLFuZLKeMf/LhQICuLxwL6Hg3QltAkPUzNEGuxk0HV//R5sx78A9JoYJBui1vnJL 6/TVn4IcwdbgNcjccXYw/BZFmLYxeX5kcFn0C4uPGm8Yq0T8o1n1PBqaTrnhK4zHEuJA/ BPah/KCFNIas5cYk31NS4f9DehuzZyfKzELFu6OClToxGJ2mdEchdEXlo3HGJLUifHKxJ //Fbskj9UmQ0/0HodosbVaCCAQI7+Ra9ABFmShs/EcEWa4iIdf36QHyX+moO00EsQUF10 wXrZrhTrNXkpkdLgloTQYvAogmw3VLdCXfdXRK47E2WItPAid3M6YcUxKlezIe4ynUQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796131462; 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=rrovevgIg3cicX5e5pKQQyf04rlNi4RrguzBB3oH60I=; b=wk6x+MBOthDRoFzEBA4vVNW4XBzmTudvPh+lHm4LB0IwuZbsYosVU0vk1mX7GEFI1uOz IsS2L0xKxheKoyUeIMWuh+0c/XrB9fyvh3RIUovRlrUwsaLGkDlky308K4STr12hm/KEB r7LF9zzO9gJvasWXiLT2shwN79TpsL3UrR59giCIX9GGRK+3COSaN89TMGsnzPrlIBAkF 0dpVqvj6KQV9bvHaHwrZcBcD3Lxpp2BXxo+TQKnFIaENCUMaAdDGOqa/ti84r4Sgsw/cA 267TpGUqJ5BiXbDBc9vIqfHF9vnV6y3fikUk2fXbxYWBMtrjatNe7c//nuFsdu97Dhw== 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=W8In56Zk; 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 E49D5606A31A; Tue, 1 Dec 2026 13:24:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E49D5606A31A 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=W8In56Zk 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=rrovevgIg3cicX5e5pKQQyf04rlNi4RrguzBB3oH60I=; b=W8In56ZkTdJR/WTC8PiQWpkw20zJW0mhlPukyzvN02FhMWSkmW8A//rIJqMplL 3euwCXgnP+jyZguN4TIDk2K6BUEGSqTXijuFT31rVLQ6LiqnyTFX20ZhIgI6Mq4g +F6lK2T5MoWkBP0BBmNmVUJweRgcTe6f5fl3Z4M6H+3WI= Received: from [IPV6:2409:8900:1ec4:9e84:760f:95b0:7eb1:cfb2] (unknown []) by gzga-smtp-mtada-g0-4 (Coremail) with SMTP id _____wD3n4dcyg5rPPwMMg--.60090S2; Tue, 01 Dec 2026 21:23:42 +0800 (CST) Message-ID: Date: Tue, 1 Dec 2026 21:23:39 +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 v2] 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: <20261201125126.1651071-1-zhanxusheng1024@gmail.com> Content-Language: en-US From: liubaolin In-Reply-To: <20261201125126.1651071-1-zhanxusheng1024@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-CM-TRANSID:_____wD3n4dcyg5rPPwMMg--.60090S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZw1DWr1ktr4rXw4fXFW7twb_yoWrtrWrpr ZI9Fs8GrWv934UGrna93ZYq3WFgw4fJr4UCryruw12y3yktr1vgFWUtrWrWrs2qrZYyF15 tF4v93y7C3WDX3DanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UsTmDUUUUU= X-CM-SenderInfo: xolxutxrol0iasrtmqqrwthudrp/xtbC6h4o8msOyl5zrwAA3D 在 2026/12/1 20:51, 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. 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 > --- > v2: check the file size under the invalidate lock, as a truncate > between the fault and the zeroing left the initialized size past the > end of the file (Baolin). The check is in > ntfs_extend_initialized_size(), so it also covers > ntfs_file_mmap_prepare(), which reads the file size without the lock as > well; that path is not tested. > > v1: https://lore.kernel.org/all/20261130030220.1100209-1-zhanxusheng1024@gmail.com/ > > Tested in a VM on ntfs-next 329758f9d73b, with all free space filled > with a known pattern first. The example above, bytes of old data in > [0, 64K) after remount: > > ntfs-next v2 > 4K clusters 0 0 > 64K clusters 61440 0 > > With a test-only msleep() before the zeroing, the store at 2M waiting > there and the file shrunk to 64K meanwhile, the store gets SIGBUS in > both versions. After remount, v1 leaves "initialized size 2097152, > data size 65536" and the inode cannot be read; v2 leaves 65536 for > both. > > 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. > > fs/ntfs/file.c | 15 +++++++++++++++ > fs/ntfs/inode.c | 9 +++++++-- > 2 files changed, 22 insertions(+), 2 deletions(-) > > diff --git a/fs/ntfs/file.c b/fs/ntfs/file.c > index 31ff35176e05..213a867dd2a4 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,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. > diff --git a/fs/ntfs/inode.c b/fs/ntfs/inode.c > index 3aac1d7d310f..84762691046e 100644 > --- a/fs/ntfs/inode.c > +++ b/fs/ntfs/inode.c > @@ -2647,10 +2647,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; > } Looks good to me. Reviewed-by: Baolin Liu