[PATCH RFC] LoongArch: BPF: Add support for UHMUL and SHMUL
From: Tiezhu Yang
Date: Tue Nov 24 2026 - 00:04:52 EST
Implement JIT lowering for UHMUL and SHMUL on LoongArch by utilizing
hardware multiplication instructions MULH.{DU,D} and enabling support
via bpf_jit_supports_hmul().
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
Not tested yet, just RFC.
arch/loongarch/include/asm/inst.h | 2 ++
arch/loongarch/net/bpf_jit.c | 24 ++++++++++++++++++++++--
2 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/arch/loongarch/include/asm/inst.h b/arch/loongarch/include/asm/inst.h
index e061f966f7d1..fd6b39d5f0d0 100644
--- a/arch/loongarch/include/asm/inst.h
+++ b/arch/loongarch/include/asm/inst.h
@@ -777,6 +777,8 @@ DEF_EMIT_REG3_FORMAT(subw, subw_op)
DEF_EMIT_REG3_FORMAT(subd, subd_op)
DEF_EMIT_REG3_FORMAT(mulw, mulw_op)
DEF_EMIT_REG3_FORMAT(muld, muld_op)
+DEF_EMIT_REG3_FORMAT(mulhd, mulhd_op)
+DEF_EMIT_REG3_FORMAT(mulhdu, mulhdu_op)
DEF_EMIT_REG3_FORMAT(divw, divw_op)
DEF_EMIT_REG3_FORMAT(divd, divd_op)
DEF_EMIT_REG3_FORMAT(modw, modw_op)
diff --git a/arch/loongarch/net/bpf_jit.c b/arch/loongarch/net/bpf_jit.c
index 567729921d6d..b12da3e422c6 100644
--- a/arch/loongarch/net/bpf_jit.c
+++ b/arch/loongarch/net/bpf_jit.c
@@ -683,6 +683,21 @@ static int add_exception_handler(const struct bpf_insn *insn,
return 0;
}
+static void emit_various_muld(struct jit_ctx *ctx, u8 dst, u8 src, s16 off)
+{
+ switch (off) {
+ case BPF_UHMUL:
+ emit_insn(ctx, mulhdu, dst, dst, src);
+ break;
+ case BPF_SHMUL:
+ emit_insn(ctx, mulhd, dst, dst, src);
+ break;
+ default:
+ emit_insn(ctx, muld, dst, dst, src);
+ break;
+ }
+}
+
static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool extra_pass)
{
u8 tm = -1;
@@ -804,7 +819,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
emit_insn(ctx, mulw, dst, dst, src);
break;
case BPF_ALU64 | BPF_MUL | BPF_X:
- emit_insn(ctx, muld, dst, dst, src);
+ emit_various_muld(ctx, dst, src, off);
break;
/* dst = dst * imm */
@@ -814,7 +829,7 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx, bool ext
break;
case BPF_ALU64 | BPF_MUL | BPF_K:
move_imm(ctx, t1, imm);
- emit_insn(ctx, muld, dst, dst, t1);
+ emit_various_muld(ctx, dst, t1, off);
break;
/* dst = dst / src */
@@ -2571,3 +2586,8 @@ bool bpf_jit_supports_callx(void)
{
return true;
}
+
+bool bpf_jit_supports_hmul(void)
+{
+ return true;
+}
--
2.42.0