locking/qspinlock: Allow lock to store lock holder cpu number
Subsystems (3)
Waiman Long
reviewercover locking/qspinlock: Allow lock to store lock holder cpu number
This patchset modifies the qspinlock code to allow it to store the lock holder cpu number in the lock itself if feasible for easier debugging and crash dump analysis. This lock holder cpu information may also be useful to architectures like PowerPC that needs the lock holder cpu number for better paravirtual spinlock performance.
A new config option QUEUED_SPINLOCKS_CPUINFO is added. If this config option is set, lock holder cpu number will always be stored if the number is small enough. Without this option, lock holder cpu number will only be stored in the slowpath of the native qspinlock.
Waiman Long (2):
locking/qspinlock: Store lock holder cpu in lock if feasible locking/pvqspinlock: Optionally store lock holder cpu into lock
arch/Kconfig | 12 ++++++ arch/x86/include/asm/qspinlock_paravirt.h | 9 ++-- include/asm-generic/qspinlock.h | 13 ++++-- include/asm-generic/qspinlock_types.h | 5 +++ kernel/locking/qspinlock.c | 50 +++++++++++++++-------- kernel/locking/qspinlock_paravirt.h | 41 ++++++++++--------- 6 files changed, 87 insertions(+), 43 deletions(-)
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 1/2 locking/qspinlock: Store lock holder cpu in lock if feasible
When doing crash dump analysis with lock, it is cumbersome to find out who the lock holder is. One have to look at the call traces of all the cpus to figure that out. It will be helpful if the lock itself can provide some clue about the lock holder cpu.
The locking state is determined by whether the locked byte is set or not (1 or 0). There is another special value for PV qspinlock (3). The whole byte is used because of performance reason. So there is space to store additional information such as the lock holder cpu as long as the cpu number is small enough to fit into a little less than a byte which is true for most typical 1 or 2-socket systems.
There may be a slight performance overhead in encoding the lock holder cpu number especially in the fastpath. Doing it in the slowpath, however, should be essentially free.
This patch modifies the slowpath code to add the encoded cpu number (cpu_nr + 2) to the locked byte as long as it is less than 253. A locked byte value of 1 means the lock holder cpu is not known. It is set in the fast path or when the cpu number is just too large. The special locked byte value of 255 is reserved for PV qspinlock.
--- include/asm-generic/qspinlock_types.h | 5 +++ kernel/locking/qspinlock.c | 47 +++++++++++++++++---------- 2 files changed, 35 insertions(+), 17 deletions(-)
include/asm-generic/qspinlock_types.h +5 -0
@@ -71,6 +71,11 @@ typedef struct qspinlock {* 8: pending * 9-10: tail index * 11-31: tail cpu (+1) +* +* Lock acquired via the fastpath will have a locked byte value of 1. Lock +* acquired via the slowpath may have a locked byte value of (cpu_nr + 2) +* if cpu_nr < 253. For cpu number beyond that range, a value of 1 will +* always be used. */ #define _Q_SET_MASK (type) (((1U << _Q_ ## type ## _BITS) - 1)\ << _Q_ ## type ## _OFFSET)
kernel/locking/qspinlock.c +30 -17
112 lines not on this page · raw message
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 2/2 locking/pvqspinlock: Optionally store lock holder cpu into lock
In reply to Waiman LongThe previous patch enables native qspinlock to store lock holder cpu number into the lock word when the lock is acquired via the slowpath. Since PV qspinlock uses atomic unlock, allowing the fastpath and slowpath to put different values into the lock word will further slow down the performance. This is certainly undesirable.
The only way we can do that without too much performance impact is to make fastpath and slowpath put in the same value. Still there is a slight performance overhead in the additional access to a percpu variable in the fastpath as well as the less optimized x86-64 PV qspinlock unlock path.
A new config option QUEUED_SPINLOCKS_CPUINFO is now added to enable distros to decide if they want to enable lock holder cpu information in the lock itself for both native and PV qspinlocks across both fastpath and slowpath. If this option is not configureed, only native qspinlocks in the slowpath will put the lock holder cpu information in the lock word.
--- arch/Kconfig | 12 +++++++ arch/x86/include/asm/qspinlock_paravirt.h | 9 +++-- include/asm-generic/qspinlock.h | 13 +++++-- kernel/locking/qspinlock.c | 5 ++- kernel/locking/qspinlock_paravirt.h | 41 ++++++++++++----------- 5 files changed, 53 insertions(+), 27 deletions(-)
arch/Kconfig +12 -0
@@ -954,6 +954,18 @@ config LOCK_EVENT_COUNTS the chance of application behavior change because of timing differences. The counts are reported via debugfs. +config QUEUED_SPINLOCKS_CPUINFO + bool "Store lock holder cpu information into queued spinlocks" + depends on QUEUED_SPINLOCKS + help + Enable queued spinlocks to encode (by adding 2) the lock holder + cpu number into the least significant 8 bits of the 32-bit + lock word as long as the cpu number is not larger than 252. + A value of 1 will be stored for larger cpu numbers. Having the + lock holder cpu information in the lock helps debugging and + crash dump analysis. There might be a very slight performance + overhead in enabling this option. + # Select if the architecture has support for applying RELR relocations. config ARCH_HAS_RELR bool
arch/x86/include/asm/qspinlock_paravirt.h +6 -3
@@ -7,8 +7,11 @@* registers. For i386, however, only 1 32-bit register needs to be saved * and restored. So an optimized version of __pv_queued_spin_unlock() is * hand-coded for 64-bit, but it isn't worthwhile to do it for 32-bit. +* +* Accessing percpu variable when QUEUED_SPINLOCKS_CPUINFO is defined is +* tricky. So disable this optimization for now. */ -#ifdef CONFIG_64BIT +#if defined(CONFIG_64BIT ) && !defined(CONFIG_QUEUED_SPINLOCKS_CPUINFO )PV_CALLEE_SAVE_REGS_THUNK (__pv_queued_spin_unlock_slowpath);#define __pv_queued_spin_unlock __pv_queued_spin_unlock
@@ -26,13 +29,13 @@ PV_CALLEE_SAVE_REGS_THUNK(__pv_queued_spin_unlock_slowpath);* * if (likely(lockval == _Q_LOCKED_VAL)) * return; -* pv_queued_spin_unlock_slowpath(lock, lockval); +* __pv_queued_spin_unlock_slowpath(lock, lockval); * } * * For x86-64, * rdi = lock (first argument) * rsi = lockval (second argument) -* rdx = internal variable (set to 0) +* rdx = internal variable for cmpxchg */ asm (".pushsection .text;" ".globl " PV_UNLOCK ";"
include/asm-generic/qspinlock.h +10 -3
@@ -12,6 +12,13 @@#include <asm-generic/qspinlock_types.h> +#ifdef CONFIG_QUEUED_SPINLOCKS_CPUINFO +DECLARE_PER_CPU_READ_MOSTLY (u8, pcpu_lockval); +#define QUEUED_LOCKED_VAL this_cpu_read(pcpu_lockval) +#else +#define QUEUED_LOCKED_VAL _Q_LOCKED_VAL +#endif +/** * queued_spin_is_locked - is the spinlock locked? * @lock: Pointer to queued spinlock structure
@@ -20,7 +27,7 @@ static __always_inline int queued_spin_is_locked(struct qspinlock *lock) {/* -* Any !0 state indicates it is locked, even if _Q_LOCKED_VAL +* Any !0 state indicates it is locked, even if QUEUED_LOCKED_VAL * isn't immediately observable. */ return atomic_read (&lock->val);
@@ -62,7 +69,7 @@ static __always_inline int queued_spin_trylock(struct qspinlock *lock)if (unlikely (val))return 0 ; -return likely (atomic_try_cmpxchg_acquire (&lock->val, &val, _Q_LOCKED_VAL)); +return likely (atomic_try_cmpxchg_acquire (&lock->val, &val,QUEUED_LOCKED_VAL )); }extern void queued_spin_lock_slowpath (struct qspinlock *lock,u32 val);
@@ -75,7 +82,7 @@ static __always_inline void queued_spin_lock(struct qspinlock *lock) {u32 val =0 ; -if (likely (atomic_try_cmpxchg_acquire (&lock->val, &val, _Q_LOCKED_VAL))) +if (likely (atomic_try_cmpxchg_acquire (&lock->val, &val,QUEUED_LOCKED_VAL )))return ;queued_spin_lock_slowpath (lock, val);
kernel/locking/qspinlock.c +4 -1
@@ -106,7 +106,10 @@ struct qnode {* PV doubles the storage and uses the second cacheline for PV state. */ static DEFINE_PER_CPU_ALIGNED(struct qnode, qnodes[MAX_NODES]); -static DEFINE_PER_CPU_READ_MOSTLY(u8, pcpu_lockval) = _Q_LOCKED_VAL; +DEFINE_PER_CPU_READ_MOSTLY (u8 ,pcpu_lockval ) = _Q_LOCKED_VAL; +#ifdef CONFIG_QUEUED_SPINLOCKS_CPUINFO +EXPORT_PER_CPU_SYMBOL (pcpu_lockval); +#endif /* * We must be able to distinguish between no-tail and the tail at 0:0,
kernel/locking/qspinlock_paravirt.h +21 -20
135 lines not on this page · raw message
Signature · 1 line
2.18.1
Peter Zijlstra
maintainerRe: [PATCH 1/2] locking/qspinlock: Store lock holder cpu in lock if feasible
In reply to Waiman LongQuoted 12 lines · Waiman Long
kernel/locking/qspinlock.c +5
-0+static DEFINE_PER_CPU_READ_MOSTLY(u8, pcpu_lockval) = _Q_LOCKED_VAL;/* * We must be able to distinguish between no-tail and the tail at 0:0, @@ -138,6 +139,19 @@ struct mcs_spinlock *grab_mcs_node(struct mcs_spinlock *base, int idx)#define _Q_LOCKED_PENDING_MASK (_Q_LOCKED_MASK | _Q_PENDING_MASK) +static __init int __init_pcpu_lockval (void ) +{ +int cpu; +
kernel/locking/qspinlock.c struct mcs_spinlock *grab_mcs_node(struct mcs_spinlock *base, int idx) · line 155
Quoted 10 lines
kernel/locking/qspinlock.c +9
-0+for_each_possible_cpu (cpu) { +u8 lockval = (cpu +2 < _Q_LOCKED_MASK -1 ) ? cpu +2 + : _Q_LOCKED_VAL; +per_cpu (pcpu_lockval, cpu) = lockval; + } +return 0 ; +} +early_initcall (__init_pcpu_lockval); +u8 lockval =this_cpu_read (pcpu_lockval);
Urgh... so you'd rather read a guaranteed cold line than to use smp_processor_id(), which we already use anyway?
I'm skeptical this helps anything, and it certainly makes the code more horrible :-(
Peter Zijlstra
maintainerRe: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
In reply to Waiman LongQuoted 11 lines · Waiman Long
The previous patch enables native qspinlock to store lock holder cpu number into the lock word when the lock is acquired via the slowpath. Since PV qspinlock uses atomic unlock, allowing the fastpath and slowpath to put different values into the lock word will further slow down the performance. This is certainly undesirable.
The only way we can do that without too much performance impact is to make fastpath and slowpath put in the same value. Still there is a slight performance overhead in the additional access to a percpu variable in the fastpath as well as the less optimized x86-64 PV qspinlock unlock path.
Quoted 6 lines
A new config option
QUEUED_SPINLOCKS_CPUINFOis now added to enable distros to decide if they want to enable lock holder cpu information in the lock itself for both native and PV qspinlocks across both fastpath and slowpath. If this option is not configureed, only native qspinlocks in the slowpath will put the lock holder cpu information in the lock word.
And this kills it,.. if it doesn't make unconditional sense, we're not going to do this. It's just too ugly.
Waiman Long
reviewerRe: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Waiman Long
The previous patch enables native qspinlock to store lock holder cpu number into the lock word when the lock is acquired via the slowpath. Since PV qspinlock uses atomic unlock, allowing the fastpath and slowpath to put different values into the lock word will further slow down the performance. This is certainly undesirable.
The only way we can do that without too much performance impact is to make fastpath and slowpath put in the same value. Still there is a slight performance overhead in the additional access to a percpu variable in the fastpath as well as the less optimized x86-64 PV qspinlock unlock path.
Quoted 9 lines
Quoted 6 lines
A new config option
QUEUED_SPINLOCKS_CPUINFOis now added to enable distros to decide if they want to enable lock holder cpu information in the lock itself for both native and PV qspinlocks across both fastpath and slowpath. If this option is not configureed, only native qspinlocks in the slowpath will put the lock holder cpu information in the lock word.And this kills it,.. if it doesn't make unconditional sense, we're not going to do this. It's just too ugly.
You mean it has to be unconditional, no option config if we want to do it. Right?
It can certainly be made unconditional after I figure out how to make the optimized PV unlock code work.
Cheers,
Longman
Re: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
Excerpts from Waiman Long's message of July 13, 2020 9:05 am:
Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Waiman Long
Quoted 17 lines
The previous patch enables native qspinlock to store lock holder cpu number into the lock word when the lock is acquired via the slowpath. Since PV qspinlock uses atomic unlock, allowing the fastpath and slowpath to put different values into the lock word will further slow down the performance. This is certainly undesirable.
The only way we can do that without too much performance impact is to make fastpath and slowpath put in the same value. Still there is a slight performance overhead in the additional access to a percpu variable in the fastpath as well as the less optimized x86-64 PV qspinlock unlock path.
A new config option
QUEUED_SPINLOCKS_CPUINFOis now added to enable distros to decide if they want to enable lock holder cpu information in the lock itself for both native and PV qspinlocks across both fastpath and slowpath. If this option is not configureed, only native qspinlocks in the slowpath will put the lock holder cpu information in the lock word.
Quoted 8 lines
Quoted 3 lines
And this kills it,.. if it doesn't make unconditional sense, we're not going to do this. It's just too ugly.
You mean it has to be unconditional, no option config if we want to do it. Right?
It can certainly be made unconditional after I figure out how to make the optimized PV unlock code work.
Sorry I've not had a lot of time to get back to this thread and test things -- don't spend loads of effort or complexity on it until we get some more numbers. I did see some worse throughput results (with no attention to fairness) with the PV spin lock, but it was a really quick limited few tests, I need to get something a bit more substantial.
I do very much appreciate your help with the powerpc patches, and interest in the PV issues though. I'll try to find more time to help out.
Thanks,
Nick
Peter Zijlstra
maintainerRe: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
In reply to Waiman LongQuoted 7 lines · Waiman Long
Quoted 3 lines · Peter Zijlstra
And this kills it,.. if it doesn't make unconditional sense, we're not going to do this. It's just too ugly.
You mean it has to be unconditional, no option config if we want to do it. Right?
Yeah, the very last thing we need in this code is spurious complexity.
Waiman Long
reviewerRe: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
In reply to Nicholas PigginQuoted 34 lines · Nicholas Piggin
Excerpts from Waiman Long's message of July 13, 2020 9:05 am:
Quoted 19 lines · Peter Zijlstra
Quoted 18 lines · Waiman Long
Quoted 17 lines
The previous patch enables native qspinlock to store lock holder cpu number into the lock word when the lock is acquired via the slowpath. Since PV qspinlock uses atomic unlock, allowing the fastpath and slowpath to put different values into the lock word will further slow down the performance. This is certainly undesirable.
The only way we can do that without too much performance impact is to make fastpath and slowpath put in the same value. Still there is a slight performance overhead in the additional access to a percpu variable in the fastpath as well as the less optimized x86-64 PV qspinlock unlock path.
A new config option
QUEUED_SPINLOCKS_CPUINFOis now added to enable distros to decide if they want to enable lock holder cpu information in the lock itself for both native and PV qspinlocks across both fastpath and slowpath. If this option is not configureed, only native qspinlocks in the slowpath will put the lock holder cpu information in the lock word.Quoted 8 lines
Quoted 3 lines
And this kills it,.. if it doesn't make unconditional sense, we're not going to do this. It's just too ugly.
You mean it has to be unconditional, no option config if we want to do it. Right?
It can certainly be made unconditional after I figure out how to make the optimized PV unlock code work.
Sorry I've not had a lot of time to get back to this thread and test things -- don't spend loads of effort or complexity on it until we get some more numbers. I did see some worse throughput results (with no attention to fairness) with the PV spin lock, but it was a really quick limited few tests, I need to get something a bit more substantial.
Quoted 3 lines
I do very much appreciate your help with the powerpc patches, and interest in the PV issues though. I'll try to find more time to help out.
Native qspinlock is usually not a problem performance-wise. PV qspinlock, however, is usually the challenging part. It took me a long time to get the PV code right so that I can merge qspinlock upstream. I do some interest to get qspinlock used by ppc, though.
Storing the cpu number into the lock can be useful for other reason too. It is not totally related to PPC support.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
Quoted 2 lines · Waiman Long
Storing the cpu number into the lock can be useful for other reason too. It is not totally related to PPC support.
Well, the thing you did only works for 'small' (<253 CPU) systems. There's a number of Power systems that's distinctly larger than that. So it simply cannot work as anything other than a suggestion/hint. It must not be a correctness thing.
Waiman Long
reviewerRe: [PATCH 2/2] locking/pvqspinlock: Optionally store lock holder cpu into lock
Quoted 8 lines · Peter Zijlstra
Quoted 2 lines · Waiman Long
Storing the cpu number into the lock can be useful for other reason too. It is not totally related to PPC support.
Well, the thing you did only works for 'small' (<253 CPU) systems. There's a number of Power systems that's distinctly larger than that. So it simply cannot work as anything other than a suggestion/hint. It must not be a correctness thing.
Yes, there are limit on how much data one can put into the lock byte. So it is not a sure way to find out who the lock holder is. There are certainly large systems with hundreds or even thousands of cpus, but they are the minority in the sea of Linux systems out there.
If the lock holder goes through the slowpath, the one behind it can save its cpu number which can be used a hint of who the lock holder is though it is not that reliable as lock stealing can happen.
BTW, I did get the optimized PV unlock asm code working now. I will post the updated unconditional patch later this week for further discussion.
Cheers,
Longman