From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
Johannes Thumshirn <johannes.thumshirn@wdc.com>,
Naohiro Aota <naohiro.aota@wdc.com>
Subject: [PATCH v3 2/3] btrfs: zoned: wait for zone finish works at removal and unmount
Date: Fri, 4 Dec 2026 18:59:56 +0000 [thread overview]
Message-ID: <20261204185957.313982-3-mc@raidlab.org> (raw)
In-Reply-To: <20261204185957.313982-1-mc@raidlab.org>
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().
Also wait in btrfs_remove_block_group(). A removed block group is no
longer in the tree that unmount walks, and its zones are reset after
the removal.
Fixes: 56fbb0a4e8b3 ("btrfs: zoned: properly finish block group on metadata write")
CC: stable@vger.kernel.org # 5.19+
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
v3:
- Also wait in btrfs_remove_block_group(). A block group removed before
unmount is no longer in the tree that unmount walks (Sashiko).
- Reviewed-by from Johannes.
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.
With the delay at the start of the first data work, deleting the data
and unmounting right away hit a use-after-free of the space_info in 2
of 2 runs with v2 of this patch, and in none of 2 runs with v3.
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/block-group.c | 7 +++++++
fs/btrfs/disk-io.c | 7 +++++++
fs/btrfs/zoned.c | 19 +++++++++++++++++++
fs/btrfs/zoned.h | 3 +++
4 files changed, 36 insertions(+)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index dd13856c4b0d..e217500c618d 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -1200,6 +1200,13 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
goto out;
}
+ /*
+ * The zones are reset after the removal, and unmount only waits for the
+ * block groups in the tree. Wait for a zone finish work first.
+ */
+ if (block_group->zone_finish_work.func)
+ flush_work(&block_group->zone_finish_work);
+
write_lock(&fs_info->block_group_cache_lock);
rb_erase_cached(&block_group->cache_node,
&fs_info->block_group_cache_tree);
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 377fc4b46f6c..24ad23808efd 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3853,6 +3853,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);
@@ -4624,6 +4625,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 23c79191a7d8..27c304429b6e 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2931,6 +2931,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 2c17c31437ca..78433afacf8e 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);
@@ -261,6 +262,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
next prev parent reply other threads:[~2026-12-04 19:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 18:59 [PATCH v3 0/3] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
2026-12-04 18:59 ` [PATCH v3 1/3] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
2026-12-04 18:59 ` Miroslav Cimerman [this message]
2026-12-04 18:59 ` [PATCH v3 3/3] btrfs: zoned: finish raid stripe tree zones once all data is written Miroslav Cimerman
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=20261204185957.313982-3-mc@raidlab.org \
--to=mc@raidlab.org \
--cc=dsterba@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=mason@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox