[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250428033617.3797686-5-roman.gushchin@linux.dev>
Date: Mon, 28 Apr 2025 03:36:09 +0000
From: Roman Gushchin <roman.gushchin@...ux.dev>
To: linux-kernel@...r.kernel.org
Cc: Andrew Morton <akpm@...ux-foundation.org>,
Alexei Starovoitov <ast@...nel.org>,
Johannes Weiner <hannes@...xchg.org>,
Michal Hocko <mhocko@...nel.org>,
Shakeel Butt <shakeel.butt@...ux.dev>,
Suren Baghdasaryan <surenb@...gle.com>,
David Rientjes <rientjes@...gle.com>,
Josh Don <joshdon@...gle.com>,
Chuyi Zhou <zhouchuyi@...edance.com>,
cgroups@...r.kernel.org,
linux-mm@...ck.org,
bpf@...r.kernel.org,
Roman Gushchin <roman.gushchin@...ux.dev>
Subject: [PATCH rfc 04/12] mm: introduce bpf_oom_kill_process() bpf kfunc
Introduce bpf_oom_kill_process() bpf kfunc, which is supposed
to be used by bpf OOM programs. It allows to kill a process
in exactly the same way the OOM killer does: using the OOM reaper,
bumping corresponding memcg and global statistics, respecting
memory.oom.group etc.
On success, it sets om_control's bpf_memory_freed field to true,
enabling the bpf program to bypass the kernel OOM killer.
Signed-off-by: Roman Gushchin <roman.gushchin@...ux.dev>
---
mm/oom_kill.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index d00776b63c0a..2e922e75a9df 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -1299,6 +1299,42 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsigned int, flags)
#ifdef CONFIG_BPF_SYSCALL
+__bpf_kfunc_start_defs();
+/*
+ * Kill a process in a way similar to the kernel OOM killer.
+ * This means dump the necessary information to dmesg, adjust memcg
+ * statistics, leverage the oom reaper, respect memory.oom.group etc.
+ *
+ * bpf_oom_kill_process() marks the forward progress by setting
+ * oc->bpf_memory_freed. If the progress was made, the bpf program
+ * is free to decide if the kernel oom killer should be invoked.
+ * Otherwise it's enforced, so that a bad bpf program can't
+ * deadlock the machine on memory.
+ */
+__bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc,
+ struct task_struct *task,
+ const char *message__str)
+{
+ if (oom_unkillable_task(task))
+ return -EPERM;
+
+ /* paired with put_task_struct() in oom_kill_process() */
+ task = tryget_task_struct(task);
+ if (!task)
+ return -EINVAL;
+
+ oc->chosen = task;
+
+ oom_kill_process(oc, message__str);
+
+ oc->chosen = NULL;
+ oc->bpf_memory_freed = true;
+
+ return 0;
+}
+
+__bpf_kfunc_end_defs();
+
__bpf_hook_start();
/*
@@ -1319,6 +1355,16 @@ static const struct btf_kfunc_id_set bpf_oom_hook_set = {
.owner = THIS_MODULE,
.set = &bpf_oom_hooks,
};
+
+BTF_KFUNCS_START(bpf_oom_kfuncs)
+BTF_ID_FLAGS(func, bpf_oom_kill_process, KF_SLEEPABLE | KF_TRUSTED_ARGS)
+BTF_KFUNCS_END(bpf_oom_kfuncs)
+
+static const struct btf_kfunc_id_set bpf_oom_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &bpf_oom_kfuncs,
+};
+
static int __init bpf_oom_init(void)
{
int err;
@@ -1326,6 +1372,10 @@ static int __init bpf_oom_init(void)
err = register_btf_fmodret_id_set(&bpf_oom_hook_set);
if (err)
pr_warn("error while registering bpf oom hooks: %d", err);
+ err = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+ &bpf_oom_kfunc_set);
+ if (err)
+ pr_warn("error while registering bpf oom kfuncs: %d", err);
return err;
}
--
2.49.0.901.g37484f566f-goog
Powered by blists - more mailing lists