arm: eBPF JIT compiler
Subsystems (5)
PATCH v3 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.
This patch needs to be applied after the fix from Daniel Borkmann, that is "[net-next,v2,1/2] bpf: make htab inlining more robust wrt assumptions"
with message ID:
03f4e86a029058d0f674fd9bf288e55a5ec07df3.1503104831.git.daniel at iogearbox.net
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.
--- arch/arm/Kconfig | 2 +- arch/arm/net/bpf_jit_32.c | 2441 +++++++++++++++++++++++++++++++-------------- arch/arm/net/bpf_jit_32.h | 108 +- 3 files changed, 1750 insertions(+), 801 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 select HAVE_CC_STACKPROTECTOR select HAVE_CONTEXT_TRACKING select HAVE_C_RECORDMCOUNT
arch/arm/net/bpf_jit_32.c +1666 -775
2,684 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
Quoted 42 lines · Shubham Bansal
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_DWImplementation 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.
This patch needs to be applied after the fix from Daniel Borkmann, that is "[net-next,v2,1/2] bpf: make htab inlining more robust wrt assumptions"
with message ID:
03f4e86a029058d0f674fd9bf288e55a5ec07df3.1503104831.git.daniel at iogearbox.netTested 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: OKAbove tests are all done with following flags enabled discreatly.
1) bpf_jit_enable=1
a)CONFIG_FRAME_POINTERenabled
b)CONFIG_FRAME_POINTERdisabled
2) bpf_jit_enable=1 and bpf_jit_harden=2
a)CONFIG_FRAME_POINTERenabled
b)CONFIG_FRAME_POINTERdisabled
Quoted 3 lines
See
Documentation/networking/filter.txtfor more information.Signed-off-by Shubham Bansal <[email protected]>
impressive work.
Any performance numbers with vs without JIT ?
arch/arm/net/bpf_jit_32.c line 59
Quoted 5 lines
arch/arm/net/bpf_jit_32.c +5
-0+static const u8 bpf2a32[][2 ] = { +/* return value from in-kernel function, and exit value from eBPF */ + [BPF_REG_0 ] = {ARM_R1 ,ARM_R0 }, +/* arguments from eBPF program to in-kernel function */ + [BPF_REG_1 ] = {ARM_R3 ,ARM_R2 },
as far as i understand arm32 calling convention the mapping makes sense to me. Hard to come up with anything better than the above.
Quoted 9 lines
arch/arm/net/bpf_jit_32.c +9
-0+/* function call */ +case BPF_JMP |BPF_CALL : + { +const u8 *r0 = bpf2a32[BPF_REG_0 ]; +const u8 *r1 = bpf2a32[BPF_REG_1 ]; +const u8 *r2 = bpf2a32[BPF_REG_2 ]; +const u8 *r3 = bpf2a32[BPF_REG_3 ]; +const u8 *r4 = bpf2a32[BPF_REG_4 ]; +const u8 *r5 = bpf2a32[BPF_REG_5 ];
arch/arm/net/bpf_jit_32.c static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx) · line 1731
Quoted 10 lines
arch/arm/net/bpf_jit_32.c +10
-0+const u32 func = (u32 )__bpf_call_base + (u32 )imm; + +emit_a32_mov_r64 (true, r0, r1, false, false, ctx); +emit_a32_mov_r64 (true, r1, r2, false, true, ctx); +emit_push_r64 (r5,0 , ctx); +emit_push_r64 (r4,8 , ctx); +emit_push_r64 (r3,16 , ctx); + +emit_a32_mov_i (tmp[1 ], func, false, ctx); +emit_blx_r (tmp[1 ], ctx);
to improve the cost of call we can teach verifier to mark the registers actually used to pass arguments, so not all pushes would be needed. But it may be drop in the bucket comparing to the cost of compound 64-bit alu ops.
There was some work on llvm side to use 32-bit subregisters which should help 32-bit architectures and JITs, but it didn't go far. So if you're interested further improving bpf program speeds on arm32 you may take a look at llvm side. I can certainly provide the tips.
Quoted 2 lines
impressive work.
Thanks :)
I can't take all the credit. It was Daniel and Kees who helped me a lot. I would have given up a long time ago without them.
Quoted 2 lines
Any performance numbers with vs without JIT ?
Here is the mail from Kees on v1 of the patch.
For what it's worth, I did an comparison of the numbers Shubham posted in another thread for the JIT, comparing the eBPF interpreter with his new JIT. The post is here:
https://www.spinics.net/lists/netdev/msg436402.html
Other than that I can send the test runs which have time, but I will not be able to compare them like kees this week.
Does that sound good?
Quoted 11 lines
arch/arm/net/bpf_jit_32.c line 59
Quoted 7 lines
(quoted diff) +2
-0+static const u8 bpf2a32[][2] = { + /* return value from in-kernel function, and exit value from eBPF*/
arch/arm/net/bpf_jit_32.c +3
-0+ [BPF_REG_0] = {ARM_R1, ARM_R0}, + /* arguments from eBPF program to in-kernel function */ + [BPF_REG_1] = {ARM_R3, ARM_R2},as far as i understand arm32 calling convention the mapping makes sense to me. Hard to come up with anything better than the above.
I tried different versions of it, according to the need of different eBPF instructions, as you can see, we are register deficient. This is the best I could come up with.
Would love to hear any improvement over this.
Quoted 18 lines
Quoted 18 lines
arch/arm/net/bpf_jit_32.c +17
-0+/* function call */ +case BPF_JMP |BPF_CALL : + { +const u8 *r0 = bpf2a32[BPF_REG_0 ]; +const u8 *r1 = bpf2a32[BPF_REG_1 ]; +const u8 *r2 = bpf2a32[BPF_REG_2 ]; +const u8 *r3 = bpf2a32[BPF_REG_3 ]; +const u8 *r4 = bpf2a32[BPF_REG_4 ]; +const u8 *r5 = bpf2a32[BPF_REG_5 ]; +const u32 func = (u32 )__bpf_call_base + (u32 )imm; + +emit_a32_mov_r64 (true, r0, r1, false, false, ctx); +emit_a32_mov_r64 (true, r1, r2, false, true, ctx); +emit_push_r64 (r5,0 , ctx); +emit_push_r64 (r4,8 , ctx); +emit_push_r64 (r3,16 , ctx); +
Quoted 8 lines
arch/arm/net/bpf_jit_32.c static inline void emit_mov_i(int rd, u32 val, struct jit_ctx *ctx) · line 1731
Quoted 2 lines
arch/arm/net/bpf_jit_32.c +2
-0+emit_a32_mov_i (tmp[1 ], func, false, ctx); +emit_blx_r (tmp[1 ], ctx);to improve the cost of call we can teach verifier to mark the registers actually used to pass arguments, so not all pushes would be needed. But it may be drop in the bucket comparing to the cost of compound 64-bit alu ops.
Thats right. But still an improvement I guess. I think I discussed it with Daniel and I thought, I should get this patch reach mainstream first then I can improve on it.
Quoted 4 lines
There was some work on llvm side to use 32-bit subregisters which should help 32-bit architectures and JITs, but it didn't go far. So if you're interested further improving bpf program speeds on arm32 you may take a look at llvm side. I can certainly provide the tips.
Sure. Sounds good.
Best,
Shubham
One more thing I forgot to mention.
I think this is the first implementation of eBPF JIT on any 32 bit arch, correct me if I am wrong. I think we can use this as a POC to implement eBPF on other 32 bit arch as well like x86, depends on its need I guess.
Quoted 2 lines · Shubham Bansal
not be able to compare them like kees this week.
Does that sound good?
yeah. that's fine. I was more interested in selftests/.../test_progs numbers before/after, since they're more representative of real world performance vs test_bpf.ko
Quoted 3 lines
Thats right. But still an improvement I guess. I think I discussed it with Daniel and I thought, I should get this patch reach mainstream first then I can improve on it.
agree. To me the patch looks ready to land.
Here are numbers.
Without any JIT enabled
test_pkt_access:PASS:ipv4 1823 nsec test_pkt_access:PASS:ipv6 1743 nsec
test_xdp:PASS:ipv4 769022 nsec
test_xdp:PASS:ipv6 15408 nsec
test_l4lb:PASS:ipv4 12441 nsec
test_l4lb:PASS:ipv6 18131 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found by get_next_id 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found by get_next_id 0 nsec test_pkt_md_access:PASS: 5816 nsec
Summary: 30 PASSED, 0 FAILED
With only bpf_jit_enable
test_pkt_access:PASS:ipv4 263 nsec test_pkt_access:PASS:ipv6 337 nsec
test_xdp:PASS:ipv4 1377212 nsec
test_xdp:PASS:ipv6 1290381 nsec
test_l4lb:PASS:ipv4 2689 nsec
test_l4lb:PASS:ipv6 3804 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found by get_next_id 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found by get_next_id 0 nsec test_pkt_md_access:PASS: 41609 nsec
Summary: 30 PASSED, 0 FAILED
With bpf_jit_enable and bpf_jit_harden set
test_pkt_access:PASS:ipv4 416 nsec test_pkt_access:PASS:ipv6 509 nsec
test_xdp:PASS:ipv4 1512428 nsec
test_xdp:PASS:ipv6 962318 nsec
test_l4lb:PASS:ipv4 4135 nsec
test_l4lb:PASS:ipv6 5497 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found by get_next_id 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found by get_next_id 0 nsec test_pkt_md_access:PASS: 24774 nsec
Summary: 30 PASSED, 0 FAILED
-Shubham
Qemu is giving me different numbers different time. Another stats.
With bpf_jit_enable set
test_pkt_access:PASS:ipv4 271 nsec test_pkt_access:PASS:ipv6 297 nsec
test_xdp:PASS:ipv4 961517 nsec <--- Here is the difference.
test_xdp:PASS:ipv6 615855 nsec <--- Here is the difference.
test_l4lb:PASS:ipv4 3049 nsec
test_l4lb:PASS:ipv6 3906 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found by get_next_id 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found by get_next_id 0 nsec
test_pkt_md_access:PASS: 30459 nsec <--- Here is the difference.
Summary: 30 PASSED, 0 FAILED
Quoted 106 lines · Shubham Bansal
Here are numbers.
Without any JIT enabled
test_pkt_access:PASS:ipv4 1823 nsec test_pkt_access:PASS:ipv6 1743 nsec
test_xdp:PASS:ipv4 769022 nsec
test_xdp:PASS:ipv6 15408 nsec
test_l4lb:PASS:ipv4 12441 nsec
test_l4lb:PASS:ipv6 18131 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found byget_next_id0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found byget_next_id0 nsec test_pkt_md_access:PASS: 5816 nsec
Summary: 30 PASSED, 0 FAILEDWith only
bpf_jit_enabletest_pkt_access:PASS:ipv4 263 nsec test_pkt_access:PASS:ipv6 337 nsec
test_xdp:PASS:ipv4 1377212 nsec
test_xdp:PASS:ipv6 1290381 nsec
test_l4lb:PASS:ipv4 2689 nsec
test_l4lb:PASS:ipv6 3804 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found byget_next_id0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found byget_next_id0 nsec test_pkt_md_access:PASS: 41609 nsec
Summary: 30 PASSED, 0 FAILEDWith
bpf_jit_enableandbpf_jit_hardensettest_pkt_access:PASS:ipv4 416 nsec test_pkt_access:PASS:ipv6 509 nsec
test_xdp:PASS:ipv4 1512428 nsec
test_xdp:PASS:ipv6 962318 nsec
test_l4lb:PASS:ipv4 4135 nsec
test_l4lb:PASS:ipv6 5497 nsec
test_tcp_estats:PASS: 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-prog-id 0 nsec test_bpf_obj_id:PASS:get-fd-by-notexist-map-id 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-info(fd) 0 nsec test_bpf_obj_id:PASS:get-map-info(fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-prog-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-prog-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total prog id found byget_next_id0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:get-map-fd(next_id) 0 nsec test_bpf_obj_id:PASS:check get-map-info(next_id->fd) 0 nsec test_bpf_obj_id:PASS:check total map id found byget_next_id0 nsec test_pkt_md_access:PASS: 24774 nsec
Summary: 30 PASSED, 0 FAILED-Shubham
Quoted 4 lines · Shubham Bansal
test_pkt_access:PASS:ipv4 271 nsec
test_pkt_access:PASS:ipv6 297 nsec
test_xdp:PASS:ipv4 961517 nsec <--- Here is the difference.
test_xdp:PASS:ipv6 615855 nsec <--- Here is the difference.
yes. this is expected. These two numbers are single run on cold cache, so there will be run-to-run variation.
Quoted 2 lines
test_l4lb:PASS:ipv4 3049 nsec
test_l4lb:PASS:ipv6 3906 nsec
These two and the first two were the ones I was interested in, since they do many iterations over the same set and
the best to compare code gen changes.
The delta % is actually better than I expected judging by test_bpf micro-benchmarks, so the results are very encouraging.
Thanks!
Quoted 1 line
David, Russell, Kees and Daniel, Anything from your side? Is this patch ready to land in net-next?
Daniel Borkmann
maintainerOn 08/19/2017 11:20 AM, Shubham Bansal wrote:
[...]
Quoted 35 lines
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 select HAVE_CC_STACKPROTECTOR select HAVE_CONTEXT_TRACKING select HAVE_C_RECORDMCOUNTarch/arm/net/bpf_jit_32.c +3
-1@@ -1,6 +1,7 @@/* -* Just-In-Time compiler for BPF filters on 32bit ARM +* Just-In-Time compiler for eBPF filters on 32bit ARM * +* Copyright (c) 2017 Shubham Bansal <[email protected]> * Copyright (c) 2011 Mircea Gherzan <[email protected]> * * This program is free software; you can redistribute it and/or modify it @@ -8,6 +9,7 @@* Free Software Foundation; version 2 of the License. */ +#include <linux/bpf.h> #include <linux/bitops.h> #include <linux/compiler.h> #include <linux/errno.h>
arch/arm/net/bpf_jit_32.c line 28
Quoted 10 lines
arch/arm/net/bpf_jit_32.c +1
-1@@ -18,50 +20,96 @@#include <linux/if_vlan.h> #include <asm/cacheflush.h> -#include <asm/set_memory.h> #include <asm/hwcap.h> #include <asm/opcodes.h> #include "bpf_jit_32.h" +int bpf_jit_enable__read_mostly ;
[...]
With the below #ifdef __LITTLE_ENDIAN spanning the entire bpf_int_jit_compile(), a user can then enable and compile eBPF JIT for big endian, even set the bpf_jit_enable to 1 to turn it on, but it won't JIT anything, which is contrary to the expectation.
This should rather be a hard dependency in the Kconfig, if I got it correctly, expressed as e.g.
select HAVE_EBPF_JIT if !CPU_ENDIAN_BE32
Quoted 187 lines
arch/arm/net/bpf_jit_32.c +108
-37+struct bpf_prog *bpf_int_jit_compile (struct bpf_prog *prog) { +#ifdef __LITTLE_ENDIAN +struct bpf_prog *tmp, *orig_prog = prog;struct bpf_binary_header *header; +bool tmp_blinded = false;struct jit_ctx ctx; -unsigned tmp_idx; -unsigned alloc_size; -u8 *target_ptr; +unsigned int tmp_idx; +unsigned int image_size; +u8 *image_ptr; +/* If BPF JIT was not enabled then we must fall back to +* the interpreter. +*/ if (!bpf_jit_enable) -return ; +return orig_prog; -memset (&ctx,0 ,sizeof (ctx)); - ctx.skf = fp; - ctx.ret0_fp_idx =-1 ; +/* If constant blinding was enabled and we failed during blinding +* then we must fall back to the interpreter. Otherwise, we save +* the new JITed code. +*/ + tmp =bpf_jit_blind_constants (prog); - ctx.offsets =kzalloc (4 * (ctx.skf->len +1 ),GFP_KERNEL ); -if (ctx.offsets ==NULL ) -return ; +if (IS_ERR (tmp)) +return orig_prog; +if (tmp != prog) { + tmp_blinded = true; + prog = tmp; + } + +memset (&ctx,0 ,sizeof (ctx)); + ctx.prog = prog; -/* fake pass to fill in the ctx->seen */ -if (unlikely (build_body (&ctx))) +/* Not able to allocate memory for offsets[] , then +* we must fall back to the interpreter +*/ + ctx.offsets =kcalloc (prog->len,sizeof (int ),GFP_KERNEL ); +if (ctx.offsets ==NULL ) { + prog = orig_prog; goto out; + } + +/* 1) fake pass to find in the length of the JITed code, +* to compute ctx->offsets and other context variables +* needed to compute final JITed code. +* Also, calculate random starting pointer/start of JITed code +* which is prefixed by random number of fault instructions. +* +* If the first pass fails then there is no chance of it +* being successful in the second pass, so just fall back +* to the interpreter. +*/ +if (build_body (&ctx)) { + prog = orig_prog; + goto out_off; + } tmp_idx = ctx.idx;build_prologue (&ctx); ctx.prologue_bytes = (ctx.idx - tmp_idx) *4 ; + ctx.epilogue_offset = ctx.idx; +#if __LINUX_ARM_ARCH__ <7 tmp_idx = ctx.idx;build_epilogue (&ctx);@@ -1021,64 +1878,98 @@ void bpf_jit_compile(struct bpf_prog *fp) ctx.idx += ctx.imm_count;if (ctx.imm_count) { - ctx.imms =kzalloc (4 * ctx.imm_count,GFP_KERNEL ); -if (ctx.imms ==NULL ) - goto out; + ctx.imms =kcalloc (ctx.imm_count,sizeof (u32),GFP_KERNEL ); +if (ctx.imms ==NULL ) { + prog = orig_prog; + goto out_off; + } }#else -/* there's nothing after the epilogue on ARMv7 */ +/* there's nothing about the epilogue on ARMv7 */ build_epilogue (&ctx);#endif - alloc_size =4 * ctx.idx; - header =bpf_jit_binary_alloc (alloc_size, &target_ptr, -4 , jit_fill_hole); -if (header ==NULL ) - goto out; +/* Now we can get the actual image size of the JITed arm code. +* Currently, we are not considering the THUMB-2 instructions +* for jit, although it can decrease the size of the image. +* +* As each arm instruction is of length 32bit, we are translating +* number of JITed intructions into the size required to store these +* JITed code. +*/ + image_size =sizeof (u32) * ctx.idx; + +/* Now we know the size of the structure to make */ + header =bpf_jit_binary_alloc (image_size, &image_ptr, +sizeof (u32), jit_fill_hole); +/* Not able to allocate memory for the structure then +* we must fall back to the interpretation +*/ +if (header ==NULL ) { + prog = orig_prog; + goto out_imms; + } - ctx.target = (u32 *) target_ptr; +/* 2.) Actual pass to generate final JIT code */ + ctx.target = (u32 *) image_ptr; ctx.idx =0 ;build_prologue (&ctx); + +/* If building the body of the JITed code fails somehow, +* we fall back to the interpretation. +*/ if (build_body (&ctx) <0 ) { -#if __LINUX_ARM_ARCH__ <7 -if (ctx.imm_count) -kfree (ctx.imms); -#endif + image_ptr =NULL ;bpf_jit_binary_free (header); - goto out; + prog = orig_prog; + goto out_imms; }build_epilogue (&ctx); +/* 3.) Extra pass to validate JITed Code */ +if (validate_code (&ctx)) { + image_ptr =NULL ; +bpf_jit_binary_free (header); + prog = orig_prog; + goto out_imms; + }flush_icache_range ((u32 )header, (u32)(ctx.target + ctx.idx)); -#if __LINUX_ARM_ARCH__ <7 -if (ctx.imm_count) -kfree (ctx.imms); -#endif -if (bpf_jit_enable >1 )/* there are 2 passes here */ -bpf_jit_dump (fp->len, alloc_size,2 , ctx.target); +bpf_jit_dump (prog->len, image_size,2 , ctx.target);set_memory_ro ((unsigned long )header, header->pages); - fp->bpf_func = (void *)ctx.target; - fp->jited =1 ; -out: + prog->bpf_func = (void *)ctx.target; + prog->jited =1 ; + prog->jited_len = image_size; + +out_imms: +#if __LINUX_ARM_ARCH__ <7 +if (ctx.imm_count) +kfree (ctx.imms); +#endif +out_off:kfree (ctx.offsets); -return ; +out: +if (tmp_blinded) +bpf_jit_prog_release_other (prog, prog == orig_prog ? + tmp : orig_prog); +#endif /* __LITTLE_ENDIAN */ +return prog; }
Quoted 10 lines
With the below #ifdef
__LITTLE_ENDIANspanning the entirebpf_int_jit_compile(), a user can then enable and compile eBPF JIT for big endian, even set thebpf_jit_enableto 1 to turn it on, but it won't JIT anything, which is contrary to the expectation.This should rather be a hard dependency in the
Kconfig, if I got it correctly, expressed as e.g.select
HAVE_EBPF_JITif !CPU_ENDIAN_BE32
Will do it. That's a good catch. Thanks Daniel.