mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/1] btrfs: free remap records when balance stops early
@ 2026-09-21  6:48 Xixin Liu
  2026-09-21  6:48 ` [PATCH 1/1] " Xixin Liu
  0 siblings, 1 reply; 2+ messages in thread
From: Xixin Liu @ 2026-09-21  6:48 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, mason, linux-kernel, liuxixin

Hi,

This patch frees leaked remap records when balance stops early. Early
error returns left kmalloc records on the stack list. Share one free
helper at the remap pass end and on error when the list is non-empty.

Tested on linux-next next-20260914 for cancel and complete cases.

Thanks,
Xixin Liu

---
liuxixin (1):
  btrfs: free remap records when balance stops early

 fs/btrfs/volumes.c | 65 ++++++++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 29 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH 1/1] btrfs: free remap records when balance stops early
  2026-09-21  6:48 [PATCH 0/1] btrfs: free remap records when balance stops early Xixin Liu
@ 2026-09-21  6:48 ` Xixin Liu
  0 siblings, 0 replies; 2+ messages in thread
From: Xixin Liu @ 2026-09-21  6:48 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba, mason, linux-kernel, liuxixin

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-21  8:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-21  6:48 [PATCH 0/1] btrfs: free remap records when balance stops early Xixin Liu
2026-09-21  6:48 ` [PATCH 1/1] " Xixin Liu

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®