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

Daniel Xu posted 2 patches 2 years ago
[PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Posted by Daniel Xu 2 years ago
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 <dxu@dxuuu.xyz>
---
 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
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Posted by Jiri Olsa 2 years ago
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
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Posted by Daniel Xu 2 years ago
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
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Posted by Andrii Nakryiko 2 years ago
On Tue, Feb 6, 2024 at 7:44 AM Daniel Xu <dxu@dxuuu.xyz> 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
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Posted by Jiri Olsa 2 years ago
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
Re: [PATCH bpf-next v2 1/2] bpf: Have bpf_rdonly_cast() take a const pointer
Posted by Yonghong Song 2 years ago
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 <dxu@dxuuu.xyz>

Acked-by: Yonghong Song <yonghong.song@linux.dev>