__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")
Assisted-by: Claude:claude-opus-5-5
}
}
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);