From: Xixin Liu <liuxixin@kylinos.cn>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.com, mason@kernel.org, linux-kernel@vger.kernel.org,
liuxixin@kylinos.cn
Subject: [PATCH 1/1] btrfs: free remap records when balance stops early
Date: Mon, 21 Sep 2026 14:48:00 +0800 [thread overview]
Message-ID: <cb7acb099780.1789973253.git.liuxixin@kylinos.cn> (raw)
In-Reply-To: <cover.1789973253.git.liuxixin@kylinos.cn>
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@kylinos.cn>
---
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
prev parent reply other threads:[~2026-09-21 8:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-21 6:48 [PATCH 0/1] " Xixin Liu
2026-09-21 6:48 ` Xixin Liu [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cb7acb099780.1789973253.git.liuxixin@kylinos.cn \
--to=liuxixin@kylinos.cn \
--cc=dsterba@suse.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mason@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®