Skip to contentSkip to conversation

qspinlock: Introducing a 4-byte queue spinlock

RFC PATCH Superseded Waiman Long 1 participant 3 messages Aug 13, 2013

Subsystems (5)

v2 of 16 Superseded Latest is v16 →

lore
Share & tools
lore
#0 lore raw

v2 cover qspinlock: Introducing a 4-byte queue spinlock

v1->v2:
- Add some more comments to document what the code does.
- Add a numerous CPU mode to support >= 16K CPUs
- Add a configuration option to allow lock stealing which can further improve performance in many cases.
- Enable wakeup of queue head CPU at unlock time for non-numerous CPU mode.

This patch set introduces a queue-based spinlock implementation that can be used to replace the default ticket spinlock without increasing the size of the spinlock data structure. As a result, critical kernel data structures that embed spinlock won't increase in size and breaking data alignments.

The queue spinlock has about the same performance as the ticket spinlock in uncontended case. Its performance is better with moderate to heavy contention. It may be slightly slower in light to moderate level of lock contention where the queue depth is around 1-2. This patch has the potential of improving the performance of all the workloads that have moderate to heavy spinlock contention.

The queue spinlock is especially suitable for NUMA machines with at least 2 sockets, though noticeable performance benefit probably won't show up in machines with less than 4 sockets.

There is also an experimental option to allow lock stealing thus breaking the FIFO guarantee of lock acquisition. However, performance usually goes up with an unfair lock.

The purpose of this patch set is not to solve any particular spinlock contention problems. Those need to be solved by refactoring the code to make more efficient use of the lock or finer granularity ones. The main purpose is to make the lock contention problems more tolerable until someone can spend the time and effort to fix them.

Waiman Long (2):
qspinlock: Introducing a 4-byte queue spinlock implementation qspinlock x86: Enable x86 to use queue spinlock

 arch/x86/Kconfig                      |    3 +
 arch/x86/include/asm/spinlock.h       |    2 +
 arch/x86/include/asm/spinlock_types.h |    4 +
 arch/x86/kernel/paravirt-spinlocks.c  |   10 +
 include/asm-generic/qspinlock.h       |  207 +++++++++++++
 lib/Kconfig                           |   25 ++
 lib/Makefile                          |    1 +
 lib/qspinlock.c                       |  522 +++++++++++++++++++++++++++++++++
 8 files changed, 774 insertions(+), 0 deletions(-)
 create mode 100644 include/asm-generic/qspinlock.h
 create mode 100644 lib/qspinlock.c
#1 lore raw

RFC PATCH v2 1/2 qspinlock: Introducing a 4-byte queue spinlock implementation

This patch introduces a new queue spinlock implementation that can serve as an alternative to the default ticket spinlock. Compared with the ticket spinlock, this queue spinlock should be almost as fair as the ticket spinlock. It has about the same speed in single-thread and it can be much faster in high contention situations. Only in light to moderate contention where the average queue depth is around 1-2 will this queue spinlock be potentially a bit slower due to the higher slowpath overhead.

This queue spinlock is especially suit to NUMA machines with a large number of cores as the chance of spinlock contention is much higher in those machines. The cost of contention is also higher because of slower inter-node memory traffic.

The idea behind this spinlock implementation is the fact that spinlocks are acquired with preemption disabled. In other words, the process will not be migrated to another CPU while it is trying to get a spinlock. Ignoring interrupt handling, a CPU can only be contending in one spinlock at any one time. Of course, interrupt handler can try to acquire one spinlock while the interrupted user process is in the process of getting another spinlock. By allocating a set of per-cpu queue nodes and used them to form a waiting queue, we can encode the queue node address into a much smaller 16-bit size. Together with the 1-byte lock bit, this queue spinlock implementation will only need 4 bytes to hold all the information that it needs.

The default queue node address encoding is as follows:
Bits 0-1 : queue node index in the per-cpu array (4 entries) Bits 2-16: cpu number + 1 (max cpus = 16383)

If more CPUs are to be supported (numerous CPU mode), 24 bits of the 32-bit lock will be used to support at least (64K-1) CPUs. There are additional overhead which can potentially slow it down a little bit.

In the extremely unlikely case that all the queue node entries are used up, the current code will fall back to busy spinning without waiting in a queue with warning message.

This patch also has an experimental configuration option (QSPINLOCK_UNFAIR) to enable lock stealing. That will break the FIFO guarantee of lock acquisition process, but it usually results in better performance when lock contention happens.

The patch allows the optional replacement of architecture specific implementation of ticket spinlock by this generic version of queue spinlock. Two new config parameters are introduced:

1. QSPINLOCK
A select-able option that enables the building and replacement of architecture specific spinlock by queue spinlock.
2. ARCH_QSPINLOCK
Have to be defined in arch/$(ARCH)/Kconfig to enable QUEUE_SPINLOCK option. This option, by itself, will not enable the queue spinlock feature.

For single-thread performance (no contention), a 256K lock/unlock loop was run on a 2.93Ghz Westmere x86-64 CPU. The following table shows the average time (in ns) for a single lock/unlock sequence (including the looping and timing overhead):

Lock Type Time (ns)
--------- ---------
Ticket spinlock 12.2
Queue spinlock (Normal) 11.9
Queue spinlock (Numerous CPU) 7.9

So the normal queue spinlock has about the same performance as a ticket spinlock. In numerous CPU mode, the queue spinlock is much faster because of less overhead in the unlock fast path.

The AIM7 benchmark was run on a 8-socket 80-core DL980 with Westmere x86-64 CPUs and HT off to evaluate the performance impact of this patch on the 3.10.5 kernel. The following modified kernels were tested:
1) Queue spinlock (normal)
2) Queue spinlock (normal) with lock stealing
3) Queue spinlock (numerous CPU)

The jobs per minute (JPM) results for the 1100-2000 user ranges are shown below:

+------------+---------+---------+---------+---------+
| Kernel     | 3.10.5  | 3.10.5  | 3.10.5  | 3.10.5  |
|            |         | qlock 1 | qlock 2 | qlock 3 |
+------------+---------+---------+---------+---------+
|alltests    |  401450 |  402184 |  406803 |  400031 |
|% Change    |    -    |   +0.2% |   +1.3% |   -0.4% |
|custom      |  306544 |  305525 |  310030 |  307530 |
|% Change    |    -    |   -0.3% |   +1.1% |   +0.3% |
|dbase       |  897496 |  900656 |  901985 |  900234 |
|% Change    |    -    |   +0.4% |   +0.5% |   +0.3% |
|disk        |  211852 |  269357 |  347108 |  274298 |
|% Change    |    -    |  +27.1% |  +63.8% |  +29.5% |
|five_sec    |  145519 |  157018 |  159820 |  155627 |
|% Change    |    -    |   +7.9% |   +9.8% |   +7.0% |
|fserver     |  454512 |  468861 |  466659 |  459497 |
|% Change    |    -    |   +3.2% |   +2.7% |   +1.1% |
|high_systime|  135079 |  138337 |  140992 |  140198 |
|% Change    |    -    |   +2.4% |   +4.4% |   +3.8% |
|new_dbase   |  936614 |  937732 |  937403 |  936805 |
|% Change    |    -    |   +0.1% |   +0.1% |    0.0% |
|new_fserver |  440808 |  449432 |  450547 |  440155 |
|% Change    |    -    |   +2.0% |   +2.2% |   -0.2% |
|shared      |  371142 |  368907 |  369868 |  370862 |
|% Change    |    -    |   -0.6% |   -0.3% |   -0.1% |
|short       | 1058806 | 3010190 | 6058205 | 3104796 |
|% Change    |    -    | +184.3% | +472.2% | +193.2% |
+------------+---------+---------+---------+---------+

Due to variability of AIM7 results, a change of a few percentage points may not indicate a real change in performance. However, the general trend is that enabling lock stealing can improve performance significantly in workloads with a lot of lock contention. The more than double in performance in the short workload is particularly impressive.

As for the normal and numerous CPU modes, one was slightly better in some workloads, but slightly worse in the others. The trade-off between these two modes are as follows:
1) The normal mode enables unlock wake-up and hence has less lock cacheline contention.
2) The numerous CPU mode has less latency between the unlock operation of one CPU and the lock operation of the next one in line.

Depending on which factor is more important, one will be slightly faster than the other.

For the disk workload, the spinlock bottleneck is the standalone mb_cache_spinlock in fs/mbcache.c. For the short workload, the spinlock bottleneck is the d_lock in the dentry structure.

The following table shows the %time used up by the locks as reported by the perf command at 1000 users for disk workload & 1500 users for short workload with HT off.

--------------------------------+----------+----------+----------+
Configuration | ticket | qlock | qlock |
| spinlock | fastpath | slowpath |
--------------------------------+----------+----------+----------+

Disk w/o patch			|  69.0%   |    -     |    -     |
Disk with patch         	|    -     |  0.31%   |  73.2%   |
Disk with patch	& lock stealing |    -     |  0.39%   |  67.5%   |
Short w/o patch			|  74.3%   |    -     |    -     |
Short with patch		|    -     |  0.76%   |  73.0%   |

