Skip to content

Commit f390525

Browse files
Chen Ridonggregkh
authored andcommitted
cgroup/bpf: only cgroup v2 can be attached by bpf programs
[ Upstream commit 2190df6 ] Only cgroup v2 can be attached by bpf programs, so this patch introduces that cgroup_bpf_inherit and cgroup_bpf_offline can only be called in cgroup v2, and this can fix the memleak mentioned by commit 04f8ef5 ("cgroup: Fix memory leak caused by missing cgroup_bpf_offline"), which has been reverted. Fixes: 2b0d3d3 ("percpu_ref: reduce memory footprint of percpu_ref in fast path") Fixes: 4bfc0bb ("bpf: decouple the lifetime of cgroup_bpf from cgroup itself") Link: https://lore.kernel.org/cgroups/aka2hk5jsel5zomucpwlxsej6iwnfw4qu5jkrmjhyfhesjlfdw@46zxhg5bdnr7/ Signed-off-by: Chen Ridong <chenridong@huawei.com> Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent dd6ade9 commit f390525

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

kernel/cgroup/cgroup.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,8 +2140,10 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask)
21402140
if (ret)
21412141
goto exit_stats;
21422142

2143-
ret = cgroup_bpf_inherit(root_cgrp);
2144-
WARN_ON_ONCE(ret);
2143+
if (root == &cgrp_dfl_root) {
2144+
ret = cgroup_bpf_inherit(root_cgrp);
2145+
WARN_ON_ONCE(ret);
2146+
}
21452147

21462148
trace_cgroup_setup_root(root);
21472149

@@ -5708,9 +5710,11 @@ static struct cgroup *cgroup_create(struct cgroup *parent, const char *name,
57085710
if (ret)
57095711
goto out_kernfs_remove;
57105712

5711-
ret = cgroup_bpf_inherit(cgrp);
5712-
if (ret)
5713-
goto out_psi_free;
5713+
if (cgrp->root == &cgrp_dfl_root) {
5714+
ret = cgroup_bpf_inherit(cgrp);
5715+
if (ret)
5716+
goto out_psi_free;
5717+
}
57145718

57155719
/*
57165720
* New cgroup inherits effective freeze counter, and
@@ -6024,7 +6028,8 @@ static int cgroup_destroy_locked(struct cgroup *cgrp)
60246028

60256029
cgroup1_check_for_release(parent);
60266030

6027-
cgroup_bpf_offline(cgrp);
6031+
if (cgrp->root == &cgrp_dfl_root)
6032+
cgroup_bpf_offline(cgrp);
60286033

60296034
/* put the base reference */
60306035
percpu_ref_kill(&cgrp->self.refcnt);

0 commit comments

Comments
 (0)