| directory | patches | on | authors | notes |
|---|---|---|---|---|
| `logical-ino-costs` | 3 | | Zygo, Kai Krakow | patch 3 is Kai's, already posted by him |
-| `balance-resume` | 4 | | Zygo | patch 1 fixes a pre-existing limit filter bug (independent); patch 1: Fixes tag, no Cc stable (Zygo 11-11); patch 4 optional, offered for rejection |
-| `remap-balance-leak` | 1 | | Zygo | was balance-resume patch 1 until 2026-11-08; independent of it |
+| `balance-resume` | 8 | | Zygo | fixes first: patch 1 a pre-existing limit filter bug (independent), patches 2-4 the METADATA_REMAP fixes (the former `remap-balance-leak` series, merged in 2026-11-11), all with Fixes tags; no Cc stable for any of them (patch 1: Zygo 11-11; 2-4 and 7: the remap tree is experimental, Zygo 11-11); patch 8 optional, offered for rejection; cover: DRAFT for Zygo (rm1111, 8 patches) |
| `compress-fixes` | 5 | | Zygo | |
| `deadlock-fixes` | 3 | | Zygo | the relock fix is folded into patch 2 (2026-10-30) |
| `dedupe-fixes` | 1 | | Zygo | |
cd ~/share/linux
P=~/share/linux-upstream
B=bf921b86bdf2
-ORDER="logical-ino-costs balance-resume remap-balance-leak compress-fixes deadlock-fixes dedupe-fixes
+ORDER="logical-ino-costs balance-resume compress-fixes deadlock-fixes dedupe-fixes
delalloc-fixes dev-extent-defrag prealloc-size raid56-fixes read-repair-fixes
raid56-scrub-replace reclaim-fixes stripe-alloc low-space
allocator-hints read-policies replace-fixes dev-stats resize-missing
* Information about one device.
*
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index a7a37ffe79764c9265eec2ec9af18dcf1a0d45e3..3f44fa89ad53aaeb2d6bc8354047133886c64ff9 100644
+index aa0f1acf682e2774aaa6e1fb8b76e3d7dc1907c2..2d714eb7a30c086d0ed339d526ad842d2ed3bfa2 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
-@@ -7126,7 +7126,293 @@ bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical)
+@@ -7156,7 +7156,293 @@ bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical)
return ret;
}
-From 9732a4fa540f1db0c048915a83d5606dd5615a0e Mon Sep 17 00:00:00 2001
+From 02f11b2bbcbcf475062e037c537c5f16cf4cd088 Mon Sep 17 00:00:00 2001
From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 10 Nov 2026 03:08:20 -0500
-Subject: [PATCH 0/4] btrfs: balance resume selects what the original request selected
+Date: Wed, 11 Nov 2026 20:58:43 -0500
+Subject: [PATCH 0/8] btrfs: balance resume selects what the original request selected
To: linux-btrfs@vger.kernel.org
When a paused or interrupted balance is resumed, the kernel alters the
existing block group, a `vrange` filter can be added, or the end of an
existing vrange limited, without changing any other filter parameter.
Adding vrange to any balance filter makes it idempotent, except for the
-limit filter which is handled in the third patch. This eliminates the
+limit filter which is handled in patch 6. This eliminates the
need for guesswork, and with the floor described below, this guarantees a
correct balance resume result.
still counting them against the limit, and relocates 2M + 1 - L chunks
instead of M, or none when L >= 2M + 1. For example, `limit=9` with 7
matching data chunks relocates 6. The patch applies the minimum only to
-a limit range. The bug dates from the addition of the limit range in
-v4.4, and the patch does not depend on the rest of the series.
+a limit range. It fixes 12907fc79818 ("btrfs: extend balance filter
+limit to take minimum and maximum"), v4.4, and does not depend on the
+rest of the series.
-Patch 2 in this series removes the surprising heuristics and replaces
+Patches 2-4 fix the handling of METADATA_REMAP chunks in balance, on
+filesystems with the remap tree. __btrfs_balance() does not relocate
+them during its chunk walk: should_balance_chunk() selects them as
+metadata chunks (the metadata type flag, the metadata filters, and the
+metadata limit or limit_max, which it counts down when it selects one),
+and the walk puts them on a local list and balances them all after it
+has walked the other chunks, in balance_remap_chunks(). The three
+patches fix 81e5a4551c32 ("btrfs: allow balancing remap tree"), v7.0.
+The remap tree is still experimental (CONFIG_BTRFS_EXPERIMENTAL), so
+none of them is marked for stable.
+
+Patch 2 frees the list entries when the walk leaves early through the
+error label (a pause or cancel request, or an error during the walk).
+Before it, only balance_remap_chunks() freed them, so every entry queued
+so far leaked each time a balance was paused, canceled or failed.
+Nothing else needs undoing: the block group lookup, its reference and
+the read-only count are all taken later, in balance_remap_chunks().
+
+Patch 3 gives the dropped chunks' metadata limit back at the same label.
+A balance paused after the walk has queued a remap chunk otherwise keeps
+the limit used for a chunk it never balanced, and the resume on the same
+mount balances fewer chunks than asked: with -mlimit=1..1 and the remap
+chunk selected as the one metadata chunk, the resumed balance balances
+no metadata chunk at all.
+
+Patch 4 makes the limit_min check count METADATA_REMAP chunks as
+metadata. The counting pass counts them in count_meta, but the check in
+the second pass is the only place that tests the raw METADATA bit, which
+a METADATA_REMAP chunk does not have, so with -mlimit=MIN..MAX and fewer
+than MIN matching metadata chunks the balance skips every METADATA chunk,
+as asked, but still relocates the remap chunk. With the patch, remap
+chunks count as metadata in both passes, and the limit bounds the total
+number of block groups relocated.
+
+Patch 5 in this series removes the surprising heuristics and replaces
them with a `vrange` filter, leaving the remaining filter parameters
unmodified. Before the balance item is written, the vrange filter of
each chunk type is limited to the addresses below the next chunk to be
introduced in 2012, so there is no compatibility issue arising from
its use.
-Patch 3 handles the limit filter. A limit counts chunks instead of
+Patch 6 handles the limit filter. A limit counts chunks instead of
selecting them by address, so no vrange can make it idempotent. Balance
counts the limit down in memory as it selects chunks, and a resume after
a pause continues with what is left, but the balance item keeps the
again, and can relocate up to the full limit a second time. A
`limit=min..max` range has a second problem: the resumed balance counts
only the chunks that are left, which can be fewer than min, and then it
-relocates nothing. Patch 3 counts each chunk the balance processes,
+relocates nothing. Patch 6 counts each chunk the balance processes,
relocated or left in place by ENOSPC or an active swapfile (as the
in-memory count always did), against the limit, or against both ends of
a range (min stops at 0), and writes the remaining limits to the balance
can make the resumed balance relocate one chunk more than requested,
never fewer.
+Patch 7 keeps the queued METADATA_REMAP chunks out of those writes.
+should_balance_chunk() counts a remap chunk down from the metadata limit
+when the walk queues it, so any limited chunk processed after that wrote
+the item with the queued chunk counted as done. If the balance stopped
+before the end of the walk (a pause, an unmount or a crash), the queue
+was gone, and the balance resumed from the item had too little left of
+its metadata limit to balance the chunks it never balanced. The item is
+now written with the queued chunks added back to the metadata limit; the
+in-memory limit is unchanged, so the walk still stops selecting chunks
+once the limit is used up.
+
If one of the new balance item writes fails (the one-time write-back of
-a bounded item at resume in patch 2, or a limit update after a chunk in
-patch 3), the balance ends with the error and its item is deleted, the
+a bounded item at resume in patch 5, or a limit update after a chunk in
+patch 6), the balance ends with the error and its item is deleted, the
same as for any other error at that point in the existing code. We
considered keeping the item and leaving the balance paused so the
resume could be retried, but chose to stay consistent with the existing
failed write-back at resume is logged, because the mount-time resume
thread would otherwise drop the error silently.
-Patch 4 is optional. I offer it for completeness, but there's a tradeoff
+Patch 8 is optional. I offer it for completeness, but there's a tradeoff
that might not be worth the cost. It can be dropped without breaking
the rest of the series.
A balance item written by an older kernel has no vrange, so with patch
-2, a balance that starts on an old kernel and resumes on a new kernel
+5, a balance that starts on an old kernel and resumes on a new kernel
restarts from the beginning, redoing much of the work already done.
That behavior may be an unpleasant surprise for some users, so for each
-chunk type without a `vrange` filter, patch 4 resumes with the old
+chunk type without a `vrange` filter, patch 8 resumes with the old
usage=90 and convert-soft heuristics. The cost is keeping a layer of
surprising behavior and code in the kernel to avoid a one-time issue
that affects few users, is relatively easy to recover from, and will
chunk. A limited balance is also recorded with dm-log-writes and
replayed to every flush point: in every replayed state with a balance
item, the chunks already relocated plus the remaining limit must equal
- the request, or exceed it by one. Without patch 3, the resume after an
+ the request, or exceed it by one. Without patch 6, the resume after an
unmount relocates up to the full limit again, and the `limit=M..M`
resumes relocate nothing. Single limits larger than the number of
matching chunks (`limit=M+2`, `limit=2M` and `limit=2M+1`) must
filesystem needs an --enable-experimental btrfs-progs with a mkfs fix
for remap-tree to fix a NULL deref bug.
+ - remap-balance-limit-test.sh (remap-tree filesystems, the limits of
+ patches 3, 4 and 7, with a kprobe on btrfs_inc_block_group_ro() to see
+ which block groups balance set read-only): `-mlimit=50..60` on a fresh
+ filesystem with fewer metadata chunks than that must relocate nothing
+ and leave the remap chunk alone; `-mlimit=1..1000` must balance it;
+ `-mlimit=1..1` with one METADATA chunk and the remap chunk matching
+ must relocate exactly one of them. A `-dlimit=1..1000 -mlimit=1..1`
+ balance with the remap chunk queued, paused while it relocates data
+ chunks below it, must show the metadata limit 1..1 and balance the
+ remap chunk when resumed on the same mount; unmounted instead, the
+ balance item's metadata limit_max must read 1, and the resume at mount
+ must balance the remap chunk. Without the series the limit_min case
+ relocates the remap chunk and the paused-on-mount case shows 1..0 and
+ leaves it; the item case fails only with patch 6 and without patch 7.
+
+ - remap-balance-leak-test.sh (remap-tree filesystems, patch 2): traces
+ the allocations and frees of the list entries while a full balance is
+ paused after its walk has queued a remap chunk, then remounted and
+ resumed. With the patch every entry is freed, the paused walk's at
+ the error label; without it the paused walk's entry is never freed.
+ Only the pause is driven; cancel and the error exits leave through the
+ same label.
+
- balance-resume-legacy-test.sh: data balances paused by a stock 6.12
kernel, then resumed on the patched kernel. Every item must show,
already before the resume, a vrange ending at the end of the last
chunk: added to a plain or convert item, lowered from a user-given
- end of 1 TiB or U64_MAX. With patch 4 a plain item also resumes with
+ end of 1 TiB or U64_MAX. With patch 8 a plain item also resumes with
usage=90 and a convert item with soft, and an item with a user-given
vrange, converting or not, with neither. The resumed balance is
paused again and the filesystem remounted: it must resume with the
resume of a 1 TiB item allocates its chunks from 1 TiB up. A balance
started on the patched kernel must get the vrange and no usage filter.
-Zygo Blaxell (4):
+Zygo Blaxell (8):
btrfs: apply the balance limit_min filter only to a limit range
+ btrfs: free queued remap chunks when balance bails out
+ btrfs: give back the limit of queued remap chunks when balance bails
+ out
+ btrfs: count METADATA_REMAP chunks as metadata in the balance
+ limit_min check
btrfs: replace the balance resume heuristics with a vrange bound
btrfs: write the remaining balance limits back to the balance item
+ btrfs: do not write queued remap chunks as processed to the balance
+ item
btrfs: keep the old resume filters for balance items without a vrange
fs/btrfs/fs.h | 6 +
- fs/btrfs/volumes.c | 296 ++++++++++++++++++++++++++++++++++++---------
+ fs/btrfs/volumes.c | 334 +++++++++++++++++++++++++++++++++++++--------
fs/btrfs/volumes.h | 6 +
- 3 files changed, 253 insertions(+), 55 deletions(-)
+ 3 files changed, 291 insertions(+), 55 deletions(-)
--
From 53738adfdeb5ceba662ef852896dd6b4ea81d4ee Mon Sep 17 00:00:00 2001
From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
Date: Mon, 9 Nov 2026 23:34:11 -0500
-Subject: [PATCH 1/4] btrfs: apply the balance limit_min filter only to a limit
+Subject: [PATCH 1/8] btrfs: apply the balance limit_min filter only to a limit
range
To: linux-btrfs@vger.kernel.org
--- /dev/null
+From f792c9df39fbcb95e764aa82c86b6c788cb9cef0 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 13 Oct 2026 01:05:21 -0400
+Subject: [PATCH 2/8] btrfs: free queued remap chunks when balance bails out
+To: linux-btrfs@vger.kernel.org
+
+__btrfs_balance() queues METADATA_REMAP chunks on a local list and
+balances them only after the main loop, in balance_remap_chunks(), which
+also frees the entries. Every path that leaves the loop through the
+error label skips that call: a pause or cancel request, a chunk tree
+search error, should_balance_chunk() failing, btrfs_may_alloc_data_chunk()
+or relocation errors, or the allocation of a later entry failing. The
+entries queued so far are leaked.
+
+Free the leftover entries at the error label. Nothing else needs undoing:
+the block group lookup, the reference and the read-only count are all
+taken inside balance_remap_chunks(), so a queued entry holds no block
+group and none is left read-only.
+
+Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
+---
+ fs/btrfs/volumes.c | 8 ++++++++
+ 1 file changed, 8 insertions(+)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 8cf1645f899a6..a0d7f4faf23db 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4769,6 +4769,14 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+ }
+ }
+ error:
++ /* Entries left over when the loop bailed out before balancing them. */
++ while (!list_empty(&remap_chunks)) {
++ rci = list_first_entry(&remap_chunks, struct remap_chunk_info,
++ list);
++ list_del(&rci->list);
++ kfree(rci);
++ }
++
+ if (enospc_errors) {
+ btrfs_info(fs_info, "%d enospc errors during balance",
+ enospc_errors);
+--
+2.47.3
+
+++ /dev/null
-From cd2e90d19d386642d877eb4170bb8f9d97bd8639 Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Wed, 16 Sep 2026 02:29:10 -0400
-Subject: [PATCH 2/4] btrfs: replace the balance resume heuristics with a
- vrange bound
-To: linux-btrfs@vger.kernel.org
-
-When a balance is resumed after a pause or an unmount,
-update_balance_args() adds a usage=90 filter to every chunk type that is
-not being converted. That changes the set of block groups the original
-request selected. It skips full block groups the interrupted balance had
-not reached yet, so the resumed balance can finish without having done
-what was asked, and it selects again any block group the interrupted
-balance created that is less than 90% full, relocating the same data a
-second time. For the chunk types that are being converted it adds the
-soft filter instead, so the resume does not convert again the block
-groups the interrupted balance converted, but it also skips the ones
-that already had the target profile when the balance started, which the
-original request selected.
-
-Replace it with a bound fixed when the balance starts. Before the
-balance item is written, limit the vrange filter of each chunk type to
-the addresses below the next chunk to be allocated: add a 0..end range
-when the request had no vrange filter, or lower the end of the one it
-had. Every block group that existed when the balance started stays
-selected until the balance relocates it.
-
-New chunks are allocated after the last chunk, but relocation can remove
-the highest chunks without allocating any (their data fits in free space
-lower down), and the next chunk would then reuse their addresses, below
-the bound. A resumed balance would select it again, although the
-interrupted one, walking downward, never would have. So while a balance
-item exists, find_next_chunk() never returns an address below the bound:
-an in-memory floor, set when the balance starts and when the balance
-item is recovered at mount, and cleared when the item is deleted. Every
-chunk allocated in that time, relocation targets included, stays outside
-the range, on the first run and after a resume. The gap the floor can
-leave in the logical address space is harmless.
-
-A resume now uses the arguments exactly as they were stored: the bound
-already keeps out every block group the interrupted balance created,
-converted ones included, so neither the usage filter nor the soft filter
-is needed, and update_balance_args() goes away.
-
-The bound does not move as the balance progresses. Block groups the
-balance has relocated are gone, so on resume the walk only revisits the
-chunks it left in place above the point where it stopped (filtered out,
-ENOSPC, active swapfile) and checks them against the filters again.
-Nothing is skipped.
-
-Because the bound is written with the balance item and never changes,
-there is no progress to persist: a resume after an unmount or a crash
-needs no extra writes.
-
-A balance item recovered at mount is limited in the same way before the
-floor is set from it: a vrange 0..end is added to each chunk type that
-has none, and a vrange that ends above the next chunk is lowered to it.
-That covers items written by older kernels, which have no vrange or a
-vrange the user gave, which can end anywhere (a 1 TiB end would put
-every new chunk at 1 TiB and above). A bound written by this kernel
-changes only when relocation removed the highest chunks and allocated
-none above it; no chunk exists between the next chunk and the old end,
-so lowering it drops nothing. When the recovered arguments change,
-they are written back to the balance item once, when the balance
-resumes, so a resume that is interrupted again resumes with the same
-bound and not with a new one that includes the chunks the first resume
-allocated. If that write fails, the balance is not resumed: as for the
-other errors at that point, it ends with the error logged, and its item
-is deleted. Nothing has been relocated yet, so if the item survives (the
-delete fails too, or the system crashes first), the next mount bounds it
-again to the same chunks.
-
-A balance item written by an older kernel therefore resumes with the
-bound and without the usage or soft filter: the resumed balance restarts
-from the beginning, relocating again every block group the request
-selects, including the ones the interrupted run created or converted,
-but it skips none.
-
-The bound is visible: `btrfs balance status -v`, the
-BTRFS_IOC_BALANCE_PROGRESS ioctl and the "balance: start" and "balance:
-resume" log lines show a vrange the user did not give.
-
----
- fs/btrfs/fs.h | 6 ++
- fs/btrfs/volumes.c | 188 +++++++++++++++++++++++++++++++++------------
- fs/btrfs/volumes.h | 6 ++
- 3 files changed, 150 insertions(+), 50 deletions(-)
-
-diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
-index 2dd167c32d612..a42aa4de2bcc6 100644
---- a/fs/btrfs/fs.h
-+++ b/fs/btrfs/fs.h
-@@ -785,6 +785,12 @@ struct btrfs_fs_info {
- atomic_t balance_cancel_req;
- struct btrfs_balance_control *balance_ctl;
- wait_queue_head_t balance_wait_q;
-+ /*
-+ * 0, or the end of the balance's vrange bound: while a balance item
-+ * exists (running or paused) find_next_chunk() allocates at or above
-+ * it, so the balance never selects a chunk it allocated.
-+ */
-+ u64 balance_chunk_floor;
-
- /* Cancellation requests for chunk relocation */
- atomic_t reloc_cancel_req;
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 8cf1645f899a6..ca364971c3828 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -2035,7 +2035,7 @@ static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
- }
- read_unlock(&fs_info->mapping_tree_lock);
-
-- return ret;
-+ return max(ret, READ_ONCE(fs_info->balance_chunk_floor));
- }
-
- static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
-@@ -3970,13 +3970,27 @@ static void btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
- disk->stripes_max = cpu_to_le32(cpu->stripes_max);
- }
-
-+static void set_balance_item(struct extent_buffer *leaf,
-+ struct btrfs_balance_item *item,
-+ const struct btrfs_balance_control *bctl)
-+{
-+ struct btrfs_disk_balance_args disk_bargs;
-+
-+ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
-+ btrfs_set_balance_data(leaf, item, &disk_bargs);
-+ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
-+ btrfs_set_balance_meta(leaf, item, &disk_bargs);
-+ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
-+ btrfs_set_balance_sys(leaf, item, &disk_bargs);
-+ btrfs_set_balance_flags(leaf, item, bctl->flags & ~BTRFS_BALANCE_RESUME);
-+}
-+
- static int insert_balance_item(struct btrfs_fs_info *fs_info,
- struct btrfs_balance_control *bctl)
- {
- struct btrfs_root *root = fs_info->tree_root;
- struct btrfs_trans_handle *trans;
- struct btrfs_balance_item *item;
-- struct btrfs_disk_balance_args disk_bargs;
- struct btrfs_path *path;
- struct extent_buffer *leaf;
- struct btrfs_key key;
-@@ -4005,14 +4019,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
- item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
-
- memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
--
-- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
-- btrfs_set_balance_data(leaf, item, &disk_bargs);
-- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
-- btrfs_set_balance_meta(leaf, item, &disk_bargs);
-- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
-- btrfs_set_balance_sys(leaf, item, &disk_bargs);
-- btrfs_set_balance_flags(leaf, item, bctl->flags);
-+ set_balance_item(leaf, item, bctl);
- out:
- btrfs_free_path(path);
- if (ret == 0)
-@@ -4023,6 +4030,43 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
- return ret;
- }
-
-+/* Write the arguments of a resumed balance to its existing item. */
-+static int update_balance_item(struct btrfs_fs_info *fs_info,
-+ const struct btrfs_balance_control *bctl)
-+{
-+ struct btrfs_root *root = fs_info->tree_root;
-+ struct btrfs_trans_handle *trans;
-+ BTRFS_PATH_AUTO_FREE(path);
-+ struct btrfs_key key;
-+ int ret;
-+
-+ path = btrfs_alloc_path();
-+ if (!path)
-+ return -ENOMEM;
-+
-+ trans = btrfs_start_transaction(root, 0);
-+ if (IS_ERR(trans))
-+ return PTR_ERR(trans);
-+
-+ key.objectid = BTRFS_BALANCE_OBJECTID;
-+ key.type = BTRFS_TEMPORARY_ITEM_KEY;
-+ key.offset = 0;
-+
-+ ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
-+ if (ret > 0)
-+ ret = -ENOENT;
-+ if (ret < 0) {
-+ btrfs_end_transaction(trans);
-+ return ret;
-+ }
-+
-+ set_balance_item(path->nodes[0],
-+ btrfs_item_ptr(path->nodes[0], path->slots[0],
-+ struct btrfs_balance_item), bctl);
-+ btrfs_release_path(path);
-+ return btrfs_commit_transaction(trans);
-+}
-+
- static int del_balance_item(struct btrfs_fs_info *fs_info)
- {
- struct btrfs_root *root = fs_info->tree_root;
-@@ -4064,47 +4108,61 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
- return ret;
- }
-
-+static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
-+{
-+ if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
-+ bargs->flags |= BTRFS_BALANCE_ARGS_VRANGE;
-+ bargs->vstart = 0;
-+ bargs->vend = end;
-+ return true;
-+ }
-+ if (bargs->vend <= end)
-+ return false;
-+ bargs->vend = end;
-+ return true;
-+}
-+
- /*
-- * This is a heuristic used to reduce the number of chunks balanced on
-- * resume after balance was interrupted.
-+ * Limit the request to the chunks that exist now. The bound is written with
-+ * the balance item and never moves, so there is no progress to persist. New
-+ * chunks are allocated after the last chunk, but once relocation has removed
-+ * the highest chunks the last chunk is lower than the bound, so while the
-+ * balance item exists find_next_chunk() also observes balance_chunk_floor:
-+ * every chunk allocated in that time, relocation targets included, stays
-+ * outside the range, on this run and after a resume.
-+ *
-+ * Return true if any of the arguments changed.
- */
--static void update_balance_args(struct btrfs_balance_control *bctl)
-+static bool limit_balance_to_existing_chunks(struct btrfs_fs_info *fs_info,
-+ struct btrfs_balance_control *bctl)
- {
-- /*
-- * Turn on soft mode for chunk types that were being converted.
-- */
-- if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
-- bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
-- if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
-- bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
-- if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
-- bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
-+ const u64 end = find_next_chunk(fs_info);
-+ bool changed = false;
-
-- /*
-- * Turn on usage filter if is not already used. The idea is
-- * that chunks that we have already balanced should be
-- * reasonably full. Don't do it for chunks that are being
-- * converted - that will keep us from relocating unconverted
-- * (albeit full) chunks.
-- */
-- if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
-- !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
-- !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
-- bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
-- bctl->data.usage = 90;
-- }
-- if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
-- !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
-- !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
-- bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
-- bctl->sys.usage = 90;
-- }
-- if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
-- !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
-- !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
-- bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
-- bctl->meta.usage = 90;
-- }
-+ if (bctl->flags & BTRFS_BALANCE_DATA)
-+ changed |= limit_balance_vrange(&bctl->data, end);
-+ if (bctl->flags & BTRFS_BALANCE_METADATA)
-+ changed |= limit_balance_vrange(&bctl->meta, end);
-+ if (bctl->flags & BTRFS_BALANCE_SYSTEM)
-+ changed |= limit_balance_vrange(&bctl->sys, end);
-+ return changed;
-+}
-+
-+/* The highest vrange end of the selected chunk types, 0 if none has one. */
-+static u64 balance_chunk_floor(const struct btrfs_balance_control *bctl)
-+{
-+ u64 floor = 0;
-+
-+ if ((bctl->flags & BTRFS_BALANCE_DATA) &&
-+ (bctl->data.flags & BTRFS_BALANCE_ARGS_VRANGE))
-+ floor = max(floor, bctl->data.vend);
-+ if ((bctl->flags & BTRFS_BALANCE_METADATA) &&
-+ (bctl->meta.flags & BTRFS_BALANCE_ARGS_VRANGE))
-+ floor = max(floor, bctl->meta.vend);
-+ if ((bctl->flags & BTRFS_BALANCE_SYSTEM) &&
-+ (bctl->sys.flags & BTRFS_BALANCE_ARGS_VRANGE))
-+ floor = max(floor, bctl->sys.vend);
-+ return floor;
- }
-
- /*
-@@ -4123,6 +4181,7 @@ static void reset_balance_state(struct btrfs_fs_info *fs_info)
-
- kfree(bctl);
- ret = del_balance_item(fs_info);
-+ WRITE_ONCE(fs_info->balance_chunk_floor, 0);
- if (ret)
- btrfs_handle_fs_error(fs_info, ret, NULL);
- }
-@@ -5093,6 +5152,9 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
- btrfs_bg_type_to_raid_name(data_target));
- }
-
-+ if (!(bctl->flags & BTRFS_BALANCE_RESUME))
-+ limit_balance_to_existing_chunks(fs_info, bctl);
-+
- ret = insert_balance_item(fs_info, bctl);
- if (ret && ret != -EEXIST)
- goto out;
-@@ -5103,11 +5165,26 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
- spin_lock(&fs_info->balance_lock);
- fs_info->balance_ctl = bctl;
- spin_unlock(&fs_info->balance_lock);
-+ WRITE_ONCE(fs_info->balance_chunk_floor,
-+ balance_chunk_floor(bctl));
- } else {
- BUG_ON(ret != -EEXIST);
-- spin_lock(&fs_info->balance_lock);
-- update_balance_args(bctl);
-- spin_unlock(&fs_info->balance_lock);
-+ if (bctl->update_item) {
-+ /*
-+ * Nothing has been relocated yet, so the item on disk,
-+ * bounded again at the next mount, is still right. As
-+ * for the other errors here, the balance ends and its
-+ * item is deleted.
-+ */
-+ ret = update_balance_item(fs_info, bctl);
-+ if (ret) {
-+ btrfs_err(fs_info,
-+ "balance: failed to write the bounded balance item: %d",
-+ ret);
-+ goto out;
-+ }
-+ bctl->update_item = false;
-+ }
- }
-
- ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
-@@ -5260,6 +5337,17 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
- btrfs_balance_sys(leaf, item, &disk_bargs);
- btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
-
-+ /*
-+ * Bound the request as a new balance would be: an item written by an
-+ * older kernel has no vrange, and one a user gave to such a kernel can
-+ * end anywhere. The floor is still 0, so find_next_chunk() returns the
-+ * end of the last chunk, and lowering a vend above it drops no block
-+ * group from the range. The bound is written back to the item when
-+ * the balance resumes, so a resume that is interrupted again keeps it.
-+ */
-+ bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
-+ WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
-+
- /*
- * This should never happen, as the paused balance state is recovered
- * during mount without any chance of other exclusive ops to collide.
-diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
-index 891bc2432870a..e7112257014e8 100644
---- a/fs/btrfs/volumes.h
-+++ b/fs/btrfs/volumes.h
-@@ -665,6 +665,12 @@ struct btrfs_balance_control {
-
- u64 flags;
-
-+ /*
-+ * The arguments recovered at mount were bounded and differ from the
-+ * balance item: write them back when the balance resumes.
-+ */
-+ bool update_item;
-+
- struct btrfs_balance_progress stat;
- };
-
---
-2.47.3
-
--- /dev/null
+From 6cd032e84e26f78ae50022e4b885156cdca277cc Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 11 Nov 2026 00:32:31 -0500
+Subject: [PATCH 3/8] btrfs: give back the limit of queued remap chunks when
+ balance bails out
+To: linux-btrfs@vger.kernel.org
+
+__btrfs_balance() does not relocate METADATA_REMAP chunks in its chunk
+walk: it queues them and balances them all after the walk, in
+balance_remap_chunks(). should_balance_chunk() selects them as metadata
+chunks, and a metadata limit filter counts them down when it selects
+them, like any other chunk.
+
+A pause request ends the walk at the error label, which drops the queue:
+the queued chunks are not balanced, but the metadata limit still counts
+them. The balance control stays in memory for the resume, which then
+sees the chunks again and finds the limit already used up for them. For
+example, with -mlimit=1 and the remap chunk selected as the one metadata
+chunk, a balance paused while it relocates the data chunks below the
+remap chunk relocates no metadata chunk at all after the resume.
+
+Add the dropped chunks back to the metadata limit (limit, or limit_max of
+a range, whichever should_balance_chunk() counted down) at the error
+label. For the other ways out through the label (cancel, errors) the
+balance ends and the count does not matter.
+
+Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
+---
+ fs/btrfs/volumes.c | 13 ++++++++++++-
+ 1 file changed, 12 insertions(+), 1 deletion(-)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index a0d7f4faf23db..79df7e08b4302 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4769,7 +4769,18 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+ }
+ }
+ error:
+- /* Entries left over when the loop bailed out before balancing them. */
++ /*
++ * Entries left over when the loop bailed out before balancing them.
++ * should_balance_chunk() took each of them from the metadata limit
++ * when it selected them. Give that back, so a balance resumed after
++ * a pause still processes as many chunks as it was asked to.
++ */
++ if (!list_empty(&remap_chunks)) {
++ if (bctl->meta.flags & BTRFS_BALANCE_ARGS_LIMIT)
++ bctl->meta.limit += num_remap_chunks;
++ else if (bctl->meta.flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
++ bctl->meta.limit_max += num_remap_chunks;
++ }
+ while (!list_empty(&remap_chunks)) {
+ rci = list_first_entry(&remap_chunks, struct remap_chunk_info,
+ list);
+--
+2.47.3
+
+++ /dev/null
-From 40ddcea6883e9fdd6232327fd6c69e1a15f4d638 Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Sun, 8 Nov 2026 22:24:56 -0500
-Subject: [PATCH 3/4] btrfs: write the remaining balance limits back to the
- balance item
-To: linux-btrfs@vger.kernel.org
-
-The limit filters are counted down in memory: should_balance_chunk()
-decrements limit, or limit_max of a limit_min..limit_max range, for every
-chunk that passes the other filters, and __btrfs_balance() restores them
-after its counting pass. A balance resumed after a pause continues with
-what is left. The balance item is never updated, so a balance resumed
-after an unmount or a crash starts again from the limits that were given:
-every resume can relocate up to limit more chunks. The minimum shifts
-too: the counting pass of the resume sees only the chunks that are left,
-which can be fewer than limit_min, and the resume then relocates nothing.
-
-Count a chunk against both ends of the range, and write the remaining
-limits to the balance item, once the chunk has been processed. limit and
-limit_max are still decremented when the chunk is selected, as before,
-and limit_min is now decremented (down to 0) when it has been processed,
-after the limit_min check has let it through, so limit_min means the
-remaining minimum, on disk and in memory. A resume after an unmount or a
-crash then continues from the remaining limits, like a resume after a
-pause, and its limit_min check compares the chunks that are left with the
-minimum that is left. The values written are those of the real pass:
-the counting pass returns to the loop before any chunk is processed, and
-its decrements are undone before the real pass starts.
-
-A chunk counts as processed when its relocation has returned for that
-chunk: relocated, or left in place by ENOSPC or an active swapfile. This
-is what the in-memory limit has always counted, so a resume after an
-unmount or a crash now behaves exactly like a resume after a pause. A
-pause cannot stop a chunk halfway: the pause request is checked between
-chunks, so the chunk being relocated is processed and written first. A
-cancel or a fatal signal can stop a relocation, but it also ends the
-balance and deletes the item. If the item cannot be written, the balance
-stops with the error, like any other error in the loop, and its item is
-deleted; if the item survives that (an aborted transaction), it holds
-the last committed limits, within the bound below.
-
-The update takes its own handle on the running transaction once the
-relocation has returned, and ends it without committing. The
-transaction that removed the relocated chunk has ended by then, and is
-usually still the running one, so both changes usually commit together,
-and the update can never commit before the removal. A crash in between
-loses the count of that one chunk, and the resumed balance can relocate
-one chunk more than requested, never one fewer. Making the update part
-of the removal's transaction would mean passing the balance into
-btrfs_relocate_chunk(), which device shrink and removal also use.
-
-Only chunk types with a limit filter write the item, once per processed
-chunk; a balance without limits makes no more writes than before. The
-remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
-already returned them during the balance and now returns the same values
-after a remount, and the "balance: resume" log line shows them.
-
----
- fs/btrfs/volumes.c | 54 +++++++++++++++++++++++++++++++++++++++++++---
- 1 file changed, 51 insertions(+), 3 deletions(-)
-
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index ca364971c3828..0f6ae59dc4b70 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -4030,9 +4030,13 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
- return ret;
- }
-
--/* Write the arguments of a resumed balance to its existing item. */
-+/*
-+ * Write the arguments of a balance to its existing item. With @commit false
-+ * the update joins the running transaction and is committed with it.
-+ */
- static int update_balance_item(struct btrfs_fs_info *fs_info,
-- const struct btrfs_balance_control *bctl)
-+ const struct btrfs_balance_control *bctl,
-+ bool commit)
- {
- struct btrfs_root *root = fs_info->tree_root;
- struct btrfs_trans_handle *trans;
-@@ -4064,6 +4068,8 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
- btrfs_item_ptr(path->nodes[0], path->slots[0],
- struct btrfs_balance_item), bctl);
- btrfs_release_path(path);
-+ if (!commit)
-+ return btrfs_end_transaction(trans);
- return btrfs_commit_transaction(trans);
- }
-
-@@ -4486,6 +4492,44 @@ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *
- return true;
- }
-
-+/*
-+ * The limit filters count a chunk when should_balance_chunk() selects it, so
-+ * it counts whether its relocation succeeds or leaves it in place (ENOSPC, an
-+ * active swapfile). Once the chunk has been processed, count it against
-+ * limit_min as well, and write the remaining limits to the balance item, so a
-+ * balance resumed after an unmount or a crash processes only what is left of
-+ * the request, as one resumed after a pause does. Chunk types without a limit
-+ * filter write nothing.
-+ */
-+static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
-+{
-+ struct btrfs_balance_control *bctl = fs_info->balance_ctl;
-+ struct btrfs_balance_args *bargs;
-+
-+ if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
-+ bargs = &bctl->data;
-+ else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
-+ bargs = &bctl->sys;
-+ else
-+ bargs = &bctl->meta;
-+
-+ if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
-+ if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE))
-+ return 0;
-+ if (bargs->limit_min > 0)
-+ bargs->limit_min--;
-+ }
-+
-+ /*
-+ * A relocated chunk was removed in a transaction that has ended but
-+ * may not have committed. The update usually joins that transaction
-+ * and never lands in an earlier one, so a crash can lose the count of
-+ * the last relocated chunk, and the resumed balance can then relocate
-+ * one chunk more than requested, but never one fewer.
-+ */
-+ return update_balance_item(fs_info, bctl, false);
-+}
-+
- struct remap_chunk_info {
- struct list_head list;
- u64 offset;
-@@ -4803,6 +4847,10 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
- bctl->stat.completed++;
- spin_unlock(&fs_info->balance_lock);
- }
-+
-+ ret = count_limited_chunk(fs_info, chunk_type);
-+ if (ret)
-+ goto error;
- loop:
- if (found_key.offset == 0)
- break;
-@@ -5176,7 +5224,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
- * for the other errors here, the balance ends and its
- * item is deleted.
- */
-- ret = update_balance_item(fs_info, bctl);
-+ ret = update_balance_item(fs_info, bctl, true);
- if (ret) {
- btrfs_err(fs_info,
- "balance: failed to write the bounded balance item: %d",
---
-2.47.3
-
--- /dev/null
+From 23be6dae21f0079afea51a7dc23714a2b3bc6d1b Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 11 Nov 2026 00:32:39 -0500
+Subject: [PATCH 4/8] btrfs: count METADATA_REMAP chunks as metadata in the
+ balance limit_min check
+To: linux-btrfs@vger.kernel.org
+
+should_balance_chunk() already treats a METADATA_REMAP chunk as a
+metadata chunk: the metadata type flag selects it, the metadata filters
+apply to it, and it is charged against the metadata limit or limit_max
+when it is selected, so the remap chunks queued by __btrfs_balance()
+stay within the MAX of -mlimit=MIN..MAX. The counting pass of
+__btrfs_balance() also counts it in count_meta.
+
+The limit_min check in the second pass is the only place that tests the
+raw METADATA bit, which a METADATA_REMAP chunk does not have. With
+-mlimit=MIN..MAX and fewer than MIN matching metadata chunks, the
+balance skips every METADATA chunk, as asked, but still relocates the
+METADATA_REMAP chunk.
+
+Test the METADATA_REMAP bit in that check as well, so that remap chunks
+count as metadata in both passes and the limit bounds the total number
+of block groups relocated.
+
+Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
+---
+ fs/btrfs/volumes.c | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 79df7e08b4302..7193b1ab9636a 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4680,7 +4680,8 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+ /* Apply the limit_min filter of a limit range. */
+ if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
+ chunk_limit_min_filter(&bctl->data, count_data)) ||
+- ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
++ ((chunk_type & (BTRFS_BLOCK_GROUP_METADATA |
++ BTRFS_BLOCK_GROUP_METADATA_REMAP)) &&
+ chunk_limit_min_filter(&bctl->meta, count_meta)) ||
+ ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
+ chunk_limit_min_filter(&bctl->sys, count_sys))) {
+--
+2.47.3
+
+++ /dev/null
-From 9732a4fa540f1db0c048915a83d5606dd5615a0e Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 13 Oct 2026 14:24:01 -0400
-Subject: [PATCH 4/4] btrfs: keep the old resume filters for balance items
- without a vrange
-To: linux-btrfs@vger.kernel.org
-
-This patch is optional. It is offered so that reviewers can take it or
-drop it; the author's preference is to drop it. The previous patches do
-not depend on it.
-
-A balance item written by a kernel without the vrange bound has no vrange
-filter. With the previous patch alone, a balance that was paused or
-interrupted on such a kernel and is resumed on a new one restarts from
-the beginning: every block group the original request selects is
-relocated again, including the ones the interrupted run created.
-
-This patch restores the old resume adjustments for exactly those items.
-When a chunk type has no vrange filter in the stored item, the resume
-changes its arguments as before: it adds the soft filter if the type is
-being converted, and usage=90 if it has no usage, usage range or convert
-filter. This is decided when the item is recovered at mount, before
-the previous patch adds the bound, and is written back with the bound.
-A chunk type with a vrange, which is every balance started by a kernel
-with the previous patch and any balance the user gave a vrange, relies
-on the vrange and is resumed unchanged.
-
-The trade-off: the legacy resume neither completes the original request
-(block groups more than 90% full are skipped, and so are the ones being
-converted that already had the target profile) nor avoids much work on a
-filesystem that is more than 90% full, and keeping it adds a layer of
-surprising behavior and code for a case that only arises once, when a
-balance spans the kernel upgrade. Without it, such a balance repeats
-work but skips nothing.
-
----
- fs/btrfs/volumes.c | 37 +++++++++++++++++++++++++++++++++++++
- 1 file changed, 37 insertions(+)
-
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 0f6ae59dc4b70..eacc70f2ac90a 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -4114,6 +4114,41 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
- return ret;
- }
-
-+/*
-+ * A balance item written by a kernel without the vrange bound has no vrange
-+ * filter. Resume it as those kernels did. A chunk type with a vrange relies
-+ * on it and is resumed unchanged.
-+ */
-+static void update_legacy_balance_args(struct btrfs_balance_args *bargs)
-+{
-+ if (bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)
-+ return;
-+
-+ /* Turn on soft mode for chunk types that were being converted. */
-+ if (bargs->flags & BTRFS_BALANCE_ARGS_CONVERT) {
-+ bargs->flags |= BTRFS_BALANCE_ARGS_SOFT;
-+ return;
-+ }
-+
-+ /*
-+ * Turn on the usage filter if it is not already used. The idea is
-+ * that chunks that we have already balanced should be reasonably full.
-+ */
-+ if (!(bargs->flags & (BTRFS_BALANCE_ARGS_USAGE |
-+ BTRFS_BALANCE_ARGS_USAGE_RANGE))) {
-+ bargs->flags |= BTRFS_BALANCE_ARGS_USAGE;
-+ bargs->usage = 90;
-+ }
-+}
-+
-+/* Update the arguments of a balance item recovered at mount. */
-+static void update_balance_args(struct btrfs_balance_control *bctl)
-+{
-+ update_legacy_balance_args(&bctl->data);
-+ update_legacy_balance_args(&bctl->sys);
-+ update_legacy_balance_args(&bctl->meta);
-+}
-+
- static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
- {
- if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
-@@ -5392,7 +5427,9 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
- * end of the last chunk, and lowering a vend above it drops no block
- * group from the range. The bound is written back to the item when
- * the balance resumes, so a resume that is interrupted again keeps it.
-+ * Adjust the chunk types that have no vrange on disk first.
- */
-+ update_balance_args(bctl);
- bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
- WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
-
---
-2.47.3
-
--- /dev/null
+From 528a8239008fb6f7fac00b65ecbc57d49729a9aa Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 16 Sep 2026 02:29:10 -0400
+Subject: [PATCH 5/8] btrfs: replace the balance resume heuristics with a
+ vrange bound
+To: linux-btrfs@vger.kernel.org
+
+When a balance is resumed after a pause or an unmount,
+update_balance_args() adds a usage=90 filter to every chunk type that is
+not being converted. That changes the set of block groups the original
+request selected. It skips full block groups the interrupted balance had
+not reached yet, so the resumed balance can finish without having done
+what was asked, and it selects again any block group the interrupted
+balance created that is less than 90% full, relocating the same data a
+second time. For the chunk types that are being converted it adds the
+soft filter instead, so the resume does not convert again the block
+groups the interrupted balance converted, but it also skips the ones
+that already had the target profile when the balance started, which the
+original request selected.
+
+Replace it with a bound fixed when the balance starts. Before the
+balance item is written, limit the vrange filter of each chunk type to
+the addresses below the next chunk to be allocated: add a 0..end range
+when the request had no vrange filter, or lower the end of the one it
+had. Every block group that existed when the balance started stays
+selected until the balance relocates it.
+
+New chunks are allocated after the last chunk, but relocation can remove
+the highest chunks without allocating any (their data fits in free space
+lower down), and the next chunk would then reuse their addresses, below
+the bound. A resumed balance would select it again, although the
+interrupted one, walking downward, never would have. So while a balance
+item exists, find_next_chunk() never returns an address below the bound:
+an in-memory floor, set when the balance starts and when the balance
+item is recovered at mount, and cleared when the item is deleted. Every
+chunk allocated in that time, relocation targets included, stays outside
+the range, on the first run and after a resume. The gap the floor can
+leave in the logical address space is harmless.
+
+A resume now uses the arguments exactly as they were stored: the bound
+already keeps out every block group the interrupted balance created,
+converted ones included, so neither the usage filter nor the soft filter
+is needed, and update_balance_args() goes away.
+
+The bound does not move as the balance progresses. Block groups the
+balance has relocated are gone, so on resume the walk only revisits the
+chunks it left in place above the point where it stopped (filtered out,
+ENOSPC, active swapfile) and checks them against the filters again.
+Nothing is skipped.
+
+Because the bound is written with the balance item and never changes,
+there is no progress to persist: a resume after an unmount or a crash
+needs no extra writes.
+
+A balance item recovered at mount is limited in the same way before the
+floor is set from it: a vrange 0..end is added to each chunk type that
+has none, and a vrange that ends above the next chunk is lowered to it.
+That covers items written by older kernels, which have no vrange or a
+vrange the user gave, which can end anywhere (a 1 TiB end would put
+every new chunk at 1 TiB and above). A bound written by this kernel
+changes only when relocation removed the highest chunks and allocated
+none above it; no chunk exists between the next chunk and the old end,
+so lowering it drops nothing. When the recovered arguments change,
+they are written back to the balance item once, when the balance
+resumes, so a resume that is interrupted again resumes with the same
+bound and not with a new one that includes the chunks the first resume
+allocated. If that write fails, the balance is not resumed: as for the
+other errors at that point, it ends with the error logged, and its item
+is deleted. Nothing has been relocated yet, so if the item survives (the
+delete fails too, or the system crashes first), the next mount bounds it
+again to the same chunks.
+
+A balance item written by an older kernel therefore resumes with the
+bound and without the usage or soft filter: the resumed balance restarts
+from the beginning, relocating again every block group the request
+selects, including the ones the interrupted run created or converted,
+but it skips none.
+
+The bound is visible: `btrfs balance status -v`, the
+BTRFS_IOC_BALANCE_PROGRESS ioctl and the "balance: start" and "balance:
+resume" log lines show a vrange the user did not give.
+
+---
+ fs/btrfs/fs.h | 6 ++
+ fs/btrfs/volumes.c | 188 +++++++++++++++++++++++++++++++++------------
+ fs/btrfs/volumes.h | 6 ++
+ 3 files changed, 150 insertions(+), 50 deletions(-)
+
+diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
+index 2dd167c32d612..a42aa4de2bcc6 100644
+--- a/fs/btrfs/fs.h
++++ b/fs/btrfs/fs.h
+@@ -785,6 +785,12 @@ struct btrfs_fs_info {
+ atomic_t balance_cancel_req;
+ struct btrfs_balance_control *balance_ctl;
+ wait_queue_head_t balance_wait_q;
++ /*
++ * 0, or the end of the balance's vrange bound: while a balance item
++ * exists (running or paused) find_next_chunk() allocates at or above
++ * it, so the balance never selects a chunk it allocated.
++ */
++ u64 balance_chunk_floor;
+
+ /* Cancellation requests for chunk relocation */
+ atomic_t reloc_cancel_req;
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 7193b1ab9636a..0d661700b3f6e 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -2035,7 +2035,7 @@ static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
+ }
+ read_unlock(&fs_info->mapping_tree_lock);
+
+- return ret;
++ return max(ret, READ_ONCE(fs_info->balance_chunk_floor));
+ }
+
+ static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
+@@ -3970,13 +3970,27 @@ static void btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
+ disk->stripes_max = cpu_to_le32(cpu->stripes_max);
+ }
+
++static void set_balance_item(struct extent_buffer *leaf,
++ struct btrfs_balance_item *item,
++ const struct btrfs_balance_control *bctl)
++{
++ struct btrfs_disk_balance_args disk_bargs;
++
++ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
++ btrfs_set_balance_data(leaf, item, &disk_bargs);
++ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
++ btrfs_set_balance_meta(leaf, item, &disk_bargs);
++ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
++ btrfs_set_balance_sys(leaf, item, &disk_bargs);
++ btrfs_set_balance_flags(leaf, item, bctl->flags & ~BTRFS_BALANCE_RESUME);
++}
++
+ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+ struct btrfs_balance_control *bctl)
+ {
+ struct btrfs_root *root = fs_info->tree_root;
+ struct btrfs_trans_handle *trans;
+ struct btrfs_balance_item *item;
+- struct btrfs_disk_balance_args disk_bargs;
+ struct btrfs_path *path;
+ struct extent_buffer *leaf;
+ struct btrfs_key key;
+@@ -4005,14 +4019,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+ item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
+
+ memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
+-
+- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
+- btrfs_set_balance_data(leaf, item, &disk_bargs);
+- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
+- btrfs_set_balance_meta(leaf, item, &disk_bargs);
+- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
+- btrfs_set_balance_sys(leaf, item, &disk_bargs);
+- btrfs_set_balance_flags(leaf, item, bctl->flags);
++ set_balance_item(leaf, item, bctl);
+ out:
+ btrfs_free_path(path);
+ if (ret == 0)
+@@ -4023,6 +4030,43 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+ return ret;
+ }
+
++/* Write the arguments of a resumed balance to its existing item. */
++static int update_balance_item(struct btrfs_fs_info *fs_info,
++ const struct btrfs_balance_control *bctl)
++{
++ struct btrfs_root *root = fs_info->tree_root;
++ struct btrfs_trans_handle *trans;
++ BTRFS_PATH_AUTO_FREE(path);
++ struct btrfs_key key;
++ int ret;
++
++ path = btrfs_alloc_path();
++ if (!path)
++ return -ENOMEM;
++
++ trans = btrfs_start_transaction(root, 0);
++ if (IS_ERR(trans))
++ return PTR_ERR(trans);
++
++ key.objectid = BTRFS_BALANCE_OBJECTID;
++ key.type = BTRFS_TEMPORARY_ITEM_KEY;
++ key.offset = 0;
++
++ ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
++ if (ret > 0)
++ ret = -ENOENT;
++ if (ret < 0) {
++ btrfs_end_transaction(trans);
++ return ret;
++ }
++
++ set_balance_item(path->nodes[0],
++ btrfs_item_ptr(path->nodes[0], path->slots[0],
++ struct btrfs_balance_item), bctl);
++ btrfs_release_path(path);
++ return btrfs_commit_transaction(trans);
++}
++
+ static int del_balance_item(struct btrfs_fs_info *fs_info)
+ {
+ struct btrfs_root *root = fs_info->tree_root;
+@@ -4064,47 +4108,61 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
+ return ret;
+ }
+
++static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
++{
++ if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
++ bargs->flags |= BTRFS_BALANCE_ARGS_VRANGE;
++ bargs->vstart = 0;
++ bargs->vend = end;
++ return true;
++ }
++ if (bargs->vend <= end)
++ return false;
++ bargs->vend = end;
++ return true;
++}
++
+ /*
+- * This is a heuristic used to reduce the number of chunks balanced on
+- * resume after balance was interrupted.
++ * Limit the request to the chunks that exist now. The bound is written with
++ * the balance item and never moves, so there is no progress to persist. New
++ * chunks are allocated after the last chunk, but once relocation has removed
++ * the highest chunks the last chunk is lower than the bound, so while the
++ * balance item exists find_next_chunk() also observes balance_chunk_floor:
++ * every chunk allocated in that time, relocation targets included, stays
++ * outside the range, on this run and after a resume.
++ *
++ * Return true if any of the arguments changed.
+ */
+-static void update_balance_args(struct btrfs_balance_control *bctl)
++static bool limit_balance_to_existing_chunks(struct btrfs_fs_info *fs_info,
++ struct btrfs_balance_control *bctl)
+ {
+- /*
+- * Turn on soft mode for chunk types that were being converted.
+- */
+- if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
+- bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
+- if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
+- bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
+- if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
+- bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
++ const u64 end = find_next_chunk(fs_info);
++ bool changed = false;
+
+- /*
+- * Turn on usage filter if is not already used. The idea is
+- * that chunks that we have already balanced should be
+- * reasonably full. Don't do it for chunks that are being
+- * converted - that will keep us from relocating unconverted
+- * (albeit full) chunks.
+- */
+- if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
+- !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
+- !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
+- bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
+- bctl->data.usage = 90;
+- }
+- if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
+- !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
+- !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
+- bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
+- bctl->sys.usage = 90;
+- }
+- if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
+- !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
+- !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
+- bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
+- bctl->meta.usage = 90;
+- }
++ if (bctl->flags & BTRFS_BALANCE_DATA)
++ changed |= limit_balance_vrange(&bctl->data, end);
++ if (bctl->flags & BTRFS_BALANCE_METADATA)
++ changed |= limit_balance_vrange(&bctl->meta, end);
++ if (bctl->flags & BTRFS_BALANCE_SYSTEM)
++ changed |= limit_balance_vrange(&bctl->sys, end);
++ return changed;
++}
++
++/* The highest vrange end of the selected chunk types, 0 if none has one. */
++static u64 balance_chunk_floor(const struct btrfs_balance_control *bctl)
++{
++ u64 floor = 0;
++
++ if ((bctl->flags & BTRFS_BALANCE_DATA) &&
++ (bctl->data.flags & BTRFS_BALANCE_ARGS_VRANGE))
++ floor = max(floor, bctl->data.vend);
++ if ((bctl->flags & BTRFS_BALANCE_METADATA) &&
++ (bctl->meta.flags & BTRFS_BALANCE_ARGS_VRANGE))
++ floor = max(floor, bctl->meta.vend);
++ if ((bctl->flags & BTRFS_BALANCE_SYSTEM) &&
++ (bctl->sys.flags & BTRFS_BALANCE_ARGS_VRANGE))
++ floor = max(floor, bctl->sys.vend);
++ return floor;
+ }
+
+ /*
+@@ -4123,6 +4181,7 @@ static void reset_balance_state(struct btrfs_fs_info *fs_info)
+
+ kfree(bctl);
+ ret = del_balance_item(fs_info);
++ WRITE_ONCE(fs_info->balance_chunk_floor, 0);
+ if (ret)
+ btrfs_handle_fs_error(fs_info, ret, NULL);
+ }
+@@ -5113,6 +5172,9 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+ btrfs_bg_type_to_raid_name(data_target));
+ }
+
++ if (!(bctl->flags & BTRFS_BALANCE_RESUME))
++ limit_balance_to_existing_chunks(fs_info, bctl);
++
+ ret = insert_balance_item(fs_info, bctl);
+ if (ret && ret != -EEXIST)
+ goto out;
+@@ -5123,11 +5185,26 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+ spin_lock(&fs_info->balance_lock);
+ fs_info->balance_ctl = bctl;
+ spin_unlock(&fs_info->balance_lock);
++ WRITE_ONCE(fs_info->balance_chunk_floor,
++ balance_chunk_floor(bctl));
+ } else {
+ BUG_ON(ret != -EEXIST);
+- spin_lock(&fs_info->balance_lock);
+- update_balance_args(bctl);
+- spin_unlock(&fs_info->balance_lock);
++ if (bctl->update_item) {
++ /*
++ * Nothing has been relocated yet, so the item on disk,
++ * bounded again at the next mount, is still right. As
++ * for the other errors here, the balance ends and its
++ * item is deleted.
++ */
++ ret = update_balance_item(fs_info, bctl);
++ if (ret) {
++ btrfs_err(fs_info,
++ "balance: failed to write the bounded balance item: %d",
++ ret);
++ goto out;
++ }
++ bctl->update_item = false;
++ }
+ }
+
+ ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
+@@ -5280,6 +5357,17 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
+ btrfs_balance_sys(leaf, item, &disk_bargs);
+ btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
+
++ /*
++ * Bound the request as a new balance would be: an item written by an
++ * older kernel has no vrange, and one a user gave to such a kernel can
++ * end anywhere. The floor is still 0, so find_next_chunk() returns the
++ * end of the last chunk, and lowering a vend above it drops no block
++ * group from the range. The bound is written back to the item when
++ * the balance resumes, so a resume that is interrupted again keeps it.
++ */
++ bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
++ WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
++
+ /*
+ * This should never happen, as the paused balance state is recovered
+ * during mount without any chance of other exclusive ops to collide.
+diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
+index 891bc2432870a..e7112257014e8 100644
+--- a/fs/btrfs/volumes.h
++++ b/fs/btrfs/volumes.h
+@@ -665,6 +665,12 @@ struct btrfs_balance_control {
+
+ u64 flags;
+
++ /*
++ * The arguments recovered at mount were bounded and differ from the
++ * balance item: write them back when the balance resumes.
++ */
++ bool update_item;
++
+ struct btrfs_balance_progress stat;
+ };
+
+--
+2.47.3
+
--- /dev/null
+From b3e77dc15afd8a273d24293f5ca7ba9406835848 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Sun, 8 Nov 2026 22:24:56 -0500
+Subject: [PATCH 6/8] btrfs: write the remaining balance limits back to the
+ balance item
+To: linux-btrfs@vger.kernel.org
+
+The limit filters are counted down in memory: should_balance_chunk()
+decrements limit, or limit_max of a limit_min..limit_max range, for every
+chunk that passes the other filters, and __btrfs_balance() restores them
+after its counting pass. A balance resumed after a pause continues with
+what is left. The balance item is never updated, so a balance resumed
+after an unmount or a crash starts again from the limits that were given:
+every resume can relocate up to limit more chunks. The minimum shifts
+too: the counting pass of the resume sees only the chunks that are left,
+which can be fewer than limit_min, and the resume then relocates nothing.
+
+Count a chunk against both ends of the range, and write the remaining
+limits to the balance item, once the chunk has been processed. limit and
+limit_max are still decremented when the chunk is selected, as before,
+and limit_min is now decremented (down to 0) when it has been processed,
+after the limit_min check has let it through, so limit_min means the
+remaining minimum, on disk and in memory. A resume after an unmount or a
+crash then continues from the remaining limits, like a resume after a
+pause, and its limit_min check compares the chunks that are left with the
+minimum that is left. The values written are those of the real pass:
+the counting pass returns to the loop before any chunk is processed, and
+its decrements are undone before the real pass starts.
+
+A chunk counts as processed when its relocation has returned for that
+chunk: relocated, or left in place by ENOSPC or an active swapfile. This
+is what the in-memory limit has always counted, so a resume after an
+unmount or a crash now behaves exactly like a resume after a pause. A
+pause cannot stop a chunk halfway: the pause request is checked between
+chunks, so the chunk being relocated is processed and written first. A
+cancel or a fatal signal can stop a relocation, but it also ends the
+balance and deletes the item. If the item cannot be written, the balance
+stops with the error, like any other error in the loop, and its item is
+deleted; if the item survives that (an aborted transaction), it holds
+the last committed limits, within the bound below.
+
+The update takes its own handle on the running transaction once the
+relocation has returned, and ends it without committing. The
+transaction that removed the relocated chunk has ended by then, and is
+usually still the running one, so both changes usually commit together,
+and the update can never commit before the removal. A crash in between
+loses the count of that one chunk, and the resumed balance can relocate
+one chunk more than requested, never one fewer. Making the update part
+of the removal's transaction would mean passing the balance into
+btrfs_relocate_chunk(), which device shrink and removal also use.
+
+Only chunk types with a limit filter write the item, once per processed
+chunk; a balance without limits makes no more writes than before. The
+remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
+already returned them during the balance and now returns the same values
+after a remount, and the "balance: resume" log line shows them.
+
+---
+ fs/btrfs/volumes.c | 54 +++++++++++++++++++++++++++++++++++++++++++---
+ 1 file changed, 51 insertions(+), 3 deletions(-)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 0d661700b3f6e..f9e299b382ed0 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4030,9 +4030,13 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+ return ret;
+ }
+
+-/* Write the arguments of a resumed balance to its existing item. */
++/*
++ * Write the arguments of a balance to its existing item. With @commit false
++ * the update joins the running transaction and is committed with it.
++ */
+ static int update_balance_item(struct btrfs_fs_info *fs_info,
+- const struct btrfs_balance_control *bctl)
++ const struct btrfs_balance_control *bctl,
++ bool commit)
+ {
+ struct btrfs_root *root = fs_info->tree_root;
+ struct btrfs_trans_handle *trans;
+@@ -4064,6 +4068,8 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
+ btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_balance_item), bctl);
+ btrfs_release_path(path);
++ if (!commit)
++ return btrfs_end_transaction(trans);
+ return btrfs_commit_transaction(trans);
+ }
+
+@@ -4486,6 +4492,44 @@ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *
+ return true;
+ }
+
++/*
++ * The limit filters count a chunk when should_balance_chunk() selects it, so
++ * it counts whether its relocation succeeds or leaves it in place (ENOSPC, an
++ * active swapfile). Once the chunk has been processed, count it against
++ * limit_min as well, and write the remaining limits to the balance item, so a
++ * balance resumed after an unmount or a crash processes only what is left of
++ * the request, as one resumed after a pause does. Chunk types without a limit
++ * filter write nothing.
++ */
++static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
++{
++ struct btrfs_balance_control *bctl = fs_info->balance_ctl;
++ struct btrfs_balance_args *bargs;
++
++ if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
++ bargs = &bctl->data;
++ else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
++ bargs = &bctl->sys;
++ else
++ bargs = &bctl->meta;
++
++ if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
++ if (!(bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE))
++ return 0;
++ if (bargs->limit_min > 0)
++ bargs->limit_min--;
++ }
++
++ /*
++ * A relocated chunk was removed in a transaction that has ended but
++ * may not have committed. The update usually joins that transaction
++ * and never lands in an earlier one, so a crash can lose the count of
++ * the last relocated chunk, and the resumed balance can then relocate
++ * one chunk more than requested, but never one fewer.
++ */
++ return update_balance_item(fs_info, bctl, false);
++}
++
+ struct remap_chunk_info {
+ struct list_head list;
+ u64 offset;
+@@ -4804,6 +4848,10 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+ bctl->stat.completed++;
+ spin_unlock(&fs_info->balance_lock);
+ }
++
++ ret = count_limited_chunk(fs_info, chunk_type);
++ if (ret)
++ goto error;
+ loop:
+ if (found_key.offset == 0)
+ break;
+@@ -5196,7 +5244,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+ * for the other errors here, the balance ends and its
+ * item is deleted.
+ */
+- ret = update_balance_item(fs_info, bctl);
++ ret = update_balance_item(fs_info, bctl, true);
+ if (ret) {
+ btrfs_err(fs_info,
+ "balance: failed to write the bounded balance item: %d",
+--
+2.47.3
+
--- /dev/null
+From 5bd028df8cd1b1ecb2cd6789304f83a0ffc357fc Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 11 Nov 2026 00:32:39 -0500
+Subject: [PATCH 7/8] btrfs: do not write queued remap chunks as processed to
+ the balance item
+To: linux-btrfs@vger.kernel.org
+
+The balance item gets the remaining limits after each processed chunk
+(count_limited_chunk()). METADATA_REMAP chunks are not processed in the
+chunk walk: __btrfs_balance() queues them and balances them after the
+walk, but should_balance_chunk() has already counted them down from the
+metadata limit when it selected them. Any limited chunk processed after
+that writes the item with the queued chunks counted as done. If the
+balance stops before the end of the walk, by a pause, an unmount or a
+crash, the queue is gone, and the balance resumed from the item has too
+little left of its metadata limit to balance the chunks it never
+balanced: with -mlimit=1..1 and the remap chunk selected as the one
+metadata chunk, the resumed balance balances no metadata chunk.
+
+Write the item with the queued chunks added back to the metadata limit
+(limit, or limit_max of a range, as should_balance_chunk() counted them).
+The in-memory limit is not changed: the walk needs it to stop selecting
+chunks once the limit is used up.
+
+---
+ fs/btrfs/volumes.c | 40 +++++++++++++++++++++++++++++-----------
+ 1 file changed, 29 insertions(+), 11 deletions(-)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index f9e299b382ed0..2fd20d58d43e0 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -3970,15 +3970,26 @@ static void btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
+ disk->stripes_max = cpu_to_le32(cpu->stripes_max);
+ }
+
++/*
++ * @queued_meta: metadata chunks the limit filter has counted that are queued
++ * but not processed yet. Their count is written back to the metadata limit.
++ */
+ static void set_balance_item(struct extent_buffer *leaf,
+ struct btrfs_balance_item *item,
+- const struct btrfs_balance_control *bctl)
++ const struct btrfs_balance_control *bctl,
++ u32 queued_meta)
+ {
+ struct btrfs_disk_balance_args disk_bargs;
++ struct btrfs_balance_args meta = bctl->meta;
++
++ if (meta.flags & BTRFS_BALANCE_ARGS_LIMIT)
++ meta.limit += queued_meta;
++ else if (meta.flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
++ meta.limit_max += queued_meta;
+
+ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
+ btrfs_set_balance_data(leaf, item, &disk_bargs);
+- btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
++ btrfs_cpu_balance_args_to_disk(&disk_bargs, &meta);
+ btrfs_set_balance_meta(leaf, item, &disk_bargs);
+ btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
+ btrfs_set_balance_sys(leaf, item, &disk_bargs);
+@@ -4019,7 +4030,7 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+ item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
+
+ memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
+- set_balance_item(leaf, item, bctl);
++ set_balance_item(leaf, item, bctl, 0);
+ out:
+ btrfs_free_path(path);
+ if (ret == 0)
+@@ -4031,12 +4042,13 @@ static int insert_balance_item(struct btrfs_fs_info *fs_info,
+ }
+
+ /*
+- * Write the arguments of a balance to its existing item. With @commit false
+- * the update joins the running transaction and is committed with it.
++ * Write the arguments of a balance to its existing item, see
++ * set_balance_item() for @queued_meta. With @commit false the update joins
++ * the running transaction and is committed with it.
+ */
+ static int update_balance_item(struct btrfs_fs_info *fs_info,
+ const struct btrfs_balance_control *bctl,
+- bool commit)
++ u32 queued_meta, bool commit)
+ {
+ struct btrfs_root *root = fs_info->tree_root;
+ struct btrfs_trans_handle *trans;
+@@ -4066,7 +4078,7 @@ static int update_balance_item(struct btrfs_fs_info *fs_info,
+
+ set_balance_item(path->nodes[0],
+ btrfs_item_ptr(path->nodes[0], path->slots[0],
+- struct btrfs_balance_item), bctl);
++ struct btrfs_balance_item), bctl, queued_meta);
+ btrfs_release_path(path);
+ if (!commit)
+ return btrfs_end_transaction(trans);
+@@ -4500,8 +4512,14 @@ static int should_balance_chunk(struct extent_buffer *leaf, struct btrfs_chunk *
+ * balance resumed after an unmount or a crash processes only what is left of
+ * the request, as one resumed after a pause does. Chunk types without a limit
+ * filter write nothing.
++ *
++ * METADATA_REMAP chunks are counted when they are selected too, but they are
++ * queued and balanced only after the chunk walk. @queued_meta of them are
++ * waiting: the item gets them back on the metadata limit, so a balance that
++ * stops before it gets to them still processes them when it is resumed.
+ */
+-static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
++static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type,
++ u32 queued_meta)
+ {
+ struct btrfs_balance_control *bctl = fs_info->balance_ctl;
+ struct btrfs_balance_args *bargs;
+@@ -4527,7 +4545,7 @@ static int count_limited_chunk(struct btrfs_fs_info *fs_info, u64 chunk_type)
+ * the last relocated chunk, and the resumed balance can then relocate
+ * one chunk more than requested, but never one fewer.
+ */
+- return update_balance_item(fs_info, bctl, false);
++ return update_balance_item(fs_info, bctl, queued_meta, false);
+ }
+
+ struct remap_chunk_info {
+@@ -4849,7 +4867,7 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
+ spin_unlock(&fs_info->balance_lock);
+ }
+
+- ret = count_limited_chunk(fs_info, chunk_type);
++ ret = count_limited_chunk(fs_info, chunk_type, num_remap_chunks);
+ if (ret)
+ goto error;
+ loop:
+@@ -5244,7 +5262,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
+ * for the other errors here, the balance ends and its
+ * item is deleted.
+ */
+- ret = update_balance_item(fs_info, bctl, true);
++ ret = update_balance_item(fs_info, bctl, 0, true);
+ if (ret) {
+ btrfs_err(fs_info,
+ "balance: failed to write the bounded balance item: %d",
+--
+2.47.3
+
--- /dev/null
+From 02f11b2bbcbcf475062e037c537c5f16cf4cd088 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 13 Oct 2026 14:24:01 -0400
+Subject: [PATCH 8/8] btrfs: keep the old resume filters for balance items
+ without a vrange
+To: linux-btrfs@vger.kernel.org
+
+This patch is optional. It is offered so that reviewers can take it or
+drop it; the author's preference is to drop it. The previous patches do
+not depend on it.
+
+A balance item written by a kernel without the vrange bound has no vrange
+filter. With the previous patch alone, a balance that was paused or
+interrupted on such a kernel and is resumed on a new one restarts from
+the beginning: every block group the original request selects is
+relocated again, including the ones the interrupted run created.
+
+This patch restores the old resume adjustments for exactly those items.
+When a chunk type has no vrange filter in the stored item, the resume
+changes its arguments as before: it adds the soft filter if the type is
+being converted, and usage=90 if it has no usage, usage range or convert
+filter. This is decided when the item is recovered at mount, before
+the previous patch adds the bound, and is written back with the bound.
+A chunk type with a vrange, which is every balance started by a kernel
+with the previous patch and any balance the user gave a vrange, relies
+on the vrange and is resumed unchanged.
+
+The trade-off: the legacy resume neither completes the original request
+(block groups more than 90% full are skipped, and so are the ones being
+converted that already had the target profile) nor avoids much work on a
+filesystem that is more than 90% full, and keeping it adds a layer of
+surprising behavior and code for a case that only arises once, when a
+balance spans the kernel upgrade. Without it, such a balance repeats
+work but skips nothing.
+
+---
+ fs/btrfs/volumes.c | 37 +++++++++++++++++++++++++++++++++++++
+ 1 file changed, 37 insertions(+)
+
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index 2fd20d58d43e0..5522b9452117f 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -4126,6 +4126,41 @@ static int del_balance_item(struct btrfs_fs_info *fs_info)
+ return ret;
+ }
+
++/*
++ * A balance item written by a kernel without the vrange bound has no vrange
++ * filter. Resume it as those kernels did. A chunk type with a vrange relies
++ * on it and is resumed unchanged.
++ */
++static void update_legacy_balance_args(struct btrfs_balance_args *bargs)
++{
++ if (bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)
++ return;
++
++ /* Turn on soft mode for chunk types that were being converted. */
++ if (bargs->flags & BTRFS_BALANCE_ARGS_CONVERT) {
++ bargs->flags |= BTRFS_BALANCE_ARGS_SOFT;
++ return;
++ }
++
++ /*
++ * Turn on the usage filter if it is not already used. The idea is
++ * that chunks that we have already balanced should be reasonably full.
++ */
++ if (!(bargs->flags & (BTRFS_BALANCE_ARGS_USAGE |
++ BTRFS_BALANCE_ARGS_USAGE_RANGE))) {
++ bargs->flags |= BTRFS_BALANCE_ARGS_USAGE;
++ bargs->usage = 90;
++ }
++}
++
++/* Update the arguments of a balance item recovered at mount. */
++static void update_balance_args(struct btrfs_balance_control *bctl)
++{
++ update_legacy_balance_args(&bctl->data);
++ update_legacy_balance_args(&bctl->sys);
++ update_legacy_balance_args(&bctl->meta);
++}
++
+ static bool limit_balance_vrange(struct btrfs_balance_args *bargs, u64 end)
+ {
+ if (!(bargs->flags & BTRFS_BALANCE_ARGS_VRANGE)) {
+@@ -5430,7 +5465,9 @@ int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
+ * end of the last chunk, and lowering a vend above it drops no block
+ * group from the range. The bound is written back to the item when
+ * the balance resumes, so a resume that is interrupted again keeps it.
++ * Adjust the chunk types that have no vrange on disk first.
+ */
++ update_balance_args(bctl);
+ bctl->update_item = limit_balance_to_existing_chunks(fs_info, bctl);
+ WRITE_ONCE(fs_info->balance_chunk_floor, balance_chunk_floor(bctl));
+
+--
+2.47.3
+
resume without soft and relocate
the target-profile chunks below the bound.
-Patch numbers are the series' current ones (since 2026-11-10): 1 the
+Since 2026-11-11 (Zygo, rm1111) the series has 8 patches: 1 the
+limit_min fix, 2-4 the METADATA_REMAP fixes (free the queued remap
+chunks at the error label, give their limit back, count them as metadata
+in the limit_min check), 5 the vrange bound, 6 the limit write-back, 7
+queued remap chunks are not written to the balance item as processed, 8
+the legacy filters. The remap-balance-leak series (2-4) is merged in;
+its TESTING.md is folded in at the end. The 2026-11-11 round below uses
+these numbers. 6.18 has no remap tree and carries 1, 5, 6 and 8 only.
+Before 2026-11-11 the numbers were (since 2026-11-10): 1 the
limit_min fix, 2 the vrange bound, 3 the limit write-back, 4 the legacy
-filters. The rounds before 2026-11-10 number the series without the
+filters; read the 2026-11-10 round's 2, 3 and 4 as 5, 6 and 8. The rounds before 2026-11-10 number the series without the
fix (1 vrange, 2 limit write-back, 3 legacy, the oldest rounds already
renumbered to that): add one to their patch numbers. The remap leak fix
that the rounds before 2026-11-08 also tested, "btrfs: free queued remap
-chunks when balance bails out", is now its own series,
-`topics/remap-balance-leak`; its leak test and results are in that
-directory's TESTING.md.
+chunks when balance bails out", was its own series,
+`topics/remap-balance-leak`, from 2026-11-08 to 2026-11-11, and is now
+patch 2 again; its leak test and results are at the end of this file.
Kernel under test for the "Current" column: misc-next zb64 568c45b64012
(uname 7.3.0-rc7+ #5, KASAN) unless a row says otherwise. Test logs are
Not tested.
- The 2026-11-08 round's first open item (the alias) is closed by patch 1.
+## 2026-11-11 round (rm1111: the METADATA_REMAP fixes, patches 2-4 and 7)
+
+Zygo (2026-11-11): "Chase the remap gaps", then on placement: the
+remap-balance-leak series joins balance-resume, fixes first; "do not
+write queued remap chunks as processed to the balance item" stays its own
+commit; no Cc stable for any of it (the remap tree is experimental:
+BTRFS_FEATURE_INCOMPAT_REMAP_TREE is in INCOMPAT_SUPP only under
+CONFIG_BTRFS_EXPERIMENTAL, on both 83cd81353093 and v7.3.3). Patches 2-4
+carry Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree"), v7.0.
+
+The model, from the source (identical on the kdave misc-next base
+83cd81353093 and v7.3.3): `should_balance_chunk()` maps a METADATA_REMAP
+chunk to METADATA (the -m type flag selects it, the -m args filter it, and
+it decrements `limit`, or `limit_max` of a range, when selected); the
+counting pass counts it in `count_meta`; the limit_min check tests the raw
+METADATA bit, which a remap chunk (type bit 12 only) never has. The walk
+queues remap chunks and `balance_remap_chunks()` balances them after it
+(set read-only, COW the whole remap tree, mark the group unused). A pause
+leaves the walk through the error label with -ECANCELED, which drops the
+queue. Two gaps: (gap 1) `-mlimit=MIN..MAX` with fewer than MIN matches
+skips every METADATA chunk but still balances the remap chunk (patch 4);
+(gap 2) the dropped queue keeps its limit, in memory for a resume on the
+same mount (upstream, patch 3), and on disk via patch 6's per-chunk item
+write (ours only, patch 7).
+
+New test `remap-balance-limit-test.sh` (fresh remap-tree fs: SYSTEM
+1048576, METADATA 5242880, METADATA_REMAP 13631488). "Balanced" = a
+kprobe on `btrfs_inc_block_group_ro()` (BTF arg `cache->start`, needs
+CONFIG_PROBE_EVENTS_BTF_ARGS) saw the group set read-only, in a private
+tracefs instance; group emptiness is not proof, because data relocation on
+a remap-tree fs COWs the remap tree and its leaves move between remap
+chunks (a first detector based on "used 0" SKIPped g2u falsely).
+
+| arm | checks | patch(es) |
+|---|---|---|
+| g1 | `-mlimit=50..60` relocates 0 and the remap chunk is never set read-only | 4 |
+| g1p | `-mlimit=1..1000` balances the remap chunk (detector control) | - |
+| g1m | `-mlimit=1..1` with the METADATA and the remap chunk matching sets exactly one of them read-only (the MAX bound, Zygo 11-11); progs reports 2, the SYSTEM chunk getting its own 1..1 from progs' copy of the -m args | 4 must not break it |
+| g2m | D_top > R' > D1..Dn, `-dlimit=1..1000 -mlimit=1..1` paused with R' queued: `status -v` shows the metadata limit 1..1, `balance resume` (no unmount) balances R' | 3 |
+| g2u | the same, unmounted: the item's metadata limit_max reads 1, the mount-resume balances R' | 7 (fails with 6 and without 7) |
+
+Kernels: KASAN inline + lockdep + BTRFS_ASSERT, bzImage only, bhive.
+
+| kernel | source | md5 |
+|---|---|---|
+| 7.3.0+ #46 | misc-next zb64 efaeb5436501 (patches 1, 2, 5, 6, 8: no 3, 4, 7) | cdddf6a4920729e3b2649a193fe84c83 |
+| 7.3.0+ #47, #52 | kdave misc-next base 83cd81353093 (stock) | 5984506f23651cd39fbca44a410582eb, 046587a946c082ed92b39b04b33cfa33 |
+| 7.3.3+ #49, #53 | v7.3.3 (stock) | 60ae27f32a6b927bd7f0b607510e388f, 5d7dfba86a10ec3deff4ed00bb45a1b4 |
+| 7.3.0+ #51, #54 | misc-next with all 8 patches (the tree promoted 2026-11-11) | 01b7f01f1ca950ecbfc3cb4b2087eb9f, f7521cc7eb69a7a6abfc39bcda59ea26 |
+| 7.3.3+ #50, #55 | 7.3 with all 8 patches (the tree promoted 2026-11-11) | 289eba67a5bd0ec07c8218ad79667916, 1091186bad905c370ce43d18d8dfd356 |
+
+| arm | stock misc-next #47/#52 | stock v7.3.3 #49/#53 | #46 (no 3, 4, 7) | misc-next #51/#54 | 7.3 #50/#55 |
+|---|---|---|---|---|---|
+| g1 | FAIL: relocated 1, remap chunk set RO | FAIL (same) | FAIL (same) | PASS | PASS |
+| g1p | PASS | PASS | PASS | PASS | PASS |
+| g1m (#52-#55 only) | PASS: only the remap chunk RO | PASS (same) | - | PASS (same) | PASS (same) |
+| g2m | FAIL: 1..0 after the pause, R' never RO on resume | FAIL (same) | FAIL (same) | PASS | PASS |
+| g2u | PASS: item 1 1 (stock never writes it mid-balance) | PASS | FAIL: item 1 0, R' never RO | PASS | PASS |
+
+Gate on #51 and #50 (same trees as #54/#55): `balance-resume-limit-test.sh`
+(all 13 arms), convert x2, `balance-resume-test.sh`,
+`balance-resume-reuse-test.sh`, `remap-balance-resume-test.sh`,
+`remap-balance-leak-test.sh`, `nounmount-check.sh`, legacy plain, convert,
+vrange, convert-vrange, vrange-max (images by Debian 6.12.115): all PASS.
+Every FAIL above is a control's, as predicted (chunk counts; no ENOSPC, no
+loss of acked data). Taint 0, debug_locks 1, no KASAN/WARNING/BUG/lockdep/
+hung-task lines, no NORESERVE or unwritten-on-end lines, sampled
+unwritten_on_end* counters 0 (truncated delta 0) on the series kernels.
+
+Order and build: patches 2-3 replay onto patch 1 without conflict; patch
+4 is resolved against patch 1's `chunk_limit_min_filter()` (the METADATA
+test there gains `| BTRFS_BLOCK_GROUP_METADATA_REMAP`; message unchanged,
+it describes the type test, which is the same); 5-8 replay unchanged.
+The tip tree equals the tree the kernels above ran. Per-commit build of
+base..patch 8 on misc-next and 7.3 (CONFIG_BTRFS_EXPERIMENTAL=y checked
+at every commit): built=9 failed=0, no new warnings, tip links.
+
+Open, for this round:
+
+- Cancel and the other bail-outs share the error label with the pause
+ but are not driven (patches 2, 3).
+- 2026-11-10's open item (a range minimum never skips a remap chunk) is
+ closed by patch 4.
+
+
+### Patch 2: the remap-balance-leak series' record (until 2026-11-11 its own TESTING.md; its patch 1 is this series' patch 2)
+
+The one patch frees the METADATA_REMAP chunks that `__btrfs_balance()`
+queues on a local list when the walk leaves the loop through the error
+label (pause, cancel, chunk search or relocation errors, a failed
+allocation of a later entry). Before it, only `balance_remap_chunks()`
+freed the entries, and the error label skipped it. To verify: a balance
+that bails out with remap chunks queued must free every entry, and a
+completed walk must still free its entries in `balance_remap_chunks()`.
+
+Until 2026-11-08 the patch was patch 1 of `balance-resume`; the results
+below are from that time. It does not depend on that series, textually
+or in behavior: it applies to the base on its own, and the lanes merge it
+right after `balance-resume`. The test drives the bail-out with
+`remap-balance-resume-test.sh`, whose pause and resume use the balance
+item; the test kernels had `balance-resume` too.
+
+Kernel under test, unless a row says otherwise: misc-next zb64
+568c45b64012 (uname 7.3.0-rc7+ #5, KASAN), dhive; logs under
+`/mnt/wh/t-dhive/logs/<test>-<stamp>/` while recent, copied to
+`raid56-wh-harness/forensics/t-dhive-topics-20261018/`. The negative
+control ran on the misc-next base 0c48d26d46bd without any topic (dhive
+KASAN build 1 of #5's config, bzImage md5
+83a4d5325c9a195746cf230032929c76).
+
+#### Tests
+
+| test | verifies | patch(es) | duration | last result | date | kernel | harness | evidence |
+|---|---|---|---|---|---|---|---|---|
+| `remap-balance-leak-test.sh` | every queued `remap_chunk_info` is freed, including when the walk bails out at a pause | 1 | 171 s | PASS: 4 entries allocated in btrfs_balance, 4 freed; the paused walk's entry was freed from its own call site (btrfs_balance+0x1a26), the completed walks' from balance_remap_chunks (+0x4119) | 2026-10-18 | misc-next #5 | 0a72db0 | dhive `logs/remap-balance-leak-test-20261018-042918`, trace in `ev/remap-leak-1/trace.txt` |
+| `remap-balance-leak-test.sh` (negative control, `LEAK_ONLY=1`) | the same, on the base without the series | 1 | 172 s | FAIL as expected: 3 allocated, 1 unfreed (the paused walk's) | 2026-10-18 | misc-next base 0c48d26d46bd, dhive KASAN build 1 | 27b253a | dhive `logs/neg-remap-balance-leak-test-20261018-055959` |
+
+**remap-balance-leak-test.sh.** The test kernels have no kmemleak, so the
+test watches the allocations: a private tracefs instance records
+kmem:kmalloc events of 40 bytes (sizeof(struct remap_chunk_info)) whose
+call site is in btrfs_balance (`__btrfs_balance()` and
+`balance_remap_chunks()` are inlined into it) and every kmem:kfree called
+from btrfs_balance, while `remap-balance-resume-test.sh` runs. That test
+pauses a full balance after the walk has queued a METADATA_REMAP chunk --
+the bail-out that leaked -- then remounts and resumes. PASS needs the remap
+test to PASS (so the paused walk did queue the chunk), at least two
+allocations (the remap chunk move before the balance, and the paused walk),
+and a matching kfree for every allocation. Unfixed, the paused walk's entry
+is never freed. On #5 the paused walk's entry was freed from a different
+offset in btrfs_balance than the entries of the completed walks, which is
+the new free at the error label. **Shown to fail without the patch**: on
+the misc-next base 0c48d26d46bd (no topics; KASAN build of #5's config,
+dhive build 1, 2026-10-18), with `LEAK_ONLY=1` (the remap test is told to
+pause even without the vrange bound and its resume verdict is ignored), 3
+entries were allocated and 1 was never freed (allocated at
+btrfs_balance+0x2345, the paused walk's queue), and the remap chunk survived
+the resume, as expected without balance-resume's vrange bound
+(`logs/neg-remap-balance-leak-test-20261018-055959`). Limits: only the pause bail-out is driven, not
+cancel or the error exits (they share the error label); it depends on the
+inlining of both functions into btrfs_balance (a different inlining
+changes the call-site filter and the test then fails as blind with fewer
+than 2 allocations).
+
+Later results (the patch unchanged):
+
+| run | result | kernel |
+|---|---|---|
+| 2026-11-05 (br1105) | PASS on dhive and ehive; SKIP on 6.18 (no remap tree: the kernel cannot mount the fs), as expected | misc-next #67 |
+| 2026-11-06 (fl1106) | PASS on dhive and ehive, and on #67 (dhive); SKIP on 6.18 as expected | misc-next #68 |
+| 2026-11-09 (nc1108, as its own series merged after `balance-resume`) | PASS: 4 entries allocated in btrfs_balance, 0 unfreed; `remap-balance-resume-test.sh` PASS (the remap chunk at 6472859648 emptied by the resumed run); no KASAN, taint 0 | misc-next zb64 fb7546be6023 (7.3.0+ #8, KASAN + lockdep + BTRFS_ASSERT, md5 87693553da3b79a8991c445f42888365), ahive |
+| 2026-11-10 (lr1109, balance-resume gains its limit_min fix) | PASS: 4 entries allocated in btrfs_balance, 0 unfreed; `remap-balance-resume-test.sh` PASS (remap chunk at 6472859648 emptied); taint 0, no KASAN | misc-next zb64 d4cc0ed0de64 (7.3.0+ #71, KASAN + lockdep + BTRFS_ASSERT, md5 6f355e6b685de61483e8dd34dee0c876), dhive |
+
+#### Not covered / open (patch 2)
+
+- The other bail-outs (cancel, chunk search or relocation errors, a
+ failed allocation of a later entry) share the error label but are not
+ driven.
+- The negative control ran on the base kernel without any topic, not on
+ #5 with only this patch reverted; the test targets this patch's code
+ path, but the base differs in more than that patch (it also lacks
+ balance-resume's vrange bound, which is why its remap chunk survived the
+ resume).
+
## Not covered / open
- `balance-resume-test.sh` and `balance-resume-reuse-test.sh` were not run
+++ /dev/null
-From d0136e6493082d001aa33ed4174dae6062abb49a Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Mon, 9 Nov 2026 23:27:03 -0500
-Subject: [PATCH 0/1] btrfs: free queued remap chunks when balance bails out
-To: linux-btrfs@vger.kernel.org
-
-On a filesystem with the remap tree, a balance that stops early leaks
-memory. __btrfs_balance() puts each METADATA_REMAP chunk it selects on
-a local list and relocates those chunks only after it has walked all the
-others, in balance_remap_chunks(), which also frees the list entries. A
-walk that leaves the loop through the error label (a pause or cancel
-request, or an error during the walk) never gets there, and every entry
-queued so far is leaked: one small allocation per selected remap chunk,
-each time a balance is paused, canceled or fails.
-
-The patch frees the leftover entries at the error label. Nothing else
-needs undoing: the block group lookup, its reference and the read-only
-count are all taken later, in balance_remap_chunks(), so a queued entry
-holds no block group and leaves none read-only.
-
-Tested on KASAN kernels by tracing the allocations and frees of the list
-entries while a full balance on a remap-tree filesystem is paused after
-its walk has queued a remap chunk, then remounted and resumed. With the
-patch every entry is freed, the paused walk's at the error label;
-without it the paused walk's entry is never freed. Only the pause is
-driven; cancel and the error exits leave through the same label.
-
-Zygo Blaxell (1):
- btrfs: free queued remap chunks when balance bails out
-
- fs/btrfs/volumes.c | 8 ++++++++
- 1 file changed, 8 insertions(+)
-
-
---
-2.47.3
-
+++ /dev/null
-From d0136e6493082d001aa33ed4174dae6062abb49a Mon Sep 17 00:00:00 2001
-From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
-Date: Tue, 13 Oct 2026 01:05:21 -0400
-Subject: [PATCH 1/1] btrfs: free queued remap chunks when balance bails out
-To: linux-btrfs@vger.kernel.org
-
-__btrfs_balance() queues METADATA_REMAP chunks on a local list and
-balances them only after the main loop, in balance_remap_chunks(), which
-also frees the entries. Every path that leaves the loop through the
-error label skips that call: a pause or cancel request, a chunk tree
-search error, should_balance_chunk() failing, btrfs_may_alloc_data_chunk()
-or relocation errors, or the allocation of a later entry failing. The
-entries queued so far are leaked.
-
-Free the leftover entries at the error label. Nothing else needs undoing:
-the block group lookup, the reference and the read-only count are all
-taken inside balance_remap_chunks(), so a queued entry holds no block
-group and none is left read-only.
-
-Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
----
- fs/btrfs/volumes.c | 8 ++++++++
- 1 file changed, 8 insertions(+)
-
-diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 3d4ef6725de0e..788f60910acf5 100644
---- a/fs/btrfs/volumes.c
-+++ b/fs/btrfs/volumes.c
-@@ -4756,6 +4756,14 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
- }
- }
- error:
-+ /* Entries left over when the loop bailed out before balancing them. */
-+ while (!list_empty(&remap_chunks)) {
-+ rci = list_first_entry(&remap_chunks, struct remap_chunk_info,
-+ list);
-+ list_del(&rci->list);
-+ kfree(rci);
-+ }
-+
- if (enospc_errors) {
- btrfs_info(fs_info, "%d enospc errors during balance",
- enospc_errors);
---
-2.47.3
-
+++ /dev/null
-# Testing: remap-balance-leak
-
-The one patch frees the METADATA_REMAP chunks that `__btrfs_balance()`
-queues on a local list when the walk leaves the loop through the error
-label (pause, cancel, chunk search or relocation errors, a failed
-allocation of a later entry). Before it, only `balance_remap_chunks()`
-freed the entries, and the error label skipped it. To verify: a balance
-that bails out with remap chunks queued must free every entry, and a
-completed walk must still free its entries in `balance_remap_chunks()`.
-
-Until 2026-11-08 the patch was patch 1 of `balance-resume`; the results
-below are from that time. It does not depend on that series, textually
-or in behavior: it applies to the base on its own, and the lanes merge it
-right after `balance-resume`. The test drives the bail-out with
-`remap-balance-resume-test.sh`, whose pause and resume use the balance
-item; the test kernels had `balance-resume` too.
-
-Kernel under test, unless a row says otherwise: misc-next zb64
-568c45b64012 (uname 7.3.0-rc7+ #5, KASAN), dhive; logs under
-`/mnt/wh/t-dhive/logs/<test>-<stamp>/` while recent, copied to
-`raid56-wh-harness/forensics/t-dhive-topics-20261018/`. The negative
-control ran on the misc-next base 0c48d26d46bd without any topic (dhive
-KASAN build 1 of #5's config, bzImage md5
-83a4d5325c9a195746cf230032929c76).
-
-## Tests
-
-| test | verifies | patch(es) | duration | last result | date | kernel | harness | evidence |
-|---|---|---|---|---|---|---|---|---|
-| `remap-balance-leak-test.sh` | every queued `remap_chunk_info` is freed, including when the walk bails out at a pause | 1 | 171 s | PASS: 4 entries allocated in btrfs_balance, 4 freed; the paused walk's entry was freed from its own call site (btrfs_balance+0x1a26), the completed walks' from balance_remap_chunks (+0x4119) | 2026-10-18 | misc-next #5 | 0a72db0 | dhive `logs/remap-balance-leak-test-20261018-042918`, trace in `ev/remap-leak-1/trace.txt` |
-| `remap-balance-leak-test.sh` (negative control, `LEAK_ONLY=1`) | the same, on the base without the series | 1 | 172 s | FAIL as expected: 3 allocated, 1 unfreed (the paused walk's) | 2026-10-18 | misc-next base 0c48d26d46bd, dhive KASAN build 1 | 27b253a | dhive `logs/neg-remap-balance-leak-test-20261018-055959` |
-
-**remap-balance-leak-test.sh.** The test kernels have no kmemleak, so the
-test watches the allocations: a private tracefs instance records
-kmem:kmalloc events of 40 bytes (sizeof(struct remap_chunk_info)) whose
-call site is in btrfs_balance (`__btrfs_balance()` and
-`balance_remap_chunks()` are inlined into it) and every kmem:kfree called
-from btrfs_balance, while `remap-balance-resume-test.sh` runs. That test
-pauses a full balance after the walk has queued a METADATA_REMAP chunk --
-the bail-out that leaked -- then remounts and resumes. PASS needs the remap
-test to PASS (so the paused walk did queue the chunk), at least two
-allocations (the remap chunk move before the balance, and the paused walk),
-and a matching kfree for every allocation. Unfixed, the paused walk's entry
-is never freed. On #5 the paused walk's entry was freed from a different
-offset in btrfs_balance than the entries of the completed walks, which is
-the new free at the error label. **Shown to fail without the patch**: on
-the misc-next base 0c48d26d46bd (no topics; KASAN build of #5's config,
-dhive build 1, 2026-10-18), with `LEAK_ONLY=1` (the remap test is told to
-pause even without the vrange bound and its resume verdict is ignored), 3
-entries were allocated and 1 was never freed (allocated at
-btrfs_balance+0x2345, the paused walk's queue), and the remap chunk survived
-the resume, as expected without balance-resume's vrange bound
-(`logs/neg-remap-balance-leak-test-20261018-055959`). Limits: only the pause bail-out is driven, not
-cancel or the error exits (they share the error label); it depends on the
-inlining of both functions into btrfs_balance (a different inlining
-changes the call-site filter and the test then fails as blind with fewer
-than 2 allocations).
-
-Later results (the patch unchanged):
-
-| run | result | kernel |
-|---|---|---|
-| 2026-11-05 (br1105) | PASS on dhive and ehive; SKIP on 6.18 (no remap tree: the kernel cannot mount the fs), as expected | misc-next #67 |
-| 2026-11-06 (fl1106) | PASS on dhive and ehive, and on #67 (dhive); SKIP on 6.18 as expected | misc-next #68 |
-| 2026-11-09 (nc1108, as its own series merged after `balance-resume`) | PASS: 4 entries allocated in btrfs_balance, 0 unfreed; `remap-balance-resume-test.sh` PASS (the remap chunk at 6472859648 emptied by the resumed run); no KASAN, taint 0 | misc-next zb64 fb7546be6023 (7.3.0+ #8, KASAN + lockdep + BTRFS_ASSERT, md5 87693553da3b79a8991c445f42888365), ahive |
-| 2026-11-10 (lr1109, balance-resume gains its limit_min fix) | PASS: 4 entries allocated in btrfs_balance, 0 unfreed; `remap-balance-resume-test.sh` PASS (remap chunk at 6472859648 emptied); taint 0, no KASAN | misc-next zb64 d4cc0ed0de64 (7.3.0+ #71, KASAN + lockdep + BTRFS_ASSERT, md5 6f355e6b685de61483e8dd34dee0c876), dhive |
-
-## Not covered / open
-
-- The other bail-outs (cancel, chunk search or relocation errors, a
- failed allocation of a later entry) share the error label but are not
- driven.
-- The negative control ran on the base kernel without any topic, not on
- #5 with only this patch reverted; the test targets this patch's code
- path, but the base differs in more than that patch (it also lacks
- balance-resume's vrange bound, which is why its remap chunk survived the
- resume).