From: Miroslav Cimerman <mc@raidlab.org>
To: linux-btrfs@vger.kernel.org
Cc: David Sterba <dsterba@suse.com>, Chris Mason <mason@kernel.org>,
Naohiro Aota <naohiro.aota@wdc.com>,
Johannes Thumshirn <johannes.thumshirn@wdc.com>,
linux-kernel@vger.kernel.org
Subject: [PATCH] btrfs: start writeback of relocated data before dirty throttling
Date: Wed, 18 Nov 2026 12:16:31 +0000 [thread overview]
Message-ID: <20261118121631.6644-1-mc@raidlab.org> (raw)
When over the dirty limit, relocate_one_folio() waits in
balance_dirty_pages() for the flusher to write back enough. The flusher
skips a filesystem whose s_umount is held for writing, but an unmount
and a remount read-only hold it while they wait for a relocation by the
reclaim work to finish: close_ctree() in cancel_work_sync(), and
btrfs_remount_ro() in btrfs_commit_super() for the cleaner_mutex the
relocation holds. Nothing else is sure to write back the relocated
data, and both hang:
task:kworker/u16:1 state:D
Workqueue: events_unbound btrfs_reclaim_bgs_work
balance_dirty_pages
relocate_file_extent_cluster
relocate_data_extent.constprop.0
relocate_block_group
btrfs_relocate_block_group
btrfs_relocate_chunk
btrfs_reclaim_block_groups
task:umount state:D
cancel_work_sync
close_ctree
generic_shutdown_super
kill_anon_super
btrfs_kill_super
The same goes for a resumed balance, which both wait for in
btrfs_pause_balance(), and for the relocation of the zoned
RECLAIM_ZONES space flushing, whose work both cancel.
When balance_dirty_pages() would make us wait, start writeback of the
data relocation inode first, then wait as before. Writeback errors are
still reported by btrfs_wait_ordered_range() at the end of the stage.
Fixes: 18bb8bbf13c1 ("btrfs: zoned: automatically reclaim zones")
CC: stable@vger.kernel.org # 6.1+
Signed-off-by: Miroslav Cimerman <mc@raidlab.org>
---
Found by an LLM while testing "btrfs: wait for block group relocation
on remount read-only" with a low dirty limit. The fix and the commit
message were prepared with the help of an LLM, which also ran the tests
below. I reviewed everything.
Tested on btrfs for-next (6f4bbde7e036) in QEMU with KASAN, one zoned
null_blk device mounted with commit=1, the reclaim thresholds lowered
and vm.dirty_bytes at 2 MiB. An unmount while the reclaim work
relocates hangs as above without the patch (5 of 5) and takes 0.2 s
with it (8 of 8, 3 of them with the default commit interval instead).
A remount read-only retried in a loop hangs without it (3 of 3) and
goes through with it (3 of 3); the relocation then still writes after
the remount, which "btrfs: wait for block group relocation on remount
read-only" fixes. With both patches, remount, remount with an
immediate unmount, and unmount are clean (9 of 9).
A balance of 200 MiB with a slow writer on another filesystem at the
dirty limit takes 3.4 to 4.2 s with the patch (4 runs) and 3.0 to
4.3 s without it (12 runs), 2.2 to 2.3 s without the writer either
way. Writing back each folio and waiting for it, instead of throttling,
also fixes the hang, but made that balance take 22.7 s.
The resumed balance and RECLAIM_ZONES cases are from reading the code:
a resumed balance can hang this way since 837d5b6e46d1 ("Btrfs: allow
for pausing restriper"), and the Fixes tag names the reclaim work,
which the reproducer uses. balance_dirty_pages_ratelimited_flags() is
exported since 6.1, hence the stable tag. The cleaner's autodefrag
throttles the same way in btrfs_defrag_file() and is not changed here.
fs/btrfs/relocation.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 072bc943d62e..6252608b0983 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -3045,7 +3045,19 @@ static int relocate_one_folio(struct reloc_control *rc,
folio_unlock(folio);
folio_put(folio);
- balance_dirty_pages_ratelimited(inode->i_mapping);
+ /*
+ * Start writeback of our pages ourselves before balance_dirty_pages()
+ * makes us wait for the flusher. The flusher skips the filesystem
+ * while s_umount is held, and both a remount read-only and an unmount
+ * hold it while they wait for a relocation by the reclaim work to
+ * finish. Writeback errors are found by btrfs_wait_ordered_range()
+ * at the end of the stage, as with the flusher.
+ */
+ if (balance_dirty_pages_ratelimited_flags(inode->i_mapping,
+ BDP_ASYNC) == -EAGAIN) {
+ filemap_flush(inode->i_mapping);
+ balance_dirty_pages_ratelimited(inode->i_mapping);
+ }
btrfs_throttle(fs_info);
if (btrfs_should_cancel_balance(fs_info))
ret = -ECANCELED;
--
2.53.0
reply other threads:[~2026-11-18 12:19 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=20261118121631.6644-1-mc@raidlab.org \
--to=mc@raidlab.org \
--cc=dsterba@suse.com \
--cc=johannes.thumshirn@wdc.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
--cc=naohiro.aota@wdc.com \
/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®