From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796491228; b=s+PRuMbOS2O6JP4G6qBulpsVOStpr7sIjGv3gBL37avUNTEykWKfw4I0Ern8Yx5gl2y0 F0EgSIeHBavB2rnGym6d3H+L3DqLQax6IOeRfFJ/k1J7biYe2Zkq952SOGagbabK99Zz1 yfdivplBQWn1+ZCIdheR6Am33D3mhrZ1Pu8z+vEHk+IVogs1Lld7Orw6F3fSAVKJAWrlg iWm4ihEzbZ/YbGQs1goERgwwf8dY5XOvSxppu3UfA+qsHsin45Yymmh7xO6gJNS0iuuLR ZGznJfWJe+ScN2BeQFRaOjUImKxXV399niQs0Wlc9TPaz8CG7O7h7a3kCPxQr19eNmg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796491228; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=hhBrB5p26Lxqrjugkyhhmp9PtwN2nLWE/CVC/qMtqgY=; b=azS3nh0kFi4YXmQzDeUhn0WR57FPACfPiEwROUs0RcILp2o6beFhrVYFhrYdkrGMKq5p tE8xM9igUEfZAnZibHupdgoTthLlgBGrdrnrwZ9aapzUo5RcEYi3TM977o1uv6HTr90hH NgCg5Y4NlZF/dxfWewSrBFaZeyKJHfMVDXXKHl35UCB/PUSuPfcL3z42jrRpeLXYMllhF b95EKjVOvd89JtI3GwwAmayiV/ID8PZesaXeWhw9KEUilmNtoRM/NJTB3A5c8R+H6Xe1I 1Sl3LW1Q6cj1zmIfusuaP10oD3mF8BMrajsuuiWx+HeaJQnv2RHDlCzuN4xwe8gx2fg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=Ae32cq5u; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B1BC606A31F for ; Sat, 5 Dec 2026 17:20:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=Ae32cq5u Received: by smtp.kernel.org (Postfix) with ESMTPSA id DBD651F0092B; Sat, 5 Dec 2026 17:20:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796491228; bh=hhBrB5p26Lxqrjugkyhhmp9PtwN2nLWE/CVC/qMtqgY=; h=Date:To:From:In-Reply-To:References:Subject; b=Ae32cq5u3vOklQMUFrS7vdJZ0MW9Qnq+n7BERog4mnx9+pnv0c+P2+HciF+F16vDF zynpRL2Tu1SNA7sAQ8ES2MFEvGEU/HUQvfiK2bPLvXSJwJgOsEUDK3pdD6fBY7PvJy DYrYtxDqs7N1Lk0TN58QKkuCDf3WWmUCcTd3Nek0= Date: Sat, 05 Dec 2026 09:20:27 -0800 To: mm-commits@vger.kernel.org,mark@fasheh.com,jlbec@evilplan.org, heming.zhao@suse.com,hch@lst.de,joseph.qi@linux.alibaba.com, akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <20261205143959.704982-11-joseph.qi@linux.alibaba.com> References: <20261205143959.704982-11-joseph.qi@linux.alibaba.com> Subject: + ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch added to mm-nonmm-unstable branch Message-Id: <20261205172027.DBD651F0092B@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list The patch titled Subject: ocfs2: make buffered writes wait for in-flight direct writes has been added to the -mm mm-nonmm-unstable branch. Its filename is ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch This patch will later appear in the mm-nonmm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there most days ------------------------------------------------------ From: Joseph Qi Subject: ocfs2: make buffered writes wait for in-flight direct writes Date: Sat, 05 Dec 2026 22:39:59 +0800 ocfs2_unwritten_check() lets a buffered write skip zeroing and unwritten conversion for a cluster that an in-flight direct write has put on ip_unwritten_list, on the assumption that the dio will convert it. The dio only converts the range it actually completed, and converts nothing at all when it fails, so a dio which ends short or errors leaves the cluster unwritten while the buffered data sits in a dirty folio. Once that folio is written back and evicted the data reads back as zeroes. Wait for foreign entries on ip_unwritten_wq before taking any lock in ocfs2_write_begin(), the same way ocfs2_dio_wr_map_blocks() already does. The wait must be outside ip_alloc_sem and the inode cluster lock because ocfs2_dio_end_io_write() takes both to convert. Every caller holds i_rwsem and entries are only added under it, so the set can only shrink and no retry loop is needed. The folio is not locked yet at this point, so there is no ordering conflict with ocfs2_grab_folios_direct(). The same race exists in the gap between i_size and an extending write: ocfs2_zero_extend() skips unwritten clusters, so an in-flight dio there could later convert a cluster whose tail past i_size was never zeroed, exposing stale blocks once this write extends i_size past it. ocfs2_dio_zero_eof() therefore waits for foreign entries in [i_size, pos) before taking the inode lock, and ocfs2_wait_foreign_unwritten() widens cstart down to i_size so an extending buffered write waits on the cluster containing i_size too. page_mkwrite is deliberately left alone. It holds no i_rwsem, so entries can appear between the wait and taking ip_alloc_sem, and it holds the inode cluster lock and ip_alloc_sem that ocfs2_dio_end_io_write() needs to convert, so a wait there would deadlock against the completion of any in-flight dio on the range. It is also reachable through get_user_pages() from bio_iov_iter_get_pages() when a direct read's destination buffer is an mmap of the same file. The mmap window therefore remains open. Link: https://patch.msgid.link/20261205143959.704982-11-joseph.qi@linux.alibaba.com Fixes: 4506cfb6f8ca ("ocfs2: record UNWRITTEN extents when populate write desc") Signed-off-by: Joseph Qi Signed-off-by: Andrew Morton Cc: Heming Zhao Cc: Christoph Hellwig Cc: Mark Fasheh Cc: Joel Becker fs/ocfs2/aops.c | 79 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) --- a/fs/ocfs2/aops.c~ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes +++ a/fs/ocfs2/aops.c @@ -1808,12 +1808,14 @@ static bool ocfs2_dio_foreign_unwritten( * write might get shortened afterwards. * * TODO: - * Buffered writes or mmap page_mkwrite faults clear c_needs_zero and - * c_clear_unwritten, relying on the in-flight direct write to zero the cluster - * and convert the extent. If the direct write fails after - * ocfs2_dio_wr_map_blocks() drops ip_alloc_sem and ocfs2_inode_lock() before - * user pages are pinned or bios are submitted or rw_lock, they will lose data - * or expose uninitialized disk blocks. + * mmap page_mkwrite faults clear c_needs_zero and c_clear_unwritten, relying + * on the in-flight direct write to zero the cluster and convert the extent. + * If the direct write fails after ocfs2_dio_wr_map_blocks() drops ip_alloc_sem + * and ocfs2_inode_lock() before user pages are pinned or bios are submitted or + * rw_lock, they will lose data or expose uninitialized disk blocks. Buffered + * writes wait for the dio first, see ocfs2_wait_foreign_unwritten(), but + * page_mkwrite holds the inode cluster lock and ip_alloc_sem the conversion + * needs, so it cannot wait there. */ static void ocfs2_unwritten_check_locked(struct inode *inode, struct ocfs2_write_ctxt *wc, @@ -2628,6 +2630,39 @@ out: return ret; } +/* + * Wait for direct writes still in flight on [pos, pos + len) to finish. + * A buffered write must not defer its zeroing to a dio which may never + * convert the cluster: if that dio ends short or fails, the cluster stays + * unwritten on disk and the buffered data reads back as zeroes. + * + * Must be called without ip_alloc_sem or the inode cluster lock held, + * because ocfs2_dio_end_io_write() takes both to convert. i_rwsem is held + * by every caller and all entries are added under it, so the set can only + * shrink while we wait and no retry is needed. + */ +static int ocfs2_wait_foreign_unwritten(struct inode *inode, loff_t pos, + unsigned int len) +{ + struct ocfs2_inode_info *oi = OCFS2_I(inode); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); + /* + * An extending write zeroes the gap from i_size, so wait on the + * cluster containing i_size too: a dio there may still convert it + * after ocfs2_zero_extend() skipped it as unwritten, exposing the + * unzeroed tail below the new i_size. + */ + u32 cstart = min_t(loff_t, pos, i_size_read(inode)) >> + osb->s_clustersize_bits; + u32 cend = ocfs2_clusters_for_bytes(inode->i_sb, pos + len); + + if (!ocfs2_dio_foreign_unwritten(oi, cstart, cend, NULL)) + return 0; + + return wait_event_killable(oi->ip_unwritten_wq, + !ocfs2_dio_foreign_unwritten(oi, cstart, cend, NULL)); +} + static int ocfs2_write_begin(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned len, @@ -2637,6 +2672,10 @@ static int ocfs2_write_begin(const struc struct buffer_head *di_bh = NULL; struct inode *inode = mapping->host; + ret = ocfs2_wait_foreign_unwritten(inode, pos, len); + if (ret) + return ret; + ret = ocfs2_inode_lock(inode, &di_bh, 1); if (ret) { mlog_errno(ret); @@ -2977,6 +3016,7 @@ static int ocfs2_write_end(const struct int ocfs2_dio_zero_eof(struct inode *inode, loff_t pos, size_t count) { struct ocfs2_inode_info *oi = OCFS2_I(inode); + struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); struct buffer_head *di_bh = NULL; int ret; loff_t isize = i_size_read(inode); @@ -2989,9 +3029,32 @@ int ocfs2_dio_zero_eof(struct inode *ino * volume there is nothing to do here and no need to take the cluster * lock. The non-sparse path below always has allocation work left. */ - if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)) && pos <= isize) + if (ocfs2_sparse_alloc(osb) && pos <= isize) return 0; + /* + * An in-flight dio may own an unwritten cluster in the gap [i_size, + * pos). ocfs2_zero_extend() skips unwritten clusters, so that dio + * would later convert the cluster to written with its tail past + * i_size never zeroed, exposing stale blocks once this write extends + * i_size beyond it. Wait for those dios to finish first: afterwards + * the cluster is either written (and gets zeroed here) or unwritten + * and unowned (and stays unwritten, reading as zeroes). i_rwsem is + * held, so no new dio records clusters while we wait and the set only + * shrinks. Wait before taking the inode lock and ip_alloc_sem, which + * ->end_io() needs to convert. + */ + if (pos > isize) { + u32 cstart = isize >> osb->s_clustersize_bits; + u32 cend = ocfs2_clusters_for_bytes(inode->i_sb, pos); + + ret = wait_event_killable(oi->ip_unwritten_wq, + !ocfs2_dio_foreign_unwritten(oi, cstart, cend, + NULL)); + if (ret) + return ret; + } + ret = ocfs2_inode_lock(inode, &di_bh, 1); if (ret) { mlog_errno(ret); @@ -2999,7 +3062,7 @@ int ocfs2_dio_zero_eof(struct inode *ino } down_write(&oi->ip_alloc_sem); - if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb))) + if (ocfs2_sparse_alloc(osb)) ret = ocfs2_zero_tail(inode, di_bh, pos); else ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos, count, _ Patches currently in -mm which might be from joseph.qi@linux.alibaba.com are ocfs2-exempt-orphaned-dinodes-from-the-zero-nlink-check.patch ocfs2-abstract-the-zero-nlink-or-mode-dinode-check.patch ocfs2-abstract-the-dinode-i_blkno-mismatch-check.patch ocfs2-abstract-the-dinode-ocfs2_valid_fl-check.patch ocfs2-abstract-the-dinode-fs_generation-mismatch-check.patch ocfs2-abstract-the-dinode-suballoc-slot-checks.patch ocfs2-abstract-the-dinode-suballoc-bit-check.patch ocfs2-abstract-the-dinode-orphaned-slot-check.patch ocfs2-abstract-the-dinode-dio-orphaned-slot-check.patch ocfs2-abstract-the-inline-data-dinode-guard-and-cluster-check.patch ocfs2-abstract-the-inline-data-id_count-bound-check.patch ocfs2-abstract-the-inline-data-i_size-bound-check.patch ocfs2-abstract-the-fast-symlink-dinode-guard-and-size-check.patch ocfs2-abstract-the-fast-symlink-nul-termination-check.patch ocfs2-abstract-the-chain-list-dinode-guard-and-count-check.patch ocfs2-abstract-the-chain-list-next-free-record-check.patch ocfs2-abstract-the-chain-list-bits-per-cluster-check.patch ocfs2-abstract-the-dinode-refcount-location-check.patch ocfs2-abstract-the-zero-extent-list-count-check.patch ocfs2-abstract-the-oversized-extent-list-count-check.patch ocfs2-abstract-the-extent-list-next-free-record-check.patch ocfs2-report-unrepairable-corruption-from-online-filecheck-fix.patch ocfs2-check-dinode-nlink-and-mode-in-online-filecheck.patch ocfs2-check-dinode-suballoc-slot-in-online-filecheck.patch ocfs2-check-dinode-suballoc-bit-in-online-filecheck.patch ocfs2-check-dinode-orphaned-slots-in-online-filecheck.patch ocfs2-check-inline-data-dinodes-in-online-filecheck.patch ocfs2-check-fast-symlink-dinodes-in-online-filecheck.patch ocfs2-check-chain-list-dinodes-in-online-filecheck.patch ocfs2-check-dinode-refcount-location-in-online-filecheck.patch ocfs2-check-extent-list-dinodes-in-online-filecheck.patch ocfs2-dont-bug_on-when-a-group-extend-leaves-too-few-free-bits.patch ocfs2-validate-chain-allocator-clusters-per-group.patch ocfs2-check-chain-allocator-clusters-per-group-in-online-filecheck.patch ocfs2-keep-sub-page-overwrites-of-written-blocks-on-direct-i-o.patch ocfs2-write-the-dio-zeroed-head-tail-buffers-out-before-completion.patch ocfs2-make-buffered-writes-wait-for-in-flight-direct-writes.patch