From: Alexei Starovoitov A stack walker reads a few words per frame, leaves the loop when a read fails, and appends up to 32 words per frame: bpf_for(i, 0, 100) { if (frames == 8) break; if (READ(a) || READ(b) || READ(c) || READ(d) || READ(e)) break; n = 32 - (a & 1); for (j = 0; j < n; j++) out[idx + j] = b + c + d + e; idx += n; frames++; } 'R1 unbounded memory access': only 'frames == 8' limits idx, and nothing looks at that jump. A back edge has four places for the jumps that leave the loop and dominate it. The nearest ones take them: the NULL test of bpf_iter_num_next() and three statuses. None of them counts. Scalar evolution knows which jumps test a counter. Collect the jumps again when it is done and pass over the others. The latch keeps its place. The search ends at the head of the loop, as for a loop with less than four such jumps. Every jump that is kept tells iterations in which the back edge is not taken, so any choice is sound. The loop has 9 iterations at most. idx has no closed form, so they are looked at one by one: idx is 0, [31, 32], [62, 64] .. [217, 224]. pyperf.h with STACK_MAX_LEN 250 and UNROLL_COUNT 8 leaves its loops from the second of 8 copies of the body: 59713 -> 2877 insns. A jump can leave the loop by way of a flag. These jumps are known before scalar evolution starts, so mark_latches() asks for the SCEV of each. do { ..; more = ++i < 256 && v->in[i & 63]; if (READ(a) || READ(b) || READ(c)) break; } while (more); The four places went to jumps that are nearer to the back edge: "R1 unbounded memory access", 12562 insns on bpf-next. Now 63. Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 2 ++ kernel/bpf/loops.c | 27 ++++++++++++++++++++------- kernel/bpf/scev.c | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 61f133e4d72f..656ed55b96ec 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -716,6 +716,7 @@ struct bpf_backedge { * Conditional jumps within the loop that dominate 'from' and have * a successor outside the loop, nearest first. latches[0] is the * latch; the remaining entries are additional exit conditions. + * After scalar evolution: those that test a counter, see bpf_scev_jump_counts(). * One of them may leave the loop by way of a flag, see bpf_edge_leaves_loop(). * Each usable condition tells iterations in which the back edge is * not taken, so keeping only a subset is sound. @@ -2212,6 +2213,7 @@ bool bpf_is_nested_loop(struct bpf_verifier_env *env, int inner_header, int oute int bpf_init_scev(struct bpf_verifier_env *env); void bpf_free_scev(struct bpf_verifier_env *env); int bpf_compute_scev(struct bpf_verifier_env *env); +int bpf_scev_jump_counts(struct bpf_verifier_env *env, int header, int n, bool taken_leaves); int bpf_compute_loop_iters(struct bpf_verifier_env *env, struct bpf_verifier_state *st, struct bpf_loop_iters *iters); diff --git a/kernel/bpf/loops.c b/kernel/bpf/loops.c index 0f3b0411003b..ce6cef5e1c15 100644 --- a/kernel/bpf/loops.c +++ b/kernel/bpf/loops.c @@ -307,18 +307,19 @@ int bpf_loop_at_index(struct bpf_verifier_env *env, u32 idx) /* * Collect conditional jumps within n's loop that dominate 'n' and leave * the loop on one edge, see bpf_edge_leaves_loop(), nearest first. + * A jump that cannot count the iterations takes no place, see bpf_scev_jump_counts(). */ -static void find_dominating_conditions(struct bpf_verifier_env *env, int n, - struct bpf_backedge *backedge) +static int find_dominating_conditions(struct bpf_verifier_env *env, int n, + struct bpf_backedge *backedge) { struct bpf_insn *insns = env->prog->insnsi; bool t_exits_loop, f_exits_loop; int *idoms = env->idoms; - int n_loop; + int n_loop, counts; n_loop = bpf_loop_at_index(env, n); if (idoms_intersect(env, n, n_loop) != n_loop) - return; + return 0; while (n >= 0) { if (is_cond_jmp_insn(&insns[n]) && bpf_loop_at_index(env, n) == n_loop) { t_exits_loop = bpf_edge_leaves_loop(env, n_loop, n, n + insns[n].off + 1); @@ -326,22 +327,32 @@ static void find_dominating_conditions(struct bpf_verifier_env *env, int n, /* latches[0] is the latch: it has to leave the loop itself */ if ((t_exits_loop || f_exits_loop) && (backedge->latches_cnt || bpf_jump_leaves_loop(env, n_loop, n))) { + counts = !backedge->latches_cnt ? 1 : + bpf_scev_jump_counts(env, n_loop, n, t_exits_loop); + if (counts < 0) + return counts; + if (!counts) + goto next; if (backedge->latches_cnt == MAX_LATCHES) - return; + return 0; if (t_exits_loop) backedge->taken_leaves |= BIT(backedge->latches_cnt); backedge->latches[backedge->latches_cnt++] = n; } } +next: if (n == n_loop) break; n = idoms[n]; } + return 0; } /* * Without @flags: the jumps that leave a loop themselves. That tells where the verifier may add * may_goto. It takes the constants that hold with these edges to follow a flag. + * Once more after scalar evolution, which knows the jumps that count. The flags that were found + * are remembered. */ int bpf_find_latches(struct bpf_verifier_env *env, bool flags) { @@ -377,8 +388,10 @@ int bpf_find_latches(struct bpf_verifier_env *env, bool flags) */ if (bpf_loop_at_index(env, backedge->from) != i) continue; - find_dominating_conditions(env, backedge->from, backedge); - for (k = 1; k < backedge->latches_cnt; k++) + err = find_dominating_conditions(env, backedge->from, backedge); + if (err) + return err; + for (k = 1; flags && k < backedge->latches_cnt; k++) if ((env->log.level & BPF_LOG_LEVEL2) && !bpf_jump_leaves_loop(env, i, backedge->latches[k])) bpf_log(&env->log, "loop at %d, exit by way of a flag at %d\n", diff --git a/kernel/bpf/scev.c b/kernel/bpf/scev.c index 4ce8ad5d5a27..64903ab738a0 100644 --- a/kernel/bpf/scev.c +++ b/kernel/bpf/scev.c @@ -1452,6 +1452,10 @@ static void mark_latches(struct bpf_verifier_env *env) int i, j, k, from; for (i = 0; i < len; i++) { + /* An exit by way of a flag. It may have no place yet, see bpf_scev_jump_counts() */ + if (aux[i].edge_leaves && + can_compute_loop_scev(aux[bpf_loop_at_index(env, i)].loop)) + aux[i].need_scev = true; loop = aux[i].loop; if (!loop) continue; @@ -1864,6 +1868,11 @@ int bpf_compute_scev(struct bpf_verifier_env *env) find_header_env(scev, header)); } + /* Now it's known which jumps test a counter: the places of a back edge are for them */ + err = bpf_find_latches(env, false); + if (err) + return err; + if (env->log.level & BPF_LOG_LEVEL2) { err = log_scevs(env); if (err) @@ -2190,6 +2199,21 @@ static int match_linear_latch(struct bpf_verifier_env *env, return false; } +/* + * Can the jump at @n tell the number of iterations of the loop at @header? The status of a call + * and a NULL test cannot. 1 as long as scalar evolution has not looked at the program. + */ +int bpf_scev_jump_counts(struct bpf_verifier_env *env, int header, int n, bool taken_leaves) +{ + struct linear_latch latch; + + if (!env->scev) + return 1; + if (!find_loop_env(env->scev, header, n)) + return 0; + return match_linear_latch(env, header, n, taken_leaves, &latch); +} + /* What is known about a scalar or about the offset of a pointer. */ struct scev_value { u64 umin, umax; -- 2.55.0