Skip to contentSkip to conversation

arm: reduce power use by contented spin locks with WFE/SEV

PATCH In review David Vrabel 5 participants 7 messages Jul 31, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: reduce power use by contented spin locks with WFE/SEV

Instead of cpu_relax() while spinning and observing the ticket head, introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +3 -2
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();
 }
 
 void _spin_unlock_irq(spinlock_t *lock)
@@ -228,7 +229,7 @@ void _spin_barrier(spinlock_t *lock)
     if ( sample.head != sample.tail )
     {
         while ( observe_head(&lock->tickets) == sample.head )
-            cpu_relax();
+            spin_relax();
 #ifdef LOCK_PROFILE
         if ( lock->profile )
         {
xen/include/asm-arm/spinlock.h +2 -1
index 81955d1..311764b 100644
@@ -1,6 +1,7 @@
 #ifndef __ASM_SPINLOCK_H
 #define __ASM_SPINLOCK_H
 
-/* Nothing ARM specific. */
+#define spin_relax() asm volatile("wfe" ::: "memory");
+#define spin_signal() asm volatile("sev" ::: "memory");
 
 #endif /* __ASM_SPINLOCK_H */
xen/include/asm-x86/spinlock.h +3 -0
index 7d69e75..0c04a4e 100644
@@ -4,4 +4,7 @@
 #define _raw_read_unlock(l) \
     asm volatile ( "lock; dec%z0 %0" : "+m" ((l)->lock) :: "memory" )
 
+#define spin_relax() cpu_relax()
+#define spin_signal()
+
 #endif /* __ASM_SPINLOCK_H */
Signature · 1 line
2.1.4
#1 lore raw
Quoted 9 lines · David Vrabel

Instead of cpu_relax() while spinning and observing the ticket head, introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

x86 bits Reviewed-by: Andrew Cooper <[email protected]>

Quoted 63 lines

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +3 -2
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();
 }
 
 void _spin_unlock_irq(spinlock_t *lock)
@@ -228,7 +229,7 @@ void _spin_barrier(spinlock_t *lock)
     if ( sample.head != sample.tail )
     {
         while ( observe_head(&lock->tickets) == sample.head )
-            cpu_relax();
+            spin_relax();
 #ifdef LOCK_PROFILE
         if ( lock->profile )
         {
xen/include/asm-arm/spinlock.h +2 -1
index 81955d1..311764b 100644
@@ -1,6 +1,7 @@
 #ifndef __ASM_SPINLOCK_H
 #define __ASM_SPINLOCK_H
 
-/* Nothing ARM specific. */
+#define spin_relax() asm volatile("wfe" ::: "memory");
+#define spin_signal() asm volatile("sev" ::: "memory");
 
 #endif /* __ASM_SPINLOCK_H */
xen/include/asm-x86/spinlock.h +3 -0
index 7d69e75..0c04a4e 100644
@@ -4,4 +4,7 @@
 #define _raw_read_unlock(l) \
     asm volatile ( "lock; dec%z0 %0" : "+m" ((l)->lock) :: "memory" )
 
+#define spin_relax() cpu_relax()
+#define spin_signal()
+
 #endif /* __ASM_SPINLOCK_H */
#2 lore raw
In reply to David Vrabel
Quoted 31 lines · David Vrabel

Instead of cpu_relax() while spinning and observing the ticket head, introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +1 -1
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();

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

Quoted 4 lines
xen/common/spinlock.c +1 -0
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();

It occurs to me that perhaps there should be a barrier between the add_sized() and the spin_signal() so the update value is visible before we signal (otherwise the spinner may be woken and observe the old value and WFE again).

spin_relax() and spin_signal() might be better named arch_lock_relax() and arch_lock_signal() to match the naming of existing arch_lock_*() hooks.

I think someone with more arm experience (and the means to test) should take this patch on.

David

#3 lore raw

Hi David,

Quoted 33 lines · David Vrabel
Quoted 32 lines · David Vrabel

Instead of cpu_relax() while spinning and observing the ticket head, introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +1 -1
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
Quoted 8 lines

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

Quoted 3 lines
xen/common/spinlock.c +1 -0
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();

It occurs to me that perhaps there should be a barrier between the add_sized() and the spin_signal() so the update value is visible before we signal (otherwise the spinner may be woken and observe the old value and WFE again).

sev is usually precede by dsb to ensure that all the instructions before as completed before executing the sev.

Quoted 5 lines

spin_relax() and spin_signal() might be better named arch_lock_relax() and arch_lock_signal() to match the naming of existing arch_lock_*() hooks.

I think someone with more arm experience (and the means to test) should take this patch on.

I will give a look next week.

Regards,

Signature · 1 line
Julien Grall
#4 lore raw
Quoted 45 lines · Julien Grall

Hi David,

Quoted 33 lines · David Vrabel
Quoted 32 lines · David Vrabel

Instead of cpu_relax() while spinning and observing the ticket head, introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +1 -1
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
Quoted 8 lines

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

Quoted 3 lines
xen/common/spinlock.c +1 -0
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();

It occurs to me that perhaps there should be a barrier between the add_sized() and the spin_signal() so the update value is visible before we signal (otherwise the spinner may be woken and observe the old value and WFE again).

Quoted 2 lines

sev is usually precede by dsb to ensure that all the instructions before as completed before executing the sev.

Yes, a dsb() is required. This being common code, we could use wmb().

#5 lore raw
Quoted 49 lines · Stefano Stabellini
Quoted 48 lines · Julien Grall

Hi David,

Quoted 44 lines · David Vrabel
Quoted 32 lines · David Vrabel

Instead of cpu_relax() while spinning and observing the ticket head,
introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +1 -1
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();

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

Quoted 4 lines
xen/common/spinlock.c +1 -0
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();

It occurs to me that perhaps there should be a barrier between the add_sized() and the spin_signal() so the update value is visible before
we signal (otherwise the spinner may be woken and observe the old value
and WFE again).

Quoted 5 lines
Quoted 3 lines

sev is usually precede by dsb to ensure that all the instructions before
as completed before executing the sev.

Yes, a dsb() is required. This being common code, we could use wmb().

IMHO it should be in spin_{relax,signal} if it is needed.

Ian.

#6 lore raw
In reply to Stefano Stabellini
Quoted 46 lines · Stefano Stabellini
Quoted 45 lines · Julien Grall

Hi David,

Quoted 41 lines · David Vrabel
Quoted 31 lines · David Vrabel

Instead of cpu_relax() while spinning and observing the ticket head, introduce spin_relax() which executes a WFE instruction. After the ticket head is changed call spin_signal() to execute an SVE instruction to wake any spinners.

This should improve power consumption when locks are contented and spinning.

---
I've not tested this but it looks straight-forward...

---
 xen/common/spinlock.c          | 5 +++--
 xen/include/asm-arm/spinlock.h | 3 ++-
 xen/include/asm-x86/spinlock.h | 3 +++
 3 files changed, 8 insertions(+), 3 deletions(-)
xen/common/spinlock.c +1 -1
index 29149d1..fc3e8e7 100644
@@ -141,7 +141,7 @@ void _spin_lock(spinlock_t *lock)
     while ( tickets.tail != observe_head(&lock->tickets) )
     {
         LOCK_PROFILE_BLOCK;
-        cpu_relax();
+        spin_relax();
     }
     LOCK_PROFILE_GOT;
     preempt_disable();

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

Quoted 4 lines
xen/common/spinlock.c +1 -0
@@ -170,6 +170,7 @@ void _spin_unlock(spinlock_t *lock)
     preempt_enable();
     LOCK_PROFILE_REL;
     add_sized(&lock->tickets.head, 1);
+    spin_signal();

It occurs to me that perhaps there should be a barrier between the add_sized() and the spin_signal() so the update value is visible before we signal (otherwise the spinner may be woken and observe the old value and WFE again).

Quoted 4 lines
Quoted 2 lines

sev is usually precede by dsb to ensure that all the instructions before as completed before executing the sev.

Yes, a dsb() is required. This being common code, we could use wmb().

You should put the barrier required for the SEV in spin_signal() so an additional barrier is not required on other archs.

David