summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/cgroup_iter_memcg.c
blob: a5afd16705f05221a6772895752c4f1f39859ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */
#include <test_progs.h>
#include <bpf/libbpf.h>
#include <bpf/btf.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include "cgroup_helpers.h"
#include "cgroup_iter_memcg.h"
#include "cgroup_iter_memcg.skel.h"

static int read_stats(struct bpf_link *link)
{
	int fd, ret = 0;
	ssize_t bytes;

	fd = bpf_iter_create(bpf_link__fd(link));
	if (!ASSERT_OK_FD(fd, "bpf_iter_create"))
		return 1;

	/*
	 * Invoke iter program by reading from its fd. We're not expecting any
	 * data to be written by the bpf program so the result should be zero.
	 * Results will be read directly through the custom data section
	 * accessible through skel->data_query.memcg_query.
	 */
	bytes = read(fd, NULL, 0);
	if (!ASSERT_EQ(bytes, 0, "read fd"))
		ret = 1;

	close(fd);
	return ret;
}

static void test_anon(struct bpf_link *link, struct memcg_query *memcg_query)
{
	void *map;
	size_t len;

	len = sysconf(_SC_PAGESIZE) * 1024;

	/*
	 * Increase memcg anon usage by mapping and writing
	 * to a new anon region.
	 */
	map = mmap(NULL, len, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (!ASSERT_NEQ(map, MAP_FAILED, "mmap anon"))
		return;

	memset(map, 1, len);

	if (!ASSERT_OK(read_stats(link), "read stats"))
		goto cleanup;

	ASSERT_GT(memcg_query->nr_anon_mapped, 0, "final anon mapped val");

cleanup:
	munmap(map, len);
}

static void test_file(struct bpf_link *link, struct memcg_query *memcg_query)
{
	void *map;
	size_t len;
	char *path;
	int fd;

	len = sysconf(_SC_PAGESIZE) * 1024;
	path = "/tmp/test_cgroup_iter_memcg";

	/*
	 * Increase memcg file usage by creating and writing
	 * to a mapped file.
	 */
	fd = open(path, O_CREAT | O_RDWR, 0644);
	if (!ASSERT_OK_FD(fd, "open fd"))
		return;
	if (!ASSERT_OK(ftruncate(fd, len), "ftruncate"))
		goto cleanup_fd;

	map = mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, 0);
	if (!ASSERT_NEQ(map, MAP_FAILED, "mmap file"))
		goto cleanup_fd;

	memset(map, 1, len);

	if (!ASSERT_OK(read_stats(link), "read stats"))
		goto cleanup_map;

	ASSERT_GT(memcg_query->nr_file_pages, 0, "final file value");
	ASSERT_GT(memcg_query->nr_file_mapped, 0, "final file mapped value");

cleanup_map:
	munmap(map, len);
cleanup_fd:
	close(fd);
	unlink(path);
}

static void test_shmem(struct bpf_link *link, struct memcg_query *memcg_query)
{
	size_t len;
	int fd;

	len = sysconf(_SC_PAGESIZE) * 1024;

	/*
	 * Increase memcg shmem usage by creating and writing
	 * to a shmem object.
	 */
	fd = shm_open("/tmp_shmem", O_CREAT | O_RDWR, 0644);
	if (!ASSERT_OK_FD(fd, "shm_open"))
		return;

	if (!ASSERT_OK(fallocate(fd, 0, 0, len), "fallocate"))
		goto cleanup;

	if (!ASSERT_OK(read_stats(link), "read stats"))
		goto cleanup;

	ASSERT_GT(memcg_query->nr_shmem, 0, "final shmem value");

cleanup:
	close(fd);
	shm_unlink("/tmp_shmem");
}

#define NR_PIPES 64
static void test_kmem(struct bpf_link *link, struct memcg_query *memcg_query)
{
	int fds[NR_PIPES][2], i;

	/*
	 * Increase kmem value by creating pipes which will allocate some
	 * kernel buffers.
	 */
	for (i = 0; i < NR_PIPES; i++) {
		if (!ASSERT_OK(pipe(fds[i]), "pipe"))
			goto cleanup;
	}

	if (!ASSERT_OK(read_stats(link), "read stats"))
		goto cleanup;

	ASSERT_GT(memcg_query->memcg_kmem, 0, "kmem value");

cleanup:
	for (i = i - 1; i >= 0; i--) {
		close(fds[i][0]);
		close(fds[i][1]);
	}
}

static void test_pgfault(struct bpf_link *link, struct memcg_query *memcg_query)
{
	void *map;
	size_t len;

	len = sysconf(_SC_PAGESIZE) * 1024;

	/* Create region to use for triggering a page fault. */
	map = mmap(NULL, len, PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
	if (!ASSERT_NEQ(map, MAP_FAILED, "mmap anon"))
		return;

	/* Trigger page fault. */
	memset(map, 1, len);

	if (!ASSERT_OK(read_stats(link), "read stats"))
		goto cleanup;

	ASSERT_GT(memcg_query->pgfault, 0, "final pgfault val");

cleanup:
	munmap(map, len);
}

void test_cgroup_iter_memcg(void)
{
	char *cgroup_rel_path = "/cgroup_iter_memcg_test";
	struct cgroup_iter_memcg *skel;
	struct bpf_link *link;
	int cgroup_fd;

	cgroup_fd = cgroup_setup_and_join(cgroup_rel_path);
	if (!ASSERT_OK_FD(cgroup_fd, "cgroup_setup_and_join"))
		return;

	skel = cgroup_iter_memcg__open_and_load();
	if (!ASSERT_OK_PTR(skel, "cgroup_iter_memcg__open_and_load"))
		goto cleanup_cgroup_fd;

	DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
	union bpf_iter_link_info linfo = {
		.cgroup.cgroup_fd = cgroup_fd,
		.cgroup.order = BPF_CGROUP_ITER_SELF_ONLY,
	};
	opts.link_info = &linfo;
	opts.link_info_len = sizeof(linfo);

	link = bpf_program__attach_iter(skel->progs.cgroup_memcg_query, &opts);
	if (!ASSERT_OK_PTR(link, "bpf_program__attach_iter"))
		goto cleanup_skel;

	if (test__start_subtest("cgroup_iter_memcg__anon"))
		test_anon(link, &skel->data_query->memcg_query);
	if (test__start_subtest("cgroup_iter_memcg__shmem"))
		test_shmem(link, &skel->data_query->memcg_query);
	if (test__start_subtest("cgroup_iter_memcg__file"))
		test_file(link, &skel->data_query->memcg_query);
	if (test__start_subtest("cgroup_iter_memcg__kmem"))
		test_kmem(link, &skel->data_query->memcg_query);
	if (test__start_subtest("cgroup_iter_memcg__pgfault"))
		test_pgfault(link, &skel->data_query->memcg_query);

	bpf_link__destroy(link);
cleanup_skel:
	cgroup_iter_memcg__destroy(skel);
cleanup_cgroup_fd:
	close(cgroup_fd);
	cleanup_cgroup_environment();
}