BPF List
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Anton Protopopov <a.s.protopopov@gmail.com>
Cc: bpf@vger.kernel.org, Alexei Starovoitov <ast@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 3/4] libbpf: Look up the functions of a module first
Date: Sat, 5 Dec 2026 12:45:53 +0100	[thread overview]
Message-ID: <axP5cT1OKBxMTb5x@krava> (raw)
In-Reply-To: <20261204154705.1411391-4-a.s.protopopov@gmail.com>

On Fri, Dec 04, 2026 at 03:47:02PM +0000, Anton Protopopov wrote:
> For "mod:func" libbpf looks func up in the BTF of the module, and
> that lookup searches the types of vmlinux first. If vmlinux has a
> function of that name too, libbpf takes its ID, and the program
> attaches to the module function with the vmlinux prototype. The
> kernel now refuses this when the module has a function of its own of
> that name. A glob of a tracing multi program for a module walks the
> types of vmlinux too, and takes both IDs for such a name.
> 
> Look up the function of the module first, and take the one of
> vmlinux only if the module has no function of that name. The dedup
> of BTF merges a module function of the same prototype into the
> vmlinux one, which is then the type of the module function.

nice, as Alexei noted, this change is still needed because of the
duplicate ids check in the multi link attach, one nit below

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka


> 
> Fixes: 8f8a024272f3 ("libbpf: support "module: Function" syntax for tracing programs")
> Fixes: f2aa370dfe57 ("libbpf: Add support to create tracing multi link")
> Signed-off-by: Anton Protopopov <a.s.protopopov@gmail.com>
> ---
>  tools/lib/bpf/libbpf.c | 75 ++++++++++++++++++++++++++++--------------
>  1 file changed, 51 insertions(+), 24 deletions(-)
> 
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 8ed4786e0e51..72330598a0f8 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -11869,7 +11869,14 @@ static int find_btf_by_prefix_kind(const struct btf *btf, const char *prefix,
>  	 */
>  	if (ret < 0 || ret >= sizeof(btf_type_name))
>  		return -ENAMETOOLONG;
> -	return btf__find_by_name_kind(btf, btf_type_name, kind);
> +	/*
> +	 * A function of a module first, then the one of vmlinux, which the
> +	 * dedup of BTF merges a module function of the same prototype into
> +	 */
> +	ret = btf__find_by_name_kind_own(btf, btf_type_name, kind);
> +	if (ret > 0 || !btf__base_btf(btf))
> +		return ret;
> +	return btf__find_by_name_kind(btf__base_btf(btf), btf_type_name, kind);
>  }
>  
>  static inline int find_attach_btf_id(struct btf *btf, const char *name,
> @@ -13887,40 +13894,60 @@ bool btf_type_is_traceable_func(const struct btf *btf, const struct btf_type *t)
>  	return true;
>  }
>  
> +static int add_func_id_by_glob(const struct btf *btf, __u32 type_id, const char *pattern,
> +			       bool skip_own, __u32 **ids, size_t *cap, size_t *cnt)
> +{
> +	const struct btf_type *t = btf__type_by_id(btf, type_id);
> +	const char *name;
> +
> +	if (btf_kind(t) != BTF_KIND_FUNC)
> +		return 0;
> +	name = btf__name_by_offset(btf, t->name_off);
> +	if (!name || !glob_match(name, pattern))
> +		return 0;
> +	if (!btf_type_is_traceable_func(btf, t))
> +		return 0;
> +	if (skip_own && btf__find_by_name_kind_own(btf, name, BTF_KIND_FUNC) > 0)
> +		return 0;
> +
> +	if (libbpf_ensure_mem((void **) ids, cap, sizeof(**ids), *cnt + 1))
> +		return -ENOMEM;
> +	(*ids)[(*cnt)++] = type_id;
> +	return 0;
> +}
> +
>  static int
>  collect_btf_func_ids_by_glob(const struct btf *btf, const char *pattern, __u32 **ids)
>  {
> -	__u32 type_id, nr_types = btf__type_cnt(btf);
> +	const struct btf *base_btf = btf__base_btf(btf);
> +	__u32 type_id, start_id, nr_types = btf__type_cnt(btf);
>  	size_t cap = 0, cnt = 0;
> +	int err;
>  
>  	if (!pattern)
>  		return -EINVAL;
>  
> -	for (type_id = 1; type_id < nr_types; type_id++) {
> -		const struct btf_type *t = btf__type_by_id(btf, type_id);
> -		const char *name;
> -		int err;
> -
> -		if (btf_kind(t) != BTF_KIND_FUNC)
> -			continue;
> -		name = btf__name_by_offset(btf, t->name_off);
> -		if (!name)
> -			continue;
> -
> -		if (!glob_match(name, pattern))
> -			continue;
> -		if (!btf_type_is_traceable_func(btf, t))
> -			continue;
> -
> -		err = libbpf_ensure_mem((void **) ids, &cap, sizeof(**ids), cnt + 1);
> -		if (err) {
> -			free(*ids);
> -			return -ENOMEM;
> -		}
> -		(*ids)[cnt++] = type_id;
> +	/*
> +	 * The functions of a module first, then the ones of vmlinux, which the
> +	 * dedup of BTF merges module functions of the same prototype into
> +	 */
> +	start_id = base_btf ? btf__type_cnt(base_btf) : 1;
> +	for (type_id = start_id; type_id < nr_types; type_id++) {
> +		err = add_func_id_by_glob(btf, type_id, pattern, false, ids, &cap, &cnt);

nit, add_func_id_by_glob fails only with -ENOMEM, so we can drop
err and return -ENOMEM directly on the error path


> +		if (err)
> +			goto err_out;
> +	}
> +	for (type_id = 1; type_id < start_id; type_id++) {
> +		err = add_func_id_by_glob(btf, type_id, pattern, true, ids, &cap, &cnt);
> +		if (err)
> +			goto err_out;
>  	}
>  
>  	return cnt;
> +
> +err_out:
> +	free(*ids);
> +	return err;
>  }
>  
>  static int collect_func_ids_by_glob(const struct bpf_program *prog, const char *pattern, __u32 **ids)
> -- 
> 2.43.0
> 

  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 [this message]
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

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=axP5cT1OKBxMTb5x@krava \
    --to=olsajiri@gmail.com \
    --cc=a.s.protopopov@gmail.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=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