BPF List
 help / color / mirror / Atom feed
From: Ihor Solodrai <ihor.solodrai@linux.dev>
To: Eduard Zingerman <eddyz87@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>
Cc: Alan Maguire <alan.maguire@oracle.com>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Jiri Olsa <jolsa@kernel.org>, Song Liu <song@kernel.org>,
	Tejun Heo <tj@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	sched-ext@lists.linux.dev
Subject: Re: [PATCH bpf-next v3 3/6] libbpf: Resolve kfunc variants by prototype
Date: Thu, 3 Dec 2026 21:51:55 -0700	[thread overview]
Message-ID: <fe08250b-fd33-4bca-91de-f5307da63e01@linux.dev> (raw)
In-Reply-To: <a5ece3a1e01d2557b4b39fc89670df677e4f3b0f.camel@gmail.com>

On 2026-11-25 1:28 p.m., Eduard Zingerman wrote:
> 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@linux.dev>
>> ---
> 
> I have no nits besides what Andrii points out.

Hi Eduard. Thank you for the review.

> 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.

Did that after I saw your comment (and had a chance).

IMO TL;DR given our prior discussions is that we can accept the
breakages. An updated (or additional, depending on what kernels BPF
program tries to cover) kfunc declaration on BPF side fixes them.

Sloppy prose with the details follows. I think this is useful to have
on record for the bots and future reference.

I answered your other questions as well, scroll please :)

   <ai-prose-trimmed-and-edited-lightly>

A bot replayed libbpf's kfunc resolution for every corpus [1] object
against the BTF of kernels v6.1, v6.6, v6.8, v6.10, v6.12, v6.18, v7.2
and bpf-next. The corpus builds multiple variants of some progs
(different compilers etc), giving 672 total BPF objects that declare
kfuncs.

With this series (v3), 92 of them (14%) have a declaration that no
longer resolves on at least one of the tested kernels, for 19 distinct
kfuncs. Outside the kernel tree, that's 22 objects in 4 projects: 18
scx schedulers, bpfjailer (2), libbpf-rs (1) and bpf-tutorial (1).

Of these objects, only bpfjailer's verity_enforce declares kfuncs
without __weak (bpf_verify_pkcs7_signature() and
bpf_get_fsverity_digest()), so it fails to load on v6.1 to v6.10.  The
other declarations are __weak and stay unresolved. If the call is
guarded by bpf_ksym_exists(), the program takes its fallback path;
otherwise it fails to load.

                                          kfuncs  objects  kernels
   kernel types before v6.11                  15       43  v6.1-v6.10
     BPF selftests                            15       39
     bpfjailer, libbpf-rs, bpf-tutorial        5        4
   stacked ___v2___compat names                3       46  v7.2, bpf-next
     tools/ and selftests/sched_ext            2       28
     scx repo                                  3       18
   bpf_cpumask_populate()                      1        3  v6.18, v7.2
     BPF selftests                             1        3

This is what the CI bot reported [2]. Commit cce4c40b9606 ("bpf:
treewide: Align kfunc signatures to prog point-of-view") changed these
kfuncs in v6.11 from kernel types to the UAPI types that programs
pass. A sample, with the v6.10 prototype and the number of objects:

   bpf_dynptr_from_skb(struct sk_buff *, ...)                 25
   bpf_dynptr_slice(const struct bpf_dynptr_kern *, ...)      23
   bpf_dynptr_from_xdp(struct xdp_buff *, ...)                 9
   bpf_get_file_xattr(..., struct bpf_dynptr_kern *)           4
   bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *, ...)   4
   bpf_sk_assign_tcp_reqsk(struct sk_buff *, ...)              1

The other 9 are dynptr and crypto kfuncs taking struct bpf_dynptr_kern.

With this series, such a declaration doesn't resolve on v6.1 to
v6.10. No single declaration resolves on both sides of v6.11. A
program that has to run on both sides can declare both prototypes and
choose in the prog:

   extern int bpf_dynptr_from_skb(struct __sk_buff *, __u64,
           struct bpf_dynptr *) __weak __ksym;
   extern int bpf_dynptr_from_skb___old(struct sk_buff *, __u64,
           struct bpf_dynptr_kern *) __weak __ksym;

   if (bpf_ksym_exists(bpf_dynptr_from_skb))
           bpf_dynptr_from_skb(skb, 0, &ptr);
   else
           bpf_dynptr_from_skb___old((void *)skb, 0, (void *)&ptr);

Re: scx_bpf_dsq_insert___v2___compat.

With this series its essential name is scx_bpf_dsq_insert___v2, while
the kernel's scx_bpf_dsq_insert___v2 is a variant of
scx_bpf_dsq_insert. So it stays unresolved, and compat.bpf.h falls
back to scx_bpf_dsq_insert(). Patch 2 makes compat.bpf.h try
scx_bpf_dsq_insert___v2 first; the scx repo needs the same change.

[1] https://github.com/4ast/bpf-corpus
[2] 
https://lore.kernel.org/bpf/bdaa5b918a0e59230fcce839fea102b598efeb3a65de54f668c15f29573370df@mail.kernel.org/

   </ai-prose-trimmed-and-edited-lightly>


> 
> ...
> 
>> +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?

bpf_core_types_are_compat() rejects int vs enum too, I'd leave it
consistent. No declaration in the corpus is affected by this.

> 
>> +
>> +	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?

I don't think we care.

bpf_core_types_are_compat() does the same: integers of any size and
signedness are compatible, also behind a pointer.

The verifier looks at the size based on the kernel prototype, so a
byte buffer passed to int * will fail to load.  And resolve_btfids
check between variants will catch this as well.

See get_kfunc_arg_type() (or check_kfunc_args() on <=7.2).

> 
>> [...]

  reply	other threads:[~2026-12-04  4:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 22:48 [PATCH bpf-next v3 0/6] libbpf: Support kernel-side kfunc variants Ihor Solodrai
2026-11-24 22:48 ` [PATCH bpf-next v3 1/6] libbpf: Separate interfaces shared with in-tree tools Ihor Solodrai
2026-11-24 22:50   ` Ihor Solodrai
2026-11-24 22:53     ` Andrii Nakryiko
2026-11-24 23:40   ` bot+bpf-ci
2026-11-24 22:48 ` [PATCH bpf-next v3 2/6] sched_ext: Prefer ___v2 kfunc names in compat.bpf.h Ihor Solodrai
2026-11-24 22:48 ` [PATCH bpf-next v3 3/6] libbpf: Resolve kfunc variants by prototype Ihor Solodrai
2026-11-24 23:40   ` bot+bpf-ci
2026-11-25  1:22   ` Andrii Nakryiko
2026-11-25 20:28   ` Eduard Zingerman
2026-12-04  4:51     ` Ihor Solodrai [this message]
2026-11-24 22:48 ` [PATCH bpf-next v3 4/6] selftests/bpf: Test kfunc variant selection " Ihor Solodrai
2026-11-24 22:48 ` [PATCH bpf-next v3 5/6] resolve_btfids: Reject ambiguous kfunc variants Ihor Solodrai
2026-11-24 22:48 ` [PATCH bpf-next v3 6/6] docs/bpf: Describe how to change a kfunc's prototype Ihor Solodrai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fe08250b-fd33-4bca-91de-f5307da63e01@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=memxor@gmail.com \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox