Skip to content

Commit 4498cb8

Browse files
laoarKernel Patches Daemon
authored and
Kernel Patches Daemon
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]>
1 parent d670355 commit 4498cb8

File tree

2 files changed

+231
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)