Add a BPF selftest attaching a BPF OOM handler to a small memory cgroup, which calls bpf_mem_cgroup_raise_max() to expand memory.max on OOM. This allows a 24 MB allocation to succeed without terminating the allocating task. Also verify BPF_PROG_TYPE_SYSCALL negative and boundary cases: equal or lower limits and root memcg return -EINVAL, while UINT64_MAX clamps cleanly to "max". Signed-off-by: Ben Gardon --- .../bpf/prog_tests/test_oom_raise_max.c | 135 ++++++++++++++++++ .../selftests/bpf/progs/test_oom_raise_max.c | 55 +++++++ .../selftests/bpf/progs/test_oom_raise_max.h | 10 ++ 3 files changed, 200 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_oom_raise_max.c create mode 100644 tools/testing/selftests/bpf/progs/test_oom_raise_max.c create mode 100644 tools/testing/selftests/bpf/progs/test_oom_raise_max.h diff --git a/tools/testing/selftests/bpf/prog_tests/test_oom_raise_max.c b/tools/testing/selftests/bpf/prog_tests/test_oom_raise_max.c new file mode 100644 index 000000000000..6b803d4560e9 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_oom_raise_max.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include "cgroup_helpers.h" +#include "memory_holder.h" + +#define MB (1024UL * 1024UL) +#include "progs/test_oom_raise_max.h" +#include "test_oom_raise_max.skel.h" + +/* + * Serialized because this test attaches a bpf_oom_ops handler and triggers a + * real memcg OOM, which can collide with other OOM tests under test_progs -j. + */ +void serial_test_oom_raise_max(void) +{ + LIBBPF_OPTS(bpf_cgroup_opts, opts); + const char *cg_path = "/oom_raise_test"; + struct test_oom_raise_max *skel = NULL; + struct bpf_link *link = NULL; + struct raise_max_args args = {}; + struct memory_holder worker; + int cg_fd = -1, prog_fd, status = 0; + unsigned long long cg_id; + char buf[256] = {}; + char pbuf[32]; + LIBBPF_OPTS(bpf_test_run_opts, topts, + .ctx_in = &args, + .ctx_size_in = sizeof(args), + ); + + memory_holder_init(&worker); + + if (!ASSERT_OK(setup_cgroup_environment(), "setup_cgroup_env")) + return; + + cg_fd = create_and_get_cgroup(cg_path); + if (!ASSERT_GE(cg_fd, 0, "create_cgroup")) + goto cleanup; + + cg_id = get_cgroup_id(cg_path); + if (!ASSERT_GT(cg_id, 0, "get_cgroup_id")) + goto cleanup; + + snprintf(buf, sizeof(buf), "%lu", 16 * MB); + if (!ASSERT_OK(write_cgroup_file(cg_path, "memory.max", buf), + "set memory.max")) + goto cleanup; + write_cgroup_file(cg_path, "memory.swap.max", "0"); + + skel = test_oom_raise_max__open_and_load(); + if (!ASSERT_OK_PTR(skel, "open_and_load")) + goto cleanup; + + skel->bss->new_max_bytes = 64 * MB; + + link = bpf_map__attach_cgroup_opts(skel->maps.test_bpf_oom_raise, + cg_fd, &opts); + if (!ASSERT_OK_PTR(link, "attach_cgroup")) { + link = NULL; + goto cleanup; + } + + if (!ASSERT_OK(memory_holder_spawn(&worker, 24 * MB, 0, 0, true), + "spawn_worker")) + goto cleanup; + + snprintf(pbuf, sizeof(pbuf), "%d", worker.pid); + if (!ASSERT_OK(write_cgroup_file(cg_path, "cgroup.procs", pbuf), + "place_worker") || + !ASSERT_OK(memory_holder_start(&worker), "start_worker")) + goto cleanup; + + ASSERT_EQ(waitpid(worker.pid, &status, 0), worker.pid, "waitpid"); + worker.pid = -1; + ASSERT_TRUE(WIFEXITED(status) && WEXITSTATUS(status) == 0, + "child_survived"); + + if (ASSERT_OK(read_cgroup_file(cg_path, "memory.max", buf, sizeof(buf)), + "read memory.max")) { + char expected[64]; + + snprintf(expected, sizeof(expected), "%lu", 64 * MB); + ASSERT_OK_PTR(strstr(buf, expected), "memory.max raised"); + } + + if (ASSERT_OK(read_cgroup_file(cg_path, "memory.events", buf, + sizeof(buf)), "read memory.events")) { + ASSERT_OK_PTR(strstr(buf, "oom 1"), "oom event recorded"); + ASSERT_OK_PTR(strstr(buf, "oom_kill 0"), "no task killed"); + } + + /* Verify SYSCALL boundary and negative checks */ + prog_fd = bpf_program__fd(skel->progs.test_raise_max_syscall); + + /* Equal limit must return -EINVAL */ + args.cgroup_id = cg_id; + args.max_bytes = 64 * MB; + if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_equal")) + ASSERT_EQ((int)topts.retval, -EINVAL, "reject_equal"); + + /* Lower limit must return -EINVAL */ + args.cgroup_id = cg_id; + args.max_bytes = 32 * MB; + if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_lower")) + ASSERT_EQ((int)topts.retval, -EINVAL, "reject_lower"); + + /* Root memcg must return -EINVAL */ + args.cgroup_id = 0; + args.max_bytes = UINT64_MAX; + if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_root")) + ASSERT_EQ((int)topts.retval, -EINVAL, "reject_root"); + + /* UINT64_MAX must clamp cleanly to "max" */ + args.cgroup_id = cg_id; + args.max_bytes = UINT64_MAX; + if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_max")) + ASSERT_OK((int)topts.retval, "raise_to_max"); + + if (ASSERT_OK(read_cgroup_file(cg_path, "memory.max", buf, sizeof(buf)), + "read memory.max after UINT64_MAX")) + ASSERT_OK_PTR(strstr(buf, "max"), "memory.max unlimited"); + +cleanup: + memory_holder_cleanup(&worker); + bpf_link__destroy(link); + if (cg_fd >= 0) + close(cg_fd); + test_oom_raise_max__destroy(skel); + cleanup_cgroup_environment(); +} diff --git a/tools/testing/selftests/bpf/progs/test_oom_raise_max.c b/tools/testing/selftests/bpf/progs/test_oom_raise_max.c new file mode 100644 index 000000000000..e58e382791aa --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_oom_raise_max.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "vmlinux.h" +#include +#include +#include +#include "test_oom_raise_max.h" + +char _license[] SEC("license") = "GPL"; + +__u64 new_max_bytes; + +SEC("struct_ops.s/handle_out_of_memory") +int BPF_PROG(test_raise_max_oom, struct oom_control *oc, struct cgroup *cgrp) +{ + struct mem_cgroup *memcg = oc->memcg; + + if (!memcg) + return 0; + + if (bpf_mem_cgroup_raise_max(memcg, new_max_bytes)) + return 0; + + return 1; +} + +SEC("syscall") +int test_raise_max_syscall(struct raise_max_args *args) +{ + struct mem_cgroup *memcg; + int ret; + + if (args->cgroup_id) { + struct cgroup *cgrp = bpf_cgroup_from_id(args->cgroup_id); + + if (!cgrp) + return -ENOENT; + memcg = bpf_get_mem_cgroup(&cgrp->self); + bpf_cgroup_release(cgrp); + } else { + memcg = bpf_get_root_mem_cgroup(); + } + + if (!memcg) + return -ENODEV; + + ret = bpf_mem_cgroup_raise_max(memcg, args->max_bytes); + bpf_put_mem_cgroup(memcg); + return ret; +} + +SEC(".struct_ops.link") +struct bpf_oom_ops test_bpf_oom_raise = { + .name = "bpf_oom_raise_max", + .handle_out_of_memory = (void *)test_raise_max_oom, +}; diff --git a/tools/testing/selftests/bpf/progs/test_oom_raise_max.h b/tools/testing/selftests/bpf/progs/test_oom_raise_max.h new file mode 100644 index 000000000000..f25bde9a295a --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_oom_raise_max.h @@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __TEST_OOM_RAISE_MAX_H +#define __TEST_OOM_RAISE_MAX_H + +struct raise_max_args { + __u64 cgroup_id; + __u64 max_bytes; +}; + +#endif /* __TEST_OOM_RAISE_MAX_H */ -- 2.56.1.911.g883d8bcb20-goog