[RFC PATCH bpf-next v1 3/3] selftests/bpf: Add selftest for "raw_tp.o"

Fuyu Zhao posted 3 patches 2 weeks, 1 day ago
[RFC PATCH bpf-next v1 3/3] selftests/bpf: Add selftest for "raw_tp.o"
Posted by Fuyu Zhao 2 weeks, 1 day ago
Add test for the new BPF_PROG_TYPE_RAW_TRACEPOINT_OVERRIDE program type.
This test verifies whether a BPF program can successfully override the
target tracepoint probe function.

Signed-off-by: Fuyu Zhao <zhaofuyu@vivo.com>
---
 .../bpf/prog_tests/raw_tp_override_test_run.c | 23 +++++++++++++++++++
 .../bpf/progs/test_raw_tp_override_test_run.c | 20 ++++++++++++++++
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  7 ++++++
 3 files changed, 50 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/raw_tp_override_test_run.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_raw_tp_override_test_run.c

diff --git a/tools/testing/selftests/bpf/prog_tests/raw_tp_override_test_run.c b/tools/testing/selftests/bpf/prog_tests/raw_tp_override_test_run.c
new file mode 100644
index 000000000000..02301253cd9b
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/raw_tp_override_test_run.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <test_progs.h>
+#include "bpf/libbpf_internal.h"
+#include "test_raw_tp_override_test_run.skel.h"
+
+void test_raw_tp_override_test_run(void)
+{
+	struct test_raw_tp_override_test_run *skel;
+
+	skel = test_raw_tp_override_test_run__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "test_raw_tp_override_test_run__open_and_load"))
+		return;
+
+	if (!ASSERT_OK(test_raw_tp_override_test_run__attach(skel),
+		       "test_raw_tp_override_test_run__attach"))
+		goto cleanup;
+	ASSERT_OK(trigger_module_test_write(1), "trigger_write");
+	ASSERT_EQ(skel->bss->flag, 1, "check_flag");
+
+cleanup:
+	test_raw_tp_override_test_run__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_raw_tp_override_test_run.c b/tools/testing/selftests/bpf/progs/test_raw_tp_override_test_run.c
new file mode 100644
index 000000000000..eb6d24e1c737
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_raw_tp_override_test_run.c
@@ -0,0 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+__u32 flag = 0;
+
+/**
+ * This program overrides raw_tp_override_probe handler in
+ * tracepoint bpf_testmode_test_raw_tp_null_tp.
+ */
+SEC("raw_tp.o/bpf_testmod_test_write_bare_tp:raw_tp_override_probe")
+int BPF_PROG(tp_override, struct task_struct *task, char *comm)
+{
+	flag = 1;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
index 2beb9b2fcbd8..7a49178d2343 100644
--- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
+++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod.c
@@ -1628,6 +1628,11 @@ static struct bpf_testmod_multi_st_ops multi_st_ops_cfi_stubs = {
 	.test_1 = bpf_testmod_multi_st_ops__test_1,
 };
 
+static void raw_tp_override_probe(void *ignored, struct task_struct *task,
+				  struct bpf_testmod_test_write_ctx *ctx)
+{
+}
+
 struct bpf_struct_ops testmod_multi_st_ops = {
 	.verifier_ops = &bpf_testmod_verifier_ops,
 	.init = multi_st_ops_init,
@@ -1665,6 +1670,7 @@ static int bpf_testmod_init(void)
 	ret = ret ?: register_btf_id_dtor_kfuncs(bpf_testmod_dtors,
 						 ARRAY_SIZE(bpf_testmod_dtors),
 						 THIS_MODULE);
+	ret = ret ?: register_trace_bpf_testmod_test_write_bare_tp(raw_tp_override_probe, NULL);
 	if (ret < 0)
 		return ret;
 	if (bpf_fentry_test1(0) < 0)
@@ -1701,6 +1707,7 @@ static void bpf_testmod_exit(void)
 	bpf_kfunc_close_sock();
 	sysfs_remove_bin_file(kernel_kobj, &bin_attr_bpf_testmod_file);
 	unregister_bpf_testmod_uprobe();
+	unregister_trace_bpf_testmod_test_write_bare_tp(raw_tp_override_probe, NULL);
 }
 
 module_init(bpf_testmod_init);
-- 
2.43.0