From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245489; b=V7zkVVcDclLAWeqddB3yFXKtUJiLBvM+4ZMTon9u6XH5HidpY6c29zGfP7LbNQVndNd8 5Gc/0yKPwe1NeaPY4c3dmmfWxmuk0r+fQH3rD4T1pExdm+DKShbW7pjGjHpo46T3eYcmF 83fj3r3j4CtLKiY3UYYdrzeYVx/AJ9pOB6MAQTqj29RFtjktaBRCn5g570/BGrOM69N3I aiglsSTTItKeYgFUkveKwkZKv6SghZ3f2jkxHdxsxRT6TeLPOGx8QoftTW7BmfSAJ2Ayn bphMJAOBjPOdXFX60VM77UCpgiS429VrpXnQ7j499HvQRHjcIJKXNCIv6TfpX/iL/Jw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245489; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=jTSmC+lgtHVu3JzQxjGmuRNrDl2JnPKL34vCgdEYYzQ=; b=gNEXdgCM9L9Ce0PgwE8h64S0Sa5J73AyHofv0xWef/R9dejPMJHgM49NNcne/MT/4i2Q 2L9+PfHq8Bsi8f+ZXz/woB7LBgSgPrCXwTRt6sUoAwNTSm81W9ZVE4SNZayCkNwLkr/CP pDSW8IacRD96Q/bnJGhGlDLQRaBw3uoANKmTOlv4YY9tN1cNecHtIBsp3TuRERmqS+x/k a8kLXrWL008F5NwhCF8sEfsTKA7VnEwVn7KfrovajjAu62pIVQGyrW6t4vfBgmeUT8zsm ZPk4/EBGu5EtBNVn1U9rI1cAh0Tv5wS/6lYTZokma7IQPKZsCbNgnEEpOQrcFzFtLDA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--bgardon.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pgeWWbEm; arc=none smtp.remote-ip=209.85.167.197 Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A8E0760A6F62 for ; Wed, 2 Dec 2026 21:04:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A8E0760A6F62 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgardon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pgeWWbEm Received: by mail-oi1-f197.google.com with SMTP id 5614622812f47-4cbf700ddccso10103427b6e.0 for ; Wed, 02 Dec 2026 13:04:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796245488; x=1796850288; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jTSmC+lgtHVu3JzQxjGmuRNrDl2JnPKL34vCgdEYYzQ=; b=pgeWWbEmF6wFveYBz1o4PKIFG4nAaKh7OPh1qcP0N7t+RdYhzJ0syK3v9X9ZXZbqRG eOEAGagVnBZdeLTf2rkCN1OXkhhDJfHOsYj3AWWMmF1gZuIyN2Tv6i7ySk5KTlwMjBiQ 4kLxCOy7SFAL3pZMURyGLcHVm766rpsJ+Orwb5VBfFj97T0FCYW6p6G+j1iMU/PbAeY9 Rofm7vw7Y2+IbT/BfCwnkoXyUBh4bvnTxG/t7/20G5nlD+m02rhuydAG4jDSmzlzKV/p SPklCIZOA1I8htI09eybu8GdfxRYb+YdqU57fpa1Ht+u51ElLg78CngoznAVYvwPrcZe 1gTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245488; x=1796850288; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jTSmC+lgtHVu3JzQxjGmuRNrDl2JnPKL34vCgdEYYzQ=; b=egwxtb3iiTqM7+XfiyrF96Ac8ejITNRaTertUtn41xbPWmoYBZ4EoSEm1JWvuPvEjf Xsg9u326Oxgl5yf0XAa1acMzoVUaup7YUunoJRRkXMC2WOSG2F4oWWMVHUtC52/RH/hQ ra/YGI0b4WaoTqNxGL0IlanvofAttyeQwu6Wtnl3x8G66dw/0bEjo1A/unGx1Wxe8QDH JXy2FXVpa8dZrUOspW4XgEW7ZvVfeo79GeoQhegd+Oes28CVWoFlqPx5YN8l2ZirVBsE Hu+ecvdz9Fut/0NiWoDaljbL9YqqhcxIw+IlIYQMac8Df+NLaEhUQ4b+knCTXzmWhqO0 FbpA== X-Forwarded-Encrypted: i=1; AL+hErHqq8YmiHjiMmPgooILjyjNaKVddPAALETbIWBV7uMkjus8Fp4gwQgqB9rEp4ZabhXNetFtLWMktFk=@vger.kernel.org X-Gm-Message-State: APuU4Z8ik+5+POdkhnPvkEMSSHOYGM7z27S9jtJ9/OIeOiarS9pd1ZCg 5NisNc5q6ciVCYYlKGe73DQflbcx/rhYeCDBud/ATP1BtPnfCi861v/Gy5sCpy4zV22BggNkEz7 GluJhFXkftQ== X-Received: from ioop20.prod.google.com ([2002:a05:6602:2594:b0:9e9:db36:fd4]) (user=bgardon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:c1d3:b0:571:de49:991 with SMTP id 5614622812f47-57a0b34ae64mr41126461b6e.43.1796245487504; Wed, 02 Dec 2026 13:04:47 -0800 (PST) Date: Wed, 2 Dec 2026 13:04:07 -0800 In-Reply-To: <20261202210415.517651-1-bgardon@google.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202210415.517651-1-bgardon@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202210415.517651-18-bgardon@google.com> Subject: [PATCH bpf-next v4 17/17] Documentation/bpf: Document BPF OOM handling and kfuncs From: Ben Gardon To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Andrew Morton , Johannes Weiner , Michal Hocko , Shakeel Butt , Roman Gushchin , Tejun Heo Cc: JP Kobryn , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Rientjes , Suren Baghdasaryan , Josh Don , Sweet Tea Dorminy , Chuyi Zhou , Yafang Shao , benbwgardon@gmail.com, bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , Jonathan Corbet , Shuah Khan , Randy Dunlap , linux-doc@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Document BPF OOM policy attachment, execution semantics, and kfuncs in Documentation/bpf/bpf_oom.rst, link it into Documentation/bpf/index.rst, and add it to the BPF OOM entry in MAINTAINERS. Signed-off-by: Ben Gardon --- Documentation/bpf/bpf_oom.rst | 138 ++++++++++++++++++++++++++++++++++ Documentation/bpf/index.rst | 1 + MAINTAINERS | 1 + 3 files changed, 140 insertions(+) create mode 100644 Documentation/bpf/bpf_oom.rst diff --git a/Documentation/bpf/bpf_oom.rst b/Documentation/bpf/bpf_oom.rst new file mode 100644 index 000000000000..5fc63269aec5 --- /dev/null +++ b/Documentation/bpf/bpf_oom.rst @@ -0,0 +1,138 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================ +BPF OOM Handling +================ + +Overview +======== + +Linux supports customizing out-of-memory (OOM) handling with BPF in two ways: + +1. **Reactive OOM policies** via ``struct bpf_oom_ops`` (cgroup-attached + ``struct_ops``) invoked by ``out_of_memory()`` before the kernel's + OOM selector. Policies can choose one or more tasks to terminate, raise + ``memory.max`` on the cgroup, or back off while an existing OOM + target exits. +2. **Proactive OOM invocation and termination** via BPF kfuncs callable + from ``BPF_PROG_TYPE_SYSCALL`` programs and select tracepoints (such as + ``tp_btf/psi_avgs_work_tp``) to trigger OOM handling or directly + terminate a chosen task without a separate userspace OOM daemon. + +BPF OOM handling requires ``CONFIG_CGROUP_BPF=y`` and ``CONFIG_MEMCG=y``. +PSI tracepoint integration additionally requires ``CONFIG_PSI=y`` and +``CONFIG_BPF_EVENTS=y``. + +Attachment and Execution Order +============================== + +A reactive policy defines a ``struct bpf_oom_ops`` map with +``SEC(".struct_ops.link")`` and attaches it to a cgroup v2 directory via +``bpf_map__attach_cgroup_opts()``. + +- Attaching to the root cgroup covers system-wide OOMs as well as + fallback from descendant memory cgroups. +- Effective handlers run from the OOMing memory cgroup upward toward the + root by default, unless an ancestor attaches with ``BPF_F_PREORDER``. +- Multiple links on the same cgroup are allowed; relative ordering + uses ``BPF_F_BEFORE`` / ``BPF_F_AFTER`` with ``BPF_F_LINK`` (and + ``BPF_F_ID`` when specifying ``relative_id``). Do not pass + ``BPF_F_ALLOW_MULTI`` (rejected with ``-EINVAL``). +- Handlers are invoked with ``oom_lock`` held and page faults disabled. +- Returning a positive value signals that the OOM event has been + handled and stops traversal. Returning ``<= 0`` delegates to the + next effective handler and ultimately to the in-kernel OOM handler. +- ``SysRq-f`` (``is_sysrq_oom(oc)``) always bypasses BPF handlers. + +Struct Ops Interface +==================== + +``struct bpf_oom_ops`` (``include/linux/bpf_oom.h``): + +.. code-block:: c + + struct bpf_oom_ops { + int (*handle_out_of_memory)(struct oom_control *oc, + struct cgroup *cgrp); + char name[BPF_OOM_NAME_MAX_LEN]; + }; + +- ``handle_out_of_memory(oc, cgrp)``: Sleepable callback + (``SEC("struct_ops.s/handle_out_of_memory")``). ``oc`` is a read-only + ``struct oom_control *`` whose ``oc->memcg`` field is verified as + ``TRUSTED_OR_NULL`` (``NULL`` for global OOMs, trusted pointer for + memcg OOMs). ``cgrp`` is the cgroup where this policy instance is + attached. ``handle_out_of_memory`` is required and validated at map + load time. +- ``name``: Policy name logged in OOM reports (validated via + ``bpf_obj_name_cpy()``; defaults to ``"bpf_defined_policy"`` if empty). + +BPF Kfuncs +========== + +Kfuncs for ``struct bpf_oom_ops`` +--------------------------------- + +``int bpf_oom_kill_process(struct oom_control *oc, struct task_struct *task, const char *message__str)`` + Sleepable kfunc (restricted via default-deny filter to ``bpf_oom_ops``) + that terminates ``task`` through ``oom_kill_process()``, preserving OOM + logging, memcg ``memory.events`` accounting, asynchronous OOM memory + reclamation, and ``memory.oom.group`` recursive behavior. + + Kernel-enforced constraints: + + - Returns ``-EPERM`` for tasks exempt via ``oom_unkillable_task()`` (such + as ``init`` and kernel threads). + - Returns ``-EINVAL`` if ``task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN`` + (``-1000``), or if ``oc->memcg`` is non-NULL and ``task`` is outside + ``oc->memcg``'s hierarchy. + - Returns ``-ESRCH`` if ``task`` has no live ``mm`` or already has + ``MMF_OOM_SKIP`` set. + +Kfuncs for Proactive OOM (``BPF_PROG_TYPE_SYSCALL`` & ``tp_btf/psi_avgs_work_tp``) +---------------------------------------------------------------------------------- + +Note: ``tp_btf/psi_avgs_work_tp`` runs in non-sleepable tracepoint +context; schedule a sleepable ``bpf_task_work`` or ``bpf_wq`` callback to +invoke sleepable OOM kfuncs from PSI handlers. + +``int bpf_out_of_memory(struct mem_cgroup *memcg__nullable, int order, u64 flags)`` + Acquires ``oom_lock`` (waiting interruptibly for fatal signals if + ``BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK`` is set, else returning ``-EBUSY`` on + contention), accounts ``MEMCG_OOM`` for non-NULL ``memcg``, and runs + ``out_of_memory()``, invoking any attached ``bpf_oom_ops`` policies and + falling back to ``select_bad_process()`` if unhandled. Root ``memcg`` is + normalized to ``NULL``; dying memcgs return ``-EINVAL``. BPF-declared + OOMs do not panic the kernel if no eligible task exists, skip + ``sysctl_oom_kill_allocating_task``, and omit ``dump_header()`` noise. + +``int bpf_oom_proactive_kill(struct mem_cgroup *origin__nullable, struct task_struct *task, const char *message__str, u64 flags)`` + Directly terminates ``task`` outside of an allocation failure without + falling back to ``select_bad_process()``. Synchronizes on ``oom_lock`` + according to ``enum bpf_oom_flags``, applies the same ``-EPERM`` / + ``-EINVAL`` / ``-ESRCH`` protections as ``bpf_oom_kill_process()``, + verifies ``task`` is within ``origin__nullable`` when non-NULL (which + also scopes ``memory.oom.group`` traversal and ``oom_memcg`` logging), + and omits ``dump_header()`` stack/meminfo noise while retaining the + single-line ``oom-kill:`` and ``Killed process`` records. + +Memory Cgroup Kfuncs +-------------------- + +Handlers commonly pair the above with the kfuncs in ``mm/bpf_memcontrol.c``: +``bpf_get_root_mem_cgroup()``, ``bpf_get_mem_cgroup()``, +``bpf_put_mem_cgroup()``, ``bpf_mem_cgroup_usage()``, +``bpf_mem_cgroup_page_state()``, ``bpf_mem_cgroup_vm_events()``, +``bpf_mem_cgroup_memory_events()``, ``bpf_mem_cgroup_flush_stats()``, and: + +``int bpf_mem_cgroup_raise_max(struct mem_cgroup *memcg, u64 max_bytes)`` + Sleepable kfunc that non-destructively raises ``memcg``'s ``memory.max`` + limit to ``max_bytes`` (truncated to ``PAGE_SIZE`` and clamped to + ``PAGE_COUNTER_MAX``, which represents unlimited/``"max"``; callers may + pass ``U64_MAX``). Only strictly higher limits on live, non-root + memory cgroups are accepted (returning ``-EINVAL`` otherwise) so no + reclaim is triggered while holding ``oom_lock``. On cgroup v1, enforces + ``memory.max <= memsw.max``. + +To check whether a task is already exiting due to OOM, read +``BPF_CORE_READ(task, signal, oom_mm) != NULL`` directly via CO-RE. diff --git a/Documentation/bpf/index.rst b/Documentation/bpf/index.rst index 638a00d42bc2..f226cfc41dc7 100644 --- a/Documentation/bpf/index.rst +++ b/Documentation/bpf/index.rst @@ -22,6 +22,7 @@ that goes into great technical depth about the BPF Architecture. kfuncs cpumasks fs_kfuncs + bpf_oom programs maps bpf_prog_run diff --git a/MAINTAINERS b/MAINTAINERS index 8bb61211b7f0..039d934d0a02 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5177,6 +5177,7 @@ M: Shakeel Butt L: bpf@vger.kernel.org L: linux-mm@kvack.org S: Maintained +F: Documentation/bpf/bpf_oom.rst F: include/linux/bpf_oom.h F: mm/bpf_memcontrol.c F: mm/bpf_oom.c -- 2.56.1.911.g883d8bcb20-goog