Skip to contentSkip to conversation

riscv, bpf: Introduce Zba optimization

PATCH In review Xiao Wang 3 participants 6 messages May 24, 2024 – May 30, 2024

Subsystems (4)
lore
Share & tools
lore

Xiao Wang

author
#0 lore raw

v4 cover riscv, bpf: Introduce Zba optimization

The riscv Zba extension provides instructions to accelerate the generation of addresses that index into arrays of basic data types, bpf JIT generated insn counts could be reduced by leveraging Zba for address calculation.

The first patch introduces RISCV_ISA_ZBA Kconfig option and uses Zba add.uw insn to optimize zextw operation.

The second patch uses Zba shift-and-add insns to optimize address calculation for array of unsigned long data.

Thanks,
Xiao

v4:
* Combine the second patch (previously v1) and the first patch into a series. (Bjorn)
* Rebase the second patch on bpf-next tree.
* Link to v3: https://lore.kernel.org/bpf/[email protected]/

v3:
* Remove the Kconfig dependencies on TOOLCHAIN_HAS_ZBA and RISCV_ALTERNATIVE. (Andrew)
* Link to v2: https://lore.kernel.org/bpf/[email protected]/

v2:
* Add Zba description in the Kconfig. (Lehui)
* Reword the Kconfig help message to make it clearer. (Conor)
* Link to v1: https://lore.kernel.org/bpf/[email protected]/

Xiao Wang (2):
riscv, bpf: Optimize zextw insn with Zba extension riscv, bpf: Introduce shift add helper with Zba optimization

 arch/riscv/Kconfig              | 12 ++++++++
 arch/riscv/net/bpf_jit.h        | 51 +++++++++++++++++++++++++++++++++
 arch/riscv/net/bpf_jit_comp32.c |  3 +-
 arch/riscv/net/bpf_jit_comp64.c |  9 ++----
 4 files changed, 67 insertions(+), 8 deletions(-)

base-commit: 5c1672705a1a2389f5ad78e0fea6f08ed32d6f18

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v4 1/2 riscv, bpf: Optimize zextw insn with Zba extension

The Zba extension provides add.uw insn which can be used to implement zext.w with rs2 set as ZERO.

---
 arch/riscv/Kconfig       | 12 ++++++++++++
 arch/riscv/net/bpf_jit.h | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+)
arch/riscv/Kconfig +12 -0
index be09c8836d56..a117adff5810 100644
@@ -593,6 +593,18 @@ config TOOLCHAIN_HAS_VECTOR_CRYPTO
 	def_bool $(as-instr, .option arch$(comma) +v$(comma) +zvkb)
 	depends on AS_HAS_OPTION_ARCH
 
+config RISCV_ISA_ZBA
+	bool "Zba extension support for bit manipulation instructions"
+	default y
+	help
+	   Add support for enabling optimisations in the kernel when the Zba
+	   extension is detected at boot.
+
+	   The Zba extension provides instructions to accelerate the generation
+	   of addresses that index into arrays of basic data types.
+
+	   If you don't know what to do here, say Y.
+
 config RISCV_ISA_ZBB
 	bool "Zbb extension support for bit manipulation instructions"
 	depends on TOOLCHAIN_HAS_ZBB
arch/riscv/net/bpf_jit.h +18 -0
index fdbf88ca8b70..97041b58237a 100644
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
 	return IS_ENABLED(CONFIG_RISCV_ISA_C);
 }
 
+static inline bool rvzba_enabled(void)
+{
+	return IS_ENABLED(CONFIG_RISCV_ISA_ZBA) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBA);
+}
+
 static inline bool rvzbb_enabled(void)
 {
 	return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);
@@ -939,6 +944,14 @@ static inline u16 rvc_sdsp(u32 imm9, u8 rs2)
 	return rv_css_insn(0x7, imm, rs2, 0x2);
 }
 
+/* RV64-only ZBA instructions. */
+
+static inline u32 rvzba_zextw(u8 rd, u8 rs1)
+{
+	/* add.uw rd, rs1, ZERO */
+	return rv_r_insn(0x04, RV_REG_ZERO, rs1, 0, rd, 0x3b);
+}
+
 #endif /* __riscv_xlen == 64 */
 
 /* Helper functions that emit RVC instructions when possible. */
@@ -1161,6 +1174,11 @@ static inline void emit_zexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
 
 static inline void emit_zextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
 {
+	if (rvzba_enabled()) {
+		emit(rvzba_zextw(rd, rs), ctx);
+		return;
+	}
+
 	emit_slli(rd, rs, 32, ctx);
 	emit_srli(rd, rd, 32, ctx);
 }
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v4 2/2 riscv, bpf: Introduce shift add helper with Zba optimization

In reply to Xiao Wang

Zba extension is very useful for generating addresses that index into array of basic data types. This patch introduces sh2add and sh3add helpers for RV32 and RV64 respectively, to accelerate addressing for array of unsigned long data.

---
 arch/riscv/net/bpf_jit.h        | 33 +++++++++++++++++++++++++++++++++
 arch/riscv/net/bpf_jit_comp32.c |  3 +--
 arch/riscv/net/bpf_jit_comp64.c |  9 +++------
 3 files changed, 37 insertions(+), 8 deletions(-)
arch/riscv/net/bpf_jit.h +33 -0
index 97041b58237a..1d1c78d4cff1 100644
@@ -742,6 +742,17 @@ static inline u16 rvc_swsp(u32 imm8, u8 rs2)
 	return rv_css_insn(0x6, imm, rs2, 0x2);
 }
 
