Skip to contentSkip to conversation

m68k, bpf: Add initial BPF JIT compiler support

PATCH In review Kuan-Wei Chiu 3 participants 4 messages Nov 17

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v10 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 bpf selftests jit_insns and jit_insns_blinded, excluding 64 bit, kfunc, and bpf subprogram call subtests:
Summary: 2/41 PASSED, 0 SKIPPED, 0/0 FAILED

---
Changes in v10:
- Fold redundant BPF_CLASS() checks into the switch cases in emit_math_call().
- Rebase onto latest mainline and retest with bpf selftests jit_insns and jit_insns_blinded after test_bpf was removed.

 .../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                  | 1752 +++++++++++++++++
 6 files changed, 1763 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 d738990161bd..bf5ded204037 100644
@@ -4997,6 +4997,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 eb84c3af92c0..b403a1b5c9ff 100644
@@ -32,6 +32,7 @@ config M68K
 	select HAVE_ARCH_TLB_REMOVE_TABLE if MMU_MOTOROLA
 	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 +1752 -0

1,752 lines not on this page · raw message

Signature · 1 line
2.56.1.703.gf848ed807c-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] m68k, bpf: premature tail-call counter decrement in emit_tail_call() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] m68k, bpf: premature tail-call counter decrement in emit_tail_call()
--

commit 1499906cea3bda86a820cc6d5342bc01d96944c1
Author: Kuan-Wei Chiu <[email protected]>

m68k, bpf: Add initial BPF JIT compiler support

This commit adds an eBPF JIT compiler for m68k targeting m68020+ and MMU ColdFire processors.

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

[ ... ]

Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5 -0
+static void emit_tail_call(const struct bpf_insn *insn, struct jit_ctx *ctx)
+{
+	const s8 *r2 = bpf2m68k[BPF_REG_2];
+	const s8 *r3 = bpf2m68k[BPF_REG_3];
+	int jmp_out_1, jmp_out_2, jmp_out_3;

[ ... ]

Quoted 14 lines
arch/m68k/net/bpf_jit_comp.c new file +14 -0
+	emit_16(ctx, 0x262e);					/* move.l d16(%fp), %d3 */
+	emit_16(ctx, (u16)STACK_OFFSET(BPF_TC_LO));
+	emit_16(ctx, 0x4a83);					/* tst.l %d3 */
+	emit_16(ctx, 0x6700);					/* beq.w jmp_out_2 */
+	jmp_out_2 = ctx->idx;
+	emit_16(ctx, 0);
+
+	emit_16(ctx, 0x5383);					/* subq.l #1, %d3 */
+	emit_16(ctx, 0x2d43);					/* move.l %d3, d16(%fp) */
+	emit_16(ctx, (u16)STACK_OFFSET(BPF_TC_LO));
+
+	emit_16(ctx, 0xd482);					/* add.l %d2, %d2 */
+	emit_16(ctx, 0xd482);					/* add.l %d2, %d2 */
+	emit_16(ctx, 0x43e8);					/* lea d16(%a0), %a1 */

arch/m68k/net/bpf_jit_comp.c line 1177

Quoted 10 lines
arch/m68k/net/bpf_jit_comp.c new file +10 -0
+	emit_16(ctx, offsetof(struct bpf_array, ptrs));
+	emit_16(ctx, 0xd3c2);					/* adda.l %d2, %a1 */
+
+	emit_16(ctx, 0x2051);					/* movea.l (%a1), %a0 */
+
+	emit_16(ctx, 0x2408);					/* move.l %a0, %d2 */
+	emit_16(ctx, 0x4a82);					/* tst.l %d2 */
+	emit_16(ctx, 0x6700);					/* beq.w jmp_out_3 */
+	jmp_out_3 = ctx->idx;
+	emit_16(ctx, 0);

[Severity: Medium]
Should BPF_TC_LO be decremented and stored back to the stack only after checking that the target bpf_prog pointer in (%a1) is non-NULL? As written, attempting a tail call on an empty prog-array slot branches to jmp_out_3 after already decrementing the counter, so failed tail calls to NULL slots consume remaining tail-call attempts and exhaust MAX_TAIL_CALL_CNT early.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Kuan-Wei Chiu
TL;DR: bpf_int_jit_compile() has no check for an already-JITed prog, so jit_subprogs()'s second pass may leak the first executable image, e.g. with an exception_callback tag, letting a CAP_BPF user…
Quoted 1 line

m68k, bpf: Add initial BPF JIT compiler support

TL;DR: bpf_int_jit_compile() has no check for an already-JITed prog, so jit_subprogs()'s second pass may leak the first executable image, e.g. with an exception_callback tag, letting a CAP_BPF user exhaust JIT memory.

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0

[ ... ]

Quoted 10 lines
arch/m68k/net/bpf_jit_comp.c new file +10 -0
+struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+	struct bpf_binary_header *header;
+	struct jit_ctx ctx;
+	unsigned int image_size;
+	u8 *image_ptr;
+
+	if (!prog->jit_requested)
+		return prog;
+

[ ... ]

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3 -0
+	header = bpf_jit_binary_alloc(image_size, &image_ptr, 2, jit_fill_hole);
+	if (!header)
+		goto out_off;

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1745

Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4 -0
+	prog->bpf_func = (void *)ctx.target;
+	prog->jited = 1;
+	prog->jited_len = image_size;
+	goto out_off;

If jit_subprogs() calls bpf_int_jit_compile() a second time on a func[i] that is already JITed, doesn't this allocate a new header and overwrite prog->bpf_func, leaving the first image unfreed?

jit_subprogs() in kernel/bpf/fixups.c calls bpf_int_jit_compile() twice for every subprog, once in the first loop and again in the extra pass:

    old_bpf_func = func[i]->bpf_func;
    tmp = bpf_int_jit_compile(env, func[i]);
    if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
        verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
        err = -ENOTSUPP;
        goto out_free;
    }

