From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
Johannes Thumshirn <johannes.thumshirn@wdc.com>,
Filipe Manana <fdmanana@suse.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH] btrfs: zoned: fix deadlock between freeze and RECLAIM_ZONES
Date: Mon, 23 Nov 2026 01:24:59 +0000 [thread overview]
Message-ID: <20261123012459.436826-1-mc@raidlab.org> (raw)
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
reply other threads:[~2026-11-23 1:25 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123012459.436826-1-mc@raidlab.org \
--to=mc@raidlab.org \
--cc=dsterba@suse.com \
--cc=fdmanana@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®