eBPF JIT for RV32G
Subsystems (7)
v5 cover eBPF JIT for RV32G
This series adds an eBPF JIT for 32-bit RISC-V (RV32G) to the kernel, adapted from the RV64 JIT 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:
The RV32 JIT supports the same features and instructions as the RV64 JIT, with the following exceptions:
- ALU64 DIV/MOD: Requires loops to implement on 32-bit hardware.
- BPF_XADD | BPF_DW: There's no 8-byte atomic instruction in RV32.
These features are also unsupported on other BPF JITs for 32-bit architectures.
Testing:
- lib/test_bpf.ctest_bpf: Summary: 378 PASSED, 0 FAILED, [349/366 JIT'ed]test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED
The tests that are not JITed are all due to use of 64-bit div/mod or 64-bit xadd.
- tools/testing/selftests/bpf/test_verifier.c
Summary: 1415 PASSED, 122 SKIPPED, 43 FAILED
Tested both with and without BPF JIT hardening.
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 the RV32 JIT using our automated verification tool, Serval. We have used Serval in the past to verify patches to the RV64 JIT. We also used Serval 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/serval-bpf/tree/rv32-jit-v5
Thanks again for all the comments!
Changelog:
v4 -> v5:
* Factored common code (build_body, bpf_int_jit_compile, etc) to bpf_jit_core.c (Björn Töpel).
* Moved RV32-specific changes to bpf_jit.h from patch 1 to patch 2 (Björn Töpel).
* Removed "_rv32_" from function names in JIT as it is redundant (Björn Töpel).
* Added commit message to MAINTAINERS and made sure to keep entries in order (Andy Shevchenko).
v3 -> v4:
* Added more comments and cleaned up style nits (Björn Töpel).
* Factored common code in RV64 and RV32 JITs into a separate header (Song Liu, Björn Töpel).
* Added an optimization in the BPF_ALU64 BPF_ADD BPF_X case.
* Updated MAINTAINERS and kernel documentation (Björn Töpel).
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 (Jiong Wang).
v1 -> v2:
* Added support for far conditional branches.
* Added the zero-extension optimization (Jiong Wang).
* Added more optimizations for operations with an immediate operand.
Luke Nelson (4):
riscv, bpf: factor common RISC-V JIT code riscv, bpf: add RV32G eBPF JIT
bpf, doc: add BPF JIT for RV32G to BPF documentation
MAINTAINERS: add entry for RV32G BPF JIT
Documentation/admin-guide/sysctl/net.rst | 3 +-
Documentation/networking/filter.txt | 2 +-
MAINTAINERS | 13 +-
arch/riscv/Kconfig | 2 +-
arch/riscv/net/Makefile | 9 +-
arch/riscv/net/bpf_jit.h | 514 +++++++
arch/riscv/net/bpf_jit_comp32.c | 1310 +++++++++++++++++
.../net/{bpf_jit_comp.c => bpf_jit_comp64.c} | 605 +-------
arch/riscv/net/bpf_jit_core.c | 166 +++
9 files changed, 2018 insertions(+), 606 deletions(-)
create mode 100644 arch/riscv/net/bpf_jit.h
create mode 100644 arch/riscv/net/bpf_jit_comp32.c
rename arch/riscv/net/{bpf_jit_comp.c => bpf_jit_comp64.c} (69%)
create mode 100644 arch/riscv/net/bpf_jit_core.cSignature · 1 line
2.20.1
PATCH v5 1/4 riscv, bpf: factor common RISC-V JIT code
This patch factors out code that can be used by both the RV64 and RV32 BPF JITs to a common bpf_jit.h and bpf_jit_core.c.
Move struct definitions and macro-like functions to header. Rename rv_sb_insn/rv_uj_insn to rv_b_insn/rv_j_insn to match the RISC-V specification.
Move reusable functions emit_body() and bpf_int_jit_compile() to bpf_jit_core.c with minor simplifications. Rename emit_insn() and build_{prologue,epilogue}() to be prefixed with "bpf_jit_" as they are no longer static.
Rename bpf_jit_comp.c to bpf_jit_comp64.c to be more explicit.
---
arch/riscv/net/Makefile | 3 +-
arch/riscv/net/bpf_jit.h | 466 ++++++++++++++
.../net/{bpf_jit_comp.c => bpf_jit_comp64.c} | 605 +-----------------
arch/riscv/net/bpf_jit_core.c | 166 +++++
4 files changed, 639 insertions(+), 601 deletions(-)
create mode 100644 arch/riscv/net/bpf_jit.h
rename arch/riscv/net/{bpf_jit_comp.c => bpf_jit_comp64.c} (69%)
create mode 100644 arch/riscv/net/bpf_jit_core.carch/riscv/net/Makefile +2 -1
@@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o + +obj-$(CONFIG_BPF_JIT) += bpf_jit_core.o bpf_jit_comp64.o
arch/riscv/net/bpf_jit.h new file +466 -0
466 lines not on this page · raw message
arch/riscv/net/bpf_jit_comp64.c renamed from arch/riscv/net/bpf_jit_comp.c +5 -600
662 lines not on this page · raw message
arch/riscv/net/bpf_jit_core.c new file +166 -0
166 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v5 2/4 riscv, bpf: add RV32G eBPF JIT
In reply to Luke NelsonThis 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.
This patch also makes related changes to bpf_jit.h, such as adding RISC-V instructions required by the RV32 JIT.
Supported features:
The RV32 JIT supports the same features and instructions as the RV64 JIT, with the following exceptions:
- ALU64 DIV/MOD: Requires loops to implement on 32-bit hardware.
- BPF_XADD | BPF_DW: There's no 8-byte atomic instruction in RV32.
These features are also unsupported on other BPF JITs for 32-bit architectures.
Testing:
- lib/test_bpf.ctest_bpf: Summary: 378 PASSED, 0 FAILED, [349/366 JIT'ed]test_bpf: test_skb_segment: Summary: 2 PASSED, 0 FAILED
The tests that are not JITed are all due to use of 64-bit div/mod or 64-bit xadd.
- tools/testing/selftests/bpf/test_verifier.c
Summary: 1415 PASSED, 122 SKIPPED, 43 FAILED
Tested both with and without BPF JIT hardening.
This is the same set of tests that pass using the BPF interpreter with the JIT disabled.
Verification and synthesis:
We developed the RV32 JIT using our automated verification tool, Serval. We have used Serval in the past to verify patches to the RV64 JIT. We also used Serval 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/serval-bpf/tree/rv32-jit-v5
--- arch/riscv/Kconfig | 2 +- arch/riscv/net/Makefile | 8 +- arch/riscv/net/bpf_jit.h | 48 ++ arch/riscv/net/bpf_jit_comp32.c | 1310 +++++++++++++++++++++++++++++++ 4 files changed, 1366 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/net/bpf_jit_comp32.c
arch/riscv/Kconfig +1 -1
@@ -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 +7 -1
@@ -1,3 +1,9 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-$(CONFIG_BPF_JIT) += bpf_jit_core.o bpf_jit_comp64.o +obj-$(CONFIG_BPF_JIT) += bpf_jit_core.o + +ifeq ($(CONFIG_ARCH_RV64I),y) + obj-$(CONFIG_BPF_JIT) += bpf_jit_comp64.o +else + obj-$(CONFIG_BPF_JIT) += bpf_jit_comp32.o +endif
arch/riscv/net/bpf_jit.h +48 -0
99 lines not on this page · raw message
arch/riscv/net/bpf_jit_comp32.c new file +1310 -0
1,310 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v5 3/4 bpf, doc: add BPF JIT for RV32G to BPF documentation
In reply to Luke NelsonUpdate filter.txt and admin-guide to mention the BPF JIT for RV32G.
--- Documentation/admin-guide/sysctl/net.rst | 3 ++- Documentation/networking/filter.txt | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
Documentation/admin-guide/sysctl/net.rst +2 -1
@@ -67,7 +67,8 @@ two flavors of JITs, the newer eBPF JIT currently supported on: - sparc64 - mips64 - s390x - - riscv + - riscv64 + - riscv32 And the older cBPF JIT supported on the following archs:
Documentation/networking/filter.txt +1 -1
@@ -606,7 +606,7 @@ before a conversion to the new layout is being done behind the scenes! Currently, the classic BPF format is being used for JITing on most 32-bit architectures, whereas x86-64, aarch64, s390x, powerpc64, -sparc64, arm32, riscv (RV64G) perform JIT compilation from eBPF +sparc64, arm32, riscv64, riscv32 perform JIT compilation from eBPF instruction set. Some core changes of the new internal format:
Signature · 1 line
2.20.1
PATCH v5 4/4 MAINTAINERS: add entry for RV32G BPF JIT
In reply to Luke NelsonAdd a new entry for the 32-bit RISC-V JIT to MAINTAINERS and change mailing list from netdev to bpf following the guidelines from commit e42da4c62abb ("docs/bpf: Update bpf development Q/A file").
--- MAINTAINERS | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
MAINTAINERS +11 -2
@@ -3213,11 +3213,20 @@ L: [email protected] S: Maintained F: arch/powerpc/net/ -BPF JIT for RISC-V (RV64G) +BPF JIT for RISC-V (32-bit) +M: Luke Nelson <[email protected]> +M: Xi Wang <[email protected]> +L: [email protected] +S: Maintained +F: arch/riscv/net/ +X: arch/riscv/net/bpf_jit_comp64.c + +BPF JIT for RISC-V (64-bit) M: Björn Töpel <[email protected]> -L: [email protected] +L: [email protected] S: Maintained F: arch/riscv/net/ +X: arch/riscv/net/bpf_jit_comp32.c BPF JIT for S390 M: Ilya Leoshkevich <[email protected]>
Signature · 1 line
2.20.1
Quoted 4 lines · Luke Nelson
This series adds an eBPF JIT for 32-bit RISC-V (RV32G) to the kernel, adapted from the RV64 JIT and the 32-bit ARM JIT.
Nice work! Thanks for hanging in there!
For the series,
Quoted 11 lines · Björn Töpel
Quoted 4 lines · Luke Nelson
This series adds an eBPF JIT for 32-bit RISC-V (RV32G) to the kernel, adapted from the RV64 JIT and the 32-bit ARM JIT.
Nice work! Thanks for hanging in there!
For the series,
Acked-by Björn Töpel <[email protected]>Reviewed-by Björn Töpel <[email protected]>
Applied, thanks everyone!
P.s.: I fixed the MAINTAINERS entry in the last one to have both netdev and bpf to be consistent with all the other JIT entries there.
Quoted 9 lines · Daniel Borkmann
Quoted 8 lines · Björn Töpel
Quoted 4 lines · Luke Nelson
This series adds an eBPF JIT for 32-bit RISC-V (RV32G) to the kernel, adapted from the RV64 JIT and the 32-bit ARM JIT.
Nice work! Thanks for hanging in there!
Quoted 8 lines
Quoted 3 lines
For the series,
Acked-by Björn Töpel <[email protected]>Reviewed-by Björn Töpel <[email protected]>Applied, thanks everyone!
P.s.: I fixed the
MAINTAINERSentry in the last one to have both netdev and bpf to be consistent with all the other JIT entries there.
Does parse-maintainer.pl happy about your changes?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 1 line · Daniel Borkmann
[...]
Quoted 4 lines
Applied, thanks everyone!
P.s.: I fixed the
MAINTAINERSentry in the last one to have both netdev and bpf to be consistent with all the other JIT entries there.
Ah, I asked specifically Xi and Luke to *remove* the netdev entry, due to the bpf_devel_QA.rst change. :-)
Daniel Borkmann
maintainerQuoted 10 lines · Björn Töpel
Quoted 1 line · Daniel Borkmann
[...]
Quoted 4 lines
Applied, thanks everyone!
P.s.: I fixed the
MAINTAINERSentry in the last one to have both netdev and bpf to be consistent with all the other JIT entries there.Ah, I asked specifically Xi and Luke to *remove* the netdev entry, due to the
bpf_devel_QA.rstchange. :-)
Ah right, although all the BPF entries in MAINTAINERS file have both lists mentioned today. I think it doesn't hurt to have potentially more eyes for reviews.
Thanks,
Daniel