From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245480; b=P8onYuEBSPPNmTLVfV1DfEelZm0hXsJLLQ3XDo1qUMdxM7tkigI2//5iN8KSTZGKenN/ vwOeP9zCiapouPXQ2VvrQrPoGWVNpLtDHqll3Pb2SY7QIdI8acS9psfmwpiM5TKgq2Xnh 8o1A6eue3wKEP4NFStHLEzlqLL1LmfbnukkgqM4FSrF50Kq7OgWcv1xSJf6gko7ZwA+NL XS5MMS5P+w1j9/AIrXUyT8qK57OZvtYLeLnnsq/JRJtQVozVKW8RSbqybXDG2mZqzQ09v qXLwvySTKq/CeBjkk20gvOpFMtd1KiS6cnMpNdxxPvcAMLtLuAJ+JWZZcpx6mj4a8rQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245480; 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=AQyOYj3c7cbKC8yFSWKRHB3LMrRgDMShHxT53p5FC3s=; b=CjS9YdtpO85oxeM4f54Ff7BDpo4v3+t3yN4B0AuuTvT3zg9lUUb/K8YvTZOO6jjSRWGb Gxl3jrUymkDEXuNidn1K1V0Qo/YjKtFF7OzZz7Oi4eI03Lizx2azvLZHs+No5WqTWIbFu kFMQJgki1svy0mSgCbH4wDtPcaa6ccNjtF3mix0jwvevJ59j+MmckSoqv8eWDFfQ11GWt 0LyT13pF34NAnd6Y78JhSVWEtRVbgKCM5z4SEZXmkOxH0+GO2NA/K8rfwXkoFtDA8XDB0 aXKoDxdEHwmK+WZ1pTi8+80H+m9Mfp52FHfN/Vhe4ZVZvChzvKTd+/y3vRow3tW5g/A== 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=iDe8nYTz; arc=none smtp.remote-ip=209.85.167.198 Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) (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 4A31F60A260C for ; Wed, 2 Dec 2026 21:04:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4A31F60A260C 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=iDe8nYTz Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-574ae6cc614so353220b6e.1 for ; Wed, 02 Dec 2026 13:04:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796245479; x=1796850279; 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=AQyOYj3c7cbKC8yFSWKRHB3LMrRgDMShHxT53p5FC3s=; b=iDe8nYTzhmc5af5GBeJPUoCgho3FHS6B8W9aJCg7aBKwhGhAkIXDcSMywla6zFRucb +EsEyrQBw35noR+yUJFw7WErvKm9YV/RXE3X4ofZZ+G3UgPiKIkQM2GSTa3IOLKYRy84 YsY++7anpfT1+Qdbs6EHhez4d9JL3NR4bd+3OFhTGuzaDgGpLDWTeQ3LpKldUOXGm5Lu OS3hPyxUTcg/CDWUutZIwG/90oy7PcMNb7jTc91QlCLiRvGsDjf50VDzXt+mW/0Gpl94 wmxSAPvM1CApZNTqteznkW27tBR/ffyoX28V6Ih//TruLFdt0nj/WHNueUCj2XmTMJNX XXbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245479; x=1796850279; 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=AQyOYj3c7cbKC8yFSWKRHB3LMrRgDMShHxT53p5FC3s=; b=W+HnUryLYcj+weZFNBoR3GX+5OZPC7DE94GbcTLhrkp739QgKnMStLfqucTJ01ebYz +a8xPA2tWOcWtCjYPyGs4NTy7wWeArdbvdD8lpMXgju8FfBrWem36kVHCTr+1egzXS8K 430cz54t3JOWKUh28/7u638ZPSue5IIfY1zahxt8khaEX1cMyjfp2C9GXeYbEgcbB6tl frdFFNupLpho30rj5plI+L8dKyZGXMOAAtkbz5TfEt8qUP001alUihCBLfajyZ2ERZLU iUFdz12arIeacVl5wxqPoi/ZiTmQgwi3VmNmGYvCEo/kKiU7F3YOBBPEiSGMowyPUID9 3xlQ== X-Forwarded-Encrypted: i=1; AL+hErGdXBUELzyH6z1dl+X4UFZPzJ1Pg1LFch7ZNWJBsHX8FQzKntxnY+TvCCZWX9yx5BFYzUEDiMoV@vger.kernel.org X-Gm-Message-State: APuU4Z846Cm5/X9IvdmDNqgdYmHUhzRuBEDTO8ENg8DnfK1n/0Z2V5Wt eZy10XQfs98Oc+lMl5yLRjsiNOC6BFq4pG+I7mOOx+Sewi7E0uf8pUN/iWyc/Pd7Y8DtTaOH51u YV85vNQ16LA== X-Received: from oica8.prod.google.com ([2002:a05:6808:988:b0:57b:1d3a:4284]) (user=bgardon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:4f68:b0:4d6:9133:cff6 with SMTP id 5614622812f47-57a0bb5403dmr34918616b6e.29.1796245478883; Wed, 02 Dec 2026 13:04:38 -0800 (PST) Date: Wed, 2 Dec 2026 13:04:02 -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-13-bgardon@google.com> Subject: [PATCH bpf-next v4 12/17] mm, bpf: introduce bpf_oom_proactive_kill() for proactive OOM prevention 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 , Michal Hocko , Christian Brauner Content-Type: text/plain; charset="UTF-8" bpf_out_of_memory() invokes out_of_memory(), which falls back to select_bad_process() if no attached bpf_oom_ops policy handles the event. It cannot directly terminate a specific task chosen by a proactive userspace/BPF policy. Existing userspace options (pidfd_send_signal(SIGKILL) + process_mrelease() or cgroup.kill) cannot atomically honor memory.oom.group expansion from a chosen task, do not record memory.events oom_kill / oom_group_kill counters or unified kernel OOM logging, and cannot be invoked directly from a PSI tracepoint callback. Add bpf_oom_proactive_kill to bpf_declare_oom_kfuncs. The function synchronizes on oom_lock, validates that @task resides under a memcg (if specified), and terminates @task through bpf_oom_kill_process(). Thus asynchronous OOM memory reclamation, memcg OOM event counters, and memory.oom.group semantics apply as usual. Set oc->bpf_declared so dump_header() is skipped for proactive terminations since there is no failed allocation stack, meminfo snapshot, or task table to diagnose. dump_oom_victim() and "Killed process" still record the target, origin memcg, and reason string. Signed-off-by: Ben Gardon --- include/linux/oom.h | 3 ++ mm/oom_kill.c | 71 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/linux/oom.h b/include/linux/oom.h index 168b64dfb6ad..2725f17c7b40 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -64,6 +64,7 @@ struct oom_control { /* Declared via BPF outside an allocation failure */ bool bpf_declared; + #endif }; @@ -77,6 +78,7 @@ static inline bool is_bpf_declared_oom(const struct oom_control *oc) { return oc->bpf_declared; } + #else static inline const char *oom_handler_name(const struct oom_control *oc) { @@ -87,6 +89,7 @@ static inline bool is_bpf_declared_oom(const struct oom_control *oc) { return false; } + #endif extern struct mutex oom_lock; diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 21b88332a46a..42c986ec1520 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1384,6 +1384,76 @@ __bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable, return ret; } +/** + * bpf_oom_proactive_kill() - Terminate a task proactively outside of OOM + * @origin__nullable: memcg bounding OOM group and log attribution, or NULL + * @task: task to be terminated + * @message__str: reason string logged in dmesg + * @flags: bitmask of &enum bpf_oom_flags + * + * Terminate @task through the OOM machinery (asynchronous OOM memory + * reclamation, memcg OOM events, memory.oom.group recursive behavior, and + * single-line summary) without an allocation failure having occurred and + * without falling back to select_bad_process(). The full allocation-failure + * dump_header() report is suppressed because no page allocation failed. + * + * Synchronizes on oom_lock: waits (interruptible by fatal signals) when + * BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK is set, or returns -EBUSY immediately if + * oom_lock is contended. + * + * When @origin__nullable is non-NULL (and not the root memcg), @task must + * belong to @origin__nullable or one of its descendants, and + * mem_cgroup_get_oom_group() bounds any memory.oom.group expansion at + * @origin__nullable. + * + * Return: 0 on success, or a negative errno on failure. + */ +__bpf_kfunc int bpf_oom_proactive_kill(struct mem_cgroup *origin__nullable, + struct task_struct *task, + const char *message__str, + u64 flags) +{ + struct oom_control oc = { + .gfp_mask = GFP_USER, + .order = 0, + .bpf_declared = true, + }; + int ret; + + if (flags & ~BPF_OOM_FLAGS_ALL) + return -EINVAL; + + if (origin__nullable && mem_cgroup_is_root(origin__nullable)) + origin__nullable = NULL; + + if (origin__nullable && + !mem_cgroup_task_in_hierarchy(task, origin__nullable)) + return -EINVAL; + + oc.memcg = origin__nullable; + oc.constraint = origin__nullable ? CONSTRAINT_MEMCG : CONSTRAINT_NONE; + + lockdep_assert_not_held(&oom_lock); + if (flags & BPF_OOM_FLAGS_WAIT_ON_OOM_LOCK) { + ret = mutex_lock_killable(&oom_lock); + if (ret) + return ret; + } else if (!mutex_trylock(&oom_lock)) { + return -EBUSY; + } + + if (oom_killer_disabled) { + ret = -EBUSY; + goto out_unlock; + } + + ret = bpf_oom_kill_process(&oc, task, message__str); + +out_unlock: + mutex_unlock(&oom_lock); + return ret; +} + __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_oom_kfuncs) @@ -1413,6 +1483,7 @@ static const struct btf_kfunc_id_set bpf_oom_kfunc_set = { BTF_KFUNCS_START(bpf_declare_oom_kfuncs) BTF_ID_FLAGS(func, bpf_out_of_memory, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_oom_proactive_kill, KF_SLEEPABLE) BTF_KFUNCS_END(bpf_declare_oom_kfuncs) static const struct btf_kfunc_id_set bpf_declare_oom_syscall_kfunc_set = { -- 2.56.1.911.g883d8bcb20-goog