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

selftests/bpf: Test re-attachment fix for bpf_tracing_prog_attach

Add a test case to verify the fix for "prog->aux->dst_trampoline and
tgt_prog is NULL" branch in bpf_tracing_prog_attach. The sequence of
events:

1. load rawtp program
2. load fentry program with rawtp as target_fd
3. create tracing link for fentry program with target_fd = 0
4. repeat 3

Acked-by: Jiri Olsa <olsajiri@gmail.com>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Dmitrii Dolgov <9erthalion6@gmail.com>
Link: https://lore.kernel.org/r/20240103190559.14750-5-9erthalion6@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Dmitrii Dolgov and committed by
Alexei Starovoitov
e02feb3f 715d82ba

+53
+44
tools/testing/selftests/bpf/prog_tests/recursive_attach.c
··· 105 105 if (test__start_subtest("detach")) 106 106 test_recursive_fentry_chain(true, true); 107 107 } 108 + 109 + /* Test that a tracing prog reattachment (when we land in 110 + * "prog->aux->dst_trampoline and tgt_prog is NULL" branch in 111 + * bpf_tracing_prog_attach) does not lead to a crash due to missing attach_btf 112 + */ 113 + void test_fentry_attach_btf_presence(void) 114 + { 115 + struct fentry_recursive_target *target_skel = NULL; 116 + struct fentry_recursive *tracing_skel = NULL; 117 + struct bpf_program *prog; 118 + int err, link_fd, tgt_prog_fd; 119 + 120 + target_skel = fentry_recursive_target__open_and_load(); 121 + if (!ASSERT_OK_PTR(target_skel, "fentry_recursive_target__open_and_load")) 122 + goto close_prog; 123 + 124 + tracing_skel = fentry_recursive__open(); 125 + if (!ASSERT_OK_PTR(tracing_skel, "fentry_recursive__open")) 126 + goto close_prog; 127 + 128 + prog = tracing_skel->progs.recursive_attach; 129 + tgt_prog_fd = bpf_program__fd(target_skel->progs.fentry_target); 130 + err = bpf_program__set_attach_target(prog, tgt_prog_fd, "fentry_target"); 131 + if (!ASSERT_OK(err, "bpf_program__set_attach_target")) 132 + goto close_prog; 133 + 134 + err = fentry_recursive__load(tracing_skel); 135 + if (!ASSERT_OK(err, "fentry_recursive__load")) 136 + goto close_prog; 137 + 138 + tgt_prog_fd = bpf_program__fd(tracing_skel->progs.recursive_attach); 139 + link_fd = bpf_link_create(tgt_prog_fd, 0, BPF_TRACE_FENTRY, NULL); 140 + if (!ASSERT_GE(link_fd, 0, "link_fd")) 141 + goto close_prog; 142 + 143 + fentry_recursive__detach(tracing_skel); 144 + 145 + err = fentry_recursive__attach(tracing_skel); 146 + ASSERT_ERR(err, "fentry_recursive__attach"); 147 + 148 + close_prog: 149 + fentry_recursive_target__destroy(target_skel); 150 + fentry_recursive__destroy(tracing_skel); 151 + }
+9
tools/testing/selftests/bpf/progs/fentry_recursive_target.c
··· 14 14 { 15 15 return 0; 16 16 } 17 + 18 + /* Dummy bpf prog for testing attach_btf presence when attaching an fentry 19 + * program. 20 + */ 21 + SEC("raw_tp/sys_enter") 22 + int BPF_PROG(fentry_target, struct pt_regs *regs, long id) 23 + { 24 + return 0; 25 + }