arm: eBPF JIT compiler
Subsystems (5)
PATCH v4 1/1 arm: eBPF JIT compiler
The JIT compiler emits ARM 32 bit instructions. Currently, It supports eBPF only. Classic BPF is supported because of the conversion by BPF core.
This patch is essentially changing the current implementation of JIT compiler of Berkeley Packet Filter from classic to internal with almost all instructions from eBPF ISA supported except the following
BPF_ALU64 | BPF_DIV | BPF_K BPF_ALU64 | BPF_DIV | BPF_X BPF_ALU64 | BPF_MOD | BPF_K BPF_ALU64 | BPF_MOD | BPF_X BPF_STX | BPF_XADD | BPF_W BPF_STX | BPF_XADD | BPF_DW
Implementation is using scratch space to emulate 64 bit eBPF ISA on 32 bit ARM because of deficiency of general purpose registers on ARM. Currently, only LITTLE ENDIAN machines are supported in this eBPF JIT Compiler.
Tested on ARMv7 with QEMU by me (Shubham Bansal).
Testing results on ARMv7:
1) test_bpf: Summary: 341 PASSED, 0 FAILED, [312/333 JIT'ed]
2) test_tag: OK (40945 tests)
3) test_progs: Summary: 30 PASSED, 0 FAILED
4) test_lpm: OK
5) test_lru_map: OK
Above tests are all done with following flags enabled discreatly.
1) bpf_jit_enable=1
a) CONFIG_FRAME_POINTER enabled
b) CONFIG_FRAME_POINTER disabled
2) bpf_jit_enable=1 and bpf_jit_harden=2
a) CONFIG_FRAME_POINTER enabled
b) CONFIG_FRAME_POINTER disabled
See Documentation/networking/filter.txt for more information.
---
v2
- Solved many bugs for ARMv5 and ARMv6 support.
v3
- Implemented BPF_JMP | BPF_CALL.
- Changed tail call opcode to BPF_JMP | BPF_TAIL_CALL.
- Filled prog->jited_len value.
- Implemented BPF_JLT, BPF_JLE, BPF_JSLT, BPF_JSLE.
- Solved many bugs noticed by bpf selftests.
v4
- Proper ENDIANNESS handling in kconfig.
- Changed alignment from 16 bytes to 4 bytes.
- Setup ctx->stack_size for optimization.
- Removed unnecessary bpf_jit_free from the JIT compiler.
--- arch/arm/Kconfig | 2 +- arch/arm/net/bpf_jit_32.c | 2448 ++++++++++++++++++++++++++++++--------------- arch/arm/net/bpf_jit_32.h | 108 +- 3 files changed, 1747 insertions(+), 811 deletions(-)
arch/arm/Kconfig +1 -1
@@ -50,7 +50,7 @@ config ARM select HAVE_ARCH_SECCOMP_FILTER if (AEABI && !OABI_COMPAT) select HAVE_ARCH_TRACEHOOK select HAVE_ARM_SMCCC if CPU_V7 - select HAVE_CBPF_JIT + select HAVE_EBPF_JIT if !CPU_ENDIAN_BE32 select HAVE_CC_STACKPROTECTOR select HAVE_CONTEXT_TRACKING select HAVE_C_RECORDMCOUNT
arch/arm/net/bpf_jit_32.c +1663 -785
2,685 lines not on this page · raw message
arch/arm/net/bpf_jit_32.h +83 -25
219 lines not on this page · raw message
Signature · 1 line
2.7.4
Russell, David, Alexei, Daniel and Kees. Please check this patch and lets finish it.
Thanks.
Daniel Borkmann
maintainerOn 08/22/2017 08:36 AM, Shubham Bansal wrote:
[...]
arch/arm/net/bpf_jit_32.c static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx) · line 936
Quoted 2 lines
arch/arm/net/bpf_jit_32.c +2
-0+ +static int out_offset =-1 ;/* initialized on the first pass of build_body() */
Hm, why is this a global var actually? There can be multiple parallel calls to bpf_int_jit_compile(), we don't take a global lock on this. Unless I'm missing something this should really reside in jit_ctx, no?
Given this is on emit_bpf_tail_call(), did you get tail calls working the way I suggested to test?
Quoted 2 lines
+static int
emit_bpf_tail_call(struct jit_ctx *ctx)
{
[...]
Quoted 3 lines
arch/arm/net/bpf_jit_32.c +3
-0+const int idx0 = ctx->idx; +#define cur_offset (ctx->idx - idx0) +#define jmp_offset (out_offset - (cur_offset))
[...]
Quoted 13 lines
arch/arm/net/bpf_jit_32.c +12
-0+ +/* out: */ +if (out_offset ==-1 ) + out_offset = cur_offset; +if (cur_offset != out_offset) { +pr_err_once ("tail_call out_offset = %d, expected %d!\n" , + cur_offset, out_offset); +return -1 ; + } +return 0 ; +#undef cur_offset +#undef jmp_offset }
Daniel Borkmann
maintainerQuoted 9 lines · Daniel Borkmann
On 08/22/2017 08:36 AM, Shubham Bansal wrote:
[...]arch/arm/net/bpf_jit_32.c static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx) · line 936
Quoted 2 lines
arch/arm/net/bpf_jit_32.c +2
-0+ +static int out_offset =-1 ;/* initialized on the first pass of build_body() */ Hm, why is this a global var actually? There can be multiple parallel calls to
bpf_int_jit_compile(), we don't take a global lock on this. Unless I'm missing something this should really reside injit_ctx, no?
Hm, okay, it's for generating the out jmp offsets in tail call emission which are supposed to always be the same relative offsets; should be fine then.
Quoted 23 lines
Given this is on
emit_bpf_tail_call(), did you get tail calls working the way I suggested to test?Quoted 2 lines
+static int
emit_bpf_tail_call(struct jit_ctx *ctx)
{[...]
Quoted 3 lines
arch/arm/net/bpf_jit_32.c +3
-0+const int idx0 = ctx->idx; +#define cur_offset (ctx->idx - idx0) +#define jmp_offset (out_offset - (cur_offset))[...]
Quoted 13 lines
arch/arm/net/bpf_jit_32.c +12
-0+ +/* out: */ +if (out_offset ==-1 ) + out_offset = cur_offset; +if (cur_offset != out_offset) { +pr_err_once ("tail_call out_offset = %d, expected %d!\n" , + cur_offset, out_offset); +return -1 ; + } +return 0 ; +#undef cur_offset +#undef jmp_offset }
David Miller
maintainerYou posted this 4 times. :-(
I hope I applied the right one.
Go check net-next and please send me any necessary fix up patches.
Thanks.
Hi David,
Quoted 4 lines · David Miller
You posted this 4 times. :-(
I hope I applied the right one.
All 4 of these are the same patch. I mistakenly sent it 4 times. My apologies for that.
Quoted 2 lines
Go check net-next and please send me any necessary fix up patches.
I just checked. Its the correct patch.
Thanks a lot David. :)