[PATCH bpf-next v3 1/5] libbpf: Count entry programs for bpf_gen__init()

From: Mahe Tardy

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


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 <mahe.tardy@xxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 02e75b183ed0..a7cbe7204043 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -9800,7 +9800,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;

/*
@@ -9815,13 +9815,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++)
+ for (i = 0; i < obj->nr_programs; i++) {
+ if (prog_is_subprog(obj, &obj->programs[i]))
+ continue;
+ nr_obj_progs++;
if (obj->programs[i].load_strategy != BPF_PROG_LOAD_STRATEGY_DISABLED &&
- !prog_is_subprog(obj, &obj->programs[i]) &&
(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