+/* RVZBA instructions. */
+static inline u32 rvzba_sh2add(u8 rd, u8 rs1, u8 rs2)
+{
+	return rv_r_insn(0x10, rs2, rs1, 0x4, rd, 0x33);
+}
+
+static inline u32 rvzba_sh3add(u8 rd, u8 rs1, u8 rs2)
+{
+	return rv_r_insn(0x10, rs2, rs1, 0x6, rd, 0x33);
+}
+
 /* RVZBB instructions. */
 static inline u32 rvzbb_sextb(u8 rd, u8 rs1)
 {
@@ -1095,6 +1106,28 @@ static inline void emit_sw(u8 rs1, s32 off, u8 rs2, struct rv_jit_context *ctx)
 		emit(rv_sw(rs1, off, rs2), ctx);
 }
 
+static inline void emit_sh2add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
+{
+	if (rvzba_enabled()) {
+		emit(rvzba_sh2add(rd, rs1, rs2), ctx);
+		return;
+	}
+
+	emit_slli(rd, rs1, 2, ctx);
+	emit_add(rd, rd, rs2, ctx);
+}
+
+static inline void emit_sh3add(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
+{
+	if (rvzba_enabled()) {
+		emit(rvzba_sh3add(rd, rs1, rs2), ctx);
+		return;
+	}
+
+	emit_slli(rd, rs1, 3, ctx);
+	emit_add(rd, rd, rs2, ctx);
+}
+
 /* RV64-only helper functions. */
 #if __riscv_xlen == 64
 
arch/riscv/net/bpf_jit_comp32.c +1 -2
index f5ba73bb153d..592dd86fbf81 100644
@@ -811,8 +811,7 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
 	 * if (!prog)
 	 *   goto out;
 	 */
-	emit(rv_slli(RV_REG_T0, lo(idx_reg), 2), ctx);
-	emit(rv_add(RV_REG_T0, RV_REG_T0, lo(arr_reg)), ctx);
+	emit_sh2add(RV_REG_T0, lo(idx_reg), lo(arr_reg), ctx);
 	off = offsetof(struct bpf_array, ptrs);
 	if (is_12b_check(off, insn))
 		return -1;
arch/riscv/net/bpf_jit_comp64.c +3 -6
index 79a001d5533e..30ede3ce42d1 100644
@@ -380,8 +380,7 @@ static int emit_bpf_tail_call(int insn, struct rv_jit_context *ctx)
 	 * if (!prog)
 	 *     goto out;
 	 */
-	emit_slli(RV_REG_T2, RV_REG_A2, 3, ctx);
-	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_A1, ctx);
+	emit_sh3add(RV_REG_T2, RV_REG_A2, RV_REG_A1, ctx);
 	off = offsetof(struct bpf_array, ptrs);
 	if (is_12b_check(off, insn))
 		return -1;
@@ -1097,12 +1096,10 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 			/* Load current CPU number in T1 */
 			emit_ld(RV_REG_T1, offsetof(struct thread_info, cpu),
 				RV_REG_TP, ctx);
-			/* << 3 because offsets are 8 bytes */
-			emit_slli(RV_REG_T1, RV_REG_T1, 3, ctx);
 			/* Load address of __per_cpu_offset array in T2 */
 			emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx);
-			/* Add offset of current CPU to  __per_cpu_offset */
-			emit_add(RV_REG_T1, RV_REG_T2, RV_REG_T1, ctx);
+			/* Get address of __per_cpu_offset[cpu] in T1 */
+			emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx);
 			/* Load __per_cpu_offset[cpu] in T1 */
 			emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx);
 			/* Add the offset to Rd */
Signature · 1 line
2.25.1
maintainer Acked-by on v4 2/2
#3 lore raw

Re: [PATCH bpf-next v4 2/2] riscv, bpf: Introduce shift add helper with Zba optimization

Xiao,

Quoted 4 lines · Xiao Wang

Zba extension is very useful for generating addresses that index into array of basic data types. This patch introduces sh2add and sh3add helpers for RV32 and RV64 respectively, to accelerate addressing for array of unsigned long data.

This patched slipped! Apologies for the slow reply.

#4 lore raw

RE: [PATCH bpf-next v4 2/2] riscv, bpf: Introduce shift add helper with Zba optimization

Quoted 20 lines

-----Original Message-----
From: Björn Töpel <[email protected]>
Sent: Wednesday, May 29, 2024 5:41 PM
To: Wang, Xiao W <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; [email protected]; [email protected]; Li, Haicheng <[email protected]>; Wang, Xiao W <[email protected]>
Subject: Re: [PATCH bpf-next v4 2/2] riscv, bpf: Introduce shift add helper with Zba optimization

Xiao,

Quoted 8 lines
Quoted 4 lines · Xiao Wang

Zba extension is very useful for generating addresses that index into array of basic data types. This patch introduces sh2add and sh3add helpers for RV32 and RV64 respectively, to accelerate addressing for array of unsigned long data.

This patched slipped! Apologies for the slow reply.

No worry. Thanks for picking it up.

BRs,
Xiao

Quoted 2 lines
#5 lore raw
In reply to Xiao Wang
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · On Fri, 24 May 2024 15:55:41 +0800 you wrote:

The riscv Zba extension provides instructions to accelerate the generation of addresses that index into arrays of basic data types, bpf JIT generated insn counts could be reduced by leveraging Zba for address calculation.

The first patch introduces RISCV_ISA_ZBA Kconfig option and uses Zba add.uw insn to optimize zextw operation.

[...]

Here is the summary with links:
- [bpf-next,v4,1/2] riscv, bpf: Optimize zextw insn with Zba extension

    https://git.kernel.org/bpf/bpf-next/c/c12603e76ef6

- [bpf-next,v4,2/2] riscv, bpf: Introduce shift add helper with Zba optimization (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html