2021-10-07 12:18:46

by quanyang wang

[permalink] [raw]
Subject: [PATCH] cgroup: fix memory leak caused by missing cgroup_bpf_offline

From: Quanyang Wang <[email protected]>

When enabling CONFIG_CGROUP_BPF, kmemleak can be observed by running
the command as below:

$mount -t cgroup -o none,name=foo cgroup cgroup/
$umount cgroup/

unreferenced object 0xc3585c40 (size 64):
comm "mount", pid 425, jiffies 4294959825 (age 31.990s)
hex dump (first 32 bytes):
01 00 00 80 84 8c 28 c0 00 00 00 00 00 00 00 00 ......(.........
00 00 00 00 00 00 00 00 6c 43 a0 c3 00 00 00 00 ........lC......
backtrace:
[<e95a2f9e>] cgroup_bpf_inherit+0x44/0x24c
[<1f03679c>] cgroup_setup_root+0x174/0x37c
[<ed4b0ac5>] cgroup1_get_tree+0x2c0/0x4a0
[<f85b12fd>] vfs_get_tree+0x24/0x108
[<f55aec5c>] path_mount+0x384/0x988
[<e2d5e9cd>] do_mount+0x64/0x9c
[<208c9cfe>] sys_mount+0xfc/0x1f4
[<06dd06e0>] ret_fast_syscall+0x0/0x48
[<a8308cb3>] 0xbeb4daa8

This is because that root_cgrp->bpf.refcnt.data is allocated by the
function percpu_ref_init in cgroup_bpf_inherit which is called by
cgroup_setup_root when mounting, but not freed along with root_cgrp
when umounting. Adding cgroup_bpf_offline which calls percpu_ref_kill
to cgroup_kill_sb can free root_cgrp->bpf.refcnt.data in umount path.

Fixes: 2b0d3d3e4fcfb ("percpu_ref: reduce memory footprint of percpu_ref in fast path")
Signed-off-by: Quanyang Wang <[email protected]>
---
kernel/cgroup/cgroup.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c
index c8b811e039cc2..ce636deec5e41 100644
--- a/kernel/cgroup/cgroup.c
+++ b/kernel/cgroup/cgroup.c
@@ -2147,8 +2147,10 @@ static void cgroup_kill_sb(struct super_block *sb)
* And don't kill the default root.
*/
if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
- !percpu_ref_is_dying(&root->cgrp.self.refcnt))
+ !percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
+ cgroup_bpf_offline(&root->cgrp);
percpu_ref_kill(&root->cgrp.self.refcnt);
+ }
cgroup_put(&root->cgrp);
kernfs_kill_sb(sb);
}
--
2.25.1


2021-10-11 16:43:02

by Michal Koutný

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix memory leak caused by missing cgroup_bpf_offline

Hello.

On Thu, Oct 07, 2021 at 08:16:03PM +0800, [email protected] wrote:
> This is because that root_cgrp->bpf.refcnt.data is allocated by the
> function percpu_ref_init in cgroup_bpf_inherit which is called by
> cgroup_setup_root when mounting, but not freed along with root_cgrp
> when umounting.

Good catch!

> Adding cgroup_bpf_offline which calls percpu_ref_kill to
> cgroup_kill_sb can free root_cgrp->bpf.refcnt.data in umount path.

That is sensible.

> Fixes: 2b0d3d3e4fcfb ("percpu_ref: reduce memory footprint of percpu_ref in fast path")

Why this Fixes:? Is the leak absent before the percpu_ref refactoring?
I guess the embedded data are free'd together with cgroup. Makes me
wonder why struct cgroup_bpf has a separate percpu_ref counter from
struct cgroup...

> +++ b/kernel/cgroup/cgroup.c
> @@ -2147,8 +2147,10 @@ static void cgroup_kill_sb(struct super_block *sb)
> * And don't kill the default root.
> */
> if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
> - !percpu_ref_is_dying(&root->cgrp.self.refcnt))
> + !percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
> + cgroup_bpf_offline(&root->cgrp);

(You made some unnecessary whitespace here breaking indention :-)

> percpu_ref_kill(&root->cgrp.self.refcnt);
> + }
> cgroup_put(&root->cgrp);
> kernfs_kill_sb(sb);
> }

2021-10-11 16:43:47

by Roman Gushchin

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix memory leak caused by missing cgroup_bpf_offline

On Mon, Oct 11, 2021 at 06:21:28PM +0200, Michal Koutny wrote:
> Hello.
>
> On Thu, Oct 07, 2021 at 08:16:03PM +0800, [email protected] wrote:
> > This is because that root_cgrp->bpf.refcnt.data is allocated by the
> > function percpu_ref_init in cgroup_bpf_inherit which is called by
> > cgroup_setup_root when mounting, but not freed along with root_cgrp
> > when umounting.
>
> Good catch!

+1

>
> > Adding cgroup_bpf_offline which calls percpu_ref_kill to
> > cgroup_kill_sb can free root_cgrp->bpf.refcnt.data in umount path.
>
> That is sensible.
>
> > Fixes: 2b0d3d3e4fcfb ("percpu_ref: reduce memory footprint of percpu_ref in fast path")
>
> Why this Fixes:? Is the leak absent before the percpu_ref refactoring?