On the second call only prog->jit_requested is checked, which is still set, so both passes run again. bpf_jit_binary_alloc() returns a new header and prog->bpf_func is overwritten. The out_free path then calls bpf_jit_free(func[i]), which frees only the header that bpf_jit_binary_hdr(func[i]) finds, i.e. the new one. The first image and its bpf_jit_charge_modmem() charge are never released.

Rejecting BPF_PSEUDO_CALL in emit_call() and BPF_PSEUDO_FUNC in build_insn() doesn't cover every path to the extra pass. A program whose main function has an "exception_callback:" BTF decl tag gets a second subprog via bpf_find_exception_callback_insn_off() -> add_subprog() in kernel/bpf/verifier.c. The main program doesn't need to call that subprog or use bpf_throw(). Both functions JIT successfully in the first loop since they contain no calls, and the extra pass then leaks func[0]'s first image on every load. A CAP_BPF user (needed for BTF) loading such programs repeatedly could leak executable memory without bound and keep growing bpf_jit_current.

The JITs that support bpf-to-bpf calls (x86, arm64, riscv, powerpc, loongarch, parisc, arc) detect the extra pass via prog->jited or jit_data and reuse the image. Would an early return when prog->jited is already set work here? jit_subprogs() would then see the same bpf_func and finish, and that seems safe because any real call or callback has already made the first pass fail.

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

#3 lore raw
Quoted 69 lines · On Tue, Nov 17, 2026 at 10:36:39AM +0000, [email protected] wrote:
Quoted 1 line

m68k, bpf: Add initial BPF JIT compiler support

TL;DR: bpf_int_jit_compile() has no check for an already-JITed prog, so jit_subprogs()'s second pass may leak the first executable image, e.g. with an exception_callback tag, letting a CAP_BPF user exhaust JIT memory.

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +0 -0

[ ... ]

Quoted 10 lines
arch/m68k/net/bpf_jit_comp.c new file +10 -0
+struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
+{
+	struct bpf_binary_header *header;
+	struct jit_ctx ctx;
+	unsigned int image_size;
+	u8 *image_ptr;
+
+	if (!prog->jit_requested)
+		return prog;
+

[ ... ]

Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3 -0
+	header = bpf_jit_binary_alloc(image_size, &image_ptr, 2, jit_fill_hole);
+	if (!header)
+		goto out_off;

[ ... ]

arch/m68k/net/bpf_jit_comp.c line 1745

Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4 -0
+	prog->bpf_func = (void *)ctx.target;
+	prog->jited = 1;
+	prog->jited_len = image_size;
+	goto out_off;

If jit_subprogs() calls bpf_int_jit_compile() a second time on a func[i] that is already JITed, doesn't this allocate a new header and overwrite prog->bpf_func, leaving the first image unfreed?

jit_subprogs() in kernel/bpf/fixups.c calls bpf_int_jit_compile() twice for every subprog, once in the first loop and again in the extra pass:

    old_bpf_func = func[i]->bpf_func;
    tmp = bpf_int_jit_compile(env, func[i]);
    if (tmp != func[i] || func[i]->bpf_func != old_bpf_func) {
        verbose(env, "JIT doesn't support bpf-to-bpf calls\n");
        err = -ENOTSUPP;
        goto out_free;
    }

On the second call only prog->jit_requested is checked, which is still set, so both passes run again. bpf_jit_binary_alloc() returns a new header and prog->bpf_func is overwritten. The out_free path then calls bpf_jit_free(func[i]), which frees only the header that bpf_jit_binary_hdr(func[i]) finds, i.e. the new one. The first image and its bpf_jit_charge_modmem() charge are never released.

Rejecting BPF_PSEUDO_CALL in emit_call() and BPF_PSEUDO_FUNC in build_insn() doesn't cover every path to the extra pass. A program whose main function has an "exception_callback:" BTF decl tag gets a second subprog via bpf_find_exception_callback_insn_off() -> add_subprog() in kernel/bpf/verifier.c. The main program doesn't need to call that subprog or use bpf_throw(). Both functions JIT successfully in the first loop since they contain no calls, and the extra pass then leaks func[0]'s first image on every load. A CAP_BPF user (needed for BTF) loading such programs repeatedly could leak executable memory without bound and keep growing bpf_jit_current.

Quoted 6 lines

The JITs that support bpf-to-bpf calls (x86, arm64, riscv, powerpc, loongarch, parisc, arc) detect the extra pass via prog->jited or jit_data and reuse the image. Would an early return when prog->jited is already set work here? jit_subprogs() would then see the same bpf_func and finish, and that seems safe because any real call or callback has already made the first pass fail.

Since this initial JIT doesn't support subprogs anyway, I'll just reject prog->is_func upfront in bpf_int_jit_compile() in v11.

Regards,
Kuan-Wei