Skip to contentSkip to conversation

riscv, bpf: Optimize zextw insn with Zba extension

PATCH Superseded Xiao Wang 3 participants 9 messages May 11, 2024 – May 15, 2024

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Xiao Wang

author
#0 lore raw

PATCH v2 1/1 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.

---
v2:
* Add Zba description in the Kconfig. (Lehui)
* Reword the Kconfig help message to make it clearer. (Conor)

---
 arch/riscv/Kconfig       | 22 ++++++++++++++++++++++
 arch/riscv/net/bpf_jit.h | 18 ++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/riscv/Kconfig +22 -0
index 6bec1bce6586..e262a8668b41 100644
@@ -586,6 +586,14 @@ config RISCV_ISA_V_PREEMPTIVE
 	  preemption. Enabling this config will result in higher memory
 	  consumption due to the allocation of per-task's kernel Vector context.
 
+config TOOLCHAIN_HAS_ZBA
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zba)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zba)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900
+	depends on AS_HAS_OPTION_ARCH
+
 config TOOLCHAIN_HAS_ZBB
 	bool
 	default y
@@ -601,6 +609,20 @@ 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"
+	depends on TOOLCHAIN_HAS_ZBA
+	depends on RISCV_ALTERNATIVE
+	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 f4b6b3b9edda..18a7885ba95e 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);
@@ -937,6 +942,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. */
@@ -1159,6 +1172,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
#1 lore raw
Quoted 33 lines · Xiao Wang

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

---
v2:
* Add Zba description in the Kconfig. (Lehui)
* Reword the Kconfig help message to make it clearer. (Conor)

---
 arch/riscv/Kconfig       | 22 ++++++++++++++++++++++
 arch/riscv/net/bpf_jit.h | 18 ++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/riscv/Kconfig +8 -0
index 6bec1bce6586..e262a8668b41 100644
@@ -586,6 +586,14 @@ config RISCV_ISA_V_PREEMPTIVE
 	  preemption. Enabling this config will result in higher memory
 	  consumption due to the allocation of per-task's kernel Vector context.
 
+config TOOLCHAIN_HAS_ZBA
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zba)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zba)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900
+	depends on AS_HAS_OPTION_ARCH
+
 config TOOLCHAIN_HAS_ZBB
 	bool
 	default y

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 6 lines
arch/riscv/Kconfig +3 -0
@@ -601,6 +609,20 @@ 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"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 1 line
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Quoted 59 lines
arch/riscv/Kconfig +10 -0
+	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 f4b6b3b9edda..18a7885ba95e 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);
@@ -937,6 +942,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. */
@@ -1159,6 +1172,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

Thanks,
drew

#2 lore raw
Quoted 55 lines

-----Original Message-----
From: Andrew Jones <[email protected]>
Sent: Tuesday, May 14, 2024 12:53 AM
To: Wang, Xiao W <[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]; [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]>; [email protected]
Subject: Re: [PATCH v2] riscv, bpf: Optimize zextw insn with Zba extension

Quoted 18 lines · Xiao Wang

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

---
v2:
* Add Zba description in the Kconfig. (Lehui)
* Reword the Kconfig help message to make it clearer. (Conor)

---
 arch/riscv/Kconfig       | 22 ++++++++++++++++++++++
 arch/riscv/net/bpf_jit.h | 18 ++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/riscv/Kconfig +0 -0
index 6bec1bce6586..e262a8668b41 100644

arch/riscv/Kconfig config RISCV_ISA_V_PREEMPTIVE · line 587

Quoted 2 lines
arch/riscv/Kconfig +0 -0
@@ -586,6 +586,14 @@ config RISCV_ISA_V_PREEMPTIVE
 	  preemption. Enabling this config will result in higher memory
 	  consumption due to the allocation of per-task's kernel Vector

context.

Quoted 16 lines
arch/riscv/Kconfig +8 -0
+config TOOLCHAIN_HAS_ZBA
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zba)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zba)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900
+	depends on AS_HAS_OPTION_ARCH
+
 config TOOLCHAIN_HAS_ZBB
 	bool
 	default y
@@ -601,6 +609,20 @@ config TOOLCHAIN_HAS_VECTOR_CRYPTO
 	def_bool $(as-instr, .option arch$(comma) +v$(comma) +zvkb)
 	depends on AS_HAS_OPTION_ARCH
 
Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 3 lines
arch/riscv/Kconfig +3 -0
+config RISCV_ISA_ZBA
+	bool "Zba extension support for bit manipulation instructions"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

Good point, we don't need toolchain support for this bpf jit case.

Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 2 lines
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Agree, it's not required. For this bpf jit case, we should drop these two dependencies.

BTW, Zbb is used in bpf jit, the usage there also doesn't depend on toolchain and RISCV_ALTERNATIVE, but the Kconfig for RISCV_ISA_ZBB has forced the dependencies due to Zbb assembly programming elsewhere.
Maybe we could just dynamically check the existence of RISCV_ISA_ZB* before jit code emission? or introduce new config options for bpf jit? I prefer the first method and welcome any comments.

Thanks,
Xiao

[...]

Quoted 15 lines
Quoted 10 lines
arch/riscv/Kconfig +5 -1
 {
+	if (rvzba_enabled()) {
+		emit(rvzba_zextw(rd, rs), ctx);
+		return;
+	}
+
 	emit_slli(rd, rs, 32, ctx);
 	emit_srli(rd, rd, 32, ctx);
 }
--
Quoted 2 lines

2.25.1

Thanks,
drew

#3 lore raw
Quoted 69 lines · Xiao W
Quoted 55 lines

-----Original Message-----
From: Andrew Jones <[email protected]>
Sent: Tuesday, May 14, 2024 12:53 AM
To: Wang, Xiao W <[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]; [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]>; [email protected]
Subject: Re: [PATCH v2] riscv, bpf: Optimize zextw insn with Zba extension

Quoted 18 lines · Xiao Wang

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

---
v2:
* Add Zba description in the Kconfig. (Lehui)
* Reword the Kconfig help message to make it clearer. (Conor)

---
 arch/riscv/Kconfig       | 22 ++++++++++++++++++++++
 arch/riscv/net/bpf_jit.h | 18 ++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/riscv/Kconfig +0 -0
index 6bec1bce6586..e262a8668b41 100644

arch/riscv/Kconfig config RISCV_ISA_V_PREEMPTIVE · line 587

Quoted 2 lines
arch/riscv/Kconfig +0 -0
@@ -586,6 +586,14 @@ config RISCV_ISA_V_PREEMPTIVE
 	  preemption. Enabling this config will result in higher memory
 	  consumption due to the allocation of per-task's kernel Vector

context.

Quoted 16 lines
arch/riscv/Kconfig +8 -0
+config TOOLCHAIN_HAS_ZBA
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zba)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zba)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900
+	depends on AS_HAS_OPTION_ARCH
+
 config TOOLCHAIN_HAS_ZBB
 	bool
 	default y
@@ -601,6 +609,20 @@ config TOOLCHAIN_HAS_VECTOR_CRYPTO
 	def_bool $(as-instr, .option arch$(comma) +v$(comma) +zvkb)
 	depends on AS_HAS_OPTION_ARCH
 
Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 3 lines
arch/riscv/Kconfig +3 -0
+config RISCV_ISA_ZBA
+	bool "Zba extension support for bit manipulation instructions"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

Good point, we don't need toolchain support for this bpf jit case.

Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 2 lines
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Quoted 8 lines

Agree, it's not required. For this bpf jit case, we should drop these two dependencies.

BTW, Zbb is used in bpf jit, the usage there also doesn't depend on toolchain and RISCV_ALTERNATIVE, but the Kconfig for RISCV_ISA_ZBB has forced the dependencies due to Zbb assembly programming elsewhere.
Maybe we could just dynamically check the existence of RISCV_ISA_ZB* before jit code emission? or introduce new config options for bpf jit? I prefer the first method and welcome any comments.

My preferences is to remove as much of the TOOLCHAIN_HAS_ stuff as possible. We should audit the extensions which have them to see if they're really necessary. I don't mind depending on RISCV_ALTERNATIVE, since it's almost required for riscv at this point anyway.

Thanks,
drew

Quoted 20 lines

Thanks,
Xiao

[...]

Quoted 15 lines
Quoted 10 lines
arch/riscv/Kconfig +5 -1
 {
+	if (rvzba_enabled()) {
+		emit(rvzba_zextw(rd, rs), ctx);
+		return;
+	}
+
 	emit_slli(rd, rs, 32, ctx);
 	emit_srli(rd, rd, 32, ctx);
 }
--
Quoted 2 lines

2.25.1

Thanks,
drew

#4 lore raw
Quoted 100 lines

