Skip to contentSkip to conversation

ARC: atomic: cleanup and optimization fixes

PATCH Applied Sergey Matyukevich 2 participants 4 messages Feb 22, 2022 – Apr 18, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover ARC: atomic: cleanup and optimization fixes

From: Sergey Matyukevich <[email protected]>

Hi Vineet and all,

This patch set provides cleanup for atomic-llsc code and fixes for unwanted compiler optimizations in ARCv2 32bit and 64bit atomics.

Regards,
Sergey

Sergey Matyukevich (2):
ARC: atomic: cleanup atomic-llsc definitions
ARC: atomic: avoid unwanted gcc optimizations in atomic operations

 arch/arc/include/asm/atomic-llsc.h    | 68 +++++++++++++--------------
 arch/arc/include/asm/atomic64-arcv2.h | 58 +++++++++++------------
 2 files changed, 63 insertions(+), 63 deletions(-)
Signature · 7 lines
2.25.1


_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc
#1 lore raw

PATCH 1/2 ARC: atomic: cleanup atomic-llsc definitions

From: Sergey Matyukevich <[email protected]>

Remove redundant c_op macro argument. Only asm_op is needed to define atomic operations using llock/scond.

---
 arch/arc/include/asm/atomic-llsc.h | 32 +++++++++++++++---------------
 1 file changed, 16 insertions(+), 16 deletions(-)
arch/arc/include/asm/atomic-llsc.h +16 -16
index 088d348781c1..1b0ffaeee16d 100644
@@ -5,7 +5,7 @@
 
 #define arch_atomic_set(v, i) WRITE_ONCE(((v)->counter), (i))
 
-#define ATOMIC_OP(op, c_op, asm_op)					\
+#define ATOMIC_OP(op, asm_op)					\
 static inline void arch_atomic_##op(int i, atomic_t *v)			\
 {									\
 	unsigned int val;						\
@@ -21,7 +21,7 @@ static inline void arch_atomic_##op(int i, atomic_t *v)			\
 	: "cc");							\
 }									\
 
-#define ATOMIC_OP_RETURN(op, c_op, asm_op)				\
+#define ATOMIC_OP_RETURN(op, asm_op)				\
 static inline int arch_atomic_##op##_return_relaxed(int i, atomic_t *v)	\
 {									\
 	unsigned int val;						\
@@ -42,7 +42,7 @@ static inline int arch_atomic_##op##_return_relaxed(int i, atomic_t *v)	\
 #define arch_atomic_add_return_relaxed		arch_atomic_add_return_relaxed
 #define arch_atomic_sub_return_relaxed		arch_atomic_sub_return_relaxed
 
-#define ATOMIC_FETCH_OP(op, c_op, asm_op)				\
+#define ATOMIC_FETCH_OP(op, asm_op)				\
 static inline int arch_atomic_fetch_##op##_relaxed(int i, atomic_t *v)	\
 {									\
 	unsigned int val, orig;						\
@@ -69,23 +69,23 @@ static inline int arch_atomic_fetch_##op##_relaxed(int i, atomic_t *v)	\
 #define arch_atomic_fetch_or_relaxed		arch_atomic_fetch_or_relaxed
 #define arch_atomic_fetch_xor_relaxed		arch_atomic_fetch_xor_relaxed
 
-#define ATOMIC_OPS(op, c_op, asm_op)					\
-	ATOMIC_OP(op, c_op, asm_op)					\
-	ATOMIC_OP_RETURN(op, c_op, asm_op)				\
-	ATOMIC_FETCH_OP(op, c_op, asm_op)
+#define ATOMIC_OPS(op, asm_op)					\
+	ATOMIC_OP(op, asm_op)					\
+	ATOMIC_OP_RETURN(op, asm_op)				\
+	ATOMIC_FETCH_OP(op, asm_op)
 
-ATOMIC_OPS(add, +=, add)
-ATOMIC_OPS(sub, -=, sub)
+ATOMIC_OPS(add, add)
+ATOMIC_OPS(sub, sub)
 
 #undef ATOMIC_OPS
-#define ATOMIC_OPS(op, c_op, asm_op)					\
-	ATOMIC_OP(op, c_op, asm_op)					\
-	ATOMIC_FETCH_OP(op, c_op, asm_op)
+#define ATOMIC_OPS(op, asm_op)					\
+	ATOMIC_OP(op, asm_op)					\
+	ATOMIC_FETCH_OP(op, asm_op)
 
-ATOMIC_OPS(and, &=, and)
-ATOMIC_OPS(andnot, &= ~, bic)
-ATOMIC_OPS(or, |=, or)
-ATOMIC_OPS(xor, ^=, xor)
+ATOMIC_OPS(and, and)
+ATOMIC_OPS(andnot, bic)
+ATOMIC_OPS(or, or)
+ATOMIC_OPS(xor, xor)
 
 #define arch_atomic_andnot		arch_atomic_andnot
 
Signature · 7 lines
2.25.1


_______________________________________________
linux-snps-arc mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-snps-arc
maintainer Applied
#2 lore raw

Re: [PATCH 1/2] ARC: atomic: cleanup atomic-llsc definitions

Quoted 6 lines · Sergey Matyukevich

From: Sergey Matyukevich<[email protected]>

Remove redundant c_op macro argument. Only asm_op is needed to define atomic operations using llock/scond.

Applied.

Thx,
-Vineet

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

#3 lore raw

PATCH 2/2 ARC: atomic: avoid unwanted gcc optimizations in atomic operations

In reply to Sergey Matyukevich

From: Sergey Matyukevich <[email protected]>

All the llock/scond based atomic operations read and write atomic counter field. However write operation performed by the scond instruction is not properly communicated to the compiler: inline assembly shows atomic argument as an input parameter and its clobber list does not include 'memory'. As a result, compiler can optimize the usage of atomic argument. This issue can be observed with the following simple test functions:

static void test_atomic_simple(void)

{
        int v0 = 0xfaceabab;
        atomic_t v;
        int r;

        atomic_set(&v, v0);
        r = v0;

        atomic_inc(&v);
        r += 1;
        BUG_ON(v.counter != r);
}

static void test_atomic_simple64(void)

{
        long long v0 = 0xfaceabadf00df001LL;
        atomic64_t v;
        long long r;

        atomic64_set(&v, v0);
        r = v0;

        atomic64_inc(&v);
        r += 1LL;
        BUG_ON(v.counter != r);
}

This commit fixes unwanted optimizations notifying compiler about write operations. For this purpose atomic argument is moved to the output list. Besides "memory" is added to the list of clobber arguments unless it is explicitly clarified by the comment why it is not needed. Finally, appropriate memory constraint 'ATO' is set for atomic ops arguments.

---
 arch/arc/include/asm/atomic-llsc.h    | 36 ++++++++---------
 arch/arc/include/asm/atomic64-arcv2.h | 58 +++++++++++++--------------
 2 files changed, 47 insertions(+), 47 deletions(-)
arch/arc/include/asm/atomic-llsc.h +18 -18
index 1b0ffaeee16d..b6cbc3ea1f52 100644
@@ -11,14 +11,14 @@ static inline void arch_atomic_##op(int i, atomic_t *v)			\
 	unsigned int val;						\
 									\
 	__asm__ __volatile__(						\
-	"1:	llock   %[val], [%[ctr]]		\n"		\
+	"1:	llock   %[val], %[ctr]			\n"		\
 	"	" #asm_op " %[val], %[val], %[i]	\n"		\
-	"	scond   %[val], [%[ctr]]		\n"		\
+	"	scond   %[val], %[ctr]			\n"		\
 	"	bnz     1b				\n"		\
-	: [val]	"=&r"	(val) /* Early clobber to prevent reg reuse */	\
-	: [ctr]	"r"	(&v->counter), /* Not "m": llock only supports reg direct addr mode */	\
-	  [i]	"ir"	(i)						\
-	: "cc");							\
+	: [val]	"=&r"	(val), /* Early clobber to prevent reg reuse */	\
+	  [ctr] "+ATO" (v->counter)				\
+	: [i]	"ir"	(i)						\
+	: "cc", "memory");						\
 }									\
 
 #define ATOMIC_OP_RETURN(op, asm_op)				\
@@ -27,14 +27,14 @@ static inline int arch_atomic_##op##_return_relaxed(int i, atomic_t *v)	\
 	unsigned int val;						\
 									\
 	__asm__ __volatile__(						\
-	"1:	llock   %[val], [%[ctr]]		\n"		\
+	"1:	llock   %[val], %[ctr]			\n"		\
 	"	" #asm_op " %[val], %[val], %[i]	\n"		\
-	"	scond   %[val], [%[ctr]]		\n"		\
+	"	scond   %[val], %[ctr]			\n"		\
 	"	bnz     1b				\n"		\
-	: [val]	"=&r"	(val)						\
-	: [ctr]	"r"	(&v->counter),					\
-	  [i]	"ir"	(i)						\
-	: "cc");							\
+	: [val]	"=&r"	(val),						\
+	  [ctr] "+ATO" (v->counter)				\
+	: [i]	"ir"	(i)						\
+	: "cc", "memory");						\
 									\
 	return val;							\
 }
@@ -48,15 +48,15 @@ static inline int arch_atomic_fetch_##op##_relaxed(int i, atomic_t *v)	\
 	unsigned int val, orig;						\
 									\
 	__asm__ __volatile__(						\
-	"1:	llock   %[orig], [%[ctr]]		\n"		\
+	"1:	llock   %[orig], %[ctr]			\n"		\
 	"	" #asm_op " %[val], %[orig], %[i]	\n"		\
-	"	scond   %[val], [%[ctr]]		\n"		\
+	"	scond   %[val], %[ctr]			\n"		\
 	"	bnz     1b				\n"		\
 	: [val]	"=&r"	(val),						\
-	  [orig] "=&r" (orig)						\
-	: [ctr]	"r"	(&v->counter),					\
-	  [i]	"ir"	(i)						\
-	: "cc");							\
+	  [orig] "=&r" (orig),						\
+	  [ctr] "+ATO" (v->counter)				\
+	: [i]	"ir"	(i)						\
+	: "cc", "memory");						\
 									\
 	return orig;							\
 }
arch/arc/include/asm/atomic64-arcv2.h +29 -29

127 lines not on this page · raw message

Signature · 7 lines
2.25.1


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