From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796462696; b=wf1lYzIArDyPE40CMibx7XizEDbRwhpkLMbEVY+imEM/TWnvBSuFAf/7/iI3QC2iFmXD yOtETciIMa9UogvAGOM3GkxPjH3PucXbHDYu+rG2I/m7pse7F95+bZEM6u2aMMl2Il1iJ YkhPuRxAOPf7ffay+8s2EdrAzQs8itWiZE79nnJKLHiPosILv4Adu+xcdBCsuoUKvZPit WgAOgJbfxTD01cZe5ka6sp7f4MttgdmbwqFA2O8SjCA5yp1mKCYl/RtdJpA1+hi1j4Krd jh+d76B5iP2BI69qup4vQGbiDlN9u0FQZk8DRoMO4E/lYHnz7o6YD0hYUSr1dMTTHoA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796462696; 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=nMKAQWVkWyPE5xveU6LkTpQSuLOw0pFobJWO68SYa+s=; b=vCMrMNqbjYfJZbG00e6jTDE3GpfwyJHIiTaU23t72OMZ71HvHkjEohHRJKOhBZMmloJI O1raDIF0bbN2TEx3/P8MW8RDjdyh1r/b658GajLWkW8UJJdsju4gxWnSkHASB2eKUMVMG GC8NWN31WH+5/n/AvrEIzXpCRExu1Wm0fCdMZLwb+zqA9zXEQtBwsYBidnElyVMHnTWxp lH9F0KyenJ2neGY8RcruMOSib7iBBF6EtrmgZQFi+TKTSV1HMI/E8reOPKmAlZO4dE/AQ v36tNW8JplSV8V93NihUgv9vUMTEy3C4kX9M+BtqcvSDonRjR4PYUYxo04OrTpZTHJg== 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=JdqnPHQz; arc=none smtp.remote-ip=209.85.210.171 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 A06DA606A32F for ; Sat, 5 Dec 2026 09:24:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A06DA606A32F 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=JdqnPHQz Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8df56ef2a1aso307316b3a.2 for ; Sat, 05 Dec 2026 01:24:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796462695; x=1797067495; 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=nMKAQWVkWyPE5xveU6LkTpQSuLOw0pFobJWO68SYa+s=; b=JdqnPHQzTQeRekRMk8diKBPHkh3sKuWA9C77NyrnvmhS7P9tU5GM2kiMNp/0pn1ahZ XVISBjOM/IfSVPI73VObQEzw4ghwbBp2lSzMlk+nn/B+QHIjmPaaC5TBRj+0fmzxhrGZ qEcn4Bt12ELoRMYwJjcu3NjLgIGCv3STCKBvw48OTwQ/qKEaaiU2PUyze/PdS9+YQzIC /giIA5dpg8BFL8+ZmQTvn26Mf50+LfPg7LoD2Eq7sM6Da8ji7LhrcCD73MoqkcvSJDMe yfdn2FMhKPFrfWLEiCMwsFYG2GH9uAC/htWAVaGfeWmsOs3n9DNHJR1VEuRGGJzXOy1L kmwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796462695; x=1797067495; 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=nMKAQWVkWyPE5xveU6LkTpQSuLOw0pFobJWO68SYa+s=; b=xLWZE5jIO1rJcQY/MW2eQ6SJR42PCl9166/4nLNc2E92Wfq8+ZcLMxR4jJ5dYlStRX Xkb3yuxK8kz0EX348Icxzwo6XEPP6JDf+XVyvHcGUMFU8z6ftAu26q6IeM1D8N6SLV66 sn5D/R+thyUzDnnMADHdEUKaDDPGe3gyjST4vW+LxJtZ2xT0D1kGyisqowg1G6EpThWz ikB2ylMv2jSYCbUVsvFNTFlg4HAxXJbHmTGrkf62Xx0r/Vgpnz73ylGZYkKxPYOqM0vu g4dr01ZuzZ7iTvUasGQ8P1o5x2UeFeZ8vZ6sb3IThB4kiBEQwnTiamdVsOfaiiTgNTjA 2wYg== X-Forwarded-Encrypted: i=1; AL+hErHHIRwGxErn5HIAm89dN3UZchFroQYpdHFiLrFlO3CJuD4xe6NFtjuBUqK1rnF9616zLY0SGo98QEEc0VY=@vger.kernel.org X-Gm-Message-State: APuU4Z8ptfrbMtyz4KqMatveR/Zv8hPeqw9pUsOjrR4UyD+f0T566Rq8 muwgnXtCinVsFDgOqVQXIFtMeGXVUIp4+bDDLSxNs8Cjgjz0kK2czKnK X-Gm-Gg: AZFdTZet4FmlomfrODYXmQzgzigDCxFSN+Sf5x0m2jprFtRsbWyP8Y2MSO1lXnemSn3 IPf7JFMa0uHZiONltr6ZaCHqg79/R2KBVrWYOwWNAH/IWvR0PyrfiTz1z7h3U++gsqj+PIhrhwb 7l6oVRR3QKX0IyP4Rm7lvfj4kNobpwM+thgpgSA3Gp0IVfFknw4t5VQHuRArW5p37cnsWqWgzVT ln3N7hhcBXw0VVKDmuyxKS//l2z4hexkOlHLETuf8P3rqsSZtBqJTrIXeSOBfASa++pRWT1ni2Y w8N8SOiZDCyB7jMK8NdiRR3eqXTPpnEWEywc8lheK7x2UVVb8xS/Uvah+frIei3ve3PDmMXDJq7 fMD/rBrSjfN/M2kYsHgnNcbgKHdU2othyR/hDtJkME/7/wywiRvInJLwx34bhBcm/9pMe2nGHlm N6IlDMOKkmLg/+4488suRQ7sweLYJKJTId8Puaj68yJbZZ1iQA9QkEi/1/72gVdxorf2VnFzjbz lozJYXjq7lzPTyZanbuh3/UWTCS3eLt81HxSbbxRw0OWu6AW1rcE766/7aSusH6KEJnY0EQPIdB 12GJsLw85UTQjRoZxenLQ5PG34PxB/eFjSEgiG2ebEp/QA== X-Received: by 2002:a05:6a00:bd10:b0:8a3:df84:4c14 with SMTP id d2e1a72fcca58-8de2ebaf8e7mr3962943b3a.57.1796462694821; Sat, 05 Dec 2026 01:24:54 -0800 (PST) Received: from osman.mioffice.cn ([43.224.245.178]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8de12db32besm3221621b3a.34.2026.12.05.01.24.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 01:24:54 -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 v4] ntfs: zero up to a page that a shared mapping makes writable Date: Sat, 5 Dec 2026 17:24:48 +0800 Message-ID: <20261205092448.3416486-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. 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; + } } } @@ -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 -- 2.43.0