From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Amery Hung" <ameryhung@gmail.com>, <bpf@vger.kernel.org>
Cc: <netdev@vger.kernel.org>, <alexei.starovoitov@gmail.com>,
<andrii@kernel.org>, <daniel@iogearbox.net>, <eddyz87@gmail.com>,
<memxor@gmail.com>, <martin.lau@kernel.org>,
<shakeel.butt@linux.dev>, <roman.gushchin@linux.dev>,
<kuniyu@google.com>, <kerneljasonxing@gmail.com>,
<kernel-team@meta.com>
Subject: Re: [PATCH bpf-next v3 13/15] libbpf: Support attaching struct_ops to a cgroup
Date: Tue, 14 Jul 2026 02:48:44 -0400 [thread overview]
Message-ID: <DJY3DJSWEMPW.3ODE44OQVL7J3@etsalapatis.com> (raw)
In-Reply-To: <20260706171918.317102-14-ameryhung@gmail.com>
On Mon Jul 6, 2026 at 1:19 PM EDT, Amery Hung wrote:
> From: Martin KaFai Lau <martin.lau@kernel.org>
>
> Add bpf_map__attach_cgroup_opts() to attach a struct_ops map to a cgroup
> through a BPF link.
>
> Also extend struct bpf_prog_query_opts with a type_id field so a
> BPF_STRUCT_OPS query on a cgroup can select the struct_ops type to
> enumerate.
>
> Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
> Signed-off-by: Amery Hung <ameryhung@gmail.com>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
> ---
> tools/lib/bpf/bpf.c | 2 ++
> tools/lib/bpf/bpf.h | 3 +-
> tools/lib/bpf/libbpf.c | 64 ++++++++++++++++++++++++++++++++++++++++
> tools/lib/bpf/libbpf.h | 3 ++
> tools/lib/bpf/libbpf.map | 1 +
> 5 files changed, 72 insertions(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
> index 96819c082c77..a9de7f107cf7 100644
> --- a/tools/lib/bpf/bpf.c
> +++ b/tools/lib/bpf/bpf.c
> @@ -934,6 +934,7 @@ int bpf_link_create(int prog_fd, int target_fd,
> case BPF_CGROUP_GETSOCKOPT:
> case BPF_CGROUP_SETSOCKOPT:
> case BPF_LSM_CGROUP:
> + case BPF_STRUCT_OPS:
> relative_fd = OPTS_GET(opts, cgroup.relative_fd, 0);
> relative_id = OPTS_GET(opts, cgroup.relative_id, 0);
> if (relative_fd && relative_id)
> @@ -1056,6 +1057,7 @@ int bpf_prog_query_opts(int target, enum bpf_attach_type type,
> attr.query.attach_type = type;
> attr.query.query_flags = OPTS_GET(opts, query_flags, 0);
> attr.query.count = OPTS_GET(opts, count, 0);
> + attr.query.type_id = OPTS_GET(opts, type_id, 0);
> attr.query.prog_ids = ptr_to_u64(OPTS_GET(opts, prog_ids, NULL));
> attr.query.link_ids = ptr_to_u64(OPTS_GET(opts, link_ids, NULL));
> attr.query.prog_attach_flags = ptr_to_u64(OPTS_GET(opts, prog_attach_flags, NULL));
> diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
> index 7534a593edae..490e8cb4ba53 100644
> --- a/tools/lib/bpf/bpf.h
> +++ b/tools/lib/bpf/bpf.h
> @@ -637,9 +637,10 @@ struct bpf_prog_query_opts {
> __u32 *link_ids;
> __u32 *link_attach_flags;
> __u64 revision;
> + __u32 type_id;
> size_t :0;
> };
> -#define bpf_prog_query_opts__last_field revision
> +#define bpf_prog_query_opts__last_field type_id
>
> /**
> * @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 7162146280a8..50a83b9217e4 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -14104,6 +14104,70 @@ struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map)
> return &link->link;
> }
>
> +struct bpf_link *bpf_map__attach_cgroup_opts(const struct bpf_map *map, int cgroup_fd,
> + const struct bpf_cgroup_opts *opts)
> +{
> + LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
> + struct bpf_link_struct_ops *link;
> + __u32 relative_id, zero = 0;
> + int err, fd, relative_fd;
> +
> + if (!OPTS_VALID(opts, bpf_cgroup_opts))
> + return libbpf_err_ptr(-EINVAL);
> +
> + if (!bpf_map__is_struct_ops(map)) {
> + pr_warn("map '%s': can't attach non-struct_ops map\n", map->name);
> + return libbpf_err_ptr(-EINVAL);
> + }
> +
> + if (map->fd < 0) {
> + pr_warn("map '%s': can't attach BPF map without FD (was it created?)\n", map->name);
> + return libbpf_err_ptr(-EINVAL);
> + }
> +
> + if (!(map->def.map_flags & BPF_F_LINK)) {
> + pr_warn("map '%s': can't attach to cgroup without BPF_F_LINK\n", map->name);
> + return libbpf_err_ptr(-EINVAL);
> + }
> +
> + relative_id = OPTS_GET(opts, relative_id, 0);
> + relative_fd = OPTS_GET(opts, relative_fd, 0);
> +
> + if (relative_fd && relative_id) {
> + pr_warn("map '%s': relative_fd and relative_id cannot be set at the same time\n",
> + map->name);
> + return libbpf_err_ptr(-EINVAL);
> + }
> +
> + link_create_opts.cgroup.expected_revision = OPTS_GET(opts, expected_revision, 0);
> + link_create_opts.cgroup.relative_fd = relative_fd;
> + link_create_opts.cgroup.relative_id = relative_id;
> + link_create_opts.flags = OPTS_GET(opts, flags, 0);
> +
> + link = calloc(1, sizeof(*link));
> + if (!link)
> + return libbpf_err_ptr(-ENOMEM);
> +
> + err = bpf_map_update_elem(map->fd, &zero, map->st_ops->kern_vdata, 0);
> + if (err && err != -EBUSY) {
> + free(link);
> + return libbpf_err_ptr(err);
> + }
> +
> + link->link.detach = bpf_link__detach_struct_ops;
> +
> + fd = bpf_link_create(map->fd, cgroup_fd, BPF_STRUCT_OPS, &link_create_opts);
> + if (fd < 0) {
> + free(link);
> + return libbpf_err_ptr(fd);
> + }
> +
> + link->link.fd = fd;
> + link->map_fd = map->fd;
> +
> + return &link->link;
> +}
> +
> /*
> * Swap the back struct_ops of a link with a new struct_ops map.
> */
> diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
> index b965ad571540..0e5f4e9bba41 100644
> --- a/tools/lib/bpf/libbpf.h
> +++ b/tools/lib/bpf/libbpf.h
> @@ -960,6 +960,9 @@ bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
> struct bpf_map;
>
> LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
> +LIBBPF_API struct bpf_link *bpf_map__attach_cgroup_opts(const struct bpf_map *map,
> + int cgroup_fd,
> + const struct bpf_cgroup_opts *opts);
> LIBBPF_API int bpf_link__update_map(struct bpf_link *link, const struct bpf_map *map);
>
> struct bpf_iter_attach_opts {
> diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
> index b731df19ae69..5131fdb979b7 100644
> --- a/tools/lib/bpf/libbpf.map
> +++ b/tools/lib/bpf/libbpf.map
> @@ -461,4 +461,5 @@ LIBBPF_1.8.0 {
> bpf_program__attach_tracing_multi;
> bpf_program__clone;
> btf__new_empty_opts;
> + bpf_map__attach_cgroup_opts;
> } LIBBPF_1.7.0;
next prev parent reply other threads:[~2026-07-14 6:48 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-06 17:19 [PATCH bpf-next v3 00/15] bpf: A common way to attach struct_ops to a cgroup Amery Hung
2026-07-06 17:19 ` [PATCH bpf-next v3 01/15] bpf: Remove __rcu tagging in st_link->map Amery Hung
2026-07-13 19:02 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 02/15] bpf: Make struct_ops tasks_rcu grace period optional Amery Hung
2026-07-13 19:01 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 03/15] bpf: Add bpf_struct_ops accessor helpers Amery Hung
2026-07-13 20:36 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 04/15] bpf: Remove unnecessary prog_list_prog() check Amery Hung
2026-07-13 20:35 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 05/15] bpf: Replace prog_list_prog() check with direct pl->prog and pl->link check Amery Hung
2026-07-13 21:27 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 06/15] bpf: Add prog_list_init_item(), prog_list_replace_item(), and prog_list_id() Amery Hung
2026-07-13 21:56 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 07/15] bpf: Move LSM trampoline unlink into bpf_cgroup_link_auto_detach() Amery Hung
2026-07-13 21:57 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 08/15] bpf: Add a few bpf_cgroup_array_* helper functions Amery Hung
2026-07-13 21:57 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 09/15] bpf: Add infrastructure to support attaching struct_ops to cgroups Amery Hung
2026-07-14 6:21 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 10/15] bpf: Allow all struct_ops to use bpf_dynptr_from_skb() Amery Hung
2026-07-14 6:23 ` Emil Tsalapatis
2026-07-06 17:19 ` [PATCH bpf-next v3 11/15] bpf: tcp: Support selected sock_ops callbacks as struct_ops Amery Hung
2026-07-06 18:28 ` bot+bpf-ci
2026-07-06 23:11 ` Amery Hung
2026-07-06 17:19 ` [PATCH bpf-next v3 12/15] bpf: tcp: Support parse/len/write header option hooks in bpf_tcp_ops Amery Hung
2026-07-06 17:19 ` [PATCH bpf-next v3 13/15] libbpf: Support attaching struct_ops to a cgroup Amery Hung
2026-07-14 6:48 ` Emil Tsalapatis [this message]
2026-07-06 17:19 ` [PATCH bpf-next v3 14/15] selftests/bpf: Test " Amery Hung
2026-07-06 17:19 ` [PATCH bpf-next v3 15/15] selftests/bpf: Add test for bpf_tcp_ops header option hooks Amery Hung
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DJY3DJSWEMPW.3ODE44OQVL7J3@etsalapatis.com \
--to=emil@etsalapatis.com \
--cc=alexei.starovoitov@gmail.com \
--cc=ameryhung@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kernel-team@meta.com \
--cc=kerneljasonxing@gmail.com \
--cc=kuniyu@google.com \
--cc=martin.lau@kernel.org \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=roman.gushchin@linux.dev \
--cc=shakeel.butt@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox