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>, Michal Hocko <mhocko@suse.com>
Subject: [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() BPF kfunc
Date: Wed, 2 Dec 2026 13:03:54 -0800 [thread overview]
Message-ID: <20261202210415.517651-5-bgardon@google.com> (raw)
In-Reply-To: <20261202210415.517651-1-bgardon@google.com>
From: Roman Gushchin <roman.gushchin@linux.dev>
Introduce bpf_out_of_memory() to allow BPF programs to declare an
OOM proactively.
BPF-declared OOMs are marked via oc->bpf_declared so they do not panic the
kernel when no killable tasks remain or panic_on_oom is set. They do not
terminate the calling task via sysctl_oom_kill_allocating_task either.
Registered directly on BPF_PROG_TYPE_SYSCALL (extended to PSI tracepoints
later in the series) so bpf_oom_ops and arbitrary tracing/LSM hooks cannot
deadlock on oom_lock.
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:
- Registered directly on BPF_PROG_TYPE_SYSCALL instead of
BPF_PROG_TYPE_UNSPEC.
- Moved BPF_OOM_FLAGS_ALL outside BTF-exported enum bpf_oom_flags.
- Added oc->bpf_declared guard against panic() and
sysctl_oom_kill_allocating_task, clamped oc->totalpages in
constrained_alloc(), normalized root_mem_cgroup to NULL, rejected
dying memcgs, and accounted MEMCG_OOM.
v3: https://lore.kernel.org/all/20260127024421.494929-10-roman.gushchin@linux.dev/
include/linux/oom.h | 19 +++++++++
mm/oom_kill.c | 101 ++++++++++++++++++++++++++++++++++++++++----
2 files changed, 111 insertions(+), 9 deletions(-)
diff --git a/include/linux/oom.h b/include/linux/oom.h
index ca3192c42533..168b64dfb6ad 100644
--- a/include/linux/oom.h
+++ b/include/linux/oom.h
@@ -14,6 +14,12 @@ struct notifier_block;
struct mem_cgroup;
struct task_struct;
+enum bpf_oom_flags {
+ BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK = BIT(0),
+};
+
+#define BPF_OOM_FLAGS_ALL BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK
+
enum oom_constraint {
CONSTRAINT_NONE,
CONSTRAINT_CPUSET,
@@ -55,6 +61,9 @@ struct oom_control {
#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_MEMCG)
/* Active BPF OOM handler name */
const char *bpf_handler_name;
+
+ /* Declared via BPF outside an allocation failure */
+ bool bpf_declared;
#endif
};
@@ -63,11 +72,21 @@ static inline const char *oom_handler_name(const struct oom_control *oc)
{
return oc->bpf_handler_name;
}
+
+static inline bool is_bpf_declared_oom(const struct oom_control *oc)
+{
+ return oc->bpf_declared;
+}
#else
static inline const char *oom_handler_name(const struct oom_control *oc)
{
return NULL;
}
+
+static inline bool is_bpf_declared_oom(const struct oom_control *oc)
+{
+ return false;
+}
#endif
extern struct mutex oom_lock;
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 85da2a39e18a..9b5df00f98ab 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -256,7 +256,9 @@ static enum oom_constraint constrained_alloc(struct oom_control *oc)
int nid;
if (is_memcg_oom(oc)) {
- oc->totalpages = mem_cgroup_get_max(oc->memcg) ?: 1;
+ unsigned long max = mem_cgroup_get_max(oc->memcg);
+
+ oc->totalpages = min(max, totalram_pages() + total_swap_pages) ?: 1;
return CONSTRAINT_MEMCG;
}
@@ -1024,7 +1026,8 @@ static void oom_kill_process(struct oom_control *oc, const char *message)
task_unlock(victim);
if (__ratelimit(&oom_rs)) {
- dump_header(oc);
+ if (!is_bpf_declared_oom(oc))
+ dump_header(oc);
dump_oom_victim(oc, victim);
}
@@ -1065,8 +1068,8 @@ static void check_panic_on_oom(struct oom_control *oc)
if (oc->constraint != CONSTRAINT_NONE)
return;
}
- /* Do not panic for oom kills triggered by sysrq */
- if (is_sysrq_oom(oc))
+ /* Do not panic for oom kills triggered by sysrq or BPF */
+ if (is_sysrq_oom(oc) || is_bpf_declared_oom(oc))
return;
dump_header(oc);
panic("Out of memory: %s panic_on_oom is enabled\n",
@@ -1138,7 +1141,8 @@ bool out_of_memory(struct oom_control *oc)
oc->nodemask = NULL;
check_panic_on_oom(oc);
- if (!is_memcg_oom(oc) && sysctl_oom_kill_allocating_task &&
+ if (!is_memcg_oom(oc) && !is_bpf_declared_oom(oc) &&
+ sysctl_oom_kill_allocating_task &&
current->mm && !oom_unkillable_task(current) &&
oom_cpuset_eligible(current, oc) &&
current->signal->oom_score_adj != OOM_SCORE_ADJ_MIN) {
@@ -1165,7 +1169,8 @@ bool out_of_memory(struct oom_control *oc)
* system level, we cannot survive this and will enter
* an endless loop in the allocator. Bail out now.
*/
- if (!is_sysrq_oom(oc) && !is_memcg_oom(oc))
+ if (!is_sysrq_oom(oc) && !is_memcg_oom(oc) &&
+ !is_bpf_declared_oom(oc))
panic("System is deadlocked on memory\n");
}
if (oc->chosen && oc->chosen != (void *)-1UL)
@@ -1319,6 +1324,66 @@ __bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
return 0;
}
+/**
+ * bpf_out_of_memory() - Declare an OOM condition and invoke OOM handling
+ * @memcg__nullable: target memory cgroup, or NULL for system-wide OOM
+ * @order: allocation page order (0..MAX_PAGE_ORDER), passed through to
+ * bpf_oom_ops handlers as a memory-reclaim size hint
+ * @flags: bitmask of &enum bpf_oom_flags
+ *
+ * Synchronizes on oom_lock: waits (interruptible by fatal signals) when
+ * BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK is set, or returns -EBUSY immediately if
+ * oom_lock is contended. Root @memcg__nullable is normalized to NULL
+ * (system-wide OOM); dying memory cgroups are rejected with -EINVAL.
+ *
+ * Return: 1 if forward progress was made, 0 if no process was terminated, or a
+ * negative errno on failure.
+ */
+__bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable,
+ int order, u64 flags)
+{
+ struct oom_control oc = {
+ .gfp_mask = GFP_KERNEL,
+ .order = order,
+ .bpf_declared = true,
+ };
+ int ret;
+
+ if (flags & ~BPF_OOM_FLAGS_ALL)
+ return -EINVAL;
+
+ /*
+ * Keep order so callers can signal a higher-order memory need to
+ * reactive bpf_oom_ops handlers (or exercise high-order OOM policies).
+ */
+ if (oc.order < 0 || oc.order > MAX_PAGE_ORDER)
+ return -EINVAL;
+
+ if (memcg__nullable && mem_cgroup_is_root(memcg__nullable))
+ memcg__nullable = NULL;
+
+ if (memcg__nullable && memcg_is_dying(memcg__nullable))
+ return -EINVAL;
+
+ oc.memcg = memcg__nullable;
+
+ if (flags & BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK) {
+ ret = mutex_lock_killable(&oom_lock);
+ if (ret)
+ return ret;
+ } else if (!mutex_trylock(&oom_lock)) {
+ return -EBUSY;
+ }
+
+ if (oc.memcg)
+ memcg_memory_event(oc.memcg, MEMCG_OOM);
+
+ ret = out_of_memory(&oc);
+
+ mutex_unlock(&oom_lock);
+ return ret;
+}
+
__bpf_kfunc_end_defs();
BTF_KFUNCS_START(bpf_oom_kfuncs)
@@ -1346,16 +1411,34 @@ static const struct btf_kfunc_id_set bpf_oom_kfunc_set = {
.filter = bpf_oom_kfunc_filter,
};
+BTF_KFUNCS_START(bpf_declare_oom_kfuncs)
+BTF_ID_FLAGS(func, bpf_out_of_memory, KF_SLEEPABLE)
+BTF_KFUNCS_END(bpf_declare_oom_kfuncs)
+
+static const struct btf_kfunc_id_set bpf_declare_oom_syscall_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &bpf_declare_oom_kfuncs,
+};
+
static int __init bpf_oom_init(void)
{
int err;
err = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS,
&bpf_oom_kfunc_set);
- if (err)
- pr_warn("error while registering bpf oom kfuncs: %d\n", err);
+ if (err) {
+ pr_warn("error while registering struct_ops bpf oom kfuncs: %d\n", err);
+ return err;
+ }
+
+ err = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+ &bpf_declare_oom_syscall_kfunc_set);
+ if (err) {
+ pr_warn("error while registering syscall bpf oom kfuncs: %d\n", err);
+ return err;
+ }
- return err;
+ return 0;
}
late_initcall(bpf_oom_init);
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-02 21:04 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 ` Ben Gardon [this message]
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 ` [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
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-5-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-mm@kvack.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mhocko@kernel.org \
--cc=mhocko@suse.com \
--cc=rientjes@google.com \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
--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