Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
	Naohiro Aota <naohiro.aota@wdc.com>,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>,
	Qu Wenruo <wqu@suse.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH v2 2/3] btrfs: zoned: wait for zone finish works at unmount
Date: Sun, 29 Nov 2026 23:52:25 +0000	[thread overview]
Message-ID: <20261129235226.459661-3-mc@raidlab.org> (raw)
In-Reply-To: <20261129235226.459661-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().

Fixes: 56fbb0a4e8b3 ("btrfs: zoned: properly finish block group on metadata write")
CC: stable@vger.kernel.org # 5.19+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
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


  parent reply	other threads:[~2026-11-29 23:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 23:52 [PATCH v2 0/3] btrfs: zoned: unwritten copies of mirrored data writes Miroslav Cimerman
2026-11-29 23:52 ` [PATCH v2 1/3] btrfs: zoned: use zone append for every copy of a mirrored write Miroslav Cimerman
2026-11-29 23:52 ` Miroslav Cimerman [this message]
2026-11-29 23:52 ` [PATCH v2 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=20261129235226.459661-3-mc@raidlab.org \
    --to=mc@raidlab.org \
    --cc=dsterba@suse.com \
    --cc=johannes.thumshirn@wdc.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mason@kernel.org \
    --cc=naohiro.aota@wdc.com \
    --cc=wqu@suse.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