Skip to contentSkip to conversation

m68k, bpf: Add initial BPF JIT compiler support

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

Subsystems (3)

v9 of 12 Superseded Latest is v12 →

lore
Share & tools
lore
#0 lore raw

PATCH v9 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 v9:
- Reject BPF_PSEUDO_CALL/BPF_PSEUDO_FUNC and fall back to interpreter.
- Reserve 8 extra bytes on the stack frame for may_goto.
- Drop bpf_jit_needs_zext() override.

 .../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                  | 1760 +++++++++++++++++
 6 files changed, 1771 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 +1760 -0

1,760 lines not on this page · raw message

Signature · 1 line
2.56.0.385.gd3acb90ef8-goog
#1 lore raw
TL;DR: Nit only: in the m68k JIT's emit_math_call(), the BPF_CLASS checks on the signed div/mod selection look redundant given is_64, and could be folded into the switch cases. Not a bug.
Quoted 1 line

m68k, bpf: Add initial BPF JIT compiler support

TL;DR: Nit only: in the m68k JIT's emit_math_call(), the BPF_CLASS checks on the signed div/mod selection look redundant given is_64, and could be folded into the switch cases. Not a bug.

Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0
new file mode 100644
index 000000000000..e585cbcd7564

[ ... ]

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

[ ... ]

Quoted 23 lines
arch/m68k/net/bpf_jit_comp.c new file +23 -0
+	if (is_64) {
+		switch (op) {
+		case BPF_MUL:
+			func = (void *)jit_mul64;
+			break;
+		case BPF_DIV:
+			func = (void *)jit_div64;
+			break;
+		case BPF_MOD:
+			func = (void *)jit_mod64;
+			break;
+		}
+		if (BPF_CLASS(insn->code) == BPF_ALU64 && op == BPF_DIV && insn->off == 1)
+			func = (void *)jit_sdiv64;
+		if (BPF_CLASS(insn->code) == BPF_ALU64 && op == BPF_MOD && insn->off == 1)
+			func = (void *)jit_smod64;
+	} else {
+		switch (op) {
+		case BPF_MUL:
+			func = (void *)jit_mul32;
+			break;
+		case BPF_DIV:
+			func = (void *)jit_div32;

arch/m68k/net/bpf_jit_comp.c line 816

Quoted 10 lines
arch/m68k/net/bpf_jit_comp.c new file +10 -0
+			break;
+		case BPF_MOD:
+			func = (void *)jit_mod32;
+			break;
+		}
+		if (BPF_CLASS(insn->code) == BPF_ALU && op == BPF_DIV && insn->off == 1)
+			func = (void *)jit_sdiv32;
+		if (BPF_CLASS(insn->code) == BPF_ALU && op == BPF_MOD && insn->off == 1)
+			func = (void *)jit_smod32;
+	}

This isn't a bug, but since is_64 already encodes the instruction class, are the BPF_CLASS(insn->code) == BPF_ALU64 and BPF_CLASS(insn->code) == BPF_ALU tests in these four conditions always true?

Could the signed/unsigned choice be made inside the BPF_DIV and BPF_MOD cases of the switch instead, for example:

	case BPF_DIV:
		func = insn->off ? (void *)jit_sdiv64 : (void *)jit_div64;
		break;

That would drop the redundant class checks and the second assignment that overrides the one made in the switch.

[ ... ]

---
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/38122863281