From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D3E246EF9B; Tue, 21 Jul 2026 17:57:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784656656; cv=none; b=qyXC5LUpCkbuabUpABv+ll4nJE5aKVvyGHcWiKg9lviKkjQ2OKgBCHAjFIQ3oF/MTURg8SKolY8qIA0mFMBA3jdZ61pBGBXaq+0618WMNPeBnYf8VLil2hicjkrqelhZ9hHq4qXBXcDv7JzOT6fiT1gLqbSri6yQy5Z0N4v5sqg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784656656; c=relaxed/simple; bh=P6bkut9qFGT2uJdQFPv2tP8Cr6Kp1ReutPw081ycrZM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aam3/WL+SaA3MOd5N8wMhAQgz6sEoKDDSv2uAeWkw3lUKvX2Wq7vVducKrYuDhwsHGjcChdXegTsUbn32MkfO8BpdN0lWL5jpfzmDs+v+zQfCPrR2b3kF7OoJPufNSX/IBQgHDvF2aJShKtiorvti9ai0BinJgS6joDAFdPUbVU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=lcjWkHQX; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="lcjWkHQX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DAB651F00A3A; Tue, 21 Jul 2026 17:57:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1784656655; bh=tkXrKpg0PB4P80dQFwK4SUZ4agkAurwM6H+CgJ/xXwY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lcjWkHQXDjyduA5zEFWdxiynP2qAxb6xSqn4kMUITG1kPly2uoQ9m92P3tVJXfXuz oCQWEY4DkKBXiiYkMhwF10njHEKY4dTpL5tYs2OdkCVnI3VosG++dUTMOjsCM/DYhE 4KND6ZoerqW6ndGiFnoXhaZ8jrhbWloLUuk5F3Eo= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Cen Zhang , David Sterba , Sasha Levin Subject: [PATCH 6.18 0426/1611] btrfs: annotate lockless read of defrag_bytes in should_nocow() Date: Tue, 21 Jul 2026 17:09:02 +0200 Message-ID: <20260721152524.850895909@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260721152514.750365251@linuxfoundation.org> References: <20260721152514.750365251@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Cen Zhang [ Upstream commit 89c0dc3de7a73e8aba5e9bfef543eee047a3d0d2 ] should_nocow() reads inode->defrag_bytes without holding inode->lock, while btrfs_set_delalloc_extent() and btrfs_clear_delalloc_extent() update it under that spinlock. This is a data race. The read is a quick check used to decide whether to fall back to COW for a NOCOW inode: if defrag_bytes is non-zero and the range is tagged EXTENT_DEFRAG, we force COW so that defragmentation can rewrite the extent. Reading a stale value is harmless because: - A missed increment may skip COW once, but the defrag pass will redo the extent later. - A stale non-zero may force an unnecessary COW, which is a minor efficiency loss, not a correctness issue. On 64-bit platforms an aligned u64 load is naturally atomic so tearing cannot happen. On 32-bit platforms u64 may tear, but we only test for zero vs non-zero, so the heuristic stays correct regardless. Use data_race() annotation. Fixes: 47059d930f0e ("Btrfs: make defragment work with nodatacow option") Signed-off-by: Cen Zhang [ Use data_race() instead of READ_ONCXE() ] Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index b972d4cba98023..d37e694b3b38cb 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -2332,7 +2332,7 @@ static noinline int run_delalloc_nocow(struct btrfs_inode *inode, static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end) { if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) { - if (inode->defrag_bytes && + if (data_race(inode->defrag_bytes) && btrfs_test_range_bit_exists(&inode->io_tree, start, end, EXTENT_DEFRAG)) return false; return true; -- 2.53.0