Skip to content

Commit

Permalink
bpf: don't emit mov A,A on return
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Daniel Borkmann authored and David S. Miller committed Feb 22, 2016
1 parent 2f72959 commit 6205b9c
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions net/core/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,12 +530,14 @@ static int bpf_convert_filter(struct sock_filter *prog, int len,
*insn = BPF_MOV64_REG(BPF_REG_A, BPF_REG_TMP);
break;

/* RET_K, RET_A are remaped into 2 insns. */
/* RET_K is remaped into 2 insns. RET_A case doesn't need an
* extra mov as BPF_REG_0 is already mapped into BPF_REG_A.
*/
case BPF_RET | BPF_A:
case BPF_RET | BPF_K:
*insn++ = BPF_MOV32_RAW(BPF_RVAL(fp->code) == BPF_K ?
BPF_K : BPF_X, BPF_REG_0,
BPF_REG_A, fp->k);
if (BPF_RVAL(fp->code) == BPF_K)
*insn++ = BPF_MOV32_RAW(BPF_K, BPF_REG_0,
0, fp->k);
*insn = BPF_EXIT_INSN();
break;

Expand Down

0 comments on commit 6205b9c

Please sign in to comment.