All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs: set space_info before adding new free space in btrfs_make_block_group()
@ 2026-08-24 16:47 Johannes Thumshirn
  2026-08-31 21:57 ` Boris Burkov
  0 siblings, 1 reply; 2+ messages in thread
From: Johannes Thumshirn @ 2026-08-24 16:47 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Naohiro Aota, Johannes Thumshirn

btrfs_make_block_group() calls btrfs_add_new_free_space() before
assigning cache->space_info. On a zoned filesystem that ends up in
__btrfs_add_free_space_zoned(), which dereferences
block_group->space_info and thus hits a NULL pointer dereference when a
non-initial free space range is added (e.g. during relocation).

Assign cache->space_info before the btrfs_add_new_free_space() call.

Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
 fs/btrfs/block-group.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 830460a40e86..ee182369254c 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3074,21 +3074,25 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran
 		return ERR_PTR(ret);
 	}
 
-	ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL);
-	btrfs_free_excluded_extents(cache);
-	if (ret) {
-		btrfs_put_block_group(cache);
-		return ERR_PTR(ret);
-	}
-
 	/*
 	 * Ensure the corresponding space_info object is created and
 	 * assigned to our block group. We want our bg to be added to the rbtree
 	 * with its ->space_info set.
+	 *
+	 * On a zoned filesystem btrfs_add_new_free_space() ends up in
+	 * __btrfs_add_free_space_zoned(), which dereferences
+	 * block_group->space_info, so it has to be set beforehand.
 	 */
 	cache->space_info = space_info;
 	ASSERT(cache->space_info);
 
+	ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL);
+	btrfs_free_excluded_extents(cache);
+	if (ret) {
+		btrfs_put_block_group(cache);
+		return ERR_PTR(ret);
+	}
+
 	ret = btrfs_add_block_group_cache(cache);
 	if (ret) {
 		btrfs_remove_free_space_cache(cache);
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] btrfs: set space_info before adding new free space in btrfs_make_block_group()
  2026-08-24 16:47 [PATCH] btrfs: set space_info before adding new free space in btrfs_make_block_group() Johannes Thumshirn
@ 2026-08-31 21:57 ` Boris Burkov
  0 siblings, 0 replies; 2+ messages in thread
From: Boris Burkov @ 2026-08-31 21:57 UTC (permalink / raw)
  To: Johannes Thumshirn; +Cc: linux-btrfs, Naohiro Aota

On Mon, Aug 24, 2026 at 06:47:58PM +0200, Johannes Thumshirn wrote:
> btrfs_make_block_group() calls btrfs_add_new_free_space() before
> assigning cache->space_info. On a zoned filesystem that ends up in
> __btrfs_add_free_space_zoned(), which dereferences
> block_group->space_info and thus hits a NULL pointer dereference when a
> non-initial free space range is added (e.g. during relocation).
> 
> Assign cache->space_info before the btrfs_add_new_free_space() call.
> 
Reviewed-by: Boris Burkov <boris@bur.io>
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> ---
>  fs/btrfs/block-group.c | 18 +++++++++++-------
>  1 file changed, 11 insertions(+), 7 deletions(-)
> 
> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
> index 830460a40e86..ee182369254c 100644
> --- a/fs/btrfs/block-group.c
> +++ b/fs/btrfs/block-group.c
> @@ -3074,21 +3074,25 @@ struct btrfs_block_group *btrfs_make_block_group(struct btrfs_trans_handle *tran
>  		return ERR_PTR(ret);
>  	}
>  
> -	ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL);
> -	btrfs_free_excluded_extents(cache);
> -	if (ret) {
> -		btrfs_put_block_group(cache);
> -		return ERR_PTR(ret);
> -	}
> -
>  	/*
>  	 * Ensure the corresponding space_info object is created and
>  	 * assigned to our block group. We want our bg to be added to the rbtree
>  	 * with its ->space_info set.
> +	 *
> +	 * On a zoned filesystem btrfs_add_new_free_space() ends up in
> +	 * __btrfs_add_free_space_zoned(), which dereferences
> +	 * block_group->space_info, so it has to be set beforehand.
>  	 */
>  	cache->space_info = space_info;
>  	ASSERT(cache->space_info);
>  
> +	ret = btrfs_add_new_free_space(cache, chunk_offset, chunk_offset + size, NULL);
> +	btrfs_free_excluded_extents(cache);
> +	if (ret) {
> +		btrfs_put_block_group(cache);
> +		return ERR_PTR(ret);
> +	}
> +
>  	ret = btrfs_add_block_group_cache(cache);
>  	if (ret) {
>  		btrfs_remove_free_space_cache(cache);
> -- 
> 2.55.0
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-08-31 21:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-24 16:47 [PATCH] btrfs: set space_info before adding new free space in btrfs_make_block_group() Johannes Thumshirn
2026-08-31 21:57 ` Boris Burkov

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.