|
|
Log in / Subscribe / Register

bpf: Add UHMUL and SHMUL instructions

From:  Yazhou Tang <tangyazhou-AT-zju.edu.cn>
To:  bpf-AT-vger.kernel.org, Alexei Starovoitov <ast-AT-kernel.org>, Daniel Borkmann <daniel-AT-iogearbox.net>, Andrii Nakryiko <andrii-AT-kernel.org>, Eduard Zingerman <eddyz87-AT-gmail.com>, Kumar Kartikeya Dwivedi <memxor-AT-gmail.com>, David Vernet <void-AT-manifault.com>, bpf-AT-ietf.org
Subject:  [PATCH bpf-next v3 0/7] bpf: Add UHMUL and SHMUL instructions
Date:  Fri, 23 Oct 2026 00:14:36 +0800
Message-ID:  <20261023-bpf-hmul-v3-0-b99e808cb28b@outlook.com>
Cc:  John Fastabend <john.fastabend-AT-gmail.com>, Martin KaFai Lau <martin.lau-AT-linux.dev>, Song Liu <song-AT-kernel.org>, Yonghong Song <yonghong.song-AT-linux.dev>, Jiri Olsa <jolsa-AT-kernel.org>, Emil Tsalapatis <emil-AT-etsalapatis.com>, Ihor Solodrai <ihor.solodrai-AT-linux.dev>, Quentin Monnet <qmo-AT-kernel.org>, Puranjay Mohan <puranjay-AT-kernel.org>, Xu Kuohai <xukuohai-AT-huaweicloud.com>, Björn Töpel <bjorn-AT-kernel.org>, Pu Lehui <pulehui-AT-huawei.com>, Catalin Marinas <catalin.marinas-AT-arm.com>, Will Deacon <will-AT-kernel.org>, Mark Rutland <mark.rutland-AT-arm.com>, Luke Nelson <luke.r.nels-AT-gmail.com>, Xi Wang <xi.wang-AT-gmail.com>, Jonathan Corbet <corbet-AT-lwn.net>, linux-doc-AT-vger.kernel.org, Tianci Cao <ziye-AT-zju.edu.cn>, Yazhou Tang <tangyazhou518-AT-outlook.com>, Shenghao Yuan <shenghaoyuan0928-AT-163.com>, Alexei Starovoitov <ast-AT-kernel.org>
Archive-link:  Article

This patchset adds unsigned and signed high-half multiplication to the BPF
ISA. UHMUL returns the high 64 bits of an unsigned 64-by-64-bit product,
while SHMUL does the same for a signed product.

Both operations are BPF_ALU64 BPF_MUL variants selected by insn->off.
off == 0 remains the existing low-half multiplication, while off == 1
and off == 2 select UHMUL and SHMUL, respectively. The immediate forms
use the usual sign-extended 32-bit BPF immediate.

The patchset adds interpreter, disassembler, verifier, and documentation
support, along with JIT support for x86-64, arm64 and riscv64. JITs opt
in through bpf_jit_supports_hmul(). The verifier rejects HMUL when the
program is offloaded to hardware or JIT compilation is requested but
the JIT does not support HMUL. UHMUL and SHMUL remain available through
the interpreter when JIT compilation is disabled. For now, the verifier
computes exact results for constant operands and otherwise
conservatively marks the destination unknown.

Patch 7/7 uses raw bytecode for UHMUL and SHMUL because LLVM support has
not been merged yet. Once it is available, the raw encodings can be
replaced with the uh*= and sh*= mnemonics. The corresponding LLVM patch
is available for review at:

  https://github.com/llvm/llvm-project/pull/234076

