m68k, bpf: Add initial BPF JIT compiler support
Subsystems (3)
PATCH v6 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 v6:
- Fix kconfig sort order for HAVE_EBPF_JIT.
- Update eBPF-JIT/arch-support.txt
.../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 | 1811 +++++++++++++++++ 6 files changed, 1822 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
@@ -13,7 +13,7 @@ | csky: | TODO | | hexagon: | TODO | | loongarch: | ok | - | m68k: | TODO | + | m68k: | ok | | microblaze: | TODO | | mips: | ok | | nios2: | TODO |
MAINTAINERS +6 -0
@@ -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
@@ -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
@@ -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
@@ -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 +1811 -0
1,811 lines not on this page · raw message
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] m68k/bpf: Hardcoded prologue size in tail call causes offset mismatch on ColdFire - [High]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] m68k/bpf: Hardcoded prologue size in tail call causes offset mismatch on ColdFire
- [High] m68k/bpf: Silent JIT failure in extra_pass allows execution of incompletely JITed programs --
commit 2847ebf4b1948509dbfb51929065f4ab0a56fcf6
Author: Kuan-Wei Chiu <[email protected]>
m68k, bpf: Add initial BPF JIT compiler support
This commit adds a BPF JIT compiler for the m68k architecture. It generates machine code targeting m68020+ and coldfire processors with an MMU, while excluding plain 68000/68010 and non-MMU coldfire processors.
Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +0
-0
[ ... ]
Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2
-0+static void emit_tail_call (const struct bpf_insn *insn,struct jit_ctx *ctx) +{
[ ... ]
arch/m68k/net/bpf_jit_comp.c line 1203
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5
-0+emit_16 (ctx,0x2068 );/* movea.l d16(%a0), %a0 */ +emit_16 (ctx, offsetof(struct bpf_prog , bpf_func)); + +emit_16 (ctx,0x4ee8 );/* jmp d16(%a0) */ +emit_16 (ctx,44 );
[Severity: High]
Does this hardcoded displacement of 44 bytes account for the ColdFire prologue?
In build_prologue(), the ColdFire prologue takes 48 bytes due to an expanded register saving sequence:
if (CPU_IS_COLDFIRE) {
emit_16(ctx, 0x4fef); /* lea -16(%sp), %sp */
emit_16(ctx, 0xfff0);
emit_16(ctx, 0x48d7); /* movem.l d2-d5, (%sp) */
emit_16(ctx, 0x003c);
} else {If a BPF program executes a tail call on a ColdFire processor, the tail call will jump to byte 44, unintentionally executing the last 4 bytes of the prologue. This writes an uncontrolled %d0 value into BPF_R10_HI, corrupting it.
Since BPF programs can be loaded by unprivileged users, can this bypass verifier bounds tracking and lead to arbitrary memory access if the program performs 64-bit pointer arithmetic on R10?
[ ... ]
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; +bool extra_pass = prog->jited; + +if (!prog->jit_requested) +return prog;
arch/m68k/net/bpf_jit_comp.c line 1757
Quoted 10 lines
arch/m68k/net/bpf_jit_comp.c new file +10
-0+ +if (prog->jited && !prog->aux->func) +return prog; + +memset (&ctx,0 ,sizeof (ctx)); + ctx.prog = prog; + + ctx.offsets =kvcalloc (prog->len +1 ,sizeof (int ),GFP_KERNEL ); +if (!ctx.offsets) +return prog;
[Severity: High]
Can a failure to allocate ctx.offsets during an extra_pass lead to the execution of an incompletely JITed program?
If kvcalloc fails during the extra_pass in bpf_int_jit_compile(), it returns prog directly without clearing prog->bpf_func. Because prog->bpf_func remains unchanged from the first pass, jit_subprogs() assumes the JIT compilation succeeded.
The BPF program could then be executed with a JIT image containing unresolved subprogram call addresses, which resolve to a 0 address, resulting in a kernel panic when executed.
Since unprivileged users can trigger this by loading BPF programs during memory pressure, can this cause a denial of service?
arch/m68k/net/bpf_jit_comp.c line 1761
Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4
-0+ +build_prologue (&ctx); +if (build_body (&ctx) <0 ) + goto out_off;
[Severity: High]
Similarly, if the dry run of build_body() fails, it jumps to out_off which frees ctx.offsets and returns prog without clearing the BPF function pointer. Can this also hide the failure from jit_subprogs() when loading a BPF program with subprograms?
Quoted 1 line
arch/m68k/net/bpf_jit_comp.c new file +1
-0+build_epilogue (&ctx);
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewCan this code handle 32-bit BPF_LOAD_ACQ and BPF_STORE_REL atomics?
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +0
-0
[ ... ]
Quoted 32 lines
arch/m68k/net/bpf_jit_comp.c new file +32
-0+static int jit_atomic32 (u32 *ptr,u32 val,int op,u32 cmp) +{ +unsigned long flags; +u32 old; + +local_irq_save (flags); + old = *ptr; +switch (op) { +case BPF_ADD : + *ptr = old + val; +break ; +case BPF_AND : + *ptr = old & val; +break ; +case BPF_OR : + *ptr = old | val; +break ; +case BPF_XOR : + *ptr = old ^ val; +break ; +case BPF_XCHG : + *ptr = val; +break ; +case BPF_CMPXCHG : +if (old == cmp) + *ptr = val; +break ; + } +local_irq_restore (flags); + +return old; +}
[ ... ]
Quoted 33 lines
arch/m68k/net/bpf_jit_comp.c new file +33
-0+static void emit_atomic (const struct bpf_insn *insn,struct jit_ctx *ctx) +{ +const s8 *src =bpf_get_reg64_ref (insn->src_reg, tmp2, ctx); +const s8 *dst =bpf_get_reg64_ref (insn->dst_reg, tmp1, ctx); + +if (BPF_SIZE (insn->code) ==BPF_W ) { +s8 s_lo =bpf_get_reg32 (src[1 ], tmp2[1 ], ctx); + +if (insn->imm !=BPF_CMPXCHG ) { +if (is_stacked (dst[1 ])) { +emit_16 (ctx,0x206e );/* movea.l d16(%fp), %a0 */ +emit_16 (ctx, (u16 )STACK_OFFSET (dst[1 ])); + }else { +emit_16 (ctx,0x2040 | dst[1 ]);/* movea.l dst, %a0 */ + } + +emit_16 (ctx,0xd1fc );/* adda.l #imm, %a0 */ +emit_16 (ctx, (u16)((s16 )insn->off >>16 )); +emit_16 (ctx, (u16 )insn->off); + +if (BPF_OP (insn->code) ==BPF_XCHG ) { +emit_16 (ctx,0x4eb9 ); +emit_32 (ctx, (u32 )jit_atomic32); + }else { +emit_16 (ctx,0x2f00 | s_lo); +emit_16 (ctx,0x2f3c ); +emit_32 (ctx, insn->imm); +emit_16 (ctx,0x2f08 ); +emit_16 (ctx,0x4eb9 ); +emit_32 (ctx, (u32 )jit_atomic32); +emit_16 (ctx,0x4fef ); +emit_16 (ctx,12 ); + }
[ ... ]
arch/m68k/net/bpf_jit_comp.c line 1628
Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4
-0+case BPF_STX |BPF_ATOMIC |BPF_W : +case BPF_STX |BPF_ATOMIC |BPF_DW : +emit_atomic (insn, ctx); +break ;
Can this code handle 32-bit BPF_LOAD_ACQ and BPF_STORE_REL atomics?
Looking at build_insn(), every BPF_STX | BPF_ATOMIC | BPF_W instruction is sent to emit_atomic() without checking insn->imm.
On 32-bit arches, the verifier accepts BPF_W-sized BPF_LOAD_ACQ (0x100) and BPF_STORE_REL (0x110). In check_atomic() from kernel/bpf/verifier.c, these operations are only rejected when BPF_SIZE is BPF_DW and BITS_PER_LONG != 64:
case BPF_LOAD_ACQ:
if (BPF_SIZE(insn->code) == BPF_DW && BITS_PER_LONG != 64) {
...
return -EOPNOTSUPP;
}
return check_atomic_load(env, insn);For BPF_LOAD_ACQ, emit_atomic() loads %a0 from dst_reg + off and calls jit_atomic32(ptr = dst_reg + off, val = src, op = 0x100, cmp). The switch in jit_atomic32() has no case for 0x100, so it only runs old = *ptr; on dst_reg + off.
But dst_reg is the destination register for a load. check_load_mem() in the verifier only checks dst_reg with DST_OP_NO_MARK, so it can hold any scalar or be uninitialized. The resulting dereference of dst_reg + off (instead of src_reg + off) can read from an arbitrary address with no exception-table entry.
Also, is_fetch is false (0x100 & BPF_FETCH == 0), so dst_reg is never written back. The verifier tracks dst_reg as holding the loaded value, but at runtime it keeps its old value.
For BPF_STORE_REL, jit_atomic32() is called with op = 0x110. No case matches, so the store is silently dropped.
Other 32-bit JITs (mips32, x86_32, riscv32, parisc32) reject unsupported atomic imm values. Could emit_atomic() or build_insn() accept only the imm values that jit_atomic32/jit_atomic64 implement and return -EOPNOTSUPP for the rest?
[ ... ]
arch/m68k/net/bpf_jit_comp.c line 1423
Quoted 4 lines
arch/m68k/net/bpf_jit_comp.c new file +4
-0+static void build_prologue (struct jit_ctx *ctx) +{ +int bpf_stack =MAX_BPF_STACK ; +int total_stack =SCRATCH_SIZE + bpf_stack;
Can this stack reservation overflow the kernel stack?
Every function reserves SCRATCH_SIZE + MAX_BPF_STACK = 96 + 512 = 608 bytes, plus 16 bytes for d2-d5, 8 bytes for the return address and saved %fp, and emit_call() pushes 40 bytes of arguments for each bpf-to-bpf call.
That's 632 bytes for the main frame and 672 bytes for each nested subprog frame, independent of prog->aux->stack_depth.
The verifier allows MAX_CALL_FRAMES (16) nested frames. For CAP_BPF programs, it rounds each frame's stack_depth up to 16, so subprogs with no stack use cost nothing against the 512-byte combined limit.
A program with a chain of 16 small static subprogs is therefore accepted and uses 632 + 15 * 672 = 10712 bytes of kernel stack on top of the caller's stack.
On m68k, THREAD_SIZE is 8KB for every configuration where this JIT is enabled, thread_info sits at the base of the stack, and there is no guard page. Even 12 levels (632 + 11 * 672 = 8024 bytes) overflow by themselves.
Shallower nesting can overflow when a program runs in softirq on top of an already deep stack. Callbacks invoked from helpers such as bpf_loop add a C frame plus another full JIT frame per level.
The main entry needs MAX_BPF_STACK because a tail-call target reuses its frame. Subprogs can never be tail-call targets, and they cannot issue tail calls here because bpf_jit_supports_subprog_tailcalls() is the weak false default.
Could subprog frames be sized from the actual stack_depth instead of always reserving MAX_BPF_STACK?
[ ... ]
arch/m68k/net/bpf_jit_comp.c line 1683
Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2
-0+case BPF_JMP |BPF_CALL : +return emit_call (insn, ctx);
Does this code copy back the callee's R2 for aggregate returns?
For a subprog whose BTF return type is 9-16 bytes, bpf_compute_subprog_ret_regs() in kernel/bpf/verifier.c sets subprog_info->ret_reg_pair and prog->jit_required.
On exit, prepare_func_exit() copies the callee's R2 state into the caller, including pointer types for static subprogs.
In this JIT, BPF R2 is not a machine register. It is a slot at STACK_OFFSET() from %fp, so each frame has its own copy.
The callee writes R2 into its own frame, BPF_EXIT runs build_epilogue(), and unlk discards that frame. emit_call() only fixes up d0/d1 for R0 and never copies R2 back.
The caller's R2 slot keeps its old value (for example the argument it passed in).
This creates a verifier/runtime mismatch. For example, after 'r2 = 0xdeadbeef; call sub' where sub returns {r0, r2 = map_value_ptr}, the verifier thinks r2 is a PTR_TO_MAP_VALUE in '*(u64 *)(r2 + 0) = x', but at run time the store goes to 0xdeadbeef.
Because jit_required is set, there is no interpreter fallback.
Every other JIT that supports BPF_PSEUDO_CALL keeps R2 in a hardware register, and arm32/x86_32 reject subprog calls.
Could the callee's R2 be copied into the caller's R2 slot after a BPF_PSEUDO_CALL, for example by returning it in d2/d3 or through a scratch location?
[ ... ]
arch/m68k/net/bpf_jit_comp.c line 1691
Quoted 3 lines
arch/m68k/net/bpf_jit_comp.c new file +3
-0+default : +return -EOPNOTSUPP ; + }
Does this code handle the BPF_ST | BPF_NOSPEC instruction?
build_insn() has no 'case BPF_ST | BPF_NOSPEC:', so the speculation barrier the verifier inserts falls to the default and returns -EOPNOTSUPP.
m68k does not override bpf_jit_bypass_spec_v1() or v4(), which default to false.
For any program loaded without CAP_PERFMON while mitigations are on (unprivileged, CAP_BPF-only, or through a BPF token), the verifier emits BPF_ST_NOSPEC() for speculative-path sanitation and after stack spills, in kernel/bpf/fixups.c:958-962 and 1035-1042.
build_body() then fails and the whole program is not JITed. With CONFIG_BPF_JIT_ALWAYS_ON, which this patch makes selectable by adding HAVE_EBPF_JIT, and whenever prog->jit_required is set, the load fails with -ENOTSUPP instead of falling back to the interpreter.
test_bpf.ko runs as root and so never hits this.
Every other eBPF JIT, including arm32, mips32 and x86_32, handles BPF_ST | BPF_NOSPEC as a no-op or a barrier.
Could this case be added, or should bpf_jit_bypass_spec_v1/v4() be overridden if m68k does not speculate?
---
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/36350420232
Geert Uytterhoeven
maintainerHi Kuwan-Wei,
Quoted 11 lines · Kuan-Wei Chiu
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]
Quoted 8 lines
Tested-by Daniel Palmer <[email protected]>Co-developed-by Greg Ungerer <[email protected]>Signed-off-by Greg Ungerer <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>---
Changes in v6:
- Fix kconfig sort order forHAVE_EBPF_JIT.
- UpdateeBPF-JIT/arch-support.txt
Thanks for the update!
Are any of the issues raised by the bpf-ci bot valid? Thanks!
Gr{oetje,eeting}s,
Geert
Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected] In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
Hi Geert,
Quoted 23 lines · Geert Uytterhoeven
Hi Kuwan-Wei,
Quoted 11 lines · Kuan-Wei Chiu
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]Quoted 8 lines
Tested-by Daniel Palmer <[email protected]>Co-developed-by Greg Ungerer <[email protected]>Signed-off-by Greg Ungerer <[email protected]>Signed-off-by Kuan-Wei Chiu <[email protected]>---
Changes in v6:
- Fix kconfig sort order forHAVE_EBPF_JIT.
- UpdateeBPF-JIT/arch-support.txt
Quoted 5 lines
Thanks for the update!
Are any of the issues raised by the bpf-ci bot valid? Thanks!
Yeah, I'll address those and send out v7 shortly.
Regards,
Kuan-Wei