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

selftests/bpf: Add CO-RE relos and SEC("?...") to linked_funcs selftests

Enhance linked_funcs selftest with two tricky features that might not
obviously work correctly together. We add CO-RE relocations to entry BPF
programs and mark those programs as non-autoloadable with SEC("?...")
annotation. This makes sure that libbpf itself handles .BTF.ext CO-RE
relocation data matching correctly for SEC("?...") programs, as well as
ensures that BPF static linker handles this correctly (this was the case
before, no changes are necessary, but it wasn't explicitly tested).

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220426004511.2691730-6-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Alexei Starovoitov
b82bb1ff 11d5daa8

+18 -2
+6
tools/testing/selftests/bpf/prog_tests/linked_funcs.c
··· 14 14 if (!ASSERT_OK_PTR(skel, "skel_open")) 15 15 return; 16 16 17 + /* handler1 and handler2 are marked as SEC("?raw_tp/sys_enter") and 18 + * are set to not autoload by default 19 + */ 20 + bpf_program__set_autoload(skel->progs.handler1, true); 21 + bpf_program__set_autoload(skel->progs.handler2, true); 22 + 17 23 skel->rodata->my_tid = syscall(SYS_gettid); 18 24 skel->bss->syscall_id = SYS_getpgid; 19 25
+6 -1
tools/testing/selftests/bpf/progs/linked_funcs1.c
··· 61 61 /* here we'll force set_output_ctx2() to be __hidden in the final obj file */ 62 62 __hidden extern void set_output_ctx2(__u64 *ctx); 63 63 64 - SEC("raw_tp/sys_enter") 64 + SEC("?raw_tp/sys_enter") 65 65 int BPF_PROG(handler1, struct pt_regs *regs, long id) 66 66 { 67 + static volatile int whatever; 68 + 67 69 if (my_tid != (u32)bpf_get_current_pid_tgid() || id != syscall_id) 68 70 return 0; 71 + 72 + /* make sure we have CO-RE relocations in main program */ 73 + whatever = bpf_core_type_size(struct task_struct); 69 74 70 75 set_output_val2(1000); 71 76 set_output_ctx2(ctx); /* ctx definition is hidden in BPF_PROG macro */
+6 -1
tools/testing/selftests/bpf/progs/linked_funcs2.c
··· 61 61 /* here we'll force set_output_ctx1() to be __hidden in the final obj file */ 62 62 __hidden extern void set_output_ctx1(__u64 *ctx); 63 63 64 - SEC("raw_tp/sys_enter") 64 + SEC("?raw_tp/sys_enter") 65 65 int BPF_PROG(handler2, struct pt_regs *regs, long id) 66 66 { 67 + static volatile int whatever; 68 + 67 69 if (my_tid != (u32)bpf_get_current_pid_tgid() || id != syscall_id) 68 70 return 0; 71 + 72 + /* make sure we have CO-RE relocations in main program */ 73 + whatever = bpf_core_type_size(struct task_struct); 69 74 70 75 set_output_val1(2000); 71 76 set_output_ctx1(ctx); /* ctx definition is hidden in BPF_PROG macro */