Skip to contentSkip to conversation

RV32G eBPF JIT

PATCH Superseded Luke Nelson 4 participants 7 messages Feb 20, 2020 – Feb 28, 2020 1 message not in the archive

Subsystems (6)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 RV32G eBPF JIT

This is an eBPF JIT for RV32G, adapted from the JIT for RV64G and the 32-bit ARM JIT.

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.

Supported features:

This JIT supports the same features and instructions as RV64, with the following exceptions:

- ALU64 DIV/MOD: Requires loops to implement on 32-bit hardware.

- BPF_XADD | BPF_DW: Requires either an 8-byte atomic instruction in the target (which doesn't exist in RV32), or acqusition of locks in generated code.

These features are also unsupported on other BPF JITs for 32-bit architectures.

Testing:

- lib/test_bpf.c
test_bpf: Summary: 378 PASSED, 0 FAILED, [349/366 JIT'ed]
test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED

- tools/testing/selftests/bpf/test_verifier.c
Summary: 1415 PASSED, 122 SKIPPED, 43 FAILED

This is the same set of tests that pass using the BPF interpreter with the JIT disabled.

Running the BPF kernel tests / selftests on riscv32 is non-trivial, to help others reproduce the test results I made a guide here:
https://github.com/lukenels/meta-linux-utils/tree/master/rv32-linux

Verification and synthesis:

We developed this JIT using our verification tool that we have used in the past to verify patches to the RV64 JIT. We also used the tool to superoptimize the resulting code through program synthesis.

You can find the tool and a guide to the approach and results here:
https://github.com/uw-unsat/bpf-jit-verif

Changelog:

v2 -> v3:
* Added support for far jumps / branches similar to RV64 JIT.
* Added support for tail calls.
* Cleaned up code with more optimizations and comments.
* Removed special zero-extension instruction from BPF_ALU64 case, pointed out by Jiong Wang.

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 | 1840 +++++++++++++++++++++++++++++++
 3 files changed, 1847 insertions(+), 2 deletions(-)
 create mode 100644 arch/riscv/net/bpf_jit_comp32.c
arch/riscv/Kconfig +1 -1
index 73f029eae0cc..e9791717e006 100644
@@ -56,7 +56,7 @@ config RISCV
 	select ARCH_HAS_PTE_SPECIAL
 	select ARCH_HAS_MMIOWB
 	select ARCH_HAS_DEBUG_VIRTUAL
-	select HAVE_EBPF_JIT if 64BIT
+	select HAVE_EBPF_JIT
 	select EDAC_SUPPORT
 	select ARCH_HAS_GIGANTIC_PAGE
 	select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
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 +1840 -0

1,840 lines not on this page · raw message

Signature · 1 line
2.20.1
reviewer
#1 lore raw
Quoted 3 lines · Luke Nelson

This is an eBPF JIT for RV32G, adapted from the JIT for RV64G and the 32-bit ARM JIT.

I found a lot of common code between RV32G and RV64G. Can we move these code into a header file?

Thanks,
Song

A message that is not in the archive (sent off-list or not yet received): CADasFoBoEs+UArgqwQSqesKJhmvZEJP5so9nN-u9s6a3rQEUdA@mail.gmail.com

reviewer
#3 lore raw
Quoted 7 lines · Luke Nelson
Quoted 4 lines

I found a lot of common code between RV32G and RV64G. Can we move these code into a header file?

Certainly! I was planning on doing this in a separate patch to minimize the changes to the RV64 JIT. I can also do it in another revision of this patch if that seems like a better idea.

I would recommend expand this patch into two patches: one patch moves common code
to a header, the other adds logic for RV32G.

Thanks,
Song

#4 lore raw
In reply to Luke Nelson
Quoted 3 lines · Luke Nelson

This is an eBPF JIT for RV32G, adapted from the JIT for RV64G and the 32-bit ARM JIT.

...

Quoted 12 lines

v2 -> v3:
* Added support for far jumps / branches similar to RV64 JIT.
* Added support for tail calls.
* Cleaned up code with more optimizations and comments.
* Removed special zero-extension instruction from BPF_ALU64 case, pointed out by Jiong Wang.

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.

Quoted 4 lines

Bjorn,

please review.

#6 lore raw
In reply to Luke Nelson
Quoted 1 line · Luke Nelson

[...]

Quoted 3 lines

This is an eBPF JIT for RV32G, adapted from the JIT for RV64G and the 32-bit ARM JIT.

Luke/Xi, apologies for the slow reponse. (All my RV work is done on non-payed time, so that's that. :-)) Very nice that you're still working on it!

Quoted 12 lines

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.

Quoted 7 lines

Supported features:

This JIT supports the same features and instructions as RV64, with the following exceptions:

- ALU64 DIV/MOD: Requires loops to implement on 32-bit hardware.

Even though it requires loops, JIT support would be nice. OTOH, arm doesn't support that either...

Quoted 7 lines

- BPF_XADD | BPF_DW: Requires either an 8-byte atomic instruction in the target (which doesn't exist in RV32), or acqusition of locks in generated code.

These features are also unsupported on other BPF JITs for 32-bit architectures.

Any ideas how this could be addressed for RV32G?

Quoted 16 lines

Testing:

- lib/test_bpf.c
test_bpf: Summary: 378 PASSED, 0 FAILED, [349/366 JIT'ed]
test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED

- tools/testing/selftests/bpf/test_verifier.c
Summary: 1415 PASSED, 122 SKIPPED, 43 FAILED

This is the same set of tests that pass using the BPF interpreter with the JIT disabled.

Running the BPF kernel tests / selftests on riscv32 is non-trivial, to help others reproduce the test results I made a guide here:
https://github.com/lukenels/meta-linux-utils/tree/master/rv32-linux

Quoted 9 lines

Verification and synthesis:

We developed this JIT using our verification tool that we have used in the past to verify patches to the RV64 JIT. We also used the tool to superoptimize the resulting code through program synthesis.

You can find the tool and a guide to the approach and results here:
https://github.com/uw-unsat/bpf-jit-verif

Nice!

Quoted 9 lines

Changelog:

v2 -> v3:
* Added support for far jumps / branches similar to RV64 JIT.
* Added support for tail calls.
* Cleaned up code with more optimizations and comments.
* Removed special zero-extension instruction from BPF_ALU64 case, pointed out by Jiong Wang.

Quoted 6 lines

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.

Cc Jiong Wang <[email protected]>

Jiong is no longer at netronome.

Quoted 3 lines

In general I agree with Song; It would be good if the 64/32 bit variants would share more code. RISC-V 64/32 *are* very similar, and we should be able to benefit from that codewise.

Pull out all functions are that common -- most of the emit_*, the parts of the registers, the branch relaxation, and context structs. Hopefully, the acutal RV32/64 specfic parts will be pretty small.

Finally; There are some checkpatch issues: run 'checkpatch.pl --strict'.

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+static s8 hi(const s8 *r)

Everywhere else "const s8 r[]" is used, except in hi()/lo().

arch/riscv/net/bpf_jit_comp32.c line 113

Quoted 5 lines
arch/riscv/net/bpf_jit_comp32.c new file +5 -0
+{
+    return r[0];
+}
+
+static s8 lo(const s8 *r)

Likewise.

arch/riscv/net/bpf_jit_comp32.c line 126

Quoted 13 lines
arch/riscv/net/bpf_jit_comp32.c new file +13 -0
+{
+    return r[1];
+}
+
+struct rv_jit_context {
+    struct bpf_prog *prog;
+    u32 *insns; /* RV insns */
+    int ninsns;
+    int epilogue_offset;
+    int *offset; /* BPF to RV */
+    unsigned long flags;
+    int stack_size;
+};

Can be shared!

arch/riscv/net/bpf_jit_comp32.c line 132

Quoted 6 lines
arch/riscv/net/bpf_jit_comp32.c new file +6 -0
+
+struct rv_jit_data {
+    struct bpf_binary_header *header;
+    u8 *image;
+    struct rv_jit_context ctx;
+};

...and this...

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+static void emit(const u32 insn, struct rv_jit_context *ctx)

...and most of the emit/encoding code!

[...]

Quoted 19 lines
arch/riscv/net/bpf_jit_comp32.c new file +19 -0
+    switch (code) {
+    case BPF_ALU64 | BPF_MOV | BPF_X:
+
+    case BPF_ALU64 | BPF_ADD | BPF_X:
+    case BPF_ALU64 | BPF_ADD | BPF_K:
+
+    case BPF_ALU64 | BPF_SUB | BPF_X:
+    case BPF_ALU64 | BPF_SUB | BPF_K:
+
+    case BPF_ALU64 | BPF_AND | BPF_X:
+    case BPF_ALU64 | BPF_OR | BPF_X:
+    case BPF_ALU64 | BPF_XOR | BPF_X:
+
+    case BPF_ALU64 | BPF_MUL | BPF_X:
+    case BPF_ALU64 | BPF_MUL | BPF_K:
+
+    case BPF_ALU64 | BPF_LSH | BPF_X:
+    case BPF_ALU64 | BPF_RSH | BPF_X:
+    case BPF_ALU64 | BPF_ARSH | BPF_X:

arch/riscv/net/bpf_jit_comp32.c line 1349

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c new file +10 -0
+        if (BPF_SRC(code) == BPF_K) {
+            emit_imm32(tmp2, imm, ctx);
+            src = tmp2;
+        }
+        emit_rv32_alu_r64(dst, src, ctx, BPF_OP(code));
+        break;
+
+    case BPF_ALU64 | BPF_NEG:
+        emit_rv32_alu_r64(dst, tmp2, ctx, BPF_OP(code));
+        break;

This is neat; I should do it like this for RV64.

[...]

Quoted 14 lines
arch/riscv/net/bpf_jit_comp32.c new file +14 -0
+    case BPF_ALU | BPF_END | BPF_FROM_BE:
+    {
+        const s8 *rd = rv32_bpf_get_reg64(dst, tmp1, ctx);
+
+        switch (imm) {
+        case 16:
+            emit_rv32_rev16(lo(rd), ctx);
+            if (!ctx->prog->aux->verifier_zext)
+                emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);
+            break;
+        case 32:
+            emit_rv32_rev32(lo(rd), ctx);
+            if (!ctx->prog->aux->verifier_zext)
+                emit(rv_addi(hi(rd), RV_REG_ZERO, 0), ctx);

arch/riscv/net/bpf_jit_comp32.c line 1470

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c new file +10 -0
+            break;
+        case 64:
+            /* Swap upper and lower halves. */
+            emit(rv_addi(RV_REG_T0, lo(rd), 0), ctx);
+            emit(rv_addi(lo(rd), hi(rd), 0), ctx);
+            emit(rv_addi(hi(rd), RV_REG_T0, 0), ctx);
+
+            /* Swap each half. */
+            emit_rv32_rev32(lo(rd), ctx);
+            emit_rv32_rev32(hi(rd), ctx);

Waiting for that B-ext to be ratified? ;-)

[...]

Quoted 5 lines
arch/riscv/net/bpf_jit_comp32.c new file +5 -0
+    case BPF_JMP32 | BPF_JSET | BPF_K:
+        rvoff = rv_offset(i, off, ctx);
+        if (BPF_SRC(code) == BPF_K) {
+            s = ctx->ninsns;
+            emit_imm32(tmp2, imm, ctx);

arch/riscv/net/bpf_jit_comp32.c line 1574

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c new file +10 -0
+            src = tmp2;
+            e = ctx->ninsns;
+            rvoff -= (e - s) << 2;
+        }
+
+        if (is64) {
+            emit_rv32_branch_r64(dst, src, rvoff, ctx, BPF_OP(code));
+        } else {
+            emit_rv32_branch_r32(dst, src, rvoff, ctx, BPF_OP(code));
+        }

No need for {} here.

[...]

arch/riscv/net/bpf_jit_comp32.c line 1625

Quoted 2 lines
arch/riscv/net/bpf_jit_comp32.c new file +2 -0
+    case BPF_STX | BPF_XADD | BPF_DW:
+        goto notsupported;

The goto is not needed here.

arch/riscv/net/bpf_jit_comp32.c line 1628

Quoted 3 lines
arch/riscv/net/bpf_jit_comp32.c new file +3 -0
+
+notsupported:
+        pr_info_once("*** NOT SUPPORTED: opcode %02x ***\n", code);

A bit inconsistent, compared to the pr_err messages. The errors are "bpf-jit" prefixed.

arch/riscv/net/bpf_jit_comp32.c line 1641

Quoted 13 lines
arch/riscv/net/bpf_jit_comp32.c new file +13 -0
+        return -EFAULT;
+
+    default:
+        pr_err("bpf-jit: unknown opcode %02x\n", code);
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static void build_prologue(struct rv_jit_context *ctx)
+{
+    int stack_adjust = 4 * 9, store_offset, bpf_stack_adjust;

A comment why the magic number 4 * 9 is there would help future readers.

Quoted 52 lines
arch/riscv/net/bpf_jit_comp32.c new file +52 -0
+
+    bpf_stack_adjust = round_up(ctx->prog->aux->stack_depth, 16);
+    stack_adjust += bpf_stack_adjust;
+
+    store_offset = stack_adjust - 4;
+
+    stack_adjust += 4 * BPF_JIT_SCRATCH_REGS;
+
+    /* First instruction sets tail-call-counter,
+     * skipped by tail call.
+     */
+    emit(rv_addi(RV_REG_TCC, RV_REG_ZERO, MAX_TAIL_CALL_CNT), ctx);
+
+    emit(rv_addi(RV_REG_SP, RV_REG_SP, -stack_adjust), ctx);
+
+    /* Save callee-save registers. */
+    emit(rv_sw(RV_REG_SP, store_offset - 0, RV_REG_RA), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 4, RV_REG_FP), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 8, RV_REG_S1), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 12, RV_REG_S2), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 16, RV_REG_S3), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 20, RV_REG_S4), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 24, RV_REG_S5), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 28, RV_REG_S6), ctx);
+    emit(rv_sw(RV_REG_SP, store_offset - 32, RV_REG_S7), ctx);
+
+    /* Set fp: used as the base address for stacked BPF registers. */
+    emit(rv_addi(RV_REG_FP, RV_REG_SP, stack_adjust), ctx);
+
+    /* Set up BPF stack pointer. */
+    emit(rv_addi(lo(bpf2rv32[BPF_REG_FP]), RV_REG_SP, bpf_stack_adjust), ctx);
+    emit(rv_addi(hi(bpf2rv32[BPF_REG_FP]), RV_REG_ZERO, 0), ctx);
+
+    /* Set up context pointer. */
+    emit(rv_addi(lo(bpf2rv32[BPF_REG_1]), RV_REG_A0, 0), ctx);
+    emit(rv_addi(hi(bpf2rv32[BPF_REG_1]), RV_REG_ZERO, 0), ctx);
+
+    ctx->stack_size = stack_adjust;
+}
+
+static int build_body(struct rv_jit_context *ctx, bool extra_pass, int *offset)
+{
+    const struct bpf_prog *prog = ctx->prog;
+    int i;
+
+    for (i = 0; i < prog->len; i++) {
+        const struct bpf_insn *insn = &prog->insnsi[i];
+        int ret;
+
+        ret = emit_insn(insn, ctx, extra_pass);
+        if (ret > 0)
+            /* BPF_LD | BPF_IMM | BPF_DW:

arch/riscv/net/bpf_jit_comp32.c line 1703

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c new file +10 -0
+             * Skip next instruction.
+             */
+            i++;
+        if (offset)
+            offset[i] = ctx->ninsns;
+        if (ret < 0)
+            return ret;
+    }
+    return 0;
+}

