BPF Archive mirror
 help / color / mirror / Atom feed
From: Daniel Xu <dxu@dxuuu.xyz>
To: ast@kernel.org, daniel@iogearbox.net, qmo@kernel.org,
	 andrii@kernel.org, olsajiri@gmail.com, quentin@isovalent.com,
	 alan.maguire@oracle.com, acme@kernel.org, eddyz87@gmail.com
Cc: martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev,
	 john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com,
	haoluo@google.com,  jolsa@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org,  kernel-team@meta.com
Subject: Re: [PATCH bpf-next v3 2/2] bpftool: Support dumping kfunc prototypes from BTF
Date: Mon, 13 May 2024 15:14:44 -0600	[thread overview]
Message-ID: <f27zhdp4ydckd36j7qskqjty227nax47uo62gqoimgw6hoanmv@bt2oxuxdngvs> (raw)
In-Reply-To: <6b16417c2c05019e83e420240c6d9796f9324a6c.1715625447.git.dxu@dxuuu.xyz>

On Mon, May 13, 2024 at 12:38:59PM GMT, Daniel Xu wrote:
> This patch enables dumping kfunc prototypes from bpftool. This is useful
> b/c with this patch, end users will no longer have to manually define
> kfunc prototypes. For the kernel tree, this also means we can optionally
> drop kfunc prototypes from:
> 
>         tools/testing/selftests/bpf/bpf_kfuncs.h
>         tools/testing/selftests/bpf/bpf_experimental.h
> 
> Example usage:
> 
>         $ make PAHOLE=/home/dxu/dev/pahole/build/pahole -j30 vmlinux
> 
>         $ ./tools/bpf/bpftool/bpftool btf dump file ./vmlinux format c | rg "__ksym;" | head -3
>         extern void cgroup_rstat_updated(struct cgroup *cgrp, int cpu) __weak __ksym;
>         extern void cgroup_rstat_flush(struct cgroup *cgrp) __weak __ksym;
>         extern struct bpf_key *bpf_lookup_user_key(u32 serial, u64 flags) __weak __ksym;
> 
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---
>  tools/bpf/bpftool/btf.c | 54 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 54 insertions(+)
> 
> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index 91fcb75babe3..884af6589f0d 100644
> --- a/tools/bpf/bpftool/btf.c
> +++ b/tools/bpf/bpftool/btf.c
> @@ -20,6 +20,8 @@
>  #include "json_writer.h"
>  #include "main.h"
>  
> +#define KFUNC_DECL_TAG		"bpf_kfunc"
> +
>  static const char * const btf_kind_str[NR_BTF_KINDS] = {
>  	[BTF_KIND_UNKN]		= "UNKNOWN",
>  	[BTF_KIND_INT]		= "INT",
> @@ -454,6 +456,48 @@ static int dump_btf_raw(const struct btf *btf,
>  	return 0;
>  }
>  
> +static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf)
> +{
> +	LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts);
> +	int cnt = btf__type_cnt(btf);
> +	int i;
> +
> +	printf("\n/* BPF kfuncs */\n");
> +
> +	for (i = 1; i < cnt; i++) {
> +		const struct btf_type *t = btf__type_by_id(btf, i);
> +		const char *name;
> +		int err;
> +
> +		if (!btf_is_decl_tag(t))
> +			continue;
> +
> +		if (btf_decl_tag(t)->component_idx != -1)
> +			continue;
> +
> +		name = btf__name_by_offset(btf, t->name_off);
> +		if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)))
> +			continue;
> +
> +		t = btf__type_by_id(btf, t->type);
> +		if (!btf_is_func(t))
> +			continue;
> +
> +		printf("extern ");
> +
> +		opts.field_name = btf__name_by_offset(btf, t->name_off);
> +		err = btf_dump__emit_type_decl(d, t->type, &opts);
> +		if (err)
> +			return err;
> +
> +		printf(" __weak __ksym;\n");
> +	}
> +
> +	printf("\n");
> +
> +	return 0;
> +}
> +
>  static void __printf(2, 0) btf_dump_printf(void *ctx,
>  					   const char *fmt, va_list args)
>  {
> @@ -476,6 +520,12 @@ static int dump_btf_c(const struct btf *btf,
>  	printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
>  	printf("#pragma clang attribute push (__attribute__((preserve_access_index)), apply_to = record)\n");
>  	printf("#endif\n\n");
> +	printf("#ifndef __ksym\n");
> +	printf("#define __ksym __attribute__((section(\".ksyms\")))\n");
> +	printf("#endif\n\n");
> +	printf("#ifndef __weak\n");
> +	printf("#define __weak __attribute__((weak))\n");
> +	printf("#endif\n\n");
>  
>  	if (root_type_cnt) {
>  		for (i = 0; i < root_type_cnt; i++) {
> @@ -491,6 +541,10 @@ static int dump_btf_c(const struct btf *btf,
>  			if (err)
>  				goto done;
>  		}
> +
> +		err = dump_btf_kfuncs(d, btf);
> +		if (err)
> +			goto done;
>  	}
>  
>  	printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
> -- 
> 2.44.0
> 
> 

Oh, looks like selftests fail to build. I will fix that for v4.

      reply	other threads:[~2024-05-13 21:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-13 18:38 [PATCH bpf-next v3 0/2] bpf: bpftool: Support dumping kfunc prototypes from BTF Daniel Xu
2024-05-13 18:38 ` [PATCH bpf-next v3 1/2] kbuild: bpf: Tell pahole to DECL_TAG kfuncs Daniel Xu
2024-05-13 18:38 ` [PATCH bpf-next v3 2/2] bpftool: Support dumping kfunc prototypes from BTF Daniel Xu
2024-05-13 21:14   ` Daniel Xu [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=f27zhdp4ydckd36j7qskqjty227nax47uo62gqoimgw6hoanmv@bt2oxuxdngvs \
    --to=dxu@dxuuu.xyz \
    --cc=acme@kernel.org \
    --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=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernel-team@meta.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=olsajiri@gmail.com \
    --cc=qmo@kernel.org \
    --cc=quentin@isovalent.com \
    --cc=sdf@google.com \
    --cc=song@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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).