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=1796381970; b=KpQ8Q5/gl9/3wIMudc3WTsYoYTwrAU5XN4wZ0xCP5VwpORqDUdcuHlw1GxGPAKpdQVrP FXxlXg7fCkV56+l04AmVnTsNx741CwA37Re8q1TvoGb3CDE7HIk74sdRbgGJyXm6ZKjyF t8z1UPz7LbRpxEj/dB5XgBoV3EWsrLamo1wbtjx8SkOOfwk0QYfjKB2kj34sAXLohaDAf zE41LuFZdtLkEWGacayrwf/0uPGEkxuH5XUTheQwC71ajkGjD/rp4zheKp1PNCX6NiplQ kRqkESpwaG+re70lQLyC1VvahMbbMPHGB0nP5mhxTeA5PyhydxNiwRaeeY9cu/2dH0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381970; 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=DY7KKEuYHxcmmLh/sDizw1yZ/IY8qeSTv988LM+Yh8Q=; b=am2ZJh+5Kr0cnkNaXVgHE0Thpea4g9Hdw0CpLbdsDkwb8uE/SX/5pNQKc8+lO8RmjfV1 +X3TJOb5TSF0XSnjPdYTFgOZM2a2lYwg4L+GFJdhHnSjwtF5ydINl5unBTNZCJKAGlCGP 11cJ7BrE5pGKw/B5oZh0NqebzHGSn8gRO8ZjXvugImdyvdVOV25UzIK+VhM2Hzo/cqrXH DxvIZbRgsz/RDmbHy+hMiVd50xnepDDPpMlDMr6u8OZ9Kux0buyZiovWrN+uA+zMQKg7h dXwspKr8NYafvk2UYGc0pLTR5fRj2YRKre6ArRDxiSgvg3hNeiLqYNPivOmxuQbuGCw== 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=ZD6FEy+y; 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 DDECB606A322; Fri, 4 Dec 2026 10:59:30 +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=ZD6FEy+y Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55BB31F000C2; Fri, 4 Dec 2026 10:59:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381970; bh=DY7KKEuYHxcmmLh/sDizw1yZ/IY8qeSTv988LM+Yh8Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ZD6FEy+yRB4k7edYRlwsHWBK/MxK63SAVGEdPXg62xO7Pv9lFAHZSNmOhNwZcYWrK 43+MU9+n9+61aslE3014xVM47n2Fa2M1eAjRPOJp81wN5gjktbNlqUiHjPr0EaxEz6 a5EuRK1TVutrVmbhWF/AnAME+VKFWXtbcxyPTshU= 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 252/703] btrfs: pre-flush reflink source before taking inode locks Date: Fri, 4 Dec 2026 19:02:31 +0900 Message-ID: <20261204100634.500223244@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 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 c6f8db5027a42..30e1d8b05fdcd 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