Skip to contentSkip to conversation

Use ticket locks for spinlocks

PATCH Superseded David Vrabel 7 participants 44 messages Apr 10, 2015 – Apr 21, 2015

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Use ticket locks for spinlocks

Use ticket locks for spin locks instead of the current byte locks. Ticket locks are fair. This is an important property for hypervisor locks.

This has the following limitations:

- No ARM optimized xadd() implementation. The generic implementation usign cmpxchg is probably sub-optimal.

Note that spin_lock_irq() and spin_lock_irqsave() now spin with irqs disabled (previously, they would spin with irqs enabled if possible). This is required to prevent deadlocks when the irq handler tries to take the same lock with a higher ticket.

A quick survey of the interrupt disabling spin locks suggests that this should not introduce significant extra interrupt latency (there aren't many such locks and I don't think they're heavily contended) but we are attempting to collect experimental evidence to support this.

Performance results (using a earlier prototype) of aggregate network throughput between 20 VIF pairs shows good improvements.

http://xenbits.xen.org/people/dvrabel/3336.png

This benchmark is limited by contention on the map track lock.

Changes in v2:
- Reimplemented in C, requiring only an arch-specific xadd() op
- Optimize spin_lock_recursive() to call spin_lock() since trylock loops are bad with ticket locks.

David

#1 lore raw

PATCH v2 1/6 x86/hvm: don't include asm/spinlock.h

asm/spinlock.h should not be included directly.

---
 xen/arch/x86/hvm/hvm.c     |    1 -
 xen/arch/x86/hvm/svm/svm.c |    1 -
 xen/arch/x86/hvm/vmx/vmx.c |    1 -
 3 files changed, 3 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -1
index 4734d71..6937857 100644
@@ -52,7 +52,6 @@
 #include <asm/xstate.h>
 #include <asm/traps.h>
 #include <asm/mc146818rtc.h>
-#include <asm/spinlock.h>
 #include <asm/mce.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/vpt.h>
xen/arch/x86/hvm/svm/svm.c +0 -1
index b6e77cd..6734fb6 100644
@@ -41,7 +41,6 @@
 #include <asm/msr.h>
 #include <asm/i387.h>
 #include <asm/iocap.h>
-#include <asm/spinlock.h>
 #include <asm/hvm/emulate.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
xen/arch/x86/hvm/vmx/vmx.c +0 -1
index e1c55ce..2a0610b 100644
@@ -35,7 +35,6 @@
 #include <asm/types.h>
 #include <asm/debugreg.h>
 #include <asm/msr.h>
-#include <asm/spinlock.h>
 #include <asm/paging.h>
 #include <asm/p2m.h>
 #include <asm/mem_sharing.h>
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCHv2 1/6] x86/hvm: don't include asm/spinlock.h

Quoted 3 lines · David Vrabel

asm/spinlock.h should not be included directly.

s/asm/xen/g instead of a straight delete?

Otherwise you are relying on pulling in xen/spinlock.h implicitly.

~Andrew

Quoted 42 lines
---
 xen/arch/x86/hvm/hvm.c     |    1 -
 xen/arch/x86/hvm/svm/svm.c |    1 -
 xen/arch/x86/hvm/vmx/vmx.c |    1 -
 3 files changed, 3 deletions(-)
xen/arch/x86/hvm/hvm.c +0 -1
index 4734d71..6937857 100644
@@ -52,7 +52,6 @@
 #include <asm/xstate.h>
 #include <asm/traps.h>
 #include <asm/mc146818rtc.h>
-#include <asm/spinlock.h>
 #include <asm/mce.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/vpt.h>
xen/arch/x86/hvm/svm/svm.c +0 -1
index b6e77cd..6734fb6 100644
@@ -41,7 +41,6 @@
 #include <asm/msr.h>
 #include <asm/i387.h>
 #include <asm/iocap.h>
-#include <asm/spinlock.h>
 #include <asm/hvm/emulate.h>
 #include <asm/hvm/hvm.h>
 #include <asm/hvm/support.h>
xen/arch/x86/hvm/vmx/vmx.c +0 -1
index e1c55ce..2a0610b 100644
@@ -35,7 +35,6 @@
 #include <asm/types.h>
 #include <asm/debugreg.h>
 #include <asm/msr.h>
-#include <asm/spinlock.h>
 #include <asm/paging.h>
 #include <asm/p2m.h>
 #include <asm/mem_sharing.h>
#3 lore raw

Re: [PATCHv2 1/6] x86/hvm: don't include asm/spinlock.h

Quoted 8 lines · Andrew Cooper
Quoted 3 lines · David Vrabel

asm/spinlock.h should not be included directly.

s/asm/xen/g instead of a straight delete?

Otherwise you are relying on pulling in xen/spinlock.h implicitly.

None of these files declare a spinlock so getting the header implicitly is correct, IMO.

David

Quoted 3 lines
Quoted 3 lines
 xen/arch/x86/hvm/hvm.c     |    1 -
 xen/arch/x86/hvm/svm/svm.c |    1 -
 xen/arch/x86/hvm/vmx/vmx.c |    1 -
Acked-by
#4 lore raw

Re: [PATCHv2 1/6] x86/hvm: don't include asm/spinlock.h

Quoted 10 lines · David Vrabel
Quoted 7 lines · Andrew Cooper
Quoted 3 lines · David Vrabel

asm/spinlock.h should not be included directly.

s/asm/xen/g instead of a straight delete?

Otherwise you are relying on pulling in xen/spinlock.h implicitly.

None of these files declare a spinlock so getting the header implicitly is correct, IMO.

In which case, good riddance.

#6 lore raw

PATCH v2 2/6 x86/mtrr: include asm/atomic.h

In reply to David Vrabel

asm/atomic.h is needed but only included indirectly via asm/spinlock.h.

---
 xen/arch/x86/cpu/mtrr/main.c |    1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/cpu/mtrr/main.c +1 -0
index f5d5317..030a911 100644
@@ -36,6 +36,7 @@
 #include <xen/lib.h>
 #include <xen/smp.h>
 #include <xen/spinlock.h>
+#include <asm/atomic.h>
 #include <asm/mtrr.h>
 #include <asm/uaccess.h>
 #include <asm/processor.h>
Signature · 1 line
1.7.10.4
Reviewed-by on v2 2/6
#7 lore raw

Re: [PATCHv2 2/6] x86/mtrr: include asm/atomic.h

Quoted 4 lines · David Vrabel

asm/atomic.h is needed but only included indirectly via asm/spinlock.h.

Quoted 16 lines
---
 xen/arch/x86/cpu/mtrr/main.c |    1 +
 1 file changed, 1 insertion(+)
xen/arch/x86/cpu/mtrr/main.c +1 -0
index f5d5317..030a911 100644
@@ -36,6 +36,7 @@
 #include <xen/lib.h>
 #include <xen/smp.h>
 #include <xen/spinlock.h>
+#include <asm/atomic.h>
 #include <asm/mtrr.h>
 #include <asm/uaccess.h>
 #include <asm/processor.h>
#9 lore raw

PATCH v2 3/6 xen: generic xadd() for ticket locks

In reply to David Vrabel

Provide a generic xadd() implementation for architectures that don't provide one.

This is only temporary until arm/arm64 provides an xadd() implementation.

---
 xen/common/spinlock.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
xen/common/spinlock.c +21 -0
index 5fd8b1c..0c4289c 100644
@@ -10,6 +10,27 @@
 #include <asm/processor.h>
 #include <asm/atomic.h>
 
+#ifndef xadd
+
+static u32 generic_xaddl(volatile u32 *ptr, u32 v)
+{
+    u32 old, new, prev;
+
+    old = read_atomic(ptr);
+    for(;;) {
+        new = old + v;
+        prev = cmpxchg(ptr, old, new);
+        if (prev == old)
+            break;
+        old = prev;
+    }
+    return old;
+}
+
+#define xadd(ptr, v) generic_xaddl((ptr), (v))
+
+#endif
+
 #ifndef NDEBUG
 
 static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0);
Signature · 1 line
1.7.10.4
#10 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 2 lines · David Vrabel

This is only temporary until arm/arm64 provides an xadd() implementation.

I'll assume that you aren't planning on doing this and add it to my own TODO list. Although, I'm more than willing to be preempted by another ARM dev...

Quoted 38 lines
---
 xen/common/spinlock.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
xen/common/spinlock.c +21 -0
index 5fd8b1c..0c4289c 100644
@@ -10,6 +10,27 @@
 #include <asm/processor.h>
 #include <asm/atomic.h>
 
+#ifndef xadd
+
+static u32 generic_xaddl(volatile u32 *ptr, u32 v)
+{
+    u32 old, new, prev;
+
+    old = read_atomic(ptr);
+    for(;;) {
+        new = old + v;
+        prev = cmpxchg(ptr, old, new);
+        if (prev == old)
+            break;
+        old = prev;
+    }
+    return old;
+}
+
+#define xadd(ptr, v) generic_xaddl((ptr), (v))
+
+#endif
+
 #ifndef NDEBUG
 
 static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0);
#11 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 7 lines · Ian Campbell
Quoted 2 lines · David Vrabel

This is only temporary until arm/arm64 provides an xadd() implementation.

I'll assume that you aren't planning on doing this and add it to my own TODO list. Although, I'm more than willing to be preempted by another ARM dev...

Thanks. I'd hoped I could lift something from Linux but there's nothing directly suited.

David

#12 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 11 lines · David Vrabel
Quoted 7 lines · Ian Campbell
Quoted 2 lines · David Vrabel

This is only temporary until arm/arm64 provides an xadd() implementation.

I'll assume that you aren't planning on doing this and add it to my own TODO list. Although, I'm more than willing to be preempted by another ARM dev...

Thanks. I'd hoped I could lift something from Linux but there's nothing directly suited.

I looked too and didn't find anything suitable. It should be pretty trivial using ldrex and friends, I've added it to my list.

Ian.

Reviewed-by on v2 3/6
#13 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

In reply to David Vrabel
Quoted 7 lines · David Vrabel

Provide a generic xadd() implementation for architectures that don't provide one.

This is only temporary until arm/arm64 provides an xadd() implementation.

The logic looks fine, but please:
- add a comment explaining what it does; and
- convert it to Xen coding style to match the rest of the file.

With that,

Cheers,

Tim.

Quoted 44 lines
---
 xen/common/spinlock.c |   21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
xen/common/spinlock.c +21 -0
index 5fd8b1c..0c4289c 100644
@@ -10,6 +10,27 @@
 #include <asm/processor.h>
 #include <asm/atomic.h>
 
+#ifndef xadd
+
+static u32 generic_xaddl(volatile u32 *ptr, u32 v)
+{
+    u32 old, new, prev;
+
+    old = read_atomic(ptr);
+    for(;;) {
+        new = old + v;
+        prev = cmpxchg(ptr, old, new);
+        if (prev == old)
+            break;
+        old = prev;
+    }
+    return old;
+}
+
+#define xadd(ptr, v) generic_xaddl((ptr), (v))
+
+#endif
+
 #ifndef NDEBUG
 
 static atomic_t spin_debug __read_mostly = ATOMIC_INIT(0);
Signature · 7 lines
1.7.10.4


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#14 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

In reply to David Vrabel
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 16:19, <[email protected]> wrote:

+#define xadd(ptr, v) generic_xaddl((ptr), (v))

I think it is at least confusing to call the thing xadd (looking to be size generic) and then expand to generic_xaddl (only supporting 32-bit operations), yet subsequently implementing a size-generic xadd() for x86. Plus the extra parentheses should be removed to not needlessly obfuscate things.

Jan

#15 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 16:19, <[email protected]> wrote:

+#define xadd(ptr, v) generic_xaddl((ptr), (v))

I think it is at least confusing to call the thing xadd (looking to be size generic) and then expand to generic_xaddl (only supporting 32-bit operations), yet subsequently implementing a size-generic xadd() for x86.

Indeed, and I went to build on arm32 prior to hacking up a proper xadd and:

spinlock.c: In function ‘_spin_lock’:
spinlock.c:145:5: error: passing argument 1 of ‘generic_xaddl’ from incompatible pointer type [-Werror]

     tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
     ^
spinlock.c:15:12: note: expected ‘volatile u32 *’ but argument is of type ‘union spinlock_tickets_t *’
 static u32 generic_xaddl(volatile u32 *ptr, u32 v)
            ^

(I hope to knock up the arm asm version in the next hour or so, so you may not care...)

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#16 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 11 lines · Ian Campbell
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 16:19, <[email protected]> wrote:

+#define xadd(ptr, v) generic_xaddl((ptr), (v))

I think it is at least confusing to call the thing xadd (looking to be size generic) and then expand to generic_xaddl (only supporting 32-bit operations), yet subsequently implementing a size-generic xadd() for x86.

Indeed, and I went to build on arm32 prior to hacking up a proper xadd and:

Slightly more surprisingly I also see something similar on arm64:

spinlock.c: In function ‘_spin_lock’:
spinlock.c:145:5: error: passing argument 1 of ‘generic_xaddl’ from incompatible pointer type [-Werror]

     tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
     ^
spinlock.c:15:12: note: expected ‘volatile u32 *’ but argument is of
type ‘union spinlock_tickets_t *’
 static u32 generic_xaddl(volatile u32 *ptr, u32 v)
            ^
Quoted 2 lines

(I hope to knock up the arm asm version in the next hour or so, so you may not care...)

ditto.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

In reply to Ian Campbell
Quoted 14 lines · Ian Campbell
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 16:19, <[email protected]> wrote:

+#define xadd(ptr, v) generic_xaddl((ptr), (v))

I think it is at least confusing to call the thing xadd (looking to be size generic) and then expand to generic_xaddl (only supporting 32-bit operations), yet subsequently implementing a size-generic xadd() for x86.

Indeed, and I went to build on arm32 prior to hacking up a proper xadd and:

spinlock.c: In function ‘_spin_lock’:
spinlock.c:145:5: error: passing argument 1 of ‘generic_xaddl’ from incompatible pointer type [-Werror]

Quoted 8 lines
     tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
     ^
spinlock.c:15:12: note: expected ‘volatile u32 *’ but argument is of type ‘union spinlock_tickets_t *’
 static u32 generic_xaddl(volatile u32 *ptr, u32 v)
            ^

(I hope to knock up the arm asm version in the next hour or so, so you may not care...)

Can you use

git://xenbits.xen.org/people/dvrabel/xen.git ticketlocks-v3

as a base instead?

David

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by
#18 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 21 lines · David Vrabel
Quoted 20 lines · Ian Campbell
Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 16:19, <[email protected]> wrote:

+#define xadd(ptr, v) generic_xaddl((ptr), (v))

I think it is at least confusing to call the thing xadd (looking to be size generic) and then expand to generic_xaddl (only supporting 32-bit operations), yet subsequently implementing a size-generic xadd() for x86.

Indeed, and I went to build on arm32 prior to hacking up a proper xadd and:

