selftests/bpf: Add kprobe session test
authorJiri Olsa <jolsa@kernel.org>
Tue, 30 Apr 2024 11:28:29 +0000 (13:28 +0200)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 30 Apr 2024 17:23:01 +0000 (10:23 -0700)
Adding kprobe session test and testing that the entry program
return value controls execution of the return probe program.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20240430112830.1184228-7-jolsa@kernel.org
tools/testing/selftests/bpf/bpf_kfuncs.h
tools/testing/selftests/bpf/prog_tests/kprobe_multi_test.c
tools/testing/selftests/bpf/progs/kprobe_multi_session.c [new file with mode: 0644]

index 14ebe7d9e1a3652e6fb95f2383a5c6b9ece834fb..cb946d9fd63a13c269d77b1227e5d2f791727c0d 100644 (file)
@@ -75,4 +75,6 @@ extern void bpf_key_put(struct bpf_key *key) __ksym;
 extern int bpf_verify_pkcs7_signature(struct bpf_dynptr *data_ptr,
                                      struct bpf_dynptr *sig_ptr,
                                      struct bpf_key *trusted_keyring) __ksym;
+
+extern bool bpf_session_is_return(void) __ksym __weak;
 #endif
index 51628455b6f528cc2330956a7712ebe4c68ac4e7..42d6592f1e7fcbd43382c04810051709846b56e0 100644 (file)
@@ -4,6 +4,7 @@
 #include "trace_helpers.h"
 #include "kprobe_multi_empty.skel.h"
 #include "kprobe_multi_override.skel.h"
+#include "kprobe_multi_session.skel.h"
 #include "bpf/libbpf_internal.h"
 #include "bpf/hashmap.h"
 
@@ -326,6 +327,42 @@ cleanup:
        kprobe_multi__destroy(skel);
 }
 
+static void test_session_skel_api(void)
+{
+       struct kprobe_multi_session *skel = NULL;
+       LIBBPF_OPTS(bpf_kprobe_multi_opts, opts);
+       LIBBPF_OPTS(bpf_test_run_opts, topts);
+       struct bpf_link *link = NULL;
+       int i, err, prog_fd;
+
+       skel = kprobe_multi_session__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "kprobe_multi_session__open_and_load"))
+               return;
+
+       skel->bss->pid = getpid();
+
+       err = kprobe_multi_session__attach(skel);
+       if (!ASSERT_OK(err, " kprobe_multi_session__attach"))
+               goto cleanup;
+
+       prog_fd = bpf_program__fd(skel->progs.trigger);
+       err = bpf_prog_test_run_opts(prog_fd, &topts);
+       ASSERT_OK(err, "test_run");
+       ASSERT_EQ(topts.retval, 0, "test_run");
+
+       /* bpf_fentry_test1-4 trigger return probe, result is 2 */
+       for (i = 0; i < 4; i++)
+               ASSERT_EQ(skel->bss->kprobe_session_result[i], 2, "kprobe_session_result");
+
+       /* bpf_fentry_test5-8 trigger only entry probe, result is 1 */
+       for (i = 4; i < 8; i++)
+               ASSERT_EQ(skel->bss->kprobe_session_result[i], 1, "kprobe_session_result");
+
+cleanup:
+       bpf_link__destroy(link);
+       kprobe_multi_session__destroy(skel);
+}
+
 static size_t symbol_hash(long key, void *ctx __maybe_unused)
 {
        return str_hash((const char *) key);
@@ -690,4 +727,6 @@ void test_kprobe_multi_test(void)
                test_attach_api_fails();
        if (test__start_subtest("attach_override"))
                test_attach_override();
+       if (test__start_subtest("session"))
+               test_session_skel_api();
 }
diff --git a/tools/testing/selftests/bpf/progs/kprobe_multi_session.c b/tools/testing/selftests/bpf/progs/kprobe_multi_session.c
new file mode 100644 (file)
index 0000000..bbba9eb
--- /dev/null
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <stdbool.h>
+#include "bpf_kfuncs.h"
+
+#define ARRAY_SIZE(x) (int)(sizeof(x) / sizeof((x)[0]))
+
+char _license[] SEC("license") = "GPL";
+
+extern const void bpf_fentry_test1 __ksym;
+extern const void bpf_fentry_test2 __ksym;
+extern const void bpf_fentry_test3 __ksym;
+extern const void bpf_fentry_test4 __ksym;
+extern const void bpf_fentry_test5 __ksym;
+extern const void bpf_fentry_test6 __ksym;
+extern const void bpf_fentry_test7 __ksym;
+extern const void bpf_fentry_test8 __ksym;
+
+int pid = 0;
+
+__u64 kprobe_session_result[8];
+
+static int session_check(void *ctx)
+{
+       unsigned int i;
+       __u64 addr;
+       const void *kfuncs[] = {
+               &bpf_fentry_test1,
+               &bpf_fentry_test2,
+               &bpf_fentry_test3,
+               &bpf_fentry_test4,
+               &bpf_fentry_test5,
+               &bpf_fentry_test6,
+               &bpf_fentry_test7,
+               &bpf_fentry_test8,
+       };
+
+       if (bpf_get_current_pid_tgid() >> 32 != pid)
+               return 1;
+
+       addr = bpf_get_func_ip(ctx);
+
+       for (i = 0; i < ARRAY_SIZE(kfuncs); i++) {
+               if (kfuncs[i] == (void *) addr) {
+                       kprobe_session_result[i]++;
+                       break;
+               }
+       }
+
+       /*
+        * Force probes for function bpf_fentry_test[5-8] not to
+        * install and execute the return probe
+        */
+       if (((const void *) addr == &bpf_fentry_test5) ||
+           ((const void *) addr == &bpf_fentry_test6) ||
+           ((const void *) addr == &bpf_fentry_test7) ||
+           ((const void *) addr == &bpf_fentry_test8))
+               return 1;
+
+       return 0;
+}
+
+/*
+ * No tests in here, just to trigger 'bpf_fentry_test*'
+ * through tracing test_run
+ */
+SEC("fentry/bpf_modify_return_test")
+int BPF_PROG(trigger)
+{
+       return 0;
+}
+
+SEC("kprobe.session/bpf_fentry_test*")
+int test_kprobe(struct pt_regs *ctx)
+{
+       return session_check(ctx);
+}