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=1796381973; b=FDfj4JEuuRdSu1SmnRCYWkcLaOJFQ1JWyV9HhgxtuUbpMDL4C7FG1fsvbkE5h2BPRW2J Ao5oVo69UxAhcW5dIFY0F1sPCWSsRtS/yqWHLZg9lRUdPaW5UoR7Y0NDpZ0t1lmP6puuT jGv1HqZJuVa6ia/w+WLlluCU6KFn2d3H3epDpItXsxHhfXsC5jTaCBcW4GpEl/nI27ELk XRLOiBfjLnlsDeKJq9ccnw8FEemx/iF73IaJQ4dE+MIjJkSIYIC9vME4R5qfAAb3k8bDB sHdf/2ASxcS9QSl5e4HTE00VRLHOVax0Bu1+0H3wvdFTdVez3A8ghCwn/Rhdn0QN1Fg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381973; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=e6eVKJcsOGBgOGt2xyTbD5CTab4SpH9CkLAAFP8k4us=; b=hmbgaqbzzJDGBXyjb+GPtWXsTDkVLQ2H6OOfsjqEIvre80HxurOX48hTdEAy7vjJzKxy qfZ2Z4V3WNJpxGy7KrqZWwvlobQNM10R79xKVfwo4G8Vlz8GqXBu8L7OVUx9e9NAVU5o5 oCCmWW4OpiLTetiEwRzwni+6qVyOuZX48yfc55HrQHFngsbEFkdcLUT01r4YE6lArBgfz ppU/yoEdxnPXJDs83KsJJb4celkm9BmtmJbipGubSyB72054mwGe3T1Lc+PIKsc2yObuU aIYdDkQ4rn9BMThTMruiCSdbyV4xlrF3Lex0cA6Ebi2/oO6Ci1CgEVFdA3wqTrqr5DQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=eTfYWcYe; 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 1D115606A322; Fri, 4 Dec 2026 10:59:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=eTfYWcYe Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8DB161F00897; Fri, 4 Dec 2026 10:59:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381973; bh=e6eVKJcsOGBgOGt2xyTbD5CTab4SpH9CkLAAFP8k4us=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=eTfYWcYeR5HCH5/HIzHvNYJFjHS2YbmxcZMn5G2GGU24n9n26lx8pJnHWPfLriRMw EW3A6TKr5cen0g5qKBMMWm9v6FHLRrlquD/+YzNcs2K+avXBwVNOj3vFOOqCZmqstS YgB8KAsJ/ti+sagqXCoEHP7b0vHwCakwuUHFERw0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Filipe Manana , Boris Burkov , David Sterba , Sasha Levin Subject: [PATCH 6.18 253/703] btrfs: downgrade the reflink source inode lock for tree walking Date: Fri, 4 Dec 2026 19:02:32 +0900 Message-ID: <20261204100634.525534007@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Boris Burkov [ Upstream commit cf02c310d018c7e5363b80dfd68ffdf159f3c914 ] When doing a reflink, we are not writing to the src inode or modifying it in any way, so it is counter-intuitive to have to lock it exclusively. This has the cost of blocking other shared lock users of the src file for the duration of any new writeback since the optimistic pre-flush and all the actual reflinking. So we would like to downgrade the lock to shared as much as possible. There are two important gotchas to do with direct io that must be addressed to make this idea work. The first gotcha is that dio writes that come in under EOF try to take inode->i_rwsem shared, which would result in messed up views of the extents in the reflinked file. Therefore, add "being used as a reflink src" as another inode flag exception for the dio write shared tests that already exist to check for other dangerous conditions. In order to ensure that this flag is visible to the dio writer, reflink must first take i_rwsem exclusive, then set the flag, then downgrade to shared. The second is that dio reads increment inode->i_dio_count which blocks inode_dio_wait() in __generic_remap_file_range_prep(). Therefore, we must remain exclusive to dio reads through finishing the prep or risk starving the clone on a stream of dio readers. There is still O(extents) work in btrfs_clone() which can be done without holding the lock exclusive, so we should downgrade to shared and admit dio readers (and other shared lock users like lseek) after we are finished with inode_dio_wait(). By running concurrent clones and lseeks or dio reads on a src file with 32k extents on a vm, I can demonstrate 100ms+ stalls on the readers without this patch and they go away entirely with the patch. Reviewed-by: Filipe Manana Signed-off-by: Boris Burkov Signed-off-by: David Sterba Stable-dep-of: ad4bb983915d ("btrfs: fix race between concurrent sync direct IO writes") Signed-off-by: Sasha Levin --- fs/btrfs/btrfs_inode.h | 6 ++++++ fs/btrfs/direct-io.c | 7 +++++++ fs/btrfs/reflink.c | 17 +++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index b6b46f951f11a..03e5d27970a31 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h @@ -98,6 +98,12 @@ enum { * range). */ BTRFS_INODE_COW_WRITE_ERROR, + /* + * Set by reflink while it holds the source's VFS inode lock shared. + * Direct IO writes also take that lock shared; one that finds this bit + * set after acquiring it must retry with the exclusive lock. + */ + BTRFS_INODE_REFLINK_SRC, /* * Indicate this is a directory that points to a subvolume for which * there is no root reference item. That's a case like the following: diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c index 5f2a99e683353..a5f7f1bb2364b 100644 --- a/fs/btrfs/direct-io.c +++ b/fs/btrfs/direct-io.c @@ -909,6 +909,13 @@ ssize_t btrfs_direct_write(struct kiocb *iocb, struct iov_iter *from) goto relock; } + if ((ilock_flags & BTRFS_ILOCK_SHARED) && + test_bit(BTRFS_INODE_REFLINK_SRC, &BTRFS_I(inode)->runtime_flags)) { + btrfs_inode_unlock(BTRFS_I(inode), ilock_flags); + ilock_flags &= ~BTRFS_ILOCK_SHARED; + goto relock; + } + ret = generic_write_checks(iocb, from); if (ret <= 0) { btrfs_inode_unlock(BTRFS_I(inode), ilock_flags); diff --git a/fs/btrfs/reflink.c b/fs/btrfs/reflink.c index 30e1d8b05fdcd..c7e16785462d3 100644 --- a/fs/btrfs/reflink.c +++ b/fs/btrfs/reflink.c @@ -928,6 +928,7 @@ loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, struct btrfs_inode *dst_inode = BTRFS_I(file_inode(dst_file)); bool same_inode = dst_inode == src_inode; u64 wb_start, wb_len; + bool src_downgraded = false; int ret; if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) @@ -956,6 +957,12 @@ loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, if (ret < 0 || len == 0) goto out_unlock; + if (!same_inode) { + set_bit(BTRFS_INODE_REFLINK_SRC, &src_inode->runtime_flags); + downgrade_write(&src_inode->vfs_inode.i_rwsem); + src_downgraded = true; + } + if (remap_flags & REMAP_FILE_DEDUP) ret = btrfs_extent_same(&src_inode->vfs_inode, off, len, &dst_inode->vfs_inode, destoff); @@ -967,8 +974,14 @@ loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, btrfs_inode_unlock(src_inode, BTRFS_ILOCK_MMAP); } else { btrfs_double_mmap_unlock(src_inode, dst_inode); - unlock_two_nondirectories(&src_inode->vfs_inode, - &dst_inode->vfs_inode); + if (src_downgraded) { + clear_bit(BTRFS_INODE_REFLINK_SRC, &src_inode->runtime_flags); + inode_unlock_shared(&src_inode->vfs_inode); + inode_unlock(&dst_inode->vfs_inode); + } else { + unlock_two_nondirectories(&src_inode->vfs_inode, + &dst_inode->vfs_inode); + } } /* -- 2.53.0