From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245489; b=VihBSUV37rsGyanu7zr/cUGAB0eeC7UbjVD1y/nKnfHmIWEUAcunBtgR0kCYx/8ZYGQc +22UvvOR532li87Z72/NVPByam66wKpO0x+0Es7X5qYvy1b1HK5r/nCcgmL2TJw2kv//E BcMvC2cOaaTzl/OU9Sou8mm2r2mhQRELp64AZ2kOBsZDYlXKSgyWSnWlolzom7pSKDWfE D3k//XpVSvbbvqIDGwkVMbA87rH+3sjqvA0oW/6o8X2whdglqUe409OQyDU8IDAh7sFFo rZSIubg0+JFbzFP6Gw+0Vn4gTOqTzLWcqSNLLJ7r0k37Mcp971jCPv3iGK1PGX741sg== 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=CRimD74VKnDomNgxUFsYgrSfu8An0hjT/RSJAK76cuR+nZMDlFasZ4mK7D0Fyhrct7iU OwLU7sFcAkB7jZqL+R+pTpuLnF2mbVx9CH4+CV9mzilRdCRmpuKuejp4bcWPsjoH4ebGd dtMqpXKA4bulqY6MLw61aVnAEYpyUH7reNFIzYHA1IQ9RvYjngOkNzFBF+YsAVy/fSi09 ikQRilBI48XT5BlEDj3Fd67y+9cw6YlnTal7GV2qMaL7A79oxO9YqUKm7ZFjTgjavLS4B tnscSQEuKdvEpfGZ4tb1tOU4XkS0a5P4vahs18sod7+qYuraHo7ad46gx7AtD1nT8wQ== 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.200 Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) (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 C837F606A680 for ; Wed, 2 Dec 2026 21:04:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C837F606A680 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-f200.google.com with SMTP id 5614622812f47-4e937c5bd4fso12213267b6e.2 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=N1D+UTb0iADfv62Ur1LTK1ECXTQobwoTl8Bt7ZKlO6TXJnJCMxOH8wvRnjuUjE0cdd i3JT3rgBg2yQBwJuA1F20ZNm11wxL1ZtT6tCINMzoG6SHG5LYzgwWLyouIdE5Rjp2gmM Xm4q545cpKaCiNz8XCF1ETsn1lLZhIEohT2tZBUq/fwq7rfylxnpAd6BGKTX7CVwC8hE Fh6/bLFBeLjNtSaV7/ZKp6d5Lp9mwzs+ICC8UdnFT63zV2Zrk1CBtLV7HsysNqgiw9RJ yyNOxsmVMsoDeG33hpGUgCdaK+8a50CmjlQDQoCy3VNc6GVbbnyWHyLNqacIwNhG/r4o xLtg== X-Forwarded-Encrypted: i=1; AL+hErEk6cJ64Sqc1s3QkHW0MmMQh/pLlIT80sKJGyXme4Bm4W5AJd3R9eo0epNRQYZ4c0qAIiNhN2Cd@vger.kernel.org X-Gm-Message-State: APuU4Z9X6xv8s1z6TvCFMZW+X0DjhtSPKS+ULD+rSLUAf+SZjmgb9GmB bUwsuOXdRFdqkgperThXhFXESinCtrZcK4uuIQesVLwIgBvPMloIzJ6+sU6biz2MUKwfpnMx2iD 1Sw/ACF5klw== 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: cgroups@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