I agree, the "fixes" tag looks dubious to me.

> I guess the embedded data are free'd together with cgroup. Makes me
> wonder why struct cgroup_bpf has a separate percpu_ref counter from
> struct cgroup...

This is because a cgroup can stay a long time (sometimes effectively forever)
in a dying state, so we want to release bpf structures earlier.

Thanks!

2021-10-12 06:23:59

by quanyang wang

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix memory leak caused by missing cgroup_bpf_offline

Hi Michal & Roman,

Thank you for your review.

On 10/12/21 12:21 AM, Michal Koutný wrote:
> Hello.
>
> On Thu, Oct 07, 2021 at 08:16:03PM +0800, [email protected] wrote:
>> This is because that root_cgrp->bpf.refcnt.data is allocated by the
>> function percpu_ref_init in cgroup_bpf_inherit which is called by
>> cgroup_setup_root when mounting, but not freed along with root_cgrp
>> when umounting.
>
> Good catch!
>
>> Adding cgroup_bpf_offline which calls percpu_ref_kill to
>> cgroup_kill_sb can free root_cgrp->bpf.refcnt.data in umount path.
>
> That is sensible.
>
>> Fixes: 2b0d3d3e4fcfb ("percpu_ref: reduce memory footprint of percpu_ref in fast path")
>
> Why this Fixes:? Is the leak absent before the percpu_ref refactoring?
Before this commit, percpu_ref is embedded in cgroup, it can be freed
along with cgroup, so there is no memory leak. Since this commit, it
causes the memory leak.
Should I change it to "Fixes: 4bfc0bb2c60e ("bpf: decouple the lifetime
of cgroup_bpf from cgroup itself")"?
> I guess the embedded data are free'd together with cgroup. Makes me
> wonder why struct cgroup_bpf has a separate percpu_ref counter from
> struct cgroup...
>
>> +++ b/kernel/cgroup/cgroup.c
>> @@ -2147,8 +2147,10 @@ static void cgroup_kill_sb(struct super_block *sb)
>> * And don't kill the default root.
>> */
>> if (list_empty(&root->cgrp.self.children) && root != &cgrp_dfl_root &&
>> - !percpu_ref_is_dying(&root->cgrp.self.refcnt))
>> + !percpu_ref_is_dying(&root->cgrp.self.refcnt)) {
>> + cgroup_bpf_offline(&root->cgrp);
>
> (You made some unnecessary whitespace here breaking indention :-)
Thanks for pointing it out. I will send a V2 to fix this.

Thanks,
Quanyang
>
>> percpu_ref_kill(&root->cgrp.self.refcnt);
>> + }
>> cgroup_put(&root->cgrp);
>> kernfs_kill_sb(sb);
>> }

2021-10-12 09:37:41

by Michal Koutný

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix memory leak caused by missing cgroup_bpf_offline

On Tue, Oct 12, 2021 at 02:22:13PM +0800, Quanyang Wang <[email protected]> wrote:
> Before this commit, percpu_ref is embedded in cgroup, it can be freed along
> with cgroup, so there is no memory leak. Since this commit, it causes the
> memory leak.
> Should I change it to "Fixes: 4bfc0bb2c60e ("bpf: decouple the lifetime of
> cgroup_bpf from cgroup itself")"?

I see. The leak is a product so I'd tag both of them and explain it in
the commit message.

Thank you,
Michal

2021-10-22 15:54:49

by John Fastabend

[permalink] [raw]
Subject: Re: [PATCH] cgroup: fix memory leak caused by missing cgroup_bpf_offline

Roman Gushchin wrote:
> On Mon, Oct 11, 2021 at 06:21:28PM +0200, Michal Koutny wrote:
> > Hello.
> >
> > On Thu, Oct 07, 2021 at 08:16:03PM +0800, [email protected] wrote:
> > > This is because that root_cgrp->bpf.refcnt.data is allocated by the
> > > function percpu_ref_init in cgroup_bpf_inherit which is called by
> > > cgroup_setup_root when mounting, but not freed along with root_cgrp
> > > when umounting.
> >
> > Good catch!
>
> +1
>
> >
> > > Adding cgroup_bpf_offline which calls percpu_ref_kill to
> > > cgroup_kill_sb can free root_cgrp->bpf.refcnt.data in umount path.
> >
> > That is sensible.
> >
> > > Fixes: 2b0d3d3e4fcfb ("percpu_ref: reduce memory footprint of percpu_ref in fast path")
> >
> > Why this Fixes:? Is the leak absent before the percpu_ref refactoring?
>
> I agree, the "fixes" tag looks dubious to me.
>
> > I guess the embedded data are free'd together with cgroup. Makes me
> > wonder why struct cgroup_bpf has a separate percpu_ref counter from
> > struct cgroup...
>
> This is because a cgroup can stay a long time (sometimes effectively forever)
> in a dying state, so we want to release bpf structures earlier.
>
> Thanks!

Other than whitespace LGTM.

Acked-by: John Fastabend <[email protected]>