From: Alexei Starovoitov Each main program has passes of its own, and each pass walks all insns of the load several times: snapshots, landing pads, loops. With many main programs that have a lot of code of their own the shared load is much slower than copies. 64 tc programs, 5K insns each, that call one common function: copies 0.42 s, shared 3.26 s -> 0.52 s A main program needs only the functions that it gets to. Keep the call graph of sort_subprogs_topo(), mark these functions and walk only them. The graph has no edges of callx and of dead code, so mark the functions that have their address taken or are recursive for all main programs. The last main program still walks all functions, so what is emitted is settled over the whole load as before. A conflict in a caller that only an earlier program gets to shows there, and the load is rejected. A load with one main program is not affected. Signed-off-by: Alexei Starovoitov --- include/linux/bpf_verifier.h | 9 ++++++++ kernel/bpf/fixups.c | 5 +++-- kernel/bpf/flow.c | 25 +++++++++++---------- kernel/bpf/pads.c | 34 ++++++++++++++++++---------- kernel/bpf/precision.c | 16 +++++++------- kernel/bpf/verifier.c | 43 +++++++++++++++++++++++++++--------- 6 files changed, 88 insertions(+), 44 deletions(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 619df34f0ced..acef0bd410b1 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -2013,6 +2013,8 @@ struct bpf_verifier_env { struct bpf_subprog_info *subprog_info; /* subprog indices sorted in topological order: leaves first, callers last */ int *subprog_topo_order; + struct bpf_call_graph calls; /* marks: see mark_pass_subprogs() */ + u32 pass_len; /* insns of the marked subprogs */ /* * Pointers to static subprogs found in frozen read-only maps of the * program, see resolve_func_ptrs(). Sorted by map and map_off. @@ -2204,6 +2206,13 @@ static inline void bpf_note_bug(struct bpf_verifier_env *env) bpf_log(&env->log, "verifier bug: " fmt "\n", ##args); \ }) +/* Two loops, break does not leave it */ +#define bpf_for_each_pass_insn(env, i) \ + for (u32 ___s = 0, ___end; ___s < (env)->subprog_cnt; ___s++) \ + for (i = (env)->subprog_info[___s].start, \ + ___end = (env)->calls.marks[___s] ? (env)->subprog_info[___s + 1].start : 0; \ + i < ___end; i++) + static inline void mark_calls_callback(struct bpf_verifier_env *env, int idx) { env->insn_aux_data[idx].calls_callback = true; diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 4d76b9a83b39..f8c70dd90bfb 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -708,7 +708,7 @@ int bpf_mark_subprogs_that_unwind(struct bpf_verifier_env *env, bool in_pass) do { changed = false; /* callees come first: only callx and a cycle of calls take more than one round */ - err = in_pass ? bpf_add_cost_and_check(env, env->prog->len, BPF_COST_SCAN) : + err = in_pass ? bpf_add_cost_and_check(env, env->pass_len, BPF_COST_SCAN) : bpf_must_abort() ? -EINTR : 0; if (err) return err; @@ -716,7 +716,8 @@ int bpf_mark_subprogs_that_unwind(struct bpf_verifier_env *env, bool in_pass) s = env->subprog_topo_order[k]; sub = &env->subprog_info[s]; if ((sub->can_unwind && sub->can_cancel) || - (!sub->main && !sub->bail_unwinds)) + (!sub->main && !sub->bail_unwinds) || + (in_pass && !env->calls.marks[s])) continue; unwinds = sub->can_unwind; cancels = sub->can_cancel; diff --git a/kernel/bpf/flow.c b/kernel/bpf/flow.c index 30758913c0a0..4ccd5386805c 100644 --- a/kernel/bpf/flow.c +++ b/kernel/bpf/flow.c @@ -344,7 +344,7 @@ bool bpf_flow_loops_lost_bound(struct bpf_verifier_env *env) struct bpf_loop *loop; int h; - for (h = 0; h < env->prog->len; h++) { + bpf_for_each_pass_insn(env, h) { loop = env->insn_aux_data[h].loop; if (loop && loop->rt_bounded && !flow_loop_is_bounded(env, h)) return true; @@ -388,8 +388,8 @@ static void flow_forget_loop(struct bpf_verifier_env *env, struct bpf_loop *loop /* A pass starts. It verifies the main prog and the global functions. */ void bpf_flow_new_pass(struct bpf_verifier_env *env) { - struct bpf_insn_aux_data *aux = env->insn_aux_data; struct bpf_flow *flow = env->flow; + struct bpf_insn_aux_data *aux; int i; memset(flow->cost, 0, env->subprog_cnt * sizeof(*flow->cost)); @@ -397,7 +397,8 @@ void bpf_flow_new_pass(struct bpf_verifier_env *env) flow->budget_func_cnt = 0; flow->apart_cost = 0; flow->start_cnt++; - for (i = 0; i < env->prog->len; i++, aux++) { + bpf_for_each_pass_insn(env, i) { + aux = &env->insn_aux_data[i]; if (aux->callback_loop) aux->callback_loop->this_pass = (struct bpf_loop_per_pass){}; if (aux->recursion_loop) @@ -422,9 +423,9 @@ void bpf_flow_note_guards(struct bpf_verifier_env *env) struct bpf_insn_aux_data *aux = env->insn_aux_data; int i; - for (i = 0; i < env->prog->len; i++, aux++) { - aux->needs_guard |= aux->loop && aux->loop->this_pass.needs_guard; - aux->no_guard_edges |= aux->loop && aux->loop->this_pass.no_guard_edges; + bpf_for_each_pass_insn(env, i) { + aux[i].needs_guard |= aux[i].loop && aux[i].loop->this_pass.needs_guard; + aux[i].no_guard_edges |= aux[i].loop && aux[i].loop->this_pass.no_guard_edges; } } @@ -434,7 +435,7 @@ void bpf_flow_drop_guards(struct bpf_verifier_env *env) struct bpf_insn_aux_data *aux = env->insn_aux_data; int i, h; - for (i = 0; i < env->prog->len; i++) { + bpf_for_each_pass_insn(env, i) { h = bpf_loop_at_index(env, i); if (h >= 0) aux[i].loop_guard &= aux[h].needs_guard | @@ -534,11 +535,11 @@ void bpf_flow_new_main(struct bpf_verifier_env *env) env->flow->start_cnt = 0; memset(&env->per_pass, 0, offsetof(struct bpf_env_per_pass, seen_direct_write)); - for (i = 0; i < env->prog->len; i++, aux++) { - aux->pessimized = false; - flow_reset_loop(env, aux->loop); - flow_reset_loop(env, aux->callback_loop); - flow_reset_loop(env, aux->recursion_loop); + bpf_for_each_pass_insn(env, i) { + aux[i].pessimized = false; + flow_reset_loop(env, aux[i].loop); + flow_reset_loop(env, aux[i].callback_loop); + flow_reset_loop(env, aux[i].recursion_loop); } } diff --git a/kernel/bpf/pads.c b/kernel/bpf/pads.c index befe9a729115..25455fd508d8 100644 --- a/kernel/bpf/pads.c +++ b/kernel/bpf/pads.c @@ -535,23 +535,26 @@ void bpf_pads_keep(struct bpf_verifier_env *env, struct bpf_pads_moved *kept) struct bpf_insn_aux_data *aux = env->insn_aux_data; int i; - for (i = 0; i < env->prog->len && !env->no_mem; i++, aux++, kept++) - env->no_mem = copy_plans(env, &kept->plans, aux->pad_plans) || - copy_notes(env, &kept->note, aux->pad_note); + bpf_for_each_pass_insn(env, i) { + env->no_mem = env->no_mem || copy_plans(env, &kept->plans, aux[i].pad_plans) || + copy_notes(env, &kept->note, aux[i].pad_note); + kept++; + } } /* * The records and the notes are results of a pass, so they move with the snapshot of the pass, see - * copy_snapshot(): to @moved, which has an element per insn, if @save, and back otherwise. What was - * at the destination is freed. + * copy_snapshot(): to @moved, which has an element per insn of the pass, if @save, and back + * otherwise. What was at the destination is freed. */ void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, bool save) { - struct bpf_insn_aux_data *aux = env->insn_aux_data; + struct bpf_insn_aux_data *aux; int i; env->pad_skip_gen++; - for (i = 0; i < env->prog->len; i++, aux++, moved++) { + bpf_for_each_pass_insn(env, i) { + aux = &env->insn_aux_data[i]; if (save) { env->mem -= free_plans(moved->plans); env->mem -= bpf_pad_note_free(moved->note); @@ -567,6 +570,7 @@ void bpf_pads_move(struct bpf_verifier_env *env, struct bpf_pads_moved *moved, b moved->plans = NULL; moved->note = NULL; } + moved++; } } @@ -2542,10 +2546,12 @@ int bpf_pads_must_unwind(struct bpf_verifier_env *env) do { changed = false; - err = bpf_add_cost_and_check(env, env->prog->len, BPF_COST_SCAN); + err = bpf_add_cost_and_check(env, env->pass_len, BPF_COST_SCAN); if (err) return err; for (k = 0; k < env->subprog_cnt; k++) { + if (!env->calls.marks[env->subprog_topo_order[k]]) + continue; sub = &env->subprog_info[env->subprog_topo_order[k]]; must = sub->must_unwind; must_cancel = sub->must_cancel; @@ -2612,10 +2618,12 @@ static int sites_of_subprogs(struct bpf_verifier_env *env) } do { changed = false; - err = bpf_add_cost_and_check(env, env->prog->len, BPF_COST_SCAN); + err = bpf_add_cost_and_check(env, env->pass_len, BPF_COST_SCAN); if (err) return err; for (k = 0; k < env->subprog_cnt; k++) { + if (!env->calls.marks[env->subprog_topo_order[k]]) + continue; sub = &env->subprog_info[env->subprog_topo_order[k]]; kinds = sub->left_at; for (i = sub->start; i < sub[1].start; i++) { @@ -2982,6 +2990,8 @@ static int settle_slots(struct bpf_verifier_env *env) env->subprog_info[s].pend_off = 0; } for (s = 0; s < env->subprog_cnt; s++) { + if (!env->calls.marks[s]) + continue; sub = &env->subprog_info[s]; all_ptrs = !acquires_stack_obj(env, sub); do { @@ -3471,7 +3481,7 @@ void bpf_pads_mark_used(struct bpf_verifier_env *env) struct bpf_pad_plan *plan; int i; - for (i = 0; i < env->prog->len; i++) + bpf_for_each_pass_insn(env, i) for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) plan->used = bpf_site_is_used(env, i, plan); } @@ -3493,7 +3503,7 @@ int bpf_pads_settle(struct bpf_verifier_env *env) if (err) return err; /* sort the releases first: hidden slots are inserted into that order */ - for (i = 0; i < env->prog->len; i++) { + bpf_for_each_pass_insn(env, i) { for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) { err = order_ents(env, plan); if (err) @@ -3503,7 +3513,7 @@ int bpf_pads_settle(struct bpf_verifier_env *env) err = settle_slots(env); if (err) return err; - for (i = 0; i < env->prog->len; i++) { + bpf_for_each_pass_insn(env, i) { if (env->insn_aux_data[i].pad_plans && bpf_must_abort()) return -EINTR; for (plan = env->insn_aux_data[i].pad_plans; plan; plan = plan->next) { diff --git a/kernel/bpf/precision.c b/kernel/bpf/precision.c index 725de358f5ca..a5cc813817c4 100644 --- a/kernel/bpf/precision.c +++ b/kernel/bpf/precision.c @@ -720,11 +720,11 @@ void bpf_find_unproven_after_pass(struct bpf_verifier_env *env) for (i = 0; i < env->subprog_cnt; i++) note_unproven_after_pass(env, env->subprog_info[i].deepest_insn); - for (i = 0; i < env->prog->len; i++, insn++, aux++) { - if (aux->pessimized || bpf_pads_conflict(env, aux)) { + bpf_for_each_pass_insn(env, i) { + if (aux[i].pessimized || bpf_pads_conflict(env, &aux[i])) { note_unproven_after_pass(env, i); - } else if (aux->seen && (bpf_calls_function(env, i) || is_stack_arg_st(insn) || - is_stack_arg_stx(insn))) { + } else if (aux[i].seen && (bpf_calls_function(env, i) || is_stack_arg_st(&insn[i]) || + is_stack_arg_stx(&insn[i]))) { __set_bit(i, env->precision->this_pass.call_insns); suspect_loops_of_insn(env, i); } @@ -1694,7 +1694,7 @@ int bpf_partition_for_pessimized(struct bpf_verifier_env *env) /* bpf_partition_more() can fail before partition_next() makes this copy */ if (prec->data_deps) memcpy(prec->undo, prec->data_deps, 5 * prec->words * sizeof(long)); - for (i = 0; i < env->prog->len; i++) + bpf_for_each_pass_insn(env, i) if (env->insn_aux_data[i].pessimized) note_unproven_after_pass(env, i); ret = bpf_partition_more(env); @@ -1814,9 +1814,9 @@ int bpf_precision_init(struct bpf_verifier_env *env) prec->join_live_types = bpf_prog_is_offloaded(env->prog->aux); prec->budget_left = prec->budget = bpf_precision_first_budget(env); prec->first_unproven_insn = prec->loop_carried_at = U32_MAX; - for (i = 0; i < env->prog->len && !err; i++, aux++) - err = add_loop(prec, aux->loop) ?: add_loop(prec, aux->callback_loop) ?: - add_loop(prec, aux->recursion_loop); + bpf_for_each_pass_insn(env, i) + err = err ?: add_loop(prec, aux[i].loop) ?: add_loop(prec, aux[i].callback_loop) ?: + add_loop(prec, aux[i].recursion_loop); return err; } diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index cc5a39abe9a7..461a0db06b87 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -3779,7 +3779,7 @@ static int sort_subprogs_topo(struct bpf_verifier_env *env) verbose(env, "topo_order[%d] = %s\n", i, bpf_subprog_name(env, env->subprog_topo_order[i])); out: - bpf_call_graph_free(&cg); + env->calls = cg; return ret; } @@ -24836,7 +24836,7 @@ struct bpf_snapshot { struct bpf_env_per_pass env; struct bpf_prog_aux aux; struct bpf_insn_fixups *insns; - struct bpf_pads_moved *pads; /* per insn, see bpf_pads_move() */ + struct bpf_pads_moved *pads; /* both: per insn of the pass */ struct bpf_subprog_info *subprogs; struct bpf_func_info_aux *funcs; size_t bytes; @@ -24847,6 +24847,7 @@ static void copy_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna { bool has_callchain_buf = env->prog->has_callchain_buf; struct bpf_prog_aux *aux = env->prog->aux; + struct bpf_insn_fixups *insns = snapshot->insns; struct bpf_subprog_info *sub; int i; @@ -24867,8 +24868,8 @@ static void copy_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna COPY_AUX(tail_call_reachable); COPY_AUX(jits_use_priv_stack); COPY_AUX(kprobe_write_ctx); - for (i = 0; i < env->prog->len; i++) - COPY(snapshot->insns[i], env->insn_aux_data[i].fixups); + bpf_for_each_pass_insn(env, i) + COPY(*insns++, env->insn_aux_data[i].fixups); bpf_pads_move(env, snapshot->pads, save); for (i = 0, sub = env->subprog_info; i < env->subprog_cnt; i++, sub++) { /* the sum over all passes */ @@ -24880,14 +24881,14 @@ static void copy_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna COPY(snapshot->funcs[i], aux->func_info_aux[i]); #undef COPY_AUX #undef COPY - bpf_add_cost(env, env->prog->len, BPF_COST_XREG); + bpf_add_cost(env, max(env->pass_len, env->subprog_cnt), BPF_COST_XREG); } static struct bpf_snapshot *save_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *snapshot) { - size_t insns = array_size(env->prog->len, sizeof(*snapshot->insns)); - size_t pads = array_size(env->prog->len, sizeof(*snapshot->pads)); + size_t insns = array_size(env->pass_len, sizeof(*snapshot->insns)); + size_t pads = array_size(env->pass_len, sizeof(*snapshot->pads)); size_t subprogs = env->subprog_cnt * sizeof(*snapshot->subprogs); if (!snapshot) { @@ -24912,7 +24913,7 @@ static void free_snapshot(struct bpf_verifier_env *env, struct bpf_snapshot *sna { if (!snapshot) return; - env->mem -= snapshot->bytes + bpf_pads_free_moved(snapshot->pads, env->prog->len); + env->mem -= snapshot->bytes + bpf_pads_free_moved(snapshot->pads, env->pass_len); kvfree(snapshot); } @@ -24998,7 +24999,7 @@ static int settle_late_unwinds(struct bpf_verifier_env *env, const struct bpf_sn sub->can_unwind = initial->subprogs[s].can_unwind; sub->can_cancel = initial->subprogs[s].can_cancel; /* The walk did not finish: keep what the states of the pass found. */ - if (err) + if (err || !env->calls.marks[s]) continue; late |= bpf_subprog_is_global(env, s) && ((unwinds && !initial->subprogs[s].did_unwind) || @@ -25528,7 +25529,7 @@ static int check_proven_mains(struct bpf_verifier_env *env) verbose(env, "programs with bpf_throw return different values when cancelled\n"); return -EINVAL; } - for (i = 0; i < env->prog->len; i++) { + bpf_for_each_pass_insn(env, i) { if (aux[i].needs_guard && aux[i].no_guard_edges) { verbose(env, "loop at insn %d needs may_goto for some programs only\n", i); return -EINVAL; @@ -25551,6 +25552,26 @@ static int check_proven_mains(struct bpf_verifier_env *env) return 0; } +/* + * The passes of a main prog need only the functions that it gets to. The graph has no edges of + * callx and of dead code, so add what they can get to. The last main prog looks at all functions: + * what is settled after it is for all. + */ +static void mark_pass_subprogs(struct bpf_verifier_env *env) +{ + struct bpf_subprog_info *sub = env->subprog_info; + bool last = env->pass_main + 1 == env->main_cnt; + u16 *marks = env->calls.marks; + int s; + + for (s = 0; s < env->subprog_cnt; s++) + marks[s] = last || s == env->mains[env->pass_main].subprog || + sub[s].addr_taken || sub[s].recursive; + bpf_call_graph_mark_callees(&env->calls, BIT(0)); + for (s = 0, env->pass_len = 0; s < env->subprog_cnt; s++, sub++) + env->pass_len += marks[s] ? sub[1].start - sub->start : 0; +} + /* Each main prog has passes of its own. They start from what the progs before it wrote. */ static int do_check_mains(struct bpf_verifier_env *env) { @@ -25561,6 +25582,7 @@ static int do_check_mains(struct bpf_verifier_env *env) env->cost = cost; env->max_cost = max_cost; env->max_mem = max_mem; + mark_pass_subprogs(env); bpf_flow_new_main(env); env->handed_back = false; err = do_check_all(env); @@ -27975,6 +27997,7 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, kvfree(env->idoms); kvfree(env->subprog_info); kvfree(env->subprog_topo_order); + bpf_call_graph_free(&env->calls); kvfree(env->mains); kvfree(env); return ret; -- 2.55.0