From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=109.224.244.107 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795996544; b=bWfsfL2jfv+DUam4GzuymmqJEk1twot7whUA6pQymnPbpzAphRSmAvwrB8La+73kwhSx lrGIiKeqlq0O6qr8Are3+HSH8ZM93aS6lhXSupUN0Zaw5GN5SHje9IIPXmkDyBl0h1b4p 7hs/e4vQ1uvpkFrb05WSE5Lh+2AOyfGRCbvN81CAa1Y0QN19H6E7EuoP5aZVvIsmzVncR oBkigBjY2qGp6q/AjjUZZmVCKQWC+3zu04Q/vVOI2fmsgqaghlBeVqdFxGYQbGeeg5uT8 1zUb/TM00+2DM5xy+Hxu1QRNWGsE9/Oi3SzXUSL3rmjmhPZLXqECpOSZG8JpaCIz+Sw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795996544; h=DMARC-Filter:DKIM-Signature:X-Pm-Submission-Id:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=XNoYsLtDBa38S0ZDfA3Fh1LxrwGynH7JJsZPOWeEYQU=; b=G9aNQ5POf3AwGghLKlTPVUkpeP1Rb/+VlKrbxxhSVOrq5fKxTq+oopRh/brURnCRPlOT otxV2PRyClaaHan9QXiHZlwayJbi3dLkfuNR+lQx7KvPD/JIWCrGoZTzQe3uDiC7ZlXAt NTUbaIkl2Z79Z+H69Ng3YyhIape+4suyUuks+Inmzo3bz6THu/1L9av2aOplTqfWBWdwH O6PW/ZwtoftxidhC/fu6vCfKjjZoCUp8wSVSddm9wyv30vpckXph/PEHHvcNeHxpijMab LyG43vA1Y99Ov6lOGD+6Gf9bLFTYdmpo/EhtPOjyp0GqDSNcZw8K/S2JhUDp8jWYi9w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=raidlab.org; spf=pass smtp.mailfrom=raidlab.org; dkim=pass header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b="Bj/1wzRd"; arc=none smtp.remote-ip=109.224.244.107 Received: from mail-244107.protonmail.ch (mail-244107.protonmail.ch [109.224.244.107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7980A606A327 for ; Sun, 29 Nov 2026 23:55:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7980A606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=raidlab.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=raidlab.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=raidlab.org header.i=@raidlab.org header.a=rsa-sha256 header.s=protonmail header.b=Bj/1wzRd DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raidlab.org; s=protonmail; t=1795996541; x=1796255741; bh=XNoYsLtDBa38S0ZDfA3Fh1LxrwGynH7JJsZPOWeEYQU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=Bj/1wzRd5GiWICy1DbysoLkr3pTmJuINQU2G9t8okq+L4H27nuQprcSnWHyX28ho/ a91gpvVce5Z+FC6T0RRYyeUXX696lZ65OH2u2S1z8thnbtk2iEDFLfIQcJ9U11F1ZP b2GyPTHXOkN52bsxGFlsjk9Sxo7lFQEUDupbMwrnu0uxy9v6U99QP2TgiSXZrkABrc If9KUGIdU3yAqcJuZeJx/i6+botHfe7aE5rxoe51DjxeoiDtTq8PxJPaH+C1gk65d0 dJG5taoVoeX4uL9XuHuhSsUHKpMbZ1ZD0z/TRTVevt4QEJABwPn6x86RBQStRycr6c 1ex4N0tly1JmA== X-Pm-Submission-Id: 4jd3tz0qBKz2ScP5 From: Miroslav Cimerman To: linux-btrfs@vger.kernel.org Cc: David Sterba , Chris Mason , Naohiro Aota , Johannes Thumshirn , Qu Wenruo , 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 Message-ID: <20261129235226.459661-3-mc@raidlab.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261129235226.459661-1-mc@raidlab.org> References: <20261129235226.459661-1-mc@raidlab.org> X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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