Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
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>
Subject: [PATCH bpf-next v4 00/17] mm: BPF OOM
Date: Wed,  2 Dec 2026 13:03:50 -0800	[thread overview]
Message-ID: <20261202210415.517651-1-bgardon@google.com> (raw)

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                      | 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



             reply	other threads:[~2026-12-02 21:04 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 21:03 Ben Gardon [this message]
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:03 ` [PATCH bpf-next v4 03/17] mm, bpf: introduce bpf_oom_kill_process() BPF kfunc Ben Gardon
2026-12-02 21:03 ` [PATCH bpf-next v4 04/17] mm, bpf: introduce bpf_out_of_memory() " Ben Gardon
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: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:04 ` [PATCH bpf-next v4 12/17] mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention Ben Gardon
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:04 ` [PATCH bpf-next v4 15/17] mm, bpf: introduce bpf_mem_cgroup_raise_max() kfunc Ben Gardon
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

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-1-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=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