spinlock.c: In function ‘_spin_lock’:
spinlock.c:145:5: error: passing argument 1 of ‘generic_xaddl’ from incompatible pointer type [-Werror]

     tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
     ^
spinlock.c:15:12: note: expected ‘volatile u32 *’ but argument is of type ‘union spinlock_tickets_t *’
 static u32 generic_xaddl(volatile u32 *ptr, u32 v)
            ^
Quoted 8 lines
Quoted 2 lines

(I hope to knock up the arm asm version in the next hour or so, so you may not care...)

Can you use

git://xenbits.xen.org/people/dvrabel/xen.git ticketlocks-v3

as a base instead?

I tried that and it built and booted just fine on both arm32 and arm64.

I eyeballed the assembly produced via the use of __sync_fetch_and_add (for _spin_lock only) and it is exactly what I would have written in my own versions.

I was using gcc 4.8.3 in both cases. For arm64 I'm pretty sure we don't want to consider anything earlier.

For arm32 I have also tried gcc 4.6.3 (Debian Wheezy's compiler) and it built and booted, and eyeballing shows the same asm. I think that's the earliest we really need to worry about.

IOW I'm not going to bother with custom versions of these functions on ARM. If you wanted you could drop the #ifndef xadd from asm-arm/system.h.

Perhaps it would be useful to add some of the info from my tests reported above, or a reference to this mail, to the commit log?

in either case you can add:

to the patch below.

commit b08cf3fa4791d7ff0d01fb932192e02078ce670a

Author: David Vrabel <[email protected]>
Date:   Thu Apr 16 15:31:18 2015 +0100

arm: provide xadd()

xadd() atomically adds a value and returns the previous value. This is needed to implement ticket locks.

This generic arm implementation uses the GCC __sync_fetch_and_add() builtin, but a arm32 or arm64 specific variant could be provided in the future (e.g., if required to support older versions of GCC).

xen/include/asm-arm/system.h +4 -0
index ce3d38a..f037e84 100644
@@ -51,6 +51,10 @@
 # error "unknown ARM variant"
 #endif
 
+#ifndef xadd
+#  define xadd(x, v) __sync_fetch_and_add(x, v)
+#endif
+
 extern struct vcpu *__context_switch(struct vcpu *prev, struct vcpu *next);
 
 #endif

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#19 lore raw

Re: [PATCHv2 3/6] xen: generic xadd() for ticket locks

Quoted 5 lines · Ian Campbell
Quoted 4 lines · David Vrabel

Can you use

git://xenbits.xen.org/people/dvrabel/xen.git ticketlocks-v3

git://xenbits.xen.org/people/dvrabel/xen.git ticketlock-v3

Quoted 17 lines

I tried that and it built and booted just fine on both arm32 and arm64.

I eyeballed the assembly produced via the use of __sync_fetch_and_add (for _spin_lock only) and it is exactly what I would have written in my own versions.

I was using gcc 4.8.3 in both cases. For arm64 I'm pretty sure we don't want to consider anything earlier.

For arm32 I have also tried gcc 4.6.3 (Debian Wheezy's compiler) and it built and booted, and eyeballing shows the same asm. I think that's the earliest we really need to worry about.

IOW I'm not going to bother with custom versions of these functions on ARM. If you wanted you could drop the #ifndef xadd from asm-arm/system.h.

Quoted 6 lines

Perhaps it would be useful to add some of the info from my tests reported above, or a reference to this mail, to the commit log?

in either case you can add:

Thanks, Ian!

David

#20 lore raw

PATCH v2 4/6 x86: provide xadd()

In reply to David Vrabel

xadd() atomically adds a value and returns the previous value. This is needed to implement ticket locks.

---
 xen/include/asm-x86/system.h |   29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
xen/include/asm-x86/system.h +29 -0
index 7111329..1e6c6a8 100644
@@ -117,6 +117,35 @@ static always_inline unsigned long __cmpxchg(
                                    (unsigned long)__n,sizeof(*(ptr)))); \
 })
 
