From nobody Mon Feb 9 10:57:47 2026 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.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 7FD7639E19D for ; Wed, 4 Feb 2026 09:01:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770195685; cv=none; b=mLjeLISxgEJnBuUxC1mrPJcTXHu59lGT5i0TE0KvXz8ymaHiKEfPnMh9yz2pSOZLLoxrrx/ZM7kkdz5s4WgUlZDrI01/krorePfQghWZcMsBZiR9Uy6lkHlZxhmtvcT22C+bgX8Op4DBXq9VfWG9mxELEBzE/ubdh7GD+n00HqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770195685; c=relaxed/simple; bh=19Xv4SJytKnNwfnv8gDBA/pqtpAPEfHpfhuLwQaKvY0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=isYoQ9Mbuk+kLis6ZvyQTyYqwMJeTZnJPMyqS+YVhQrES1iZqQmVynLE6pFisgE02r/xW5g7ctZwuZH1SbOrlu2AnXE47zaauikZQmtdNDlpsHYQEZ/HzmzyUnBAcxYsCLrreDiCpVFTXACP22yofmeaOPA66TrQPLF4D002KJo= 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=SIefoGYN; arc=none smtp.client-ip=91.218.175.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="SIefoGYN" 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=1770195683; 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=mlIGArAtV7gHW6WPqD211i7i7QqxUXQqAhh8012Ef70=; b=SIefoGYN4WQ/0eYryNFoK/eNuWiH5iFjUWVAZNw0eYu5sTB3US7HrXD2XPwm4R+tTXNiRV /Pbd7yB0LUsrNG+ml82Q4pxs1FiATbK6+5asphWlhRMGqfrB3cbiQO8rq6Fe7MDkzLH/JK wcitEnnIGLdZBuxNzZAAcNZKO4mLoRg= From: Hui Zhu To: Andrew Morton , Johannes Weiner , Michal Hocko , Roman Gushchin , Shakeel Butt , Muchun Song , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan , Peter Zijlstra , Miguel Ojeda , Nathan Chancellor , Kees Cook , Tejun Heo , Jeff Xu , mkoutny@suse.com, Jan Hendrik Farr , Christian Brauner , Randy Dunlap , Brian Gerst , Masahiro Yamada , davem@davemloft.net, Jakub Kicinski , Jesper Dangaard Brouer , JP Kobryn , Willem de Bruijn , Jason Xing , Paul Chaignon , Anton Protopopov , Amery Hung , Chen Ridong , Lance Yang , Jiayuan Chen , linux-kernel@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Hui Zhu , Geliang Tang Subject: [RFC PATCH bpf-next v6 10/12] mm/bpf: Add BPF_F_ALLOW_OVERRIDE support for memcg_bpf_ops Date: Wed, 4 Feb 2026 17:00:06 +0800 Message-ID: <274f34842dcc1c73c43f05cd1bcac6763107cbe6.1770194182.git.zhuhui@kylinos.cn> In-Reply-To: References: 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" From: Hui Zhu To allow for more flexible attachment policies in nested cgroup hierarchies, this patch introduces support for the `BPF_F_ALLOW_OVERRIDE` flag for `memcg_bpf_ops`. When a `memcg_bpf_ops` is attached to a cgroup with this flag, it permits child cgroups to attach their own, different `memcg_bpf_ops`, overriding the parent's inherited program. Without this flag, attaching a BPF program to a cgroup that already has one (either directly or via inheritance) will fail. The implementation involves: - Adding a `bpf_ops_flags` field to `struct mem_cgroup`. - During registration (`bpf_memcg_ops_reg`), checking for existing programs and the `BPF_F_ALLOW_OVERRIDE` flag. - During unregistration (`bpf_memcg_ops_unreg`), correctly restoring the parent's BPF program to the cgroup hierarchy. - Ensuring flags are inherited by child cgroups during online events. This change enables complex, multi-level policy enforcement where different subtrees of the cgroup hierarchy can have distinct memory management BPF programs. Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu --- include/linux/memcontrol.h | 1 + mm/bpf_memcontrol.c | 96 ++++++++++++++++++++++++++------------ 2 files changed, 66 insertions(+), 31 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index d91dbb95069b..c7b32a01a854 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -355,6 +355,7 @@ struct mem_cgroup { =20 #ifdef CONFIG_BPF_SYSCALL struct memcg_bpf_ops *bpf_ops; + u32 bpf_ops_flags; #endif =20 struct mem_cgroup_per_node *nodeinfo[]; diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c index 72b720400628..909751263f98 100644 --- a/mm/bpf_memcontrol.c +++ b/mm/bpf_memcontrol.c @@ -204,10 +204,11 @@ void memcontrol_bpf_online(struct mem_cgroup *memcg) =20 /* * Because only functions bpf_memcg_ops_reg and bpf_memcg_ops_unreg - * write to memcg->bpf_ops under the protection of cgroup_mutex, - * ensuring that cgroup_mutex is already locked here allows safe - * reading and writing of memcg->bpf_ops without needing to acquire - * a lock on memcg_bpf_srcu. + * write to memcg->bpf_ops and memcg->bpf_ops_flags under the + * protection of cgroup_mutex, ensuring that cgroup_mutex is already + * locked here allows safe reading and writing of memcg->bpf_ops and + * memcg->bpf_ops_flags without needing to acquire a lock on + * memcg_bpf_srcu. */ lockdep_assert_held(&cgroup_mutex); =20 @@ -218,6 +219,7 @@ void memcontrol_bpf_online(struct mem_cgroup *memcg) if (!ops) return; WRITE_ONCE(memcg->bpf_ops, ops); + memcg->bpf_ops_flags =3D parent_memcg->bpf_ops_flags; =20 /* * If the BPF program implements it, call the online handler to @@ -239,7 +241,7 @@ void memcontrol_bpf_offline(struct mem_cgroup *memcg) { struct memcg_bpf_ops *ops; =20 - /* Same with function memcontrol_bpf_online. */ + /* Same locking rules as memcontrol_bpf_online(). */ lockdep_assert_held(&cgroup_mutex); =20 ops =3D READ_ONCE(memcg->bpf_ops); @@ -335,48 +337,62 @@ static int bpf_memcg_ops_init_member(const struct btf= _type *t, return 0; } =20 -/** - * clean_memcg_bpf_ops - Clear BPF ops from a memory cgroup hierarchy - * @memcg: Root memory cgroup to start from - * @ops: The specific BPF ops to remove - * - * Walks the cgroup hierarchy and clears bpf_ops for any cgroup that - * matches @ops. - */ -static void clean_memcg_bpf_ops(struct mem_cgroup *memcg, - struct memcg_bpf_ops *ops) -{ - struct mem_cgroup *iter =3D NULL; - - while ((iter =3D mem_cgroup_iter(memcg, iter, NULL))) { - if (READ_ONCE(iter->bpf_ops) =3D=3D ops) - WRITE_ONCE(iter->bpf_ops, NULL); - } -} - static int bpf_memcg_ops_reg(void *kdata, struct bpf_link *link) { struct bpf_struct_ops_link *ops_link =3D container_of(link, struct bpf_struct_ops_link, link); - struct memcg_bpf_ops *ops =3D kdata; - struct mem_cgroup *memcg, *iter =3D NULL; + struct memcg_bpf_ops *ops =3D kdata, *old_ops; + struct mem_cgroup *memcg, *iter; int err =3D 0; =20 + if (ops_link->flags & ~BPF_F_ALLOW_OVERRIDE) { + pr_err("only BPF_F_ALLOW_OVERRIDE supported for struct_ops\n"); + return -EOPNOTSUPP; + } + memcg =3D mem_cgroup_get_from_ino(ops_link->cgroup_id); if (IS_ERR(memcg)) return PTR_ERR(memcg); =20 cgroup_lock(); + + /* + * Check if memcg has bpf_ops and whether it is inherited from + * parent. + * If inherited and BPF_F_ALLOW_OVERRIDE is set, allow override. + */ + old_ops =3D READ_ONCE(memcg->bpf_ops); + if (old_ops) { + struct mem_cgroup *parent_memcg =3D parent_mem_cgroup(memcg); + + if (!parent_memcg || + !(memcg->bpf_ops_flags & BPF_F_ALLOW_OVERRIDE) || + READ_ONCE(parent_memcg->bpf_ops) !=3D old_ops) { + err =3D -EBUSY; + goto unlock_out; + } + } + + /* Check for incompatible bpf_ops in descendants. */ + iter =3D NULL; while ((iter =3D mem_cgroup_iter(memcg, iter, NULL))) { - if (READ_ONCE(iter->bpf_ops)) { + struct memcg_bpf_ops *iter_ops =3D READ_ONCE(iter->bpf_ops); + + if (iter_ops && iter_ops !=3D old_ops) { + /* cannot override existing bpf_ops of sub-cgroup. */ mem_cgroup_iter_break(memcg, iter); err =3D -EBUSY; - break; + goto unlock_out; } + } + + iter =3D NULL; + while ((iter =3D mem_cgroup_iter(memcg, iter, NULL))) { WRITE_ONCE(iter->bpf_ops, ops); + iter->bpf_ops_flags =3D ops_link->flags; } - if (err) - clean_memcg_bpf_ops(memcg, ops); + +unlock_out: cgroup_unlock(); =20 mem_cgroup_put(memcg); @@ -390,13 +406,31 @@ static void bpf_memcg_ops_unreg(void *kdata, struct b= pf_link *link) =3D container_of(link, struct bpf_struct_ops_link, link); struct memcg_bpf_ops *ops =3D kdata; struct mem_cgroup *memcg; + struct mem_cgroup *iter; + struct memcg_bpf_ops *parent_bpf_ops =3D NULL; + u32 parent_bpf_ops_flags =3D 0; =20 memcg =3D mem_cgroup_get_from_ino(ops_link->cgroup_id); if (IS_ERR_OR_NULL(memcg)) goto out; =20 cgroup_lock(); - clean_memcg_bpf_ops(memcg, ops); + + /* Get the parent bpf_ops and bpf_ops_flags */ + iter =3D parent_mem_cgroup(memcg); + if (iter) { + parent_bpf_ops =3D READ_ONCE(iter->bpf_ops); + parent_bpf_ops_flags =3D iter->bpf_ops_flags; + } + + iter =3D NULL; + while ((iter =3D mem_cgroup_iter(memcg, iter, NULL))) { + if (READ_ONCE(iter->bpf_ops) =3D=3D ops) { + WRITE_ONCE(iter->bpf_ops, parent_bpf_ops); + iter->bpf_ops_flags =3D parent_bpf_ops_flags; + } + } + cgroup_unlock(); =20 mem_cgroup_put(memcg); --=20 2.43.0