From: Alexei Starovoitov clang unrolls the loop of pyperf600 in part: 4 iterations with 150 copies of the body. Each copy has two 'break' and a 'return', thus the loop has 451 exits. The list of exits of a loop holds 256. When the list is full scalar evolution gives up on the loop: loop header at 164, unsupported loop: too many exits The complete list is for the loop around. Scalar evolution takes a nested loop for one insn, and the exits are the successors of this insn. No result for the loop itself depends on the list: the number of iterations comes from the jumps that dominate the back edge. mark_latches() prepares the exits for the loop around, and compute_max_iters() asks if there is only one exit. Keep the two apart: exits_overflow the list of this loop is cut nested_exits_overflow the list of a loop in this one is cut, and the list of this loop can lack exits too Only the second one makes scalar evolution give up. loop header at 164, header_count is [0..4] A loop around a loop with more than 256 exits is not analysed, as before. pyperf600 and pyperf180 have 5 such loops each. They get a count. Each of them changes one more register that has no formula, thus the verifier still unrolls them. Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 3 +++ kernel/bpf/loops.c | 6 ++++-- kernel/bpf/scev.c | 15 +++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 724f3b60130f..95e0d7face11 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -746,7 +746,10 @@ struct bpf_loop { int exits_cnt; bool irreducible; bool backedges_overflow; + /* @exits is cut at MAX_LOOP_EXITS. Only a loop around this one needs all of them. */ bool exits_overflow; + /* @exits of a loop in this one is cut. Then @exits of this loop can lack exits too. */ + bool nested_exits_overflow; /* * Loop-entry registers used to compute stack addresses for loads, * stores and calls requiring fixed stack offsets, in this and nested diff --git a/kernel/bpf/loops.c b/kernel/bpf/loops.c index 2568079c2945..e584549bd208 100644 --- a/kernel/bpf/loops.c +++ b/kernel/bpf/loops.c @@ -555,7 +555,7 @@ int bpf_compute_loops(struct bpf_verifier_env *env) * s: ... * } */ - /* Enclosing loops inherit the overflow, see below */ + /* Enclosing loops get nested_exits_overflow, see below */ if (aux[t].loop->exits_overflow) break; err = add_exit(aux[t].loop, i, s); @@ -572,7 +572,7 @@ int bpf_compute_loops(struct bpf_verifier_env *env) if (!loop || !loop->exits_overflow) continue; for (t = aux[i].loop_header; t >= 0; t = aux[t].loop_header) - aux[t].loop->exits_overflow = true; + aux[t].loop->nested_exits_overflow = true; } if (env->log.level & BPF_LOG_LEVEL2) { @@ -587,6 +587,8 @@ int bpf_compute_loops(struct bpf_verifier_env *env) bpf_log(log, ", irreducible"); if (loop->exits_overflow) bpf_log(log, ", too many exits"); + if (loop->nested_exits_overflow) + bpf_log(log, ", too many exits of a nested loop"); bpf_log(log, "\n"); for (j = 0; j < loop->backedges_cnt; j++) { backedge = &loop->backedges[j]; diff --git a/kernel/bpf/scev.c b/kernel/bpf/scev.c index 0d49c9021e30..e0d71df9c0b4 100644 --- a/kernel/bpf/scev.c +++ b/kernel/bpf/scev.c @@ -1260,10 +1260,16 @@ static int join_successor(struct bpf_verifier_env *env, int cur_header, int succ return 0; } -/* Unsupported loops get only an unknown header environment. */ +/* + * Unsupported loops get only an unknown header environment. + * A nested loop is processed as a single instruction, its exits are the successors: all of + * them are needed. No result for the loop itself depends on the list of its own exits: + * mark_latches() prepares them for the loop around, and compute_max_iters() asks if there is + * only one, which a list that is cut does not have. + */ static bool can_compute_loop_scev(const struct bpf_loop *loop) { - return !loop->irreducible && !loop->exits_overflow; + return !loop->irreducible && !loop->nested_exits_overflow; } static int compute_scev_for_loop(struct bpf_verifier_env *env, int cur_header) @@ -3050,12 +3056,13 @@ int bpf_compute_loop_iters(struct bpf_verifier_env *env, struct bpf_verifier_sta */ loop = aux[insn_idx].loop; if (!loop || loop->irreducible || loop->backedges_cnt != 1 || loop->backedges_overflow || - loop->exits_overflow) { + loop->nested_exits_overflow) { if (log->level & BPF_LOG_LEVEL2) bpf_log(log, "loop header at %d, unsupported loop:%s%s%s\n", insn_idx, !loop || loop->irreducible ? " irreducible" : "", loop && loop->backedges_cnt > 1 ? " multiple backedges" : "", - loop && loop->exits_overflow ? " too many exits" : ""); + loop && loop->nested_exits_overflow ? + " too many exits of a nested loop" : ""); return 0; } -- 2.55.0