Skip to contentSkip to conversation

btrfs: free remap records when balance stops early

PATCH New Xixin Liu 1 participant 2 messages Sep 21 Btrfs file system

lore
Share & tools
lore

Xixin Liu

author
#0 lore raw

cover btrfs: free remap records when balance stops early

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(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/1 btrfs: free remap records when balance stops early

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")
---
 fs/btrfs/volumes.c | 65 ++++++++++++++++++++++++++---------------------
 1 file changed, 36 insertions(+), 29 deletions(-)
fs/btrfs/volumes.c +36 -29
index 4ddabadc9..4c1e54c8d 100644
@@ -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);
Signature · 1 line
2.43.0