Skip to content

Commit 5870ec7

Browse files
Jiasheng Jiangkdave
authored andcommitted
btrfs: reset block group size class when it becomes empty
Block group size classes are managed consistently everywhere. Currently, btrfs_use_block_group_size_class() sets a block group's size class to specialize it for a specific allocation size. However, this size class remains "stale" even if the block group becomes completely empty (both used and reserved bytes reach zero). This happens in two scenarios: 1. When space reservations are freed (e.g., due to errors or transaction aborts) via btrfs_free_reserved_bytes(). 2. When the last extent in a block group is freed via btrfs_update_block_group(). While size classes are advisory, a stale size class can cause find_free_extent to unnecessarily skip candidate block groups during initial search loops. This undermines the purpose of size classes to reduce fragmentation by keeping block groups restricted to a specific size class when they could be reused for any size. Fix this by resetting the size class to BTRFS_BG_SZ_NONE whenever a block group's used and reserved counts both reach zero. This ensures that empty block groups are fully available for any allocation size in the next cycle. Fixes: 52bb7a2 ("btrfs: introduce size class to block group allocator") Reviewed-by: Boris Burkov <boris@bur.io> Signed-off-by: Jiasheng Jiang <jiashengjiangcool@gmail.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent be6324a commit 5870ec7

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

fs/btrfs/block-group.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,6 +3760,14 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
37603760
return ret;
37613761
}
37623762

3763+
static void btrfs_maybe_reset_size_class(struct btrfs_block_group *bg)
3764+
{
3765+
lockdep_assert_held(&bg->lock);
3766+
if (btrfs_block_group_should_use_size_class(bg) &&
3767+
bg->used == 0 && bg->reserved == 0)
3768+
bg->size_class = BTRFS_BG_SZ_NONE;
3769+
}
3770+
37633771
int btrfs_update_block_group(struct btrfs_trans_handle *trans,
37643772
u64 bytenr, u64 num_bytes, bool alloc)
37653773
{
@@ -3824,6 +3832,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
38243832
old_val -= num_bytes;
38253833
cache->used = old_val;
38263834
cache->pinned += num_bytes;
3835+
btrfs_maybe_reset_size_class(cache);
38273836
btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
38283837
space_info->bytes_used -= num_bytes;
38293838
space_info->disk_used -= num_bytes * factor;
@@ -3952,6 +3961,7 @@ void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, u64 num_bytes,
39523961
spin_lock(&cache->lock);
39533962
bg_ro = cache->ro;
39543963
cache->reserved -= num_bytes;
3964+
btrfs_maybe_reset_size_class(cache);
39553965
if (is_delalloc)
39563966
cache->delalloc_bytes -= num_bytes;
39573967
spin_unlock(&cache->lock);

0 commit comments

Comments
 (0)