Add NUMA-awareness to qspinlock
Subsystems (4)
v5 cover Add NUMA-awareness to qspinlock
Changes from v4:
----------------
- Switch to a deterministic bound on the number of intra-node handoffs, as suggested by Longman.
- Scan the main queue after acquiring the MCS lock and before acquiring the spinlock (pre-scan), as suggested by Longman. If no thread is found in pre-scan, try again after acquiring the spinlock, resuming from the same place where pre-scan stopped.
- Convert the secondary queue to a cyclic list such that the tail’s @next points to the head of the queue. Store the pointer to the secondary queue tail (rather than head) in @locked. This eliminates the need for the @tail field in CNA nodes, making space for fields required by the two changes above.
- Change arch_mcs_spin_lock_contended() to arch_mcs_spin_lock(), and fix misuse of old macro names, as suggested by Hanjun.
Summary
-------
Lock throughput can be increased by handing a lock to a waiter on the same NUMA node as the lock holder, provided care is taken to avoid starvation of waiters on other NUMA nodes. This patch introduces CNA (compact NUMA-aware lock) as the slow path for qspinlock. It is enabled through a configuration option (NUMA_AWARE_SPINLOCKS).
CNA is a NUMA-aware version of the MCS lock. Spinning threads are organized in two queues, a main queue for threads running on the same node as the current lock holder, and a secondary queue for threads running on other nodes. Threads store the ID of the node on which they are running in their queue nodes. After acquiring the MCS lock and before acquiring the spinlock, the lock holder scans the main queue looking for a thread running on the same node (pre-scan). If found (call it thread T), all threads in the main queue between the current lock holder and T are moved to the end of the secondary queue. If such T is not found, we make another scan of the main queue after acquiring the spinlock when unlocking the MCS lock (post-scan), starting at the node where pre-scan stopped. If both scans fail to find such T, the MCS lock is passed to the first thread in the secondary queue. If the secondary queue is empty, the MCS lock is passed to the next thread in the main queue. To avoid starvation of threads in the secondary queue, those threads are moved back to the head of the main queue after a certain number of intra-node lock hand-offs.
More details are available at https://arxiv.org/abs/1810.05600.
We have done some performance evaluation with the locktorture module as well as with several benchmarks from the will-it-scale repo. The following locktorture results are from an Oracle X5-4 server (four Intel Xeon E7-8895 v3 @ 2.60GHz sockets with 18 hyperthreaded cores each). Each number represents an average (over 25 runs) of the total number of ops (x10^7) reported at the end of each run. The standard deviation is also reported in (), and in general is about 3% from the average. The 'stock' kernel is v5.4.0-rc1,
commit d90f2df63c5c, compiled in the default configuration. 'patch-CNA' is the modified kernel with NUMA_AWARE_SPINLOCKS set; the speedup is calculated dividing 'patch-CNA' by 'stock'.
#thr stock patch-CNA speedup (patch-CNA/stock)
1 2.674 (0.118) 2.736 (0.119) 1.023 2 2.588 (0.141) 2.603 (0.108) 1.006 4 4.230 (0.120) 4.220 (0.127) 0.998 8 5.362 (0.181) 6.679 (0.182) 1.246 16 6.639 (0.133) 8.050 (0.200) 1.213 32 7.359 (0.149) 8.792 (0.168) 1.195 36 7.443 (0.142) 8.873 (0.230) 1.192 72 6.554 (0.147) 9.317 (0.158) 1.421 108 6.156 (0.093) 9.404 (0.191) 1.528 142 5.659 (0.093) 9.361 (0.184) 1.654
The following tables contain throughput results (ops/us) from the same setup for will-it-scale/open1_threads:
#thr stock patch-CNA speedup (patch-CNA/stock)
1 0.532 (0.002) 0.532 (0.003) 1.000 2 0.785 (0.024) 0.779 (0.025) 0.992 4 1.426 (0.018) 1.409 (0.021) 0.988 8 1.779 (0.101) 1.711 (0.127) 0.962 16 1.761 (0.093) 1.671 (0.104) 0.949 32 0.935 (0.063) 1.619 (0.093) 1.731 36 0.936 (0.082) 1.591 (0.086) 1.699 72 0.839 (0.043) 1.667 (0.097) 1.988 108 0.842 (0.035) 1.701 (0.091) 2.021 142 0.830 (0.037) 1.714 (0.098) 2.066
and will-it-scale/lock2_threads:
#thr stock patch-CNA speedup (patch-CNA/stock)
1 1.555 (0.009) 1.577 (0.002) 1.014 2 2.644 (0.060) 2.682 (0.062) 1.014 4 5.159 (0.205) 5.197 (0.231) 1.007 8 4.302 (0.221) 4.279 (0.318) 0.995 16 4.259 (0.111) 4.087 (0.163) 0.960 32 2.583 (0.112) 4.077 (0.120) 1.578 36 2.499 (0.106) 4.076 (0.106) 1.631 72 1.979 (0.085) 4.077 (0.123) 2.061 108 2.096 (0.090) 4.043 (0.130) 1.929 142 1.913 (0.109) 3.984 (0.108) 2.082
Our evaluation shows that CNA also improves performance of user applications that have hot pthread mutexes. Those mutexes are blocking, and waiting threads park and unpark via the futex mechanism in the kernel. Given that kernel futex chains, which are hashed by the mutex address, are each protected by a chain-specific spin lock, the contention on a user-mode mutex translates into contention on a kernel level spinlock.
Here are the results for the leveldb ‘readrandom’ benchmark:
#thr stock patch-CNA speedup (patch-CNA/stock)
1 0.532 (0.007) 0.535 (0.015) 1.006 2 0.665 (0.030) 0.673 (0.034) 1.011 4 0.715 (0.023) 0.716 (0.026) 1.002 8 0.686 (0.023) 0.686 (0.024) 1.001 16 0.719 (0.030) 0.737 (0.025) 1.025 32 0.740 (0.034) 0.959 (0.105) 1.296 36 0.730 (0.024) 1.079 (0.112) 1.478 72 0.652 (0.018) 1.160 (0.024) 1.778 108 0.622 (0.016) 1.157 (0.028) 1.860 142 0.600 (0.015) 1.145 (0.035) 1.908
Additional performance numbers are available in previous revisions of the series.
Further comments are welcome and appreciated.
Alex Kogan (5):
locking/qspinlock: Rename mcs lock/unlock macros and make them more
generic
locking/qspinlock: Refactor the qspinlock slow path locking/qspinlock: Introduce CNA into the slow path of qspinlock locking/qspinlock: Introduce starvation avoidance into CNA locking/qspinlock: Introduce the shuffle reduction optimization into
CNA
arch/arm/include/asm/mcs_spinlock.h | 6 +- arch/x86/Kconfig | 19 +++ arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 41 +++++ include/asm-generic/mcs_spinlock.h | 4 +- kernel/locking/mcs_spinlock.h | 20 +-- kernel/locking/qspinlock.c | 77 ++++++++- kernel/locking/qspinlock_cna.h | 312 ++++++++++++++++++++++++++++++++++++ kernel/locking/qspinlock_paravirt.h | 2 +- 9 files changed, 462 insertions(+), 23 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.h
Signature · 7 lines
2.11.0 (Apple Git-81) _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
PATCH v5 1/5 locking/qspinlock: Rename mcs lock/unlock macros and make them more generic
The mcs unlock macro (arch_mcs_pass_lock) should accept the value to be stored into the lock argument as another argument. This allows using the same macro in cases where the value to be stored when passing the lock is different from 1.
--- arch/arm/include/asm/mcs_spinlock.h | 6 +++--- include/asm-generic/mcs_spinlock.h | 4 ++-- kernel/locking/mcs_spinlock.h | 18 +++++++++--------- kernel/locking/qspinlock.c | 4 ++-- kernel/locking/qspinlock_paravirt.h | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-)
arch/arm/include/asm/mcs_spinlock.h +3 -3
@@ -6,7 +6,7 @@#include <asm/spinlock.h> /* MCS spin-locking. */ -#define arch_mcs_spin_lock_contended (lock) \ +#define arch_mcs_spin_lock (lock) \ do { \/* Ensure prior stores are observed before we enter wfe. */ \smp_mb (); \
@@ -14,9 +14,9 @@ do { \wfe (); \ } while (0) \ -#define arch_mcs_spin_unlock_contended (lock) \ +#define arch_mcs_pass_lock (lock, val) \ do { \ - smp_store_release(lock, 1); \ + smp_store_release((lock), (val)); \ dsb_sev(); \ } while (0)
include/asm-generic/mcs_spinlock.h +2 -2
@@ -4,8 +4,8 @@/* * Architectures can define their own: * -* arch_mcs_spin_lock_contended(l) -* arch_mcs_spin_unlock_contended(l) +* arch_mcs_spin_lock(l) +* arch_mcs_pass_lock(l, val) * * See kernel/locking/mcs_spinlock.c. */
kernel/locking/mcs_spinlock.h +9 -9
@@ -21,7 +21,7 @@ struct mcs_spinlock {int count;/* nesting count, see qspinlock.c */ }; -#ifndef arch_mcs_spin_lock_contended +#ifndef arch_mcs_spin_lock/* * Using smp_cond_load_acquire() provides the acquire semantics * required so that subsequent operations happen after the
@@ -29,20 +29,20 @@ struct mcs_spinlock {* ARM64 would like to do spin-waiting instead of purely * spinning, and smp_cond_load_acquire() provides that behavior. */ -#define arch_mcs_spin_lock_contended (l) \ -do { \ - smp_cond_load_acquire(l, VAL); \ +#define arch_mcs_spin_lock (l) \ +do { \ + smp_cond_load_acquire(l, VAL); \ } while (0)#endif -#ifndef arch_mcs_spin_unlock_contended +#ifndef arch_mcs_spin_unlock/* * smp_store_release() provides a memory barrier to ensure all * operations in the critical section has been completed before * unlocking. */ -#define arch_mcs_spin_unlock_contended (l) \ - smp_store_release((l), 1) +#define arch_mcs_pass_lock (l, val) \ + smp_store_release((l), (val))#endif /*
@@ -91,7 +91,7 @@ void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node)WRITE_ONCE (prev->next, node);/* Wait until the lock holder passes the lock down. */ -arch_mcs_spin_lock_contended (&node->locked); +arch_mcs_spin_lock (&node->locked); }/*
@@ -115,7 +115,7 @@ void mcs_spin_unlock(struct mcs_spinlock **lock, struct mcs_spinlock *node) }/* Pass lock to next waiter. */ -arch_mcs_spin_unlock_contended (&next->locked); +arch_mcs_pass_lock (&next->locked,1 ); }#endif /* __LINUX_MCS_SPINLOCK_H */
kernel/locking/qspinlock.c +2 -2
@@ -470,7 +470,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)WRITE_ONCE (prev->next, node);pv_wait_node (node, prev); -arch_mcs_spin_lock_contended (&node->locked); +arch_mcs_spin_lock (&node->locked);/* * While waiting for the MCS lock, the next pointer may have
@@ -549,7 +549,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)if (!next) next =smp_cond_load_relaxed (&node->next, (VAL )); -arch_mcs_spin_unlock_contended (&next->locked); +arch_mcs_pass_lock (&next->locked,1 );pv_kick_node (lock, next); release:
kernel/locking/qspinlock_paravirt.h +1 -1
@@ -368,7 +368,7 @@ static void pv_kick_node(struct qspinlock *lock, struct mcs_spinlock *node)* * Matches with smp_store_mb() and cmpxchg() in pv_wait_node() * -* The write to next->locked in arch_mcs_spin_unlock_contended() +* The write to next->locked in arch_mcs_pass_lock() * must be ordered before the read of pn->state in the cmpxchg() * below for the code to work correctly. To guarantee full ordering * irrespective of the success or failure of the cmpxchg(),
Signature · 1 line
2.11.0 (Apple Git-81)
PATCH v5 2/5 locking/qspinlock: Refactor the qspinlock slow path
In reply to Alex KoganMove some of the code manipulating the spin lock into separate functions. This would allow easier integration of alternative ways to manipulate that lock.
--- kernel/locking/qspinlock.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-)
kernel/locking/qspinlock.c +36 -2
@@ -288,6 +288,34 @@ static __always_inline u32 __pv_wait_head_or_lock(struct qspinlock *lock,#define queued_spin_lock_slowpath native_queued_spin_lock_slowpath#endif +/* +* __try_clear_tail - try to clear tail by setting the lock value to +* _Q_LOCKED_VAL. +* @lock: Pointer to the queued spinlock structure +* @val: Current value of the lock +* @node: Pointer to the MCS node of the lock holder +*/ +static __always_inline bool __try_clear_tail(struct qspinlock *lock, +u32 val, +struct mcs_spinlock *node) +{ +return atomic_try_cmpxchg_relaxed (&lock->val, &val, _Q_LOCKED_VAL); +} + +/* +* __mcs_pass_lock - pass the MCS lock to the next waiter +* @node: Pointer to the MCS node of the lock holder +* @next: Pointer to the MCS node of the first waiter in the MCS queue +*/ +static __always_inline void __mcs_pass_lock(struct mcs_spinlock *node, +struct mcs_spinlock *next) +{ +arch_mcs_pass_lock (&next->locked,1 ); +} + +#define try_clear_tail __try_clear_tail +#define mcs_pass_lock __mcs_pass_lock +#endif /* _GEN_PV_LOCK_SLOWPATH */ /**
@@ -532,7 +560,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)* PENDING will make the uncontended transition fail. */ if ((val & _Q_TAIL_MASK) == tail) { -if (atomic_try_cmpxchg_relaxed (&lock->val, &val, _Q_LOCKED_VAL)) +if (try_clear_tail (lock, val, node)) goto release;/* No contention */ }
@@ -549,7 +577,7 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)if (!next) next =smp_cond_load_relaxed (&node->next, (VAL )); -arch_mcs_pass_lock (&next->locked,1 ); +mcs_pass_lock (node, next);pv_kick_node (lock, next); release:
@@ -574,6 +602,12 @@ EXPORT_SYMBOL(queued_spin_lock_slowpath);#undef pv_kick_node#undef pv_wait_head_or_lock +#undef try_clear_tail +#define try_clear_tail __try_clear_tail + +#undef mcs_pass_lock +#define mcs_pass_lock __mcs_pass_lock +#undef queued_spin_lock_slowpath#define queued_spin_lock_slowpath __pv_queued_spin_lock_slowpath
Signature · 1 line
2.11.0 (Apple Git-81)
PATCH v5 3/5 locking/qspinlock: Introduce CNA into the slow path of qspinlock
In reply to Alex KoganIn CNA, spinning threads are organized in two queues, a main queue for threads running on the same node as the current lock holder, and a secondary queue for threads running on other nodes. After acquiring the MCS lock and before acquiring the spinlock, the lock holder scans the main queue looking for a thread running on the same node (pre-scan). If found (call it thread T), all threads in the main queue between the current lock holder and T are moved to the end of the secondary queue. If such T is not found, we make another scan of the main queue when unlocking the MCS lock (post-scan), starting at the position where pre-scan stopped. If both scans fail to find such T, the MCS lock is passed to the first thread in the secondary queue. If the secondary queue is empty, the lock is passed to the next thread in the main queue. For more details, see https://arxiv.org/abs/1810.05600.
Note that this variant of CNA may introduce starvation by continuously passing the lock to threads running on the same node. This issue will be addressed later in the series.
Enabling CNA is controlled via a new configuration option (NUMA_AWARE_SPINLOCKS). By default, the CNA variant is patched in at the boot time only if we run on a multi-node machine in native environment and the new config is enabled. (For the time being, the patching requires CONFIG_PARAVIRT_SPINLOCKS to be enabled as well. However, this should be resolved once static_call() is available.) This default behavior can be overridden with the new kernel boot command-line option "numa_spinlock=on/off" (default is "auto").
--- arch/x86/Kconfig | 19 +++ arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 41 +++++++ kernel/locking/mcs_spinlock.h | 2 +- kernel/locking/qspinlock.c | 34 +++++- kernel/locking/qspinlock_cna.h | 258 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 353 insertions(+), 5 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.h
arch/x86/Kconfig +19 -0
@@ -1573,6 +1573,25 @@ config NUMA Otherwise, you should say N. +config NUMA_AWARE_SPINLOCKS + bool "Numa-aware spinlocks" + depends on NUMA + depends on QUEUED_SPINLOCKS + # For now, we depend on PARAVIRT_SPINLOCKS to make the patching work. + # This is awkward, but hopefully would be resolved once static_call() + # is available. + depends on PARAVIRT_SPINLOCKS + default y + help + Introduce NUMA (Non Uniform Memory Access) awareness into + the slow path of spinlocks. + + In this variant of qspinlock, the kernel will try to keep the lock + on the same node, thus reducing the number of remote cache misses, + while trading some of the short term fairness for better performance. + + Say N if you want absolute first come first serve fairness. + config AMD_NUMA def_bool y prompt "Old style AMD Opteron NUMA detection"
arch/x86/include/asm/qspinlock.h +4 -0
@@ -27,6 +27,10 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *loreturn val; } +#ifdef CONFIG_NUMA_AWARE_SPINLOCKS +extern void __cna_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +#endif +#ifdef CONFIG_PARAVIRT_SPINLOCKS extern void native_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val);extern void __pv_init_lock_hash (void );
arch/x86/kernel/alternative.c +41 -0
53 lines not on this page · raw message
kernel/locking/mcs_spinlock.h +1 -1
@@ -17,7 +17,7 @@struct mcs_spinlock {struct mcs_spinlock *next; -int locked;/* 1 if lock acquired */ +unsigned int locked;/* 1 if lock acquired */ int count;/* nesting count, see qspinlock.c */ };
kernel/locking/qspinlock.c +30 -4
@@ -11,7 +11,7 @@* Peter Zijlstra <[email protected]> */ -#ifndef _GEN_PV_LOCK_SLOWPATH +#if !defined(_GEN_PV_LOCK_SLOWPATH) && !defined(_GEN_CNA_LOCK_SLOWPATH)#include <linux/smp.h> #include <linux/bug.h>
@@ -70,7 +70,8 @@/* * On 64-bit architectures, the mcs_spinlock structure will be 16 bytes in * size and four of them will fit nicely in one 64-byte cacheline. For -* pvqspinlock, however, we need more space for extra data. To accommodate +* pvqspinlock, however, we need more space for extra data. The same also +* applies for the NUMA-aware variant of spinlocks (CNA). To accommodate * that, we insert two more long words to pad it up to 32 bytes. IOW, only * two of them can fit in a cacheline in this case. That is OK as it is rare * to have more than 2 levels of slowpath nesting in actual use. We don't
@@ -79,7 +80,7 @@*/ struct qnode {struct mcs_spinlock mcs; -#ifdef CONFIG_PARAVIRT_SPINLOCKS +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) || defined(CONFIG_NUMA_AWARE_SPINLOCKS )long reserved[2 ];#endif };
@@ -103,6 +104,8 @@ struct qnode {* Exactly fits one 64-byte cacheline on a 64-bit architecture. * * PV doubles the storage and uses the second cacheline for PV state. +* CNA also doubles the storage and uses the second cacheline for +* CNA-specific state. */ static DEFINE_PER_CPU_ALIGNED(struct qnode, qnodes[MAX_NODES]);
@@ -316,7 +319,7 @@ static __always_inline void __mcs_pass_lock(struct mcs_spinlock *node,#define try_clear_tail __try_clear_tail#define mcs_pass_lock __mcs_pass_lock -#endif /* _GEN_PV_LOCK_SLOWPATH */ +#endif /* _GEN_PV_LOCK_SLOWPATH && _GEN_CNA_LOCK_SLOWPATH */ /** * queued_spin_lock_slowpath - acquire the queued spinlock
@@ -589,6 +592,29 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)EXPORT_SYMBOL (queued_spin_lock_slowpath);/* +* Generate the code for NUMA-aware spinlocks +*/ +#if !defined(_GEN_CNA_LOCK_SLOWPATH) && defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +#define _GEN_CNA_LOCK_SLOWPATH + +#undef pv_wait_head_or_lock +#define pv_wait_head_or_lock cna_pre_scan + +#undef try_clear_tail +#define try_clear_tail cna_try_change_tail + +#undef mcs_pass_lock +#define mcs_pass_lock cna_pass_lock + +#undef queued_spin_lock_slowpath +#define queued_spin_lock_slowpath __cna_queued_spin_lock_slowpath + +#include "qspinlock_cna.h" +#include "qspinlock.c" + +#endif + +/* * Generate the paravirt code for queued_spin_unlock_slowpath(). */ #if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS )
kernel/locking/qspinlock_cna.h new file +258 -0
258 lines not on this page · raw message
Signature · 1 line
2.11.0 (Apple Git-81)
Waiman Long
reviewerRe: [PATCH v5 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
Quoted 59 lines · Alex Kogan
In CNA, spinning threads are organized in two queues, a main queue for threads running on the same node as the current lock holder, and a secondary queue for threads running on other nodes. After acquiring the MCS lock and before acquiring the spinlock, the lock holder scans the main queue looking for a thread running on the same node (pre-scan). If found (call it thread T), all threads in the main queue between the current lock holder and T are moved to the end of the secondary queue. If such T is not found, we make another scan of the main queue when unlocking the MCS lock (post-scan), starting at the position where pre-scan stopped. If both scans fail to find such T, the MCS lock is passed to the first thread in the secondary queue. If the secondary queue is empty, the lock is passed to the next thread in the main queue. For more details, see https://arxiv.org/abs/1810.05600.
Note that this variant of CNA may introduce starvation by continuously passing the lock to threads running on the same node. This issue will be addressed later in the series.
Enabling CNA is controlled via a new configuration option (
NUMA_AWARE_SPINLOCKS). By default, the CNA variant is patched in at the boot time only if we run on a multi-node machine in native environment and the new config is enabled. (For the time being, the patching requiresCONFIG_PARAVIRT_SPINLOCKSto be enabled as well. However, this should be resolved oncestatic_call()is available.) This default behavior can be overridden with the new kernel boot command-line option "numa_spinlock=on/off" (default is "auto").Signed-off-by Alex Kogan <[email protected]>Reviewed-by Steve Sistare <[email protected]>--- arch/x86/Kconfig | 19 +++ arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 41 +++++++ kernel/locking/mcs_spinlock.h | 2 +- kernel/locking/qspinlock.c | 34 +++++- kernel/locking/qspinlock_cna.h | 258 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 353 insertions(+), 5 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.harch/x86/Kconfig +12
-0@@ -1573,6 +1573,25 @@ config NUMA Otherwise, you should say N. +config NUMA_AWARE_SPINLOCKS + bool "Numa-aware spinlocks" + depends on NUMA + depends on QUEUED_SPINLOCKS + # For now, we depend on PARAVIRT_SPINLOCKS to make the patching work. + # This is awkward, but hopefully would be resolved once static_call() + # is available. + depends on PARAVIRT_SPINLOCKS + default y + help + Introduce NUMA (Non Uniform Memory Access) awareness into + the slow path of spinlocks.
arch/x86/Kconfig config NUMA · line 1597
Quoted 10 lines
arch/x86/Kconfig +7
-0+ + In this variant of qspinlock, the kernel will try to keep the lock + on the same node, thus reducing the number of remote cache misses, + while trading some of the short term fairness for better performance. + + Say N if you want absolute first come first serve fairness. + config AMD_NUMA def_bool y prompt "Old style AMD Opteron NUMA detection"
I forgot to mention that you should also document the new boot command line option at Documentation/admin-guide/kernel-parameters.txt.
Quoted 250 lines
arch/x86/include/asm/qspinlock.h +4
-0@@ -27,6 +27,10 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *loreturn val; } +#ifdef CONFIG_NUMA_AWARE_SPINLOCKS +extern void __cna_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +#endif +#ifdef CONFIG_PARAVIRT_SPINLOCKS extern void native_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val);extern void __pv_init_lock_hash (void );arch/x86/kernel/alternative.c +41
-0@@ -698,6 +698,33 @@ static void __init int3_selftest(void)unregister_die_notifier (&int3_exception_nb); } +#if defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +/* +* Constant (boot-param configurable) flag selecting the NUMA-aware variant +* of spinlock. Possible values: -1 (off) / 0 (auto, default) / 1 (on). +*/ +static int numa_spinlock_flag; + +static int __init numa_spinlock_setup (char *str) +{ +if (!strcmp (str,"auto" )) { + numa_spinlock_flag =0 ; +return 1 ; + }else if (!strcmp (str,"on" )) { + numa_spinlock_flag =1 ; +return 1 ; + }else if (!strcmp (str,"off" )) { + numa_spinlock_flag =-1 ; +return 1 ; + } + +return 0 ; +} + +__setup ("numa_spinlock=" , numa_spinlock_setup); + +#endif +void __init alternative_instructions (void ) {int3_selftest ();@@ -738,6 +765,20 @@ void __init alternative_instructions(void) }#endif +#if defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +/* +* By default, switch to the NUMA-friendly slow path for +* spinlocks when we have multiple NUMA nodes in native environment. +*/ +if ((numa_spinlock_flag ==1 ) || + (numa_spinlock_flag ==0 && nr_node_ids >1 && + pv_ops.lock.queued_spin_lock_slowpath == + native_queued_spin_lock_slowpath)) { + pv_ops.lock.queued_spin_lock_slowpath = + __cna_queued_spin_lock_slowpath; + } +#endif +apply_paravirt (__parainstructions, __parainstructions_end);restart_nmi ();kernel/locking/mcs_spinlock.h +1
-1@@ -17,7 +17,7 @@struct mcs_spinlock {struct mcs_spinlock *next; -int locked;/* 1 if lock acquired */ +unsigned int locked;/* 1 if lock acquired */ int count;/* nesting count, see qspinlock.c */ };kernel/locking/qspinlock.c +30
-4@@ -11,7 +11,7 @@* Peter Zijlstra <[email protected]> */ -#ifndef _GEN_PV_LOCK_SLOWPATH +#if !defined(_GEN_PV_LOCK_SLOWPATH) && !defined(_GEN_CNA_LOCK_SLOWPATH)#include <linux/smp.h> #include <linux/bug.h> @@ -70,7 +70,8 @@/* * On 64-bit architectures, the mcs_spinlock structure will be 16 bytes in * size and four of them will fit nicely in one 64-byte cacheline. For -* pvqspinlock, however, we need more space for extra data. To accommodate +* pvqspinlock, however, we need more space for extra data. The same also +* applies for the NUMA-aware variant of spinlocks (CNA). To accommodate * that, we insert two more long words to pad it up to 32 bytes. IOW, only * two of them can fit in a cacheline in this case. That is OK as it is rare * to have more than 2 levels of slowpath nesting in actual use. We don't @@ -79,7 +80,7 @@*/ struct qnode {struct mcs_spinlock mcs; -#ifdef CONFIG_PARAVIRT_SPINLOCKS +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) || defined(CONFIG_NUMA_AWARE_SPINLOCKS )long reserved[2 ];#endif };@@ -103,6 +104,8 @@ struct qnode {* Exactly fits one 64-byte cacheline on a 64-bit architecture. * * PV doubles the storage and uses the second cacheline for PV state. +* CNA also doubles the storage and uses the second cacheline for +* CNA-specific state. */ static DEFINE_PER_CPU_ALIGNED(struct qnode, qnodes[MAX_NODES]);@@ -316,7 +319,7 @@ static __always_inline void __mcs_pass_lock(struct mcs_spinlock *node,#define try_clear_tail __try_clear_tail#define mcs_pass_lock __mcs_pass_lock -#endif /* _GEN_PV_LOCK_SLOWPATH */ +#endif /* _GEN_PV_LOCK_SLOWPATH && _GEN_CNA_LOCK_SLOWPATH */ /** * queued_spin_lock_slowpath - acquire the queued spinlock @@ -589,6 +592,29 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)EXPORT_SYMBOL (queued_spin_lock_slowpath);/* +* Generate the code for NUMA-aware spinlocks +*/ +#if !defined(_GEN_CNA_LOCK_SLOWPATH) && defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +#define _GEN_CNA_LOCK_SLOWPATH + +#undef pv_wait_head_or_lock +#define pv_wait_head_or_lock cna_pre_scan + +#undef try_clear_tail +#define try_clear_tail cna_try_change_tail + +#undef mcs_pass_lock +#define mcs_pass_lock cna_pass_lock + +#undef queued_spin_lock_slowpath +#define queued_spin_lock_slowpath __cna_queued_spin_lock_slowpath + +#include "qspinlock_cna.h" +#include "qspinlock.c" + +#endif + +/* * Generate the paravirt code for queued_spin_unlock_slowpath(). */ #if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS )kernel/locking/qspinlock_cna.h new file +77
-0@@ -0,0 +1,258 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _GEN_CNA_LOCK_SLOWPATH +#error "do not include this file" +#endif + +#include <linux/topology.h> + +/* +* Implement a NUMA-aware version of MCS (aka CNA, or compact NUMA-aware lock). +* +* In CNA, spinning threads are organized in two queues, a main queue for +* threads running on the same NUMA node as the current lock holder, and a +* secondary queue for threads running on other nodes. Schematically, it +* looks like this: +* +* cna_node +* +----------+ +--------+ +--------+ +* |mcs:next | -> |mcs:next| -> ... |mcs:next| -> NULL [Main queue] +* |mcs:locked| -+ +--------+ +--------+ +* +----------+ | +* +----------------------+ +* \/ +* +--------+ +--------+ +* |mcs:next| -> ... |mcs:next| [Secondary queue] +* +--------+ +--------+ +* ^ | +* +--------------------+ +* +* N.B. locked = 1 if secondary queue is absent. Othewrise, it contains the +* encoded pointer to the tail of the secondary queue, which is organized as a +* circular list. +* +* After acquiring the MCS lock and before acquiring the spinlock, the lock +* holder scans the main queue looking for a thread running on the same node +* (pre-scan). If found (call it thread T), all threads in the main queue +* between the current lock holder and T are moved to the end of the secondary +* queue. If such T is not found, we make another scan of the main queue when +* unlocking the MCS lock (post-scan), starting at the node where pre-scan +* stopped. If both scans fail to find such T, the MCS lock is passed to the +* first thread in the secondary queue. If the secondary queue is empty, the +* lock is passed to the next thread in the main queue. +* +* For more details, see https://arxiv.org/abs/1810.05600. +* +* Authors: Alex Kogan <[email protected]> +* Dave Dice <[email protected]> +*/ + +struct cna_node { +struct mcs_spinlock mcs; +int numa_node; +u32 encoded_tail; +u32 pre_scan_result;/* 0 or an encoded tail */ +}; + +static void __init cna_init_nodes_per_cpu (unsigned int cpu) +{ +struct mcs_spinlock *base =per_cpu_ptr (&qnodes[0 ].mcs, cpu); +int numa_node =cpu_to_node (cpu); +int i; + +for (i =0 ; i <MAX_NODES ; i++) { +struct cna_node *cn = (struct cna_node *)grab_mcs_node (base, i); + + cn->numa_node = numa_node; + cn->encoded_tail =encode_tail (cpu, i); +/* +* @encoded_tail has to be larger than 1, so we do not confuse +* it with other valid values for @locked or @pre_scan_result +* (0 or 1) +*/ +WARN_ON (cn->encoded_tail <=1 ); + } +} + +static void __init cna_init_nodes (void ) +{
kernel/locking/qspinlock_cna.h line 87
Quoted 10 lines
kernel/locking/qspinlock_cna.h new file +10
-0+unsigned int cpu; + +BUILD_BUG_ON (sizeof (struct cna_node ) >sizeof (struct qnode )); +/* we store an ecoded tail word in the node's @locked field */ +BUILD_BUG_ON (sizeof (u32) >sizeof (unsigned int )); + +for_each_possible_cpu (cpu) +cna_init_nodes_per_cpu (cpu); +} +early_initcall (cna_init_nodes);
typedef int (*initcall_t)(void);
So cna_init_nodes() should return an integer value. You can just return 0 in this case.
Quoted 17 lines
kernel/locking/qspinlock_cna.h new file +17
-0+ +static inline bool cna_try_change_tail (struct qspinlock *lock,u32 val, +struct mcs_spinlock *node) +{ +struct mcs_spinlock *head_2nd, *tail_2nd; +u32 new; + +/* If the secondary queue is empty, do what MCS does. */ +if (node->locked <=1 ) +return __try_clear_tail (lock, val, node); + +/* +* Try to update the tail value to the last node in the secondary queue. +* If successful, pass the lock to the first thread in the secondary +* queue. Doing those two actions effectively moves all nodes from the +* secondary queue into the main one. +*/
kernel/locking/qspinlock_cna.h line 114
Quoted 10 lines
kernel/locking/qspinlock_cna.h new file +10
-0+ tail_2nd =decode_tail (node->locked); + head_2nd = tail_2nd->next; + new = ((struct cna_node *)tail_2nd)->encoded_tail + _Q_LOCKED_VAL; + +if (atomic_try_cmpxchg_relaxed (&lock->val, &val, new)) { +/* +* Try to reset @next in tail_2nd to NULL, but no need to check +* the result - if failed, a new successor has updated it. +*/ +cmpxchg64_relaxed (&tail_2nd->next, head_2nd,NULL );
Why do you use cmpxchg64*()? That can be problematic when compiling on 32-bit architecture. I think you should just use cmpxhg_relaxed() for automatic sizing.
kernel/locking/qspinlock_cna.h line 120
Quoted 6 lines
kernel/locking/qspinlock_cna.h new file +6
-0+arch_mcs_pass_lock (&head_2nd->locked,1 ); +return true; + } + +return false; +}
Cheers,
Longman
Re: [PATCH v5 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
Quoted 68 lines · Waiman Long
Quoted 64 lines · Alex Kogan
In CNA, spinning threads are organized in two queues, a main queue for
threads running on the same node as the current lock holder, and a
secondary queue for threads running on other nodes. After acquiring the
MCS lock and before acquiring the spinlock, the lock holder scans the
main queue looking for a thread running on the same node (pre-scan). If
found (call it thread T), all threads in the main queue between the
current lock holder and T are moved to the end of the secondary queue.
If such T is not found, we make another scan of the main queue when
unlocking the MCS lock (post-scan), starting at the position where
pre-scan stopped. If both scans fail to find such T, the MCS lock is
passed to the first thread in the secondary queue. If the secondary queue
is empty, the lock is passed to the next thread in the main queue.
For more details, see https://urldefense.proofpoint.com/v2/url?u=https-3A__arxiv.org_abs_1810.05600&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=Hvhk3F4omdCk-GE1PTOm3Kn0A7ApWOZ2aZLTuVxFK4k&m=YE_EINNtJ6kvX7-r1fCoSPo_hPkyTTkYRKJ2WSQgURA&s=Hxk5PX1WhsVBJMb5LB-bw_EBT3ZXMbr0G40LbDQWtqg&e= .Note that this variant of CNA may introduce starvation by continuously passing the lock to threads running on the same node. This issue will be addressed later in the series.
Enabling CNA is controlled via a new configuration option (
NUMA_AWARE_SPINLOCKS). By default, the CNA variant is patched in at the boot time only if we run on a multi-node machine in native environment and the new config is enabled. (For the time being, the patching requiresCONFIG_PARAVIRT_SPINLOCKSto be enabled as well. However, this should be resolved oncestatic_call()is available.) This default behavior can be overridden with the new kernel boot command-line option "numa_spinlock=on/off" (default is "auto").Signed-off-by Alex Kogan <[email protected]>Reviewed-by Steve Sistare <[email protected]>---
arch/x86/Kconfig | 19 +++ arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 41 +++++++ kernel/locking/mcs_spinlock.h | 2 +- kernel/locking/qspinlock.c | 34 +++++- kernel/locking/qspinlock_cna.h | 258 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 353 insertions(+), 5 deletions(-)create mode 100644
kernel/locking/qspinlock_cna.harch/x86/Kconfig +17
-0@@ -1573,6 +1573,25 @@ config NUMA Otherwise, you should say N. +config NUMA_AWARE_SPINLOCKS + bool "Numa-aware spinlocks" + depends on NUMA + depends on QUEUED_SPINLOCKS + # For now, we depend on PARAVIRT_SPINLOCKS to make the patching work. + # This is awkward, but hopefully would be resolved once static_call() + # is available. + depends on PARAVIRT_SPINLOCKS + default y + help + Introduce NUMA (Non Uniform Memory Access) awareness into + the slow path of spinlocks. + + In this variant of qspinlock, the kernel will try to keep the lock + on the same node, thus reducing the number of remote cache misses, + while trading some of the short term fairness for better performance. +
Quoted 8 lines
Quoted 5 lines
arch/x86/Kconfig +2
-0+ Say N if you want absolute first come first serve fairness. +config
AMD_NUMA
def_booly
prompt "Old style AMD Opteron NUMA detection"I forgot to mention that you should also document the new boot command line option at
Documentation/admin-guide/kernel-parameters.txt.
Will do.
Quoted 264 lines
Quoted 252 lines
arch/x86/include/asm/qspinlock.h +4
-0@@ -27,6 +27,10 @@ static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *loreturn val; } +#ifdef CONFIG_NUMA_AWARE_SPINLOCKS +extern void __cna_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val); +#endif +#ifdef CONFIG_PARAVIRT_SPINLOCKS extern void native_queued_spin_lock_slowpath (struct qspinlock *lock,u32 val);extern void __pv_init_lock_hash (void );arch/x86/kernel/alternative.c +41
-0@@ -698,6 +698,33 @@ static void __init int3_selftest(void)unregister_die_notifier (&int3_exception_nb); } +#if defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +/* +* Constant (boot-param configurable) flag selecting the NUMA-aware variant +* of spinlock. Possible values: -1 (off) / 0 (auto, default) / 1 (on). +*/ +static int numa_spinlock_flag; + +static int __init numa_spinlock_setup (char *str) +{ +if (!strcmp (str,"auto" )) { + numa_spinlock_flag =0 ; +return 1 ; + }else if (!strcmp (str,"on" )) { + numa_spinlock_flag =1 ; +return 1 ; + }else if (!strcmp (str,"off" )) { + numa_spinlock_flag =-1 ; +return 1 ; + } + +return 0 ; +} + +__setup ("numa_spinlock=" , numa_spinlock_setup); + +#endif +void __init alternative_instructions (void ) {int3_selftest ();@@ -738,6 +765,20 @@ void __init alternative_instructions(void) }#endif +#if defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +/* +* By default, switch to the NUMA-friendly slow path for +* spinlocks when we have multiple NUMA nodes in native environment. +*/ +if ((numa_spinlock_flag ==1 ) || + (numa_spinlock_flag ==0 && nr_node_ids >1 && + pv_ops.lock.queued_spin_lock_slowpath == + native_queued_spin_lock_slowpath)) { + pv_ops.lock.queued_spin_lock_slowpath = + __cna_queued_spin_lock_slowpath; + } +#endif +apply_paravirt (__parainstructions, __parainstructions_end);restart_nmi ();kernel/locking/mcs_spinlock.h +1
-1@@ -17,7 +17,7 @@struct mcs_spinlock {struct mcs_spinlock *next; -int locked;/* 1 if lock acquired */ +unsigned int locked;/* 1 if lock acquired */ int count;/* nesting count, see qspinlock.c */ };kernel/locking/qspinlock.c +30
-4@@ -11,7 +11,7 @@* Peter Zijlstra <[email protected]> */ -#ifndef _GEN_PV_LOCK_SLOWPATH +#if !defined(_GEN_PV_LOCK_SLOWPATH) && !defined(_GEN_CNA_LOCK_SLOWPATH)#include <linux/smp.h> #include <linux/bug.h> @@ -70,7 +70,8 @@/* * On 64-bit architectures, the mcs_spinlock structure will be 16 bytes in * size and four of them will fit nicely in one 64-byte cacheline. For -* pvqspinlock, however, we need more space for extra data. To accommodate +* pvqspinlock, however, we need more space for extra data. The same also +* applies for the NUMA-aware variant of spinlocks (CNA). To accommodate * that, we insert two more long words to pad it up to 32 bytes. IOW, only * two of them can fit in a cacheline in this case. That is OK as it is rare * to have more than 2 levels of slowpath nesting in actual use. We don't @@ -79,7 +80,7 @@*/ struct qnode {struct mcs_spinlock mcs; -#ifdef CONFIG_PARAVIRT_SPINLOCKS +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) || defined(CONFIG_NUMA_AWARE_SPINLOCKS )long reserved[2 ];#endif };@@ -103,6 +104,8 @@ struct qnode {* Exactly fits one 64-byte cacheline on a 64-bit architecture. * * PV doubles the storage and uses the second cacheline for PV state. +* CNA also doubles the storage and uses the second cacheline for +* CNA-specific state. */ static DEFINE_PER_CPU_ALIGNED(struct qnode, qnodes[MAX_NODES]);@@ -316,7 +319,7 @@ static __always_inline void __mcs_pass_lock(struct mcs_spinlock *node,#define try_clear_tail __try_clear_tail#define mcs_pass_lock __mcs_pass_lock -#endif /* _GEN_PV_LOCK_SLOWPATH */ +#endif /* _GEN_PV_LOCK_SLOWPATH && _GEN_CNA_LOCK_SLOWPATH */ /** * queued_spin_lock_slowpath - acquire the queued spinlock @@ -589,6 +592,29 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)EXPORT_SYMBOL (queued_spin_lock_slowpath);/* +* Generate the code for NUMA-aware spinlocks +*/ +#if !defined(_GEN_CNA_LOCK_SLOWPATH) && defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +#define _GEN_CNA_LOCK_SLOWPATH + +#undef pv_wait_head_or_lock +#define pv_wait_head_or_lock cna_pre_scan + +#undef try_clear_tail +#define try_clear_tail cna_try_change_tail + +#undef mcs_pass_lock +#define mcs_pass_lock cna_pass_lock + +#undef queued_spin_lock_slowpath +#define queued_spin_lock_slowpath __cna_queued_spin_lock_slowpath + +#include "qspinlock_cna.h" +#include "qspinlock.c" + +#endif + +/* * Generate the paravirt code for queued_spin_unlock_slowpath(). */ #if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS )kernel/locking/qspinlock_cna.h new file +77
-0@@ -0,0 +1,258 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _GEN_CNA_LOCK_SLOWPATH +#error "do not include this file" +#endif + +#include <linux/topology.h> + +/* +* Implement a NUMA-aware version of MCS (aka CNA, or compact NUMA-aware lock). +* +* In CNA, spinning threads are organized in two queues, a main queue for +* threads running on the same NUMA node as the current lock holder, and a +* secondary queue for threads running on other nodes. Schematically, it +* looks like this: +* +* cna_node +* +----------+ +--------+ +--------+ +* |mcs:next | -> |mcs:next| -> ... |mcs:next| -> NULL [Main queue] +* |mcs:locked| -+ +--------+ +--------+ +* +----------+ | +* +----------------------+ +* \/ +* +--------+ +--------+ +* |mcs:next| -> ... |mcs:next| [Secondary queue] +* +--------+ +--------+ +* ^ | +* +--------------------+ +* +* N.B. locked = 1 if secondary queue is absent. Othewrise, it contains the +* encoded pointer to the tail of the secondary queue, which is organized as a +* circular list. +* +* After acquiring the MCS lock and before acquiring the spinlock, the lock +* holder scans the main queue looking for a thread running on the same node +* (pre-scan). If found (call it thread T), all threads in the main queue +* between the current lock holder and T are moved to the end of the secondary +* queue. If such T is not found, we make another scan of the main queue when +* unlocking the MCS lock (post-scan), starting at the node where pre-scan +* stopped. If both scans fail to find such T, the MCS lock is passed to the +* first thread in the secondary queue. If the secondary queue is empty, the +* lock is passed to the next thread in the main queue. +* +* For more details, see https://urldefense.proofpoint.com/v2/url?u=https-3A__arxiv.org_abs_1810.05600&d=DwICaQ&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=Hvhk3F4omdCk-GE1PTOm3Kn0A7ApWOZ2aZLTuVxFK4k&m=YE_EINNtJ6kvX7-r1fCoSPo_hPkyTTkYRKJ2WSQgURA&s=Hxk5PX1WhsVBJMb5LB-bw_EBT3ZXMbr0G40LbDQWtqg&e= . +* +* Authors: Alex Kogan <[email protected]> +* Dave Dice <[email protected]> +*/ + +struct cna_node { +struct mcs_spinlock mcs; +int numa_node; +u32 encoded_tail; +u32 pre_scan_result;/* 0 or an encoded tail */ +}; + +static void __init cna_init_nodes_per_cpu (unsigned int cpu) +{ +struct mcs_spinlock *base =per_cpu_ptr (&qnodes[0 ].mcs, cpu); +int numa_node =cpu_to_node (cpu); +int i; + +for (i =0 ; i <MAX_NODES ; i++) { +struct cna_node *cn = (struct cna_node *)grab_mcs_node (base, i); + + cn->numa_node = numa_node; + cn->encoded_tail =encode_tail (cpu, i); +/* +* @encoded_tail has to be larger than 1, so we do not confuse +* it with other valid values for @locked or @pre_scan_result +* (0 or 1) +*/ +WARN_ON (cn->encoded_tail <=1 ); + } +} + +static void __init cna_init_nodes (void ) +{kernel/locking/qspinlock_cna.h line 87
Quoted 10 lines
kernel/locking/qspinlock_cna.h new file +10
-0+unsigned int cpu; + +BUILD_BUG_ON (sizeof (struct cna_node ) >sizeof (struct qnode )); +/* we store an ecoded tail word in the node's @locked field */ +BUILD_BUG_ON (sizeof (u32) >sizeof (unsigned int )); + +for_each_possible_cpu (cpu) +cna_init_nodes_per_cpu (cpu); +} +early_initcall (cna_init_nodes);typedef int (*initcall_t)(void);
Quoted 2 lines
So
cna_init_nodes()should return an integer value. You can just return 0 in this case.
I'll fix that, thanks.
Quoted 24 lines
Quoted 24 lines
kernel/locking/qspinlock_cna.h new file +23
-0+ +static inline bool cna_try_change_tail (struct qspinlock *lock,u32 val, +struct mcs_spinlock *node) +{ +struct mcs_spinlock *head_2nd, *tail_2nd; +u32 new; + +/* If the secondary queue is empty, do what MCS does. */ +if (node->locked <=1 ) +return __try_clear_tail (lock, val, node); + +/* +* Try to update the tail value to the last node in the secondary queue. +* If successful, pass the lock to the first thread in the secondary +* queue. Doing those two actions effectively moves all nodes from the +* secondary queue into the main one. +*/ + tail_2nd =decode_tail (node->locked); + head_2nd = tail_2nd->next; + new = ((struct cna_node *)tail_2nd)->encoded_tail + _Q_LOCKED_VAL; + +if (atomic_try_cmpxchg_relaxed (&lock->val, &val, new)) { +/*
Quoted 8 lines
kernel/locking/qspinlock_cna.h line 114
Quoted 4 lines
kernel/locking/qspinlock_cna.h new file +4
-0+* Try to reset @next in tail_2nd to NULL, but no need to check +* the result - if failed, a new successor has updated it. +*/ +cmpxchg64_relaxed (&tail_2nd->next, head_2nd,NULL );Why do you use cmpxchg64*()? That can be problematic when compiling on 32-bit architecture. I think you should just use
cmpxhg_relaxed()for automatic sizing.
That’s right. I will change this.
Thanks,
— Alex
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Waiman Long
reviewerRe: [PATCH v5 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
In reply to Alex KoganQuoted 15 lines · Alex Kogan
kernel/locking/qspinlock_cna.h new file +15
-0+static inline void cna_pass_lock (struct mcs_spinlock *node, +struct mcs_spinlock *next) +{ +struct cna_node *cn = (struct cna_node *)node; +struct mcs_spinlock *next_holder = next, *tail_2nd; +u32 val =1 ; + +u32 scan = cn->pre_scan_result; + +/* +* check if a successor from the same numa node has not been found in +* pre-scan, and if so, try to find it in post-scan starting from the +* node where pre-scan stopped (stored in @pre_scan_result) +*/ +if (scan >0 )
kernel/locking/qspinlock_cna.h line 247
Quoted 10 lines
kernel/locking/qspinlock_cna.h new file +10
-0+ scan =cna_scan_main_queue (node,decode_tail (scan)); + +if (!scan) {/* if found a successor from the same numa node */ + next_holder = node->next; +/* +* make sure @val gets 1 if current holder's @locked is 0 as +* we have to store a non-zero value in successor's @locked +* to pass the lock +*/ + val = node->locked + (node->locked ==0 );
node->locked can be 0 when the cpu enters into an empty MCS queue. We could unconditionally set node->locked to 1 for this case in qspinlock.c or with your above code. Perhaps, a comment about when node->locked will be 0.
It may be easier to understand if you just do
val = node->locked ? node->locked : 1;
Cheers,
Longman
Re: [PATCH v5 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
Quoted 25 lines · Waiman Long
Quoted 21 lines · Alex Kogan
kernel/locking/qspinlock_cna.h new file +21
-0+static inline void cna_pass_lock (struct mcs_spinlock *node, +struct mcs_spinlock *next) +{ +struct cna_node *cn = (struct cna_node *)node; +struct mcs_spinlock *next_holder = next, *tail_2nd; +u32 val =1 ; + +u32 scan = cn->pre_scan_result; + +/* +* check if a successor from the same numa node has not been found in +* pre-scan, and if so, try to find it in post-scan starting from the +* node where pre-scan stopped (stored in @pre_scan_result) +*/ +if (scan >0 ) + scan =cna_scan_main_queue (node,decode_tail (scan)); + +if (!scan) {/* if found a successor from the same numa node */ + next_holder = node->next; +/* +* make sure @val gets 1 if current holder's @locked is 0 as
Quoted 8 lines
kernel/locking/qspinlock_cna.h line 247
Quoted 4 lines
kernel/locking/qspinlock_cna.h new file +4
-0+* we have to store a non-zero value in successor's @locked +* to pass the lock +*/ + val = node->locked + (node->locked ==0 );
node->lockedcan be 0 when the cpu enters into an empty MCS queue. We could unconditionally setnode->lockedto 1 for this case inqspinlock.cor with your above code.
Right, I was doing that in the first two versions of the series. It adds unnecessary store into @locked for non-CNA variants, and even if it does not have any real performance implications, I think Peter did not like that (or, at least, the comment I had to explain why we needed that store).
Quoted 2 lines
Perhaps, a comment about when
node->lockedwill
be 0.
Yeah, I was tinkering with this comment. Here is how it read in v3:
/*
* We unlock a successor by passing a non-zero value,
* so set @val to 1 iff @locked is 0, which will happen
* if we acquired the MCS lock when its queue was empty */
I can change back to something like that if it is better.
Quoted 4 lines
It may be easier to understand if you just do
val = node->locked ? node->locked : 1;
You’re right, that’s another possibility.
However, it adds yet another if-statement on the critical path, which I was trying to avoid that.
Best regards,
— Alex
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Waiman Long
reviewerRe: [PATCH v5 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
Quoted 38 lines · Alex Kogan
Quoted 23 lines · Waiman Long
Quoted 20 lines · Alex Kogan
kernel/locking/qspinlock_cna.h new file +20
-0+static inline void cna_pass_lock (struct mcs_spinlock *node, +struct mcs_spinlock *next) +{ +struct cna_node *cn = (struct cna_node *)node; +struct mcs_spinlock *next_holder = next, *tail_2nd; +u32 val =1 ; + +u32 scan = cn->pre_scan_result; + +/* +* check if a successor from the same numa node has not been found in +* pre-scan, and if so, try to find it in post-scan starting from the +* node where pre-scan stopped (stored in @pre_scan_result) +*/ +if (scan >0 ) + scan =cna_scan_main_queue (node,decode_tail (scan)); + +if (!scan) {/* if found a successor from the same numa node */ + next_holder = node->next; +/* Quoted 8 lines
kernel/locking/qspinlock_cna.h line 247
Quoted 5 lines
kernel/locking/qspinlock_cna.h new file +5
-0+* make sure @val gets 1 if current holder's @locked is 0 as +* we have to store a non-zero value in successor's @locked +* to pass the lock +*/ + val = node->locked + (node->locked ==0 );
node->lockedcan be 0 when the cpu enters into an empty MCS queue. We could unconditionally setnode->lockedto 1 for this case inqspinlock.cor with your above code.Right, I was doing that in the first two versions of the series. It adds unnecessary store into @locked for non-CNA variants, and even if it does not have any real performance implications, I think Peter did not like that (or, at least, the comment I had to explain why we needed that store).
Quoted 2 lines
Perhaps, a comment about when
node->lockedwill
be 0.
Quoted 8 lines
Yeah, I was tinkering with this comment. Here is how it read in v3:
/*
* We unlock a successor by passing a non-zero value,
* so set @val to 1 iff @locked is 0, which will happen
* if we acquired the MCS lock when its queue was empty */I can change back to something like that if it is better.
That looks OK.
Quoted 6 lines
Quoted 3 lines
It may be easier to understand if you just do
val = node->locked ? node->locked : 1;You’re right, that’s another possibility.
However, it adds yet another if-statement on the critical path, which I was trying to avoid that.
Have you compared the generated assembly code if one is better than the other? I am OK with whatever one generates a better code, but often time cmove is used for ?: statements. If the same code is generated, I will prefer an easier to understand statement.
Cheers,
Longman
PATCH v5 4/5 locking/qspinlock: Introduce starvation avoidance into CNA
In reply to Alex KoganKeep track of the number of intra-node lock handoffs, and force inter-node handoff once this number reaches a preset threshold.
--- kernel/locking/qspinlock.c | 3 +++ kernel/locking/qspinlock_cna.h | 30 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-)
kernel/locking/qspinlock.c +3 -0
@@ -597,6 +597,9 @@ EXPORT_SYMBOL(queued_spin_lock_slowpath);#if !defined(_GEN_CNA_LOCK_SLOWPATH) && defined(CONFIG_NUMA_AWARE_SPINLOCKS )#define _GEN_CNA_LOCK_SLOWPATH +#undef pv_init_node +#define pv_init_node cna_init_node +#undef pv_wait_head_or_lock#define pv_wait_head_or_lock cna_pre_scan
kernel/locking/qspinlock_cna.h +27 -3
@@ -50,9 +50,19 @@ struct cna_node {struct mcs_spinlock mcs;int numa_node;u32 encoded_tail; -u32 pre_scan_result;/* 0 or an encoded tail */ +u32 pre_scan_result;/* 0, 1 or an encoded tail */ +u32 intra_count; }; +/* +* Controls the threshold for the number of intra-node lock hand-offs. It can +* be tuned and depend, e.g., on the number of CPUs per node. For now, +* choose a value that provides reasonable long-term fairness without +* sacrificing performance compared to a version that does not have any +* fairness guarantees. +*/ +#define INTRA_NODE_HANDOFF_THRESHOLD (1 << 16) +static void __init cna_init_nodes_per_cpu (unsigned int cpu) {struct mcs_spinlock *base =per_cpu_ptr (&qnodes[0 ].mcs, cpu);
@@ -86,6 +96,11 @@ static void __init cna_init_nodes(void) }early_initcall (cna_init_nodes); +static __always_inline void cna_init_node(struct mcs_spinlock *node) +{ + ((struct cna_node *)node)->intra_count =0 ; +} +static inline bool cna_try_change_tail (struct qspinlock *lock,u32 val,struct mcs_spinlock *node) {
@@ -215,7 +230,13 @@ __always_inline u32 cna_pre_scan(struct qspinlock *lock, {struct cna_node *cn = (struct cna_node *)node; - cn->pre_scan_result =cna_scan_main_queue (node, node); +/* +* setting @pre_scan_result to 1 indicates that no post-scan +* should be made in cna_pass_lock() +*/ + cn->pre_scan_result = + cn->intra_count ==INTRA_NODE_HANDOFF_THRESHOLD ? +1 :cna_scan_main_queue (node, node);return 0 ; }
@@ -234,7 +255,7 @@ static inline void cna_pass_lock(struct mcs_spinlock *node,* pre-scan, and if so, try to find it in post-scan starting from the * node where pre-scan stopped (stored in @pre_scan_result) */ -if (scan >0 ) +if (scan >1 ) scan =cna_scan_main_queue (node,decode_tail (scan));if (!scan) {/* if found a successor from the same numa node */
@@ -245,6 +266,9 @@ static inline void cna_pass_lock(struct mcs_spinlock *node,* to pass the lock */ val = node->locked + (node->locked ==0 ); +/* inc @intra_count if the secondary queue is not empty */ + ((struct cna_node *)next_holder)->intra_count = + cn->intra_count + (node->locked >1 ); }else if (node->locked >1 ) {/* if secondary queue is not empty */ /* next holder will be the first node in the secondary queue */ tail_2nd =decode_tail (node->locked);
Signature · 1 line
2.11.0 (Apple Git-81)
Waiman Long
reviewerRe: [PATCH v5 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 35 lines · Alex Kogan
Keep track of the number of intra-node lock handoffs, and force inter-node handoff once this number reaches a preset threshold.
Signed-off-by Alex Kogan <[email protected]>Reviewed-by Steve Sistare <[email protected]>--- kernel/locking/qspinlock.c | 3 +++ kernel/locking/qspinlock_cna.h | 30 +++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-)kernel/locking/qspinlock.c +3
-0@@ -597,6 +597,9 @@ EXPORT_SYMBOL(queued_spin_lock_slowpath);#if !defined(_GEN_CNA_LOCK_SLOWPATH) && defined(CONFIG_NUMA_AWARE_SPINLOCKS )#define _GEN_CNA_LOCK_SLOWPATH +#undef pv_init_node +#define pv_init_node cna_init_node +#undef pv_wait_head_or_lock#define pv_wait_head_or_lock cna_pre_scankernel/locking/qspinlock_cna.h +2
-1@@ -50,9 +50,19 @@ struct cna_node {struct mcs_spinlock mcs;int numa_node;u32 encoded_tail; -u32 pre_scan_result;/* 0 or an encoded tail */ +u32 pre_scan_result;/* 0, 1 or an encoded tail */ +u32 intra_count;
kernel/locking/qspinlock_cna.h struct cna_node { · line 64
Quoted 10 lines
kernel/locking/qspinlock_cna.h +8
-0}; +/* +* Controls the threshold for the number of intra-node lock hand-offs. It can +* be tuned and depend, e.g., on the number of CPUs per node. For now, +* choose a value that provides reasonable long-term fairness without +* sacrificing performance compared to a version that does not have any +* fairness guarantees. +*/ +#define INTRA_NODE_HANDOFF_THRESHOLD (1 << 16)
I think 64k is too high. I will be more comfortable with a number like (1 << 8). The worst case latency for a lock waiter from the other node is just not acceptable.
Cheers,
Longman
PATCH v5 5/5 locking/qspinlock: Introduce the shuffle reduction optimization into CNA
In reply to Alex KoganThis optimization reduces the probability threads will be shuffled between the main and secondary queues when the secondary queue is empty. It is helpful when the lock is only lightly contended.
--- kernel/locking/qspinlock_cna.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
kernel/locking/qspinlock_cna.h +30 -0
@@ -4,6 +4,7 @@#endif #include <linux/topology.h> +#include <linux/random.h> /* * Implement a NUMA-aware version of MCS (aka CNA, or compact NUMA-aware lock).
@@ -63,6 +64,34 @@ struct cna_node {*/ #define INTRA_NODE_HANDOFF_THRESHOLD (1 << 16) +/* +* Controls the probability for enabling the scan of the main queue when +* the secondary queue is empty. The chosen value reduces the amount of +* unnecessary shuffling of threads between the two waiting queues when +* the contention is low, while responding fast enough and enabling +* the shuffling when the contention is high. +*/ +#define SHUFFLE_REDUCTION_PROB_ARG (7) + +/* Per-CPU pseudo-random number seed */ +static DEFINE_PER_CPU(u32, seed); + +/* +* Return false with probability 1 / 2^@num_bits. +* Intuitively, the larger @num_bits the less likely false is to be returned. +* @num_bits must be a number between 0 and 31. +*/ +static bool probably (unsigned int num_bits) +{ +u32 s; + + s =this_cpu_read (seed); + s =next_pseudo_random32 (s); +this_cpu_write (seed, s); + +return s & ((1 << num_bits) -1 ); +} +static void __init cna_init_nodes_per_cpu (unsigned int cpu) {struct mcs_spinlock *base =per_cpu_ptr (&qnodes[0 ].mcs, cpu);
@@ -235,6 +264,7 @@ __always_inline u32 cna_pre_scan(struct qspinlock *lock,* should be made in cna_pass_lock() */ cn->pre_scan_result = + (node->locked <=1 &&probably (SHUFFLE_REDUCTION_PROB_ARG )) || cn->intra_count ==INTRA_NODE_HANDOFF_THRESHOLD ?1 :cna_scan_main_queue (node, node);
Signature · 1 line
2.11.0 (Apple Git-81)
Waiman Long
reviewerQuoted 139 lines · Alex Kogan
Changes from v4:
----------------- Switch to a deterministic bound on the number of intra-node handoffs, as suggested by Longman.
- Scan the main queue after acquiring the MCS lock and before acquiring the spinlock (pre-scan), as suggested by Longman. If no thread is found in pre-scan, try again after acquiring the spinlock, resuming from the same place where pre-scan stopped.
- Convert the secondary queue to a cyclic list such that the tail’s @next points to the head of the queue. Store the pointer to the secondary queue tail (rather than head) in @locked. This eliminates the need for the @tail field in CNA nodes, making space for fields required by the two changes above.
- Change
arch_mcs_spin_lock_contended()toarch_mcs_spin_lock(), and fix misuse of old macro names, as suggested by Hanjun.Summary
-------Lock throughput can be increased by handing a lock to a waiter on the same NUMA node as the lock holder, provided care is taken to avoid starvation of waiters on other NUMA nodes. This patch introduces CNA (compact NUMA-aware lock) as the slow path for qspinlock. It is enabled through a configuration option (
NUMA_AWARE_SPINLOCKS).CNA is a NUMA-aware version of the MCS lock. Spinning threads are organized in two queues, a main queue for threads running on the same node as the current lock holder, and a secondary queue for threads running on other nodes. Threads store the ID of the node on which they are running in their queue nodes. After acquiring the MCS lock and before acquiring the spinlock, the lock holder scans the main queue looking for a thread running on the same node (pre-scan). If found (call it thread T), all threads in the main queue between the current lock holder and T are moved to the end of the secondary queue. If such T is not found, we make another scan of the main queue after acquiring the spinlock when unlocking the MCS lock (post-scan), starting at the node where pre-scan stopped. If both scans fail to find such T, the MCS lock is passed to the first thread in the secondary queue. If the secondary queue is empty, the MCS lock is passed to the next thread in the main queue. To avoid starvation of threads in the secondary queue, those threads are moved back to the head of the main queue after a certain number of intra-node lock hand-offs.
More details are available at https://arxiv.org/abs/1810.05600.
We have done some performance evaluation with the locktorture module as well as with several benchmarks from the will-it-scale repo. The following locktorture results are from an Oracle X5-4 server (four Intel Xeon E7-8895 v3 @ 2.60GHz sockets with 18 hyperthreaded cores each). Each number represents an average (over 25 runs) of the total number of ops (x10^7) reported at the end of each run. The standard deviation is also reported in (), and in general is about 3% from the average. The 'stock' kernel is v5.4.0-rc1,
commitd90f2df63c5c, compiled in the default configuration. 'patch-CNA' is the modified kernel withNUMA_AWARE_SPINLOCKSset; the speedup is calculated dividing 'patch-CNA' by 'stock'.#thr stock patch-CNA speedup (patch-CNA/stock)
1 2.674 (0.118) 2.736 (0.119) 1.023 2 2.588 (0.141) 2.603 (0.108) 1.006 4 4.230 (0.120) 4.220 (0.127) 0.998 8 5.362 (0.181) 6.679 (0.182) 1.246 16 6.639 (0.133) 8.050 (0.200) 1.213 32 7.359 (0.149) 8.792 (0.168) 1.195 36 7.443 (0.142) 8.873 (0.230) 1.192 72 6.554 (0.147) 9.317 (0.158) 1.421 108 6.156 (0.093) 9.404 (0.191) 1.528 142 5.659 (0.093) 9.361 (0.184) 1.654The following tables contain throughput results (ops/us) from the same setup for will-it-scale/open1_threads:
#thr stock patch-CNA speedup (patch-CNA/stock)
1 0.532 (0.002) 0.532 (0.003) 1.000 2 0.785 (0.024) 0.779 (0.025) 0.992 4 1.426 (0.018) 1.409 (0.021) 0.988 8 1.779 (0.101) 1.711 (0.127) 0.962 16 1.761 (0.093) 1.671 (0.104) 0.949 32 0.935 (0.063) 1.619 (0.093) 1.731 36 0.936 (0.082) 1.591 (0.086) 1.699 72 0.839 (0.043) 1.667 (0.097) 1.988 108 0.842 (0.035) 1.701 (0.091) 2.021 142 0.830 (0.037) 1.714 (0.098) 2.066and will-it-scale/lock2_threads:
#thr stock patch-CNA speedup (patch-CNA/stock)
1 1.555 (0.009) 1.577 (0.002) 1.014 2 2.644 (0.060) 2.682 (0.062) 1.014 4 5.159 (0.205) 5.197 (0.231) 1.007 8 4.302 (0.221) 4.279 (0.318) 0.995 16 4.259 (0.111) 4.087 (0.163) 0.960 32 2.583 (0.112) 4.077 (0.120) 1.578 36 2.499 (0.106) 4.076 (0.106) 1.631 72 1.979 (0.085) 4.077 (0.123) 2.061 108 2.096 (0.090) 4.043 (0.130) 1.929 142 1.913 (0.109) 3.984 (0.108) 2.082Our evaluation shows that CNA also improves performance of user applications that have hot pthread mutexes. Those mutexes are blocking, and waiting threads park and unpark via the futex mechanism in the kernel. Given that kernel futex chains, which are hashed by the mutex address, are each protected by a chain-specific spin lock, the contention on a user-mode mutex translates into contention on a kernel level spinlock.
Here are the results for the leveldb ‘readrandom’ benchmark:
#thr stock patch-CNA speedup (patch-CNA/stock)
1 0.532 (0.007) 0.535 (0.015) 1.006 2 0.665 (0.030) 0.673 (0.034) 1.011 4 0.715 (0.023) 0.716 (0.026) 1.002 8 0.686 (0.023) 0.686 (0.024) 1.001 16 0.719 (0.030) 0.737 (0.025) 1.025 32 0.740 (0.034) 0.959 (0.105) 1.296 36 0.730 (0.024) 1.079 (0.112) 1.478 72 0.652 (0.018) 1.160 (0.024) 1.778 108 0.622 (0.016) 1.157 (0.028) 1.860 142 0.600 (0.015) 1.145 (0.035) 1.908Additional performance numbers are available in previous revisions of the series.
Further comments are welcome and appreciated.
Alex Kogan (5):
locking/qspinlock: Rename mcs lock/unlock macros and make them more
generic
locking/qspinlock: Refactor the qspinlock slow path locking/qspinlock: Introduce CNA into the slow path of qspinlock locking/qspinlock: Introduce starvation avoidance into CNA locking/qspinlock: Introduce the shuffle reduction optimization into
CNA
Quoted 12 lines
arch/arm/include/asm/mcs_spinlock.h | 6 +- arch/x86/Kconfig | 19 +++ arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 41 +++++ include/asm-generic/mcs_spinlock.h | 4 +- kernel/locking/mcs_spinlock.h | 20 +-- kernel/locking/qspinlock.c | 77 ++++++++- kernel/locking/qspinlock_cna.h | 312 ++++++++++++++++++++++++++++++++++++ kernel/locking/qspinlock_paravirt.h | 2 +- 9 files changed, 462 insertions(+), 23 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.h
I have reviewed this patchset. Asides from a few issues I had raised in earlier emails, I don't see other problems in the code. Thanks for your hard work. I think we are almost there.
Cheers,
Longman