Skip to contentSkip to conversation

Zbb support and code simplification for RV64 JIT

PATCH RESEND Applied Pu Lehui 6 participants 26 messages Jan 15, 2024 – Jan 31, 2024

Subsystems (6)
lore
Share & tools
lore

Pu Lehui

author
#0 lore raw

v3 cover Zbb support and code simplification for RV64 JIT

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

v2: https://lore.kernel.org/bpf/[email protected]
- Add runtime detection for Zbb instructions.
- Correct formatting issues detected by checkpatch.

v1: https://lore.kernel.org/bpf/[email protected]

Pu Lehui (6):
riscv, bpf: Unify 32-bit sign-extension to emit_sextw riscv, bpf: Unify 32-bit zero-extension to emit_zextw riscv, bpf: Simplify sext and zext logics in branch instructions riscv, bpf: Add necessary Zbb instructions
riscv, bpf: Optimize sign-extention mov insns with Zbb support riscv, bpf: Optimize bswap insns with Zbb support

 arch/riscv/net/bpf_jit.h        | 134 ++++++++++++++++++++
 arch/riscv/net/bpf_jit_comp64.c | 210 +++++++++++---------------------
 2 files changed, 205 insertions(+), 139 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH RESEND v3 1/6 riscv, bpf: Unify 32-bit sign-extension to emit_sextw

From: Pu Lehui <[email protected]>

For code unification, add emit_sextw wrapper to unify all the 32-bit sign-extension operations.

---
 arch/riscv/net/bpf_jit.h        |  5 +++++
 arch/riscv/net/bpf_jit_comp64.c | 10 +++++-----
 2 files changed, 10 insertions(+), 5 deletions(-)
arch/riscv/net/bpf_jit.h +5 -0
index a5ce1ab76ece..f9f8d86e762f 100644
@@ -1087,6 +1087,11 @@ static inline void emit_subw(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
 		emit(rv_subw(rd, rs1, rs2), ctx);
 }
 
+static inline void emit_sextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
+{
+	emit_addiw(rd, rs, 0, ctx);
+}
+
 #endif /* __riscv_xlen == 64 */
 
 void bpf_jit_build_prologue(struct rv_jit_context *ctx);
arch/riscv/net/bpf_jit_comp64.c +5 -5
index 58dc64dd94a8..73f8a0938ada 100644
@@ -417,8 +417,8 @@ static void emit_zext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
 
 static void emit_sext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
 {
-	emit_addiw(RV_REG_T2, *rd, 0, ctx);
-	emit_addiw(RV_REG_T1, *rs, 0, ctx);
+	emit_sextw(RV_REG_T2, *rd, ctx);
+	emit_sextw(RV_REG_T1, *rs, ctx);
 	*rd = RV_REG_T2;
 	*rs = RV_REG_T1;
 }
@@ -433,7 +433,7 @@ static void emit_zext_32_rd_t1(u8 *rd, struct rv_jit_context *ctx)
 
 static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
 {
-	emit_addiw(RV_REG_T2, *rd, 0, ctx);
+	emit_sextw(RV_REG_T2, *rd, ctx);
 	*rd = RV_REG_T2;
 }
 
@@ -1103,7 +1103,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 			emit_srai(rd, RV_REG_T1, 64 - insn->off, ctx);
 			break;
 		case 32:
-			emit_addiw(rd, rs, 0, ctx);
+			emit_sextw(rd, rs, ctx);
 			break;
 		}
 		if (!is64 && !aux->verifier_zext)
@@ -1503,7 +1503,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 		 * as t1 is used only in comparison against zero.
 		 */
 		if (!is64 && imm < 0)
-			emit_addiw(RV_REG_T1, RV_REG_T1, 0, ctx);
+			emit_sextw(RV_REG_T1, RV_REG_T1, ctx);
 		e = ctx->ninsns;
 		rvoff -= ninsns_rvoff(e - s);
 		emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
Signature · 1 line
2.34.1
author
#2 lore raw

PATCH RESEND v3 2/6 riscv, bpf: Unify 32-bit zero-extension to emit_zextw

In reply to Pu Lehui

From: Pu Lehui <[email protected]>

For code unification, add emit_zextw wrapper to unify all the 32-bit zero-extension operations.

---
 arch/riscv/net/bpf_jit.h        |  6 +++
 arch/riscv/net/bpf_jit_comp64.c | 80 +++++++++++++++------------------
 2 files changed, 43 insertions(+), 43 deletions(-)
arch/riscv/net/bpf_jit.h +6 -0
index f9f8d86e762f..e30501b46f8f 100644
@@ -1092,6 +1092,12 @@ static inline void emit_sextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
 	emit_addiw(rd, rs, 0, ctx);
 }
 
+static inline void emit_zextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
+{
+	emit_slli(rd, rs, 32, ctx);
+	emit_srli(rd, rd, 32, ctx);
+}
+
 #endif /* __riscv_xlen == 64 */
 
 void bpf_jit_build_prologue(struct rv_jit_context *ctx);
arch/riscv/net/bpf_jit_comp64.c +37 -43

287 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#3 lore raw

PATCH RESEND v3 3/6 riscv, bpf: Simplify sext and zext logics in branch instructions

In reply to Pu Lehui

From: Pu Lehui <[email protected]>

There are many extension helpers in the current branch instructions, and the implementation is a bit complicated. We simplify this logic through two simple extension helpers with alternate register.

---
 arch/riscv/net/bpf_jit_comp64.c | 79 +++++++++++++--------------------
 1 file changed, 31 insertions(+), 48 deletions(-)
arch/riscv/net/bpf_jit_comp64.c +31 -48
index 20deb906e495..d90784674677 100644
@@ -141,6 +141,19 @@ static bool in_auipc_jalr_range(s64 val)
 		val < ((1L << 31) - (1L << 11));
 }
 
+/* Modify rd pointer to alternate reg to avoid corrupting original reg */
+static void emit_sextw_alt(u8 *rd, u8 ra, struct rv_jit_context *ctx)
+{
+	emit_sextw(ra, *rd, ctx);
+	*rd = ra;
+}
+
+static void emit_zextw_alt(u8 *rd, u8 ra, struct rv_jit_context *ctx)
+{
+	emit_zextw(ra, *rd, ctx);
+	*rd = ra;
+}
+
 /* Emit fixed-length instructions for address */
 static int emit_addr(u8 rd, u64 addr, bool extra_pass, struct rv_jit_context *ctx)
 {
@@ -399,38 +412,6 @@ static void init_regs(u8 *rd, u8 *rs, const struct bpf_insn *insn,
 		*rs = bpf_to_rv_reg(insn->src_reg, ctx);
 }
 
-static void emit_zext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
-{
-	emit_mv(RV_REG_T2, *rd, ctx);
-	emit_zextw(RV_REG_T2, RV_REG_T2, ctx);
-	emit_mv(RV_REG_T1, *rs, ctx);
-	emit_zextw(RV_REG_T1, RV_REG_T1, ctx);
-	*rd = RV_REG_T2;
-	*rs = RV_REG_T1;
-}
-
-static void emit_sext_32_rd_rs(u8 *rd, u8 *rs, struct rv_jit_context *ctx)
-{
-	emit_sextw(RV_REG_T2, *rd, ctx);
-	emit_sextw(RV_REG_T1, *rs, ctx);
-	*rd = RV_REG_T2;
-	*rs = RV_REG_T1;
-}
-
-static void emit_zext_32_rd_t1(u8 *rd, struct rv_jit_context *ctx)
-{
-	emit_mv(RV_REG_T2, *rd, ctx);
-	emit_zextw(RV_REG_T2, RV_REG_T2, ctx);
-	emit_zextw(RV_REG_T1, RV_REG_T2, ctx);
-	*rd = RV_REG_T2;
-}
-
-static void emit_sext_32_rd(u8 *rd, struct rv_jit_context *ctx)
-{
-	emit_sextw(RV_REG_T2, *rd, ctx);
-	*rd = RV_REG_T2;
-}
-
 static int emit_jump_and_link(u8 rd, s64 rvoff, bool fixed_addr,
 			      struct rv_jit_context *ctx)
 {
@@ -1418,10 +1399,13 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 		rvoff = rv_offset(i, off, ctx);
 		if (!is64) {
 			s = ctx->ninsns;
-			if (is_signed_bpf_cond(BPF_OP(code)))
-				emit_sext_32_rd_rs(&rd, &rs, ctx);
-			else
-				emit_zext_32_rd_rs(&rd, &rs, ctx);
+			if (is_signed_bpf_cond(BPF_OP(code))) {
+				emit_sextw_alt(&rs, RV_REG_T1, ctx);
+				emit_sextw_alt(&rd, RV_REG_T2, ctx);
+			} else {
+				emit_zextw_alt(&rs, RV_REG_T1, ctx);
+				emit_zextw_alt(&rd, RV_REG_T2, ctx);
+			}
 			e = ctx->ninsns;
 
 			/* Adjust for extra insns */
@@ -1432,8 +1416,7 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 			/* Adjust for and */
 			rvoff -= 4;
 			emit_and(RV_REG_T1, rd, rs, ctx);
-			emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff,
-				    ctx);
+			emit_branch(BPF_JNE, RV_REG_T1, RV_REG_ZERO, rvoff, ctx);
 		} else {
 			emit_branch(BPF_OP(code), rd, rs, rvoff, ctx);
 		}
@@ -1462,18 +1445,18 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 	case BPF_JMP32 | BPF_JSLE | BPF_K:
 		rvoff = rv_offset(i, off, ctx);
 		s = ctx->ninsns;
-		if (imm) {
+		if (imm)
 			emit_imm(RV_REG_T1, imm, ctx);
-			rs = RV_REG_T1;
-		} else {
-			/* If imm is 0, simply use zero register. */
-			rs = RV_REG_ZERO;
-		}
+		rs = imm ? RV_REG_T1 : RV_REG_ZERO;
 		if (!is64) {
-			if (is_signed_bpf_cond(BPF_OP(code)))
-				emit_sext_32_rd(&rd, ctx);
-			else
-				emit_zext_32_rd_t1(&rd, ctx);
+			if (is_signed_bpf_cond(BPF_OP(code))) {
+				emit_sextw_alt(&rd, RV_REG_T2, ctx);
+				/* rs has been sign extended */
+			} else {
+				emit_zextw_alt(&rd, RV_REG_T2, ctx);
+				if (imm)
+					emit_zextw(rs, rs, ctx);
+			}
 		}
 		e = ctx->ninsns;
 
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH RESEND v3 4/6 riscv, bpf: Add necessary Zbb instructions

In reply to Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
 arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +32 -0
index e30501b46f8f..51f6d214086f 100644
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
 	return IS_ENABLED(CONFIG_RISCV_ISA_C);
 }
 
+static inline bool rvzbb_enabled(void)
+{
+	return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);
+}
+
 enum {
 	RV_REG_ZERO =	0,	/* The constant value 0 */
 	RV_REG_RA =	1,	/* Return address */
@@ -730,6 +735,33 @@ static inline u16 rvc_swsp(u32 imm8, u8 rs2)
 	return rv_css_insn(0x6, imm, rs2, 0x2);
 }
 
+/* RVZBB instrutions. */
+static inline u32 rvzbb_sextb(u8 rd, u8 rs1)
+{
+	return rv_i_insn(0x604, rs1, 1, rd, 0x13);
+}
+
+static inline u32 rvzbb_sexth(u8 rd, u8 rs1)
+{
+	return rv_i_insn(0x605, rs1, 1, rd, 0x13);
+}
+
+static inline u32 rvzbb_zexth(u8 rd, u8 rs)
+{
+	if (IS_ENABLED(CONFIG_64BIT))
+		return rv_i_insn(0x80, rs, 4, rd, 0x3b);
+
+	return rv_i_insn(0x80, rs, 4, rd, 0x33);
+}
+
+static inline u32 rvzbb_rev8(u8 rd, u8 rs)
+{
+	if (IS_ENABLED(CONFIG_64BIT))
+		return rv_i_insn(0x6b8, rs, 5, rd, 0x13);
+
+	return rv_i_insn(0x698, rs, 5, rd, 0x13);
+}
+
 /*
  * RV64-only instructions.
  *
Signature · 1 line
2.34.1
maintainer
#5 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
 arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 23

Quoted 6 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
 	return IS_ENABLED(CONFIG_RISCV_ISA_C);
 }
 
+static inline bool rvzbb_enabled(void)
+{
+	return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Björn

maintainer
#6 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 29 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
  arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
  1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 23

Quoted 6 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
  	return IS_ENABLED(CONFIG_RISCV_ISA_C);
  }
 
+static inline bool rvzbb_enabled(void)
+{
+	return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

Quoted 4 lines

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

Quoted 2 lines

Björn

maintainer
#7 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 29 lines · Pu Lehui
Quoted 28 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
  arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++

  1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 22

Quoted 5 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
       return IS_ENABLED(CONFIG_RISCV_ISA_C);
   }
+static inline bool rvzbb_enabled(void)
+{
+    return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

Quoted 5 lines
Quoted 3 lines

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

So if I understand you correctly, only relying on the riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) part would not work - iow, the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) is mandatory here?

Thanks,
Daniel

P.s.: Given Bjorn's review and tests I took the series into bpf-next now. Thanks everyone!

maintainer
#8 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 31 lines · Daniel Borkmann
Quoted 30 lines · Pu Lehui
Quoted 29 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
  arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++

  1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 23

Quoted 6 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
       return IS_ENABLED(CONFIG_RISCV_ISA_C);
   }
+static inline bool rvzbb_enabled(void)
+{
+    return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && 
 riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

So if I understand you correctly, only relying on the riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) part would not work - iow, the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) is mandatory here?

Yes, it should be IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB).

Quoted 4 lines

Thanks,
Daniel

P.s.: Given Bjorn's review and tests I took the series into bpf-next now. Thanks everyone!

Thanks Daniel and Björn

maintainer
#9 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

In reply to Daniel Borkmann
Quoted 34 lines · Daniel Borkmann
Quoted 33 lines · Pu Lehui
Quoted 31 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
  arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++

  1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 22

Quoted 5 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
       return IS_ENABLED(CONFIG_RISCV_ISA_C);
   }
+static inline bool rvzbb_enabled(void)
+{
+    return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Quoted 5 lines
Quoted 5 lines

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

What I'm trying to say (and drew as well in the other reply) is that "riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n" should also make the JIT emit Zbb insns. The platform check should be sufficient.

Quoted 9 lines

So if I understand you correctly, only relying on the riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) part would not work - iow, the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) is mandatory here?

Thanks,
Daniel

P.s.: Given Bjorn's review and tests I took the series into bpf-next now. Thanks everyone!

Thanks! Yes, this is mainly a semantic discussion, and it can be further relaxed later with a follow up -- if applicable.

Björn

maintainer
#10 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 42 lines · Björn Töpel
Quoted 34 lines · Daniel Borkmann
Quoted 33 lines · Pu Lehui
Quoted 31 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
   arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
   1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 22

Quoted 5 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
       return IS_ENABLED(CONFIG_RISCV_ISA_C);
   }
+static inline bool rvzbb_enabled(void)
+{
+    return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Quoted 5 lines
Quoted 5 lines

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

Quoted 4 lines

What I'm trying to say (and drew as well in the other reply) is that "riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n" should also make the JIT emit Zbb insns. The platform check should be sufficient.

Ooh, this is really beyond my expectation. The test_progs can pass when with only platform check and it can recognize the zbb instructions. Now I know it. Sorry for misleading.🙁

Curious if CONFIG_RISCV_ISA_ZBB is still necessary?

Quoted 16 lines
Quoted 10 lines

So if I understand you correctly, only relying on the riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) part would not work - iow, the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) is mandatory here?

Thanks,
Daniel

P.s.: Given Bjorn's review and tests I took the series into bpf-next now. Thanks everyone!

Thanks! Yes, this is mainly a semantic discussion, and it can be further relaxed later with a follow up -- if applicable.

Björn

maintainer
#11 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 48 lines · Pu Lehui
Quoted 42 lines · Björn Töpel
Quoted 34 lines · Daniel Borkmann
Quoted 33 lines · Pu Lehui
Quoted 31 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
   arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
   1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 22

Quoted 5 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
       return IS_ENABLED(CONFIG_RISCV_ISA_C);
   }
+static inline bool rvzbb_enabled(void)
+{
+    return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Quoted 5 lines
Quoted 5 lines

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

Quoted 4 lines

What I'm trying to say (and drew as well in the other reply) is that "riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n" should also make the JIT emit Zbb insns. The platform check should be sufficient.

Quoted 5 lines

Ooh, this is really beyond my expectation. The test_progs can pass when with only platform check and it can recognize the zbb instructions. Now I know it. Sorry for misleading.🙁

Curious if CONFIG_RISCV_ISA_ZBB is still necessary?

You don't need IS_ENABLED(CONFIG_RISCV_ISA_ZBB) for the JIT, but the kernel needs it.

Feel free to follow up with a patch to remove it.

Cheers,
Björn

author
#12 lore raw

Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

Quoted 54 lines · Björn Töpel
Quoted 52 lines · Pu Lehui
Quoted 42 lines · Björn Töpel
Quoted 34 lines · Daniel Borkmann
Quoted 33 lines · Pu Lehui
Quoted 31 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
    arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
    1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 22

Quoted 5 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
        return IS_ENABLED(CONFIG_RISCV_ISA_C);
    }
+static inline bool rvzbb_enabled(void)
+{
+    return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

Quoted 5 lines
Quoted 5 lines

Not enough, because riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) is a platform capability check, and the other one is a kernel image capability check. We can pass the check riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n. And my local test prove it.

Quoted 4 lines

What I'm trying to say (and drew as well in the other reply) is that "riscv_has_extension_likely(RISCV_ISA_EXT_ZBB) when CONFIG_RISCV_ISA_ZBB=n" should also make the JIT emit Zbb insns. The platform check should be sufficient.

Ooh, this is really beyond my expectation. The test_progs can pass when with only platform check and it can recognize the zbb instructions. Now I know it. Sorry for misleading.🙁

Quoted 7 lines
Quoted 1 line

Curious if CONFIG_RISCV_ISA_ZBB is still necessary?

You don't need IS_ENABLED(CONFIG_RISCV_ISA_ZBB) for the JIT, but the kernel needs it.

Feel free to follow up with a patch to remove it.

Maybe we can implement more extensions about bitmanip.

Quoted 3 lines

Cheers,
Björn

#13 lore raw

Re: Re: [PATCH RESEND bpf-next v3 4/6] riscv, bpf: Add necessary Zbb instructions

In reply to Björn Töpel
Quoted 29 lines · Björn Töpel
Quoted 17 lines · Pu Lehui

From: Pu Lehui <[email protected]>

Add necessary Zbb instructions introduced by [0] to reduce code size and improve performance of RV64 JIT. Meanwhile, a runtime deteted helper is added to check whether the CPU supports Zbb instructions.

---
 arch/riscv/net/bpf_jit.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
arch/riscv/net/bpf_jit.h +0 -0
index e30501b46f8f..51f6d214086f 100644

arch/riscv/net/bpf_jit.h static inline bool rvc_enabled(void) · line 23

Quoted 6 lines
arch/riscv/net/bpf_jit.h +3 -0
@@ -18,6 +18,11 @@ static inline bool rvc_enabled(void)
 	return IS_ENABLED(CONFIG_RISCV_ISA_C);
 }
 
+static inline bool rvzbb_enabled(void)
+{
+	return IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && riscv_has_extension_likely(RISCV_ISA_EXT_ZBB);

Hmm, I'm thinking about the IS_ENABLED(CONFIG_RISCV_ISA_ZBB) semantics for a kernel JIT compiler.

Quoted 4 lines

IS_ENABLED(CONFIG_RISCV_ISA_ZBB) affects the kernel compiler flags. Should it be enough to just have the run-time check? Should a kernel built w/o Zbb be able to emit Zbb from the JIT?

My two cents (which might be worth less than two cents due to my lack of BPF knowledge) is yes, the JIT should be allowed to emit Zbb instructions even when the kernel is not built with a compiler which has done so. In fact, we have insn-def.h for situations similar to this.

Thanks,
drew

author
#14 lore raw

PATCH RESEND v3 5/6 riscv, bpf: Optimize sign-extention mov insns with Zbb support

In reply to Pu Lehui

From: Pu Lehui <[email protected]>

Add 8-bit and 16-bit sign-extention wraper with Zbb support to optimize sign-extension mov instructions.

---
 arch/riscv/net/bpf_jit.h        | 22 ++++++++++++++++++++++
 arch/riscv/net/bpf_jit_comp64.c |  5 +++--
 2 files changed, 25 insertions(+), 2 deletions(-)
arch/riscv/net/bpf_jit.h +22 -0
index 51f6d214086f..b00c5c0591d2 100644
@@ -1119,6 +1119,28 @@ static inline void emit_subw(u8 rd, u8 rs1, u8 rs2, struct rv_jit_context *ctx)
 		emit(rv_subw(rd, rs1, rs2), ctx);
 }
 
+static inline void emit_sextb(u8 rd, u8 rs, struct rv_jit_context *ctx)
+{
+	if (rvzbb_enabled()) {
+		emit(rvzbb_sextb(rd, rs), ctx);
+		return;
+	}
+
+	emit_slli(rd, rs, 56, ctx);
+	emit_srai(rd, rd, 56, ctx);
+}
+
+static inline void emit_sexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
+{
+	if (rvzbb_enabled()) {
+		emit(rvzbb_sexth(rd, rs), ctx);
+		return;
+	}
+
+	emit_slli(rd, rs, 48, ctx);
+	emit_srai(rd, rd, 48, ctx);
+}
+
 static inline void emit_sextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
 {
 	emit_addiw(rd, rs, 0, ctx);
arch/riscv/net/bpf_jit_comp64.c +3 -2
index d90784674677..18bbf8122eb3 100644
@@ -1073,9 +1073,10 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx,
 			emit_mv(rd, rs, ctx);
 			break;
 		case 8:
+			emit_sextb(rd, rs, ctx);
+			break;
 		case 16:
-			emit_slli(RV_REG_T1, rs, 64 - insn->off, ctx);
-			emit_srai(rd, RV_REG_T1, 64 - insn->off, ctx);
+			emit_sexth(rd, rs, ctx);
 			break;
 		case 32:
 			emit_sextw(rd, rs, ctx);
Signature · 1 line
2.34.1
author
#15 lore raw

PATCH RESEND v3 6/6 riscv, bpf: Optimize bswap insns with Zbb support

In reply to Pu Lehui

From: Pu Lehui <[email protected]>

Optimize bswap instructions by rev8 Zbb instruction conbined with srli instruction. And Optimize 16-bit zero-extension with Zbb support.

---
 arch/riscv/net/bpf_jit.h        | 69 +++++++++++++++++++++++++++++++++
 arch/riscv/net/bpf_jit_comp64.c | 50 +-----------------------
 2 files changed, 71 insertions(+), 48 deletions(-)
arch/riscv/net/bpf_jit.h +69 -0
index b00c5c0591d2..8b35f12a4452 100644
@@ -1146,12 +1146,81 @@ static inline void emit_sextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
 	emit_addiw(rd, rs, 0, ctx);
 }
 
+static inline void emit_zexth(u8 rd, u8 rs, struct rv_jit_context *ctx)
+{
+	if (rvzbb_enabled()) {
+		emit(rvzbb_zexth(rd, rs), ctx);
+		return;
+	}
+
+	emit_slli(rd, rs, 48, ctx);
+	emit_srli(rd, rd, 48, ctx);
+}
+
 static inline void emit_zextw(u8 rd, u8 rs, struct rv_jit_context *ctx)
 {
 	emit_slli(rd, rs, 32, ctx);
 	emit_srli(rd, rd, 32, ctx);
 }
 
+static inline void emit_bswap(u8 rd, s32 imm, struct rv_jit_context *ctx)
+{
+	if (rvzbb_enabled()) {
+		int bits = 64 - imm;
+
+		emit(rvzbb_rev8(rd, rd), ctx);
+		if (bits)
+			emit_srli(rd, rd, bits, ctx);
+		return;
+	}
+
+	emit_li(RV_REG_T2, 0, ctx);
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+	if (imm == 16)
+		goto out_be;
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+	if (imm == 32)
+		goto out_be;
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+	emit_slli(RV_REG_T2, RV_REG_T2, 8, ctx);
+	emit_srli(rd, rd, 8, ctx);
+out_be:
+	emit_andi(RV_REG_T1, rd, 0xff, ctx);
+	emit_add(RV_REG_T2, RV_REG_T2, RV_REG_T1, ctx);
+
+	emit_mv(rd, RV_REG_T2, ctx);
+}
+
 #endif /* __riscv_xlen == 64 */
 
 void bpf_jit_build_prologue(struct rv_jit_context *ctx);
arch/riscv/net/bpf_jit_comp64.c +2 -48

64 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#16 lore raw
In reply to Pu Lehui
Quoted 5 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

Quoted 7 lines

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Björn

author
#17 lore raw
Quoted 12 lines · Björn Töpel
Quoted 10 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

Quoted 9 lines
Quoted 2 lines

v3:
- Change to early-exit code style and make code more explicit.

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

arch/riscv/net/bpf_jit_comp64.c +0 -0
index 42cfd1ed295e..5c4e0ac389d0 100644
@@ -795,6 +795,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,
        struct bpf_tramp_links *fentry = &tlinks[BPF_TRAMP_FENTRY];
        struct bpf_tramp_links *fexit = &tlinks[BPF_TRAMP_FEXIT];
        struct bpf_tramp_links *fmod_ret = 

&tlinks[BPF_TRAMP_MODIFY_RETURN];

+       bool is_struct_ops = flags & BPF_TRAMP_F_INDIRECT;
        void *orig_call = func_addr;
        bool save_ret;
        u32 insn;

@@ -878,7 +879,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,

        stack_size = round_up(stack_size, 16);

- if (func_addr) {
+ if (!is_struct_ops) {
/* For the trampoline called from function entry,
* the frame of traced function and the frame of
* trampoline need to be considered.
@@ -998,7 +999,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im,

        emit_ld(RV_REG_S1, -sreg_off, RV_REG_FP, ctx);

- if (func_addr) {

+       if (!is_struct_ops) {
                /* trampoline called from function entry */
                emit_ld(RV_REG_T0, stack_size - 8, RV_REG_SP, ctx);
                emit_ld(RV_REG_FP, stack_size - 16, RV_REG_SP, ctx);
Quoted 2 lines

Björn

maintainer
#18 lore raw
Quoted 16 lines · Pu Lehui
Quoted 15 lines · Björn Töpel
Quoted 12 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Quoted 8 lines
Quoted 6 lines

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

Awesome, I just started bisecting the following test_progs sub-test fails on 6.8-rc1:

bpf_iter_setsockopt:

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
Oops [#1]
Modules linked in: bpf_testmod(OE) drm fuse i2c_core dm_mod drm_panel_orientation_quirks backlight configfs ip_tables x_tables
CPU: 1 PID: 458 Comm: test_progs Tainted: G           OE      6.8.0-rc1-kselftest_plain #1
Hardware name: riscv-virtio,qemu (DT)

epc : 0x0
ra : tcp_set_ca_state+0x2c/0x9a
epc : 0000000000000000 ra : ffffffff80cdc6b2 sp : ff2000000000b910
gp : ffffffff82587b60 tp : ff60000087ea8040 t0 : 0000000000000000 t1 : ffffffff801ed15e t2 : 0000000000000000 s0 : ff2000000000b930 s1 : ff600000879296c0 a0 : ff20000000497000 a1 : 0000000000000008 a2 : 0000000000000001 a3 : ff60000087ea83a0 a4 : 0000000000000000 a5 : 0000000000000106 a6 : 0000000000000021 a7 : 0000000000000000 s2 : 0000000000000000 s3 : ff60000086878008 s4 : ff60000082ce2f40 s5 : ff60000084f56040 s6 : ff60000087929040 s7 : ff60000086878008 s8 : ff2000000000ba5f s9 : ff60000087928a00 s10: 0000000000000002
s11: ff60000087928040 t3 : 000000000001ffff t4 : 0100000000000000 t5 : 0000000000000000 t6 : ff6000008792a118
status: 0000000200000120 badaddr: 0000000000000000 cause: 000000000000000c

Code: Unable to access instruction at 0xffffffffffffffec.
---[ end trace 0000000000000000 ]---

bpf_tcp_ca:

Unable to handle kernel paging request at virtual address ff60000088554500
Oops [#1]
Modules linked in: iptable_raw xt_connmark bpf_testmod(OE) drm fuse i2c_core drm_panel_orientation_quirks backlight dm_mod configfs ip_tables x_tables [last unloaded: bpf_testmod(OE)]
CPU: 3 PID: 458 Comm: test_progs Tainted: G           OE      6.8.0-rc1-kselftest_plain #1
Hardware name: riscv-virtio,qemu (DT)

epc : 0xff60000088554500
ra : tcp_ack+0x288/0x1232
epc : ff60000088554500 ra : ffffffff80cc7166 sp : ff2000000117ba50
gp : ffffffff82587b60 tp : ff60000087be0040 t0 : ff60000088554500 t1 : ffffffff801ed24e t2 : 0000000000000000 s0 : ff2000000117bbc0 s1 : 0000000000000500 a0 : ff20000000691000 a1 : 0000000000000018 a2 : 0000000000000001 a3 : ff60000087be03a0 a4 : 0000000000000000 a5 : 0000000000000000 a6 : 0000000000000021 a7 : ffffffff8263f880 s2 : 000000004ac3c13b s3 : 000000004ac3c13a s4 : 0000000000008200 s5 : 0000000000000001 s6 : 0000000000000104 s7 : ff2000000117bb00 s8 : ff600000885544c0 s9 : 0000000000000000 s10: ff60000086ff0b80
s11: 000055557983a9c0 t3 : 0000000000000000 t4 : 000000000000ffc4 t5 : ffffffff8154f170 t6 : 0000000000000030
status: 0000000200000120 badaddr: ff60000088554500 cause: 000000000000000c

Code: c796 67d7 0000 0000 0052 0002 c13b 4ac3 0000 0000 (0001) 0000 
---[ end trace 0000000000000000 ]---

dummy_st_ops:

Unable to handle kernel access to user memory without uaccess routines at virtual address 0000000000043022
Oops [#1]
Modules linked in: iptable_raw xt_connmark bpf_testmod(OE) drm fuse i2c_core drm_panel_orientation_quirks backlight dm_mod configfs ip_tables x_tables [last unloaded: bpf_testmod(OE)]
CPU: 1 PID: 452 Comm: test_progs Tainted: G           OE      6.8.0-rc1-kselftest_plain #1
Hardware name: riscv-virtio,qemu (DT)

epc : 0x43022
ra : bpf_struct_ops_test_run+0x188/0x37a
epc : 0000000000043022 ra : ffffffff80c75d1a sp : ff200000002a3ce0
gp : ffffffff82587b60 tp : ff6000008356b840 t0 : 0000000000043023 t1 : ffffffff801ed062 t2 : 000000000000ff00 s0 : ff200000002a3d40 s1 : ffffffff78207000 a0 : fffffffff2f3f4f5 a1 : 0000000000000008 a2 : 0000000000000001 a3 : ff6000008356bba0 a4 : 0000000000000000 a5 : fffffffff2f3f4f5 a6 : 0000000000000021 a7 : 0000000052464e43 s2 : 0000000000000000 s3 : ff60000080b33b80 s4 : 0000000000000084 s5 : ff60000083334c00 s6 : ff60000084861580 s7 : 00007ffff0887668 s8 : 00007fffaa859030 s9 : 0000000000000000 s10: 000055556631ca4c
s11: 000055556631c9c0 t3 : 000000000000000f t4 : 0000000000000800 t5 : 0001000000000000 t6 : ff6000008adb9bf8
status: 0000000200000120 badaddr: 0000000000043022 cause: 000000000000000c

Code: Unable to access instruction at 0x000000000004300e.
---[ end trace 0000000000000000 ]---

Environment: OpenSBI 1.4, Qemu 8.2.0, U-boot UEFI

Is that the same that you see?

I'll take your patch for a spin!

Björn

maintainer Tested-by Acked-by
#19 lore raw
Quoted 18 lines · Björn Töpel
Quoted 16 lines · Pu Lehui
Quoted 15 lines · Björn Töpel
Quoted 12 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

Pu, with your patch bpf_iter_setsockopt, bpf_tcp_ca, and dummy_st_ops passes!

Please spin a proper fixes patch, and feel free to add:

Björn

author
#20 lore raw
Quoted 29 lines · Björn Töpel
Quoted 18 lines · Björn Töpel
Quoted 16 lines · Pu Lehui
Quoted 15 lines · Björn Töpel
Quoted 12 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

Quoted 8 lines

Pu, with your patch bpf_iter_setsockopt, bpf_tcp_ca, and dummy_st_ops passes!

Please spin a proper fixes patch, and feel free to add:

Is that in a hurry? If not, I would like to send it with the upcoming patchset.

Quoted 2 lines

Björn

maintainer
#21 lore raw
Quoted 33 lines · Pu Lehui
Quoted 32 lines · Björn Töpel
Quoted 18 lines · Björn Töpel
Quoted 16 lines · Pu Lehui
Quoted 15 lines · Björn Töpel
Quoted 12 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

Pu, with your patch bpf_iter_setsockopt, bpf_tcp_ca, and dummy_st_ops passes!

Quoted 8 lines
Quoted 5 lines

Please spin a proper fixes patch, and feel free to add:

Is that in a hurry? If not, I would like to send it with the upcoming patchset.

This is a separate fix, right? What patchset are you referring to where the fix would be in?

As of now 6.8-rc1 is broken! It would be a great with a fix asap...

Cheers,
Björn

author
#22 lore raw
Quoted 41 lines · Björn Töpel
Quoted 39 lines · Pu Lehui
Quoted 37 lines · Björn Töpel
Quoted 18 lines · Björn Töpel
Quoted 16 lines · Pu Lehui
Quoted 15 lines · Björn Töpel
Quoted 12 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

Pu, with your patch bpf_iter_setsockopt, bpf_tcp_ca, and dummy_st_ops passes!

Please spin a proper fixes patch, and feel free to add:

Quoted 6 lines
Quoted 2 lines

Is that in a hurry? If not, I would like to send it with the upcoming patchset.

This is a separate fix, right? What patchset are you referring to where the fix would be in?

Yes, you are right! I'll send a bug fix patch as soon as possible. Coming soon is the bpf_prog_pack for RV64 Trampoline, which currently does not seem to be related to the bugfix, will populate the commit message and send it later.😁

Quoted 5 lines

As of now 6.8-rc1 is broken! It would be a great with a fix asap...

Cheers,
Björn

maintainer
#23 lore raw
In reply to Björn Töpel
Quoted 96 lines · Björn Töpel
Quoted 16 lines · Pu Lehui
Quoted 15 lines · Björn Töpel
Quoted 12 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

v3 resend:
- resend for mail be treated as spam.

v3:
- Change to early-exit code style and make code more explicit.

Quoted 8 lines
Quoted 6 lines

Lehui,

Sorry for the delay. I'm chasing a struct_ops RISC-V BPF regression in 6.8-rc1, I will need to wrap my head around that prior reviewing properly.

Oh, I also found the problem with struct ops and fixed it

Awesome, I just started bisecting the following test_progs sub-test fails on 6.8-rc1:

bpf_iter_setsockopt:

Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
Oops [#1]
Modules linked in: bpf_testmod(OE) drm fuse i2c_core dm_mod drm_panel_orientation_quirks backlight configfs ip_tables x_tables
CPU: 1 PID: 458 Comm: test_progs Tainted: G           OE      6.8.0-rc1-kselftest_plain #1
Hardware name: riscv-virtio,qemu (DT)

epc : 0x0
ra : tcp_set_ca_state+0x2c/0x9a
epc : 0000000000000000 ra : ffffffff80cdc6b2 sp : ff2000000000b910
gp : ffffffff82587b60 tp : ff60000087ea8040 t0 : 0000000000000000 t1 : ffffffff801ed15e t2 : 0000000000000000 s0 : ff2000000000b930 s1 : ff600000879296c0 a0 : ff20000000497000 a1 : 0000000000000008 a2 : 0000000000000001 a3 : ff60000087ea83a0 a4 : 0000000000000000 a5 : 0000000000000106 a6 : 0000000000000021 a7 : 0000000000000000 s2 : 0000000000000000 s3 : ff60000086878008 s4 : ff60000082ce2f40 s5 : ff60000084f56040 s6 : ff60000087929040 s7 : ff60000086878008 s8 : ff2000000000ba5f s9 : ff60000087928a00 s10: 0000000000000002
s11: ff60000087928040 t3 : 000000000001ffff t4 : 0100000000000000 t5 : 0000000000000000 t6 : ff6000008792a118
status: 0000000200000120 badaddr: 0000000000000000 cause: 000000000000000c

Code: Unable to access instruction at 0xffffffffffffffec.
---[ end trace 0000000000000000 ]---

bpf_tcp_ca:

Unable to handle kernel paging request at virtual address ff60000088554500
Oops [#1]
Modules linked in: iptable_raw xt_connmark bpf_testmod(OE) drm fuse i2c_core drm_panel_orientation_quirks backlight dm_mod configfs ip_tables x_tables [last unloaded: bpf_testmod(OE)]
CPU: 3 PID: 458 Comm: test_progs Tainted: G           OE      6.8.0-rc1-kselftest_plain #1
Hardware name: riscv-virtio,qemu (DT)

epc : 0xff60000088554500
ra : tcp_ack+0x288/0x1232
epc : ff60000088554500 ra : ffffffff80cc7166 sp : ff2000000117ba50
gp : ffffffff82587b60 tp : ff60000087be0040 t0 : ff60000088554500 t1 : ffffffff801ed24e t2 : 0000000000000000 s0 : ff2000000117bbc0 s1 : 0000000000000500 a0 : ff20000000691000 a1 : 0000000000000018 a2 : 0000000000000001 a3 : ff60000087be03a0 a4 : 0000000000000000 a5 : 0000000000000000 a6 : 0000000000000021 a7 : ffffffff8263f880 s2 : 000000004ac3c13b s3 : 000000004ac3c13a s4 : 0000000000008200 s5 : 0000000000000001 s6 : 0000000000000104 s7 : ff2000000117bb00 s8 : ff600000885544c0 s9 : 0000000000000000 s10: ff60000086ff0b80
s11: 000055557983a9c0 t3 : 0000000000000000 t4 : 000000000000ffc4 t5 : ffffffff8154f170 t6 : 0000000000000030
status: 0000000200000120 badaddr: ff60000088554500 cause: 000000000000000c

Code: c796 67d7 0000 0000 0052 0002 c13b 4ac3 0000 0000 (0001) 0000
---[ end trace 0000000000000000 ]---

dummy_st_ops:

Unable to handle kernel access to user memory without uaccess routines at virtual address 0000000000043022
Oops [#1]
Modules linked in: iptable_raw xt_connmark bpf_testmod(OE) drm fuse i2c_core drm_panel_orientation_quirks backlight dm_mod configfs ip_tables x_tables [last unloaded: bpf_testmod(OE)]
CPU: 1 PID: 452 Comm: test_progs Tainted: G           OE      6.8.0-rc1-kselftest_plain #1
Hardware name: riscv-virtio,qemu (DT)

epc : 0x43022
ra : bpf_struct_ops_test_run+0x188/0x37a
epc : 0000000000043022 ra : ffffffff80c75d1a sp : ff200000002a3ce0
gp : ffffffff82587b60 tp : ff6000008356b840 t0 : 0000000000043023 t1 : ffffffff801ed062 t2 : 000000000000ff00 s0 : ff200000002a3d40 s1 : ffffffff78207000 a0 : fffffffff2f3f4f5 a1 : 0000000000000008 a2 : 0000000000000001 a3 : ff6000008356bba0 a4 : 0000000000000000 a5 : fffffffff2f3f4f5 a6 : 0000000000000021 a7 : 0000000052464e43 s2 : 0000000000000000 s3 : ff60000080b33b80 s4 : 0000000000000084 s5 : ff60000083334c00 s6 : ff60000084861580 s7 : 00007ffff0887668 s8 : 00007fffaa859030 s9 : 0000000000000000 s10: 000055556631ca4c
s11: 000055556631c9c0 t3 : 000000000000000f t4 : 0000000000000800 t5 : 0001000000000000 t6 : ff6000008adb9bf8
status: 0000000200000120 badaddr: 0000000000043022 cause: 000000000000000c

Quoted 6 lines
Code: Unable to access instruction at 0x000000000004300e.
---[ end trace 0000000000000000 ]---

Environment: OpenSBI 1.4, Qemu 8.2.0, U-boot UEFI

Is that the same that you se >

Yes, is the same issue. The question is that the commit 2cd3e3772e41 ("x86/cfi,bpf: Fix bpf_struct_ops CFI") change func_addr of arch_prepare_bpf_trampoline from NULL to not NULL, while we use func_addr to distinguish struct_ops and regular trampoline. After commit 2cd3e3772e41, we can use BPF_TRAMP_F_INDIRECT to distinguish them as it always be set in struct_ops.

Quoted 4 lines

I'll take your patch for a spin!

Björn

maintainer Tested-by Acked-by on the v3 series
#24 lore raw
In reply to Pu Lehui
Quoted 4 lines · Pu Lehui

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

Lehui, apologies for the delay. Nice work! I have a minor comment in patch 4, but not a blocker...

For the series,

#25 lore raw
In reply to Pu Lehui
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 Mon, 15 Jan 2024 13:12:29 +0000 you wrote:

Add Zbb support [0] to optimize code size and performance of RV64 JIT. Meanwhile, adjust the code for unification and simplification. Tests test_bpf.ko and test_verifier have passed, as well as the relative testcases of test_progs*.

[...]

Here is the summary with links:
- [RESEND,bpf-next,v3,1/6] riscv, bpf: Unify 32-bit sign-extension to emit_sextw

    https://git.kernel.org/bpf/bpf-next/c/e33758f7493c
  - [RESEND,bpf-next,v3,2/6] riscv, bpf: Unify 32-bit zero-extension to emit_zextw
    https://git.kernel.org/bpf/bpf-next/c/914c7a5ff18a
  - [RESEND,bpf-next,v3,3/6] riscv, bpf: Simplify sext and zext logics in branch instructions
    https://git.kernel.org/bpf/bpf-next/c/361db44c3c59
  - [RESEND,bpf-next,v3,4/6] riscv, bpf: Add necessary Zbb instructions
    https://git.kernel.org/bpf/bpf-next/c/647b93f65daa
  - [RESEND,bpf-next,v3,5/6] riscv, bpf: Optimize sign-extention mov insns with Zbb support
    https://git.kernel.org/bpf/bpf-next/c/519fb722bea0
  - [RESEND,bpf-next,v3,6/6] riscv, bpf: Optimize bswap insns with Zbb support
    https://git.kernel.org/bpf/bpf-next/c/06a33d024838

You are awesome, thank you!

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