All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ben Gardon <bgardon@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	 Andrii Nakryiko <andrii@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	 Johannes Weiner <hannes@cmpxchg.org>,
	Michal Hocko <mhocko@kernel.org>,
	 Shakeel Butt <shakeel.butt@linux.dev>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	 Tejun Heo <tj@kernel.org>
Cc: JP Kobryn <jp.kobryn@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>,
	 Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	 Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	 Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	 David Rientjes <rientjes@google.com>,
	Suren Baghdasaryan <surenb@google.com>,
	Josh Don <joshdon@google.com>,
	 Sweet Tea Dorminy <sweettea-kernel@dorminy.me>,
	Chuyi Zhou <zhouchuyi@bytedance.com>,
	 Yafang Shao <laoar.shao@gmail.com>,
	benbwgardon@gmail.com, bpf@vger.kernel.org,  linux-mm@kvack.org,
	cgroups@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ben Gardon <bgardon@google.com>, Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org
Subject: [PATCH bpf-next v4 13/17] selftests/bpf: Add bpf_oom_proactive_kill() proactive OOM test
Date: Wed,  2 Dec 2026 13:04:03 -0800	[thread overview]
Message-ID: <20261202210415.517651-14-bgardon@google.com> (raw)
In-Reply-To: <20261202210415.517651-1-bgardon@google.com>

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@google.com>
---
 .../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


  parent reply	other threads:[~2026-12-02 21:04 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 21:03 [PATCH bpf-next v4 00/17] mm: BPF OOM Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 01/17] bpf: mark struct oom_control's memcg field as TRUSTED_OR_NULL Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 02/17] mm, bpf: introduce BPF OOM struct ops Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() " Ben Gardon
2026-12-02 21:18   ` sashiko-bot
2026-12-02 21:03 ` [PATCH bpf-next v4 05/17] selftests/bpf: Add BPF OOM struct ops test Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 06/17] sched/psi: add a tracepoint to psi_avgs_work() Ben Gardon
2026-12-02 21:15   ` sashiko-bot
2026-12-02 21:03 ` [PATCH bpf-next v4 07/17] sched/psi: add cgroup_id field to psi_group structure Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 08/17] mm, bpf: allow calling bpf_out_of_memory() from a PSI tracepoint Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 09/17] selftests/bpf: Enable CONFIG_PSI Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 10/17] selftests/bpf: Add PSI tracepoint OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 11/17] selftests/bpf: Add systemd-oomd style OOM policy test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 12/17] mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention Ben Gardon
2026-12-02 21:04 ` Ben Gardon [this message]
2026-12-02 21:04 ` [PATCH bpf-next v4 14/17] selftests/bpf: Add negative and verifier tests for BPF OOM kfuncs Ben Gardon
2026-12-02 21:15   ` sashiko-bot
2026-12-02 21:04 ` [PATCH bpf-next v4 15/17] mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc Ben Gardon
2026-12-02 21:45   ` sashiko-bot
2026-12-02 21:04 ` [PATCH bpf-next v4 16/17] selftests/bpf: Add bpf_mem_cgroup_raise_max() OOM test Ben Gardon
2026-12-02 21:04 ` [PATCH bpf-next v4 17/17] Documentation/bpf: Document BPF OOM handling and kfuncs Ben Gardon
2026-12-02 21:38 ` [PATCH bpf-next v4 00/17] mm: BPF OOM Ben Gardon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202210415.517651-14-bgardon@google.com \
    --to=bgardon@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=benbwgardon@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=cgroups@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=joshdon@google.com \
    --cc=jp.kobryn@linux.dev \
    --cc=laoar.shao@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=mhocko@kernel.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=surenb@google.com \
    --cc=sweettea-kernel@dorminy.me \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    --cc=zhouchuyi@bytedance.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.