bpf: Add UHMUL and SHMUL instructions
Subsystems (14)
v3 cover bpf: Add UHMUL and SHMUL instructions
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/[email protected]
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/[email protected]
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/[email protected]/
To: [email protected]
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: David Vernet <[email protected]>
To: [email protected]
Cc: John Fastabend <[email protected]>
Cc: Martin KaFai Lau <[email protected]>
Cc: Song Liu <[email protected]>
Cc: Yonghong Song <[email protected]>
Cc: Jiri Olsa <[email protected]>
Cc: Emil Tsalapatis <[email protected]>
Cc: Ihor Solodrai <[email protected]>
Cc: Quentin Monnet <[email protected]>
Cc: Puranjay Mohan <[email protected]>
Cc: Xu Kuohai <[email protected]>
Cc: Björn Töpel <[email protected]>
Cc: Pu Lehui <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Mark Rutland <[email protected]>
Cc: Luke Nelson <[email protected]>
Cc: Xi Wang <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: [email protected]
---
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,
Signature · 1 line
Yazhou Tang <[email protected]>
PATCH v3 1/7 bpf: Add UHMUL and SHMUL instructions
From: Tianci Cao <[email protected]>
Define UHMUL and SHMUL as BPF_ALU64 BPF_MUL variants selected by insn->off. UHMUL returns the high 64 bits of an unsigned 64-by-64-bit product, while SHMUL returns the high 64 bits of a signed product. Keep off == 0 as the existing low-half multiplication, and assign off == 1 and off == 2 to UHMUL and SHMUL, respectively.
These operations are inspired by the UHMUL and SHMUL instructions introduced as part of the PQR instruction class in sBPF (Solana's BPF variant)[1].
The encoding and immediate semantics differ from current sBPF.
1. sBPF assigns these operations dedicated opcodes in the BPF_PQR class, whereas this implementation selects them through the offset field of BPF_MUL.
2. sBPF initially sign-extended the immediate operand of UHMUL, but later changed unsigned PQR immediates, including UHMUL, to be zero-extended[2]. This was an ISA design choice to treat immediates of explicitly unsigned operations as unsigned 32-bit values[3].
Linux instead follows the existing BPF_ALU64 immediate convention. Both immediate forms first sign-extend the 32-bit immediate to 64 bits. UHMUL then interprets the resulting bit pattern as unsigned, while SHMUL interprets it as signed.
Add portable helpers for both operations, using mul_u64_u64_shr() and a signed correction instead of relying on a compiler-provided 128-bit integer type. Add interpreter support and disassemble UHMUL and SHMUL as uh*= and sh*=, respectively. Keep BPF_ALU multiplication restricted to the existing low-half variant.
Document the encoding and semantics in the BPF instruction set. Introduce a new divmul64v2 conformance group that includes divmul64 plus UHMUL and SHMUL. Keep divmul64 unchanged so existing implementations remain conformant without supporting the new instructions.
[1] https://github.com/solana-labs/rbpf/pull/498
[2] https://github.com/solana-labs/rbpf/pull/642
[3] https://lore.kernel.org/bpf/[email protected]/
--- .../bpf/standardization/instruction-set.rst | 37 +++++++++++++++---- include/linux/filter.h | 26 ++++++++++++++ include/uapi/linux/bpf.h | 6 ++++ kernel/bpf/core.c | 41 +++++++++++++++++++++- kernel/bpf/disasm.c | 18 ++++++++++ tools/include/uapi/linux/bpf.h | 6 ++++ 6 files changed, 126 insertions(+), 8 deletions(-)
Documentation/bpf/standardization/instruction-set.rst +30 -7
@@ -142,8 +142,9 @@ This document defines the following conformance groups: * atomic32: includes 32-bit atomic operation instructions (see `Atomic operations`_). * atomic64: includes atomic32, plus 64-bit atomic operation instructions. * divmul32: includes 32-bit division, multiplication, and modulo instructions. -* divmul64: includes divmul32, plus 64-bit division, multiplication, +* divmul64: includes divmul32, plus 64-bit division, low-half multiplication, and modulo instructions. +* divmul64v2: includes divmul64, plus high-half multiplication instructions. * packet: deprecated packet access instructions. Instruction encoding
@@ -330,6 +331,8 @@ register. ADD 0x0 0 dst += src SUB 0x1 0 dst -= src MUL 0x2 0 dst \*= src + UHMUL 0x2 1 ``dst uh*= src`` (unsigned high half, ``ALU64`` only) + SHMUL 0x2 2 ``dst sh*= src`` (signed high half, ``ALU64`` only) DIV 0x3 0 dst = (src != 0) ? (dst / src) : 0 SDIV 0x3 1 dst = (src == 0) ? 0 : ((src == -1 && dst == LLONG_MIN) ? LLONG_MIN : (dst s/ src)) OR 0x4 0 dst \|= src
@@ -379,13 +382,33 @@ where '(u32)' indicates that the upper 32 bits are zeroed. dst = dst ^ imm -Note that most arithmetic instructions have 'offset' set to 0. Only three instructions -(``SDIV``, ``SMOD``, ``MOVSX``) have a non-zero 'offset'. +Note that most arithmetic instructions have 'offset' set to 0. The following +instructions may have a non-zero 'offset': + +* ``SDIV`` and ``SMOD``: ``offset == 1`` selects the signed variant. +* ``MOVSX``: ``offset`` selects the source operand width. +* ``MUL``: ``offset`` selects the multiplication variant (see below). + +Division, low-half multiplication (``MUL`` with ``offset == 0``), and +modulo operations for ``ALU`` are part of the "divmul32" conformance +group, and their ``ALU64`` variants are part of the "divmul64" +conformance group. + +For ``ALU64`` multiplication, ``offset == 1`` selects unsigned +high-half multiplication (``UHMUL``), and ``offset == 2`` selects +signed high-half multiplication (``SHMUL``). Both instructions belong +to the "divmul64v2" conformance group. + +For ``X`` mode, ``src64`` is the 64-bit value in ``src_reg``. For +``K`` mode, ``src64`` is ``imm`` sign-extended from 32 to 64 bits. +The operations are:: + + UHMUL: dst = (u64)(((u128)dst * (u128)(u64)src64) >> 64) + SHMUL: dst = (u64)(((s128)(s64)dst * (s128)(s64)src64) >> 64) + +For ``ALU`` multiplication, only ``offset == 0`` is valid. For ``ALU64`` +multiplication, offset values other than 0, 1, and 2 are reserved. -Division, multiplication, and modulo operations for ``ALU`` are part -of the "divmul32" conformance group, and division, multiplication, and -modulo operations for ``ALU64`` are part of the "divmul64" conformance -group. The division and modulo operations support both unsigned and signed flavors. For unsigned operations (``DIV`` and ``MOD``), for ``ALU``,
include/linux/filter.h +26 -0
@@ -23,6 +23,7 @@#include <linux/sockptr.h> #include <linux/static_call.h> #include <linux/u64_stats_sync.h> +#include <linux/math64.h> #include <net/sch_generic.h>
@@ -152,6 +153,31 @@ struct ctl_table_header;#define BPF_ALU32_IMM (OP ,DST ,IMM ) \ BPF_ALU32_IMM_OFF(OP, DST, IMM, 0) +static inline u64 bpf_uhmul64 (u64 a,u64 b) +{ +return mul_u64_u64_shr (a, b,64 ); +} + +static inline u64 bpf_shmul64 (s64 a,s64 b) +{ +u64 ua = a, ub = b; +u64 hi =bpf_uhmul64 (ua, ub); + +/* +* Let M = 2^64, sa = (a < 0), and sb = (b < 0). Then: +* +* a * b = (ua - sa * M) * (ub - sb * M) +* = ua * ub - sa * M * ub - sb * M * ua + sa * sb * M^2 +* +* Therefore, signed_hi = unsigned_hi - sa * ub - sb * ua (mod M). +*/ +if (a <0 ) + hi -= ub; +if (b <0 ) + hi -= ua; +return hi; +} +/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */ #define BPF_ENDIAN (TYPE ,DST ,LEN ) \
include/uapi/linux/bpf.h +6 -0
@@ -34,6 +34,12 @@#define BPF_FROM_LE BPF_TO_LE#define BPF_FROM_BE BPF_TO_BE +/* BPF_MUL off values for high-half multiplication; BPF_ALU64 only. */ +enum bpf_mul_variant { +BPF_UHMUL =1 , +BPF_SHMUL =2 , +}; +/* jmp encodings */ #define BPF_JNE 0x50/* jump != */ #define BPF_JLT 0xa0/* LT is unsigned, '<' */
kernel/bpf/core.c +40 -1
47 lines not on this page · raw message
kernel/bpf/disasm.c +18 -0
@@ -163,6 +163,22 @@ static bool is_sdiv_smod(const struct bpf_insn *insn) insn->off ==1 ; } +/* Return the UHMUL or SHMUL operator string, or NULL for all other encodings. */ +static const char *hmul_op_string (const struct bpf_insn *insn) +{ +if (BPF_CLASS (insn->code) !=BPF_ALU64 || +BPF_OP (insn->code) !=BPF_MUL ) +return NULL ; +switch (insn->off) { +case BPF_UHMUL : +return "uh*=" ; +case BPF_SHMUL : +return "sh*=" ; +default : +return NULL ; + } +} +static bool is_movsx (const struct bpf_insn *insn) { return BPF_OP(insn->code) == BPF_MOV &&
@@ -215,6 +231,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs, verbose(cbs->private_data, "(%02x) %c%d %s %s%c%d", insn->code, class == BPF_ALU ? 'w' : 'r', insn->dst_reg, + hmul_op_string(insn) ?: is_sdiv_smod(insn) ? bpf_alu_sign_string[BPF_OP(insn->code) >> 4] : bpf_alu_string[BPF_OP(insn->code) >> 4], is_movsx(insn) ? bpf_movsx_string[(insn->off >> 3) - 1] : "",
@@ -224,6 +241,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,verbose (cbs->private_data,"(%02x) %c%d %s %d" , insn->code, class ==BPF_ALU ?'w' :'r' , insn->dst_reg, +hmul_op_string (insn) ?:is_sdiv_smod (insn) ? bpf_alu_sign_string[BPF_OP (insn->code) >>4 ] : bpf_alu_string[BPF_OP (insn->code) >>4 ], insn->imm);
tools/include/uapi/linux/bpf.h +6 -0
@@ -34,6 +34,12 @@#define BPF_FROM_LE BPF_TO_LE#define BPF_FROM_BE BPF_TO_BE +/* BPF_MUL off values for high-half multiplication; BPF_ALU64 only. */ +enum bpf_mul_variant { +BPF_UHMUL =1 , +BPF_SHMUL =2 , +}; +/* jmp encodings */ #define BPF_JNE 0x50/* jump != */ #define BPF_JLT 0xa0/* LT is unsigned, '<' */
Signature · 1 line
2.56.0
PATCH v3 2/7 bpf, x86: JIT UHMUL and SHMUL on x86-64
In reply to Yazhou TangFrom: Tianci Cao <[email protected]>
JIT the UHMUL and SHMUL instructions with the one-operand x86-64 mul and imul instructions. These instructions multiply RAX by an explicit source and return the full product in RDX:RAX, so the desired high half is available in RDX.
Copy register or immediate sources into AUX_REG, load the destination into RAX, and move RDX back to the BPF destination after multiplication. Preserve RAX and RDX when they hold other live BPF registers, while handling BPF_REG_0 and BPF_REG_3 destinations without restoring over the result.
Leave the existing JIT handling of ordinary BPF_MUL unchanged.
Add bpf_insn_is_hmul() to identify the high-half multiplication variants. Introduce bpf_jit_supports_hmul() with a weak default returning false, and override it to return true for the x86-64 JIT. This allows the verifier to check JIT support without adding rejection logic to every unsupported backend.
--- arch/x86/net/bpf_jit_comp.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/filter.h | 8 +++++++ kernel/bpf/core.c | 6 +++++ 3 files changed, 67 insertions(+)
arch/x86/net/bpf_jit_comp.c +53 -0
@@ -1378,6 +1378,38 @@ static int emit_kasan_check(struct bpf_verifier_env *env, u8 **pprog,return 0 ; } +/* +* The one-operand x86 mul/imul forms use RAX as the implicit first +* operand and store the 128-bit product in RDX:RAX. +* The JIT uses AUX_REG as the explicit second operand. +*/ +static void emit_hmul_aux (u8 **pprog,u32 dst_reg,bool is_signed) +{ +u8 *prog = *pprog; +bool save_rax = dst_reg !=BPF_REG_0 ; +bool save_rdx = dst_reg !=BPF_REG_3 ; +u8 mul_modrm_base = is_signed ?0xE8 :0xE0 ;/* imul /5 or mul /4 */ + +if (save_rax) +EMIT1 (0x50 );/* push rax */ +if (save_rdx) +EMIT1 (0x52 );/* push rdx */ + +if (save_rax) +emit_mov_reg (&prog, true,BPF_REG_0 , dst_reg); +maybe_emit_1mod (&prog,AUX_REG , true); +EMIT2 (0xF7 ,add_1reg (mul_modrm_base,AUX_REG )); +if (save_rdx) +emit_mov_reg (&prog, true, dst_reg,BPF_REG_3 ); + +if (save_rdx) +EMIT1 (0x5A );/* pop rdx */ +if (save_rax) +EMIT1 (0x58 );/* pop rax */ + + *pprog = prog; +} +/* LDX: dst_reg = *(u8*)(src_reg + off) */ static void emit_ldx (u8 **pprog,u32 size,u32 dst_reg,u32 src_reg,int off) {
@@ -2916,6 +2948,14 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *case BPF_ALU |BPF_MUL |BPF_K :case BPF_ALU64 |BPF_MUL |BPF_K : +if (bpf_insn_is_hmul (insn)) { +/* imul/mul use AUX_REG as src. */ +emit_mov_imm32 (&prog, true,AUX_REG , imm32); +emit_hmul_aux (&prog, dst_reg, + insn->off ==BPF_SHMUL ); +break ; + } +/* * lea for 3, 5 and 9, which LLVM leaves as a * multiplication, but not for r13, which needs a
@@ -2951,6 +2991,14 @@ static int do_jit(struct bpf_verifier_env *env, struct bpf_prog *bpf_prog, int *case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU64 |BPF_MUL |BPF_X : +if (bpf_insn_is_hmul (insn)) { +/* imul/mul use AUX_REG as src. */ +emit_mov_reg (&prog, true,AUX_REG , src_reg); +emit_hmul_aux (&prog, dst_reg, + insn->off ==BPF_SHMUL ); +break ; + } +maybe_emit_mod (&prog, src_reg, dst_reg,BPF_CLASS (insn->code) ==BPF_ALU64 );
@@ -5126,6 +5174,11 @@ bool bpf_jit_supports_unwind(void)return true; } +bool bpf_jit_supports_hmul (void ) +{ +return true; +} +const struct bpf_jit_arg_abi *bpf_jit_arg_abi (void ) {return &x86_arg_abi;
include/linux/filter.h +8 -0
@@ -178,6 +178,13 @@ static inline u64 bpf_shmul64(s64 a, s64 b)return hi; } +static inline bool bpf_insn_is_hmul (const struct bpf_insn *insn) +{ +return BPF_CLASS (insn->code) ==BPF_ALU64 && +BPF_OP (insn->code) ==BPF_MUL && + (insn->off ==BPF_UHMUL || insn->off ==BPF_SHMUL ); +} +/* Endianess conversion, cpu_to_{l,b}e(), {l,b}e_to_cpu() */ #define BPF_ENDIAN (TYPE ,DST ,LEN ) \
@@ -1307,6 +1314,7 @@ bool bpf_jit_supports_kfunc_call(void);bool bpf_jit_supports_callx (void );bool bpf_jit_supports_kfunc_ret_reg_pair (void );bool bpf_jit_supports_unwind (void ); +bool bpf_jit_supports_hmul (void );bool bpf_jit_supports_stack_args (void );bool bpf_jit_supports_arena_args (void );bool bpf_jit_supports_far_kfunc_call (void );
kernel/bpf/core.c +6 -0
@@ -3444,6 +3444,12 @@ bool __weak bpf_jit_supports_unwind(void)return false; } +/* uhmul and shmul */ +bool __weak bpf_jit_supports_hmul (void ) +{ +return false; +} +/* * How this arch places a by-value kfunc argument, or NULL for one that has * not opted in and so only takes an argument of a single eightbyte, which
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 2/7] bpf, x86: JIT UHMUL and SHMUL on x86-64
TL;DR: bpf_jit_supports_hmul() has no caller, and the verifier appears to still reject UHMUL/SHMUL encodings, so the new JIT code may be unreachable; other JITs could silently emit a low-half…
Quoted 1 line
bpf, x86: JIT UHMUL and SHMUL on x86-64
TL;DR: bpf_jit_supports_hmul() has no caller, and the verifier appears to still reject UHMUL/SHMUL encodings, so the new JIT code may be unreachable; other JITs could silently emit a low-half multiply. Is a verifier patch missing?
Quoted 3 lines
kernel/bpf/core.c +0
-0
[ ... ]
kernel/bpf/core.c bool __weak bpf_jit_supports_unwind(void) · line 3452
Quoted 10 lines
kernel/bpf/core.c +6
-0@@ -3444,6 +3444,12 @@ bool __weak bpf_jit_supports_unwind(void)return false; } +/* uhmul and shmul */ +bool __weak bpf_jit_supports_hmul (void ) +{ +return false; +} +
The commit message says bpf_jit_supports_hmul() "allows the verifier to check JIT support without adding rejection logic to every unsupported backend."
Is there a caller for this hook? Nothing in this commit, or anywhere up to the end of the series (bpf, arm64: JIT UHMUL and SHMUL), calls bpf_jit_supports_hmul(). The arm64 patch only adds a second override.
The verifier also appears to still reject the new encodings. check_alu_fields() is unchanged across the series, and for BPF_MUL with BPF_SRC(insn->code) == BPF_X it still has:
case BPF_MUL:
case BPF_DIV:
case BPF_MOD:
if (BPF_SRC(insn->code) == BPF_X) {
if (insn->imm != 0 || (insn->off != 0 && insn->off != 1) ||
(insn->off == 1 && opcode != BPF_MOD && opcode != BPF_DIV)) {
verbose(env, "BPF_ALU uses reserved fields\n");
return -EINVAL;The BPF_K form has the same check. A BPF_ALU64 | BPF_MUL instruction with off == BPF_UHMUL or BPF_SHMUL therefore fails to load with "BPF_ALU uses reserved fields", which means the new bpf_insn_is_hmul() branches in do_jit() and emit_hmul_aux() cannot be reached.
It also leaves the hook unused for the JITs that ignore insn->off for BPF_MUL (x86-32, riscv, s390, powerpc, loongarch, mips, sparc, arm32), which would emit a low-half multiply for these encodings once the verifier check is relaxed.
Is a verifier patch that calls bpf_jit_supports_hmul() missing from this series, or is it planned as a follow-up? If it is a follow-up, could the commit message say so?
---
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/39814644569
PATCH v3 3/7 bpf, arm64: JIT UHMUL and SHMUL
In reply to Yazhou TangFrom: Yazhou Tang <[email protected]>
arm64 provides UMULH and SMULH instructions that return the high 64 bits of an unsigned or signed 64-bit multiplication, respectively.
Add encodings for these instructions and use them to lower the UHMUL and SHMUL BPF_MUL variants for both register and immediate sources. Keep the existing low-half and ALU32 multiplication lowering unchanged.
Opt the arm64 JIT into HMUL support by returning true from bpf_jit_supports_hmul().
--- 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 ++++++++++++++++++-- 4 files changed, 34 insertions(+), 2 deletions(-)
arch/arm64/include/asm/insn.h +4 -0
@@ -247,6 +247,8 @@ enum aarch64_insn_data2_type {enum aarch64_insn_data3_type {AARCH64_INSN_DATA3_MADD ,AARCH64_INSN_DATA3_MSUB , +AARCH64_INSN_DATA3_SMULH , +AARCH64_INSN_DATA3_UMULH , };enum aarch64_insn_logic_type {
@@ -396,6 +398,8 @@ __AARCH64_INSN_FUNCS(sub_ext, 0x7FE00000, 0x4B200000) __AARCH64_INSN_FUNCS(subs_ext, 0x7FE00000, 0x6B200000) __AARCH64_INSN_FUNCS(madd, 0x7FE08000, 0x1B000000) __AARCH64_INSN_FUNCS(msub, 0x7FE08000, 0x1B008000) +__AARCH64_INSN_FUNCS(smulh, 0x7FE0FC00, 0x1B407C00) +__AARCH64_INSN_FUNCS(umulh, 0x7FE0FC00, 0x1BC07C00) __AARCH64_INSN_FUNCS(udiv, 0x7FE0FC00, 0x1AC00800) __AARCH64_INSN_FUNCS(sdiv, 0x7FE0FC00, 0x1AC00C00)__AARCH64_INSN_FUNCS (lslv,0x7FE0FC00 ,0x1AC02000 )
arch/arm64/lib/insn.c +6 -0
@@ -1155,6 +1155,12 @@ u32 aarch64_insn_gen_data3(enum aarch64_insn_register dst,case AARCH64_INSN_DATA3_MSUB : insn =aarch64_insn_get_msub_value ();break ; +case AARCH64_INSN_DATA3_SMULH : + insn =aarch64_insn_get_smulh_value (); +break ; +case AARCH64_INSN_DATA3_UMULH : + insn =aarch64_insn_get_umulh_value (); +break ;default :pr_err ("%s: unknown data3 encoding %d\n" , __func__, type);return AARCH64_BREAK_FAULT ;
arch/arm64/net/bpf_jit.h +6 -0
@@ -281,6 +281,12 @@ A64_VARIANT(sf), AARCH64_INSN_DATA3_MSUB)/* Rd = Rn * Rm */ #define A64_MUL (sf, Rd, Rn, Rm) A64_MADD(sf, Rd, A64_ZR, Rn, Rm) +/* Rd = high 64 bits of signed Rn * Rm */ +#define A64_SMULH (Rd, Rn, Rm) aarch64_insn_gen_data3(Rd, A64_ZR, Rn, Rm, \ + AARCH64_INSN_VARIANT_64BIT, AARCH64_INSN_DATA3_SMULH) +/* Rd = high 64 bits of unsigned Rn * Rm */ +#define A64_UMULH (Rd, Rn, Rm) aarch64_insn_gen_data3(Rd, A64_ZR, Rn, Rm, \ + AARCH64_INSN_VARIANT_64BIT, AARCH64_INSN_DATA3_UMULH)/* Logical (shifted register) */ #define A64_LOGIC_SREG (sf, Rd, Rn, Rm, type) \
arch/arm64/net/bpf_jit_comp.c +18 -2
@@ -144,6 +144,17 @@ static inline void emit_a64_mov_i(const int is64, const int reg, } } +static inline void emit_a64_mul (const bool is64,const u8 dst,const u8 src, +const s16 off,struct jit_ctx *ctx) +{ +if (is64 && off ==BPF_UHMUL ) +emit (A64_UMULH (dst, dst, src), ctx); +else if (is64 && off ==BPF_SHMUL ) +emit (A64_SMULH (dst, dst, src), ctx); +else +emit (A64_MUL (is64, dst, dst, src), ctx); +} +static int i64_i16_blocks (const u64 val,bool inverse) { return (((val >> 0) & 0xffff) != (inverse ? 0xffff : 0x0000)) +
@@ -1531,7 +1542,7 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insnbreak ;case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU64 |BPF_MUL |BPF_X : -emit (A64_MUL (is64, dst, dst, src), ctx); +emit_a64_mul (is64, dst, src, off, ctx);break ;case BPF_ALU |BPF_DIV |BPF_X :case BPF_ALU64 |BPF_DIV |BPF_X :
@@ -1660,7 +1671,7 @@ static int build_insn(const struct bpf_verifier_env *env, const struct bpf_insncase BPF_ALU |BPF_MUL |BPF_K :case BPF_ALU64 |BPF_MUL |BPF_K :emit_a64_mov_i (is64, tmp, imm, ctx); -emit (A64_MUL (is64, dst, dst, tmp), ctx); +emit_a64_mul (is64, dst, tmp, off, ctx);break ;case BPF_ALU |BPF_DIV |BPF_K :case BPF_ALU64 |BPF_DIV |BPF_K :
@@ -2595,6 +2606,11 @@ bool bpf_jit_supports_callx(void)return true; } +bool bpf_jit_supports_hmul (void ) +{ +return true; +} +bool bpf_jit_supports_large_stack (void ) {return true;
Signature · 1 line
2.56.0
PATCH v3 4/7 bpf, riscv: JIT UHMUL and SHMUL on RV64
In reply to Yazhou TangFrom: Yazhou Tang <[email protected]>
On RV64, the RISC-V M extension provides MULHU and MULH instructions that return the high 64 bits of an unsigned or signed 64-bit multiplication, respectively.
Add the missing MULH encoder and use MULHU and MULH to lower the UHMUL and SHMUL BPF_MUL variants for both register and immediate sources. Keep the existing low-half and ALU32 multiplication lowering unchanged.
Opt the RV64 JIT into HMUL support by returning true from bpf_jit_supports_hmul().
--- arch/riscv/net/bpf_jit.h | 5 +++++ arch/riscv/net/bpf_jit_comp64.c | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)
arch/riscv/net/bpf_jit.h +5 -0
@@ -436,6 +436,11 @@ static inline u32 rv_mul(u8 rd, u8 rs1, u8 rs2)return rv_r_insn (1 , rs2, rs1,0 , rd,0x33 ); } +static inline u32 rv_mulh (u8 rd,u8 rs1,u8 rs2) +{ +return rv_r_insn (1 , rs2, rs1,1 , rd,0x33 ); +} +static inline u32 rv_mulhu (u8 rd,u8 rs1,u8 rs2) {return rv_r_insn (1 , rs2, rs1,3 , rd,0x33 );
arch/riscv/net/bpf_jit_comp64.c +20 -3
@@ -197,6 +197,19 @@ static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)emit_addi (rd, rd, lower, ctx); } +static void emit_mul (u8 rd,u8 rs,bool is64,s16 off, +struct rv_jit_context *ctx) +{ +if (!is64) +emit (rv_mulw (rd, rd, rs), ctx); +else if (off ==BPF_UHMUL ) +emit (rv_mulhu (rd, rd, rs), ctx); +else if (off ==BPF_SHMUL ) +emit (rv_mulh (rd, rd, rs), ctx); +else +emit (rv_mul (rd, rd, rs), ctx); +} +static void __build_epilogue (bool is_tail_call,struct rv_jit_context *ctx) {int stack_adjust = ctx->stack_size, store_offset = stack_adjust -8 ;
@@ -1526,7 +1539,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,break ;case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU64 |BPF_MUL |BPF_X : -emit (is64 ?rv_mul (rd, rd, rs) :rv_mulw (rd, rd, rs), ctx); +emit_mul (rd, rs, is64, off, ctx);if (!is64 && !aux->verifier_zext)emit_zextw (rd, rd, ctx);break ;
@@ -1662,8 +1675,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,case BPF_ALU |BPF_MUL |BPF_K :case BPF_ALU64 |BPF_MUL |BPF_K :emit_imm (RV_REG_T1 , imm, ctx); -emit (is64 ?rv_mul (rd, rd,RV_REG_T1 ) : -rv_mulw (rd, rd,RV_REG_T1 ), ctx); +emit_mul (rd,RV_REG_T1 , is64, off, ctx);if (!is64 && !aux->verifier_zext)emit_zextw (rd, rd, ctx);break ;
@@ -2303,3 +2315,8 @@ bool bpf_jit_supports_callx(void) {return true; } + +bool bpf_jit_supports_hmul (void ) +{ +return true; +}
Signature · 1 line
2.56.0
Re: [PATCH bpf-next v3 4/7] bpf, riscv: JIT UHMUL and SHMUL on RV64
Quoted 86 lines · Yazhou Tang
From: Yazhou Tang <[email protected]>
On RV64, the RISC-V M extension provides MULHU and MULH instructions that return the high 64 bits of an unsigned or signed 64-bit multiplication, respectively.
Add the missing MULH encoder and use MULHU and MULH to lower the UHMUL and SHMUL
BPF_MULvariants for both register and immediate sources. Keep the existing low-half and ALU32 multiplication lowering unchanged.Opt the RV64 JIT into HMUL support by returning true from
bpf_jit_supports_hmul().Suggested-by Alexei Starovoitov <[email protected]>Signed-off-by Yazhou Tang <[email protected]>Co-developed-by Tianci Cao <[email protected]>Signed-off-by Tianci Cao <[email protected]>Co-developed-by Shenghao Yuan <[email protected]>Signed-off-by Shenghao Yuan <[email protected]>Acked-by Björn Töpel <[email protected]>--- arch/riscv/net/bpf_jit.h | 5 +++++ arch/riscv/net/bpf_jit_comp64.c | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-)arch/riscv/net/bpf_jit.h +5
-0@@ -436,6 +436,11 @@ static inline u32 rv_mul(u8 rd, u8 rs1, u8 rs2)return rv_r_insn (1 , rs2, rs1,0 , rd,0x33 ); } +static inline u32 rv_mulh (u8 rd,u8 rs1,u8 rs2) +{ +return rv_r_insn (1 , rs2, rs1,1 , rd,0x33 ); +} +static inline u32 rv_mulhu (u8 rd,u8 rs1,u8 rs2) {return rv_r_insn (1 , rs2, rs1,3 , rd,0x33 );arch/riscv/net/bpf_jit_comp64.c +15
-3@@ -197,6 +197,19 @@ static void emit_imm(u8 rd, s64 val, struct rv_jit_context *ctx)emit_addi (rd, rd, lower, ctx); } +static void emit_mul (u8 rd,u8 rs,bool is64,s16 off, +struct rv_jit_context *ctx) +{ +if (!is64) +emit (rv_mulw (rd, rd, rs), ctx); +else if (off ==BPF_UHMUL ) +emit (rv_mulhu (rd, rd, rs), ctx); +else if (off ==BPF_SHMUL ) +emit (rv_mulh (rd, rd, rs), ctx); +else +emit (rv_mul (rd, rd, rs), ctx); +} +static void __build_epilogue (bool is_tail_call,struct rv_jit_context *ctx) {int stack_adjust = ctx->stack_size, store_offset = stack_adjust -8 ;@@ -1526,7 +1539,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,break ;case BPF_ALU |BPF_MUL |BPF_X :case BPF_ALU64 |BPF_MUL |BPF_X : -emit (is64 ?rv_mul (rd, rd, rs) :rv_mulw (rd, rd, rs), ctx); +emit_mul (rd, rs, is64, off, ctx);if (!is64 && !aux->verifier_zext)emit_zextw (rd, rd, ctx);break ;@@ -1662,8 +1675,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,case BPF_ALU |BPF_MUL |BPF_K :case BPF_ALU64 |BPF_MUL |BPF_K :emit_imm (RV_REG_T1 , imm, ctx); -emit (is64 ?rv_mul (rd, rd,RV_REG_T1 ) : -rv_mulw (rd, rd,RV_REG_T1 ), ctx); +emit_mul (rd,RV_REG_T1 , is64, off, ctx);if (!is64 && !aux->verifier_zext)emit_zextw (rd, rd, ctx);break ;
arch/riscv/net/bpf_jit_comp64.c bool bpf_jit_supports_callx(void) · line 2322
Quoted 8 lines
arch/riscv/net/bpf_jit_comp64.c +5
-0@@ -2303,3 +2315,8 @@ bool bpf_jit_supports_callx(void) {return true; } + +bool bpf_jit_supports_hmul (void ) +{ +return true; +}
PATCH v3 5/7 bpf: Refactor ALU instruction variant validation
In reply to Yazhou TangFrom: Yazhou Tang <[email protected]>
check_alu_fields() currently validates insn->off together with the source-specific reserved fields. This duplicates the DIV and MOD variant rules between register and immediate forms and makes the valid instruction variants difficult to identify.
Move the opcode-specific insn->off validation into is_valid_alu_variant(). The helper permits off == 1 for DIV and MOD and requires off == 0 for all other ALU operations. Validate the variant once, then separately check the reserved immediate field for register sources and src_reg for immediate sources.
This is a pure refactoring and does not change which instruction encodings the verifier accepts.
--- kernel/bpf/verifier.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +15 -9
@@ -21194,6 +21194,18 @@ static int fd_array_get_map_idx(struct bpf_verifier_env *env, u32 idx)return -EPROTO ; } +static bool is_valid_alu_variant (const struct bpf_insn *insn) +{ +switch (BPF_OP (insn->code)) { +case BPF_DIV : +case BPF_MOD : +/* off == 1 selects SDIV/SMOD. */ +return insn->off ==0 || insn->off ==1 ; +default : +return insn->off ==0 ; + } +} +static int check_alu_fields (struct bpf_verifier_env *env,struct bpf_insn *insn) {u8 class =BPF_CLASS (insn->code);
@@ -21249,15 +21261,9 @@ static int check_alu_fields(struct bpf_verifier_env *env, struct bpf_insn *insn)case BPF_MUL :case BPF_DIV :case BPF_MOD : -if (BPF_SRC (insn->code) ==BPF_X ) { -if (insn->imm !=0 || (insn->off !=0 && insn->off !=1 ) || - (insn->off ==1 && opcode !=BPF_MOD && opcode !=BPF_DIV )) { -verbose (env,"BPF_ALU uses reserved fields\n" ); -return -EINVAL ; - } - }else if (insn->src_reg !=BPF_REG_0 || - (insn->off !=0 && insn->off !=1 ) || - (insn->off ==1 && opcode !=BPF_MOD && opcode !=BPF_DIV )) { +if (!is_valid_alu_variant (insn) || + (BPF_SRC (insn->code) ==BPF_X && insn->imm !=0 ) || + (BPF_SRC (insn->code) ==BPF_K && insn->src_reg !=BPF_REG_0 )) {verbose (env,"BPF_ALU uses reserved fields\n" );return -EINVAL ; }
Signature · 1 line
2.56.0
PATCH v3 6/7 bpf: Add verifier support for UHMUL and SHMUL
In reply to Yazhou TangFrom: Tianci Cao <[email protected]>
Extend ALU variant validation to accept off == 1 and off == 2 as UHMUL and SHMUL, respectively, for BPF_ALU64 MUL instructions. Both offsets remain reserved for BPF_ALU MUL. All other MUL offsets remain invalid.
Reject HMUL instructions when the program is offloaded to hardware or when JIT compilation is requested but the JIT does not support HMUL. Return -EOPNOTSUPP with a verifier log message in both cases. UHMUL and SHMUL remain available through the interpreter when JIT compilation is disabled.
For scalar tracking, compute the exact high-half result when both operands are constants. Mark the destination unknown when either operand is variable until range analysis for these variants is added. Keep the existing scalar analysis for ordinary low-half MUL unchanged.
Reject UHMUL and SHMUL when either operand is PTR_TO_ARENA. Arena arithmetic can be rewritten from BPF_ALU64 to BPF_ALU to enforce its 32-bit pointer representation, but such a rewrite cannot preserve the semantics of variants that are only defined for BPF_ALU64.
--- kernel/bpf/verifier.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
kernel/bpf/verifier.c +57 -0
@@ -16664,6 +16664,24 @@ static void scalar_min_max_mul(struct bpf_reg_state *dst_reg, cnum64_from_srange(smin, smax)); } +static void scalar_min_max_uhmul (struct bpf_reg_state *dst_reg, +struct bpf_reg_state *src_reg) +{ +u64 dst =reg_const_value (dst_reg, false); +u64 src =reg_const_value (src_reg, false); + +___mark_reg_known (dst_reg,bpf_uhmul64 (dst, src)); +} + +static void scalar_min_max_shmul (struct bpf_reg_state *dst_reg, +struct bpf_reg_state *src_reg) +{ +s64 dst =reg_const_value (dst_reg, false); +s64 src =reg_const_value (src_reg, false); + +___mark_reg_known (dst_reg,bpf_shmul64 (dst, src)); +} +static void scalar32_min_max_udiv (struct bpf_reg_state *dst_reg,struct bpf_reg_state *src_reg) {
@@ -17364,6 +17382,25 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,reg_step_reset (dst_reg);break ;case BPF_MUL : +/* +* Range analysis for UHMUL/SHMUL is not implemented yet. +* Compute an exact result only when both operands are constants. +* Ordinary low-half MUL continues to use the analysis below. +*/ +if (bpf_insn_is_hmul (insn) && + (!is_reg_const (dst_reg, false) || + !is_reg_const (&src_reg, false))) { +__mark_reg_unknown (env, dst_reg); +break ; + } +if (!alu32 && insn->off ==BPF_UHMUL ) { +scalar_min_max_uhmul (dst_reg, &src_reg); +break ; + } +if (!alu32 && insn->off ==BPF_SHMUL ) { +scalar_min_max_shmul (dst_reg, &src_reg); +break ; + }scalar_step_mul (dst_reg, &src_reg); dst_reg->var_off =tnum_mul (dst_reg->var_off, src_reg.var_off);scalar32_min_max_mul (dst_reg, &src_reg);
@@ -17499,6 +17536,11 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,/* Case where at least one operand is an arena. */ if (dst_reg->type ==PTR_TO_ARENA || (src_reg && src_reg->type ==PTR_TO_ARENA )) { +if (bpf_insn_is_hmul (insn)) { +verbose (env,"UHMUL/SHMUL with arena pointers are not supported\n" ); +return -EOPNOTSUPP ; + } +if (dst_reg->type !=PTR_TO_ARENA ) *dst_reg = *src_reg;
@@ -21197,6 +21239,8 @@ static int fd_array_get_map_idx(struct bpf_verifier_env *env, u32 idx)static bool is_valid_alu_variant (const struct bpf_insn *insn) {switch (BPF_OP (insn->code)) { +case BPF_MUL : +return insn->off ==0 ||bpf_insn_is_hmul (insn);case BPF_DIV :case BPF_MOD :/* off == 1 selects SDIV/SMOD. */
@@ -21206,6 +21250,17 @@ static bool is_valid_alu_variant(const struct bpf_insn *insn) } } +/* The interpreter supports uhmul and shmul. JITs opt in. */ +static int check_hmul_jit (struct bpf_verifier_env *env) +{ +if (bpf_prog_is_offloaded (env->prog->aux) || + (env->prog->jit_requested && !bpf_jit_supports_hmul ())) { +verbose (env,"JIT doesn't support uhmul and shmul\n" ); +return -EOPNOTSUPP ; + } +return 0 ; +} +static int check_alu_fields (struct bpf_verifier_env *env,struct bpf_insn *insn) {u8 class =BPF_CLASS (insn->code);
@@ -21267,6 +21322,8 @@ static int check_alu_fields(struct bpf_verifier_env *env, struct bpf_insn *insn)verbose (env,"BPF_ALU uses reserved fields\n" );return -EINVAL ; } +if (bpf_insn_is_hmul (insn)) +return check_hmul_jit (env);return 0 ;default :verbose (env,"invalid BPF_ALU opcode %x\n" , opcode);
Signature · 1 line
2.56.0
PATCH v3 7/7 selftests/bpf: Add bytecode tests for UHMUL and SHMUL
In reply to Yazhou TangFrom: Yazhou Tang <[email protected]>
Add test_progs coverage for UHMUL and SHMUL with the following changes:
1. Add arithmetic execution tests in verifier_hmul.c and register the new test suite with the verifier test runner:
- Gate the execution cases on x86, arm64, and RV64, matching the JIT support added in this series, and provide a dummy program for other targets.
- Inject only UHMUL and SHMUL as raw .8byte values built with BPF_RAW_INSN(), while keeping the surrounding instructions in the existing assembly syntax. This avoids requiring assembler support for the new mnemonics.
- Exercise register and immediate sources, signed and unsigned boundary values, negative immediate sign extension, and R0, R1, and R3 destinations. The R3 case covers the x86 JIT path where BPF R3 aliases the implicit RDX result register. Run these cases in both privileged and unprivileged modes.
- BPF_PROG_TEST_RUN exposes only the low 32 bits of R0 as its return value, so a direct __retval() check cannot validate the upper 32 bits. Build a 64-bit mismatch mask as actual ^ expected, then fold it to 32 bits as (u32)mismatch | (u32)(mismatch >> 32) and return the folded value through R0. The return value is zero if and only if the complete 64-bit result matches the expected value.
2. Add arena pointer rejection tests in verifier_hmul_arena.c. Use an arena pointer as the destination of UHMUL and as the source of SHMUL, and verify that the verifier rejects both programs.
Restrict the arena cases to JIT-enabled x86-64, arm64, and RV64. This ensures the programs reach the arena pointer checks instead of being rejected by the earlier HMUL JIT capability check.
Keep these cases in a separate BPF object with a dedicated userspace runner. Probe BPF_MAP_TYPE_ARENA support before loading the object, and skip this suite when arena maps are unavailable, while keeping the arithmetic tests in verifier_hmul.c runnable. Retain __BPF_FEATURE_ADDR_SPACE_CAST gating and a dummy skip program for toolchains without addr_space_cast support.
3. Extend the illegal instruction encoding tests in verifier_value_illegal_alu.c. Check reserved MUL offsets and UHMUL and SHMUL encodings in the BPF_ALU class, reusing the existing DEFINE_BAD_OFFSET_TEST() macro.
--- 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 +- 5 files changed, 336 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -59,6 +59,7 @@#include "verifier_helper_packet_access.skel.h" #include "verifier_helper_restricted.skel.h" #include "verifier_helper_value_access.skel.h" +#include "verifier_hmul.skel.h" #include "verifier_idoms.skel.h" #include "verifier_int_ptr.skel.h" #include "verifier_iterating_callbacks.skel.h"
@@ -251,6 +252,7 @@ void test_verifier_helper_access_var_len(void) { RUN(verifier_helper_access_var_void test_verifier_helper_packet_access (void ) {RUN (verifier_helper_packet_access); }void test_verifier_helper_restricted (void ) {RUN (verifier_helper_restricted); }void test_verifier_helper_value_access (void ) {RUN (verifier_helper_value_access); } +void test_verifier_hmul (void ) {RUN (verifier_hmul); }void test_verifier_idoms (void ) {RUN (verifier_idoms); }void test_verifier_int_ptr (void ) {RUN (verifier_int_ptr); }void test_verifier_iterating_callbacks (void ) {RUN (verifier_iterating_callbacks); }
tools/testing/selftests/bpf/prog_tests/verifier_hmul_arena.c new file +19 -0
@@ -0,0 +1,19 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <test_progs.h> +#include "verifier_hmul_arena.skel.h" + +void test_verifier_hmul_arena (void ) +{ +int ret; + +/* Arena support can depend on CPU features, such as Zacas on RV64. */ + ret =libbpf_probe_bpf_map_type (BPF_MAP_TYPE_ARENA ,NULL ); +if (!ASSERT_GE (ret,0 ,"probe_arena" )) +return ; +if (!ret) { +test__skip (); +return ; + } +RUN_TESTS (verifier_hmul_arena); +}
tools/testing/selftests/bpf/progs/verifier_hmul.c new file +235 -0
235 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_hmul_arena.c new file +74 -0
74 lines not on this page · raw message
tools/testing/selftests/bpf/progs/verifier_value_illegal_alu.c +6 -1
@@ -367,7 +367,8 @@ __naked void flow_keys_illegal_variable_offset_alu(void)/* * Offset fields of 0 and 1 are legal for BPF_{DIV,MOD} instructions. -* Offset fields of 0 are legal for the rest of ALU instructions. +* Offset fields of 0, 1 and 2 are legal for BPF_ALU64 | BPF_MUL. +* Only offset 0 is legal for BPF_ALU | BPF_MUL. * Test that error is reported for illegal offsets, assuming that tests * for legal offsets exist. */
@@ -377,5 +378,9 @@ DEFINE_BAD_OFFSET_TEST(bad_offset_addx, BPF_ALU64 | BPF_ADD | BPF_X, -1, 0) DEFINE_BAD_OFFSET_TEST(bad_offset_divx2, BPF_ALU64 | BPF_DIV | BPF_X, 2, 0) DEFINE_BAD_OFFSET_TEST(bad_offset_modk2, BPF_ALU64 | BPF_MOD | BPF_K, 2, 1) DEFINE_BAD_OFFSET_TEST(bad_offset_addx2, BPF_ALU64 | BPF_ADD | BPF_X, 1, 0) +DEFINE_BAD_OFFSET_TEST(bad_offset_mulx, BPF_ALU64 | BPF_MUL | BPF_X, -1, 0) +DEFINE_BAD_OFFSET_TEST(bad_offset_mulk, BPF_ALU64 | BPF_MUL | BPF_K, 3, 0) +DEFINE_BAD_OFFSET_TEST(bad_uhmul32_x, BPF_ALU | BPF_MUL | BPF_X, BPF_UHMUL, 0) +DEFINE_BAD_OFFSET_TEST(bad_shmul32_k, BPF_ALU | BPF_MUL | BPF_K, BPF_SHMUL, 0) char _license[] SEC("license") = "GPL";
Signature · 1 line
2.56.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Fri, 23 Oct 2026 00:14:36 +0800 you wrote:
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_ALU64BPF_MULvariants selected byinsn->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.[...]
Here is the summary with links:
- [bpf-next,v3,1/7] bpf: Add UHMUL and SHMUL instructions
https://git.kernel.org/bpf/bpf-next/c/f09c72811afa - [bpf-next,v3,2/7] bpf, x86: JIT UHMUL and SHMUL on x86-64 https://git.kernel.org/bpf/bpf-next/c/eab118af696e - [bpf-next,v3,3/7] bpf, arm64: JIT UHMUL and SHMUL https://git.kernel.org/bpf/bpf-next/c/a07460d1665c - [bpf-next,v3,4/7] bpf, riscv: JIT UHMUL and SHMUL on RV64 https://git.kernel.org/bpf/bpf-next/c/503b293a4fa5 - [bpf-next,v3,5/7] bpf: Refactor ALU instruction variant validation https://git.kernel.org/bpf/bpf-next/c/997add3abaa3 - [bpf-next,v3,6/7] bpf: Add verifier support for UHMUL and SHMUL https://git.kernel.org/bpf/bpf-next/c/c9ec5e4256c1 - [bpf-next,v3,7/7] selftests/bpf: Add bytecode tests for UHMUL and SHMUL https://git.kernel.org/bpf/bpf-next/c/ddd86412ac01
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html