Add NUMA-awareness to qspinlock
Subsystems (5)
v8 cover Add NUMA-awareness to qspinlock
Minor changes from v7 based on feedback from Longman:
-----------------------------------------------------
- Move __init functions from alternative.c to qspinlock_cna.h
- Introduce enum for return values from cna_pre_scan(), for better readability.
- Add/revise a few comments to improve readability.
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.
The series applies on top of v5.5.0-rc2, commit ea200dec51. 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
.../admin-guide/kernel-parameters.txt | 18 + arch/arm/include/asm/mcs_spinlock.h | 6 +- arch/x86/Kconfig | 20 + arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 4 + include/asm-generic/mcs_spinlock.h | 4 +- kernel/locking/mcs_spinlock.h | 20 +- kernel/locking/qspinlock.c | 82 +++- kernel/locking/qspinlock_cna.h | 400 ++++++++++++++++++ kernel/locking/qspinlock_paravirt.h | 2 +- 10 files changed, 537 insertions(+), 23 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.h
Signature · 1 line
2.21.0 (Apple Git-122.2)
PATCH v8 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.21.0 (Apple Git-122.2)
PATCH v8 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.21.0 (Apple Git-122.2)
PATCH v8 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").
--- .../admin-guide/kernel-parameters.txt | 10 + arch/x86/Kconfig | 20 ++ arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 4 + kernel/locking/mcs_spinlock.h | 2 +- kernel/locking/qspinlock.c | 39 ++- kernel/locking/qspinlock_cna.h | 319 ++++++++++++++++++ 7 files changed, 393 insertions(+), 5 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.h
Documentation/admin-guide/kernel-parameters.txt +10 -0
@@ -3190,6 +3190,16 @@ nox2apic [X86-64,APIC] Do not enable x2APIC mode. + numa_spinlock= [NUMA, PV_OPS] Select the NUMA-aware variant + of spinlock. The options are: + auto - Enable this variant if running on a multi-node + machine in native environment. + on - Unconditionally enable this variant. + off - Unconditionally disable this variant. + + Not specifying this option is equivalent to + numa_spinlock=auto. + cpu0_hotplug [X86] Turn on CPU0 hotplug feature when CONFIG_BOOTPARAM_HOTPLUG_CPU0 is off. Some features depend on CPU0. Known dependencies are:
arch/x86/Kconfig +20 -0
@@ -1562,6 +1562,26 @@ config NUMA Otherwise, you should say N. +config NUMA_AWARE_SPINLOCKS + bool "Numa-aware spinlocks" + depends on NUMA + depends on QUEUED_SPINLOCKS + depends on 64BIT + # 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_configure_spin_lock_slowpath (void ); +#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 +4 -0
@@ -738,6 +738,10 @@ void __init alternative_instructions(void) }#endif +#if defined(CONFIG_NUMA_AWARE_SPINLOCKS ) +cna_configure_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 +35 -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
@@ -588,6 +591,34 @@ 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 +/* +* defer defining queued_spin_lock_slowpath until after the include to +* avoid a name clash with the identically named field in pv_ops.lock +* (see cna_configure_spin_lock_slowpath()) +*/ +#include "qspinlock_cna.h" +#define queued_spin_lock_slowpath __cna_queued_spin_lock_slowpath + +#include "qspinlock.c" + +#endif +/* * Generate the paravirt code for queued_spin_unlock_slowpath(). */
kernel/locking/qspinlock_cna.h new file +319 -0
319 lines not on this page · raw message
Signature · 1 line
2.21.0 (Apple Git-122.2)
Waiman Long
reviewerRe: [PATCH v8 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
Quoted 6 lines · Alex Kogan
+/*
+ *cna_scan_main_queue- scan the main waiting queue looking for the first
+ * thread running on the same NUMA node as the lock holder. If found (call it
+ * thread T), move all threads in the main queue between the lock holder and
+ * T to the end of the secondary queue and return 0
+ * (=SUCCESSOR_FROM_SAME_NUMA_NODE_FOUND); otherwise, return the encoded
Are you talking about LOCAL_WAITER_FOUND?
Quoted 61 lines
+ * pointer of the last scanned node in the primary queue (so a subsequent scan
+ * can be resumed from that node).
+ *
+ * Schematically, this may look like the following (nn stands fornuma_nodeand
+ * et stands forencoded_tail).
+ *
+ * whencna_scan_main_queue()is called (the secondary queue is empty):
+ *
+ * A+------------+ B+--------+ C+--------+ T+--------++ * |mcs:next | -> |mcs:next| -> |mcs:next| -> |mcs:next| -> NULL + * |mcs:locked=1| |cna:nn=0| |cna:nn=2| |cna:nn=1| + * |cna:nn=1 | +--------+ +--------+ +--------+ + * +----------- ++ *
+ * whencna_scan_main_queue()returns (the secondary queue contains B and C):
+ *
+ * A+----------------+ T+--------++ * |mcs:next | -> |mcs:next| -> NULL + * |mcs:locked=C.et | -+ |cna:nn=1| + * |cna:nn=1 | | +--------+ + * +--------------- + +-----+ + * \/ + * B+--------+ C+--------+ + * |mcs:next| -> |mcs:next| -+ + * |cna:nn=0| |cna:nn=2| | + * +--------+ +--------+ |+ * ^ |
+ * +---------------------+
+ *
+ * The worst case complexity of the scan is O(n), where n is the number
+ * of current waiters. However, the amortized complexity is close to O(1),
+ * as the immediate successor is likely to be running on the same node once
+ * threads from other nodes are moved to the secondary queue.
+ *
+ * @node : Pointer to the MCS node of the lock holder
+ * @pred_start: Pointer to the MCS node of the waiter whose successor should be
+ * the first node in the scan
+ * Return :LOCAL_WAITER_FOUNDor encoded tail of the last scanned waiter
+ */kernel/locking/qspinlock_cna.h new file +22
-0+static u32 cna_scan_main_queue (struct mcs_spinlock *node, +struct mcs_spinlock *pred_start) +{ +struct cna_node *cn = (struct cna_node *)node; +struct cna_node *cni = (struct cna_node *)READ_ONCE (pred_start->next); +struct cna_node *last; +int my_numa_node = cn->numa_node; + +/* find any next waiter on 'our' NUMA node */ +for (last = cn; + cni && cni->numa_node != my_numa_node; + last = cni, cni = (struct cna_node *)READ_ONCE (cni->mcs.next)) + ; + +/* if found, splice any skipped waiters onto the secondary queue */ +if (cni) { +if (last != cn)/* did we skip any waiters? */ +cna_splice_tail (node, node->next, + (struct mcs_spinlock *)last); +return LOCAL_WAITER_FOUND ; + } +
kernel/locking/qspinlock_cna.h line 308
Quoted 10 lines
kernel/locking/qspinlock_cna.h new file +9
-0+return last->encoded_tail; +} + +/* +* Switch to the NUMA-friendly slow path for spinlocks when we have +* multiple NUMA nodes in native environment, unless the user has +* overridden this default behavior by setting the numa_spinlock flag. +*/ +void cna_configure_spin_lock_slowpath (void )
Nit: There should be a __init.
kernel/locking/qspinlock_cna.h line 319
Quoted 11 lines
kernel/locking/qspinlock_cna.h new file +11
-0+{ +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; + +pr_info ("Enabling CNA spinlock\n" ); + } +}
Other than these two minor nits, the rests looks good to me.
Cheers,
Longman
Re: [PATCH v8 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
Quoted 12 lines · Waiman Long
Quoted 6 lines · Alex Kogan
+/*
+ *cna_scan_main_queue- scan the main waiting queue looking for the first
+ * thread running on the same NUMA node as the lock holder. If found (call it
+ * thread T), move all threads in the main queue between the lock holder and
+ * T to the end of the secondary queue and return 0
+ * (=SUCCESSOR_FROM_SAME_NUMA_NODE_FOUND); otherwise, return the encodedAre you talking about
LOCAL_WAITER_FOUND?
Ahh, yes — good catch!
Quoted 65 lines
Quoted 65 lines
+ * pointer of the last scanned node in the primary queue (so a subsequent scan
+ * can be resumed from that node).
+ *
+ * Schematically, this may look like the following (nn stands fornuma_nodeand
+ * et stands forencoded_tail).
+ *
+ * whencna_scan_main_queue()is called (the secondary queue is empty):
+ *
+ * A+------------+ B+--------+ C+--------+ T+--------++ * |mcs:next | -> |mcs:next| -> |mcs:next| -> |mcs:next| -> NULL + * |mcs:locked=1| |cna:nn=0| |cna:nn=2| |cna:nn=1| + * |cna:nn=1 | +--------+ +--------+ +--------+ + * +----------- ++ *
+ * whencna_scan_main_queue()returns (the secondary queue contains B and C):
+ *
+ * A+----------------+ T+--------++ * |mcs:next | -> |mcs:next| -> NULL + * |mcs:locked=C.et | -+ |cna:nn=1| + * |cna:nn=1 | | +--------+ + * +--------------- + +-----+ + * \/ + * B+--------+ C+--------+ + * |mcs:next| -> |mcs:next| -+ + * |cna:nn=0| |cna:nn=2| | + * +--------+ +--------+ |+ * ^ |
+ * +---------------------+
+ *
+ * The worst case complexity of the scan is O(n), where n is the number
+ * of current waiters. However, the amortized complexity is close to O(1),
+ * as the immediate successor is likely to be running on the same node once
+ * threads from other nodes are moved to the secondary queue.
+ *
+ * @node : Pointer to the MCS node of the lock holder
+ * @pred_start: Pointer to the MCS node of the waiter whose successor should be
+ * the first node in the scan
+ * Return :LOCAL_WAITER_FOUNDor encoded tail of the last scanned waiter
+ */kernel/locking/qspinlock_cna.h new file +25
-0+static u32 cna_scan_main_queue (struct mcs_spinlock *node, +struct mcs_spinlock *pred_start) +{ +struct cna_node *cn = (struct cna_node *)node; +struct cna_node *cni = (struct cna_node *)READ_ONCE (pred_start->next); +struct cna_node *last; +int my_numa_node = cn->numa_node; + +/* find any next waiter on 'our' NUMA node */ +for (last = cn; + cni && cni->numa_node != my_numa_node; + last = cni, cni = (struct cna_node *)READ_ONCE (cni->mcs.next)) + ; + +/* if found, splice any skipped waiters onto the secondary queue */ +if (cni) { +if (last != cn)/* did we skip any waiters? */ +cna_splice_tail (node, node->next, + (struct mcs_spinlock *)last); +return LOCAL_WAITER_FOUND ; + } + +return last->encoded_tail; +} +
Quoted 7 lines
kernel/locking/qspinlock_cna.h line 308
Quoted 6 lines
kernel/locking/qspinlock_cna.h new file +6
-0+/* +* Switch to the NUMA-friendly slow path for spinlocks when we have +* multiple NUMA nodes in native environment, unless the user has +* overridden this default behavior by setting the numa_spinlock flag. +*/ +void cna_configure_spin_lock_slowpath (void )Nit: There should be a
__init.
True. I will fix that.
Quoted 5 lines
Quoted 5 lines
kernel/locking/qspinlock_cna.h new file +5
-0+{ +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)) {
Quoted 8 lines
kernel/locking/qspinlock_cna.h line 319
Quoted 6 lines
kernel/locking/qspinlock_cna.h new file +6
-0+ pv_ops.lock.queued_spin_lock_slowpath = + __cna_queued_spin_lock_slowpath; + +pr_info ("Enabling CNA spinlock\n" ); + } +}Other than these two minor nits, the rests looks good to me.
Great. I will revise and resubmit.
Best regards,
— Alex
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Peter Zijlstra
maintainerRe: [PATCH v8 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
In reply to Alex KoganQuoted 1 line · Alex Kogan
+#define
try_clear_tailcna_try_change_tail
That's inconsistent; please run 's/cna_try_change_tail/cna_try_clear_tail/g' on your patch.
Peter Zijlstra
maintainerRe: [PATCH v8 3/5] locking/qspinlock: Introduce CNA into the slow path of qspinlock
In reply to Alex KoganQuoted 1 line · Alex Kogan
+#define
pv_wait_head_or_lockcna_pre_scan
Also inconsitent naming.
kernel/locking/qspinlock_cna.h line 239
Quoted 9 lines
kernel/locking/qspinlock_cna.h new file +9
-0+__always_inline u32 cna_pre_scan(struct qspinlock *lock, +struct mcs_spinlock *node) +{ +struct cna_node *cn = (struct cna_node *)node; + + cn->pre_scan_result =cna_scan_main_queue (node, node); + +return 0 ; +}
The thinking here is that we're trying to make use of the time otherwise spend spinning on atomic_cond_read_acquire(), to search for a potential unlock candidate?
Surely that deserves a comment.
PATCH v8 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. The default value for the threshold can be overridden with the new kernel boot command-line option "numa_spinlock_threshold".
--- .../admin-guide/kernel-parameters.txt | 8 ++++ kernel/locking/qspinlock.c | 3 ++ kernel/locking/qspinlock_cna.h | 41 ++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-)
Documentation/admin-guide/kernel-parameters.txt +8 -0
@@ -3200,6 +3200,14 @@ Not specifying this option is equivalent to numa_spinlock=auto. + numa_spinlock_threshold= [NUMA, PV_OPS] + Set the threshold for the number of intra-node + lock hand-offs before the NUMA-aware spinlock + is forced to be passed to a thread on another NUMA node. + Valid values are in the [0..31] range. Smaller values + result in a more fair, but less performant spinlock, and + vice versa. The default value is 16. + cpu0_hotplug [X86] Turn on CPU0 hotplug feature when CONFIG_BOOTPARAM_HOTPLUG_CPU0 is off. Some features depend on CPU0. Known dependencies are:
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 +40 -1
75 lines not on this page · raw message
Signature · 1 line
2.21.0 (Apple Git-122.2)
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 62 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. The default value for the threshold can be overridden with the new kernel boot command-line option "
numa_spinlock_threshold".Signed-off-by Alex Kogan <[email protected]>Reviewed-by Steve Sistare <[email protected]>--- .../admin-guide/kernel-parameters.txt | 8 ++++ kernel/locking/qspinlock.c | 3 ++ kernel/locking/qspinlock_cna.h | 41 ++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-)Documentation/admin-guide/kernel-parameters.txt +8
-0@@ -3200,6 +3200,14 @@ Not specifying this option is equivalent to numa_spinlock=auto. + numa_spinlock_threshold= [NUMA, PV_OPS] + Set the threshold for the number of intra-node + lock hand-offs before the NUMA-aware spinlock + is forced to be passed to a thread on another NUMA node. + Valid values are in the [0..31] range. Smaller values + result in a more fair, but less performant spinlock, and + vice versa. The default value is 16. + cpu0_hotplug [X86] Turn on CPU0 hotplug feature when CONFIG_BOOTPARAM_HOTPLUG_CPU0 is off. Some features depend on CPU0. Known dependencies are: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
-0@@ -51,13 +51,25 @@ struct cna_node {int numa_node;u32 encoded_tail;u32 pre_scan_result;/* encoded tail or enum val */ +u32 intra_count; };enum {LOCAL_WAITER_FOUND =2 ,/* 0 and 1 are reserved for @locked */ +FLUSH_SECONDARY_QUEUE =3 ,MIN_ENCODED_TAIL };
kernel/locking/qspinlock_cna.h struct cna_node { · line 71
Quoted 10 lines
kernel/locking/qspinlock_cna.h +9
-0+/* +* Controls the threshold for the number of intra-node lock hand-offs before +* the NUMA-aware variant of spinlock is forced to be passed to a thread on +* another NUMA node. By default, the chosen value provides reasonable +* long-term fairness without sacrificing performance compared to a lock +* that does not have any fairness guarantees. The default setting can +* be changed with the "numa_spinlock_threshold" boot option. +*/ +int intra_node_handoff_threshold__ro_after_init =1 <<16 ;
Another minor nit. (1 << 31) is negative for an int value. I will suggest that you either make intra_node_handoff_threshold an unsigned int or limit the upper bound to 30.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
In reply to Alex Kogankernel/locking/qspinlock_cna.h struct cna_node { · line 71
Quoted 9 lines · Alex Kogan
kernel/locking/qspinlock_cna.h +9
-0+/* +* Controls the threshold for the number of intra-node lock hand-offs before +* the NUMA-aware variant of spinlock is forced to be passed to a thread on +* another NUMA node. By default, the chosen value provides reasonable +* long-term fairness without sacrificing performance compared to a lock +* that does not have any fairness guarantees. The default setting can +* be changed with the "numa_spinlock_threshold" boot option. +*/ +int intra_node_handoff_threshold__ro_after_init =1 <<16 ;
There is a distinct lack of quantitative data to back up that 'reasonable' claim there.
Where is the table of inter-node latencies observed for the various values tested, and on what criteria is this number deemed reasonable?
To me, 64k lock hold times seems like a giant number, entirely outside of reasonable.
Quoted 17 lines
kernel/locking/qspinlock_cna.h +6
-0+static void __init cna_init_nodes_per_cpu (unsigned int cpu) {struct mcs_spinlock *base =per_cpu_ptr (&qnodes[0 ].mcs, cpu);@@ -97,6 +109,11 @@ static int __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 ; +} +/* this function is called only when the primary queue is empty */ static inline bool cna_try_change_tail (struct qspinlock *lock,u32 val,struct mcs_spinlock *node)
kernel/locking/qspinlock_cna.h __always_inline u32 cna_pre_scan(struct qspinlock *lock, · line 255
Quoted 7 lines
kernel/locking/qspinlock_cna.h +3
-1@@ -233,7 +250,9 @@ __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); + cn->pre_scan_result = + cn->intra_count == intra_node_handoff_threshold ? +FLUSH_SECONDARY_QUEUE :cna_scan_main_queue (node, node);
Because:
if (cn->intra_count < intra_node_handoff_threshold) cn->pre_scan_result = cna_scan_main_queue(node, node); else cn->pre_scan_result = FLUSH_SECONDARY_QUEUE;
was too readable?
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 15 lines · Peter Zijlstra
kernel/locking/qspinlock_cna.h struct cna_node { · line 71
Quoted 9 lines · Alex Kogan
kernel/locking/qspinlock_cna.h +9
-0+/* +* Controls the threshold for the number of intra-node lock hand-offs before +* the NUMA-aware variant of spinlock is forced to be passed to a thread on +* another NUMA node. By default, the chosen value provides reasonable +* long-term fairness without sacrificing performance compared to a lock +* that does not have any fairness guarantees. The default setting can +* be changed with the "numa_spinlock_threshold" boot option. +*/ +int intra_node_handoff_threshold__ro_after_init =1 <<16 ;There is a distinct lack of quantitative data to back up that 'reasonable' claim there.
Quoted 5 lines
Where is the table of inter-node latencies observed for the various values tested, and on what criteria is this number deemed reasonable?
To me, 64k lock hold times seems like a giant number, entirely outside of reasonable.
Daniel, IIRC you just did a paper on constructing worst case latencies from measuring pieces. Do you have data on average lock hold times?
Re: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 15 lines · Peter Zijlstra
Quoted 14 lines · Peter Zijlstra
kernel/locking/qspinlock_cna.h struct cna_node { · line 71
Quoted 9 lines · Alex Kogan
kernel/locking/qspinlock_cna.h +9
-0+/* +* Controls the threshold for the number of intra-node lock hand-offs before +* the NUMA-aware variant of spinlock is forced to be passed to a thread on +* another NUMA node. By default, the chosen value provides reasonable +* long-term fairness without sacrificing performance compared to a lock +* that does not have any fairness guarantees. The default setting can +* be changed with the "numa_spinlock_threshold" boot option. +*/ +int intra_node_handoff_threshold__ro_after_init =1 <<16 ;There is a distinct lack of quantitative data to back up that 'reasonable' claim there.
Quoted 8 lines
Quoted 5 lines
Where is the table of inter-node latencies observed for the various values tested, and on what criteria is this number deemed reasonable?
To me, 64k lock hold times seems like a giant number, entirely outside of reasonable.
Daniel, IIRC you just did a paper on constructing worst case latencies from measuring pieces. Do you have data on average lock hold times?
I am still writing the paper, but I do not have the (avg) lock times. It is it is in the TODO list, though!
-- Daniel
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
In reply to Peter ZijlstraQuoted 14 lines · Peter Zijlstra
kernel/locking/qspinlock_cna.h struct cna_node { · line 71
Quoted 9 lines · Alex Kogan
kernel/locking/qspinlock_cna.h +9
-0+/* +* Controls the threshold for the number of intra-node lock hand-offs before +* the NUMA-aware variant of spinlock is forced to be passed to a thread on +* another NUMA node. By default, the chosen value provides reasonable +* long-term fairness without sacrificing performance compared to a lock +* that does not have any fairness guarantees. The default setting can +* be changed with the "numa_spinlock_threshold" boot option. +*/ +int intra_node_handoff_threshold__ro_after_init =1 <<16 ;There is a distinct lack of quantitative data to back up that 'reasonable' claim there.
Quoted 5 lines
Where is the table of inter-node latencies observed for the various values tested, and on what criteria is this number deemed reasonable?
To me, 64k lock hold times seems like a giant number, entirely outside of reasonable.
I actually had similar question before, but having the capability of changing the default with boot time parameter alleviate some of my concern. I will certainly like to see actual data on how different values will affect the performance of the code.
Cheers,
Longman
A message that is not in the archive (sent off-list or not yet received): [email protected]
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 1 line · Alex Kogan
Let me put this question to you. What do you think the number should be?
I think it would be very good to keep the inter-node latency below 1ms.
But to realize that we need data on the lock hold times. Specifically for the heavily contended locks that make CNA worth it in the first place.
I don't see that data, so I don't see how we can argue about this let alone call something reasonable.
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 3 lines · Peter Zijlstra
Quoted 1 line · Alex Kogan
Let me put this question to you. What do you think the number should be?
I think it would be very good to keep the inter-node latency below 1ms.
It is hard to guarantee that given that lock hold times can vary quite a lot depending on the workload. What we can control is just how many later lock waiters can jump ahead before a given waiter.
Quoted 7 lines
But to realize that we need data on the lock hold times. Specifically for the heavily contended locks that make CNA worth it in the first place.
I don't see that data, so I don't see how we can argue about this let alone call something reasonable.
In essence, CNA lock is for improving throughput on NUMA machines at the expense of increasing worst case latency. If low latency is important, it should be disabled. If CONFIG_PREEMPT_RT is on, CONFIG_NUMA_AWARE_SPINLOCKS should be off.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 7 lines · Waiman Long
Quoted 3 lines · Peter Zijlstra
Quoted 1 line · Alex Kogan
Let me put this question to you. What do you think the number should be?
I think it would be very good to keep the inter-node latency below 1ms.
It is hard to guarantee that given that lock hold times can vary quite a lot depending on the workload. What we can control is just how many later lock waiters can jump ahead before a given waiter.
We're not into this for easy. And exactly because it depends on a lot we need a lot of data.
Worst case lock acquisition times directly translate into worst case IRQ-off latencies, and even the most die hard throughput oriented workloads don't like to experience multiple ticks worth of irq-off latencies.
Quoted 9 lines
Quoted 7 lines
But to realize that we need data on the lock hold times. Specifically for the heavily contended locks that make CNA worth it in the first place.
I don't see that data, so I don't see how we can argue about this let alone call something reasonable.
In essence, CNA lock is for improving throughput on NUMA machines at the expense of increasing worst case latency. If low latency is important,
Latency is _always_ important. Otherwise we'd never have put so much time and effort into fair locks to begin with. Unbounded latency sucks unconditionally.
Quoted 2 lines
it should be disabled. If
CONFIG_PREEMPT_RTis on,CONFIG_NUMA_AWARE_SPINLOCKSshould be off.
You're spouting nonsense. You cannot claim any random number is reasonable without argument.
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
In reply to Waiman LongQuoted 11 lines · Waiman Long
Quoted 3 lines · Peter Zijlstra
Quoted 1 line · Alex Kogan
Let me put this question to you. What do you think the number should be?
I think it would be very good to keep the inter-node latency below 1ms.
It is hard to guarantee that given that lock hold times can vary quite a lot depending on the workload. What we can control is just how many later lock waiters can jump ahead before a given waiter.
Quoted 4 lines
But to realize that we need data on the lock hold times. Specifically for the heavily contended locks that make CNA worth it in the first place.
Quoted 7 lines
Quoted 3 lines
I don't see that data, so I don't see how we can argue about this let alone call something reasonable.
In essence, CNA lock is for improving throughput on NUMA machines at the expense of increasing worst case latency. If low latency is important, it should be disabled. If
CONFIG_PREEMPT_RTis on,CONFIG_NUMA_AWARE_SPINLOCKSshould be off.
Actually, what we are worrying about is the additional latency that can be added to important tasks or execution contexts that are waiting for a lock. Maybe we can make CNA lock behaves somewhat like qrwlock is that requests from interrupt context are giving priority. We could add a priority flag in the CNA node. If the flag is set, we will never put it into the secondary queue. In fact, we can transfer control next to it even if it is not on the same node. We may also set the priority flag if it is a RT task that is trying to acquire the lock.
In this way, we can guarantee that important tasks or contexts will not suffer a delay in acquiring the lock. Those less important tasks, however, may need to wait a bit longer before they can get the lock.
What do you guys think about that?
Regards,
Longman
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 68 lines · Waiman Long
Quoted 67 lines · Alex Kogan
Quoted 67 lines
On Jan 24, 2020, at 11:46 AM, Waiman Long <[email protected] <mailto:[email protected]>> wrote:
Quoted 51 lines · Alex Kogan
Quoted 16 lines
On Jan 24, 2020, at 10:19 AM, Waiman Long <[email protected] <mailto:[email protected]>> wrote:
Quoted 10 lines · Waiman Long
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Alex Kogan
Let me put this question to you. What do you think the number should be?
I think it would be very good to keep the inter-node latency below 1ms.
It is hard to guarantee that given that lock hold times can vary quite a
lot depending on the workload. What we can control is just how many later lock waiters can jump ahead before a given waiter.I totally agree. I do not think you can guarantee that latency even today.
With the existing spinlock, you join the queue and wait for as long as it takes
for each and every thread in front of you to execute its critical section.Quoted 16 lines
Quoted 9 lines
Quoted 9 lines
But to realize that we need data on the lock hold times. Specifically
for the heavily contended locks that make CNA worth it in the first place.I don't see that data, so I don't see how we can argue about this let
alone call something reasonable.Quoted 6 lines
In essence, CNA lock is for improving throughput on NUMA machines at the
expense of increasing worst case latency. If low latency is important,
it should be disabled. IfCONFIG_PREEMPT_RTis on,CONFIG_NUMA_AWARE_SPINLOCKSshould be off.Quoted 12 lines
Actually, what we are worrying about is the additional latency that can
be added to important tasks or execution contexts that are waiting for a
lock. Maybe we can make CNA lock behaves somewhat like qrwlock is that requests from interrupt context are giving priority. We could add a priority flag in the CNA node. If the flag is set, we will never put it
into the secondary queue. In fact, we can transfer control next to it even if it is not on the same node. We may also set the priority flag if
it is a RT task that is trying to acquire the lock.Quoted 6 lines
I think this is possible, and in fact, we have been thinking along those lines
about ways to better support RT tasks with CNA. However, this will_probably
require changes to API and will_certainlycomplicate the code quite a bit.What you need to do is to modify
cna_init_node()to check the current locking context and set the priority flag accordingly.
Quoted 9 lines
Quoted 1 line
Is there a lightweight way to identify such a “prioritized” thread?
You can use the
in_task()macro ininclude/linux/preempt.h. This is just a percpupreempt_countread and test. Ifin_task()is false, it is in a {soft|hard}irq or nmi context. If it is true, you can check thert_task()macro to see if it is an RT task. That will access to the current task structure. So it may cost a little bit more if you want to handle the RT task the same way.
We may not need to do that for softIRQ context. If that is the case, you can use in_irq() which checks for hardirq and nmi only. Peter, what is your thought on that?
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
On Fri, Jan 24, 2020 at 01:51:34PM -0500, Waiman Long wrote:
< 71 lines of garbage >
Quoted 10 lines
Quoted 7 lines
You can use the
in_task()macro ininclude/linux/preempt.h. This is just a percpupreempt_countread and test. Ifin_task()is false, it is in a {soft|hard}irq or nmi context. If it is true, you can check thert_task()macro to see if it is an RT task. That will access to the current task structure. So it may cost a little bit more if you want to handle the RT task the same way.We may not need to do that for softIRQ context. If that is the case, you can use
in_irq()which checks for hardirq and nmi only. Peter, what is your thought on that?
Can you lot please start trimming emails when you reply?
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
In reply to Waiman LongQuoted 10 lines · Waiman Long
Quoted 7 lines
You can use the
in_task()macro ininclude/linux/preempt.h. This is just a percpupreempt_countread and test. Ifin_task()is false, it is in a {soft|hard}irq or nmi context. If it is true, you can check thert_task()macro to see if it is an RT task. That will access to the current task structure. So it may cost a little bit more if you want to handle the RT task the same way.We may not need to do that for softIRQ context. If that is the case, you can use
in_irq()which checks for hardirq and nmi only. Peter, what is your thought on that?
In second thought, we should do that for softIRQ as well. Also, we may want to also check if irqs_disabled() is true as well by calls like spin_lock_irq() or spin_lock_irqsave(). We do not want to unnecessarily prolong the irq off period.
Cheers,
Longman
A message that is not in the archive (sent off-list or not yet received): [email protected]
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 27 lines · Alex Kogan
Quoted 16 lines
Quoted 9 lines
We also probably do not want those “prioritized” threads to disrupt normal
CNA operation. E.g., if the main queue looks likeT1_1,P2_1,T1_2, …, whereT1_xis a thread running on node 1 andP2_1is a prioritized thread running
on node 2, we want to pass the lock fromT1_1toP2_1and then toT1_2(rather than haveP2_1to scan for another thread on node 2).Quoted 6 lines
There is a way to achieve that — when we pass the lock to
P2_1, we can set its numa node field to 1. This means that we need to reset the numa
node field incna_init_node(), but AFAICT this is relatively cheap. The rest
of the CNA logic should not change.Quoted 6 lines
I won't recommend doing that. If the lock cacheline has been moved from node 1 to 2, I will say it is better to stick with node 2 rather than switching back to node 1. That will mean that the secondary queue may contain lock waiters from the same nodes, but they will eventually be flushed back to the primary queue.
That’s right, assuming we do not reset
intra_nodecount when transferring the
lock to a prioritized thread from another node. Otherwise, we may starve waiters in the secondary queue.
Quoted 5 lines
Still, that can make the lock even less fair to non-prioritized threads. When
you flush the secondary queue, the preference may remain with the same node. This will not happen in the current form of CNA, as we never get threads from the preferred node in the secondary queue.
That is true.
However, it is no different from the current scheme that a waiter from another node may have to wait for 64k other waiters to go first before it has a chance to get it. Now that waiter can be from the same node as well.
Quoted 6 lines
Besides, I think that will decrease the benefit of CNA, especially if such prioritized threads are frequent, switching the preference from one node to another. But this is something I can evaluate, unless
there is some fundamental objection to the idea of tweaking the numa node of prioritized threads.
Usually the locks used in interrupt context are not that contended to the point that CNA can kick in. Of course, there are exceptions in some circumstances and we do not want to introduce additional latency to their lock waiting time.
Cheers,
Longman
Re: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 38 lines · Waiman Long
Quoted 27 lines · Alex Kogan
Quoted 16 lines
Quoted 9 lines
We also probably do not want those “prioritized” threads to disrupt normal
CNA operation. E.g., if the main queue looks likeT1_1,P2_1,T1_2, …, whereT1_xis a thread running on node 1 andP2_1is a prioritized thread running
on node 2, we want to pass the lock fromT1_1toP2_1and then toT1_2(rather than haveP2_1to scan for another thread on node 2).Quoted 6 lines
There is a way to achieve that — when we pass the lock to
P2_1, we can set its numa node field to 1. This means that we need to reset the numa
node field incna_init_node(), but AFAICT this is relatively cheap. The rest
of the CNA logic should not change.Quoted 6 lines
I won't recommend doing that. If the lock cacheline has been moved from node 1 to 2, I will say it is better to stick with node 2 rather than switching back to node 1. That will mean that the secondary queue may contain lock waiters from the same nodes, but they will eventually be flushed back to the primary queue.
That’s right, assuming we do not reset
intra_nodecount when transferring the
lock to a prioritized thread from another node. Otherwise, we may starve waiters in the secondary queue.Quoted 5 lines
Still, that can make the lock even less fair to non-prioritized threads. When
you flush the secondary queue, the preference may remain with the same node. This will not happen in the current form of CNA, as we never get threads from the preferred node in the secondary queue.
Quoted 6 lines
That is true.
However, it is no different from the current scheme that a waiter from another node may have to wait for 64k other waiters to go first before it has a chance to get it. Now that waiter can be from the same node as well.
The difference is that in the current form of CNA, the preferred node _will change after 64k lock transitions. In the change you propose, this is no longer the case. It may take another ~64k transitions for that to happen. More generally, I think this makes the analysis of the lock behavior more convoluted.
I think we should treat those prioritized threads as “wild” cards, passing the lock through them, but keeping the preferred node intact. This will potentially cost one extra lock migration, but will make reasoning about the lock behavior easier.
Regards,
— Alex
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 47 lines · Alex Kogan
Quoted 35 lines · Waiman Long
Quoted 26 lines · Alex Kogan
Quoted 15 lines
Quoted 9 lines
We also probably do not want those “prioritized” threads to disrupt normal
CNA operation. E.g., if the main queue looks likeT1_1,P2_1,T1_2, …, whereT1_xis a thread running on node 1 andP2_1is a prioritized thread running
on node 2, we want to pass the lock fromT1_1toP2_1and then toT1_2(rather than haveP2_1to scan for another thread on node 2).Quoted 6 lines
There is a way to achieve that — when we pass the lock to
P2_1, we can set its numa node field to 1. This means that we need to reset the numa
node field incna_init_node(), but AFAICT this is relatively cheap. The rest
of the CNA logic should not change.Quoted 6 lines
I won't recommend doing that. If the lock cacheline has been moved from node 1 to 2, I will say it is better to stick with node 2 rather than switching back to node 1. That will mean that the secondary queue may contain lock waiters from the same nodes, but they will eventually be flushed back to the primary queue.
That’s right, assuming we do not reset
intra_nodecount when transferring the
lock to a prioritized thread from another node. Otherwise, we may starve waiters in the secondary queue.Quoted 5 lines
Still, that can make the lock even less fair to non-prioritized threads. When
you flush the secondary queue, the preference may remain with the same node. This will not happen in the current form of CNA, as we never get threads from the preferred node in the secondary queue.Quoted 6 lines
That is true.
However, it is no different from the current scheme that a waiter from another node may have to wait for 64k other waiters to go first before it has a chance to get it. Now that waiter can be from the same node as well.
The difference is that in the current form of CNA, the preferred node
_willchange after 64k lock transitions. In the change you propose, this is no longer the case. It may take another ~64k transitions for that to happen. More generally, I think this makes the analysis of the lock behavior more convoluted.
Quoted 4 lines
I think we should treat those prioritized threads as “wild” cards, passing the lock through them, but keeping the preferred node intact. This will potentially cost one extra lock migration, but will make reasoning about the lock behavior easier.
It seems like you prefer mathematical purity than practical consideration. I won't object to that if you insist that is the right way to go. Just be mindful that you may need to add a preferred node value to do that. It will also complicate the code, but it is your choice.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
In reply to a message not in the archiveQuoted 1 line · Alex Kogan
Is there a lightweight way to identify such a “prioritized” thread?
No; people might for instance care about tail latencies between their identically spec'ed worker tasks.
In general it turns out that priority is a dynamic concept, not a static one.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Re: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 9 lines · Peter Zijlstra
Quoted 1 line · Alex Kogan
Is there a lightweight way to identify such a “prioritized” thread?
No; people might for instance care about tail latencies between their identically spec'ed worker tasks.
I would argue that those users need to tune/reduce the intra-node handoff threshold for their needs. Or disable CNA altogether.
In general, Peter, seems like you are not on board with the way Longman suggested to handle prioritized threads. Am I right?
Thanks,
— Alex
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 5 lines · Alex Kogan
Quoted 5 lines · Peter Zijlstra
On Fri, Jan 24, 2020 at 01:19:05PM -0500, Alex Kogan wrote:
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Is there a lightweight way to identify such a “prioritized” thread?
No; people might for instance care about tail latencies between their identically spec'ed worker tasks.
I would argue that those users need to tune/reduce the intra-node handoff threshold for their needs. Or disable CNA altogether.
I really don't like boot time arguments (or tunables in generic) for a machine to work as it should.
The default really should 'just work'.
Quoted 2 lines
In general, Peter, seems like you are not on board with the way Longman suggested to handle prioritized threads. Am I right?
Right.
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Alex Kogan
Quoted 5 lines · Peter Zijlstra
Quoted 1 line · Alex Kogan
Is there a lightweight way to identify such a “prioritized” thread?
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
No; people might for instance care about tail latencies between their identically spec'ed worker tasks.
I would argue that those users need to tune/reduce the intra-node handoff threshold for their needs. Or disable CNA altogether.
I really don't like boot time arguments (or tunables in generic) for a machine to work as it should.
The default really should 'just work'.
That will be the ideal case. In reality, it usually takes a while for the code to mature enough to do some kind of self tuning. In the mean time, having some configuration options available allows us to have more time to figure what the best configuration options to be.
Quoted 7 lines
Quoted 2 lines
In general, Peter, seems like you are not on board with the way Longman suggested to handle prioritized threads. Am I right?
Right.
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
Today, a 2-socket Rome server can have 128 cores and 256 threads. If we scale up more, we could easily have more than 1000 threads in a system. With that many logical cpus available, it is easy to envision some heavy spinlock contention can happen fairly regularly. This patch can alleviate the congestion and improve performance under that circumstance. Of course, the specific locks that are contended will depend on the workloads.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 6 lines · Waiman Long
Quoted 3 lines · Peter Zijlstra
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
Quoted 7 lines
Today, a 2-socket Rome server can have 128 cores and 256 threads. If we scale up more, we could easily have more than 1000 threads in a system. With that many logical cpus available, it is easy to envision some heavy spinlock contention can happen fairly regularly. This patch can alleviate the congestion and improve performance under that circumstance. Of course, the specific locks that are contended will depend on the workloads.
Not the point. If there isn't an issue today, we don't have anything to fix.
Furthermore, we've always adressed specific issues by looking at the locking granularity, first.
So again, what specific lock inspired all these patches?
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Waiman Long
Quoted 3 lines · Peter Zijlstra
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
Today, a 2-socket Rome server can have 128 cores and 256 threads. If we scale up more, we could easily have more than 1000 threads in a system. With that many logical cpus available, it is easy to envision some heavy spinlock contention can happen fairly regularly. This patch can alleviate the congestion and improve performance under that circumstance. Of course, the specific locks that are contended will depend on the workloads.
Quoted 5 lines
Not the point. If there isn't an issue today, we don't have anything to fix.
Furthermore, we've always adressed specific issues by looking at the locking granularity, first.
You are right in that. Unlike ticket spinlock where performance can drop precipitately over a cliff when there is heavy contention, qspinlock won't have this kind of performance drop. My suspicion is that slowdowns caused by heavy spinlock contention in actual workloads are likely to be more transient in nature and harder to pinpoint. These days, I seldom get bug report that is related to heavy spinlock contention.
Quoted 3 lines
So again, what specific lock inspired all these patches?
Maybe Alex has some data to share.
Cheers,
Longman
A message that is not in the archive (sent off-list or not yet received): [email protected]
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 4 lines · Alex Kogan
Quoted 4 lines · Waiman Long
Quoted 1 line · Peter Zijlstra
On Mon, Feb 03, 2020 at 09:59:12AM -0500, Waiman Long wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Peter Zijlstra
Quoted 3 lines
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
There are quite a few actually. files_struct.file_lock, file_lock_context.flc_lock and lockref.lock are some concrete examples that get very hot in will-it-scale benchmarks.
Right, that's all a variant of banging on the same resources across nodes. I'm not sure there's anything fundamental we can fix there.
Quoted 3 lines
And then there are spinlocks in __futex_data.queues, which get hot when applications have contended (pthread) locks — LevelDB is an example.
A numa aware rework of futexes has been on the todo list for years :/
Quoted 7 lines
Our initial motivation was based on an observation that kernel qspinlock is not NUMA-aware. So what, you may ask. Much like people realized in the past that global spinning is bad for performance, and they switched from ticket lock to locks with local spinning (e.g., MCS), I think everyone would agree these days that bouncing a lock (and cache lines in general) across numa nodes is similarly bad. And as CNA demonstrates, we are easily leaving 2-3x speedups on the table by doing just that with the current qspinlock.
Actual benchmarks with performance numbers are required. It helps motivate the patches as well as gives reviewers clues on how to reproduce / inspect the claims made.
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Waiman Long
reviewerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Alex Kogan
Quoted 8 lines · Waiman Long
Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Waiman Long
Quoted 3 lines · Peter Zijlstra
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
There are quite a few actually. files_struct.file_lock, file_lock_context.flc_lock and lockref.lock are some concrete examples that get very hot in will-it-scale benchmarks.
Quoted 7 lines
Right, that's all a variant of banging on the same resources across nodes. I'm not sure there's anything fundamental we can fix there.
Quoted 3 lines
And then there are spinlocks in __futex_data.queues, which get hot when applications have contended (pthread) locks — LevelDB is an example.
A numa aware rework of futexes has been on the todo list for years :/
Now, we are going to get that for free with this patchset:-)
Quoted 8 lines
Quoted 8 lines
Our initial motivation was based on an observation that kernel qspinlock is not NUMA-aware. So what, you may ask. Much like people realized in the past that global spinning is bad for performance, and they switched from ticket lock to locks with local spinning (e.g., MCS), I think everyone would agree these days that bouncing a lock (and cache lines in general) across numa nodes is similarly bad. And as CNA demonstrates, we are easily leaving 2-3x speedups on the table by doing just that with the current qspinlock.
Quoted 4 lines
Actual benchmarks with performance numbers are required. It helps motivate the patches as well as gives reviewers clues on how to reproduce / inspect the claims made.
I think the cover-letter does have some benchmark results listed. Are you saying that some benchmark results should be put into individual patches themselves?
Cheers,
Longman
Re: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
Quoted 11 lines · Waiman Long
Quoted 6 lines · Peter Zijlstra
Quoted 5 lines · Alex Kogan
Quoted 5 lines · Waiman Long
Quoted 2 lines · Peter Zijlstra
Quoted 1 line · Waiman Long
On 2/3/20 8:45 AM, Peter Zijlstra wrote:
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Presumably you have a workload where CNA is actually a win? That is, what inspired you to go down this road? Which actual kernel lock is so contended on NUMA machines that we need to do this?
There are quite a few actually. files_struct.file_lock, file_lock_context.flc_lock and lockref.lock are some concrete examples that get very hot in will-it-scale benchmarks.
Right, that's all a variant of banging on the same resources across nodes. I'm not sure there's anything fundamental we can fix there.
Not much, except gain that 2x from a better lock.
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
And then there are spinlocks in __futex_data.queues, which get hot when applications have contended (pthread) locks — LevelDB is an example.
A numa aware rework of futexes has been on the todo list for years :/
Now, we are going to get that for free with this patchset:-)
Exactly!!
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Our initial motivation was based on an observation that kernel qspinlock is not NUMA-aware. So what, you may ask. Much like people realized in the past that global spinning is bad for performance, and they switched from ticket lock to locks with local spinning (e.g., MCS), I think everyone would agree these days that bouncing a lock (and cache lines in general) across numa nodes is similarly bad. And as CNA demonstrates, we are easily leaving 2-3x speedups on the table by doing just that with the current qspinlock.
Quoted 5 lines
Quoted 4 lines
Actual benchmarks with performance numbers are required. It helps motivate the patches as well as gives reviewers clues on how to reproduce / inspect the claims made.
I think the cover-letter does have some benchmark results listed.
To clarify on that, I _used to include benchmark results in the cover letter for previous revisions. I stopped doing that as the changes between revisions were rather minor — maybe that is the missing part? If so, my apologies, I can certainly publish them again.
The point is that we have numbers for actual benchmarks, plus the kernel build robot has sent quite a few reports on positive improvements in the performance of AIM7 and other benchmarks due to CNA (plus ARM folks noticed improvement in their benchmarks, although I think those were mostly microbenchmarks. Yet, it is evident that the improvements are cross-platform.)
Regards,
— Alex
Peter Zijlstra
maintainerRe: [PATCH v8 4/5] locking/qspinlock: Introduce starvation avoidance into CNA
In reply to a message not in the archiveQuoted 3 lines · Waiman Long
I also thought about that. As you said, it can be hard to guarantee that reliable time value can be retrieved in a timely manner across all the archs.
Rememer that this code is limited to 64bit archs that have NUMA, my quick grep says that is limited to:
alpha, arm64, ia64, mips, powerpc, s390, sparc, x86
afaict, x86 is the one with the worst clocks between the lot of them (with exception of ia64, which has been completely buggered for a while and nobody cares).
Quoted 2 lines
Even if we can do that, we will introduce latency to important tasks or contexts. I like the first approach better.
In general, the kernel has no clues what is actually important.
PATCH v8 5/5 locking/qspinlock: Introduce the shuffle reduction optimization into CNA
In reply to Alex KoganThis performance 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 | 46 ++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-)
kernel/locking/qspinlock_cna.h +44 -2
@@ -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).
@@ -57,6 +58,7 @@ struct cna_node {enum {LOCAL_WAITER_FOUND =2 ,/* 0 and 1 are reserved for @locked */ FLUSH_SECONDARY_QUEUE =3 , +PASS_LOCK_IMMEDIATELY =4 ,MIN_ENCODED_TAIL };
@@ -70,6 +72,34 @@ enum {*/ int intra_node_handoff_threshold__ro_after_init =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);
@@ -251,8 +281,11 @@ __always_inline u32 cna_pre_scan(struct qspinlock *lock,struct cna_node *cn = (struct cna_node *)node; cn->pre_scan_result = - cn->intra_count == intra_node_handoff_threshold ? -FLUSH_SECONDARY_QUEUE :cna_scan_main_queue (node, node); + (node->locked <=1 &&probably (SHUFFLE_REDUCTION_PROB_ARG )) ? +PASS_LOCK_IMMEDIATELY : + cn->intra_count == intra_node_handoff_threshold ? +FLUSH_SECONDARY_QUEUE : +cna_scan_main_queue (node, node);return 0 ; }
@@ -266,6 +299,14 @@ static inline void cna_pass_lock(struct mcs_spinlock *node,u32 scan = cn->pre_scan_result; +/* +* perf. optimization - check if we can skip the logic of triaging +* through other possible values in @scan (helps under light lock +* contention) +*/ +if (scan ==PASS_LOCK_IMMEDIATELY ) + goto pass_lock; +/* * 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
@@ -294,6 +335,7 @@ static inline void cna_pass_lock(struct mcs_spinlock *node, tail_2nd->next = next; } +pass_lock:arch_mcs_pass_lock (&next_holder->locked, val); }
Signature · 1 line
2.21.0 (Apple Git-122.2)
Peter Zijlstra
maintainerRe: [PATCH v8 5/5] locking/qspinlock: Introduce the shuffle reduction optimization into CNA
Quoted 4 lines · Alex Kogan
kernel/locking/qspinlock_cna.h +0
-0@@ -251,8 +281,11 @@ __always_inline u32 cna_pre_scan(struct qspinlock *lock,struct cna_node *cn = (struct cna_node *)node; cn->pre_scan_result =
kernel/locking/qspinlock_cna.h __always_inline u32 cna_pre_scan(struct qspinlock *lock, · line 291
Quoted 10 lines
kernel/locking/qspinlock_cna.h +5
-2- cn->intra_count == intra_node_handoff_threshold ? -FLUSH_SECONDARY_QUEUE :cna_scan_main_queue (node, node); + (node->locked <=1 &&probably (SHUFFLE_REDUCTION_PROB_ARG )) ? +PASS_LOCK_IMMEDIATELY : + cn->intra_count == intra_node_handoff_threshold ? +FLUSH_SECONDARY_QUEUE : +cna_scan_main_queue (node, node);return 0 ; }
Let me just, once again, remind people that the Linux Kernel is not part of the Obfuscated C code contest.
Quoted 1 line
Reviewed-by Steve Sistare <[email protected]>
Seriously, in what universe is that actually readable code? Steve quick, say what it does.
Quoted 55 lines · Alex Kogan
Minor changes from v7 based on feedback from Longman:
------------------------------------------------------ Move
__initfunctions fromalternative.ctoqspinlock_cna.h- Introduce enum for return values from
cna_pre_scan(), for better readability.- Add/revise a few comments to improve readability.
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.
The series applies on top of v5.5.0-rc2, commit
ea200dec51. 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 13 lines
.../admin-guide/kernel-parameters.txt | 18 + arch/arm/include/asm/mcs_spinlock.h | 6 +- arch/x86/Kconfig | 20 + arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 4 + include/asm-generic/mcs_spinlock.h | 4 +- kernel/locking/mcs_spinlock.h | 20 +- kernel/locking/qspinlock.c | 82 +++- kernel/locking/qspinlock_cna.h | 400 ++++++++++++++++++ kernel/locking/qspinlock_paravirt.h | 2 +- 10 files changed, 537 insertions(+), 23 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.h
I have reviewed this patch series. Besides a few minor nits, the rests look solid to me. So you can put my review tag.
Peter and Will, would you mind taking a look to see if you have anything to add?
Thanks,
Longman
Hi Will,
Quoted 55 lines · Alex Kogan
Minor changes from v7 based on feedback from Longman:
------------------------------------------------------ Move
__initfunctions fromalternative.ctoqspinlock_cna.h- Introduce enum for return values from
cna_pre_scan(), for better readability.- Add/revise a few comments to improve readability.
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://urldefense.proofpoint.com/v2/url?u=https-3A__arxiv.org_abs_1810.05600&d=DwIDAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=G9w4KsPaQLACBfGCL35PtiRH996yqJDxAZwrWegU2qQ&m=AoOLTQlgNjtdBvY_yWd6ViBXrVM6o2wqXOdFA4B_F2A&s=yUjG9gfi3BtLKDEjgki86h52GVXMvDQ6ZClMvoIG034&e= .
The series applies on top of v5.5.0-rc2, commit
ea200dec51. 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 16 lines
.../admin-guide/kernel-parameters.txt | 18 + arch/arm/include/asm/mcs_spinlock.h | 6 +- arch/x86/Kconfig | 20 + arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 4 + include/asm-generic/mcs_spinlock.h | 4 +- kernel/locking/mcs_spinlock.h | 20 +- kernel/locking/qspinlock.c | 82 +++- kernel/locking/qspinlock_cna.h | 400 ++++++++++++++++++ kernel/locking/qspinlock_paravirt.h | 2 +- 10 files changed, 537 insertions(+), 23 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.hSignature · 1 line
2.21.0 (Apple Git-122.2)
Tried out queued spinlock slowpath improvements on arm64 (ThunderX2) by hardwiring CNA APIs to queued_spin_lock_slowpath() and numbers are pretty good with the CNA changes.
Speed-up on v5.5-rc4 kernel:
will-it-scale/open1_threads:
#thr speed-up
1 1.00 2 0.97 4 0.98 8 1.02 16 0.95 32 1.63 64 1.70 128 2.09 224 2.16
will-it-scale/lock2_threads:
#thr speed-up
1 0.98 2 0.99 4 0.90 8 0.98 16 0.99 32 1.52 64 2.31 128 2.25 224 2.04
#thr - number of threads
speed-up - number with CNA patch / number with stock kernel
Please share your thoughts on best way to enable this series on arm64.
Thanks,
Shijith
Hi Will/Catalin,
Quoted 103 lines · Shijith Thotton
Quoted 55 lines · Alex Kogan
Minor changes from v7 based on feedback from Longman:
------------------------------------------------------ Move
__initfunctions fromalternative.ctoqspinlock_cna.h- Introduce enum for return values from
cna_pre_scan(), for better readability.- Add/revise a few comments to improve readability.
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://urldefense.proofpoint.com/v2/url?u=https-3A__arxiv.org_abs_1810.05600&d=DwIDAg&c=nKjWec2b6R0mOyPaz7xtfQ&r=G9w4KsPaQLACBfGCL35PtiRH996yqJDxAZwrWegU2qQ&m=AoOLTQlgNjtdBvY_yWd6ViBXrVM6o2wqXOdFA4B_F2A&s=yUjG9gfi3BtLKDEjgki86h52GVXMvDQ6ZClMvoIG034&e= .
The series applies on top of v5.5.0-rc2, commit
ea200dec51. 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
CNAQuoted 16 lines
.../admin-guide/kernel-parameters.txt | 18 + arch/arm/include/asm/mcs_spinlock.h | 6 +- arch/x86/Kconfig | 20 + arch/x86/include/asm/qspinlock.h | 4 + arch/x86/kernel/alternative.c | 4 + include/asm-generic/mcs_spinlock.h | 4 +- kernel/locking/mcs_spinlock.h | 20 +- kernel/locking/qspinlock.c | 82 +++- kernel/locking/qspinlock_cna.h | 400 ++++++++++++++++++ kernel/locking/qspinlock_paravirt.h | 2 +- 10 files changed, 537 insertions(+), 23 deletions(-) create mode 100644 kernel/locking/qspinlock_cna.hSignature · 1 line
2.21.0 (Apple Git-122.2)Tried out queued spinlock slowpath improvements on arm64 (ThunderX2) by hardwiring CNA APIs to
queued_spin_lock_slowpath()and numbers are pretty good with the CNA changes.Speed-up on v5.5-rc4 kernel:
will-it-scale/open1_threads:
#thr speed-up1 1.00 2 0.97 4 0.98 8 1.02 16 0.95 32 1.63 64 1.70 128 2.09 224 2.16will-it-scale/lock2_threads:
#thr speed-up1 0.98 2 0.99 4 0.90 8 0.98 16 0.99 32 1.52 64 2.31 128 2.25 224 2.04
Quoted 4 lines
#thr - number of threads
speed-up - number with CNA patch / number with stock kernelPlease share your thoughts on best way to enable this series on arm64.
Please comment if you got a chance to look at this.
Thanks,
Shijith