Skip to contentSkip to conversation

selftests/bpf: Skip some verifier tests on BTFless kernels

PATCH Superseded Ilya Leoshkevich 2 participants 2 messages Sep 21, 2020 – Sep 23, 2020

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

PATCH 1/1 selftests/bpf: Skip some verifier tests on BTFless kernels

Mark seven tests as requiring vmlinux btf. Check whether vmlinux btf is available, and if not, skip them.

---
 tools/testing/selftests/bpf/test_verifier.c   | 24 +++++++++++++++++++
 tools/testing/selftests/bpf/verifier/d_path.c |  2 ++
 .../testing/selftests/bpf/verifier/map_ptr.c  |  4 ++++
 .../selftests/bpf/verifier/map_ptr_mixing.c   |  1 +
 4 files changed, 31 insertions(+)
tools/testing/selftests/bpf/test_verifier.c +24 -0
index 9be395d9dc64..dc696aa2b9e9 100644
@@ -30,8 +30,10 @@
 #include <linux/bpf.h>
 #include <linux/if_ether.h>
 #include <linux/btf.h>
+#include <linux/err.h>
 
 #include <bpf/bpf.h>
+#include <bpf/btf.h>
 #include <bpf/libbpf.h>
 
 #ifdef HAVE_GENHDR
@@ -115,6 +117,7 @@ struct bpf_test {
 	};
 	enum bpf_attach_type expected_attach_type;
 	const char *kfunc;
+	bool need_vmlinux_btf;
 };
 
 /* Note we want this to be 64 bit aligned so that the end of our array is
@@ -926,6 +929,19 @@ static bool cmp_str_seq(const char *log, const char *exp)
 	return true;
 }
 
+static bool vmlinux_btf_available;
+
+static void probe_vmlinux_btf(void)
+{
+	struct btf *btf_vmlinux;
+
+	btf_vmlinux = libbpf_find_kernel_btf();
+	if (IS_ERR(btf_vmlinux))
+		return;
+	btf__free(btf_vmlinux);
+	vmlinux_btf_available = true;
+}
+
 static void do_test_single(struct bpf_test *test, bool unpriv,
 			   int *passes, int *errors)
 {
@@ -940,6 +956,12 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
 	__u32 pflags;
 	int i, err;
 
+	if (test->need_vmlinux_btf && !vmlinux_btf_available) {
+		printf("SKIP (no vmlinux BTF)\n");
+		skips++;
+		goto done;
+	}
+
 	for (i = 0; i < MAX_NR_MAPS; i++)
 		map_fds[i] = -1;
 
@@ -1097,6 +1119,7 @@ static void do_test_single(struct bpf_test *test, bool unpriv,
 	close(fd_prog);
 	for (i = 0; i < MAX_NR_MAPS; i++)
 		close(map_fds[i]);
+done:
 	sched_yield();
 	return;
 fail_log:
@@ -1230,5 +1253,6 @@ int main(int argc, char **argv)
 	}
 
 	bpf_semi_rand_init();
+	probe_vmlinux_btf();
 	return do_test(unpriv, from, to);
 }
tools/testing/selftests/bpf/verifier/d_path.c +2 -0
index b988396379a7..08613013f828 100644
@@ -15,6 +15,7 @@
 	.prog_type = BPF_PROG_TYPE_TRACING,
 	.expected_attach_type = BPF_TRACE_FENTRY,
 	.kfunc = "dentry_open",
+	.need_vmlinux_btf = true,
 },
 {
 	"d_path reject",
@@ -34,4 +35,5 @@
 	.prog_type = BPF_PROG_TYPE_TRACING,
 	.expected_attach_type = BPF_TRACE_FENTRY,
 	.kfunc = "d_path",
+	.need_vmlinux_btf = true,
 },
tools/testing/selftests/bpf/verifier/map_ptr.c +4 -0
index 637f9293bda8..c6e075dadac6 100644
@@ -12,6 +12,7 @@
 	.errstr_unpriv = "bpf_array access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
 	.result = REJECT,
 	.errstr = "R1 is bpf_array invalid negative access: off=-8",
+	.need_vmlinux_btf = true,
 },
 {
 	"bpf_map_ptr: write rejected",
@@ -29,6 +30,7 @@
 	.errstr_unpriv = "bpf_array access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
 	.result = REJECT,
 	.errstr = "only read from bpf_array is supported",
+	.need_vmlinux_btf = true,
 },
 {
 	"bpf_map_ptr: read non-existent field rejected",
@@ -44,6 +46,7 @@
 	.errstr_unpriv = "bpf_array access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
 	.result = REJECT,
 	.errstr = "cannot access ptr member ops with moff 0 in struct bpf_map with off 1 size 4",
+	.need_vmlinux_btf = true,
 },
 {
 	"bpf_map_ptr: read ops field accepted",
@@ -59,6 +62,7 @@
 	.errstr_unpriv = "bpf_array access is allowed only to CAP_PERFMON and CAP_SYS_ADMIN",
 	.result = ACCEPT,
 	.retval = 1,
+	.need_vmlinux_btf = true,
 },
 {
 	"bpf_map_ptr: r = 0, map_ptr = map_ptr + r",
tools/testing/selftests/bpf/verifier/map_ptr_mixing.c +1 -0
index 1f2b8c4cb26d..75afb8fc3aad 100644
@@ -57,6 +57,7 @@
 	.fixup_map_array_48b = { 13 },
 	.result = REJECT,
 	.errstr = "only read from bpf_array is supported",
+	.need_vmlinux_btf = true,
 },
 {
 	"cond: two branches returning different map pointers for lookup (tail, tail)",
Signature · 1 line
2.25.4
#1 lore raw
Quoted 6 lines · Ilya Leoshkevich

Mark seven tests as requiring vmlinux btf. Check whether vmlinux btf is available, and if not, skip them.

---

Ilya,

These changes look good to me, though kernel BTF has been a requirement for successful BPF selftests run for a while now, so I'm not sure if we want to do this... It will be up to Alexei and Daniel to decide, I suppose. I'm fine either way.

But in any case, it seems like patchworks lost your patch, so please resend it so that it's visible there and can go through normal patch review workflow. Thanks!

Quoted 6 lines
 tools/testing/selftests/bpf/test_verifier.c   | 24 +++++++++++++++++++
 tools/testing/selftests/bpf/verifier/d_path.c |  2 ++
 .../testing/selftests/bpf/verifier/map_ptr.c  |  4 ++++
 .../selftests/bpf/verifier/map_ptr_mixing.c   |  1 +
 4 files changed, 31 insertions(+)

[...]