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=1796383524; b=pEh22VYhfnvJM6mC0OmdJ7PfaDgvo1Y+cMyDK091iv4egj2zOqOKpyp2HUpfLGnZXOpm o7nyLJWHSTSyjepYA6SnF8hrPuWNBUOwarbjbcQqp56T0mflunF9WMd8OTuRhjW4HwLKH cEsCQ8kVWjDBzrNVq+2K0r0JNJ8ZgnNQvbPX+dJaGwr3alEZQAEqFQYHXuwJN6P7/rvyw lPReAHJynR4GFuz/9sbDp6qlVLeREXu0QHJ9k+v767CcxJoC6rHO90/wH4/EOa4Em2tvE 3Ui4DeQF8UvCr8QQQjMkyZdsII7LVg5/QQjZWDigfyMK4ZPzeG+cXKp19an3T6HlpjQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796383524; 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=bFvYfR+gfRIiXDJtqLHOpwrhmBwFxQeBplzIqRoM2rc=; b=JPQ2YtaFCLUjByTbY3D2ZUlNoYeZRXU8hYvpRRjypeEm/sbaIcs3hPAtEnyw7SzGDNoG /TbVI9CwiWY6KmQF48Ovztqva4DAOUUvv0eysJ2mutyighdPd/CFz62fx021mMsXUIHz3 tZRmfb5W51kE/23tkjLKD3dVcwJ3t9IheCU6fP8lym/kcPIF70A2+lZ1/7GqWNnr4z8zf sudtj601U6lx0rTQL0URr6qCR2gb/WrxVZ+6Tkn/B97heKZIQ+DWhNwKnYglhuZbbOvE8 J1HNds5h6w0l8O1suxmfLWPaMapA8kleogLJYm5/j4AlJYG2BgRDqhMbvpkniwPuIgw== 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=0luRukW2; 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 C111E606A695; Fri, 4 Dec 2026 11:25:24 +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=0luRukW2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2B0CF1F00A06; Fri, 4 Dec 2026 11:25:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796383524; bh=bFvYfR+gfRIiXDJtqLHOpwrhmBwFxQeBplzIqRoM2rc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=0luRukW2ngICIbehY8BKACFxAlMi0jkxS7LjTUBmFCCMeROB/I496hQJwXcAZJ8do hy5ryag8ofPKkqyLphmcckYpE3Pt/gAP13MPGCYmg3KypAz716oKZkbcY2IqlOhliu kPbCy8D1kvaze/O5tPXbn3eTUa0KNxDtGiDXpP0I= 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.12 258/555] btrfs: pre-flush reflink source before taking inode locks Date: Fri, 4 Dec 2026 19:05:01 +0900 Message-ID: <20261204100618.954071449@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100603.988357525@linuxfoundation.org> References: <20261204100603.988357525@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.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Boris Burkov [ Upstream commit 220c44b1b90fbff746472cbb3dde778b02afbef9 ] Consider the following sketch of a shell script: dd if=/dev/urandom of=/mnt/src bs=1M count=4096 cp --reflink=always /mnt/src /mnt/dst & sleep 0.1 # let the clone reach its flush time dd if=/mnt/src of=/dev/null bs=4K count=1 iflag=direct The current logic in reflink ensures the existence and stability of both the src and destination inode by locking them both and then flushing all dirty pages / ordered_extents under the inode lock. This blocks concurrent usage by even readers of the inode locks, like direct reads or seeks for the duration of writeback on the entirety of the two files. We observe this particular contention frequently in the Meta fleet. It is also a relatively common pattern to write the src file, then reflink it while it is still dirty, so that typical path naturally hits this contention. This workload motivates a relatively simple optimization: trigger the unavoidable src flushing outside the locked region. Therefore, do an optimistic btrfs_wait_ordered_range() without the locks while relying on the calls to btrfs_wait_ordered_range() under the locks in btrfs_remap_file_range_prep() to ensure correctness. In the worst case with concurrent writes while unlocked, we can end up doing the writeback twice, which I think is a reasonable price to pay to avoid victimizing innocent readers in a common case. With and without this patch, the above reproducer runs the reflink in the same ~0.5s on my system. Without the patch, the direct read blocks for basically the full duration of the reflink while with the patch it returns in a few milliseconds. 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/reflink.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/fs/btrfs/reflink.c b/fs/btrfs/reflink.c index acd6280ac29bb..0eb68b7742e29 100644 --- a/fs/btrfs/reflink.c +++ b/fs/btrfs/reflink.c @@ -826,6 +826,17 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src, return ret; } +static u64 calc_remap_wb_len(struct btrfs_inode *src_inode, loff_t off, loff_t len, + unsigned int remap_flags) +{ + const u32 bs = src_inode->root->fs_info->sectorsize; + const loff_t isize = i_size_read(&src_inode->vfs_inode); + + if (len == 0 && !(remap_flags & REMAP_FILE_DEDUP)) + return ALIGN(isize, bs) - ALIGN_DOWN(off, bs); + return ALIGN(len, bs); +} + static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in, struct file *file_out, loff_t pos_out, loff_t *len, unsigned int remap_flags) @@ -865,10 +876,7 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in, * not for the ordered extents to complete. We need to wait for them * to complete so that new file extent items are in the fs tree. */ - if (*len == 0 && !(remap_flags & REMAP_FILE_DEDUP)) - wb_len = ALIGN(inode_in->vfs_inode.i_size, bs) - ALIGN_DOWN(pos_in, bs); - else - wb_len = ALIGN(*len, bs); + wb_len = calc_remap_wb_len(inode_in, pos_in, *len, remap_flags); /* * Workaround to make sure NOCOW buffered write reach disk as NOCOW. @@ -919,11 +927,23 @@ loff_t btrfs_remap_file_range(struct file *src_file, loff_t off, struct btrfs_inode *src_inode = BTRFS_I(file_inode(src_file)); struct btrfs_inode *dst_inode = BTRFS_I(file_inode(dst_file)); bool same_inode = dst_inode == src_inode; + u64 wb_start, wb_len; int ret; if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) return -EINVAL; + /* + * Optimistically write out the src inode before taking locks. + * Consistency is properly ensured by the btrfs_wait_ordered_range() + * inside btrfs_remap_file_range_prep(). + */ + wb_start = ALIGN_DOWN(off, src_inode->root->fs_info->sectorsize); + wb_len = calc_remap_wb_len(src_inode, off, len, remap_flags); + ret = btrfs_wait_ordered_range(src_inode, wb_start, wb_len); + if (ret < 0) + return ret; + if (same_inode) { btrfs_inode_lock(src_inode, BTRFS_ILOCK_MMAP); } else { -- 2.53.0