netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/3] RISC-V selftest/bpf fixes
@ 2020-11-17  8:26 Björn Töpel
  2020-11-17  8:26 ` [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build Björn Töpel
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Björn Töpel @ 2020-11-17  8:26 UTC (permalink / raw)
  To: ast, daniel, netdev, bpf
  Cc: Björn Töpel, xi.wang, luke.r.nels, linux-riscv

This series contain some fixes for selftests/bpf when building/running
on a RISC-V host. Details can be found in each individual commit.


Cheers,
Björn

Björn Töpel (3):
  selftests/bpf: Fix broken riscv build
  selftests/bpf: Avoid running unprivileged tests with alignment
    requirements
  selftests/bpf: Mark tests that require unaligned memory access

 tools/testing/selftests/bpf/Makefile          |  3 +-
 tools/testing/selftests/bpf/test_verifier.c   | 12 +++--
 .../selftests/bpf/verifier/ctx_sk_lookup.c    |  7 +++
 .../bpf/verifier/direct_value_access.c        |  3 ++
 .../testing/selftests/bpf/verifier/map_ptr.c  |  1 +
 .../selftests/bpf/verifier/raw_tp_writable.c  |  1 +
 .../selftests/bpf/verifier/ref_tracking.c     |  4 ++
 .../testing/selftests/bpf/verifier/regalloc.c |  8 ++++
 .../selftests/bpf/verifier/wide_access.c      | 46 +++++++++++--------
 9 files changed, 63 insertions(+), 22 deletions(-)

-- 
2.27.0


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build
  2020-11-17  8:26 [PATCH bpf-next 0/3] RISC-V selftest/bpf fixes Björn Töpel
@ 2020-11-17  8:26 ` Björn Töpel
  2020-11-18  1:43   ` Andrii Nakryiko
  2020-11-17  8:26 ` [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements Björn Töpel
  2020-11-17  8:26 ` [PATCH bpf-next 3/3] selftests/bpf: Mark tests that require unaligned memory access Björn Töpel
  2 siblings, 1 reply; 8+ messages in thread
From: Björn Töpel @ 2020-11-17  8:26 UTC (permalink / raw)
  To: ast, daniel, netdev, bpf
  Cc: Björn Töpel, xi.wang, luke.r.nels, linux-riscv

The selftests/bpf Makefile includes system include directories from
the host, when building BPF programs. On RISC-V glibc requires that
__riscv_xlen is defined. This is not the case for "clang -target bpf",
which messes up __WORDSIZE (errno.h -> ... -> wordsize.h) and breaks
the build.

By explicitly defining __risc_xlen correctly for riscv, we can
workaround this.

Fixes: 167381f3eac0 ("selftests/bpf: Makefile fix "missing" headers on build with -idirafter")
Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
---
 tools/testing/selftests/bpf/Makefile | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
index c1708ffa6b1c..9d48769ad268 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -219,7 +219,8 @@ $(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids	\
 # build would have failed anyways.
 define get_sys_includes
 $(shell $(1) -v -E - </dev/null 2>&1 \
-	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
+	| sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
+	$(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' |sed 's/#define /-D/' | sed 's/ /=/')
 endef
 
 # Determine target endianness.
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements
  2020-11-17  8:26 [PATCH bpf-next 0/3] RISC-V selftest/bpf fixes Björn Töpel
  2020-11-17  8:26 ` [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build Björn Töpel
@ 2020-11-17  8:26 ` Björn Töpel
  2020-11-18  1:43   ` Andrii Nakryiko
  2020-11-17  8:26 ` [PATCH bpf-next 3/3] selftests/bpf: Mark tests that require unaligned memory access Björn Töpel
  2 siblings, 1 reply; 8+ messages in thread
From: Björn Töpel @ 2020-11-17  8:26 UTC (permalink / raw)
  To: ast, daniel, netdev, bpf
  Cc: Björn Töpel, xi.wang, luke.r.nels, linux-riscv

Some architectures have strict alignment requirements. In that case,
the BPF verifier detects if a program has unaligned accesses and
rejects them. A user can pass BPF_F_ANY_ALIGNMENT to a program to
override this check. That, however, will only work when a privileged
user loads a program. A unprivileged user loading a program with this
flag will be rejected prior entering the verifier.

Hence, it does not make sense to load unprivileged programs without
strict alignment when testing the verifier. This patch avoids exactly
that.

Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
---
 tools/testing/selftests/bpf/test_verifier.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
index 9be395d9dc64..2075f6a98813 100644
--- a/tools/testing/selftests/bpf/test_verifier.c
+++ b/tools/testing/selftests/bpf/test_verifier.c
@@ -1152,9 +1152,15 @@ static void get_unpriv_disabled()
 
 static bool test_as_unpriv(struct bpf_test *test)
 {
-	return !test->prog_type ||
-	       test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
-	       test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
+	bool req_aligned = false;
+
+#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
+	req_aligned = test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS;
+#endif
+	return (!test->prog_type ||
+		test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
+		test->prog_type == BPF_PROG_TYPE_CGROUP_SKB) &&
+		!req_aligned;
 }
 
 static int do_test(bool unpriv, unsigned int from, unsigned int to)
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH bpf-next 3/3] selftests/bpf: Mark tests that require unaligned memory access
  2020-11-17  8:26 [PATCH bpf-next 0/3] RISC-V selftest/bpf fixes Björn Töpel
  2020-11-17  8:26 ` [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build Björn Töpel
  2020-11-17  8:26 ` [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements Björn Töpel
@ 2020-11-17  8:26 ` Björn Töpel
  2 siblings, 0 replies; 8+ messages in thread
From: Björn Töpel @ 2020-11-17  8:26 UTC (permalink / raw)
  To: ast, daniel, netdev, bpf
  Cc: Björn Töpel, xi.wang, luke.r.nels, linux-riscv

A lot of tests require unaligned memory access to work. Mark the tests
as such, so that they can be avoided on unsupported architectures such
as RISC-V.

Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
---
 .../selftests/bpf/verifier/ctx_sk_lookup.c    |  7 +++
 .../bpf/verifier/direct_value_access.c        |  3 ++
 .../testing/selftests/bpf/verifier/map_ptr.c  |  1 +
 .../selftests/bpf/verifier/raw_tp_writable.c  |  1 +
 .../selftests/bpf/verifier/ref_tracking.c     |  4 ++
 .../testing/selftests/bpf/verifier/regalloc.c |  8 ++++
 .../selftests/bpf/verifier/wide_access.c      | 46 +++++++++++--------
 7 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c b/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c
index 2ad5f974451c..fb13ca2d5606 100644
--- a/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c
+++ b/tools/testing/selftests/bpf/verifier/ctx_sk_lookup.c
@@ -266,6 +266,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"invalid 8-byte read from bpf_sk_lookup remote_ip4 field",
@@ -292,6 +293,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"invalid 8-byte read from bpf_sk_lookup remote_port field",
@@ -305,6 +307,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"invalid 8-byte read from bpf_sk_lookup local_ip4 field",
@@ -331,6 +334,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"invalid 8-byte read from bpf_sk_lookup local_port field",
@@ -344,6 +348,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 /* invalid 1,2,4-byte reads from 8-byte fields in bpf_sk_lookup */
 {
@@ -410,6 +415,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"invalid 4-byte unaligned read from bpf_sk_lookup at even offset",
@@ -422,6 +428,7 @@
 	.result = REJECT,
 	.prog_type = BPF_PROG_TYPE_SK_LOOKUP,
 	.expected_attach_type = BPF_SK_LOOKUP,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 /* in-bound and out-of-bound writes to bpf_sk_lookup */
 {
diff --git a/tools/testing/selftests/bpf/verifier/direct_value_access.c b/tools/testing/selftests/bpf/verifier/direct_value_access.c
index 988f46a1a4c7..c0648dc009b5 100644
--- a/tools/testing/selftests/bpf/verifier/direct_value_access.c
+++ b/tools/testing/selftests/bpf/verifier/direct_value_access.c
@@ -69,6 +69,7 @@
 	.fixup_map_array_48b = { 1 },
 	.result = REJECT,
 	.errstr = "R1 min value is outside of the allowed memory range",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"direct map access, write test 7",
@@ -195,6 +196,7 @@
 	.fixup_map_array_48b = { 1, 3 },
 	.result = REJECT,
 	.errstr = "invalid access to map value, value_size=48 off=47 size=2",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"direct map access, write test 17",
@@ -209,6 +211,7 @@
 	.fixup_map_array_48b = { 1, 3 },
 	.result = REJECT,
 	.errstr = "invalid access to map value, value_size=48 off=47 size=2",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"direct map access, write test 18",
diff --git a/tools/testing/selftests/bpf/verifier/map_ptr.c b/tools/testing/selftests/bpf/verifier/map_ptr.c
index 637f9293bda8..b117bdd3806d 100644
--- a/tools/testing/selftests/bpf/verifier/map_ptr.c
+++ b/tools/testing/selftests/bpf/verifier/map_ptr.c
@@ -44,6 +44,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",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"bpf_map_ptr: read ops field accepted",
diff --git a/tools/testing/selftests/bpf/verifier/raw_tp_writable.c b/tools/testing/selftests/bpf/verifier/raw_tp_writable.c
index 95b5d70a1dc1..2978fb5a769d 100644
--- a/tools/testing/selftests/bpf/verifier/raw_tp_writable.c
+++ b/tools/testing/selftests/bpf/verifier/raw_tp_writable.c
@@ -31,4 +31,5 @@
 	.fixup_map_hash_8b = { 1, },
 	.prog_type = BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE,
 	.errstr = "R6 invalid variable buffer offset: off=0, var_off=(0x0; 0xffffffff)",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
diff --git a/tools/testing/selftests/bpf/verifier/ref_tracking.c b/tools/testing/selftests/bpf/verifier/ref_tracking.c
index 006b5bd99c08..3b6ee009c00b 100644
--- a/tools/testing/selftests/bpf/verifier/ref_tracking.c
+++ b/tools/testing/selftests/bpf/verifier/ref_tracking.c
@@ -675,6 +675,7 @@
 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
 	.result = REJECT,
 	.errstr = "invalid mem access",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"reference tracking: use ptr from bpf_sk_fullsock() after release",
@@ -698,6 +699,7 @@
 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
 	.result = REJECT,
 	.errstr = "invalid mem access",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"reference tracking: use ptr from bpf_sk_fullsock(tp) after release",
@@ -725,6 +727,7 @@
 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
 	.result = REJECT,
 	.errstr = "invalid mem access",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"reference tracking: use sk after bpf_sk_release(tp)",
@@ -747,6 +750,7 @@
 	.prog_type = BPF_PROG_TYPE_SCHED_CLS,
 	.result = REJECT,
 	.errstr = "invalid mem access",
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"reference tracking: use ptr from bpf_get_listener_sock() after bpf_sk_release(sk)",
diff --git a/tools/testing/selftests/bpf/verifier/regalloc.c b/tools/testing/selftests/bpf/verifier/regalloc.c
index 4ad7e05de706..bb0dd89dd212 100644
--- a/tools/testing/selftests/bpf/verifier/regalloc.c
+++ b/tools/testing/selftests/bpf/verifier/regalloc.c
@@ -21,6 +21,7 @@
 	.fixup_map_hash_48b = { 4 },
 	.result = ACCEPT,
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc negative",
@@ -71,6 +72,7 @@
 	.fixup_map_hash_48b = { 4 },
 	.result = ACCEPT,
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc src_reg negative",
@@ -97,6 +99,7 @@
 	.result = REJECT,
 	.errstr = "invalid access to map value, value_size=48 off=44 size=8",
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc and spill",
@@ -126,6 +129,7 @@
 	.fixup_map_hash_48b = { 4 },
 	.result = ACCEPT,
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc and spill negative",
@@ -156,6 +160,7 @@
 	.result = REJECT,
 	.errstr = "invalid access to map value, value_size=48 off=48 size=8",
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc three regs",
@@ -182,6 +187,7 @@
 	.fixup_map_hash_48b = { 4 },
 	.result = ACCEPT,
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc after call",
@@ -210,6 +216,7 @@
 	.fixup_map_hash_48b = { 4 },
 	.result = ACCEPT,
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc in callee",
@@ -240,6 +247,7 @@
 	.fixup_map_hash_48b = { 4 },
 	.result = ACCEPT,
 	.prog_type = BPF_PROG_TYPE_TRACEPOINT,
+	.flags = F_NEEDS_EFFICIENT_UNALIGNED_ACCESS,
 },
 {
 	"regalloc, spill, JEQ",
diff --git a/tools/testing/selftests/bpf/verifier/wide_access.c b/tools/testing/selftests/bpf/verifier/wide_access.c
index ccade9312d21..55af248efa93 100644
--- a/tools/testing/selftests/bpf/verifier/wide_access.c
+++ b/tools/testing/selftests/bpf/verifier/wide_access.c
@@ -1,4 +1,4 @@
-#define BPF_SOCK_ADDR_STORE(field, off, res, err) \
+#define BPF_SOCK_ADDR_STORE(field, off, res, err, flgs)	\
 { \
 	"wide store to bpf_sock_addr." #field "[" #off "]", \
 	.insns = { \
@@ -11,31 +11,36 @@
 	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
 	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
 	.errstr = err, \
+	.flags = flgs, \
 }
 
 /* user_ip6[0] is u64 aligned */
 BPF_SOCK_ADDR_STORE(user_ip6, 0, ACCEPT,
-		    NULL),
+		    NULL, 0),
 BPF_SOCK_ADDR_STORE(user_ip6, 1, REJECT,
-		    "invalid bpf_context access off=12 size=8"),
+		    "invalid bpf_context access off=12 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 BPF_SOCK_ADDR_STORE(user_ip6, 2, ACCEPT,
-		    NULL),
+		    NULL, 0),
 BPF_SOCK_ADDR_STORE(user_ip6, 3, REJECT,
-		    "invalid bpf_context access off=20 size=8"),
+		    "invalid bpf_context access off=20 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 
 /* msg_src_ip6[0] is _not_ u64 aligned */
 BPF_SOCK_ADDR_STORE(msg_src_ip6, 0, REJECT,
-		    "invalid bpf_context access off=44 size=8"),
+		    "invalid bpf_context access off=44 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 BPF_SOCK_ADDR_STORE(msg_src_ip6, 1, ACCEPT,
-		    NULL),
+		    NULL, 0),
 BPF_SOCK_ADDR_STORE(msg_src_ip6, 2, REJECT,
-		    "invalid bpf_context access off=52 size=8"),
+		    "invalid bpf_context access off=52 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT,
-		    "invalid bpf_context access off=56 size=8"),
+		    "invalid bpf_context access off=56 size=8", 0),
 
 #undef BPF_SOCK_ADDR_STORE
 
