arm: reduce power use by contented spin locks with WFE/SEV
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
@@ -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
@@ -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
@@ -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
Quoted 9 lines · David Vrabel
Instead of
cpu_relax()while spinning and observing the ticket head, introducespin_relax()which executes a WFE instruction. After the ticket head is changed callspin_signal()to execute an SVE instruction to wake any spinners.This should improve power consumption when locks are contented and spinning.
Signed-off-by David Vrabel <[email protected]>
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@@ -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@@ -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@@ -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 */
Quoted 31 lines · David Vrabel
Instead of
cpu_relax()while spinning and observing the ticket head, introducespin_relax()which executes a WFE instruction. After the ticket head is changed callspin_signal()to execute an SVE instruction to wake any spinners.This should improve power consumption when locks are contented and spinning.
Signed-off-by David Vrabel <[email protected]>---
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@@ -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
Hi David,
Quoted 33 lines · David Vrabel
Quoted 32 lines · David Vrabel
Instead of
cpu_relax()while spinning and observing the ticket head, introducespin_relax()which executes a WFE instruction. After the ticket head is changed callspin_signal()to execute an SVE instruction to wake any spinners.This should improve power consumption when locks are contented and spinning.
Signed-off-by David Vrabel <[email protected]>---
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@@ -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
-0LOCK_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 thespin_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()andspin_signal()might be better namedarch_lock_relax()andarch_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
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, introducespin_relax()which executes a WFE instruction. After the ticket head is changed callspin_signal()to execute an SVE instruction to wake any spinners.This should improve power consumption when locks are contented and spinning.
Signed-off-by David Vrabel <[email protected]>---
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@@ -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
-0LOCK_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 thespin_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().
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,
introducespin_relax()which executes a WFE instruction. After the ticket head is changed callspin_signal()to execute an SVE instruction to wake any spinners.This should improve power consumption when locks are contented and spinning.
Signed-off-by David Vrabel <[email protected]>---
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@@ -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 thespin_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 usewmb().
IMHO it should be in spin_{relax,signal} if it is needed.
Ian.
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, introducespin_relax()which executes a WFE instruction. After the ticket head is changed callspin_signal()to execute an SVE instruction to wake any spinners.This should improve power consumption when locks are contented and spinning.
Signed-off-by David Vrabel <[email protected]>---
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@@ -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 thespin_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 usewmb().
You should put the barrier required for the SEV in spin_signal() so an additional barrier is not required on other archs.
David