BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Daniel Xu <dxu@dxuuu.xyz>,
	acme@kernel.org, jolsa@kernel.org,  quentin@isovalent.com,
	alan.maguire@oracle.com
Cc: andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net,
	 bpf@vger.kernel.org
Subject: Re: [PATCH dwarves v4 2/2] pahole: Inject kfunc decl tags into BTF
Date: Tue, 06 Feb 2024 01:31:20 +0200	[thread overview]
Message-ID: <1853738ac796d75c53970e21b6d61bf5140a6cc1.camel@gmail.com> (raw)
In-Reply-To: <28e81ccf28d6dd33f6db50af6526dc1770502b8d.1707071969.git.dxu@dxuuu.xyz>

On Sun, 2024-02-04 at 11:40 -0700, Daniel Xu wrote:
> This commit teaches pahole to parse symbols in .BTF_ids section in
> vmlinux and discover exported kfuncs. Pahole then takes the list of
> kfuncs and injects a BTF_KIND_DECL_TAG for each kfunc.
> 
> Example of encoding:
> 
>         $ bpftool btf dump file .tmp_vmlinux.btf | rg "DECL_TAG 'bpf_kfunc'" | wc -l
>         121
> 
>         $ bpftool btf dump file .tmp_vmlinux.btf | rg 56337
>         [56337] FUNC 'bpf_ct_change_timeout' type_id=56336 linkage=static
>         [127861] DECL_TAG 'bpf_kfunc' type_id=56337 component_idx=-1
> 
> This enables downstream users and tools to dynamically discover which
> kfuncs are available on a system by parsing vmlinux or module BTF, both
> available in /sys/kernel/btf.
> 
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---

I've tested this patch-set using kernel built both with clang and gcc,
on current bpf-next master (2d9a925d0fbf), both times get 124 kfunc definitions.

Tested-by: Eduard Zingerman <eddyz87@gmail.com>

Two nitpicks below.

[...]

> +static char *get_func_name(const char *sym)
> +{
> +	char *func, *end;
> +
> +	if (strncmp(sym, BTF_ID_FUNC_PFX, sizeof(BTF_ID_FUNC_PFX) - 1))
> +		return NULL;
> +
> +	/* Strip prefix */
> +	func = strdup(sym + sizeof(BTF_ID_FUNC_PFX) - 1);
> +
> +	/* Strip suffix */
> +	end = strrchr(func, '_');
> +	if (!end || *(end - 1) != '_') {

Nit: this would do out of bounds access on malformed input
     "__BTF_ID__func___"

> +		free(func);
> +		return NULL;
> +	}
> +	*(end - 1) = '\0';
> +
> +	return func;
> +}

[...]

> +static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder)
> +{

[...]

> +	elf = elf_begin(fd, ELF_C_READ, NULL);
> +	if (elf == NULL) {
> +		elf_error("Cannot update ELF file");
> +		goto out;
> +	}
> +
> +	/* Location symbol table and .BTF_ids sections */
> +	elf_getshdrstrndx(elf, &strndx);

Nit: in theory elf_getshdrstrndx() could fail and strndx would remain
     uninitialized.

[...]

  parent reply	other threads:[~2024-02-05 23:31 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-04 18:40 [PATCH dwarves v4 0/2] pahole: Inject kfunc decl tags into BTF Daniel Xu
2024-02-04 18:40 ` [PATCH dwarves v4 1/2] pahole: Add --btf_feature=decl_tag_kfuncs feature Daniel Xu
2024-02-06 12:47   ` Jiri Olsa
2024-02-06 14:14     ` Alan Maguire
2024-02-04 18:40 ` [PATCH dwarves v4 2/2] pahole: Inject kfunc decl tags into BTF Daniel Xu
2024-02-05 16:54   ` Eduard Zingerman
2024-02-05 23:31   ` Eduard Zingerman [this message]
2024-02-28 16:07     ` Daniel Xu
2024-02-28 21:33       ` Eduard Zingerman
2024-03-15 19:43         ` Daniel Xu
2024-02-08 10:00   ` Alan Maguire
2024-02-29  0:57     ` Daniel Xu
2024-03-13  3:17       ` Daniel Xu
2024-03-13  9:36         ` Alan Maguire

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=1853738ac796d75c53970e21b6d61bf5140a6cc1.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=acme@kernel.org \
    --cc=alan.maguire@oracle.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dxu@dxuuu.xyz \
    --cc=jolsa@kernel.org \
    --cc=quentin@isovalent.com \
    /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