The instruction design is inspired by the UHMUL and SHMUL operations in
sBPF (Solana's BPF variant). Patch 1/7 describes the encoding and
immediate-semantics differences.

---
Changes in v3:
- Make HMUL JIT support opt-in and check it in the verifier, replacing
  the per-backend rejection patch. (Alexei)
- Move bpf_insn_is_hmul() into the x86 JIT patch and simplify it.
- Link to v2: https://patch.msgid.link/20261021-bpf-hmul-v2-0-88e260099...

Changes in v2:
- Introduce a new conformance group "divmul64v2" for UHMUL/SHMUL. (bot+bpf-ci)
- Shorten the name of bpf_mul_variant. (Alexei)
- Remove redundant validation for off field in interpreter. (Alexei)
- Split arena test to two separate files.
- Link to v1: https://patch.msgid.link/20260917041712.1860722-1-tangyaz...

Changes in v1:
- Add JIT support on arm64 and riscv64. (Alexei)
- Fix the HMUL check on PowerPC JIT backend. (Sashiko AI reviewer)
- Link to RFC: https://lore.kernel.org/bpf/20260902070534.2141812-1-tang...

To: bpf@vger.kernel.org
To: Alexei Starovoitov <ast@kernel.org>
To: Daniel Borkmann <daniel@iogearbox.net>
To: Andrii Nakryiko <andrii@kernel.org>
To: Eduard Zingerman <eddyz87@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>
To: David Vernet <void@manifault.com>
To: bpf@ietf.org
Cc: John Fastabend <john.fastabend@gmail.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Song Liu <song@kernel.org>
Cc: Yonghong Song <yonghong.song@linux.dev>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Emil Tsalapatis <emil@etsalapatis.com>
Cc: Ihor Solodrai <ihor.solodrai@linux.dev>
Cc: Quentin Monnet <qmo@kernel.org>
Cc: Puranjay Mohan <puranjay@kernel.org>
Cc: Xu Kuohai <xukuohai@huaweicloud.com>
Cc: Björn Töpel <bjorn@kernel.org>
Cc: Pu Lehui <pulehui@huawei.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Will Deacon <will@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Luke Nelson <luke.r.nels@gmail.com>
Cc: Xi Wang <xi.wang@gmail.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: linux-doc@vger.kernel.org

---
Tianci Cao (3):
      bpf: Add UHMUL and SHMUL instructions
      bpf, x86: JIT UHMUL and SHMUL on x86-64
      bpf: Add verifier support for UHMUL and SHMUL

Yazhou Tang (4):
      bpf, arm64: JIT UHMUL and SHMUL
      bpf, riscv: JIT UHMUL and SHMUL on RV64
      bpf: Refactor ALU instruction variant validation
      selftests/bpf: Add bytecode tests for UHMUL and SHMUL

 .../bpf/standardization/instruction-set.rst        |  37 +++-
 arch/arm64/include/asm/insn.h                      |   4 +
 arch/arm64/lib/insn.c                              |   6 +
 arch/arm64/net/bpf_jit.h                           |   6 +
 arch/arm64/net/bpf_jit_comp.c                      |  20 +-
 arch/riscv/net/bpf_jit.h                           |   5 +
 arch/riscv/net/bpf_jit_comp64.c                    |  23 +-
 arch/x86/net/bpf_jit_comp.c                        |  53 +++++
 include/linux/filter.h                             |  34 +++
 include/uapi/linux/bpf.h                           |   6 +
 kernel/bpf/core.c                                  |  47 ++++-
 kernel/bpf/disasm.c                                |  18 ++
 kernel/bpf/verifier.c                              |  81 ++++++-
 tools/include/uapi/linux/bpf.h                     |   6 +
 tools/testing/selftests/bpf/prog_tests/verifier.c  |   2 +
 .../selftests/bpf/prog_tests/verifier_hmul_arena.c |  19 ++
 tools/testing/selftests/bpf/progs/verifier_hmul.c  | 235 +++++++++++++++++++++
 .../selftests/bpf/progs/verifier_hmul_arena.c      |  74 +++++++
 .../bpf/progs/verifier_value_illegal_alu.c         |   7 +-
 19 files changed, 660 insertions(+), 23 deletions(-)
---
base-commit: b116ac6087541d3d98031e786adbe9c733cd6df3
change-id: 20261021-bpf-hmul-40deb6203548

Best regards,
--  
Yazhou Tang <tangyazhou518@outlook.com>




Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds