Skip to contentSkip to conversation

xen: arm: update asm primitives (bitops, spinlocks, atomics)

PATCH In review Ian Campbell 3 participants 16 messages Jul 19, 2013 – Aug 22, 2013

lore
Share & tools
lore
#0 lore raw

cover xen: arm: update asm primitives (bitops, spinlocks, atomics)

The following patches update and resync some of the assembly primitives which we got from Linux.

The first two have been posted independently before, the last one is new.

#1 lore raw

PATCH 1/3 xen/arm64: Assembly optimized bitops from Linux

This patch replaces the previous hashed lock implementaiton of bitops with assembly optimized ones taken from Linux v3.10-rc4.

The Linux derived ASM only supports 8 byte aligned bitmaps (which under Linux are unsigned long * rather than our void *). We do have actually uses of 4 byte alignment (i.e. the bitmaps in struct xmem_pool) which trigger alignment faults.

Therefore adjust the assembly to work in 4 byte increments, which involved:
- bit offset now bits 4:0 => mask #31 not #63
- use wN register not xN for load/modify/store loop.

There is no need to adjust the shift used to calculate the word offset, the difference is already acounted for in the #63->#31 change.

NB: Xen's build system cannot cope with the change from .c to .S file, remove xen/arch/arm/arm64/lib/.bitops.o.d or clean your build tree.

---
v2: No need to adjust the word offset shift size.

---
 xen/arch/arm/arm64/lib/bitops.S    |   68 ++++++++++++
 xen/arch/arm/arm64/lib/bitops.c    |   22 ----
 xen/include/asm-arm/arm64/bitops.h |  203 ++----------------------------------
 3 files changed, 76 insertions(+), 217 deletions(-)
 create mode 100644 xen/arch/arm/arm64/lib/bitops.S
 delete mode 100644 xen/arch/arm/arm64/lib/bitops.c
xen/arch/arm/arm64/lib/bitops.S new file +68 -0
new file mode 100644
index 0000000..80cc903
@@ -0,0 +1,68 @@
+/*
+ * Based on linux/arch/arm64/lib/bitops.h which in turn is
+ * Based on arch/arm/lib/bitops.h
+ *
+ * Copyright (C) 2013 ARM Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <xen/config.h>
+
+/*
+ * x0: bits 4:0  bit offset
+ *     bits 31:5 word offset
+ * x1: address
+ */
+	.macro	bitop, name, instr
+ENTRY(	\name	)
+	and	w3, w0, #31		// Get bit offset
+	eor	w0, w0, w3		// Clear low bits
+	mov	x2, #1
+	add	x1, x1, x0, lsr #3	// Get word offset
+	lsl	x3, x2, x3		// Create mask
+1:	ldxr	w2, [x1]
+	\instr	w2, w2, w3
+	stxr	w0, w2, [x1]
+	cbnz	w0, 1b
+	ret
+ENDPROC(\name	)
+	.endm
+
+	.macro	testop, name, instr
+ENTRY(	\name	)
+	and	w3, w0, #31		// Get bit offset
+	eor	w0, w0, w3		// Clear low bits
+	mov	x2, #1
+	add	x1, x1, x0, lsr #3	// Get word offset
+	lsl	x4, x2, x3		// Create mask
+1:	ldaxr	w2, [x1]
+	lsr	w0, w2, w3		// Save old value of bit
+	\instr	w2, w2, w4		// toggle bit
+	stlxr	w5, w2, [x1]
+	cbnz	w5, 1b
+	and	w0, w0, #1
+3:	ret
+ENDPROC(\name	)
+	.endm
+
+/*
+ * Atomic bit operations.
+ */
+	bitop	change_bit, eor
+	bitop	clear_bit, bic
+	bitop	set_bit, orr
+
+	testop	test_and_change_bit, eor
+	testop	test_and_clear_bit, bic
+	testop	test_and_set_bit, orr
xen/arch/arm/arm64/lib/bitops.c deleted +0 -22
deleted file mode 100644
index 02d8d78..0000000
@@ -1,22 +0,0 @@
-/*
- * Copyright (C) 2012 ARM Limited
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <xen/spinlock.h>
-#include <xen/bitops.h>
-
-spinlock_t __atomic_hash[ATOMIC_HASH_SIZE] /*__lock_aligned*/ = {
-       [0 ... (ATOMIC_HASH_SIZE-1)]  = SPIN_LOCK_UNLOCKED
-};
xen/include/asm-arm/arm64/bitops.h +8 -195

216 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#2 lore raw

PATCH 2/3 xen/arm64: resync atomics and spinlock asm with Linux

In reply to Ian Campbell

This picks up the changes from Linux commit 3a0310eb369a:
arm64: atomics: fix grossly inconsistent asm constraints for exclusives

Our uses of inline asm constraints for atomic operations are fairly wild and varied. We basically need to guarantee the following:

1. Any instructions with barrier implications (load-acquire/store-release) have a "memory" clobber

2. When performing exclusive accesses, the addresing mode is generated using the "Q" constraint

3. Atomic blocks which use the condition flags, have a "cc" clobber

This patch addresses these concerns which, as well as fixing the semantics of the code, stops GCC complaining about impossible asm constraints.

---
 xen/include/asm-arm/arm64/atomic.h   |   66 +++++++++++++++---------------
 xen/include/asm-arm/arm64/spinlock.h |   48 +++++++++++-----------
 xen/include/asm-arm/arm64/system.h   |   74 +++++++++++++++++-----------------
 3 files changed, 94 insertions(+), 94 deletions(-)
xen/include/asm-arm/arm64/atomic.h +33 -33
index 5e4ffed..a279755 100644
@@ -33,12 +33,12 @@ static inline void atomic_add(int i, atomic_t *v)
 	int result;
 
 	asm volatile("// atomic_add\n"
-"1:	ldxr	%w0, [%3]\n"
-"	add	%w0, %w0, %w4\n"
-"	stxr	%w1, %w0, [%3]\n"
+"1:	ldxr	%w0, %2\n"
+"	add	%w0, %w0, %w3\n"
+"	stxr	%w1, %w0, %2\n"
 "	cbnz	%w1, 1b"
-	: "=&r" (result), "=&r" (tmp), "+o" (v->counter)
-	: "r" (&v->counter), "Ir" (i)
+	: "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
+	: "Ir" (i)
 	: "cc");
 }
 
@@ -48,13 +48,13 @@ static inline int atomic_add_return(int i, atomic_t *v)
 	int result;
 
 	asm volatile("// atomic_add_return\n"
-"1:	ldaxr	%w0, [%3]\n"
-"	add	%w0, %w0, %w4\n"
-"	stlxr	%w1, %w0, [%3]\n"
+"1:	ldaxr	%w0, %2\n"
+"	add	%w0, %w0, %w3\n"
+"	stlxr	%w1, %w0, %2\n"
 "	cbnz	%w1, 1b"
-	: "=&r" (result), "=&r" (tmp), "+o" (v->counter)
-	: "r" (&v->counter), "Ir" (i)
-	: "cc");
+	: "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
+	: "Ir" (i)
+	: "cc", "memory");
 
 	return result;
 }
@@ -65,12 +65,12 @@ static inline void atomic_sub(int i, atomic_t *v)
 	int result;
 
 	asm volatile("// atomic_sub\n"
-"1:	ldxr	%w0, [%3]\n"
-"	sub	%w0, %w0, %w4\n"
-"	stxr	%w1, %w0, [%3]\n"
+"1:	ldxr	%w0, %2\n"
+"	sub	%w0, %w0, %w3\n"
+"	stxr	%w1, %w0, %2\n"
 "	cbnz	%w1, 1b"
-	: "=&r" (result), "=&r" (tmp), "+o" (v->counter)
-	: "r" (&v->counter), "Ir" (i)
+	: "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
+	: "Ir" (i)
 	: "cc");
 }
 
