* [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF
@ 2024-01-29 1:35 Daniel Xu
2024-01-29 12:11 ` Jiri Olsa
2024-01-30 4:33 ` Andrii Nakryiko
0 siblings, 2 replies; 5+ messages in thread
From: Daniel Xu @ 2024-01-29 1:35 UTC (permalink / raw)
To: quentin, daniel, ast, andrii, alexei.starovoitov, olsajiri,
alan.maguire, memxor
Cc: martin.lau, eddyz87, song, yonghong.song, john.fastabend, kpsingh,
sdf, haoluo, jolsa, bpf, linux-kernel
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) __ksym;
extern void cgroup_rstat_flush(struct cgroup * cgrp) __ksym;
extern struct bpf_key * bpf_lookup_user_key(u32 serial, u64 flags) __ksym;
Note that this patch is only effective after enabling pahole [0]
and kernel [1] changes are merged.
[0]: https://lore.kernel.org/bpf/0f25134ec999e368478c4ca993b3b729c2a03383.1706491733.git.dxu@dxuuu.xyz/
[1]: https://lore.kernel.org/bpf/cover.1706491398.git.dxu@dxuuu.xyz/
Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
tools/bpf/bpftool/btf.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
index 91fcb75babe3..9ab26ed12733 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,28 @@ static int dump_btf_raw(const struct btf *btf,
return 0;
}
+static void dump_btf_kfuncs(const struct btf *btf)
+{
+ 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);
+ char kfunc_sig[1024];
+ const char *name;
+
+ 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;
+
+ btf_dumper_type_only(btf, t->type, kfunc_sig, sizeof(kfunc_sig));
+ printf("extern %s __ksym;\n\n", kfunc_sig);
+ }
+}
+
static void __printf(2, 0) btf_dump_printf(void *ctx,
const char *fmt, va_list args)
{
@@ -476,6 +500,9 @@ 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");
if (root_type_cnt) {
for (i = 0; i < root_type_cnt; i++) {
@@ -491,6 +518,8 @@ static int dump_btf_c(const struct btf *btf,
if (err)
goto done;
}
+
+ dump_btf_kfuncs(btf);
}
printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n");
--
2.42.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF 2024-01-29 1:35 [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF Daniel Xu @ 2024-01-29 12:11 ` Jiri Olsa 2024-01-29 16:13 ` Daniel Xu 2024-01-30 4:33 ` Andrii Nakryiko 1 sibling, 1 reply; 5+ messages in thread From: Jiri Olsa @ 2024-01-29 12:11 UTC (permalink / raw) To: Daniel Xu Cc: quentin, daniel, ast, andrii, alexei.starovoitov, olsajiri, alan.maguire, memxor, martin.lau, eddyz87, song, yonghong.song, john.fastabend, kpsingh, sdf, haoluo, bpf, linux-kernel On Sun, Jan 28, 2024 at 06:35:33PM -0700, 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 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) __ksym; > extern void cgroup_rstat_flush(struct cgroup * cgrp) __ksym; > extern struct bpf_key * bpf_lookup_user_key(u32 serial, u64 flags) __ksym; hi, I'm getting following declaration for bpf_rbtree_add_impl: extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; and it fails to compile with: In file included from skeleton/pid_iter.bpf.c:3: ./vmlinux.h:164511:141: error: expected ')' 164511 | extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; | ^ ./vmlinux.h:164511:31: note: to match this '(' 164511 | extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; looks like the btf_dumper_type_only won't dump function pointer argument properly.. I guess we should fix that, but looking at the other stuff in vmlinux.h like *_ops struct we can print function pointers properly, so perhaps another way around is to use btf_dumper interface instead jirka > > Note that this patch is only effective after enabling pahole [0] > and kernel [1] changes are merged. > > [0]: https://lore.kernel.org/bpf/0f25134ec999e368478c4ca993b3b729c2a03383.1706491733.git.dxu@dxuuu.xyz/ > [1]: https://lore.kernel.org/bpf/cover.1706491398.git.dxu@dxuuu.xyz/ > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > --- > tools/bpf/bpftool/btf.c | 29 +++++++++++++++++++++++++++++ > 1 file changed, 29 insertions(+) > > diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c > index 91fcb75babe3..9ab26ed12733 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,28 @@ static int dump_btf_raw(const struct btf *btf, > return 0; > } > > +static void dump_btf_kfuncs(const struct btf *btf) > +{ > + 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); > + char kfunc_sig[1024]; > + const char *name; > + > + 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; > + > + btf_dumper_type_only(btf, t->type, kfunc_sig, sizeof(kfunc_sig)); > + printf("extern %s __ksym;\n\n", kfunc_sig); > + } > +} > + > static void __printf(2, 0) btf_dump_printf(void *ctx, > const char *fmt, va_list args) > { > @@ -476,6 +500,9 @@ 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"); > > if (root_type_cnt) { > for (i = 0; i < root_type_cnt; i++) { > @@ -491,6 +518,8 @@ static int dump_btf_c(const struct btf *btf, > if (err) > goto done; > } > + > + dump_btf_kfuncs(btf); > } > > printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n"); > -- > 2.42.1 > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF 2024-01-29 12:11 ` Jiri Olsa @ 2024-01-29 16:13 ` Daniel Xu 2024-01-30 8:15 ` Jiri Olsa 0 siblings, 1 reply; 5+ messages in thread From: Daniel Xu @ 2024-01-29 16:13 UTC (permalink / raw) To: Jiri Olsa Cc: quentin, daniel, ast, andrii, alexei.starovoitov, alan.maguire, memxor, martin.lau, eddyz87, song, yonghong.song, john.fastabend, kpsingh, sdf, haoluo, bpf, linux-kernel Hi Jiri, On Mon, Jan 29, 2024 at 01:11:17PM +0100, Jiri Olsa wrote: > On Sun, Jan 28, 2024 at 06:35:33PM -0700, 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 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) __ksym; > > extern void cgroup_rstat_flush(struct cgroup * cgrp) __ksym; > > extern struct bpf_key * bpf_lookup_user_key(u32 serial, u64 flags) __ksym; > > hi, > I'm getting following declaration for bpf_rbtree_add_impl: > > extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; > > and it fails to compile with: > > In file included from skeleton/pid_iter.bpf.c:3: > ./vmlinux.h:164511:141: error: expected ')' > 164511 | extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; > | ^ > ./vmlinux.h:164511:31: note: to match this '(' > 164511 | extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; > > looks like the btf_dumper_type_only won't dump function pointer argument > properly.. I guess we should fix that, but looking at the other stuff in > vmlinux.h like *_ops struct we can print function pointers properly, so > perhaps another way around is to use btf_dumper interface instead Ah, crap, looks like between all the branch switching I didn't build vmlinux with kfunc annotations. Having fixed that, I can repro this build failure. I'll take a look and see what the best way to fix this is. Given that end to end the whole flow basically works, should we start working on merging patches? Thanks, Daniel [..] ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF 2024-01-29 16:13 ` Daniel Xu @ 2024-01-30 8:15 ` Jiri Olsa 0 siblings, 0 replies; 5+ messages in thread From: Jiri Olsa @ 2024-01-30 8:15 UTC (permalink / raw) To: Daniel Xu Cc: Jiri Olsa, quentin, daniel, ast, andrii, alexei.starovoitov, alan.maguire, memxor, martin.lau, eddyz87, song, yonghong.song, john.fastabend, kpsingh, sdf, haoluo, bpf, linux-kernel On Mon, Jan 29, 2024 at 09:13:13AM -0700, Daniel Xu wrote: > Hi Jiri, > > On Mon, Jan 29, 2024 at 01:11:17PM +0100, Jiri Olsa wrote: > > On Sun, Jan 28, 2024 at 06:35:33PM -0700, 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 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) __ksym; > > > extern void cgroup_rstat_flush(struct cgroup * cgrp) __ksym; > > > extern struct bpf_key * bpf_lookup_user_key(u32 serial, u64 flags) __ksym; > > > > hi, > > I'm getting following declaration for bpf_rbtree_add_impl: > > > > extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; > > > > and it fails to compile with: > > > > In file included from skeleton/pid_iter.bpf.c:3: > > ./vmlinux.h:164511:141: error: expected ')' > > 164511 | extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; > > | ^ > > ./vmlinux.h:164511:31: note: to match this '(' > > 164511 | extern int bpf_rbtree_add_impl(struct bpf_rb_root * root, struct bpf_rb_node * node, bool (struct bpf_rb_node *, const struct bpf_rb_node *)* less, void * meta__ign, u64 off) __ksym; > > > > looks like the btf_dumper_type_only won't dump function pointer argument > > properly.. I guess we should fix that, but looking at the other stuff in > > vmlinux.h like *_ops struct we can print function pointers properly, so > > perhaps another way around is to use btf_dumper interface instead > > Ah, crap, looks like between all the branch switching I didn't build > vmlinux with kfunc annotations. Having fixed that, I can repro this > build failure. > > I'll take a look and see what the best way to fix this is. > > Given that end to end the whole flow basically works, should we start > working on merging patches? yes, the flow looks good to me.. will check the rest of the patches jirka ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF 2024-01-29 1:35 [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF Daniel Xu 2024-01-29 12:11 ` Jiri Olsa @ 2024-01-30 4:33 ` Andrii Nakryiko 1 sibling, 0 replies; 5+ messages in thread From: Andrii Nakryiko @ 2024-01-30 4:33 UTC (permalink / raw) To: Daniel Xu Cc: quentin, daniel, ast, andrii, alexei.starovoitov, olsajiri, alan.maguire, memxor, martin.lau, eddyz87, song, yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa, bpf, linux-kernel On Sun, Jan 28, 2024 at 5:35 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) __ksym; > extern void cgroup_rstat_flush(struct cgroup * cgrp) __ksym; > extern struct bpf_key * bpf_lookup_user_key(u32 serial, u64 flags) __ksym; > All kfuncs have to be declared __weak, otherwise it will be impossible to feature-detect them in BPF code > Note that this patch is only effective after enabling pahole [0] > and kernel [1] changes are merged. > > [0]: https://lore.kernel.org/bpf/0f25134ec999e368478c4ca993b3b729c2a03383.1706491733.git.dxu@dxuuu.xyz/ > [1]: https://lore.kernel.org/bpf/cover.1706491398.git.dxu@dxuuu.xyz/ > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > --- > tools/bpf/bpftool/btf.c | 29 +++++++++++++++++++++++++++++ > 1 file changed, 29 insertions(+) > > diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c > index 91fcb75babe3..9ab26ed12733 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,28 @@ static int dump_btf_raw(const struct btf *btf, > return 0; > } > > +static void dump_btf_kfuncs(const struct btf *btf) > +{ > + 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); > + char kfunc_sig[1024]; > + const char *name; > + > + 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; > + > + btf_dumper_type_only(btf, t->type, kfunc_sig, sizeof(kfunc_sig)); > + printf("extern %s __ksym;\n\n", kfunc_sig); > + } > +} > + > static void __printf(2, 0) btf_dump_printf(void *ctx, > const char *fmt, va_list args) > { > @@ -476,6 +500,9 @@ 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"); > > if (root_type_cnt) { > for (i = 0; i < root_type_cnt; i++) { > @@ -491,6 +518,8 @@ static int dump_btf_c(const struct btf *btf, > if (err) > goto done; > } > + > + dump_btf_kfuncs(btf); > } > > printf("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n"); > -- > 2.42.1 > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-01-30 8:15 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-01-29 1:35 [PATCH bpf-next] bpftool: Support dumping kfunc prototypes from BTF Daniel Xu 2024-01-29 12:11 ` Jiri Olsa 2024-01-29 16:13 ` Daniel Xu 2024-01-30 8:15 ` Jiri Olsa 2024-01-30 4:33 ` Andrii Nakryiko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox