* [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF
2026-11-30 21:44 [PATCH bpf-next v2 0/4] bpf: Support struct_ops in light skeletons Siddharth Nayyar
@ 2026-11-30 21:44 ` Siddharth Nayyar
2026-11-30 22:05 ` sashiko-bot
2026-11-30 21:44 ` [PATCH bpf-next v2 2/4] libbpf: Support struct_ops in light skeleton loader Siddharth Nayyar
` (2 subsequent siblings)
3 siblings, 1 reply; 6+ messages in thread
From: Siddharth Nayyar @ 2026-11-30 21:44 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Quentin Monnet, Shuah Khan
Cc: bpf, linux-kernel, linux-kselftest, Anton Protopopov,
Giuliano Procida, Neill Kapron, Thiébaud Weksteen,
Maxime Ripard, Siddharth Nayyar, Alexei Starovoitov
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 zeroed padding holes, 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.
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
include/linux/bpf.h | 14 +++
include/uapi/linux/bpf.h | 17 ++-
kernel/bpf/bpf_struct_ops.c | 236 +++++++++++++++++++++++++++++++++++++++--
kernel/bpf/syscall.c | 14 ++-
tools/include/uapi/linux/bpf.h | 17 ++-
5 files changed, 284 insertions(+), 14 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 4f9c2bf64643..74cf542464fb 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -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;
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index 28904c6f0787..5d44bce55d5e 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -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 */
diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
index 3d334cd526c0..96eca8493892 100644
--- a/kernel/bpf/bpf_struct_ops.c
+++ b/kernel/bpf/bpf_struct_ops.c
@@ -14,6 +14,7 @@
#include <linux/rcupdate_wait.h>
#include <linux/poll.h>
#include <linux/bpf-cgroup.h>
+#include "../tools/lib/bpf/relo_core.h"
struct bpf_struct_ops_value {
struct bpf_struct_ops_common_value common;
@@ -42,6 +43,12 @@ struct bpf_struct_ops_map {
void *image_pages[MAX_TRAMP_IMAGE_PAGES];
/* The owner moduler's btf. */
struct btf *btf;
+ /*
+ * Like in CO-RE "local" is what the BTF of the bpf object has.
+ * When local_type is set map_update_elem() takes this struct.
+ */
+ struct btf *local_btf;
+ const struct btf_type *local_type;
/* uvalue->data stores the kernel struct
* (e.g. tcp_congestion_ops) that is more useful
* to userspace than the kvalue. For example,
@@ -593,7 +600,7 @@ static void bpf_struct_ops_map_free_image(struct bpf_struct_ops_map *st_map)
st_map->image_pages_cnt = 0;
}
-static int check_zero_holes(const struct btf *btf, const struct btf_type *t, void *data)
+static int check_zero_holes(const struct btf *btf, const struct btf_type *t, const void *data)
{
const struct btf_member *member;
u32 i, moff, msize, prev_mend = 0;
@@ -729,8 +736,152 @@ static void bpf_struct_ops_map_free_ksyms(struct bpf_struct_ops_map *st_map)
}
}
-static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
- void *value, u64 flags)
+static const struct btf_member *find_member(const struct btf *btf,
+ const struct btf_type *t,
+ const char *name)
+{
+ const struct btf_member *member;
+ u32 i;
+
+ if (!*name)
+ return NULL;
+ for_each_member(i, t, member)
+ if (!strcmp(btf_name_by_offset(btf, member->name_off), name))
+ return member;
+ return NULL;
+}
+
+/*
+ * 'id' is a local struct, e.g. tcp_congestion_ops or tcp_congestion_ops___v2.
+ * Find bpf_struct_ops_tcp_congestion_ops in the BTF of vmlinux or of a module.
+ * A module can have it for a struct of vmlinux. The caller does btf_put(*kern_btf).
+ */
+static const struct bpf_struct_ops_desc *
+find_desc_by_local_type(const struct btf *btf, u32 id, struct btf **kern_btf)
+{
+ const struct btf_type *t = btf_type_by_id(btf, id);
+ const struct bpf_struct_ops_desc *st_ops_desc;
+ char name[KSYM_NAME_LEN];
+ const char *tname;
+ s32 value_id;
+
+ if (!t || !__btf_type_is_struct(t))
+ return ERR_PTR(-EINVAL);
+ tname = btf_name_by_offset(btf, t->name_off);
+ if (snprintf(name, sizeof(name), VALUE_PREFIX "%.*s",
+ (int)bpf_core_essential_name_len(tname), tname) >= sizeof(name))
+ return ERR_PTR(-EINVAL);
+ value_id = bpf_find_btf_id(name, BTF_KIND_STRUCT, kern_btf);
+ if (value_id < 0)
+ return ERR_PTR(value_id);
+ st_ops_desc = bpf_struct_ops_find_value(*kern_btf, value_id);
+ if (!st_ops_desc) {
+ btf_put(*kern_btf);
+ return ERR_PTR(-EOPNOTSUPP);
+ }
+ return st_ops_desc;
+}
+
+/*
+ * attach_btf_id is a local struct and expected_attach_type is the index of its
+ * member. Replace them, and the BTF, with what the kernel has under the same names.
+ */
+int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr)
+{
+ const struct bpf_struct_ops_desc *st_ops_desc;
+ const struct btf_member *member;
+ const struct btf_type *t;
+ struct btf *kern_btf;
+
+ st_ops_desc = find_desc_by_local_type(*btf, attr->attach_btf_id, &kern_btf);
+ if (IS_ERR(st_ops_desc))
+ return PTR_ERR(st_ops_desc);
+ t = btf_type_by_id(*btf, attr->attach_btf_id);
+ if (attr->expected_attach_type >= btf_type_vlen(t)) {
+ btf_put(kern_btf);
+ return -EINVAL;
+ }
+ member = btf_type_member(t) + attr->expected_attach_type;
+ member = find_member(kern_btf, st_ops_desc->type,
+ btf_name_by_offset(*btf, member->name_off));
+ if (!member) {
+ btf_put(kern_btf);
+ return -ENOENT;
+ }
+ attr->attach_btf_id = st_ops_desc->type_id;
+ attr->expected_attach_type = member - btf_type_member(st_ops_desc->type);
+ btf_put(*btf);
+ *btf = kern_btf;
+ return 0;
+}
+
+u32 bpf_struct_ops_map_update_size(const struct bpf_map *map)
+{
+ const struct bpf_struct_ops_map *st_map = (const struct bpf_struct_ops_map *)map;
+
+ return st_map->local_type ? st_map->local_type->size : map->value_size;
+}
+
+/*
+ * 'data' has the layout of the local struct. Copy every member to the place
+ * of the member with the same name in the struct of the kernel.
+ */
+static void *local_to_kern_value(const struct bpf_struct_ops_map *st_map, const void *data)
+{
+ const struct btf_type *t = st_map->local_type, *kern_t = st_map->st_ops_desc->type;
+ const struct btf *btf = st_map->local_btf, *kern_btf = st_map->btf;
+ const struct btf_member *member, *kern_member;
+ const struct btf_type *mtype, *kern_mtype;
+ struct bpf_struct_ops_value *value;
+ u32 i, size, kern_size;
+ int err = -EINVAL;
+
+ value = kvzalloc(st_map->map.value_size, GFP_USER);
+ if (!value)
+ return ERR_PTR(-ENOMEM);
+
+ for_each_member(i, t, member) {
+ const void *src = data + __btf_member_bit_offset(t, member) / 8;
+
+ if (__btf_member_bitfield_size(t, member) ||
+ IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, member->type), &size)))
+ goto err;
+ kern_member = find_member(kern_btf, kern_t,
+ btf_name_by_offset(btf, member->name_off));
+ if (!kern_member) {
+ /* This kernel doesn't have the member. That's fine when it's not in use. */
+ if (!memchr_inv(src, 0, size))
+ continue;
+ err = -ENOENT;
+ goto err;
+ }
+ if (__btf_member_bitfield_size(kern_t, kern_member) ||
+ IS_ERR(btf_resolve_size(kern_btf,
+ btf_type_by_id(kern_btf, kern_member->type),
+ &kern_size)) ||
+ size != kern_size)
+ goto err;
+ mtype = btf_type_skip_modifiers(btf, member->type, NULL);
+ kern_mtype = btf_type_skip_modifiers(kern_btf, kern_member->type, NULL);
+ if (BTF_INFO_KIND(mtype->info) != BTF_INFO_KIND(kern_mtype->info))
+ goto err;
+ if (btf_type_is_ptr(mtype) &&
+ !!btf_type_resolve_func_ptr(btf, member->type, NULL) !=
+ !!btf_type_resolve_func_ptr(kern_btf, kern_member->type, NULL))
+ goto err;
+ memcpy(value->data + __btf_member_bit_offset(kern_t, kern_member) / 8, src, size);
+ }
+ err = check_zero_holes(btf, t, data);
+ if (err)
+ goto err;
+ return value;
+err:
+ kvfree(value);
+ return ERR_PTR(err);
+}
+
+static long __bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
+ void *value, u64 flags)
{
struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
const struct bpf_struct_ops_desc *st_ops_desc = st_map->st_ops_desc;
@@ -967,6 +1118,23 @@ static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
return err;
}
+static long bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
+ void *value, u64 flags)
+{
+ struct bpf_struct_ops_map *st_map = (struct bpf_struct_ops_map *)map;
+ long err;
+
+ if (!st_map->local_type)
+ return __bpf_struct_ops_map_update_elem(map, key, value, flags);
+
+ value = local_to_kern_value(st_map, value);
+ if (IS_ERR(value))
+ return PTR_ERR(value);
+ err = __bpf_struct_ops_map_update_elem(map, key, value, flags);
+ kvfree(value);
+ return err;
+}
+
static long bpf_struct_ops_map_delete_elem(struct bpf_map *map, void *key)
{
enum bpf_struct_ops_state prev_state;
@@ -1029,6 +1197,7 @@ static void __bpf_struct_ops_map_free(struct bpf_map *map)
bpf_map_area_free(st_map->ksyms);
bpf_struct_ops_map_free_image(st_map);
bpf_map_area_free(st_map->uvalue);
+ btf_put(st_map->local_btf);
bpf_map_area_free(st_map);
}
@@ -1063,11 +1232,57 @@ static int bpf_struct_ops_map_alloc_check(union bpf_attr *attr)
{
if (attr->key_size != sizeof(unsigned int) || attr->max_entries != 1 ||
(attr->map_flags & ~(BPF_F_LINK | BPF_F_VTYPE_BTF_OBJ_FD)) ||
- !attr->btf_vmlinux_value_type_id)
+ (!attr->btf_vmlinux_value_type_id && !attr->btf_value_type_id))
return -EINVAL;
return 0;
}
+/*
+ * btf_value_type_id and value_size are of a local struct. Replace them with
+ * btf_vmlinux_value_type_id and value_size of the struct_ops with the same name,
+ * so that everything but map_update_elem() sees a regular struct_ops map.
+ * Return the local BTF. A module that has the struct_ops is pinned.
+ */
+static struct btf *local_to_kern_attr(union bpf_attr *attr, const struct btf_type **local_type,
+ struct btf **kern_btf, struct module **mod)
+{
+ const struct bpf_struct_ops_desc *st_ops_desc;
+ struct btf *btf;
+
+ if (attr->btf_key_type_id || (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD))
+ return ERR_PTR(-EINVAL);
+ btf = btf_get_by_fd(attr->btf_fd);
+ if (IS_ERR(btf))
+ return btf;
+ if (btf_is_kernel(btf)) {
+ btf_put(btf);
+ return ERR_PTR(-EACCES);
+ }
+ st_ops_desc = find_desc_by_local_type(btf, attr->btf_value_type_id, kern_btf);
+ if (IS_ERR(st_ops_desc)) {
+ btf_put(btf);
+ return ERR_CAST(st_ops_desc);
+ }
+ *local_type = btf_type_by_id(btf, attr->btf_value_type_id);
+ if (attr->value_size != (*local_type)->size)
+ goto err;
+ if (btf_is_module(*kern_btf)) {
+ *mod = btf_try_get_module(*kern_btf);
+ if (!*mod)
+ goto err;
+ }
+ attr->btf_value_type_id = 0;
+ attr->btf_vmlinux_value_type_id = st_ops_desc->value_id;
+ attr->value_size = st_ops_desc->value_type->size;
+ /* The module holds its BTF. The BTF of vmlinux doesn't go away. */
+ btf_put(*kern_btf);
+ return btf;
+err:
+ btf_put(*kern_btf);
+ btf_put(btf);
+ return ERR_PTR(-EINVAL);
+}
+
static u32 count_func_ptrs(const struct btf *btf, const struct btf_type *t)
{
int i;
@@ -1086,13 +1301,17 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
const struct bpf_struct_ops_desc *st_ops_desc;
size_t st_map_size;
struct bpf_struct_ops_map *st_map;
- const struct btf_type *t, *vt;
+ const struct btf_type *t, *vt, *local_type = NULL;
+ struct btf *btf, *local_btf = NULL;
struct module *mod = NULL;
struct bpf_map *map;
- struct btf *btf;
int ret;
- if (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD) {
+ if (attr->btf_value_type_id) {
+ local_btf = local_to_kern_attr(attr, &local_type, &btf, &mod);
+ if (IS_ERR(local_btf))
+ return ERR_CAST(local_btf);
+ } else if (attr->map_flags & BPF_F_VTYPE_BTF_OBJ_FD) {
/* The map holds btf for its whole life time. */
btf = btf_get_by_fd(attr->value_type_btf_obj_fd);
if (IS_ERR(btf))
@@ -1165,6 +1384,8 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
goto errout_free;
}
st_map->btf = btf;
+ st_map->local_btf = local_btf;
+ st_map->local_type = local_type;
mutex_init(&st_map->lock);
bpf_map_init_from_attr(map, attr);
@@ -1180,6 +1401,7 @@ static struct bpf_map *bpf_struct_ops_map_alloc(union bpf_attr *attr)
__bpf_struct_ops_map_free(map);
errout:
module_put(mod);
+ btf_put(local_btf);
return ERR_PTR(ret);
}
diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 198438b95849..3a8c25285236 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -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) {
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 28904c6f0787..5d44bce55d5e 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -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 */
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF
2026-11-30 21:44 ` [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF Siddharth Nayyar
@ 2026-11-30 22:05 ` sashiko-bot
0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-30 22:05 UTC (permalink / raw)
To: Siddharth Nayyar; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: out-of-bounds heap read in local_to_kern_value() on
crafted local BTF
- [Medium] bpf: unlocked struct_ops_tab access in
find_desc_by_local_type()
--
commit 358556cdd1778df7a46e8f700b565dd624b3828f
Author: Siddharth Nayyar <sidnayyar@google.com>
bpf: Support struct_ops with object BTF
This commit allows BPF_MAP_TYPE_STRUCT_OPS maps and BPF_PROG_TYPE_STRUCT_OPS
programs to specify their target struct and members using the BPF object's
own BTF, resolving the corresponding kernel struct_ops type and members by
name.
> diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
> index 3d334cd526c03..96eca8493892a 100644
> --- a/kernel/bpf/bpf_struct_ops.c
> +++ b/kernel/bpf/bpf_struct_ops.c
[ ... ]
> @@ -729,8 +736,152 @@ static void bpf_struct_ops_map_free_ksyms(struct bpf_struct_ops_map *st_map)
[ ... ]
> +/*
> + * 'id' is a local struct, e.g. tcp_congestion_ops or tcp_congestion_ops___v2.
> + * Find bpf_struct_ops_tcp_congestion_ops in the BTF of vmlinux or of a module.
> + * A module can have it for a struct of vmlinux. The caller does btf_put(*kern_btf).
> + */
> +static const struct bpf_struct_ops_desc *
> +find_desc_by_local_type(const struct btf *btf, u32 id, struct btf **kern_btf)
> +{
> + const struct btf_type *t = btf_type_by_id(btf, id);
> + const struct bpf_struct_ops_desc *st_ops_desc;
> + char name[KSYM_NAME_LEN];
> + const char *tname;
> + s32 value_id;
> +
> + if (!t || !__btf_type_is_struct(t))
> + return ERR_PTR(-EINVAL);
> + tname = btf_name_by_offset(btf, t->name_off);
> + if (snprintf(name, sizeof(name), VALUE_PREFIX "%.*s",
> + (int)bpf_core_essential_name_len(tname), tname) >= sizeof(name))
> + return ERR_PTR(-EINVAL);
> + value_id = bpf_find_btf_id(name, BTF_KIND_STRUCT, kern_btf);
> + if (value_id < 0)
> + return ERR_PTR(value_id);
> + st_ops_desc = bpf_struct_ops_find_value(*kern_btf, value_id);
> + if (!st_ops_desc) {
> + btf_put(*kern_btf);
> + return ERR_PTR(-EOPNOTSUPP);
> + }
> + return st_ops_desc;
> +}
> +
> +/*
> + * attach_btf_id is a local struct and expected_attach_type is the index of its
> + * member. Replace them, and the BTF, with what the kernel has under the same names.
> + */
> +int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr)
> +{
> + const struct bpf_struct_ops_desc *st_ops_desc;
> + const struct btf_member *member;
> + const struct btf_type *t;
> + struct btf *kern_btf;
> +
> + st_ops_desc = find_desc_by_local_type(*btf, attr->attach_btf_id, &kern_btf);
> + if (IS_ERR(st_ops_desc))
> + return PTR_ERR(st_ops_desc);
> + t = btf_type_by_id(*btf, attr->attach_btf_id);
> + if (attr->expected_attach_type >= btf_type_vlen(t)) {
> + btf_put(kern_btf);
> + return -EINVAL;
> + }
> + member = btf_type_member(t) + attr->expected_attach_type;
> + member = find_member(kern_btf, st_ops_desc->type,
> + btf_name_by_offset(*btf, member->name_off));
[Severity: Medium]
Can find_desc_by_local_type() race with btf_add_struct_ops() and trigger a
use-after-free on (*kern_btf)->struct_ops_tab?
When a module loads, btf_module_notify() publishes the module BTF in
btf_idr on MODULE_STATE_COMING before the module's init function runs and
before BTF_MODULE_F_LIVE is set on MODULE_STATE_LIVE. While init runs,
register_bpf_struct_ops() calls btf_add_struct_ops(), which reallocates
btf->struct_ops_tab via krealloc() once tab->cnt reaches tab->capacity:
CPU0 CPU1
module_init()
register_bpf_struct_ops()
btf_add_struct_ops()
bpf_struct_ops_resolve_attach()
find_desc_by_local_type()
bpf_find_btf_id()
bpf_struct_ops_find_value()
krealloc(btf->struct_ops_tab)
find_member(..., st_ops_desc->type)
<-- use-after-free
Existing struct_ops paths (such as check_struct_ops_btf_id() and the
BPF_F_VTYPE_BTF_OBJ_FD branch in bpf_struct_ops_map_alloc()) guard against
this by calling btf_try_get_module() to verify BTF_MODULE_F_LIVE before
calling bpf_struct_ops_find() or bpf_struct_ops_find_value(). Here,
bpf_struct_ops_resolve_attach() never calls btf_try_get_module(), and
local_to_kern_attr() calls it only after find_desc_by_local_type() has
already searched struct_ops_tab and cached st_ops_desc.
> + if (!member) {
> + btf_put(kern_btf);
> + return -ENOENT;
> + }
> + attr->attach_btf_id = st_ops_desc->type_id;
> + attr->expected_attach_type = member - btf_type_member(st_ops_desc->type);
> + btf_put(*btf);
> + *btf = kern_btf;
> + return 0;
> +}
> +
> +u32 bpf_struct_ops_map_update_size(const struct bpf_map *map)
> +{
> + const struct bpf_struct_ops_map *st_map = (const struct bpf_struct_ops_map *)map;
> +
> + return st_map->local_type ? st_map->local_type->size : map->value_size;
> +}
> +
> +/*
> + * 'data' has the layout of the local struct. Copy every member to the place
> + * of the member with the same name in the struct of the kernel.
> + */
> +static void *local_to_kern_value(const struct bpf_struct_ops_map *st_map, const void *data)
> +{
> + const struct btf_type *t = st_map->local_type, *kern_t = st_map->st_ops_desc->type;
> + const struct btf *btf = st_map->local_btf, *kern_btf = st_map->btf;
> + const struct btf_member *member, *kern_member;
> + const struct btf_type *mtype, *kern_mtype;
> + struct bpf_struct_ops_value *value;
> + u32 i, size, kern_size;
> + int err = -EINVAL;
> +
> + value = kvzalloc(st_map->map.value_size, GFP_USER);
> + if (!value)
> + return ERR_PTR(-ENOMEM);
> +
> + for_each_member(i, t, member) {
> + const void *src = data + __btf_member_bit_offset(t, member) / 8;
> +
> + if (__btf_member_bitfield_size(t, member) ||
> + IS_ERR(btf_resolve_size(btf, btf_type_by_id(btf, member->type), &size)))
> + goto err;
> + kern_member = find_member(kern_btf, kern_t,
> + btf_name_by_offset(btf, member->name_off));
> + if (!kern_member) {
> + /* This kernel doesn't have the member. That's fine when it's not in use. */
> + if (!memchr_inv(src, 0, size))
> + continue;
> + err = -ENOENT;
> + goto err;
> + }
> + if (__btf_member_bitfield_size(kern_t, kern_member) ||
> + IS_ERR(btf_resolve_size(kern_btf,
> + btf_type_by_id(kern_btf, kern_member->type),
> + &kern_size)) ||
> + size != kern_size)
> + goto err;
> + mtype = btf_type_skip_modifiers(btf, member->type, NULL);
> + kern_mtype = btf_type_skip_modifiers(kern_btf, kern_member->type, NULL);
> + if (BTF_INFO_KIND(mtype->info) != BTF_INFO_KIND(kern_mtype->info))
> + goto err;
> + if (btf_type_is_ptr(mtype) &&
> + !!btf_type_resolve_func_ptr(btf, member->type, NULL) !=
> + !!btf_type_resolve_func_ptr(kern_btf, kern_member->type, NULL))
> + goto err;
> + memcpy(value->data + __btf_member_bit_offset(kern_t, kern_member) / 8, src, size);
> + }
> + err = check_zero_holes(btf, t, data);
[Severity: High]
Can a crafted local BTF cause local_to_kern_value() to read past the end of
data?
map_update_elem() allocates data with size st_map->local_type->size
(t->size). Unlike kernel BTF, a user-loaded BTF validated by BPF_BTF_LOAD
allows a BTF_KIND_INT member where member_type->size exceeds the remaining
bytes in t->size: btf_int_check_meta() only requires
BITS_ROUNDUP_BYTES(nr_bits) <= t->size, and btf_int_check_member() checks
struct_size - bytes_offset >= BITS_ROUNDUP_BYTES(nr_copy_bits) rather than
member_type->size, while btf_resolve_size() returns size =
member_type->size. Because neither local_to_kern_value() nor
check_zero_holes() verifies that moff + size <= t->size, memchr_inv(src, 0,
size) on an absent member (or memcpy() on a matched member) can read out of
bounds on the kernel heap.
Also, __btf_member_bitfield_size(t, member) returns 0 whenever
!btf_type_kflag(t), so non-kflag bitfields and non-byte-aligned bit offsets
(__btf_member_bit_offset(t, member) % 8 != 0) are not rejected. And
because btf_struct_check_meta() only checks that member offsets are
non-decreasing rather than non-overlapping (moff >= prev_mend), overlapping
members in t can move prev_mend backwards in check_zero_holes() and bypass
padding hole checks.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261130-lskel-struct-ops-v2-0-8b76e65073ab@google.com?part=1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next v2 2/4] libbpf: Support struct_ops in light skeleton loader
2026-11-30 21:44 [PATCH bpf-next v2 0/4] bpf: Support struct_ops in light skeletons Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF Siddharth Nayyar
@ 2026-11-30 21:44 ` Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 4/4] selftests/bpf: Add light skeleton struct_ops tests Siddharth Nayyar
3 siblings, 0 replies; 6+ messages in thread
From: Siddharth Nayyar @ 2026-11-30 21:44 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Quentin Monnet, Shuah Khan
Cc: bpf, linux-kernel, linux-kselftest, Anton Protopopov,
Giuliano Procida, Neill Kapron, Thiébaud Weksteen,
Maxime Ripard, Siddharth Nayyar, Alexei Starovoitov
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.
Suggested-by: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
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(-)
diff --git a/tools/lib/bpf/bpf_gen_internal.h b/tools/lib/bpf/bpf_gen_internal.h
index c30e05b66abc..6fc3eb02f173 100644
--- a/tools/lib/bpf/bpf_gen_internal.h
+++ b/tools/lib/bpf/bpf_gen_internal.h
@@ -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,
diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c
index 27b4bc9e55ac..749a86c43e7f 100644
--- a/tools/lib/bpf/gen_loader.c
+++ b/tools/lib/bpf/gen_loader.c
@@ -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)
{
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 61a388868373..e66e848eb463 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1459,6 +1459,63 @@ static int bpf_map__init_kern_struct_ops(struct bpf_map *map)
return 0;
}
+/*
+ * The kernel finds its struct_ops and the members by the names that they have
+ * in obj->btf.
+ */
+static int bpf_map__init_gen_struct_ops(struct bpf_map *map)
+{
+ const struct btf *btf = map->obj->btf;
+ struct bpf_struct_ops *st_ops = map->st_ops;
+ const struct btf_type *type = btf__type_by_id(btf, st_ops->type_id);
+ const struct btf_member *member = btf_members(type);
+ __u32 i, vlen = btf_vlen(type);
+
+ map->mod_btf_fd = -1;
+ for (i = 0; i < vlen; i++, member++) {
+ const struct btf_type *mtype;
+ struct bpf_program *prog;
+ const char *mname;
+ __u32 moff;
+
+ mname = btf__name_by_offset(btf, member->name_off);
+ if (btf_member_bitfield_size(type, i)) {
+ pr_warn("struct_ops init_kern %s: local bitfield %s is not supported\n",
+ map->name, mname);
+ return -ENOTSUP;
+ }
+
+ mtype = skip_mods_and_typedefs(btf, member->type, NULL);
+ if (!btf_is_ptr(mtype))
+ continue;
+ mtype = skip_mods_and_typedefs(btf, mtype->type, NULL);
+ if (!btf_is_func_proto(mtype))
+ continue;
+
+ moff = btf_member_bit_offset(type, i) / 8;
+ prog = *(void **)(st_ops->data + moff);
+ if (st_ops->progs[i] && st_ops->progs[i] != prog)
+ st_ops->progs[i]->load_strategy = BPF_PROG_LOAD_STRATEGY_DISABLED;
+ st_ops->progs[i] = prog;
+ if (!prog)
+ continue;
+
+ if (!is_valid_st_ops_program(map->obj, prog)) {
+ pr_warn("struct_ops init_kern %s: member %s is not a struct_ops program\n",
+ map->name, mname);
+ return -ENOTSUP;
+ }
+
+ /* The kernel rejects a prog that two maps use for different members */
+ if (prog->attach_btf_id)
+ continue;
+ prog->attach_btf_id = st_ops->type_id;
+ prog->expected_attach_type = i;
+ }
+
+ return 0;
+}
+
static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
{
struct bpf_map *map;
@@ -1474,7 +1531,10 @@ static int bpf_object__init_kern_struct_ops_maps(struct bpf_object *obj)
if (!map->autocreate)
continue;
- err = bpf_map__init_kern_struct_ops(map);
+ if (obj->gen_loader)
+ err = bpf_map__init_gen_struct_ops(map);
+ else
+ err = bpf_map__init_kern_struct_ops(map);
if (err)
return err;
}
@@ -5615,7 +5675,8 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b
map->btf_value_type_id = 0;
break;
case BPF_MAP_TYPE_STRUCT_OPS:
- create_attr.btf_value_type_id = 0;
+ if (!obj->gen_loader)
+ create_attr.btf_value_type_id = 0;
break;
default:
break;
@@ -9767,6 +9828,46 @@ static int bpf_map_prepare_vdata(const struct bpf_map *map)
return 0;
}
+/* The loader keeps the fds of the programs in the order of bpf_object__load_progs() */
+static int bpf_program_gen_idx(const struct bpf_program *prog)
+{
+ const struct bpf_program *p;
+ int idx = 0;
+
+ for (p = prog->obj->programs; p != prog; p++)
+ if (!prog_is_subprog(prog->obj, p) &&
+ p->load_strategy == BPF_PROG_LOAD_STRATEGY_AUTO)
+ idx++;
+ return idx;
+}
+
+static int bpf_map_gen_struct_ops_update(struct bpf_map *map)
+{
+ struct bpf_struct_ops *st_ops = map->st_ops;
+ __u32 i, cnt = 0, *fd_offs = st_ops->kern_func_off;
+ struct bpf_object *obj = map->obj;
+ const struct btf_type *type;
+ int *prog_idxs;
+
+ type = btf__type_by_id(obj->btf, st_ops->type_id);
+ prog_idxs = calloc(btf_vlen(type), sizeof(*prog_idxs));
+ if (!prog_idxs)
+ return -ENOMEM;
+
+ for (i = 0; i < btf_vlen(type); i++) {
+ if (!st_ops->progs[i])
+ continue;
+ fd_offs[cnt] = btf_member_bit_offset(type, i) / 8;
+ /* struct bpf_program * doesn't go into the blob. The loader stores the fd here. */
+ memset(st_ops->data + fd_offs[cnt], 0, sizeof(__u64));
+ prog_idxs[cnt++] = bpf_program_gen_idx(st_ops->progs[i]);
+ }
+ bpf_gen__struct_ops_update_elem(obj->gen_loader, map - obj->maps, st_ops->data,
+ type->size, fd_offs, prog_idxs, cnt);
+ free(prog_idxs);
+ return 0;
+}
+
static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
{
struct bpf_map *map;
@@ -9781,7 +9882,10 @@ static int bpf_object_prepare_struct_ops(struct bpf_object *obj)
if (!map->autocreate)
continue;
- err = bpf_map_prepare_vdata(map);
+ if (obj->gen_loader)
+ err = bpf_map_gen_struct_ops_update(map);
+ else
+ err = bpf_map_prepare_vdata(map);
if (err)
return err;
}
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH bpf-next v2 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons
2026-11-30 21:44 [PATCH bpf-next v2 0/4] bpf: Support struct_ops in light skeletons Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 1/4] bpf: Support struct_ops with object BTF Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 2/4] libbpf: Support struct_ops in light skeleton loader Siddharth Nayyar
@ 2026-11-30 21:44 ` Siddharth Nayyar
2026-11-30 21:44 ` [PATCH bpf-next v2 4/4] selftests/bpf: Add light skeleton struct_ops tests Siddharth Nayyar
3 siblings, 0 replies; 6+ messages in thread
From: Siddharth Nayyar @ 2026-11-30 21:44 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Quentin Monnet, Shuah Khan
Cc: bpf, linux-kernel, linux-kselftest, Anton Protopopov,
Giuliano Procida, Neill Kapron, Thiébaud Weksteen,
Maxime Ripard, 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.
Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
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
^ permalink raw reply related [flat|nested] 6+ messages in thread* [PATCH bpf-next v2 4/4] selftests/bpf: Add light skeleton struct_ops tests
2026-11-30 21:44 [PATCH bpf-next v2 0/4] bpf: Support struct_ops in light skeletons Siddharth Nayyar
` (2 preceding siblings ...)
2026-11-30 21:44 ` [PATCH bpf-next v2 3/4] bpftool: Generate attach and detach for struct_ops in light skeletons Siddharth Nayyar
@ 2026-11-30 21:44 ` Siddharth Nayyar
3 siblings, 0 replies; 6+ messages in thread
From: Siddharth Nayyar @ 2026-11-30 21:44 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Quentin Monnet, Shuah Khan
Cc: bpf, linux-kernel, linux-kselftest, Anton Protopopov,
Giuliano Procida, Neill Kapron, Thiébaud Weksteen,
Maxime Ripard, 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 and a
shared callback), testing kernel-side object-BTF resolution, scalar
member copying, detach, and per-map re-attachment.
Signed-off-by: Siddharth Nayyar <sidnayyar@google.com>
---
tools/testing/selftests/bpf/Makefile.skel | 3 +-
.../selftests/bpf/prog_tests/struct_ops_lskel.c | 122 +++++++++++++++++++++
tools/testing/selftests/bpf/progs/struct_ops_mod.c | 52 +++++++++
.../selftests/bpf/progs/struct_ops_vmlinux.c | 42 +++++++
4 files changed, 218 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/bpf/Makefile.skel b/tools/testing/selftests/bpf/Makefile.skel
index 45e4ce5dca4d..334342928d01 100644
--- a/tools/testing/selftests/bpf/Makefile.skel
+++ b/tools/testing/selftests/bpf/Makefile.skel
@@ -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
diff --git a/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
new file mode 100644
index 000000000000..588d9223bf81
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/struct_ops_lskel.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "network_helpers.h"
+#include "struct_ops_vmlinux.lskel.h"
+#include "struct_ops_mod.lskel.h"
+
+static int set_tcp_ca_cb(int fd, void *opts)
+{
+ const char *tcp_ca = opts;
+
+ if (!ASSERT_OK(setsockopt(fd, IPPROTO_TCP, TCP_CONGESTION,
+ tcp_ca, strlen(tcp_ca)), "setsockopt"))
+ return -1;
+ return 0;
+}
+
+static void test_vmlinux(void)
+{
+ char tcp_ca[] = "bpf_lskel_ca";
+ struct network_helper_opts opts = {
+ .post_socket_cb = set_tcp_ca_cb,
+ .cb_opts = tcp_ca,
+ };
+ struct struct_ops_vmlinux_lskel *skel;
+ int srv_fd = -1, cli_fd = -1, sock_fd = -1;
+ int err;
+
+ skel = struct_ops_vmlinux_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ err = struct_ops_vmlinux_lskel__attach(skel);
+ if (!ASSERT_OK(err, "attach"))
+ goto cleanup;
+
+ srv_fd = start_server_str(AF_INET6, SOCK_STREAM, NULL, 0, &opts);
+ if (!ASSERT_NEQ(srv_fd, -1, "start_server_str"))
+ goto cleanup;
+
+ cli_fd = connect_to_fd_opts(srv_fd, &opts);
+ if (!ASSERT_NEQ(cli_fd, -1, "connect_to_fd_opts"))
+ goto cleanup;
+
+ ASSERT_GT(skel->bss->ca_init_cnt, 0, "ca_init_cnt");
+
+ close(cli_fd);
+ cli_fd = -1;
+ close(srv_fd);
+ srv_fd = -1;
+
+ struct_ops_vmlinux_lskel__detach(skel);
+ ASSERT_EQ(skel->links.lskel_ca_fd, 0, "lskel_ca_fd_detached");
+
+ /* Verify congestion control was unregistered on detach */
+ sock_fd = socket(AF_INET6, SOCK_STREAM, 0);
+ if (!ASSERT_GE(sock_fd, 0, "socket"))
+ goto cleanup;
+
+ err = setsockopt(sock_fd, IPPROTO_TCP, TCP_CONGESTION,
+ tcp_ca, strlen(tcp_ca));
+ ASSERT_EQ(err, -1, "setsockopt_after_detach");
+ ASSERT_EQ(errno, ENOENT, "errno_after_detach");
+
+cleanup:
+ if (sock_fd != -1)
+ close(sock_fd);
+ if (cli_fd != -1)
+ close(cli_fd);
+ if (srv_fd != -1)
+ close(srv_fd);
+ struct_ops_vmlinux_lskel__destroy(skel);
+}
+
+static void test_module(void)
+{
+ struct struct_ops_mod_lskel *skel;
+ int err, link_fd;
+
+ if (!env.has_testmod) {
+ test__skip();
+ return;
+ }
+
+ skel = struct_ops_mod_lskel__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ err = struct_ops_mod_lskel__attach(skel);
+ if (!ASSERT_OK(err, "attach"))
+ goto cleanup;
+
+ ASSERT_GT(skel->links.testmod_1_fd, 0, "testmod_1_fd");
+ ASSERT_GT(skel->links.testmod_2_fd, 0, "testmod_2_fd");
+ ASSERT_EQ(skel->bss->test_1_cnt, 2, "test_1_cnt");
+ ASSERT_EQ(skel->bss->testmod_1_result, 17, "testmod_1_result");
+ ASSERT_EQ(skel->bss->testmod_2_result, 28, "testmod_2_result");
+
+ struct_ops_mod_lskel__detach(skel);
+ ASSERT_EQ(skel->links.testmod_1_fd, 0, "testmod_1_fd_detached");
+ ASSERT_EQ(skel->links.testmod_2_fd, 0, "testmod_2_fd_detached");
+ memset(skel->bss, 0, sizeof(*skel->bss));
+
+ link_fd = struct_ops_mod_lskel__testmod_1__attach(skel);
+ if (!ASSERT_GT(link_fd, 0, "testmod_1_attach"))
+ goto cleanup;
+
+ ASSERT_EQ(skel->bss->test_1_cnt, 1, "reattach_test_1_cnt");
+ ASSERT_EQ(skel->bss->testmod_1_result, 17, "reattach_testmod_1_result");
+ ASSERT_EQ(skel->bss->testmod_2_result, 0, "reattach_testmod_2_result");
+
+cleanup:
+ struct_ops_mod_lskel__destroy(skel);
+}
+
+void test_struct_ops_lskel(void)
+{
+ if (test__start_subtest("vmlinux"))
+ test_vmlinux();
+ if (test__start_subtest("module"))
+ test_module();
+}
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_mod.c b/tools/testing/selftests/bpf/progs/struct_ops_mod.c
new file mode 100644
index 000000000000..1d22267baa6b
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_mod.c
@@ -0,0 +1,52 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../test_kmods/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+int test_1_cnt = 0;
+int testmod_1_result = 0;
+int testmod_2_result = 0;
+
+SEC("struct_ops/test_1")
+int BPF_PROG(test_1)
+{
+ test_1_cnt++;
+ return 0;
+}
+
+SEC("struct_ops/test_2")
+void BPF_PROG(test_2, int a, int b)
+{
+ testmod_1_result = a + b;
+}
+
+SEC("struct_ops/test_2_v2")
+void BPF_PROG(test_2_v2, int a, int b)
+{
+ testmod_2_result = a * b;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+ .test_1 = (void *)test_1,
+ .test_2 = (void *)test_2,
+ .data = 13,
+};
+
+struct bpf_testmod_ops___v2 {
+ int data;
+ int zeroed_missing;
+ void (*test_2)(int a, int b);
+ int (*test_1)(void);
+};
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops___v2 testmod_2 = {
+ .data = 7,
+ .test_2 = (void *)test_2_v2,
+ .test_1 = (void *)test_1,
+};
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c b/tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c
new file mode 100644
index 000000000000..1d8b573ad799
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/struct_ops_vmlinux.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "bpf_tracing_net.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+int ca_init_cnt = 0;
+
+SEC("struct_ops/ca_lskel_init")
+void BPF_PROG(ca_lskel_init, struct sock *sk)
+{
+ ca_init_cnt++;
+}
+
+SEC("struct_ops/ca_lskel_cong_control")
+void BPF_PROG(ca_lskel_cong_control, struct sock *sk, __u32 ack, int flag,
+ const struct rate_sample *rs)
+{
+}
+
+SEC("struct_ops/ca_lskel_ssthresh")
+__u32 BPF_PROG(ca_lskel_ssthresh, struct sock *sk)
+{
+ return tcp_sk(sk)->snd_ssthresh;
+}
+
+SEC("struct_ops/ca_lskel_undo_cwnd")
+__u32 BPF_PROG(ca_lskel_undo_cwnd, struct sock *sk)
+{
+ return tcp_sk(sk)->snd_cwnd;
+}
+
+SEC(".struct_ops.link")
+struct tcp_congestion_ops lskel_ca = {
+ .init = (void *)ca_lskel_init,
+ .cong_control = (void *)ca_lskel_cong_control,
+ .ssthresh = (void *)ca_lskel_ssthresh,
+ .undo_cwnd = (void *)ca_lskel_undo_cwnd,
+ .name = "bpf_lskel_ca",
+};
--
2.56.1.839.g22a8290db9-goog
^ permalink raw reply related [flat|nested] 6+ messages in thread