Re: [PATCH bpf-next v2 3/6] libbpf: Resolve kfunc variants by prototype

From: bot+bpf-ci

Date: Fri Nov 20 2026 - 20:14:16 EST


> libbpf: Resolve kfunc variants by prototype

TL;DR: A module BTF load error in find_kfunc_btf_id() may be returned as
-EINVAL and mistaken for "no compatible prototype", so a weak kfunc in vmlinux
could be treated as missing and strong ones get a misleading error.

> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c

[ ... ]

> +/*
> + * Search vmlinux and module BTFs. Return:
> + * - a BTF ID for a unique match;
> + * - -ESRCH if the family is absent;
> + * - -EINVAL if no candidate has a compatible prototype;
> + * - -ENOTUNIQ if more than one candidate has a compatible prototype;
> + * - another negative error on failure.
> + */
> +static int find_kfunc_btf_id(struct bpf_object *obj, const struct extern_desc *ext,
> + struct btf **res_btf, struct module_btf **res_mod_btf)
> +{
> + size_t name_len = bpf_core_essential_name_len(ext->name);
> + int kfunc_id = 0, nr_cands = 0, err;
> + size_t i;
> +
> + err = load_module_btfs(obj);
> + if (err)
> + return err;

Can this return -EINVAL for a reason other than a prototype mismatch?

load_module_btfs() calls btf_get_from_fd(), which goes through btf_new()
and can return -EINVAL from btf_parse_hdr(), btf_parse_type_sec() or
btf_sanity_check() when libbpf rejects a module's BTF. That error is
returned here unchanged.

[ ... ]

> + if (kfunc_id == -EINVAL) {
> if (ext->is_weak)
> return 0;
> + pr_warn("extern (func ksym) '%s': no compatible prototype in kernel or module BTFs\n",
> + ext->name);
> + return kfunc_id;
> }

bpf_object__resolve_ksym_func_btf_id() treats that -EINVAL as "no
compatible prototype". A __weak kfunc is then left unresolved even if it
exists in vmlinux, so bpf_ksym_exists() reports it as missing. A strong
kfunc fails with the misleading "no compatible prototype" message.

Also, load_module_btfs() has already set obj->btf_modules_loaded = true by
then, so later kfuncs and the CO-RE lookups in bpf_core_find_cands() would
continue with a partial module list instead of failing the load.

Before this patch, a kfunc found in vmlinux never called
load_module_btfs(). When the lookup did reach it, find_ksym_btf_id()
returned the error as is, and only -ESRCH was treated as absence.

Other errors from load_module_btfs() (-ENOMEM, errors from the BTF
syscalls) also return here without a message naming the extern, because
the generic "if (kfunc_id < 0) return kfunc_id;" path prints nothing.

Would it make sense to call load_module_btfs() in the caller, or to keep
its error apart from the -ESRCH/-EINVAL/-ENOTUNIQ results?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/44845964556