btrfs: start writeback of relocated data before dirty throttling
PATCH 1/1 btrfs: start writeback of relocated data before dirty throttling
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.
---
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(-)
fs/btrfs/relocation.c +13 -1
@@ -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 ;
Signature · 1 line
2.53.0