[PATCH 1/1] btrfs: free remap records when balance stops early

From: Xixin Liu

Date: Mon Sep 21 2026 - 04:55:51 EST


Balance queues metadata remap records on a stack list. They are freed
only after the remap pass. Early scan failures return with records still
linked, so they leak.

Free still-linked records on the early exit path. Reuse the same release
path as the remap pass end. Records without a block group are only
unlinked and freed. Records with a block group return that reference the
same way the existing end path already did.

Fixes: 81e5a4551c32 ("btrfs: allow balancing remap tree")
Signed-off-by: liuxixin <liuxixin@xxxxxxxxxx>
---
fs/btrfs/volumes.c | 65 ++++++++++++++++++++++++++---------------------
1 file changed, 36 insertions(+), 29 deletions(-)

diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 4ddabadc9..4c1e54c8d 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -4403,6 +4403,38 @@ static int cow_remap_tree(struct btrfs_trans_handle *trans, struct btrfs_path *p
return ret;
}

+static void free_balance_remap_chunks(struct list_head *chunks)
+{
+ struct remap_chunk_info *rci, *tmp;
+
+ list_for_each_entry_safe(rci, tmp, chunks, list) {
+ struct btrfs_block_group *bg = rci->bg;
+ bool is_unused;
+
+ if (!bg)
+ goto free_record;
+
+ /*
+ * This is a bit racy and the 'used' status can change
+ * but this is not a problem as later functions will
+ * verify it again.
+ */
+ spin_lock(&bg->lock);
+ is_unused = !btrfs_is_block_group_used(bg);
+ spin_unlock(&bg->lock);
+
+ if (is_unused)
+ btrfs_mark_bg_unused(bg);
+ if (rci->made_ro)
+ btrfs_dec_block_group_ro(bg);
+ btrfs_put_block_group(bg);
+
+free_record:
+ list_del(&rci->list);
+ kfree(rci);
+ }
+}
+
static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
struct list_head *chunks)
{
@@ -4442,35 +4474,7 @@ static int balance_remap_chunks(struct btrfs_fs_info *fs_info, struct btrfs_path
btrfs_commit_transaction(trans);

end:
- while (!list_empty(chunks)) {
- bool is_unused;
- struct btrfs_block_group *bg;
-
- rci = list_first_entry(chunks, struct remap_chunk_info, list);
-
- bg = rci->bg;
- if (bg) {
- /*
- * This is a bit racy and the 'used' status can change
- * but this is not a problem as later functions will
- * verify it again.
- */
- spin_lock(&bg->lock);
- is_unused = !btrfs_is_block_group_used(bg);
- spin_unlock(&bg->lock);
-
- if (is_unused)
- btrfs_mark_bg_unused(bg);
-
- if (rci->made_ro)
- btrfs_dec_block_group_ro(bg);
-
- btrfs_put_block_group(bg);
- }
-
- list_del(&rci->list);
- kfree(rci);
- }
+ free_balance_remap_chunks(chunks);

return ret;
}
@@ -4710,6 +4714,9 @@ static int __btrfs_balance(struct btrfs_fs_info *fs_info)
}
}
error:
+ if (!list_empty(&remap_chunks))
+ free_balance_remap_chunks(&remap_chunks);
+
if (enospc_errors) {
btrfs_info(fs_info, "%d enospc errors during balance",
enospc_errors);
--
2.43.0