From nobody Fri Oct 2 14:03:06 2026 Received: from mail-yw1-f178.google.com (mail-yw1-f178.google.com [209.85.128.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B22B25A645 for ; Fri, 31 Jul 2026 00:30:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785457803; cv=none; b=jqn9tobbfPBAkIGe4d2VFMrK+5eEB1sfQbKmd/FuxR6n82CXmyDE8h4Eu5MtL/7x7Nyn0jvnZmBxeD3rgRSY5oAA0AOKVjqIOzCC6WO+TpTOBGR5bixneAYm+v0bSyzUfyMsj1uutJe5f2Cz6irH2iR7b7qROskgbDtIaUjpyO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785457803; c=relaxed/simple; bh=mSA3vRjYQjkXAyBy0gklxrbC1mTx7y/m2jr4N1Je8Os=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=io10VDSFAXGYiagZTUX1k/QR9LsDc1uyoW+Bt7++0vel597pRiApsvu33jajzrsa/NucF8d5vJ5wx1w5EFwvGtt6TRbv1YioAdO9coL7PLFyuPq0m4UQ6R2o/IU6o3iUh3YliB04km92h6l35mR+CuBDNj67R7CcaNpgOinrahQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EwxqFIMF; arc=none smtp.client-ip=209.85.128.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EwxqFIMF" Received: by mail-yw1-f178.google.com with SMTP id 00721157ae682-81f52945098so5272557b3.0 for ; Thu, 30 Jul 2026 17:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785457801; x=1786062601; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=QRMRalxGkRReFOWmTtsSnVADD1z6LkTNb+5taBWJoZU=; b=EwxqFIMFvVUmFVdYV5X440E85cyFAcPSUY2+eXVBkCulCL2ViBocSBzLREXA+liC85 kMsCV8YkNLf08UP7KXFtZae6Yr+Cpk5Auq+Ena8CqScIlF7bOgLTpO9p02dNWG4zgnKD Q1AP/OzJu2USsiwRmkL7vwxqrsv7C5b+b52D9Xbb6ccxrBZyUUuuym+CGiAJBZZi7uCw 4zdyRGMvvH/lmAvCVdjg70+All/gdaPxvhD9H1x9DUPhb5zZ2J72TQzoGCtlhtihbtrn S5zn5rt+FyjUrLI19qr8qF9I1zWVceq/wLzfUP0+QSLne15v5uybxm38zcWkdxDYlV5r +MAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785457801; x=1786062601; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=QRMRalxGkRReFOWmTtsSnVADD1z6LkTNb+5taBWJoZU=; b=byxgWcq9YnjPxIMsRAselheYtdaAHn4Q9NrfCyA41xxC/RRhDt8RF2AxJZSsIq/XMO iOBDh+DW3fxWzmwVI+LYx4NOrw2dixjnBTZ6U0skzENO84oted3zVlqTwOqgZcAml16j r7Kh3RkQKwLW6PDVxLr/5wY8G3k5+DX1PwTfz1ONxl+qcaaeRVZ1f699yuFc2uzoP4P5 CLHfFo0rCvLV6DL5uakNEfTb/3GzCRpjdTcH2TTkV5PxhqbL566R5cjO9hHnV7WDVYY+ Hydxo2oZmvJX2lQLrKwO8BgJh9zrhWk99aogAdT2aatNE1fVwRXTdzZO5VLqes5zY7Rj Nmrg== X-Forwarded-Encrypted: i=1; AHgh+RrFW0e5jlcIZ+EMYIFrsRk4mbBBuIR7neX7JJ9OynB2R0a9ifXT2I5mjnkJ7CkropkZ+VqFIX93KAg1Qj4=@vger.kernel.org X-Gm-Message-State: AOJu0YxDFgU0lvYjrJV6k81SeUgTcKCEwvf6mIr+KxgExhq5F8vpTKot V88AoUe1otWZpsh4lGTh8OZu416aivRWWCGh0V9lRYGnvGrJOn9DdzNj X-Gm-Gg: AR+sD13z/aOM487SY6AGWpbPRxj2KEZADaxlrgMBj7YD8ipSwReGyHh174hOnFhMBeO 17QIPGfUgqyTh1cI9e+7DROr1Dl0by/uyEZgfjMmurZnT7BRVmbvBLHJL/tBjm1DGK8YgHay1X+ 9IjiEEO0WrEYAC+yqw2aM79+KJYYYwHVjQpieHh4tOmaEEhd0gCijl2doxdN+6pcrhp0H55ulm1 bZ8GkcMdgyyoHpGpy9EO9WlJtXHZK5kBWh4Jf+KE1si9WD7fMp9076VuTkCers+1NeGNjD3Lr/B nLEbyAPrCYgx8S2ArRpi0da/ClOVZseXeq9m5elhES4LeQdf8g5DXHcy2h2Gco5rf51VtQ3KQRz bxUglipyUUTRJ6NpvGCcHM8vGLaa4pv0cNpthbWRy1I9WaOfq7EcigN/BTVHd4zqu3QtjC8xen+ We38dTIVac9/hv6NKPhuRge1yurbouw24+5q31MfcaWMjvR2cCjLnYjHjWsZMtU07C71JJFA== X-Received: by 2002:a05:690c:a01b:b0:81e:94bd:6473 with SMTP id 00721157ae682-81fc1896b01mr15268507b3.30.1785457801138; Thu, 30 Jul 2026 17:30:01 -0700 (PDT) Received: from TurinLinux.home ([187.15.144.227]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-6692c6e8de7sm2355997d50.4.2026.07.30.17.29.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2026 17:30:00 -0700 (PDT) From: Nicholas Dudar To: bpf@vger.kernel.org, udknight@gmail.com, x86@kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, linux-kernel@vger.kernel.org, visitorckw@gmail.com Subject: [PATCH bpf-next v3] bpf, x86: Add support for BPF_SDIV and BPF_SMOD in the i386 JIT Date: Thu, 30 Jul 2026 20:29:50 -0400 Message-Id: <20260731002950.179573-1-main.kalliope@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D=3D 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 =3D=3D 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. Signed-off-by: Nicholas Dudar Assisted-by: Claude:claude-opus-4-8 --- v3: - Correct the incomplete recipient list. 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. v2: https://lore.kernel.org/bpf/20260714023939.616686-1-main.kalliope@gmail= .com/ v1: https://lore.kernel.org/bpf/20260713185848.120137-1-main.kalliope@gmail= .com/ [1]: https://lore.kernel.org/bpf/fa8a040f-64c4-484f-9538-fb0ec287639f@huawe= i.com/ arch/x86/net/bpf_jit_comp32.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp32.c b/arch/x86/net/bpf_jit_comp32.c index 852baf2e4db4..012161da3e82 100644 --- a/arch/x86/net/bpf_jit_comp32.c +++ b/arch/x86/net/bpf_jit_comp32.c @@ -432,11 +432,13 @@ static inline void emit_ia32_to_be_r64(const u8 dst[]= , s32 val, * ALU operation (32 bit) * dst =3D 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 =3D *pprog; int cnt =3D 0; + const u8 op =3D BPF_OP(insn->code); + const bool is_signed =3D (insn->off =3D=3D 1); =20 if (sstk) /* mov ecx,dword ptr [ebp+off] */ @@ -454,10 +456,17 @@ static inline void emit_ia32_div_mod_r(const u8 op, c= onst u8 dst, const u8 src, /* mov eax,dst */ EMIT2(0x8B, add_2reg(0xC0, dst, IA32_EAX)); =20 - /* 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)); + } =20 if (op =3D=3D BPF_MOD) { if (dstk) @@ -1795,14 +1804,14 @@ static int do_jit(struct bpf_prog *bpf_prog, int *a= ddrs, 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: 863f3ddd0b8ac65abfb50d3be0869268ac0e277b