Skip to contentSkip to conversation

riscv: Fix build with CONFIG_CC_OPTIMIZE_FOR_SIZE=y

PATCH Applied Samuel Holland 10 participants 22 messages Sep 22, 2022 – Feb 2, 2023 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 riscv: Fix build with CONFIG_CC_OPTIMIZE_FOR_SIZE=y

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

 arch/riscv/include/asm/hwcap.h          |  3 ---
 arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
 2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
  */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +10 -15
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif
-	}
 	barrier();
 }
 
Signature · 1 line
2.35.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Samuel Holland

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

clang-15 chucks a fit too..

Quoted 111 lines

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

 arch/riscv/include/asm/hwcap.h          |  3 ---
 arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
 2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
  */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +10 -15
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif
-	}
 	barrier();
 }
 
Signature · 7 lines
2.35.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#2 lore raw
In reply to Samuel Holland

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

 arch/riscv/include/asm/hwcap.h          |  3 ---
 arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
 2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
  */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Heiko

arch/riscv/include/asm/vdso/processor.h line 28

Quoted 5 lines
arch/riscv/include/asm/vdso/processor.h +0 -1
-	}
 	barrier();
 }
 

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#3 lore raw
Quoted 108 lines · Heiko Stuebner

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Jess

#4 lore raw

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#5 lore raw
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Quoted 9 lines

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

--
Regards,
Atish

#6 lore raw
Quoted 142 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Quoted 8 lines

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 24 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Quoted 9 lines

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

--
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#7 lore raw
Quoted 149 lines · Conor Dooley
Quoted 147 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Quoted 9 lines
Quoted 3 lines

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes. I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 30 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 24 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Quoted 9 lines

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

--
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Signature · 7 lines
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#8 lore raw
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:

	long divisor = 2, dividend = 100000, dest;
	asm("div %0, zero, zero" : "=r" (dest));
and equates to:
	sd      a5,-48(s0)
	div     a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:

	long divisor = 2, dividend = 100000, dest;
	asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:

	sd      a5,-48(s0)
	ld      a5,-48(s0)
	ld      a4,-40(s0)
	div     a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Conor.

Quoted 32 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 22 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 16 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Quoted 9 lines

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Quoted 19 lines
Quoted 8 lines
Quoted 8 lines

--
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Signature · 7 lines
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#9 lore raw
Quoted 195 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:

        long divisor = 2, dividend = 100000, dest;
        asm("div %0, zero, zero" : "=r" (dest));
and equates to:
        sd      a5,-48(s0)
        div     a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:

        long divisor = 2, dividend = 100000, dest;
        asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:

        sd      a5,-48(s0)
        ld      a5,-48(s0)
        ld      a4,-40(s0)
        div     a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Quoted 6 lines

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

Quoted 53 lines

Conor.

Quoted 51 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 30 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 24 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Quoted 9 lines

Heiko

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

--
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

--
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

Signature · 7 lines
Regards,
Atish

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#10 lore raw
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:

        long divisor = 2, dividend = 100000, dest;
        asm("div %0, zero, zero" : "=r" (dest));
and equates to:
        sd      a5,-48(s0)
        div     a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:

        long divisor = 2, dividend = 100000, dest;
        asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:

        sd      a5,-48(s0)
        ld      a5,-48(s0)
        ld      a4,-40(s0)
        div     a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.
insn pause
min 2.3 3.2
max 9.5 10.6
avg 27.0 29.1
5% 2.9 4.2
95% 18.1 19.1

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

#11 lore raw
Quoted 220 lines · Conor Dooley
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {
   RISCV_ISA_EXT_KEY_FPU,          /* For 'F' and 'D' */
-  RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
   RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
           return RISCV_ISA_EXT_KEY_FPU;
   case RISCV_ISA_EXT_d:
           return RISCV_ISA_EXT_KEY_FPU;
-  case RISCV_ISA_EXT_ZIHINTPAUSE:
-          return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
   default:
           return -EINVAL;
   }
arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-  if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-          int dummy;
-          /* In lieu of a halt instruction, induce a long-latency stall. */
-          __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+  int dummy;
+  /* In lieu of a halt instruction, induce a long-latency stall. */
+  __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-  } else {
-          /*
-           * Reduce instruction retirement.
-           * This assumes the PC changes.
-           */
+  /*
+   * Reduce instruction retirement.
+   * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+   */
 #ifdef __riscv_zihintpause
-          __asm__ __volatile__ ("pause");
+  __asm__ __volatile__ ("pause");
 #else
-          /* Encoding of the pause instruction */
-          __asm__ __volatile__ (".4byte 0x100000F");
+  /* Encoding of the pause instruction */
+  __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:

        long divisor = 2, dividend = 100000, dest;
        asm("div %0, zero, zero" : "=r" (dest));
and equates to:
        sd      a5,-48(s0)
        div     a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:

        long divisor = 2, dividend = 100000, dest;
        asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:

        sd      a5,-48(s0)
        ld      a5,-48(s0)
        ld      a4,-40(s0)
        div     a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.

        insn    pause
min     2.3     3.2
max     9.5     10.6
avg     27.0    29.1
5%      2.9     4.2
95%     18.1    19.1
Quoted 5 lines

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

Thanks for running the experiments.

Quoted 27 lines

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Signature · 2 lines
Regards,
Atish
#12 lore raw
Quoted 23 lines · Atish Patra
Quoted 15 lines · Conor Dooley
Quoted 2 lines

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.

        insn    pause
min     2.3     3.2
max     9.5     10.6
avg     27.0    29.1
5%      2.9     4.2
95%     18.1    19.1
Quoted 5 lines

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Quoted 4 lines

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

Thanks for running the experiments.

I've lost track, does that mean the patch is okay as, is or needs to be changed? The former, right?

Thanks,
Conor.

#13 lore raw
In reply to Atish Patra
Quoted 222 lines · Atish Patra
Quoted 220 lines · Conor Dooley
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -0
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {

RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
- RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
RISCV_ISA_EXT_KEY_MAX,
};

arch/riscv/include/asm/hwcap.h +0 -0
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 return RISCV_ISA_EXT_KEY_FPU;

case RISCV_ISA_EXT_d:
return RISCV_ISA_EXT_KEY_FPU;
- case RISCV_ISA_EXT_ZIHINTPAUSE:
- return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
default:
return -EINVAL;
}

arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
- if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
- int dummy;
- /* In lieu of a halt instruction, induce a long-latency stall. */
- __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+ int dummy;
+ /* In lieu of a halt instruction, induce a long-latency stall. */
+ __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
- } else {
- /*
- * Reduce instruction retirement.
- * This assumes the PC changes.
- */
+ /*
+ * Reduce instruction retirement.
+ * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+ */
 #ifdef __riscv_zihintpause
- __asm__ __volatile__ ("pause");
+ __asm__ __volatile__ ("pause");
 #else
- /* Encoding of the pause instruction */
- __asm__ __volatile__ (".4byte 0x100000F");
+ /* Encoding of the pause instruction */
+ __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:
long divisor = 2, dividend = 100000, dest;
asm("div %0, zero, zero" : "=r" (dest));
and equates to:
sd a5,-48(s0)
div a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:
long divisor = 2, dividend = 100000, dest;
asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:
sd a5,-48(s0)
ld a5,-48(s0)
ld a4,-40(s0)
div a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.
insn pause
min 2.3 3.2
max 9.5 10.6
avg 27.0 29.1
5% 2.9 4.2
95% 18.1 19.1

Quoted 8 lines
Quoted 5 lines

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

But does that actually matter in practice? If it doesn’t noticeable affect performance then you don’t need to keep the div. There are a lot of architectures that even just define cpu_relax() as barrier().

Jess

Quoted 34 lines

Thanks for running the experiments.

Quoted 27 lines

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

Signature · 2 lines
Regards,
Atish
#14 lore raw
Quoted 230 lines · Jessica Clarke
Quoted 228 lines · Atish Patra
Quoted 225 lines · Conor Dooley
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 91 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints 285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto' 41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

arch/riscv/include/asm/hwcap.h | 3 --- arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------

2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -0
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
 */
 enum riscv_isa_ext_key {

RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
- RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
RISCV_ISA_EXT_KEY_MAX,
};

arch/riscv/include/asm/hwcap.h +0 -0
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 return RISCV_ISA_EXT_KEY_FPU;

case RISCV_ISA_EXT_d:
return RISCV_ISA_EXT_KEY_FPU;
- case RISCV_ISA_EXT_ZIHINTPAUSE:
- return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
default:
return -EINVAL;
}

arch/riscv/include/asm/vdso/processor.h +6 -11
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
- if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
- int dummy;
- /* In lieu of a halt instruction, induce a long-latency stall. */
- __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+ int dummy;
+ /* In lieu of a halt instruction, induce a long-latency stall. */
+ __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
- } else {
- /*
- * Reduce instruction retirement.
- * This assumes the PC changes.
- */
+ /*
+ * Reduce instruction retirement.
+ * This assumes the PC changes.

arch/riscv/include/asm/vdso/processor.h line 25

Quoted 10 lines
arch/riscv/include/asm/vdso/processor.h +4 -3
+ */
 #ifdef __riscv_zihintpause
- __asm__ __volatile__ ("pause");
+ __asm__ __volatile__ ("pause");
 #else
- /* Encoding of the pause instruction */
- __asm__ __volatile__ (".4byte 0x100000F");
+ /* Encoding of the pause instruction */
+ __asm__ __volatile__ (".4byte 0x100000F");
 #endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:
long divisor = 2, dividend = 100000, dest;
asm("div %0, zero, zero" : "=r" (dest));
and equates to:
sd a5,-48(s0)
div a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:
long divisor = 2, dividend = 100000, dest;
asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:
sd a5,-48(s0)
ld a5,-48(s0)
ld a4,-40(s0)
div a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.
insn pause
min 2.3 3.2
max 9.5 10.6
avg 27.0 29.1
5% 2.9 4.2
95% 18.1 19.1

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Quoted 6 lines
Quoted 2 lines

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

But does that actually matter in practice? If it doesn’t noticeable affect performance then you don’t need to keep the div. There are a lot of architectures that even just define cpu_relax() as barrier().

Div is not semantic accurate for standard code, it should be in vendors' errata. I agree to leave nop as default and put a pause instead after the feature is detected.

Quoted 38 lines

Jess

Quoted 34 lines

Thanks for running the experiments.

Quoted 27 lines

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

--
Regards,
Atish

Signature · 2 lines
Best Regards
 Guo Ren
#15 lore raw
Quoted 235 lines · Guo Ren
Quoted 233 lines · Jessica Clarke
Quoted 230 lines · Atish Patra
Quoted 220 lines · Conor Dooley
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 108 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:

Quoted 101 lines

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke
building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'
41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'
41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that
function is widely inlined, including in some quite complex functions
like in the VDSO. A quick measurement shows this static branch is
responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of
instructions anyway. If Zihintpause is supported, we trade the nop from
the static branch for a div. If Zihintpause is unsupported, we trade the
jump from the static branch for (what gets interpreted as) a nop.

Fixes: 8eb060e10185 ("arch/riscv: add Zihintpause support")
Signed-off-by: Samuel Holland <[email protected]>
---

arch/riscv/include/asm/hwcap.h | 3 ---
arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
index 6f59ec64175e..b21d46e68386 100644
--- a/arch/riscv/include/asm/hwcap.h
+++ b/arch/riscv/include/asm/hwcap.h
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
*/
enum riscv_isa_ext_key {
RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
- RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
RISCV_ISA_EXT_KEY_MAX,
};

@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
return RISCV_ISA_EXT_KEY_FPU;
case RISCV_ISA_EXT_d:
return RISCV_ISA_EXT_KEY_FPU;
- case RISCV_ISA_EXT_ZIHINTPAUSE:
- return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
default:
return -EINVAL;
}
diff --git a/arch/riscv/include/asm/vdso/processor.h b/arch/riscv/include/asm/vdso/processor.h
index 1e4f8b4aef79..789bdb8211a2 100644
--- a/arch/riscv/include/asm/vdso/processor.h
+++ b/arch/riscv/include/asm/vdso/processor.h
@@ -4,30 +4,25 @@

#ifndef __ASSEMBLY__

-#include <linux/jump_label.h>
#include <asm/barrier.h>
-#include <asm/hwcap.h>

static inline void cpu_relax(void)
{
- if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
#ifdef __riscv_muldiv
- int dummy;
- /* In lieu of a halt instruction, induce a long-latency stall. */
- __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+ int dummy;
+ /* In lieu of a halt instruction, induce a long-latency stall. */
+ __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
#endif
- } else {
- /*
- * Reduce instruction retirement.
- * This assumes the PC changes.
- */
+ /*
+ * Reduce instruction retirement.
+ * This assumes the PC changes.
+ */
#ifdef __riscv_zihintpause
- __asm__ __volatile__ ("pause");
+ __asm__ __volatile__ ("pause");
#else
- /* Encoding of the pause instruction */
- __asm__ __volatile__ (".4byte 0x100000F");
+ /* Encoding of the pause instruction */
+ __asm__ __volatile__ (".4byte 0x100000F");
#endif

hmm, though before this part of the code was only ever accessed when the zhintpause extension was really available on the running machine while now the pause instruction is called every time.

Quoted 2 lines

So I'm just wondering, can't this run into some "illegal instruction" thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:
long divisor = 2, dividend = 100000, dest;
asm("div %0, zero, zero" : "=r" (dest));
and equates to:
sd a5,-48(s0)
div a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:
long divisor = 2, dividend = 100000, dest;
asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:
sd a5,-48(s0)
ld a5,-48(s0)
ld a4,-40(s0)
div a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.
insn pause
min 2.3 3.2
max 9.5 10.6
avg 27.0 29.1
5% 2.9 4.2
95% 18.1 19.1

Quoted 5 lines

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

Quoted 6 lines
Quoted 3 lines

But does that actually matter in practice? If it doesn’t noticeable affect performance then you don’t need to keep the div. There are a lot of architectures that even just define cpu_relax() as barrier().

Div is not semantic accurate for standard code, it should be in vendors' errata. I agree to leave nop as default and put a pause instead after the feature is detected.

Nobody’s suggesting a literal nop instruction, that would be worse than either div or pause. It’s always safe to execute pause, the question is just whether on existing systems that don’t implement Zihintpause it gets executed too quickly such that performance is degraded due to spinning more aggressively.

Jess

Quoted 43 lines
Quoted 38 lines

Jess

Quoted 34 lines

Thanks for running the experiments.

Quoted 27 lines

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

--
Regards,
Atish

Signature · 2 lines
Best Regards
Guo Ren
#16 lore raw

,

Quoted 244 lines · Jessica Clarke
Quoted 235 lines · Guo Ren
Quoted 233 lines · Jessica Clarke
Quoted 230 lines · Atish Patra
Quoted 220 lines · Conor Dooley
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 112 lines · Heiko Stuebner
Quoted 110 lines

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:
commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke
building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'
41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'
41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that
function is widely inlined, including in some quite complex functions
like in the VDSO. A quick measurement shows this static branch is
responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of
instructions anyway. If Zihintpause is supported, we trade the nop from
the static branch for a div. If Zihintpause is unsupported, we trade the
jump from the static branch for (what gets interpreted as) a nop.

Fixes: 8eb060e10185 ("arch/riscv: add Zihintpause support")
Signed-off-by: Samuel Holland <[email protected]>
---

arch/riscv/include/asm/hwcap.h | 3 ---
arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
index 6f59ec64175e..b21d46e68386 100644
--- a/arch/riscv/include/asm/hwcap.h
+++ b/arch/riscv/include/asm/hwcap.h
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
*/
enum riscv_isa_ext_key {
RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
- RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
RISCV_ISA_EXT_KEY_MAX,
};

@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
return RISCV_ISA_EXT_KEY_FPU;
case RISCV_ISA_EXT_d:
return RISCV_ISA_EXT_KEY_FPU;
- case RISCV_ISA_EXT_ZIHINTPAUSE:
- return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
default:
return -EINVAL;
}
diff --git a/arch/riscv/include/asm/vdso/processor.h b/arch/riscv/include/asm/vdso/processor.h
index 1e4f8b4aef79..789bdb8211a2 100644
--- a/arch/riscv/include/asm/vdso/processor.h
+++ b/arch/riscv/include/asm/vdso/processor.h
@@ -4,30 +4,25 @@

#ifndef __ASSEMBLY__

-#include <linux/jump_label.h>
#include <asm/barrier.h>
-#include <asm/hwcap.h>

static inline void cpu_relax(void)
{
- if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
#ifdef __riscv_muldiv
- int dummy;
- /* In lieu of a halt instruction, induce a long-latency stall. */
- __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+ int dummy;
+ /* In lieu of a halt instruction, induce a long-latency stall. */
+ __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
#endif
- } else {
- /*
- * Reduce instruction retirement.
- * This assumes the PC changes.
- */
+ /*
+ * Reduce instruction retirement.
+ * This assumes the PC changes.
+ */
#ifdef __riscv_zihintpause
- __asm__ __volatile__ ("pause");
+ __asm__ __volatile__ ("pause");
#else
- /* Encoding of the pause instruction */
- __asm__ __volatile__ (".4byte 0x100000F");
+ /* Encoding of the pause instruction */
+ __asm__ __volatile__ (".4byte 0x100000F");
#endif

hmm, though before this part of the code was only ever accessed
when the zhintpause extension was really available on the running
machine while now the pause instruction is called every time.

So I'm just wondering, can't this run into some "illegal instruction"
thingy on machines not supporting the extension?

Quoted 11 lines

No. The encoding for pause was deliberately chosen to be one of the “useless” encodings of fence, with the hope that existing microarchitectures might take a while to execute it and thus it would still function as a slow-running instruction. It’s somewhat questionable whether the div is even needed, the worst that happens is cpu_relax isn’t very relaxed and you spin a bit faster. Any implementations where that’s true probably also don’t have fancy clock/power management anyway, and div isn’t going to be a low-power operation so the only real effect is likely hammering on contended atomics a bit more, and who cares about that on the low core count systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:
long divisor = 2, dividend = 100000, dest;
asm("div %0, zero, zero" : "=r" (dest));
and equates to:
sd a5,-48(s0)
div a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:
long divisor = 2, dividend = 100000, dest;
asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:
sd a5,-48(s0)
ld a5,-48(s0)
ld a4,-40(s0)
div a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.
insn pause
min 2.3 3.2
max 9.5 10.6
avg 27.0 29.1
5% 2.9 4.2
95% 18.1 19.1

Quoted 5 lines

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

Quoted 6 lines
Quoted 3 lines

But does that actually matter in practice? If it doesn’t noticeable affect performance then you don’t need to keep the div. There are a lot of architectures that even just define cpu_relax() as barrier().

Div is not semantic accurate for standard code, it should be in vendors' errata. I agree to leave nop as default and put a pause instead after the feature is detected.

Quoted 5 lines

Nobody’s suggesting a literal nop instruction, that would be worse than either div or pause. It’s always safe to execute pause, the question is just whether on existing systems that don’t implement Zihintpause it gets executed too quickly such that performance is degraded due to spinning more aggressively.

Why do you ensure pause can't be an illegal instruction in some old machine? Why do you ensure div could save power for all microarchitectures?

nop (default) -> div/<other instructions> (moved into vendor errata) -> pause (when ZiHintPause feature detected)

Quoted 47 lines

Jess

Quoted 43 lines
Quoted 38 lines

Jess

Quoted 34 lines

Thanks for running the experiments.

Quoted 27 lines

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

--
Regards,
Atish

--
Best Regards
Guo Ren

Signature · 2 lines
Best Regards
 Guo Ren
#17 lore raw
Quoted 248 lines · Guo Ren

,

Quoted 244 lines · Jessica Clarke
Quoted 241 lines · Guo Ren
Quoted 230 lines · Jessica Clarke
Quoted 228 lines · Atish Patra
Quoted 225 lines · Conor Dooley
Quoted 199 lines · Atish Patra
Quoted 198 lines · Conor Dooley
Quoted 154 lines · Atish Patra
Quoted 153 lines · Conor Dooley
Quoted 150 lines · Atish Patra
Quoted 128 lines · Heiko Stuebner

Hi,

Am Donnerstag, 22. September 2022, 17:52:46 CEST schrieb Jessica Clarke:

Quoted 123 lines · Heiko Stuebner

Am Donnerstag, 22. September 2022, 08:09:58 CEST schrieb Samuel Holland:
commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke
building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'
41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'
41 | asm_volatile_goto(
| ^~~~~~~~~~~~~~~~~
make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that
function is widely inlined, including in some quite complex functions
like in the VDSO. A quick measurement shows this static branch is
responsible by itself for around 40% of the jump table.

Drop the static branch, which ends up being the same number of
instructions anyway. If Zihintpause is supported, we trade the nop from
the static branch for a div. If Zihintpause is unsupported, we trade the
jump from the static branch for (what gets interpreted as) a nop.

Fixes: 8eb060e10185 ("arch/riscv: add Zihintpause support")
Signed-off-by: Samuel Holland <[email protected]>
---

arch/riscv/include/asm/hwcap.h | 3 ---
arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/arch/riscv/include/asm/hwcap.h b/arch/riscv/include/asm/hwcap.h
index 6f59ec64175e..b21d46e68386 100644
--- a/arch/riscv/include/asm/hwcap.h
+++ b/arch/riscv/include/asm/hwcap.h
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
*/
enum riscv_isa_ext_key {
RISCV_ISA_EXT_KEY_FPU, /* For 'F' and 'D' */
- RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
RISCV_ISA_EXT_KEY_MAX,
};

@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
return RISCV_ISA_EXT_KEY_FPU;
case RISCV_ISA_EXT_d:
return RISCV_ISA_EXT_KEY_FPU;
- case RISCV_ISA_EXT_ZIHINTPAUSE:
- return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
default:
return -EINVAL;
}
diff --git a/arch/riscv/include/asm/vdso/processor.h b/arch/riscv/include/asm/vdso/processor.h
index 1e4f8b4aef79..789bdb8211a2 100644
--- a/arch/riscv/include/asm/vdso/processor.h
+++ b/arch/riscv/include/asm/vdso/processor.h
@@ -4,30 +4,25 @@

#ifndef __ASSEMBLY__

-#include <linux/jump_label.h>
#include <asm/barrier.h>
-#include <asm/hwcap.h>

static inline void cpu_relax(void)
{
- if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
#ifdef __riscv_muldiv
- int dummy;
- /* In lieu of a halt instruction, induce a long-latency stall. */
- __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+ int dummy;
+ /* In lieu of a halt instruction, induce a long-latency stall. */
+ __asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
#endif
- } else {
- /*
- * Reduce instruction retirement.
- * This assumes the PC changes.
- */
+ /*
+ * Reduce instruction retirement.
+ * This assumes the PC changes.
+ */
#ifdef __riscv_zihintpause
- __asm__ __volatile__ ("pause");
+ __asm__ __volatile__ ("pause");
#else
- /* Encoding of the pause instruction */
- __asm__ __volatile__ (".4byte 0x100000F");
+ /* Encoding of the pause instruction */
+ __asm__ __volatile__ (".4byte 0x100000F");
#endif

hmm, though before this part of the code was only ever accessed
when the zhintpause extension was really available on the running
machine while now the pause instruction is called every time.

So I'm just wondering, can't this run into some "illegal instruction"
thingy on machines not supporting the extension?

No. The encoding for pause was deliberately chosen to be one of the
“useless” encodings of fence, with the hope that existing
microarchitectures might take a while to execute it and thus it would
still function as a slow-running instruction. It’s somewhat
questionable whether the div is even needed, the worst that happens is
cpu_relax isn’t very relaxed and you spin a bit faster. Any
implementations where that’s true probably also don’t have fancy
clock/power management anyway, and div isn’t going to be a low-power
operation so the only real effect is likely hammering on contended
atomics a bit more, and who cares about that on the low core count
systems we have today.

Quoted 7 lines

thanks a lot for that explanation, which made things a lot clearer.

So as you said, dropping the div part might make the function even smaller, though somehow part of me would want to add some sort of comment to the function for when the next developer stumbles over the unconditional use of pause :-) .

I agree. If that's what microarch will do, we can drop div altogether. Though microarch may be treated as nop even if it is undesirable. IIRC, the div was introduced for the rocket chip which would induce a long latency stall with div instruction (zero as operands).

Does any other core or newer rocket chip actually induce a latency stall with div instruction ?
If not, it is equivalent to NOP as well. We can definitely remove the div. The only cores affected will be the older rocket core.

Tagging some folks to understand what their core does.

@Paul Walmsley @Guo Ren @Conor Dooley ?

Quoted 7 lines
Quoted 5 lines

I am no microarch expert by _any_ stretch of the imagination, but from a quick experiment it looks like the u54s on PolarFire SoC behave in the same way, and div w/ zero operands does in fact take significantly longer than regular division (looks to be about 3x).

Thanks. Do you have any data on how much the "pause" instruction takes.

So these numbers you may consider as being pulled out of a magic hat as all I am doing is reading the counters from userspace and there is some variance etc. Plus the fact that I just started hacking at some existing code I had lying around as I'm pretty snowed under at the moment.

Doing the following takes about 70 cycles on both a PolarFire SoC and an unmatched:
long divisor = 2, dividend = 100000, dest;
asm("div %0, zero, zero" : "=r" (dest));
and equates to:
sd a5,-48(s0)
div a5,zero,zero

Clocking in at about 40 cycles is some actual divisions, I just did the following a dozen times, doing a trivial computation:
long divisor = 2, dividend = 100000, dest;
asm("div %0, %1, %2" : "=r" (dividend) : "r" (dividend), "r" (divisor))

ie, a load of the following:
sd a5,-48(s0)
ld a5,-48(s0)
ld a4,-40(s0)
div a5,a5,a4

So clearly the div w/ zero args makes a difference...

On PolarFire SoC, 0x100000F takes approx 6 cycles. On my unmatched, it takes approx 40. Again, I just had an asm block & called the instruction a number times and took the average - here it was 48 times.

Take the actual numbers with a fist full of salt, but at least the relative numbers should be of some use to you.

Quoted 6 lines
Quoted 3 lines

Hope that's somewhat helpful, maybe next week I can do something a little more useful for you...

Thanks. It would be good to understand what happens when "pause" is executed on these boards ?

The actual pause instruction? uhh, so with the usual "I don't know what I am doing" disclaimer, I ran each of the .insn and pause instruction 48 times in a row and checked the time elapsed via rdcycle & then ran that c program 1000 times in a bash loop. Got the below, the insns were run first and then the pauses.
insn pause
min 2.3 3.2
max 9.5 10.6
avg 27.0 29.1
5% 2.9 4.2
95% 18.1 19.1

Swapping the pause & insn order around made a minor difference, but not enough to report on. I'd be very wary of drawing any real conclusions from this data, but at least both are roughly similar (and certainly not even close to doing the div w/ zero args.

Quoted 6 lines
Quoted 2 lines

Yeah. That's what I was expecting. So we can't drop the div for now. Otherwise, the existing hardware(don't support Zhintpause) suffers by spinning faster.

But does that actually matter in practice? If it doesn’t noticeable affect performance then you don’t need to keep the div. There are a lot of architectures that even just define cpu_relax() as barrier().

Div is not semantic accurate for standard code, it should be in vendors' errata. I agree to leave nop as default and put a pause instead after the feature is detected.

Quoted 7 lines
Quoted 5 lines

Nobody’s suggesting a literal nop instruction, that would be worse than either div or pause. It’s always safe to execute pause, the question is just whether on existing systems that don’t implement Zihintpause it gets executed too quickly such that performance is degraded due to spinning more aggressively.

Why do you ensure pause can't be an illegal instruction in some old machine?

Because that’s how it’s defined; it uses one of the many hints (instructions that aren’t a canonical nop but are defined to behave like one in terms of architectural side-effects) from RV32I/RV64I.

Quoted 1 line

Why do you ensure div could save power for all microarchitectures?

I don’t. In fact it almost certainly won’t make the core enter a low power state. It will just help reduce the amount of memory traffic by taking a while to execute. I would rather not use div at all.

Jess

Quoted 55 lines

nop (default) -> div/<other instructions> (moved into vendor errata) -> pause (when ZiHintPause feature detected)

Quoted 47 lines

Jess

Quoted 43 lines
Quoted 38 lines

Jess

Quoted 34 lines

Thanks for running the experiments.

Quoted 27 lines

Again, hope that is helpful?
Conor.

Quoted 23 lines
Quoted 23 lines

Conor.

Quoted 20 lines

I understand that it is not available in these cores. Just wanted to understand if microarchitecture
actually takes a while executing the useless encoding as pointed out by Jessica.

If that's the case, we can remove the div instruction altogether. Otherwise, this patch will cause some performance regression for existing SoC (HiFive unleashed has the same core. Not sure about unmatched though).
This needs to be documented at least.

Quoted 10 lines

Hope that's helpful,
Conor.

(I just did a quick check of what pretty much amounted to a bunch of div a5,zero,zero in a row versus div a5,a5,a5)

Quoted 4 lines

(Please add anybody who may have an insight to execution flow on existing Linux capable cores)

--
Regards,
Atish

--
Best Regards
Guo Ren

Signature · 2 lines
Best Regards
Guo Ren
#18 lore raw
In reply to Samuel Holland
Quoted 26 lines · Samuel Holland

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Quoted 4 lines

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Hi Samuel,

I'm not sure whether it's correct to remove static branch usage from cpu_relax, but your report inspired my patch of constifying arguments of arch_static_branch() and arch_static_branch_jump() [1]. Could you please also test it?

Thanks very much

[1]https://lore.kernel.org/linux-riscv/[email protected]/T/#u

Quoted 78 lines
Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

 arch/riscv/include/asm/hwcap.h          |  3 ---
 arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
 2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
  */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +10 -15
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif
-	}
 	barrier();
 }
 
Signature · 1 line
2.35.1
maintainer
#19 lore raw
Quoted 34 lines · On Wed, 05 Oct 2022 23:48:11 PDT (-0700), [email protected] wrote:
Quoted 26 lines · Samuel Holland

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Having a static branch in cpu_relax() is problematic because that function is widely inlined, including in some quite complex functions like in the VDSO. A quick measurement shows this static branch is responsible by itself for around 40% of the jump table.

Quoted 4 lines

Drop the static branch, which ends up being the same number of instructions anyway. If Zihintpause is supported, we trade the nop from the static branch for a div. If Zihintpause is unsupported, we trade the jump from the static branch for (what gets interpreted as) a nop.

Hi Samuel,

Quoted 8 lines

I'm not sure whether it's correct to remove static branch usage from cpu_relax, but your report inspired my patch of constifying arguments of arch_static_branch() and arch_static_branch_jump() [1]. Could you please also test it?

Thanks very much

[1]https://lore.kernel.org/linux-riscv/[email protected]/T/#u

Thanks. IMO that's the better short-term fix, as that sorts out the build errors without dropping the div routine and we need the div routine to avoid regression on the old SiFive cores (like the one in the PolarFire SOC). We can make a few improvements, though:

* If folks are worried about the performance of the static branch then we can convert this over to an alternative. It should be safe to default to pause as it aliases a fence, it's just not as good at slowing down the core.
* We can just drop the Zihintpause detection entirely and go with .4byte/.insn to encode the pause. That's essentially what we've dove with the T-Head Zicbom stuff, but not sure it's worth it here because Zihintpause is new.

Quoted 79 lines
Quoted 79 lines
Fixes 8eb060e10185 ("arch/riscv: add Zihintpause support")

---

 arch/riscv/include/asm/hwcap.h          |  3 ---
 arch/riscv/include/asm/vdso/processor.h | 25 ++++++++++---------------
 2 files changed, 10 insertions(+), 18 deletions(-)
arch/riscv/include/asm/hwcap.h +0 -3
index 6f59ec64175e..b21d46e68386 100644
@@ -68,7 +68,6 @@ enum riscv_isa_ext_id {
  */
 enum riscv_isa_ext_key {
 	RISCV_ISA_EXT_KEY_FPU,		/* For 'F' and 'D' */
-	RISCV_ISA_EXT_KEY_ZIHINTPAUSE,
 	RISCV_ISA_EXT_KEY_MAX,
 };
 
@@ -88,8 +87,6 @@ static __always_inline int riscv_isa_ext2key(int num)
 		return RISCV_ISA_EXT_KEY_FPU;
 	case RISCV_ISA_EXT_d:
 		return RISCV_ISA_EXT_KEY_FPU;
-	case RISCV_ISA_EXT_ZIHINTPAUSE:
-		return RISCV_ISA_EXT_KEY_ZIHINTPAUSE;
 	default:
 		return -EINVAL;
 	}
arch/riscv/include/asm/vdso/processor.h +10 -16
index 1e4f8b4aef79..789bdb8211a2 100644
@@ -4,30 +4,25 @@
 
 #ifndef __ASSEMBLY__
 
-#include <linux/jump_label.h>
 #include <asm/barrier.h>
-#include <asm/hwcap.h>
 
 static inline void cpu_relax(void)
 {
-	if (!static_branch_likely(&riscv_isa_ext_keys[RISCV_ISA_EXT_KEY_ZIHINTPAUSE])) {
 #ifdef __riscv_muldiv
-		int dummy;
-		/* In lieu of a halt instruction, induce a long-latency stall. */
-		__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
+	int dummy;
+	/* In lieu of a halt instruction, induce a long-latency stall. */
+	__asm__ __volatile__ ("div %0, %0, zero" : "=r" (dummy));
 #endif
-	} else {
-		/*
-		 * Reduce instruction retirement.
-		 * This assumes the PC changes.
-		 */
+	/*
+	 * Reduce instruction retirement.
+	 * This assumes the PC changes.
+	 */
 #ifdef __riscv_zihintpause
-		__asm__ __volatile__ ("pause");
+	__asm__ __volatile__ ("pause");
 #else
-		/* Encoding of the pause instruction */
-		__asm__ __volatile__ (".4byte 0x100000F");
+	/* Encoding of the pause instruction */
+	__asm__ __volatile__ (".4byte 0x100000F");
 #endif
-	}
 	barrier();
 }
 
--

2.35.1

#20 lore raw
In reply to Samuel Holland
Quoted 16 lines · Samuel Holland

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

Quoted 6 lines
   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

[...]

Applied, thanks!

[1/1] riscv: Fix build with CONFIG_CC_OPTIMIZE_FOR_SIZE=y

      https://git.kernel.org/palmer/c/3c349eacc559

Best regards,

Signature · 1 line
Palmer Dabbelt <[email protected]>
#21 lore raw
Quoted 22 lines · Palmer Dabbelt
Quoted 21 lines · Samuel Holland

commit 8eb060e10185 ("arch/riscv: add Zihintpause support") broke building with CONFIG_CC_OPTIMIZE_FOR_SIZE enabled (gcc 11.1.0):

CC arch/riscv/kernel/vdso/vgettimeofday.o
In file included from <command-line>:
./arch/riscv/include/asm/jump_label.h: In function 'cpu_relax':
././include/linux/compiler_types.h:285:33: warning: 'asm' operand 0 probably does not match constraints
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

././include/linux/compiler_types.h:285:33: error: impossible constraint in 'asm'
285 | #define asm_volatile_goto(x...) asm goto(x)
| ^~~
./arch/riscv/include/asm/jump_label.h:41:9: note: in expansion of macro 'asm_volatile_goto'

   41 |         asm_volatile_goto(
      |         ^~~~~~~~~~~~~~~~~

make[1]: *** [scripts/Makefile.build:249: arch/riscv/kernel/vdso/vgettimeofday.o] Error 1
make: *** [arch/riscv/Makefile:128: vdso_prepare] Error 2

Quoted 3 lines
Quoted 1 line

[...]

Applied, thanks!

Hi Palmer,

I assume the build error is already fixed in v6.1 and can't be reproduced.

Thanks

Quoted 7 lines

[1/1] riscv: Fix build with CONFIG_CC_OPTIMIZE_FOR_SIZE=y

      https://git.kernel.org/palmer/c/3c349eacc559

Best regards,

Signature · 1 line
Palmer Dabbelt <[email protected]>