During light skeleton generation (`bpftool gen skeleton -L`), libbpf runs in gen_loader mode. Previously, `bpf_object__load_vmlinux_btf()` completely bypassed loading the kernel vmlinux BTF (`obj->btf_vmlinux`) if `gen_loader` was active. However, BPF `struct_ops` maps (such as `sched_ext_ops` maps) require resolving the kernel-side struct type IDs and member sizes at compile/skeleton generation time. Without loading `btf_vmlinux`, libbpf cannot query the kernel BTF types, causing light skeleton generation for `struct_ops` to fail or omit crucial type information. Fix this by modifying the check to load `btf_vmlinux` even in `gen_loader` mode if the BPF object contains `struct_ops` maps. Signed-off-by: Siddharth Nayyar --- tools/lib/bpf/libbpf.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 3a80a018fc7d..b159faae7f9c 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -3544,15 +3544,20 @@ static bool prog_needs_vmlinux_btf(struct bpf_program *prog) return false; } -static bool map_needs_vmlinux_btf(struct bpf_map *map) +static bool obj_maps_need_vmlinux_btf(const struct bpf_object *obj) { - return bpf_map__is_struct_ops(map); + struct bpf_map *map; + + bpf_object__for_each_map(map, obj) { + if (bpf_map__is_struct_ops(map)) + return true; + } + return false; } static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) { struct bpf_program *prog; - struct bpf_map *map; int i; /* CO-RE relocations need kernel BTF, only when btf_custom_path @@ -3577,12 +3582,7 @@ static bool obj_needs_vmlinux_btf(const struct bpf_object *obj) return true; } - bpf_object__for_each_map(map, obj) { - if (map_needs_vmlinux_btf(map)) - return true; - } - - return false; + return obj_maps_need_vmlinux_btf(obj); } static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) @@ -3590,7 +3590,11 @@ static int bpf_object__load_vmlinux_btf(struct bpf_object *obj, bool force) int err; /* btf_vmlinux could be loaded earlier */ - if (obj->btf_vmlinux || obj->gen_loader) + if (obj->btf_vmlinux) + return 0; + + /* only struct_ops maps need btf_vmlinux in gen_loader */ + if (obj->gen_loader && !obj_maps_need_vmlinux_btf(obj)) return 0; if (!force && !obj_needs_vmlinux_btf(obj)) -- 2.54.0.746.g67dd491aae-goog For BPF `STRUCT_OPS` maps (such as `sched_ext_ops` maps), the kernel BPF subsystem enforces strict map-creation safety validations inside `map_create()`. That is, if `btf_vmlinux_value_type_id` is set, the kernel forbids passing any userspace `btf_key_type_id` or `btf_value_type_id` (they must both be `0`). However, inside libbpf's map-creation options initialization (`bpf_object__create_map()`), libbpf zeroed out `create_attr.btf_value_type_id` but does not zero out `create_attr.btf_key_type_id`. Fix this by explicitly zeroing out `create_attr.btf_key_type_id`. Signed-off-by: Siddharth Nayyar --- tools/lib/bpf/libbpf.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index b159faae7f9c..f9c653541005 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -5440,6 +5440,7 @@ 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_key_type_id = 0; create_attr.btf_value_type_id = 0; break; default: -- 2.54.0.746.g67dd491aae-goog BPF `STRUCT_OPS` maps (such as `sched_ext_ops` maps) require resolving and plumbing the kernel-side structure value type ID (`btf_vmlinux_value_type_id`) into the BPF map creation system call attributes. Additionally, when `btf_vmlinux_value_type_id` is supplied, the kernel requires a valid userspace BTF file descriptor (`btf_fd`) to be supplied to verify types. Previously, the `gen_loader` map creation generator (`bpf_gen__map_create()`) omitted plumbing `btf_vmlinux_value_type_id`. Furthermore, `gen_loader.c` only copied the loaded `btf_fd` from the stack to the attributes blob if `btf_value_type_id` was non-zero. Because `STRUCT_OPS` maps explicitly zero out `btf_value_type_id`, the loader program skipped copying `btf_fd`, leaving it as `0` (standard input), which caused the kernel's `btf_get_by_fd(0)` check to fail. Fix this by: 1. Copying `btf_vmlinux_value_type_id` from the options inside `bpf_gen__map_create()`. 2. Modifying the `btf_fd` copying condition to populate `btf_fd` if either `btf_value_type_id` OR `btf_vmlinux_value_type_id` is set. Signed-off-by: Siddharth Nayyar --- tools/lib/bpf/gen_loader.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/gen_loader.c b/tools/lib/bpf/gen_loader.c index cd5c2543f54d..a9be0c241025 100644 --- a/tools/lib/bpf/gen_loader.c +++ b/tools/lib/bpf/gen_loader.c @@ -525,13 +525,14 @@ void bpf_gen__map_create(struct bpf_gen *gen, attr.max_entries = tgt_endian(max_entries); attr.btf_key_type_id = tgt_endian(map_attr->btf_key_type_id); attr.btf_value_type_id = tgt_endian(map_attr->btf_value_type_id); + attr.btf_vmlinux_value_type_id = tgt_endian(map_attr->btf_vmlinux_value_type_id); map_create_attr = add_data(gen, &attr, attr_size); pr_debug("gen: map_create: %s idx %d type %d value_type_id %d, attr: off %d size %d\n", map_name, map_idx, map_type, map_attr->btf_value_type_id, map_create_attr, attr_size); - if (map_attr->btf_value_type_id) + if (map_attr->btf_value_type_id || map_attr->btf_vmlinux_value_type_id) /* populate union bpf_attr with btf_fd saved in the stack earlier */ move_stack2blob(gen, attr_field(map_create_attr, btf_fd), 4, stack_off(btf_fd)); -- 2.54.0.746.g67dd491aae-goog