mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Ihor Solodrai <ihor.solodrai@linux.dev>,
	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: Wed, 25 Nov 2026 13:28:29 -0700	[thread overview]
Message-ID: <a5ece3a1e01d2557b4b39fc89670df677e4f3b0f.camel@gmail.com> (raw)
In-Reply-To: <20261124224811.16134-4-ihor.solodrai@linux.dev>

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.
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;
> +	}
> +}

...

  parent reply	other threads:[~2026-11-25 20:28 UTC|newest]

Thread overview: 13+ 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 [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=a5ece3a1e01d2557b4b39fc89670df677e4f3b0f.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=alan.maguire@oracle.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --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

all inboxes | Powered by JetHome®