Skip to contentSkip to conversation

locking/qspinlock: Enhance pvqspinlock & introduce queued unfair lock

PATCH In review Waiman Long 4 participants 27 messages Jul 11, 2015 – Aug 3, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover locking/qspinlock: Enhance pvqspinlock & introduce queued unfair lock

This patchset consists of two parts:

1) Patches 1-5 enhance the performance of PV qspinlock especially for overcommitted guest. The first patch moves all the CPU kicking to the unlock code. The 2nd and 3rd patches implement a kick-ahead and wait-early mechanism that was shown to improve performance for overcommitted guest. They are inspired by the "Do Virtual Machines Really Scale?" blog from Sanidhya Kashyap. The 4th patch adds code to collect PV qspinlock statistics. The last patch adds the pending bit support to PV qspinlock to improve performance at light load. This is important as the PV queuing code has even higher overhead than the native queuing code.

2) Patches 6 introduces queued unfair lock as a replacement of the existing unfair byte lock. The queued unfair lock is fairer than the byte lock currently in the qspinlock while improving performance at high contention level. Patch 7 adds a kernel command line option to KVM for disabling PV spinlock, similar to the one in Xen, if the administrators choose to do so. The last patch adds statistics collection to the queued unfair lock code.

Linux kernel builds were run in KVM guest on an 8-socket, 4 cores/socket Westmere-EX system and a 4-socket, 8 cores/socket Haswell-EX system. So both systems have 32 physical CPUs. VM guests (no NUMA pinning) were set up with 32, 48 and 60 vCPUs. The kernel build times (make -j <n>, where <n> was the number of vCPUs) on various configurations were as follows:

Westere-EX (8x4):

Kernel 32 vCPUs 48 vCPUs 60 vCPUs
------ -------- -------- --------
pvticketlock (4.1.1) 5m02.0s 13m27.6s 15m49.9s
pvqspinlock (4.2-rc1) 3m39.9s 11.17.8s 12m19.9s
patched pvqspinlock 3m38.5s 9m27.8s 9m39.4s
unfair byte lock 4m23.8s 7m14.7s 8m50.4s
unfair queued lock 3m03.4s 3m29.7s 4m15.4s

Haswell-EX (4x8):

Kernel 32 vCPUs 48 vCPUs 60 vCPUs
------ -------- -------- --------
pvticketlock (4.1.1) 1m58.9s 18m57.0s 20m46.1s
pvqspinlock (4.2-rc1) 1m59.9s 18m44.2s 18m57.0s
patched pvqspinlock 2m01.7s 8m03.7s 8m29.5s
unfair byte lock 2m04.5s 2m46.7s 3m15.6s
unfair queued lock 1m59.4s 2m04.9s 2m18.6s

It can be seen that queued unfair lock has the best performance in almost all the cases. As can be seen in patch 4, the overhead of PV kicking and waiting is quite high. Unfair locks avoid those overhead and spend the time on productive work instead. On the other hand, the pvqspinlock is fair while the byte lock is not. The queued unfair lock is kind of in the middle between those two. It is not as fair as the pvqspinlock, but is fairer than the byte lock.

Looking at the PV locks, the pvqspinlock patch did increase performance in the overcommitted guests by about 20% in Westmere-EX and more than 2X in Haswell-EX. More investigation may be needed to find out why there was slowdown in Haswell-EX compared with Westmere-EX.

In conclusion, unfair lock is actually better performance-wise when a VM guest is over-committed. If there is no over-commitment, PV locks work fine, too.

When the VM guest was changed to NUMA pinned (direct mapping between physical and virtual CPUs) in the Westmere-EX system, the build times became:

Kernel 32 vCPUs
------ --------
pvticketlock (4.1.1) 2m47.1s
pvqspinlock (4.2-rc1) 2m45.9s
patched pvqspinlock 2m45.2s
unfair byte lock 2m45.4s
unfair queued lock 2m44.9s

It can be seen that the build times are virtually the same for all the configurations.

Waiman Long (7):
locking/pvqspinlock: Only kick CPU at unlock time locking/pvqspinlock: Allow vCPUs kick-ahead locking/pvqspinlock: Implement wait-early for overcommitted guest locking/pvqspinlock: Collect slowpath lock statistics locking/pvqspinlock: Add pending bit support locking/qspinlock: A fairer queued unfair lock locking/qspinlock: Collect queued unfair lock slowpath statistics

 arch/x86/Kconfig                    |    8 +
 arch/x86/include/asm/qspinlock.h    |   17 +-
 kernel/locking/qspinlock.c          |  140 ++++++++++-
 kernel/locking/qspinlock_paravirt.h |  436 ++++++++++++++++++++++++++++++++---
 kernel/locking/qspinlock_unfair.h   |  327 ++++++++++++++++++++++++++
 5 files changed, 880 insertions(+), 48 deletions(-)
 create mode 100644 kernel/locking/qspinlock_unfair.h
#1 lore raw

PATCH 1/7 locking/pvqspinlock: Only kick CPU at unlock time

For an over-committed guest with more vCPUs than physical CPUs available, it is possible that a vCPU may be kicked twice before getting the lock - one before it becomes queue head and once before it gets the lock. All these CPU kicking and halting (VMEXIT) can be expensive and slow down system performance.

This patch adds a new vCPU state (vcpu_hashed) which enables the code to delay CPU kicking until at unlock time. Once this state is set, the new lock holder will set _Q_SLOW_VAL and fill in the hash table on behalf of the halted queue head vCPU. The original vcpu_halted state will be used by pv_wait_node() only to differentiate other queue nodes from the qeue head.

---
 kernel/locking/qspinlock.c          |   10 ++--
 kernel/locking/qspinlock_paravirt.h |   83 ++++++++++++++++++++++++++---------
 2 files changed, 67 insertions(+), 26 deletions(-)
kernel/locking/qspinlock.c +5 -5
index 38c4920..d2e0fc1 100644
@@ -239,8 +239,8 @@ static __always_inline void set_locked(struct qspinlock *lock)
 
 static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_node(struct mcs_spinlock *node) { }