@@ -80,13 +80,13 @@ static inline int atomic_sub_return(int i, atomic_t *v)
 	int result;
 
 	asm volatile("// atomic_sub_return\n"
-"1:	ldaxr	%w0, [%3]\n"
-"	sub	%w0, %w0, %w4\n"
-"	stlxr	%w1, %w0, [%3]\n"
+"1:	ldaxr	%w0, %2\n"
+"	sub	%w0, %w0, %w3\n"
+"	stlxr	%w1, %w0, %2\n"
 "	cbnz	%w1, 1b"
-	: "=&r" (result), "=&r" (tmp), "+o" (v->counter)
-	: "r" (&v->counter), "Ir" (i)
-	: "cc");
+	: "=&r" (result), "=&r" (tmp), "+Q" (v->counter)
+	: "Ir" (i)
+	: "cc", "memory");
 
 	return result;
 }
@@ -97,15 +97,15 @@ static inline int atomic_cmpxchg(atomic_t *ptr, int old, int new)
 	int oldval;
 
 	asm volatile("// atomic_cmpxchg\n"
-"1:	ldaxr	%w1, [%3]\n"
-"	cmp	%w1, %w4\n"
+"1:	ldaxr	%w1, %2\n"
+"	cmp	%w1, %w3\n"
 "	b.ne	2f\n"
-"	stlxr	%w0, %w5, [%3]\n"
+"	stlxr	%w0, %w4, %2\n"
 "	cbnz	%w0, 1b\n"
 "2:"
-	: "=&r" (tmp), "=&r" (oldval), "+o" (ptr->counter)
-	: "r" (&ptr->counter), "Ir" (old), "r" (new)
-	: "cc");
+	: "=&r" (tmp), "=&r" (oldval), "+Q" (ptr->counter)
+	: "Ir" (old), "r" (new)
+	: "cc", "memory");
 
 	return oldval;
 }
@@ -115,12 +115,12 @@ static inline void atomic_clear_mask(unsigned long mask, unsigned long *addr)
 	unsigned long tmp, tmp2;
 
 	asm volatile("// atomic_clear_mask\n"
-"1:	ldxr	%0, [%3]\n"
-"	bic	%0, %0, %4\n"
-"	stxr	%w1, %0, [%3]\n"
+"1:	ldxr	%0, %2\n"
+"	bic	%0, %0, %3\n"
+"	stxr	%w1, %0, %2\n"
 "	cbnz	%w1, 1b"
-	: "=&r" (tmp), "=&r" (tmp2), "+o" (*addr)
-	: "r" (addr), "Ir" (mask)
+	: "=&r" (tmp), "=&r" (tmp2), "+Q" (*addr)
+	: "Ir" (mask)
 	: "cc");
 }
 
xen/include/asm-arm/arm64/spinlock.h +24 -24

92 lines not on this page · raw message

xen/include/asm-arm/arm64/system.h +37 -37

133 lines not on this page · raw message

Signature · 1 line
1.7.2.5
#3 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 14 lines · Ian Campbell

This picks up the changes from Linux commit 3a0310eb369a:
arm64: atomics: fix grossly inconsistent asm constraints for exclusives

Our uses of inline asm constraints for atomic operations are fairly wild and varied. We basically need to guarantee the following:

1. Any instructions with barrier implications (load-acquire/store-release) have a "memory" clobber

2. When performing exclusive accesses, the addresing mode is generated using the "Q" constraint

3. Atomic blocks which use the condition flags, have a "cc" clobber

Quoted 8 lines

This patch addresses these concerns which, as well as fixing the semantics of the code, stops GCC complaining about impossible asm constraints.

In so far as this is just pulling in upstream fixes to code we copied from linux, Acked-by: Tim Deegan <[email protected]>

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The ARM ARM says:

"The synchronization primitives follow the memory order model of the
memory type accessed by the instructions. For this reason:
- Portable code for claiming a spin-lock must include a Data Memory Barrier (DMB) operation, performed by a DMB instruction, between claiming the spin-lock and making any access that makes use of the spin-lock.
- Portable code for releasing a spin-lock must include a DMB instruction before writing to clear the spin-lock.
This requirement applies to code using:
- the Load-Exclusive/Store-Exclusive instruction pairs, for example LDREX/STREX
- the deprecated synchronization primitives, SWP/SWPB."

Are the callers of these ops expected to put in the DMBs separately?

Tim.

#4 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 19 lines · Tim Deegan
Quoted 18 lines · Ian Campbell

This picks up the changes from Linux commit 3a0310eb369a:
arm64: atomics: fix grossly inconsistent asm constraints for exclusives

Our uses of inline asm constraints for atomic operations are fairly wild and varied. We basically need to guarantee the following:

1. Any instructions with barrier implications (load-acquire/store-release) have a "memory" clobber

2. When performing exclusive accesses, the addresing mode is generated using the "Q" constraint

3. Atomic blocks which use the condition flags, have a "cc" clobber

This patch addresses these concerns which, as well as fixing the semantics of the code, stops GCC complaining about impossible asm constraints.

Quoted 7 lines
Quoted 4 lines

In so far as this is just pulling in upstream fixes to code we copied from linux, Acked-by: Tim Deegan <[email protected]>

Thanks.

Quoted 3 lines

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

I've no idea, lets ask Will & Catalin (both CCd).

Quoted 16 lines

The ARM ARM says:

"The synchronization primitives follow the memory order model of the
memory type accessed by the instructions. For this reason:
- Portable code for claiming a spin-lock must include a Data Memory Barrier (DMB) operation, performed by a DMB instruction, between claiming the spin-lock and making any access that makes use of the spin-lock.
- Portable code for releasing a spin-lock must include a DMB instruction before writing to clear the spin-lock.
This requirement applies to code using:
- the Load-Exclusive/Store-Exclusive instruction pairs, for example LDREX/STREX
- the deprecated synchronization primitives, SWP/SWPB."

Quoted 1 line

Are the callers of these ops expected to put in the DMBs separately?

Given that this is an interface used from common code I really expect not.

Ian.

#5 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 4 lines · Ian Campbell
Quoted 3 lines · Tim Deegan

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The acquire/release instructions imply half barriers, but GCC may still re-order instructions across them unless we include the memory clobber.

Will

#6 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 8 lines · Will Deacon
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Tim Deegan

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The acquire/release instructions imply half barriers, but GCC may still re-order instructions across them unless we include the memory clobber.

The instructions which Tim is querying patch the are load/store-exclusive (ldxr,stxr) not acquire/release (ldaxr/stlxr et al) ones. The a/r ones do indeed have implicit barriers but do the l/s-excl do too?

The ARM ARM Tim quoted was the v7 version but I don't see anything in the v8 docs which I have available (which I know are incomplete and may well be out of date) which says anything about barriers (implicit or otherwise) wrt the l/s-excl instructions. I do see stuff relating to the a/r instructions having implicit barriers.

Ian.

#7 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 15 lines · Ian Campbell
Quoted 8 lines · Will Deacon
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Tim Deegan

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The acquire/release instructions imply half barriers, but GCC may still re-order instructions across them unless we include the memory clobber.

The instructions which Tim is querying patch the are load/store-exclusive (ldxr,stxr) not acquire/release (ldaxr/stlxr et al) ones. The a/r ones do indeed have implicit barriers but do the l/s-excl do too?

Quoted 5 lines

The ARM ARM Tim quoted was the v7 version but I don't see anything in the v8 docs which I have available (which I know are incomplete and may well be out of date) which says anything about barriers (implicit or otherwise) wrt the l/s-excl instructions. I do see stuff relating to the a/r instructions having implicit barriers.

Ah sorry, should've looked more closely. This is basically because not all atomic operations in Linux imply barriers.

Take a look at Documentation/atomic_ops.txt and check whether it matches the semantics required by Xen.

Will

#8 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 22 lines · Will Deacon
Quoted 15 lines · Ian Campbell
Quoted 8 lines · Will Deacon
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Tim Deegan

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The acquire/release instructions imply half barriers, but GCC may still re-order instructions across them unless we include the memory clobber.

The instructions which Tim is querying patch the are load/store-exclusive (ldxr,stxr) not acquire/release (ldaxr/stlxr et al) ones. The a/r ones do indeed have implicit barriers but do the l/s-excl do too?

Quoted 5 lines

The ARM ARM Tim quoted was the v7 version but I don't see anything in the v8 docs which I have available (which I know are incomplete and may well be out of date) which says anything about barriers (implicit or otherwise) wrt the l/s-excl instructions. I do see stuff relating to the a/r instructions having implicit barriers.

Quoted 5 lines

Ah sorry, should've looked more closely. This is basically because not all atomic operations in Linux imply barriers.

Take a look at Documentation/atomic_ops.txt and check whether it matches the semantics required by Xen.

Ohoo, I wasn't aware of that (it's not that surprising mind). Some auditing of the Xen code may be required here!

Ian.

#9 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 23 lines · Ian Campbell
Quoted 22 lines · Will Deacon
Quoted 20 lines · Ian Campbell
Quoted 8 lines · Will Deacon
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Tim Deegan

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The acquire/release instructions imply half barriers, but GCC may still re-order instructions across them unless we include the memory clobber.

The instructions which Tim is querying patch the are load/store-exclusive (ldxr,stxr) not acquire/release (ldaxr/stlxr et al) ones. The a/r ones do indeed have implicit barriers but do the l/s-excl do too?

The ARM ARM Tim quoted was the v7 version but I don't see anything in the v8 docs which I have available (which I know are incomplete and may well be out of date) which says anything about barriers (implicit or otherwise) wrt the l/s-excl instructions. I do see stuff relating to the a/r instructions having implicit barriers.

Quoted 8 lines
Quoted 5 lines

Ah sorry, should've looked more closely. This is basically because not all atomic operations in Linux imply barriers.

Take a look at Documentation/atomic_ops.txt and check whether it matches the semantics required by Xen.

Ohoo, I wasn't aware of that (it's not that surprising mind). Some auditing of the Xen code may be required here!

If you need overly strong barrier semantics (i.e. dmb(); op(); dmb()) then you could look at the GCC atomic builtins, which are fairly heavy on the barrier-front iirc.

Will

#10 lore raw

Re: [PATCH 2/3] xen/arm64: resync atomics and spinlock asm with Linux

Quoted 30 lines · Will Deacon
Quoted 29 lines · Ian Campbell
Quoted 27 lines · Will Deacon
Quoted 15 lines · Ian Campbell
Quoted 8 lines · Will Deacon
Quoted 4 lines · Ian Campbell
Quoted 3 lines · Tim Deegan

But I don't understand the new 'memory' clobbers around in this patch. Or rather, I don't understand why there are memory clobbers but not DMBs.

The acquire/release instructions imply half barriers, but GCC may still re-order instructions across them unless we include the memory clobber.

The instructions which Tim is querying patch the are load/store-exclusive (ldxr,stxr) not acquire/release (ldaxr/stlxr et al) ones. The a/r ones do indeed have implicit barriers but do the l/s-excl do too?

Quoted 5 lines

The ARM ARM Tim quoted was the v7 version but I don't see anything in the v8 docs which I have available (which I know are incomplete and may well be out of date) which says anything about barriers (implicit or otherwise) wrt the l/s-excl instructions. I do see stuff relating to the a/r instructions having implicit barriers.

Ah sorry, should've looked more closely. This is basically because not all atomic operations in Linux imply barriers.

Take a look at Documentation/atomic_ops.txt and check whether it matches the semantics required by Xen.

Quoted 6 lines
Quoted 2 lines

Ohoo, I wasn't aware of that (it's not that surprising mind). Some auditing of the Xen code may be required here!

If you need overly strong barrier semantics (i.e. dmb(); op(); dmb()) then you could look at the GCC atomic builtins, which are fairly heavy on the barrier-front iirc.

Go tip, thanks. Xen's x86 background means it probably assumes some fairly heavy barriers in some places but we aren't generally averse to just fixing those where needed.

Ian.

#11 lore raw

PATCH 3/3 xen: arm: retry trylock if strex fails on free lock.

In reply to Ian Campbell

This comes from the Linux patches 15e7e5c1ebf5 for arm32 and 4ecf7ccb1973 for arm64 by Will Deacon and Catalin Marinas respectively. The Linux commit message says:

An exclusive store instruction may fail for reasons other than lock contention (e.g. a cache eviction during the critical section) so, in line with other architectures using similar exclusive instructions (alpha, mips, powerpc), retry the trylock operation if the lock appears to be free but the strex reported failure.

I have observed this due to register_cpu_notifier containing:
if ( !spin_trylock(&cpu_add_remove_lock) )
BUG(); /* Should never fail as we are called only during boot. */
which was spuriously failing.

The ARMv8 variant is taken directly from the Linux patch. For v7 I had to reimplement since we don't currently use ticket locks.

---
 xen/include/asm-arm/arm32/spinlock.h |   25 ++++++++++++++-----------
 xen/include/asm-arm/arm64/spinlock.h |    3 ++-
 2 files changed, 16 insertions(+), 12 deletions(-)