Short with patch & lock stealing| - | 2.37% | 43.3% |
--------------------------------+----------+----------+----------+

There are 3 observations here:
1. With lock stealing enabled, less time are spent in the lock slowpath, thus allowing the system to do more useful work.
2. The %CPU time actually increases in the disk workload with the patch without lock stealing. The fact that most of them are spinning on their own cacheline with less congestion can probably skew the number up.
3. Both the short and disk workloads spend about 70% of their time in the lock without lock stealing. However, performance improvement is 2.8X vs 1.3X. This is probably due to the fact that d_lock is embedded next to the d_count to be updated whereas mb_cache_spinlock is in a standalone cacheline not shared by the data to be updated.

---
 include/asm-generic/qspinlock.h |  207 ++++++++++++++++
 lib/Kconfig                     |   25 ++
 lib/Makefile                    |    1 +
 lib/qspinlock.c                 |  522 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 755 insertions(+), 0 deletions(-)
 create mode 100644 include/asm-generic/qspinlock.h
 create mode 100644 lib/qspinlock.c
include/asm-generic/qspinlock.h new file +207 -0
new file mode 100644
index 0000000..1e0403f
@@ -0,0 +1,207 @@
+/*
+ * Queue spinlock
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * (C) Copyright 2013 Hewlett-Packard Development Company, L.P.
+ *
+ * Authors: Waiman Long <[email protected]>
+ */
+#ifndef __ASM_GENERIC_QSPINLOCK_H
+#define __ASM_GENERIC_QSPINLOCK_H
+
+#include <linux/types.h>
+#include <asm/cmpxchg.h>
+#include <asm/barrier.h>
+#include <asm/processor.h>
+#include <asm/byteorder.h>
+
+#if !defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
+#error "Missing either LITTLE_ENDIAN or BIG_ENDIAN definition."
+#endif
+
+#ifdef CONFIG_PARAVIRT_SPINLOCKS
+#define	qspinlock arch_spinlock
+#endif
+
+/*
+ * There is 2 slightly different implementations of queue spinlock.
+ * The faster one can support only upto (16K-1) CPUs, but the lock
+ * word has to be 16 bits. The slower one requires a 8-bit lock word
+ * and can currently support (64K-1). However, it is possible to support
+ * more CPUs than the current limit, if necessary.
+ */
+#if !defined(QSPINLOCK_MANYCPUS) && (CONFIG_NR_CPUS >= (16*1024-1))
+#define QSPINLOCK_MANYCPUS
+#endif
+
+#ifdef QSPINLOCK_MANYCPUS
+#define	lock8	locked
+#else
+#define	lock16	locked
+#endif
+
+/*
+ * The queue spinlock data structure
+ * The lock word should always be the least significant portion of the
+ * 32-bit word.
+ */
+typedef struct qspinlock {
+	union {
+		u32		qlock;
+		struct {
+#ifdef __LITTLE_ENDIAN
+			union {
+				u8	lock8;	/* 8-bit lock word */
+				u16	lock16;	/* 16-bit lock word */
+			};
+			u16	qcode;		/* Wait queue code */
+#else
+			u16	qcode;		/* Wait queue code */
+			union {
+				struct {
+					u8	dummy;	/* Reserved	   */
+					u8	lock8;	/* 8-bit lock word */
+				};
+				u16		lock16;	/* 16-bit lock word */
+			};
+#endif
+		};
+	};
+} arch_spinlock_t;
+
+/* Remove the temporary lock8 or lock16 macro */
+#ifdef lock8
+#undef lock8
+#endif
+#ifdef lock16
+#undef lock16
+#endif
+
+#define QSPINLOCK_LOCKED	1
+
+/*
+ * The CONFIG_QSPINLOCK_UNFAIR config parameter breaks the FIFO fairness
+ * guarantee of the queue spinlock and allows some lock stealing to happen.
+ * This slight unfairness may increase the overall throughput and performance
+ * of the system in some circumstances. An example will be a virtual guest
+ * with over-commited CPUs. If a CPU at the queue head is suspended, other
+ * newly arrived CPUs wanting to get the lock may be able to get it while
+ * the others in the queue have to wait until the queue head process returns.
+ */
+#ifdef CONFIG_QSPINLOCK_UNFAIR
+#define	qlock_word	locked
+#else
+#define qlock_word	qlock
+#endif
+
+/*
+ * External function declarations
+ */
+extern void queue_spin_lock_slowpath(struct qspinlock *lock);
+#ifndef QSPINLOCK_MANYCPUS
+extern void queue_spin_unlock_slowpath(struct qspinlock *lock, u16 qcode);
+#endif
+
+/**
+ * queue_spin_is_locked - is the spinlock locked?
+ * @lock: Pointer to queue spinlock structure
+ * Return: 1 if it is locked, 0 otherwise
+ */
+static __always_inline int queue_spin_is_locked(struct qspinlock *lock)
+{
+	return ACCESS_ONCE(lock->locked);
+}
+
+/**
+ * queue_spin_is_contended - check if the lock is contended
+ * @lock : Pointer to queue spinlock structure
+ * Return: 1 if lock contended, 0 otherwise
+ */
+static __always_inline int queue_spin_is_contended(struct qspinlock *lock)
+{
+	return ACCESS_ONCE(lock->qcode);
+}
+/**
+ * queue_spin_trylock - try to acquire the queue spinlock
+ * @lock : Pointer to queue spinlock structure
+ * Return: 1 if lock acquired, 0 if failed
+ */
+static __always_inline int queue_spin_trylock(struct qspinlock *lock)
+{
+	if (!ACCESS_ONCE(lock->qlock_word) &&
+	   (cmpxchg(&lock->qlock_word, 0, QSPINLOCK_LOCKED) == 0))
+		return 1;
+	return 0;
+}
+
+/**
+ * queue_spin_lock - acquire a queue spinlock
+ * @lock: Pointer to queue spinlock structure
+ */
+static __always_inline void queue_spin_lock(struct qspinlock *lock)
+{
+	/*
+	 * To reduce memory access to only once for the cold cache case,
+	 * a direct cmpxchg() is performed in the fastpath to optimize the
+	 * uncontended case. The contended performance, however, may suffer
+	 * a bit because of that.
+	 */
+	if (likely(cmpxchg(&lock->qlock_word, 0, QSPINLOCK_LOCKED) == 0))
+		return;
+	queue_spin_lock_slowpath(lock);
+}
+
+/**
+ * queue_spin_unlock - release a queue spinlock
+ * @lock : Pointer to queue spinlock structure
+ */
+static __always_inline void queue_spin_unlock(struct qspinlock *lock)
+{
+	/*
+	 * This unlock function may get inlined into the caller. The
+	 * compiler barrier is needed to make sure that the lock will
+	 * not be released before changes to the critical section is done.
+	 * The ending smp_wmb() allows queue head process to get the lock ASAP.
+	 */
+	barrier();
+#ifndef QSPINLOCK_MANYCPUS
+	{
+		u16 qcode = ACCESS_ONCE(lock->locked);
+		if (unlikely(qcode != QSPINLOCK_LOCKED))
+			queue_spin_unlock_slowpath(lock, qcode);
+	}
+#endif
+	ACCESS_ONCE(lock->locked) = 0;
+	smp_wmb();
+}
+
+#undef qlock_word
+
+/*
+ * Initializier
+ */
+#define	__ARCH_SPIN_LOCK_UNLOCKED	{ { 0 } }
+
+#ifndef CONFIG_PARAVIRT_SPINLOCKS
+/*
+ * Remapping spinlock architecture specific functions to the corresponding
+ * queue spinlock functions.
+ */
+#define arch_spin_is_locked(l)		queue_spin_is_locked(l)
+#define arch_spin_is_contended(l)	queue_spin_is_contended(l)
+#define arch_spin_lock(l)		queue_spin_lock(l)
+#define arch_spin_trylock(l)		queue_spin_trylock(l)
+#define arch_spin_unlock(l)		queue_spin_unlock(l)
+#define arch_spin_lock_flags(l, f)	queue_spin_lock(l)
+#endif
+
+#endif /* __ASM_GENERIC_QSPINLOCK_H */
lib/Kconfig +25 -0
index 71d9f81..d0306a0 100644
@@ -410,6 +410,31 @@ config SIGNATURE
 	  Implementation is done using GnuPG MPI library
 
 #
