diff mbox series

[bpf-next,2/2] selftests/bpf: Add test for bpf_kfunc_exists().

Message ID 20230315223607.50803-3-alexei.starovoitov@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Add detection of kfuncs. | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 20 this patch: 20
netdev/cc_maintainers warning 11 maintainers not CCed: linux-kselftest@vger.kernel.org mykolal@fb.com song@kernel.org shuah@kernel.org sdf@google.com haoluo@google.com yhs@fb.com john.fastabend@gmail.com kpsingh@kernel.org jolsa@kernel.org martin.lau@linux.dev
netdev/build_clang success Errors and warnings before: 18 this patch: 18
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 20 this patch: 20
netdev/checkpatch warning CHECK: No space is necessary after a cast
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR fail PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-7 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-8 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-11 pending Logs for test_maps on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-32 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-35 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32 on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_progs_no_alu32_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-29 success Logs for test_progs_parallel on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-30 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-31 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-33 success Logs for test_verifier on aarch64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-34 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-36 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-16 fail Logs for test_progs on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-21 fail Logs for test_progs_no_alu32 on s390x with gcc

Commit Message

Alexei Starovoitov March 15, 2023, 10:36 p.m. UTC
From: Alexei Starovoitov <ast@kernel.org>

Add load and run time test for bpf_kfunc_exists() and check that the verifier
performs dead code elimination for non-existing kfunc.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 .../selftests/bpf/progs/task_kfunc_success.c       | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

Comments

Andrii Nakryiko March 16, 2023, 8:34 p.m. UTC | #1
On Wed, Mar 15, 2023 at 3:36 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> From: Alexei Starovoitov <ast@kernel.org>
>
> Add load and run time test for bpf_kfunc_exists() and check that the verifier
> performs dead code elimination for non-existing kfunc.
>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---

we have prog_tests/ksyms_btf.c and progs/test_ksyms_weak.c which do
these kind of tests for variable ksyms, let's just add kfunc ksyms
there (user-space part has also checking that captured pointer value
is correct and stuff like that, we probably want that for kfuncs as
well)


