[PATCH bpf-next v4 08/17] mm, bpf: allow calling bpf_out_of_memory() from a PSI tracepoint
From: Ben Gardon
Date: Wed Dec 02 2026 - 16:05:18 EST
From: Roman Gushchin <roman.gushchin@xxxxxxxxx>
Register bpf_declare_oom_kfuncs on BPF_PROG_TYPE_TRACING with a
filter restricted to the btf_trace_psi_avgs_work_tp tracepoint.
psi_avgs_work() runs in workqueue context outside oom_lock, and because
bpf_out_of_memory() is KF_SLEEPABLE, tracepoint programs invoke it
asynchronously via a sleepable callback (such as bpf_task_work
or bpf_wq), avoiding any deadlock on oom_lock or avgs_lock.
Signed-off-by: Roman Gushchin <roman.gushchin@xxxxxxxxx>
Co-developed-by: Ben Gardon <bgardon@xxxxxxxxxx>
Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
---
v3 -> v4:
- Registered directly on BPF_PROG_TYPE_TRACING with a tracing-only
filter checking BPF_TRACE_RAW_TP and !prog->aux->dst_prog.
- Guarded with CONFIG_PSI && CONFIG_BPF_EVENTS and updated to
btf_trace_psi_avgs_work_tp.
v3: https://lore.kernel.org/all/20260127024421.494929-16-roman.gushchin@xxxxxxxxx/
mm/oom_kill.c | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/mm/oom_kill.c b/mm/oom_kill.c
index 9b5df00f98ab..21b88332a46a 100644
--- a/mm/oom_kill.c
+++ b/mm/oom_kill.c
@@ -1420,6 +1420,32 @@ static const struct btf_kfunc_id_set bpf_declare_oom_syscall_kfunc_set = {
.set = &bpf_declare_oom_kfuncs,
};
+#if defined(CONFIG_PSI) && defined(CONFIG_BPF_EVENTS)
+BTF_ID_LIST_SINGLE(bpf_oom_trace_ids, typedef, btf_trace_psi_avgs_work_tp)
+
+static int bpf_declare_oom_tracing_filter(const struct bpf_prog *prog,
+ u32 kfunc_id)
+{
+ if (!btf_id_set8_contains(&bpf_declare_oom_kfuncs, kfunc_id))
+ return 0;
+
+ if (prog->type == BPF_PROG_TYPE_TRACING &&
+ prog->expected_attach_type == BPF_TRACE_RAW_TP &&
+ !prog->aux->dst_prog &&
+ bpf_oom_trace_ids[0] &&
+ prog->aux->attach_btf_id == bpf_oom_trace_ids[0])
+ return 0;
+
+ return -EACCES;
+}
+
+static const struct btf_kfunc_id_set bpf_declare_oom_tracing_kfunc_set = {
+ .owner = THIS_MODULE,
+ .set = &bpf_declare_oom_kfuncs,
+ .filter = bpf_declare_oom_tracing_filter,
+};
+#endif /* CONFIG_PSI && CONFIG_BPF_EVENTS */
+
static int __init bpf_oom_init(void)
{
int err;
@@ -1438,6 +1464,15 @@ static int __init bpf_oom_init(void)
return err;
}
+#if defined(CONFIG_PSI) && defined(CONFIG_BPF_EVENTS)
+ err = register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
+ &bpf_declare_oom_tracing_kfunc_set);
+ if (err) {
+ pr_warn("error while registering tracing bpf oom kfuncs: %d\n", err);
+ return err;
+ }
+#endif
+
return 0;
}
late_initcall(bpf_oom_init);
--
2.56.1.911.g883d8bcb20-goog