]> git.hungrycats.org Git - linux/commitdiff
btrfs: free queued remap chunks when balance bails out misc-next/topics/remap-balance-leak
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Tue, 13 Oct 2026 05:05:21 +0000 (01:05 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:11:50 +0000 (21:11 -0500)
__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
fs/btrfs/volumes.c

index 3d4ef6725de0e7e4e3894eae51d74d73ab3f82bf..788f60910acf50bdac293387a823ee7e132a0c00 100644 (file)
@@ -4756,6 +4756,14 @@ loop:
                }
        }
 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);