m68k, bpf: Add initial BPF JIT compiler support
Subsystems (3)
PATCH v7 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 v7:
- Reject unsupported atomic operations in emit_atomic().
- Size subprog stack frames from stack_depth instead of MAX_BPF_STACK.
- Preserve callee's r2 across BPF_PSEUDO_CALL for aggregate returns.
- Handle BPF_ST | BPF_NOSPEC as a no-op.
- Fix tail call prologue offset on CF.
- Clean up state on extra_pass failure in bpf_int_jit_compile().
.../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 | 1840 +++++++++++++++++ 6 files changed, 1851 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 +1840 -0
1,840 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 1 potential issue(s) to consider: - [High] bpf: m68k JIT triggers page fault by prematurely locking subprogram memory --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] bpf: m68k JIT triggers page fault by prematurely locking subprogram memory --
commit fa231b58c29a2f776715a46043e7efdefca56e74
Author: Kuan-Wei Chiu <[email protected]>
Subject: m68k, bpf: Add initial BPF JIT compiler support
This commit introduces a BPF JIT compiler for the m68k architecture. It generates m68k machine code targeting m68020+ processors and coldfire parts with an mmu.
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+struct bpf_prog *bpf_int_jit_compile (struct bpf_verifier_env *env,struct bpf_prog *prog) +{
[ ... ]
arch/m68k/net/bpf_jit_comp.c line 1822
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5
-0+if (bpf_jit_enable >1 ) +bpf_jit_dump (prog->len, image_size,2 , ctx.target); + +if ((!prog->aux->func || extra_pass) &&bpf_jit_binary_lock_ro (header)) + goto out_free_hdr;
[Severity: High]
Could this condition trigger a kernel page fault when JIT compiling BPF programs with subprograms?
When loading a BPF program with subprograms, the BPF core invokes the JIT compiler twice. In the first pass, prog->aux->func is NULL, but prog->is_func is set to 1. Because this checks !prog->aux->func instead of !prog->is_func, it evaluates to true and calls bpf_jit_binary_lock_ro() during the first pass.
When the second pass runs, the JIT attempts to write the finalized instructions to the already locked, read-only memory buffer, which would trigger a page fault.
Should this check use !prog->is_func instead?
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5
-0+ + prog->bpf_func = (void *)ctx.target; + prog->jited =1 ; + prog->jited_len = image_size; + goto out_off;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewTL;DR: Nit only, not a bug: the ColdFire byte-swap instruction sequences are open-coded repeatedly in emit_to_le16/32/64() in the m68k JIT; suggests small shared helpers, as arm's bpf_jit_32.c does.
Quoted 1 line
m68k, bpf: Add initial BPF JIT compiler support
TL;DR: Nit only, not a bug: the ColdFire byte-swap instruction sequences are open-coded repeatedly in emit_to_le16/32/64() in the m68k JIT; suggests small shared helpers, as arm's bpf_jit_32.c does.
Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +0
-0@@ -0,0 +1,1840 @@
[ ... ]
Quoted 8 lines
arch/m68k/net/bpf_jit_comp.c new file +8
-0+static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */
[ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */
[ ... ]
Quoted 9 lines
arch/m68k/net/bpf_jit_comp.c new file +9
-0+static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ + +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */
arch/m68k/net/bpf_jit_comp.c line 706
Quoted 10 lines
arch/m68k/net/bpf_jit_comp.c new file +10
-0+emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */
[ ... ]
This isn't a bug, but the ColdFire byte-swap sequence (mvz.w / mvz.b / lsr.l #8 / lsl.l #8 / or.l) is open-coded once in emit_to_le16(), twice in emit_to_le32() and four times in emit_to_le64(), differing only in which register receives the result. The ror.w #8 / swap / ror.w #8 sequence is likewise repeated in emit_to_le32() and twice in emit_to_le64().
Could these go into small helpers, similar to emit_rev16() and emit_rev32() in arch/arm/net/bpf_jit_32.c, so that emit_to_le32() and emit_to_le64() reuse them instead of repeating the instructions?
---
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/37360793550
Why thank you LLM review :-)
Quoted 63 lines · On 6/10/26 05:48, [email protected] wrote:
Quoted 1 line
m68k, bpf: Add initial BPF JIT compiler support
TL;DR: Nit only, not a bug: the ColdFire byte-swap instruction sequences are open-coded repeatedly in emit_to_le16/32/64() in the m68k JIT; suggests small shared helpers, as arm's
bpf_jit_32.cdoes.Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +0
-0@@ -0,0 +1,1840 @@[ ... ]
Quoted 8 lines
arch/m68k/net/bpf_jit_comp.c new file +8
-0+static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ [ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ [ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ + +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ [ ... ]
Quoted 4 lines
This isn't a bug, but the ColdFire byte-swap sequence (mvz.w / mvz.b / lsr.l #8 / lsl.l #8 / or.l) is open-coded once in
emit_to_le16(), twice inemit_to_le32()and four times inemit_to_le64(), differing only in which register receives the result.
I did consider using helpers for these during coding, but ultimately decided against it. The open coded versions allow for a handful of extra instruction optimizations - due to that flexibility of producing the 16bit swap sequence result in different registers. For example I could combine a stack pop with the or'ing of the result, and more efficiently store an intermediate result into the temporary stack storage.
The patch below gives an example of an implementation using helpers. I am not tied to the open coded version: Kuan-Wei if you prefer the code with helpers feel free to use this instead.
FWIW, the to_le32 coded sequence is 1 instruction longer (17 instructions to 18). The to_le64 coded sequence is 5 instructions longer (34 instructions to 39). Total byte count differs less, due to use of offsets in the open coded versions.
Regards
Greg
m68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +27 -57
@@ -640,14 +640,19 @@bpf_put_reg32 (dst[0 ], d_hi, ctx); } +static inline void emit_cf_swap16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +} +static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_cf_swap16 (ctx, d_lo, d_hi); }else {emit_16 (ctx,0x0280 | d_lo);/* andi.l #0xffff, d_lo */ emit_32 (ctx,0xffff );
@@ -657,25 +662,23 @@emit_16 (ctx,0x7000 | (d_hi <<9 ));/* moveq #0, d_hi */ } +static inline void emit_cf_swap32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_cf_swap16 (ctx, d_lo, d_hi); +emit_16 (ctx,0x2000 | (d_lo <<9 ) | d_hi);/* move.l d_lo, d_hi */ +emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_cf_swap16 (ctx, d_lo, d_hi); +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ +} +static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ -emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ -emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ +emit_cf_swap32 (ctx, d_lo, d_hi); }else {emit_16 (ctx,0xe058 | d_lo);/* ror.w #8, d_lo */ emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */
@@ -688,45 +691,12 @@static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ - -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ -emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ -emit_16 (ctx,0x0004 ); -emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ -emit_16 (ctx,0x0004 ); -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x81af | (d_lo <<9 ));/* or.l d_lo, 4(%sp) */ -emit_16 (ctx,0x0004 ); - -emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_cf_swap32 (ctx, d_lo, d_hi); +emit_16 (ctx,0x2000 | (d_lo <<9 ) | d_hi);/* move.l d_lo, d_hi */ emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ - +emit_cf_swap32 (ctx, d_lo, d_hi);emit_16 (ctx,0x201f | (d_hi <<9 ));/* move.l (%sp)+, d_hi */ }else {emit_16 (ctx,0xe058 | d_lo);/* ror.w #8, d_lo */
Quoted 78 lines · Greg Ungerer
Why thank you LLM review :-)
Quoted 63 lines · On 6/10/26 05:48, [email protected] wrote:
Quoted 1 line
m68k, bpf: Add initial BPF JIT compiler support
TL;DR: Nit only, not a bug: the ColdFire byte-swap instruction sequences are open-coded repeatedly in emit_to_le16/32/64() in the m68k JIT; suggests small shared helpers, as arm's
bpf_jit_32.cdoes.Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +0
-0@@ -0,0 +1,1840 @@[ ... ]
Quoted 8 lines
arch/m68k/net/bpf_jit_comp.c new file +8
-0+static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ [ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ [ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ + +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ [ ... ]
Quoted 4 lines
This isn't a bug, but the ColdFire byte-swap sequence (mvz.w / mvz.b / lsr.l #8 / lsl.l #8 / or.l) is open-coded once in
emit_to_le16(), twice inemit_to_le32()and four times inemit_to_le64(), differing only in which register receives the result.I did consider using helpers for these during coding, but ultimately decided against it. The open coded versions allow for a handful of extra instruction optimizations - due to that flexibility of producing the 16bit swap sequence result in different registers. For example I could combine a stack pop with the or'ing of the result, and more efficiently store an intermediate result into the temporary stack storage.
Quoted 3 lines
The patch below gives an example of an implementation using helpers. I am not tied to the open coded version: Kuan-Wei if you prefer the code with helpers feel free to use this instead.
I don't have a strong opinion either way, since it sounds like you already made a deliberate judgment call when writing it. Maybe I can switch to the helper version for now to make this already large patch a bit smaller. If anyone cares or if it ever shows up in benchmarks in the future, we can always go back to the open-coded version to optimize it.
Quoted 38 lines
FWIW, the
to_le32coded sequence is 1 instruction longer (17 instructions to 18). Theto_le64coded sequence is 5 instructions longer (34 instructions to 39). Total byte count differs less, due to use of offsets in the open coded versions.Regards
Gregm68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +10
-5@@ -640,14 +640,19 @@bpf_put_reg32 (dst[0 ], d_hi, ctx); } +static inline void emit_cf_swap16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +} +static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_cf_swap16 (ctx, d_lo, d_hi); }else {emit_16 (ctx,0x0280 | d_lo);/* andi.l #0xffff, d_lo */ emit_32 (ctx,0xffff );
m68k/net/bpf_jit_comp.c line 670
Quoted 9 lines
m68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +6
-0@@ -657,25 +662,23 @@emit_16 (ctx,0x7000 | (d_hi <<9 ));/* moveq #0, d_hi */ } +static inline void emit_cf_swap32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_cf_swap16 (ctx, d_lo, d_hi); +emit_16 (ctx,0x2000 | (d_lo <<9 ) | d_hi);/* move.l d_lo, d_hi */
Should be emit_16(ctx, 0x2000 | (d_hi << 9) | d_lo) ?
Regards,
Kuan-Wei
Quoted 83 lines
m68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +11
-52+emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_cf_swap16 (ctx, d_lo, d_hi); +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ +} +static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ -emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ -emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ +emit_cf_swap32 (ctx, d_lo, d_hi); }else {emit_16 (ctx,0xe058 | d_lo);/* ror.w #8, d_lo */ emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ @@ -688,45 +691,12 @@static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ - -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ -emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ -emit_16 (ctx,0x0004 ); -emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ -emit_16 (ctx,0x0004 ); -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x81af | (d_lo <<9 ));/* or.l d_lo, 4(%sp) */ -emit_16 (ctx,0x0004 ); - -emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_cf_swap32 (ctx, d_lo, d_hi); +emit_16 (ctx,0x2000 | (d_lo <<9 ) | d_hi);/* move.l d_lo, d_hi */ emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ - +emit_cf_swap32 (ctx, d_lo, d_hi);emit_16 (ctx,0x201f | (d_hi <<9 ));/* move.l (%sp)+, d_hi */ }else {emit_16 (ctx,0xe058 | d_lo);/* ror.w #8, d_lo */
Quoted 3 lines · Kuan-Wei Chiu
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]
test_bpf.ko doesn't go through the verifier.
It has no bpf-to-bpf calls and no callbacks.
Quoted 4 lines
Changes in v7:
- Size subprog stack frames fromstack_depthinstead ofMAX_BPF_STACK.
- Preserve callee's r2 acrossBPF_PSEUDO_CALLfor aggregate returns.
- Clean up state onextra_passfailure inbpf_int_jit_compile().
So these are fixes for bot findings in the code that was never run. It doesn't work. See below.
[...]
arch/m68k/net/bpf_jit_comp.c line 1221
Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2
-0+emit_16 (ctx,0x4ee8 );/* jmp d16(%a0) */ +emit_16 (ctx,CPU_IS_COLDFIRE ?48 :44 );
When the target prog has subprogs its prologue is the is_func flavor. It's 110 bytes, and 44 is in the middle of
move.l %d0, BPF_R2_LO(%fp). The cpu executes 0xfff0.
arch/m68k/net/bpf_jit_comp.c line 1446
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 = ctx->prog->is_func ? +round_up (ctx->prog->aux->stack_depth,4 ) :MAX_BPF_STACK ;
stack_depth can be more than MAX_BPF_STACK.may_goto adds 8 bytes on top of what the prog uses.
See stack_depth_extra in bpf_do_misc_fixups().
When the prog uses 512 bytes the counter is at r10 - 520. That's where movem.l saved %d4 and %d5, so the C caller of the prog gets them back corrupted.
The target of a tail call reuses the frame, so every frame needs that extra space.
arch/m68k/net/bpf_jit_comp.c line 1472
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5
-0+if (ctx->prog->is_func) { +const s8 arg_regs[] = {BPF_REG_1 ,BPF_REG_2 ,BPF_REG_3 ,BPF_REG_4 ,BPF_REG_5 }; + +for (i =0 ; i <5 ; i++) { +int offset =8 + i *8 ;
jit_subprogs() sets is_func for func[0] too, which is the main prog. It's called from C as bpf_func(ctx, insnsi), so 8(%fp) is ctx and 12(%fp) is insnsi. This loop makes R1 = (u64)ctx << 32 | insnsi. Every prog that has a subprog or a callback reads its ctx out of prog->insnsi.
arm32 and x86-32 JITs don't support bpf-to-bpf calls. Drop them from this patch.
Add them in a follow up when you can run test_progs.
pw-bot: cr
Quoted 51 lines · Alexei Starovoitov
Quoted 3 lines · Kuan-Wei Chiu
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]test_bpf.ko doesn't go through the verifier.
It has no bpf-to-bpf calls and no callbacks.Quoted 4 lines
Changes in v7:
- Size subprog stack frames fromstack_depthinstead ofMAX_BPF_STACK.
- Preserve callee's r2 acrossBPF_PSEUDO_CALLfor aggregate returns.
- Clean up state onextra_passfailure inbpf_int_jit_compile().So these are fixes for bot findings in the code that was never run. It doesn't work. See below.
[...]
arch/m68k/net/bpf_jit_comp.c line 1221
Quoted 2 lines
arch/m68k/net/bpf_jit_comp.c new file +2
-0+emit_16 (ctx,0x4ee8 );/* jmp d16(%a0) */ +emit_16 (ctx,CPU_IS_COLDFIRE ?48 :44 );When the target prog has subprogs its prologue is the
is_funcflavor. It's 110 bytes, and 44 is in the middle of
move.l %d0,BPF_R2_LO(%fp). The cpu executes0xfff0.arch/m68k/net/bpf_jit_comp.c line 1446
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 = ctx->prog->is_func ? +round_up (ctx->prog->aux->stack_depth,4 ) :MAX_BPF_STACK ;
stack_depthcan be more thanMAX_BPF_STACK.may_gotoadds 8 bytes on top of what the prog uses.
Seestack_depth_extrainbpf_do_misc_fixups().
When the prog uses 512 bytes the counter is at r10 - 520. That's where movem.l saved %d4 and %d5, so the C caller of the prog gets them back corrupted.
The target of a tail call reuses the frame, so every frame needs that extra space.arch/m68k/net/bpf_jit_comp.c line 1472
Quoted 5 lines
arch/m68k/net/bpf_jit_comp.c new file +5
-0+if (ctx->prog->is_func) { +const s8 arg_regs[] = {BPF_REG_1 ,BPF_REG_2 ,BPF_REG_3 ,BPF_REG_4 ,BPF_REG_5 }; + +for (i =0 ; i <5 ; i++) { +int offset =8 + i *8 ;
jit_subprogs()setsis_funcfor func[0] too, which is the main prog. It's called from C asbpf_func(ctx, insnsi), so 8(%fp) is ctx and 12(%fp) is insnsi. This loop makes R1 = (u64)ctx << 32 | insnsi. Every prog that has a subprog or a callback reads its ctx out ofprog->insnsi.
Quoted 3 lines
arm32 and x86-32 JITs don't support bpf-to-bpf calls. Drop them from this patch.
Add them in a follow up when you can runtest_progs.
Thanks for the review and the detailed explaination.
My initial patch didn't include subprog support, but I endedd up trying to deal with it after the bots complained about it.
I'll reject BPF_PSEUDO_CALL, and reserve the extra 8 bytes on the stack.
Regards,
Kuan-Wei
Hi Kuan-Wei,
Quoted 131 lines · Kuan-Wei Chiu
Quoted 78 lines · Greg Ungerer
Why thank you LLM review :-)
Quoted 63 lines · On 6/10/26 05:48, [email protected] wrote:
Quoted 1 line
m68k, bpf: Add initial BPF JIT compiler support
TL;DR: Nit only, not a bug: the ColdFire byte-swap instruction sequences are open-coded repeatedly in emit_to_le16/32/64() in the m68k JIT; suggests small shared helpers, as arm's
bpf_jit_32.cdoes.Quoted 6 lines
arch/m68k/net/bpf_jit_comp.c new file +0
-0@@ -0,0 +1,1840 @@[ ... ]
Quoted 8 lines
arch/m68k/net/bpf_jit_comp.c new file +8
-0+static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ [ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ [ ... ]
Quoted 19 lines
arch/m68k/net/bpf_jit_comp.c new file +19
-0+static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +if (CPU_IS_COLDFIRE ) { +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ + +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ +emit_16 (ctx,0x0004 ); +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ [ ... ]
Quoted 4 lines
This isn't a bug, but the ColdFire byte-swap sequence (mvz.w / mvz.b / lsr.l #8 / lsl.l #8 / or.l) is open-coded once in
emit_to_le16(), twice inemit_to_le32()and four times inemit_to_le64(), differing only in which register receives the result.I did consider using helpers for these during coding, but ultimately decided against it. The open coded versions allow for a handful of extra instruction optimizations - due to that flexibility of producing the 16bit swap sequence result in different registers. For example I could combine a stack pop with the or'ing of the result, and more efficiently store an intermediate result into the temporary stack storage.
Quoted 3 lines
The patch below gives an example of an implementation using helpers. I am not tied to the open coded version: Kuan-Wei if you prefer the code with helpers feel free to use this instead.
I don't have a strong opinion either way, since it sounds like you already made a deliberate judgment call when writing it. Maybe I can switch to the helper version for now to make this already large patch a bit smaller. If anyone cares or if it ever shows up in benchmarks in the future, we can always go back to the open-coded version to optimize it.
Quoted 41 lines
FWIW, the
to_le32coded sequence is 1 instruction longer (17 instructions to 18). Theto_le64coded sequence is 5 instructions longer (34 instructions to 39). Total byte count differs less, due to use of offsets in the open coded versions.Regards
Gregm68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +10
-5@@ -640,14 +640,19 @@bpf_put_reg32 (dst[0 ], d_hi, ctx); } +static inline void emit_cf_swap16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ +emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ +emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ +emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ +emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +} +static inline void emit_to_le16 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ +emit_cf_swap16 (ctx, d_lo, d_hi); }else {emit_16 (ctx,0x0280 | d_lo);/* andi.l #0xffff, d_lo */ emit_32 (ctx,0xffff );@@ -657,25 +662,23 @@emit_16 (ctx,0x7000 | (d_hi <<9 ));/* moveq #0, d_hi */ }
Quoted 8 lines
m68k/net/bpf_jit_comp.c line 670
Quoted 6 lines
m68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +6
-0+static inline void emit_cf_swap32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) +{ +emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_cf_swap16 (ctx, d_lo, d_hi); +emit_16 (ctx,0x2000 | (d_lo <<9 ) | d_hi);/* move.l d_lo, d_hi */ Should be
emit_16(ctx, 0x2000 | (d_hi << 9) | d_lo)?
Sorry, yes, you are right.
Sloppy testing on my part, I forgot to enable bpf_jit_enable before loading and running the test_bpf module.
I can see you fixed it in v9, so all good now.
Regards
Greg
Quoted 86 lines
Regards,
Kuan-WeiQuoted 83 lines
m68k/net/bpf_jit_comp.c renamed from m68k/net/bpf_jit_comp.c.org +11
-52+emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ +emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ +emit_cf_swap16 (ctx, d_lo, d_hi); +emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ +emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ +} +static inline void emit_to_le32 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ -emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ -emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ +emit_cf_swap32 (ctx, d_lo, d_hi); }else {emit_16 (ctx,0xe058 | d_lo);/* ror.w #8, d_lo */ emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ @@ -688,45 +691,12 @@static inline void emit_to_le64 (struct jit_ctx *ctx,s8 d_lo,s8 d_hi) {if (CPU_IS_COLDFIRE ) { -emit_16 (ctx,0x2f00 | d_lo);/* move.l d_lo, -(%sp) */ emit_16 (ctx,0x2f00 | d_hi);/* move.l d_hi, -(%sp) */ - -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ -emit_16 (ctx,0x202f | (d_lo <<9 ));/* move.l 4(%sp), d_lo */ -emit_16 (ctx,0x0004 ); -emit_16 (ctx,0x2f40 | d_hi);/* move.l d_hi, 4(%sp) */ -emit_16 (ctx,0x0004 ); -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x81af | (d_lo <<9 ));/* or.l d_lo, 4(%sp) */ -emit_16 (ctx,0x0004 ); - -emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_hi <<9 ) | d_lo);/* or.l d_lo, d_hi */ +emit_cf_swap32 (ctx, d_lo, d_hi); +emit_16 (ctx,0x2000 | (d_lo <<9 ) | d_hi);/* move.l d_lo, d_hi */ emit_16 (ctx,0x2017 | (d_lo <<9 ));/* move.l (%sp), d_lo */ emit_16 (ctx,0x2e80 | d_hi);/* move.l d_hi, (%sp) */ -emit_16 (ctx,0x71c0 | (d_lo <<9 ) | d_lo);/* mvz.w d_lo, d_lo */ -emit_16 (ctx,0x7180 | (d_hi <<9 ) | d_lo);/* mvz.b d_lo, d_hi */ -emit_16 (ctx,0xe088 | d_lo);/* lsr.l #8, d_lo */ -emit_16 (ctx,0xe188 | d_hi);/* lsl.l #8, d_hi */ -emit_16 (ctx,0x8080 | (d_lo <<9 ) | d_hi);/* or.l d_hi, d_lo */ -emit_16 (ctx,0x4840 | d_lo);/* swap d_lo */ -emit_16 (ctx,0x809f | (d_lo <<9 ));/* or.l (%sp)+, d_lo */ - +emit_cf_swap32 (ctx, d_lo, d_hi);emit_16 (ctx,0x201f | (d_hi <<9 ));/* move.l (%sp)+, d_hi */ }else {emit_16 (ctx,0xe058 | d_lo);/* ror.w #8, d_lo */