bpf_opt_remove_dead_code() removes every range of instructions that the verifier never reached with verifier_remove_insns(). Each removal moves the tail of the program and of its aux data and walks all of it, so a program with many unreached ranges pays O(prog->len) for each of them. Queue the removal of each unreached instruction with bpf_patch_list_del() and commit them once. Signed-off-by: Qiliang Yuan --- kernel/bpf/fixups.c | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 1a1a374a076b1..95f4fe5d607e6 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -1018,21 +1018,14 @@ int bpf_opt_remove_dead_code(struct bpf_verifier_env *env) keep_funcs_with_addr_taken(env); for (i = 0; i < insn_cnt; i++) { - int j; - - j = 0; - while (i + j < insn_cnt && !aux_data[i + j].seen) - j++; - if (!j) + if (aux_data[i].seen) continue; - - err = verifier_remove_insns(env, i, j); + err = bpf_patch_list_del(env, i); if (err) return err; - insn_cnt = env->prog->len; } - return 0; + return bpf_patch_list_commit(env); } int bpf_opt_remove_nops(struct bpf_verifier_env *env) -- 2.43.0