diff mbox series

[PATCHv3,bpf-next,7/7] selftests/bpf: Add test for bpf_get_func_ip in kprobe+offset probe

Message ID 20210707214751.159713-8-jolsa@kernel.org (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf, x86: Add bpf_get_func_ip helper | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 4 maintainers not CCed: linux-kselftest@vger.kernel.org andrii@kernel.org shuah@kernel.org kpsingh@kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning CHECK: No space is necessary after a cast WARNING: From:/Signed-off-by: email address mismatch: 'From: Jiri Olsa <jolsa@redhat.com>' != 'Signed-off-by: Jiri Olsa <jolsa@kernel.org>' WARNING: externs should be avoided in .c files
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success Link

Commit Message

Jiri Olsa July 7, 2021, 9:47 p.m. UTC
Adding test for bpf_get_func_ip in kprobe+ofset probe.
Because of the offset value it's arch specific, adding
it only for x86_64 architecture.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
 .../testing/selftests/bpf/progs/get_func_ip_test.c  | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Andrii Nakryiko July 8, 2021, 12:18 a.m. UTC | #1
On Wed, Jul 7, 2021 at 2:54 PM Jiri Olsa <jolsa@redhat.com> wrote:
>
> Adding test for bpf_get_func_ip in kprobe+ofset probe.

typo: offset

> Because of the offset value it's arch specific, adding
> it only for x86_64 architecture.

I'm not following, you specified +0x5 offset explicitly, why is this
arch-specific?

>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
>  .../testing/selftests/bpf/progs/get_func_ip_test.c  | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> index 8ca54390d2b1..e8a9428a0ea3 100644
> --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> @@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym;
>  extern const void bpf_fentry_test3 __ksym;
>  extern const void bpf_fentry_test4 __ksym;
>  extern const void bpf_modify_return_test __ksym;
> +extern const void bpf_fentry_test6 __ksym;
>
>  __u64 test1_result = 0;
>  SEC("fentry/bpf_fentry_test1")
> @@ -60,3 +61,15 @@ int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
>         test5_result = (const void *) addr == &bpf_modify_return_test;
>         return ret;
>  }
> +
> +#ifdef __x86_64__
> +__u64 test6_result = 0;

see, and you just forgot to update the user-space part of the test to
even check test6_result...

please group variables together and do explicit ASSERT_EQ

> +SEC("kprobe/bpf_fentry_test6+0x5")
> +int test6(struct pt_regs *ctx)
> +{
> +       __u64 addr = bpf_get_func_ip(ctx);
> +
> +       test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
> +       return 0;
> +}
> +#endif
> --
> 2.31.1
>
Jiri Olsa July 11, 2021, 2:48 p.m. UTC | #2
On Wed, Jul 07, 2021 at 05:18:49PM -0700, Andrii Nakryiko wrote:
> On Wed, Jul 7, 2021 at 2:54 PM Jiri Olsa <jolsa@redhat.com> wrote:
> >
> > Adding test for bpf_get_func_ip in kprobe+ofset probe.
> 
> typo: offset
> 
> > Because of the offset value it's arch specific, adding
> > it only for x86_64 architecture.
> 
> I'm not following, you specified +0x5 offset explicitly, why is this
> arch-specific?

I need some instruction offset != 0 in the traced function,
x86_64's fentry jump is 5 bytes, other archs will be different

> 
> >
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  .../testing/selftests/bpf/progs/get_func_ip_test.c  | 13 +++++++++++++
> >  1 file changed, 13 insertions(+)
> >
> > diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > index 8ca54390d2b1..e8a9428a0ea3 100644
> > --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > @@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym;
> >  extern const void bpf_fentry_test3 __ksym;
> >  extern const void bpf_fentry_test4 __ksym;
> >  extern const void bpf_modify_return_test __ksym;
> > +extern const void bpf_fentry_test6 __ksym;
> >
> >  __u64 test1_result = 0;
> >  SEC("fentry/bpf_fentry_test1")
> > @@ -60,3 +61,15 @@ int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
> >         test5_result = (const void *) addr == &bpf_modify_return_test;
> >         return ret;
> >  }
> > +
> > +#ifdef __x86_64__
> > +__u64 test6_result = 0;
> 
> see, and you just forgot to update the user-space part of the test to
> even check test6_result...
> 
> please group variables together and do explicit ASSERT_EQ

