linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: Fix refcnt leak in btrfs_recover_relocation
@ 2020-04-20  5:39 Xiyu Yang
  2020-04-20 17:34 ` Filipe Manana
  0 siblings, 1 reply; 4+ messages in thread
From: Xiyu Yang @ 2020-04-20  5:39 UTC (permalink / raw)
  To: Chris Mason, Josef Bacik, David Sterba, Jeff Mahoney,
	linux-btrfs, linux-kernel
  Cc: yuanxzhang, kjlu, Xiyu Yang, Xin Tan

btrfs_recover_relocation() invokes btrfs_join_transaction(), which joins
a btrfs_trans_handle object into transactions and returns a reference of
it with increased refcount to "trans".

When btrfs_recover_relocation() returns, "trans" becomes invalid, so the
refcount should be decreased to keep refcount balanced.

The reference counting issue happens in one exception handling path of
btrfs_recover_relocation(). When read_fs_root() failed, the refcnt
increased by btrfs_join_transaction() is not decreased, causing a refcnt
leak.

Fix this issue by calling btrfs_end_transaction() on this error path
when read_fs_root() failed.

Fixes: 79787eaab461 ("btrfs: replace many BUG_ONs with proper error
handling")
Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
---
 fs/btrfs/relocation.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 995d4b8b1cfd..46a451594c7a 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -4606,6 +4606,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
 		if (IS_ERR(fs_root)) {
 			err = PTR_ERR(fs_root);
 			list_add_tail(&reloc_root->root_list, &reloc_roots);
+			btrfs_end_transaction(trans);
 			goto out_free;
 		}
 
-- 
2.7.4


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

* Re: [PATCH] btrfs: Fix refcnt leak in btrfs_recover_relocation
  2020-04-20  5:39 [PATCH] btrfs: Fix refcnt leak in btrfs_recover_relocation Xiyu Yang
@ 2020-04-20 17:34 ` Filipe Manana
  2020-04-20 17:35   ` Filipe Manana
  0 siblings, 1 reply; 4+ messages in thread
From: Filipe Manana @ 2020-04-20 17:34 UTC (permalink / raw)
  To: Xiyu Yang
  Cc: Chris Mason, Josef Bacik, David Sterba, Jeff Mahoney,
	linux-btrfs, linux-kernel, yuanxzhang, kjlu, Xin Tan

On Mon, Apr 20, 2020 at 6:50 AM Xiyu Yang <xiyuyang19@fudan.edu.cn> wrote:
>
> btrfs_recover_relocation() invokes btrfs_join_transaction(), which joins
> a btrfs_trans_handle object into transactions and returns a reference of
> it with increased refcount to "trans".
>
> When btrfs_recover_relocation() returns, "trans" becomes invalid, so the
> refcount should be decreased to keep refcount balanced.
>
> The reference counting issue happens in one exception handling path of
> btrfs_recover_relocation(). When read_fs_root() failed, the refcnt
> increased by btrfs_join_transaction() is not decreased, causing a refcnt
> leak.
>
> Fix this issue by calling btrfs_end_transaction() on this error path
> when read_fs_root() failed.
>
> Fixes: 79787eaab461 ("btrfs: replace many BUG_ONs with proper error
> handling")
> Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>

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

Looks good, thanks.

> ---
>  fs/btrfs/relocation.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> index 995d4b8b1cfd..46a451594c7a 100644
> --- a/fs/btrfs/relocation.c
> +++ b/fs/btrfs/relocation.c
> @@ -4606,6 +4606,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
>                 if (IS_ERR(fs_root)) {
>                         err = PTR_ERR(fs_root);
>                         list_add_tail(&reloc_root->root_list, &reloc_roots);
> +                       btrfs_end_transaction(trans);
>                         goto out_free;
>                 }
>
> --
> 2.7.4
>


-- 
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”

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

* Re: [PATCH] btrfs: Fix refcnt leak in btrfs_recover_relocation
  2020-04-20 17:34 ` Filipe Manana
@ 2020-04-20 17:35   ` Filipe Manana
  2020-04-20 22:32     ` David Sterba
  0 siblings, 1 reply; 4+ messages in thread
From: Filipe Manana @ 2020-04-20 17:35 UTC (permalink / raw)
  To: Xiyu Yang
  Cc: Chris Mason, Josef Bacik, David Sterba, Jeff Mahoney,
	linux-btrfs, linux-kernel, yuanxzhang, kjlu, Xin Tan

