Skip to contentSkip to conversation

LoongArch/percpu: Simplify _percpu_read() and _percpu_write()

PATCH Superseded Uros Bizjak 3 participants 11 messages Sep 5, 2024 – Sep 6, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 LoongArch/percpu: Simplify _percpu_read() and _percpu_write()

_percpu_read() and _percpu_write() macros call __percpu_read() and __percpu_write() static inline functions that result in a single assembly instruction. Percpu infrastructure expects its leaf definitions to encode the size of their percpu variable, so the patch merges asm clauses from the static inline function into the corresponding leaf macros.

The secondary effect of this change is to avoid explicit __percpu function arguments. Currently, __percpu macro is defined in include/linux/compiler_types.h, but with proposed patch [1], __percpu definition will need macros from include/asm-generic/percpu.h, creating forward dependency loop.

The proposed solution is the same as x86 architecture uses.

Patch is compile tested only.

[1] https://lore.kernel.org/lkml/[email protected]/

Cc Huacai Chen <[email protected]>
Cc WANG Xuerui <[email protected]>
Cc Xi Ruoyao <[email protected]>
Cc Thomas Gleixner <[email protected]>

---
v2: Add a missing cast and a missing coma in the asm template,
reported by kernel test robot. Some formatting changes.
v3: Check the type of _val in _percpu_write().

---
 arch/loongarch/include/asm/percpu.h | 135 ++++++++++------------------
 1 file changed, 46 insertions(+), 89 deletions(-)
arch/loongarch/include/asm/percpu.h +46 -89
index 8f290e5546cf..90ece62ec24c 100644
@@ -68,75 +68,6 @@ PERCPU_OP(and, and, &)
 PERCPU_OP(or, or, |)
 #undef PERCPU_OP
 
-static __always_inline unsigned long __percpu_read(void __percpu *ptr, int size)
-{
-	unsigned long ret;
-
-	switch (size) {
-	case 1:
-		__asm__ __volatile__ ("ldx.b %[ret], $r21, %[ptr]	\n"
-		: [ret] "=&r"(ret)
-		: [ptr] "r"(ptr)
-		: "memory");
-		break;
-	case 2:
-		__asm__ __volatile__ ("ldx.h %[ret], $r21, %[ptr]	\n"
-		: [ret] "=&r"(ret)
-		: [ptr] "r"(ptr)
-		: "memory");
-		break;
-	case 4:
-		__asm__ __volatile__ ("ldx.w %[ret], $r21, %[ptr]	\n"
-		: [ret] "=&r"(ret)
-		: [ptr] "r"(ptr)
-		: "memory");
-		break;
-	case 8:
-		__asm__ __volatile__ ("ldx.d %[ret], $r21, %[ptr]	\n"
-		: [ret] "=&r"(ret)
-		: [ptr] "r"(ptr)
-		: "memory");
-		break;
-	default:
-		ret = 0;
-		BUILD_BUG();
-	}
-
-	return ret;
-}
-
-static __always_inline void __percpu_write(void __percpu *ptr, unsigned long val, int size)
-{
-	switch (size) {
-	case 1:
-		__asm__ __volatile__("stx.b %[val], $r21, %[ptr]	\n"
-		:
-		: [val] "r" (val), [ptr] "r" (ptr)
-		: "memory");
-		break;
-	case 2:
-		__asm__ __volatile__("stx.h %[val], $r21, %[ptr]	\n"
-		:
-		: [val] "r" (val), [ptr] "r" (ptr)
-		: "memory");
-		break;
-	case 4:
-		__asm__ __volatile__("stx.w %[val], $r21, %[ptr]	\n"
-		:
-		: [val] "r" (val), [ptr] "r" (ptr)
-		: "memory");
-		break;
-	case 8:
-		__asm__ __volatile__("stx.d %[val], $r21, %[ptr]	\n"
-		:
-		: [val] "r" (val), [ptr] "r" (ptr)
-		: "memory");
-		break;
-	default:
-		BUILD_BUG();
-	}
-}
-
 static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, int size)
 {
 	switch (size) {
@@ -157,6 +88,44 @@ static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val,
 	return 0;
 }
 
+#define __pcpu_op_1(op)		op ".b "
+#define __pcpu_op_2(op)		op ".h "
+#define __pcpu_op_4(op)		op ".w "
+#define __pcpu_op_8(op)		op ".d "
+
+#define _percpu_read(size, _pcp)					\
+({									\
+	unsigned long __pcp_ret;					\
+									\
+	__asm__ __volatile__(						\
+		__pcpu_op_##size("ldx") "%[ret], $r21, %[ptr]	\n"	\
+		: [ret] "=&r"(__pcp_ret)				\
+		: [ptr] "r"(&(_pcp))					\
+		: "memory");						\
+	(typeof(_pcp))__pcp_ret;					\
+})
+
+#define __pcpu_cast_1(val)	(((unsigned long) val) & 0xff)
+#define __pcpu_cast_2(val)	(((unsigned long) val) & 0xffff)
+#define __pcpu_cast_4(val)	(((unsigned long) val) & 0xffffffff)
+#define __pcpu_cast_8(val)	((unsigned long) val)
+
+#define _percpu_write(size, _pcp, _val)					\
+do {									\
+	unsigned long __pcp_val = __pcpu_cast_##size(_val);		\
+									\
+	if (0) {		                                        \
+		typeof(_pcp) pto_tmp__;					\
+		pto_tmp__ = (_val);					\
+		(void)pto_tmp__;					\
+	}								\
+	__asm__ __volatile__(						\
+		__pcpu_op_##size("stx") "%[val], $r21, %[ptr]	\n"	\
+		:							\
+		: [val] "r"(__pcp_val), [ptr] "r"(&(_pcp))		\
+		: "memory");						\
+} while (0)
+
 /* this_cpu_cmpxchg */
 #define _protect_cmpxchg_local(pcp, o, n)			\
 ({								\
@@ -167,18 +136,6 @@ static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val,
 	__ret;							\
 })
 
-#define _percpu_read(pcp)						\
-({									\
-	typeof(pcp) __retval;						\
-	__retval = (typeof(pcp))__percpu_read(&(pcp), sizeof(pcp));	\
-	__retval;							\
-})
-
-#define _percpu_write(pcp, val)						\
-do {									\
-	__percpu_write(&(pcp), (unsigned long)(val), sizeof(pcp));	\
-} while (0)								\
-
 #define _pcp_protect(operation, pcp, val)			\
 ({								\
 	typeof(pcp) __retval;					\
@@ -215,15 +172,15 @@ do {									\
 #define this_cpu_or_4(pcp, val) _percpu_or(pcp, val)
 #define this_cpu_or_8(pcp, val) _percpu_or(pcp, val)
 
-#define this_cpu_read_1(pcp) _percpu_read(pcp)
-#define this_cpu_read_2(pcp) _percpu_read(pcp)
-#define this_cpu_read_4(pcp) _percpu_read(pcp)
-#define this_cpu_read_8(pcp) _percpu_read(pcp)
+#define this_cpu_read_1(pcp) _percpu_read(1, pcp)
+#define this_cpu_read_2(pcp) _percpu_read(2, pcp)
+#define this_cpu_read_4(pcp) _percpu_read(4, pcp)
+#define this_cpu_read_8(pcp) _percpu_read(8, pcp)
 
-#define this_cpu_write_1(pcp, val) _percpu_write(pcp, val)
-#define this_cpu_write_2(pcp, val) _percpu_write(pcp, val)
-#define this_cpu_write_4(pcp, val) _percpu_write(pcp, val)
-#define this_cpu_write_8(pcp, val) _percpu_write(pcp, val)
+#define this_cpu_write_1(pcp, val) _percpu_write(1, pcp, val)
+#define this_cpu_write_2(pcp, val) _percpu_write(2, pcp, val)
+#define this_cpu_write_4(pcp, val) _percpu_write(4, pcp, val)
+#define this_cpu_write_8(pcp, val) _percpu_write(8, pcp, val)
 
 #define this_cpu_xchg_1(pcp, val) _percpu_xchg(pcp, val)
 #define this_cpu_xchg_2(pcp, val) _percpu_xchg(pcp, val)
Signature · 1 line
2.46.0
maintainer
#1 lore raw

Hi, Uros,

Quoted 125 lines · Uros Bizjak

_percpu_read() and _percpu_write() macros call __percpu_read() and __percpu_write() static inline functions that result in a single assembly instruction. Percpu infrastructure expects its leaf definitions to encode the size of their percpu variable, so the patch merges asm clauses from the static inline function into the corresponding leaf macros.

The secondary effect of this change is to avoid explicit __percpu function arguments. Currently, __percpu macro is defined in include/linux/compiler_types.h, but with proposed patch [1], __percpu definition will need macros from include/asm-generic/percpu.h, creating forward dependency loop.

The proposed solution is the same as x86 architecture uses.

Patch is compile tested only.

[1] https://lore.kernel.org/lkml/[email protected]/

Cc Huacai Chen <[email protected]>
Cc WANG Xuerui <[email protected]>
Cc Xi Ruoyao <[email protected]>
Cc Thomas Gleixner <[email protected]>

---
v2: Add a missing cast and a missing coma in the asm template,
reported by kernel test robot. Some formatting changes.
v3: Check the type of _val in _percpu_write().

---
 arch/loongarch/include/asm/percpu.h | 135 ++++++++++------------------
 1 file changed, 46 insertions(+), 89 deletions(-)
arch/loongarch/include/asm/percpu.h +8 -69
index 8f290e5546cf..90ece62ec24c 100644
@@ -68,75 +68,6 @@ PERCPU_OP(and, and, &)
 PERCPU_OP(or, or, |)
 #undef PERCPU_OP
 
-static __always_inline unsigned long __percpu_read(void __percpu *ptr, int size)
-{
-       unsigned long ret;
-
-       switch (size) {
-       case 1:
-               __asm__ __volatile__ ("ldx.b %[ret], $r21, %[ptr]       \n"
-               : [ret] "=&r"(ret)
-               : [ptr] "r"(ptr)
-               : "memory");
-               break;
-       case 2:
-               __asm__ __volatile__ ("ldx.h %[ret], $r21, %[ptr]       \n"
-               : [ret] "=&r"(ret)
-               : [ptr] "r"(ptr)
-               : "memory");
-               break;
-       case 4:
-               __asm__ __volatile__ ("ldx.w %[ret], $r21, %[ptr]       \n"
-               : [ret] "=&r"(ret)
-               : [ptr] "r"(ptr)
-               : "memory");
-               break;
-       case 8:
-               __asm__ __volatile__ ("ldx.d %[ret], $r21, %[ptr]       \n"
-               : [ret] "=&r"(ret)
-               : [ptr] "r"(ptr)
-               : "memory");
-               break;
-       default:
-               ret = 0;
-               BUILD_BUG();
-       }
-
-       return ret;
-}
-
-static __always_inline void __percpu_write(void __percpu *ptr, unsigned long val, int size)
-{
-       switch (size) {
-       case 1:
-               __asm__ __volatile__("stx.b %[val], $r21, %[ptr]        \n"
-               :
-               : [val] "r" (val), [ptr] "r" (ptr)
-               : "memory");
-               break;
-       case 2:
-               __asm__ __volatile__("stx.h %[val], $r21, %[ptr]        \n"
-               :
-               : [val] "r" (val), [ptr] "r" (ptr)
-               : "memory");
-               break;
-       case 4:
-               __asm__ __volatile__("stx.w %[val], $r21, %[ptr]        \n"
-               :
-               : [val] "r" (val), [ptr] "r" (ptr)
-               : "memory");
-               break;
-       case 8:
-               __asm__ __volatile__("stx.d %[val], $r21, %[ptr]        \n"
-               :
-               : [val] "r" (val), [ptr] "r" (ptr)
-               : "memory");
-               break;
-       default:
-               BUILD_BUG();
-       }
-}
-
 static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, int size)
 {
        switch (size) {
@@ -157,6 +88,44 @@ static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val,
        return 0;
 }
 
+#define __pcpu_op_1(op)                op ".b "
+#define __pcpu_op_2(op)                op ".h "
+#define __pcpu_op_4(op)                op ".w "
+#define __pcpu_op_8(op)                op ".d "
+
+#define _percpu_read(size, _pcp)                                       \
+({                                                                     \
+       unsigned long __pcp_ret;                                        \

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 108

Quoted 10 lines
arch/loongarch/include/asm/percpu.h +10 -0
+                                                                       \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("ldx") "%[ret], $r21, %[ptr]   \n"     \
+               : [ret] "=&r"(__pcp_ret)                                \
+               : [ptr] "r"(&(_pcp))                                    \
+               : "memory");                                            \
+       (typeof(_pcp))__pcp_ret;                                        \
+})
+
+#define __pcpu_cast_1(val)     (((unsigned long) val) & 0xff)

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 121

Quoted 13 lines
arch/loongarch/include/asm/percpu.h +13 -0
+#define __pcpu_cast_2(val)     (((unsigned long) val) & 0xffff)
+#define __pcpu_cast_4(val)     (((unsigned long) val) & 0xffffffff)
+#define __pcpu_cast_8(val)     ((unsigned long) val)
+
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       unsigned long __pcp_val = __pcpu_cast_##size(_val);             \
+                                                                       \
+       if (0) {                                                        \
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \

Emmm, in V2 I just confirm that whether it is worth to use macro instead of inline functions, I think we don't really need such a checking here. :)

Huacai

Quoted 56 lines
arch/loongarch/include/asm/percpu.h +15 -21
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("stx") "%[val], $r21, %[ptr]   \n"     \
+               :                                                       \
+               : [val] "r"(__pcp_val), [ptr] "r"(&(_pcp))              \
+               : "memory");                                            \
+} while (0)
+
 /* this_cpu_cmpxchg */
 #define _protect_cmpxchg_local(pcp, o, n)                      \
 ({                                                             \
@@ -167,18 +136,6 @@ static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val,
        __ret;                                                  \
 })
 
-#define _percpu_read(pcp)                                              \
-({                                                                     \
-       typeof(pcp) __retval;                                           \
-       __retval = (typeof(pcp))__percpu_read(&(pcp), sizeof(pcp));     \
-       __retval;                                                       \
-})
-
-#define _percpu_write(pcp, val)                                                \
-do {                                                                   \
-       __percpu_write(&(pcp), (unsigned long)(val), sizeof(pcp));      \
-} while (0)                                                            \
-
 #define _pcp_protect(operation, pcp, val)                      \
 ({                                                             \
        typeof(pcp) __retval;                                   \
@@ -215,15 +172,15 @@ do {                                                                      \
 #define this_cpu_or_4(pcp, val) _percpu_or(pcp, val)
 #define this_cpu_or_8(pcp, val) _percpu_or(pcp, val)
 
-#define this_cpu_read_1(pcp) _percpu_read(pcp)
-#define this_cpu_read_2(pcp) _percpu_read(pcp)
-#define this_cpu_read_4(pcp) _percpu_read(pcp)
-#define this_cpu_read_8(pcp) _percpu_read(pcp)
+#define this_cpu_read_1(pcp) _percpu_read(1, pcp)
+#define this_cpu_read_2(pcp) _percpu_read(2, pcp)
+#define this_cpu_read_4(pcp) _percpu_read(4, pcp)
+#define this_cpu_read_8(pcp) _percpu_read(8, pcp)
 
-#define this_cpu_write_1(pcp, val) _percpu_write(pcp, val)
-#define this_cpu_write_2(pcp, val) _percpu_write(pcp, val)
-#define this_cpu_write_4(pcp, val) _percpu_write(pcp, val)
-#define this_cpu_write_8(pcp, val) _percpu_write(pcp, val)
+#define this_cpu_write_1(pcp, val) _percpu_write(1, pcp, val)
+#define this_cpu_write_2(pcp, val) _percpu_write(2, pcp, val)
+#define this_cpu_write_4(pcp, val) _percpu_write(4, pcp, val)
+#define this_cpu_write_8(pcp, val) _percpu_write(8, pcp, val)
 
 #define this_cpu_xchg_1(pcp, val) _percpu_xchg(pcp, val)
 #define this_cpu_xchg_2(pcp, val) _percpu_xchg(pcp, val)
--

2.46.0

#2 lore raw
Quoted 4 lines · Huacai Chen
Quoted 4 lines
arch/loongarch/include/asm/percpu.h +4 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       unsigned long __pcp_val = __pcpu_cast_##size(_val);             \
+                                                                       \
Quoted 8 lines
Quoted 5 lines

+       if (0) {                                                        \
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \

Emmm, in V2 I just confirm that whether it is worth to use macro instead of inline functions, I think we don't really need such a checking here. :)

It's still (slighly) better (just from aesthetics view) to use inline function instead of macro. Is it possible to clear the header dependency and break the circle so we can still use __percpu in arch percpu.h instead?

(Simply removing __percpu from the function parameter list causes many warnings with make C=1.)

Signature · 2 lines
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University
#4 lore raw
In reply to Huacai Chen
Quoted 29 lines · Huacai Chen

Hi, Uros,

Quoted 25 lines · Uros Bizjak

_percpu_read() and _percpu_write() macros call __percpu_read() and __percpu_write() static inline functions that result in a single assembly instruction. Percpu infrastructure expects its leaf definitions to encode the size of their percpu variable, so the patch merges asm clauses from the static inline function into the corresponding leaf macros.

The secondary effect of this change is to avoid explicit __percpu function arguments. Currently, __percpu macro is defined in include/linux/compiler_types.h, but with proposed patch [1], __percpu definition will need macros from include/asm-generic/percpu.h, creating forward dependency loop.

The proposed solution is the same as x86 architecture uses.

arch/loongarch/include/asm/percpu.h +9 -0
+#define _percpu_read(size, _pcp)                                       \
+({                                                                     \
+       unsigned long __pcp_ret;                                        \
+                                                                       \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("ldx") "%[ret], $r21, %[ptr]   \n"     \
+               : [ret] "=&r"(__pcp_ret)                                \
+               : [ptr] "r"(&(_pcp))                                    \
+               : "memory");                                            \
Quoted 8 lines

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 108

Quoted 4 lines
arch/loongarch/include/asm/percpu.h +4 -0
+       (typeof(_pcp))__pcp_ret;                                        \
+})
+
+#define __pcpu_cast_1(val)     (((unsigned long) val) & 0xff)

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

Uros.

#5 lore raw
Quoted 7 lines · Uros Bizjak
Quoted 4 lines

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

It's not relevant. For example when size is 2 __pcpu_op_##size("stx") is expanded to stx.h, and stx.h only stores the lower 16 bits of a register into MEM[r21 + ptr], the high bits are ignored anyway.

Thus we can just have

+#define _percpu_write(size, _pcp, _val) \
+do { \
+ if (0) { \
+ typeof(_pcp) pto_tmp__; \
+ pto_tmp__ = (_val); \
+ (void)pto_tmp__; \
+ } \
+ __asm__ __volatile__( \
+ __pcpu_op_##size("stx") "%[val], $r21, %[ptr] \n" \
+ : \
+ : [val] "r"(_val), [ptr] "r"(&(_pcp)) \
+ : "memory"); \
+} while (0)

Signature · 2 lines
Xi Ruoyao <[email protected]>
School of Aerospace Science and Technology, Xidian University
#3 lore raw
In reply to Xi Ruoyao
Quoted 15 lines · Xi Ruoyao
Quoted 4 lines · Huacai Chen
Quoted 4 lines
arch/loongarch/include/asm/percpu.h +4 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       unsigned long __pcp_val = __pcpu_cast_##size(_val);             \
+                                                                       \
Quoted 8 lines

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 121

Quoted 5 lines
arch/loongarch/include/asm/percpu.h +5 -0
+       if (0) {                                                        \
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \

Emmm, in V2 I just confirm that whether it is worth to use macro instead of inline functions, I think we don't really need such a checking here. :)

Quoted 4 lines

It's still (slighly) better (just from aesthetics view) to use inline function instead of macro. Is it possible to clear the header dependency and break the circle so we can still use __percpu in arch percpu.h instead?

Unfortunately, it is not possible. __percpu will use macros, defined in <include/percpu.h>, and <include/percpu.h> needs <loongarch/include/asm/percpu.h>. Up to now, it was enough to include <include/linux/compiler_types.h>, so it was possible to avoid unresolved include dependency.

Quoted 2 lines

(Simply removing __percpu from the function parameter list causes many warnings with make C=1.)

This, and if/when loongson will use percpu named address checks, it will break compilation.

FYI, it is possible to implement the same percpu address space checks for loongson (as for x86) with clang and its address space attribute. The compiler has to provide __typeof_unqual__ and the arch has to define

# define __per_cpu_qual __attribute__((address_space(1)))

in its percpu.h file.

But we are not there yet ;)

Uros.

#6 lore raw
In reply to Xi Ruoyao
Quoted 19 lines · Xi Ruoyao
Quoted 7 lines · Uros Bizjak
Quoted 4 lines

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

It's not relevant. For example when size is 2 __pcpu_op_##size("stx") is expanded to stx.h, and stx.h only stores the lower 16 bits of a register into MEM[r21 + ptr], the high bits are ignored anyway.

Thus we can just have

arch/loongarch/include/asm/percpu.h +3 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       if (0) {                                                        \

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 127

Quoted 10 lines
arch/loongarch/include/asm/percpu.h +10 -0
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("stx") "%[val], $r21, %[ptr]   \n"     \
+               :                                                       \
+               : [val] "r"(_val), [ptr] "r"(&(_pcp))           \
+               : "memory");                                            \
+} while (0)

Nice, the less code, the better. If it works for loongson target, then we don't need this paranoia.

I just played safe and took the approach that x86 took.

Uros.

#7 lore raw
Quoted 35 lines · Uros Bizjak
Quoted 19 lines · Xi Ruoyao
Quoted 7 lines · Uros Bizjak
Quoted 4 lines

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

It's not relevant. For example when size is 2 __pcpu_op_##size("stx") is expanded to stx.h, and stx.h only stores the lower 16 bits of a register into MEM[r21 + ptr], the high bits are ignored anyway.

Thus we can just have

arch/loongarch/include/asm/percpu.h +3 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       if (0) {                                                        \

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 127

Quoted 10 lines
arch/loongarch/include/asm/percpu.h +10 -0
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("stx") "%[val], $r21, %[ptr]   \n"     \
+               :                                                       \
+               : [val] "r"(_val), [ptr] "r"(&(_pcp))           \
+               : "memory");                                            \
+} while (0)

Nice, the less code, the better. If it works for loongson target, then we don't need this paranoia.

Quoted 1 line

I just played safe and took the approach that x86 took.

Please note that the original code extended the value to a long type. If the simplified macro works, then the usage of macros will result in a better assembly code, where zero-extends will be omitted.

Uros.

maintainer
#8 lore raw
Quoted 37 lines · Uros Bizjak
Quoted 35 lines · Uros Bizjak
Quoted 19 lines · Xi Ruoyao
Quoted 7 lines · Uros Bizjak
Quoted 4 lines

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

It's not relevant. For example when size is 2 __pcpu_op_##size("stx") is expanded to stx.h, and stx.h only stores the lower 16 bits of a register into MEM[r21 + ptr], the high bits are ignored anyway.

Thus we can just have

arch/loongarch/include/asm/percpu.h +3 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       if (0) {                                                        \

arch/loongarch/include/asm/percpu.h static __always_inline unsigned long __percpu_xchg(void *ptr, unsigned long val, · line 127

Quoted 10 lines
arch/loongarch/include/asm/percpu.h +10 -0
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("stx") "%[val], $r21, %[ptr]   \n"     \
+               :                                                       \
+               : [val] "r"(_val), [ptr] "r"(&(_pcp))           \
+               : "memory");                                            \
+} while (0)

Nice, the less code, the better. If it works for loongson target, then we don't need this paranoia.

Quoted 5 lines
Quoted 1 line

I just played safe and took the approach that x86 took.

Please note that the original code extended the value to a long type. If the simplified macro works, then the usage of macros will result in a better assembly code, where zero-extends will be omitted.

OK, please send a simplified V4, remember to remove the if(0) checking, which is the same as V2, thanks.

Huacai

Quoted 2 lines

Uros.

#9 lore raw
Quoted 39 lines · Huacai Chen
Quoted 37 lines · Uros Bizjak
Quoted 35 lines · Uros Bizjak
Quoted 19 lines · Xi Ruoyao
Quoted 7 lines · Uros Bizjak
Quoted 4 lines

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

It's not relevant. For example when size is 2 __pcpu_op_##size("stx") is expanded to stx.h, and stx.h only stores the lower 16 bits of a register into MEM[r21 + ptr], the high bits are ignored anyway.

Thus we can just have

(quoted diff) +3 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       if (0) {                                                        \

Review

Quoted 10 lines
(quoted diff) +10 -0
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("stx") "%[val], $r21, %[ptr]   \n"     \
+               :                                                       \
+               : [val] "r"(_val), [ptr] "r"(&(_pcp))           \
+               : "memory");                                            \
+} while (0)

Nice, the less code, the better. If it works for loongson target, then we don't need this paranoia.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I just played safe and took the approach that x86 took.

Please note that the original code extended the value to a long type. If the simplified macro works, then the usage of macros will result in a better assembly code, where zero-extends will be omitted.

OK, please send a simplified V4, remember to remove the if(0) checking, which is the same as V2, thanks.

Are you sure we want to remove type checking on _val ? I'd rather leave the if(0) part, but remove forcing zero-extension.

Uros.

maintainer
#10 lore raw
Quoted 43 lines · Uros Bizjak
Quoted 41 lines · Huacai Chen
Quoted 39 lines · Uros Bizjak
Quoted 36 lines · Uros Bizjak
Quoted 19 lines · Xi Ruoyao
Quoted 7 lines · Uros Bizjak
Quoted 4 lines

If the input value is less than 0xff, then "& 0xff" is meaningless, if the input value is more than 0xff, this conversion still cannot give a correct result for the caller. So I think for all sizes it is enough to just use "((unsigned long) val)".

This part is used to force unsigned extension, otherwise the compiler will use sign-extension of the possibly signed variable.

It's not relevant. For example when size is 2 __pcpu_op_##size("stx") is expanded to stx.h, and stx.h only stores the lower 16 bits of a register into MEM[r21 + ptr], the high bits are ignored anyway.

Thus we can just have

(quoted diff) +3 -0
+#define _percpu_write(size, _pcp, _val)                                        \
+do {                                                                   \
+       if (0) {                                                        \

Review

Quoted 10 lines
(quoted diff) +10 -0
+               typeof(_pcp) pto_tmp__;                                 \
+               pto_tmp__ = (_val);                                     \
+               (void)pto_tmp__;                                        \
+       }                                                               \
+       __asm__ __volatile__(                                           \
+               __pcpu_op_##size("stx") "%[val], $r21, %[ptr]   \n"     \
+               :                                                       \
+               : [val] "r"(_val), [ptr] "r"(&(_pcp))           \
+               : "memory");                                            \
+} while (0)

Nice, the less code, the better. If it works for loongson target, then we don't need this paranoia.

I just played safe and took the approach that x86 took.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Please note that the original code extended the value to a long type. If the simplified macro works, then the usage of macros will result in a better assembly code, where zero-extends will be omitted.

OK, please send a simplified V4, remember to remove the if(0) checking, which is the same as V2, thanks.

Are you sure we want to remove type checking on _val ? I'd rather leave the if(0) part, but remove forcing zero-extension.

Yes, please, the checking is not very useful.

Huacai

Quoted 2 lines

Uros.