From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 55D95606A32C for ; Wed, 2 Dec 2026 21:06:02 +0000 (UTC) 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=NRiGdjCT Received: by kanga.kvack.org (Postfix) id 825706B05C6; Wed, 2 Dec 2026 16:04:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7899A6B05C7; Wed, 2 Dec 2026 16:04:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B5506B05C8; Wed, 2 Dec 2026 16:04:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 091016B05C6 for ; Wed, 2 Dec 2026 16:04:50 -0500 (EST) Received: from smtpin09.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 883EB404AC for ; Wed, 2 Dec 2026 21:04:50 +0000 (UTC) X-FDA: 85500272340.09.12CB24A Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by imf13.hostedemail.com (Postfix) with ESMTP id CDC002000A for ; Wed, 2 Dec 2026 21:04:48 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=NRiGdjCT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 374cQawcKCKABGARDONGOOGLE.COMLINUX-MMKVACK.ORG@flex--bgardon.bounces.google.com designates 209.85.210.70 as permitted sender) smtp.mailfrom=374cQawcKCKABGARDONGOOGLE.COMLINUX-MMKVACK.ORG@flex--bgardon.bounces.google.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796245488; b=f401KCee/ZRqdf/hVRfnBGMYDWVZwitJJtd2sJhZOcIlwtQzFahMnhwCQ4TDivon1k1bWf VjRMW2oKFI7X4RbrC/vcB05NABdg0QG7tV/A4uQpzTlUK0QCFJYMSBM3zy07U8DGzEfWMa XyHnNp8nha3z/sgwTNN+Ov3w9D8QWQc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796245488; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=jTSmC+lgtHVu3JzQxjGmuRNrDl2JnPKL34vCgdEYYzQ=; b=ZDYU4BeURCfoDeaWpvWnfgB86erfFNdDKLui0If26Jz5D3IWxoSLxY8HOi5km2pGm+bj0Z /lqcrvqByu0Sus2FS1PDTMIroVRHOQ1QsD4D71MyhpzBDA9Kind97wCGex3NZnl7DnEbU5 D74+GGVJSnFpdgWYaEMsp1w3g5WtD4E= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=google.com header.s=20260707 header.b=NRiGdjCT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf13.hostedemail.com: domain of 374cQawcKCKABGARDONGOOGLE.COMLINUX-MMKVACK.ORG@flex--bgardon.bounces.google.com designates 209.85.210.70 as permitted sender) smtp.mailfrom=374cQawcKCKABGARDONGOOGLE.COMLINUX-MMKVACK.ORG@flex--bgardon.bounces.google.com Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-862ab20625eso5371317a34.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=kvack.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=NRiGdjCT95CtMCLGrSJKNgS40koBGzaG2y0H3ZOLhQWjg00B3cZPHtO+ARocAGrRhI OlvPnsV5VUIXmO7SsUs7zAFYv1fgzGmXALcKGI0Euc9S4OPBi9aPEmMR6zCCW1K9DM4+ NS/n4Ppx+n5IQ0pLdDviCVtE+vYyKF8h0OFPZSh3bSqY9AfsLjDK3gdkP8AMpiZ4vRP/ iklbIEDXj8hKIXFVcky8L3mCGSBbVq1h6HSoM5+5BfX3od1nWBZY+0u2ZPFCSSwDRxZE huVlU5tXNS1qB/PgM1uq+HgMZdFFPKdc2iCCLvC+r/YjGQFG2i0PcE7v28YSrMVKV6IN Z6MQ== 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=UfWo1knVp/dNHoqchOpCKxuyD6cNti7geUsBXiPqPcZNTUkiBNejaV8CE4os7U2ZAZ FoQ+xKA61rg7mRPZqnYvLB9/BdVC9kdOg6Dn1SQSEUk0T3b5GyEp4dybKfi50VyFr9nM lQr7rla+munXeLsT2PCKTahnJKOThPnwJ2gvIyDEAoip6V5Si2R9xZEy/hFNOnl0yQcL Er0/XIoqp2a/u4IeZRVeJLK8s0qzyA1PHmBofJVWjQY5J5RG8a/UWuMVE1H5qRx0GfBi RY2K+2TUfdQEt+pBgwFS9AdYynVVfGkBaOl7/JIg9BeR+8oJiMhwgyFeAGhtbu1UQGTL PcGQ== X-Forwarded-Encrypted: i=1; AL+hErH1iLQ0EdIzzWxd6y1L9kLY/sTPKXaoe2otoUieucetFkArpPEOL+uruyF2UzueQT0eV/NshRQgsA==@kvack.org X-Gm-Message-State: APuU4Z8f75R06jA79EIIGjsLVFURtDbS/dmK7MWAIAn3s5iU49Axbiwo zn/kT0dHf+vA2ilBACbIX2/4Vrfe+lKakAL4OUx5jfKB2vNxuoicq8zK/8Ak0CJXUNOWIqzqO2y FVImr1y6wXA== 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-Archived-By: 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" X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: CDC002000A X-Rspam-User: X-Stat-Signature: m76bki1c7bet85ft5rawjhmexsx46bfj X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796245488-825706 X-HE-Meta: U2FsdGVkX19PzlC4drd+BqlSna7oNlQ65CkBKHNspMv3+xGLVOtvqRHJbpkRBEGMh0va/gA5q2oF6UaY/6kfe172WZjqg849vtvqVMSSqwVjlFbSAaQouud+5suiWy2Xwp7bRzXpXH+CsTY9bImjL8QkixtaNfbZnN94CehSQB7m2Iwn4eUov8FWtENF/ZiMYMypLX/0OzoO3H3tatSdMdWSdvBry0pLEVHfn7Od4ZYpgqn0sTHwJN/4oc2UF00xrA/8CyS48mIurvCw2jNQpjH2WZO/9JPpbxs/Ccc26TicRzduoJ+3z1ugv1B6zySpP93ROgYydNYCGrEYt5c6CTqs64NGLlpJMzYCegTIAl2UvMXuDtIP11t3+N3D/6pH3pR4x8jq98XVeTDSNVj+W2yBpKPj70QHd2t1Np7caVnQKm/AcnjbXl6r2ts1d1+sXNCRMBJwqiD9sdRFimD5dLecNXZOnX3B/xkJQEGYyJtwfVHCLC+u1RDqKcjBCpfr/XkS1O7T1mjnwHw89C4hBAthGgMVa0roONK9RFIE/az51r2HsYsiefI1zxIxvod0p3GnLq/LB118GYtpdukaAD3Q5o6B4FxuhPHpze1vHbwfHJh6J/ut2ebmU/OsBaPGgL8JXgjOGMpNiUIOK/ZIOsGKI+vS7ctfCyIAqf7UWp/HSmeMWhWoxqh9zoGymqoAxcV+J5R0VOXMOHC2I+FAQsRREv6uS5LXQXzYh4jdHcj4CkfuyN2ABz1wBLr7+JgfvbLvT+mnmryIUeYuFNDp4L1m7SxVjPtS569AC5cYwz/6UpiHD4bk2FJSwYVQrwLsO1uo7xB1GzPh6yxg0ePt1gAIDcGjQ0+fl9Uj48BIgcz0uq56WOrOKYCVHDLzejflXfxmRT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kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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