diff mbox series

[bpf-next,v4,06/12] bpf: selftests: Fix bpf_session_cookie() kfunc prototype

Message ID 34708481d71ea72c23a78a5209e04a76b261a01d.1717881178.git.dxu@dxuuu.xyz (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Support dumping kfunc prototypes from BTF | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Unittests
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-11 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-18 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / build-release
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-17 / build / build for x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-17 / build-release / build for x86_64 with llvm-17-O2
bpf/vmtest-bpf-next-VM_Test-34 success Logs for x86_64-llvm-17 / veristat
bpf/vmtest-bpf-next-VM_Test-35 success Logs for x86_64-llvm-18 / build / build for x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-36 success Logs for x86_64-llvm-18 / build-release / build for x86_64 with llvm-18-O2
bpf/vmtest-bpf-next-VM_Test-42 success Logs for x86_64-llvm-18 / veristat
bpf/vmtest-bpf-next-VM_Test-9 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_maps, false, 360) / test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / test (test_progs, false, 360) / test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-30 success Logs for x86_64-llvm-17 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-31 success Logs for x86_64-llvm-17 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-32 success Logs for x86_64-llvm-17 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-33 success Logs for x86_64-llvm-17 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-17
bpf/vmtest-bpf-next-VM_Test-37 success Logs for x86_64-llvm-18 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-41 success Logs for x86_64-llvm-18 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-38 success Logs for x86_64-llvm-18 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-39 success Logs for x86_64-llvm-18 / test (test_progs_cpuv4, false, 360) / test_progs_cpuv4 on x86_64 with llvm-18
bpf/vmtest-bpf-next-VM_Test-40 success Logs for x86_64-llvm-18 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-18
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/apply fail Patch does not apply to bpf-next-0

Commit Message

Daniel Xu June 8, 2024, 9:16 p.m. UTC
The prototype defined in bpf_kfuncs.h was not in line with how the
actual kfunc was defined. This causes compilation errors when kfunc
prototypes are generated from BTF.

Fix by aligning with actual kfunc definition.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
---
 tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
 tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Comments

Jiri Olsa June 10, 2024, 1:04 p.m. UTC | #1
On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> The prototype defined in bpf_kfuncs.h was not in line with how the
> actual kfunc was defined. This causes compilation errors when kfunc
> prototypes are generated from BTF.
> 
> Fix by aligning with actual kfunc definition.
> 
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---
>  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
>  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> index be91a6919315..3b6675ab4086 100644
> --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
>  				      struct bpf_key *trusted_keyring) __ksym;
>  
>  extern bool bpf_session_is_return(void) __ksym __weak;
> -extern long *bpf_session_cookie(void) __ksym __weak;
> +extern __u64 *bpf_session_cookie(void) __ksym __weak;

the original intent was to expose long instead of __u64 :-\

could we rather change the bpf_session_cookie function to return long?
should be just return value type change

thanks,
jirka


>  #endif
> diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> index d49070803e22..0835b5edf685 100644
> --- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> @@ -25,7 +25,7 @@ int BPF_PROG(trigger)
>  
>  static int check_cookie(__u64 val, __u64 *result)
>  {
> -	long *cookie;
> +	__u64 *cookie;
>  
>  	if (bpf_get_current_pid_tgid() >> 32 != pid)
>  		return 1;
> -- 
> 2.44.0
>
Daniel Xu June 11, 2024, 3:54 p.m. UTC | #2
Hi Jiri,

On Mon, Jun 10, 2024 at 03:04:25PM GMT, Jiri Olsa wrote:
> On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > The prototype defined in bpf_kfuncs.h was not in line with how the
> > actual kfunc was defined. This causes compilation errors when kfunc
> > prototypes are generated from BTF.
> > 
> > Fix by aligning with actual kfunc definition.
> > 
> > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > ---
> >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> >  2 files changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > index be91a6919315..3b6675ab4086 100644
> > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> >  				      struct bpf_key *trusted_keyring) __ksym;
> >  
> >  extern bool bpf_session_is_return(void) __ksym __weak;
> > -extern long *bpf_session_cookie(void) __ksym __weak;
> > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
> 
> the original intent was to expose long instead of __u64 :-\
> 
> could we rather change the bpf_session_cookie function to return long?
> should be just return value type change

