Skip to content

Commit 64e845a

Browse files
inwardvesselKernel Patches Daemon
authored andcommitted
memcg: selftests for memcg stat kfuncs
Add test coverage for the kfuncs that fetch memcg stats. Using some common stats, test before and after scenarios ensuring that the given stat increases by some arbitrary amount. The stats selected cover the three categories represented by the enums: node_stat_item, memcg_stat_item, vm_event_item. Since only a subset of all stats are queried, use a static struct made up of fields for each stat. Write to the struct with the fetched values when the bpf program is invoked and read the fields in the user mode program for verification. Signed-off-by: JP Kobryn <[email protected]>
1 parent 83216e7 commit 64e845a

File tree

3 files changed

+374
-0
lines changed

3 files changed

+374
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
#ifndef __CGROUP_ITER_MEMCG_H
4+
#define __CGROUP_ITER_MEMCG_H
5+
6+
struct memcg_query {
7+
/* some node_stat_item's */
8+
long nr_anon_mapped;
9+
long nr_shmem;
10+
long nr_file_pages;
11+
long nr_file_mapped;
12+
/* some memcg_stat_item */
13+
long memcg_kmem;
14+
/* some vm_event_item */
15+
long pgfault;
16+
};
17+
18+
#endif /* __CGROUP_ITER_MEMCG_H */
Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
#include <test_progs.h>
4+
#include <bpf/libbpf.h>
5+
#include <bpf/btf.h>
6+
#include <fcntl.h>
7+
#include <sys/mman.h>
8+
#include <unistd.h>
9+
#include "cgroup_helpers.h"
10+
#include "cgroup_iter_memcg.h"
11+
#include "cgroup_iter_memcg.skel.h"
12+
13+
int read_stats(struct bpf_link *link)
14+
{
15+
int fd, ret = 0;
16+
ssize_t bytes;
17+
18+
fd = bpf_iter_create(bpf_link__fd(link));
19+
if (!ASSERT_OK_FD(fd, "bpf_iter_create"))
20+
return 1;
21+
22+
/*
23+
* Invoke iter program by reading from its fd. We're not expecting any
24+
* data to be written by the bpf program so the result should be zero.
25+
* Results will be read directly through the custom data section
26+
* accessible through skel->data_query.memcg_query.
27+
*/
28+
bytes = read(fd, NULL, 0);
29+
if (!ASSERT_EQ(bytes, 0, "read fd"))
30+
ret = 1;
31+
32+
close(fd);
33+
return ret;
34+
}
35+
36+
static void test_anon(struct bpf_link *link,
37+
struct memcg_query *memcg_query)
38+
{
39+
void *map;
40+
size_t len;
41+
long val;
42+
43+
len = sysconf(_SC_PAGESIZE) * 1024;
44+
45+
if (!ASSERT_OK(read_stats(link), "read stats"))
46+
return;
47+
48+
val = memcg_query->nr_anon_mapped;
49+
if (!ASSERT_GE(val, 0, "initial anon mapped val"))
50+
return;
51+
52+
/*
53+
* Increase memcg anon usage by mapping and writing
54+
* to a new anon region.
55+
*/
56+
map = mmap(NULL, len, PROT_READ | PROT_WRITE,
57+
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
58+
if (!ASSERT_NEQ(map, MAP_FAILED, "mmap anon"))
59+
return;
60+
61+
memset(map, 1, len);
62+
63+
if (!ASSERT_OK(read_stats(link), "read stats"))
64+
goto cleanup;
65+
66+
ASSERT_GT(memcg_query->nr_anon_mapped, val, "final anon mapped val");
67+
68+
cleanup:
69+
munmap(map, len);
70+
}
71+
72+
static void test_file(struct bpf_link *link,
73+
struct memcg_query *memcg_query)
74+
{
75+
void *map;
76+
size_t len;
77+
long val_pages, val_mapped;
78+
FILE *f;
79+
int fd;
80+
81+
len = sysconf(_SC_PAGESIZE) * 1024;
82+
83+
if (!ASSERT_OK(read_stats(link), "read stats"))
84+
return;
85+
86+
val_pages = memcg_query->nr_file_pages;
87+
if (!ASSERT_GE(val_pages, 0, "initial file val"))
88+
return;
89+
val_mapped = memcg_query->nr_file_mapped;
90+
if (!ASSERT_GE(val_mapped, 0, "initial file mapped val"))
91+
return;
92+
93+
/*
94+
* Increase memcg file usage by creating and writing
95+
* to a temoprary mapped file.
96+
*/
97+
f = tmpfile();
98+
if (!ASSERT_OK_PTR(f, "tmpfile"))
99+
return;
100+
fd = fileno(f);
101+
if (!ASSERT_OK_FD(fd, "open fd"))
102+
return;
103+
if (!ASSERT_OK(ftruncate(fd, len), "ftruncate"))
104+
goto cleanup_fd;
105+
106+
map = mmap(NULL, len, PROT_READ | PROT_WRITE,
107+
MAP_SHARED, fd, 0);
108+
if (!ASSERT_NEQ(map, MAP_FAILED, "mmap file"))
109+
goto cleanup_fd;
110+
111+
memset(map, 1, len);
112+
113+
if (!ASSERT_OK(read_stats(link), "read stats"))
114+
goto cleanup_map;
115+
116+
ASSERT_GT(memcg_query->nr_file_pages, val_pages, "final file value");
117+
ASSERT_GT(memcg_query->nr_file_mapped, val_mapped,
118+
"final file mapped value");
119+
120+
cleanup_map:
121+
munmap(map, len);
122+
cleanup_fd:
123+
close(fd);
124+
}
125+
126+
static void test_shmem(struct bpf_link *link,
127+
struct memcg_query *memcg_query)
128+
{
129+
size_t len;
130+
int fd;
131+
void *map;
132+
long val;
133+
134+
len = sysconf(_SC_PAGESIZE) * 1024;
135+
136+
if (!ASSERT_OK(read_stats(link), "read stats"))
137+
return;
138+
139+
val = memcg_query->nr_shmem;
140+
if (!ASSERT_GE(val, 0, "init shmem val"))
141+
return;
142+
143+
/*
144+
* Increase memcg shmem usage by creating and writing
145+
* to a shmem object.
146+
*/
147+
fd = shm_open("/tmp_shmem", O_CREAT | O_RDWR, 0644);
148+
if (!ASSERT_OK_FD(fd, "shm_open"))
149+
return;
150+
151+
if (!ASSERT_OK(ftruncate(fd, len), "ftruncate"))
152+
goto cleanup_fd;
153+
154+
map = mmap(NULL, len, PROT_READ | PROT_WRITE,
155+
MAP_SHARED, fd, 0);
156+
if (!ASSERT_NEQ(map, MAP_FAILED, "mmap shmem"))
157+
goto cleanup_fd;
158+
159+
memset(map, 1, len);
160+
161+
if (!ASSERT_OK(read_stats(link), "read stats"))
162+
goto cleanup_map;
163+
164+
ASSERT_GT(memcg_query->nr_shmem, val, "final shmem value");
165+
166+
cleanup_map:
167+
munmap(map, len);
168+
cleanup_fd:
169+
close(fd);
170+
shm_unlink("/tmp_shmem");
171+
}
172+
173+
static void test_kmem(struct bpf_link *link,
174+
struct memcg_query *memcg_query)
175+
{
176+
int fds[2];
177+
int err;
178+
ssize_t bytes;
179+
size_t len;
180+
char *buf;
181+
long val;
182+
183+
len = sysconf(_SC_PAGESIZE) * 1024;
184+
185+
if (!ASSERT_OK(read_stats(link), "read stats"))
186+
return;
187+
188+
val = memcg_query->memcg_kmem;
189+
if (!ASSERT_GE(val, 0, "initial kmem val"))
190+
return;
191+
192+
err = pipe2(fds, O_NONBLOCK);
193+
if (!ASSERT_OK(err, "pipe"))
194+
return;
195+
196+
buf = malloc(len);
197+
memset(buf, 1, len);
198+
bytes = write(fds[1], buf, len);
199+
if (!ASSERT_GT(bytes, 0, "write"))
200+
goto cleanup;
201+
202+
if (!ASSERT_OK(read_stats(link), "read stats"))
203+
goto cleanup;
204+
205+
ASSERT_GT(memcg_query->memcg_kmem, val, "kmem value");
206+
207+
cleanup:
208+
free(buf);
209+
close(fds[0]);
210+
close(fds[1]);
211+
}
212+
213+
static void test_pgfault(struct bpf_link *link,
214+
struct memcg_query *memcg_query)
215+
{
216+
void *map;
217+
size_t len;
218+
long val;
219+
220+
len = sysconf(_SC_PAGESIZE) * 1024;
221+
222+
if (!ASSERT_OK(read_stats(link), "read stats"))
223+
return;
224+
225+
val = memcg_query->pgfault;
226+
if (!ASSERT_GE(val, 0, "initial pgfault val"))
227+
return;
228+
229+
/* Create region to use for triggering a page fault. */
230+
map = mmap(NULL, len, PROT_READ | PROT_WRITE,
231+
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
232+
if (!ASSERT_NEQ(map, MAP_FAILED, "mmap anon"))
233+
return;
234+
235+
/* Trigger page fault. */
236+
memset(map, 1, len);
237+
238+
if (!ASSERT_OK(read_stats(link), "read stats"))
239+
goto cleanup;
240+
241+
ASSERT_GT(memcg_query->pgfault, val, "final pgfault val");
242+
243+
cleanup:
244+
munmap(map, len);
245+
}
246+
247+
void test_cgroup_iter_memcg(void)
248+
{
249+
char *cgroup_rel_path = "/cgroup_iter_memcg_test";
250+
struct cgroup_iter_memcg *skel;
251+
struct bpf_link *link;
252+
int cgroup_fd, err;
253+
254+
cgroup_fd = cgroup_setup_and_join(cgroup_rel_path);
255+
if (!ASSERT_OK_FD(cgroup_fd, "cgroup_setup_and_join"))
256+
return;
257+
258+
skel = cgroup_iter_memcg__open();
259+
if (!ASSERT_OK_PTR(skel, "cgroup_iter_memcg__open"))
260+
goto cleanup_cgroup_fd;
261+
262+
err = cgroup_iter_memcg__load(skel);
263+
if (!ASSERT_OK(err, "cgroup_iter_memcg__load"))
264+
goto cleanup_skel;
265+
266+
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
267+
union bpf_iter_link_info linfo = {
268+
.cgroup.cgroup_fd = cgroup_fd,
269+
.cgroup.order = BPF_CGROUP_ITER_SELF_ONLY,
270+
};
271+
opts.link_info = &linfo;
272+
opts.link_info_len = sizeof(linfo);
273+
274+
link = bpf_program__attach_iter(skel->progs.cgroup_memcg_query, &opts);
275+
if (!ASSERT_OK_PTR(link, "bpf_program__attach_iter"))
276+
goto cleanup_cgroup_fd;
277+
278+
if (test__start_subtest("cgroup_iter_memcg__anon"))
279+
test_anon(link, &skel->data_query->memcg_query);
280+
if (test__start_subtest("cgroup_iter_memcg__shmem"))
281+
test_shmem(link, &skel->data_query->memcg_query);
282+
if (test__start_subtest("cgroup_iter_memcg__file"))
283+
test_file(link, &skel->data_query->memcg_query);
284+
if (test__start_subtest("cgroup_iter_memcg__kmem"))
285+
test_kmem(link, &skel->data_query->memcg_query);
286+
if (test__start_subtest("cgroup_iter_memcg__pgfault"))
287+
test_pgfault(link, &skel->data_query->memcg_query);
288+
289+
bpf_link__destroy(link);
290+
cleanup_skel:
291+
cgroup_iter_memcg__destroy(skel);
292+
cleanup_cgroup_fd:
293+
close(cgroup_fd);
294+
cleanup_cgroup_environment();
295+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_core_read.h>
5+
#include "cgroup_iter_memcg.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
extern void memcg_flush_stats(struct cgroup *cgrp) __ksym;
10+
extern unsigned long memcg_stat_fetch(struct cgroup *cgrp,
11+
enum memcg_stat_item item) __ksym;
12+
extern unsigned long memcg_node_stat_fetch(struct cgroup *cgrp,
13+
enum node_stat_item item) __ksym;
14+
extern unsigned long memcg_vm_event_fetch(struct cgroup *cgrp,
15+
enum vm_event_item item) __ksym;
16+
17+
/* The latest values read are stored here. */
18+
struct memcg_query memcg_query SEC(".data.query");
19+
20+
/*
21+
* Helpers for fetching any of the three different types of memcg stats.
22+
* BPF core macros are used to ensure an enumerator is present in the given
23+
* kernel. Falling back on -1 indicates its absence.
24+
*/
25+
#define node_stat_fetch_if_exists(cgrp, item) \
26+
bpf_core_enum_value_exists(enum node_stat_item, item) ? \
27+
memcg_node_stat_fetch((cgrp), bpf_core_enum_value( \
28+
enum node_stat_item, item)) : -1
29+
30+
#define memcg_stat_fetch_if_exists(cgrp, item) \
31+
bpf_core_enum_value_exists(enum memcg_stat_item, item) ? \
32+
memcg_node_stat_fetch((cgrp), bpf_core_enum_value( \
33+
enum memcg_stat_item, item)) : -1
34+
35+
#define vm_event_fetch_if_exists(cgrp, item) \
36+
bpf_core_enum_value_exists(enum vm_event_item, item) ? \
37+
memcg_vm_event_fetch((cgrp), bpf_core_enum_value( \
38+
enum vm_event_item, item)) : -1
39+
40+
SEC("iter.s/cgroup")
41+
int cgroup_memcg_query(struct bpf_iter__cgroup *ctx)
42+
{
43+
struct cgroup *cgrp = ctx->cgroup;
44+
45+
if (!cgrp)
46+
return 1;
47+
48+
memcg_flush_stats(cgrp);
49+
50+
memcg_query.nr_anon_mapped = node_stat_fetch_if_exists(cgrp,
51+
NR_ANON_MAPPED);
52+
memcg_query.nr_shmem = node_stat_fetch_if_exists(cgrp, NR_SHMEM);
53+
memcg_query.nr_file_pages = node_stat_fetch_if_exists(cgrp,
54+
NR_FILE_PAGES);
55+
memcg_query.nr_file_mapped = node_stat_fetch_if_exists(cgrp,
56+
NR_FILE_MAPPED);
57+
memcg_query.memcg_kmem = memcg_stat_fetch_if_exists(cgrp, MEMCG_KMEM);
58+
memcg_query.pgfault = vm_event_fetch_if_exists(cgrp, PGFAULT);
59+
60+
return 0;
61+
}

0 commit comments

Comments
 (0)