From: Boris Burkov Date: Tue, 19 Aug 2025 01:58:47 +0000 (-0400) Subject: btrfs: explicitly ref count block_group on new_bgs list X-Git-Tag: v6.12.44~176 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9a20e95694cfac92054cc8c1a1154046d2478b77;p=thirdparty%2Fkernel%2Fstable.git btrfs: explicitly ref count block_group on new_bgs list [ Upstream commit 7cbce3cb4c5cfffd8b08f148e2136afc1ec1ba94 ] All other users of the bg_list list_head increment the refcount when adding to a list and decrement it when deleting from the list. Just for the sake of uniformity and to try to avoid refcounting bugs, do it for this list as well. This does not fix any known ref-counting bug, as the reference belongs to a single task (trans_handle is not shared and this represents trans_handle->new_bgs linkage) and will not lose its original refcount while that thread is running. And BLOCK_GROUP_FLAG_NEW protects against ref-counting errors "moving" the block group to the unused list without taking a ref. With that said, I still believe it is simpler to just hold the extra ref count for this list user as well. Reviewed-by: Filipe Manana Signed-off-by: Boris Burkov Signed-off-by: David Sterba Stable-dep-of: 62be7afcc13b ("btrfs: zoned: requeue to unused block group list if zone finish failed") Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index 7eef79ece5b3c..5d5985fe87182 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -2807,6 +2807,7 @@ next: spin_lock(&fs_info->unused_bgs_lock); list_del_init(&block_group->bg_list); clear_bit(BLOCK_GROUP_FLAG_NEW, &block_group->runtime_flags); + btrfs_put_block_group(block_group); spin_unlock(&fs_info->unused_bgs_lock); /* @@ -2945,6 +2946,7 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran } #endif + btrfs_get_block_group(cache); list_add_tail(&cache->bg_list, &trans->new_bgs); btrfs_inc_delayed_refs_rsv_bg_inserts(fs_info); diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c index dbef80cd5a9f1..1a029392eac52 100644 --- a/fs/btrfs/transaction.c +++ b/fs/btrfs/transaction.c @@ -2113,6 +2113,7 @@ static void btrfs_cleanup_pending_block_groups(struct btrfs_trans_handle *trans) */ spin_lock(&fs_info->unused_bgs_lock); list_del_init(&block_group->bg_list); + btrfs_put_block_group(block_group); spin_unlock(&fs_info->unused_bgs_lock); } }