-static __always_inline void __pv_kick_node(struct mcs_spinlock *node) { }
-
+static __always_inline void __pv_scan_next(struct qspinlock *lock,
+					   struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_head(struct qspinlock *lock,
 					   struct mcs_spinlock *node) { }
 
@@ -248,7 +248,7 @@ static __always_inline void __pv_wait_head(struct qspinlock *lock,
 
 #define pv_init_node		__pv_init_node
 #define pv_wait_node		__pv_wait_node
-#define pv_kick_node		__pv_kick_node
+#define pv_scan_next		__pv_scan_next
 #define pv_wait_head		__pv_wait_head
 
 #ifdef CONFIG_PARAVIRT_SPINLOCKS
@@ -440,7 +440,7 @@ queue:
 		cpu_relax();
 
 	arch_mcs_spin_unlock_contended(&next->locked);
-	pv_kick_node(next);
+	pv_scan_next(lock, next);
 
 release:
 	/*
@@ -461,7 +461,7 @@ EXPORT_SYMBOL(queued_spin_lock_slowpath);
 
 #undef pv_init_node
 #undef pv_wait_node
-#undef pv_kick_node
+#undef pv_scan_next
 #undef pv_wait_head
 
 #undef  queued_spin_lock_slowpath
kernel/locking/qspinlock_paravirt.h +62 -21

158 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#2 lore raw

Re: [PATCH 1/7] locking/pvqspinlock: Only kick CPU at unlock time

kernel/locking/qspinlock_paravirt.h static void pv_wait_node(struct mcs_spinlock *node) · line 192

Quoted 9 lines · Waiman Long
kernel/locking/qspinlock_paravirt.h +2 -2
@@ -181,9 +187,9 @@ static void pv_wait_node(struct mcs_spinlock *node)
 			pv_wait(&pn->state, vcpu_halted);
 
 		/*
-		 * Reset the vCPU state to avoid unncessary CPU kicking
+		 * Reset the state except when vcpu_hashed is set.
 		 */
-		WRITE_ONCE(pn->state, vcpu_running);
+		cmpxchg(&pn->state, vcpu_halted, vcpu_running);

Why? Suppose we did get advanced into the hashed state, and then get a (spurious) wakeup, this means we'll observe our ->locked == 1 condition and fall out of pv_wait_node().

We'll then enter pv_wait_head(), which with your modification:

Quoted 6 lines
kernel/locking/qspinlock_paravirt.h +1 -1
@@ -229,19 +244,42 @@ static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
 {
 	struct pv_node *pn = (struct pv_node *)node;
 	struct __qspinlock *l = (void *)lock;
-	struct qspinlock **lp = NULL;
+	struct qspinlock **lp;

kernel/locking/qspinlock_paravirt.h static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node) · line 257

Quoted 10 lines
kernel/locking/qspinlock_paravirt.h +7 -0
 	int loop;
 
+	/*
+	 * Initialize lp to a non-NULL value if it has already been in the
+	 * pv_hashed state so that pv_hash() won't be called again.
+	 */
+	lp = (READ_ONCE(pn->state) == vcpu_hashed) ? (struct qspinlock **)1
+						   : NULL;
 	for (;;) {
+		WRITE_ONCE(pn->state, vcpu_running);

Will instantly and unconditionally write vcpu_running.

maintainer
#3 lore raw

Re: [PATCH 1/7] locking/pvqspinlock: Only kick CPU at unlock time

Quoted 25 lines · Peter Zijlstra

kernel/locking/qspinlock_paravirt.h static void pv_wait_node(struct mcs_spinlock *node) · line 192

Quoted 9 lines · Waiman Long
kernel/locking/qspinlock_paravirt.h +2 -2
@@ -181,9 +187,9 @@ static void pv_wait_node(struct mcs_spinlock *node)
 			pv_wait(&pn->state, vcpu_halted);
 
 		/*
-		 * Reset the vCPU state to avoid unncessary CPU kicking
+		 * Reset the state except when vcpu_hashed is set.
 		 */
-		WRITE_ONCE(pn->state, vcpu_running);
+		cmpxchg(&pn->state, vcpu_halted, vcpu_running);

Why? Suppose we did get advanced into the hashed state, and then get a (spurious) wakeup, this means we'll observe our ->locked == 1 condition and fall out of pv_wait_node().

We'll then enter pv_wait_head(), which with your modification:

Quoted 8 lines
kernel/locking/qspinlock_paravirt.h +1 -1
@@ -229,19 +244,42 @@ static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
 {
 	struct pv_node *pn = (struct pv_node *)node;
 	struct __qspinlock *l = (void *)lock;
-	struct qspinlock **lp = NULL;
+	struct qspinlock **lp;
 	int loop;
 
Quoted 6 lines

kernel/locking/qspinlock_paravirt.h static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node) · line 255

Quoted 6 lines
kernel/locking/qspinlock_paravirt.h +6 -0
+	/*
+	 * Initialize lp to a non-NULL value if it has already been in the
+	 * pv_hashed state so that pv_hash() won't be called again.
+	 */
+	lp = (READ_ONCE(pn->state) == vcpu_hashed) ? (struct qspinlock **)1
+						   : NULL;

Because that ^

Quoted 6 lines

Review static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node) · line 257

Quoted 2 lines
(quoted diff) +1 -0
 	for (;;) {
+		WRITE_ONCE(pn->state, vcpu_running);

Will instantly and unconditionally write vcpu_running.

#4 lore raw

Re: [PATCH 1/7] locking/pvqspinlock: Only kick CPU at unlock time

In reply to Peter Zijlstra
Quoted 24 lines · Peter Zijlstra

kernel/locking/qspinlock_paravirt.h static void pv_wait_node(struct mcs_spinlock *node) · line 192

Quoted 9 lines · Waiman Long
kernel/locking/qspinlock_paravirt.h +2 -2
@@ -181,9 +187,9 @@ static void pv_wait_node(struct mcs_spinlock *node)
  			pv_wait(&pn->state, vcpu_halted);
 
  		/*
-		 * Reset the vCPU state to avoid unncessary CPU kicking
+		 * Reset the state except when vcpu_hashed is set.
  		 */
-		WRITE_ONCE(pn->state, vcpu_running);
+		cmpxchg(&pn->state, vcpu_halted, vcpu_running);

Why? Suppose we did get advanced into the hashed state, and then get a (spurious) wakeup, this means we'll observe our ->locked == 1 condition and fall out of pv_wait_node().

We'll then enter pv_wait_head(), which with your modification:

Quoted 8 lines
kernel/locking/qspinlock_paravirt.h +1 -1
@@ -229,19 +244,42 @@ static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
  {
  	struct pv_node *pn = (struct pv_node *)node;
  	struct __qspinlock *l = (void *)lock;
-	struct qspinlock **lp = NULL;
+	struct qspinlock **lp;
  	int loop;
 
Quoted 11 lines

kernel/locking/qspinlock_paravirt.h static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node) · line 257

Quoted 8 lines
kernel/locking/qspinlock_paravirt.h +7 -0
+	/*
+	 * Initialize lp to a non-NULL value if it has already been in the
+	 * pv_hashed state so that pv_hash() won't be called again.
+	 */
+	lp = (READ_ONCE(pn->state) == vcpu_hashed) ? (struct qspinlock **)1
+						   : NULL;
  	for (;;) {
+		WRITE_ONCE(pn->state, vcpu_running);

Will instantly and unconditionally write vcpu_running.

This code is kind of complicated. I am going to get rid of the current tri-state setup, and switch to a separate sync variable for defer kicking.

Cheers,
Longman

maintainer
#5 lore raw

Re: [PATCH 1/7] locking/pvqspinlock: Only kick CPU at unlock time

In reply to Waiman Long
Quoted 53 lines · Waiman Long
(quoted diff) +26 -3
@@ -229,19 +244,42 @@ static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
 {
 	struct pv_node *pn = (struct pv_node *)node;
 	struct __qspinlock *l = (void *)lock;
-	struct qspinlock **lp = NULL;
+	struct qspinlock **lp;
 	int loop;
 
+	/*
+	 * Initialize lp to a non-NULL value if it has already been in the
+	 * pv_hashed state so that pv_hash() won't be called again.
+	 */
+	lp = (READ_ONCE(pn->state) == vcpu_hashed) ? (struct qspinlock **)1
+						   : NULL;
 	for (;;) {
+		WRITE_ONCE(pn->state, vcpu_running);
 		for (loop = SPIN_THRESHOLD; loop; loop--) {
 			if (!READ_ONCE(l->locked))
 				return;
 			cpu_relax();
 		}
 
-		WRITE_ONCE(pn->state, vcpu_halted);
+		/*
+		 * Recheck lock value after setting vcpu_hashed state
+		 *
+		 * [S] state = vcpu_hashed	[S] l->locked = 0
+		 *     MB			    MB
+		 * [L] l->locked		[L] state == vcpu_hashed
+		 *
+		 * Matches smp_store_mb() in __pv_queued_spin_unlock()
+		 */
+		smp_store_mb(pn->state, vcpu_hashed);
+
+		if (!READ_ONCE(l->locked)) {
+			WRITE_ONCE(pn->state, vcpu_running);
+			return;
+		}
+
 		if (!lp) { /* ONCE */
 			lp = pv_hash(lock, pn);
+
 			/*
 			 * lp must be set before setting _Q_SLOW_VAL
 			 *
@@ -305,13 +343,16 @@ __visible void __pv_queued_spin_unlock(struct qspinlock *lock)
 	 * Now that we have a reference to the (likely) blocked pv_node,
 	 * release the lock.
 	 */
-	smp_store_release(&l->locked, 0);
+	smp_store_mb(l->locked, 0);
 
 	/*

Review __visible void __pv_queued_spin_unlock(struct qspinlock *lock) · line 357

Quoted 10 lines
(quoted diff) +4 -1
 	 * At this point the memory pointed at by lock can be freed/reused,
 	 * however we can still use the pv_node to kick the CPU.
+	 * The other vCPU may not really be halted, but kicking an active
+	 * vCPU is harmless other than the additional latency in completing
+	 * the unlock.
 	 */
-	if (READ_ONCE(node->state) == vcpu_halted)
+	if (READ_ONCE(node->state) == vcpu_hashed)
 		pv_kick(node->cpu);
 }

I think most of that is not actually required; if we let pv_kick_node() set vcpu_hashed and avoid writing another value in pv_wait_head(), then __pv_queued_spin_unlock() has two cases:

- pv_kick_node() set _SLOW_VAL, which is the same 'thread' and things observe program order and we're trivially guaranteed to see node->state and the hash state.

- pv_wait_head() set it, and because it does the @lp branch the existing barrier from our cmpxchg() there setting _SLOW_VAL matches the cmpxchg() in __pv_queued_spin_unlock() observing _SLOW_VAL and we still observe the right node and hash values.

Giving something like the patch below.

On the s/pv_scan_node/pv_kick_node/ rename; conceptually we still make the waiter go, the fact that we don't wake it is an implementation detail.

---
Subject: locking/pvqspinlock: Only kick CPU at unlock time
From: Waiman Long <[email protected]>
Date: Sat, 11 Jul 2015 16:36:52 -0400

For an over-committed guest with more vCPUs than physical CPUs available, it is possible that a vCPU may be kicked twice before getting the lock - once before it becomes queue head and once again before it gets the lock. All these CPU kicking and halting (VMEXIT) can be expensive and slow down system performance.

This patch adds a new vCPU state (vcpu_hashed) which enables the code to delay CPU kicking until at unlock time. Once this state is set, the new lock holder will set _Q_SLOW_VAL and fill in the hash table on behalf of the halted queue head vCPU. The original vcpu_halted state will be used by pv_wait_node() only to differentiate other queue nodes from the qeue head.

Cc Scott J Norton <[email protected]>
Cc Douglas Hatch <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
[peterz: s/pv_scan_node/pv_kick_node/, simplification of pv_wait_head()]
---
 kernel/locking/qspinlock.c          |    6 +--
 kernel/locking/qspinlock_paravirt.h |   72 +++++++++++++++++++++++++++---------
 2 files changed, 57 insertions(+), 21 deletions(-)
kernel/locking/qspinlock.c +3 -3
@@ -239,8 +239,8 @@ static __always_inline void set_locked(s
 
 static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_node(struct mcs_spinlock *node) { }
-static __always_inline void __pv_kick_node(struct mcs_spinlock *node) { }
-
+static __always_inline void __pv_kick_node(struct qspinlock *lock,
+					   struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_head(struct qspinlock *lock,
 					   struct mcs_spinlock *node) { }
 
@@ -440,7 +440,7 @@ void queued_spin_lock_slowpath(struct qs
 		cpu_relax();
 
 	arch_mcs_spin_unlock_contended(&next->locked);
-	pv_kick_node(next);
+	pv_kick_node(lock, next);
 
 release:
 	/*
kernel/locking/qspinlock_paravirt.h +54 -18

141 lines not on this page · raw message

maintainer
#6 lore raw

Re: [PATCH 1/7] locking/pvqspinlock: Only kick CPU at unlock time

kernel/locking/qspinlock_paravirt.h static void pv_wait_head(struct qspinloc · line 274

Quoted 12 lines · Peter Zijlstra
kernel/locking/qspinlock_paravirt.h +7 -1
@@ -239,9 +265,16 @@ static void pv_wait_head(struct qspinloc
 			cpu_relax();
 		}
 
-		WRITE_ONCE(pn->state, vcpu_halted);
+		/*
+		 * Either pv_kick_node() advanced us and ->state is already
+		 * vcpu_hashed and this store is superfluous, or this is the
+		 * first in which case the below cmpxchg() provides the
+		 * required barriers.
+		 */
+		WRITE_ONCE(pn->state, vcpu_hashed);

The easier option is of course to just move this store into the branch below. That immediately clarifies the logic and avoids the superfluous store.

Quoted 6 lines
kernel/locking/qspinlock_paravirt.h +1 -0
 		if (!lp) { /* ONCE */
 			lp = pv_hash(lock, pn);
+
 			/*
 			 * lp must be set before setting _Q_SLOW_VAL
 			 *
#7 lore raw

Re: [PATCH 1/7] locking/pvqspinlock: Only kick CPU at unlock time

In reply to Peter Zijlstra
Quoted 68 lines · Peter Zijlstra
Quoted 53 lines · Waiman Long
kernel/locking/qspinlock_paravirt.h +26 -3
@@ -229,19 +244,42 @@ static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
  {
  	struct pv_node *pn = (struct pv_node *)node;
  	struct __qspinlock *l = (void *)lock;
-	struct qspinlock **lp = NULL;
+	struct qspinlock **lp;
  	int loop;
 
+	/*
+	 * Initialize lp to a non-NULL value if it has already been in the
+	 * pv_hashed state so that pv_hash() won't be called again.
+	 */
+	lp = (READ_ONCE(pn->state) == vcpu_hashed) ? (struct qspinlock **)1
+						   : NULL;
  	for (;;) {
+		WRITE_ONCE(pn->state, vcpu_running);
  		for (loop = SPIN_THRESHOLD; loop; loop--) {
  			if (!READ_ONCE(l->locked))
  				return;
  			cpu_relax();
  		}
 
-		WRITE_ONCE(pn->state, vcpu_halted);
+		/*
+		 * Recheck lock value after setting vcpu_hashed state
+		 *
+		 * [S] state = vcpu_hashed	[S] l->locked = 0
+		 *     MB			    MB
+		 * [L] l->locked		[L] state == vcpu_hashed
+		 *
+		 * Matches smp_store_mb() in __pv_queued_spin_unlock()
+		 */
+		smp_store_mb(pn->state, vcpu_hashed);
+
+		if (!READ_ONCE(l->locked)) {
+			WRITE_ONCE(pn->state, vcpu_running);
+			return;
+		}
+
  		if (!lp) { /* ONCE */
  			lp = pv_hash(lock, pn);
+
  			/*
  			 * lp must be set before setting _Q_SLOW_VAL
  			 *
@@ -305,13 +343,16 @@ __visible void __pv_queued_spin_unlock(struct qspinlock *lock)
  	 * Now that we have a reference to the (likely) blocked pv_node,
  	 * release the lock.
  	 */
-	smp_store_release(&l->locked, 0);
+	smp_store_mb(l->locked, 0);
 
  	/*

kernel/locking/qspinlock_paravirt.h __visible void __pv_queued_spin_unlock(struct qspinlock *lock) · line 357

Quoted 10 lines
kernel/locking/qspinlock_paravirt.h +4 -1
  	 * At this point the memory pointed at by lock can be freed/reused,
  	 * however we can still use the pv_node to kick the CPU.
+	 * The other vCPU may not really be halted, but kicking an active
+	 * vCPU is harmless other than the additional latency in completing
+	 * the unlock.
  	 */
-	if (READ_ONCE(node->state) == vcpu_halted)
+	if (READ_ONCE(node->state) == vcpu_hashed)
  		pv_kick(node->cpu);
  }

I think most of that is not actually required; if we let pv_kick_node() set vcpu_hashed and avoid writing another value in pv_wait_head(), then __pv_queued_spin_unlock() has two cases:

Quoted 3 lines

- pv_kick_node() set _SLOW_VAL, which is the same 'thread' and things observe program order and we're trivially guaranteed to see node->state and the hash state.

I just found out that letting pv_kick_node() to wakeup vCPUs at locking time can have a slightly better performance in some cases. So I am going to keep it, but defer kicking to the unlock time when we can do multiple kicks. The advantage of doing it at unlock time is that the kicking can be done outside of the critical section. So I am going to keep the current name.

Cheers,
Longman

#8 lore raw

[tip:locking/core] locking/pvqspinlock: Only kick CPU at unlock time

In reply to Waiman Long
locking/pvqspinlock: Only kick CPU at unlock time
Commit-ID:  75d2270280686bff21b9ba66c7f3dd379c887981
Gitweb:     http://git.kernel.org/tip/75d2270280686bff21b9ba66c7f3dd379c887981
Author:     Waiman Long <[email protected]>
AuthorDate: Sat, 11 Jul 2015 16:36:52 -0400
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 3 Aug 2015 10:57:11 +0200

locking/pvqspinlock: Only kick CPU at unlock time

For an over-committed guest with more vCPUs than physical CPUs available, it is possible that a vCPU may be kicked twice before getting the lock - once before it becomes queue head and once again before it gets the lock. All these CPU kicking and halting (VMEXIT) can be expensive and slow down system performance.

This patch adds a new vCPU state (vcpu_hashed) which enables the code to delay CPU kicking until at unlock time. Once this state is set, the new lock holder will set _Q_SLOW_VAL and fill in the hash table on behalf of the halted queue head vCPU. The original vcpu_halted state will be used by pv_wait_node() only to differentiate other queue nodes from the qeue head.

Cc Andrew Morton <[email protected]>
Cc Douglas Hatch <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Scott J Norton <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 kernel/locking/qspinlock.c          |  6 ++--
 kernel/locking/qspinlock_paravirt.h | 66 +++++++++++++++++++++++++++----------
 2 files changed, 51 insertions(+), 21 deletions(-)
kernel/locking/qspinlock.c +3 -3
index 38c4920..337c881 100644
@@ -239,8 +239,8 @@ static __always_inline void set_locked(struct qspinlock *lock)
 
 static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_node(struct mcs_spinlock *node) { }
-static __always_inline void __pv_kick_node(struct mcs_spinlock *node) { }
-
+static __always_inline void __pv_kick_node(struct qspinlock *lock,
+					   struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_head(struct qspinlock *lock,
 					   struct mcs_spinlock *node) { }
 
@@ -440,7 +440,7 @@ queue:
 		cpu_relax();
 
 	arch_mcs_spin_unlock_contended(&next->locked);
-	pv_kick_node(next);
+	pv_kick_node(lock, next);
 
 release:
 	/*
kernel/locking/qspinlock_paravirt.h +48 -18

135 lines not on this page · raw message

#9 lore raw

PATCH 2/7 locking/pvqspinlock: Allow vCPUs kick-ahead

In reply to Waiman Long

Frequent CPU halting (vmexit) and CPU kicking (vmenter) lengthens critical section and block forward progress. This patch implements a kick-ahead mechanism where the unlocker will kick the queue head vCPUs as well as up to two additional vCPUs next to the queue head if they were halted. The kickings are done after exiting the critical section to improve parallelism.

The amount of kick-ahead allowed depends on the number of vCPUs in the VM guest. This change should improve overall system performance in a busy overcommitted guest.

---
 kernel/locking/qspinlock_paravirt.h |   71 ++++++++++++++++++++++++++++++++++-
 1 files changed, 69 insertions(+), 2 deletions(-)
kernel/locking/qspinlock_paravirt.h +69 -2
index d302c39..4c1a299 100644
@@ -67,6 +67,12 @@ static struct pv_hash_entry *pv_lock_hash;
 static unsigned int pv_lock_hash_bits __read_mostly;
 
 /*
+ * Allow kick-ahead of vCPUs at unlock time
+ */
+#define PV_KICK_AHEAD_MAX	4
+static int pv_kick_ahead __read_mostly;
+
+/*
  * Allocate memory for the PV qspinlock hash buckets
  *
  * This function should be called from the paravirt spinlock initialization
@@ -74,7 +80,16 @@ static unsigned int pv_lock_hash_bits __read_mostly;
  */
 void __init __pv_init_lock_hash(void)
 {
-	int pv_hash_size = ALIGN(4 * num_possible_cpus(), PV_HE_PER_LINE);
+	int ncpus = num_possible_cpus();
+	int pv_hash_size = ALIGN(4 * ncpus, PV_HE_PER_LINE);
+	int i;
+
+	/*
+	 * The minimum number of vCPUs required in each kick-ahead level
+	 */
+	static const u8 kick_ahead_threshold[PV_KICK_AHEAD_MAX] = {
+		4, 8, 16, 32
+	};
 
 	if (pv_hash_size < PV_HE_MIN)
 		pv_hash_size = PV_HE_MIN;
@@ -88,6 +103,18 @@ void __init __pv_init_lock_hash(void)
 					       pv_hash_size, 0, HASH_EARLY,
 					       &pv_lock_hash_bits, NULL,
 					       pv_hash_size, pv_hash_size);
+	/*
+	 * Enable the unlock kick ahead mode according to the number of
+	 * vCPUs available.
+	 */
+	for (i = PV_KICK_AHEAD_MAX; i > 0; i--)
+		if (ncpus >= kick_ahead_threshold[i - 1]) {
+			pv_kick_ahead = i;
+			break;
+		}
+	if (pv_kick_ahead)
+		printk(KERN_INFO "PV unlock kick ahead level %d enabled\n",
+		       pv_kick_ahead);
 }
 
 #define for_each_hash_entry(he, offset, hash)						\
@@ -317,13 +344,33 @@ static void pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *node)
 }
 
 /*
+ * Helper to get the address of the next kickable node
+ * The node has to be in the halted state and is being transitioned to
+ * running state by this function. Otherwise, NULL will be returned.
+ */
+static inline struct pv_node *pv_get_kick_node(struct pv_node *node)
+{
+	struct pv_node *next = (struct pv_node *)READ_ONCE(node->mcs.next);
+
+	if (!next)
+		return NULL;
+
+	if ((READ_ONCE(next->state) != vcpu_halted) ||
+	    (cmpxchg(&next->state, vcpu_halted, vcpu_running) != vcpu_halted))
+		next = NULL;	/* No kicking is needed */
+
+	return next;
+}
+
+/*
  * PV version of the unlock function to be used in stead of
  * queued_spin_unlock().
  */
 __visible void __pv_queued_spin_unlock(struct qspinlock *lock)
 {
 	struct __qspinlock *l = (void *)lock;
-	struct pv_node *node;
+	struct pv_node *node, *nxt, *next[PV_KICK_AHEAD_MAX];
+	int i, nr_kick;
 
 	/*
 	 * We must not unlock if SLOW, because in that case we must first
@@ -340,6 +387,20 @@ __visible void __pv_queued_spin_unlock(struct qspinlock *lock)
 	node = pv_unhash(lock);
 
 	/*
+	 * Implement kick-ahead mode
+	 *
+	 * Access the next group of nodes, if available, and prepare to kick
+	 * them after releasing the lock if they are in the halted state. This
+	 * should improve performance on an overcommitted system.
+	 */
+	for (nr_kick = 0, nxt = node; nr_kick < pv_kick_ahead;
+	     nxt = next[nr_kick], nr_kick++) {
+		next[nr_kick] = pv_get_kick_node(nxt);
+		if (!next[nr_kick])
+			break;
+	}
+
+	/*
 	 * Now that we have a reference to the (likely) blocked pv_node,
 	 * release the lock.
 	 */
@@ -354,6 +415,12 @@ __visible void __pv_queued_spin_unlock(struct qspinlock *lock)
 	 */
 	if (READ_ONCE(node->state) == vcpu_hashed)
 		pv_kick(node->cpu);
+
+	/*
+	 * Kick the next group of vCPUs, if available.
+	 */
+	for (i = 0; i < nr_kick; i++)
+		pv_kick(next[i]->cpu);
 }
 /*
  * Include the architecture specific callee-save thunk of the
Signature · 1 line
1.7.1
maintainer
#10 lore raw

Re: [PATCH 2/7] locking/pvqspinlock: Allow vCPUs kick-ahead

Quoted 10 lines · Waiman Long

Frequent CPU halting (vmexit) and CPU kicking (vmenter) lengthens critical section and block forward progress. This patch implements a kick-ahead mechanism where the unlocker will kick the queue head vCPUs as well as up to two additional vCPUs next to the queue head if they were halted. The kickings are done after exiting the critical section to improve parallelism.

The amount of kick-ahead allowed depends on the number of vCPUs in the VM guest. This change should improve overall system performance in a busy overcommitted guest.

-ENONUMBERS... also highly workload sensitive, if the lock hold time is just above our spin time you're wasting gobs of runtime.

#11 lore raw

Re: [PATCH 2/7] locking/pvqspinlock: Allow vCPUs kick-ahead

Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long

Frequent CPU halting (vmexit) and CPU kicking (vmenter) lengthens critical section and block forward progress. This patch implements a kick-ahead mechanism where the unlocker will kick the queue head vCPUs as well as up to two additional vCPUs next to the queue head if they were halted. The kickings are done after exiting the critical section to improve parallelism.

Quoted 5 lines
Quoted 3 lines

The amount of kick-ahead allowed depends on the number of vCPUs in the VM guest. This change should improve overall system performance in a busy overcommitted guest.

-ENONUMBERS... also highly workload sensitive, if the lock hold time is just above our spin time you're wasting gobs of runtime.

Currently SPIN_THRESHOLD is (1<<15). My test of the pause instruction is about 3ns in my 2.5GHz Haswell-EX system. That translates to a spinning time of at least 100us. I don't think we have critical sections in the kernel that take that long. I also found out that the kick-to-wakeup time can be pretty long, kicking ahead will enable the next cpu waiting in line to get the lock faster as some of the wakeup time will overlap with the critical sections of previous CPUs. Will have the performance number in the v2 patch.

Cheers,
Longman

#12 lore raw

PATCH 3/7 locking/pvqspinlock: Implement wait-early for overcommitted guest

In reply to Waiman Long

In an overcommitted guest where some vCPUs have to be halted to make forward progress in other areas, it is highly likely that a vCPU later in the spinlock queue will be spinning while the ones earlier in the queue would have been halted already. The spinning in the later vCPUs is then just a waste of precious CPU cycles because they are not going to get the lock soon as the earlier ones have to be woken up and take their turn to get the lock.

This patch implements a wait-early mechanism where the vCPU will call pv_wait() earlier if the previous vCPU is in the halted state already. In this case, it will spin less before calling pv_wait(). On the other hand, if the previous vCPU was running and then becomes halted, the current vCPU will call pv_wait() immmediately in this case.

This patch also separates the spin threshold for queue head and queue nodes. It favors the queue head by allowing it to spin longer before calling pv_wait().

---
 kernel/locking/qspinlock.c          |    5 ++-
 kernel/locking/qspinlock_paravirt.h |   52 +++++++++++++++++++++++++++++++++--
 2 files changed, 52 insertions(+), 5 deletions(-)
kernel/locking/qspinlock.c +3 -2
index d2e0fc1..782bc18 100644
@@ -238,7 +238,8 @@ static __always_inline void set_locked(struct qspinlock *lock)
  */
 
 static __always_inline void __pv_init_node(struct mcs_spinlock *node) { }
-static __always_inline void __pv_wait_node(struct mcs_spinlock *node) { }
+static __always_inline void __pv_wait_node(struct mcs_spinlock *node,
+					   struct mcs_spinlock *prev) { }
 static __always_inline void __pv_scan_next(struct qspinlock *lock,
 					   struct mcs_spinlock *node) { }
 static __always_inline void __pv_wait_head(struct qspinlock *lock,
@@ -391,7 +392,7 @@ queue:
 		prev = decode_tail(old);
 		WRITE_ONCE(prev->next, node);
 
-		pv_wait_node(node);
+		pv_wait_node(node, prev);
 		arch_mcs_spin_lock_contended(&node->locked);
 	}
 
kernel/locking/qspinlock_paravirt.h +49 -3

77 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#13 lore raw

Re: [PATCH 3/7] locking/pvqspinlock: Implement wait-early for overcommitted guest

Quoted 14 lines · Waiman Long

In an overcommitted guest where some vCPUs have to be halted to make forward progress in other areas, it is highly likely that a vCPU later in the spinlock queue will be spinning while the ones earlier in the queue would have been halted already. The spinning in the later vCPUs is then just a waste of precious CPU cycles because they are not going to get the lock soon as the earlier ones have to be woken up and take their turn to get the lock.

This patch implements a wait-early mechanism where the vCPU will call pv_wait() earlier if the previous vCPU is in the halted state already. In this case, it will spin less before calling pv_wait(). On the other hand, if the previous vCPU was running and then becomes halted, the current vCPU will call pv_wait() immmediately in this case.

Quoted 3 lines

This patch also separates the spin threshold for queue head and queue nodes. It favors the queue head by allowing it to spin longer before calling pv_wait().

-ENONUMBERS

#14 lore raw

Re: [PATCH 3/7] locking/pvqspinlock: Implement wait-early for overcommitted guest

In reply to Waiman Long
Quoted 7 lines · Waiman Long

/*
+ * Queued Spinlock Spin Thresholds
+ * -------------------------------
+ * Because of the cacheline contention effect of the ticket spinlock, the
+ * same spin threshold for queued spinlock will run a bit faster. So we set
+ * a slight larger threshold for the queue head (1.25X) while the other queue
+ * nodes will keep the same threshold.

How did you come to 1.25x? These sort of magic numbers scare me.

#15 lore raw

Re: [PATCH 3/7] locking/pvqspinlock: Implement wait-early for overcommitted guest

Quoted 10 lines · Davidlohr Bueso
Quoted 7 lines · Waiman Long

/*
+ * Queued Spinlock Spin Thresholds
+ * -------------------------------
+ * Because of the cacheline contention effect of the ticket spinlock, the
+ * same spin threshold for queued spinlock will run a bit faster. So we set
+ * a slight larger threshold for the queue head (1.25X) while the other queue
+ * nodes will keep the same threshold.

How did you come to 1.25x? These sort of magic numbers scare me.

Yes, this is kind of arbitrary. I am going to get rid of it in the v2 patch.

Cheers,
Longman

#16 lore raw

PATCH 4/7 locking/pvqspinlock: Collect slowpath lock statistics

In reply to Waiman Long

This patch enables the accumulation of kicking and waiting related PV qspinlock statistics when the new QUEUED_LOCK_STAT configuration option is selected. It also enables the collection of kicking and wakeup latencies which have a heavy dependency on the CPUs being used.

The measured latencies for different CPUs are:

	CPU		Wakeup		Kicking
	---		------		-------
	Haswell-EX	26.4us		 9.2us
	Westmere-EX	99.4US		25.5us

So Haswell is much faster than Westmere.

The accumulated lock statistics will be reported in debugfs under the pv-qspinlock directory.

---
 arch/x86/Kconfig                    |    7 ++
 kernel/locking/qspinlock_paravirt.h |  173 ++++++++++++++++++++++++++++++++++-
 2 files changed, 177 insertions(+), 3 deletions(-)
arch/x86/Kconfig +7 -0
index 55bced1..299a1c4 100644
@@ -679,6 +679,13 @@ config PARAVIRT_SPINLOCKS
 
 	  If you are unsure how to answer this question, answer Y.
 
+config QUEUED_LOCK_STAT
+	bool "Paravirt queued lock statistics"
+	depends on PARAVIRT && DEBUG_FS && QUEUED_SPINLOCKS
+	---help---
+	  Enable the collection of statistical data on the behavior of
+	  paravirtualized queued spinlocks and report them on debugfs.
+
 source "arch/x86/xen/Kconfig"
 
 config KVM_GUEST
kernel/locking/qspinlock_paravirt.h +170 -3

238 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#17 lore raw

Re: [PATCH 4/7] locking/pvqspinlock: Collect slowpath lock statistics

Quoted 12 lines · Waiman Long

This patch enables the accumulation of kicking and waiting related PV qspinlock statistics when the new QUEUED_LOCK_STAT configuration option is selected. It also enables the collection of kicking and wakeup latencies which have a heavy dependency on the CPUs being used.

The measured latencies for different CPUs are:

	CPU		Wakeup		Kicking
	---		------		-------
	Haswell-EX	26.4us		 9.2us
	Westmere-EX	99.4US		25.5us
Quoted 4 lines

So Haswell is much faster than Westmere.

The accumulated lock statistics will be reported in debugfs under the pv-qspinlock directory.

You've again stuck this behind non-trivial patches :-(

#18 lore raw

Re: [PATCH 4/7] locking/pvqspinlock: Collect slowpath lock statistics

Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Waiman Long

This patch enables the accumulation of kicking and waiting related PV qspinlock statistics when the new QUEUED_LOCK_STAT configuration option is selected. It also enables the collection of kicking and wakeup latencies which have a heavy dependency on the CPUs being used.

The measured latencies for different CPUs are:

	CPU		Wakeup		Kicking
	---		------		-------
	Haswell-EX	26.4us		 9.2us
	Westmere-EX	99.4US		25.5us
Quoted 5 lines
Quoted 4 lines

So Haswell is much faster than Westmere.

The accumulated lock statistics will be reported in debugfs under the pv-qspinlock directory.

You've again stuck this behind non-trivial patches :-(

I will move that patch into the front in the new patchset.

Cheers,
Longman

#19 lore raw

PATCH 5/7 locking/pvqspinlock: Add pending bit support

In reply to Waiman Long

Like the native qspinlock, using the pending bit when it is lightly loaded to acquire the lock is faster than going through the PV queuing process which is even slower than the native queuing process. It also avoids loading two additional cachelines (the MCS and PV nodes).

This patch adds the pending bit support for PV qspinlock. The pending bit code has a smaller spin threshold. It will default back to the queuing method if it cannot acquired the lock within a certain time limit.

---
 kernel/locking/qspinlock.c          |   27 +++++++++++++++-
 kernel/locking/qspinlock_paravirt.h |   61 +++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+), 1 deletions(-)
kernel/locking/qspinlock.c +26 -1
index 782bc18..5a25e89 100644
@@ -162,6 +162,17 @@ static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
 	WRITE_ONCE(l->locked_pending, _Q_LOCKED_VAL);
 }
 
+/**
+ * clear_pending - clear the pending bit.
+ * @lock: Pointer to queued spinlock structure
+ */
+static __always_inline void clear_pending(struct qspinlock *lock)
+{
+	struct __qspinlock *l = (void *)lock;
+
+	WRITE_ONCE(l->pending, 0);
+}
+
 /*
  * xchg_tail - Put in the new queue tail code word & retrieve previous one
  * @lock : Pointer to queued spinlock structure
@@ -193,6 +204,15 @@ static __always_inline void clear_pending_set_locked(struct qspinlock *lock)
 }
 
 /**
+ * clear_pending - clear the pending bit.
+ * @lock: Pointer to queued spinlock structure
+ */
+static __always_inline void clear_pending(struct qspinlock *lock)
+{
+	atomic_add(-_Q_PENDING_VAL, &lock->val);
+}
+
+/**
  * xchg_tail - Put in the new queue tail code word & retrieve previous one
  * @lock : Pointer to queued spinlock structure
  * @tail : The new queue tail code word
@@ -246,6 +266,7 @@ static __always_inline void __pv_wait_head(struct qspinlock *lock,
 					   struct mcs_spinlock *node) { }
 
 #define pv_enabled()		false
+#define pv_pending_lock(l, v)	false
 
 #define pv_init_node		__pv_init_node
 #define pv_wait_node		__pv_wait_node
@@ -287,8 +308,11 @@ void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
 
 	BUILD_BUG_ON(CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS));
 
-	if (pv_enabled())
+	if (pv_enabled()) {
+		if (pv_pending_lock(lock, val))
+			return;	/* Got the lock via pending bit */
 		goto queue;
+	}
 
 	if (virt_queued_spin_lock(lock))
 		return;
@@ -464,6 +488,7 @@ EXPORT_SYMBOL(queued_spin_lock_slowpath);
 #undef pv_wait_node
 #undef pv_scan_next
 #undef pv_wait_head
+#undef pv_pending_lock
 
 #undef  queued_spin_lock_slowpath
 #define queued_spin_lock_slowpath	__pv_queued_spin_lock_slowpath
kernel/locking/qspinlock_paravirt.h +61 -0

85 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#20 lore raw

Re: [PATCH 5/7] locking/pvqspinlock: Add pending bit support

Quoted 9 lines · Waiman Long

Like the native qspinlock, using the pending bit when it is lightly loaded to acquire the lock is faster than going through the PV queuing process which is even slower than the native queuing process. It also avoids loading two additional cachelines (the MCS and PV nodes).

This patch adds the pending bit support for PV qspinlock. The pending bit code has a smaller spin threshold. It will default back to the queuing method if it cannot acquired the lock within a certain time limit.

-ENONUMBERS

#21 lore raw

Re: [PATCH 5/7] locking/pvqspinlock: Add pending bit support

Quoted 11 lines · Peter Zijlstra
Quoted 9 lines · Waiman Long

Like the native qspinlock, using the pending bit when it is lightly loaded to acquire the lock is faster than going through the PV queuing process which is even slower than the native queuing process. It also avoids loading two additional cachelines (the MCS and PV nodes).

This patch adds the pending bit support for PV qspinlock. The pending bit code has a smaller spin threshold. It will default back to the queuing method if it cannot acquired the lock within a certain time limit.

-ENONUMBERS

Will send out an updated patchset with patch-to-patch performance numbers.

Cheers,
Longman

#22 lore raw

PATCH 6/7 locking/qspinlock: A fairer queued unfair lock

In reply to Waiman Long

For a virtual guest with the qspinlock patch, a simple unfair byte lock will be used if PV spinlock is not configured in or the hypervisor isn't either KVM or Xen. The byte lock works fine with small guest of just a few vCPUs. On a much larger guest, however, byte lock can have the following problems:

1) Lock starvation is a real possibility especially if the number of vCPUs is large.
2) The constant reading and occasionally writing to the lock word can put a lot of cacheline contention traffic on the affected cacheline.

This patch introduces a queue-based unfair lock where all the vCPUs on the queue can opportunistically steal the lock, but the frequency of doing so decreases the further it is away from the queue head. It can encourage a more FIFO like order of getting the lock and hence greatly reduce the chance of lock starvation. It can also reduce cacheline contention problem and so improve the performance of the system.

This patch has no impact on native qspinlock performance at all. The unfair lock code will only be compiled in if CONFIG_HYPERVISOR_GUEST is defined.

A microbenchmark of running 1 million lock-unlock operation for various number of threads running on a KVM guest with 32 pinned vCPUs and 4 vCPUs per node (8 nodes). This microbenchmark is intended to measure the variability of the execution times.

	Kernel		Threads	   Min/Avg/Max(ms)	  SD(ms)
	------		-------	   ---------------	  ------
    Unfair byte lock	   4	  133.1/386.0/509.0	  153.48
    			   8	 720.5/939.5/1,068.0	  117.08
			  16	2,237.8/6,045.8/7,550.3	  1747.37
			  32	5,880.2/37,028.2/44,668.7 10136.30
    Unfair qspinlock	   4	  326.1/453.7/523.0	  80.44
    			   8	 681.6/1,126.4/1,486.5	  304.85
			  16	1,543.0/3,633.4/4,568.1	  1000.47
			  32	2,356.8/7,103.3/7,894.9	  1231.11

With small number of contending threads, both the performance and variability of both types of unfair lock are similar. However, when the number of contending threads increases, the byte lock has a much higher variability than the unfair qspinlock.

---
 arch/x86/include/asm/qspinlock.h  |   17 ++--
 kernel/locking/qspinlock.c        |   98 ++++++++++++++-
 kernel/locking/qspinlock_unfair.h |  238 +++++++++++++++++++++++++++++++++++++
 3 files changed, 340 insertions(+), 13 deletions(-)
 create mode 100644 kernel/locking/qspinlock_unfair.h
arch/x86/include/asm/qspinlock.h +9 -8
index 9d51fae..bc82ace 100644
@@ -39,18 +39,19 @@ static inline void queued_spin_unlock(struct qspinlock *lock)
 }
 #endif
 
-#define virt_queued_spin_lock virt_queued_spin_lock
+#ifdef CONFIG_HYPERVISOR_GUEST
+#ifndef static_cpu_has_hypervisor
+#define static_cpu_has_hypervisor	static_cpu_has(X86_FEATURE_HYPERVISOR)
+#endif
 
-static inline bool virt_queued_spin_lock(struct qspinlock *lock)
+#define queued_spin_trylock_unfair queued_spin_trylock_unfair
+static inline bool queued_spin_trylock_unfair(struct qspinlock *lock)
 {
-	if (!static_cpu_has(X86_FEATURE_HYPERVISOR))
-		return false;
-
-	while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0)
-		cpu_relax();
+	u8 *l = (u8 *)lock;
 
-	return true;
+	return !READ_ONCE(*l) && (xchg(l, _Q_LOCKED_VAL) == 0);
 }
+#endif
 
 #include <asm-generic/qspinlock.h>
 
kernel/locking/qspinlock.c +93 -5

139 lines not on this page · raw message

kernel/locking/qspinlock_unfair.h new file +238 -0

238 lines not on this page · raw message

Signature · 1 line
1.7.1
maintainer
#23 lore raw

Re: [PATCH 6/7] locking/qspinlock: A fairer queued unfair lock

Quoted 3 lines · Waiman Long

For a virtual guest with the qspinlock patch, a simple unfair byte lock will be used if PV spinlock is not configured in or the hypervisor isn't either KVM or Xen.

Why do we care about this case enough to add over 300 lines of code?

#24 lore raw

Re: [PATCH 6/7] locking/qspinlock: A fairer queued unfair lock

Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long

For a virtual guest with the qspinlock patch, a simple unfair byte lock will be used if PV spinlock is not configured in or the hypervisor isn't either KVM or Xen.

Why do we care about this case enough to add over 300 lines of code?

From my testing, I found the queued unfair lock to be superior to both the byte lock or the PV qspinlock when the VM is overcommitted. My current opinion is to use PV qspinlock for VMs that are not likely to run into the overcommited problem. For other VMs that are overcommitted, it will be better to use the queued unfair lock. However, this is a choice that the system administrators have to made. That is also the reason why I sent out another patch to add a KVM command line option to disable PV spinlock like what Xen already has. In this way, depending on how the kernel is booted, we can choose either PV qspinlock or queued unfair lock.

Cheers,
Longman

maintainer
#25 lore raw

Re: [PATCH 6/7] locking/qspinlock: A fairer queued unfair lock

Quoted 7 lines · Waiman Long
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long

For a virtual guest with the qspinlock patch, a simple unfair byte lock will be used if PV spinlock is not configured in or the hypervisor isn't either KVM or Xen.

Why do we care about this case enough to add over 300 lines of code?

Quoted 9 lines

From my testing, I found the queued unfair lock to be superior to both the byte lock or the PV qspinlock when the VM is overcommitted. My current opinion is to use PV qspinlock for VMs that are not likely to run into the overcommited problem. For other VMs that are overcommitted, it will be better to use the queued unfair lock. However, this is a choice that the system administrators have to made. That is also the reason why I sent out another patch to add a KVM command line option to disable PV spinlock like what Xen already has. In this way, depending on how the kernel is booted, we can choose either PV qspinlock or queued unfair lock.

No, we're not going to add another 300 line lock implementation and a knob.

#26 lore raw

PATCH 7/7 locking/qspinlock: Collect queued unfair lock slowpath statistics

In reply to Waiman Long

This patch enables the accumulation of unfair qspinlock statistics when the CONFIG_QUEUED_LOCK_STAT configuration parameter is set.

The accumulated lock statistics will be reported in debugfs under the unfair-qspinlock directory.

On a KVM guest with 32 vCPUs, the statistics counts after bootup were:

lsteal_cnts = 172219 2377 425 118 33 8 5 12 14 0 0 0 trylock_cnt = 1495372

So most of the lock stealing happened in the initial trylock before entering the queue. Once a vCPU is in the queue, the chance of getting the lock drop off significantly the further it is away from queue head.

---
 arch/x86/Kconfig                  |    7 ++-
 kernel/locking/qspinlock.c        |    2 +-
 kernel/locking/qspinlock_unfair.h |   89 +++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 4 deletions(-)
arch/x86/Kconfig +4 -3
index 299a1c4..aee6236 100644
@@ -680,11 +680,12 @@ config PARAVIRT_SPINLOCKS
 	  If you are unsure how to answer this question, answer Y.
 
 config QUEUED_LOCK_STAT
-	bool "Paravirt queued lock statistics"
-	depends on PARAVIRT && DEBUG_FS && QUEUED_SPINLOCKS
+	bool "Paravirt/Unfair queued lock statistics"
+	depends on DEBUG_FS && QUEUED_SPINLOCKS
 	---help---
 	  Enable the collection of statistical data on the behavior of
-	  paravirtualized queued spinlocks and report them on debugfs.
+	  paravirtualized and unfair queued spinlocks and report them
+	  on debugfs.
 
 source "arch/x86/xen/Kconfig"
 
kernel/locking/qspinlock.c +1 -1
index 65dead9..12e2e89 100644
@@ -538,7 +538,7 @@ EXPORT_SYMBOL(queued_spin_lock_slowpath);
 #ifdef queued_spin_trylock
 #undef queued_spin_trylock
 #endif
-#define queued_spin_trylock		queued_spin_trylock_unfair
+#define queued_spin_trylock		__queued_spin_trylock_unfair
 
 /*
  * The unfair lock code is used internally and so don't need to be exported
kernel/locking/qspinlock_unfair.h +89 -0

107 lines not on this page · raw message

Signature · 1 line
1.7.1