bpf: Move kernel test kfuncs into bpf_testmod
Subsystems (5)
Jiri Olsa
reviewercover bpf: Move kernel test kfuncs into bpf_testmod
hi,
I noticed several times in discussions that we should move test kfuncs into kernel module, now perhaps even more pressing with all the kfunc effort. This patchset moves all the test kfuncs into bpf_testmod.
I added bpf_testmod/bpf_testmod_kfunc.h header that is shared between bpf_testmod kernel module and BPF programs, which brings some difficulties with __ksym define. But I'm not sure having separate headers for BPF programs and for kernel module would be better.
This patchset also needs:
74bc3a5acc82 bpf: Add missing btf_put to register_btf_id_dtor_kfuncs
which is only in bpf/master now.
thanks,
jirka
---
Jiri Olsa (5):
selftests/bpf: Move kfunc exports to bpf_testmod/bpf_testmod_kfunc.h selftests/bpf: Move test_progs helpers to testing_helpers object selftests/bpf: Load bpf_testmod for verifier test selftests/bpf: Allow to use kfunc from testmod.ko in test_verifier
bpf: Move kernel test kfuncs to bpf_testmod
net/bpf/test_run.c | 253 +------------------------------------------------------------------------------------------ tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h | 89 ++++++++++++++++++++++++++++++++ tools/testing/selftests/bpf/progs/cb_refs.c | 1 + tools/testing/selftests/bpf/progs/jit_probe_mem.c | 3 +- tools/testing/selftests/bpf/progs/kfunc_call_destructive.c | 3 +- tools/testing/selftests/bpf/progs/kfunc_call_fail.c | 9 +--- tools/testing/selftests/bpf/progs/kfunc_call_race.c | 3 +- tools/testing/selftests/bpf/progs/kfunc_call_test.c | 15 +----- tools/testing/selftests/bpf/progs/kfunc_call_test_subprog.c | 17 +++++-- tools/testing/selftests/bpf/progs/map_kptr.c | 1 + tools/testing/selftests/bpf/progs/map_kptr_fail.c | 1 + tools/testing/selftests/bpf/test_progs.c | 67 +----------------------- tools/testing/selftests/bpf/test_verifier.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++++-------- tools/testing/selftests/bpf/testing_helpers.c | 63 +++++++++++++++++++++++ tools/testing/selftests/bpf/testing_helpers.h | 10 ++++ 16 files changed, 520 insertions(+), 371 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h
Jiri Olsa
reviewerPATCH 1/5 selftests/bpf: Move kfunc exports to bpf_testmod/bpf_testmod_kfunc.h
Move all kfunc exports into separate header file and include it in tests that need it.
We will move all test kfuncs into bpf_testmod in following change, so it's convenient to have declarations in single place.
--- .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 89 +++++++++++++++++++ tools/testing/selftests/bpf/progs/cb_refs.c | 1 + .../selftests/bpf/progs/jit_probe_mem.c | 3 +- .../bpf/progs/kfunc_call_destructive.c | 3 +- .../selftests/bpf/progs/kfunc_call_fail.c | 9 +- .../selftests/bpf/progs/kfunc_call_race.c | 3 +- .../selftests/bpf/progs/kfunc_call_test.c | 15 +--- .../bpf/progs/kfunc_call_test_subprog.c | 17 +++- tools/testing/selftests/bpf/progs/map_kptr.c | 1 + .../selftests/bpf/progs/map_kptr_fail.c | 1 + 10 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h new file +89 -0
@@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _BPF_TESTMOD_KFUNC_H +#define _BPF_TESTMOD_KFUNC_H + +#ifndef __ksym +#define __ksym __attribute__((section(".ksyms"))) +#endif + +struct prog_test_pass1 { +int x0; +struct { +int x1; +struct { +int x2; +struct { +int x3; + }; + }; + }; +}; + +struct prog_test_pass2 { +int len; +short arr1[4 ]; +struct { +char arr2[4 ]; +unsigned long arr3[8 ]; + } x; +}; + +struct prog_test_fail1 { +void *p; +int x; +}; + +struct prog_test_fail2 { +int x8; +struct prog_test_pass1 x; +}; + +struct prog_test_fail3 { +int len; +char arr1[2 ]; +char arr2[]; +}; + +struct prog_test_member1 { +int a; +}; + +struct prog_test_member { +struct prog_test_member1 m; +int c; +}; + +struct prog_test_ref_kfunc { +int a; +int b; +struct prog_test_member memb; +struct prog_test_ref_kfunc *next; +refcount_t cnt; +}; + +extern struct prog_test_ref_kfunc * +bpf_kfunc_call_test_acquire (unsigned long *scalar_ptr) __ksym; +extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym; + +extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym; +extern int *bpf_kfunc_call_test_get_rdwr_mem (struct prog_test_ref_kfunc *p,const int rdwr_buf_size) __ksym; +extern int *bpf_kfunc_call_test_get_rdonly_mem (struct prog_test_ref_kfunc *p,const int rdonly_buf_size) __ksym; +extern int *bpf_kfunc_call_test_acq_rdonly_mem (struct prog_test_ref_kfunc *p,const int rdonly_buf_size) __ksym; +extern void bpf_kfunc_call_int_mem_release(int *p) __ksym; + +extern void bpf_testmod_test_mod_kfunc(int i) __ksym; + +extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, + __u32 c, __u64 d) __ksym; +extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym; +extern struct sock *bpf_kfunc_call_test3 (struct sock *sk) __ksym; + +extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; +extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym; +extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; +extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; + +extern void bpf_kfunc_call_test_destructive(void) __ksym; + +#endif /* _BPF_TESTMOD_KFUNC_H */
tools/testing/selftests/bpf/progs/cb_refs.c +1 -0
@@ -2,6 +2,7 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value { struct prog_test_ref_kfunc __kptr_ref *ptr;
tools/testing/selftests/bpf/progs/jit_probe_mem.c +1 -2
@@ -3,12 +3,11 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" static struct prog_test_ref_kfunc __kptr_ref *v;long total_sum =-1 ; -extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire (unsigned long *sp) __ksym; -extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym; SEC("tc")int test_jit_probe_mem (struct __sk_buff *ctx)
tools/testing/selftests/bpf/progs/kfunc_call_destructive.c +1 -2
@@ -1,8 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <vmlinux.h> #include <bpf/bpf_helpers.h> - -extern void bpf_kfunc_call_test_destructive(void) __ksym; +#include "bpf_testmod/bpf_testmod_kfunc.h" SEC("tc")int kfunc_destructive_test (void )
tools/testing/selftests/bpf/progs/kfunc_call_fail.c +1 -8
@@ -2,14 +2,7 @@/* Copyright (c) 2021 Facebook */ #include <vmlinux.h> #include <bpf/bpf_helpers.h> - -extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire (unsigned long *sp) __ksym; -extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym; -extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym; -extern int *bpf_kfunc_call_test_get_rdwr_mem (struct prog_test_ref_kfunc *p,const int rdwr_buf_size) __ksym; -extern int *bpf_kfunc_call_test_get_rdonly_mem (struct prog_test_ref_kfunc *p,const int rdonly_buf_size) __ksym; -extern int *bpf_kfunc_call_test_acq_rdonly_mem (struct prog_test_ref_kfunc *p,const int rdonly_buf_size) __ksym; -extern void bpf_kfunc_call_int_mem_release(int *p) __ksym; +#include "bpf_testmod/bpf_testmod_kfunc.h" struct syscall_test_args {__u8 data[16 ];
tools/testing/selftests/bpf/progs/kfunc_call_race.c +1 -2
@@ -1,8 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <vmlinux.h> #include <bpf/bpf_helpers.h> - -extern void bpf_testmod_test_mod_kfunc(int i) __ksym; +#include "bpf_testmod/bpf_testmod_kfunc.h" SEC("tc")int kfunc_call_fail (struct __sk_buff *ctx)
tools/testing/selftests/bpf/progs/kfunc_call_test.c +1 -14
@@ -2,20 +2,7 @@/* Copyright (c) 2021 Facebook */ #include <vmlinux.h> #include <bpf/bpf_helpers.h> - -extern int bpf_kfunc_call_test2(struct sock *sk, __u32 a, __u32 b) __ksym; -extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, - __u32 c, __u64 d) __ksym; - -extern struct prog_test_ref_kfunc *bpf_kfunc_call_test_acquire (unsigned long *sp) __ksym; -extern void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) __ksym; -extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; -extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym; -extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; -extern void bpf_kfunc_call_test_mem_len_pass1(void *mem, int len) __ksym; -extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; -extern int *bpf_kfunc_call_test_get_rdwr_mem (struct prog_test_ref_kfunc *p,const int rdwr_buf_size) __ksym; -extern int *bpf_kfunc_call_test_get_rdonly_mem (struct prog_test_ref_kfunc *p,const int rdonly_buf_size) __ksym; +#include "bpf_testmod/bpf_testmod_kfunc.h" SEC("tc")int kfunc_call_test2 (struct __sk_buff *skb)
tools/testing/selftests/bpf/progs/kfunc_call_test_subprog.c +14 -3
@@ -4,10 +4,21 @@#include <bpf/bpf_helpers.h> #include "bpf_tcp_helpers.h" +/* +* We can't include vmlinux.h, because it conflicts with bpf_tcp_helpers.h, +* but we need refcount_t typedef for bpf_testmod_kfunc.h. +* Adding it directly. +*/ +typedef struct { +int counter; +}atomic_t ; +typedef struct refcount_struct { +atomic_t refs; +}refcount_t ; + +#include "bpf_testmod/bpf_testmod_kfunc.h" + extern const int bpf_prog_active __ksym; -extern __u64 bpf_kfunc_call_test1(struct sock *sk, __u32 a, __u64 b, - __u32 c, __u64 d) __ksym; -extern struct sock *bpf_kfunc_call_test3 (struct sock *sk) __ksym;int active_res =-1 ;int sk_state_res =-1 ;
tools/testing/selftests/bpf/progs/map_kptr.c +1 -0
@@ -2,6 +2,7 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value { struct prog_test_ref_kfunc __kptr *unref_ptr;
tools/testing/selftests/bpf/progs/map_kptr_fail.c +1 -0
@@ -4,6 +4,7 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_core_read.h> #include "bpf_misc.h" +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value {char buf[8 ];
Signature · 1 line
2.39.1
Re: [PATCH bpf-next 1/5] selftests/bpf: Move kfunc exports to bpf_testmod/bpf_testmod_kfunc.h
Quoted 35 lines · Jiri Olsa
Move all kfunc exports into separate header file and include it in tests that need it.
We will move all test kfuncs into
bpf_testmodin following change, so it's convenient to have declarations in single place.Signed-off-by Jiri Olsa <[email protected]>--- .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 89 +++++++++++++++++++ tools/testing/selftests/bpf/progs/cb_refs.c | 1 + .../selftests/bpf/progs/jit_probe_mem.c | 3 +- .../bpf/progs/kfunc_call_destructive.c | 3 +- .../selftests/bpf/progs/kfunc_call_fail.c | 9 +- .../selftests/bpf/progs/kfunc_call_race.c | 3 +- .../selftests/bpf/progs/kfunc_call_test.c | 15 +--- .../bpf/progs/kfunc_call_test_subprog.c | 17 +++- tools/testing/selftests/bpf/progs/map_kptr.c | 1 + .../selftests/bpf/progs/map_kptr_fail.c | 1 + 10 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.htools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h new file +8
-0@@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _BPF_TESTMOD_KFUNC_H +#define _BPF_TESTMOD_KFUNC_H + +#ifndef __ksym +#define __ksym __attribute__((section(".ksyms"))) +#endif
...
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h line 89
Quoted 8 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h new file +8
-0+extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; +extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym; +extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; +extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; + +extern void bpf_kfunc_call_test_destructive(void) __ksym; + +#endif /* _BPF_TESTMOD_KFUNC_H */
This is missing bpf_kfunc_call_test_kptr_get() prototype, the function is moved with the rest in the 5th patch.
Quoted 29 lines
tools/testing/selftests/bpf/progs/cb_refs.c +1
-0@@ -2,6 +2,7 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value { struct prog_test_ref_kfunc __kptr_ref *ptr;tools/testing/selftests/bpf/progs/map_kptr.c +1
-0@@ -2,6 +2,7 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value { struct prog_test_ref_kfunc __kptr *unref_ptr;tools/testing/selftests/bpf/progs/map_kptr_fail.c +0
-0
tools/testing/selftests/bpf/progs/map_kptr_fail.c line 10
Quoted 7 lines
tools/testing/selftests/bpf/progs/map_kptr_fail.c +1
-0@@ -4,6 +4,7 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_core_read.h> #include "bpf_misc.h" +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value {char buf[8 ];
These three are missing old prototype removal.
Signature · 1 line
Artem
Re: [PATCH bpf-next 1/5] selftests/bpf: Move kfunc exports to bpf_testmod/bpf_testmod_kfunc.h
Quoted 39 lines · Artem Savkov
Quoted 35 lines · Jiri Olsa
Move all kfunc exports into separate header file and include it in tests that need it.
We will move all test kfuncs into
bpf_testmodin following change, so it's convenient to have declarations in single place.Signed-off-by Jiri Olsa <[email protected]>--- .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 89 +++++++++++++++++++ tools/testing/selftests/bpf/progs/cb_refs.c | 1 + .../selftests/bpf/progs/jit_probe_mem.c | 3 +- .../bpf/progs/kfunc_call_destructive.c | 3 +- .../selftests/bpf/progs/kfunc_call_fail.c | 9 +- .../selftests/bpf/progs/kfunc_call_race.c | 3 +- .../selftests/bpf/progs/kfunc_call_test.c | 15 +--- .../bpf/progs/kfunc_call_test_subprog.c | 17 +++- tools/testing/selftests/bpf/progs/map_kptr.c | 1 + .../selftests/bpf/progs/map_kptr_fail.c | 1 + 10 files changed, 111 insertions(+), 31 deletions(-) create mode 100644 tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.htools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h new file +8
-0@@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _BPF_TESTMOD_KFUNC_H +#define _BPF_TESTMOD_KFUNC_H + +#ifndef __ksym +#define __ksym __attribute__((section(".ksyms"))) +#endif ...
Quoted 11 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h line 89
Quoted 8 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h new file +8
-0+extern void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) __ksym; +extern void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) __ksym; +extern void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) __ksym; +extern void bpf_kfunc_call_test_mem_len_fail2(__u64 *mem, int len) __ksym; + +extern void bpf_kfunc_call_test_destructive(void) __ksym; + +#endif /* _BPF_TESTMOD_KFUNC_H */ This is missing
bpf_kfunc_call_test_kptr_get()prototype, the function is moved with the rest in the 5th patch.
ok, will add that one as well
Quoted 35 lines
Quoted 35 lines
tools/testing/selftests/bpf/progs/cb_refs.c +1
-0@@ -2,6 +2,7 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value { struct prog_test_ref_kfunc __kptr_ref *ptr;tools/testing/selftests/bpf/progs/map_kptr.c +1
-0@@ -2,6 +2,7 @@#include <vmlinux.h> #include <bpf/bpf_tracing.h> #include <bpf/bpf_helpers.h> +#include "bpf_testmod/bpf_testmod_kfunc.h" struct map_value { struct prog_test_ref_kfunc __kptr *unref_ptr;tools/testing/selftests/bpf/progs/map_kptr_fail.c +1
-0@@ -4,6 +4,7 @@#include <bpf/bpf_helpers.h> #include <bpf/bpf_core_read.h> #include "bpf_misc.h" +#include "bpf_testmod/bpf_testmod_kfunc.h"
Quoted 4 lines
tools/testing/selftests/bpf/progs/map_kptr_fail.c line 10
Quoted 2 lines
tools/testing/selftests/bpf/progs/map_kptr_fail.c +0
-0struct map_value {char buf[8 ];These three are missing old prototype removal.
right, will remove, thanks
jirka
Jiri Olsa
reviewerPATCH 2/5 selftests/bpf: Move test_progs helpers to testing_helpers object
In reply to Jiri OlsaMoving test_progs helpers to testing_helpers object so they can be used from test_verifier in following changes.
Also adding missing ifndef header guard to testing_helpers.h header.
--- tools/testing/selftests/bpf/test_progs.c | 67 +------------------ tools/testing/selftests/bpf/testing_helpers.c | 63 +++++++++++++++++ tools/testing/selftests/bpf/testing_helpers.h | 10 +++ 3 files changed, 75 insertions(+), 65 deletions(-)
tools/testing/selftests/bpf/test_progs.c +2 -65
@@ -11,7 +11,6 @@#include <signal.h> #include <string.h> #include <execinfo.h> /* backtrace */ -#include <linux/membarrier.h> #include <sys/sysinfo.h> /* get_nprocs */ #include <netinet/in.h> #include <sys/select.h>
@@ -615,68 +614,6 @@ int extract_build_id(char *build_id, size_t size)return -1 ; } -static int finit_module (int fd,const char *param_values,int flags) -{ -return syscall (__NR_finit_module, fd, param_values, flags); -} - -static int delete_module (const char *name,int flags) -{ -return syscall (__NR_delete_module, name, flags); -} - -/* -* Trigger synchronize_rcu() in kernel. -*/ -int kern_sync_rcu (void ) -{ -return syscall (__NR_membarrier,MEMBARRIER_CMD_SHARED ,0 ,0 ); -} - -static void unload_bpf_testmod (void ) -{ -if (kern_sync_rcu ()) -fprintf (env.stderr,"Failed to trigger kernel-side RCU sync!\n" ); -if (delete_module ("bpf_testmod" ,0 )) { -if (errno ==ENOENT ) { -if (verbose ()) -fprintf (stdout,"bpf_testmod.ko is already unloaded.\n" ); -return ; - } -fprintf (env.stderr,"Failed to unload bpf_testmod.ko from kernel: %d\n" , -errno); -return ; - } -if (verbose ()) -fprintf (stdout,"Successfully unloaded bpf_testmod.ko.\n" ); -} - -static int load_bpf_testmod (void ) -{ -int fd; - -/* ensure previous instance of the module is unloaded */ -unload_bpf_testmod (); - -if (verbose ()) -fprintf (stdout,"Loading bpf_testmod.ko...\n" ); - - fd =open ("bpf_testmod.ko" ,O_RDONLY ); -if (fd <0 ) { -fprintf (env.stderr,"Can't find bpf_testmod.ko kernel module: %d\n" , -errno); -return -ENOENT ; - } -if (finit_module (fd,"" ,0 )) { -fprintf (env.stderr,"Failed to load bpf_testmod.ko into the kernel: %d\n" , -errno); -close (fd); -return -EINVAL ; - } -close (fd); - -if (verbose ()) -fprintf (stdout,"Successfully loaded bpf_testmod.ko.\n" ); -return 0 ; -} -/* extern declarations for test funcs */ #define DEFINE_TEST (name) \ extern void test_##name(void) __weak; \
@@ -1617,7 +1554,7 @@ int main(int argc, char **argv) env.stderr = stderr; env.has_testmod = true; -if (!env.list_test_names &&load_bpf_testmod ()) { +if (!env.list_test_names &&load_bpf_testmod (env.stderr,verbose ())) {fprintf (env.stderr,"WARNING! Selftests relying on bpf_testmod.ko will be skipped.\n" ); env.has_testmod = false; }
@@ -1716,7 +1653,7 @@ int main(int argc, char **argv)close (env.saved_netns_fd); out:if (!env.list_test_names && env.has_testmod) -unload_bpf_testmod (); +unload_bpf_testmod (env.stderr,verbose ());free_test_selector (&env.test_selector);free_test_selector (&env.subtest_selector);
tools/testing/selftests/bpf/testing_helpers.c +63 -0
72 lines not on this page · raw message
tools/testing/selftests/bpf/testing_helpers.h +10 -0
@@ -1,5 +1,9 @@/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ /* Copyright (C) 2020 Facebook, Inc. */ + +#ifndef __TRACING_HELPERS_H +#define __TRACING_HELPERS_H +#include <stdbool.h> #include <bpf/bpf.h> #include <bpf/libbpf.h>
@@ -20,3 +24,9 @@ struct test_filter_set;int parse_test_list (const char *s,struct test_filter_set *test_set,bool is_glob_pattern); + +int load_bpf_testmod (FILE *err,bool verbose); +void unload_bpf_testmod (FILE *err,bool verbose); +int kern_sync_rcu (void ); + +#endif /* __TRACING_HELPERS_H */
Signature · 1 line
2.39.1
Jiri Olsa
reviewerPATCH 3/5 selftests/bpf: Load bpf_testmod for verifier test
In reply to Jiri OlsaLoading bpf_testmod kernel module for verifier test. We will move all the tests kfuncs into bpf_testmod in following change.
--- tools/testing/selftests/bpf/test_verifier.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/testing/selftests/bpf/test_verifier.c +6 -0
@@ -45,6 +45,7 @@#include "bpf_util.h" #include "test_btf.h" #include "../../../include/linux/filter.h" +#include "testing_helpers.h" #ifndef ENOTSUPP #define ENOTSUPP 524
@@ -1705,6 +1706,9 @@ static int do_test(bool unpriv, unsigned int from, unsigned int to) {int i, passes =0 , errors =0 ; +if (load_bpf_testmod (stderr, verbose)) +return EXIT_FAILURE ; +for (i = from; i < to; i++) {struct bpf_test *test = &tests[i];
@@ -1732,6 +1736,8 @@ static int do_test(bool unpriv, unsigned int from, unsigned int to) } } +unload_bpf_testmod (stderr, verbose); +printf ("Summary: %d PASSED, %d SKIPPED, %d FAILED\n" , passes, skips, errors);return errors ?EXIT_FAILURE :EXIT_SUCCESS ;
Signature · 1 line
2.39.1
Jiri Olsa
reviewerPATCH 4/5 selftests/bpf: Allow to use kfunc from testmod.ko in test_verifier
In reply to Jiri OlsaCurrently the test_verifier allows test to specify kfunc symbol and search for it in the kernel BTF.
Adding the possibility to search for kfunc also in bpf_testmod module when it's not found in kernel BTF.
To find bpf_testmod btf we need to get back SYS_ADMIN cap.
--- tools/testing/selftests/bpf/test_verifier.c | 159 +++++++++++++++++--- 1 file changed, 137 insertions(+), 22 deletions(-)
tools/testing/selftests/bpf/test_verifier.c +137 -22
@@ -879,8 +879,138 @@ static int create_map_kptr(void)return fd; } +static void set_root (bool set) +{ +__u64 caps; + +if (set) { +if (cap_enable_effective (1ULL <<CAP_SYS_ADMIN , &caps)) +perror ("cap_disable_effective(CAP_SYS_ADMIN)" ); + }else { +if (cap_disable_effective (1ULL <<CAP_SYS_ADMIN , &caps)) +perror ("cap_disable_effective(CAP_SYS_ADMIN)" ); + } +} + +static inline __u64 ptr_to_u64 (const void *ptr) +{ +return (__u64 ) (unsigned long ) ptr; +} + +static struct btf *btf__load_testmod_btf (struct btf *vmlinux) +{ +struct bpf_btf_info info; +__u32 len =sizeof (info); +struct btf *btf =NULL ; +char name[64 ]; +__u32 id =0 ; +int err, fd; + +/* Iterate all loaded BTF objects and find bpf_testmod, +* we need SYS_ADMIN cap for that. +*/ +set_root (true); + +while (true) { + err =bpf_btf_get_next_id (id, &id); +if (err) { +if (errno ==ENOENT ) { + err =0 ; +break ; + } +perror ("bpf_btf_get_next_id failed" ); +break ; + } + + fd =bpf_btf_get_fd_by_id (id); +if (fd <0 ) { +if (errno ==ENOENT ) +continue ; +perror ("bpf_btf_get_fd_by_id failed" ); +break ; + } + +memset (&info,0 ,sizeof (info)); + info.name_len =sizeof (name); + info.name =ptr_to_u64 (name); + len =sizeof (info); + + err =bpf_obj_get_info_by_fd (fd, &info, &len); +if (err) { +perror ("bpf_obj_get_info_by_fd failed" ); +break ; + } + +if (strcmp ("bpf_testmod" , name)) { +close (fd); +continue ; + } + + btf =btf__load_from_kernel_by_id_split (id, vmlinux); + +/* We need the fd to stay open so it can be used in fd_array. +* The final cleanup call to btf__free will free btf object +* and close the file descriptor. +*/ +if (btf) +btf__set_fd (btf, fd); +break ; + } + +set_root (false); +return btf; +} + +static struct btf *testmod_btf; +static struct btf *vmlinux_btf; + +static void kfuncs_cleanup (void ) +{ +btf__free (testmod_btf); +btf__free (vmlinux_btf); +} + +static void fixup_prog_kfuncs (struct bpf_insn *prog,int *fd_array, +struct kfunc_btf_id_pair *fixup_kfunc_btf_id) +{ +/* Patch in kfunc BTF IDs */ +while (fixup_kfunc_btf_id->kfunc) { +int btf_id =0 ; + +/* try to find kfunc in kernel BTF */ + vmlinux_btf = vmlinux_btf ?:btf__load_vmlinux_btf (); +if (vmlinux_btf) { + btf_id =btf__find_by_name_kind (vmlinux_btf, + fixup_kfunc_btf_id->kfunc, +BTF_KIND_FUNC ); + btf_id = btf_id <0 ?0 : btf_id; + } + +/* kfunc not found in kernel BTF, try bpf_testmod BTF */ +if (!btf_id) { + testmod_btf = testmod_btf ?:btf__load_testmod_btf (vmlinux_btf); +if (testmod_btf) { + btf_id =btf__find_by_name_kind (testmod_btf, + fixup_kfunc_btf_id->kfunc, +BTF_KIND_FUNC ); + btf_id = btf_id <0 ?0 : btf_id; +if (btf_id) { +/* We put single bpf_testmod module id into fd_array +* and its index 1 into instruction 'off'. +*/ + *fd_array =btf__fd (testmod_btf); + prog[fixup_kfunc_btf_id->insn_idx].off =1 ; + } + } + } + + prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id; + fixup_kfunc_btf_id++; + } +} +static void do_test_fixup (struct bpf_test *test,enum bpf_prog_type prog_type, -struct bpf_insn *prog,int *map_fds) +struct bpf_insn *prog,int *map_fds,int *fd_array) {int *fixup_map_hash_8b = test->fixup_map_hash_8b;int *fixup_map_hash_48b = test->fixup_map_hash_48b;
@@ -905,7 +1035,6 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,int *fixup_map_ringbuf = test->fixup_map_ringbuf;int *fixup_map_timer = test->fixup_map_timer;int *fixup_map_kptr = test->fixup_map_kptr; -struct kfunc_btf_id_pair *fixup_kfunc_btf_id = test->fixup_kfunc_btf_id;if (test->fill_helper) { test->fill_insns =calloc (MAX_TEST_INSNS ,sizeof (struct bpf_insn ));
@@ -1106,25 +1235,7 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, }while (*fixup_map_kptr); } -/* Patch in kfunc BTF IDs */ -if (fixup_kfunc_btf_id->kfunc) { -struct btf *btf; -int btf_id; - -do { - btf_id =0 ; - btf =btf__load_vmlinux_btf (); -if (btf) { - btf_id =btf__find_by_name_kind (btf, - fixup_kfunc_btf_id->kfunc, -BTF_KIND_FUNC ); - btf_id = btf_id <0 ?0 : btf_id; - } -btf__free (btf); - prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id; - fixup_kfunc_btf_id++; - }while (fixup_kfunc_btf_id->kfunc); - } +fixup_prog_kfuncs (prog, fd_array, test->fixup_kfunc_btf_id); }struct libcap {
@@ -1451,6 +1562,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,int run_errs, run_successes;int map_fds[MAX_NR_MAPS ];const char *expected_err; +int fd_array[2 ] = {-1 ,-1 };int saved_errno;int fixup_skips;__u32 pflags;
@@ -1464,7 +1576,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,if (!prog_type) prog_type =BPF_PROG_TYPE_SOCKET_FILTER ; fixup_skips = skips; -do_test_fixup (test, prog_type, prog, map_fds); +do_test_fixup (test, prog_type, prog, map_fds, &fd_array[1 ]);if (test->fill_insns) { prog = test->fill_insns; prog_len = test->prog_len;
@@ -1498,6 +1610,8 @@ static void do_test_single(struct bpf_test *test, bool unpriv,else opts.log_level =DEFAULT_LIBBPF_LOG_LEVEL ; opts.prog_flags = pflags; +if (fd_array[1 ] !=-1 ) + opts.fd_array = &fd_array[0 ];if ((prog_type ==BPF_PROG_TYPE_TRACING || prog_type ==BPF_PROG_TYPE_LSM ) && test->kfunc) {
@@ -1737,6 +1851,7 @@ static int do_test(bool unpriv, unsigned int from, unsigned int to) }unload_bpf_testmod (stderr, verbose); +kfuncs_cleanup ();printf ("Summary: %d PASSED, %d SKIPPED, %d FAILED\n" , passes, skips, errors);
Signature · 1 line
2.39.1
Jiri Olsa
reviewerPATCH 5/5 bpf: Move kernel test kfuncs to bpf_testmod
In reply to Jiri OlsaMoving kernel test kfuncs into bpf_testmod kernel module, and adding necessary init calls and BTF IDs records.
--- net/bpf/test_run.c | 253 +----------------- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 191 ++++++++++++- 2 files changed, 191 insertions(+), 253 deletions(-)
net/bpf/test_run.c +1 -252
@@ -535,201 +535,6 @@ int noinline bpf_modify_return_test(int a, int *b)return a + *b; } -u64 noinline bpf_kfunc_call_test1(struct sock *sk, u32 a, u64 b, u32 c, u64 d) -{ -return a + b + c + d; -} - -int noinline bpf_kfunc_call_test2(struct sock *sk, u32 a, u32 b) -{ -return a + b; -} - -struct sock * noinline bpf_kfunc_call_test3(struct sock *sk) -{ -return sk; -} - -struct prog_test_member1 { -int a; -}; - -struct prog_test_member { -struct prog_test_member1 m; -int c; -}; - -struct prog_test_ref_kfunc { -int a; -int b; -struct prog_test_member memb; -struct prog_test_ref_kfunc *next; -refcount_t cnt; -}; - -static struct prog_test_ref_kfunc prog_test_struct = { - .a =42 , - .b =108 , - .next = &prog_test_struct, - .cnt =REFCOUNT_INIT (1 ), -}; - -noinline struct prog_test_ref_kfunc * -bpf_kfunc_call_test_acquire (unsigned long *scalar_ptr) -{ -refcount_inc (&prog_test_struct.cnt); -return &prog_test_struct; -} - -noinline struct prog_test_member * -bpf_kfunc_call_memb_acquire (void ) -{ -WARN_ON_ONCE (1 ); -return NULL ; -} - -noinline void bpf_kfunc_call_test_release(struct prog_test_ref_kfunc *p) -{ -if (!p) -return ; - -refcount_dec (&p->cnt); -} - -noinline void bpf_kfunc_call_memb_release(struct prog_test_member *p) -{ -} - -noinline void bpf_kfunc_call_memb1_release(struct prog_test_member1 *p) -{ -WARN_ON_ONCE (1 ); -} - -static int *__bpf_kfunc_call_test_get_mem (struct prog_test_ref_kfunc *p,const int size) -{ -if (size >2 *sizeof (int )) -return NULL ; - -return (int *)p; -} - -noinline int *bpf_kfunc_call_test_get_rdwr_mem(struct prog_test_ref_kfunc *p, const int rdwr_buf_size) -{ -return __bpf_kfunc_call_test_get_mem (p, rdwr_buf_size); -} - -noinline int *bpf_kfunc_call_test_get_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) -{ -return __bpf_kfunc_call_test_get_mem (p, rdonly_buf_size); -} - -/* the next 2 ones can't be really used for testing expect to ensure -* that the verifier rejects the call. -* Acquire functions must return struct pointers, so these ones are -* failing. -*/ -noinline int *bpf_kfunc_call_test_acq_rdonly_mem(struct prog_test_ref_kfunc *p, const int rdonly_buf_size) -{ -return __bpf_kfunc_call_test_get_mem (p, rdonly_buf_size); -} - -noinline void bpf_kfunc_call_int_mem_release(int *p) -{ -} - -noinline struct prog_test_ref_kfunc * -bpf_kfunc_call_test_kptr_get (struct prog_test_ref_kfunc **pp,int a,int b) -{ -struct prog_test_ref_kfunc *p =READ_ONCE (*pp); - -if (!p) -return NULL ; -refcount_inc (&p->cnt); -return p; -} - -struct prog_test_pass1 { -int x0; -struct { -int x1; -struct { -int x2; -struct { -int x3; - }; - }; - }; -}; - -struct prog_test_pass2 { -int len; -short arr1[4 ]; -struct { -char arr2[4 ]; -unsigned long arr3[8 ]; - } x; -}; - -struct prog_test_fail1 { -void *p; -int x; -}; - -struct prog_test_fail2 { -int x8; -struct prog_test_pass1 x; -}; - -struct prog_test_fail3 { -int len; -char arr1[2 ]; -char arr2[]; -}; - -noinline void bpf_kfunc_call_test_pass_ctx(struct __sk_buff *skb) -{ -} - -noinline void bpf_kfunc_call_test_pass1(struct prog_test_pass1 *p) -{ -} - -noinline void bpf_kfunc_call_test_pass2(struct prog_test_pass2 *p) -{ -} - -noinline void bpf_kfunc_call_test_fail1(struct prog_test_fail1 *p) -{ -} - -noinline void bpf_kfunc_call_test_fail2(struct prog_test_fail2 *p) -{ -} - -noinline void bpf_kfunc_call_test_fail3(struct prog_test_fail3 *p) -{ -} - -noinline void bpf_kfunc_call_test_mem_len_pass1(void *mem, int mem__sz) -{ -} - -noinline void bpf_kfunc_call_test_mem_len_fail1(void *mem, int len) -{ -} - -noinline void bpf_kfunc_call_test_mem_len_fail2(u64 *mem, int len) -{ -} - -noinline void bpf_kfunc_call_test_ref(struct prog_test_ref_kfunc *p) -{ -} - -noinline void bpf_kfunc_call_test_destructive(void) -{ -} -__diag_pop ();BTF_SET8_START (bpf_test_modify_return_ids)
@@ -742,33 +547,6 @@ static const struct btf_kfunc_id_set bpf_test_modify_return_set = { .set = &bpf_test_modify_return_ids, }; -BTF_SET8_START (test_sk_check_kfunc_ids) -BTF_ID_FLAGS (func ,bpf_kfunc_call_test1 ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test2) -BTF_ID_FLAGS (func, bpf_kfunc_call_test3) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_acquire,KF_ACQUIRE |KF_RET_NULL ) -BTF_ID_FLAGS (func, bpf_kfunc_call_memb_acquire,KF_ACQUIRE |KF_RET_NULL ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_release,KF_RELEASE ) -BTF_ID_FLAGS (func, bpf_kfunc_call_memb_release,KF_RELEASE ) -BTF_ID_FLAGS (func, bpf_kfunc_call_memb1_release,KF_RELEASE ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_get_rdwr_mem,KF_RET_NULL ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_get_rdonly_mem,KF_RET_NULL ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_acq_rdonly_mem,KF_ACQUIRE |KF_RET_NULL ) -BTF_ID_FLAGS (func, bpf_kfunc_call_int_mem_release,KF_RELEASE ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_kptr_get,KF_ACQUIRE |KF_RET_NULL |KF_KPTR_GET ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_pass_ctx) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_pass1) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_pass2) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_fail1) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_fail2) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_fail3) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_mem_len_pass1) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_mem_len_fail1) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_mem_len_fail2) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_ref,KF_TRUSTED_ARGS ) -BTF_ID_FLAGS (func, bpf_kfunc_call_test_destructive,KF_DESTRUCTIVE ) -BTF_SET8_END (test_sk_check_kfunc_ids) -static void *bpf_test_init (const union bpf_attr *kattr,u32 user_size,u32 size,u32 headroom,u32 tailroom) {
@@ -1651,37 +1429,8 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog,return err; } -static const struct btf_kfunc_id_set bpf_prog_test_kfunc_set = { - .owner =THIS_MODULE , - .set = &test_sk_check_kfunc_ids, -}; - -BTF_ID_LIST (bpf_prog_test_dtor_kfunc_ids) -BTF_ID(struct, prog_test_ref_kfunc) -BTF_ID (func, bpf_kfunc_call_test_release) -BTF_ID (struct, prog_test_member) -BTF_ID (func, bpf_kfunc_call_memb_release) -static int __init bpf_prog_test_run_init (void ) { -const struct btf_id_dtor_kfunc bpf_prog_test_dtor_kfunc[] = { - { - .btf_id = bpf_prog_test_dtor_kfunc_ids[0 ], - .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[1 ] - }, - { - .btf_id = bpf_prog_test_dtor_kfunc_ids[2 ], - .kfunc_btf_id = bpf_prog_test_dtor_kfunc_ids[3 ], - }, - }; -int ret; - - ret =register_btf_fmodret_id_set (&bpf_test_modify_return_set); - ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SCHED_CLS , &bpf_prog_test_kfunc_set); - ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_TRACING , &bpf_prog_test_kfunc_set); - ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_SYSCALL , &bpf_prog_test_kfunc_set); -return ret ?:register_btf_id_dtor_kfuncs (bpf_prog_test_dtor_kfunc, -ARRAY_SIZE (bpf_prog_test_dtor_kfunc), -THIS_MODULE ); +return register_btf_fmodret_id_set (&bpf_test_modify_return_set); }late_initcall (bpf_prog_test_run_init);
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +190 -1
216 lines not on this page · raw message
Signature · 1 line
2.39.1
Quoted 10 lines · Jiri Olsa
hi,
I noticed several times in discussions that we should move test kfuncs into kernel module, now perhaps even more pressing with all the kfunc effort. This patchset moves all the test kfuncs intobpf_testmod.I added
bpf_testmod/bpf_testmod_kfunc.hheader that is shared betweenbpf_testmodkernel module and BPF programs, which brings some difficulties with__ksymdefine. But I'm not sure having separate headers for BPF programs and for kernel module would be better.
This part looks fine and overall it's great. Thanks a lot for working on this.
But see failing tests.
test_progs-no_alu32 -t cb_refs
is consistently failing.
Also it seems it introduces instability into other tests. BPF CI isn't happy.
Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · Jiri Olsa
hi,
I noticed several times in discussions that we should move test kfuncs into kernel module, now perhaps even more pressing with all the kfunc effort. This patchset moves all the test kfuncs intobpf_testmod.I added
bpf_testmod/bpf_testmod_kfunc.hheader that is shared betweenbpf_testmodkernel module and BPF programs, which brings some difficulties with__ksymdefine. But I'm not sure having separate headers for BPF programs and for kernel module would be better.
Quoted 4 lines
This part looks fine and overall it's great.
Thanks a lot for working on this.
But see failing tests.
test_progs-no_alu32 -tcb_refs
oops, forgot about alu32 :-\ will check
jirka
Quoted 3 lines
is consistently failing.
Also it seems it introduces instability into other tests. BPF CI isn't happy.
Quoted 13 lines · Jiri Olsa
Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · Jiri Olsa
hi,
I noticed several times in discussions that we should move test kfuncs into kernel module, now perhaps even more pressing with all the kfunc effort. This patchset moves all the test kfuncs intobpf_testmod.I added
bpf_testmod/bpf_testmod_kfunc.hheader that is shared betweenbpf_testmodkernel module and BPF programs, which brings some difficulties with__ksymdefine. But I'm not sure having separate headers for BPF programs and for kernel module would be better.
Quoted 6 lines
Quoted 4 lines
This part looks fine and overall it's great.
Thanks a lot for working on this.
But see failing tests.
test_progs-no_alu32 -tcb_refsoops, forgot about alu32 :-\ will check
it seems to be related to missing commit in bpf-next/master:
74bc3a5acc82 bpf: Add missing btf_put to register_btf_id_dtor_kfuncs
it's in bpf/master now and I have it on my branch, when I revert it I see same errors as in CI
I can include it in next post or wait for bpf-next to catch up with bpf
jirka