Skip to content

Commit 6205b9c

Browse files
borkmanndavem330
authored andcommitted
bpf: don't emit mov A,A on return
While debugging with bpf_jit_disasm I noticed emissions of 'mov %eax,%eax', and found that this comes from BPF_RET | BPF_A translations from classic BPF. Emitting this is unnecessary as BPF_REG_A is mapped into BPF_REG_0 already, therefore only emit a mov when immediates are used as return value. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2f72959 commit 6205b9c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

net/core/filter.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,14 @@ static int bpf_convert_filter(struct sock_filter *prog, int len,
530530
*insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
531531
break;
532532

533-
/* RET_K, RET_A are remaped into 2 insns. */
533+
/* RET_K is remaped into 2 insns. RET_A case doesn't need an
534+
* extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
535+
*/
534536
case BPF_RET | BPF_A:
535537
case BPF_RET | BPF_K:
536-
*insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
537-
BPF_K : BPF_X, BPF_REG_0,
538-
BPF_REG_A, fp->k);
538+
if (BPF_RVAL(fp->code) == BPF_K)
539+
*insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
540+
0, fp->k);
539541
*insn = BPF_EXIT_INSN();
540542
break;
541543

0 commit comments

Comments
 (0)