+# Generic queue spinlock
+#
+config QSPINLOCK
+	bool "Use queue spinlock"
+	depends on ARCH_QSPINLOCK
+	default n
+	help
+	  This option enables an alternative spinlock implementation
+	  that has the same spinlock structure size but is more
+	  optimized for larger NUMA systems with a lot of CPU
+	  cores. Specifically, waiting lock spinners are put to wait
+	  in a queue on a local cacheline rather than all spinning
+	  on the same lock cacheline.
+
+config QSPINLOCK_UNFAIR
+	bool "Allow lock stealing in queue spinlock (EXPERIMENTAL)"
+	depends on QSPINLOCK
+	default n
+	help
+	  This option allows some lock stealing to happen and
+	  breaks the FIFO guarantee on which process will get the
+	  lock next. This kind of unfairness may help to improve
+	  performance for some workloads.
+
+#
 # libfdt files, only selected if needed.
 #
 config LIBFDT
lib/Makefile +1 -0
index 7baccfd..a557f8c 100644
@@ -187,3 +187,4 @@ quiet_cmd_build_OID_registry = GEN     $@
 clean-files	+= oid_registry_data.c
 
 obj-$(CONFIG_UCS2_STRING) += ucs2_string.o
+obj-$(CONFIG_QSPINLOCK) += qspinlock.o
lib/qspinlock.c new file +522 -0

