Re: [PATCH bpf-next v3 3/6] libbpf: Resolve kfunc variants by prototype
From: Eduard Zingerman
Date: Wed Nov 25 2026 - 15:28:38 EST
On Tue, 2026-11-24 at 15:48 -0700, Ihor Solodrai wrote:
> libbpf resolves a kfunc declaration by exact name, after dropping its
> last ___ suffix, and only then checks the prototype. The name alone
> picks the kernel function, so the kernel can't serve both prototypes of
> a changed kfunc. For example, vmlinux.h declares:
>
> bool scx_bpf_dsq_move_to_local(u64 dsq_id);
> bool scx_bpf_dsq_move_to_local___v2(u64 dsq_id, u64 enq_flags);
>
> libbpf resolves the second to scx_bpf_dsq_move_to_local() and rejects
> its prototype. Keeping the old prototype as ___v1 and giving the new
> one the unsuffixed name would break old declarations the same way.
>
> Select a kfunc in two steps instead:
>
> 1. Collect the functions in vmlinux and module BTFs with the same
> essential name as the declaration, as CO-RE does for types. The
> "bpf_kfunc" decl tag is ignored.
>
> 2. Resolve the candidate with a compatible prototype. If there is
> none, a __weak kfunc stays unresolved. More than one is an error,
> even for a __weak kfunc.
>
> The prototype check is stricter than bpf_core_types_are_compat(). It
> also compares the names of structs, unions and forward declarations,
> ignoring ___ suffixes. Members aren't compared. Typedefs and qualifiers
> are skipped. For example:
>
> typedef struct foo foo_t;
>
> void kfunc(struct foo *arg);
> void kfunc(struct foo___local *arg);
> void kfunc(foo_t *arg);
>
> are compatible, but not with:
>
> void kfunc(struct bar *arg);
>
> So a declaration with an outdated struct name no longer resolves. For
> example, commit 8740156ad33b ("bpf: Require a BPF cpumask for
> bpf_cpumask_populate()") changed:
>
> before: int bpf_cpumask_populate(struct cpumask *, void *, size_t);
> after: int bpf_cpumask_populate(struct bpf_cpumask *, void *, size_t);
>
> Each declaration now resolves only on a kernel with the same prototype.
> A stacked suffix, as in foo___v2___compat, no longer resolves foo___v2,
> because its essential name is foo___v2. compat.bpf.h now tries foo___v2
> first.
>
> Module BTFs are now loaded for every object with kfuncs: 0.5 and 1.4 ms
> for 8 and 32 small modules in a VM. Lookup cost per kfunc:
>
> before (hit / miss) after
> sorted, 23 modules 0.14 / 0.65 us 0.8 us
> unsorted (v6.18) 0.1 / 0.5 ms 0.7 ms
>
> Signed-off-by: Ihor Solodrai <ihor.solodrai@xxxxxxxxx>
> ---
I have no nits besides what Andrii points out.
I'm curious if you tried this one the BPF programs corpus shared by Alexei,
to check how many real world programs are broken by this change.
...
> +static __u16 kfunc_type_kind(const struct btf_type *t)
> +{
> + if (btf_is_fwd(t))
> + return btf_kflag(t) ? BTF_KIND_UNION : BTF_KIND_STRUCT;
> + if (btf_is_enum64(t))
> + return BTF_KIND_ENUM;
> + return btf_kind(t);
> +}
> +
> +/*
> + * Return 1 if the types match, 0 if they don't, or -EINVAL if a type
> + * can't be looked up or the types nest deeper than depth.
> + */
> +static int kfunc_type_match(const struct btf *a, __u32 a_id,
> + const struct btf *b, __u32 b_id, int depth)
> +{
> + const struct btf_type *at = skip_mods_and_typedefs(a, a_id, NULL);
> + const struct btf_type *bt = skip_mods_and_typedefs(b, b_id, NULL);
> + __u16 kind;
> + int i, ret;
> +
> + if (!at || !bt || !depth)
> + return -EINVAL;
> +
> + kind = kfunc_type_kind(at);
> + if (kind != kfunc_type_kind(bt))
> + return 0;
This makes enums incompatible with int types, do we want an exception
for such a case?
> +
> + switch (kind) {
> + case BTF_KIND_STRUCT:
> + case BTF_KIND_UNION: {
> + const char *an = btf__name_by_offset(a, at->name_off);
> + const char *bn = btf__name_by_offset(b, bt->name_off);
> + size_t name_len = bpf_core_essential_name_len(an);
> +
> + return name_len == bpf_core_essential_name_len(bn) &&
> + strncmp(an, bn, name_len) == 0;
> + }
> + case BTF_KIND_UNKN:
> + case BTF_KIND_ENUM:
> + return 1;
> + case BTF_KIND_INT:
> + return btf_int_offset(at) == 0 && btf_int_offset(bt) == 0;
> + case BTF_KIND_PTR:
> + return kfunc_type_match(a, at->type, b, bt->type, depth - 1);
This makes `int *` compatible with e.g. `char *`, do we care?
> + case BTF_KIND_ARRAY:
> + return kfunc_type_match(a, btf_array(at)->type, b, btf_array(bt)->type, depth - 1);
> + case BTF_KIND_FUNC_PROTO:
> + if (btf_vlen(at) != btf_vlen(bt))
> + return 0;
> + for (i = 0; i < btf_vlen(at); i++) {
> + ret = kfunc_type_match(a, btf_params(at)[i].type,
> + b, btf_params(bt)[i].type, depth - 1);
> + if (ret <= 0)
> + return ret;
> + }
> + return kfunc_type_match(a, at->type, b, bt->type, depth - 1);
> + default:
> + return 0;
> + }
> +}
...