-#define BPF_SOCK_ADDR_LOAD(field, off, res, err) \
+#define BPF_SOCK_ADDR_LOAD(field, off, res, err, flgs)	\
 { \
 	"wide load from bpf_sock_addr." #field "[" #off "]", \
 	.insns = { \
@@ -48,26 +53,31 @@ BPF_SOCK_ADDR_STORE(msg_src_ip6, 3, REJECT,
 	.prog_type = BPF_PROG_TYPE_CGROUP_SOCK_ADDR, \
 	.expected_attach_type = BPF_CGROUP_UDP6_SENDMSG, \
 	.errstr = err, \
+	.flags = flgs, \
 }
 
 /* user_ip6[0] is u64 aligned */
 BPF_SOCK_ADDR_LOAD(user_ip6, 0, ACCEPT,
-		   NULL),
+		   NULL, 0),
 BPF_SOCK_ADDR_LOAD(user_ip6, 1, REJECT,
-		   "invalid bpf_context access off=12 size=8"),
+		   "invalid bpf_context access off=12 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 BPF_SOCK_ADDR_LOAD(user_ip6, 2, ACCEPT,
-		   NULL),
+		   NULL, 0),
 BPF_SOCK_ADDR_LOAD(user_ip6, 3, REJECT,
-		   "invalid bpf_context access off=20 size=8"),
+		   "invalid bpf_context access off=20 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 
 /* msg_src_ip6[0] is _not_ u64 aligned */
 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 0, REJECT,
