bpf: Fix fill_link_info and add selftest
Subsystems (4)
v5 cover bpf: Fix fill_link_info and add selftest
Patch #1: Fix an error in fill_link_info reported by Dan Patch #2: Add selftest for #1
v4->v5:
- Comments from Yonghong
- Replace 'offset' with '0'
- Set err=-1 for default case
- Only check return value of verify_kmulti_link_info
- Comments from Jiri
- Avoid retprobe argument
- Use bpf_fentry_test* instead
- Rename verify_kmulti_user_buffer
- Define some variables as global value to simplify the code
v3->v4:
- Comments from Yonghong
- re-word the kptr comment
- Avoid unnecessary switch statement
- No need to check the return value of bpf_link__fd()
- offset is always 0
- Return directly when test_fill_link_info__open_and_load() fails
- Cleanup skel when load_kallsyms_refresh() fails
v2->v3:
- Comments from Jiri
- Verify wrong arguments as soon as possible
- Use CONFIG_X86_KERNEL_IBT
- No need to make the test serial
- Add test case for kprobe_multi
v1->v2:
- Fix BPF CI failure due to the enabled ENDBDR
Yafang Shao (2):
bpf: Fix uninitialized symbol in bpf_perf_link_fill_kprobe() selftests/bpf: Add selftest for fill_link_info
kernel/bpf/syscall.c | 4 +- tools/testing/selftests/bpf/DENYLIST.aarch64 | 3 + .../selftests/bpf/prog_tests/fill_link_info.c | 342 +++++++++++++++++++++ .../selftests/bpf/progs/test_fill_link_info.c | 42 +++ 4 files changed, 389 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/fill_link_info.c create mode 100644 tools/testing/selftests/bpf/progs/test_fill_link_info.c
Signature · 1 line
1.8.3.1
PATCH v5 1/2 bpf: Fix uninitialized symbol in bpf_perf_link_fill_kprobe()
The patch 1b715e1b0ec5: "bpf: Support ->fill_link_info for perf_event" from Jul 9, 2023, leads to the following Smatch static checker warning:
kernel/bpf/syscall.c:3416 bpf_perf_link_fill_kprobe()
error: uninitialized symbol 'type'.
That can happens when uname is NULL. So fix it by verifying the uname when we really need to fill it.
--- kernel/bpf/syscall.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/syscall.c +2 -2
@@ -3378,14 +3378,14 @@ static int bpf_perf_link_fill_common(const struct perf_event *event,if (!ulen ^ !uname)return -EINVAL ; -if (!uname) -return 0 ; err =bpf_get_perf_event_info (event, &prog_id, fd_type, &buf, probe_offset, probe_addr);if (err)return err; +if (!uname) +return 0 ;if (buf) { len =strlen (buf); err =bpf_copy_to_user (uname, buf, ulen, len);
Signature · 1 line
1.8.3.1
PATCH v5 2/2 selftests/bpf: Add selftest for fill_link_info
In reply to Yafang ShaoAdd selftest for the fill_link_info of uprobe, kprobe and tracepoint. The result:
$ tools/testing/selftests/bpf/test_progs --name=fill_link_info #79/1 fill_link_info/kprobe_link_info:OK #79/2 fill_link_info/kretprobe_link_info:OK #79/3 fill_link_info/kprobe_invalid_ubuff:OK #79/4 fill_link_info/tracepoint_link_info:OK #79/5 fill_link_info/uprobe_link_info:OK #79/6 fill_link_info/uretprobe_link_info:OK #79/7 fill_link_info/kprobe_multi_link_info:OK #79/8 fill_link_info/kretprobe_multi_link_info:OK #79/9 fill_link_info/kprobe_multi_invalid_ubuff:OK #79 fill_link_info:OK Summary: 1/9 PASSED, 0 SKIPPED, 0 FAILED
The test case for kprobe_multi won't be run on aarch64, as it is not supported.
--- tools/testing/selftests/bpf/DENYLIST.aarch64 | 3 + .../selftests/bpf/prog_tests/fill_link_info.c | 342 +++++++++++++++++++++ .../selftests/bpf/progs/test_fill_link_info.c | 42 +++ 3 files changed, 387 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fill_link_info.c create mode 100644 tools/testing/selftests/bpf/progs/test_fill_link_info.c
tools/testing/selftests/bpf/DENYLIST.aarch64 +3 -0
@@ -12,3 +12,6 @@ kprobe_multi_test/skel_api # libbpf: failed to load BPF sk module_attach # prog 'kprobe_multi': failed to auto-attach: -95 fentry_test/fentry_many_args # fentry_many_args:FAIL:fentry_many_args_attach unexpected error: -524 fexit_test/fexit_many_args # fexit_many_args:FAIL:fexit_many_args_attach unexpected error: -524 +fill_link_info/kprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95 +fill_link_info/kretprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95 +fill_link_info/kprobe_multi_ubuff # bpf_program__attach_kprobe_multi_opts unexpected error: -95
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +342 -0
342 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_fill_link_info.c new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add selftest for fill_link_info
On Fri, Aug 11, 2023 at 02:36:47AM +0000, Yafang Shao wrote:
SNIP
Quoted 20 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +20
-0+void test_fill_link_info (void ) +{ +struct test_fill_link_info *skel; +int i; + + skel =test_fill_link_info__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; + +/* load kallsyms to compare the addr */ +if (!ASSERT_OK (load_kallsyms_refresh (),"load_kallsyms_refresh" )) + goto cleanup; + + kprobe_addr =ksym_get_addr (KPROBE_FUNC ); +if (test__start_subtest ("kprobe_link_info" )) +test_kprobe_fill_link_info (skel,BPF_PERF_EVENT_KPROBE , false); +if (test__start_subtest ("kretprobe_link_info" )) +test_kprobe_fill_link_info (skel,BPF_PERF_EVENT_KRETPROBE , false); +if (test__start_subtest ("kprobe_invalid_ubuff" )) +test_kprobe_fill_link_info (skel,BPF_PERF_EVENT_KPROBE , true);
tools/testing/selftests/bpf/prog_tests/fill_link_info.c line 330
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +10
-0+if (test__start_subtest ("tracepoint_link_info" )) +test_tp_fill_link_info (skel); + + uprobe_offset =get_uprobe_offset (&uprobe_func); +if (test__start_subtest ("uprobe_link_info" )) +test_uprobe_fill_link_info (skel,BPF_PERF_EVENT_UPROBE ); +if (test__start_subtest ("uretprobe_link_info" )) +test_uprobe_fill_link_info (skel,BPF_PERF_EVENT_URETPROBE ); + +qsort (kmulti_syms,KMULTI_CNT ,sizeof (kmulti_syms[0 ]), symbols_cmp_r);
hum, what's the reason for sorting the symbols?
other than that it looks good to me
jirka
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +12
-0+for (i =0 ; i <KMULTI_CNT ; i++) + kmulti_addrs[i] =ksym_get_addr (kmulti_syms[i]); +if (test__start_subtest ("kprobe_multi_link_info" )) +test_kprobe_multi_fill_link_info (skel, false, false); +if (test__start_subtest ("kretprobe_multi_link_info" )) +test_kprobe_multi_fill_link_info (skel, true, false); +if (test__start_subtest ("kprobe_multi_invalid_ubuff" )) +test_kprobe_multi_fill_link_info (skel, true, true); + +cleanup: +test_fill_link_info__destroy (skel); +}
SNIP
Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add selftest for fill_link_info
Quoted 29 lines · Jiri Olsa
On Fri, Aug 11, 2023 at 02:36:47AM +0000, Yafang Shao wrote:
SNIP
Quoted 24 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +24
-0+void test_fill_link_info (void ) +{ +struct test_fill_link_info *skel; +int i; + + skel =test_fill_link_info__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open" )) +return ; + +/* load kallsyms to compare the addr */ +if (!ASSERT_OK (load_kallsyms_refresh (),"load_kallsyms_refresh" )) + goto cleanup; + + kprobe_addr =ksym_get_addr (KPROBE_FUNC ); +if (test__start_subtest ("kprobe_link_info" )) +test_kprobe_fill_link_info (skel,BPF_PERF_EVENT_KPROBE , false); +if (test__start_subtest ("kretprobe_link_info" )) +test_kprobe_fill_link_info (skel,BPF_PERF_EVENT_KRETPROBE , false); +if (test__start_subtest ("kprobe_invalid_ubuff" )) +test_kprobe_fill_link_info (skel,BPF_PERF_EVENT_KPROBE , true); +if (test__start_subtest ("tracepoint_link_info" )) +test_tp_fill_link_info (skel); + + uprobe_offset =get_uprobe_offset (&uprobe_func);
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c line 330
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +6
-0+if (test__start_subtest ("uprobe_link_info" )) +test_uprobe_fill_link_info (skel,BPF_PERF_EVENT_UPROBE ); +if (test__start_subtest ("uretprobe_link_info" )) +test_uprobe_fill_link_info (skel,BPF_PERF_EVENT_URETPROBE ); + +qsort (kmulti_syms,KMULTI_CNT ,sizeof (kmulti_syms[0 ]), symbols_cmp_r);hum, what's the reason for sorting the symbols?
Kernel will sort the kprobe_multi symbols, and then store the corresponding addresses in the same order into kmulti_link->addr. In order to compare the addresses, we should sort them and then get the addresses.
Quoted 21 lines
other than that it looks good to me
Acked-by Jiri Olsa <[email protected]>jirka
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/fill_link_info.c new file +12
-0+for (i =0 ; i <KMULTI_CNT ; i++) + kmulti_addrs[i] =ksym_get_addr (kmulti_syms[i]); +if (test__start_subtest ("kprobe_multi_link_info" )) +test_kprobe_multi_fill_link_info (skel, false, false); +if (test__start_subtest ("kretprobe_multi_link_info" )) +test_kprobe_multi_fill_link_info (skel, true, false); +if (test__start_subtest ("kprobe_multi_invalid_ubuff" )) +test_kprobe_multi_fill_link_info (skel, true, true); + +cleanup: +test_fill_link_info__destroy (skel); +}SNIP
Signature · 2 lines
Regards Yafang
Martin KaFai Lau
reviewerRe: [PATCH bpf-next v5 2/2] selftests/bpf: Add selftest for fill_link_info
In reply to Yafang Shaotools/testing/selftests/bpf/DENYLIST.aarch64 kprobe_multi_test/skel_api # libbpf: failed to load BPF sk · line 17
Quoted 11 lines · Yafang Shao
tools/testing/selftests/bpf/DENYLIST.aarch64 +3
-0@@ -12,3 +12,6 @@ kprobe_multi_test/skel_api # libbpf: failed to load BPF sk module_attach # prog 'kprobe_multi': failed to auto-attach: -95 fentry_test/fentry_many_args # fentry_many_args:FAIL:fentry_many_args_attach unexpected error: -524 fexit_test/fexit_many_args # fexit_many_args:FAIL:fexit_many_args_attach unexpected error: -524 +fill_link_info/kprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95 +fill_link_info/kretprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95 +fill_link_info/kprobe_multi_ubuff # bpf_program__attach_kprobe_multi_opts unexpected error: -95
The bpf CI is failing: https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
The patch author is responsible to monitor the result from patchwork.
Re: [PATCH bpf-next v5 2/2] selftests/bpf: Add selftest for fill_link_info
Quoted 17 lines · Martin KaFai Lau
tools/testing/selftests/bpf/DENYLIST.aarch64 kprobe_multi_test/skel_api # libbpf: failed to load BPF sk · line 17
Quoted 11 lines · Yafang Shao
tools/testing/selftests/bpf/DENYLIST.aarch64 +3
-0@@ -12,3 +12,6 @@ kprobe_multi_test/skel_api # libbpf: failed to load BPF sk module_attach # prog 'kprobe_multi': failed to auto-attach: -95 fentry_test/fentry_many_args # fentry_many_args:FAIL:fentry_many_args_attach unexpected error: -524 fexit_test/fexit_many_args # fexit_many_args:FAIL:fexit_many_args_attach unexpected error: -524 +fill_link_info/kprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95 +fill_link_info/kretprobe_multi_link_info # bpf_program__attach_kprobe_multi_opts unexpected error: -95 +fill_link_info/kprobe_multi_ubuff # bpf_program__attach_kprobe_multi_opts unexpected error: -95The bpf CI is failing:
https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/
Quoted 1 line
The patch author is responsible to monitor the result from patchwork.
Thanks for your reminder. Should rename
fill_link_info/kprobe_multi_ubuff to
fill_link_info/kprobe_multi_invalid_ubuff
Signature · 2 lines
Regards Yafang