Skip to contentSkip to conversation

Btrfs: fix memory leak of transaction when deleting unused block group

PATCH Superseded [email protected] 3 participants 8 messages Apr 17, 2020 – Apr 22, 2020 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Btrfs: fix memory leak of transaction when deleting unused block group

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Fix it by decrementing the reference count.

Fixes fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")
---
 fs/btrfs/block-group.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/block-group.c +4 -0
index 47f66c6a7d7f..93c180ffcb80 100644
@@ -1288,11 +1288,15 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
 	if (ret)
 		goto err;
 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
+	if (prev_trans)
+		refcount_dec(&prev_trans->use_count);
 
 	return true;
 
 err:
 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
+	if (prev_trans)
+		refcount_dec(&prev_trans->use_count);
 	btrfs_dec_block_group_ro(bg);
 	return false;
 }
Signature · 1 line
2.11.0
#1 lore raw
Quoted 8 lines · On Fri, Apr 17, 2020 at 03:40:12PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Quoted 4 lines

Fix it by decrementing the reference count.

Fixes fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")

Added to misc-next, thanks.

#2 lore raw
Quoted 10 lines · David Sterba
Quoted 8 lines · On Fri, Apr 17, 2020 at 03:40:12PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Quoted 6 lines
Quoted 4 lines

Fix it by decrementing the reference count.

Fixes fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")

Added to misc-next, thanks.

This is actually wrong, I'll send a v2 soon.

Reviewed-by on v1 1/2
#3 lore raw
In reply to [email protected]
Quoted 8 lines · On 17.04.20 г. 17:40 ч., [email protected] wrote:

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Quoted 4 lines

Fix it by decrementing the reference count.

Fixes fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")
#4 lore raw

PATCH v2 1/2 Btrfs: fix memory leak of transaction when deleting unused block group

In reply to [email protected]

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Fix it by decrementing the reference count.

---

V2: Use btrfs_put_transaction() and not refcount_dec(), otherwise we are
not really releasing the memory used by the transaction in case its refcount is 1. Stupid mistake.

 fs/btrfs/block-group.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/block-group.c +4 -0
index 47f66c6a7d7f..af9e9a008724 100644
@@ -1288,11 +1288,15 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
 	if (ret)
 		goto err;
 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
+	if (prev_trans)
+		btrfs_put_transaction(prev_trans);
 
 	return true;
 
 err:
 	mutex_unlock(&fs_info->unused_bg_unpin_mutex);
+	if (prev_trans)
+		btrfs_put_transaction(prev_trans);
 	btrfs_dec_block_group_ro(bg);
 	return false;
 }
Signature · 1 line
2.11.0
#5 lore raw
Quoted 8 lines · On Fri, Apr 17, 2020 at 04:36:15PM +0100, [email protected] wrote:

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Quoted 8 lines

Fix it by decrementing the reference count.

---

V2: Use btrfs_put_transaction() and not refcount_dec(), otherwise we are
not really releasing the memory used by the transaction in case its refcount is 1. Stupid mistake.

I missed it too. As Nik pointed out on IRC, the API should be consistent so that for put there's a get, or refcount_inc/refcount_dec. In this case there's non-trivial work on the put side, so the wrappers make sense.

A good example may be btrfs_get_block_group/btrfs_put_block_group, same as for the transaction.

Trivial wrappers around plain _inc/_dec wouldn't be so great, but in case there's eg refcount_dec_and_test + kfree, then I think this can be considered a good pattern.

#6 lore raw
In reply to [email protected]
Quoted 9 lines · On Fri, Apr 17, 2020 at 4:38 PM <[email protected]> wrote:

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Quoted 3 lines

Fix it by decrementing the reference count.

Fixes fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")

And missed that in v2, but had it in v1.

Quoted 32 lines

---

V2: Use btrfs_put_transaction() and not refcount_dec(), otherwise we are
not really releasing the memory used by the transaction in case its refcount is 1. Stupid mistake.

 fs/btrfs/block-group.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/btrfs/block-group.c +4 -1
index 47f66c6a7d7f..af9e9a008724 100644
@@ -1288,11 +1288,15 @@ static bool clean_pinned_extents(struct btrfs_trans_handle *trans,
        if (ret)
                goto err;
        mutex_unlock(&fs_info->unused_bg_unpin_mutex);
+       if (prev_trans)
+               btrfs_put_transaction(prev_trans);
 
        return true;
 
 err:
        mutex_unlock(&fs_info->unused_bg_unpin_mutex);
+       if (prev_trans)
+               btrfs_put_transaction(prev_trans);
        btrfs_dec_block_group_ro(bg);
        return false;
 }
--

2.11.0

#7 lore raw
Quoted 10 lines · Filipe Manana
Quoted 9 lines · On Fri, Apr 17, 2020 at 4:38 PM <[email protected]> wrote:

From: Filipe Manana <[email protected]>

When cleaning pinned extents right before deleting an unused block group, we check if there's still a previous transaction running and if so we increment its reference count before using it for cleaning pinned ranges in its pinned extents iotree. However we ended up never decrementing the reference count after using the transaction, resulting in a memory leak.

Quoted 7 lines
Quoted 3 lines

Fix it by decrementing the reference count.

Fixes fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")

And missed that in v2, but had it in v1.

Patch updated, thanks.