[PATCH bpf-next v3 4/5] bpftool: Add attach option for light skeleton generation

From: Mahe Tardy

Date: Mon Nov 02 2026 - 15:15:05 EST


Add an optional attach keyword to gen skeleton when the loader backend
is selected, and pass the request through gen_loader_opts.gen_attach.

Extend the loader context to receive link FDs, using zero for empty slots.
Generate attach functions that check the returned links, and detach
functions that close and reset the FDs. Document the option and add shell
completion.

Signed-off-by: Mahe Tardy <mahe.tardy@xxxxxxxxx>
---
.../bpf/bpftool/Documentation/bpftool-gen.rst | 11 ++-
tools/bpf/bpftool/bash-completion/bpftool | 2 +-
tools/bpf/bpftool/gen.c | 67 +++++++++++++------
3 files changed, 56 insertions(+), 24 deletions(-)

diff --git a/tools/bpf/bpftool/Documentation/bpftool-gen.rst b/tools/bpf/bpftool/Documentation/bpftool-gen.rst
index d0a36f442db7..4bf8b0b99ea2 100644
--- a/tools/bpf/bpftool/Documentation/bpftool-gen.rst
+++ b/tools/bpf/bpftool/Documentation/bpftool-gen.rst
@@ -24,7 +24,7 @@ GEN COMMANDS
=============

| **bpftool** **gen object** *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...]
-| **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*]
+| **bpftool** **gen skeleton** *FILE* [**name** *OBJECT_NAME*] [**attach**]
| **bpftool** **gen subskeleton** *FILE* [**name** *OBJECT_NAME*]
| **bpftool** **gen min_core_btf** *INPUT* *OUTPUT* *OBJECT* [*OBJECT*...]
| **bpftool** **gen help**
@@ -51,6 +51,15 @@ bpftool gen object *OUTPUT_FILE* *INPUT_FILE* [*INPUT_FILE*...]
bpftool gen skeleton *FILE*
Generate BPF skeleton C header file for a given *FILE*.

