From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.100 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796032970; b=U33CnH1N2+d13AHTd9qNsf14pk7KVcVpZOg4WhOQkQ1f6WcIOHG7uRw1qmemG20l+b6d yNdE9y4fQvPSAuGIUsCTihRjX/hokvgQM6jWsNQYUrmRRhIV1NJ08j69q+PJiRPLD26ra N1rdCcLzSLYK9gKcxsN3Aija16+Di06IKxrMMKO38dajR47A3GJMpsnWebwQTR3QlGuMq hrMXFn4DlQoV/bsHKrK9JqeMwLWtvldfZwDLgPCP1hD8VNWhxBLDwYaiGsYRQEAIs8cl+ BeMU6TRnFdZnoXfnmsNz41rfdyKEtI1OgUzxYaq+7TQ9M2ao8JYZb4XG+KaWz1v5uSw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796032970; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=7ciuUvc9dCcY9hFg32GigbaDL2iU+v3W3nvCZvjMqhE=; b=PFOmM3LB7qGhI5+FiM0/ANRfTa89XaIE7MYh6aWQUTXJ5mwC4lu35rHulkHX4cYbOXOI oQiPsMvfsFFlIwWhoAj69zZbo1u6HM/Rcc85CqlN5e3UzfeoB9qy+F0QMyVJwCqsYvEvg FgyysTZ1MLdVmf2UQfQp7HYcroEwkYDNIAWmRBKHke3lp6WWqcbJxcIJjzdNvqDLBBfkU 6OEWfuporhVFlKWA+xbwQQdM44Lg0y6KBkjD0+WfWgPDLezLZsm+yN89YiszrQuwvmLgB O3CmSLqWlGegZGJafoyG+R3HDmtAwLtkbADZpPog0zggvW90lEZTJVm9gR7l9eYl5pA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=rXoYpc+M; arc=none smtp.remote-ip=115.124.30.100 Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (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 DA1B1606A6B2 for ; Mon, 30 Nov 2026 10:02:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DA1B1606A6B2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=rXoYpc+M DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796032968; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=7ciuUvc9dCcY9hFg32GigbaDL2iU+v3W3nvCZvjMqhE=; b=rXoYpc+M7iZtXcuZpygqEUwSKz4sNZtkHTaBWzgWmvuBRhilv5U+jmBYEMPjiTMFXQDHRCmSYZd4O3KO2LafKXIGDd5uIg4vHYoEBhewY3r4kE40Xhyuozhqtop7Vk/EFICxksu6vDtGv17knVx0QbW0bLgGZA64TG9PMvJsYZs= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R201e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033045133197;MF=joseph.qi@linux.alibaba.com;NM=1;PH=DS;RN=8;SR=0;TI=SMTPD_---0XFjST9v_1796032966; Received: from localhost(mailfrom:joseph.qi@linux.alibaba.com fp:SMTPD_---0XFjST9v_1796032966 cluster:ay36) by smtp.aliyun-inc.com; Mon, 30 Nov 2026 18:02:47 +0800 From: Joseph Qi To: Heming Zhao , hch@lst.de Cc: Andrew Morton , Mark Fasheh , Joel Becker , ocfs2-devel@lists.linux.dev, linux-kernel@vger.kernel.org, Joseph Qi Subject: [RFC PATCH v13 10/10] ocfs2: make buffered writes wait for in-flight direct writes Date: Mon, 30 Nov 2026 18:02:03 +0800 Message-Id: <20261130100203.383093-11-joseph.qi@linux.alibaba.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20261130100203.383093-1-joseph.qi@linux.alibaba.com> References: <20261130100203.383093-1-joseph.qi@linux.alibaba.com> 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 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(). 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 is reachable through get_user_pages() from bio_iov_iter_get_pages() when the dio source buffer is an mmap of the same file. Waiting there with no owner would match the dio's own entries, which are only removed at end_io, and deadlock against itself. The mmap window therefore remains open and needs a way to exclude the current task's own in-flight dio first. Signed-off-by: Joseph Qi --- fs/ocfs2/aops.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/fs/ocfs2/aops.c b/fs/ocfs2/aops.c index 831be79a9a377..5fbaf83b8daaa 100644 --- a/fs/ocfs2/aops.c +++ b/fs/ocfs2/aops.c @@ -2569,6 +2569,32 @@ int ocfs2_write_begin_nolock(struct address_space *mapping, 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); + u32 cstart = pos >> 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, @@ -2578,6 +2604,10 @@ static int ocfs2_write_begin(const struct kiocb *iocb, 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); -- 2.39.3