Skip to contentSkip to conversation

arm64: eBPF JIT compiler

RFC PATCH Superseded Zi Shen Lim 3 participants 5 messages Jul 8, 2014 – Jul 10, 2014

Subsystems (6)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 arm64: eBPF JIT compiler

The JIT compiler emits A64 instructions. It supports eBPF only. Legacy BPF is supported thanks to conversion by BPF core.

JIT is enabled in the same way as for other architectures:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or for additional compiler output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

See Documentation/networking/filter.txt for more information.

The implementation passes all 57 tests in lib/test_bpf.c on ARMv8 Foundation Model :)

---
RFCv1->RFCv2:

Addressed review comments from Alexei:
- use core-$(CONFIG_NET)
- use GENMASK
- lower-case function names in header file
- drop LD_ABS+DW and LD_IND+DW, which do not exist in eBPF yet
- use pr_xxx_once() to prevent spamming logs
- clarify 16B stack alignment requirement
- drop usage of EMIT macro which was saving just one argument, turns out having additional argument wasn't too much of an eyesore

Also, per discussion with Alexei, and additional suggestion from Daniel:
- moved load_pointer() from net/core/filter.c into filter.h as bpf_load_pointer()
which is done as a separate preparatory patch. [1]

[1] http://patchwork.ozlabs.org/patch/366906/

NOTES:

* This patch applies on top of net-next @ bd4578bc84a8 ("drivers/net/hyperv/netvsc.c: remove unnecessary null test before kfree").

* Patch [1] above must be applied first, followed by this patch.

* bpf_jit_comp.c is checkpatch clean.

* Checkpatch warns about long lines for bpf_jit.h, but those lines are actually more readable as is.

* The following sparse warning is not applicable:
warning: symbol 'bpf_jit_enable' was not declared. Should it be static?

* Per discussion with Will, I'll also look into consolidating bpf_jit.h into arch/arm64/kernel/insn.{c,h}.

FUTURE WORK:

1. Implement remaining classes of eBPF instructions: ST|MEM, STX|XADD which currently do not have corresponding test cases in test_bpf.

2. Further compiler optimization, such as optimization for small immediates.

---
 Documentation/networking/filter.txt |   2 +-
 arch/arm64/Kconfig                  |   1 +
 arch/arm64/Makefile                 |   1 +
 arch/arm64/net/Makefile             |   4 +
 arch/arm64/net/bpf_jit.h            | 317 +++++++++++++++++
 arch/arm64/net/bpf_jit_comp.c       | 681 ++++++++++++++++++++++++++++++++++++
 6 files changed, 1005 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/net/Makefile
 create mode 100644 arch/arm64/net/bpf_jit.h
 create mode 100644 arch/arm64/net/bpf_jit_comp.c
Documentation/networking/filter.txt +1 -1
index ee78eba..d71e616 100644
@@ -462,7 +462,7 @@ JIT compiler
 ------------
 
 The Linux kernel has a built-in BPF JIT compiler for x86_64, SPARC, PowerPC,
-ARM and s390 and can be enabled through CONFIG_BPF_JIT. The JIT compiler is
+ARM, ARM64 and s390 and can be enabled through CONFIG_BPF_JIT. The JIT compiler is
 transparently invoked for each attached filter from user space or for internal
 kernel users if it has been previously enabled by root:
 
arch/arm64/Kconfig +1 -0
index a474de34..b0a4ff8 100644
@@ -32,6 +32,7 @@ config ARM64
 	select HAVE_ARCH_KGDB
 	select HAVE_ARCH_TRACEHOOK
 	select HAVE_C_RECORDMCOUNT
+	select HAVE_BPF_JIT
 	select HAVE_DEBUG_BUGVERBOSE
 	select HAVE_DEBUG_KMEMLEAK
 	select HAVE_DMA_API_DEBUG
arch/arm64/Makefile +1 -0
index 8185a91..9820fa7 100644
@@ -43,6 +43,7 @@ TEXT_OFFSET := 0x00080000
 export	TEXT_OFFSET GZFLAGS
 
 core-y		+= arch/arm64/kernel/ arch/arm64/mm/
+core-$(CONFIG_NET) += arch/arm64/net/
 core-$(CONFIG_KVM) += arch/arm64/kvm/
 core-$(CONFIG_XEN) += arch/arm64/xen/
 core-$(CONFIG_CRYPTO) += arch/arm64/crypto/
arch/arm64/net/Makefile new file +4 -0
new file mode 100644
index 0000000..da97633
@@ -0,0 +1,4 @@
+#
+# ARM64 networking code
+#
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
arch/arm64/net/bpf_jit.h new file +317 -0

317 lines not on this page · raw message

arch/arm64/net/bpf_jit_comp.c new file +681 -0

681 lines not on this page · raw message

Signature · 1 line
1.9.1
Acked-by on v2 1/1
#1 lore raw
Quoted 31 lines · Zi Shen Lim

The JIT compiler emits A64 instructions. It supports eBPF only. Legacy BPF is supported thanks to conversion by BPF core.

JIT is enabled in the same way as for other architectures:

echo 1 > /proc/sys/net/core/bpf_jit_enable

Or for additional compiler output:

echo 2 > /proc/sys/net/core/bpf_jit_enable

See Documentation/networking/filter.txt for more information.

The implementation passes all 57 tests in lib/test_bpf.c on ARMv8 Foundation Model :)

---
RFCv1->RFCv2:

Addressed review comments from Alexei:
- use core-$(CONFIG_NET)
- use GENMASK
- lower-case function names in header file
- drop LD_ABS+DW and LD_IND+DW, which do not exist in eBPF yet
- use pr_xxx_once() to prevent spamming logs
- clarify 16B stack alignment requirement
- drop usage of EMIT macro which was saving just one argument, turns out having additional argument wasn't too much of an eyesore

Quoted 7 lines

Also, per discussion with Alexei, and additional suggestion from Daniel:
- moved load_pointer() from net/core/filter.c into filter.h as bpf_load_pointer()
which is done as a separate preparatory patch. [1]

[1] http://patchwork.ozlabs.org/patch/366906/

Just to clarify. This patch is on top of [1]

Quoted 52 lines
arch/arm64/net/bpf_jit_comp.c new file +52 -0
+       /* R0 = ntohx(*(size *)(((struct sk_buff *)R6)->data + src + imm)) */
+       case BPF_LD | BPF_IND | BPF_W:
+       case BPF_LD | BPF_IND | BPF_H:
+       case BPF_LD | BPF_IND | BPF_B:
+       {
+               const u8 r0 = bpf2a64[BPF_REG_0]; /* r0 = return value */
+               const u8 r6 = bpf2a64[BPF_REG_6]; /* r6 = pointer to sk_buff */
+               const u8 fp = bpf2a64[BPF_REG_FP];
+               const u8 r1 = bpf2a64[BPF_REG_1]; /* r1: struct sk_buff *skb */
+               const u8 r2 = bpf2a64[BPF_REG_2]; /* r2: int k */
+               const u8 r3 = bpf2a64[BPF_REG_3]; /* r3: unsigned int size */
+               const u8 r4 = bpf2a64[BPF_REG_4]; /* r4: void *buffer */
+               const u8 r5 = bpf2a64[BPF_REG_5]; /* r5: void *(*func)(...) */
+               int size;
+
+               emit(A64_MOV(1, r1, r6), ctx);
+               emit_a64_mov_i(0, r2, imm, ctx);
+               if (BPF_MODE(code) == BPF_IND)
+                       emit(A64_ADD(0, r2, r2, src), ctx);
+               switch (BPF_SIZE(code)) {
+               case BPF_W:
+                       size = 4;
+                       break;
+               case BPF_H:
+                       size = 2;
+                       break;
+               case BPF_B:
+                       size = 1;
+                       break;
+               default:
+                       return -EINVAL;
+               }
+               emit_a64_mov_i64(r3, size, ctx);
+               emit(A64_ADD_I(1, r4, fp, MAX_BPF_STACK), 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);
+               emit(A64_CBZ(1, r0, jmp_offset), ctx);
+               emit(A64_MOV(1, r5, r0), ctx);
+               switch (BPF_SIZE(code)) {
+               case BPF_W:
+                       emit(A64_LDR32(r0, r5, A64_ZR), ctx);
+#ifndef CONFIG_CPU_BIG_ENDIAN
+                       emit(A64_REV32(0, r0, r0), ctx);
+#endif
+                       break;

arch/arm64/net/bpf_jit_comp.c line 566

Quoted 10 lines
arch/arm64/net/bpf_jit_comp.c new file +10 -0
+               case BPF_H:
+                       emit(A64_LDRH(r0, r5, A64_ZR), ctx);
+#ifndef CONFIG_CPU_BIG_ENDIAN
+                       emit(A64_REV16(0, r0, r0), ctx);
+#endif
+                       break;
+               case BPF_B:
+                       emit(A64_LDRB(r0, r5, A64_ZR), ctx);
+                       break;
+               case BPF_DW:

this case can be removed too.

Great work. Thanks.

Quoted 8 lines
arch/arm64/net/bpf_jit_comp.c new file +8 -0
+                       emit(A64_LDR64(r0, r5, A64_ZR), ctx);
+#ifndef CONFIG_CPU_BIG_ENDIAN
+                       emit(A64_REV64(r0, r0), ctx);
+#endif
+                       break;
+               }
+               break;
+       }
author
#2 lore raw
Quoted 1 line · Alexei Starovoitov

On Tue, Jul 8, 2014 at 12:06 AM, Zi Shen Lim <[email protected]> wrote:

[...]

Quoted 9 lines
Quoted 7 lines

Also, per discussion with Alexei, and additional suggestion from Daniel:
- moved load_pointer() from net/core/filter.c into filter.h as bpf_load_pointer()
which is done as a separate preparatory patch. [1]

[1] http://patchwork.ozlabs.org/patch/366906/

Just to clarify. This patch is on top of [1]

Yes, that's correct. This patch uses bpf_load_pointer() from [1].

BTW, looks like David has applied it in net-next 9f12fbe603f7 ("net: filter: move load_pointer() into filter.h").

[...]

Quoted 3 lines
Quoted 1 line

+ case BPF_DW:

this case can be removed too.

Thanks for catching this :) I've removed it.

Quoted 4 lines

Great work. Thanks.

Thanks for your time and feedback!

#3 lore raw
In reply to Zi Shen Lim
Quoted 2 lines · Zi Shen Lim

* Per discussion with Will, I'll also look into consolidating bpf_jit.h into arch/arm64/kernel/insn.{c,h}.

I'm happy to review this once you have something ready.

Quoted 4 lines

FUTURE WORK:

1. Implement remaining classes of eBPF instructions: ST|MEM, STX|XADD which currently do not have corresponding test cases in test_bpf.

I can help out with atomics, if you need it. Are the memory barrier semantics well-defined for eBPF?

Will

#4 lore raw
Quoted 7 lines · Will Deacon
Quoted 3 lines · Zi Shen Lim

1. Implement remaining classes of eBPF instructions: ST|MEM, STX|XADD which currently do not have corresponding test cases in test_bpf.

I can help out with atomics, if you need it. Are the memory barrier semantics well-defined for eBPF?

There are no barrier or cmpxchg instructions in eBPF, since such use case doesn't exist yet. There is only xadd that is equivalent to atomic_add which can be JITed as exclusive load/store on arm64.