+ With **-L**, **attach** requests attachment during execution of the
+ generated loader. Supported programs are ordinary LSM programs and
+ BTF-based tracing programs using **fentry**, **fexit**, **fmod_ret**, or
+ **tp_btf** sections, with kernel targets resolved during loading.
+ The generated **__load()** function loads and attaches the programs;
+ **__attach()** checks that the links exist, and **__detach()** closes them.
+ Reattaching after detachment is not supported in this mode. Without
+ **attach**, loading and attachment remain separate operations.
+
BPF skeleton is an alternative interface to existing libbpf APIs for
working with BPF objects. Skeleton code is intended to significantly
shorten and simplify code to load and work with BPF programs from userspace
diff --git a/tools/bpf/bpftool/bash-completion/bpftool b/tools/bpf/bpftool/bash-completion/bpftool
index a28af31937ad..9247a9fdcaef 100644
--- a/tools/bpf/bpftool/bash-completion/bpftool
+++ b/tools/bpf/bpftool/bash-completion/bpftool
@@ -1021,7 +1021,7 @@ _bpftool()
return 0
;;
*)
- _bpftool_once_attr 'name'
+ _bpftool_once_attr 'name attach'
return 0
;;
esac
diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index ee680f7c2116..62e21bd773ff 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -566,7 +566,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)
+static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name,
+ bool gen_attach)
{
struct bpf_program *prog;

@@ -582,21 +583,30 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
int prog_fd = skel->progs.%2$s.prog_fd; \n\
", obj_name, bpf_program__name(prog));

- switch (bpf_program__type(prog)) {
- case BPF_PROG_TYPE_RAW_TRACEPOINT:
- tp_name = strchr(bpf_program__section_name(prog), '/') + 1;
- printf("\tint fd = skel_raw_tracepoint_open(\"%s\", prog_fd);\n", tp_name);
- break;
- case BPF_PROG_TYPE_TRACING:
- case BPF_PROG_TYPE_LSM:
- if (bpf_program__expected_attach_type(prog) == BPF_TRACE_ITER)
- printf("\tint fd = skel_link_create(prog_fd, 0, BPF_TRACE_ITER);\n");
+ if (gen_attach) {
+ if (bpf_program__autoload(prog) && bpf_program__autoattach(prog))
+ printf("\tint fd = ((void)prog_fd, skel->links.%s_fd <= 0 ? -ENOLINK : 0);\n",
+ bpf_program__name(prog));
else
- printf("\tint fd = skel_raw_tracepoint_open(NULL, prog_fd);\n");
- break;
- default:
- printf("\tint fd = ((void)prog_fd, 0); /* auto-attach not supported */\n");
- break;
+ printf("\tint fd = ((void)prog_fd, 0);\n");
+ } else {
+ switch (bpf_program__type(prog)) {
+ case BPF_PROG_TYPE_RAW_TRACEPOINT:
+ tp_name = strchr(bpf_program__section_name(prog), '/') + 1;
+ printf("\tint fd = skel_raw_tracepoint_open(\"%s\", prog_fd);\n",
+ tp_name);
+ break;
+ case BPF_PROG_TYPE_TRACING:
+ case BPF_PROG_TYPE_LSM:
+ if (bpf_program__expected_attach_type(prog) == BPF_TRACE_ITER)
+ printf("\tint fd = skel_link_create(prog_fd, 0, BPF_TRACE_ITER);\n");
+ else
+ printf("\tint fd = skel_raw_tracepoint_open(NULL, prog_fd);\n");
+ break;
+ default:
+ printf("\tint fd = ((void)prog_fd, 0); /* auto-attach not supported */\n");
+ break;
+ }
}
codegen("\
\n\
@@ -640,6 +650,8 @@ static void codegen_attach_detach(struct bpf_object *obj, const char *obj_name)
\n\
skel_closenz(skel->links.%1$s_fd); \n\
", bpf_program__name(prog));
+ if (gen_attach)
+ printf("\tskel->links.%s_fd = 0;\n", bpf_program__name(prog));
}

codegen("\
@@ -691,7 +703,8 @@ static void codegen_destroy(struct bpf_object *obj, const char *obj_name)
obj_name);
}

-static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard)
+static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *header_guard,
+ bool gen_attach)
{
DECLARE_LIBBPF_OPTS(gen_loader_opts, opts);
struct bpf_load_and_run_opts sopts = {};
@@ -702,6 +715,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
char ident[256];
int err = 0;

+ opts.gen_attach = gen_attach;
if (sign_progs)
opts.gen_hash = true;

@@ -726,7 +740,7 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
", obj_name);


- codegen_attach_detach(obj, obj_name);
+ codegen_attach_detach(obj, obj_name, gen_attach);

codegen_destroy(obj, obj_name);

@@ -743,6 +757,8 @@ static int gen_trace(struct bpf_object *obj, const char *obj_name, const char *h
skel->ctx.sz = (char *)&skel->links - (char *)skel; \n\
",
obj_name, opts.data_sz);
+ if (gen_attach)
+ printf("\tskel->ctx.sz += sizeof(skel->links);\n");
bpf_object__for_each_map(map, obj) {
const void *mmap_data = NULL;
size_t mmap_size = 0;
@@ -1249,6 +1265,7 @@ static int do_skeleton(int argc, char **argv)
struct bpf_map *map;
struct btf *btf;
struct stat st;
+ bool gen_attach = false;

if (!REQ_ARGS(1)) {
usage();
@@ -1257,10 +1274,9 @@ static int do_skeleton(int argc, char **argv)
file = GET_ARG();

while (argc) {
- if (!REQ_ARGS(2))
- return -1;
-
if (is_prefix(*argv, "name")) {
+ if (!REQ_ARGS(2))
+ return -1;
NEXT_ARG();

if (obj_name[0] != '\0') {
@@ -1270,6 +1286,8 @@ static int do_skeleton(int argc, char **argv)

strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1);
obj_name[MAX_OBJ_NAME_LEN - 1] = '\0';
+ } else if (is_prefix(*argv, "attach")) {
+ gen_attach = true;
} else {
p_err("unknown arg %s", *argv);
return -1;
@@ -1278,6 +1296,11 @@ static int do_skeleton(int argc, char **argv)
NEXT_ARG();
}

+ if (gen_attach && !use_loader) {
+ p_err("attach requires a light skeleton (-L)");
+ return -1;
+ }
+
if (argc) {
p_err("extra unknown arguments");
return -1;
@@ -1441,7 +1464,7 @@ static int do_skeleton(int argc, char **argv)
goto out;
}
if (use_loader) {
- err = gen_trace(obj, obj_name, header_guard);
+ err = gen_trace(obj, obj_name, header_guard, gen_attach);
goto out;
}

@@ -2005,7 +2028,7 @@ static int do_help(int argc, char **argv)

fprintf(stderr,
"Usage: %1$s %2$s object OUTPUT_FILE INPUT_FILE [INPUT_FILE...]\n"
- " %1$s %2$s skeleton FILE [name OBJECT_NAME]\n"
+ " %1$s %2$s skeleton FILE [name OBJECT_NAME] [attach]\n"
" %1$s %2$s subskeleton FILE [name OBJECT_NAME]\n"
" %1$s %2$s min_core_btf INPUT OUTPUT OBJECT [OBJECT...]\n"
" %1$s %2$s help\n"
--
2.43.0