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 16/17] selftests/bpf: Add bpf_mem_cgroup_raise_max() OOM test
Date: Wed, 2 Dec 2026 13:04:06 -0800 [thread overview]
Message-ID: <20261202210415.517651-17-bgardon@google.com> (raw)
In-Reply-To: <20261202210415.517651-1-bgardon@google.com>
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 <bgardon@google.com>
---
.../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 <errno.h>
+#include <stdint.h>
+#include <sys/wait.h>
+#include <test_progs.h>
+#include <bpf/bpf.h>
+
+#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 <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#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
next prev parent reply other threads:[~2026-12-02 21:05 UTC|newest]
Thread overview: 18+ 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: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: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 ` [PATCH bpf-next v4 13/17] selftests/bpf: Add bpf_oom_proactive_kill() proactive OOM test Ben Gardon
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:04 ` [PATCH bpf-next v4 15/17] mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc Ben Gardon
2026-12-02 21:04 ` Ben Gardon [this message]
2026-12-02 21:04 ` [PATCH bpf-next v4 17/17] Documentation/bpf: Document BPF OOM handling and kfuncs 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-17-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox