Skip to contentSkip to conversation

m68k, bpf: Add initial BPF JIT compiler support

PATCH Superseded Kuan-Wei Chiu 2 participants 2 messages Oct 10

Subsystems (3)

v8 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 1/1 m68k, bpf: Add initial BPF JIT compiler support

Add a BPF JIT compiler for the m68k architecture.

The JIT generates m68k machine code targeting m68020+ processors and coldfire parts with an mmu (ISA_B/ISA_C). It excludes plain 68000/68010 and non-mmu coldfire processors, as it relies on 32-bit branch displacements (b<cc>.l) to handle large bpf programs.

Tested with the test_bpf.ko:
test_bpf: Summary: 1061 PASSED, 0 FAILED, [1049/1049 JIT'ed]
test_bpf: test_tail_calls: Summary: 10 PASSED, 0 FAILED, [10/10 JIT'ed]

---
Changes in v8:
- Apply Greg's CF helper fixup (with minor move.l correction).
- Check !prog->is_func instead of !prog->aux->func

 .../features/core/eBPF-JIT/arch-support.txt   |    2 +-
 MAINTAINERS                                   |    6 +
 arch/m68k/Kbuild                              |    2 +-
 arch/m68k/Kconfig                             |    1 +
 arch/m68k/net/Makefile                        |    2 +
 arch/m68k/net/bpf_jit_comp.c                  | 1810 +++++++++++++++++
 6 files changed, 1821 insertions(+), 2 deletions(-)
 create mode 100644 arch/m68k/net/Makefile
 create mode 100644 arch/m68k/net/bpf_jit_comp.c
Documentation/features/core/eBPF-JIT/arch-support.txt +1 -1
index 83f77f55fc87..dacb50aef055 100644
@@ -13,7 +13,7 @@
     |        csky: | TODO |
     |     hexagon: | TODO |
     |   loongarch: |  ok  |
-    |        m68k: | TODO |
+    |        m68k: |  ok  |
     |  microblaze: | TODO |
     |        mips: |  ok  |
     |       nios2: | TODO |
MAINTAINERS +6 -0
index c31a32a2c748..b3b310c972a4 100644
@@ -4807,6 +4807,12 @@ L:	[email protected]
 S:	Maintained
 F:	arch/loongarch/net/
 
+BPF JIT for M68K
+M:	Kuan-Wei Chiu <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	arch/m68k/net/
+
 BPF JIT for MIPS (32-BIT AND 64-BIT)
 M:	Johan Almbladh <[email protected]>
 M:	Paul Burton <[email protected]>
arch/m68k/Kbuild +1 -1
index 7762af9f6def..058ad41a728c 100644
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
-obj-y				+= kernel/ mm/
+obj-y				+= kernel/ mm/ net/
 obj-$(CONFIG_Q40)		+= q40/
 obj-$(CONFIG_AMIGA)		+= amiga/
 obj-$(CONFIG_ATARI)		+= atari/
arch/m68k/Kconfig +1 -0
index 11835eb59d94..f334fca80f3f 100644
@@ -31,6 +31,7 @@ config M68K
 	select HAVE_ARCH_SECCOMP_FILTER
 	select HAVE_ASM_MODVERSIONS
 	select HAVE_DEBUG_BUGVERBOSE
+	select HAVE_EBPF_JIT if MMU
 	select HAVE_EFFICIENT_UNALIGNED_ACCESS if !CPU_HAS_NO_UNALIGNED
 	select HAVE_LD_DEAD_CODE_DATA_ELIMINATION
 	select HAVE_MOD_ARCH_SPECIFIC
arch/m68k/net/Makefile new file +2 -0
new file mode 100644
index 000000000000..ec5b14763316
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0-only
+obj-$(CONFIG_BPF_JIT) += bpf_jit_comp.o
arch/m68k/net/bpf_jit_comp.c new file +1810 -0

1,810 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#1 lore raw
TL;DR: m68k JIT returns true from bpf_jit_needs_zext() but still zero-extends after every ALU32 op and load, so verifier-inserted zexts only add code. Results stay correct; should it honour…
Quoted 1 line

m68k, bpf: Add initial BPF JIT compiler support

TL;DR: m68k JIT returns true from bpf_jit_needs_zext() but still zero-extends after every ALU32 op and load, so verifier-inserted zexts only add code. Results stay correct; should it honour verifier_zext or drop the override?

Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0
new file mode 100644
index 000000000000..debbf5fbe95b
@@ -0,0 +1,1810 @@

[ ... ]

Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2 -0
+static void emit_alu32_x(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 268

Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +6 -0
+	}
+
+	bpf_put_reg32(dst[1], d_reg, ctx);
+	emit_16(ctx, 0x7000 | (tmp1[0] << 9));			/* moveq #0, tmp1 */
+	bpf_put_reg32(dst[0], tmp1[0], ctx);
+}

emit_alu32_k() ends with the same three lines. Is it necessary to clear the upper 32 bits after every ALU32 operation here?

The file also has:

arch/m68k/net/bpf_jit_comp.c line 1731

Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4 -0
+bool bpf_jit_needs_zext(void)
+{
+	return true;
+}

Because bpf_jit_needs_zext() returns true, bpf_opt_subreg_zext_lo32_rnd_hi32() adds a BPF_ZEXT_REG(dst) after every 32-bit def whose upper half is read later, and it sets prog->aux->verifier_zext. Nothing in this JIT reads verifier_zext, so each inserted zext goes through emit_alu32_x() as a plain mov32 and clears the high word a second time.

For a stack-backed register (R1-R9, AX) the inserted zext costs six extra instructions, about 20 bytes and four memory accesses:

move.l d16(%fp),%d2
move.l d16(%fp),%d4
move.l %d4,%d2
move.l %d2,d16(%fp)
moveq #0,%d3
move.l %d3,d16(%fp)

The other 32-bit JITs that return true from bpf_jit_needs_zext() skip the high word clear when verifier_zext is set, for example arm32 (emit_a32_alu_r64 and the special mov32 for zext case), x86-32, riscv32 (bpf_put_reg32) and powerpc32. emit_ldx() also always zero-extends here.

Results are still correct, but the opt-in only adds code. Should emit_alu32_x(), emit_alu32_k() and emit_ldx() honour ctx->prog->aux->verifier_zext, with the imm == 1 zext mov clearing only the high word? Alternatively, since the JIT already zero-extends everywhere, could the bpf_jit_needs_zext() override be dropped?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/38037161364