mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] btrfs: zoned: fix deadlock between freeze and RECLAIM_ZONES
@ 2026-11-23  1:24 Miroslav Cimerman
  0 siblings, 0 replies; only message in thread
From: Miroslav Cimerman @ 2026-11-23  1:24 UTC (permalink / raw)
  To: linux-btrfs
  Cc: David Sterba, Chris Mason, Johannes Thumshirn, Filipe Manana,
	linux-kernel

RECLAIM_ZONES calls btrfs_reclaim_block_groups() to serve data space
tickets. That function takes the super write lock with
sb_start_write(). A writer waiting on such a ticket holds that lock
already, so a freeze waits for the writer while the reclaim waits for
the freeze:

  writer             freeze               async data reclaim
  ------             ------               ------------------
  sb_start_write()
  wait on a data
  space ticket
                     freeze_super()
                       wait for writers
                                          flush_space(RECLAIM_ZONES)
                                            btrfs_reclaim_block_groups()
                                              sb_start_write()
                                              wait for the freeze

Nothing serves the ticket, and all three wait.

Take the lock with sb_start_write_trylock() and skip the reclaim while
the filesystem is being frozen or is frozen. The space flushing goes on
with its next states. If they free nothing, the ticket fails with
ENOSPC, the same as when the reclaim is skipped for a running balance.
The block groups stay on the reclaim list, and the cleaner queues the
reclaim work again after the thaw.

The reclaim worker, which also runs on non-zoned filesystems, skips
during a freeze as well instead of waiting for the thaw. Commit
ca5e4ea0beae ("btrfs: zoned: mark relocation as writing") added the lock
to keep relocations out of a frozen filesystem, and skipping them still
does that.

Fixes: e2a7fd22378f ("btrfs: zoned: add zone reclaim flush state for DATA space_info")
CC: stable@vger.kernel.org # 7.1+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Sashiko pointed at this deadlock in its review of "btrfs: wait for
block group relocation on remount read-only". It was checked,
reproduced and fixed with the help of an LLM, which also prepared the
commit message. I reviewed everything. It needed a test-only delay
before RECLAIM_ZONES to reproduce: with the delay, 2 of 3 runs hung
without the patch and none of 3 with it.

This also removes the lockdep dependency Sashiko reported on v2 of that
patch, from s_umount through cancel_work_sync() of the reclaim works to
the super write lock. close_ctree() cancels both reclaim works and
btrfs_remount_ro() the async data reclaim work, with s_umount held. With
lockdep, after a reclaim, a remount and an unmount, /proc/lockdep showed
the reclaim works depending on sb_writers without this patch, and no
such dependency with it. Neither run printed a lockdep warning.

Johannes's pending "btrfs: zoned: wait for a running block group reclaim
in RECLAIM_ZONES" (v7 15/27) adds a flush_work() of the reclaim worker
to RECLAIM_ZONES. The worker calls btrfs_reclaim_block_groups() too and
blocks on the freeze the same way, so that flush_work() can hang as
well. In my test with that patch applied, the flusher got past the
flush_work() and hung in the direct call, 2 of 3 runs. With the trylock
none of 3 hung.

 fs/btrfs/block-group.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index a40eedc65dd2..0736132624f8 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -2098,10 +2098,16 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
 	if (!btrfs_should_reclaim(fs_info))
 		return;
 
-	guard(super_write)(fs_info->sb);
+	/*
+	 * Skip the reclaim while the filesystem is frozen or being frozen.
+	 * RECLAIM_ZONES calls this for a space ticket, and a writer waiting
+	 * on the ticket holds off the freeze, which holds off sb_start_write().
+	 */
+	if (!sb_start_write_trylock(fs_info->sb))
+		return;
 
 	if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
-		return;
+		goto out;
 
 	/*
 	 * Long running balances can keep us blocked here for eternity, so
@@ -2109,7 +2115,7 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
 	 */
 	if (!mutex_trylock(&fs_info->reclaim_bgs_lock)) {
 		btrfs_exclop_finish(fs_info);
-		return;
+		goto out;
 	}
 
 	spin_lock(&fs_info->unused_bgs_lock);
@@ -2159,6 +2165,8 @@ void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limi
 	list_splice_tail(&retry_list, &fs_info->reclaim_bgs);
 	spin_unlock(&fs_info->unused_bgs_lock);
 	btrfs_exclop_finish(fs_info);
+out:
+	sb_end_write(fs_info->sb);
 }
 
 void btrfs_reclaim_bgs_work(struct work_struct *work)

base-commit: 65101fe10b3008186b813b628ee3a611b0e70ce6
-- 
2.53.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-23  1:25 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23  1:24 [PATCH] btrfs: zoned: fix deadlock between freeze and RECLAIM_ZONES Miroslav Cimerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®