On Mon, Apr 20, 2020 at 6:34 PM Filipe Manana <fdmanana@gmail.com> wrote:
>
> On Mon, Apr 20, 2020 at 6:50 AM Xiyu Yang <xiyuyang19@fudan.edu.cn> wrote:
> >
> > btrfs_recover_relocation() invokes btrfs_join_transaction(), which joins
> > a btrfs_trans_handle object into transactions and returns a reference of
> > it with increased refcount to "trans".
> >
> > When btrfs_recover_relocation() returns, "trans" becomes invalid, so the
> > refcount should be decreased to keep refcount balanced.
> >
> > The reference counting issue happens in one exception handling path of
> > btrfs_recover_relocation(). When read_fs_root() failed, the refcnt
> > increased by btrfs_join_transaction() is not decreased, causing a refcnt
> > leak.
> >
> > Fix this issue by calling btrfs_end_transaction() on this error path
> > when read_fs_root() failed.
> >
> > Fixes: 79787eaab461 ("btrfs: replace many BUG_ONs with proper error
> > handling")
> > Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> > Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
>
> Reviewed-by: Filipe Manana <fdmanana@suse.com>
>
> Looks good, thanks.

Btw, the subject could be more clear.
Instead of

"btrfs: Fix refcnt leak in btrfs_recover_relocation"

something like

"btrfs: fix transaction leak in ..."

David can probably fixup that when he picks the patch.

Thanks.

>
> > ---
> >  fs/btrfs/relocation.c | 1 +
> >  1 file changed, 1 insertion(+)
> >
> > diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
> > index 995d4b8b1cfd..46a451594c7a 100644
> > --- a/fs/btrfs/relocation.c
> > +++ b/fs/btrfs/relocation.c
> > @@ -4606,6 +4606,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
> >                 if (IS_ERR(fs_root)) {
> >                         err = PTR_ERR(fs_root);
> >                         list_add_tail(&reloc_root->root_list, &reloc_roots);
> > +                       btrfs_end_transaction(trans);
> >                         goto out_free;
> >                 }
> >
> > --
> > 2.7.4
> >
>
>
> --
> Filipe David Manana,
>
> “Whether you think you can, or you think you can't — you're right.”



-- 
Filipe David Manana,

“Whether you think you can, or you think you can't — you're right.”

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

* Re: [PATCH] btrfs: Fix refcnt leak in btrfs_recover_relocation
  2020-04-20 17:35   ` Filipe Manana
@ 2020-04-20 22:32     ` David Sterba
  0 siblings, 0 replies; 4+ messages in thread
From: David Sterba @ 2020-04-20 22:32 UTC (permalink / raw)
  To: Filipe Manana
  Cc: Xiyu Yang, Chris Mason, Josef Bacik, David Sterba, Jeff Mahoney,
	linux-btrfs, linux-kernel, yuanxzhang, kjlu, Xin Tan

On Mon, Apr 20, 2020 at 06:35:56PM +0100, Filipe Manana wrote:
> On Mon, Apr 20, 2020 at 6:34 PM Filipe Manana <fdmanana@gmail.com> wrote:
> >
> > On Mon, Apr 20, 2020 at 6:50 AM Xiyu Yang <xiyuyang19@fudan.edu.cn> wrote:
> > >
> > > btrfs_recover_relocation() invokes btrfs_join_transaction(), which joins
> > > a btrfs_trans_handle object into transactions and returns a reference of
> > > it with increased refcount to "trans".
> > >
> > > When btrfs_recover_relocation() returns, "trans" becomes invalid, so the
> > > refcount should be decreased to keep refcount balanced.
> > >
> > > The reference counting issue happens in one exception handling path of
> > > btrfs_recover_relocation(). When read_fs_root() failed, the refcnt
> > > increased by btrfs_join_transaction() is not decreased, causing a refcnt
> > > leak.
> > >
> > > Fix this issue by calling btrfs_end_transaction() on this error path
> > > when read_fs_root() failed.
> > >
> > > Fixes: 79787eaab461 ("btrfs: replace many BUG_ONs with proper error
> > > handling")
> > > Signed-off-by: Xiyu Yang <xiyuyang19@fudan.edu.cn>
> > > Signed-off-by: Xin Tan <tanxin.ctf@gmail.com>
> >
> > Reviewed-by: Filipe Manana <fdmanana@suse.com>
> >
> > Looks good, thanks.
> 
> Btw, the subject could be more clear.
> Instead of
> 
> "btrfs: Fix refcnt leak in btrfs_recover_relocation"
> 
> something like
> 
> "btrfs: fix transaction leak in ..."
> 
> David can probably fixup that when he picks the patch.

Yes, thanks, simple fixups are fine.

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

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

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-20  5:39 [PATCH] btrfs: Fix refcnt leak in btrfs_recover_relocation Xiyu Yang
2020-04-20 17:34 ` Filipe Manana
2020-04-20 17:35   ` Filipe Manana
2020-04-20 22:32     ` David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).