2024-02-04 21:07:10

by Daniel Xu

[permalink] [raw]
Subject: [PATCH bpf-next v2 0/2] 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(-)

--
2.42.1



2024-02-04 21:07:17

by Daniel Xu

[permalink] [raw]
Subject: [PATCH bpf-next 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.

Signed-off-by: Daniel Xu <[email protected]>
---
kernel/bpf/helpers.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 4db1c658254c..3503949b4c1b 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -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)
--
2.42.1


2024-02-04 21:07:50

by Daniel Xu

[permalink] [raw]
Subject: [PATCH bpf-next v2 2/2] bpftool: Support dumping kfunc prototypes from BTF

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/[email protected]/

Signed-off-by: Daniel Xu <[email protected]>
---
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);
+ 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");
--
2.42.1


2024-02-05 00:33:17

by Yonghong Song

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer


On 2/4/24 1:06 PM, Daniel Xu wrote:
> 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.
>
> Signed-off-by: Daniel Xu <[email protected]>

Acked-by: Yonghong Song <[email protected]>


2024-02-06 13:44:55

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer

On Sun, Feb 04, 2024 at 02:06:34PM -0700, Daniel Xu wrote:
> 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;

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

2024-02-06 15:44:48

by Daniel Xu

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer

Hi Jiri,

On Tue, Feb 06, 2024 at 02:42:22PM +0100, Jiri Olsa wrote:
> On Sun, Feb 04, 2024 at 02:06:34PM -0700, Daniel Xu wrote:
> > 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;
>
> 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

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

2024-02-06 16:03:43

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer

On Tue, Feb 06, 2024 at 08:44:18AM -0700, Daniel Xu wrote:
> Hi Jiri,
>
> On Tue, Feb 06, 2024 at 02:42:22PM +0100, Jiri Olsa wrote:
> > On Sun, Feb 04, 2024 at 02:06:34PM -0700, Daniel Xu wrote:
> > > 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;
> >
> > 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
>
> 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).

ok, I guess if the fix goes in together with the scripts/Makefile.btf
change then we're fine

jirka

2024-02-06 17:32:33

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer

On Tue, Feb 6, 2024 at 7:44 AM Daniel Xu <[email protected]> wrote:
>
> Hi Jiri,
>
> On Tue, Feb 06, 2024 at 02:42:22PM +0100, Jiri Olsa wrote:
> > On Sun, Feb 04, 2024 at 02:06:34PM -0700, Daniel Xu wrote:
> > > 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;
> >
> > 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
>
> 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.

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.


>
> 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

2024-02-08 00:50:39

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 2/2] bpftool: Support dumping kfunc prototypes from BTF

On Sun, Feb 4, 2024 at 1:07 PM Daniel Xu <[email protected]> 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/[email protected]/
>
> Signed-off-by: Daniel Xu <[email protected]>
> ---
> 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
>

2024-02-13 19:09:53

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 0/2] bpf, bpftool: Support dumping kfunc prototypes from BTF

On Sun, Feb 4, 2024 at 1:06 PM Daniel Xu <[email protected]> wrote:
>
> 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

I've applied patch #1 as it's a good change regardless. Please send v2
for patch #2.

>
> kernel/bpf/helpers.c | 4 ++--
> tools/bpf/bpftool/btf.c | 45 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 47 insertions(+), 2 deletions(-)
>
> --
> 2.42.1
>

2024-02-13 21:23:14

by Daniel Xu

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 0/2] bpf, bpftool: Support dumping kfunc prototypes from BTF

On Tue, Feb 13, 2024 at 11:09:29AM -0800, Andrii Nakryiko wrote:
> On Sun, Feb 4, 2024 at 1:06 PM Daniel Xu <[email protected]> wrote:
> >
> > 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
>
> I'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.


2024-03-18 18:37:35

by Daniel Xu

[permalink] [raw]
Subject: Re: [PATCH bpf-next v2 2/2] bpftool: Support dumping kfunc prototypes from BTF

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 <[email protected]> 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/[email protected]/
> >
> > Signed-off-by: Daniel Xu <[email protected]>
> > ---
> > 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