]> git.hungrycats.org Git - linux/commitdiff
btrfs: free queued remap chunks when balance bails out 7.3/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>
Sun, 8 Nov 2026 06:08:41 +0000 (01:08 -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 85ea9c5d4536c8a6357977b543bc1409ad77cfcf..57d1df29f8f0431c3f50f91dff93665da796e6ab 100644 (file)
@@ -4706,6 +4706,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);