right.. will change

thanks,
jirka

> 
> > +SEC("kprobe/bpf_fentry_test6+0x5")
> > +int test6(struct pt_regs *ctx)
> > +{
> > +       __u64 addr = bpf_get_func_ip(ctx);
> > +
> > +       test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
> > +       return 0;
> > +}
> > +#endif
> > --
> > 2.31.1
> >
>
Andrii Nakryiko July 12, 2021, 11:32 p.m. UTC | #3
On Sun, Jul 11, 2021 at 7:48 AM Jiri Olsa <jolsa@redhat.com> wrote:
>
> On Wed, Jul 07, 2021 at 05:18:49PM -0700, Andrii Nakryiko wrote:
> > On Wed, Jul 7, 2021 at 2:54 PM Jiri Olsa <jolsa@redhat.com> wrote:
> > >
> > > Adding test for bpf_get_func_ip in kprobe+ofset probe.
> >
> > typo: offset
> >
> > > Because of the offset value it's arch specific, adding
> > > it only for x86_64 architecture.
> >
> > I'm not following, you specified +0x5 offset explicitly, why is this
> > arch-specific?
>
> I need some instruction offset != 0 in the traced function,
> x86_64's fentry jump is 5 bytes, other archs will be different

Right, ok. I don't see an easy way to detect this offset, but the
#ifdef __x86_64__ detection doesn't work because we are compiling with
-target bpf. Please double-check that it actually worked in the first
place.

I think a better way would be to have test6 defined unconditionally in
BPF code, but then disable loading test6 program on anything but
x86_64 platform at runtime with bpf_program__set_autoload(false).

>
> >
> > >
> > > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > > ---
> > >  .../testing/selftests/bpf/progs/get_func_ip_test.c  | 13 +++++++++++++
> > >  1 file changed, 13 insertions(+)
> > >
> > > diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > > index 8ca54390d2b1..e8a9428a0ea3 100644
> > > --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > > +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > > @@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym;
> > >  extern const void bpf_fentry_test3 __ksym;
> > >  extern const void bpf_fentry_test4 __ksym;
> > >  extern const void bpf_modify_return_test __ksym;
> > > +extern const void bpf_fentry_test6 __ksym;
> > >
> > >  __u64 test1_result = 0;
> > >  SEC("fentry/bpf_fentry_test1")
> > > @@ -60,3 +61,15 @@ int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
> > >         test5_result = (const void *) addr == &bpf_modify_return_test;
> > >         return ret;
> > >  }
> > > +
> > > +#ifdef __x86_64__
> > > +__u64 test6_result = 0;
> >
> > see, and you just forgot to update the user-space part of the test to
> > even check test6_result...
> >
> > please group variables together and do explicit ASSERT_EQ
>
> right.. will change
>
> thanks,
> jirka
>
> >
> > > +SEC("kprobe/bpf_fentry_test6+0x5")
> > > +int test6(struct pt_regs *ctx)
> > > +{
> > > +       __u64 addr = bpf_get_func_ip(ctx);
> > > +
> > > +       test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
> > > +       return 0;
> > > +}
> > > +#endif
> > > --
> > > 2.31.1
> > >
> >
>
Jiri Olsa July 13, 2021, 2:15 p.m. UTC | #4
On Mon, Jul 12, 2021 at 04:32:25PM -0700, Andrii Nakryiko wrote:
> On Sun, Jul 11, 2021 at 7:48 AM Jiri Olsa <jolsa@redhat.com> wrote:
> >
> > On Wed, Jul 07, 2021 at 05:18:49PM -0700, Andrii Nakryiko wrote:
> > > On Wed, Jul 7, 2021 at 2:54 PM Jiri Olsa <jolsa@redhat.com> wrote:
> > > >
> > > > Adding test for bpf_get_func_ip in kprobe+ofset probe.
> > >
> > > typo: offset
> > >
> > > > Because of the offset value it's arch specific, adding
> > > > it only for x86_64 architecture.
> > >
> > > I'm not following, you specified +0x5 offset explicitly, why is this
> > > arch-specific?
> >
> > I need some instruction offset != 0 in the traced function,
> > x86_64's fentry jump is 5 bytes, other archs will be different
> 
> Right, ok. I don't see an easy way to detect this offset, but the
> #ifdef __x86_64__ detection doesn't work because we are compiling with
> -target bpf. Please double-check that it actually worked in the first
> place.

