[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID:
<AM6PR03MB508078EF9EDA7CAC87E8AC3299FE2@AM6PR03MB5080.eurprd03.prod.outlook.com>
Date: Fri, 14 Feb 2025 00:26:57 +0000
From: Juntong Deng <juntong.deng@...look.com>
To: ast@...nel.org,
daniel@...earbox.net,
john.fastabend@...il.com,
andrii@...nel.org,
martin.lau@...ux.dev,
eddyz87@...il.com,
song@...nel.org,
yonghong.song@...ux.dev,
kpsingh@...nel.org,
sdf@...ichev.me,
haoluo@...gle.com,
jolsa@...nel.org,
memxor@...il.com,
snorcht@...il.com
Cc: bpf@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: [RFC PATCH bpf-next 6/6] selftests/bpf: Add test cases for demonstrating runtime acquire/release reference tracking
This patch adds test cases for demonstrating runtime acquire/release
reference tracking.
Test cases include simple, branch, and loop.
Simple test case has no branches or loops.
Branch test case contains if statements.
Loop test case contains the bpf_iter_num iterator.
Signed-off-by: Juntong Deng <juntong.deng@...look.com>
---
tools/testing/selftests/runtime/Makefile | 20 ++++++++++
tools/testing/selftests/runtime/branch.bpf.c | 42 ++++++++++++++++++++
tools/testing/selftests/runtime/branch.c | 19 +++++++++
tools/testing/selftests/runtime/loop.bpf.c | 37 +++++++++++++++++
tools/testing/selftests/runtime/loop.c | 19 +++++++++
tools/testing/selftests/runtime/simple.bpf.c | 35 ++++++++++++++++
tools/testing/selftests/runtime/simple.c | 19 +++++++++
7 files changed, 191 insertions(+)
create mode 100644 tools/testing/selftests/runtime/Makefile
create mode 100644 tools/testing/selftests/runtime/branch.bpf.c
create mode 100644 tools/testing/selftests/runtime/branch.c
create mode 100644 tools/testing/selftests/runtime/loop.bpf.c
create mode 100644 tools/testing/selftests/runtime/loop.c
create mode 100644 tools/testing/selftests/runtime/simple.bpf.c
create mode 100644 tools/testing/selftests/runtime/simple.c
diff --git a/tools/testing/selftests/runtime/Makefile b/tools/testing/selftests/runtime/Makefile
new file mode 100644
index 000000000000..d03133786a26
--- /dev/null
+++ b/tools/testing/selftests/runtime/Makefile
@@ -0,0 +1,20 @@
+targets = simple branch loop
+
+all: $(targets)
+
+vmlinux.h:
+ bpftool btf dump file /sys/kernel/btf/vmlinux format c > vmlinux.h
+
+%.bpf.o: %.bpf.c vmlinux.h
+ clang -O2 -g -target bpf -c $*.bpf.c -o $*.bpf.o
+
+%.skel.h: %.bpf.o
+ bpftool gen skeleton $*.bpf.o > $*.skel.h
+
+$(targets): %: %.c %.skel.h
+ clang $< -lelf -lbpf -o $@
+
+clean:
+ rm -f *.o *.skel.h vmlinux.h $(targets)
+
+.SECONDARY:
diff --git a/tools/testing/selftests/runtime/branch.bpf.c b/tools/testing/selftests/runtime/branch.bpf.c
new file mode 100644
index 000000000000..87697151299c
--- /dev/null
+++ b/tools/testing/selftests/runtime/branch.bpf.c
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+void bpf_task_release(struct task_struct *p) __ksym;
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
+
+int test = 5;
+
+SEC("syscall")
+int test_branch(void *arg)
+{
+ struct task_struct *task1;
+
+ task1 = bpf_task_from_pid(1);
+
+ if (test > 2) {
+ struct task_struct *task2;
+
+ task2 = bpf_task_from_pid(2);
+ if (task2)
+ bpf_task_release(task2);
+ }
+
+ if (test < 2) {
+ struct task_struct *task3;
+
+ task3 = bpf_task_from_pid(3);
+ if (task3)
+ bpf_task_release(task3);
+ }
+
+ if (task1)
+ bpf_task_release(task1);
+
+ return 0;
+}
diff --git a/tools/testing/selftests/runtime/branch.c b/tools/testing/selftests/runtime/branch.c
new file mode 100644
index 000000000000..3592e14f1f75
--- /dev/null
+++ b/tools/testing/selftests/runtime/branch.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+#include "branch.skel.h"
+
+int main(int argc, char **argv)
+{
+ struct branch_bpf *skel;
+ int err, prog_fd;
+
+ skel = branch_bpf__open_and_load();
+ prog_fd = bpf_program__fd(skel->progs.test_branch);
+ err = bpf_prog_test_run_opts(prog_fd, NULL);
+
+ branch_bpf__destroy(skel);
+ return err;
+}
diff --git a/tools/testing/selftests/runtime/loop.bpf.c b/tools/testing/selftests/runtime/loop.bpf.c
new file mode 100644
index 000000000000..2b49ec9e1058
--- /dev/null
+++ b/tools/testing/selftests/runtime/loop.bpf.c
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+void bpf_task_release(struct task_struct *p) __ksym;
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
+
+SEC("syscall")
+int test_loop(void *arg)
+{
+ struct task_struct *task_loop;
+ struct task_struct *task1;
+ int *v;
+
+ task1 = bpf_task_from_pid(1);
+
+ struct bpf_iter_num it;
+
+ bpf_iter_num_new(&it, 1, 3);
+ while ((v = bpf_iter_num_next(&it))) {
+ task_loop = bpf_task_from_pid(*v);
+ if (task_loop)
+ bpf_task_release(task_loop);
+ }
+
+ bpf_iter_num_destroy(&it);
+
+ if (task1)
+ bpf_task_release(task1);
+
+ return 0;
+}
diff --git a/tools/testing/selftests/runtime/loop.c b/tools/testing/selftests/runtime/loop.c
new file mode 100644
index 000000000000..bde83e5595e4
--- /dev/null
+++ b/tools/testing/selftests/runtime/loop.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+#include "loop.skel.h"
+
+int main(int argc, char **argv)
+{
+ struct loop_bpf *skel;
+ int err, prog_fd;
+
+ skel = loop_bpf__open_and_load();
+ prog_fd = bpf_program__fd(skel->progs.test_loop);
+ err = bpf_prog_test_run_opts(prog_fd, NULL);
+
+ loop_bpf__destroy(skel);
+ return err;
+}
diff --git a/tools/testing/selftests/runtime/simple.bpf.c b/tools/testing/selftests/runtime/simple.bpf.c
new file mode 100644
index 000000000000..ad7989ebb7d4
--- /dev/null
+++ b/tools/testing/selftests/runtime/simple.bpf.c
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include <bpf/bpf_core_read.h>
+
+char LICENSE[] SEC("license") = "GPL";
+
+void bpf_task_release(struct task_struct *p) __ksym;
+struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
+
+struct bpf_cpumask *bpf_cpumask_create(void) __ksym;
+void bpf_cpumask_release(struct bpf_cpumask *cpumask) __ksym;
+
+SEC("syscall")
+int test_simple(void *arg)
+{
+ struct task_struct *task;
+ struct bpf_cpumask *cpumask;
+
+ task = bpf_task_from_pid(1);
+ if (!task)
+ return 0;
+
+ cpumask = bpf_cpumask_create();
+ if (!cpumask)
+ goto error_cpumask;
+
+ bpf_cpumask_release(cpumask);
+error_cpumask:
+ bpf_task_release(task);
+
+ return 0;
+}
diff --git a/tools/testing/selftests/runtime/simple.c b/tools/testing/selftests/runtime/simple.c
new file mode 100644
index 000000000000..e65959aac89b
--- /dev/null
+++ b/tools/testing/selftests/runtime/simple.c
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <stdio.h>
+#include <bpf/libbpf.h>
+#include <bpf/bpf.h>
+#include "simple.skel.h"
+
+int main(int argc, char **argv)
+{
+ struct simple_bpf *skel;
+ int err, prog_fd;
+
+ skel = simple_bpf__open_and_load();
+ prog_fd = bpf_program__fd(skel->progs.test_simple);
+ err = bpf_prog_test_run_opts(prog_fd, NULL);
+
+ simple_bpf__destroy(skel);
+ return err;
+}
--
2.39.5
Powered by blists - more mailing lists