[PATCH bpf-next v4 13/17] selftests/bpf: Add bpf_oom_proactive_kill() proactive OOM test

From: Ben Gardon

Date: Wed Dec 02 2026 - 16:05:06 EST


Add a BPF selftest exercising bpf_oom_proactive_kill() from a
BPF_PROG_TYPE_SYSCALL program.

Verify that OOM_SCORE_ADJ_MIN (-1000) tasks and out-of-hierarchy memcg
attributions return -EINVAL while an eligible task is terminated with
SIGKILL and increments memory.events oom_kill.

Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
---
.../bpf/prog_tests/test_oom_proactive_kill.c | 119 ++++++++++++++++++
.../bpf/progs/test_oom_proactive_kill.c | 46 +++++++
.../bpf/progs/test_oom_proactive_kill.h | 12 ++
3 files changed, 177 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_oom_proactive_kill.c
create mode 100644 tools/testing/selftests/bpf/progs/test_oom_proactive_kill.c
create mode 100644 tools/testing/selftests/bpf/progs/test_oom_proactive_kill.h

diff --git a/tools/testing/selftests/bpf/prog_tests/test_oom_proactive_kill.c b/tools/testing/selftests/bpf/prog_tests/test_oom_proactive_kill.c
new file mode 100644
index 000000000000..ca2165f03278
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_oom_proactive_kill.c
@@ -0,0 +1,119 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <errno.h>
+#include <sys/wait.h>
+#include <test_progs.h>
+#include <bpf/bpf.h>
+
+#include "cgroup_helpers.h"
+#include "memory_holder.h"
+#include "progs/test_oom_proactive_kill.h"
+#include "test_oom_proactive_kill.skel.h"
+
+/*
+ * Serialized because bpf_oom_proactive_kill() acquires the global oom_lock
+ * and kills processes via the OOM path, which would collide with other OOM
+ * tests running in parallel under test_progs -j.
+ */
+void serial_test_oom_proactive_kill(void)
+{
+ const char *cg_a = "/oom_proactive_a";
+ const char *cg_b = "/oom_proactive_b";
+ struct test_oom_proactive_kill *skel = NULL;
+ struct memory_holder w_protected, w_target;
+ struct proactive_kill_args args = {};
+ int cg_a_fd = -1, cg_b_fd = -1;
+ unsigned long long id_a, id_b;
+ int prog_fd, status = 0;
+ char buf[4096] = {};
+ char pbuf[32];
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &args,
+ .ctx_size_in = sizeof(args),
+ );
+
+ memory_holder_init(&w_protected);
+ memory_holder_init(&w_target);
+
+ if (!ASSERT_OK(setup_cgroup_environment(), "setup_cgroup_env"))
+ return;
+
+ cg_a_fd = create_and_get_cgroup(cg_a);
+ cg_b_fd = create_and_get_cgroup(cg_b);
+ if (!ASSERT_GE(cg_a_fd, 0, "create cg_a") ||
+ !ASSERT_GE(cg_b_fd, 0, "create cg_b"))
+ goto cleanup;
+
+ id_a = get_cgroup_id(cg_a);
+ id_b = get_cgroup_id(cg_b);
+ if (!ASSERT_GT(id_a, 0, "get_cgroup_id cg_a") ||
+ !ASSERT_GT(id_b, 0, "get_cgroup_id cg_b"))
+ goto cleanup;
+
+ skel = test_oom_proactive_kill__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto cleanup;
+
+ prog_fd = bpf_program__fd(skel->progs.proactive_kill_prog);
+
+ if (!ASSERT_OK(memory_holder_spawn(&w_protected, 0, 0, -1000, false),
+ "spawn_protected") ||
+ !ASSERT_OK(memory_holder_spawn(&w_target, 0, 0, 0, false),
+ "spawn_target"))
+ goto cleanup;
+
+ snprintf(pbuf, sizeof(pbuf), "%d", w_protected.pid);
+ if (!ASSERT_OK(write_cgroup_file(cg_a, "cgroup.procs", pbuf),
+ "place_protected"))
+ goto cleanup;
+ snprintf(pbuf, sizeof(pbuf), "%d", w_target.pid);
+ if (!ASSERT_OK(write_cgroup_file(cg_a, "cgroup.procs", pbuf),
+ "place_target"))
+ goto cleanup;
+
+ if (!ASSERT_OK(memory_holder_start(&w_protected), "start_protected") ||
+ !ASSERT_OK(memory_holder_wait_ready(&w_protected), "wait_protected") ||
+ !ASSERT_OK(memory_holder_start(&w_target), "start_target") ||
+ !ASSERT_OK(memory_holder_wait_ready(&w_target), "wait_target"))
+ goto cleanup;
+
+ /* OOM_SCORE_ADJ_MIN (-1000) task must be rejected with -EINVAL */
+ args.cgroup_id = id_a;
+ args.pid = w_protected.pid;
+ args.wait_on_oom_lock = 1;
+ if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_protected"))
+ ASSERT_EQ((int)topts.retval, -EINVAL, "reject_oom_score_adj_min");
+
+ /* Task outside origin memcg hierarchy must be rejected with -EINVAL */
+ args.cgroup_id = id_b;
+ args.pid = w_target.pid;
+ args.wait_on_oom_lock = 1;
+ if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_wrong_cg"))
+ ASSERT_EQ((int)topts.retval, -EINVAL, "reject_wrong_hierarchy");
+
+ /* Eligible task inside origin memcg must be terminated via OOM */
+ args.cgroup_id = id_a;
+ args.pid = w_target.pid;
+ args.wait_on_oom_lock = 1;
+ if (!ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_kill") ||
+ !ASSERT_OK((int)topts.retval, "proactive_kill_retval"))
+ goto cleanup;
+
+ ASSERT_EQ(waitpid(w_target.pid, &status, 0), w_target.pid, "waitpid_target");
+ w_target.pid = -1;
+ ASSERT_TRUE(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL,
+ "target_sigkill");
+
+ if (ASSERT_OK(read_cgroup_file(cg_a, "memory.events", buf, sizeof(buf)),
+ "read memory.events"))
+ ASSERT_OK_PTR(strstr(buf, "oom_kill 1"), "oom_kill incremented");
+
+cleanup:
+ memory_holder_cleanup(&w_protected);
+ memory_holder_cleanup(&w_target);
+ if (cg_a_fd >= 0)
+ close(cg_a_fd);
+ if (cg_b_fd >= 0)
+ close(cg_b_fd);
+ test_oom_proactive_kill__destroy(skel);
+ cleanup_cgroup_environment();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_oom_proactive_kill.c b/tools/testing/selftests/bpf/progs/test_oom_proactive_kill.c
new file mode 100644
index 000000000000..591a2e0b284d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_oom_proactive_kill.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include "test_oom_proactive_kill.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("syscall")
+int proactive_kill_prog(struct proactive_kill_args *args)
+{
+ struct mem_cgroup *memcg = NULL;
+ struct task_struct *task;
+ u64 flags = args->flags;
+ struct cgroup *cgrp;
+ int ret;
+
+ if (args->wait_on_oom_lock)
+ flags |= BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK;
+
+ task = bpf_task_from_pid(args->pid);
+ if (!task)
+ return -ESRCH;
+
+ if (args->cgroup_id) {
+ cgrp = bpf_cgroup_from_id(args->cgroup_id);
+ if (!cgrp) {
+ bpf_task_release(task);
+ return -ENOENT;
+ }
+ memcg = bpf_get_mem_cgroup(&cgrp->self);
+ bpf_cgroup_release(cgrp);
+ if (!memcg) {
+ bpf_task_release(task);
+ return -ENODEV;
+ }
+ }
+
+ ret = bpf_oom_proactive_kill(memcg, task, "bpf proactive oom test",
+ flags);
+
+ if (memcg)
+ bpf_put_mem_cgroup(memcg);
+ bpf_task_release(task);
+ return ret;
+}
diff --git a/tools/testing/selftests/bpf/progs/test_oom_proactive_kill.h b/tools/testing/selftests/bpf/progs/test_oom_proactive_kill.h
new file mode 100644
index 000000000000..d3bf7596de21
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_oom_proactive_kill.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __TEST_OOM_PROACTIVE_KILL_H
+#define __TEST_OOM_PROACTIVE_KILL_H
+
+struct proactive_kill_args {
+ __u64 cgroup_id;
+ __u64 flags;
+ __s32 pid;
+ __u32 wait_on_oom_lock;
+};
+
+#endif /* __TEST_OOM_PROACTIVE_KILL_H */
--
2.56.1.911.g883d8bcb20-goog