diff mbox series

[bpf-next,v2,2/2] bpftool: Support dumping kfunc prototypes from BTF

Message ID 9b8ebd13300e28bd92a2e6de4fb04f85c1b6ce7c.1707080349.git.dxu@dxuuu.xyz (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpf, bpftool: Support dumping kfunc prototypes from BTF | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/build_tools success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 14 of 14 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: Prefer __section(\".ksyms\") over __attribute__((section(\".ksyms\"))) WARNING: Prefer __weak over __attribute__((weak))
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18 and -O2 optimization
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-13 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / veristat

Commit Message

Daniel Xu Feb. 4, 2024, 9:06 p.m. UTC
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 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;

Note that this patch is only effective after the enabling pahole [0]
change is merged and the resulting feature enabled with
--btf_features=decl_tag_kfuncs.

[0]: https://lore.kernel.org/bpf/cover.1707071969.git.dxu@dxuuu.xyz/

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
 tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

Comments

Andrii Nakryiko Feb. 8, 2024, 12:50 a.m. UTC | #1
On Sun, Feb 4, 2024 at 1:07 PM Daniel Xu <dxu@dxuuu.xyz> 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 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;
>
> Note that this patch is only effective after the enabling pahole [0]
> change is merged and the resulting feature enabled with
> --btf_features=decl_tag_kfuncs.
>
> [0]: https://lore.kernel.org/bpf/cover.1707071969.git.dxu@dxuuu.xyz/
>
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---
>  tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
>
> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index 91fcb75babe3..0fd78a476286 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,39 @@ static int dump_btf_raw(const struct btf *btf,
>         return 0;
>  }
>
> +static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf)
> +{
> +       DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts);

nit: use shorter LIBBPF_OPTS, DECLARE_LIBBPF_OPTS is a "deprecated"
macro name I hid, but didn't remove

> +       int cnt = btf__type_cnt(btf);
> +       int i;
> +
> +       for (i = 1; i < cnt; i++) {
> +               const struct btf_type *t = btf__type_by_id(btf, i);
> +               const struct btf_type *kft;
> +               const char *name;
> +               int err;
> +
> +               if (!btf_is_decl_tag(t))
> +                       continue;
> +
> +               name = btf__name_by_offset(btf, t->name_off);
> +               if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)))
> +                       continue;

should we do a bit more sanity checking here? Check that component_idx
= -1 (entire func) and pointee type is FUNC?

> +
> +               printf("extern ");
> +
> +               kft = btf__type_by_id(btf, t->type);

nit: reuse t?

> +               opts.field_name = btf__name_by_offset(btf, kft->name_off);
> +               err = btf_dump__emit_type_decl(d, kft->type, &opts);
> +               if (err)
> +                       return err;
> +
> +               printf(" __weak __ksym;\n\n");

why extra endline?

though I'd ensure two empty lines before the first kfunc declaration
to visually separate it from other type. Maybe even add a comment like
`/* BPF kfuncs */` or something like that?

> +       }
> +
> +       return 0;
> +}
> +
>  static void __printf(2, 0) btf_dump_printf(void *ctx,
>                                            const char *fmt, va_list args)
>  {
> @@ -476,6 +511,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 +532,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.42.1
>
Daniel Xu March 18, 2024, 6:37 p.m. UTC | #2
Hi Andrii,

On Wed, Feb 07, 2024 at 04:50:15PM -0800, Andrii Nakryiko wrote:
> On Sun, Feb 4, 2024 at 1:07 PM Daniel Xu <dxu@dxuuu.xyz> 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 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;
> >
> > Note that this patch is only effective after the enabling pahole [0]
> > change is merged and the resulting feature enabled with
> > --btf_features=decl_tag_kfuncs.
> >
> > [0]: https://lore.kernel.org/bpf/cover.1707071969.git.dxu@dxuuu.xyz/
> >
> > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > ---
> >  tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++
> >  1 file changed, 45 insertions(+)
> >
> > diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> > index 91fcb75babe3..0fd78a476286 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,39 @@ static int dump_btf_raw(const struct btf *btf,
> >         return 0;
> >  }
> >
> > +static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf)
> > +{
> > +       DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts);
> 
> nit: use shorter LIBBPF_OPTS, DECLARE_LIBBPF_OPTS is a "deprecated"
> macro name I hid, but didn't remove

Ack.

> 
> > +       int cnt = btf__type_cnt(btf);
> > +       int i;
> > +
> > +       for (i = 1; i < cnt; i++) {
> > +               const struct btf_type *t = btf__type_by_id(btf, i);
> > +               const struct btf_type *kft;
> > +               const char *name;
> > +               int err;
> > +
> > +               if (!btf_is_decl_tag(t))
> > +                       continue;
> > +
> > +               name = btf__name_by_offset(btf, t->name_off);
> > +               if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)))
> > +                       continue;
> 
> should we do a bit more sanity checking here? Check that component_idx
> = -1 (entire func) and pointee type is FUNC?

Makes sense. Will add.

> 
> > +
> > +               printf("extern ");
> > +
> > +               kft = btf__type_by_id(btf, t->type);
> 
> nit: reuse t?
> 
> > +               opts.field_name = btf__name_by_offset(btf, kft->name_off);
> > +               err = btf_dump__emit_type_decl(d, kft->type, &opts);
> > +               if (err)
> > +                       return err;
> > +
> > +               printf(" __weak __ksym;\n\n");
> 
> why extra endline?

Was thinking b/c other entities (structs) are double newline separated
that we should keep it up for kfunc prototypes. No opinion -- I'll
change it.

> 
> though I'd ensure two empty lines before the first kfunc declaration
> to visually separate it from other type. Maybe even add a comment like
> `/* BPF kfuncs */` or something like that?

Ack.

> 
> > +       }
> > +
> > +       return 0;
> > +}
> > +
> >  static void __printf(2, 0) btf_dump_printf(void *ctx,
> >                                            const char *fmt, va_list args)
> >  {
> > @@ -476,6 +511,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 +532,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.42.1
> >

I'll send the next rev after the pahole changes get merged.

Thanks,
Daniel
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index 91fcb75babe3..0fd78a476286 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,39 @@  static int dump_btf_raw(const struct btf *btf,
 	return 0;
 }
 
+static int dump_btf_kfuncs(struct btf_dump *d, const struct btf *btf)
+{
+	DECLARE_LIBBPF_OPTS(btf_dump_emit_type_decl_opts, opts);
+	int cnt = btf__type_cnt(btf);
+	int i;
+
+	for (i = 1; i < cnt; i++) {
+		const struct btf_type *t = btf__type_by_id(btf, i);
+		const struct btf_type *kft;
+		const char *name;
+		int err;
+
+		if (!btf_is_decl_tag(t))
+			continue;
+
+		name = btf__name_by_offset(btf, t->name_off);
+		if (strncmp(name, KFUNC_DECL_TAG, sizeof(KFUNC_DECL_TAG)))
+			continue;
+
+		printf("extern ");
+
+		kft = btf__type_by_id(btf, t->type);
+		opts.field_name = btf__name_by_offset(btf, kft->name_off);
+		err = btf_dump__emit_type_decl(d, kft->type, &opts);
+		if (err)
+			return err;
+
+		printf(" __weak __ksym;\n\n");
+	}
+
+	return 0;
+}
+
 static void __printf(2, 0) btf_dump_printf(void *ctx,
 					   const char *fmt, va_list args)
 {
@@ -476,6 +511,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 +532,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");