[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <dc66b416-6d35-4e7f-af9c-6a0831dc1fb7@linux.dev>
Date: Wed, 29 Oct 2025 17:43:02 -0700
From: Martin KaFai Lau <martin.lau@...ux.dev>
To: Roman Gushchin <roman.gushchin@...ux.dev>
Cc: linux-kernel@...r.kernel.org, Alexei Starovoitov <ast@...nel.org>,
Suren Baghdasaryan <surenb@...gle.com>, Michal Hocko <mhocko@...nel.org>,
Shakeel Butt <shakeel.butt@...ux.dev>, Johannes Weiner <hannes@...xchg.org>,
Andrii Nakryiko <andrii@...nel.org>, JP Kobryn <inwardvessel@...il.com>,
linux-mm@...ck.org, cgroups@...r.kernel.org, bpf@...r.kernel.org,
Martin KaFai Lau <martin.lau@...nel.org>, Song Liu <song@...nel.org>,
Kumar Kartikeya Dwivedi <memxor@...il.com>, Tejun Heo <tj@...nel.org>,
Andrew Morton <akpm@...ux-foundation.org>
Subject: Re: [PATCH v2 02/23] bpf: initial support for attaching struct ops to
cgroups
On 10/27/25 4:17 PM, Roman Gushchin wrote:
> When a struct ops is being attached and a bpf link is created,
> allow to pass a cgroup fd using bpf attr, so that struct ops
> can be attached to a cgroup instead of globally.
>
> Attached struct ops doesn't hold a reference to the cgroup,
> only preserves cgroup id.
>
> Signed-off-by: Roman Gushchin <roman.gushchin@...ux.dev>
> ---
> include/linux/bpf.h | 1 +
> kernel/bpf/bpf_struct_ops.c | 13 +++++++++++++
> 2 files changed, 14 insertions(+)
>
> diff --git a/include/linux/bpf.h b/include/linux/bpf.h
> index eae907218188..7205b813e25f 100644
> --- a/include/linux/bpf.h
> +++ b/include/linux/bpf.h
> @@ -1849,6 +1849,7 @@ struct bpf_struct_ops_link {
> struct bpf_link link;
> struct bpf_map __rcu *map;
> wait_queue_head_t wait_hup;
> + u64 cgroup_id;
> };
>
> struct bpf_link_primer {
> diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
> index 45cc5ee19dc2..58664779a2b6 100644
> --- a/kernel/bpf/bpf_struct_ops.c
> +++ b/kernel/bpf/bpf_struct_ops.c
> @@ -13,6 +13,7 @@
> #include <linux/btf_ids.h>
> #include <linux/rcupdate_wait.h>
> #include <linux/poll.h>
> +#include <linux/cgroup.h>
>
> struct bpf_struct_ops_value {
> struct bpf_struct_ops_common_value common;
> @@ -1359,6 +1360,18 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
> }
> bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL,
> attr->link_create.attach_type);
> +#ifdef CONFIG_CGROUPS
> + if (attr->link_create.cgroup.relative_fd) {
> + struct cgroup *cgrp;
> +
> + cgrp = cgroup_get_from_fd(attr->link_create.cgroup.relative_fd);
> + if (IS_ERR(cgrp))
> + return PTR_ERR(cgrp);
> +
> + link->cgroup_id = cgroup_id(cgrp);
Not sure storing the cgroup_id or storing the memcg/cgroup pointer is
better here. Regardless, link->cgroup_id should be cleared in
bpf_struct_ops_map_link_detach(). The cgroup_id probably is useful to
bpf_struct_ops_map_link_show_fdinfo().
> + cgroup_put(cgrp);
> + }
> +#endif /* CONFIG_CGROUPS */
>
> err = bpf_link_prime(&link->link, &link_primer);
> if (err)
Powered by blists - more mailing lists