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 10/17] selftests/bpf: Add PSI tracepoint OOM test
Date: Wed, 2 Dec 2026 13:04:00 -0800 [thread overview]
Message-ID: <20261202210415.517651-11-bgardon@google.com> (raw)
In-Reply-To: <20261202210415.517651-1-bgardon@google.com>
From: Roman Gushchin <roman.gushchin@linux.dev>
Add a BPF selftest attaching to the psi_avgs_work_tp tracepoint to
monitor memory pressure in a cgroup throttled by memory.high.
When full memory stall growth exceeds 50 ms within an update interval,
the tracepoint schedules a sleepable bpf_task_work callback that invokes
bpf_out_of_memory() for the pressured cgroup, verifying that the
allocating task is terminated with SIGKILL and memory.events records
oom_kill 1.
Signed-off-by: Roman Gushchin <roman.gushchin@linux.dev>
Co-developed-by: Ben Gardon <bgardon@google.com>
Signed-off-by: Ben Gardon <bgardon@google.com>
---
v3 -> v4:
- Updated subject/body from stale "PSI struct ops test" to reflect the
psi_avgs_work_tp tracepoint design.
- Indexed the BTF-flattened group->total array explicitly as
total[PSI_AVGS * (NR_PSI_STATES - 1) + PSI_MEM_FULL], added missing
SPDX header, removed redundant extern __ksym declarations, and
dropped the first-interval skip.
- Replaced cgroup freezing and racy fork/migration with a sync pipe,
removed dead struct_ops remnants and FD/child leaks, added runtime PSI
skip check, verified SIGKILL, and renamed to serial_test_psi().
v3: https://lore.kernel.org/all/20260127024421.494929-18-roman.gushchin@linux.dev/
.../selftests/bpf/prog_tests/test_psi.c | 107 ++++++++++++++++++
tools/testing/selftests/bpf/progs/test_psi.c | 88 ++++++++++++++
2 files changed, 195 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_psi.c
create mode 100644 tools/testing/selftests/bpf/progs/test_psi.c
diff --git a/tools/testing/selftests/bpf/prog_tests/test_psi.c b/tools/testing/selftests/bpf/prog_tests/test_psi.c
new file mode 100644
index 000000000000..9f3fdf2d6c5b
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_psi.c
@@ -0,0 +1,107 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <sys/wait.h>
+#include <test_progs.h>
+#include <bpf/bpf.h>
+#include <bpf/btf.h>
+
+#include "cgroup_helpers.h"
+#include "memory_holder.h"
+
+#define MB (1024UL * 1024UL)
+#include "test_psi.skel.h"
+
+/*
+ * Serialized because this test attaches to the global psi_avgs_work_tp
+ * tracepoint and drives real memory pressure / OOM under memory.high, which
+ * can interfere with other OOM tests running concurrently under test_progs -j.
+ */
+void serial_test_psi(void)
+{
+ const char *parent_cg = "/psi_test";
+ const char *target_cg = "/psi_test/cg";
+ struct test_psi *skel = NULL;
+ struct memory_holder worker;
+ int parent_fd = -1, target_fd = -1;
+ int status = 0, err;
+ unsigned long long cg_id;
+ char buf[4096] = {};
+
+ memory_holder_init(&worker);
+
+ if (access("/proc/pressure/memory", R_OK) != 0) {
+ test__skip();
+ return;
+ }
+
+ err = setup_cgroup_environment();
+ if (!ASSERT_OK(err, "setup_cgroup_environment"))
+ return;
+
+ parent_fd = create_and_get_cgroup(parent_cg);
+ if (!ASSERT_GE(parent_fd, 0, "create parent cgroup"))
+ goto cleanup;
+
+ err = write_cgroup_file(parent_cg, "cgroup.subtree_control", "+memory");
+ if (!ASSERT_OK(err, "enable memory controller"))
+ goto cleanup;
+
+ target_fd = create_and_get_cgroup(target_cg);
+ if (!ASSERT_GE(target_fd, 0, "create target cgroup"))
+ goto cleanup;
+
+ cg_id = get_cgroup_id(target_cg);
+ if (!ASSERT_GT(cg_id, 0, "get_cgroup_id"))
+ goto cleanup;
+
+ snprintf(buf, sizeof(buf), "%lu", 40 * MB);
+ err = write_cgroup_file(target_cg, "memory.high", buf);
+ if (!ASSERT_OK(err, "set memory.high"))
+ goto cleanup;
+ write_cgroup_file(target_cg, "memory.swap.max", "0");
+
+ err = memory_holder_spawn(&worker, 500 * MB, MB, 0, true);
+ if (!ASSERT_OK(err, "spawn worker"))
+ goto cleanup;
+
+ snprintf(buf, sizeof(buf), "%d", worker.pid);
+ err = write_cgroup_file(target_cg, "cgroup.procs", buf);
+ if (!ASSERT_OK(err, "put child into cgroup"))
+ goto cleanup;
+
+ skel = test_psi__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ goto cleanup;
+
+ skel->bss->high_pressure_cgroup_id = cg_id;
+ skel->bss->my_pid = getpid();
+
+ err = test_psi__attach(skel);
+ if (!ASSERT_OK(err, "attach"))
+ goto cleanup;
+
+ /* Release worker now that it is inside target_cg and PSI BPF is attached */
+ if (!ASSERT_OK(memory_holder_start(&worker), "release worker"))
+ goto cleanup;
+
+ if (!ASSERT_EQ(waitpid(worker.pid, &status, 0), worker.pid,
+ "waitpid worker"))
+ goto cleanup;
+ worker.pid = -1;
+
+ ASSERT_TRUE(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL,
+ "worker terminated by SIGKILL");
+
+ if (ASSERT_OK(read_cgroup_file(target_cg, "memory.events",
+ buf, sizeof(buf)),
+ "read memory.events"))
+ ASSERT_OK_PTR(strstr(buf, "oom_kill 1"), "oom_kill count check");
+
+cleanup:
+ memory_holder_cleanup(&worker);
+ if (target_fd >= 0)
+ close(target_fd);
+ if (parent_fd >= 0)
+ close(parent_fd);
+ test_psi__destroy(skel);
+ cleanup_cgroup_environment();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_psi.c b/tools/testing/selftests/bpf/progs/test_psi.c
new file mode 100644
index 000000000000..927c1b8323d0
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_psi.c
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include "bpf_experimental.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char _license[] SEC("license") = "GPL";
+
+/* cgroup which will experience the high memory pressure */
+u64 high_pressure_cgroup_id;
+u32 my_pid = 0;
+
+/* last total full memory pressure value */
+u64 last_mem_full_total = 0;
+
+struct elem {
+ struct bpf_task_work tw;
+};
+
+struct {
+ __uint(type, BPF_MAP_TYPE_ARRAY);
+ __uint(max_entries, 1);
+ __type(key, int);
+ __type(value, struct elem);
+} tw_map SEC(".maps");
+
+static int psi_oom_work(struct bpf_map *map, void *key, void *value)
+{
+ struct cgroup *cgrp;
+ struct mem_cgroup *memcg;
+
+ cgrp = bpf_cgroup_from_id(high_pressure_cgroup_id);
+ if (!cgrp)
+ return 0;
+
+ memcg = bpf_get_mem_cgroup(&cgrp->self);
+ if (memcg) {
+ bpf_out_of_memory(memcg, 0, BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK);
+ bpf_put_mem_cgroup(memcg);
+ }
+
+ bpf_cgroup_release(cgrp);
+ return 0;
+}
+
+static void schedule_oom_work(void)
+{
+ struct task_struct *task;
+ struct elem *val;
+ int key = 0;
+
+ task = bpf_task_from_pid(my_pid);
+ if (task) {
+ val = bpf_map_lookup_elem(&tw_map, &key);
+ if (val)
+ bpf_task_work_schedule_signal(task, &val->tw,
+ &tw_map, psi_oom_work);
+ bpf_task_release(task);
+ }
+}
+
+SEC("tp_btf/psi_avgs_work_tp")
+int BPF_PROG(psi_avgs, struct psi_group *group)
+{
+ u64 current_total;
+ u64 growth;
+
+ /* Monitor only a single target cgroup */
+ if (group->cgroup_id != high_pressure_cgroup_id)
+ return 0;
+
+ /*
+ * Check for memory pressure. BTF flattens the 2D array
+ * total[NR_PSI_AGGREGATORS][NR_PSI_STATES - 1] in vmlinux.h.
+ */
+ current_total = BPF_CORE_READ(group,
+ total[PSI_AVGS * (NR_PSI_STATES - 1) +
+ PSI_MEM_FULL]);
+ growth = current_total - last_mem_full_total;
+ last_mem_full_total = current_total;
+
+ /* Declare an OOM if growth > 50ms within the update period */
+ if (growth > 50000000)
+ schedule_oom_work();
+
+ return 0;
+}
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-02 21:04 UTC|newest]
Thread overview: 22+ 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 ` Ben Gardon [this message]
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: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: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-11-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.