bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates
authorLuke Nelson <lukenels@cs.washington.edu>
Fri, 8 May 2020 18:15:46 +0000 (11:15 -0700)
committerWill Deacon <will@kernel.org>
Mon, 11 May 2020 11:21:39 +0000 (12:21 +0100)
commitfd868f14818901821699988fdac680ebd80cd360
tree7cbceb89d9585da12aac300019a938310e42a0aa
parentfd49591cb49b72abd1b665222a635ccb17df7923
bpf, arm64: Optimize ADD,SUB,JMP BPF_K using arm64 add/sub immediates

The current code for BPF_{ADD,SUB} BPF_K loads the BPF immediate to a
temporary register before performing the addition/subtraction. Similarly,
BPF_JMP BPF_K cases load the immediate to a temporary register before
comparison.

This patch introduces optimizations that use arm64 immediate add, sub,
cmn, or cmp instructions when the BPF immediate fits. If the immediate
does not fit, it falls back to using a temporary register.

Example of generated code for BPF_ALU64_IMM(BPF_ADD, R0, 2):

without optimization:

  24: mov x10, #0x2
  28: add x7, x7, x10

with optimization:

  24: add x7, x7, #0x2

The code could use A64_{ADD,SUB}_I directly and check if it returns
AARCH64_BREAK_FAULT, similar to how logical immediates are handled.
However, aarch64_insn_gen_add_sub_imm from insn.c prints error messages
when the immediate does not fit, and it's simpler to check if the
immediate fits ahead of time.

Co-developed-by: Xi Wang <xi.wang@gmail.com>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
Signed-off-by: Luke Nelson <luke.r.nels@gmail.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20200508181547.24783-4-luke.r.nels@gmail.com
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/net/bpf_jit.h
arch/arm64/net/bpf_jit_comp.c