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>,
Muchun Song <muchun.song@linux.dev>
Subject: [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc
Date: Wed, 2 Dec 2026 13:03:53 -0800 [thread overview]
Message-ID: <20261202210415.517651-4-bgardon@google.com> (raw)
In-Reply-To: <20261202210415.517651-1-bgardon@google.com>
From: Roman Gushchin <roman.gushchin@linux.dev>
Introduce bpf_oom_kill_process() for bpf_oom_ops programs to terminate a chosen
process through oom_kill_process(), preserving the existing path used by the
kernel OOM implementation.
Init/kernel threads (unkillable), tasks protected with OOM_SCORE_ADJ_MIN, tasks
with no live mm or MMF_OOM_SKIP set, and tasks outside oc->memcg's hierarchy
during a memcg-scoped OOM are rejected in the kernel.
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:
- Guard BPF OOM kfunc block with CONFIG_CGROUP_BPF && CONFIG_MEMCG so
!CONFIG_MEMCG builds do not fail resolve_btfids.
- Use find_lock_task_mm() and reject tasks with no live mm or
MMF_OOM_SKIP set (-ESRCH) in addition to oom_unkillable_task() and
OOM_SCORE_ADJ_MIN checks.
- Add mem_cgroup_task_in_hierarchy() and reject tasks outside oc->memcg
with -EINVAL during memcg-scoped OOMs.
- Fix bpf_oom_kfunc_filter() to check btf_id_set8_contains() and use
default-deny so BPF_PROG_TYPE_EXT (freplace) on non-OOM struct_ops
cannot bypass the filter.
v3: https://lore.kernel.org/all/20260127024421.494929-9-roman.gushchin@linux.dev/
include/linux/memcontrol.h | 20 ++++++++
mm/oom_kill.c | 102 +++++++++++++++++++++++++++++++++++++
2 files changed, 122 insertions(+)
diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h
index 69e1bcc67c58..bc30d7baecd7 100644
--- a/include/linux/memcontrol.h
+++ b/include/linux/memcontrol.h
@@ -892,6 +892,20 @@ static inline bool mem_cgroup_is_descendant(const struct mem_cgroup *memcg,
return cgroup_is_descendant(memcg->css.cgroup, root->css.cgroup);
}
+static inline bool mem_cgroup_task_in_hierarchy(struct task_struct *task,
+ const struct mem_cgroup *origin)
+{
+ struct mem_cgroup *memcg;
+ bool ret = false;
+
+ rcu_read_lock();
+ memcg = mem_cgroup_from_task(task);
+ if (memcg)
+ ret = mem_cgroup_is_descendant(memcg, origin);
+ rcu_read_unlock();
+ return ret;
+}
+
static inline bool mm_match_cgroup(struct mm_struct *mm,
const struct mem_cgroup *memcg)
{
@@ -1513,6 +1527,12 @@ static inline u64 cgroup_id_from_mm(struct mm_struct *mm)
static inline void mem_cgroup_flush_workqueue(void) { }
+static inline bool mem_cgroup_task_in_hierarchy(struct task_struct *task,
+ const struct mem_cgroup *origin)
+{
+ return true;
+}
+
static inline int mem_cgroup_init(void) { return 0; }
#endif /* CONFIG_MEMCG */
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index fa9088c965b6..85da2a39e18a 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -1258,3 +1258,105 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
return -ENOSYS;
#endif /* CONFIG_MMU */
}
+
+#if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_MEMCG)
+
+__bpf_kfunc_start_defs();
+/**
+ * bpf_oom_kill_process() - Terminate a process via the kernel OOM handler
+ * @oc: pointer to oom_control structure describing the OOM context
+ * @task: task to be terminated
+ * @message__str: reason string logged in the OOM report
+ *
+ * Terminate @task using the kernel OOM machinery: emit the OOM report,
+ * update memcg OOM events, trigger asynchronous OOM memory reclamation,
+ * and honor memory.oom.group recursive behavior.
+ *
+ * Kernel-enforced safety constraints:
+ * - Tasks exempt via oom_unkillable_task() (init and kernel threads) are
+ * rejected with -EPERM.
+ * - Tasks with oom_score_adj == OOM_SCORE_ADJ_MIN (-1000), or tasks
+ * outside @oc->memcg's subtree during a memcg-scoped OOM, are rejected
+ * with -EINVAL.
+ * - Tasks with no live mm or with MMF_OOM_SKIP set return -ESRCH.
+ *
+ * Return: 0 on success, or a negative errno on failure.
+ */
+__bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
+ struct task_struct *task,
+ const char *message__str)
+{
+ struct task_struct *p;
+
+ if (oom_unkillable_task(task))
+ return -EPERM;
+
+ if (oc->memcg && !mem_cgroup_task_in_hierarchy(task, oc->memcg))
+ return -EINVAL;
+
+ p = find_lock_task_mm(task);
+ if (!p)
+ return -ESRCH;
+
+ if (p->signal->oom_score_adj == OOM_SCORE_ADJ_MIN) {
+ task_unlock(p);
+ return -EINVAL;
+ }
+
+ if (mm_flags_test(MMF_OOM_SKIP, p->mm)) {
+ task_unlock(p);
+ return -ESRCH;
+ }
+
+ /* paired with put_task_struct() in oom_kill_process() */
+ get_task_struct(p);
+ task_unlock(p);
+
+ oc->chosen = p;
+ oom_kill_process(oc, message__str);
+ oc->chosen = NULL;
+
+ return 0;
+}
+
+__bpf_kfunc_end_defs();
+
+BTF_KFUNCS_START(bpf_oom_kfuncs)
+BTF_ID_FLAGS(func, bpf_oom_kill_process, KF_SLEEPABLE)
+BTF_KFUNCS_END(bpf_oom_kfuncs)
+
+BTF_ID_LIST_SINGLE(bpf_oom_ops_ids, struct, bpf_oom_ops)
+
+static int bpf_oom_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
+{
+ if (!btf_id_set8_contains(&bpf_oom_kfuncs, kfunc_id))
+ return 0;
+
+ if (prog->type == BPF_PROG_TYPE_STRUCT_OPS &&
+ bpf_oom_ops_ids[0] &&
+ prog->aux->attach_btf_id == bpf_oom_ops_ids[0])
+ return 0;
+
+ return -EACCES;
+}
+
+static const struct btf_kfunc_id_set bpf_oom_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &bpf_oom_kfuncs,
+ .filter = bpf_oom_kfunc_filter,
+};
+
+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);
+
+ return err;
+}
+late_initcall(bpf_oom_init);
+
+#endif /* CONFIG_CGROUP_BPF && CONFIG_MEMCG */
--
2.56.1.911.g883d8bcb20-goog
next prev parent reply other threads:[~2026-12-02 21:04 UTC|newest]
Thread overview: 31+ 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:59 ` bot+bpf-ci
2026-12-02 21:03 ` Ben Gardon [this message]
2026-12-02 21:58 ` [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc bot+bpf-ci
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:59 ` bot+bpf-ci
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:58 ` bot+bpf-ci
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:58 ` bot+bpf-ci
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:58 ` bot+bpf-ci
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:58 ` bot+bpf-ci
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:58 ` bot+bpf-ci
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-4-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=muchun.song@linux.dev \
--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