bpf, bpftool: Support dumping kfunc prototypes from BTF
v2 cover bpf, bpftool: Support dumping kfunc prototypes from BTF
This patchset enables dumping kfunc prototypes from bpftool. This is useful b/c with this patchset, 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 patchset 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/[email protected]/
=== Changelog ===
From v1:
* Add __weak annotation
* Use btf_dump for kfunc prototypes
* Update kernel bpf_rdonly_cast() signature
Daniel Xu (2):
bpf: Have bpf_rdonly_cast() take a const pointer
bpftool: Support dumping kfunc prototypes from BTF
kernel/bpf/helpers.c | 4 ++-- tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-)
Signature · 1 line
2.42.1
PATCH v2 1/2 bpf: Have bpf_rdonly_cast() take a const pointer
Since 20d59ee55172 ("libbpf: add bpf_core_cast() macro"), libbpf is now exporting a const arg version of bpf_rdonly_cast(). This causes the following conflicting type error when generating kfunc prototypes from BTF:
In file included from skeleton/pid_iter.bpf.c:5:/home/dxu/dev/linux/tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:297:14: error: conflicting types for 'bpf_rdonly_cast'
extern void *bpf_rdonly_cast(const void *obj__ign, __u32 btf_id__k) __ksym __weak;
^./vmlinux.h:135625:14: note: previous declaration is here extern void *bpf_rdonly_cast(void *obj__ign, u32 btf_id__k) __weak __ksym;
This is b/c the kernel defines bpf_rdonly_cast() with non-const arg. Since const arg is more permissive and thus backwards compatible, we change the kernel definition as well to avoid conflicting type errors.
--- kernel/bpf/helpers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +2 -2
@@ -2484,9 +2484,9 @@ __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)return obj; } -__bpf_kfunc void *bpf_rdonly_cast(void *obj__ign, u32 btf_id__k) +__bpf_kfunc void *bpf_rdonly_cast(const void *obj__ign, u32 btf_id__k) { -return obj__ign; +return (void *)obj__ign; } __bpf_kfunc void bpf_rcu_read_lock(void)
Signature · 1 line
2.42.1
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Quoted 9 lines · Daniel Xu
Since
20d59ee55172("libbpf: addbpf_core_cast()macro"), libbpf is now exporting a const arg version ofbpf_rdonly_cast(). This causes the following conflicting type error when generating kfunc prototypes from BTF:In file included from
skeleton/pid_iter.bpf.c:5:/home/dxu/dev/linux/tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:297:14: error: conflicting types for 'bpf_rdonly_cast'extern void *bpf_rdonly_cast(const void *obj__ign, __u32 btf_id__k) __ksym __weak; ^
Quoted 8 lines
./vmlinux.h:135625:14: note: previous declaration is here extern void *bpf_rdonly_cast(void
*obj__ign, u32btf_id__k)__weak__ksym;This is b/c the kernel defines
bpf_rdonly_cast()with non-const arg. Since const arg is more permissive and thus backwards compatible, we change the kernel definition as well to avoid conflicting type errors.Signed-off-by Daniel Xu <[email protected]>
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
In reply to Daniel XuQuoted 11 lines · Daniel Xu
Since
20d59ee55172("libbpf: addbpf_core_cast()macro"), libbpf is now exporting a const arg version ofbpf_rdonly_cast(). This causes the following conflicting type error when generating kfunc prototypes from BTF:In file included from
skeleton/pid_iter.bpf.c:5:/home/dxu/dev/linux/tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:297:14: error: conflicting types for 'bpf_rdonly_cast'extern void *bpf_rdonly_cast(const void *obj__ign, __u32 btf_id__k) __ksym __weak; ^./vmlinux.h:135625:14: note: previous declaration is here extern void *bpf_rdonly_cast(void
*obj__ign, u32btf_id__k)__weak__ksym;
hi,
I'm hiting more of these when compiling bpf selftests (attached), it looks like some kfuncs declarations in bpf_kfuncs.h might be in conflict
jirka
---
CLNG-BPF [test_maps] connect_unix_prog.bpf.o
In file included from progs/connect_unix_prog.c:9:/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:11:12: error: conflicting types for 'bpf_dynptr_from_skb' extern int bpf_dynptr_from_skb(struct __sk_buff *skb, __u64 flags,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164853:12: note: previous declaration is here
extern int bpf_dynptr_from_skb(struct sk_buff *skb, u64 flags, struct bpf_dynptr_kern *ptr__uninit) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:19:12: error: conflicting types for 'bpf_dynptr_from_xdp'
extern int bpf_dynptr_from_xdp(struct xdp_md *xdp, __u64 flags,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164855:12: note: previous declaration is here
extern int bpf_dynptr_from_xdp(struct xdp_buff *xdp, u64 flags, struct bpf_dynptr_kern *ptr__uninit) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:28:14: error: conflicting types for 'bpf_dynptr_slice'
extern void *bpf_dynptr_slice(const struct bpf_dynptr *ptr, __u32 offset,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164747:14: note: previous declaration is here
extern void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset, void *buffer__opt, u32 buffer__szk) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:37:14: error: conflicting types for 'bpf_dynptr_slice_rdwr'
extern void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *ptr, __u32 offset,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164749:14: note: previous declaration is here
extern void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset, void *buffer__opt, u32 buffer__szk) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:40:12: error: conflicting types for 'bpf_dynptr_adjust'
extern int bpf_dynptr_adjust(const struct bpf_dynptr *ptr, __u32 start, __u32 end) __ksym __weak;
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164781:12: note: previous declaration is here
extern int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:41:13: error: conflicting types for 'bpf_dynptr_is_null'
extern bool bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym __weak;
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164783:13: note: previous declaration is here
extern bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:42:13: error: conflicting types for 'bpf_dynptr_is_rdonly'
extern bool bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym __weak;
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164785:13: note: previous declaration is here
extern bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:43:14: error: conflicting types for 'bpf_dynptr_size'
extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym __weak;
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164787:14: note: previous declaration is here
extern __u32 bpf_dynptr_size(const struct bpf_dynptr_kern *ptr) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:44:12: error: conflicting types for 'bpf_dynptr_clone'
extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym __weak;
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164789:12: note: previous declaration is here
extern int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr, struct bpf_dynptr_kern *clone__uninit) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:61:12: error: conflicting types for 'bpf_sk_assign_tcp_reqsk'
extern int bpf_sk_assign_tcp_reqsk(struct __sk_buff *skb, struct sock *sk,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164859:12: note: previous declaration is here
extern int bpf_sk_assign_tcp_reqsk(struct sk_buff *skb, struct sock *sk, struct bpf_tcp_req_attrs *attrs, int attrs__sz) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:68:12: error: conflicting types for 'bpf_get_file_xattr'
extern int bpf_get_file_xattr(struct file *file, const char *name,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164691:12: note: previous declaration is here
extern int bpf_get_file_xattr(struct file *file, const char *name__str, struct bpf_dynptr_kern *value_ptr) __weak __ksym;
^
In file included from progs/connect_unix_prog.c:9:
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/bpf_kfuncs.h:75:12: error: conflicting types for 'bpf_verify_pkcs7_signature'
extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
^
/home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/tools/include/vmlinux.h:164689:12: note: previous declaration is here
extern int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr, struct bpf_dynptr_kern *sig_ptr, struct bpf_key *trusted_keyring) __weak __ksym;
^12 errors generated.
make: *** [Makefile:642: /home/jolsa/kernel/linux-qemu/tools/testing/selftests/bpf/connect_unix_prog.bpf.o] Error 1
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Hi Jiri,
Quoted 13 lines · Jiri Olsa
Quoted 11 lines · Daniel Xu
Since
20d59ee55172("libbpf: addbpf_core_cast()macro"), libbpf is now exporting a const arg version ofbpf_rdonly_cast(). This causes the following conflicting type error when generating kfunc prototypes from BTF:In file included from
skeleton/pid_iter.bpf.c:5:/home/dxu/dev/linux/tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:297:14: error: conflicting types for 'bpf_rdonly_cast'extern void *bpf_rdonly_cast(const void *obj__ign, __u32 btf_id__k) __ksym __weak; ^./vmlinux.h:135625:14: note: previous declaration is here extern void *bpf_rdonly_cast(void
*obj__ign, u32btf_id__k)__weak__ksym;
Quoted 3 lines
hi,
I'm hiting more of these when compiling bpf selftests (attached), it looks like some kfuncs declarations inbpf_kfuncs.hmight be in conflict
Yep, I was actually going to put that as an office hours topic on how we want to handle that for selftests. Marking kfuncs in bpf_kfuncs.h and bpf_experimental.h as __weak is an option. ifdef is another option. Final option I can think of is bumping required pahole version up and simply deleting all the kfunc definitions.
But given that pahole changes come with the feature flag, I don't see this as a pressing issue. So I was planning on getting to that after current outstanding patchsets (just so there's less stuff for me to juggle).
[...]
Thanks,
Daniel
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Quoted 26 lines · Daniel Xu
Hi Jiri,
Quoted 13 lines · Jiri Olsa
Quoted 11 lines · Daniel Xu
Since
20d59ee55172("libbpf: addbpf_core_cast()macro"), libbpf is now exporting a const arg version ofbpf_rdonly_cast(). This causes the following conflicting type error when generating kfunc prototypes from BTF:In file included from
skeleton/pid_iter.bpf.c:5:/home/dxu/dev/linux/tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:297:14: error: conflicting types for 'bpf_rdonly_cast'extern void *bpf_rdonly_cast(const void *obj__ign, __u32 btf_id__k) __ksym __weak; ^./vmlinux.h:135625:14: note: previous declaration is here extern void *bpf_rdonly_cast(void
*obj__ign, u32btf_id__k)__weak__ksym;Quoted 3 lines
hi,
I'm hiting more of these when compiling bpf selftests (attached), it looks like some kfuncs declarations inbpf_kfuncs.hmight be in conflictYep, I was actually going to put that as an office hours topic on how we want to handle that for selftests. Marking kfuncs in
bpf_kfuncs.handbpf_experimental.has__weakis an option. ifdef is another option. Final option I can think of is bumping required pahole version up and simply deleting all the kfunc definitions.
Quoted 4 lines
But given that pahole changes come with the feature flag, I don't see this as a pressing issue. So I was planning on getting to that after current outstanding patchsets (just so there's less stuff for me to juggle).
ok, I guess if the fix goes in together with the scripts/Makefile.btf change then we're fine
jirka
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
In reply to Daniel XuQuoted 21 lines · Daniel Xu
Hi Jiri,
Quoted 13 lines · Jiri Olsa
Quoted 11 lines · Daniel Xu
Since
20d59ee55172("libbpf: addbpf_core_cast()macro"), libbpf is now exporting a const arg version ofbpf_rdonly_cast(). This causes the following conflicting type error when generating kfunc prototypes from BTF:In file included from
skeleton/pid_iter.bpf.c:5:/home/dxu/dev/linux/tools/bpf/bpftool/bootstrap/libbpf/include/bpf/bpf_core_read.h:297:14: error: conflicting types for 'bpf_rdonly_cast'extern void *bpf_rdonly_cast(const void *obj__ign, __u32 btf_id__k) __ksym __weak; ^./vmlinux.h:135625:14: note: previous declaration is here extern void *bpf_rdonly_cast(void
*obj__ign, u32btf_id__k)__weak__ksym;Quoted 3 lines
hi,
I'm hiting more of these when compiling bpf selftests (attached), it looks like some kfuncs declarations inbpf_kfuncs.hmight be in conflict
Quoted 5 lines
Yep, I was actually going to put that as an office hours topic on how we want to handle that for selftests. Marking kfuncs in
bpf_kfuncs.handbpf_experimental.has__weakis an option. ifdef is another option. Final option I can think of is bumping required pahole version up and simply deleting all the kfunc definitions.
I think we should mark all kfuncs in selftests as __weak. And then audit all of them and add consts where it makes sense and make sure that all definitions are compatible between vmlinux.h and manual declarations. I'd rather avoid #ifdef'ing anything, it should work as is.
Quoted 10 lines
But given that pahole changes come with the feature flag, I don't see this as a pressing issue. So I was planning on getting to that after current outstanding patchsets (just so there's less stuff for me to juggle).
[...]
Thanks,
Daniel
PATCH v2 2/2 bpftool: Support dumping kfunc prototypes from BTF
In reply to Daniel XuThis 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/[email protected]/
--- tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
tools/bpf/bpftool/btf.c +45 -0
@@ -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" );
Signature · 1 line
2.42.1
Re: [PATCH bpf-next v2 2/2] bpftool: Support dumping kfunc prototypes from BTF
Quoted 43 lines · Daniel Xu
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.htools/testing/selftests/bpf/bpf_experimental.hExample 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/[email protected]/
Signed-off-by Daniel Xu <[email protected]>--- tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)tools/bpf/bpftool/btf.c +2
-0@@ -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" ,
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 461
Quoted 6 lines
tools/bpf/bpftool/btf.c +3
-0@@ -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
Quoted 5 lines
tools/bpf/bpftool/btf.c +5
-0+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);
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 476
Quoted 10 lines
tools/bpf/bpftool/btf.c +10
-0+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?
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 480
Quoted 4 lines
tools/bpf/bpftool/btf.c +4
-0+ +printf ("extern " ); + + kft =btf__type_by_id (btf, t->type);
nit: reuse t?
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 486
Quoted 6 lines
tools/bpf/bpftool/btf.c +6
-0+ 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?
Quoted 35 lines
tools/bpf/bpftool/btf.c +15
-1+ } + +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
Re: [PATCH bpf-next v2 2/2] bpftool: Support dumping kfunc prototypes from BTF
Hi Andrii,
Quoted 47 lines · Andrii Nakryiko
Quoted 46 lines · Daniel Xu
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.htools/testing/selftests/bpf/bpf_experimental.hExample 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/[email protected]/
Signed-off-by Daniel Xu <[email protected]>--- tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)tools/bpf/bpftool/btf.c +2
-0@@ -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 ; }
Quoted 6 lines
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 461
Quoted 3 lines
tools/bpf/bpftool/btf.c +3
-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_OPTSis a "deprecated" macro name I hid, but didn't remove
Ack.
Quoted 11 lines
Quoted 11 lines
tools/bpf/bpftool/btf.c +10
-0+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))
Quoted 8 lines
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 476
Quoted 5 lines
tools/bpf/bpftool/btf.c +5
-0+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.
Quoted 8 lines
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 480
Quoted 5 lines
tools/bpf/bpftool/btf.c +4
-0+ +printf ("extern " ); + + kft =btf__type_by_id (btf, t->type);nit: reuse t?
Quoted 8 lines
tools/bpf/bpftool/btf.c static int dump_btf_raw(const struct btf *btf, · line 486
Quoted 6 lines
tools/bpf/bpftool/btf.c +6
-0+ 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.
Quoted 4 lines
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.
Quoted 32 lines
Quoted 32 lines
tools/bpf/bpftool/btf.c +15
-0+ } + +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; }
Quoted 4 lines
Quoted 4 lines
tools/bpf/bpftool/btf.c +0
-1printf ("#ifndef BPF_NO_PRESERVE_ACCESS_INDEX\n" ); --2.42.1
I'll send the next rev after the pahole changes get merged.
Thanks,
Daniel
Quoted 26 lines · Daniel Xu
This patchset enables dumping kfunc prototypes from bpftool. This is useful b/c with this patchset, 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.htools/testing/selftests/bpf/bpf_experimental.hExample 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 patchset 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/[email protected]/
=== Changelog ===
Quoted 8 lines
From v1:
* Add__weakannotation
* Usebtf_dumpfor kfunc prototypes
* Update kernelbpf_rdonly_cast()signatureDaniel Xu (2):
bpf: Havebpf_rdonly_cast()take a const pointer
bpftool: Support dumping kfunc prototypes from BTF
I've applied patch #1 as it's a good change regardless. Please send v2 for patch #2.
Quoted 8 lines
kernel/bpf/helpers.c | 4 ++-- tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-)--
2.42.1
Quoted 32 lines · Andrii Nakryiko
Quoted 31 lines · Daniel Xu
This patchset enables dumping kfunc prototypes from bpftool. This is useful b/c with this patchset, 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.htools/testing/selftests/bpf/bpf_experimental.hExample 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 patchset 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/[email protected]/
=== Changelog ===
From v1:
* Add__weakannotation
* Usebtf_dumpfor kfunc prototypes
* Update kernelbpf_rdonly_cast()signature
Quoted 6 lines
Quoted 3 lines
Daniel Xu (2):
bpf: Havebpf_rdonly_cast()take a const pointer
bpftool: Support dumping kfunc prototypes from BTFI've applied patch #1 as it's a good change regardless. Please send v2 for patch #2.
Ack. Been a bit busy recently - will probably have time to work on this again next weekend.