Skip to content

Commit

Permalink
bpf: verifier: remove unconditional branches by 0
Browse files Browse the repository at this point in the history
Unconditional branches by 0 instructions are basically noops
but they can result from earlier optimizations, e.g. a conditional
jumps which would never be taken or a conditional jump around
dead code.

Remove those branches.

v0.2:
 - s/opt_remove_dead_branches/opt_remove_nops/ (Jiong).

Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Reviewed-by: Jiong Wang <jiong.wang@netronome.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Jakub Kicinski authored and Alexei Starovoitov committed Jan 24, 2019
1 parent 52875a0 commit a1b14ab
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -6660,6 +6660,27 @@ static int opt_remove_dead_code(struct bpf_verifier_env *env)
return 0;
}

static int opt_remove_nops(struct bpf_verifier_env *env)
{
const struct bpf_insn ja = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
struct bpf_insn *insn = env->prog->insnsi;
int insn_cnt = env->prog->len;
int i, err;

for (i = 0; i < insn_cnt; i++) {
if (memcmp(&insn[i], &ja, sizeof(ja)))
continue;

err = verifier_remove_insns(env, i, 1);
if (err)
return err;
insn_cnt--;
i--;
}

return 0;
}

/* convert load instructions that access fields of a context type into a
* sequence of instructions that access fields of the underlying structure:
* struct __sk_buff -> struct sk_buff
Expand Down Expand Up @@ -7452,6 +7473,8 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
opt_hard_wire_dead_code_branches(env);
if (ret == 0)
ret = opt_remove_dead_code(env);
if (ret == 0)
ret = opt_remove_nops(env);
} else {
if (ret == 0)
sanitize_dead_code(env);
Expand Down

0 comments on commit a1b14ab

Please sign in to comment.