Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH v3] btrfs: zoned: reset active_meta_bg on zone finish
@ 2026-07-06 11:02 Johannes Thumshirn
  2026-07-08  1:15 ` Naohiro Aota
  0 siblings, 1 reply; 2+ messages in thread
From: Johannes Thumshirn @ 2026-07-06 11:02 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Naohiro Aota, Johannes Thumshirn, stable

do_zone_finish() clears BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE and removes the
block group from zone_active_bgs, but only the pivot path in
check_bg_is_active() resets fs_info->active_meta_bg / active_system_bg.
Any other finish path (the async zone-finish endio work,
btrfs_zone_finish(), reclaim) then 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: stable@vger.kernel.org
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
Changes to v2:
- Get reference to "tgt" inside check_bg_is_active() if it can actually
  vanish underneath us.

 fs/btrfs/zoned.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 97f06dd01693..fd578bef1f4f 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2213,6 +2213,7 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
 			}
 
 			/* Pivot active metadata/system block group. */
+			btrfs_get_block_group(tgt);
 			btrfs_zoned_meta_io_unlock(fs_info);
 			wait_eb_writebacks(tgt);
 			do_zone_finish(tgt, true);
@@ -2221,6 +2222,7 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
 				btrfs_put_block_group(tgt);
 				*active_bg = NULL;
 			}
+			btrfs_put_block_group(tgt);
 		}
 		if (!btrfs_zone_activate(block_group))
 			return false;
@@ -2535,6 +2537,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;
 
@@ -2632,6 +2635,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 (*active_bg == block_group) {
+			btrfs_put_block_group(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;
-- 
2.54.0


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

* Re: [PATCH v3] btrfs: zoned: reset active_meta_bg on zone finish
  2026-07-06 11:02 [PATCH v3] btrfs: zoned: reset active_meta_bg on zone finish Johannes Thumshirn
@ 2026-07-08  1:15 ` Naohiro Aota
  0 siblings, 0 replies; 2+ messages in thread
From: Naohiro Aota @ 2026-07-08  1:15 UTC (permalink / raw)
  To: Johannes Thumshirn, linux-btrfs; +Cc: Naohiro Aota, stable

On Mon Jul 6, 2026 at 8:02 PM JST, Johannes Thumshirn wrote:
> do_zone_finish() clears BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE and removes the
> block group from zone_active_bgs, but only the pivot path in
> check_bg_is_active() resets fs_info->active_meta_bg / active_system_bg.
> Any other finish path (the async zone-finish endio work,
> btrfs_zone_finish(), reclaim) then 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.

Hmm, but we anyway clear and set active_{meta,system}_bg when btrfs is
writing into metadata. And, if the block group is already finished,
do_zone_finish() will just exit early with
!test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, ...) test. So, I'm not sure
the issue with it.

>
> Fixes: 13bb483d32ab ("btrfs: zoned: activate metadata block group on write time")
> Cc: stable@vger.kernel.org
> Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
> ---
> Changes to v2:
> - Get reference to "tgt" inside check_bg_is_active() if it can actually
>   vanish underneath us.
>
>  fs/btrfs/zoned.c | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
>
> diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
> index 97f06dd01693..fd578bef1f4f 100644
> --- a/fs/btrfs/zoned.c
> +++ b/fs/btrfs/zoned.c
> @@ -2213,6 +2213,7 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
>  			}
>  
>  			/* Pivot active metadata/system block group. */
> +			btrfs_get_block_group(tgt);
>  			btrfs_zoned_meta_io_unlock(fs_info);
>  			wait_eb_writebacks(tgt);
>  			do_zone_finish(tgt, true);
> @@ -2221,6 +2222,7 @@ static bool check_bg_is_active(struct btrfs_eb_write_context *ctx,
>  				btrfs_put_block_group(tgt);
>  				*active_bg = NULL;
>  			}
> +			btrfs_put_block_group(tgt);
>  		}
>  		if (!btrfs_zone_activate(block_group))
>  			return false;
> @@ -2535,6 +2537,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;
>  
> @@ -2632,6 +2635,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 (*active_bg == block_group) {
> +			btrfs_put_block_group(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;


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

end of thread, other threads:[~2026-07-08  1:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-06 11:02 [PATCH v3] btrfs: zoned: reset active_meta_bg on zone finish Johannes Thumshirn
2026-07-08  1:15 ` Naohiro Aota

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox