Skip to contentSkip to conversation

bpf: Support struct_ops in light skeletons

PATCH Applied Siddharth Nayyar 2 participants 6 messages Dec 1

Subsystems (6)
Kselftest BPF BPF selftests BPF core +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover bpf: Support struct_ops in light skeletons

BPF light skeletons (bpftool gen skeleton -L) compile BPF object loading into a self-contained BPF_PROG_TYPE_SYSCALL loader program (gen_loader) embedded in a C header, removing any runtime dependency on libbpf or userspace BTF parsing. Currently, light skeletons do not support BPF_MAP_TYPE_STRUCT_OPS maps or BPF_PROG_TYPE_STRUCT_OPS programs because preparing a struct_ops map normally relies on libbpf parsing the target vmlinux or kernel module BTF in userspace to resolve type IDs, struct sizes, member byte offsets, and member indices.

Since the kernel already has access to both the BPF object's BTF (loaded by the light skeleton via btf_fd) and vmlinux/module BTFs, the loader can pass static object-BTF metadata known at skeleton generation time and let the kernel match the target struct_ops type and members by name. This avoids adding a new BPF helper or kfunc, eliminates runtime BTF queries and module BTF FD management from the loader program, and avoids pre-allocating a fixed-size kernel value buffer in the loader blob.

This series adds BPF_MAP_TYPE_STRUCT_OPS support for link-based (SEC(".struct_ops.link")) maps in light skeletons across both vmlinux and kernel module BTF targets:

- Patch 1 teaches the kernel to resolve BPF_MAP_TYPE_STRUCT_OPS maps and BPF_PROG_TYPE_STRUCT_OPS attach targets from the BPF object's BTF by name (stripping any CO-RE ___flavor suffix). BPF_MAP_CREATE accepts a local struct btf_value_type_id and value_size, BPF_PROG_LOAD accepts the object BTF FD in attach_btf_obj_fd with the local struct ID and member index, and BPF_MAP_UPDATE_ELEM validates and copies the local struct layout into the kernel struct_ops value buffer.
- Patch 2 updates libbpf's gen_loader to record local struct_ops type IDs and member indices, retain btf_value_type_id on BPF_MAP_CREATE, pass the object btf_fd in attach_btf_obj_fd on BPF_PROG_LOAD, and emit bpf_gen__struct_ops_update_elem() to populate callback FDs and issue BPF_MAP_UPDATE_ELEM.
- Patch 3 teaches bpftool to generate per-map skel_link_create() attach functions and skel__attach() / skel__detach() plumbing for BPF_F_LINK struct_ops maps in light skeletons, skips gen_st_ops_shadow() so the light skeleton ctx layout is preserved, and rejects non-link struct_ops maps.
- Patch 4 adds light skeleton struct_ops selftests covering both vmlinux (tcp_congestion_ops) and bpf_testmod (bpf_testmod_ops, including a reordered subset struct bpf_testmod_ops___v2 with a zeroed member absent from the kernel, shared callbacks, scalar fields, detach, and per-map re-attachment).

Non-link struct_ops support
===========================
Non-link struct_ops maps (SEC(".struct_ops") without BPF_F_LINK) remain rejected in light skeletons for now. Because BPF_MAP_UPDATE_ELEM runs inside the loader program during skel__load(), a non-link struct_ops map would register and activate callbacks before skel__attach() is called, prevent re-attaching after detach, and leak an active registration if a later loader step fails. Supporting non-link struct_ops can be added as follow-up work by deferring BPF_MAP_UPDATE_ELEM to userspace skel__attach().

AI assistance and testing
=========================
This series (code, selftests, commit messages, and cover letter) was developed interactively with an AI coding assistant (Google Gemini), with me guiding the design step by step across each patch and reviewing all changes.

The series was built with clang/LLVM and tested in a QEMU x86_64 VM using test_progs (covering the new struct_ops_lskel selftests for both vmlinux tcp_congestion_ops and bpf_testmod_ops, as well as existing struct_ops and light skeleton tests).

---
Changes in v3:
- Bounds-check local struct members against the local struct size and
reject non-kflag bitfields and unaligned bit offsets in local_to_kern_value().
- Pin module BTF via btf_try_get_module() before searching
struct_ops_tab in find_desc_by_local_type() to avoid racing with
module init.
- Remove the redundant function-pointer mismatch check (allowing NULL
void * callback placeholders) and local struct padding-hole check in local_to_kern_value().
- Add selftest coverage for a NULL void * callback placeholder in bpf_testmod_ops___v2 and an invalid_local_btf subtest verifying
rejection of crafted local BTF structs.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Replaced the BPF helper approach from v1 with kernel-side object-BTF
name matching as suggested by Alexei Starovoitov, replacing patches
1-5 with two patches (kernel and libbpf).
- Added kernel validation for local member BTF kind/func-ptr matching,
non-zero padding holes, and non-kernel btf_fd, and synced
st_ops->progs from st_ops->data in libbpf.
- Updated bpftool light skeleton skel__detach() to zero detached
struct_ops link FDs.
- Updated struct_ops_lskel selftests to assert detached link FDs are
zeroed and to test skipping a zeroed field absent from the kernel.
- Dropped the RFC prefix.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Siddharth Nayyar (4):
bpf: Support struct_ops with object BTF
libbpf: Support struct_ops in light skeleton loader
bpftool: Generate attach and detach for struct_ops in light skeletons selftests/bpf: Add light skeleton struct_ops tests

 include/linux/bpf.h                                |  14 ++
 include/uapi/linux/bpf.h                           |  17 +-
 kernel/bpf/bpf_struct_ops.c                        | 266 ++++++++++++++++++++-
 kernel/bpf/syscall.c                               |  14 +-
 tools/bpf/bpftool/gen.c                            |  72 +++++-
 tools/include/uapi/linux/bpf.h                     |  17 +-
 tools/lib/bpf/bpf_gen_internal.h                   |   3 +
 tools/lib/bpf/gen_loader.c                         |  35 +++
 tools/lib/bpf/libbpf.c                             | 110 ++++++++-
 tools/testing/selftests/bpf/Makefile.skel          |   3 +-
 .../selftests/bpf/prog_tests/struct_ops_lskel.c    | 200 ++++++++++++++++
 tools/testing/selftests/bpf/progs/struct_ops_mod.c |  53 ++++
 .../selftests/bpf/progs/struct_ops_vmlinux.c       |  42 ++++
 13 files changed, 824 insertions(+), 22 deletions(-)

---
base-commit: b7a509bd447085118d03dd962d2cca308e283b57
change-id: 20261102-lskel-struct-ops-03090424cfa2

Best regards,

Signature · 1 line
Siddharth Nayyar <[email protected]>
#1 lore raw

PATCH v3 1/4 bpf: Support struct_ops with object BTF

Currently, creating and populating a BPF_MAP_TYPE_STRUCT_OPS map and loading its BPF_PROG_TYPE_STRUCT_OPS programs require userspace to parse vmlinux or module BTF to resolve kernel type IDs, wrapper struct sizes, member indices, and member byte offsets. Light skeleton loaders ship a pre-generated BPF object BTF and loader program without userspace BTF parsing, while the kernel already has access to both the BPF object's BTF (via btf_fd) and vmlinux/module BTFs.

Allow BPF_MAP_TYPE_STRUCT_OPS and BPF_PROG_TYPE_STRUCT_OPS to specify their target struct and members using the BPF object's own BTF, with the kernel resolving the corresponding kernel struct_ops type and members by name (stripping any CO-RE ___flavor suffix):

- BPF_MAP_CREATE: Accept btf_value_type_id and value_size describing the local struct in btf_fd when btf_vmlinux_value_type_id is 0. Resolve bpf_struct_ops_<name> in vmlinux or module BTF, pin any owning module, and record local_btf and local_type on the map so other map operations (such as BPF_MAP_LOOKUP_ELEM) continue to see a standard struct_ops map.
- BPF_PROG_LOAD: When attach_btf_obj_fd refers to the BPF object's BTF for a BPF_PROG_TYPE_STRUCT_OPS program, resolve attach_btf_id and the local member index in expected_attach_type to the matching kernel BTF, type ID, and member index.
- BPF_MAP_UPDATE_ELEM: When local_type is set on the map, accept a value with the local struct's size and layout, validate member types, sizes, and bounds, skip zeroed members absent from the running kernel, and copy each member into the kernel struct_ops value buffer.

