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 and zero the corresponding link FDs 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 --- tools/bpf/bpftool/gen.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 4 deletions(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index ee680f7c2116..da5f7c9d99ab 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,21 @@ 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\ + skel->links.%1$s_fd = 0; \n\ + ", ident); + } + codegen("\ \n\ } \n\ @@ -1323,8 +1378,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 +1447,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.1.839.g22a8290db9-goog