From: Alexei Starovoitov Scalar evolution finds the step of a loop variable when the expression for one iteration is (+ r imm). Compilers also make other forms of it: insns in the loop expression it is ----------------------- ---------------------- ------------- r6 += 1; r6 += 1 (+ (+ r6 1) 1) (+ r6 2) r6 -= 1 (- r6 1) (+ r6 -1) r6 <<= 32; r6 >>= 32 (>> (<< r6 32) 32) (zext32 r6) The last one is '(u32)i' for cpu v1 and v2, which have no 32-bit insns. With these forms the variable has no formula and the loop is not counted. Each one is an identity for 64-bit numbers that wrap around. The first two are rules of simplify(). The last one is in transfer(), which sees the insns: an expression does not tell the width of a shift. (>> x 32) is also 'w6 >>= w2' with 32 in w2, which shifts by 0. A counted loop is verified with ranges, and there is no way back to one iteration at a time: a prog with 'r6 -= 1' that needs the exact value of a variable in each iteration, or after the loop, is rejected now, as it is with 'r6 += -1'. state_loop_first_last_equal in verifier_precision examines precision backtracking in a loop that is not counted. 'r0 += 1; r0 += 1' is a step of 2 now. Replace it with 'r0 += 2; r0 |= 0', which has no formula. expr_chain needs a deep expression: shift by 16. The verifier processes 3.8 % fewer insns for the selftests that are built without 32-bit insns (start_timer: 46008 -> 32), and 5.3 % fewer for those that gcc builds. No prog is lost. Signed-off-by: Alexei Starovoitov --- kernel/bpf/scev.c | 26 ++++++++++++++++++- .../selftests/bpf/progs/verifier_precision.c | 11 ++++---- .../selftests/bpf/progs/verifier_scev.c | 4 +-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/kernel/bpf/scev.c b/kernel/bpf/scev.c index 8521c34899b0..8662205a3b59 100644 --- a/kernel/bpf/scev.c +++ b/kernel/bpf/scev.c @@ -666,7 +666,7 @@ static int transfer(struct bpf_verifier_env *env, struct env *e, int idx) u8 mode = BPF_MODE(insn->code); u32 dst = insn->dst_reg; u32 src = insn->src_reg; - u32 op, sext; + u32 op, sext, l, r; bool need_bswap; int i, id; s64 imm; @@ -726,6 +726,18 @@ static int transfer(struct bpf_verifier_env *env, struct env *e, int idx) else goto mark_dst_unknown; + /* + * 'r1 <<= 32; r1 >>= 32' is (u32)r1 for cpu v1 and v2. Only here both + * shifts are known to be 64-bit ones: the result of a 32-bit shift is + * (zext32 ...), and (>> x 32) is 'w1 >>= w2' with 32 in w2 as well, + * which shifts by 0. + */ + if (class == BPF_ALU64 && op == BPF_RSH && id >= 0 && + is_imm(scev, id, &imm) && imm == 32 && + is_binop(scev, BPF_LSH, reg2expr[dst], &l, &r) && + is_imm(scev, r, &imm) && imm == 32) + return replace_reg(scev, e, dst, expr1(scev, ZEXT32, l)); + id = expr2(scev, op, reg2expr[dst], id); if (class == BPF_ALU) @@ -1485,6 +1497,18 @@ static int simplify(struct scev *scev, u32 id, void *priv) int a, b, l, r, t, base, slope; s64 imm1, imm2; + /* (- x imm) -> (+ x -imm) */ + if (is_binop(scev, BPF_SUB, id, &l, &r) && + is_imm(scev, r, &imm1) && imm1 != S64_MIN) + return expr2(scev, BPF_ADD, l, imm_expr(scev, -imm1)); + + /* (+ (+ x imm) imm) -> (+ x imm) */ + if (is_add(scev, id, &l, &r) && + is_imm(scev, r, &imm2) && + is_add(scev, l, &a, &b) && + is_imm(scev, b, &imm1)) + return expr2(scev, BPF_ADD, a, imm_expr(scev, (s64)((u64)imm1 + (u64)imm2))); + /* (+ (linear base slope) imm) -> (linear (+ base imm) slope) */ if (is_add(scev, id, &l, &r) && is_linear(scev, l, &base, &slope) && diff --git a/tools/testing/selftests/bpf/progs/verifier_precision.c b/tools/testing/selftests/bpf/progs/verifier_precision.c index f4459561bf39..71ac80a984d8 100644 --- a/tools/testing/selftests/bpf/progs/verifier_precision.c +++ b/tools/testing/selftests/bpf/progs/verifier_precision.c @@ -159,11 +159,11 @@ __success __log_level(2) * This is quite fragile: if verifier checkpointing heuristic changes, this * might need adjusting. */ -__msg("2: (07) r0 += 1 ; R0=6") +__msg("2: (47) r0 |= 0 ; R0=6") __msg("3: (35) if r0 >= 0xa goto pc+1") __msg("mark_precise: frame0: last_idx 3 first_idx 3 subseq_idx -1") -__msg("mark_precise: frame0: regs=r0 stack= before 2: (07) r0 += 1") -__msg("mark_precise: frame0: regs=r0 stack= before 1: (07) r0 += 1") +__msg("mark_precise: frame0: regs=r0 stack= before 2: (47) r0 |= 0") +__msg("mark_precise: frame0: regs=r0 stack= before 1: (07) r0 += 2") __msg("mark_precise: frame0: regs=r0 stack= before 4: (05) goto pc-4") __msg("mark_precise: frame0: regs=r0 stack= before 3: (35) if r0 >= 0xa goto pc+1") __msg("mark_precise: frame0: parent state regs= stack=: R0=P4") @@ -173,8 +173,9 @@ __naked int state_loop_first_last_equal(void) asm volatile ( "r0 = 0;" "l0_%=:" - "r0 += 1;" - "r0 += 1;" + "r0 += 2;" + /* scalar evolution has no formula for it: the loop is not counted */ + "r0 |= 0;" /* every few iterations we'll have a checkpoint here with * first_idx == last_idx, potentially confusing precision * backtracking logic diff --git a/tools/testing/selftests/bpf/progs/verifier_scev.c b/tools/testing/selftests/bpf/progs/verifier_scev.c index 495792b734f5..e6251c21b474 100644 --- a/tools/testing/selftests/bpf/progs/verifier_scev.c +++ b/tools/testing/selftests/bpf/progs/verifier_scev.c @@ -298,8 +298,8 @@ __naked void expr_chain(void) 1: \ if r6 == 2 goto 2f; \ r6 += 1; \ - r6 <<= 32; \ - r6 >>= 32; \ + r6 <<= 16; \ + r6 >>= 16; \ w6 += 1; \ r6 = -r6; \ w6 = -w6; \ -- 2.55.0