Existing users passing kernel BTF IDs are unaffected.

---
 include/linux/bpf.h            |  14 +++
 include/uapi/linux/bpf.h       |  17 ++-
 kernel/bpf/bpf_struct_ops.c    | 266 +++++++++++++++++++++++++++++++++++++++--
 kernel/bpf/syscall.c           |  14 ++-
 tools/include/uapi/linux/bpf.h |  17 ++-
 5 files changed, 314 insertions(+), 14 deletions(-)
include/linux/bpf.h +14 -0
index 4f9c2bf64643..74cf542464fb 100644
@@ -2444,6 +2444,8 @@ void bpf_struct_ops_put(const void *kdata);
 int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff);
 int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key,
 				       void *value);
+u32 bpf_struct_ops_map_update_size(const struct bpf_map *map);
+int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr);
 int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes,
 				      struct bpf_tramp_node *node,
 				      const struct btf_func_model *model,
@@ -2522,6 +2524,18 @@ static inline int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map,
 {
 	return -EINVAL;
 }
+
+static inline u32 bpf_struct_ops_map_update_size(const struct bpf_map *map)
+{
+	return map->value_size;
+}
+
+static inline int bpf_struct_ops_resolve_attach(struct btf **btf,
+						union bpf_attr *attr)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline int bpf_struct_ops_link_create(union bpf_attr *attr)
 {
 	return -EOPNOTSUPP;
include/uapi/linux/bpf.h +16 -1
index 28904c6f0787..5d44bce55d5e 100644
@@ -1599,6 +1599,14 @@ union bpf_attr {
 						   * struct stored as the
 						   * map value
 						   */
+		/*
+		 * BPF_MAP_TYPE_STRUCT_OPS takes one of the two ids.
+		 * btf_value_type_id is a struct in btf_fd, e.g.
+		 * tcp_congestion_ops, and value_size is its size. The kernel
+		 * finds its struct_ops and the members by name.
+		 * BPF_MAP_UPDATE_ELEM takes the struct of btf_fd.
+		 * BPF_MAP_LOOKUP_ELEM returns the struct of the kernel.
+		 */
 		/* Any per-map-type extra fields
 		 *
 		 * BPF_MAP_TYPE_BLOOM_FILTER - the lowest 4 bits indicate the
@@ -1686,7 +1694,14 @@ union bpf_attr {
 		union {
 			/* valid prog_fd to attach to bpf prog */
 			__u32		attach_prog_fd;
-			/* or valid module BTF object fd or 0 to attach to vmlinux */
+			/*
+			 * or valid module BTF object fd or 0 to attach to
+			 * vmlinux. BPF_PROG_TYPE_STRUCT_OPS takes the BTF of
+			 * the bpf object too. Then attach_btf_id is a struct
+			 * in that BTF and expected_attach_type is the index
+			 * of its member. The kernel finds its struct_ops and
+			 * the member by name.
+			 */
 			__u32		attach_btf_obj_fd;
 		};
 		__u32		core_relo_cnt;	/* number of bpf_core_relo */
kernel/bpf/bpf_struct_ops.c +259 -7

334 lines not on this page · raw message

kernel/bpf/syscall.c +9 -5
index 198438b95849..3a8c25285236 100644
@@ -1899,6 +1899,8 @@ static int map_update_elem(union bpf_attr *attr, bpfptr_t uattr)
 	}
 
 	value_size = bpf_map_value_size(map, attr->flags);
