Skip to contentSkip to conversation

selftests/bpf: Hardcode /sys/kernel/btf/vmlinux in fewer places

PATCH In review Daniel Müller 2 participants 3 messages May 12, 2022 – May 13, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: Hardcode /sys/kernel/btf/vmlinux in fewer places

Two of the BPF selftests hardcode the path to /sys/kernel/btf/vmlinux. The kernel image could potentially exist at a different location. libbpf_find_kernel_btf(), as introduced by commit fb2426ad00b1 ("libbpf:
Expose bpf_find_kernel_btf as a LIBBPF_API"), knows about said locations.

This change switches these two tests over to using this function instead, making the tests more likely to be runnable when /sys/kernel/btf/vmlinux may not be present and setting better precedent.

---
 tools/testing/selftests/bpf/prog_tests/libbpf_probes.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +4 -4
index 9f766dd..61c81a9 100644
@@ -11,8 +11,8 @@ void test_libbpf_probe_prog_types(void)
 	const struct btf_enum *e;
 	int i, n, id;
 
-	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
-	if (!ASSERT_OK_PTR(btf, "btf_parse"))
+	btf = libbpf_find_kernel_btf();
+	if (!ASSERT_OK_PTR(btf, "libbpf_find_kernel_btf"))
 		return;
 
 	/* find enum bpf_prog_type and enumerate each value */
@@ -49,8 +49,8 @@ void test_libbpf_probe_map_types(void)
 	const struct btf_enum *e;
 	int i, n, id;
 
-	btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
-	if (!ASSERT_OK_PTR(btf, "btf_parse"))
+	btf = libbpf_find_kernel_btf();
+	if (!ASSERT_OK_PTR(btf, "libbpf_find_kernel_btf"))
 		return;
 
 	/* find enum bpf_map_type and enumerate each value */
Signature · 1 line
2.30.2
#1 lore raw
Quoted 21 lines · Daniel Müller

Two of the BPF selftests hardcode the path to /sys/kernel/btf/vmlinux. The kernel image could potentially exist at a different location. libbpf_find_kernel_btf(), as introduced by commit fb2426ad00b1 ("libbpf:
Expose bpf_find_kernel_btf as a LIBBPF_API"), knows about said locations.

This change switches these two tests over to using this function instead, making the tests more likely to be runnable when /sys/kernel/btf/vmlinux may not be present and setting better precedent.

---
 tools/testing/selftests/bpf/prog_tests/libbpf_probes.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +0 -0
index 9f766dd..61c81a9 100644

tools/testing/selftests/bpf/prog_tests/libbpf_probes.c void test_libbpf_probe_prog_types(void) · line 14

Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +0 -1
@@ -11,8 +11,8 @@ void test_libbpf_probe_prog_types(void)
        const struct btf_enum *e;
        int i, n, id;
 
-       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);

Selftests go hand in hand with kernel and generally assume specific kernel features enabled (like BTF and sysfs) and having very recent (if not latest) kernel. So there is nothing bad about loading /sys/kernel/btf/vmlinux, I think, it's actually more straightforward to follow the code when it is used explicitly. Libbpf's logic for finding kernel BTF in other places is for older systems. So I'd leave it as is.

Quoted 20 lines
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +4 -4
-       if (!ASSERT_OK_PTR(btf, "btf_parse"))
+       btf = libbpf_find_kernel_btf();
+       if (!ASSERT_OK_PTR(btf, "libbpf_find_kernel_btf"))
                return;
 
        /* find enum bpf_prog_type and enumerate each value */
@@ -49,8 +49,8 @@ void test_libbpf_probe_map_types(void)
        const struct btf_enum *e;
        int i, n, id;
 
-       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);
-       if (!ASSERT_OK_PTR(btf, "btf_parse"))
+       btf = libbpf_find_kernel_btf();
+       if (!ASSERT_OK_PTR(btf, "libbpf_find_kernel_btf"))
                return;
 
        /* find enum bpf_map_type and enumerate each value */
--

2.30.2

#2 lore raw
Quoted 25 lines · Andrii Nakryiko
Quoted 24 lines · Daniel Müller

Two of the BPF selftests hardcode the path to /sys/kernel/btf/vmlinux. The kernel image could potentially exist at a different location. libbpf_find_kernel_btf(), as introduced by commit fb2426ad00b1 ("libbpf:
Expose bpf_find_kernel_btf as a LIBBPF_API"), knows about said locations.

This change switches these two tests over to using this function instead, making the tests more likely to be runnable when /sys/kernel/btf/vmlinux may not be present and setting better precedent.

---
 tools/testing/selftests/bpf/prog_tests/libbpf_probes.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +0 -0
index 9f766dd..61c81a9 100644
@@ -11,8 +11,8 @@ void test_libbpf_probe_prog_types(void)
        const struct btf_enum *e;
        int i, n, id;
 
Quoted 9 lines

tools/testing/selftests/bpf/prog_tests/libbpf_probes.c void test_libbpf_probe_prog_types(void) · line 14

Quoted 1 line
tools/testing/selftests/bpf/prog_tests/libbpf_probes.c +0 -1
-       btf = btf__parse("/sys/kernel/btf/vmlinux", NULL);

Selftests go hand in hand with kernel and generally assume specific kernel features enabled (like BTF and sysfs) and having very recent (if not latest) kernel. So there is nothing bad about loading /sys/kernel/btf/vmlinux, I think, it's actually more straightforward to follow the code when it is used explicitly. Libbpf's logic for finding kernel BTF in other places is for older systems. So I'd leave it as is.

Sounds good to me. Feel free to ignore the patch then.

Daniel