Skip to content

Commit 3607692

Browse files
laoarAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add selftests for cgroup1 hierarchy
Add selftests for cgroup1 hierarchy. The result as follows, $ tools/testing/selftests/bpf/test_progs --name=cgroup1_hierarchy #36/1 cgroup1_hierarchy/test_cgroup1_hierarchy:OK #36/2 cgroup1_hierarchy/test_root_cgid:OK #36/3 cgroup1_hierarchy/test_invalid_level:OK #36/4 cgroup1_hierarchy/test_invalid_cgid:OK #36/5 cgroup1_hierarchy/test_invalid_hid:OK #36/6 cgroup1_hierarchy/test_invalid_cgrp_name:OK #36/7 cgroup1_hierarchy/test_invalid_cgrp_name2:OK #36/8 cgroup1_hierarchy/test_sleepable_prog:OK #36 cgroup1_hierarchy:OK Summary: 1/8 PASSED, 0 SKIPPED, 0 FAILED Besides, I also did some stress test similar to the patch #2 in this series, as follows (with CONFIG_PROVE_RCU_LIST enabled): - Continuously mounting and unmounting named cgroups in some tasks, for example: cgrp_name=$1 while true do mount -t cgroup -o none,name=$cgrp_name none /$cgrp_name umount /$cgrp_name done - Continuously run this selftest concurrently, while true; do ./test_progs --name=cgroup1_hierarchy; done They can ran successfully without any RCU warnings in dmesg. Signed-off-by: Yafang Shao <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent bf47300 commit 3607692

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023 Yafang Shao <[email protected]> */
3+
4+
#include <sys/types.h>
5+
#include <unistd.h>
6+
#include <test_progs.h>
7+
#include "cgroup_helpers.h"
8+
#include "test_cgroup1_hierarchy.skel.h"
9+
10+
static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel)
11+
{
12+
struct bpf_link *lsm_link, *fentry_link;
13+
int err;
14+
15+
/* Attach LSM prog first */
16+
lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);
17+
if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
18+
return;
19+
20+
/* LSM prog will be triggered when attaching fentry */
21+
fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
22+
ASSERT_NULL(fentry_link, "fentry_attach_fail");
23+
24+
err = bpf_link__destroy(lsm_link);
25+
ASSERT_OK(err, "destroy_lsm");
26+
}
27+
28+
static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel)
29+
{
30+
struct bpf_link *lsm_link, *fentry_link;
31+
int err;
32+
33+
/* Attach LSM prog first */
34+
lsm_link = bpf_program__attach_lsm(skel->progs.lsm_s_run);
35+
if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
36+
return;
37+
38+
/* LSM prog will be triggered when attaching fentry */
39+
fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
40+
ASSERT_NULL(fentry_link, "fentry_attach_fail");
41+
42+
err = bpf_link__destroy(lsm_link);
43+
ASSERT_OK(err, "destroy_lsm");
44+
}
45+
46+
static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel)
47+
{
48+
struct bpf_link *lsm_link, *fentry_link;
49+
int err;
50+
51+
/* Attach LSM prog first */
52+
lsm_link = bpf_program__attach_lsm(skel->progs.lsm_run);
53+
if (!ASSERT_OK_PTR(lsm_link, "lsm_attach"))
54+
return;
55+
56+
/* LSM prog will be triggered when attaching fentry */
57+
fentry_link = bpf_program__attach_trace(skel->progs.fentry_run);
58+
if (!ASSERT_OK_PTR(fentry_link, "fentry_attach_success"))
59+
goto cleanup;
60+
61+
err = bpf_link__destroy(fentry_link);
62+
ASSERT_OK(err, "destroy_lsm");
63+
64+
cleanup:
65+
err = bpf_link__destroy(lsm_link);
66+
ASSERT_OK(err, "destroy_fentry");
67+
}
68+
69+
void test_cgroup1_hierarchy(void)
70+
{
71+
struct test_cgroup1_hierarchy *skel;
72+
__u64 current_cgid;
73+
int hid, err;
74+
75+
skel = test_cgroup1_hierarchy__open();
76+
if (!ASSERT_OK_PTR(skel, "open"))
77+
return;
78+
79+
skel->bss->target_pid = getpid();
80+
81+
err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1");
82+
if (!ASSERT_OK(err, "fentry_set_target"))
83+
goto destroy;
84+
85+
err = test_cgroup1_hierarchy__load(skel);
86+
if (!ASSERT_OK(err, "load"))
87+
goto destroy;
88+
89+
/* Setup cgroup1 hierarchy */
90+
err = setup_classid_environment();
91+
if (!ASSERT_OK(err, "setup_classid_environment"))
92+
goto destroy;
93+
94+
err = join_classid();
95+
if (!ASSERT_OK(err, "join_cgroup1"))
96+
goto cleanup;
97+
98+
current_cgid = get_classid_cgroup_id();
99+
if (!ASSERT_GE(current_cgid, 0, "cgroup1 id"))
100+
goto cleanup;
101+
102+
hid = get_cgroup1_hierarchy_id("net_cls");
103+
if (!ASSERT_GE(hid, 0, "cgroup1 id"))
104+
goto cleanup;
105+
skel->bss->target_hid = hid;
106+
107+
if (test__start_subtest("test_cgroup1_hierarchy")) {
108+
skel->bss->target_ancestor_cgid = current_cgid;
109+
bpf_cgroup1(skel);
110+
}
111+
112+
if (test__start_subtest("test_root_cgid")) {
113+
skel->bss->target_ancestor_cgid = 1;
114+
skel->bss->target_ancestor_level = 0;
115+
bpf_cgroup1(skel);
116+
}
117+
118+
if (test__start_subtest("test_invalid_level")) {
119+
skel->bss->target_ancestor_cgid = 1;
120+
skel->bss->target_ancestor_level = 1;
121+
bpf_cgroup1_invalid_id(skel);
122+
}
123+
124+
if (test__start_subtest("test_invalid_cgid")) {
125+
skel->bss->target_ancestor_cgid = 0;
126+
bpf_cgroup1_invalid_id(skel);
127+
}
128+
129+
if (test__start_subtest("test_invalid_hid")) {
130+
skel->bss->target_ancestor_cgid = 1;
131+
skel->bss->target_ancestor_level = 0;
132+
skel->bss->target_hid = -1;
133+
bpf_cgroup1_invalid_id(skel);
134+
}
135+
136+
if (test__start_subtest("test_invalid_cgrp_name")) {
137+
skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl");
138+
skel->bss->target_ancestor_cgid = current_cgid;
139+
bpf_cgroup1_invalid_id(skel);
140+
}
141+
142+
if (test__start_subtest("test_invalid_cgrp_name2")) {
143+
skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,");
144+
skel->bss->target_ancestor_cgid = current_cgid;
145+
bpf_cgroup1_invalid_id(skel);
146+
}
147+
148+
if (test__start_subtest("test_sleepable_prog")) {
149+
skel->bss->target_hid = hid;
150+
skel->bss->target_ancestor_cgid = current_cgid;
151+
bpf_cgroup1_sleepable(skel);
152+
}
153+
154+
cleanup:
155+
cleanup_classid_environment();
156+
destroy:
157+
test_cgroup1_hierarchy__destroy(skel);
158+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (C) 2023 Yafang Shao <[email protected]> */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <bpf/bpf_core_read.h>
8+
9+
__u32 target_ancestor_level;
10+
__u64 target_ancestor_cgid;
11+
int target_pid, target_hid;
12+
13+
struct cgroup *bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) __ksym;
14+
struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym;
15+
void bpf_cgroup_release(struct cgroup *cgrp) __ksym;
16+
17+
static int bpf_link_create_verify(int cmd)
18+
{
19+
struct cgroup *cgrp, *ancestor;
20+
struct task_struct *task;
21+
int ret = 0;
22+
23+
if (cmd != BPF_LINK_CREATE)
24+
return 0;
25+
26+
task = bpf_get_current_task_btf();
27+
28+
/* Then it can run in parallel with others */
29+
if (task->pid != target_pid)
30+
return 0;
31+
32+
cgrp = bpf_task_get_cgroup1(task, target_hid);
33+
if (!cgrp)
34+
return 0;
35+
36+
/* Refuse it if its cgid or its ancestor's cgid is the target cgid */
37+
if (cgrp->kn->id == target_ancestor_cgid)
38+
ret = -1;
39+
40+
ancestor = bpf_cgroup_ancestor(cgrp, target_ancestor_level);
41+
if (!ancestor)
42+
goto out;
43+
44+
if (ancestor->kn->id == target_ancestor_cgid)
45+
ret = -1;
46+
bpf_cgroup_release(ancestor);
47+
48+
out:
49+
bpf_cgroup_release(cgrp);
50+
return ret;
51+
}
52+
53+
SEC("lsm/bpf")
54+
int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size)
55+
{
56+
return bpf_link_create_verify(cmd);
57+
}
58+
59+
SEC("lsm.s/bpf")
60+
int BPF_PROG(lsm_s_run, int cmd, union bpf_attr *attr, unsigned int size)
61+
{
62+
return bpf_link_create_verify(cmd);
63+
}
64+
65+
SEC("fentry")
66+
int BPF_PROG(fentry_run)
67+
{
68+
return 0;
69+
}
70+
71+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)