[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20260111202457.23698-1-jiashengjiangcool@gmail.com>
Date: Sun, 11 Jan 2026 20:24:57 +0000
From: Jiasheng Jiang <jiashengjiangcool@...il.com>
To: fdmanana@...nel.org
Cc: clm@...com,
dsterba@...e.com,
jiashengjiangcool@...il.com,
linux-btrfs@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [PATCH v2] btrfs: reset block group size class when reservations are freed
Differential analysis of block-group.c shows an inconsistency between
btrfs_add_reserved_bytes() and btrfs_free_reserved_bytes().
When space is reserved, btrfs_use_block_group_size_class() is called to
set a block group's size class, specializing it for a specific allocation
size to reduce fragmentation. However, when these reservations are
subsequently freed (e.g., due to an error or transaction abort),
btrfs_free_reserved_bytes() fails to perform the corresponding cleanup.
This leads to a state leak where a block group remains stuck with a
specific size class even if it contains no used or reserved bytes. This
stale state causes find_free_extent to unnecessarily skip these block
groups for mismatched size requests, leading to suboptimal allocation
behavior.
Fix this by resetting the size class to BTRFS_BG_SZ_NONE in
btrfs_free_reserved_bytes() when the block group becomes completely
empty.
Fixes: 606d1bf10d7e ("btrfs: migrate the block group space accounting helpers")
Signed-off-by: Jiasheng Jiang <jiashengjiangcool@...il.com>
---
Changelog:
v1 -> v2:
1. Inlined btrfs_maybe_reset_size_class() function.
2. Moved check below the reserved bytes decrement in btrfs_free_reserved_bytes().
---
fs/btrfs/block-group.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 08b14449fabe..8339ad001d3f 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3867,6 +3867,12 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes,
spin_lock(&cache->lock);
bg_ro = cache->ro;
cache->reserved -= num_bytes;
+
+ if (btrfs_block_group_should_use_size_class(cache)) {
+ if (cache->used == 0 && cache->reserved == 0)
+ cache->size_class = BTRFS_BG_SZ_NONE;
+ }
+
if (is_delalloc)
cache->delalloc_bytes -= num_bytes;
spin_unlock(&cache->lock);
--
2.25.1
Powered by blists - more mailing lists