Re: [PATCH bpf-next v4 00/17] mm: BPF OOM
From: Ben Gardon
Date: Wed Dec 02 2026 - 16:38:33 EST
On Wed, Dec 2, 2026 at 1:04 PM Ben Gardon <bgardon@xxxxxxxxxx> wrote:
>
> Note: I am taking over this patch series from Roman Gushchin.
>
> This series adds support for BPF OOM handling and proactive OOM prevention.
>
> One might want this to:
> 1) Implement custom reactive OOM handling policies with smarter,
> application-specific victim selection.
> 2) Add greater NUMA awareness to task selection.
> 3) Create proactive OOM prevention policies, which terminate tasks before a
> kernel OOM is invoked, to preserve memory QoS.
>
> This creates a generic cgroup struct_ops interface, which is invoked before the
> kernel's OOM selector. A policy linked to the struct_ops can select which task
> to terminate, or expand a memory cgroup's hard limit to resolve the OOM
> non-destructively. Earlier proposals [1] have augmented the in-kernel task
> badness heuristic, but this series goes farther, leaving policy entirely up to
> the BPF program.
>
> Past attempts to standardize a single memory-cgroup-aware OOM policy [2] have
> shown that selection depends heavily on workloads and cgroup hierarchy layout.
> A BPF interface will provide flexibility while keeping the kernel OOM
> handler as a relatively simple and reliable fallback.
>
> To prevent OOMs in the first place, PSI-driven userspace daemons such as
> oomd [3] and systemd-oomd [4] terminate processes before thrashing or hard
> stalls, while leaving the kernel OOM handler as a last resort. Moving some of
> that logic into BPF avoids deploying and monitoring a separate privileged
> daemon that must itself survive severe memory pressure. It also unifies OOM
> accounting and logging, and gives policies direct access to kernel memcg and
> task state.
>
> Series Overview
> ---------------
>
> - Patch 1: Preparation: mark struct oom_control::memcg as
> TRUSTED_OR_NULL in the BPF verifier.
> - Patches 2-5: Reactive BPF OOM infrastructure: CGROUP_OOM_OPS
> struct_ops, bpf_oom_kill_process(), bpf_out_of_memory(), and
> cgroup hierarchy handling selftest.
> - Patches 6-10: PSI integration: psi_avgs_work_tp tracepoint,
> psi_group::cgroup_id, allowing bpf_out_of_memory() from
> tp_btf/psi_avgs_work_tp, and PSI selftest.
> - Patch 11: A systemd-oomd style BPF OOM policy example and selftest.
> - Patches 12-14: Allow proactive BPF policies to directly terminate chosen
> tasks without falling back to select_bad_process().
> This complements bpf_out_of_memory(), which runs the full
> out_of_memory() path and delegates selection to attached bpf_oom_ops
> handlers or the in-kernel selection algorithm.
> - Patches 15-16: Allow OOM handlers to resolve memcg OOMs by raising memory.max,
> as suggested by Michal Hocko.
> - Patch 17: Documentation/bpf/bpf_oom.rst
Apologies, a script mangled this and cut out multiple sections. Here's
the missing content. I will fix if/when I send a v5.
Base Tree, Testing & Tooling
----------------------------
This series is based on bpf-next/master (commit c846c98451b9,
v7.4-rc3-2627-gc846c98451b9).
Testing:
- Built each commit incrementally on bpf-next/master with CONFIG_WERROR=y,
CONFIG_MEMCG_V1=y, CONFIG_PROVE_LOCKING=y, and
CONFIG_DEBUG_ATOMIC_SLEEP=y, including BTF ID resolution
(--fatal_warnings).
- Ran BPF selftests in virtme-ng (--cpus 4, --memory 2G) with
test_progs -j across oom, oomd, oom_kfunc_neg (3 verifier rejection
subtests + runtime boundary checks), oom_proactive_kill,
oom_raise_max, and psi (3/3 consecutive passes on bpf-next/master, no
lockdep, atomic-sleep, or kernel warnings), as well as existing
cgroup* (23/23), task_work*, task_under_cgroup, and
verifier_kfunc_prog_types selftests.
- Verified compilation under non-default Kconfigs with CONFIG_WERROR=y:
CONFIG_MEMCG=n, CONFIG_BPF_EVENTS=n, CONFIG_MEMCG_V1=n, and
CONFIG_CGROUP_BPF=n.
Gemini was used to help rebase, address v3 review feedback, and for
pre-submission reviews. It was used in writing patches 11-17 (kfuncs,
selftests, and documentation), this cover letter, and the v4 changelog.
Everything has been reviewed and verified by hand.
Gemini also pulled from a public, but not shared v4 from Roman's GitHub:
https://github.com/torvalds/linux/compare/master...rgushchin:linux:bpfoom.4.rebased
Many of the responses to v3 feedback were already done in that branch.
[1]: https://lwn.net/ml/linux-kernel/20230810081319.65668-1-zhouchuyi@xxxxxxxxxxxxx/
[2]: https://lore.kernel.org/lkml/20171130152824.1591-1-guro@xxxxxx/
[3]: https://github.com/facebookincubator/oomd
[4]: https://www.freedesktop.org/software/systemd/man/latest/systemd-oomd.service.html
[5]: https://lore.kernel.org/all/20260917200542.3689605-1-ameryhung@xxxxxxxxx/
---
Changelog
---------
v4:
1) Taken over from Roman Gushchin by Ben Gardon.
2) Rebased on top of cgroup struct_ops attachment infrastructure.
(commit 369d9dcd8fb8, Martin KaFai Lau, Amery Hung [5]) Dropped custom
cgroup struct_ops link patches, adopted CGROUP_OOM_OPS.
3) Dropped mem_cgroup_get_from_ino() and read_cgroup_file() selftest
helper patches (both merged upstream via Shakeel Butt and Jiayuan
Chen), and split the standalone libbpf -ENOMEM bugfix out to target
the bpf tree separately.
4) Dropped bpf_task_is_oom_victim() kfunc in favor of reading
task->signal->oom_mm directly (Matt Bobrowski, Alexei
Starovoitov, Michal Hocko).
5) Dropped oc->bpf_memory_freed dual-reporting check so handlers can
return > 0 to back off while a previously selected task exits
(Michal Hocko); treat only positive return values as handled so
negative errnos delegate cleanly.
6) Bypassed bpf_handle_oom() on SysRq-triggered OOMs (is_sysrq_oom(oc))
so SysRq-f remains an unconditional failsafe (Michal Hocko).
7) Hardened bpf_oom_ops: added .validate requiring
handle_out_of_memory, validated ops->name with bpf_obj_name_cpy(),
switched bpf_oom_func_proto() from tracing_prog_func_proto() to
curated bpf_base_func_proto(), walked offline cgroups up to the
nearest online ancestor, added lockdep_assert_held(&oom_lock), and
encapsulated sleepable RCU Tasks Trace helpers in
include/linux/bpf-cgroup.h.
8) Hardened bpf_oom_kill_process(): switched to find_lock_task_mm()
with MMF_OOM_SKIP and OOM_SCORE_ADJ_MIN checks under task_lock(),
enforced oc->memcg hierarchy containment via
mem_cgroup_task_in_hierarchy(), and fixed
bpf_oom_kfunc_filter() to check set membership with default-deny
(Martin KaFai Lau).
9) Hardened bpf_out_of_memory(): registered directly on
BPF_PROG_TYPE_SYSCALL and BPF_PROG_TYPE_TRACING (restricted to
BPF_TRACE_RAW_TP on btf_trace_psi_avgs_work_tp under
CONFIG_PSI && CONFIG_BPF_EVENTS), moved BPF_OOM_FLAGS_ALL outside
enum bpf_oom_flags, added oc->bpf_declared to prevent 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.
10) Switched psi_avgs_work from TRACE_EVENT() to bare DECLARE_TRACE()
(psi_avgs_work_tp) to avoid exporting a pointer-only tracefs ABI,
added include/trace/events/psi.h to MAINTAINERS, and guarded
psi_group::cgroup_id with CONFIG_CGROUPS.
11) Generalized selftests for parallel/multi-CPU execution (serial_test_*,
pipe-synchronized pre-allocation without cgroup freezing, SIGKILL
and runtime PSI checks).
12) Added systemd-oomd style BPF OOM policy selftest (Alexei
Starovoitov, Roman Gushchin).
13) Added bpf_oom_proactive_kill() for proactive OOM prevention without
select_bad_process() fallback or dump_header() noise, with selftest
and negative/verifier kfunc tests.
14) Added bpf_mem_cgroup_raise_max() kfunc in mm/bpf_memcontrol.c for
non-destructive memcg limit expansion during OOM (Michal Hocko),
with cgroup v2 and v1 support and selftest.
15) Added Documentation/bpf/bpf_oom.rst (Michal Hocko, Matt Bobrowski,
Hao Ge).
v3: https://lore.kernel.org/all/20260127024421.494929-1-roman.gushchin@xxxxxxxxx/
v3:
1) Replaced bpf_psi struct ops with a tracepoint in psi_avgs_work() (Tejun H.)
2) Updated bpf_oom struct ops:
- removed bpf_oom_ctx, passing bpf_struct_ops_link instead (by Alexei S.)
- removed handle_cgroup_offline callback.
3) Updated kfuncs:
- bpf_out_of_memory() dropped constraint_text argument (by Michal H.)
- bpf_oom_kill_process() added check for OOM_SCORE_ADJ_MIN.
4) Libbpf: updated bpf_map__attach_struct_ops_opts to use target_fd.
(by Alexei S.)
v2: https://lore.kernel.org/all/20251027231727.472628-1-roman.gushchin@xxxxxxxxx/
v2:
1) A single bpf_oom can be attached system-wide and a single bpf_oom
per memcg.
(by Alexei Starovoitov)
2) Initial support for attaching struct ops to cgroups (Martin KaFai Lau,
Andrii Nakryiko and others)
3) bpf memcontrol kfuncs enhancements and tests (co-developed by JP Kobryn)
4) Many small-ish fixes and cleanups (suggested by Andrew Morton,
Suren Baghdasaryan,
Andrii Nakryiko and Kumar Kartikeya Dwivedi)
5) bpf_out_of_memory() is taking u64 flags instead of bool wait_on_oom_lock
(suggested by Kumar Kartikeya Dwivedi)
6) bpf_get_mem_cgroup() got KF_RCU flag (suggested by Kumar Kartikeya Dwivedi)
7) cgroup online and offline callbacks for bpf_psi, cgroup offline for bpf_oom
v1: https://lore.kernel.org/all/20250818170136.209169-1-roman.gushchin@xxxxxxxxx/
v1:
1) Both OOM and PSI parts are now implemented using bpf struct ops,
providing a path for future extensions (suggested by Kumar
Kartikeya Dwivedi,
Song Liu and Matt Bobrowski)
2) It's possible to create PSI triggers from BPF, no need for an additional
userspace agent. (suggested by Suren Baghdasaryan)
Also there is now a callback for the cgroup release event.
3) Added an ability to block on oom_lock instead of bailing out
(suggested by Michal Hocko)
4) Added bpf_task_is_oom_victim (suggested by Michal Hocko)
5) PSI callbacks are scheduled using a separate workqueue (suggested
by Suren Baghdasaryan)
RFC:
https://lwn.net/ml/all/20250428033617.3797686-1-roman.gushchin@xxxxxxxxx/
Ben Gardon (6):
mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention
selftests/bpf: Add bpf_oom_proactive_kill() proactive OOM test
selftests/bpf: Add negative and verifier tests for BPF OOM kfuncs
mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc
selftests/bpf: Add bpf_mem_cgroup_raise_max() OOM test
Documentation/bpf: Document BPF OOM handling and kfuncs
JP Kobryn (1):
selftests/bpf: Enable CONFIG_PSI
Roman Gushchin (10):
bpf: mark struct oom_control's memcg field as TRUSTED_OR_NULL
mm, bpf: introduce BPF OOM struct ops
mm, bpf: introduce bpf_oom_kill_process() BPF kfunc
mm, bpf: introduce bpf_out_of_memory() BPF kfunc
selftests/bpf: Add BPF OOM struct ops test
sched/psi: add a tracepoint to psi_avgs_work()
sched/psi: add cgroup_id field to psi_group structure
mm, bpf: allow calling bpf_out_of_memory() from a PSI tracepoint
selftests/bpf: Add PSI tracepoint OOM test
selftests/bpf: Add systemd-oomd style OOM policy test
Documentation/bpf/bpf_oom.rst | 138 ++++++++
> Documentation/bpf/index.rst | 1 +
> MAINTAINERS | 4 +
> include/linux/bpf-cgroup-defs.h | 1 +
> include/linux/bpf-cgroup.h | 16 +-
> include/linux/bpf.h | 1 +
> include/linux/bpf_oom.h | 44 +++
> include/linux/memcontrol.h | 27 ++
> include/linux/oom.h | 39 +++
> include/linux/psi_types.h | 6 +
> include/trace/events/psi.h | 19 ++
> kernel/bpf/cgroup.c | 1 +
> kernel/bpf/verifier.c | 5 +
> kernel/sched/psi.c | 7 +
> mm/Makefile | 3 +
> mm/bpf_memcontrol.c | 24 ++
> mm/bpf_oom.c | 187 +++++++++++
> mm/memcontrol-v1.c | 16 +
> mm/memcontrol-v1.h | 6 +
> mm/memcontrol.c | 38 +++
> mm/oom_kill.c | 313 ++++++++++++++++++-
> tools/testing/selftests/bpf/config | 1 +
> .../selftests/bpf/prog_tests/memory_holder.h | 158 ++++++++++
> tools/testing/selftests/bpf/prog_tests/test_oom.c | 218 +++++++++++++
> .../selftests/bpf/prog_tests/test_oom_kfunc_neg.c | 79 +++++
> .../bpf/prog_tests/test_oom_proactive_kill.c | 119 +++++++
> .../selftests/bpf/prog_tests/test_oom_raise_max.c | 135 ++++++++
> tools/testing/selftests/bpf/prog_tests/test_oomd.c | 274 ++++++++++++++++
> tools/testing/selftests/bpf/prog_tests/test_psi.c | 107 +++++++
> tools/testing/selftests/bpf/progs/test_oom.c | 132 ++++++++
> .../selftests/bpf/progs/test_oom_fail_ops_oom.c | 21 ++
> .../bpf/progs/test_oom_fail_ops_proactive.c | 23 ++
> .../selftests/bpf/progs/test_oom_fail_syscall.c | 15 +
> .../selftests/bpf/progs/test_oom_proactive_kill.c | 46 +++
> .../selftests/bpf/progs/test_oom_proactive_kill.h | 12 +
> .../selftests/bpf/progs/test_oom_raise_max.c | 55 ++++
> .../selftests/bpf/progs/test_oom_raise_max.h | 10 +
> tools/testing/selftests/bpf/progs/test_oomd.c | 346 +++++++++++++++++++++
> tools/testing/selftests/bpf/progs/test_oomd.h | 33 ++
> tools/testing/selftests/bpf/progs/test_psi.c | 88 ++++++
> 40 files changed, 2760 insertions(+), 8 deletions(-)
>
> --
> 2.56.1.911.g883d8bcb20-goog
>