Commit '259172bb6514 ("libbpf: Fix gen_loader assumption on number of programs.")' explicitly mention that we may pass a higher number of progs because we include subprogs and autoload=false to bpf_gen__init() generating a bigger cleanup section which is harmless. This is in preparation for the next commits that will introduce cleanup of the link_fds to bpf_gen__init(). We don't want to overallocate but have the correct count of progs in this situation so that the program-slot count matches the programs exposed by the skeleton. Signed-off-by: Mahe Tardy --- tools/lib/bpf/libbpf.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8fcb8cf39cc0..cd8ff7f42768 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -10077,7 +10077,7 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch * permit cross-endian creation of "light skeleton". */ if (obj->gen_loader) { - int nr_func_ptr_maps = 0, nr_progs = 0, i; + int nr_func_ptr_maps = 0, nr_progs = 0, nr_obj_progs = 0, i; bool text_has_callx = false; /* @@ -10092,12 +10092,16 @@ static int bpf_object_load(struct bpf_object *obj, int extra_log_level, const ch if (prog_is_subprog(obj, &obj->programs[i]) && prog_has_callx(&obj->programs[i])) text_has_callx = true; - for (i = 0; i < obj->nr_programs; i++) - if (obj->programs[i].autoload && !prog_is_subprog(obj, &obj->programs[i]) && + for (i = 0; i < obj->nr_programs; i++) { + if (prog_is_subprog(obj, &obj->programs[i])) + continue; + nr_obj_progs++; + if (obj->programs[i].autoload && (text_has_callx || prog_has_callx(&obj->programs[i]))) nr_progs++; + } bpf_gen__init(obj->gen_loader, obj->log_level | extra_log_level, - obj->nr_programs, obj->nr_maps, nr_func_ptr_maps * nr_progs); + nr_obj_progs, obj->nr_maps, nr_func_ptr_maps * nr_progs); } else if (!is_native_endianness(obj)) { pr_warn("object '%s': loading non-native endianness is unsupported\n", obj->name); return libbpf_err(-LIBBPF_ERRNO__ENDIAN); -- 2.43.0