-----Original Message-----
From: Andrew Jones <[email protected]>
Sent: Tuesday, May 14, 2024 9:37 PM
To: Wang, Xiao W <[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]; [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]>; [email protected]; Ben Dooks <[email protected]>
Subject: Re: [PATCH v2] riscv, bpf: Optimize zextw insn with Zba extension

Quoted 9 lines · Xiao W
Quoted 9 lines

-----Original Message-----
From: Andrew Jones <[email protected]>
Sent: Tuesday, May 14, 2024 12:53 AM
To: Wang, Xiao W <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]; [email protected]; [email protected]; [email protected];

[email protected];

Quoted 2 lines
Quoted 2 lines

[email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected];

[email protected];

Quoted 51 lines
Quoted 42 lines

[email protected]; Li, Haicheng <[email protected]>; [email protected]
Subject: Re: [PATCH v2] riscv, bpf: Optimize zextw insn with Zba extension

Quoted 18 lines · Xiao Wang

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

---
v2:
* Add Zba description in the Kconfig. (Lehui)
* Reword the Kconfig help message to make it clearer. (Conor)

---
 arch/riscv/Kconfig       | 22 ++++++++++++++++++++++
 arch/riscv/net/bpf_jit.h | 18 ++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/riscv/Kconfig +0 -0
index 6bec1bce6586..e262a8668b41 100644

arch/riscv/Kconfig config RISCV_ISA_V_PREEMPTIVE · line 587

Quoted 2 lines
arch/riscv/Kconfig +0 -0
@@ -586,6 +586,14 @@ config RISCV_ISA_V_PREEMPTIVE
 	  preemption. Enabling this config will result in higher memory
 	  consumption due to the allocation of per-task's kernel Vector

context.

Quoted 16 lines
arch/riscv/Kconfig +8 -0
+config TOOLCHAIN_HAS_ZBA
+	bool
+	default y
+	depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64ima_zba)
+	depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32ima_zba)
+	depends on LLD_VERSION >= 150000 || LD_VERSION >= 23900
+	depends on AS_HAS_OPTION_ARCH
+
 config TOOLCHAIN_HAS_ZBB
 	bool
 	default y
@@ -601,6 +609,20 @@ config TOOLCHAIN_HAS_VECTOR_CRYPTO
 	def_bool $(as-instr, .option arch$(comma) +v$(comma) +zvkb)
 	depends on AS_HAS_OPTION_ARCH
 
Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 3 lines
arch/riscv/Kconfig +3 -0
+config RISCV_ISA_ZBA
+	bool "Zba extension support for bit manipulation instructions"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

Good point, we don't need toolchain support for this bpf jit case.

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 1 line
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Agree, it's not required. For this bpf jit case, we should drop these two

dependencies.

Quoted 2 lines

BTW, Zbb is used in bpf jit, the usage there also doesn't depend on toolchain

and

Quoted 1 line

RISCV_ALTERNATIVE, but the Kconfig for RISCV_ISA_ZBB has forced the

dependencies

Quoted 2 lines

due to Zbb assembly programming elsewhere.
Maybe we could just dynamically check the existence of RISCV_ISA_ZB*

before jit code

Quoted 8 lines
Quoted 1 line

emission? or introduce new config options for bpf jit? I prefer the first

method and

Quoted 1 line

welcome any comments.

My preferences is to remove as much of the TOOLCHAIN_HAS_ stuff as possible. We should audit the extensions which have them to see if they're really necessary. I don't mind depending on RISCV_ALTERNATIVE, since it's almost required for riscv at this point anyway.

I go through all the existing TOOLCHAIN_HAS_* stuff, all of them are helpful for compiling the corresponding assembly code. So they're really necessary.

For this patch, I would drop the two dependencies for RISCV_ISA_ZBA Kconfig, as the jit doesn't depend on them.

BRs,
Xiao

Quoted 24 lines

Thanks,
drew

Quoted 20 lines

Thanks,
Xiao

[...]

Quoted 15 lines
Quoted 10 lines
arch/riscv/Kconfig +5 -1
 {
+	if (rvzba_enabled()) {
+		emit(rvzba_zextw(rd, rs), ctx);
+		return;
+	}
+
 	emit_slli(rd, rs, 32, ctx);
 	emit_srli(rd, rd, 32, ctx);
 }
--
Quoted 2 lines

2.25.1

Thanks,
drew

