Skip to content

Commit 3cbefa6

Browse files
teawaterKernel Patches Daemon
authored andcommitted
selftests/bpf: add memcg eBPF struct ops test
Add comprehensive selftest suite for memory controller eBPF support. The tests validate the core functionality of the memcg_ops struct ops implementation. Test coverage includes: 1. test_memcg_ops_load: Validates that the eBPF object file can be successfully loaded by libbpf. 2. test_memcg_ops_attach: Tests attaching the memcg_ops struct ops to the kernel, verifying the basic attachment mechanism works correctly. 3. test_memcg_ops_double_attach: Validates that only one memcg_ops instance can be attached at a time. Attempts to attach a second program should fail, ensuring the single-handler design constraint is enforced. The test suite includes: - prog_tests/memcg_ops.c: Test entry point with individual test functions using standard BPF test framework helpers like ASSERT_OK_PTR and ASSERT_ERR_PTR - progs/memcg_ops.bpf.c: Simple eBPF program implementing the struct ops interface Uses standard test_progs framework macros for consistent error reporting and handling. All tests properly clean up resources in error paths. Signed-off-by: Geliang Tang <[email protected]> Signed-off-by: Hui Zhu <[email protected]>
1 parent 0ced0b6 commit 3cbefa6

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6357,6 +6357,7 @@ F: mm/memcontrol_bpf.h
63576357
F: mm/page_counter.c
63586358
F: mm/swap_cgroup.c
63596359
F: samples/cgroup/*
6360+
F: tools/testing/selftests/bpf/*/memcg_ops.c
63606361
F: tools/testing/selftests/cgroup/memcg_protection.m
63616362
F: tools/testing/selftests/cgroup/test_hugetlb_memcg.c
63626363
F: tools/testing/selftests/cgroup/test_kmem.c
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Memory controller eBPF struct ops test
4+
*/
5+
6+
#include <test_progs.h>
7+
#include <bpf/btf.h>
8+
9+
void test_memcg_ops_load(void)
10+
{
11+
struct bpf_object *obj;
12+
int err;
13+
14+
obj = bpf_object__open_file("memcg_ops.bpf.o", NULL);
15+
err = libbpf_get_error(obj);
16+
if (CHECK_FAIL(err)) {
17+
obj = NULL;
18+
goto out;
19+
}
20+
21+
err = bpf_object__load(obj);
22+
if (CHECK_FAIL(err))
23+
goto out;
24+
25+
out:
26+
if (obj)
27+
bpf_object__close(obj);
28+
}
29+
30+
void test_memcg_ops_attach(void)
31+
{
32+
struct bpf_object *obj;
33+
struct bpf_map *map;
34+
struct bpf_link *link = NULL;
35+
int err;
36+
37+
obj = bpf_object__open_file("memcg_ops.bpf.o", NULL);
38+
err = libbpf_get_error(obj);
39+
if (CHECK_FAIL(err)) {
40+
obj = NULL;
41+
goto out;
42+
}
43+
44+
err = bpf_object__load(obj);
45+
if (CHECK_FAIL(err))
46+
goto out;
47+
48+
map = bpf_object__find_map_by_name(obj, "mcg_ops");
49+
if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
50+
goto out;
51+
52+
link = bpf_map__attach_struct_ops(map);
53+
if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
54+
goto out;
55+
56+
out:
57+
if (link)
58+
bpf_link__destroy(link);
59+
if (obj)
60+
bpf_object__close(obj);
61+
}
62+
63+
void test_memcg_ops_double_attach(void)
64+
{
65+
struct bpf_object *obj, *obj2;
66+
struct bpf_map *map, *map2;
67+
struct bpf_link *link = NULL, *link2 = NULL;
68+
int err;
69+
70+
obj = bpf_object__open_file("memcg_ops.bpf.o", NULL);
71+
err = libbpf_get_error(obj);
72+
if (CHECK_FAIL(err)) {
73+
obj = NULL;
74+
goto out;
75+
}
76+
77+
err = bpf_object__load(obj);
78+
if (CHECK_FAIL(err))
79+
goto out;
80+
81+
map = bpf_object__find_map_by_name(obj, "mcg_ops");
82+
if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
83+
goto out;
84+
85+
link = bpf_map__attach_struct_ops(map);
86+
if (!ASSERT_OK_PTR(link, "bpf_map__attach_struct_ops"))
87+
goto out;
88+
89+
obj2 = bpf_object__open_file("memcg_ops.bpf.o", NULL);
90+
err = libbpf_get_error(obj2);
91+
if (CHECK_FAIL(err)) {
92+
obj2 = NULL;
93+
goto out;
94+
}
95+
96+
err = bpf_object__load(obj2);
97+
if (CHECK_FAIL(err))
98+
goto out;
99+
100+
map2 = bpf_object__find_map_by_name(obj2, "mcg_ops");
101+
if (!ASSERT_OK_PTR(map, "bpf_object__find_map_by_name"))
102+
goto out;
103+
104+
link2 = bpf_map__attach_struct_ops(map2);
105+
if (!ASSERT_ERR_PTR(link2, "bpf_map__attach_struct_ops")) {
106+
bpf_link__destroy(link2);
107+
goto out;
108+
}
109+
110+
out:
111+
if (link)
112+
bpf_link__destroy(link);
113+
if (obj)
114+
bpf_object__close(obj);
115+
if (obj2)
116+
bpf_object__close(obj2);
117+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include "vmlinux.h"
4+
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
SEC("struct_ops/try_charge_memcg")
11+
int BPF_PROG(test_try_charge_memcg,
12+
struct try_charge_memcg *tcm)
13+
{
14+
return 0;
15+
}
16+
17+
SEC(".struct_ops")
18+
struct memcg_ops mcg_ops = {
19+
.try_charge_memcg = (void *)test_try_charge_memcg,
20+
};

0 commit comments

Comments
 (0)