[PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc
From: Ben Gardon
Date: Wed Dec 02 2026 - 16:04:44 EST
From: Roman Gushchin <roman.gushchin@xxxxxxxxx>
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@xxxxxxxxx>
Co-developed-by: Ben Gardon <bgardon@xxxxxxxxxx>
Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
---
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@xxxxxxxxx/
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