[PATCH v5 6/6] locking/pvqspinlock: Queue node adaptive spinning
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. 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. Reducing the spinning threshold is found to improve performance in an overcommitted VM guest, but decrease performance when there is no overcommittment. This patch implements an adaptive spinning mechanism where the vCPU will call pv_wait() earlier if all the following conditions are true: 1) the vCPU has not been halted before; 2) the previous vCPU is in the halted state; 3) there are a lot of pv_wait() for the current vCPU recently. 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. Both systems are configured to have 32 physical CPUs. The kernel build times before and after the patch were: WestmereHaswell Patch 32 vCPUs48 vCPUs32 vCPUs48 vCPUs - Before patch 3m03.2s 9m21.1s 2m08.9s16m14.8s After patch3m04.1s 9m28.5s 2m09.5s 8m29.3s This patch seemed to cause a tiny bit of performance degraduation for 32 vCPUs. For 48 vCPUs, there wasn't much change for Westmere, but a pretty big performance jump for Haswell. Signed-off-by: Waiman Long --- kernel/locking/qspinlock.c |5 +- kernel/locking/qspinlock_paravirt.h | 111 +- 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 94fdd27..da39d43 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -258,7 +258,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_kick_node(struct qspinlock *lock, struct mcs_spinlock *node) { } static __always_inline void __pv_wait_head(struct qspinlock *lock, @@ -415,7 +416,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(>locked); } diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h index 9996609..f03bd7a 100644 --- a/kernel/locking/qspinlock_paravirt.h +++ b/kernel/locking/qspinlock_paravirt.h @@ -31,6 +31,38 @@ #define PENDING_SPIN_THRESHOLD (SPIN_THRESHOLD >> 5) /* + * Queue Node Adaptive Spinning + * + * A queue node vCPU will spin less if the following conditions are all true: + * 1) vCPU in the previous node is halted + * 2) it has not been halted before + * 3) there is a lot of pv_wait() in the curent vCPU recently + * + * The last condition is being monitored by the wait_hist field in the pv_node + * structure which tracks the history of pv_wait() relative to slowpath calls. + * Each pv_wait will increment this field by PV_WAITHIST_INC until it exceeds + * PV_WAITHIST_MAX. Each slowpath lock call will decrement it by 1 until it + * reaches PV_WAITHIST_MIN. If its value is higher than PV_WAITHIST_THRESHOLD, + * the vCPU will spin less. The reason for this adaptive spinning is to try + * to enable wait-early mode only on over-committed guest which helps + * performance. However, it shouldn't be enabled when the guest is not + * over-committed as it will hurt performance. + * + * With PV_WAITHIST_INC set to 4, each pv_wait() while not in wait-early mode + * will increment wait_hist by 3. Each slowpath call without pv_wait() will + * decrement wait_hist by 1. The threshold is set at about 3/4 of the range + * so that about 10 steps from the edges in either direction will reach the + * threshold. If, on average, more than 1/4 of all slowpath calls results in + * a pv_wait(), it should stay in the wait-early mode. + */ +#define PV_WAITHIST_MASK 0xff +#define PV_WAITHIST_INC4 +#define PV_WAITHIST_MIN1 +#define PV_WAITHIST_MAX40 +#define PV_WAITHIST_THRESHOLD 30 +#define PV_CAN_WAIT_EARLY(w) ((w)->wait_hist > PV_WAITHIST_THRESHOLD) + +/* * Queue node uses: vcpu_running & vcpu_halted. * Queue head uses: vcpu_running & vcpu_hashed. */ @@ -46,6 +78,8 @@ struct pv_node { int cpu; u8
[PATCH v5 6/6] locking/pvqspinlock: Queue node adaptive spinning
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. 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. Reducing the spinning threshold is found to improve performance in an overcommitted VM guest, but decrease performance when there is no overcommittment. This patch implements an adaptive spinning mechanism where the vCPU will call pv_wait() earlier if all the following conditions are true: 1) the vCPU has not been halted before; 2) the previous vCPU is in the halted state; 3) there are a lot of pv_wait() for the current vCPU recently. 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. Both systems are configured to have 32 physical CPUs. The kernel build times before and after the patch were: WestmereHaswell Patch 32 vCPUs48 vCPUs32 vCPUs48 vCPUs - Before patch 3m03.2s 9m21.1s 2m08.9s16m14.8s After patch3m04.1s 9m28.5s 2m09.5s 8m29.3s This patch seemed to cause a tiny bit of performance degraduation for 32 vCPUs. For 48 vCPUs, there wasn't much change for Westmere, but a pretty big performance jump for Haswell. Signed-off-by: Waiman Long waiman.l...@hp.com --- kernel/locking/qspinlock.c |5 +- kernel/locking/qspinlock_paravirt.h | 111 +- 2 files changed, 110 insertions(+), 6 deletions(-) diff --git a/kernel/locking/qspinlock.c b/kernel/locking/qspinlock.c index 94fdd27..da39d43 100644 --- a/kernel/locking/qspinlock.c +++ b/kernel/locking/qspinlock.c @@ -258,7 +258,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_kick_node(struct qspinlock *lock, struct mcs_spinlock *node) { } static __always_inline void __pv_wait_head(struct qspinlock *lock, @@ -415,7 +416,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); } diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h index 9996609..f03bd7a 100644 --- a/kernel/locking/qspinlock_paravirt.h +++ b/kernel/locking/qspinlock_paravirt.h @@ -31,6 +31,38 @@ #define PENDING_SPIN_THRESHOLD (SPIN_THRESHOLD 5) /* + * Queue Node Adaptive Spinning + * + * A queue node vCPU will spin less if the following conditions are all true: + * 1) vCPU in the previous node is halted + * 2) it has not been halted before + * 3) there is a lot of pv_wait() in the curent vCPU recently + * + * The last condition is being monitored by the wait_hist field in the pv_node + * structure which tracks the history of pv_wait() relative to slowpath calls. + * Each pv_wait will increment this field by PV_WAITHIST_INC until it exceeds + * PV_WAITHIST_MAX. Each slowpath lock call will decrement it by 1 until it + * reaches PV_WAITHIST_MIN. If its value is higher than PV_WAITHIST_THRESHOLD, + * the vCPU will spin less. The reason for this adaptive spinning is to try + * to enable wait-early mode only on over-committed guest which helps + * performance. However, it shouldn't be enabled when the guest is not + * over-committed as it will hurt performance. + * + * With PV_WAITHIST_INC set to 4, each pv_wait() while not in wait-early mode + * will increment wait_hist by 3. Each slowpath call without pv_wait() will + * decrement wait_hist by 1. The threshold is set at about 3/4 of the range + * so that about 10 steps from the edges in either direction will reach the + * threshold. If, on average, more than 1/4 of all slowpath calls results in + * a pv_wait(), it should stay in the wait-early mode. + */ +#define PV_WAITHIST_MASK 0xff +#define PV_WAITHIST_INC4 +#define PV_WAITHIST_MIN1 +#define PV_WAITHIST_MAX40 +#define PV_WAITHIST_THRESHOLD 30 +#define PV_CAN_WAIT_EARLY(w) ((w)-wait_hist PV_WAITHIST_THRESHOLD) + +/* * Queue node uses: vcpu_running vcpu_halted. * Queue head uses: vcpu_running vcpu_hashed. */ @@ -46,6 +78,8 @@ struct pv_node { int cpu; u8