Can be shared! ...and I think this version is better than the RV64 one! :-)

Quoted 7 lines
arch/riscv/net/bpf_jit_comp32.c new file +7 -0
+
+static void bpf_fill_ill_insns(void *area, unsigned int size)
+{
+    memset(area, 0, size);
+}
+
+static void bpf_flush_icache(void *start, void *end)

arch/riscv/net/bpf_jit_comp32.c line 1720

Quoted 10 lines
arch/riscv/net/bpf_jit_comp32.c new file +10 -0
+{
+    flush_icache_range((unsigned long)start, (unsigned long)end);
+}
+
+bool bpf_jit_needs_zext(void)
+{
+    return true;
+}
+
+struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)

The functions above can be shared with RV64 as well, right?

[...]

Thanks for the hard work! I'll take it for a spin, with help from the guide above, this weekend!

Cheers,
Björn

#7 lore raw
Quoted 4 lines · Björn Töpel

Luke/Xi, apologies for the slow reponse. (All my RV work is done on non-payed time, so that's that. :-)) Very nice that you're still working on it!

No worries, thanks for reviewing!

Quoted 5 lines
Quoted 2 lines

- ALU64 DIV/MOD: Requires loops to implement on 32-bit hardware.

Even though it requires loops, JIT support would be nice. OTOH, arm doesn't support that either...

We could probably implement this by calling into a helper function for BPF_ALU64 | BPF_DIV/BPF_MOD, but none of the other JITs (e.g., arm,x86) for 32-bit architectures do this. We could add support to rv32 and other architectures in a future patch.

Quoted 6 lines
Quoted 4 lines

- BPF_XADD | BPF_DW: Requires either an 8-byte atomic instruction in the target (which doesn't exist in RV32), or acqusition of locks in generated code.

Any ideas how this could be addressed for RV32G?

I don't believe there is a simple way to correctly implement BPF_XADD | BPF_DW without hardware support for 64-bit atomic operations, like for other 32-bit JITs.

Quoted 8 lines

In general I agree with Song; It would be good if the 64/32 bit variants would share more code. RISC-V 64/32 *are* very similar, and we should be able to benefit from that codewise.

Pull out all functions are that common -- most of the emit_*, the parts of the registers, the branch relaxation, and context structs. Hopefully, the acutal RV32/64 specfic parts will be pretty small.

Thanks for the suggestion. I'll factor out the common functionality into a header for the next revision.

Quoted 2 lines

Finally; There are some checkpatch issues: run 'checkpatch.pl --strict'. [...]

I'll fix the rest of the issues you found in the next revision.

Quoted 2 lines

Thanks for the hard work! I'll take it for a spin, with help from the guide above, this weekend!

Thanks again!
- Luke