|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +#include <test_progs.h> |
| 3 | +#include <bpf/btf.h> |
| 4 | +#include <bpf/bpf.h> |
| 5 | + |
| 6 | +#include "cgroup_helpers.h" |
| 7 | +#include "test_psi.skel.h" |
| 8 | + |
| 9 | +enum psi_res { |
| 10 | + PSI_IO, |
| 11 | + PSI_MEM, |
| 12 | + PSI_CPU, |
| 13 | + PSI_IRQ, |
| 14 | + NR_PSI_RESOURCES, |
| 15 | +}; |
| 16 | + |
| 17 | +struct cgroup_desc { |
| 18 | + const char *path; |
| 19 | + unsigned long long id; |
| 20 | + int pid; |
| 21 | + int fd; |
| 22 | + size_t target; |
| 23 | + size_t high; |
| 24 | + bool victim; |
| 25 | +}; |
| 26 | + |
| 27 | +#define MB (1024 * 1024) |
| 28 | + |
| 29 | +static struct cgroup_desc cgroups[] = { |
| 30 | + { .path = "/oom_test" }, |
| 31 | + { .path = "/oom_test/cg1" }, |
| 32 | + { .path = "/oom_test/cg2", .target = 500 * MB, |
| 33 | + .high = 40 * MB, .victim = true }, |
| 34 | +}; |
| 35 | + |
| 36 | +static int spawn_task(struct cgroup_desc *desc) |
| 37 | +{ |
| 38 | + char *ptr; |
| 39 | + int pid; |
| 40 | + |
| 41 | + pid = fork(); |
| 42 | + if (pid < 0) |
| 43 | + return pid; |
| 44 | + |
| 45 | + if (pid > 0) { |
| 46 | + /* parent */ |
| 47 | + desc->pid = pid; |
| 48 | + return 0; |
| 49 | + } |
| 50 | + |
| 51 | + /* child */ |
| 52 | + ptr = (char *)malloc(desc->target); |
| 53 | + if (!ptr) |
| 54 | + return -ENOMEM; |
| 55 | + |
| 56 | + memset(ptr, 'a', desc->target); |
| 57 | + |
| 58 | + while (1) |
| 59 | + sleep(1000); |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +static void setup_environment(void) |
| 65 | +{ |
| 66 | + int i, err; |
| 67 | + |
| 68 | + err = setup_cgroup_environment(); |
| 69 | + if (!ASSERT_OK(err, "setup_cgroup_environment")) |
| 70 | + goto cleanup; |
| 71 | + |
| 72 | + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { |
| 73 | + cgroups[i].fd = create_and_get_cgroup(cgroups[i].path); |
| 74 | + if (!ASSERT_GE(cgroups[i].fd, 0, "create_and_get_cgroup")) |
| 75 | + goto cleanup; |
| 76 | + |
| 77 | + cgroups[i].id = get_cgroup_id(cgroups[i].path); |
| 78 | + if (!ASSERT_GT(cgroups[i].id, 0, "get_cgroup_id")) |
| 79 | + goto cleanup; |
| 80 | + |
| 81 | + /* Freeze the top-level cgroup and enable the memory controller */ |
| 82 | + if (i == 0) { |
| 83 | + err = write_cgroup_file(cgroups[i].path, "cgroup.freeze", "1"); |
| 84 | + if (!ASSERT_OK(err, "freeze cgroup")) |
| 85 | + goto cleanup; |
| 86 | + |
| 87 | + err = write_cgroup_file(cgroups[i].path, "cgroup.subtree_control", |
| 88 | + "+memory"); |
| 89 | + if (!ASSERT_OK(err, "enable memory controller")) |
| 90 | + goto cleanup; |
| 91 | + } |
| 92 | + |
| 93 | + /* Set memory.high */ |
| 94 | + if (cgroups[i].high) { |
| 95 | + char buf[256]; |
| 96 | + |
| 97 | + snprintf(buf, sizeof(buf), "%lu", cgroups[i].high); |
| 98 | + err = write_cgroup_file(cgroups[i].path, "memory.high", buf); |
| 99 | + if (!ASSERT_OK(err, "set memory.high")) |
| 100 | + goto cleanup; |
| 101 | + |
| 102 | + snprintf(buf, sizeof(buf), "0"); |
| 103 | + write_cgroup_file(cgroups[i].path, "memory.swap.max", buf); |
| 104 | + } |
| 105 | + |
| 106 | + /* Spawn tasks creating memory pressure */ |
| 107 | + if (cgroups[i].target) { |
| 108 | + char buf[256]; |
| 109 | + |
| 110 | + err = spawn_task(&cgroups[i]); |
| 111 | + if (!ASSERT_OK(err, "spawn task")) |
| 112 | + goto cleanup; |
| 113 | + |
| 114 | + snprintf(buf, sizeof(buf), "%d", cgroups[i].pid); |
| 115 | + err = write_cgroup_file(cgroups[i].path, "cgroup.procs", buf); |
| 116 | + if (!ASSERT_OK(err, "put child into a cgroup")) |
| 117 | + goto cleanup; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return; |
| 122 | + |
| 123 | +cleanup: |
| 124 | + cleanup_cgroup_environment(); |
| 125 | +} |
| 126 | + |
| 127 | +static int run_and_wait_for_oom(void) |
| 128 | +{ |
| 129 | + int ret = -1; |
| 130 | + bool first = true; |
| 131 | + char buf[4096] = {}; |
| 132 | + size_t size; |
| 133 | + |
| 134 | + /* Unfreeze the top-level cgroup */ |
| 135 | + ret = write_cgroup_file(cgroups[0].path, "cgroup.freeze", "0"); |
| 136 | + if (!ASSERT_OK(ret, "unfreeze cgroup")) |
| 137 | + return -1; |
| 138 | + |
| 139 | + for (;;) { |
| 140 | + int i, status; |
| 141 | + pid_t pid = wait(&status); |
| 142 | + |
| 143 | + if (pid == -1) { |
| 144 | + if (errno == EINTR) |
| 145 | + continue; |
| 146 | + /* ECHILD */ |
| 147 | + break; |
| 148 | + } |
| 149 | + |
| 150 | + if (!first) |
| 151 | + continue; |
| 152 | + first = false; |
| 153 | + |
| 154 | + /* Check which process was terminated first */ |
| 155 | + for (i = 0; i < ARRAY_SIZE(cgroups); i++) { |
| 156 | + if (!ASSERT_OK(cgroups[i].victim != |
| 157 | + (pid == cgroups[i].pid), |
| 158 | + "correct process was killed")) { |
| 159 | + ret = -1; |
| 160 | + break; |
| 161 | + } |
| 162 | + |
| 163 | + if (!cgroups[i].victim) |
| 164 | + continue; |
| 165 | + |
| 166 | + /* Check the memcg oom counter */ |
| 167 | + size = read_cgroup_file(cgroups[i].path, "memory.events", |
| 168 | + buf, sizeof(buf)); |
| 169 | + if (!ASSERT_OK(size <= 0, "read memory.events")) { |
| 170 | + ret = -1; |
| 171 | + break; |
| 172 | + } |
| 173 | + |
| 174 | + if (!ASSERT_OK(strstr(buf, "oom_kill 1") == NULL, |
| 175 | + "oom_kill count check")) { |
| 176 | + ret = -1; |
| 177 | + break; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /* Kill all remaining tasks */ |
| 182 | + for (i = 0; i < ARRAY_SIZE(cgroups); i++) |
| 183 | + if (cgroups[i].pid && cgroups[i].pid != pid) |
| 184 | + kill(cgroups[i].pid, SIGKILL); |
| 185 | + } |
| 186 | + |
| 187 | + return ret; |
| 188 | +} |
| 189 | + |
| 190 | +void test_psi(void) |
| 191 | +{ |
| 192 | + struct test_psi *skel; |
| 193 | + u64 freed_cgroup_id; |
| 194 | + int err; |
| 195 | + |
| 196 | + setup_environment(); |
| 197 | + |
| 198 | + skel = test_psi__open_and_load(); |
| 199 | + err = libbpf_get_error(skel); |
| 200 | + if (CHECK_FAIL(err)) |
| 201 | + goto cleanup; |
| 202 | + |
| 203 | + skel->bss->deleted_cgroup_id = cgroups[1].id; |
| 204 | + skel->bss->high_pressure_cgroup_id = cgroups[2].id; |
| 205 | + |
| 206 | + err = test_psi__attach(skel); |
| 207 | + if (CHECK_FAIL(err)) |
| 208 | + goto cleanup; |
| 209 | + |
| 210 | + /* Delete the first cgroup, it should trigger handle_cgroup_free() */ |
| 211 | + remove_cgroup(cgroups[1].path); |
| 212 | + |
| 213 | + /* Unfreeze all child tasks and create the memory pressure */ |
| 214 | + err = run_and_wait_for_oom(); |
| 215 | + CHECK_FAIL(err); |
| 216 | + |
| 217 | + /* Check the result of the handle_cgroup_free() handler */ |
| 218 | + freed_cgroup_id = skel->bss->deleted_cgroup_id; |
| 219 | + ASSERT_EQ(freed_cgroup_id, cgroups[1].id, "freed cgroup id"); |
| 220 | + |
| 221 | +cleanup: |
| 222 | + cleanup_cgroup_environment(); |
| 223 | + test_psi__destroy(skel); |
| 224 | +} |
0 commit comments