Re: [PATCH] btrfs: zoned: reset active_meta_bg on zone finish

Miquel Sabaté Solà <[email protected]>
Newsgroups org.kernel.vger.linux-btrfs,org.kernel.vger.stable
Message-ID <87y0fs772r.fsf@>
Hi,

If you don't mind, a couple of questions from a newcomer that is trying
to grok this part of the code :)

Johannes Thumshirn @ 2026-07-03 10:45 +02:

> do_zone_finish() clears BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE and removes the
> block group from zone_active_bgs, but only the path in
> check_bg_is_active() resets fs_info->active_meta_bg / active_system_bg.
> Any other finish path leaves active_meta_bg / active_system_bg pointing
> at an inactive, fully written block group.
>
> Reset the corresponding active_{meta,system}_bg pointer in do_zone_finish()
> so it can never go stale.
>
> Fixes: 13bb483d32ab ("btrfs: zoned: activate metadata block group on write time")
> Cc: [email protected]
> Signed-off-by: Johannes Thumshirn <[email protected]>
> ---
>  fs/btrfs/zoned.c | 15 +++++++++++++++
>  1 file changed, 15 insertions(+)
>
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index 44a13ed6b8b2..c8c850de1702 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -2539,6 +2539,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>  	const bool is_metadata = (block_group->flags &
>  			(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
>  	struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
> +	struct btrfs_block_group **active_bg = NULL;
>  	int ret = 0;
>  	int i;
>
> @@ -2636,6 +2637,20 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
>  	/* For active_bg_list */
>  	btrfs_put_block_group(block_group);
>
> +	if (block_group->flags & BTRFS_BLOCK_GROUP_SYSTEM)
> +		active_bg = &fs_info->active_system_bg;
> +	else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA)
> +		active_bg = &fs_info->active_meta_bg;
> +
> +	if (active_bg) {
> +		btrfs_zoned_meta_io_lock(fs_info);

If you need to lock/unlock in order call btrfs_put_block_group() and
then reset *active_bg, couldn't the previous if statement be written
like so?

if (active_bg && (*active_bg == block_group)) {

This would then only lock/unlock just in the case we really want to
touch this 'block_group', no?

> +		if (*active_bg == block_group) {
> +			btrfs_put_block_group(block_group);

Also, hasn't 'block_group' already been put before your patch? Won't
this try to double-free this pointer? Or it is about decreasing the
reference twice for this block group?

> +			*active_bg = NULL;
> +		}
> +		btrfs_zoned_meta_io_unlock(fs_info);
> +	}
> +
>  	clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
>
>  	return 0;

Thanks,
Miquel
signature.asc (application/pgp-signature, 897 B)
-----BEGIN PGP SIGNATURE-----

iQJiBAEBCgBMFiEEG6U8esk9yirP39qXlr6Mb9idZWUFAmpHgWwbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwyEhxtc3NvbGFAbXNzb2xhLmNvbQAKCRCWvoxv2J1l
ZdArEACvaz+Zl25HHwVBuw94kYt6yA7wSBHzYs6QqDeWZHUZpkO9Ij9wTBH/HEM2
+4tE1HlhbY+Fn+/IhhyTWgMMgYKSFAKThhe//v2oX7VKfFPCAP6W9wWkvPkf/w5l
ZAWPflN49xN0X4VGXBvqOT5Y58zF/HN0d1wVkDLa+OKmFfGmQL/4xcwyXBBKHhSU
OuwTV5MB/yb+nF4274citrBb1IUbzZnhSlhXyKdVax+vLu4AuKju9E64XVWuz8w9
6iVOKLpd9zm50r53cLoTDuVVuw++EpmpY3dIniAkNsEZLwyIkyAJnoNyxW5bO3Y1
2yLZLcs107ovIx/rPQgFJLRGp2dpDhzHDsutWJIieZe/F6yGlCE8w9fDhVWFll51
KzKYafFIdbEfV1w3z2JOcxwVSb/b9n7/Nq/MvRZi3lBuGkVBngyMdb0jXDC3QOOX
4SuYT4JkqhouWxuQ0Eulw4OnvB14UU/2KkJd3UTE86lZINvFzvQQsWX5lKIT3qZG
gvpXT1UAUoSr+MfGTLiuL31xMpcY/T1Y7d4rfvQFF4k7N+ZY4WUMaCTme8Nm6yhy
o21PYFDTcqkfaoHvPotV+GEyLRyFFXvJL0mHVPMOrUtcRs0kFyB+QkMxHJ9cq67z
IjBarQKwybf31+tgaOmthWnGAbQgfLJMh3j4H50pryl6qZiVnw==
=MF0k
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.