From: Siddharth Nayyar <sidnayyar@google.com>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
John Fastabend <john.fastabend@gmail.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Quentin Monnet <qmo@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kselftest@vger.kernel.org,
"Giuliano Procida" <gprocida@google.com>,
"Neill Kapron" <nkapron@google.com>,
"Thiébaud Weksteen" <tweek@google.com>,
"Maxime Ripard" <mripard@kernel.org>,
"Siddharth Nayyar" <sidnayyar@google.com>
Subject: [PATCH RFC bpf-next 6/7] bpftool: Generate attach and detach for struct_ops in light skeletons
Date: Fri, 06 Nov 2026 20:19:53 +0000 [thread overview]
Message-ID: <20261106-lskel-struct-ops-v1-6-a4486ffc79cb@google.com> (raw)
In-Reply-To: <20261106-lskel-struct-ops-v1-0-a4486ffc79cb@google.com>
Update codegen_attach_detach() to iterate over BPF_MAP_TYPE_STRUCT_OPS
maps with BPF_F_LINK set. Generate dedicated per-map attach functions
calling skel_link_create(), chain them in skel__attach(), and close the
corresponding link FDs with skel_closenz() in skel__detach().
Also skip gen_st_ops_shadow() when generating light skeletons so
skel->struct_ops is not emitted between skel->maps and skel->progs,
preserving the ctx layout expected by bpf_gen__finish().
Finally, reject struct_ops maps without BPF_F_LINK in light skeletons.
Because the loader program runs BPF_MAP_UPDATE_ELEM during skel__load(),
non-link struct_ops maps would register prematurely before
skel__attach() and leak in the kernel if a later load step fails.
Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
tools/bpf/bpftool/gen.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 67 insertions(+), 4 deletions(-)
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index ee680f7c2116..98e92a34326b 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -569,6 +569,8 @@ static void codegen_asserts(struct bpf_object *obj, const char *obj_name)
static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
{
struct bpf_program *prog;
+ struct bpf_map *map;
+ char ident[256];
bpf_object__for_each_program(prog, obj) {
const char *tp_name;
@@ -608,6 +610,30 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
", bpf_program__name(prog));
}
+ bpf_object__for_each_map(map, obj) {
+ if (!get_map_ident(map, ident, sizeof(ident)))
+ continue;
+ if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
+ continue;
+ if (!(bpf_map__map_flags(map) & BPF_F_LINK))
+ continue;
+
+ codegen("\
+ \n\
+ \n\
+ static inline int \n\
+ %1$s__%2$s__attach(struct %1$s *skel) \n\
+ { \n\
+ int map_fd = skel->maps.%2$s.map_fd; \n\
+ int fd = skel_link_create(map_fd, 0, BPF_STRUCT_OPS);\n\
+ \n\
+ if (fd > 0) \n\
+ skel->links.%2$s_fd = fd; \n\
+ return fd; \n\
+ } \n\
+ ", obj_name, ident);
+ }
+
codegen("\
\n\
\n\
@@ -625,6 +651,20 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
", obj_name, bpf_program__name(prog));
}
+ bpf_object__for_each_map(map, obj) {
+ if (!get_map_ident(map, ident, sizeof(ident)))
+ continue;
+ if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
+ continue;
+ if (!(bpf_map__map_flags(map) & BPF_F_LINK))
+ continue;
+
+ codegen("\
+ \n\
+ ret = ret < 0 ? ret : %1$s__%2$s__attach(skel); \n\
+ ", obj_name, ident);
+ }
+
codegen("\
\n\
return ret < 0 ? ret : 0; \n\
@@ -642,6 +682,20 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
", bpf_program__name(prog));
}
+ bpf_object__for_each_map(map, obj) {
+ if (!get_map_ident(map, ident, sizeof(ident)))
+ continue;
+ if (bpf_map__type(map) != BPF_MAP_TYPE_STRUCT_OPS)
+ continue;
+ if (!(bpf_map__map_flags(map) & BPF_F_LINK))
+ continue;
+
+ codegen("\
+ \n\
+ skel_closenz(skel->links.%1$s_fd); \n\
+ ", ident);
+ }
+
codegen("\
\n\
} \n\
@@ -1323,8 +1377,15 @@ static int do_skeleton(int argc, char **argv)
continue;
}
- if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS)
+ if (bpf_map__type(map) == BPF_MAP_TYPE_STRUCT_OPS) {
+ if (use_loader && !(bpf_map__map_flags(map) & BPF_F_LINK)) {
+ p_err("struct_ops map '%s' requires BPF_F_LINK for light skeleton",
+ bpf_map__name(map));
+ err = -ENOTSUP;
+ goto out;
+ }
attach_map_cnt++;
+ }
map_cnt++;
}
@@ -1385,9 +1446,11 @@ static int do_skeleton(int argc, char **argv)
}
btf = bpf_object__btf(obj);
- err = gen_st_ops_shadow(obj_name, btf, obj);
- if (err)
- goto out;
+ if (!use_loader) {
+ err = gen_st_ops_shadow(obj_name, btf, obj);
+ if (err)
+ goto out;
+ }
if (prog_cnt) {
printf("\tstruct {\n");
--
2.56.0.455.g6c5a503b38-goog
next prev parent reply other threads:[~2026-11-06 20:20 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-06 20:19 [PATCH RFC bpf-next 0/7] bpf: Support struct_ops in light skeletons Siddharth Nayyar
2026-11-06 20:19 ` [PATCH RFC bpf-next 1/7] bpf: Add bpf_btf_get_struct_member_info() helper Siddharth Nayyar
2026-11-06 20:37 ` sashiko-bot
2026-11-06 20:56 ` bot+bpf-ci
2026-11-06 20:19 ` [PATCH RFC bpf-next 2/7] libbpf: Implement bpf_gen__init_kern_struct_ops() for light skeletons Siddharth Nayyar
2026-11-06 20:53 ` sashiko-bot
2026-11-06 20:56 ` bot+bpf-ci
2026-11-06 20:19 ` [PATCH RFC bpf-next 3/7] libbpf: Support BPF_MAP_TYPE_STRUCT_OPS in bpf_gen__map_create() Siddharth Nayyar
2026-11-06 20:33 ` sashiko-bot
2026-11-06 20:56 ` bot+bpf-ci
2026-11-06 20:19 ` [PATCH RFC bpf-next 4/7] libbpf: Support BPF_PROG_TYPE_STRUCT_OPS in bpf_gen__prog_load() Siddharth Nayyar
2026-11-06 20:56 ` bot+bpf-ci
2026-11-06 20:19 ` [PATCH RFC bpf-next 5/7] libbpf: Implement bpf_gen__struct_ops_update_elem() for light skeletons Siddharth Nayyar
2026-11-06 20:39 ` sashiko-bot
2026-11-06 20:56 ` bot+bpf-ci
2026-11-06 20:19 ` Siddharth Nayyar [this message]
2026-11-06 20:19 ` [PATCH RFC bpf-next 7/7] selftests/bpf: Add light skeleton struct_ops tests Siddharth Nayyar
2026-11-09 19:46 ` [PATCH RFC bpf-next 0/7] bpf: Support struct_ops in light skeletons Ihor Solodrai
2026-11-09 21:39 ` Alexei Starovoitov
2026-11-10 13:08 ` Anton Protopopov
2026-11-10 13:12 ` Anton Protopopov
2026-11-11 7:35 ` Sid Nayyar
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=20261106-lskel-struct-ops-v1-6-a4486ffc79cb@google.com \
--to=sidnayyar@google.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=gprocida@google.com \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mripard@kernel.org \
--cc=nkapron@google.com \
--cc=qmo@kernel.org \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tweek@google.com \
--cc=yonghong.song@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.