bpf, x86: Add support for BPF_SDIV and BPF_SMOD in the i386 JIT
Subsystems (3)
PATCH v2 1/1 bpf, x86: Add support for BPF_SDIV and BPF_SMOD in the i386 JIT
emit_ia32_div_mod_r() emits an unsigned divide (xor edx,edx + div ecx) for BPF_DIV and BPF_MOD regardless of the instruction's signedness, so the i386 JIT does not implement signed BPF_SDIV and BPF_SMOD (off == 1), which get an unsigned quotient and remainder rather than the verifier's and the interpreter's signed result for negative operands.
Add signed support. Pass the instruction to emit_ia32_div_mod_r() and, on the signed path (off == 1), emit cdq + idiv ecx instead of xor edx,edx + div ecx, mirroring the cdq/idiv-vs-xor/div split the x86_64 JIT uses. bpf_do_misc_fixups() rewrites the zero-divisor and INT_MIN/-1 cases out of the instruction stream before the JIT runs.
The RV32 JIT has the same gap.
---
v2:
- Frame as adding support and drop the Fixes tag, per Pu Lehui [1].
- Pass struct bpf_insn * to emit_ia32_div_mod_r() rather than separate op and is_signed parameters, per Kuan-Wei Chiu.
v1: https://lore.kernel.org/bpf/[email protected]/
[1]: https://lore.kernel.org/bpf/[email protected]/
arch/x86/net/bpf_jit_comp32.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-)
arch/x86/net/bpf_jit_comp32.c +17 -8
@@ -432,11 +432,13 @@ static inline void emit_ia32_to_be_r64(const u8 dst[], s32 val,* ALU operation (32 bit) * dst = dst (div|mod) src */ -static inline void emit_ia32_div_mod_r (const u8 op,const u8 dst,const u8 src, -bool dstk,bool sstk,u8 **pprog) +static inline void emit_ia32_div_mod_r (const struct bpf_insn *insn,const u8 dst, +const u8 src,bool dstk,bool sstk,u8 **pprog) {u8 *prog = *pprog;int cnt =0 ; +const u8 op =BPF_OP (insn->code); +const bool is_signed = (insn->off ==1 );if (sstk)/* mov ecx,dword ptr [ebp+off] */
@@ -454,10 +456,17 @@ static inline void emit_ia32_div_mod_r(const u8 op, const u8 dst, const u8 src,/* mov eax,dst */ EMIT2 (0x8B ,add_2reg (0xC0 , dst,IA32_EAX )); -/* xor edx,edx */ -EMIT2 (0x31 ,add_2reg (0xC0 ,IA32_EDX ,IA32_EDX )); -/* div ecx */ -EMIT2 (0xF7 ,add_1reg (0xF0 ,IA32_ECX )); +if (is_signed) { +/* cdq */ +EMIT1 (0x99 ); +/* idiv ecx */ +EMIT2 (0xF7 ,add_1reg (0xF8 ,IA32_ECX )); + }else { +/* xor edx,edx */ +EMIT2 (0x31 ,add_2reg (0xC0 ,IA32_EDX ,IA32_EDX )); +/* div ecx */ +EMIT2 (0xF7 ,add_1reg (0xF0 ,IA32_ECX )); + }if (op ==BPF_MOD ) {if (dstk)
@@ -1795,14 +1804,14 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image,case BPF_ALU |BPF_MOD |BPF_X :switch (BPF_SRC (code)) {case BPF_X : -emit_ia32_div_mod_r (BPF_OP (code), dst_lo, +emit_ia32_div_mod_r (insn, dst_lo, src_lo, dstk, sstk, &prog);break ;case BPF_K :/* mov ecx,imm32*/ EMIT2_off32 (0xC7 ,add_1reg (0xC0 ,IA32_ECX ), imm32); -emit_ia32_div_mod_r (BPF_OP (code), dst_lo, +emit_ia32_div_mod_r (insn, dst_lo,IA32_ECX , dstk, false, &prog);break ;
base-commit: 9a3a07d06e7d74f4aecc51396c771149336ac55d
Signature · 1 line
2.34.1
[email protected]
AI reviewThis looks like a bug fix for the i386 JIT compiler. The function emit_ia32_div_mod_r() has only supported unsigned division since it was introduced. Should this include:
Quoted 12 lines
emit_ia32_div_mod_r()emits an unsigned divide (xor edx,edx + div ecx) forBPF_DIVandBPF_MODregardless of the instruction's signedness, so the i386 JIT does not implement signedBPF_SDIVandBPF_SMOD(off == 1), which get an unsigned quotient and remainder rather than the verifier's and the interpreter's signed result for negative operands.Add signed support. Pass the instruction to
emit_ia32_div_mod_r()and, on the signed path (off == 1), emit cdq + idiv ecx instead of xor edx,edx + div ecx, mirroring the cdq/idiv-vs-xor/div split thex86_64JIT uses.bpf_do_misc_fixups()rewrites the zero-divisor and INT_MIN/-1 cases out of the instruction stream before the JIT runs.
Quoted 4 lines
The RV32 JIT has the same gap.
Signed-off-by Nicholas Dudar <[email protected]>Assisted-by Claude:claude-opus-4-8
This looks like a bug fix for the i386 JIT compiler. The function emit_ia32_div_mod_r() has only supported unsigned division since it was introduced. Should this include:
---
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/29302361966
Quoted 5 lines · On Tue, Jul 14, 2026 at 03:36:45AM +0000, [email protected] wrote:
This looks like a bug fix for the i386 JIT compiler. The function
emit_ia32_div_mod_r()has only supported unsigned division since it was introduced. Should this include:Fixes 03f5781be2c7 ("bpf, x86_32: add eBPF JIT compiler for ia32")
The i386 JIT does not implement signed BPF_SDIV/BPF_SMOD. Commit ec0e2da95f72 added these instructions and updated the verifier, interpreter, and x86-64 JIT, but not the other arch JITs. This patch adds that support, targeting bpf-next as a feature, so the Fixes tag does not apply.
Commit 03f5781be2c7 predates these instructions.
Nick