* [PATCH] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs
@ 2021-07-31 14:32 Hengqi Chen
2021-08-02 2:44 ` Yonghong Song
0 siblings, 1 reply; 3+ messages in thread
From: Hengqi Chen @ 2021-07-31 14:32 UTC (permalink / raw)
To: bpf; +Cc: ast, daniel, andrii, yhs, john.fastabend, 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.
Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
---
.../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
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_module.c b/tools/testing/selftests/bpf/prog_tests/btf_module.c
new file mode 100644
index 000000000000..cad1314e3356
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/btf_module.c
@@ -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);
+}
--
2.30.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs
2021-07-31 14:32 [PATCH] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs Hengqi Chen
@ 2021-08-02 2:44 ` Yonghong Song
2021-08-02 15:50 ` Hengqi Chen
0 siblings, 1 reply; 3+ messages in thread
From: Yonghong Song @ 2021-08-02 2:44 UTC (permalink / raw)
To: Hengqi Chen, bpf; +Cc: ast, daniel, andrii, john.fastabend
On 7/31/21 7:32 AM, Hengqi Chen wrote:
> 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.
>
> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
> ---
> .../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
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_module.c b/tools/testing/selftests/bpf/prog_tests/btf_module.c
> new file mode 100644
> index 000000000000..cad1314e3356
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_module.c
> @@ -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;
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)`.
> +
> + 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);
> +}
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs
2021-08-02 2:44 ` Yonghong Song
@ 2021-08-02 15:50 ` Hengqi Chen
0 siblings, 0 replies; 3+ messages in thread
From: Hengqi Chen @ 2021-08-02 15:50 UTC (permalink / raw)
To: Yonghong Song, bpf; +Cc: ast, daniel, andrii, john.fastabend
On 8/2/21 10:44 AM, Yonghong Song wrote:
>
>
> On 7/31/21 7:32 AM, Hengqi Chen wrote:
>> 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.
>>
>> Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
>> ---
>> .../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
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_module.c b/tools/testing/selftests/bpf/prog_tests/btf_module.c
>> new file mode 100644
>> index 000000000000..cad1314e3356
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/btf_module.c
>> @@ -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;
>
> 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.
>> +
>> + 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);
>> +}
>>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-08-02 15:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-31 14:32 [PATCH] selftests/bpf: Test btf__load_vmlinux_btf/btf__load_module_btf APIs Hengqi Chen
2021-08-02 2:44 ` Yonghong Song
2021-08-02 15:50 ` Hengqi Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox