[PATCH v2 2/3] btrfs: zoned: wait for zone finish works at unmount
From: Miroslav Cimerman
Date: Sun Nov 29 2026 - 18:55:50 EST
btrfs_schedule_zone_finish_bg() finishes the zones of a full metadata
block group from zone_finish_work. The work holds a reference to the
block group, and nothing waits for it at unmount. When unmount starts
shortly after a metadata block group became full, the work can still be
queued or running when close_ctree() frees the block groups:
close_ctree() zone_finish_work
waits for the last extent buffer
btrfs_free_block_groups()
empties zone_active_bgs
ASSERT(refs == 1) fails
do_zone_finish()
drops the active_bg_list
reference a second time
Without CONFIG_BTRFS_ASSERT, the work then drops its own reference on a
freed block group.
Wait for the zone finish works of all block groups once no metadata
write is left, in close_ctree() and in the error path of open_ctree().
Fixes: 56fbb0a4e8b3 ("btrfs: zoned: properly finish block group on metadata write")
CC: stable@xxxxxxxxxxxxxxx # 5.19+
Signed-off-by: Miroslav Cimerman <mc@xxxxxxxxxxx>
---
Found while preparing v2 of 3/3. Tested with a test-only delay of 3 s in
the work and an unmount right after the writes. Without this patch, 4
of 4 runs on the metadata path hit ASSERT(refs == 1) in
btrfs_free_block_groups() and then
ASSERT(!list_empty(&block_group->active_bg_list)) in do_zone_finish().
With it, unmount waited for the works in 6 of 6 runs, metadata and data.
The flush runs after btrfs_zoned_release_dirty_metadata(), so no later
metadata write can queue a work. It runs before
invalidate_and_check_btree_folios(), since a queued metadata work holds
an extent buffer.
fs/btrfs/disk-io.c | 7 +++++++
fs/btrfs/zoned.c | 19 +++++++++++++++++++
fs/btrfs/zoned.h | 3 +++
3 files changed, 29 insertions(+)
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index fde9f4c4ea11..8f4f97e492cb 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3867,6 +3867,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
* kthreads
*/
filemap_write_and_wait(fs_info->btree_inode->i_mapping);
+ btrfs_zoned_flush_finish_works(fs_info);
fail_sysfs:
btrfs_sysfs_remove_mounted(fs_info);
@@ -4635,6 +4636,12 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
*/
btrfs_zoned_release_dirty_metadata(fs_info);
+ /*
+ * No metadata write is left that could queue a zone finish work. Wait
+ * for the queued ones, they still hold extent buffers and block groups.
+ */
+ btrfs_zoned_flush_finish_works(fs_info);
+
/*
* We must make sure there is not any read request to
* submit after we stop all workers.
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 0277b2a48a73..a4a5231daf9c 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2892,6 +2892,25 @@ void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
queue_work(system_dfl_wq, &bg->zone_finish_work);
}
+/*
+ * Wait for the zone finish works of all block groups. Unmount calls this before
+ * it stops the workers and frees the block groups.
+ */
+void btrfs_zoned_flush_finish_works(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_block_group *bg;
+
+ if (!btrfs_is_zoned(fs_info))
+ return;
+
+ for (bg = btrfs_lookup_first_block_group(fs_info, 0); bg;
+ bg = btrfs_next_block_group(bg)) {
+ /* The work is initialized when it is queued the first time. */
+ if (bg->zone_finish_work.func)
+ flush_work(&bg->zone_finish_work);
+ }
+}
+
void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg)
{
struct btrfs_fs_info *fs_info = bg->fs_info;
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 2225090839f3..e0934a93b06b 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -92,6 +92,7 @@ int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info, u64 logical,
u64 length);
void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
struct extent_buffer *eb);
+void btrfs_zoned_flush_finish_works(struct btrfs_fs_info *fs_info);
void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg);
void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info);
void btrfs_free_zone_cache(struct btrfs_fs_info *fs_info);
@@ -260,6 +261,8 @@ static inline int btrfs_zone_finish_endio(struct btrfs_fs_info *fs_info,
static inline void btrfs_schedule_zone_finish_bg(struct btrfs_block_group *bg,
struct extent_buffer *eb) { }
+static inline void btrfs_zoned_flush_finish_works(struct btrfs_fs_info *fs_info) { }
+
static inline void btrfs_clear_data_reloc_bg(struct btrfs_block_group *bg) { }
static inline void btrfs_zoned_reserve_data_reloc_bg(struct btrfs_fs_info *fs_info) { }
--
2.53.0