Skip to content

Commit 0c35ca2

Browse files
Yonghong SongAlexei Starovoitov
authored andcommitted
bpf: Remove 'may_goto 0' instruction in opt_remove_nops()
Since 'may_goto 0' insns are actually no-op, let us remove them. Otherwise, verifier will generate code like /* r10 - 8 stores the implicit loop count */ r11 = *(u64 *)(r10 -8) if r11 == 0x0 goto pc+2 r11 -= 1 *(u64 *)(r10 -8) = r11 which is the pure overhead. The following code patterns (from the previous commit) are also handled: may_goto 2 may_goto 1 may_goto 0 With this commit, the above three 'may_goto' insns are all eliminated. Signed-off-by: Yonghong Song <yonghong.song@linux.dev> Link: https://lore.kernel.org/r/20250118192029.2124584-1-yonghong.song@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent aefaa43 commit 0c35ca2

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

kernel/bpf/verifier.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20184,23 +20184,28 @@ static int opt_remove_dead_code(struct bpf_verifier_env *env)
2018420184
}
2018520185

2018620186
static const struct bpf_insn NOP = BPF_JMP_IMM(BPF_JA, 0, 0, 0);
20187+
static const struct bpf_insn MAY_GOTO_0 = BPF_RAW_INSN(BPF_JMP | BPF_JCOND, 0, 0, 0, 0);
2018720188

2018820189
static int opt_remove_nops(struct bpf_verifier_env *env)
2018920190
{
20190-
const struct bpf_insn ja = NOP;
2019120191
struct bpf_insn *insn = env->prog->insnsi;
2019220192
int insn_cnt = env->prog->len;
20193+
bool is_may_goto_0, is_ja;
2019320194
int i, err;
2019420195

2019520196
for (i = 0; i < insn_cnt; i++) {
20196-
if (memcmp(&insn[i], &ja, sizeof(ja)))
20197+
is_may_goto_0 = !memcmp(&insn[i], &MAY_GOTO_0, sizeof(MAY_GOTO_0));
20198+
is_ja = !memcmp(&insn[i], &NOP, sizeof(NOP));
20199+
20200+
if (!is_may_goto_0 && !is_ja)
2019720201
continue;
2019820202

2019920203
err = verifier_remove_insns(env, i, 1);
2020020204
if (err)
2020120205
return err;
2020220206
insn_cnt--;
20203-
i--;
20207+
/* Go back one insn to catch may_goto +1; may_goto +0 sequence */
20208+
i -= (is_may_goto_0 && i > 0) ? 2 : 1;
2020420209
}
2020520210

2020620211
return 0;

0 commit comments

Comments
 (0)