+	if (map->map_type == BPF_MAP_TYPE_STRUCT_OPS)
+		value_size = bpf_struct_ops_map_update_size(map);
 	value = kvmemdup_bpfptr(uvalue, value_size);
 	if (IS_ERR(value)) {
 		err = PTR_ERR(value);
@@ -3044,12 +3046,14 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at
 				goto put_token;
 			}
 			if (!btf_is_kernel(attach_btf)) {
-				/* attaching through specifying bpf_prog's BTF
-				 * objects directly might be supported eventually
-				 */
-				btf_put(attach_btf);
+				/* only struct_ops can name its target in the BTF of the object */
 				err = -ENOTSUPP;
-				goto put_token;
+				if (type == BPF_PROG_TYPE_STRUCT_OPS)
+					err = bpf_struct_ops_resolve_attach(&attach_btf, attr);
+				if (err) {
+					btf_put(attach_btf);
+					goto put_token;
+				}
 			}
 		}
 	} else if (attr->attach_btf_id || multi_func) {
tools/include/uapi/linux/bpf.h +16 -1
index 28904c6f0787..5d44bce55d5e 100644
@@ -1599,6 +1599,14 @@ union bpf_attr {
 						   * struct stored as the
 						   * map value
 						   */
+		/*
+		 * BPF_MAP_TYPE_STRUCT_OPS takes one of the two ids.
+		 * btf_value_type_id is a struct in btf_fd, e.g.
+		 * tcp_congestion_ops, and value_size is its size. The kernel
+		 * finds its struct_ops and the members by name.
+		 * BPF_MAP_UPDATE_ELEM takes the struct of btf_fd.
+		 * BPF_MAP_LOOKUP_ELEM returns the struct of the kernel.
+		 */
 		/* Any per-map-type extra fields
 		 *
 		 * BPF_MAP_TYPE_BLOOM_FILTER - the lowest 4 bits indicate the
@@ -1686,7 +1694,14 @@ union bpf_attr {
 		union {
 			/* valid prog_fd to attach to bpf prog */
 			__u32		attach_prog_fd;
-			/* or valid module BTF object fd or 0 to attach to vmlinux */
+			/*
+			 * or valid module BTF object fd or 0 to attach to
+			 * vmlinux. BPF_PROG_TYPE_STRUCT_OPS takes the BTF of
+			 * the bpf object too. Then attach_btf_id is a struct
+			 * in that BTF and expected_attach_type is the index
+			 * of its member. The kernel finds its struct_ops and
+			 * the member by name.
+			 */
 			__u32		attach_btf_obj_fd;
 		};
 		__u32		core_relo_cnt;	/* number of bpf_core_relo */
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#2 lore raw

PATCH v3 2/4 libbpf: Support struct_ops in light skeleton loader

In reply to Siddharth Nayyar

Teach libbpf's gen_loader to create, load, and populate BPF_MAP_TYPE_STRUCT_OPS maps and BPF_PROG_TYPE_STRUCT_OPS programs using the BPF object's own BTF, relying on the kernel to resolve the target struct_ops type and members by name:

- In bpf_object__init_kern_struct_ops_maps(), sync callback pointers from st_ops->data, validate callback programs, and record the local struct type ID and member index in prog->attach_btf_id and prog->expected_attach_type when generating a loader program.
- Keep create_attr.btf_value_type_id set in bpf_object__create_map() so bpf_gen__map_create() passes the local struct's BTF type ID and size along with the object's btf_fd.
- In bpf_gen__prog_load(), populate attach_btf_obj_fd from btf_fd on the loader stack for BPF_PROG_TYPE_STRUCT_OPS programs.
- Add bpf_gen__struct_ops_update_elem() and wire it up in bpf_object_prepare_struct_ops() to embed the local struct value (with userspace bpf_program pointers zeroed out), copy initial_value when unsigned, write each loaded callback's FD at its local member offset, and emit BPF_MAP_UPDATE_ELEM.

---
 tools/lib/bpf/bpf_gen_internal.h |   3 ++
 tools/lib/bpf/gen_loader.c       |  35 +++++++++++++
 tools/lib/bpf/libbpf.c           | 110 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 145 insertions(+), 3 deletions(-)
tools/lib/bpf/bpf_gen_internal.h +3 -0
index c30e05b66abc..6fc3eb02f173 100644
@@ -76,6 +76,9 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 			struct bpf_prog_load_opts *load_attr, int prog_idx);
 void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *value, __u32 value_size,
 			      __u64 flags);
