All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
@ 2020-04-17 14:40 fdmanana
  2020-04-17 15:22 ` David Sterba
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: fdmanana @ 2020-04-17 14:40 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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")
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/block-group.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 47f66c6a7d7f..93c180ffcb80 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -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;
 }
-- 
2.11.0


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

* Re: [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-17 14:40 [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group fdmanana
@ 2020-04-17 15:22 ` David Sterba
  2020-04-17 15:26   ` Filipe Manana
  2020-04-17 15:23 ` Nikolay Borisov
  2020-04-17 15:36 ` [PATCH v2 " fdmanana
  2 siblings, 1 reply; 8+ messages in thread
From: David Sterba @ 2020-04-17 15:22 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs

On Fri, Apr 17, 2020 at 03:40:12PM +0100, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> 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")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Added to misc-next, thanks.

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

* Re: [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-17 14:40 [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group fdmanana
  2020-04-17 15:22 ` David Sterba
@ 2020-04-17 15:23 ` Nikolay Borisov
  2020-04-17 15:36 ` [PATCH v2 " fdmanana
  2 siblings, 0 replies; 8+ messages in thread
From: Nikolay Borisov @ 2020-04-17 15:23 UTC (permalink / raw)
  To: fdmanana, linux-btrfs



On 17.04.20 г. 17:40 ч., fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> 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")
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

Reviewed-by: Nikolay Borisov <nborisov@suse.com>

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

* Re: [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-17 15:22 ` David Sterba
@ 2020-04-17 15:26   ` Filipe Manana
  0 siblings, 0 replies; 8+ messages in thread
From: Filipe Manana @ 2020-04-17 15:26 UTC (permalink / raw)
  To: dsterba, Filipe Manana, linux-btrfs

On Fri, Apr 17, 2020 at 4:23 PM David Sterba <dsterba@suse.cz> wrote:
>
> On Fri, Apr 17, 2020 at 03:40:12PM +0100, fdmanana@kernel.org wrote:
> > From: Filipe Manana <fdmanana@suse.com>
> >
> > 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")
> > Signed-off-by: Filipe Manana <fdmanana@suse.com>
>
> Added to misc-next, thanks.

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

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

* [PATCH v2 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-17 14:40 [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group fdmanana
  2020-04-17 15:22 ` David Sterba
  2020-04-17 15:23 ` Nikolay Borisov
@ 2020-04-17 15:36 ` fdmanana
  2020-04-17 16:41   ` David Sterba
  2020-04-21  8:05   ` Filipe Manana
  2 siblings, 2 replies; 8+ messages in thread
From: fdmanana @ 2020-04-17 15:36 UTC (permalink / raw)
  To: linux-btrfs

From: Filipe Manana <fdmanana@suse.com>

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.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---

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(+)

diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
index 47f66c6a7d7f..af9e9a008724 100644
--- a/fs/btrfs/block-group.c
+++ b/fs/btrfs/block-group.c
@@ -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


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

* Re: [PATCH v2 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-17 15:36 ` [PATCH v2 " fdmanana
@ 2020-04-17 16:41   ` David Sterba
  2020-04-21  8:05   ` Filipe Manana
  1 sibling, 0 replies; 8+ messages in thread
From: David Sterba @ 2020-04-17 16:41 UTC (permalink / raw)
  To: fdmanana; +Cc: linux-btrfs, nborisov

On Fri, Apr 17, 2020 at 04:36:15PM +0100, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
> 
> 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.
> 
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
> ---
> 
> 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.

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

* Re: [PATCH v2 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-17 15:36 ` [PATCH v2 " fdmanana
  2020-04-17 16:41   ` David Sterba
@ 2020-04-21  8:05   ` Filipe Manana
  2020-04-22 22:57     ` David Sterba
  1 sibling, 1 reply; 8+ messages in thread
From: Filipe Manana @ 2020-04-21  8:05 UTC (permalink / raw)
  To: linux-btrfs

On Fri, Apr 17, 2020 at 4:38 PM <fdmanana@kernel.org> wrote:
>
> From: Filipe Manana <fdmanana@suse.com>
>
> 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.
>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>

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

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

> ---
>
> 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(+)
>
> diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c
> index 47f66c6a7d7f..af9e9a008724 100644
> --- a/fs/btrfs/block-group.c
> +++ b/fs/btrfs/block-group.c
> @@ -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
>

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

* Re: [PATCH v2 1/2] Btrfs: fix memory leak of transaction when deleting unused block group
  2020-04-21  8:05   ` Filipe Manana
@ 2020-04-22 22:57     ` David Sterba
  0 siblings, 0 replies; 8+ messages in thread
From: David Sterba @ 2020-04-22 22:57 UTC (permalink / raw)
  To: Filipe Manana; +Cc: linux-btrfs

On Tue, Apr 21, 2020 at 09:05:33AM +0100, Filipe Manana wrote:
> On Fri, Apr 17, 2020 at 4:38 PM <fdmanana@kernel.org> wrote:
> >
> > From: Filipe Manana <fdmanana@suse.com>
> >
> > 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.
> >
> > Signed-off-by: Filipe Manana <fdmanana@suse.com>
> 
> Fixes: fe119a6eeb6705 ("btrfs: switch to per-transaction pinned extents")
> 
> And missed that in v2, but had it in v1.

Patch updated, thanks.

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

end of thread, other threads:[~2020-04-22 22:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-17 14:40 [PATCH 1/2] Btrfs: fix memory leak of transaction when deleting unused block group fdmanana
2020-04-17 15:22 ` David Sterba
2020-04-17 15:26   ` Filipe Manana
2020-04-17 15:23 ` Nikolay Borisov
2020-04-17 15:36 ` [PATCH v2 " fdmanana
2020-04-17 16:41   ` David Sterba
2020-04-21  8:05   ` Filipe Manana
2020-04-22 22:57     ` David Sterba

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.