selftests/bpf: Move spintest from samples/bpf to selftests
Subsystems (3)
PATCH 1/1 selftests/bpf: Move spintest from samples/bpf to selftests
According to the discussions in [1] everything from the samples/bpf directory should be moved out or deleted.
Move the spintest program from BPF samples to BPF selftests. There are no functional changes. BPF skeleton has been used for loading, etc.
[1] https://lore.kernel.org/all/CAEf4BzYv3ONhy-JnQUtknzgBSK0gpm9GBJYtbAiJQe50_eX7Uw@mail.gmail.com/
--- samples/bpf/Makefile | 3 -- .../selftests/bpf/prog_tests/spintest.c | 43 +++++++------------ .../testing/selftests/bpf/progs/test_spin.c | 15 +++---- 3 files changed, 23 insertions(+), 38 deletions(-) rename samples/bpf/spintest_user.c => tools/testing/selftests/bpf/prog_tests/spintest.c (63%) rename samples/bpf/spintest_kern.c => tools/testing/selftests/bpf/progs/test_spin.c (86%)
samples/bpf/Makefile +0 -3
@@ -23,7 +23,6 @@ tprogs-y += test_probe_write_user tprogs-y += trace_output tprogs-y += lathist tprogs-y += offwaketime -tprogs-y += spintest tprogs-y += map_perf_test tprogs-y += test_overhead tprogs-y += test_cgrp2_array_pin
@@ -86,7 +85,6 @@ test_probe_write_user-objs := test_probe_write_user_user.o trace_output-objs := trace_output_user.o lathist-objs := lathist_user.o offwaketime-objs := offwaketime_user.o $(TRACE_HELPERS) -spintest-objs := spintest_user.o $(TRACE_HELPERS) map_perf_test-objs := map_perf_test_user.o test_overhead-objs := test_overhead_user.o test_cgrp2_array_pin-objs := test_cgrp2_array_pin.o
@@ -144,7 +142,6 @@ always-y += tcbpf1_kern.o always-y += tc_l2_redirect_kern.o always-y += lathist_kern.o always-y += offwaketime_kern.o -always-y += spintest_kern.o always-y += map_perf_test_kern.o always-y += test_overhead_tp_kern.o always-y += test_overhead_raw_tp_kern.o
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +16 -27
73 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_spin.c renamed from samples/bpf/spintest_kern.c +7 -8
@@ -4,14 +4,14 @@* modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include <linux/skbuff.h> -#include <linux/netdevice.h> -#include <linux/version.h> -#include <uapi/linux/bpf.h> -#include <uapi/linux/perf_event.h> +#include <vmlinux.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> +#ifndef PERF_MAX_STACK_DEPTH +#define PERF_MAX_STACK_DEPTH 127 +#endif +struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, long);
@@ -27,8 +27,8 @@ struct {struct { __uint(type, BPF_MAP_TYPE_STACK_TRACE); - __uint(key_size, sizeof(u32)); - __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64)); + __uint(key_size, sizeof(__u32)); + __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(__u64)); __uint(max_entries, 10000); } stackmap SEC(".maps");
@@ -66,4 +66,3 @@ SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16) SEC("kprobe/htab_map_alloc")PROG(p17) char _license[] SEC("license") = "GPL"; -u32 _version SEC("version") = LINUX_VERSION_CODE;
Signature · 1 line
2.35.1
Quoted 8 lines · Puranjay Mohan
According to the discussions in [1] everything from the
samples/bpfdirectory should be moved out or deleted.Move the spintest program from BPF samples to BPF selftests. There are no functional changes. BPF skeleton has been used for loading, etc.
Great that you are helping with selftests consolidation, thanks! I have few comments which will require a new revision, but overall this is moving in the right direction.
Quoted 11 lines
[1] https://lore.kernel.org/all/CAEf4BzYv3ONhy-JnQUtknzgBSK0gpm9GBJYtbAiJQe50_eX7Uw@mail.gmail.com/
Signed-off-by Puranjay Mohan <[email protected]>--- samples/bpf/Makefile | 3 -- .../selftests/bpf/prog_tests/spintest.c | 43 +++++++------------ .../testing/selftests/bpf/progs/test_spin.c | 15 +++---- 3 files changed, 23 insertions(+), 38 deletions(-) rename samples/bpf/spintest_user.c => tools/testing/selftests/bpf/prog_tests/spintest.c (63%) rename samples/bpf/spintest_kern.c => tools/testing/selftests/bpf/progs/test_spin.c (86%)
[...]
tools/testing/selftests/bpf/prog_tests/spintest.c line 26
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +1
-4-if (load_kallsyms ()) { -printf ("failed to process /proc/kallsyms\n" ); -return 2 ; - } + err =load_kallsyms ();
do we need to use kallsyms? selftests generally follows the latest kernel, so whatever attach target doesn't exist in the kernel we should just remove
Quoted 33 lines
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +7
-19+if (!ASSERT_OK (err,"load_kallsyms" )) +return ; + skel =test_spin__open_and_load (); -snprintf (filename,sizeof (filename),"%s_kern.o" , argv[0 ]); - obj =bpf_object__open_file (filename,NULL ); -if (libbpf_get_error (obj)) { -fprintf (stderr,"ERROR: opening BPF object file failed\n" ); - obj =NULL ; - goto cleanup; - } +if (!ASSERT_OK_PTR (skel,"test_spin__open_and_load" )) +return ; -/* load BPF program */ -if (bpf_object__load (obj)) { -fprintf (stderr,"ERROR: loading BPF object file failed\n" ); - goto cleanup; - } - - map_fd =bpf_object__find_map_fd_by_name (obj,"my_map" ); -if (map_fd <0 ) { -fprintf (stderr,"ERROR: finding a map in obj file failed\n" ); - goto cleanup; - } + map_fd =bpf_map__fd (skel->maps.my_map); -bpf_object__for_each_program (prog, obj) { +bpf_object__for_each_program (prog, skel->obj) { section =bpf_program__section_name (prog);if (sscanf (section,"kprobe/%s" , symbol) !=1 )continue ;
tools/testing/selftests/bpf/prog_tests/spintest.c int main(int ac, char **argv) · line 48
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +2
-1@@ -52,7 +41,8 @@ int main(int ac, char **argv)/* Attach prog only when symbol exists */ if (ksym_get_addr (symbol)) { links[j] =bpf_program__attach (prog); -if (libbpf_get_error (links[j])) { + err =libbpf_get_error (links[j]); +if (!ASSERT_OK (err,"bpf_program__attach" )) {fprintf (stderr,"bpf_program__attach failed\n" ); links[j] =NULL ; goto cleanup;
there are a bunch of leftover prints, let's get rid of them and/or turn them into ASSERT_xxx()
there is sleep(1) in each of 5 iterations, this slows down the selftests a lot. Let's get rid of it.
there is also assert(), there shouldn't be any calls to assert() crashing the process in selftests
tools/testing/selftests/bpf/prog_tests/spintest.c int main(int ac, char **argv) · line 79
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +0
-0@@ -89,5 +79,4 @@ int main(int ac, char **argv)bpf_link__destroy (links[j]);
let's use skeleton's links "storage" and simplify the cleanup significantly.
tools/testing/selftests/bpf/prog_tests/spintest.c int main(int ac, char **argv) · line 81
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +0
-0bpf_object__close (obj);
this should be test_spin__destroy() instead
Quoted 41 lines
tools/testing/selftests/bpf/prog_tests/spintest.c renamed from samples/bpf/spintest_user.c +0
-1-return 0 ; }tools/testing/selftests/bpf/progs/test_spin.c renamed from samples/bpf/spintest_kern.c +7
-7@@ -4,14 +4,14 @@* modify it under the terms of version 2 of the GNU General Public * License as published by the Free Software Foundation. */ -#include <linux/skbuff.h> -#include <linux/netdevice.h> -#include <linux/version.h> -#include <uapi/linux/bpf.h> -#include <uapi/linux/perf_event.h> +#include <vmlinux.h> #include <bpf/bpf_helpers.h> #include <bpf/bpf_tracing.h> +#ifndef PERF_MAX_STACK_DEPTH +#define PERF_MAX_STACK_DEPTH 127 +#endif +struct { __uint(type, BPF_MAP_TYPE_HASH); __type(key, long);@@ -27,8 +27,8 @@ struct {struct { __uint(type, BPF_MAP_TYPE_STACK_TRACE); - __uint(key_size, sizeof(u32)); - __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(u64)); + __uint(key_size, sizeof(__u32)); + __uint(value_size, PERF_MAX_STACK_DEPTH * sizeof(__u64)); __uint(max_entries, 10000); } stackmap SEC(".maps");
tools/testing/selftests/bpf/progs/test_spin.c SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16) · line 66
Quoted 1 line
tools/testing/selftests/bpf/progs/test_spin.c renamed from samples/bpf/spintest_kern.c +0
-0@@ -66,4 +66,3 @@ SEC("kprobe/__htab_percpu_map_update_elem")PROG(p16) SEC("kprobe/htab_map_alloc")PROG(p17)
I don't see why we need this PROG(pXX) magic instead of just doing a function call. Let's have a common function called from each of the defined kprobe program.
Quoted 6 lines
tools/testing/selftests/bpf/progs/test_spin.c renamed from samples/bpf/spintest_kern.c +0
-2char _license[] SEC("license") = "GPL"; -u32 _version SEC("version") = LINUX_VERSION_CODE; --2.35.1