xen/include/asm-arm/arm32/spinlock.h +14 -11
index 4a11a97..ba11ad6 100644
@@ -34,17 +34,20 @@ static always_inline void _raw_spin_unlock(raw_spinlock_t *lock)
 
 static always_inline int _raw_spin_trylock(raw_spinlock_t *lock)
 {
-    unsigned long tmp;
-
-    __asm__ __volatile__(
-"   ldrex   %0, [%1]\n"
-"   teq     %0, #0\n"
-"   strexeq %0, %2, [%1]"
-    : "=&r" (tmp)
-    : "r" (&lock->lock), "r" (1)
-    : "cc");
-
-    if (tmp == 0) {
+    unsigned long contended, res;
+
+    do {
+        __asm__ __volatile__(
+    "   ldrex   %0, [%2]\n"
+    "   teq     %0, #0\n"
+    "   strexeq %1, %3, [%2]\n"
+    "   movne   %1, #0\n"
+        : "=&r" (contended), "=r" (res)
+        : "r" (&lock->lock), "r" (1)
+        : "cc");
+    } while (res);
+
+    if (!contended) {
         smp_mb();
         return 1;
     } else {
xen/include/asm-arm/arm64/spinlock.h +2 -1
index 717f2fe..3a36cfd 100644
@@ -40,9 +40,10 @@ static always_inline int _raw_spin_trylock(raw_spinlock_t *lock)
     unsigned int tmp;
 
     asm volatile(
-        "       ldaxr   %w0, %1\n"
+        "2:     ldaxr   %w0, %1\n"
         "       cbnz    %w0, 1f\n"
         "       stxr    %w0, %w2, %1\n"
+        "       cbnz    %w0, 2b\n"
         "1:\n"
         : "=&r" (tmp), "+Q" (lock->lock)
         : "r" (1)
Signature · 1 line
1.7.2.5
#12 lore raw

Re: [PATCH 3/3] xen: arm: retry trylock if strex fails on free lock.

Quoted 15 lines · Ian Campbell

This comes from the Linux patches 15e7e5c1ebf5 for arm32 and 4ecf7ccb1973 for arm64 by Will Deacon and Catalin Marinas respectively. The Linux commit message says:

An exclusive store instruction may fail for reasons other than lock contention (e.g. a cache eviction during the critical section) so, in line with other architectures using similar exclusive instructions (alpha, mips, powerpc), retry the trylock operation if the lock appears to be free but the strex reported failure.

I have observed this due to register_cpu_notifier containing:
if ( !spin_trylock(&cpu_add_remove_lock) )
BUG(); /* Should never fail as we are called only during boot. */
which was spuriously failing.

Quoted 4 lines

The ARMv8 variant is taken directly from the Linux patch. For v7 I had to reimplement since we don't currently use ticket locks.

Looks good, but:

Quoted 13 lines

static always_inline int _raw_spin_trylock(raw_spinlock_t *lock) {

xen/include/asm-arm/arm32/spinlock.h +0 -11
-    unsigned long tmp;
-
-    __asm__ __volatile__(
-"   ldrex   %0, [%1]\n"
-"   teq     %0, #0\n"
-"   strexeq %0, %2, [%1]"
-    : "=&r" (tmp)
-    : "r" (&lock->lock), "r" (1)
-    : "cc");
-
-    if (tmp == 0) {

xen/include/asm-arm/arm32/spinlock.h static always_inline void _raw_spin_unlock(raw_spinlock_t *lock) · line 46

Quoted 10 lines
xen/include/asm-arm/arm32/spinlock.h +10 -0
+    unsigned long contended, res;
+
+    do {
+        __asm__ __volatile__(
+    "   ldrex   %0, [%2]\n"
+    "   teq     %0, #0\n"
+    "   strexeq %1, %3, [%2]\n"
+    "   movne   %1, #0\n"
+        : "=&r" (contended), "=r" (res)
+        : "r" (&lock->lock), "r" (1)

Shouldn't this be using a 'Q' constraint for the lock, following the pattern set in patch 2/3?

Quoted 7 lines
xen/include/asm-arm/arm32/spinlock.h +4 -0
+        : "cc");
+    } while (res);
+
+    if (!contended) {
         smp_mb();
         return 1;
     } else {
#13 lore raw

Re: [PATCH 3/3] xen: arm: retry trylock if strex fails on free lock.

Quoted 41 lines · Tim Deegan
Quoted 15 lines · Ian Campbell

This comes from the Linux patches 15e7e5c1ebf5 for arm32 and 4ecf7ccb1973 for arm64 by Will Deacon and Catalin Marinas respectively. The Linux commit message says:

An exclusive store instruction may fail for reasons other than lock contention (e.g. a cache eviction during the critical section) so, in line with other architectures using similar exclusive instructions (alpha, mips, powerpc), retry the trylock operation if the lock appears to be free but the strex reported failure.

I have observed this due to register_cpu_notifier containing:
if ( !spin_trylock(&cpu_add_remove_lock) )
BUG(); /* Should never fail as we are called only during boot. */
which was spuriously failing.

Quoted 4 lines

The ARMv8 variant is taken directly from the Linux patch. For v7 I had to reimplement since we don't currently use ticket locks.

Looks good, but:

Quoted 18 lines

static always_inline int _raw_spin_trylock(raw_spinlock_t *lock) {

xen/include/asm-arm/arm32/spinlock.h +5 -11
-    unsigned long tmp;
-
-    __asm__ __volatile__(
-"   ldrex   %0, [%1]\n"
-"   teq     %0, #0\n"
-"   strexeq %0, %2, [%1]"
-    : "=&r" (tmp)
-    : "r" (&lock->lock), "r" (1)
-    : "cc");
-
-    if (tmp == 0) {
+    unsigned long contended, res;
+
+    do {
+        __asm__ __volatile__(
+    "   ldrex   %0, [%2]\n"
Quoted 8 lines

xen/include/asm-arm/arm32/spinlock.h static always_inline void _raw_spin_unlock(raw_spinlock_t *lock) · line 46

Quoted 5 lines
xen/include/asm-arm/arm32/spinlock.h +5 -0
+    "   teq     %0, #0\n"
+    "   strexeq %1, %3, [%2]\n"
+    "   movne   %1, #0\n"
+        : "=&r" (contended), "=r" (res)
+        : "r" (&lock->lock), "r" (1)

Shouldn't this be using a 'Q' constraint for the lock, following the pattern set in patch 2/3?

That patch was arm64 specific while this is arm32 code. 'Q' is a machine
specific constraint which is at least worded differently for 32-vs-64 in
http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints although I suppose they both read as the same thing.

I suppose the answer is that ldrex etc can take [rN, #imm] arguments, which is what "Q" rather than "r" is trying to avoid, where as the newer armv8 atomic instructions do not take a #imm (it is documented as "[rN, #0]"), so you have to use Q there.

Ian.

Quoted 8 lines
Quoted 8 lines
xen/include/asm-arm/arm32/spinlock.h +4 -0
+        : "cc");
+    } while (res);
+
+    if (!contended) {
         smp_mb();
         return 1;
     } else {
Acked-by
#14 lore raw

Re: [PATCH 3/3] xen: arm: retry trylock if strex fails on free lock.

Quoted 51 lines · Ian Campbell
Quoted 41 lines · Tim Deegan
Quoted 15 lines · Ian Campbell

This comes from the Linux patches 15e7e5c1ebf5 for arm32 and 4ecf7ccb1973 for arm64 by Will Deacon and Catalin Marinas respectively. The Linux commit message says:

An exclusive store instruction may fail for reasons other than lock contention (e.g. a cache eviction during the critical section) so, in line with other architectures using similar exclusive instructions (alpha, mips, powerpc), retry the trylock operation if the lock appears to be free but the strex reported failure.

I have observed this due to register_cpu_notifier containing:
if ( !spin_trylock(&cpu_add_remove_lock) )
BUG(); /* Should never fail as we are called only during boot. */
which was spuriously failing.

Quoted 4 lines

The ARMv8 variant is taken directly from the Linux patch. For v7 I had to reimplement since we don't currently use ticket locks.

Looks good, but:

Quoted 18 lines

static always_inline int _raw_spin_trylock(raw_spinlock_t *lock) {

xen/include/asm-arm/arm32/spinlock.h +5 -11
-    unsigned long tmp;
-
-    __asm__ __volatile__(
-"   ldrex   %0, [%1]\n"
-"   teq     %0, #0\n"
-"   strexeq %0, %2, [%1]"
-    : "=&r" (tmp)
-    : "r" (&lock->lock), "r" (1)
-    : "cc");
-
-    if (tmp == 0) {
+    unsigned long contended, res;
+
+    do {
+        __asm__ __volatile__(
+    "   ldrex   %0, [%2]\n"
Quoted 8 lines

xen/include/asm-arm/arm32/spinlock.h static always_inline void _raw_spin_unlock(raw_spinlock_t *lock) · line 46

Quoted 5 lines
xen/include/asm-arm/arm32/spinlock.h +5 -0
+    "   teq     %0, #0\n"
+    "   strexeq %1, %3, [%2]\n"
+    "   movne   %1, #0\n"
+        : "=&r" (contended), "=r" (res)
+        : "r" (&lock->lock), "r" (1)

Shouldn't this be using a 'Q' constraint for the lock, following the pattern set in patch 2/3?

Quoted 8 lines

That patch was arm64 specific while this is arm32 code. 'Q' is a machine
specific constraint which is at least worded differently for 32-vs-64 in
http://gcc.gnu.org/onlinedocs/gcc/Machine-Constraints.html#Machine-Constraints although I suppose they both read as the same thing.

I suppose the answer is that ldrex etc can take [rN, #imm] arguments, which is what "Q" rather than "r" is trying to avoid, where as the newer armv8 atomic instructions do not take a #imm (it is documented as "[rN, #0]"), so you have to use Q there.

Righto, thanks.

#15 lore raw
In reply to Ian Campbell
Quoted 5 lines · Ian Campbell

The following patches update and resync some of the assembly primitives which we got from Linux.

The first two have been posted independently before, the last one is new.

All acked by Tim and now applied, thanks.

Ian.