ugh, right

> 
> I think a better way would be to have test6 defined unconditionally in
> BPF code, but then disable loading test6 program on anything but
> x86_64 platform at runtime with bpf_program__set_autoload(false).

great, I did not know about this function, will be easier

thanks,
jirka

> 
> >
> > >
> > > >
> > > > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > > > ---
> > > >  .../testing/selftests/bpf/progs/get_func_ip_test.c  | 13 +++++++++++++
> > > >  1 file changed, 13 insertions(+)
> > > >
> > > > diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > > > index 8ca54390d2b1..e8a9428a0ea3 100644
> > > > --- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > > > +++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
> > > > @@ -10,6 +10,7 @@ extern const void bpf_fentry_test2 __ksym;
> > > >  extern const void bpf_fentry_test3 __ksym;
> > > >  extern const void bpf_fentry_test4 __ksym;
> > > >  extern const void bpf_modify_return_test __ksym;
> > > > +extern const void bpf_fentry_test6 __ksym;
> > > >
> > > >  __u64 test1_result = 0;
> > > >  SEC("fentry/bpf_fentry_test1")
> > > > @@ -60,3 +61,15 @@ int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
> > > >         test5_result = (const void *) addr == &bpf_modify_return_test;
> > > >         return ret;
> > > >  }
> > > > +
> > > > +#ifdef __x86_64__
> > > > +__u64 test6_result = 0;
> > >
> > > see, and you just forgot to update the user-space part of the test to
> > > even check test6_result...
> > >
> > > please group variables together and do explicit ASSERT_EQ
> >
> > right.. will change
> >
> > thanks,
> > jirka
> >
> > >
> > > > +SEC("kprobe/bpf_fentry_test6+0x5")
> > > > +int test6(struct pt_regs *ctx)
> > > > +{
> > > > +       __u64 addr = bpf_get_func_ip(ctx);
> > > > +
> > > > +       test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
> > > > +       return 0;
> > > > +}
> > > > +#endif
> > > > --
> > > > 2.31.1
> > > >
> > >
> >
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
index 8ca54390d2b1..e8a9428a0ea3 100644
--- a/tools/testing/selftests/bpf/progs/get_func_ip_test.c
+++ b/tools/testing/selftests/bpf/progs/get_func_ip_test.c
@@ -10,6 +10,7 @@  extern const void bpf_fentry_test2 __ksym;
 extern const void bpf_fentry_test3 __ksym;
 extern const void bpf_fentry_test4 __ksym;
 extern const void bpf_modify_return_test __ksym;
+extern const void bpf_fentry_test6 __ksym;
 
 __u64 test1_result = 0;
 SEC("fentry/bpf_fentry_test1")
@@ -60,3 +61,15 @@  int BPF_PROG(fmod_ret_test, int a, int *b, int ret)
 	test5_result = (const void *) addr == &bpf_modify_return_test;
 	return ret;
 }
+
+#ifdef __x86_64__
+__u64 test6_result = 0;
+SEC("kprobe/bpf_fentry_test6+0x5")
+int test6(struct pt_regs *ctx)
+{
+	__u64 addr = bpf_get_func_ip(ctx);
+
+	test6_result = (const void *) addr == &bpf_fentry_test6 + 5;
+	return 0;
+}
+#endif