Skip to contentSkip to conversation

RV32G eBPF JIT

PATCH Superseded Luke Nelson 2 participants 3 messages Jun 26, 2019 – Jun 28, 2019

Subsystems (6)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 RV32G eBPF JIT

From: Luke Nelson <[email protected]>

This is an eBPF JIT for RV32G, adapted from the JIT for RV64G.

There are two main changes required for this to work compared to the RV64 JIT.

First, eBPF registers are 64-bit, while RV32G registers are 32-bit. BPF registers either map directly to 2 RISC-V registers, or reside in stack scratch space and are saved and restored when used.

Second, many 64-bit ALU operations do not trivially map to 32-bit operations. Operations that move bits between high and low words, such as ADD, LSH, MUL, and others must emulate the 64-bit behavior in terms of 32-bit instructions.

It passes 360 out of 378 tests in test_bpf.ko. The failing tests are features that are not supported.
* ALU64 DIV/MOD
* BPF_XADD | BPF_DW
* Tail calls

v1 -> v2:
* Added support for far conditional branches.
* Added the zero-extension optimization pointed out by Jiong Wang.
* Added more optimizations for operations with an immediate operand.

---
 arch/riscv/Kconfig              |    2 +-
 arch/riscv/net/Makefile         |    7 +-
 arch/riscv/net/bpf_jit_comp32.c | 1655 +++++++++++++++++++++++++++++++
 3 files changed, 1662 insertions(+), 2 deletions(-)
 create mode 100644 arch/riscv/net/bpf_jit_comp32.c
arch/riscv/Kconfig +1 -1
index 0c4b12205632..153ff9ee6e6c 100644
@@ -49,7 +49,7 @@ config RISCV
 	select GENERIC_IRQ_MULTI_HANDLER
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_MMIOWB
-	select HAVE_EBPF_JIT if 64BIT
+	select HAVE_EBPF_JIT
 
 config MMU
 	def_bool y
arch/riscv/net/Makefile +6 -1
index ec5b14763316..5511fb1dd40e 100644
@@ -1,2 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
+
+ifeq ($(CONFIG_ARCH_RV64I),y)
+	obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
+else
+	obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o
+endif
arch/riscv/net/bpf_jit_comp32.c new file +1655 -0

1,655 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

Luke Nelson writes:

<snip>

Quoted 12 lines
arch/riscv/net/bpf_jit_comp32.c new file +12 -0
+
+static int emit_insn(const struct bpf_insn *insn,
+		     struct rv_jit_context *ctx,
+		     bool extra_pass)
+{
+	int rvoff, i = insn - ctx->prog->insnsi;
+	u8 code = insn->code;
+	s16 off = insn->off;
+	s32 imm = insn->imm;
+
+	const s8 *dst = bpf2rv32[insn->dst_reg];
+	const s8 *src = bpf2rv32[insn->src_reg];

arch/riscv/net/bpf_jit_comp32.c line 1148

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c new file +10 -0
+	const s8 *tmp1 = bpf2rv32[TMP_REG_1];
+	const s8 *tmp2 = bpf2rv32[TMP_REG_2];
+
+	switch (code) {
+	case BPF_ALU64 | BPF_MOV | BPF_X:
+		if (imm == 1) {
+			/* Special mov32 for zext */
+			emit_rv32_zext64(dst, ctx);
+			break;
+		}

Thanks for adding the 32-bit opt!

Just want to mention ZEXT is a special mov32, see include/linux/filter.h:

#define BPF_ZEXT_REG(DST)
((struct bpf_insn) {
.code = BPF_ALU | BPF_MOV | BPF_X

So it can't be BPF_ALU64. It is safe to remove this chunk of code.

For backend like arm, riscv etc, they are grouping several CASE label together and are sharing code. imm == 1 check is done inside the shared code to avoid moving code around given imm == 1 can't be true for ALU64 as if there is such insn (register format using imm) it should have been rejected by verifier. While mov32 variant is inserted by verifier at very late stage after main verification finished.

Regards,
Jiong

#2 lore raw
Quoted 7 lines · Jiong Wang

#define BPF_ZEXT_REG(DST)
((struct bpf_insn) {
.code = BPF_ALU | BPF_MOV | BPF_X

So it can't be BPF_ALU64. It is safe to remove this chunk of code.

Thanks! I'll fix this in the next revision.

- Luke