BPF List
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>
Cc: Anton Protopopov <a.s.protopopov@gmail.com>,
	bpf@vger.kernel.org, Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	Viktor Malik <vmalik@redhat.com>
Subject: Re: [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function
Date: Sat, 5 Dec 2026 12:45:44 +0100	[thread overview]
Message-ID: <axP5aOcFUGIWjwzA@krava> (raw)
In-Reply-To: <DNCHP7VXXVZU.31FXPPNYLFNJ1@gmail.com>

On Sat, Dec 05, 2026 at 03:47:28AM +0000, Alexei Starovoitov wrote:
> On Fri, Dec 04, 2026 at 07:46 AM Anton Protopopov <a.s.protopopov@gmail.com> wrote:
> > And one case which worked before is now refused: a caller which
> > passes the vmlinux ID while the module has its own FUNC of a
> > compatible prototype, e.g. one with other parameter names, which
> > dedup doesn't merge. libbpf with patch 3 picks the module's own ID.
> > Other loaders have to do the same. Such functions are rare. In a
> 
> It's more than one case, I think.
> vmlinux has foo(a, b), the module has foo(a), the prog doesn't read 'b'.
> x86 trampoline does:
> 
> 	nr_regs = bpf_jit_place_args(&x86_arg_abi, m, pos_of_slot);
> 
> It saves 2 regs, the module needs 1. Such prog works today.
> libbpf passes the vmlinux ID for "mod:foo" since commit 8f8a024272f3.
> These progs get -EINVAL until their libbpf is updated.
> What a prog gets with module BTF and the vmlinux ID:
> 
>   FUNC of the module            today        series   diff below
>   ----------------------------  -----------  -------  ----------
>   other names of params         works        -EINVAL  works
>   fewer args, 'b' is not read   works        -EINVAL  works
>   other type of an arg          wrong type   -EINVAL  right type
>   more args                     not saved    -EINVAL  saved
> 
> > A proper longer-term (to deploy) fix: both cases go away if
> > btf__dedup() stops merging the FUNCs of a module into the FUNCs of
> > vmlinux. A FUNC stands for a symbol of one object, so the two aren't
> > the same type even when they look equal.  Then the kernel can refuse
> > any vmlinux function ID with the BTF of a module. This needs a change
> > in btf__dedup() for split BTF and a pahole which picks it up. I'm
> > looking for comments on whether this follow-up is ok.
> 
> The 2nd case doesn't go away. It grows to every name that is in both.
> test_shadow() has:
> 
> 	btf_id[1] = btf__find_by_name_kind(mod_btf, symbol_name, BTF_KIND_FUNC);
> 
> That's the vmlinux ID. With such pahole bpf_testmod has its own FUNC
> for bpf_fentry_shadow_test() and patch 1 refuses the load.
> 
> The series finds the FUNC of the module already.
> Lets use it instead of failing.
> Old loaders get the right prototype, before and after the follow up.
> A FUNC that pahole dropped is still not covered.
> A glob that passes both IDs still gets -EINVAL. That needs patch 3.
> 
> btf_own_func_id() replaces patches 1 and 2.
> 
> diff --git a/include/linux/btf.h b/include/linux/btf.h
> index 6014282..510f587 100644
> --- a/include/linux/btf.h
> +++ b/include/linux/btf.h
> @@ -254,6 +254,7 @@ int btf_check_and_fixup_fields(const struct btf *btf, struct btf_record *rec);
>  bool btf_type_is_void(const struct btf_type *t);
>  bool btf_type_is_arena_ptr(const struct btf *btf, const struct btf_type *t);
>  s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind);
> +u32 btf_own_func_id(const struct btf *btf, u32 id);
>  s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p);
>  struct btf *btf_get_module_btf(const struct module *module);
>  __u32 btf_relocate_id(const struct btf *btf, __u32 id);
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index 3597ba1..6312b7c 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -664,19 +664,13 @@ static s32 btf_find_by_name_kind_bsearch(const struct btf *btf, const char *name
>  	return btf_nr_types(btf);
>  }
>  
> -s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
> +/* Look up only the types of btf, not of its base */
> +static s32 btf_find_by_name_kind_own(const struct btf *btf, const char *name, u8 kind)
>  {
> -	const struct btf *base_btf = btf_base_btf(btf);
>  	const struct btf_type *t;
>  	const char *tname;
>  	s32 id, total;
>  
> -	if (base_btf) {
> -		id = btf_find_by_name_kind(base_btf, name, kind);
> -		if (id > 0)
> -			return id;
> -	}
> -
>  	total = btf_nr_types(btf);
>  	if (btf->named_start_id > 0 && name[0]) {
>  		id = btf_find_by_name_kind_bsearch(btf, name);
> @@ -707,6 +701,42 @@ s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
>  	return -ENOENT;
>  }
>  
> +s32 btf_find_by_name_kind(const struct btf *btf, const char *name, u8 kind)
> +{
> +	const struct btf *base_btf = btf_base_btf(btf);
> +	s32 id;
> +
> +	if (base_btf) {
> +		id = btf_find_by_name_kind(base_btf, name, kind);
> +		if (id > 0)
> +			return id;
> +	}
> +
> +	return btf_find_by_name_kind_own(btf, name, kind);
> +}
> +
> +/*
> + * The address of an attach target is looked up by name in the module. Take
> + * the FUNC of the module when it has one of that name. Otherwise the dedup
> + * merged it into the FUNC of vmlinux.
> + */
> +u32 btf_own_func_id(const struct btf *btf, u32 id)
> +{
> +	const struct btf_type *t;
> +	s32 own_id;
> +
> +	if (id >= btf->start_id)
> +		return id;
> +
> +	t = btf_type_by_id(btf, id);
> +	if (!btf_type_is_func(t))
> +		return id;
> +
> +	own_id = btf_find_by_name_kind_own(btf, btf_name_by_offset(btf, t->name_off),
> +					   BTF_KIND_FUNC);
> +	return own_id > 0 ? own_id : id;
> +}
> +
>  s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)
>  {
>  	struct btf *btf;
> diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
> index b9cd3d5..2258861 100644
> --- a/kernel/bpf/syscall.c
> +++ b/kernel/bpf/syscall.c
> @@ -3069,6 +3069,7 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr
>  					goto put_token;
>  				}
>  			}
> +			attach_btf_id = btf_own_func_id(attach_btf, attach_btf_id);
>  		} else if (dst_prog->aux->is_entry &&
>  			   dst_prog->aux->func_info[0].type_id != attr->attach_btf_id) {
>  			/* what an entry calls is a function of its owner */
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 2031c37..42963a1 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -3918,6 +3918,11 @@ int bpf_tracing_multi_attach(struct bpf_prog *prog, const union bpf_attr *attr,
>  		}
>  	}
>  
> +	for (int i = 0; i < cnt; i++) {
> +		ids[i] = btf_own_func_id(prog->aux->attach_btf, ids[i]);

I like the fix

one nit: the btf_own_func_id name seems a bit misleading to me
when the module does not have that function we keep the vmlinux id,
so the result is not always module's own id. Would something like
btf_remap_func_id fit better?

thanks,
jirka

      reply	other threads:[~2026-12-05 11:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 15:46 [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 1/4] " Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 2/4] bpf: Refuse the vmlinux prototype for a module function in multi links Anton Protopopov
2026-12-04 15:47 ` [PATCH v2 bpf-next 3/4] libbpf: Look up the functions of a module first Anton Protopopov
2026-12-05 11:45   ` Jiri Olsa
2026-12-04 15:47 ` [PATCH v2 bpf-next 4/4] selftests/bpf: Test a vmlinux function ID with the BTF of a module Anton Protopopov
2026-12-05  3:47 ` [PATCH v2 bpf-next 0/4] bpf: Refuse the vmlinux prototype for a module function Alexei Starovoitov
2026-12-05 11:45   ` Jiri Olsa [this message]

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=axP5aOcFUGIWjwzA@krava \
    --to=olsajiri@gmail.com \
    --cc=a.s.protopopov@gmail.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --cc=vmalik@redhat.com \
    --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