Skip to contentSkip to conversation

arm64 BPF JIT updates

PATCH In review Zi Shen Lim 2 participants 6 messages Jun 9, 2016 – Jun 11, 2016

Subsystems (6)
Networking Network drivers BPF arm64 +2 more subsystems
lore
Share & tools
lore
#0 lore raw

v3 cover arm64 BPF JIT updates

Updates for arm64 eBPF JIT.
The main addition here is implementation of bpf_tail_call.

#1: Fix missing header inclusion in linux/bpf.h.
#2: Add bpf_tail_call for arm64.
#3,4: Optimizations to reduce instruction count for jitted code.

Changes since v2:
- None. Resubmit per David Miller.

Changes since v1:
- Added patch #1 to address build error due to missing header inclusion in linux/bpf.h. (Thanks to suggestion and ack by Daniel Borkmann) Ordered it ahead of bpf_tail_call patch #2 so build error is not triggered.

Zi Shen Lim (4):
bpf: fix missing header inclusion
arm64: bpf: implement bpf_tail_call() helper
arm64: bpf: optimize JMP_CALL
arm64: bpf: optimize LD_ABS, LD_IND

 arch/arm64/net/bpf_jit.h      |   3 +-
 arch/arm64/net/bpf_jit_comp.c | 111 ++++++++++++++++++++++++++++++++++++------
 include/linux/bpf.h           |   1 +
 3 files changed, 99 insertions(+), 16 deletions(-)
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/4 bpf: fix missing header inclusion

Commit 0fc174dea545 ("ebpf: make internal bpf API independent of CONFIG_BPF_SYSCALL ifdefs") introduced usage of ERR_PTR() in bpf_prog_get(), however did not include linux/err.h.

Without this patch, when compiling arm64 BPF without CONFIG_BPF_SYSCALL:
...
In file included from arch/arm64/net/bpf_jit_comp.c:21:0:
include/linux/bpf.h: In function 'bpf_prog_get':
include/linux/bpf.h:235:9: error: implicit declaration of function 'ERR_PTR' [-Werror=implicit-function-declaration]

  return ERR_PTR(-EOPNOTSUPP);
         ^
include/linux/bpf.h:235:9: warning: return makes pointer from integer without a cast [-Wint-conversion]
In file included from include/linux/rwsem.h:17:0,
                 from include/linux/mm_types.h:10,
                 from include/linux/sched.h:27,
                 from arch/arm64/include/asm/compat.h:25,
                 from arch/arm64/include/asm/stat.h:23,
                 from include/linux/stat.h:5,
                 from include/linux/compat.h:12,
                 from include/linux/filter.h:10,
                 from arch/arm64/net/bpf_jit_comp.c:22:
include/linux/err.h: At top level:
include/linux/err.h:23:35: error: conflicting types for 'ERR_PTR'
 static inline void * __must_check ERR_PTR(long error)
                                   ^
In file included from arch/arm64/net/bpf_jit_comp.c:21:0:
include/linux/bpf.h:235:9: note: previous implicit declaration of 'ERR_PTR' was here
  return ERR_PTR(-EOPNOTSUPP);
         ^

...

Fixes 0fc174dea545 ("ebpf: make internal bpf API independent of CONFIG_BPF_SYSCALL ifdefs")
---
 include/linux/bpf.h | 1 +
 1 file changed, 1 insertion(+)
include/linux/bpf.h +1 -0
index 8ee27b8..1bcae82 100644
@@ -11,6 +11,7 @@
 #include <linux/workqueue.h>
 #include <linux/file.h>
 #include <linux/percpu.h>
+#include <linux/err.h>
 
 struct bpf_map;
 
Signature · 1 line
1.9.1
#2 lore raw

PATCH v3 2/4 arm64: bpf: implement bpf_tail_call() helper

In reply to Zi Shen Lim

Add support for JMP_CALL_X (tail call) introduced by commit 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs").

bpf_tail_call() arguments:
ctx - context pointer passed to next program
array - pointer to map which type is BPF_MAP_TYPE_PROG_ARRAY index - index inside array that selects specific program to run

In this implementation arm64 JIT jumps into callee program after prologue, so callee program reuses the same stack. For tail_call_cnt, we use the callee-saved R26 (which was already saved/restored but previously unused by JIT).

With this patch a tail call generates the following code on arm64:

if (index >= array->map.max_entries)

      goto out;
  34:   mov     x10, #0x10                      // #16
  38:   ldr     w10, [x1,x10]
  3c:   cmp     w2, w10
  40:   b.ge    0x0000000000000074

if (tail_call_cnt > MAX_TAIL_CALL_CNT)

      goto out;

tail_call_cnt++;

  44:   mov     x10, #0x20                      // #32
  48:   cmp     x26, x10
  4c:   b.gt    0x0000000000000074
  50:   add     x26, x26, #0x1
  prog = array->ptrs[index];
  if (prog == NULL)
      goto out;
  54:   mov     x10, #0x68                      // #104
  58:   ldr     x10, [x1,x10]
  5c:   ldr     x11, [x10,x2]
  60:   cbz     x11, 0x0000000000000074
  goto *(prog->bpf_func + prologue_size);
  64:   mov     x10, #0x20                      // #32
  68:   ldr     x10, [x11,x10]
  6c:   add     x10, x10, #0x20
  70:   br      x10

74:

---
 arch/arm64/net/bpf_jit.h      |   3 +-
 arch/arm64/net/bpf_jit_comp.c | 105 ++++++++++++++++++++++++++++++++++++++----
 2 files changed, 98 insertions(+), 10 deletions(-)
arch/arm64/net/bpf_jit.h +2 -1
index aee5637..7c16e54 100644
@@ -1,7 +1,7 @@
 /*
  * BPF JIT compiler for ARM64
  *
- * Copyright (C) 2014-2015 Zi Shen Lim <[email protected]>
+ * Copyright (C) 2014-2016 Zi Shen Lim <[email protected]>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -55,6 +55,7 @@
 #define A64_BL(imm26) A64_BRANCH((imm26) << 2, LINK)
 
 /* Unconditional branch (register) */
+#define A64_BR(Rn)  aarch64_insn_gen_branch_reg(Rn, AARCH64_INSN_BRANCH_NOLINK)
 #define A64_BLR(Rn) aarch64_insn_gen_branch_reg(Rn, AARCH64_INSN_BRANCH_LINK)
 #define A64_RET(Rn) aarch64_insn_gen_branch_reg(Rn, AARCH64_INSN_BRANCH_RETURN)
 
arch/arm64/net/bpf_jit_comp.c +96 -9

166 lines not on this page · raw message

Signature · 1 line
1.9.1
#3 lore raw

PATCH v3 3/4 arm64: bpf: optimize JMP_CALL

In reply to Zi Shen Lim

Remove superfluous stack frame, saving us 3 instructions for every JMP_CALL.

---
 arch/arm64/net/bpf_jit_comp.c | 3 ---
 1 file changed, 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +0 -3
index 51abc97..7ae304e 100644
@@ -578,11 +578,8 @@ emit_cond_jmp:
 		const u64 func = (u64)__bpf_call_base + imm;
 
 		emit_a64_mov_i64(tmp, func, ctx);
-		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
-		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
 		emit(A64_BLR(tmp), ctx);
 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
-		emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
 		break;
 	}
 	/* tail call */
Signature · 1 line
1.9.1
#4 lore raw

PATCH v3 4/4 arm64: bpf: optimize LD_ABS, LD_IND

In reply to Zi Shen Lim

Remove superfluous stack frame, saving us 3 instructions for every LD_ABS or LD_IND.

---
 arch/arm64/net/bpf_jit_comp.c | 3 ---
 1 file changed, 3 deletions(-)
arch/arm64/net/bpf_jit_comp.c +0 -3
index 7ae304e..b2fc97a 100644
@@ -731,11 +731,8 @@ emit_cond_jmp:
 		emit_a64_mov_i64(r3, size, ctx);
 		emit(A64_SUB_I(1, r4, fp, STACK_SIZE), ctx);
 		emit_a64_mov_i64(r5, (unsigned long)bpf_load_pointer, ctx);
-		emit(A64_PUSH(A64_FP, A64_LR, A64_SP), ctx);
-		emit(A64_MOV(1, A64_FP, A64_SP), ctx);
 		emit(A64_BLR(r5), ctx);
 		emit(A64_MOV(1, r0, A64_R(0)), ctx);
-		emit(A64_POP(A64_FP, A64_LR, A64_SP), ctx);
 
 		jmp_offset = epilogue_offset(ctx);
 		check_imm19(jmp_offset);
Signature · 1 line
1.9.1