522 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

RFC PATCH v2 2/2 qspinlock x86: Enable x86 to use queue spinlock

In reply to Waiman Long

This patch makes the necessary changes at the x86 architecture specific layer to enable the presence of the CONFIG_QSPINLOCK kernel option which can be used to replace the ticket spinlock by the queue spinlock.

The turning on of ARCH_QSPINLOCK config option does not mean that the ticket spinlock will be replaced. It only means that the architecture supports the replacement. Actual replacement will only happen if the QSPINLOCK config option is also set.

---
 arch/x86/Kconfig                      |    3 +++
 arch/x86/include/asm/spinlock.h       |    2 ++
 arch/x86/include/asm/spinlock_types.h |    4 ++++
 arch/x86/kernel/paravirt-spinlocks.c  |   10 ++++++++++
 4 files changed, 19 insertions(+), 0 deletions(-)
arch/x86/Kconfig +3 -0
index b32ebf9..c70db51 100644
@@ -2344,6 +2344,9 @@ config X86_DMA_REMAP
 	bool
 	depends on STA2X11
 
+config ARCH_QSPINLOCK
+	def_bool y
+
 source "net/Kconfig"
 
 source "drivers/Kconfig"
arch/x86/include/asm/spinlock.h +2 -0
index 33692ea..d55decf 100644
@@ -34,6 +34,7 @@
 # define UNLOCK_LOCK_PREFIX
 #endif
 
+#ifndef CONFIG_QSPINLOCK
 /*
  * Ticket locks are conceptually two parts, one indicating the current head of
  * the queue, and the other indicating the current tail. The lock is acquired
@@ -130,6 +131,7 @@ static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock,
 }
 
 #endif	/* CONFIG_PARAVIRT_SPINLOCKS */
+#endif	/* !CONFIG_QSPINLOCK */
 
 static inline void arch_spin_unlock_wait(arch_spinlock_t *lock)
 {
arch/x86/include/asm/spinlock_types.h +4 -0
index ad0ad07..e030e20 100644
@@ -7,6 +7,9 @@
 
 #include <linux/types.h>
 
+#ifdef CONFIG_QSPINLOCK
+# include <asm-generic/qspinlock.h>
+#else
 #if (CONFIG_NR_CPUS < 256)
 typedef u8  __ticket_t;
 typedef u16 __ticketpair_t;
@@ -27,6 +30,7 @@ typedef struct arch_spinlock {
 } arch_spinlock_t;
 
 #define __ARCH_SPIN_LOCK_UNLOCKED	{ { 0 } }
+#endif
 
 #include <asm/rwlock.h>
 
arch/x86/kernel/paravirt-spinlocks.c +10 -0
index 676b8c7..4bcca15 100644
@@ -15,6 +15,15 @@ default_spin_lock_flags(arch_spinlock_t *lock, unsigned long flags)
 
 struct pv_lock_ops pv_lock_ops = {
 #ifdef CONFIG_SMP
+# ifdef CONFIG_QSPINLOCK
+	.spin_is_locked = queue_spin_is_locked,
+	.spin_is_contended = queue_spin_is_contended,
+
+	.spin_lock = queue_spin_lock,
+	.spin_lock_flags = default_spin_lock_flags,
+	.spin_trylock = queue_spin_trylock,
+	.spin_unlock = queue_spin_unlock,
+# else
 	.spin_is_locked = __ticket_spin_is_locked,
 	.spin_is_contended = __ticket_spin_is_contended,
 
@@ -22,6 +31,7 @@ struct pv_lock_ops pv_lock_ops = {
 	.spin_lock_flags = default_spin_lock_flags,
 	.spin_trylock = __ticket_spin_trylock,
 	.spin_unlock = __ticket_spin_unlock,
+# endif
 #endif
 };
 EXPORT_SYMBOL(pv_lock_ops);
Signature · 1 line
1.7.1