#5 lore raw
In reply to Andrew Jones
Quoted 25 lines · Andrew Jones
Quoted 15 lines · Xiao W
Quoted 6 lines

From: Andrew Jones <[email protected]>

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 3 lines
Quoted 1 line

+config RISCV_ISA_ZBA

arch/riscv/Kconfig +2 -0
+	bool "Zba extension support for bit manipulation instructions"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

Good point, we don't need toolchain support for this bpf jit case.

Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 2 lines
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Quoted 8 lines

Agree, it's not required. For this bpf jit case, we should drop these two dependencies.

BTW, Zbb is used in bpf jit, the usage there also doesn't depend on toolchain and RISCV_ALTERNATIVE, but the Kconfig for RISCV_ISA_ZBB has forced the dependencies due to Zbb assembly programming elsewhere.
Maybe we could just dynamically check the existence of RISCV_ISA_ZB* before jit code emission? or introduce new config options for bpf jit? I prefer the first method and welcome any comments.

Quoted 3 lines

My preferences is to remove as much of the TOOLCHAIN_HAS_ stuff as possible. We should audit the extensions which have them to see if they're really necessary.

While I think it is reasonable to allow the "RISCV_ISA_ZBB" option to control whether or not bpf is allowed to use it for optimisations, only allowing bpf to do that if there's toolchain support feels odd to me.. Maybe we need to sorta steal from Charlie's patchset and introduce some hidden options that have the toolchain dep that are used by the alternative macros etc?

I'll have a poke at how bad that looks I think.

Quoted 2 lines

I don't mind depending on RISCV_ALTERNATIVE, since it's almost required for riscv at this point anyway.

As you say, using on riscv_has_extension_likely() doesn't mean you depend on alternatives so effectively all this does is rule out use with XIP, since alternatives are selected when !XIP. Does BPF even work for XIP?

#6 lore raw
Quoted 30 lines · Conor Dooley
Quoted 25 lines · Andrew Jones
Quoted 15 lines · Xiao W
Quoted 6 lines

From: Andrew Jones <[email protected]>

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 3 lines
Quoted 1 line

+config RISCV_ISA_ZBA

arch/riscv/Kconfig +2 -0
+	bool "Zba extension support for bit manipulation instructions"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

Good point, we don't need toolchain support for this bpf jit case.

Quoted 5 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 2 lines
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Quoted 8 lines

Agree, it's not required. For this bpf jit case, we should drop these two dependencies.

BTW, Zbb is used in bpf jit, the usage there also doesn't depend on toolchain and RISCV_ALTERNATIVE, but the Kconfig for RISCV_ISA_ZBB has forced the dependencies due to Zbb assembly programming elsewhere.
Maybe we could just dynamically check the existence of RISCV_ISA_ZB* before jit code emission? or introduce new config options for bpf jit? I prefer the first method and welcome any comments.

Quoted 3 lines

My preferences is to remove as much of the TOOLCHAIN_HAS_ stuff as possible. We should audit the extensions which have them to see if they're really necessary.

Quoted 8 lines

While I think it is reasonable to allow the "RISCV_ISA_ZBB" option to control whether or not bpf is allowed to use it for optimisations, only allowing bpf to do that if there's toolchain support feels odd to me.. Maybe we need to sorta steal from Charlie's patchset and introduce some hidden options that have the toolchain dep that are used by the alternative macros etc?

I'll have a poke at how bad that looks I think.

I don't love this, in particular my option naming, but it would allow the Zbb optimisations in the kernel to not depend on toolchain support while not muddying the Kconfig waters for users:
https://git.kernel.org/pub/scm/linux/kernel/git/conor/linux.git/commit/?h=riscv-zbb_split
A similar model could be followed if there were to be some optimisations for Zba in the future that do require toolchain support:

#7 lore raw
Quoted 61 lines

-----Original Message-----
From: Conor Dooley <[email protected]>
Sent: Wednesday, May 15, 2024 5:33 PM
To: Andrew Jones <[email protected]>
Cc: Wang, Xiao W <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [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]; Li, Haicheng <[email protected]>; [email protected]; Ben Dooks <[email protected]>
Subject: Re: [PATCH v2] riscv, bpf: Optimize zextw insn with Zba extension

Quoted 12 lines · Conor Dooley
Quoted 11 lines · Andrew Jones
Quoted 10 lines · Xiao W
Quoted 6 lines

From: Andrew Jones <[email protected]>

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 614

Quoted 3 lines
Quoted 1 line

+config RISCV_ISA_ZBA

arch/riscv/Kconfig +2 -0
+	bool "Zba extension support for bit manipulation instructions"
+	depends on TOOLCHAIN_HAS_ZBA

We handcraft the instruction, so why do we need toolchain support?

Good point, we don't need toolchain support for this bpf jit case.

Quoted 1 line
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

arch/riscv/Kconfig config TOOLCHAIN_HAS_VECTOR_CRYPTO · line 615

Quoted 1 line
arch/riscv/Kconfig +1 -0
+	depends on RISCV_ALTERNATIVE

Also, while riscv_has_extension_likely() will be accelerated with RISCV_ALTERNATIVE, it's not required.

Agree, it's not required. For this bpf jit case, we should drop these two

dependencies.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

BTW, Zbb is used in bpf jit, the usage there also doesn't depend on

toolchain and

Quoted 1 line
Quoted 1 line
Quoted 1 line

RISCV_ALTERNATIVE, but the Kconfig for RISCV_ISA_ZBB has forced the

dependencies

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

due to Zbb assembly programming elsewhere.
Maybe we could just dynamically check the existence of RISCV_ISA_ZB*

before jit code

Quoted 1 line
Quoted 1 line
Quoted 1 line

emission? or introduce new config options for bpf jit? I prefer the first

method and

Quoted 13 lines
Quoted 5 lines
Quoted 1 line

welcome any comments.

My preferences is to remove as much of the TOOLCHAIN_HAS_ stuff as possible. We should audit the extensions which have them to see if they're really necessary.

While I think it is reasonable to allow the "RISCV_ISA_ZBB" option to control whether or not bpf is allowed to use it for optimisations, only allowing bpf to do that if there's toolchain support feels odd to me.. Maybe we need to sorta steal from Charlie's patchset and introduce some hidden options that have the toolchain dep that are used by the alternative macros etc?

Quoted 7 lines
Quoted 1 line

I'll have a poke at how bad that looks I think.

I don't love this, in particular my option naming, but it would allow the Zbb optimisations in the kernel to not depend on toolchain support while not muddying the Kconfig waters for users:
https://git.kernel.org/pub/scm/linux/kernel/git/conor/linux.git/commit/?h=ri
scv-zbb_split

In that patch, I think the bpt jit part should check IS_ENABLED(CONFIG_RISCV_ISA_ZBB) rather than IS_ENABLED(CONFIG_RISCV_ISA_ZBB_ALT).

Quoted 2 lines

A similar model could be followed if there were to be some optimisations for Zba in the future that do require toolchain support:

Though this model introduces extra hidden Kconfig option, it does provide finer config granularity. This should be a separate patch in the future, we can discuss about the option naming there.

BRs,
Xiao

#8 lore raw
Quoted 14 lines · Xiao W
Quoted 14 lines

From: Conor Dooley <[email protected]>

Quoted 12 lines
Quoted 3 lines

My preferences is to remove as much of the TOOLCHAIN_HAS_ stuff as possible. We should audit the extensions which have them to see if they're really necessary.

While I think it is reasonable to allow the "RISCV_ISA_ZBB" option to control whether or not bpf is allowed to use it for optimisations, only allowing bpf to do that if there's toolchain support feels odd to me.. Maybe we need to sorta steal from Charlie's patchset and introduce some hidden options that have the toolchain dep that are used by the alternative macros etc?

I'll have a poke at how bad that looks I think.

Quoted 8 lines
Quoted 5 lines

I don't love this, in particular my option naming, but it would allow the Zbb optimisations in the kernel to not depend on toolchain support while not muddying the Kconfig waters for users:
https://git.kernel.org/pub/scm/linux/kernel/git/conor/linux.git/commit/?h=ri
scv-zbb_split

In that patch, I think the bpt jit part should check IS_ENABLED(CONFIG_RISCV_ISA_ZBB) rather than IS_ENABLED(CONFIG_RISCV_ISA_ZBB_ALT).

D'oh, you're right. The bpf code being different was meant to be the whole point of the change...

Quoted 6 lines
Quoted 2 lines

A similar model could be followed if there were to be some optimisations for Zba in the future that do require toolchain support:

Though this model introduces extra hidden Kconfig option, it does provide finer config granularity. This should be a separate patch in the future, we can discuss about the option naming there.

Yeah, not expecting you to do this as part of this patch.

Thanks,
Conor.