Sounds reasonable to me. I don't think the kfunc has made it to a
release yet, so perhaps if we extract this commit out as a fix to bpf
tree it can still make it into 6.10. That way we won't have to worry
about any ABI changes.

Thanks,
Daniel
Alexei Starovoitov June 11, 2024, 4:03 p.m. UTC | #3
On Tue, Jun 11, 2024 at 8:54 AM Daniel Xu <dxu@dxuuu.xyz> wrote:
>
> Hi Jiri,
>
> On Mon, Jun 10, 2024 at 03:04:25PM GMT, Jiri Olsa wrote:
> > On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > > The prototype defined in bpf_kfuncs.h was not in line with how the
> > > actual kfunc was defined. This causes compilation errors when kfunc
> > > prototypes are generated from BTF.
> > >
> > > Fix by aligning with actual kfunc definition.
> > >
> > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > > ---
> > >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> > >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> > >  2 files changed, 2 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > index be91a6919315..3b6675ab4086 100644
> > > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> > >                                   struct bpf_key *trusted_keyring) __ksym;
> > >
> > >  extern bool bpf_session_is_return(void) __ksym __weak;
> > > -extern long *bpf_session_cookie(void) __ksym __weak;
> > > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
> >
> > the original intent was to expose long instead of __u64 :-\
> >
> > could we rather change the bpf_session_cookie function to return long?
> > should be just return value type change
>
> Sounds reasonable to me. I don't think the kfunc has made it to a
> release yet, so perhaps if we extract this commit out as a fix to bpf
> tree it can still make it into 6.10. That way we won't have to worry
> about any ABI changes.

kfunc-s can be changed at any time. Keep targeting bpf-next for everything.
Andrii Nakryiko June 17, 2024, 10:25 p.m. UTC | #4
On Mon, Jun 10, 2024 at 6:04 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > The prototype defined in bpf_kfuncs.h was not in line with how the
> > actual kfunc was defined. This causes compilation errors when kfunc
> > prototypes are generated from BTF.
> >
> > Fix by aligning with actual kfunc definition.
> >
> > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > ---
> >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> >  2 files changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > index be91a6919315..3b6675ab4086 100644
> > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> >                                     struct bpf_key *trusted_keyring) __ksym;
> >
> >  extern bool bpf_session_is_return(void) __ksym __weak;
> > -extern long *bpf_session_cookie(void) __ksym __weak;
> > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
>
> the original intent was to expose long instead of __u64 :-\
>