+void bpf_gen__struct_ops_update_elem(struct bpf_gen *gen, int map_idx, void *value,
+				     __u32 value_size, const __u32 *fd_offs,
+				     const int *prog_idxs, int cnt);
 void bpf_gen__map_freeze(struct bpf_gen *gen, int map_idx);
 int bpf_gen__func_ptr_map_create(struct bpf_gen *gen, const char *map_name, int obj_map_idx,
 				 void *value, __u32 value_size, const __u32 *ptr_offs,
tools/lib/bpf/gen_loader.c +35 -0
index 27b4bc9e55ac..749a86c43e7f 100644
@@ -1093,6 +1093,10 @@ void bpf_gen__prog_load(struct bpf_gen *gen,
 	/* populate union bpf_attr with btf_fd saved in the stack earlier */
 	move_stack2blob(gen, attr_field(prog_load_attr, prog_btf_fd), 4,
 			stack_off(btf_fd));
+	if (prog_type == BPF_PROG_TYPE_STRUCT_OPS)
+		/* attach_btf_id is a struct in the BTF of the object */
+		move_stack2blob(gen, attr_field(prog_load_attr, attach_btf_obj_fd), 4,
+				stack_off(btf_fd));
 	if (gen->attach_kind) {
 		emit_find_attach_target(gen);
 		/* populate union bpf_attr with btf_id and btf_obj_fd found by helper */
@@ -1198,6 +1202,37 @@ void bpf_gen__map_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue,
 	emit_map_update_elem(gen, map_idx, &attr, attr_size, key, value, value_size);
 }
 
+/*
+ * pvalue is the struct as the object has it. The kernel moves the members to
+ * where it has them. The fds of the programs prog_idxs go to fd_offs.
+ */
+void bpf_gen__struct_ops_update_elem(struct bpf_gen *gen, int map_idx, void *pvalue,
+				     __u32 value_size, const __u32 *fd_offs,
+				     const int *prog_idxs, int cnt)
+{
+	int attr_size = offsetofend(union bpf_attr, flags);
+	int value, key, zero = 0, i;
+	union bpf_attr attr;
+
+	memset(&attr, 0, attr_size);
+	value = add_data(gen, pvalue, value_size);
+	key = add_data(gen, &zero, sizeof(zero));
+
+	/* see bpf_gen__map_update_elem() */
+	if (!OPTS_GET(gen->opts, gen_hash, false))
+		emit_copy_initial_value(gen, map_idx, value, value_size, 0);
+
+	for (i = 0; i < cnt; i++) {
+		emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_2, BPF_PSEUDO_MAP_IDX_VALUE,
+						 0, 0, 0, blob_prog_fd_off(gen, prog_idxs[i])));
+		emit(gen, BPF_LDX_MEM(BPF_W, BPF_REG_0, BPF_REG_2, 0));
+		emit2(gen, BPF_LD_IMM64_RAW_FULL(BPF_REG_1, BPF_PSEUDO_MAP_IDX_VALUE,
+						 0, 0, 0, value + fd_offs[i]));
+		emit(gen, BPF_STX_MEM(BPF_DW, BPF_REG_1, BPF_REG_0, 0));
+	}
+	emit_map_update_elem(gen, map_idx, &attr, attr_size, key, value, value_size);
+}
+
 void bpf_gen__populate_outer_map(struct bpf_gen *gen, int outer_map_idx, int slot,
 				 int inner_map_idx)
 {
tools/lib/bpf/libbpf.c +107 -3

140 lines not on this page · raw message

Signature · 1 line
2.56.1.839.g22a8290db9-goog
#3 lore raw

PATCH v3 3/4 bpftool: Generate attach and detach for struct_ops in light skeletons

In reply to Siddharth Nayyar

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.

---
 tools/bpf/bpftool/gen.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 4 deletions(-)
tools/bpf/bpftool/gen.c +68 -4
index ee680f7c2116..da5f7c9d99ab 100644
@@ -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");
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#4 lore raw

PATCH v3 4/4 selftests/bpf: Add light skeleton struct_ops tests

In reply to Siddharth Nayyar

Add selftests exercising BPF_MAP_TYPE_STRUCT_OPS support in light skeletons across both vmlinux and kernel module BTF targets:

- struct_ops_vmlinux.c defines a SEC(".struct_ops.link") tcp_congestion_ops map in vmlinux BTF, testing open/load, link attachment, callback invocation over a loopback TCP connection, and unregistration upon detach.
- struct_ops_mod.c defines two SEC(".struct_ops.link") maps targeting bpf_testmod_ops in module BTF (including a reordered struct bpf_testmod_ops___v2 with a zeroed field absent from the kernel, a NULL void * callback placeholder, and a shared callback), testing kernel-side object-BTF resolution, scalar member copying, detach, and per-map re-attachment.
- invalid_local_btf verifies that BPF_MAP_UPDATE_ELEM rejects crafted local BTF structs whose INT members extend past the local struct size, use non-byte-aligned bit offsets, or encode non-kflag bitfields.

---
 tools/testing/selftests/bpf/Makefile.skel          |   3 +-
 .../selftests/bpf/prog_tests/struct_ops_lskel.c    | 200 +++++++++++++++++++++
 tools/testing/selftests/bpf/progs/struct_ops_mod.c |  53 ++++++
 .../selftests/bpf/progs/struct_ops_vmlinux.c       |  42 +++++
 4 files changed, 297 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/Makefile.skel +2 -1
index 45e4ce5dca4d..334342928d01 100644
@@ -34,7 +34,8 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h		\
 LSKELS := fexit_sleep.c trace_printk.c trace_vprintk.c map_ptr_kern.c 	\
 	core_kern.c core_kern_overflow.c test_ringbuf.c			\
 	test_ringbuf_n.c test_ringbuf_map_key.c test_ringbuf_write.c    \
-	test_ringbuf_overwrite.c callx_rodata.c many_progs.c
+	test_ringbuf_overwrite.c callx_rodata.c many_progs.c		\
+	struct_ops_vmlinux.c struct_ops_mod.c
 
 LSKELS_SIGNED := fentry_test.c fexit_test.c atomics.c
 
tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c new file +200 -0

200 lines not on this page · raw message

tools/testing/selftests/bpf/progs/struct_ops_mod.c new file +53 -0

53 lines not on this page · raw message

tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c new file +42 -0

42 lines not on this page · raw message

Signature · 1 line
2.56.1.839.g22a8290db9-goog
#5 lore raw
In reply to Siddharth Nayyar
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Tue, 01 Dec 2026 17:33:36 +0000 you wrote:

BPF light skeletons (bpftool gen skeleton -L) compile BPF object loading into a self-contained BPF_PROG_TYPE_SYSCALL loader program (gen_loader) embedded in a C header, removing any runtime dependency on libbpf or userspace BTF parsing. Currently, light skeletons do not support BPF_MAP_TYPE_STRUCT_OPS maps or BPF_PROG_TYPE_STRUCT_OPS programs because preparing a struct_ops map normally relies on libbpf parsing the target vmlinux or kernel module BTF in userspace to resolve type IDs, struct sizes, member byte offsets, and member indices.

[...]

Here is the summary with links:
- [bpf-next,v3,1/4] bpf: Support struct_ops with object BTF

    https://git.kernel.org/bpf/bpf-next/c/466ae6570ce8
  - [bpf-next,v3,2/4] libbpf: Support struct_ops in light skeleton loader
    https://git.kernel.org/bpf/bpf-next/c/9fb39c4ba9cc
  - [bpf-next,v3,3/4] bpftool: Generate attach and detach for struct_ops in light skeletons
    https://git.kernel.org/bpf/bpf-next/c/788d674512f4
  - [bpf-next,v3,4/4] selftests/bpf: Add light skeleton struct_ops tests
    https://git.kernel.org/bpf/bpf-next/c/beefc3ad9093

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html