|
| 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 | +} |
0 commit comments