Re: [PATCH bpf-next 3/3] selftests/bpf: Add bpf_get_func_ip test for uprobe inside function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 01/08/2023 08:30, Jiri Olsa wrote:
> Adding get_func_ip test for uprobe inside function that validates
> the get_func_ip helper returns correct probe address value.
> 
> Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
> ---
>  .../bpf/prog_tests/get_func_ip_test.c         | 40 ++++++++++++++++++-
>  .../bpf/progs/get_func_ip_uprobe_test.c       | 18 +++++++++
>  2 files changed, 57 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
> index 114cdbc04caf..f199220ad6de 100644
> --- a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
> +++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
> @@ -55,7 +55,16 @@ static void test_function_entry(void)
>   * offset, disabling it for all other archs

nit: comment here

/* test6 is x86_64 specific because of the instruction
 * offset, disabling it for all other archs

...should probably be updated now multiple tests are gated by the
#ifdef __x86_64__.

BTW I tested if these tests would pass on aarch64 with a few tweaks
to instruction offsets, and they do. Something like the following
gets all of the tests running and passing on aarch64:

diff --git a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
index f199220ad6de..61ac13508c58 100644
--- a/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
+++ b/tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
@@ -51,10 +51,10 @@ static void test_function_entry(void)
        get_func_ip_test__destroy(skel);
 }

-/* test6 is x86_64 specific because of the instruction
- * offset, disabling it for all other archs
+/* tests below are x86_64/aarch64 specific because of the instruction
+ * offsets, disabling them for all other archs
  */
-#ifdef __x86_64__
+#if defined(__x86_64__) || defined(__aarch64__)
 extern void uprobe_trigger_body(void);
 asm(
 ".globl uprobe_trigger_body\n"
@@ -82,7 +82,11 @@ static void test_function_body_kprobe(void)
        if (!ASSERT_OK(err, "get_func_ip_test__load"))
                goto cleanup;

+#if defined(__x86_64__)
        kopts.offset = skel->kconfig->CONFIG_X86_KERNEL_IBT ? 9 : 5;
+#elif defined(__aarch64__)
+       kopts.offset = 8;
+#endif

        link6 = bpf_program__attach_kprobe_opts(skel->progs.test6,
"bpf_fentry_test6", &kopts);
        if (!ASSERT_OK_PTR(link6, "link6"))
diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
b/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
index 052f8a4345a8..56af4a8447b9 100644
--- a/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
+++ b/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
@@ -8,11 +8,17 @@ char _license[] SEC("license") = "GPL";
 unsigned long uprobe_trigger_body;

 __u64 test1_result = 0;
+#if defined(__TARGET_ARCH_x86)
+#define OFFSET 1
 SEC("uprobe//proc/self/exe:uprobe_trigger_body+1")
+#elif defined(__TARGET_ARCH_arm64)
+#define OFFSET 4
+SEC("uprobe//proc/self/exe:uprobe_trigger_body+4")
+#endif
 int BPF_UPROBE(test1)
 {
        __u64 addr = bpf_get_func_ip(ctx);

-       test1_result = (const void *) addr == (const void *)
uprobe_trigger_body + 1;
+       test1_result = (const void *) addr == (const void *)
uprobe_trigger_body + OFFSET;
        return 0;
 }


Anyway if you're doing a later version and want to roll something like
the above in feel free, otherwise I can send a followup patch later on.
Regardless, for the series on aarch64:

Tested-by: Alan Maguire <alan.maguire@xxxxxxxxxx>

>   */
>  #ifdef __x86_64__
> -static void test_function_body(void)
> +extern void uprobe_trigger_body(void);
> +asm(
> +".globl uprobe_trigger_body\n"
> +".type uprobe_trigger_body, @function\n"
> +"uprobe_trigger_body:\n"
> +"	nop\n"
> +"	ret\n"
> +);
> +
> +static void test_function_body_kprobe(void)
>  {
>  	struct get_func_ip_test *skel = NULL;
>  	LIBBPF_OPTS(bpf_test_run_opts, topts);
> @@ -90,6 +99,35 @@ static void test_function_body(void)
>  	bpf_link__destroy(link6);
>  	get_func_ip_test__destroy(skel);
>  }
> +
> +static void test_function_body_uprobe(void)
> +{
> +	struct get_func_ip_uprobe_test *skel = NULL;
> +	int err;
> +
> +	skel = get_func_ip_uprobe_test__open_and_load();
> +	if (!ASSERT_OK_PTR(skel, "get_func_ip_uprobe_test__open_and_load"))
> +		return;
> +
> +	err = get_func_ip_uprobe_test__attach(skel);
> +	if (!ASSERT_OK(err, "get_func_ip_test__attach"))
> +		goto cleanup;
> +
> +	skel->bss->uprobe_trigger_body = (unsigned long) uprobe_trigger_body;
> +
> +	uprobe_trigger_body();
> +
> +	ASSERT_EQ(skel->bss->test1_result, 1, "test1_result");
> +
> +cleanup:
> +	get_func_ip_uprobe_test__destroy(skel);
> +}
> +
> +static void test_function_body(void)
> +{
> +	test_function_body_kprobe();
> +	test_function_body_uprobe();
> +}
>  #else
>  #define test_function_body()
>  #endif
> diff --git a/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c b/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
> new file mode 100644
> index 000000000000..052f8a4345a8
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/get_func_ip_uprobe_test.c
> @@ -0,0 +1,18 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +unsigned long uprobe_trigger_body;
> +
> +__u64 test1_result = 0;
> +SEC("uprobe//proc/self/exe:uprobe_trigger_body+1")
> +int BPF_UPROBE(test1)
> +{
> +	__u64 addr = bpf_get_func_ip(ctx);
> +
> +	test1_result = (const void *) addr == (const void *) uprobe_trigger_body + 1;
> +	return 0;
> +}




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux