Skip to contentSkip to conversation

selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs

PATCH Superseded Hengqi Chen 2 participants 3 messages Jul 31, 2021 – Aug 2, 2021

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs

Add test for btf__load_vmlinux_btf/btf__load_module_btf APIs. It first checks that if btrfs module BTF exists, if yes, load module BTF and check symbol existence.

---
 .../selftests/bpf/prog_tests/btf_module.c     | 31 +++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_module.c
tools/testing/selftests/bpf/prog_tests/btf_module.c new file +31 -0
new file mode 100644
index 000000000000..cad1314e3356
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+
+static const char *module_path = "/sys/kernel/btf/btrfs";
+static const char *module_name = "btrfs";
+
+void test_btf_module()
+{
+	struct btf *vmlinux_btf, *module_btf;
+	__s32 type_id;
+
+	if (access(module_path, F_OK))
+		return;
+
+	vmlinux_btf = btf__load_vmlinux_btf();
+	if (!ASSERT_OK_PTR(vmlinux_btf, "could not load vmlinux BTF"))
+		return;
+
+	module_btf = btf__load_module_btf(module_name, vmlinux_btf);
+	if (!ASSERT_OK_PTR(module_btf, "could not load module BTF"))
+		return;
+
+	type_id = btf__find_by_name(module_btf, "btrfs_file_open");
+	ASSERT_GT(type_id, 0, "func btrfs_file_open not found");
+
+	btf__free(module_btf);
+	btf__free(vmlinux_btf);
+}
Signature · 1 line
2.30.2
#1 lore raw
Quoted 30 lines · Hengqi Chen

Add test for btf__load_vmlinux_btf/btf__load_module_btf APIs. It first checks that if btrfs module BTF exists, if yes, load module BTF and check symbol existence.

---
  .../selftests/bpf/prog_tests/btf_module.c     | 31 +++++++++++++++++++
  1 file changed, 31 insertions(+)

create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_module.c

tools/testing/selftests/bpf/prog_tests/btf_module.c new file +14 -0
new file mode 100644
index 000000000000..cad1314e3356
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+
+static const char *module_path = "/sys/kernel/btf/btrfs";
+static const char *module_name = "btrfs";
+
+void test_btf_module()
+{
+	struct btf *vmlinux_btf, *module_btf;
+	__s32 type_id;
+

tools/testing/selftests/bpf/prog_tests/btf_module.c line 24

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/btf_module.c new file +10 -0
+	if (access(module_path, F_OK))
+		return;
+
+	vmlinux_btf = btf__load_vmlinux_btf();
+	if (!ASSERT_OK_PTR(vmlinux_btf, "could not load vmlinux BTF"))
+		return;
+
+	module_btf = btf__load_module_btf(module_name, vmlinux_btf);
+	if (!ASSERT_OK_PTR(module_btf, "could not load module BTF"))
+		return;

Should we do btf__free(vmlinux_btf) before return? From implementation perspective, maybe use "goto" so we have one place to do btf__free(vmlinux_btf).

Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/btf_module.c new file +7 -0
+
+	type_id = btf__find_by_name(module_btf, "btrfs_file_open");
+	ASSERT_GT(type_id, 0, "func btrfs_file_open not found");
+
+	btf__free(module_btf);
+	btf__free(vmlinux_btf);
+}
#2 lore raw
Quoted 39 lines · Yonghong Song
Quoted 36 lines · Hengqi Chen

Add test for btf__load_vmlinux_btf/btf__load_module_btf APIs. It first checks that if btrfs module BTF exists, if yes, load module BTF and check symbol existence.

---
  .../selftests/bpf/prog_tests/btf_module.c     | 31 +++++++++++++++++++

  1 file changed, 31 insertions(+)

  create mode 100644 tools/testing/selftests/bpf/prog_tests/btf_module.c

tools/testing/selftests/bpf/prog_tests/btf_module.c new file +20 -0
new file mode 100644
index 000000000000..cad1314e3356
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include <test_progs.h>
+#include <bpf/btf.h>
+
+static const char *module_path = "/sys/kernel/btf/btrfs";
+static const char *module_name = "btrfs";
+
+void test_btf_module()
+{
+    struct btf *vmlinux_btf, *module_btf;
+    __s32 type_id;
+
+    if (access(module_path, F_OK))
+        return;
+
+    vmlinux_btf = btf__load_vmlinux_btf();
+    if (!ASSERT_OK_PTR(vmlinux_btf, "could not load vmlinux BTF"))
+        return;
Quoted 9 lines

tools/testing/selftests/bpf/prog_tests/btf_module.c line 24

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/btf_module.c new file +4 -0
+
+    module_btf = btf__load_module_btf(module_name, vmlinux_btf);
+    if (!ASSERT_OK_PTR(module_btf, "could not load module BTF"))
+        return;

Should we do btf__free(vmlinux_btf) before return? From implementation perspective, maybe use "goto" so we have one place to do btf__free(vmlinux_btf).

Didn't notice that ASSERT macro does not abort the execution. Will add cleanup code.

Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/btf_module.c new file +7 -0
+
+    type_id = btf__find_by_name(module_btf, "btrfs_file_open");
+    ASSERT_GT(type_id, 0, "func btrfs_file_open not found");
+
+    btf__free(module_btf);
+    btf__free(vmlinux_btf);
+}