-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpf: Relax precision marking in open coded iters and may_goto loop. #7077
Conversation
Upstream branch: 6c8d759 |
fb42b3d
to
169fa82
Compare
Upstream branch: a87f34e |
8d7821e
to
a2114e7
Compare
Upstream branch: a87f34e |
a2114e7
to
2263885
Compare
169fa82
to
14f768c
Compare
Upstream branch: ecec188 |
2263885
to
e075fd0
Compare
At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=855220 expired. Closing PR. |
Upstream branch: 2c1713a |
e075fd0
to
f36381d
Compare
9d9b65b
to
d5db476
Compare
Upstream branch: f980f13 |
f36381d
to
95c8d8f
Compare
d5db476
to
80e6eff
Compare
v1->v2->v3: - Algorithm changed completely. Motivation for the patch ------------------------ Open coded iterators and may_goto is a great mechanism to implement loops, but counted loops are problematic. For example: for (i = 0; i < 100 && can_loop; i++) is verified as a bounded loop, since i < 100 condition forces the verifier to mark 'i' as precise and loop states at different iterations are not equivalent. That removes the benefit of open coded iterators and may_goto. The workaround is to do: int zero = 0; /* global or volatile variable */ for (i = zero; i < 100 && can_loop; i++) to hide from the verifier the value of 'i'. It's unnatural and so far users didn't learn such odd programming pattern. This patch aims to improve the verifier to support for (i = 0; i < 100000 && can_loop; i++) as open coded iter loop (when 'i' doesn't need to be precise). Algorithm --------- First of all: if (is_may_goto_insn_at(env, insn_idx)) { + update_loop_entry(cur, &sl->state); if (states_equal(env, &sl->state, cur, RANGE_WITHIN)) { - update_loop_entry(cur, &sl->state); It changes the definition of the verifier states loop. Previously, we considered a state loop to be such a sequence of states Si -> ... -> Sj -> ... -> Sk that states_equal(Si, Sk, RANGE_WITHIN) is true. With this change Si -> ... -> Sj -> ... Sk is a loop if call sites and instruction pointers for Si and Sk match. Whether or not Si and Sk are in the loop influences two things: (a) if exact comparison is needed for states cache; (b) if widening transformation could be applied to some scalars. All pairs (Si, Sk) marked as a loop using old definition would be marked as such using new definition (in a addition to some new pairs). Hence it is safe to apply (a) and (b) in strictly more cases. Note that update_loop_entry() relies on the following properties: - every state in the current DFS path (except current) has branches > 0; - states not in the DFS path are either: - in explored_states, are fully explored and have branches == 0; - in env->stack, are not yet explored and have branches == 0 (and also not reachable from is_state_visited()). With that the get_loop_entry() can be used to gate is_branch_taken() logic. When the verifier sees 'r1 > 1000' inside the loop and it can predict it instead of marking r1 as precise it widens both branches, so r1 becomes [0, 1000] in fallthrough and [1001, UMAX] in other_branch. Consider the loop: bpf_for_each(...) { if (r1 > 1000) break; arr[r1] = ..; } At arr[r1] access the r1 is bounded and the loop can quickly converge. Unfortunately compilers (both GCC and LLVM) often optimize loop exit condition to equality, so for (i = 0; i < 100; i++) arr[i] = 1 becomes for (i = 0; i != 100; i++) arr[1] = 1 Hence treat != and == conditions specially in the verifier. Widen only not-predicted branch and keep predict branch as is. Example: r1 = 0 goto L1 L2: arr[r1] = 1 r1++ L1: if r1 != 100 goto L2 fallthrough: r1=100 after widening other_branch: r1 stays as-is (0, 1, 2, ..) Also recognize the case where both LHS and RHS are constant and equal to each other. In this case don't widen at all and take the predicted path. This key heuristic allows the verifier detect loop end condition. Such 'for (i = 0; i != 100; i++)' is validated just like bounded loop. With that the users can use 'for (i = 0; ...' pattern everywhere and many i = zero workarounds can be removed. One tests has drastic improvement. The rest are noise. File Insns (A) Insns (B) Insns (DIFF) States (A) States (B) States (DIFF) -------------------- --------- --------- ---------------- ---------- ---------- ---------------- iters_task_vma.bpf.o 22043 132 -21911 (-99.40%) 1006 10 -996 (-99.01%) Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Remove i = zero workaround, improve arena based tests, add asm test for this_branch_reg->id == other_branch_reg->id condition. Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Upstream branch: e245ef8 |
95c8d8f
to
7e659b8
Compare
At least one diff in series https://patchwork.kernel.org/project/netdevbpf/list/?series=855823 expired. Closing PR. |
Pull request for series with
subject: bpf: Relax precision marking in open coded iters and may_goto loop.
version: 1
url: https://patchwork.kernel.org/project/netdevbpf/list/?series=854891