selftests/bpf: Add test for bpf_ksym_exists().
authorAlexei Starovoitov <ast@kernel.org>
Fri, 17 Mar 2023 20:19:20 +0000 (13:19 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 17 Mar 2023 22:46:02 +0000 (15:46 -0700)
Add load and run time test for bpf_ksym_exists() and check that the verifier
performs dead code elimination for non-existing kfunc.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Reviewed-by: Martin KaFai Lau <martin.lau@kernel.org>
Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Acked-by: John Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20230317201920.62030-5-alexei.starovoitov@gmail.com
tools/testing/selftests/bpf/progs/task_kfunc_success.c

index 4f61596..cfa7f12 100644 (file)
@@ -17,6 +17,10 @@ int err, pid;
  *         TP_PROTO(struct task_struct *p, u64 clone_flags)
  */
 
+struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym __weak;
+void invalid_kfunc(void) __ksym __weak;
+void bpf_testmod_test_mod_kfunc(int i) __ksym __weak;
+
 static bool is_test_kfunc_task(void)
 {
        int cur_pid = bpf_get_current_pid_tgid() >> 32;
@@ -26,7 +30,21 @@ 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_ksym_exists(bpf_task_acquire)) {
+               err = 3;
+               return 0;
+       }
+       if (!bpf_ksym_exists(bpf_testmod_test_mod_kfunc)) {
+               err = 4;
+               return 0;
+       }
+       if (bpf_ksym_exists(invalid_kfunc)) {
+               /* the verifier's dead code elimination should remove this */
+               err = 5;
+               asm volatile ("goto -1"); /* for (;;); */
+       }
 
        acquired = bpf_task_acquire(task);
        bpf_task_release(acquired);