+static always_inline unsigned long __xadd(
+    volatile void *ptr, unsigned long v, int size)
+{
+    switch ( size )
+    {
+    case 1:
+        asm volatile ( "lock; xaddb %b0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 2:
+        asm volatile ( "lock; xaddw %w0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 4:
+        asm volatile ( "lock; xaddl %k0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 8:
+        asm volatile ( "lock; xaddq %q0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    }
+    return 0;
+}
+
+#define xadd(ptr, v) ({                                         \
+            __xadd((ptr), (unsigned long)(v), sizeof(*(ptr)));  \
+        })
+
 /*
  * Both Intel and AMD agree that, from a programmer's viewpoint:
  *  Loads cannot be reordered relative to other loads.
Signature · 1 line
1.7.10.4
#21 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 29 lines · David Vrabel

xadd() atomically adds a value and returns the previous value. This is needed to implement ticket locks.

---
 xen/include/asm-x86/system.h |   29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
xen/include/asm-x86/system.h +13 -0
index 7111329..1e6c6a8 100644
@@ -117,6 +117,35 @@ static always_inline unsigned long __cmpxchg(
                                    (unsigned long)__n,sizeof(*(ptr)))); \
 })
 
+static always_inline unsigned long __xadd(
+    volatile void *ptr, unsigned long v, int size)
+{
+    switch ( size )
+    {
+    case 1:
+        asm volatile ( "lock; xaddb %b0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 2:
+        asm volatile ( "lock; xaddw %w0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 142

Quoted 10 lines
xen/include/asm-x86/system.h +10 -0
+    case 4:
+        asm volatile ( "lock; xaddl %k0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 8:
+        asm volatile ( "lock; xaddq %q0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    }
+    return 0;

ASSERT_UNREACHABLE(), rather? And some appropriate BUILD_BUG_ON?

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

Tim.

#22 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 43 lines · Tim Deegan
Quoted 29 lines · David Vrabel

xadd() atomically adds a value and returns the previous value. This is needed to implement ticket locks.

---
 xen/include/asm-x86/system.h |   29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
xen/include/asm-x86/system.h +13 -0
index 7111329..1e6c6a8 100644
@@ -117,6 +117,35 @@ static always_inline unsigned long __cmpxchg(
                                    (unsigned long)__n,sizeof(*(ptr)))); \
 })
 
+static always_inline unsigned long __xadd(
+    volatile void *ptr, unsigned long v, int size)
+{
+    switch ( size )
+    {
+    case 1:
+        asm volatile ( "lock; xaddb %b0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 2:
+        asm volatile ( "lock; xaddw %w0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 142

Quoted 10 lines
xen/include/asm-x86/system.h +10 -0
+    case 4:
+        asm volatile ( "lock; xaddl %k0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 8:
+        asm volatile ( "lock; xaddq %q0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    }
+    return 0;

ASSERT_UNREACHABLE(), rather? And some appropriate BUILD_BUG_ON?

Quoted 4 lines

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I wasn't aware of the GCC built-ins -- I'll try them out. I think asm volatile is a compiler barrier though.

David

#23 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 44 lines · David Vrabel
Quoted 43 lines · Tim Deegan
Quoted 29 lines · David Vrabel

xadd() atomically adds a value and returns the previous value. This is needed to implement ticket locks.

---
 xen/include/asm-x86/system.h |   29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
xen/include/asm-x86/system.h +13 -0
index 7111329..1e6c6a8 100644
@@ -117,6 +117,35 @@ static always_inline unsigned long __cmpxchg(
                                    (unsigned long)__n,sizeof(*(ptr)))); \
 })
 
+static always_inline unsigned long __xadd(
+    volatile void *ptr, unsigned long v, int size)
+{
+    switch ( size )
+    {
+    case 1:
+        asm volatile ( "lock; xaddb %b0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 2:
+        asm volatile ( "lock; xaddw %w0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;

xen/include/asm-x86/system.h static always_inline unsigned long __cmpxchg( · line 142

Quoted 10 lines
xen/include/asm-x86/system.h +10 -0
+    case 4:
+        asm volatile ( "lock; xaddl %k0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    case 8:
+        asm volatile ( "lock; xaddq %q0,%1"
+                       : "+r" (v), "+m" (*__xg((volatile void *)ptr)));
+        return v;
+    }
+    return 0;

ASSERT_UNREACHABLE(), rather? And some appropriate BUILD_BUG_ON?

Quoted 7 lines
Quoted 4 lines

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I wasn't aware of the GCC built-ins -- I'll try them out. I think asm volatile is a compiler barrier though.

Nope - you need to specify a "memory" clobber for that (as the otehr similar ops in this file do).

Cheers,

Tim.

#24 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

In reply to Tim Deegan
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 13:25, <[email protected]> wrote:

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I'm afraid that's useful only from gcc 4.5.x onwards; earlier versions (on x86 at least) simply generate a function call relying on a library to implement it.

Jan

#25 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 13:25, <[email protected]> wrote:

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I'm afraid that's useful only from gcc 4.5.x onwards; earlier versions (on x86 at least) simply generate a function call relying on a library to implement it.

Darn. :(

Tim.

#26 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 7 lines · Tim Deegan
Quoted 6 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 13:25, <[email protected]> wrote:

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

Quoted 5 lines
Quoted 3 lines

I'm afraid that's useful only from gcc 4.5.x onwards; earlier versions (on x86 at least) simply generate a function call relying on a library to implement it.

Darn. :(

Might it still be a better generic fallback than the C/cmpxchg one?

On x86 you'd get the asm version, and on ARM you need 4.5.x onwards anyway, plus in general the ARM folks seem to recommend using the builtins more anyway.

Ian.

#27 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 14:55, <[email protected]> wrote:
Quoted 11 lines · Tim Deegan
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 13:25, <[email protected]> wrote:

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I'm afraid that's useful only from gcc 4.5.x onwards; earlier versions (on x86 at least) simply generate a function call relying on a library to implement it.

Quoted 5 lines
Quoted 1 line

Darn. :(

Might it still be a better generic fallback than the C/cmpxchg one?

On x86 you'd get the asm version, and on ARM you need 4.5.x onwards

Is that the case even for ARM32?

Quoted 2 lines

anyway, plus in general the ARM folks seem to recommend using the builtins more anyway.

I assume you checked that 4.5.x actually expands said builtin for ARM (as I only checked x86)?

If the answers to both are yes, then I certainly agree.

Jan

#28 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 14:55, <[email protected]> wrote:
Quoted 11 lines · Tim Deegan
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 13:25, <[email protected]> wrote:

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I'm afraid that's useful only from gcc 4.5.x onwards; earlier versions (on x86 at least) simply generate a function call relying on a library to implement it.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Darn. :(

Might it still be a better generic fallback than the C/cmpxchg one?

On x86 you'd get the asm version, and on ARM you need 4.5.x onwards

Is that the case even for ARM32?

Erm, I thought so when I wrote it but now I think again I'm not actually so sure.

Quoted 5 lines
Quoted 2 lines

anyway, plus in general the ARM folks seem to recommend using the builtins more anyway.

I assume you checked that 4.5.x actually expands said builtin for ARM (as I only checked x86)?

No, I hadn't, I should though, you are right.

Ian.

#29 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

Quoted 25 lines · Ian Campbell
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 14:55, <[email protected]> wrote:
Quoted 11 lines · Tim Deegan
Quoted 9 lines · Jan Beulich
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 13:25, <[email protected]> wrote:

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

I'm afraid that's useful only from gcc 4.5.x onwards; earlier versions (on x86 at least) simply generate a function call relying on a library to implement it.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Darn. :(

Might it still be a better generic fallback than the C/cmpxchg one?

On x86 you'd get the asm version, and on ARM you need 4.5.x onwards

Is that the case even for ARM32?

Erm, I thought so when I wrote it but now I think again I'm not actually so sure.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

anyway, plus in general the ARM folks seem to recommend using the builtins more anyway.

I assume you checked that 4.5.x actually expands said builtin for ARM (as I only checked x86)?

No, I hadn't, I should though, you are right.

If nm on libgcc.a is any indication (it should be?) then it expands on arm64 but is an out of line function on arm32, so like Tim said: Darn.

Ian.

#30 lore raw

Re: [PATCHv2 4/6] x86: provide xadd()

In reply to Tim Deegan
Quoted 4 lines · Tim Deegan

But also: AFAICS the GCC builtin __sync_fetch_and_add() does almost exactly this (the difference being that those are also compiler barriers where this is only a CPU barrier). Should we be using it instead?

In general I'm of the opinion we should, because it makes my life easier on ARM (and perhaps we should trust compiler folks...)

Big question though would be when it became available in gcc. I think that's generally why Linux hasn't switched in many cases, or perhaps (historical?) concerns about whether they are optimal enough.

Ian.

#31 lore raw

PATCH v2 5/6 xen: use ticket locks for spin locks

In reply to David Vrabel

Replace the byte locks with ticket locks. Ticket locks are: a) fair; and b) peform better when contented since they spin without an atomic operation.

The lock is split into two ticket values: head and tail. A locker acquires a ticket by (atomically) increasing tail and using the previous tail value. A CPU holds the lock if its ticket == head. The lock is released by increasing head.

Architectures need only provide an xadd() implementation.

---
 xen/common/spinlock.c      |   80 +++++++++++++++++++++-----------------------
 xen/include/xen/spinlock.h |   16 ++++++---
 2 files changed, 50 insertions(+), 46 deletions(-)
xen/common/spinlock.c +39 -41
index 0c4289c..99163c5 100644
@@ -138,14 +138,17 @@ void spin_debug_disable(void)
 
 void _spin_lock(spinlock_t *lock)
 {
+    spinlock_tickets_t tickets = { .tail = 1, };
     LOCK_PROFILE_VAR;
 
     check_lock(&lock->debug);
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
+    tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
+    if ( tickets.tail != read_atomic(&lock->tickets.head) )
     {
         LOCK_PROFILE_BLOCK;
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
+        do {
             cpu_relax();
+        } while ( tickets.tail != read_atomic(&lock->tickets.head) );
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -153,40 +156,17 @@ void _spin_lock(spinlock_t *lock)
 
 void _spin_lock_irq(spinlock_t *lock)
 {
-    LOCK_PROFILE_VAR;
-
     ASSERT(local_irq_is_enabled());
     local_irq_disable();
-    check_lock(&lock->debug);
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
-    {
-        LOCK_PROFILE_BLOCK;
-        local_irq_enable();
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
-            cpu_relax();
-        local_irq_disable();
-    }
-    LOCK_PROFILE_GOT;
-    preempt_disable();
+    _spin_lock(lock);
 }
 
 unsigned long _spin_lock_irqsave(spinlock_t *lock)
 {
     unsigned long flags;
-    LOCK_PROFILE_VAR;
 
     local_irq_save(flags);
-    check_lock(&lock->debug);
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
-    {
-        LOCK_PROFILE_BLOCK;
-        local_irq_restore(flags);
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
-            cpu_relax();
-        local_irq_disable();
-    }
-    LOCK_PROFILE_GOT;
-    preempt_disable();
+    _spin_lock(lock);
     return flags;
 }
 
@@ -194,35 +174,44 @@ void _spin_unlock(spinlock_t *lock)
 {
     preempt_enable();
     LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    lock->tickets.head++;
 }
 
 void _spin_unlock_irq(spinlock_t *lock)
 {
-    preempt_enable();
-    LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    _spin_unlock(lock);
     local_irq_enable();
 }
 
 void _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
 {
-    preempt_enable();
-    LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    _spin_unlock(lock);
     local_irq_restore(flags);
 }
 
+static int __spin_is_locked(spinlock_t *lock)
+{
+    return lock->tickets.head != lock->tickets.tail;
+}
+
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return _raw_spin_is_locked(&lock->raw);
+    return __spin_is_locked(lock);
 }
 
 int _spin_trylock(spinlock_t *lock)
 {
+    spinlock_tickets_t old, new;
+
     check_lock(&lock->debug);
-    if ( !_raw_spin_trylock(&lock->raw) )
+    old.head_tail = read_atomic(&lock->tickets.head_tail);
+    if ( old.head != old.tail )
+        return 0;
+    new = old;
+    new.tail++;
+    if ( cmpxchg(&lock->tickets.head_tail, old.head_tail, new.head_tail)
+         != old.head_tail )
         return 0;
 #ifdef LOCK_PROFILE
     if (lock->profile)
@@ -239,7 +228,7 @@ void _spin_barrier(spinlock_t *lock)
     u64      loop = 0;
 
     check_barrier(&lock->debug);
-    do { smp_mb(); loop++;} while ( _raw_spin_is_locked(&lock->raw) );
+    do { smp_mb(); loop++;} while ( __spin_is_locked(lock) );
     if ((loop > 1) && lock->profile)
     {
         lock->profile->time_block += NOW() - block;
@@ -247,14 +236,14 @@ void _spin_barrier(spinlock_t *lock)
     }
 #else
     check_barrier(&lock->debug);
-    do { smp_mb(); } while ( _raw_spin_is_locked(&lock->raw) );
+    do { smp_mb(); } while ( __spin_is_locked(lock) );
 #endif
     smp_mb();
 }
 
 int _spin_trylock_recursive(spinlock_t *lock)
 {
-    int cpu = smp_processor_id();
+    unsigned int cpu = smp_processor_id();
 
     /* Don't allow overflow of recurse_cpu field. */
     BUILD_BUG_ON(NR_CPUS > 0xfffu);
@@ -277,8 +266,17 @@ int _spin_trylock_recursive(spinlock_t *lock)
 
 void _spin_lock_recursive(spinlock_t *lock)
 {
-    while ( !spin_trylock_recursive(lock) )
-        cpu_relax();
+    unsigned int cpu = smp_processor_id();
+
+    if ( likely(lock->recurse_cpu != cpu) )
+    {
+        spin_lock(lock);
+        lock->recurse_cpu = cpu;
+    }
+
+    /* We support only fairly shallow recursion, else the counter overflows. */
+    ASSERT(lock->recurse_cnt < 0xfu);
+    lock->recurse_cnt++;
 }
 
 void _spin_unlock_recursive(spinlock_t *lock)
xen/include/xen/spinlock.h +11 -5
index eda9b2e..bafbc74 100644
@@ -80,8 +80,7 @@ struct lock_profile_qhead {
     static struct lock_profile *__lock_profile_##name                         \
     __used_section(".lockprofile.data") =                                     \
     &__lock_profile_data_##name
-#define _SPIN_LOCK_UNLOCKED(x) { _RAW_SPIN_LOCK_UNLOCKED, 0xfffu, 0,          \
-                                 _LOCK_DEBUG, x }
+#define _SPIN_LOCK_UNLOCKED(x) { { 0 }, 0xfffu, 0, _LOCK_DEBUG, x }
 #define SPIN_LOCK_UNLOCKED _SPIN_LOCK_UNLOCKED(NULL)
 #define DEFINE_SPINLOCK(l)                                                    \
     spinlock_t l = _SPIN_LOCK_UNLOCKED(NULL);                                 \
@@ -117,8 +116,7 @@ extern void spinlock_profile_reset(unsigned char key);
 
 struct lock_profile_qhead { };
 
-#define SPIN_LOCK_UNLOCKED                                                    \
-    { _RAW_SPIN_LOCK_UNLOCKED, 0xfffu, 0, _LOCK_DEBUG }
+#define SPIN_LOCK_UNLOCKED { { 0 }, 0xfffu, 0, _LOCK_DEBUG }
 #define DEFINE_SPINLOCK(l) spinlock_t l = SPIN_LOCK_UNLOCKED
 
 #define spin_lock_init_prof(s, l) spin_lock_init(&((s)->l))
@@ -127,8 +125,16 @@ struct lock_profile_qhead { };
 
 #endif
 
+typedef union {
+    u32 head_tail;
+    struct {
+        u16 head;
+        u16 tail;
+    };
+} spinlock_tickets_t;
+
 typedef struct spinlock {
-    raw_spinlock_t raw;
+    spinlock_tickets_t tickets;
     u16 recurse_cpu:12;
     u16 recurse_cnt:4;
     struct lock_debug debug;
Signature · 1 line
1.7.10.4
#32 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

Quoted 4 lines · David Vrabel

Replace the byte locks with ticket locks. Ticket locks are: a) fair; and b) peform better when contented since they spin without an atomic operation.

Quoted 8 lines

The lock is split into two ticket values: head and tail. A locker acquires a ticket by (atomically) increasing tail and using the previous tail value. A CPU holds the lock if its ticket == head. The lock is released by increasing head.

Architectures need only provide an xadd() implementation.

While I think this implementation is fine for x86, ARM almost certainly requires some smp barriers.

~Andrew

Quoted 214 lines
---
 xen/common/spinlock.c      |   80 +++++++++++++++++++++-----------------------
 xen/include/xen/spinlock.h |   16 ++++++---
 2 files changed, 50 insertions(+), 46 deletions(-)
xen/common/spinlock.c +39 -41
index 0c4289c..99163c5 100644
@@ -138,14 +138,17 @@ void spin_debug_disable(void)
 
 void _spin_lock(spinlock_t *lock)
 {
+    spinlock_tickets_t tickets = { .tail = 1, };
     LOCK_PROFILE_VAR;
 
     check_lock(&lock->debug);
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
+    tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
+    if ( tickets.tail != read_atomic(&lock->tickets.head) )
     {
         LOCK_PROFILE_BLOCK;
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
+        do {
             cpu_relax();
+        } while ( tickets.tail != read_atomic(&lock->tickets.head) );
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -153,40 +156,17 @@ void _spin_lock(spinlock_t *lock)
 
 void _spin_lock_irq(spinlock_t *lock)
 {
-    LOCK_PROFILE_VAR;
-
     ASSERT(local_irq_is_enabled());
     local_irq_disable();
-    check_lock(&lock->debug);
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
-    {
-        LOCK_PROFILE_BLOCK;
-        local_irq_enable();
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
-            cpu_relax();
-        local_irq_disable();
-    }
-    LOCK_PROFILE_GOT;
-    preempt_disable();
+    _spin_lock(lock);
 }
 
 unsigned long _spin_lock_irqsave(spinlock_t *lock)
 {
     unsigned long flags;
-    LOCK_PROFILE_VAR;
 
     local_irq_save(flags);
-    check_lock(&lock->debug);
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
-    {
-        LOCK_PROFILE_BLOCK;
-        local_irq_restore(flags);
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
-            cpu_relax();
-        local_irq_disable();
-    }
-    LOCK_PROFILE_GOT;
-    preempt_disable();
+    _spin_lock(lock);
     return flags;
 }
 
@@ -194,35 +174,44 @@ void _spin_unlock(spinlock_t *lock)
 {
     preempt_enable();
     LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    lock->tickets.head++;
 }
 
 void _spin_unlock_irq(spinlock_t *lock)
 {
-    preempt_enable();
-    LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    _spin_unlock(lock);
     local_irq_enable();
 }
 
 void _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
 {
-    preempt_enable();
-    LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    _spin_unlock(lock);
     local_irq_restore(flags);
 }
 
+static int __spin_is_locked(spinlock_t *lock)
+{
+    return lock->tickets.head != lock->tickets.tail;
+}
+
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return _raw_spin_is_locked(&lock->raw);
+    return __spin_is_locked(lock);
 }
 
 int _spin_trylock(spinlock_t *lock)
 {
+    spinlock_tickets_t old, new;
+
     check_lock(&lock->debug);
-    if ( !_raw_spin_trylock(&lock->raw) )
+    old.head_tail = read_atomic(&lock->tickets.head_tail);
+    if ( old.head != old.tail )
+        return 0;
+    new = old;
+    new.tail++;
+    if ( cmpxchg(&lock->tickets.head_tail, old.head_tail, new.head_tail)
+         != old.head_tail )
         return 0;
 #ifdef LOCK_PROFILE
     if (lock->profile)
@@ -239,7 +228,7 @@ void _spin_barrier(spinlock_t *lock)
     u64      loop = 0;
 
     check_barrier(&lock->debug);
-    do { smp_mb(); loop++;} while ( _raw_spin_is_locked(&lock->raw) );
+    do { smp_mb(); loop++;} while ( __spin_is_locked(lock) );
     if ((loop > 1) && lock->profile)
     {
         lock->profile->time_block += NOW() - block;
@@ -247,14 +236,14 @@ void _spin_barrier(spinlock_t *lock)
     }
 #else
     check_barrier(&lock->debug);
-    do { smp_mb(); } while ( _raw_spin_is_locked(&lock->raw) );
+    do { smp_mb(); } while ( __spin_is_locked(lock) );
 #endif
     smp_mb();
 }
 
 int _spin_trylock_recursive(spinlock_t *lock)
 {
-    int cpu = smp_processor_id();
+    unsigned int cpu = smp_processor_id();
 
     /* Don't allow overflow of recurse_cpu field. */
     BUILD_BUG_ON(NR_CPUS > 0xfffu);
@@ -277,8 +266,17 @@ int _spin_trylock_recursive(spinlock_t *lock)
 
 void _spin_lock_recursive(spinlock_t *lock)
 {
-    while ( !spin_trylock_recursive(lock) )
-        cpu_relax();
+    unsigned int cpu = smp_processor_id();
+
+    if ( likely(lock->recurse_cpu != cpu) )
+    {
+        spin_lock(lock);
+        lock->recurse_cpu = cpu;
+    }
+
+    /* We support only fairly shallow recursion, else the counter overflows. */
+    ASSERT(lock->recurse_cnt < 0xfu);
+    lock->recurse_cnt++;
 }
 
 void _spin_unlock_recursive(spinlock_t *lock)
xen/include/xen/spinlock.h +11 -5
index eda9b2e..bafbc74 100644
@@ -80,8 +80,7 @@ struct lock_profile_qhead {
     static struct lock_profile *__lock_profile_##name                         \
     __used_section(".lockprofile.data") =                                     \
     &__lock_profile_data_##name
-#define _SPIN_LOCK_UNLOCKED(x) { _RAW_SPIN_LOCK_UNLOCKED, 0xfffu, 0,          \
-                                 _LOCK_DEBUG, x }
+#define _SPIN_LOCK_UNLOCKED(x) { { 0 }, 0xfffu, 0, _LOCK_DEBUG, x }
 #define SPIN_LOCK_UNLOCKED _SPIN_LOCK_UNLOCKED(NULL)
 #define DEFINE_SPINLOCK(l)                                                    \
     spinlock_t l = _SPIN_LOCK_UNLOCKED(NULL);                                 \
@@ -117,8 +116,7 @@ extern void spinlock_profile_reset(unsigned char key);
 
 struct lock_profile_qhead { };
 
-#define SPIN_LOCK_UNLOCKED                                                    \
-    { _RAW_SPIN_LOCK_UNLOCKED, 0xfffu, 0, _LOCK_DEBUG }
+#define SPIN_LOCK_UNLOCKED { { 0 }, 0xfffu, 0, _LOCK_DEBUG }
 #define DEFINE_SPINLOCK(l) spinlock_t l = SPIN_LOCK_UNLOCKED
 
 #define spin_lock_init_prof(s, l) spin_lock_init(&((s)->l))
@@ -127,8 +125,16 @@ struct lock_profile_qhead { };
 
 #endif
 
+typedef union {
+    u32 head_tail;
+    struct {
+        u16 head;
+        u16 tail;
+    };
+} spinlock_tickets_t;
+
 typedef struct spinlock {
-    raw_spinlock_t raw;
+    spinlock_tickets_t tickets;
     u16 recurse_cpu:12;
     u16 recurse_cnt:4;
     struct lock_debug debug;
#33 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

In reply to David Vrabel
Quoted 8 lines · David Vrabel
xen/common/spinlock.c +1 -0
@@ -138,14 +138,17 @@ void spin_debug_disable(void)
 
  void _spin_lock(spinlock_t *lock)
  {
+    spinlock_tickets_t tickets = { .tail = 1, };
      LOCK_PROFILE_VAR;
 
      check_lock(&lock->debug);

xen/common/spinlock.c void spin_debug_disable(void) · line 152

Quoted 10 lines
xen/common/spinlock.c +4 -2
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
+    tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
+    if ( tickets.tail != read_atomic(&lock->tickets.head) )
      {
          LOCK_PROFILE_BLOCK;
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
+        do {
              cpu_relax();
+        } while ( tickets.tail != read_atomic(&lock->tickets.head) );
      }

Why do you use both 'if' and 'while"? I.e. why not just

while ( tickets.tail != read_atomic(&lock->tickets.head) )

{
	LOCK_PROFILE_BLOCK;
	cpu_relax();
}

xen/common/spinlock.c void _spin_unlock(spinlock_t *lock) · line 177

Quoted 7 lines
xen/common/spinlock.c +1 -1
@@ -194,35 +174,44 @@ void _spin_unlock(spinlock_t *lock)
  {
      preempt_enable();
      LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    lock->tickets.head++;

Is it safe to do a plain increment here and a locked one?

-boris

#34 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

Quoted 24 lines · Boris Ostrovsky
Quoted 8 lines · David Vrabel
xen/common/spinlock.c +1 -0
@@ -138,14 +138,17 @@ void spin_debug_disable(void)
 
  void _spin_lock(spinlock_t *lock)
  {
+    spinlock_tickets_t tickets = { .tail = 1, };
      LOCK_PROFILE_VAR;
 
      check_lock(&lock->debug);

xen/common/spinlock.c void spin_debug_disable(void) · line 152

Quoted 10 lines
xen/common/spinlock.c +4 -2
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
+    tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
+    if ( tickets.tail != read_atomic(&lock->tickets.head) )
      {
          LOCK_PROFILE_BLOCK;
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
+        do {
              cpu_relax();
+        } while ( tickets.tail != read_atomic(&lock->tickets.head) );
      }

Why do you use both 'if' and 'while"? I.e. why not just

Quoted 5 lines

while ( tickets.tail != read_atomic(&lock->tickets.head) )

{
    LOCK_PROFILE_BLOCK;
    cpu_relax();
}

We need to only call LOCK_PROFILE_BLOCK once when we start blocking.

Quoted 9 lines

xen/common/spinlock.c void _spin_unlock(spinlock_t *lock) · line 177

Quoted 6 lines
xen/common/spinlock.c +1 -1
@@ -194,35 +174,44 @@ void _spin_unlock(spinlock_t *lock)
  {
      preempt_enable();
      LOCK_PROFILE_REL;
-    _raw_spin_unlock(&lock->raw);
+    lock->tickets.head++;

Is it safe to do a plain increment here and a locked one?

Yes. Only the lock holder writes to head.

David

#35 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

Quoted 25 lines · David Vrabel
Quoted 23 lines · Boris Ostrovsky
Quoted 8 lines · David Vrabel
xen/common/spinlock.c +1 -0
@@ -138,14 +138,17 @@ void spin_debug_disable(void)
 
   void _spin_lock(spinlock_t *lock)
   {
+    spinlock_tickets_t tickets = { .tail = 1, };
       LOCK_PROFILE_VAR;
 
       check_lock(&lock->debug);

xen/common/spinlock.c void spin_debug_disable(void) · line 152

Quoted 10 lines
xen/common/spinlock.c +4 -2
-    while ( unlikely(!_raw_spin_trylock(&lock->raw)) )
+    tickets.head_tail = xadd(&lock->tickets, tickets.head_tail);
+    if ( tickets.tail != read_atomic(&lock->tickets.head) )
       {
           LOCK_PROFILE_BLOCK;
-        while ( likely(_raw_spin_is_locked(&lock->raw)) )
+        do {
               cpu_relax();
+        } while ( tickets.tail != read_atomic(&lock->tickets.head) );
       }

Why do you use both 'if' and 'while"? I.e. why not just

Quoted 6 lines
Quoted 5 lines

while ( tickets.tail != read_atomic(&lock->tickets.head) )

{
     LOCK_PROFILE_BLOCK;
     cpu_relax();
}

We need to only call LOCK_PROFILE_BLOCK once when we start blocking.

LOCK_PROFILE_BLOCK is a "one-shot" assignment. Once 'block' is set to NOW() it doesn't change anymore. So it will be updated only on the first iteration of the loop.

-boris

#36 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

In reply to David Vrabel
Quoted 10 lines · David Vrabel

Replace the byte locks with ticket locks. Ticket locks are: a) fair; and b) peform better when contented since they spin without an atomic operation.

The lock is split into two ticket values: head and tail. A locker acquires a ticket by (atomically) increasing tail and using the previous tail value. A CPU holds the lock if its ticket == head. The lock is released by increasing head.

Architectures need only provide an xadd() implementation.

I like this. Thanks for measuring the IRQ latency too -- that's the most nervous-making part of the overall change.

+1 to Andrew's comment about needing some more barriers on ARM. In particular read barriers during the spin and full acquire/release on lock/unlock.

Should there be a follow-up patch that removes all the arch-specific raw_spinlock_t stuff? Or is it still in use somewhere else?

xen/common/spinlock.c void _spin_unlock(spinlock_t *lock) · line 195

Quoted 4 lines
xen/common/spinlock.c +4 -0
+static int __spin_is_locked(spinlock_t *lock)
+{
+    return lock->tickets.head != lock->tickets.tail;
+}

Maybe atomic_read the lock and do the comparison on the local value? Just from an abundance of caution - can't think of an obvious reason why this race should matter.

Quoted 5 lines
xen/common/spinlock.c +1 -1
+
 int _spin_is_locked(spinlock_t *lock)
 {
     check_lock(&lock->debug);
-    return _raw_spin_is_locked(&lock->raw);

xen/common/spinlock.c void _spin_unlock(spinlock_t *lock) · line 208

Quoted 10 lines
xen/common/spinlock.c +4 -1
+    return __spin_is_locked(lock);
 }
 
 int _spin_trylock(spinlock_t *lock)
 {
+    spinlock_tickets_t old, new;
+
     check_lock(&lock->debug);
-    if ( !_raw_spin_trylock(&lock->raw) )
+    old.head_tail = read_atomic(&lock->tickets.head_tail);

This is common enough that we might want a helper like:
static inline spinlock_tickets_t observe_lock (spinlock_tickets_t *l)

{
    smp_rmb();	
    return (spinlock_tickets_t) { .head_tail = read_atomic(&l->tickets.head_tail) };
}

(or some similar formulation that's actually valid C).

Quoted 10 lines
xen/common/spinlock.c +1 -1
@@ -239,7 +228,7 @@ void _spin_barrier(spinlock_t *lock)
     u64      loop = 0;
 
     check_barrier(&lock->debug);
-    do { smp_mb(); loop++;} while ( _raw_spin_is_locked(&lock->raw) );
+    do { smp_mb(); loop++;} while ( __spin_is_locked(lock) );
     if ((loop > 1) && lock->profile)
     {
         lock->profile->time_block += NOW() - block;

xen/common/spinlock.c void _spin_barrier(spinlock_t *lock) · line 239

Quoted 5 lines
xen/common/spinlock.c +1 -1
@@ -247,14 +236,14 @@ void _spin_barrier(spinlock_t *lock)
     }
 #else
     check_barrier(&lock->debug);
-    do { smp_mb(); } while ( _raw_spin_is_locked(&lock->raw) );
+    do { smp_mb(); } while ( __spin_is_locked(lock) );

For a contended lock this is potentially a lot stronger than the old
barrier: this waits until the lock is held _and_ no other CPU is waiting
for it. I think what we need here is:
- sample := observe_lock()
- if sample.head == sample.tail, exit immediately
- else wait until observe_lock().head != sample.head

Cheers,

Tim.

#37 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

Quoted 3 lines · Tim Deegan

Should there be a follow-up patch that removes all the arch-specific raw_spinlock_t stuff? Or is it still in use somewhere else?

This is patch #6.

David

#38 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

Quoted 6 lines · David Vrabel
Quoted 3 lines · Tim Deegan

Should there be a follow-up patch that removes all the arch-specific raw_spinlock_t stuff? Or is it still in use somewhere else?

This is patch #6.

So it is. /me needs an eye test. Sorry for the noise.

Tim.

#39 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

In reply to Tim Deegan
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 16.04.15 at 14:03, <[email protected]> wrote:
Quoted 9 lines · David Vrabel

Replace the byte locks with ticket locks. Ticket locks are: a) fair; and b) peform better when contented since they spin without an atomic operation.

The lock is split into two ticket values: head and tail. A locker acquires a ticket by (atomically) increasing tail and using the previous tail value. A CPU holds the lock if its ticket == head. The lock is released by increasing head.

Quoted 4 lines
Quoted 1 line

Architectures need only provide an xadd() implementation.

I like this. Thanks for measuring the IRQ latency too -- that's the most nervous-making part of the overall change.

While in general I like ticket locks too, the IRQ latency issue still worries me, and the promised experimental evidence that this is not an issue won't be able to fully eliminate that reservation. As already said in response to the v1 posting, while this likely isn't a primary usage scenario, running Xen itself as a guest of another hypervisor will (by analogy with what we know about Linux) immediately be known to be hampered by this. This isn't to say that I'm opposed to this going in, all I've been trying to point out is that we're set for another full re-work of spin locks if such a use case ever became more important to any user of Xen.

Jan

#40 lore raw

Re: [PATCHv2 5/6] xen: use ticket locks for spin locks

In reply to Tim Deegan
Quoted 9 lines · Tim Deegan

xen/common/spinlock.c void _spin_unlock(spinlock_t *lock) · line 195

Quoted 5 lines
xen/common/spinlock.c +4 -0
+static int __spin_is_locked(spinlock_t *lock)
+{
+    return lock->tickets.head != lock->tickets.tail;
+}

Maybe atomic_read the lock and do the comparison on the local value? Just from an abundance of caution - can't think of an obvious reason why this race should matter.

I think it would be preferable to avoid giving the impression that avoiding this race is important so I've left this as is (FWIW, the Linux implementation is like this).

David

#41 lore raw

PATCH v2 6/6 x86, arm: remove asm/spinlock.h from all architectures

In reply to David Vrabel

Now that all architecture use a common ticket lock implementation for spinlocks, remove the architecture specific byte lock implementations.

---
 xen/arch/arm/README.LinuxPrimitives  |   28 ---------------
 xen/include/asm-arm/arm32/spinlock.h |   66 ----------------------------------
 xen/include/asm-arm/arm64/spinlock.h |   63 --------------------------------
 xen/include/asm-arm/spinlock.h       |   23 ------------
 xen/include/asm-x86/spinlock.h       |   37 -------------------
 xen/include/xen/spinlock.h           |    1 -
 6 files changed, 218 deletions(-)
 delete mode 100644 xen/include/asm-arm/arm32/spinlock.h
 delete mode 100644 xen/include/asm-arm/arm64/spinlock.h
 delete mode 100644 xen/include/asm-arm/spinlock.h
 delete mode 100644 xen/include/asm-x86/spinlock.h
xen/arch/arm/README.LinuxPrimitives +0 -28
index 7f33fc7..3115f51 100644
@@ -25,16 +25,6 @@ linux/arch/arm64/include/asm/atomic.h   xen/include/asm-arm/arm64/atomic.h
 
 ---------------------------------------------------------------------
 
-spinlocks: last sync @ v3.16-rc6 (last commit: 95c4189689f9)
-
-linux/arch/arm64/include/asm/spinlock.h xen/include/asm-arm/arm64/spinlock.h
-
-Skipped:
-  5686b06 arm64: lockref: add support for lockless lockrefs using cmpxchg
-  52ea2a5 arm64: locks: introduce ticket-based spinlock implementation
-
----------------------------------------------------------------------
-
 mem*: last sync @ v3.16-rc6 (last commit: d875c9b37240)
 
 linux/arch/arm64/lib/memchr.S           xen/arch/arm/arm64/lib/memchr.S
@@ -103,24 +93,6 @@ linux/arch/arm/include/asm/atomic.h     xen/include/asm-arm/arm32/atomic.h
 
 ---------------------------------------------------------------------
 
-spinlocks: last sync: 15e7e5c1ebf5
-
-linux/arch/arm/include/asm/spinlock.h   xen/include/asm-arm/arm32/spinlock.h
-
-*** Linux has switched to ticket locks but we still use bitlocks.
-
-resync to v3.14-rc7:
-
-  7c8746a ARM: 7955/1: spinlock: ensure we have a compiler barrier before sev
-  0cbad9c ARM: 7854/1: lockref: add support for lockless lockrefs using cmpxchg64
-  9bb17be ARM: locks: prefetch the destination word for write prior to strex
-  27a8479 ARM: smp_on_up: move inline asm ALT_SMP patching macro out of spinlock.
-  00efaa0 ARM: 7812/1: rwlocks: retry trylock operation if strex fails on free lo
-  afa31d8 ARM: 7811/1: locks: use early clobber in arch_spin_trylock
-  73a6fdc ARM: spinlock: use inner-shareable dsb variant prior to sev instruction
-
----------------------------------------------------------------------
-
 mem*: last sync @ v3.16-rc6 (last commit: d98b90ea22b0)
 
 linux/arch/arm/lib/copy_template.S      xen/arch/arm/arm32/lib/copy_template.S
xen/include/asm-arm/arm32/spinlock.h deleted +0 -66

66 lines not on this page · raw message

xen/include/asm-arm/arm64/spinlock.h deleted +0 -63

63 lines not on this page · raw message

xen/include/asm-arm/spinlock.h deleted +0 -23
deleted file mode 100644
index a064f73..0000000
@@ -1,23 +0,0 @@
-#ifndef __ASM_SPINLOCK_H
-#define __ASM_SPINLOCK_H
-
-#include <xen/config.h>
-#include <xen/lib.h>
-
-#if defined(CONFIG_ARM_32)
-# include <asm/arm32/spinlock.h>
-#elif defined(CONFIG_ARM_64)
-# include <asm/arm64/spinlock.h>
-#else
-# error "unknown ARM variant"
-#endif
-
-#endif /* __ASM_SPINLOCK_H */
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- */
xen/include/asm-x86/spinlock.h deleted +0 -37
deleted file mode 100644
index 757e20b..0000000
@@ -1,37 +0,0 @@
-#ifndef __ASM_SPINLOCK_H
-#define __ASM_SPINLOCK_H
-
-#include <xen/config.h>
-#include <xen/lib.h>
-#include <asm/atomic.h>
-
-typedef struct {
-    volatile s16 lock;
-} raw_spinlock_t;
-
-#define _RAW_SPIN_LOCK_UNLOCKED /*(raw_spinlock_t)*/ { 1 }
-
-#define _raw_spin_is_locked(x) ((x)->lock <= 0)
-
-static always_inline void _raw_spin_unlock(raw_spinlock_t *lock)
-{
-    ASSERT(_raw_spin_is_locked(lock));
-    asm volatile (
-        "movw $1,%0" 
-        : "=m" (lock->lock) : : "memory" );
-}
-
-static always_inline int _raw_spin_trylock(raw_spinlock_t *lock)
-{
-    s16 oldval;
-    asm volatile (
-        "xchgw %w0,%1"
-        :"=r" (oldval), "=m" (lock->lock)
-        :"0" ((s16)0) : "memory" );
-    return (oldval > 0);
-}
-
-#define _raw_read_unlock(l) \
-    asm volatile ( "lock; dec%z0 %0" : "+m" ((l)->lock) :: "memory" )
-
-#endif /* __ASM_SPINLOCK_H */
xen/include/xen/spinlock.h +0 -1
index bafbc74..311685a 100644
@@ -2,7 +2,6 @@
 #define __SPINLOCK_H__
 
 #include <asm/system.h>
-#include <asm/spinlock.h>
 
 #ifndef NDEBUG
 struct lock_debug {
Signature · 1 line
1.7.10.4
Acked-by on v2 6/6
#43 lore raw

Re: [PATCHv2 6/6] x86, arm: remove asm/spinlock.h from all architectures

In reply to David Vrabel
Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 10.04.15 at 16:19, <[email protected]> wrote:

Now that all architecture use a common ticket lock implementation for spinlocks, remove the architecture specific byte lock implementations.

Quoted 8 lines
---
 xen/arch/arm/README.LinuxPrimitives  |   28 ---------------
 xen/include/asm-arm/arm32/spinlock.h |   66 ----------------------------------
 xen/include/asm-arm/arm64/spinlock.h |   63 --------------------------------
 xen/include/asm-arm/spinlock.h       |   23 ------------
 xen/include/asm-x86/spinlock.h       |   37 -------------------
 xen/include/xen/spinlock.h           |    1 -
 6 files changed, 218 deletions(-)

For the non-ARM bits: