From nobody Mon Feb 9 00:54:37 2026 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) (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 46CB1149C7B for ; Mon, 28 Apr 2025 03:37:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811429; cv=none; b=uO1fuq4Hx07LYpHt/tr8FRDxdt4e1SV41MmSs1rx5Pkvv37RPxCw9sY3eyVAtugTfiEXfVq2gXwXil4TBCI4d6hIjTkV8qYHCer1zaOB0E+/rQh168mtniISPi/z1Y+DMJxrKyjmPxOZSR66jLfRN5T5PHwKwdbeHgann+Pxw8E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811429; c=relaxed/simple; bh=yMb5GqcrV1LWSU+1rW5hRyuHFkaaCjPtgv7KT1ltykU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=els1//BR0YZkKMHxqSM4rqt5EITccn3GFKBF/ma9qLLXmoaFQ1kclUhwY961HFqDpnj2xzU+9JBYGSRCg3xId1fQhlPWj76phVHqXPuN15n1WP4kZOzZgaLjoxO9olo/kZxlER8VeqOOnhghYBrYfEYXZo689jcmw56ONVh7SrA= 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=c/+BhYCM; arc=none smtp.client-ip=95.215.58.178 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="c/+BhYCM" 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=1745811423; 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=8WtdDCF/fOjk7l1oSxAg6r0dFrFtCENHh8DzkFeOJww=; b=c/+BhYCMrRSBr4yTd5mv7I5T0rvQOkVKbFl9XgTAeowasGX83nsPkgUVRynyVYaolN9nj8 Wc3beDU42dsEqeUnOao909cO5CNBY1mhRNOzYIyFs52IGCEiGXH2XiE+X/xtR9CO4B/eMp PeX97/KKF4tPd4+/I2wRyhQiGvbphn8= 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 10/12] mm: introduce bpf_out_of_memory() bpf kfunc Date: Mon, 28 Apr 2025 03:36:15 +0000 Message-ID: <20250428033617.3797686-11-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_out_of_memory() bpf kfunc, which allows to declare an out of memory events and trigger the corresponding kernel OOM handling mechanism. It takes a trusted memcg pointer (or NULL for system-wide OOMs) as an argument, as well as the page order. Only one OOM can be declared and handled in the system at once, so if the function is called in parallel to another OOM handling, it bails out with -EBUSY. The function is declared as sleepable. It guarantees that it won't be called from an atomic context. It's required by the OOM handling code, which is not guaranteed to work in a non-blocking context. Handling of a memcg OOM almost always requires taking of the css_set_lock spinlock. The fact that bpf_out_of_memory() is sleepable also guarantees that it can't be called with acquired css_set_lock, so the kernel can't deadlock on it. Signed-off-by: Roman Gushchin --- mm/oom_kill.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/mm/oom_kill.c b/mm/oom_kill.c index 2e922e75a9df..246510572e34 100644 --- a/mm/oom_kill.c +++ b/mm/oom_kill.c @@ -1333,6 +1333,27 @@ __bpf_kfunc int bpf_oom_kill_process(struct oom_cont= rol *oc, return 0; } =20 +__bpf_kfunc int bpf_out_of_memory(struct mem_cgroup *memcg__nullable, int = order) +{ + struct oom_control oc =3D { + .memcg =3D memcg__nullable, + .order =3D order, + }; + int ret =3D -EINVAL; + + if (oc.order < 0 || oc.order > MAX_PAGE_ORDER) + goto out; + + ret =3D -EBUSY; + if (mutex_trylock(&oom_lock)) { + ret =3D out_of_memory(&oc); + mutex_unlock(&oom_lock); + } + +out: + return ret; +} + __bpf_kfunc_end_defs(); =20 __bpf_hook_start(); @@ -1358,6 +1379,7 @@ static const struct btf_kfunc_id_set bpf_oom_hook_set= =3D { =20 BTF_KFUNCS_START(bpf_oom_kfuncs) BTF_ID_FLAGS(func, bpf_oom_kill_process, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_out_of_memory, KF_SLEEPABLE | KF_TRUSTED_ARGS) BTF_KFUNCS_END(bpf_oom_kfuncs) =20 static const struct btf_kfunc_id_set bpf_oom_kfunc_set =3D { --=20 2.49.0.901.g37484f566f-goog