From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=185.70.43.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796410820; b=oR1c8pEKkdmAKk8CCZVpimAxnbU8OKmR/smXovOv695PoXtWmPzxQojj6CHTwPTCjwGn lF1HNaaL1YXFq6SCPKA6ITzYoE65OnnCxFTLaFNEkfZv5uCD6WTOD2AAOXf5Rb1js/klI AF0k/VofpzIOcFFt/i2e8iWHu0hxYA969s2kMNPa4uJG7nTVbs39wDASOVX+vx5HdwY0x oQHCpmUCD2Mg+NKAbJvcWSmOpkJZ6tG5UqjjNNDw8VC0BwvhvNsyeqMzv+7Q5Eg+4G+xA MQJke0hlo2p689bGw3y7imwjvE/WUvPSxCz6k696tEqG54qytEmyjVFKgncvond2sbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796410820; 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=zNCXM+S8Hl/q0b3NQdqgr703RE/82hRlB/DQd57IGTA=; b=DoQaqnvb9hs0thD+s7Jistcv+RD0AT/I/OzPmsm9eE1/8SzbTZvQDAlcaMfB4ToavIwA 6DcbgI+TydiU2QU8rTw7Xfu7akiz3hoSsnnGXKcvLH2XZUUyXsZwCv4LjYpXqIkgXvXN2 d2g4pcr5g0EY/ceNpVFbOmeINTnOqDIEKMcNVqRAHkW752hFnrjrEr1zjkYMXXcTmbp/s X3RHBGk8QvKfLGbLxLtksznpBeyMRsZbfqwMHJkI15+W+6jihnx6ZU6w2JiuCqqwurFkV o08A8li24Xj16sd18sRF6NjMlUG+n+P6JwAc4jmW7rioqmKGN69BovIt48cqlF94h7A== 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=g434f+3s; arc=none smtp.remote-ip=185.70.43.172 Received: from mail-43172.protonmail.ch (mail-43172.protonmail.ch [185.70.43.172]) (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 318EF606A33E for ; Fri, 4 Dec 2026 19:00:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 318EF606A33E 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=g434f+3s DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raidlab.org; s=protonmail; t=1796410816; x=1796670016; bh=zNCXM+S8Hl/q0b3NQdqgr703RE/82hRlB/DQd57IGTA=; 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=g434f+3szJVqGRMplAJ0deTwG/E5i7pCfIEpAg+pRdW7y64T8MMaCXbuCfJKQN+DL +V1bKO+EFEP8FXX2fFYLAZglP+NJy8V0azYRS0rClKIRDZ1FqhoRQlSjlEhADtL/xM o10KOCPq1W/Eyuj+7g2CgymvCqIHYt2UX63IjRDqLPlBSCmm3kZQO78Rm4SzzTbrg+ U5pUu7NpLztHXZ/cl0eqlvMkYwFxlDtQn7HgKh9lHyqbUhNTre6qokM7VxkqxyAE5x iqOoaw3uPhT0zHQoVbfTKkIjkSzOqxRdEISUd8xLPAZwWTsS7JGkN+xf4ImF5T3vCB QK1FOgJF9dYVA== X-Pm-Submission-Id: 4jh15m2l8Qz2ScdN From: Miroslav Cimerman To: linux-btrfs@vger.kernel.org Cc: David Sterba , Chris Mason , Johannes Thumshirn , Naohiro Aota 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 Message-ID: <20261204185957.313982-3-mc@raidlab.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261204185957.313982-1-mc@raidlab.org> References: <20261204185957.313982-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(). 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 Signed-off-by: Miroslav Cimerman --- 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