All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boris Burkov <boris@bur.io>
To: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Cc: linux-btrfs@vger.kernel.org, Naohiro Aota <naohiro.aota@wdc.com>
Subject: Re: [PATCH] btrfs: set space_info before adding new free space in btrfs_make_block_group()
Date: Mon, 31 Aug 2026 14:57:14 -0700	[thread overview]
Message-ID: <20260831215714.GD325502@zen.localdomain> (raw)
In-Reply-To: <20260824164758.2012077-1-johannes.thumshirn@wdc.com>

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
> 

      reply	other threads:[~2026-08-31 21:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260831215714.GD325502@zen.localdomain \
    --to=boris@bur.io \
    --cc=johannes.thumshirn@wdc.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=naohiro.aota@wdc.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.