selftests/bpf: more loop tests
Subsystems (5)
Alexei Starovoitov
maintainercover selftests/bpf: more loop tests
Add two bounded loop tests.
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 | 23 ++++++++++++ tools/testing/selftests/bpf/progs/loop5.c | 37 +++++++++++++++++++ 3 files changed, 62 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
Alexei Starovoitov
maintainerPATCH 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.
--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/loop4.c
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1 -0
@@ -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_RAW_TRACEPOINT },/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn.
tools/testing/selftests/bpf/progs/loop4.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#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
Re: [PATCH bpf-next 1/2] selftests/bpf: add loop test 4
Quoted 4 lines · Alexei Starovoitov
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_bodyand the program will be rejected.
The maximum processed insns will be 2^20 or 2^20 * num_of_insns_in_loop_body? I thought the verifier will stop processing once processed insns reach 1M?
Could you elaborate which potential issues in verifier you try to cover with this test case? Extra tests are always welcome. We already have scale/loop tests and some (e.g., strobemeta tests) are more complex than this one. Maybe you have something in mind for this particular test? Putting in the commit message may help people understand the concerns.
Quoted 13 lines
Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop4.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c void test_bpf_verif_scale(void) · line 74
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1
-0@@ -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_RAW_TRACEPOINT },
The program is more like a BPF_PROG_TYPE_SCHED_CLS type than a BPF_PROG_TYPE_RAW_TRACEPOINT?
tools/testing/selftests/bpf/progs/loop4.c line 4
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop4.c new file +4
-0@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h>
Since the program is a networking type,
the above two headers are probably unneeded.
Quoted 20 lines
tools/testing/selftests/bpf/progs/loop4.c new file +19
-0+#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#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; +}
Re: [PATCH bpf-next 1/2] selftests/bpf: add loop test 4
Quoted 10 lines · Yonghong Song
Quoted 4 lines · Alexei Starovoitov
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_bodyand the program will be rejected.The maximum processed insns will be 2^20 or 2^20 *
num_of_insns_in_loop_body?
the later.
Quoted 2 lines
I thought the verifier will
stop processing once processed insns reach 1M?
right.
Quoted 7 lines
Could you elaborate which potential issues in verifier you try to cover with this test case? Extra tests are always welcome. We already have scale/loop tests and some (e.g., strobemeta tests) are more complex than this one. Maybe you have something in mind for this particular test? Putting in the commit message may help people understand the concerns.
it's not testing any new functionality.
It's more targeted test that pruning happens in loop body due to precision tracking. When precision tracking is off the pruning won't be happening and 1m will be hit. Since it's a small test comparing to others it's easier to analyze.
2^20 is to make sure 1m will be hit even if loop body is 1 insn. For the given llmv the loop body is 16 insn, but it may vary.
Quoted 14 lines
Quoted 14 lines
Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop4.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0@@ -71,6 +71,7 @@ void test_bpf_verif_scale(void)
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c void test_bpf_verif_scale(void) · line 74
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1
-0{ "loop1.o", BPF_PROG_TYPE_RAW_TRACEPOINT }, { "loop2.o", BPF_PROG_TYPE_RAW_TRACEPOINT }, + { "loop4.o", BPF_PROG_TYPE_RAW_TRACEPOINT },The program is more like a
BPF_PROG_TYPE_SCHED_CLStype than aBPF_PROG_TYPE_RAW_TRACEPOINT?
right. that's more accurate.
Quoted 16 lines
tools/testing/selftests/bpf/progs/loop4.c line 4
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop4.c new file +4
-0@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> Since the program is a networking type,
the above two headers are probably unneeded.
yeah. just a copy paste. will remove
Quoted 20 lines
Quoted 20 lines
tools/testing/selftests/bpf/progs/loop4.c new file +19
-0+#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#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; +}
Re: [PATCH bpf-next 1/2] selftests/bpf: add loop test 4
In reply to Alexei StarovoitovQuoted 42 lines · Alexei Starovoitov
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_bodyand the program will be rejected.Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/loop4.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1
-0@@ -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_RAW_TRACEPOINT },/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop4.c new file +11
-0@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include <linux/bpf.h> +#include "bpf_helpers.h" + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/loop4.c line 21
Quoted 10 lines
tools/testing/selftests/bpf/progs/loop4.c new file +10
-0+ +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;
So I think the idea is that because verifier shouldn't know whether skb->len is zero or not, then you have two outcomes on every iteration leading to 2^20 states, right?
But I'm afraid that verifier can eventually be smart enough (if it's not already, btw), to figure out that ret can be either 0 or ((1 <<
21) - 1), actually. If skb->len is put into separate register, then that register's bounds will be established on first loop iteration as either == 0 on one branch or (0, inf) on another branch, after which all subsequent iterations will not branch at all (one or the other branch will be always taken).
It's also possible that LLVM/Clang is smart enough already to figure this out on its own and optimize loop into.
if (skb->len) {
for (i = 0; i < 20; i++)
ret |= 1 << i;
}So two complains:
1. Let's obfuscate this a bit more, e.g., with testing (skb->len & (1<<i)) instead, so that result really depends on actual length of the packet.
2. Is it possible to somehow turn off this precision tracking (e.g., running not under root, maybe?) and see that this same program fails in that case? That way we'll know test actually validates what we think it validates.
Thoughts?
Quoted 5 lines
tools/testing/selftests/bpf/progs/loop4.c new file +2
-1+return ret; +} --2.20.0
Re: [PATCH bpf-next 1/2] selftests/bpf: add loop test 4
Quoted 66 lines · Andrii Nakryiko
Quoted 42 lines · Alexei Starovoitov
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_bodyand the program will be rejected.Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop4.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1
-0@@ -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_RAW_TRACEPOINT },/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop4.c new file +11
-0@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include <linux/bpf.h> +#include "bpf_helpers.h" + +char _license[] SEC("license") = "GPL";tools/testing/selftests/bpf/progs/loop4.c line 21
Quoted 10 lines
tools/testing/selftests/bpf/progs/loop4.c new file +10
-0+ +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;So I think the idea is that because verifier shouldn't know whether
skb->lenis zero or not, then you have two outcomes on every iteration leading to 2^20 states, right?But I'm afraid that verifier can eventually be smart enough (if it's not already, btw), to figure out that ret can be either 0 or ((1 <<
21) - 1), actually. Ifskb->lenis put into separate register, then that register's bounds will be established on first loop iteration as either == 0 on one branch or (0, inf) on another branch, after which all subsequent iterations will not branch at all (one or the other branch will be always taken).
Quoted 8 lines
It's also possible that LLVM/Clang is smart enough already to figure this out on its own and optimize loop into.
if (skb->len) { for (i = 0; i < 20; i++) ret |= 1 << i; }
We have
volatile struct __sk_buff* skb
So from the source code, skb->len could be different for each iteration. The compiler cannot do the above optimization.
Quoted 19 lines
So two complains:
1. Let's obfuscate this a bit more, e.g., with testing (
skb->len& (1<<i)) instead, so that result really depends on actual length of the packet.
2. Is it possible to somehow turn off this precision tracking (e.g., running not under root, maybe?) and see that this same program fails in that case? That way we'll know test actually validates what we think it validates.Thoughts?
Quoted 5 lines
tools/testing/selftests/bpf/progs/loop4.c new file +2
-1+return ret; +} --2.20.0
Re: [PATCH bpf-next 1/2] selftests/bpf: add loop test 4
Quoted 78 lines · Yonghong Song
Quoted 66 lines · Andrii Nakryiko
Quoted 42 lines · Alexei Starovoitov
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_bodyand the program will be rejected.Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop4.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1
-0@@ -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_RAW_TRACEPOINT },/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop4.c new file +11
-0@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include <linux/bpf.h> +#include "bpf_helpers.h" + +char _license[] SEC("license") = "GPL";tools/testing/selftests/bpf/progs/loop4.c line 21
Quoted 10 lines
tools/testing/selftests/bpf/progs/loop4.c new file +10
-0+ +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;So I think the idea is that because verifier shouldn't know whether
skb->lenis zero or not, then you have two outcomes on every iteration leading to 2^20 states, right?But I'm afraid that verifier can eventually be smart enough (if it's not already, btw), to figure out that ret can be either 0 or ((1 <<
21) - 1), actually. Ifskb->lenis put into separate register, then that register's bounds will be established on first loop iteration as either == 0 on one branch or (0, inf) on another branch, after which all subsequent iterations will not branch at all (one or the other branch will be always taken).Quoted 8 lines
It's also possible that LLVM/Clang is smart enough already to figure this out on its own and optimize loop into.
if (skb->len) { for (i = 0; i < 20; i++) ret |= 1 << i; }
Quoted 5 lines
We have
volatile struct __sk_buff* skbSo from the source code,
skb->lencould be different for each iteration. The compiler cannot do the above optimization.
yep.
Without volatile llvm optimizes it even more than Andrii predicted :)
Quoted 11 lines
Quoted 11 lines
So two complains:
1. Let's obfuscate this a bit more, e.g., with testing (
skb->len& (1<<i)) instead, so that result really depends on actual length of the packet.
2. Is it possible to somehow turn off this precision tracking (e.g., running not under root, maybe?) and see that this same program fails in that case? That way we'll know test actually validates what we think it validates.
that's on my todo list already.
To do proper unit tests for all this stuff there should be a way to turn off not only precision, but heuristics too. All magic numbers in is_state_visited() need to be switchable. I'm still thinking on the way to expose it to tests infra.
Andrii Nakryiko
Acked-byRe: [PATCH bpf-next 1/2] selftests/bpf: add loop test 4
Quoted 80 lines · Alexei Starovoitov
Quoted 78 lines · Yonghong Song
Quoted 74 lines · Andrii Nakryiko
Quoted 42 lines · Alexei Starovoitov
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_bodyand the program will be rejected.Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop4.c | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop4.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1
-0@@ -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_RAW_TRACEPOINT },/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop4.c new file +11
-0@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#include <linux/bpf.h> +#include "bpf_helpers.h" + +char _license[] SEC("license") = "GPL";tools/testing/selftests/bpf/progs/loop4.c line 21
Quoted 10 lines
tools/testing/selftests/bpf/progs/loop4.c new file +10
-0+ +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;So I think the idea is that because verifier shouldn't know whether
skb->lenis zero or not, then you have two outcomes on every iteration leading to 2^20 states, right?But I'm afraid that verifier can eventually be smart enough (if it's not already, btw), to figure out that ret can be either 0 or ((1 <<
21) - 1), actually. Ifskb->lenis put into separate register, then that register's bounds will be established on first loop iteration as either == 0 on one branch or (0, inf) on another branch, after which all subsequent iterations will not branch at all (one or the other branch will be always taken).It's also possible that LLVM/Clang is smart enough already to figure this out on its own and optimize loop into.
if (skb->len) { for (i = 0; i < 20; i++) ret |= 1 << i; }
Quoted 8 lines
Quoted 5 lines
We have
volatile struct __sk_buff* skbSo from the source code,
skb->lencould be different for each iteration. The compiler cannot do the above optimization.yep.
Without volatile llvm optimizes it even more than Andrii predicted :)
My bad, completely missed volatile.
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
So two complains:
Quoted 7 lines
Quoted 7 lines
1. Let's obfuscate this a bit more, e.g., with testing (
skb->len& (1<<i)) instead, so that result really depends on actual length of the packet.
2. Is it possible to somehow turn off this precision tracking (e.g., running not under root, maybe?) and see that this same program fails in that case? That way we'll know test actually validates what we think it validates.
Quoted 5 lines
that's on my todo list already.
To do proper unit tests for all this stuff there should be a way to turn off not only precision, but heuristics too. All magic numbers inis_state_visited()need to be switchable. I'm still thinking on the way to expose it to tests infra.
Yep, that would be great.
I have nothing beyond what Yonghong suggested.
Alexei Starovoitov
maintainerPATCH 2/2 selftests/bpf: add loop test 5
In reply to Alexei StarovoitovAdd 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 | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/loop5.c
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +1 -0
@@ -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_RAW_TRACEPOINT }, + { "loop5.o", BPF_PROG_TYPE_RAW_TRACEPOINT },/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn.
tools/testing/selftests/bpf/progs/loop5.c new file +37 -0
@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> +#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#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 (true) { +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
Re: [PATCH bpf-next 2/2] selftests/bpf: add loop test 5
Quoted 3 lines · 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.
Agreed with motivation of this test.
Quoted 16 lines
barrier()s are needed to disable llvm optimization that combines multiple branches into fewer branches.
Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop5.c | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop5.ctools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c void test_bpf_verif_scale(void) · line 74
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0@@ -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_RAW_TRACEPOINT }, > + { "loop5.o", BPF_PROG_TYPE_RAW_TRACEPOINT },
More like a BPF_PROG_TYPE_SCHED_CLS type although probably it does not matter as we did not attach it to anywhere?
tools/testing/selftests/bpf/progs/loop5.c line 4
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop5.c new file +4
-0@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h>
The above headers probably not needed.
Quoted 34 lines
tools/testing/selftests/bpf/progs/loop5.c new file +33
-0+#include <stdint.h> +#include <stddef.h> +#include <stdbool.h> +#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 (true) { +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; +}
Re: [PATCH bpf-next 2/2] selftests/bpf: add loop test 5
Quoted 20 lines · Yonghong Song
Quoted 3 lines · 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.Agreed with motivation of this test.
Quoted 11 lines
barrier()s are needed to disable llvm optimization that combines multiple branches into fewer branches.
Signed-off-by Alexei Starovoitov <[email protected]>--- .../bpf/prog_tests/bpf_verif_scale.c | 1 + tools/testing/selftests/bpf/progs/loop5.c | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+)create mode 100644
tools/testing/selftests/bpf/progs/loop5.c
Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c void test_bpf_verif_scale(void) · line 74
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0@@ -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_RAW_TRACEPOINT }, > + { "loop5.o", BPF_PROG_TYPE_RAW_TRACEPOINT },More like a
BPF_PROG_TYPE_SCHED_CLStype although probably it does not matter as we did not attach it to anywhere?
right. will fix.
Quoted 15 lines
tools/testing/selftests/bpf/progs/loop5.c line 4
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpf_verif_scale.c +0
-0/* partial unroll. 19k insn in a loop. * Total program size 20.8k insn. tools/testing/selftests/bpf/progs/loop5.c new file +4
-0@@ -0,0 +1,37 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Facebook +#include <linux/sched.h> +#include <linux/ptrace.h> The above headers probably not needed.
will fix