Skip to contentSkip to conversation

selftests/bpf: more loop tests

PATCH Applied Alexei Starovoitov 3 participants 5 messages Aug 6, 2019

Subsystems (5)
lore
Share & tools
lore

Alexei Starovoitov

author maintainer
#0 lore raw

v2 cover selftests/bpf: more loop tests

Add two bounded loop tests.

v1-v2: addressed feedback from Yonghong.

Alexei Starovoitov (2):
selftests/bpf: add loop test 4
selftests/bpf: add loop test 5

 .../bpf/prog_tests/bpf_verif_scale.c          |  2 ++
 tools/testing/selftests/bpf/progs/loop4.c     | 18 +++++++++++
 tools/testing/selftests/bpf/progs/loop5.c     | 32 +++++++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/loop4.c
 create mode 100644 tools/testing/selftests/bpf/progs/loop5.c
Signature · 1 line
2.20.0
author maintainer
#1 lore raw

PATCH v2 1/2 selftests/bpf: add loop test 4

Add a test that returns a 'random' number between [0, 2^20) If state pruning is not working correctly for loop body the number of processed insns will be 2^20 * num_of_insns_in_loop_body and the program will be rejected.

---
 .../selftests/bpf/prog_tests/bpf_verif_scale.c |  1 +
 tools/testing/selftests/bpf/progs/loop4.c      | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/loop4.c
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1 -0
index b4be96162ff4..e9e72d8d7aae 100644
@@ -71,6 +71,7 @@ void test_bpf_verif_scale(void)
 
 		{ "loop1.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
 		{ "loop2.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
+		{ "loop4.o", BPF_PROG_TYPE_SCHED_CLS },
 
 		/* partial unroll. 19k insn in a loop.
 		 * Total program size 20.8k insn.
tools/testing/selftests/bpf/progs/loop4.c new file +18 -0
new file mode 100644
index 000000000000..650859022771
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Facebook
+#include <linux/bpf.h>
+#include "bpf_helpers.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("socket")
+int combinations(volatile struct __sk_buff* skb)
+{
+	int ret = 0, i;
+
+#pragma nounroll
+	for (i = 0; i < 20; i++)
+		if (skb->len)
+			ret |= 1 << i;
+	return ret;
+}
Signature · 1 line
2.20.0
author maintainer
#2 lore raw

PATCH v2 2/2 selftests/bpf: add loop test 5

In reply to Alexei Starovoitov

Add a test with multiple exit conditions.
It's not an infinite loop only when the verifier can properly track all math on variable 'i' through all possible ways of executing this loop.

barrier()s are needed to disable llvm optimization that combines multiple branches into fewer branches.

---
 .../bpf/prog_tests/bpf_verif_scale.c          |  1 +
 tools/testing/selftests/bpf/progs/loop5.c     | 32 +++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/loop5.c
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1 -0
index e9e72d8d7aae..0caf8eafa9eb 100644
@@ -72,6 +72,7 @@ void test_bpf_verif_scale(void)
 		{ "loop1.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
 		{ "loop2.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
 		{ "loop4.o", BPF_PROG_TYPE_SCHED_CLS },
+		{ "loop5.o", BPF_PROG_TYPE_SCHED_CLS },
 
 		/* partial unroll. 19k insn in a loop.
 		 * Total program size 20.8k insn.
tools/testing/selftests/bpf/progs/loop5.c new file +32 -0
new file mode 100644
index 000000000000..28d1d668f07c
@@ -0,0 +1,32 @@
+// SPDX-License-Identifier: GPL-2.0
+// Copyright (c) 2019 Facebook
+#include <linux/bpf.h>
+#include "bpf_helpers.h"
+#define barrier() __asm__ __volatile__("": : :"memory")
+
+char _license[] SEC("license") = "GPL";
+
+SEC("socket")
+int while_true(volatile struct __sk_buff* skb)
+{
+	int i = 0;
+
+	while (1) {
+		if (skb->len)
+			i += 3;
+		else
+			i += 7;
+		if (i == 9)
+			break;
+		barrier();
+		if (i == 10)
+			break;
+		barrier();
+		if (i == 13)
+			break;
+		barrier();
+		if (i == 14)
+			break;
+	}
+	return i;
+}
Signature · 1 line
2.20.0
Acked-by on the v2 series
#3 lore raw
In reply to Alexei Starovoitov
Quoted 7 lines · Alexei Starovoitov

Add two bounded loop tests.

v1-v2: addressed feedback from Yonghong.

Alexei Starovoitov (2):
selftests/bpf: add loop test 4
selftests/bpf: add loop test 5

Looks good to me. Ack for the whole series.

Quoted 8 lines
  .../bpf/prog_tests/bpf_verif_scale.c          |  2 ++
  tools/testing/selftests/bpf/progs/loop4.c     | 18 +++++++++++
  tools/testing/selftests/bpf/progs/loop5.c     | 32 +++++++++++++++++++
  3 files changed, 52 insertions(+)

create mode 100644 tools/testing/selftests/bpf/progs/loop4.c create mode 100644 tools/testing/selftests/bpf/progs/loop5.c

#4 lore raw
Quoted 6 lines · Yonghong Song
Quoted 2 lines · Alexei Starovoitov

Add two bounded loop tests.

Quoted 8 lines
Quoted 5 lines

v1-v2: addressed feedback from Yonghong.

Alexei Starovoitov (2):
selftests/bpf: add loop test 4
selftests/bpf: add loop test 5

Looks good to me. Ack for the whole series.

Applied. Thanks