* [PATCH v5 0/2] btrfs: zoned: fix activation deadlock and zone finishing race
@ 2026-12-03 10:18 Dongjiang Zhu
2026-12-03 10:18 ` [PATCH v5 1/2] btrfs: zoned: remove obsolete non-data block group activation helper Dongjiang Zhu
2026-12-03 10:18 ` [PATCH v5 2/2] btrfs: zoned: serialize zone finishing per block group Dongjiang Zhu
0 siblings, 2 replies; 3+ messages in thread
From: Dongjiang Zhu @ 2026-12-03 10:18 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
This series fixes a self-deadlock in allocation-time activation of
non-data block groups and a race between concurrent attempts to
finish the same block group.
It contains patches 3 and 4 from v4. Patch 2 needs further design
work, and patch 1 overlaps with Johannes's work, so those two
patches are deferred.
Changes in v5:
- Split out patches 3 and 4 from v4.
- Add a Fixes tag to the activation-helper removal patch.
v4: https://lore.kernel.org/linux-btrfs/cover.1795600611.git.zhudongjiang@fygo.io/
Dongjiang Zhu (2):
btrfs: zoned: remove obsolete non-data block group activation helper
btrfs: zoned: serialize zone finishing per block group
fs/btrfs/block-group.c | 38 ++++++--------------
fs/btrfs/block-group.h | 1 +
fs/btrfs/zoned.c | 79 +++++++++++++++---------------------------
fs/btrfs/zoned.h | 8 -----
4 files changed, 39 insertions(+), 87 deletions(-)
--
2.39.5
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v5 1/2] btrfs: zoned: remove obsolete non-data block group activation helper
2026-12-03 10:18 [PATCH v5 0/2] btrfs: zoned: fix activation deadlock and zone finishing race Dongjiang Zhu
@ 2026-12-03 10:18 ` Dongjiang Zhu
2026-12-03 10:18 ` [PATCH v5 2/2] btrfs: zoned: serialize zone finishing per block group Dongjiang Zhu
1 sibling, 0 replies; 3+ messages in thread
From: Dongjiang Zhu @ 2026-12-03 10:18 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
On an HC620 with max_active_zones=128, file writes occupied all 128
active-zone slots. With four direct-I/O writers still running, starting
a balance with
btrfs balance start -f -dusage=0 -musage=0 -susage=100 <mountpoint>
left the balance task blocked in D state. SysRq-w reported:
task:btrfs state:D stack:0 pid:8145
Call Trace:
<TASK>
__schedule+0x50e/0xfe0
schedule+0x22/0xa0
schedule_preempt_disabled+0x10/0x20
__mutex_lock.constprop.0+0x380/0x920
btrfs_inc_block_group_ro+0x6a/0x270 [btrfs]
do_zone_finish+0x2b6/0x4c0 [btrfs]
btrfs_zone_finish_one_bg+0x184/0x1f0 [btrfs]
btrfs_zoned_activate_one_bg+0x189/0x1d0 [btrfs]
reserve_chunk_space+0xdb/0x180 [btrfs]
btrfs_inc_block_group_ro+0x252/0x270 [btrfs]
btrfs_relocate_block_group+0xc2/0x2260 [btrfs]
btrfs_relocate_chunk+0x39/0x1c0 [btrfs]
btrfs_balance+0x911/0x1810 [btrfs]
</TASK>
The blocked balance task also stalls transaction commit and data writers,
so the filesystem can no longer make write progress or unmount cleanly.
This happens because the outer btrfs_inc_block_group_ro() holds
ro_block_group_mutex while reserve_chunk_space() tries to activate a
system block group. When no active-zone slot is available,
btrfs_zoned_activate_one_bg() starts finishing a partially written data
block group.
do_zone_finish() then enters btrfs_inc_block_group_ro() again. The same
task waits for ro_block_group_mutex that it already holds and
self-deadlocks.
These allocation-time activation calls were needed for
active_total_bytes accounting, which has been removed. Metadata and
system block groups can instead be activated during writeback.
Remove both calls and the unused helper. This avoids recursively
entering btrfs_inc_block_group_ro() while holding ro_block_group_mutex
and leaves activation to the writeback path, where normal metadata
and system block group switching is handled.
Fixes: b6a98021e401 ("btrfs: zoned: activate necessary block group")
Assisted-by: LLM
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
---
fs/btrfs/block-group.c | 38 +++++++++-----------------------
fs/btrfs/zoned.c | 50 ------------------------------------------
fs/btrfs/zoned.h | 8 -------
3 files changed, 10 insertions(+), 86 deletions(-)
diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 0736132624f8..f600e9f0c380 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -3220,13 +3220,6 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,
alloc_flags = btrfs_get_alloc_profile(fs_info, space_info->flags);
ret = btrfs_chunk_alloc(trans, space_info, alloc_flags, CHUNK_ALLOC_FORCE);
- if (ret < 0)
- goto out;
- /*
- * We have allocated a new chunk. We also need to activate that chunk to
- * grant metadata tickets for zoned filesystem.
- */
- ret = btrfs_zoned_activate_one_bg(space_info, true);
if (ret < 0)
goto out;
@@ -4209,29 +4202,18 @@ static void reserve_chunk_space(struct btrfs_trans_handle *trans,
if (IS_ERR(bg)) {
ret = PTR_ERR(bg);
} else {
- int activate_ret;
-
/*
- * We have a new chunk. We also need to activate it for
- * zoned filesystem.
+ * If we fail to add the chunk item here, we end
+ * up trying again at phase 2 of chunk allocation,
+ * at btrfs_create_pending_block_groups(). So
+ * ignore any error here. An ENOSPC here could
+ * happen, due to the cases described at
+ * do_chunk_alloc() - the system block group we
+ * just created was just turned into RO mode by a
+ * scrub for example, or a running discard
+ * temporarily removed its free space entries, etc.
*/
- activate_ret = btrfs_zoned_activate_one_bg(info, true);
- if (activate_ret < 0) {
- ret = activate_ret;
- } else {
- /*
- * If we fail to add the chunk item here, we end
- * up trying again at phase 2 of chunk allocation,
- * at btrfs_create_pending_block_groups(). So
- * ignore any error here. An ENOSPC here could
- * happen, due to the cases described at
- * do_chunk_alloc() - the system block group we
- * just created was just turned into RO mode by a
- * scrub for example, or a running discard
- * temporarily removed its free space entries, etc.
- */
- btrfs_chunk_alloc_add_chunk_item(trans, bg);
- }
+ btrfs_chunk_alloc_add_chunk_item(trans, bg);
}
}
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 23c79191a7d8..4dda1a470237 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -3148,56 +3148,6 @@ int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
return ret < 0 ? ret : 1;
}
-int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish)
-{
- struct btrfs_fs_info *fs_info = space_info->fs_info;
- struct btrfs_block_group *bg;
- int index;
-
- if (!btrfs_is_zoned(fs_info) || (space_info->flags & BTRFS_BLOCK_GROUP_DATA))
- return 0;
-
- for (;;) {
- int ret;
- bool need_finish = false;
-
- down_read(&space_info->groups_sem);
- for (index = 0; index < BTRFS_NR_RAID_TYPES; index++) {
- list_for_each_entry(bg, &space_info->block_groups[index],
- list) {
- if (!spin_trylock(&bg->lock))
- continue;
- if (btrfs_zoned_bg_is_full(bg) ||
- test_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE,
- &bg->runtime_flags)) {
- spin_unlock(&bg->lock);
- continue;
- }
- spin_unlock(&bg->lock);
-
- if (btrfs_zone_activate(bg)) {
- up_read(&space_info->groups_sem);
- return 1;
- }
-
- need_finish = true;
- }
- }
- up_read(&space_info->groups_sem);
-
- if (!do_finish || !need_finish)
- break;
-
- ret = btrfs_zone_finish_one_bg(fs_info);
- if (ret == 0)
- break;
- if (ret < 0)
- return ret;
- }
-
- return 0;
-}
-
/*
* Reserve zones for one metadata block group, one tree-log block group, and one
* system block group.
diff --git a/fs/btrfs/zoned.h b/fs/btrfs/zoned.h
index 2c17c31437ca..5d2236a96da0 100644
--- a/fs/btrfs/zoned.h
+++ b/fs/btrfs/zoned.h
@@ -99,7 +99,6 @@ bool btrfs_zoned_should_reclaim(const struct btrfs_fs_info *fs_info);
void btrfs_zoned_release_data_reloc_bg(struct btrfs_fs_info *fs_info, u64 logical,
u64 length);
int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info);
-int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info, bool do_finish);
void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info);
int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info, u64 num_bytes);
void btrfs_show_zoned_stats(struct btrfs_fs_info *fs_info, struct seq_file *seq);
@@ -280,13 +279,6 @@ static inline int btrfs_zone_finish_one_bg(struct btrfs_fs_info *fs_info)
return 1;
}
-static inline int btrfs_zoned_activate_one_bg(struct btrfs_space_info *space_info,
- bool do_finish)
-{
- /* Consider all the block groups are active */
- return 0;
-}
-
static inline void btrfs_check_active_zone_reservation(struct btrfs_fs_info *fs_info) { }
static inline int btrfs_reset_unused_block_groups(struct btrfs_space_info *space_info,
--
2.39.5
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v5 2/2] btrfs: zoned: serialize zone finishing per block group
2026-12-03 10:18 [PATCH v5 0/2] btrfs: zoned: fix activation deadlock and zone finishing race Dongjiang Zhu
2026-12-03 10:18 ` [PATCH v5 1/2] btrfs: zoned: remove obsolete non-data block group activation helper Dongjiang Zhu
@ 2026-12-03 10:18 ` Dongjiang Zhu
1 sibling, 0 replies; 3+ messages in thread
From: Dongjiang Zhu @ 2026-12-03 10:18 UTC (permalink / raw)
To: linux-btrfs; +Cc: johannes.thumshirn, naohiro.aota, a2jphilipp
On an HC620 with max_active_zones=128, buffered writes occupied all 128
active-zone slots. A metadata-heavy create/delete workload followed by
sync then forced a metadata block group switch.
A kernel with additional zone finish tracing reported:
zoned-debug: finish clear-runtime bg=140928614400 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
zoned-debug: finish early-inactive bg=140928614400 caller=btrfs_check_meta_write_pointer comm=sync
zoned-debug: pivot finish ret=0 old_bg=140928614400 new_bg=339839287296
zoned-debug: activate fail slot bg=339839287296 type=metadata left=0 max_active=128 comm=sync
zoned-debug: finish zone-mgmt bg=140928614400 ret=0 left=0 caller=btrfs_zone_finish_endio_workfn comm=kworker/u8:7
BTRFS error (device sdb): error while writing out transaction: -EAGAIN
BTRFS error (device sdb state A): Transaction 67 aborted (-EAGAIN)
BTRFS info (device sdb state EA): forced readonly
zoned-debug: clear-active dev=/dev/sdb pos=140928614400 left=1 max_active=128
The transaction was aborted before the old block group returned its
active-zone slot, forcing the filesystem read-only.
This happens because do_zone_finish() clears
BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE before issuing the zone finish operation.
This allows the following interleaving:
Thread A, endio worker Thread B, transaction writeback
clear ZONE_IS_ACTIVE
call_zone_finish(old)
observe old as inactive
return from do_zone_finish(old)
activate(new)
fail with left == 0
clear the device active bit
increment active_zones_left
Thread B treats the cleared active bit as a completed finish, although
thread A has not yet returned the active-zone slot.
Add ZONE_FINISHING to serialize finishers per block group. Keep
ZONE_IS_ACTIVE set until the device operations and active-list cleanup
are complete, then wake waiting finishers. Release any temporary
read-only reference acquired by a waiting caller.
Assisted-by: LLM
Tested-by: Jochen Philipp <a2jphilipp@gmail.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fygo.io>
---
fs/btrfs/block-group.h | 1 +
fs/btrfs/zoned.c | 29 ++++++++++++++++++++++++++++-
2 files changed, 29 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/block-group.h b/fs/btrfs/block-group.h
index d567ed822e55..31aca6424d54 100644
--- a/fs/btrfs/block-group.h
+++ b/fs/btrfs/block-group.h
@@ -88,6 +88,7 @@ enum btrfs_block_group_flags {
BLOCK_GROUP_FLAG_NEW,
BLOCK_GROUP_FLAG_FULLY_REMAPPED,
BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING,
+ BLOCK_GROUP_FLAG_ZONE_FINISHING,
};
enum btrfs_caching_type {
diff --git a/fs/btrfs/zoned.c b/fs/btrfs/zoned.c
index 4dda1a470237..d2c89fa12da9 100644
--- a/fs/btrfs/zoned.c
+++ b/fs/btrfs/zoned.c
@@ -2576,6 +2576,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
(BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_SYSTEM));
struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
enum btrfs_raid_types index;
+ bool bg_ro = false;
bool zones_full;
int failed = 0;
int ret = 0;
@@ -2587,6 +2588,11 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
return 0;
}
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags)) {
+ spin_unlock(&block_group->lock);
+ goto wait_finish;
+ }
+
/* Check if we have unwritten allocated space */
if (is_metadata &&
block_group->start + block_group->alloc_offset > block_group->meta_write_pointer) {
@@ -2611,6 +2617,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
ret = btrfs_inc_block_group_ro(block_group, false);
if (ret)
return ret;
+ bg_ro = true;
/* Ensure all writes in this block group finish */
btrfs_wait_block_group_reservations(block_group);
@@ -2634,6 +2641,12 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
return 0;
}
+ if (test_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ &block_group->runtime_flags)) {
+ spin_unlock(&block_group->lock);
+ goto wait_finish;
+ }
+
if (block_group->reserved ||
test_bit(BLOCK_GROUP_FLAG_ZONED_DATA_RELOC,
&block_group->runtime_flags)) {
@@ -2643,7 +2656,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
}
}
- clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
+ set_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING, &block_group->runtime_flags);
/*
* If the block group is allocated up to the zone capacity,
* everything is written and the zones should be full already. A
@@ -2702,8 +2715,13 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
btrfs_dec_block_group_ro(block_group);
spin_lock(&fs_info->zone_active_bgs_lock);
+ spin_lock(&block_group->lock);
+ clear_bit(BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE, &block_group->runtime_flags);
ASSERT(!list_empty(&block_group->active_bg_list));
list_del_init(&block_group->active_bg_list);
+ clear_and_wake_up_bit(BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ &block_group->runtime_flags);
+ spin_unlock(&block_group->lock);
spin_unlock(&fs_info->zone_active_bgs_lock);
/* For active_bg_list */
@@ -2712,6 +2730,15 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
clear_and_wake_up_bit(BTRFS_FS_NEED_ZONE_FINISH, &fs_info->flags);
return ret;
+
+wait_finish:
+ wait_on_bit_io(&block_group->runtime_flags,
+ BLOCK_GROUP_FLAG_ZONE_FINISHING,
+ TASK_UNINTERRUPTIBLE);
+ if (bg_ro)
+ btrfs_dec_block_group_ro(block_group);
+
+ return 0;
}
int btrfs_zone_finish(struct btrfs_block_group *block_group)
--
2.39.5
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 10:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:18 [PATCH v5 0/2] btrfs: zoned: fix activation deadlock and zone finishing race Dongjiang Zhu
2026-12-03 10:18 ` [PATCH v5 1/2] btrfs: zoned: remove obsolete non-data block group activation helper Dongjiang Zhu
2026-12-03 10:18 ` [PATCH v5 2/2] btrfs: zoned: serialize zone finishing per block group Dongjiang Zhu
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.