Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Fix get_func_ip offset test for CONFIG_X86_KERNEL_IBT

With CONFIG_X86_KERNEL_IBT enabled the test for kprobe with offset
won't work because of the extra endbr instruction.

As suggested by Andrii adding CONFIG_X86_KERNEL_IBT detection
and using appropriate offset value based on that.

Also removing test7 program, because it does the same as test6.

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/r/20220926153340.1621984-7-jolsa@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Jiri Olsa and committed by
Alexei Starovoitov
738c345b 0e253f7e

+62 -24
+50 -13
tools/testing/selftests/bpf/prog_tests/get_func_ip_test.c
··· 2 2 #include <test_progs.h> 3 3 #include "get_func_ip_test.skel.h" 4 4 5 - void test_get_func_ip_test(void) 5 + static void test_function_entry(void) 6 6 { 7 7 struct get_func_ip_test *skel = NULL; 8 8 int err, prog_fd; ··· 11 11 skel = get_func_ip_test__open(); 12 12 if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open")) 13 13 return; 14 - 15 - /* test6 is x86_64 specifc because of the instruction 16 - * offset, disabling it for all other archs 17 - */ 18 - #ifndef __x86_64__ 19 - bpf_program__set_autoload(skel->progs.test6, false); 20 - bpf_program__set_autoload(skel->progs.test7, false); 21 - #endif 22 14 23 15 err = get_func_ip_test__load(skel); 24 16 if (!ASSERT_OK(err, "get_func_ip_test__load")) ··· 35 43 ASSERT_EQ(skel->bss->test3_result, 1, "test3_result"); 36 44 ASSERT_EQ(skel->bss->test4_result, 1, "test4_result"); 37 45 ASSERT_EQ(skel->bss->test5_result, 1, "test5_result"); 38 - #ifdef __x86_64__ 39 - ASSERT_EQ(skel->bss->test6_result, 1, "test6_result"); 40 - ASSERT_EQ(skel->bss->test7_result, 1, "test7_result"); 41 - #endif 42 46 43 47 cleanup: 44 48 get_func_ip_test__destroy(skel); 49 + } 50 + 51 + /* test6 is x86_64 specific because of the instruction 52 + * offset, disabling it for all other archs 53 + */ 54 + #ifdef __x86_64__ 55 + static void test_function_body(void) 56 + { 57 + struct get_func_ip_test *skel = NULL; 58 + LIBBPF_OPTS(bpf_test_run_opts, topts); 59 + LIBBPF_OPTS(bpf_kprobe_opts, kopts); 60 + struct bpf_link *link6 = NULL; 61 + int err, prog_fd; 62 + 63 + skel = get_func_ip_test__open(); 64 + if (!ASSERT_OK_PTR(skel, "get_func_ip_test__open")) 65 + return; 66 + 67 + bpf_program__set_autoload(skel->progs.test6, true); 68 + 69 + err = get_func_ip_test__load(skel); 70 + if (!ASSERT_OK(err, "get_func_ip_test__load")) 71 + goto cleanup; 72 + 73 + kopts.offset = skel->kconfig->CONFIG_X86_KERNEL_IBT ? 9 : 5; 74 + 75 + link6 = bpf_program__attach_kprobe_opts(skel->progs.test6, "bpf_fentry_test6", &kopts); 76 + if (!ASSERT_OK_PTR(link6, "link6")) 77 + goto cleanup; 78 + 79 + prog_fd = bpf_program__fd(skel->progs.test1); 80 + err = bpf_prog_test_run_opts(prog_fd, &topts); 81 + ASSERT_OK(err, "test_run"); 82 + ASSERT_EQ(topts.retval, 0, "test_run"); 83 + 84 + ASSERT_EQ(skel->bss->test6_result, 1, "test6_result"); 85 + 86 + cleanup: 87 + bpf_link__destroy(link6); 88 + get_func_ip_test__destroy(skel); 89 + } 90 + #else 91 + #define test_function_body() 92 + #endif 93 + 94 + void test_get_func_ip_test(void) 95 + { 96 + test_function_entry(); 97 + test_function_body(); 45 98 }
+12 -11
tools/testing/selftests/bpf/progs/get_func_ip_test.c
··· 2 2 #include <linux/bpf.h> 3 3 #include <bpf/bpf_helpers.h> 4 4 #include <bpf/bpf_tracing.h> 5 + #include <stdbool.h> 5 6 6 7 char _license[] SEC("license") = "GPL"; 7 8 ··· 13 12 extern const void bpf_modify_return_test __ksym; 14 13 extern const void bpf_fentry_test6 __ksym; 15 14 extern const void bpf_fentry_test7 __ksym; 15 + 16 + extern bool CONFIG_X86_KERNEL_IBT __kconfig __weak; 17 + 18 + /* This function is here to have CONFIG_X86_KERNEL_IBT 19 + * used and added to object BTF. 20 + */ 21 + int unused(void) 22 + { 23 + return CONFIG_X86_KERNEL_IBT ? 0 : 1; 24 + } 16 25 17 26 __u64 test1_result = 0; 18 27 SEC("fentry/bpf_fentry_test1") ··· 75 64 } 76 65 77 66 __u64 test6_result = 0; 78 - SEC("kprobe/bpf_fentry_test6+0x5") 67 + SEC("?kprobe") 79 68 int test6(struct pt_regs *ctx) 80 69 { 81 70 __u64 addr = bpf_get_func_ip(ctx); 82 71 83 72 test6_result = (const void *) addr == 0; 84 - return 0; 85 - } 86 - 87 - __u64 test7_result = 0; 88 - SEC("kprobe/bpf_fentry_test7+5") 89 - int test7(struct pt_regs *ctx) 90 - { 91 - __u64 addr = bpf_get_func_ip(ctx); 92 - 93 - test7_result = (const void *) addr == 0; 94 73 return 0; 95 74 }