From nobody Thu Dec 18 00:09:36 2025 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [95.215.58.180]) (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 75BC332E15B; Mon, 27 Oct 2025 23:18:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761607099; cv=none; b=MZJB00g2nbgBpfv7wBKRDWoq0dPpD5NVZ+1LIzvRWQcq594MhpZg6t9hV+xxKm2eogQmuHF1q9rtOZd3XFh6JsU9SN6glwsBi/k/KJhUvcZf680I8+p0sBO+KW2RNSvoXKyzVsPNiCYNH2ZxPthxcOlP865LoLHbYQ4CE3QJVHE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761607099; c=relaxed/simple; bh=WUZ3N8Rq7Yv5T49ndtG5QvsaP9IwhAhXF5ZyS2VCMR8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iAkwnUHMEETNPO72M9FQ36dqPeookc1lujmNI+MelmYbhuNxvn06hwSjdzyUiso6IOs+CBZ6dhaNWmC5JUWZL6scofdQbOAKs0lkVEQodTwpalnlg/hIgxart4iqb1Jfb5OR2fwlI3yoOHuQaGgnEM7yReROJs/I3AQr42HTiLA= 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=hvHbWkeM; arc=none smtp.client-ip=95.215.58.180 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="hvHbWkeM" 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=1761607096; 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=0L0/Ocn9RqSsZN0Y29o3qrSHIg/Knm5QfgmpjPK++Dc=; b=hvHbWkeMCi+mGvHKY6QdSAqkwB8+dvLP330BknCQWQjexYhiDSjpkQh68nnGVIqt/WmOXA ym1pLJPCyIrwF+Fey/vwtrn/TcqXV/szfQ9TBW3DZxGU4AfLfCUEDoA0GDLWkq1yqy/SAz GwrAo1ztd805084vXph8luCWScY37+U= From: Roman Gushchin To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Alexei Starovoitov , Suren Baghdasaryan , Michal Hocko , Shakeel Butt , Johannes Weiner , Andrii Nakryiko , JP Kobryn , linux-mm@kvack.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, Martin KaFai Lau , Song Liu , Kumar Kartikeya Dwivedi , Tejun Heo , Roman Gushchin Subject: [PATCH v2 09/23] mm: introduce bpf_get_root_mem_cgroup() BPF kfunc Date: Mon, 27 Oct 2025 16:17:12 -0700 Message-ID: <20251027231727.472628-10-roman.gushchin@linux.dev> In-Reply-To: <20251027231727.472628-1-roman.gushchin@linux.dev> References: <20251027231727.472628-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 a BPF kfunc to get a trusted pointer to the root memory cgroup. It's very handy to traverse the full memcg tree, e.g. for handling a system-wide OOM. It's possible to obtain this pointer by traversing the memcg tree up from any known memcg, but it's sub-optimal and makes BPF programs more complex and less efficient. bpf_get_root_mem_cgroup() has a KF_ACQUIRE | KF_RET_NULL semantics, however in reality it's not necessarily to bump the corresponding reference counter - root memory cgroup is immortal, reference counting is skipped, see css_get(). Once set, root_mem_cgroup is always a valid memcg pointer. It's safe to call bpf_put_mem_cgroup() for the pointer obtained with bpf_get_root_mem_cgroup(), it's effectively a no-op. Signed-off-by: Roman Gushchin --- mm/bpf_memcontrol.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c index 1e46097745cf..76c342318256 100644 --- a/mm/bpf_memcontrol.c +++ b/mm/bpf_memcontrol.c @@ -10,6 +10,20 @@ =20 __bpf_kfunc_start_defs(); =20 +/** + * bpf_get_root_mem_cgroup - Returns a pointer to the root memory cgroup + * + * The function has KF_ACQUIRE semantics, even though the root memory + * cgroup is never destroyed after being created and doesn't require + * reference counting. And it's perfectly safe to pass it to + * bpf_put_mem_cgroup() + */ +__bpf_kfunc struct mem_cgroup *bpf_get_root_mem_cgroup(void) +{ + /* css_get() is not needed */ + return root_mem_cgroup; +} + /** * bpf_get_mem_cgroup - Get a reference to a memory cgroup * @css: pointer to the css structure @@ -64,6 +78,7 @@ __bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *me= mcg) __bpf_kfunc_end_defs(); =20 BTF_KFUNCS_START(bpf_memcontrol_kfuncs) +BTF_ID_FLAGS(func, bpf_get_root_mem_cgroup, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_ACQUIRE | KF_RET_NULL | KF_RCU) BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE) =20 --=20 2.51.0