-		   "invalid bpf_context access off=44 size=8"),
+		   "invalid bpf_context access off=44 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 1, ACCEPT,
-		   NULL),
+		   NULL, 0),
 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 2, REJECT,
-		   "invalid bpf_context access off=52 size=8"),
+		   "invalid bpf_context access off=52 size=8",
+		    F_NEEDS_EFFICIENT_UNALIGNED_ACCESS),
 BPF_SOCK_ADDR_LOAD(msg_src_ip6, 3, REJECT,
-		   "invalid bpf_context access off=56 size=8"),
+		   "invalid bpf_context access off=56 size=8", 0),
 
 #undef BPF_SOCK_ADDR_LOAD
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build
  2020-11-17  8:26 ` [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build Björn Töpel
@ 2020-11-18  1:43   ` Andrii Nakryiko
  2020-11-18  6:26     ` Björn Töpel
  0 siblings, 1 reply; 8+ messages in thread
From: Andrii Nakryiko @ 2020-11-18  1:43 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Alexei Starovoitov, Daniel Borkmann, Networking, bpf, xi.wang,
	luke.r.nels, linux-riscv

On Tue, Nov 17, 2020 at 12:28 AM Björn Töpel <bjorn.topel@gmail.com> wrote:
>
> The selftests/bpf Makefile includes system include directories from
> the host, when building BPF programs. On RISC-V glibc requires that
> __riscv_xlen is defined. This is not the case for "clang -target bpf",
> which messes up __WORDSIZE (errno.h -> ... -> wordsize.h) and breaks
> the build.
>
> By explicitly defining __risc_xlen correctly for riscv, we can
> workaround this.
>
> Fixes: 167381f3eac0 ("selftests/bpf: Makefile fix "missing" headers on build with -idirafter")
> Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
> ---
>  tools/testing/selftests/bpf/Makefile | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index c1708ffa6b1c..9d48769ad268 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -219,7 +219,8 @@ $(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids  \
>  # build would have failed anyways.
>  define get_sys_includes
>  $(shell $(1) -v -E - </dev/null 2>&1 \
> -       | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
> +       | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
> +       $(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' |sed 's/#define /-D/' | sed 's/ /=/')

just nits: second $(shell ) invocation should be at the same
indentation level as the first one

also '|sed' -> '| sed' ?

Otherwise I have no idea what this does and no way to try it on
RISC-V, but it doesn't break my setup, so I'm fine with it. ;)


>  endef
>
>  # Determine target endianness.
> --
> 2.27.0
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements
  2020-11-17  8:26 ` [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements Björn Töpel
@ 2020-11-18  1:43   ` Andrii Nakryiko
  2020-11-18  6:32     ` Björn Töpel
  0 siblings, 1 reply; 8+ messages in thread
From: Andrii Nakryiko @ 2020-11-18  1:43 UTC (permalink / raw)
  To: Björn Töpel
  Cc: Alexei Starovoitov, Daniel Borkmann, Networking, bpf, xi.wang,
	luke.r.nels, linux-riscv

On Tue, Nov 17, 2020 at 12:29 AM Björn Töpel <bjorn.topel@gmail.com> wrote:
>
> Some architectures have strict alignment requirements. In that case,
> the BPF verifier detects if a program has unaligned accesses and
> rejects them. A user can pass BPF_F_ANY_ALIGNMENT to a program to
> override this check. That, however, will only work when a privileged
> user loads a program. A unprivileged user loading a program with this
> flag will be rejected prior entering the verifier.

I'd include this paragraph as a code comment right next to the check below.

>
> Hence, it does not make sense to load unprivileged programs without
> strict alignment when testing the verifier. This patch avoids exactly
> that.
>
> Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
> ---
>  tools/testing/selftests/bpf/test_verifier.c | 12 +++++++++---
>  1 file changed, 9 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
> index 9be395d9dc64..2075f6a98813 100644
> --- a/tools/testing/selftests/bpf/test_verifier.c
> +++ b/tools/testing/selftests/bpf/test_verifier.c
> @@ -1152,9 +1152,15 @@ static void get_unpriv_disabled()
>
>  static bool test_as_unpriv(struct bpf_test *test)
>  {
> -       return !test->prog_type ||
> -              test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
> -              test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
> +       bool req_aligned = false;
> +
> +#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
> +       req_aligned = test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS;
> +#endif
> +       return (!test->prog_type ||
> +               test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
> +               test->prog_type == BPF_PROG_TYPE_CGROUP_SKB) &&
> +               !req_aligned;

It's a bit convoluted. This seems a bit more straightforward:

#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
    if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
        return false;
#endif
/* the rest of logic untouched */

?






>  }
>
>  static int do_test(bool unpriv, unsigned int from, unsigned int to)
> --
> 2.27.0
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build
  2020-11-18  1:43   ` Andrii Nakryiko
@ 2020-11-18  6:26     ` Björn Töpel
  0 siblings, 0 replies; 8+ messages in thread
From: Björn Töpel @ 2020-11-18  6:26 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Alexei Starovoitov, Daniel Borkmann, Networking, bpf, Xi Wang,
	Luke Nelson, linux-riscv

On Wed, 18 Nov 2020 at 02:43, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Tue, Nov 17, 2020 at 12:28 AM Björn Töpel <bjorn.topel@gmail.com> wrote:
> >
> > The selftests/bpf Makefile includes system include directories from
> > the host, when building BPF programs. On RISC-V glibc requires that
> > __riscv_xlen is defined. This is not the case for "clang -target bpf",
> > which messes up __WORDSIZE (errno.h -> ... -> wordsize.h) and breaks
> > the build.
> >
> > By explicitly defining __risc_xlen correctly for riscv, we can
> > workaround this.
> >
> > Fixes: 167381f3eac0 ("selftests/bpf: Makefile fix "missing" headers on build with -idirafter")
> > Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
> > ---
> >  tools/testing/selftests/bpf/Makefile | 3 ++-
> >  1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> > index c1708ffa6b1c..9d48769ad268 100644
> > --- a/tools/testing/selftests/bpf/Makefile
> > +++ b/tools/testing/selftests/bpf/Makefile
> > @@ -219,7 +219,8 @@ $(RESOLVE_BTFIDS): $(BPFOBJ) | $(BUILD_DIR)/resolve_btfids  \
> >  # build would have failed anyways.
> >  define get_sys_includes
> >  $(shell $(1) -v -E - </dev/null 2>&1 \
> > -       | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }')
> > +       | sed -n '/<...> search starts here:/,/End of search list./{ s| \(/.*\)|-idirafter \1|p }') \
> > +       $(shell $(1) -dM -E - </dev/null | grep '#define __riscv_xlen ' |sed 's/#define /-D/' | sed 's/ /=/')
>
> just nits: second $(shell ) invocation should be at the same
> indentation level as the first one
>
> also '|sed' -> '| sed' ?
>
> Otherwise I have no idea what this does and no way to try it on
> RISC-V, but it doesn't break my setup, so I'm fine with it. ;)
>

:-) I'll fix it up in v2.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements
  2020-11-18  1:43   ` Andrii Nakryiko
@ 2020-11-18  6:32     ` Björn Töpel
  0 siblings, 0 replies; 8+ messages in thread
From: Björn Töpel @ 2020-11-18  6:32 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: Alexei Starovoitov, Daniel Borkmann, Networking, bpf, Xi Wang,
	Luke Nelson, linux-riscv

On Wed, 18 Nov 2020 at 02:43, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
>
> On Tue, Nov 17, 2020 at 12:29 AM Björn Töpel <bjorn.topel@gmail.com> wrote:
> >
> > Some architectures have strict alignment requirements. In that case,
> > the BPF verifier detects if a program has unaligned accesses and
> > rejects them. A user can pass BPF_F_ANY_ALIGNMENT to a program to
> > override this check. That, however, will only work when a privileged
> > user loads a program. A unprivileged user loading a program with this
> > flag will be rejected prior entering the verifier.
>
> I'd include this paragraph as a code comment right next to the check below.
>
> >
> > Hence, it does not make sense to load unprivileged programs without
> > strict alignment when testing the verifier. This patch avoids exactly
> > that.
> >
> > Signed-off-by: Björn Töpel <bjorn.topel@gmail.com>
> > ---
> >  tools/testing/selftests/bpf/test_verifier.c | 12 +++++++++---
> >  1 file changed, 9 insertions(+), 3 deletions(-)
> >
> > diff --git a/tools/testing/selftests/bpf/test_verifier.c b/tools/testing/selftests/bpf/test_verifier.c
> > index 9be395d9dc64..2075f6a98813 100644
> > --- a/tools/testing/selftests/bpf/test_verifier.c
> > +++ b/tools/testing/selftests/bpf/test_verifier.c
> > @@ -1152,9 +1152,15 @@ static void get_unpriv_disabled()
> >
> >  static bool test_as_unpriv(struct bpf_test *test)
> >  {
> > -       return !test->prog_type ||
> > -              test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
> > -              test->prog_type == BPF_PROG_TYPE_CGROUP_SKB;
> > +       bool req_aligned = false;
> > +
> > +#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
> > +       req_aligned = test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS;
> > +#endif
> > +       return (!test->prog_type ||
> > +               test->prog_type == BPF_PROG_TYPE_SOCKET_FILTER ||
> > +               test->prog_type == BPF_PROG_TYPE_CGROUP_SKB) &&
> > +               !req_aligned;
>
> It's a bit convoluted. This seems a bit more straightforward:
>
> #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
>     if (test->flags & F_NEEDS_EFFICIENT_UNALIGNED_ACCESS)
>         return false;
> #endif
> /* the rest of logic untouched */
>
> ?
>

Ugh. Yes, indeed. *blush*


Björn

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-11-18  6:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-17  8:26 [PATCH bpf-next 0/3] RISC-V selftest/bpf fixes Björn Töpel
2020-11-17  8:26 ` [PATCH bpf-next 1/3] selftests/bpf: Fix broken riscv build Björn Töpel
2020-11-18  1:43   ` Andrii Nakryiko
2020-11-18  6:26     ` Björn Töpel
2020-11-17  8:26 ` [PATCH bpf-next 2/3] selftests/bpf: Avoid running unprivileged tests with alignment requirements Björn Töpel
2020-11-18  1:43   ` Andrii Nakryiko
2020-11-18  6:32     ` Björn Töpel
2020-11-17  8:26 ` [PATCH bpf-next 3/3] selftests/bpf: Mark tests that require unaligned memory access Björn Töpel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).