Cookies internally are always u64 (8 byte values). Marking them
internally in the kernel as long could lead to problems on 32-bit
architectures, potentially (it still needs to be 64-bit value
according to BPF contract, but we'll allocate only 4 bytes for them).

It seems better and safer to be explicit with __u64/u64 for cookies everywhere.

What am I missing?

> could we rather change the bpf_session_cookie function to return long?
> should be just return value type change
>
> thanks,
> jirka
>
>
> >  #endif
> > diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > index d49070803e22..0835b5edf685 100644
> > --- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > @@ -25,7 +25,7 @@ int BPF_PROG(trigger)
> >
> >  static int check_cookie(__u64 val, __u64 *result)
> >  {
> > -     long *cookie;
> > +     __u64 *cookie;
> >
> >       if (bpf_get_current_pid_tgid() >> 32 != pid)
> >               return 1;
> > --
> > 2.44.0
> >
Jiri Olsa June 18, 2024, 12:43 p.m. UTC | #5
On Mon, Jun 17, 2024 at 03:25:53PM -0700, Andrii Nakryiko wrote:
> On Mon, Jun 10, 2024 at 6:04 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > > The prototype defined in bpf_kfuncs.h was not in line with how the
> > > actual kfunc was defined. This causes compilation errors when kfunc
> > > prototypes are generated from BTF.
> > >
> > > Fix by aligning with actual kfunc definition.
> > >
> > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > > ---
> > >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> > >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> > >  2 files changed, 2 insertions(+), 2 deletions(-)
> > >
> > > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > index be91a6919315..3b6675ab4086 100644
> > > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> > >                                     struct bpf_key *trusted_keyring) __ksym;
> > >
> > >  extern bool bpf_session_is_return(void) __ksym __weak;
> > > -extern long *bpf_session_cookie(void) __ksym __weak;
> > > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
> >
> > the original intent was to expose long instead of __u64 :-\
> >
> 
> Cookies internally are always u64 (8 byte values). Marking them
> internally in the kernel as long could lead to problems on 32-bit
> architectures, potentially (it still needs to be 64-bit value
> according to BPF contract, but we'll allocate only 4 bytes for them).
> 
> It seems better and safer to be explicit with __u64/u64 for cookies everywhere.

hum, I based that on what we did for kprobe session,
but I guess it makes sense just for bpf side:

  https://lore.kernel.org/bpf/CAEf4BzbyQpKvZS-mUECLRq3gyBJbsqQghOKyAbutoB76mJM8xw@mail.gmail.com/

jirka

> 
> What am I missing?
> 
> > could we rather change the bpf_session_cookie function to return long?
> > should be just return value type change
> >
> > thanks,
> > jirka
> >
> >
> > >  #endif
> > > diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > > index d49070803e22..0835b5edf685 100644
> > > --- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > > +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > > @@ -25,7 +25,7 @@ int BPF_PROG(trigger)
> > >
> > >  static int check_cookie(__u64 val, __u64 *result)
> > >  {
> > > -     long *cookie;
> > > +     __u64 *cookie;
> > >
> > >       if (bpf_get_current_pid_tgid() >> 32 != pid)
> > >               return 1;
> > > --
> > > 2.44.0
> > >
Andrii Nakryiko June 18, 2024, 4:58 p.m. UTC | #6
On Tue, Jun 18, 2024 at 5:43 AM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Mon, Jun 17, 2024 at 03:25:53PM -0700, Andrii Nakryiko wrote:
> > On Mon, Jun 10, 2024 at 6:04 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> > >
> > > On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > > > The prototype defined in bpf_kfuncs.h was not in line with how the
> > > > actual kfunc was defined. This causes compilation errors when kfunc
> > > > prototypes are generated from BTF.
> > > >
> > > > Fix by aligning with actual kfunc definition.
> > > >
> > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > > > ---
> > > >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> > > >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> > > >  2 files changed, 2 insertions(+), 2 deletions(-)
> > > >
> > > > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > index be91a6919315..3b6675ab4086 100644
> > > > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> > > >                                     struct bpf_key *trusted_keyring) __ksym;
> > > >
> > > >  extern bool bpf_session_is_return(void) __ksym __weak;
> > > > -extern long *bpf_session_cookie(void) __ksym __weak;
> > > > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
> > >
> > > the original intent was to expose long instead of __u64 :-\
> > >
> >
> > Cookies internally are always u64 (8 byte values). Marking them
> > internally in the kernel as long could lead to problems on 32-bit
> > architectures, potentially (it still needs to be 64-bit value
> > according to BPF contract, but we'll allocate only 4 bytes for them).
> >
> > It seems better and safer to be explicit with __u64/u64 for cookies everywhere.
>
> hum, I based that on what we did for kprobe session,
> but I guess it makes sense just for bpf side:

yep, exactly, long is 64-bit only for BPF "architecture", but
internally it will be 4 bytes for 32-bit architectures, which will
potentially lead to problems. With recent kfunc vmlinux.h generation,
it's probably better to stick to explicitly sized types.

>
>   https://lore.kernel.org/bpf/CAEf4BzbyQpKvZS-mUECLRq3gyBJbsqQghOKyAbutoB76mJM8xw@mail.gmail.com/
>
> jirka
>
> >
> > What am I missing?
> >
> > > could we rather change the bpf_session_cookie function to return long?
> > > should be just return value type change
> > >
> > > thanks,
> > > jirka
> > >
> > >
> > > >  #endif
> > > > diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > > > index d49070803e22..0835b5edf685 100644
> > > > --- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > > > +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> > > > @@ -25,7 +25,7 @@ int BPF_PROG(trigger)
> > > >
> > > >  static int check_cookie(__u64 val, __u64 *result)
> > > >  {
> > > > -     long *cookie;
> > > > +     __u64 *cookie;
> > > >
> > > >       if (bpf_get_current_pid_tgid() >> 32 != pid)
> > > >               return 1;
> > > > --
> > > > 2.44.0
> > > >
Jiri Olsa June 18, 2024, 7:07 p.m. UTC | #7
On Tue, Jun 18, 2024 at 09:58:23AM -0700, Andrii Nakryiko wrote:
> On Tue, Jun 18, 2024 at 5:43 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > On Mon, Jun 17, 2024 at 03:25:53PM -0700, Andrii Nakryiko wrote:
> > > On Mon, Jun 10, 2024 at 6:04 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> > > >
> > > > On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > > > > The prototype defined in bpf_kfuncs.h was not in line with how the
> > > > > actual kfunc was defined. This causes compilation errors when kfunc
> > > > > prototypes are generated from BTF.
> > > > >
> > > > > Fix by aligning with actual kfunc definition.
> > > > >
> > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > > > > ---
> > > > >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> > > > >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> > > > >  2 files changed, 2 insertions(+), 2 deletions(-)
> > > > >
> > > > > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > > index be91a6919315..3b6675ab4086 100644
> > > > > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> > > > >                                     struct bpf_key *trusted_keyring) __ksym;
> > > > >
> > > > >  extern bool bpf_session_is_return(void) __ksym __weak;
> > > > > -extern long *bpf_session_cookie(void) __ksym __weak;
> > > > > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
> > > >
> > > > the original intent was to expose long instead of __u64 :-\
> > > >
> > >
> > > Cookies internally are always u64 (8 byte values). Marking them
> > > internally in the kernel as long could lead to problems on 32-bit
> > > architectures, potentially (it still needs to be 64-bit value
> > > according to BPF contract, but we'll allocate only 4 bytes for them).
> > >
> > > It seems better and safer to be explicit with __u64/u64 for cookies everywhere.
> >
> > hum, I based that on what we did for kprobe session,
> > but I guess it makes sense just for bpf side:
> 
> yep, exactly, long is 64-bit only for BPF "architecture", but
> internally it will be 4 bytes for 32-bit architectures, which will
> potentially lead to problems. With recent kfunc vmlinux.h generation,
> it's probably better to stick to explicitly sized types.

hm, it already got in 2b8dd87332cd, revert needs more changes in selftests
I'll send formal patch with fix below

jirka

---
diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
index 4b3fda456299..cd098846e251 100644
--- a/kernel/trace/bpf_trace.c
+++ b/kernel/trace/bpf_trace.c
@@ -3530,7 +3530,7 @@ __bpf_kfunc bool bpf_session_is_return(void)
 	return session_ctx->is_return;
 }
 
-__bpf_kfunc long *bpf_session_cookie(void)
+__bpf_kfunc __u64 *bpf_session_cookie(void)
 {
 	struct bpf_session_run_ctx *session_ctx;
 
diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
index be91a6919315..3b6675ab4086 100644
--- a/tools/testing/selftests/bpf/bpf_kfuncs.h
+++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
@@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
 				      struct bpf_key *trusted_keyring) __ksym;
 
 extern bool bpf_session_is_return(void) __ksym __weak;
-extern long *bpf_session_cookie(void) __ksym __weak;
+extern __u64 *bpf_session_cookie(void) __ksym __weak;
 #endif
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
index d49070803e22..0835b5edf685 100644
--- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
+++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
@@ -25,7 +25,7 @@ int BPF_PROG(trigger)
 
 static int check_cookie(__u64 val, __u64 *result)
 {
-	long *cookie;
+	__u64 *cookie;
 
 	if (bpf_get_current_pid_tgid() >> 32 != pid)
 		return 1;
Andrii Nakryiko June 18, 2024, 8:29 p.m. UTC | #8
On Tue, Jun 18, 2024 at 12:08 PM Jiri Olsa <olsajiri@gmail.com> wrote:
>
> On Tue, Jun 18, 2024 at 09:58:23AM -0700, Andrii Nakryiko wrote:
> > On Tue, Jun 18, 2024 at 5:43 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> > >
> > > On Mon, Jun 17, 2024 at 03:25:53PM -0700, Andrii Nakryiko wrote:
> > > > On Mon, Jun 10, 2024 at 6:04 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> > > > >
> > > > > On Sat, Jun 08, 2024 at 03:16:02PM -0600, Daniel Xu wrote:
> > > > > > The prototype defined in bpf_kfuncs.h was not in line with how the
> > > > > > actual kfunc was defined. This causes compilation errors when kfunc
> > > > > > prototypes are generated from BTF.
> > > > > >
> > > > > > Fix by aligning with actual kfunc definition.
> > > > > >
> > > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> > > > > > ---
> > > > > >  tools/testing/selftests/bpf/bpf_kfuncs.h                        | 2 +-
> > > > > >  tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c | 2 +-
> > > > > >  2 files changed, 2 insertions(+), 2 deletions(-)
> > > > > >
> > > > > > diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > > > index be91a6919315..3b6675ab4086 100644
> > > > > > --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > > > +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> > > > > > @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
> > > > > >                                     struct bpf_key *trusted_keyring) __ksym;
> > > > > >
> > > > > >  extern bool bpf_session_is_return(void) __ksym __weak;
> > > > > > -extern long *bpf_session_cookie(void) __ksym __weak;
> > > > > > +extern __u64 *bpf_session_cookie(void) __ksym __weak;
> > > > >
> > > > > the original intent was to expose long instead of __u64 :-\
> > > > >
> > > >
> > > > Cookies internally are always u64 (8 byte values). Marking them
> > > > internally in the kernel as long could lead to problems on 32-bit
> > > > architectures, potentially (it still needs to be 64-bit value
> > > > according to BPF contract, but we'll allocate only 4 bytes for them).
> > > >
> > > > It seems better and safer to be explicit with __u64/u64 for cookies everywhere.
> > >
> > > hum, I based that on what we did for kprobe session,
> > > but I guess it makes sense just for bpf side:
> >
> > yep, exactly, long is 64-bit only for BPF "architecture", but
> > internally it will be 4 bytes for 32-bit architectures, which will
> > potentially lead to problems. With recent kfunc vmlinux.h generation,
> > it's probably better to stick to explicitly sized types.
>
> hm, it already got in 2b8dd87332cd, revert needs more changes in selftests
> I'll send formal patch with fix below

Yeah, I was a bit late to the party. But I replied on the original
thread as well, I think we should use __u64 (or unsigned long long if
we worry about __u64 typedef, but I think at least for vmlinux.h it
doesn't matter).

And thanks for working on a fix!

>
> jirka
>
> ---
> diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c
> index 4b3fda456299..cd098846e251 100644
> --- a/kernel/trace/bpf_trace.c
> +++ b/kernel/trace/bpf_trace.c
> @@ -3530,7 +3530,7 @@ __bpf_kfunc bool bpf_session_is_return(void)
>         return session_ctx->is_return;
>  }
>
> -__bpf_kfunc long *bpf_session_cookie(void)
> +__bpf_kfunc __u64 *bpf_session_cookie(void)
>  {
>         struct bpf_session_run_ctx *session_ctx;
>
> diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
> index be91a6919315..3b6675ab4086 100644
> --- a/tools/testing/selftests/bpf/bpf_kfuncs.h
> +++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
> @@ -77,5 +77,5 @@ extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
>                                       struct bpf_key *trusted_keyring) __ksym;
>
>  extern bool bpf_session_is_return(void) __ksym __weak;
> -extern long *bpf_session_cookie(void) __ksym __weak;
> +extern __u64 *bpf_session_cookie(void) __ksym __weak;
>  #endif
> diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> index d49070803e22..0835b5edf685 100644
> --- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> +++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
> @@ -25,7 +25,7 @@ int BPF_PROG(trigger)
>
>  static int check_cookie(__u64 val, __u64 *result)
>  {
> -       long *cookie;
> +       __u64 *cookie;
>
>         if (bpf_get_current_pid_tgid() >> 32 != pid)
>                 return 1;
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/bpf_kfuncs.h b/tools/testing/selftests/bpf/bpf_kfuncs.h
index be91a6919315..3b6675ab4086 100644
--- a/tools/testing/selftests/bpf/bpf_kfuncs.h
+++ b/tools/testing/selftests/bpf/bpf_kfuncs.h
@@ -77,5 +77,5 @@  extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
 				      struct bpf_key *trusted_keyring) __ksym;
 
 extern bool bpf_session_is_return(void) __ksym __weak;
-extern long *bpf_session_cookie(void) __ksym __weak;
+extern __u64 *bpf_session_cookie(void) __ksym __weak;
 #endif
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
index d49070803e22..0835b5edf685 100644
--- a/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
+++ b/tools/testing/selftests/bpf/progs/kprobe_multi_session_cookie.c
@@ -25,7 +25,7 @@  int BPF_PROG(trigger)
 
 static int check_cookie(__u64 val, __u64 *result)
 {
-	long *cookie;
+	__u64 *cookie;
 
 	if (bpf_get_current_pid_tgid() >> 32 != pid)
 		return 1;