From nobody Sun Feb 8 23:04:22 2026 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (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 B0B9D1D5165 for ; Mon, 28 Apr 2025 03:36:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811411; cv=none; b=nBWhaSAC/5ItVYNhmwNumo6HqQHPhIGjxsD6l2FuBAJGJ7bLFPtmKtq/6l8A7xPIVF58dgIrtYeNt0z0JbFLOwWCuZCyCLOeIBq8PxdLPWylN14OjC6h9sinBd+weadTp5yHKsW3yVPeAIbWRQOpnszmox1l1X0NRIHZsskxTBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745811411; c=relaxed/simple; bh=6OUYSTeu++AfIl4Gcj++rgBVmfJEJojMdQW2+FM4XIs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lmnce6bSNNMbe4WYtoLuuqqIYgpIPMVmpR9yyL1k9RFtlXYjiOf1Z4kThoXbudhbu0LRRTCTaimScc6/xoXtI+59OBqv7eh1wiFP+IWzpQKvUeahehQ3wosm3kaDy8GiHmkfnR5PApV5PhWiC4zem5e1sfI49W2GN4fE/QGOX6I= 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=BzNdfSQO; arc=none smtp.client-ip=95.215.58.170 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="BzNdfSQO" 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=1745811407; 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=6cq3axG+82N0/3Jqyh4bOxzur9jfJxES+/ChQNHJI1A=; b=BzNdfSQODHW9rGTR8urNbCRUaXaCdcBN8wO7z4a9fsNytWmNoBhUr5ItTnxyymIIp3zkDv DiJJv5G0JaBfEE25qmuiGofwvLam4KGIWiAB5naNyvr+J34xIjVbkqjvUZ3/DBkBJIY40/ hDpA2TgnZb/q4LtMRPvg7UFQfW0MHpI= 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 05/12] mm: introduce bpf kfuncs to deal with memcg pointers Date: Mon, 28 Apr 2025 03:36:10 +0000 Message-ID: <20250428033617.3797686-6-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" To effectively operate with memory cgroups in bpf there is a need to convert css pointers to memcg pointers. A simple container_of cast which is used in the kernel code can't be used in bpf because from the verifier's point of view that's a out-of-bounds memory access. Introduce helper get/put kfuncs which can be used to get a refcounted memcg pointer from the css pointer: - bpf_get_mem_cgroup, - bpf_put_mem_cgroup. bpf_get_mem_cgroup() can take both memcg's css and the corresponding cgroup's "self" css. It allows it to be used with the existing cgroup iterator which iterates over cgroup tree, not memcg tree. Signed-off-by: Roman Gushchin --- include/linux/memcontrol.h | 2 + mm/Makefile | 3 ++ mm/bpf_memcontrol.c | 101 +++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 mm/bpf_memcontrol.c diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index 53364526d877..a2ecd9caacfb 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -932,6 +932,8 @@ static inline void mod_memcg_page_state(struct page *pa= ge, rcu_read_unlock(); } =20 +unsigned long memcg_events(struct mem_cgroup *memcg, int event); +unsigned long mem_cgroup_usage(struct mem_cgroup *memcg, bool swap); unsigned long memcg_page_state(struct mem_cgroup *memcg, int idx); unsigned long lruvec_page_state(struct lruvec *lruvec, enum node_stat_item= idx); unsigned long lruvec_page_state_local(struct lruvec *lruvec, diff --git a/mm/Makefile b/mm/Makefile index e7f6bbf8ae5f..3eedba68e8cb 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -105,6 +105,9 @@ obj-$(CONFIG_MEMCG) +=3D memcontrol.o vmpressure.o ifdef CONFIG_SWAP obj-$(CONFIG_MEMCG) +=3D swap_cgroup.o endif +ifdef CONFIG_BPF_SYSCALL +obj-$(CONFIG_MEMCG) +=3D bpf_memcontrol.o +endif obj-$(CONFIG_CGROUP_HUGETLB) +=3D hugetlb_cgroup.o obj-$(CONFIG_GUP_TEST) +=3D gup_test.o obj-$(CONFIG_DMAPOOL_TEST) +=3D dmapool_test.o diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c new file mode 100644 index 000000000000..dacdf53735e5 --- /dev/null +++ b/mm/bpf_memcontrol.c @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Memory Controller-related BPF kfuncs and auxiliary code + * + * Author: Roman Gushchin + */ + +#include +#include + +__bpf_kfunc_start_defs(); + +__bpf_kfunc struct mem_cgroup * +bpf_get_mem_cgroup(struct cgroup_subsys_state *css) +{ + struct mem_cgroup *memcg =3D NULL; + bool rcu_unlock =3D false; + + if (!root_mem_cgroup) + return NULL; + + if (root_mem_cgroup->css.ss !=3D css->ss) { + struct cgroup *cgroup =3D css->cgroup; + int ssid =3D root_mem_cgroup->css.ss->id; + + rcu_read_lock(); + rcu_unlock =3D true; + css =3D rcu_dereference_raw(cgroup->subsys[ssid]); + } + + if (css && css_tryget(css)) + memcg =3D container_of(css, struct mem_cgroup, css); + + if (rcu_unlock) + rcu_read_unlock(); + + return memcg; +} + +__bpf_kfunc void bpf_put_mem_cgroup(struct mem_cgroup *memcg) +{ + css_put(&memcg->css); +} + +__bpf_kfunc unsigned long bpf_mem_cgroup_events(struct mem_cgroup *memcg, = int event) +{ + + if (event < 0 || event >=3D NR_VM_EVENT_ITEMS) + return (unsigned long)-1; + + return memcg_events(memcg, event); +} + +__bpf_kfunc unsigned long bpf_mem_cgroup_usage(struct mem_cgroup *memcg, b= ool swap) +{ + return mem_cgroup_usage(memcg, swap); +} + +__bpf_kfunc unsigned long bpf_mem_cgroup_page_state(struct mem_cgroup *mem= cg, int idx) +{ + if (idx < 0 || idx >=3D MEMCG_NR_STAT) + return (unsigned long)-1; + + return memcg_page_state(memcg, idx); +} + +__bpf_kfunc void bpf_mem_cgroup_flush_stats(struct mem_cgroup *memcg) +{ + mem_cgroup_flush_stats(memcg); +} + +__bpf_kfunc_end_defs(); + +BTF_KFUNCS_START(bpf_memcontrol_kfuncs) +BTF_ID_FLAGS(func, bpf_get_mem_cgroup, KF_ACQUIRE | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_put_mem_cgroup, KF_RELEASE) + +BTF_ID_FLAGS(func, bpf_mem_cgroup_events, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_mem_cgroup_usage, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_mem_cgroup_page_state, KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_mem_cgroup_flush_stats, KF_TRUSTED_ARGS) + +BTF_KFUNCS_END(bpf_memcontrol_kfuncs) + +static const struct btf_kfunc_id_set bpf_memcontrol_kfunc_set =3D { + .owner =3D THIS_MODULE, + .set =3D &bpf_memcontrol_kfuncs, +}; + +static int __init bpf_memcontrol_init(void) +{ + int err; + + err =3D register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, + &bpf_memcontrol_kfunc_set); + if (err) + pr_warn("error while registering bpf memcontrol kfuncs: %d", err); + + return err; +} +late_initcall(bpf_memcontrol_init); --=20 2.49.0.901.g37484f566f-goog