From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.177 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796356550; b=RlCYB0oE1hBO3gdjHPHn3/rrgPEuG0MU1y+4voOuUqfosZTvoP5dRyjdgkp74RNWM5PP OSGiTiO0t0GwYch0IX0wkfm4a3BCD0HAE5EIE1j9IYO154AA7YYXmzFJ3I6BVMETtwa4S K6kyKwalT26RQuZrV9uTYzjlpmEEvd7v3+/7TWN7f4J51Ta5Wq4Co1XLr1s8RDOGGFPE1 I4AOq7N+Orh4pmWiX8CPAa5rpc/R3HUu9qzenLFZro+ocOhpIGBaSlYA8IcOsuj4eoToZ 19Br4gX871mPtF+F9Mvrs4gD8oNVtu5eKzWZyMt1H/Te5C669kEDWrwRd9qptMlbNuw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796356550; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=O509MKzUZJi1KPVIG1saqF7zvFf2PHkQGRCkdHSQWGk=; b=2IcA31kCfHV+xUYtRRwIWffPlzKiv/3lU5QJfiEErj5cmok1yVvMEPk0KuV1MUMKKj5n uof8TPsGFO4NF4XfWbBer4go221OjeZRR+mNVtrAuKVRJa30pcE2h+VYcEf8tnlF4qJ7L k06rgCBgg7xGu1fSWBJzZ5tUaHUU5nHfW86jZuD92DhOogzfFNDbtpAx/s5il8fvWADcp N+k7Aj5ObrvCNzsdvnvii9rwt78qNjA4R2xlL9m+dbJPDhD1cTZiFCeUbNXWmcj3RloLy tmq2iVXcrXRJsFzm+FQbydTGDXsBYHK8BVaLA4aOdCjGHVmyE6u/JwVzERNBvxf2tCA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b="s3lkh/MQ"; arc=none smtp.remote-ip=209.85.210.177 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.177]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E0FE7606A32C for ; Fri, 4 Dec 2026 03:55:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E0FE7606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=s3lkh/MQ Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-8a05b210703so280586b3a.0 for ; Thu, 03 Dec 2026 19:55:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796356549; x=1796961349; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=O509MKzUZJi1KPVIG1saqF7zvFf2PHkQGRCkdHSQWGk=; b=s3lkh/MQi9WOXqhltyRyM+EFnW8cJqSP/WauOHrkVvEwppmmfDMKJMruIPWineMbQk vXosYLQ3wRjz6Mq0B2wSJ8zA4hKbQ/CrPTIA+nCgpKmnw33kbxLIjtEkst85rWQEDfUT nfzJAojLBotsbv5222iyb86L6aNVmRIFv9TZtZEISsOsvcJDMxhFkj7Y+9TTRRXolHsX nB8N+0yPdpJ+YYkqVtz222e2ijSu+VRJnDLPnN2aCRlbLegycScrtJ7GrKuqSLtzJbjP 74NM5sS097hpxmuJ8WHXedwqgyoGprO9xobLJUhvaDf+x25KMWrneRYjTOWp/gbzuMpJ ozvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796356549; x=1796961349; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=O509MKzUZJi1KPVIG1saqF7zvFf2PHkQGRCkdHSQWGk=; b=CSSGtRVL4ezSANjjngGO0FIRo9X2Hbz2jhAGPVvFpUe/vrEtW88P2Eqz8G+WdKHQmQ CkD0tQ/PYaAZOlz/GGBhmjaK44I7Nr5TMHUzERTxXd+3LVUrA76tqge4C7JXxfjh16aR 6wAUo5fQKxVNvJ+VcfX+2C2Sod2dveuwFxqwGVrDkOswnYZ0zUzctx0VdfK/RiFTxKpj k/tQ/1Tn/1vS/VHwHXfBqyjDl9gA20cz7KSSBmZ6K1HxyuibQFzkaOD3qje6RhlWe0ih odTwdp0abLnj1Ykq5nKhvS4IjTN8GokzCuUfjpPC51G8mUIpI6Kn9Xa3L+K2QxcXSkB7 JRpg== X-Forwarded-Encrypted: i=1; AL+hErG7QCmiT7fpsVvPsUv0MjYDZsbo5nHwloPVmkmWGmsTe1lNLrXOO4r6ujl6SM55Um89z/FY4eGnsK4ibDk=@vger.kernel.org X-Gm-Message-State: APuU4Z9JgvMbBnNb/abTjPk2gK2xldiC3CEWNY/Ty039NnxjKIqsN2eU EoX2L5EXNiZQYHvpET+c6r6orsUcWxzZxDQa7ce2YTEDsMQCW9rhctOkenpEUbeX X-Gm-Gg: AZFdTZdBNQgrs5yY2lvpMujpoUc6i9N160h8pwjR1AYdHyVnG0LrZVQiVUjlHv0Qxe2 THc97Y44K4o3Di55RvxTW7YdJ7ZRk02QxdeiqjvLikVsV/JndJof6J5rdQlCA8mF+DSO3yVQX/C LyywGlfT7YuB2cUeWhKeYklLIIb1V9nfYUk+UPWiNWPRyTxvJiZzq8jI/ZLazojHlllwDydMhQa yACbLomoAd6uqUoUzMeYyVi5d7KghZ/GLwvrWgPhG2QEKHCMnGNSEX+TH4JbC+JgfhhYtYHG22F g5MsTm6Dcn1ZXnkpTaZnjwkKKCJotGj40mwO5l66Jzw+perDFsnRcIZCZ4VHyFlHo77WluGXScx 0Fg+TN6czHPyt0kRO9FzVTrQkf+zm8lFW9KgMxX2pCB5Hl6CSIK8RfgpBdVTBkIzELCeBGVsMj5 yrjs7JQSNEyM/ea6bD0Syy7W1cvToAw/edXFid9ko2TJ/OPMKmQBBRYSp++DzeuJAGeUdmOhLYm e+mnmEnAFr4SmmQYwk8ed235KzJFv5WNVzJGCu3NQxdiSXzTsFxx1/4x6iiy6cI9k8SOSbI0CLW Xmt6FzOfmsdzOLRzZm8o4mN7UhLFGOJ+SHCAmr/Z/cVBfA== X-Received: by 2002:a17:90b:1642:b0:3b4:1a5f:8664 with SMTP id 98e67ed59e1d1-3b5e06913demr646253a91.65.1796356549024; Thu, 03 Dec 2026 19:55:49 -0800 (PST) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5ddc7157dsm1598648a91.15.2026.12.03.19.55.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 19:55:48 -0800 (PST) From: Zhan Xusheng 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 v3] ntfs: zero up to a page that a shared mapping makes writable Date: Fri, 4 Dec 2026 11:55:43 +0800 Message-ID: <20261204035543.2835520-1-zhanxusheng1024@gmail.com> X-Mailer: git-send-email 2.43.0 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 Content-Transfer-Encoding: 8bit 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; } } @@ -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 -- 2.43.0