From nobody Sun Feb 8 20:33:09 2026 Received: from out-173.mta1.migadu.com (out-173.mta1.migadu.com [95.215.58.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9BB51C84D6 for ; Mon, 28 Apr 2025 03:36:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811408; cv=none; b=rN87sDL5O69Emo9WVxNLl0UCSA/i2ii1X/q6ZpOftX0vekGzAprdPe0pH7HHrjjVELMTMzkiYBZf6sl9F5SI7P77y3rqkyiMpV/P3mEWdaRyej8RL1kJhZh7opot2FzYVNhgs/umFk19TgxehAt5mZQxYw8rSusDdyQMKYtRRos= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811408; c=relaxed/simple; bh=nnI0SEo0cc59qBaxo7ivnHDqA/l1fQ+Yk4RuYsretf0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XdjlMyEfZtxM/nH8Ks2CtHgnKLd9TEW+8+djrZ3myQuYwgwHGipOR/p46dbO07diyBXBbIg8Q/W1REf2VXn4FtV75E7PkJBq8cEezDL4aRPZzuLfkdpATVVuBEeqQId3y1W0NRfNWy79eIOfgifhFU59bMxN+yzPXbN1nSp1jo8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=MV+ZBc7Y; arc=none smtp.client-ip=95.215.58.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="MV+ZBc7Y" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1745811404; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5UgujTTC+M44uRBA0W/V1VZG3lLjwg8UiEHcHCfP0z0=; b=MV+ZBc7Yde26EYnylyMbUZ4BNiFwo3uT9N1TpI8VmbA/Ah8V8Fhi2wG/cC+Q08x/MxWW0A woaT+KT1s0n4iTFKfJkQlQ+8pS7YpRR9DPLXhD/olJGa6ANeaafug3Do0+6zCTJcGxgaHv OSNyU6xp9Da+pSYHz2mbSZx1NDX37zc= From: Roman Gushchin To: linux-kernel@vger.kernel.org Cc: Andrew Morton , Alexei Starovoitov , Johannes Weiner , Michal Hocko , Shakeel Butt , Suren Baghdasaryan , David Rientjes , Josh Don , Chuyi Zhou , cgroups@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, Roman Gushchin Subject: [PATCH rfc 04/12] mm: introduce bpf_oom_kill_process() bpf kfunc Date: Mon, 28 Apr 2025 03:36:09 +0000 Message-ID: <20250428033617.3797686-5-roman.gushchin@linux.dev> In-Reply-To: <20250428033617.3797686-1-roman.gushchin@linux.dev> References: <20250428033617.3797686-1-roman.gushchin@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Introduce bpf_oom_kill_process() bpf kfunc, which is supposed to be used by bpf OOM programs. It allows to kill a process in exactly the same way the OOM killer does: using the OOM reaper, bumping corresponding memcg and global statistics, respecting memory.oom.group etc. On success, it sets om_control's bpf_memory_freed field to true, enabling the bpf program to bypass the kernel OOM killer. Signed-off-by: Roman Gushchin --- mm/oom_kill.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index d00776b63c0a..2e922e75a9df 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1299,6 +1299,42 @@ SYSCALL_DEFINE2(process_mrelease, int, pidfd, unsign= ed int, flags) =20 #ifdef CONFIG_BPF_SYSCALL =20 +__bpf_kfunc_start_defs(); +/* + * Kill a process in a way similar to the kernel OOM killer. + * This means dump the necessary information to dmesg, adjust memcg + * statistics, leverage the oom reaper, respect memory.oom.group etc. + * + * bpf_oom_kill_process() marks the forward progress by setting + * oc->bpf_memory_freed. If the progress was made, the bpf program + * is free to decide if the kernel oom killer should be invoked. + * Otherwise it's enforced, so that a bad bpf program can't + * deadlock the machine on memory. + */ +__bpf_kfunc int bpf_oom_kill_process(struct oom_control *oc, + struct task_struct *task, + const char *message__str) +{ + if (oom_unkillable_task(task)) + return -EPERM; + + /* paired with put_task_struct() in oom_kill_process() */ + task =3D tryget_task_struct(task); + if (!task) + return -EINVAL; + + oc->chosen =3D task; + + oom_kill_process(oc, message__str); + + oc->chosen =3D NULL; + oc->bpf_memory_freed =3D true; + + return 0; +} + +__bpf_kfunc_end_defs(); + __bpf_hook_start(); =20 /* @@ -1319,6 +1355,16 @@ static const struct btf_kfunc_id_set bpf_oom_hook_se= t =3D { .owner =3D THIS_MODULE, .set =3D &bpf_oom_hooks, }; + +BTF_KFUNCS_START(bpf_oom_kfuncs) +BTF_ID_FLAGS(func, bpf_oom_kill_process, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_KFUNCS_END(bpf_oom_kfuncs) + +static const struct btf_kfunc_id_set bpf_oom_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &bpf_oom_kfuncs, +}; + static int __init bpf_oom_init(void) { int err; @@ -1326,6 +1372,10 @@ static int __init bpf_oom_init(void) err =3D register_btf_fmodret_id_set(&bpf_oom_hook_set); if (err) pr_warn("error while registering bpf oom hooks: %d", err); + err =3D register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, + &bpf_oom_kfunc_set); + if (err) + pr_warn("error while registering bpf oom kfuncs: %d", err); =20 return err; } --=20 2.49.0.901.g37484f566f-goog