Skip to contentSkip to conversation

btrfs: zoned: fix deadlock between freeze and RECLAIM_ZONES

PATCH New Miroslav Cimerman 1 participant 1 message Nov 23 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: zoned: fix deadlock between freeze and RECLAIM_ZONES

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")

---
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(-)
fs/btrfs/block-group.c +11 -3
index a40eedc65dd2..0736132624f8 100644
@@ -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

Signature · 1 line
2.53.0