>  .../selftests/bpf/progs/task_kfunc_success.c       | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_success.c b/tools/testing/selftests/bpf/progs/task_kfunc_success.c
> index 4f61596b0242..c0a7774e0c79 100644
> --- a/tools/testing/selftests/bpf/progs/task_kfunc_success.c
> +++ b/tools/testing/selftests/bpf/progs/task_kfunc_success.c
> @@ -17,6 +17,8 @@ int err, pid;
>   *         TP_PROTO(struct task_struct *p, u64 clone_flags)
>   */
>
> +void invalid_kfunc(void) __ksym __weak;
> +
>  static bool is_test_kfunc_task(void)
>  {
>         int cur_pid = bpf_get_current_pid_tgid() >> 32;
> @@ -26,7 +28,17 @@ static bool is_test_kfunc_task(void)
>
>  static int test_acquire_release(struct task_struct *task)
>  {
> -       struct task_struct *acquired;
> +       struct task_struct *acquired = NULL;
> +
> +       if (!bpf_kfunc_exists(bpf_task_acquire)) {
> +               err = 3;
> +               return 0;
> +       }
> +       if (bpf_kfunc_exists(invalid_kfunc)) {
> +               /* the verifier's dead code elimination should remove this */
> +               err = 4;
> +               asm volatile ("goto -1"); /* for (;;); */
> +       }
>
>         acquired = bpf_task_acquire(task);
>         bpf_task_release(acquired);
> --
> 2.34.1
>
Alexei Starovoitov March 16, 2023, 10:35 p.m. UTC | #2
On Thu, Mar 16, 2023 at 1:34 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Wed, Mar 15, 2023 at 3:36 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > From: Alexei Starovoitov <ast@kernel.org>
> >
> > Add load and run time test for bpf_kfunc_exists() and check that the verifier
> > performs dead code elimination for non-existing kfunc.
> >
> > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> > ---
>
> we have prog_tests/ksyms_btf.c and progs/test_ksyms_weak.c which do
> these kind of tests for variable ksyms, let's just add kfunc ksyms
> there (user-space part has also checking that captured pointer value
> is correct and stuff like that, we probably want that for kfuncs as
> well)

That's where initially I tried to place the test, but test_ksyms_weak.c
is used in light skeleton as well which is pickier about
resolving ksyms.
libbpf was lucky in that sense.
It does:
      if (btf_is_var(t))
          err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
      else
          err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
while gen_loader for lksel assumes bpf_call insn as the only option for kfunc.
I figured I'll add basic support for kfunc detection first and
address lksel later when I have more time.
Hence the reason to pick:
 .../selftests/bpf/progs/task_kfunc_success.c       | 14 +++++++++++++-
as a location for the test.
Andrii Nakryiko March 16, 2023, 11:23 p.m. UTC | #3
On Thu, Mar 16, 2023 at 3:35 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Thu, Mar 16, 2023 at 1:34 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Wed, Mar 15, 2023 at 3:36 PM Alexei Starovoitov
> > <alexei.starovoitov@gmail.com> wrote:
> > >
> > > From: Alexei Starovoitov <ast@kernel.org>
> > >
> > > Add load and run time test for bpf_kfunc_exists() and check that the verifier
> > > performs dead code elimination for non-existing kfunc.
> > >
> > > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> > > ---
> >
> > we have prog_tests/ksyms_btf.c and progs/test_ksyms_weak.c which do
> > these kind of tests for variable ksyms, let's just add kfunc ksyms
> > there (user-space part has also checking that captured pointer value
> > is correct and stuff like that, we probably want that for kfuncs as
> > well)
>
> That's where initially I tried to place the test, but test_ksyms_weak.c
> is used in light skeleton as well which is pickier about
> resolving ksyms.
> libbpf was lucky in that sense.
> It does:
>       if (btf_is_var(t))
>           err = bpf_object__resolve_ksym_var_btf_id(obj, ext);
>       else
>           err = bpf_object__resolve_ksym_func_btf_id(obj, ext);
> while gen_loader for lksel assumes bpf_call insn as the only option for kfunc.
> I figured I'll add basic support for kfunc detection first and
> address lksel later when I have more time.
> Hence the reason to pick:
>  .../selftests/bpf/progs/task_kfunc_success.c       | 14 +++++++++++++-
> as a location for the test.

ok, sounds good, maybe mention this limitation in the commit message?
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/progs/task_kfunc_success.c b/tools/testing/selftests/bpf/progs/task_kfunc_success.c
index 4f61596b0242..c0a7774e0c79 100644
--- a/tools/testing/selftests/bpf/progs/task_kfunc_success.c
+++ b/tools/testing/selftests/bpf/progs/task_kfunc_success.c
@@ -17,6 +17,8 @@  int err, pid;
  *         TP_PROTO(struct task_struct *p, u64 clone_flags)
  */
 
+void invalid_kfunc(void) __ksym __weak;
+
 static bool is_test_kfunc_task(void)
 {
 	int cur_pid = bpf_get_current_pid_tgid() >> 32;
@@ -26,7 +28,17 @@  static bool is_test_kfunc_task(void)
 
 static int test_acquire_release(struct task_struct *task)
 {
-	struct task_struct *acquired;
+	struct task_struct *acquired = NULL;
+
+	if (!bpf_kfunc_exists(bpf_task_acquire)) {
+		err = 3;
+		return 0;
+	}
+	if (bpf_kfunc_exists(invalid_kfunc)) {
+		/* the verifier's dead code elimination should remove this */
+		err = 4;
+		asm volatile ("goto -1"); /* for (;;); */
+	}
 
 	acquired = bpf_task_acquire(task);
 	bpf_task_release(acquired);