freenode
Kernel & Low-Level

BPF programs proposed as a replacement for the kernel OOM picker

A v4 series would let cgroup-attached BPF choose victims, raise memory limits, or kill early on PSI pressure, with the kernel handler left as fallback.

Google's Ben Gardon has posted a fourth revision of work, taken over from Roman Gushchin, that would let BPF programs decide how Linux handles out-of-memory conditions instead of relying on the kernel's fixed victim heuristic.

The kernel's OOM killer still picks a task by a generic badness score when a memory cgroup, or the system, cannot satisfy an allocation. Operators who want something smarter have had to live with that score or run a privileged userspace daemon. Facebook's oomd and systemd-oomd already watch pressure-stall information and kill processes before the kernel is invoked, but those daemons must themselves stay alive under severe memory pressure, and their kills do not go through the kernel's OOM accounting.

The proposal adds a cgroup struct_ops hook that runs before the in-kernel selector. A policy attached to a cgroup can pick which task to terminate, or raise that cgroup's hard memory limit so the allocation succeeds without a kill. Handlers walk from the cgroup that ran out of memory toward the root, unless an ancestor asks for the opposite order. If no BPF policy handles the event, the existing kernel selector remains the fallback. Gardon argues that earlier attempts to bake one memory-cgroup-aware policy into the kernel failed because the right victim depends on the workload and on how the cgroup tree is laid out, so the policy should live outside the kernel.

A second path is meant to replace the userspace daemons. A bare PSI tracepoint fires as pressure averages are updated, and BPF attached there can call into the OOM path asynchronously, avoiding the OOM lock. A new helper can terminate a chosen task through the normal kill path, so memory.oom.group expansion, oom_kill counters, and kernel logging still apply. Tasks marked unkillable, and kills aimed outside the named cgroup, are rejected. Michal Hocko suggested the limit-raising helper: only a strictly higher maximum is accepted, so the update cannot itself trigger reclaim or a nested OOM while the OOM lock is held.

The series includes a sample policy that mirrors systemd-oomd's rules: omit some cgroups, demote others, and rank the rest by recent page-scan activity. It is aimed at bpf-next and is not yet merged.