qspinlock: a 4-byte queue spinlock with PV support
Subsystems (8)
v12 cover qspinlock: a 4-byte queue spinlock with PV support
v11->v12:
- Based on PeterZ's version of the qspinlock patch
(https://lkml.org/lkml/2014/6/15/63).
- Incorporated many of the review comments from Konrad Wilk and Paolo Bonzini.
- The pvqspinlock code is largely from my previous version with PeterZ's way of going from queue tail to head and his idea of using callee saved calls to KVM and XEN codes.
v10->v11:
- Use a simple test-and-set unfair lock to simplify the code, but performance may suffer a bit for large guest with many CPUs.
- Take out Raghavendra KT's test results as the unfair lock changes may render some of his results invalid.
- Add PV support without increasing the size of the core queue node structure.
- Other minor changes to address some of the feedback comments.
v9->v10:
- Make some minor changes to qspinlock.c to accommodate review feedback.
- Change author to PeterZ for 2 of the patches.
- Include Raghavendra KT's test results in patch 18.
v8->v9:
- Integrate PeterZ's version of the queue spinlock patch with some
modification:
http://lkml.kernel.org/r/[email protected] - Break the more complex patches into smaller ones to ease review effort.
- Fix a racing condition in the PV qspinlock code.
v7->v8:
- Remove one unneeded atomic operation from the slowpath, thus improving performance.
- Simplify some of the codes and add more comments.
- Test for X86_FEATURE_HYPERVISOR CPU feature bit to enable/disable unfair lock.
- Reduce unfair lock slowpath lock stealing frequency depending on its distance from the queue head.
- Add performance data for IvyBridge-EX CPU.
v6->v7:
- Remove an atomic operation from the 2-task contending code
- Shorten the names of some macros
- Make the queue waiter to attempt to steal lock when unfair lock is enabled.
- Remove lock holder kick from the PV code and fix a race condition
- Run the unfair lock & PV code on overcommitted KVM guests to collect performance data.
v5->v6:
- Change the optimized 2-task contending code to make it fairer at the expense of a bit of performance.
- Add a patch to support unfair queue spinlock for Xen.
- Modify the PV qspinlock code to follow what was done in the PV ticketlock.
- Add performance data for the unfair lock as well as the PV support code.
v4->v5:
- Move the optimized 2-task contending code to the generic file to enable more architectures to use it without code duplication.
- Address some of the style-related comments by PeterZ.
- Allow the use of unfair queue spinlock in a real para-virtualized execution environment.
- Add para-virtualization support to the qspinlock code by ensuring that the lock holder and queue head stay alive as much as possible.
v3->v4:
- Remove debugging code and fix a configuration error
- Simplify the qspinlock structure and streamline the code to make it perform a bit better
- Add an x86 version of asm/qspinlock.h for holding x86 specific optimization.
- Add an optimized x86 code path for 2 contending tasks to improve low contention performance.
v2->v3:
- Simplify the code by using numerous mode only without an unfair option.
- Use the latest smp_load_acquire()/smp_store_release() barriers.
- Move the queue spinlock code to kernel/locking.
- Make the use of queue spinlock the default for x86-64 without user configuration.
- Additional performance tuning.
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 has 3 different sections:
1) Patches 1-6: Introduces a queue-based spinlock implementation that can 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 break data alignments.
2) Patch 7: Enables the use of unfair lock in a virtual guest. This can resolve some of the locking related performance issues due to the fact that the next CPU to get the lock may have been scheduled out for a period of time.
3) Patches 8-11: Enable qspinlock para-virtualization support by halting the waiting CPUs after spinning for a certain amount of time. The unlock code will detect the a sleeping waiter and wake it up. This is essentially the same logic as the PV ticketlock code.
The queue spinlock has slightly better performance than the ticket spinlock in uncontended case. Its performance can be much better with moderate to heavy contention. 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 even at the 2-socket level, there can be significant speedup depending on the workload. I got report that the queue spinlock patch can improve the performance of an I/O and interrupt intensive stress test with a lot of spinlock contention on a 2-socket system by up to 20%.
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.
Peter Zijlstra (3):
qspinlock: Add pending bit
qspinlock: Optimize for smaller NR_CPUS
qspinlock: Revert to test-and-set on hypervisors
Waiman Long (8):
qspinlock: A simple generic 4-byte queue spinlock qspinlock, x86: Enable x86-64 to use queue spinlock
qspinlock: Extract out code snippets for the next patch
qspinlock: Use a simple write to grab the lock qspinlock, x86: Rename paravirt_ticketlocks_enabled pvqspinlock, x86: Add para-virtualization support pvqspinlock, x86: Enable PV qspinlock for KVM pvqspinlock, x86: Enable PV qspinlock for XEN
arch/x86/Kconfig | 1 + arch/x86/include/asm/paravirt.h | 20 ++ arch/x86/include/asm/paravirt_types.h | 20 ++ arch/x86/include/asm/pvqspinlock.h | 403 ++++++++++++++++++++++++++++ arch/x86/include/asm/qspinlock.h | 77 ++++++ arch/x86/include/asm/spinlock.h | 9 +- arch/x86/include/asm/spinlock_types.h | 4 + arch/x86/kernel/kvm.c | 140 ++++++++++- arch/x86/kernel/paravirt-spinlocks.c | 10 +- arch/x86/xen/spinlock.c | 151 ++++++++++- include/asm-generic/qspinlock.h | 125 +++++++++ include/asm-generic/qspinlock_types.h | 79 ++++++ kernel/Kconfig.locks | 7 + kernel/locking/Makefile | 1 + kernel/locking/mcs_spinlock.h | 1 + kernel/locking/qspinlock.c | 476 +++++++++++++++++++++++++++++++++ 16 files changed, 1512 insertions(+), 12 deletions(-) create mode 100644 arch/x86/include/asm/pvqspinlock.h create mode 100644 arch/x86/include/asm/qspinlock.h create mode 100644 include/asm-generic/qspinlock.h create mode 100644 include/asm-generic/qspinlock_types.h create mode 100644 kernel/locking/qspinlock.c
PATCH v12 1/11 qspinlock: A simple generic 4-byte queue spinlock
This patch introduces a new generic 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 especially when the spinlock is embedded within the data structure to be protected.
Only in light to moderate contention where the average queue depth is around 1-3 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.
Due to the fact that spinlocks are acquired with preemption disabled, 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. Counting soft IRQ, hard IRQ and NMI, a CPU can only have a maximum of 4 concurrent lock waiting activities. 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 24-bit size (including CPU number and queue node index) leaving one byte for the lock.
Please note that the queue node is only needed when waiting for the lock. Once the lock is acquired, the queue node can be released to be used later.
--- include/asm-generic/qspinlock.h | 118 +++++++++++++++++++ include/asm-generic/qspinlock_types.h | 58 +++++++++ kernel/Kconfig.locks | 7 + kernel/locking/Makefile | 1 + kernel/locking/mcs_spinlock.h | 1 + kernel/locking/qspinlock.c | 207 +++++++++++++++++++++++++++++++++ 6 files changed, 392 insertions(+), 0 deletions(-) create mode 100644 include/asm-generic/qspinlock.h create mode 100644 include/asm-generic/qspinlock_types.h create mode 100644 kernel/locking/qspinlock.c
include/asm-generic/qspinlock.h new file +118 -0
@@ -0,0 +1,118 @@ +/* +* 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-2014 Hewlett-Packard Development Company, L.P. +* +* Authors: Waiman Long <[email protected]> +*/ +#ifndef __ASM_GENERIC_QSPINLOCK_H +#define __ASM_GENERIC_QSPINLOCK_H + +#include <asm-generic/qspinlock_types.h> + +/** +* 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 atomic_read (&lock->val); +} + +/** +* queue_spin_value_unlocked - is the spinlock structure unlocked? +* @lock: queue spinlock structure +* Return: 1 if it is unlocked, 0 otherwise +* +* N.B. Whenever there are tasks waiting for the lock, it is considered +* locked wrt the lockref code to avoid lock stealing by the lockref +* code and change things underneath the lock. This also allows some +* optimizations to be applied without conflict with lockref. +*/ +static __always_inline int queue_spin_value_unlocked(struct qspinlock lock) +{ +return !atomic_read (&lock.val); +} + +/** +* 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 atomic_read (&lock->val) & ~_Q_LOCKED_MASK; +} +/** +* 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 (!atomic_read (&lock->val) && + (atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL) ==0 )) +return 1 ; +return 0 ; +} + +extern void queue_spin_lock_slowpath (struct qspinlock *lock,u32 val); + +/** +* queue_spin_lock - acquire a queue spinlock +* @lock: Pointer to queue spinlock structure +*/ +static __always_inline void queue_spin_lock(struct qspinlock *lock) +{ +u32 val; + + val =atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL); +if (likely (val ==0 )) +return ; +queue_spin_lock_slowpath (lock, val); +} + +#ifndef queue_spin_unlock +/** +* queue_spin_unlock - release a queue spinlock +* @lock : Pointer to queue spinlock structure +*/ +static __always_inline void queue_spin_unlock(struct qspinlock *lock) +{ +/* +* smp_mb__before_atomic() in order to guarantee release semantics +*/ +smp_mb__before_atomic_dec (); +atomic_sub (_Q_LOCKED_VAL, &lock->val); +} +#endif + +/* +* Initializier +*/ +#define __ARCH_SPIN_LOCK_UNLOCKED { ATOMIC_INIT(0) } + +/* +* 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_value_unlocked (l) queue_spin_value_unlocked(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 /* __ASM_GENERIC_QSPINLOCK_H */
include/asm-generic/qspinlock_types.h new file +58 -0
58 lines not on this page · raw message
kernel/Kconfig.locks +7 -0
@@ -231,6 +231,13 @@ config RWSEM_SPIN_ON_OWNER def_bool y depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW +config ARCH_USE_QUEUE_SPINLOCK + bool + +config QUEUE_SPINLOCK + def_bool y if ARCH_USE_QUEUE_SPINLOCK + depends on SMP && !PARAVIRT_SPINLOCKS + config ARCH_USE_QUEUE_RWLOCK bool
kernel/locking/Makefile +1 -0
@@ -16,6 +16,7 @@ endif obj-$(CONFIG_SMP) += spinlock.o obj-$(CONFIG_SMP) += lglock.o obj-$(CONFIG_PROVE_LOCKING) += spinlock.o +obj-$(CONFIG_QUEUE_SPINLOCK) += qspinlock.o obj-$(CONFIG_RT_MUTEXES) += rtmutex.o obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o
kernel/locking/mcs_spinlock.h +1 -0
@@ -17,6 +17,7 @@struct mcs_spinlock {struct mcs_spinlock *next;int locked;/* 1 if lock acquired */ +int count;/* nesting count, see qspinlock.c */ };#ifndef arch_mcs_spin_lock_contended
kernel/locking/qspinlock.c new file +207 -0
207 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 1/11 qspinlock: A simple generic 4-byte queue spinlock
In reply to Waiman LongThis patch introduces a new generic 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 especially when the spinlock is embedded within the data structure to be protected.
Only in light to moderate contention where the average queue depth is around 1-3 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.
Due to the fact that spinlocks are acquired with preemption disabled, 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. Counting soft IRQ, hard IRQ and NMI, a CPU can only have a maximum of 4 concurrent lock waiting activities. 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 24-bit size (including CPU number and queue node index) leaving one byte for the lock.
Please note that the queue node is only needed when waiting for the lock. Once the lock is acquired, the queue node can be released to be used later.
--- include/asm-generic/qspinlock.h | 118 +++++++++++++++++++ include/asm-generic/qspinlock_types.h | 58 +++++++++ kernel/Kconfig.locks | 7 + kernel/locking/Makefile | 1 + kernel/locking/mcs_spinlock.h | 1 + kernel/locking/qspinlock.c | 207 +++++++++++++++++++++++++++++++++ 6 files changed, 392 insertions(+), 0 deletions(-) create mode 100644 include/asm-generic/qspinlock.h create mode 100644 include/asm-generic/qspinlock_types.h create mode 100644 kernel/locking/qspinlock.c
include/asm-generic/qspinlock.h new file +118 -0
@@ -0,0 +1,118 @@ +/* +* 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-2014 Hewlett-Packard Development Company, L.P. +* +* Authors: Waiman Long <[email protected]> +*/ +#ifndef __ASM_GENERIC_QSPINLOCK_H +#define __ASM_GENERIC_QSPINLOCK_H + +#include <asm-generic/qspinlock_types.h> + +/** +* 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 atomic_read (&lock->val); +} + +/** +* queue_spin_value_unlocked - is the spinlock structure unlocked? +* @lock: queue spinlock structure +* Return: 1 if it is unlocked, 0 otherwise +* +* N.B. Whenever there are tasks waiting for the lock, it is considered +* locked wrt the lockref code to avoid lock stealing by the lockref +* code and change things underneath the lock. This also allows some +* optimizations to be applied without conflict with lockref. +*/ +static __always_inline int queue_spin_value_unlocked(struct qspinlock lock) +{ +return !atomic_read (&lock.val); +} + +/** +* 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 atomic_read (&lock->val) & ~_Q_LOCKED_MASK; +} +/** +* 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 (!atomic_read (&lock->val) && + (atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL) ==0 )) +return 1 ; +return 0 ; +} + +extern void queue_spin_lock_slowpath (struct qspinlock *lock,u32 val); + +/** +* queue_spin_lock - acquire a queue spinlock +* @lock: Pointer to queue spinlock structure +*/ +static __always_inline void queue_spin_lock(struct qspinlock *lock) +{ +u32 val; + + val =atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL); +if (likely (val ==0 )) +return ; +queue_spin_lock_slowpath (lock, val); +} + +#ifndef queue_spin_unlock +/** +* queue_spin_unlock - release a queue spinlock +* @lock : Pointer to queue spinlock structure +*/ +static __always_inline void queue_spin_unlock(struct qspinlock *lock) +{ +/* +* smp_mb__before_atomic() in order to guarantee release semantics +*/ +smp_mb__before_atomic_dec (); +atomic_sub (_Q_LOCKED_VAL, &lock->val); +} +#endif + +/* +* Initializier +*/ +#define __ARCH_SPIN_LOCK_UNLOCKED { ATOMIC_INIT(0) } + +/* +* 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_value_unlocked (l) queue_spin_value_unlocked(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 /* __ASM_GENERIC_QSPINLOCK_H */
include/asm-generic/qspinlock_types.h new file +58 -0
58 lines not on this page · raw message
kernel/Kconfig.locks +7 -0
@@ -231,6 +231,13 @@ config RWSEM_SPIN_ON_OWNER def_bool y depends on SMP && RWSEM_XCHGADD_ALGORITHM && ARCH_SUPPORTS_ATOMIC_RMW +config ARCH_USE_QUEUE_SPINLOCK + bool + +config QUEUE_SPINLOCK + def_bool y if ARCH_USE_QUEUE_SPINLOCK + depends on SMP && !PARAVIRT_SPINLOCKS + config ARCH_USE_QUEUE_RWLOCK bool
kernel/locking/Makefile +1 -0
@@ -16,6 +16,7 @@ endif obj-$(CONFIG_SMP) += spinlock.o obj-$(CONFIG_SMP) += lglock.o obj-$(CONFIG_PROVE_LOCKING) += spinlock.o +obj-$(CONFIG_QUEUE_SPINLOCK) += qspinlock.o obj-$(CONFIG_RT_MUTEXES) += rtmutex.o obj-$(CONFIG_DEBUG_RT_MUTEXES) += rtmutex-debug.o obj-$(CONFIG_RT_MUTEX_TESTER) += rtmutex-tester.o
kernel/locking/mcs_spinlock.h +1 -0
@@ -17,6 +17,7 @@struct mcs_spinlock {struct mcs_spinlock *next;int locked;/* 1 if lock acquired */ +int count;/* nesting count, see qspinlock.c */ };#ifndef arch_mcs_spin_lock_contended
kernel/locking/qspinlock.c new file +207 -0
207 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 2/11 qspinlock, x86: Enable x86-64 to use queue spinlock
In reply to Waiman LongThis patch makes the necessary changes at the x86 architecture specific layer to enable the use of queue spinlock for x86-64. As x86-32 machines are typically not multi-socket. The benefit of queue spinlock may not be apparent. So queue spinlock is not enabled.
Currently, there is some incompatibilities between the para-virtualized spinlock code (which hard-codes the use of ticket spinlock) and the queue spinlock. Therefore, the use of queue spinlock is disabled when the para-virtualized spinlock is enabled.
The arch/x86/include/asm/qspinlock.h header file includes some x86 specific optimization which will make the queue spinlock code perform better than the generic implementation.
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/qspinlock.h | 25 +++++++++++++++++++++++++ arch/x86/include/asm/spinlock.h | 5 +++++ arch/x86/include/asm/spinlock_types.h | 4 ++++ 4 files changed, 35 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/qspinlock.h
arch/x86/Kconfig +1 -0
@@ -123,6 +123,7 @@ config X86 select MODULES_USE_ELF_RELA if X86_64 select CLONE_BACKWARDS if X86_32 select ARCH_USE_BUILTIN_BSWAP + select ARCH_USE_QUEUE_SPINLOCK select ARCH_USE_QUEUE_RWLOCK select OLD_SIGSUSPEND3 if X86_32 || IA32_EMULATION select OLD_SIGACTION if X86_32
arch/x86/include/asm/qspinlock.h new file +25 -0
@@ -0,0 +1,25 @@ +#ifndef _ASM_X86_QSPINLOCK_H +#define _ASM_X86_QSPINLOCK_H + +#include <asm-generic/qspinlock_types.h> + +#ifndef CONFIG_X86_PPRO_FENCE + +#define queue_spin_unlock queue_spin_unlock +/** +* queue_spin_unlock - release a queue spinlock +* @lock : Pointer to queue spinlock structure +* +* An effective smp_store_release() on the least-significant byte. +*/ +static inline void queue_spin_unlock (struct qspinlock *lock) +{ +barrier (); +ACCESS_ONCE (*(u8 *)lock) =0 ; +} + +#endif /* !CONFIG_X86_PPRO_FENCE */ + +#include <asm-generic/qspinlock.h> + +#endif /* _ASM_X86_QSPINLOCK_H */
arch/x86/include/asm/spinlock.h +5 -0
@@ -42,6 +42,10 @@extern struct static_key paravirt_ticketlocks_enabled; static __always_inline bool static_key_false(struct static_key *key); +#ifdef CONFIG_QUEUE_SPINLOCK +#include <asm/qspinlock.h> +#else +#ifdef CONFIG_PARAVIRT_SPINLOCKS static inline void __ticket_enter_slowpath (arch_spinlock_t *lock)
@@ -180,6 +184,7 @@ static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock, {arch_spin_lock (lock); } +#endif /* CONFIG_QUEUE_SPINLOCK */ static inline void arch_spin_unlock_wait (arch_spinlock_t *lock) {
arch/x86/include/asm/spinlock_types.h +4 -0
@@ -23,6 +23,9 @@ typedef u32 __ticketpair_t;#define TICKET_SHIFT (sizeof(__ticket_t) * 8) +#ifdef CONFIG_QUEUE_SPINLOCK +#include <asm-generic/qspinlock_types.h> +#else typedef struct arch_spinlock { union { __ticketpair_t head_tail;
@@ -33,6 +36,7 @@ typedef struct arch_spinlock { } arch_spinlock_t;#define __ARCH_SPIN_LOCK_UNLOCKED { { 0 } } +#endif /* CONFIG_QUEUE_SPINLOCK */ #include <asm-generic/qrwlock_types.h>
Signature · 1 line
1.7.1
PATCH v12 2/11 qspinlock, x86: Enable x86-64 to use queue spinlock
In reply to Waiman LongThis patch makes the necessary changes at the x86 architecture specific layer to enable the use of queue spinlock for x86-64. As x86-32 machines are typically not multi-socket. The benefit of queue spinlock may not be apparent. So queue spinlock is not enabled.
Currently, there is some incompatibilities between the para-virtualized spinlock code (which hard-codes the use of ticket spinlock) and the queue spinlock. Therefore, the use of queue spinlock is disabled when the para-virtualized spinlock is enabled.
The arch/x86/include/asm/qspinlock.h header file includes some x86 specific optimization which will make the queue spinlock code perform better than the generic implementation.
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/qspinlock.h | 25 +++++++++++++++++++++++++ arch/x86/include/asm/spinlock.h | 5 +++++ arch/x86/include/asm/spinlock_types.h | 4 ++++ 4 files changed, 35 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/qspinlock.h
arch/x86/Kconfig +1 -0
@@ -123,6 +123,7 @@ config X86 select MODULES_USE_ELF_RELA if X86_64 select CLONE_BACKWARDS if X86_32 select ARCH_USE_BUILTIN_BSWAP + select ARCH_USE_QUEUE_SPINLOCK select ARCH_USE_QUEUE_RWLOCK select OLD_SIGSUSPEND3 if X86_32 || IA32_EMULATION select OLD_SIGACTION if X86_32
arch/x86/include/asm/qspinlock.h new file +25 -0
@@ -0,0 +1,25 @@ +#ifndef _ASM_X86_QSPINLOCK_H +#define _ASM_X86_QSPINLOCK_H + +#include <asm-generic/qspinlock_types.h> + +#ifndef CONFIG_X86_PPRO_FENCE + +#define queue_spin_unlock queue_spin_unlock +/** +* queue_spin_unlock - release a queue spinlock +* @lock : Pointer to queue spinlock structure +* +* An effective smp_store_release() on the least-significant byte. +*/ +static inline void queue_spin_unlock (struct qspinlock *lock) +{ +barrier (); +ACCESS_ONCE (*(u8 *)lock) =0 ; +} + +#endif /* !CONFIG_X86_PPRO_FENCE */ + +#include <asm-generic/qspinlock.h> + +#endif /* _ASM_X86_QSPINLOCK_H */
arch/x86/include/asm/spinlock.h +5 -0
@@ -42,6 +42,10 @@extern struct static_key paravirt_ticketlocks_enabled; static __always_inline bool static_key_false(struct static_key *key); +#ifdef CONFIG_QUEUE_SPINLOCK +#include <asm/qspinlock.h> +#else +#ifdef CONFIG_PARAVIRT_SPINLOCKS static inline void __ticket_enter_slowpath (arch_spinlock_t *lock)
@@ -180,6 +184,7 @@ static __always_inline void arch_spin_lock_flags(arch_spinlock_t *lock, {arch_spin_lock (lock); } +#endif /* CONFIG_QUEUE_SPINLOCK */ static inline void arch_spin_unlock_wait (arch_spinlock_t *lock) {
arch/x86/include/asm/spinlock_types.h +4 -0
@@ -23,6 +23,9 @@ typedef u32 __ticketpair_t;#define TICKET_SHIFT (sizeof(__ticket_t) * 8) +#ifdef CONFIG_QUEUE_SPINLOCK +#include <asm-generic/qspinlock_types.h> +#else typedef struct arch_spinlock { union { __ticketpair_t head_tail;
@@ -33,6 +36,7 @@ typedef struct arch_spinlock { } arch_spinlock_t;#define __ARCH_SPIN_LOCK_UNLOCKED { { 0 } } +#endif /* CONFIG_QUEUE_SPINLOCK */ #include <asm-generic/qrwlock_types.h>
Signature · 1 line
1.7.1
PATCH v12 3/11 qspinlock: Add pending bit
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
Because the qspinlock needs to touch a second cacheline (the per-cpu mcs_nodes[]); add a pending bit and allow a single in-word spinner before we punt to the second cacheline.
It is possible so observe the pending bit without the locked bit when the last owner has just released but the pending owner has not yet taken ownership.
In this case we would normally queue -- because the pending bit is already taken. However, in this case the pending bit is guaranteed to be released 'soon', therefore wait for it and avoid queueing.
--- include/asm-generic/qspinlock_types.h | 12 +++- kernel/locking/qspinlock.c | 119 +++++++++++++++++++++++++++------ 2 files changed, 107 insertions(+), 24 deletions(-)
include/asm-generic/qspinlock_types.h +9 -3
@@ -36,8 +36,9 @@ typedef struct qspinlock {* Bitfields in the atomic value: * * 0- 7: locked byte -* 8- 9: tail index -* 10-31: tail cpu (+1) +* 8: pending +* 9-10: tail index +* 11-31: tail cpu (+1) */ #define _Q_SET_MASK (type) (((1U << _Q_ ## type ## _BITS) - 1)\ << _Q_ ## type ## _OFFSET)
@@ -45,7 +46,11 @@ typedef struct qspinlock {#define _Q_LOCKED_BITS 8#define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED) -#define _Q_TAIL_IDX_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#define _Q_PENDING_BITS 1 +#define _Q_PENDING_MASK _Q_SET_MASK(PENDING) + +#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS)#define _Q_TAIL_IDX_BITS 2#define _Q_TAIL_IDX_MASK _Q_SET_MASK(TAIL_IDX)
@@ -54,5 +59,6 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU)#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET) +#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET)#endif /* __ASM_GENERIC_QSPINLOCK_TYPES_H */
kernel/locking/qspinlock.c +98 -21
168 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 3/11 qspinlock: Add pending bit
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
Because the qspinlock needs to touch a second cacheline (the per-cpu mcs_nodes[]); add a pending bit and allow a single in-word spinner before we punt to the second cacheline.
It is possible so observe the pending bit without the locked bit when the last owner has just released but the pending owner has not yet taken ownership.
In this case we would normally queue -- because the pending bit is already taken. However, in this case the pending bit is guaranteed to be released 'soon', therefore wait for it and avoid queueing.
--- include/asm-generic/qspinlock_types.h | 12 +++- kernel/locking/qspinlock.c | 119 +++++++++++++++++++++++++++------ 2 files changed, 107 insertions(+), 24 deletions(-)
include/asm-generic/qspinlock_types.h +9 -3
@@ -36,8 +36,9 @@ typedef struct qspinlock {* Bitfields in the atomic value: * * 0- 7: locked byte -* 8- 9: tail index -* 10-31: tail cpu (+1) +* 8: pending +* 9-10: tail index +* 11-31: tail cpu (+1) */ #define _Q_SET_MASK (type) (((1U << _Q_ ## type ## _BITS) - 1)\ << _Q_ ## type ## _OFFSET)
@@ -45,7 +46,11 @@ typedef struct qspinlock {#define _Q_LOCKED_BITS 8#define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED) -#define _Q_TAIL_IDX_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#define _Q_PENDING_BITS 1 +#define _Q_PENDING_MASK _Q_SET_MASK(PENDING) + +#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS)#define _Q_TAIL_IDX_BITS 2#define _Q_TAIL_IDX_MASK _Q_SET_MASK(TAIL_IDX)
@@ -54,5 +59,6 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU)#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET) +#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET)#endif /* __ASM_GENERIC_QSPINLOCK_TYPES_H */
kernel/locking/qspinlock.c +98 -21
168 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 4/11 qspinlock: Extract out code snippets for the next patch
In reply to Waiman LongThis is a preparatory patch that extracts out the following 2 code snippets to prepare for the next performance optimization patch.
1) the logic for the exchange of new and previous tail code words into a new xchg_tail() function.
2) the logic for clearing the pending bit and setting the locked bit into a new clear_pending_set_locked() function.
This patch also simplifies the trylock operation before queuing by calling queue_spin_trylock() directly.
--- include/asm-generic/qspinlock_types.h | 2 + kernel/locking/qspinlock.c | 91 +++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 31 deletions(-)
include/asm-generic/qspinlock_types.h +2 -0
@@ -58,6 +58,8 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) +#define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK) +#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET)#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET)
kernel/locking/qspinlock.c +60 -31
118 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 4/11 qspinlock: Extract out code snippets for the next patch
In reply to Waiman LongThis is a preparatory patch that extracts out the following 2 code snippets to prepare for the next performance optimization patch.
1) the logic for the exchange of new and previous tail code words into a new xchg_tail() function.
2) the logic for clearing the pending bit and setting the locked bit into a new clear_pending_set_locked() function.
This patch also simplifies the trylock operation before queuing by calling queue_spin_trylock() directly.
--- include/asm-generic/qspinlock_types.h | 2 + kernel/locking/qspinlock.c | 91 +++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 31 deletions(-)
include/asm-generic/qspinlock_types.h +2 -0
@@ -58,6 +58,8 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) +#define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK) +#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET)#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET)
kernel/locking/qspinlock.c +60 -31
118 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 4/11 qspinlock: Extract out code snippets for the next patch
In reply to Waiman LongThis is a preparatory patch that extracts out the following 2 code snippets to prepare for the next performance optimization patch.
1) the logic for the exchange of new and previous tail code words into a new xchg_tail() function.
2) the logic for clearing the pending bit and setting the locked bit into a new clear_pending_set_locked() function.
This patch also simplifies the trylock operation before queuing by calling queue_spin_trylock() directly.
--- include/asm-generic/qspinlock_types.h | 2 + kernel/locking/qspinlock.c | 91 +++++++++++++++++++++----------- 2 files changed, 62 insertions(+), 31 deletions(-)
include/asm-generic/qspinlock_types.h +2 -0
@@ -58,6 +58,8 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) +#define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK) +#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET)#define _Q_PENDING_VAL (1U << _Q_PENDING_OFFSET)
kernel/locking/qspinlock.c +60 -31
118 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 5/11 qspinlock: Optimize for smaller NR_CPUS
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
When we allow for a max NR_CPUS < 2^14 we can optimize the pending wait-acquire and the xchg_tail() operations.
By growing the pending bit to a byte, we reduce the tail to 16bit. This means we can use xchg16 for the tail part and do away with all the repeated compxchg() operations.
This in turn allows us to unconditionally acquire; the locked state as observed by the wait loops cannot change. And because both locked and pending are now a full byte we can use simple stores for the state transition, obviating one atomic operation entirely.
This optimization is needed to make the qspinlock achieve performance parity with ticket spinlock at light load.
All this is horribly broken on Alpha pre EV56 (and any other arch that cannot do single-copy atomic byte stores).
--- include/asm-generic/qspinlock_types.h | 13 ++++++ kernel/locking/qspinlock.c | 71 ++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletions(-)
include/asm-generic/qspinlock_types.h +13 -0
@@ -35,6 +35,14 @@ typedef struct qspinlock {/* * Bitfields in the atomic value: * +* When NR_CPUS < 16K +* 0- 7: locked byte +* 8: pending +* 9-15: not used +* 16-17: tail index +* 18-31: tail cpu (+1) +* +* When NR_CPUS >= 16K * 0- 7: locked byte * 8: pending * 9-10: tail index
@@ -47,7 +55,11 @@ typedef struct qspinlock {#define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED)#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#if CONFIG_NR_CPUS < (1U <<14 ) +#define _Q_PENDING_BITS 8 +#else #define _Q_PENDING_BITS 1 +#endif #define _Q_PENDING_MASK _Q_SET_MASK(PENDING)#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS)
@@ -58,6 +70,7 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) +#define _Q_TAIL_OFFSET _Q_TAIL_IDX_OFFSET#define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK)#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET)
kernel/locking/qspinlock.c +70 -1
102 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 5/11 qspinlock: Optimize for smaller NR_CPUS
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
When we allow for a max NR_CPUS < 2^14 we can optimize the pending wait-acquire and the xchg_tail() operations.
By growing the pending bit to a byte, we reduce the tail to 16bit. This means we can use xchg16 for the tail part and do away with all the repeated compxchg() operations.
This in turn allows us to unconditionally acquire; the locked state as observed by the wait loops cannot change. And because both locked and pending are now a full byte we can use simple stores for the state transition, obviating one atomic operation entirely.
This optimization is needed to make the qspinlock achieve performance parity with ticket spinlock at light load.
All this is horribly broken on Alpha pre EV56 (and any other arch that cannot do single-copy atomic byte stores).
--- include/asm-generic/qspinlock_types.h | 13 ++++++ kernel/locking/qspinlock.c | 71 ++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletions(-)
include/asm-generic/qspinlock_types.h +13 -0
@@ -35,6 +35,14 @@ typedef struct qspinlock {/* * Bitfields in the atomic value: * +* When NR_CPUS < 16K +* 0- 7: locked byte +* 8: pending +* 9-15: not used +* 16-17: tail index +* 18-31: tail cpu (+1) +* +* When NR_CPUS >= 16K * 0- 7: locked byte * 8: pending * 9-10: tail index
@@ -47,7 +55,11 @@ typedef struct qspinlock {#define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED)#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#if CONFIG_NR_CPUS < (1U <<14 ) +#define _Q_PENDING_BITS 8 +#else #define _Q_PENDING_BITS 1 +#endif #define _Q_PENDING_MASK _Q_SET_MASK(PENDING)#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS)
@@ -58,6 +70,7 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) +#define _Q_TAIL_OFFSET _Q_TAIL_IDX_OFFSET#define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK)#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET)
kernel/locking/qspinlock.c +70 -1
102 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 5/11 qspinlock: Optimize for smaller NR_CPUS
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
When we allow for a max NR_CPUS < 2^14 we can optimize the pending wait-acquire and the xchg_tail() operations.
By growing the pending bit to a byte, we reduce the tail to 16bit. This means we can use xchg16 for the tail part and do away with all the repeated compxchg() operations.
This in turn allows us to unconditionally acquire; the locked state as observed by the wait loops cannot change. And because both locked and pending are now a full byte we can use simple stores for the state transition, obviating one atomic operation entirely.
This optimization is needed to make the qspinlock achieve performance parity with ticket spinlock at light load.
All this is horribly broken on Alpha pre EV56 (and any other arch that cannot do single-copy atomic byte stores).
--- include/asm-generic/qspinlock_types.h | 13 ++++++ kernel/locking/qspinlock.c | 71 ++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 1 deletions(-)
include/asm-generic/qspinlock_types.h +13 -0
@@ -35,6 +35,14 @@ typedef struct qspinlock {/* * Bitfields in the atomic value: * +* When NR_CPUS < 16K +* 0- 7: locked byte +* 8: pending +* 9-15: not used +* 16-17: tail index +* 18-31: tail cpu (+1) +* +* When NR_CPUS >= 16K * 0- 7: locked byte * 8: pending * 9-10: tail index
@@ -47,7 +55,11 @@ typedef struct qspinlock {#define _Q_LOCKED_MASK _Q_SET_MASK(LOCKED)#define _Q_PENDING_OFFSET (_Q_LOCKED_OFFSET + _Q_LOCKED_BITS) +#if CONFIG_NR_CPUS < (1U <<14 ) +#define _Q_PENDING_BITS 8 +#else #define _Q_PENDING_BITS 1 +#endif #define _Q_PENDING_MASK _Q_SET_MASK(PENDING)#define _Q_TAIL_IDX_OFFSET (_Q_PENDING_OFFSET + _Q_PENDING_BITS)
@@ -58,6 +70,7 @@ typedef struct qspinlock {#define _Q_TAIL_CPU_BITS (32 - _Q_TAIL_CPU_OFFSET)#define _Q_TAIL_CPU_MASK _Q_SET_MASK(TAIL_CPU) +#define _Q_TAIL_OFFSET _Q_TAIL_IDX_OFFSET#define _Q_TAIL_MASK (_Q_TAIL_IDX_MASK | _Q_TAIL_CPU_MASK)#define _Q_LOCKED_VAL (1U << _Q_LOCKED_OFFSET)
kernel/locking/qspinlock.c +70 -1
102 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 6/11 qspinlock: Use a simple write to grab the lock
In reply to Waiman LongCurrently, atomic_cmpxchg() is used to get the lock. However, this is not really necessary if there is more than one task in the queue and the queue head don't need to reset the tail code. For that case, a simple write to set the lock bit is enough as the queue head will be the only one eligible to get the lock as long as it checks that both the lock and pending bits are not set. The current pending bit waiting code will ensure that the bit will not be set as soon as the tail code in the lock is set.
With that change, the are some slight improvement in the performance of the queue spinlock in the 5M loop micro-benchmark run on a 4-socket Westere-EX machine as shown in the tables below.
[Standalone/Embedded - same node]
# of tasks Before patch After patch %Change
---------- ----------- ---------- -------
3 2324/2321 2248/2265 -3%/-2%
4 2890/2896 2819/2831 -2%/-2%
5 3611/3595 3522/3512 -2%/-2%
6 4281/4276 4173/4160 -3%/-3%
7 5018/5001 4875/4861 -3%/-3%
8 5759/5750 5563/5568 -3%/-3%[Standalone/Embedded - different nodes]
# of tasks Before patch After patch %Change
---------- ----------- ---------- -------
3 12242/12237 12087/12093 -1%/-1%
4 10688/10696 10507/10521 -2%/-2%It was also found that this change produced a much bigger performance improvement in the newer IvyBridge-EX chip and was essentially to close the performance gap between the ticket spinlock and queue spinlock.
The disk workload of the AIM7 benchmark was run on a 4-socket Westmere-EX machine with both ext4 and xfs RAM disks at 3000 users on a 3.14 based kernel. The results of the test runs were:
AIM7 XFS Disk Test
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- ticketlock 5678233 3.17 96.61 5.81 qspinlock 5750799 3.13 94.83 5.97
AIM7 EXT4 Disk Test
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- ticketlock 1114551 16.15 509.72 7.11 qspinlock 2184466 8.24 232.99 6.01
The ext4 filesystem run had a much higher spinlock contention than the xfs filesystem run.
The "ebizzy -m" test was also run with the following results:
kernel records/s Real Time Sys Time Usr Time ----- --------- --------- -------- -------- ticketlock 2075 10.00 216.35 3.49 qspinlock 3023 10.00 198.20 4.80
--- kernel/locking/qspinlock.c | 59 ++++++++++++++++++++++++++++++++------------ 1 files changed, 43 insertions(+), 16 deletions(-)
kernel/locking/qspinlock.c +43 -16
@@ -103,24 +103,33 @@ static inline struct mcs_spinlock *decode_tail(u32 tail)* By using the whole 2nd least significant byte for the pending bit, we * can allow better optimization of the lock acquisition for the pending * bit holder. +* +* This internal structure is also used by the set_locked function which +* is not restricted to _Q_PENDING_BITS == 8. */ -#if _Q_PENDING_BITS ==8 -struct __qspinlock {union {atomic_t val; -struct {#ifdef __LITTLE_ENDIAN +u8 locked; +struct {u16 locked_pending;u16 tail; + };#else +struct {u16 tail;u16 locked_pending; -#endif }; +struct { +u8 reserved[3 ]; +u8 locked; + }; +#endif }; }; +#if _Q_PENDING_BITS ==8 /** * clear_pending_set_locked - take ownership and clear the pending bit. * @lock: Pointer to queue spinlock structure
@@ -207,6 +216,19 @@ static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)#endif /* _Q_PENDING_BITS == 8 */ /** +* set_locked - Set the lock bit and own the lock +* @lock: Pointer to queue spinlock structure +* +* *,*,0 -> *,0,1 +*/ +static __always_inline void set_locked(struct qspinlock *lock) +{ +struct __qspinlock *l = (void *)lock; + +ACCESS_ONCE (l->locked) = _Q_LOCKED_VAL; +} + +/** * queue_spin_lock_slowpath - acquire the queue spinlock * @lock: Pointer to queue spinlock structure * @val: Current value of the queue spinlock 32-bit word
@@ -339,10 +361,13 @@ queue:/* * we're at the head of the waitqueue, wait for the owner & pending to * go away. +* Load-acquired is used here because the set_locked() +* function below may not be a full memory barrier. * * *,x,y -> *,0,0 */ -while ((val =atomic_read (&lock->val)) & _Q_LOCKED_PENDING_MASK) +while ((val =smp_load_acquire (&lock->val.counter)) & + _Q_LOCKED_PENDING_MASK)cpu_relax ();/*
@@ -350,15 +375,19 @@ queue:* * n,0,0 -> 0,0,1 : lock, uncontended * *,0,0 -> *,0,1 : lock, contended +* +* If the queue head is the only one in the queue (lock value == tail), +* clear the tail code and grab the lock. Otherwise, we only need +* to grab the lock. */ for (;;) { - new = _Q_LOCKED_VAL; -if (val != tail) - new |= val; - - old =atomic_cmpxchg (&lock->val, val, new); -if (old == val) +if (val != tail) { +set_locked (lock);break ; + } + old =atomic_cmpxchg (&lock->val, val, _Q_LOCKED_VAL); +if (old == val) + goto release;/* No contention */ val = old; }
@@ -366,12 +395,10 @@ queue:/* * contended path; wait for next, release. */ -if (new != _Q_LOCKED_VAL) { -while (!(next =ACCESS_ONCE (node->next))) -cpu_relax (); +while (!(next =ACCESS_ONCE (node->next))) +cpu_relax (); -arch_mcs_spin_unlock_contended (&next->locked); - } +arch_mcs_spin_unlock_contended (&next->locked); release:/*
Signature · 1 line
1.7.1
PATCH v12 6/11 qspinlock: Use a simple write to grab the lock
In reply to Waiman LongCurrently, atomic_cmpxchg() is used to get the lock. However, this is not really necessary if there is more than one task in the queue and the queue head don't need to reset the tail code. For that case, a simple write to set the lock bit is enough as the queue head will be the only one eligible to get the lock as long as it checks that both the lock and pending bits are not set. The current pending bit waiting code will ensure that the bit will not be set as soon as the tail code in the lock is set.
With that change, the are some slight improvement in the performance of the queue spinlock in the 5M loop micro-benchmark run on a 4-socket Westere-EX machine as shown in the tables below.
[Standalone/Embedded - same node]
# of tasks Before patch After patch %Change
---------- ----------- ---------- -------
3 2324/2321 2248/2265 -3%/-2%
4 2890/2896 2819/2831 -2%/-2%
5 3611/3595 3522/3512 -2%/-2%
6 4281/4276 4173/4160 -3%/-3%
7 5018/5001 4875/4861 -3%/-3%
8 5759/5750 5563/5568 -3%/-3%[Standalone/Embedded - different nodes]
# of tasks Before patch After patch %Change
---------- ----------- ---------- -------
3 12242/12237 12087/12093 -1%/-1%
4 10688/10696 10507/10521 -2%/-2%It was also found that this change produced a much bigger performance improvement in the newer IvyBridge-EX chip and was essentially to close the performance gap between the ticket spinlock and queue spinlock.
The disk workload of the AIM7 benchmark was run on a 4-socket Westmere-EX machine with both ext4 and xfs RAM disks at 3000 users on a 3.14 based kernel. The results of the test runs were:
AIM7 XFS Disk Test
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- ticketlock 5678233 3.17 96.61 5.81 qspinlock 5750799 3.13 94.83 5.97
AIM7 EXT4 Disk Test
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- ticketlock 1114551 16.15 509.72 7.11 qspinlock 2184466 8.24 232.99 6.01
The ext4 filesystem run had a much higher spinlock contention than the xfs filesystem run.
The "ebizzy -m" test was also run with the following results:
kernel records/s Real Time Sys Time Usr Time ----- --------- --------- -------- -------- ticketlock 2075 10.00 216.35 3.49 qspinlock 3023 10.00 198.20 4.80
--- kernel/locking/qspinlock.c | 59 ++++++++++++++++++++++++++++++++------------ 1 files changed, 43 insertions(+), 16 deletions(-)
kernel/locking/qspinlock.c +43 -16
@@ -103,24 +103,33 @@ static inline struct mcs_spinlock *decode_tail(u32 tail)* By using the whole 2nd least significant byte for the pending bit, we * can allow better optimization of the lock acquisition for the pending * bit holder. +* +* This internal structure is also used by the set_locked function which +* is not restricted to _Q_PENDING_BITS == 8. */ -#if _Q_PENDING_BITS ==8 -struct __qspinlock {union {atomic_t val; -struct {#ifdef __LITTLE_ENDIAN +u8 locked; +struct {u16 locked_pending;u16 tail; + };#else +struct {u16 tail;u16 locked_pending; -#endif }; +struct { +u8 reserved[3 ]; +u8 locked; + }; +#endif }; }; +#if _Q_PENDING_BITS ==8 /** * clear_pending_set_locked - take ownership and clear the pending bit. * @lock: Pointer to queue spinlock structure
@@ -207,6 +216,19 @@ static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)#endif /* _Q_PENDING_BITS == 8 */ /** +* set_locked - Set the lock bit and own the lock +* @lock: Pointer to queue spinlock structure +* +* *,*,0 -> *,0,1 +*/ +static __always_inline void set_locked(struct qspinlock *lock) +{ +struct __qspinlock *l = (void *)lock; + +ACCESS_ONCE (l->locked) = _Q_LOCKED_VAL; +} + +/** * queue_spin_lock_slowpath - acquire the queue spinlock * @lock: Pointer to queue spinlock structure * @val: Current value of the queue spinlock 32-bit word
@@ -339,10 +361,13 @@ queue:/* * we're at the head of the waitqueue, wait for the owner & pending to * go away. +* Load-acquired is used here because the set_locked() +* function below may not be a full memory barrier. * * *,x,y -> *,0,0 */ -while ((val =atomic_read (&lock->val)) & _Q_LOCKED_PENDING_MASK) +while ((val =smp_load_acquire (&lock->val.counter)) & + _Q_LOCKED_PENDING_MASK)cpu_relax ();/*
@@ -350,15 +375,19 @@ queue:* * n,0,0 -> 0,0,1 : lock, uncontended * *,0,0 -> *,0,1 : lock, contended +* +* If the queue head is the only one in the queue (lock value == tail), +* clear the tail code and grab the lock. Otherwise, we only need +* to grab the lock. */ for (;;) { - new = _Q_LOCKED_VAL; -if (val != tail) - new |= val; - - old =atomic_cmpxchg (&lock->val, val, new); -if (old == val) +if (val != tail) { +set_locked (lock);break ; + } + old =atomic_cmpxchg (&lock->val, val, _Q_LOCKED_VAL); +if (old == val) + goto release;/* No contention */ val = old; }
@@ -366,12 +395,10 @@ queue:/* * contended path; wait for next, release. */ -if (new != _Q_LOCKED_VAL) { -while (!(next =ACCESS_ONCE (node->next))) -cpu_relax (); +while (!(next =ACCESS_ONCE (node->next))) +cpu_relax (); -arch_mcs_spin_unlock_contended (&next->locked); - } +arch_mcs_spin_unlock_contended (&next->locked); release:/*
Signature · 1 line
1.7.1
PATCH v12 6/11 qspinlock: Use a simple write to grab the lock
In reply to Waiman LongCurrently, atomic_cmpxchg() is used to get the lock. However, this is not really necessary if there is more than one task in the queue and the queue head don't need to reset the tail code. For that case, a simple write to set the lock bit is enough as the queue head will be the only one eligible to get the lock as long as it checks that both the lock and pending bits are not set. The current pending bit waiting code will ensure that the bit will not be set as soon as the tail code in the lock is set.
With that change, the are some slight improvement in the performance of the queue spinlock in the 5M loop micro-benchmark run on a 4-socket Westere-EX machine as shown in the tables below.
[Standalone/Embedded - same node]
# of tasks Before patch After patch %Change
---------- ----------- ---------- -------
3 2324/2321 2248/2265 -3%/-2%
4 2890/2896 2819/2831 -2%/-2%
5 3611/3595 3522/3512 -2%/-2%
6 4281/4276 4173/4160 -3%/-3%
7 5018/5001 4875/4861 -3%/-3%
8 5759/5750 5563/5568 -3%/-3%[Standalone/Embedded - different nodes]
# of tasks Before patch After patch %Change
---------- ----------- ---------- -------
3 12242/12237 12087/12093 -1%/-1%
4 10688/10696 10507/10521 -2%/-2%It was also found that this change produced a much bigger performance improvement in the newer IvyBridge-EX chip and was essentially to close the performance gap between the ticket spinlock and queue spinlock.
The disk workload of the AIM7 benchmark was run on a 4-socket Westmere-EX machine with both ext4 and xfs RAM disks at 3000 users on a 3.14 based kernel. The results of the test runs were:
AIM7 XFS Disk Test
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- ticketlock 5678233 3.17 96.61 5.81 qspinlock 5750799 3.13 94.83 5.97
AIM7 EXT4 Disk Test
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- ticketlock 1114551 16.15 509.72 7.11 qspinlock 2184466 8.24 232.99 6.01
The ext4 filesystem run had a much higher spinlock contention than the xfs filesystem run.
The "ebizzy -m" test was also run with the following results:
kernel records/s Real Time Sys Time Usr Time ----- --------- --------- -------- -------- ticketlock 2075 10.00 216.35 3.49 qspinlock 3023 10.00 198.20 4.80
--- kernel/locking/qspinlock.c | 59 ++++++++++++++++++++++++++++++++------------ 1 files changed, 43 insertions(+), 16 deletions(-)
kernel/locking/qspinlock.c +43 -16
@@ -103,24 +103,33 @@ static inline struct mcs_spinlock *decode_tail(u32 tail)* By using the whole 2nd least significant byte for the pending bit, we * can allow better optimization of the lock acquisition for the pending * bit holder. +* +* This internal structure is also used by the set_locked function which +* is not restricted to _Q_PENDING_BITS == 8. */ -#if _Q_PENDING_BITS ==8 -struct __qspinlock {union {atomic_t val; -struct {#ifdef __LITTLE_ENDIAN +u8 locked; +struct {u16 locked_pending;u16 tail; + };#else +struct {u16 tail;u16 locked_pending; -#endif }; +struct { +u8 reserved[3 ]; +u8 locked; + }; +#endif }; }; +#if _Q_PENDING_BITS ==8 /** * clear_pending_set_locked - take ownership and clear the pending bit. * @lock: Pointer to queue spinlock structure
@@ -207,6 +216,19 @@ static __always_inline u32 xchg_tail(struct qspinlock *lock, u32 tail)#endif /* _Q_PENDING_BITS == 8 */ /** +* set_locked - Set the lock bit and own the lock +* @lock: Pointer to queue spinlock structure +* +* *,*,0 -> *,0,1 +*/ +static __always_inline void set_locked(struct qspinlock *lock) +{ +struct __qspinlock *l = (void *)lock; + +ACCESS_ONCE (l->locked) = _Q_LOCKED_VAL; +} + +/** * queue_spin_lock_slowpath - acquire the queue spinlock * @lock: Pointer to queue spinlock structure * @val: Current value of the queue spinlock 32-bit word
@@ -339,10 +361,13 @@ queue:/* * we're at the head of the waitqueue, wait for the owner & pending to * go away. +* Load-acquired is used here because the set_locked() +* function below may not be a full memory barrier. * * *,x,y -> *,0,0 */ -while ((val =atomic_read (&lock->val)) & _Q_LOCKED_PENDING_MASK) +while ((val =smp_load_acquire (&lock->val.counter)) & + _Q_LOCKED_PENDING_MASK)cpu_relax ();/*
@@ -350,15 +375,19 @@ queue:* * n,0,0 -> 0,0,1 : lock, uncontended * *,0,0 -> *,0,1 : lock, contended +* +* If the queue head is the only one in the queue (lock value == tail), +* clear the tail code and grab the lock. Otherwise, we only need +* to grab the lock. */ for (;;) { - new = _Q_LOCKED_VAL; -if (val != tail) - new |= val; - - old =atomic_cmpxchg (&lock->val, val, new); -if (old == val) +if (val != tail) { +set_locked (lock);break ; + } + old =atomic_cmpxchg (&lock->val, val, _Q_LOCKED_VAL); +if (old == val) + goto release;/* No contention */ val = old; }
@@ -366,12 +395,10 @@ queue:/* * contended path; wait for next, release. */ -if (new != _Q_LOCKED_VAL) { -while (!(next =ACCESS_ONCE (node->next))) -cpu_relax (); +while (!(next =ACCESS_ONCE (node->next))) +cpu_relax (); -arch_mcs_spin_unlock_contended (&next->locked); - } +arch_mcs_spin_unlock_contended (&next->locked); release:/*
Signature · 1 line
1.7.1
PATCH v12 7/11 qspinlock: Revert to test-and-set on hypervisors
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
When we detect a hypervisor (!paravirt, see qspinlock paravirt support patches), revert to a simple test-and-set lock to avoid the horrors of queue preemption.
--- arch/x86/include/asm/qspinlock.h | 14 ++++++++++++++ include/asm-generic/qspinlock.h | 7 +++++++ kernel/locking/qspinlock.c | 3 +++ 3 files changed, 24 insertions(+), 0 deletions(-)
arch/x86/include/asm/qspinlock.h +14 -0
@@ -1,6 +1,7 @@#ifndef _ASM_X86_QSPINLOCK_H#define _ASM_X86_QSPINLOCK_H +#include <asm/cpufeature.h> #include <asm-generic/qspinlock_types.h> #ifndef CONFIG_X86_PPRO_FENCE
@@ -20,6 +21,19 @@ static inline void queue_spin_unlock(struct qspinlock *lock)#endif /* !CONFIG_X86_PPRO_FENCE */ +#define virt_queue_spin_lock virt_queue_spin_lock + +static inline bool virt_queue_spin_lock (struct qspinlock *lock) +{ +if (!static_cpu_has (X86_FEATURE_HYPERVISOR )) +return false; + +while (atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL) !=0 ) +cpu_relax (); + +return true; +} +#include <asm-generic/qspinlock.h> #endif /* _ASM_X86_QSPINLOCK_H */
include/asm-generic/qspinlock.h +7 -0
@@ -98,6 +98,13 @@ static __always_inline void queue_spin_unlock(struct qspinlock *lock) }#endif +#ifndef virt_queue_spin_lock +static __always_inline bool virt_queue_spin_lock(struct qspinlock *lock) +{ +return false; +} +#endif +/* * Initializier */
kernel/locking/qspinlock.c +3 -0
@@ -257,6 +257,9 @@ void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val)BUILD_BUG_ON (CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)); +if (virt_queue_spin_lock (lock)) +return ; +/* * wait for in-progress pending->locked hand-overs *
Signature · 1 line
1.7.1
PATCH v12 7/11 qspinlock: Revert to test-and-set on hypervisors
In reply to Waiman LongFrom: Peter Zijlstra <[email protected]>
When we detect a hypervisor (!paravirt, see qspinlock paravirt support patches), revert to a simple test-and-set lock to avoid the horrors of queue preemption.
--- arch/x86/include/asm/qspinlock.h | 14 ++++++++++++++ include/asm-generic/qspinlock.h | 7 +++++++ kernel/locking/qspinlock.c | 3 +++ 3 files changed, 24 insertions(+), 0 deletions(-)
arch/x86/include/asm/qspinlock.h +14 -0
@@ -1,6 +1,7 @@#ifndef _ASM_X86_QSPINLOCK_H#define _ASM_X86_QSPINLOCK_H +#include <asm/cpufeature.h> #include <asm-generic/qspinlock_types.h> #ifndef CONFIG_X86_PPRO_FENCE
@@ -20,6 +21,19 @@ static inline void queue_spin_unlock(struct qspinlock *lock)#endif /* !CONFIG_X86_PPRO_FENCE */ +#define virt_queue_spin_lock virt_queue_spin_lock + +static inline bool virt_queue_spin_lock (struct qspinlock *lock) +{ +if (!static_cpu_has (X86_FEATURE_HYPERVISOR )) +return false; + +while (atomic_cmpxchg (&lock->val,0 , _Q_LOCKED_VAL) !=0 ) +cpu_relax (); + +return true; +} +#include <asm-generic/qspinlock.h> #endif /* _ASM_X86_QSPINLOCK_H */
include/asm-generic/qspinlock.h +7 -0
@@ -98,6 +98,13 @@ static __always_inline void queue_spin_unlock(struct qspinlock *lock) }#endif +#ifndef virt_queue_spin_lock +static __always_inline bool virt_queue_spin_lock(struct qspinlock *lock) +{ +return false; +} +#endif +/* * Initializier */
kernel/locking/qspinlock.c +3 -0
@@ -257,6 +257,9 @@ void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val)BUILD_BUG_ON (CONFIG_NR_CPUS >= (1U << _Q_TAIL_CPU_BITS)); +if (virt_queue_spin_lock (lock)) +return ; +/* * wait for in-progress pending->locked hand-overs *
Signature · 1 line
1.7.1
PATCH v12 8/11 qspinlock, x86: Rename paravirt_ticketlocks_enabled
In reply to Waiman LongThis patch renames the paravirt_ticketlocks_enabled static key to a more generic paravirt_spinlocks_enabled name.
--- arch/x86/include/asm/spinlock.h | 4 ++-- arch/x86/kernel/kvm.c | 2 +- arch/x86/kernel/paravirt-spinlocks.c | 4 ++-- arch/x86/xen/spinlock.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/spinlock.h +2 -2
@@ -39,7 +39,7 @@/* How long a lock should spin before we consider blocking */ #define SPIN_THRESHOLD (1 << 15) -extern struct static_key paravirt_ticketlocks_enabled; +extern struct static_key paravirt_spinlocks_enabled; static __always_inline bool static_key_false(struct static_key *key);#ifdef CONFIG_QUEUE_SPINLOCK
@@ -150,7 +150,7 @@ static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock, static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) {if (TICKET_SLOWPATH_FLAG && -static_key_false (¶virt_ticketlocks_enabled)) { +static_key_false (¶virt_spinlocks_enabled)) {arch_spinlock_t prev; prev = *lock;
arch/x86/kernel/kvm.c +1 -1
@@ -819,7 +819,7 @@ static __init int kvm_spinlock_init_jump(void)if (!kvm_para_has_feature (KVM_FEATURE_PV_UNHALT ))return 0 ; -static_key_slow_inc (¶virt_ticketlocks_enabled); +static_key_slow_inc (¶virt_spinlocks_enabled);printk (KERN_INFO "KVM setup paravirtual spinlock\n" );return 0 ;
arch/x86/kernel/paravirt-spinlocks.c +2 -2
@@ -16,5 +16,5 @@ struct pv_lock_ops pv_lock_ops = { };EXPORT_SYMBOL (pv_lock_ops); -struct static_key paravirt_ticketlocks_enabled =STATIC_KEY_INIT_FALSE ; -EXPORT_SYMBOL (paravirt_ticketlocks_enabled); +struct static_key paravirt_spinlocks_enabled =STATIC_KEY_INIT_FALSE ; +EXPORT_SYMBOL (paravirt_spinlocks_enabled);
arch/x86/xen/spinlock.c +1 -1
@@ -293,7 +293,7 @@ static __init int xen_init_spinlocks_jump(void)if (!xen_domain ())return 0 ; -static_key_slow_inc (¶virt_ticketlocks_enabled); +static_key_slow_inc (¶virt_spinlocks_enabled);return 0 ; }early_initcall (xen_init_spinlocks_jump);
Signature · 1 line
1.7.1
PATCH v12 8/11 qspinlock, x86: Rename paravirt_ticketlocks_enabled
In reply to Waiman LongThis patch renames the paravirt_ticketlocks_enabled static key to a more generic paravirt_spinlocks_enabled name.
--- arch/x86/include/asm/spinlock.h | 4 ++-- arch/x86/kernel/kvm.c | 2 +- arch/x86/kernel/paravirt-spinlocks.c | 4 ++-- arch/x86/xen/spinlock.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/spinlock.h +2 -2
@@ -39,7 +39,7 @@/* How long a lock should spin before we consider blocking */ #define SPIN_THRESHOLD (1 << 15) -extern struct static_key paravirt_ticketlocks_enabled; +extern struct static_key paravirt_spinlocks_enabled; static __always_inline bool static_key_false(struct static_key *key);#ifdef CONFIG_QUEUE_SPINLOCK
@@ -150,7 +150,7 @@ static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock, static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) {if (TICKET_SLOWPATH_FLAG && -static_key_false (¶virt_ticketlocks_enabled)) { +static_key_false (¶virt_spinlocks_enabled)) {arch_spinlock_t prev; prev = *lock;
arch/x86/kernel/kvm.c +1 -1
@@ -819,7 +819,7 @@ static __init int kvm_spinlock_init_jump(void)if (!kvm_para_has_feature (KVM_FEATURE_PV_UNHALT ))return 0 ; -static_key_slow_inc (¶virt_ticketlocks_enabled); +static_key_slow_inc (¶virt_spinlocks_enabled);printk (KERN_INFO "KVM setup paravirtual spinlock\n" );return 0 ;
arch/x86/kernel/paravirt-spinlocks.c +2 -2
@@ -16,5 +16,5 @@ struct pv_lock_ops pv_lock_ops = { };EXPORT_SYMBOL (pv_lock_ops); -struct static_key paravirt_ticketlocks_enabled =STATIC_KEY_INIT_FALSE ; -EXPORT_SYMBOL (paravirt_ticketlocks_enabled); +struct static_key paravirt_spinlocks_enabled =STATIC_KEY_INIT_FALSE ; +EXPORT_SYMBOL (paravirt_spinlocks_enabled);
arch/x86/xen/spinlock.c +1 -1
@@ -293,7 +293,7 @@ static __init int xen_init_spinlocks_jump(void)if (!xen_domain ())return 0 ; -static_key_slow_inc (¶virt_ticketlocks_enabled); +static_key_slow_inc (¶virt_spinlocks_enabled);return 0 ; }early_initcall (xen_init_spinlocks_jump);
Signature · 1 line
1.7.1
PATCH v12 8/11 qspinlock, x86: Rename paravirt_ticketlocks_enabled
In reply to Waiman LongThis patch renames the paravirt_ticketlocks_enabled static key to a more generic paravirt_spinlocks_enabled name.
--- arch/x86/include/asm/spinlock.h | 4 ++-- arch/x86/kernel/kvm.c | 2 +- arch/x86/kernel/paravirt-spinlocks.c | 4 ++-- arch/x86/xen/spinlock.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-)
arch/x86/include/asm/spinlock.h +2 -2
@@ -39,7 +39,7 @@/* How long a lock should spin before we consider blocking */ #define SPIN_THRESHOLD (1 << 15) -extern struct static_key paravirt_ticketlocks_enabled; +extern struct static_key paravirt_spinlocks_enabled; static __always_inline bool static_key_false(struct static_key *key);#ifdef CONFIG_QUEUE_SPINLOCK
@@ -150,7 +150,7 @@ static inline void __ticket_unlock_slowpath(arch_spinlock_t *lock, static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) {if (TICKET_SLOWPATH_FLAG && -static_key_false (¶virt_ticketlocks_enabled)) { +static_key_false (¶virt_spinlocks_enabled)) {arch_spinlock_t prev; prev = *lock;
arch/x86/kernel/kvm.c +1 -1
@@ -819,7 +819,7 @@ static __init int kvm_spinlock_init_jump(void)if (!kvm_para_has_feature (KVM_FEATURE_PV_UNHALT ))return 0 ; -static_key_slow_inc (¶virt_ticketlocks_enabled); +static_key_slow_inc (¶virt_spinlocks_enabled);printk (KERN_INFO "KVM setup paravirtual spinlock\n" );return 0 ;
arch/x86/kernel/paravirt-spinlocks.c +2 -2
@@ -16,5 +16,5 @@ struct pv_lock_ops pv_lock_ops = { };EXPORT_SYMBOL (pv_lock_ops); -struct static_key paravirt_ticketlocks_enabled =STATIC_KEY_INIT_FALSE ; -EXPORT_SYMBOL (paravirt_ticketlocks_enabled); +struct static_key paravirt_spinlocks_enabled =STATIC_KEY_INIT_FALSE ; +EXPORT_SYMBOL (paravirt_spinlocks_enabled);
arch/x86/xen/spinlock.c +1 -1
@@ -293,7 +293,7 @@ static __init int xen_init_spinlocks_jump(void)if (!xen_domain ())return 0 ; -static_key_slow_inc (¶virt_ticketlocks_enabled); +static_key_slow_inc (¶virt_spinlocks_enabled);return 0 ; }early_initcall (xen_init_spinlocks_jump);
Signature · 1 line
1.7.1
PATCH v12 9/11 pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongThis patch adds para-virtualization support to the queue spinlock code base with minimal impact to the native case. There are some minor code changes in the generic qspinlock.c file which should be usable in other architectures. The other code changes are specific to x86 processors and so are all put under the arch/x86 directory.
On the lock side, there are a couple of jump labels and 2 paravirt callee saved calls that defaults to NOPs and some registered move instructions. So the performance impact should be minimal.
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
The actual paravirt code comes in 5 parts;
- init_node; this initializes the extra data members required for PV state. PV state data is kept 1 cacheline ahead of the regular data.
- link_and_wait_node; this replaces the regular MCS queuing code. CPU halting can happen if the wait is too long.
- wait_head; this waits until the lock is avialable and the CPU will be halted if the wait is too long.
- wait_check; this is called after acquiring the lock to see if the next queue head CPU is halted. If this is the case, the lock bit is changed to indicate the queue head will have to be kicked on unlock.
- queue_unlock; this routine has a jump label to check if paravirt is enabled. If yes, it has to do an atomic cmpxchg to clear the lock bit or call the slowpath function to kick the queue head cpu.
Tracking the head is done in two parts, firstly the pv_wait_head will store its cpu number in whichever node is pointed to by the tail part of the lock word. Secondly, pv_link_and_wait_node() will propagate the existing head from the old to the new tail node.
--- arch/x86/include/asm/paravirt.h | 20 ++ arch/x86/include/asm/paravirt_types.h | 20 ++ arch/x86/include/asm/pvqspinlock.h | 403 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/qspinlock.h | 44 ++++- arch/x86/kernel/paravirt-spinlocks.c | 6 + kernel/locking/qspinlock.c | 72 ++++++- 6 files changed, 558 insertions(+), 7 deletions(-) create mode 100644 arch/x86/include/asm/pvqspinlock.h
arch/x86/include/asm/paravirt.h +20 -0
@@ -712,6 +712,25 @@ static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx, #if defined(CONFIG_SMP) && defined(CONFIG_PARAVIRT_SPINLOCKS) +#ifdef CONFIG_QUEUE_SPINLOCK + +static __always_inline void pv_kick_cpu(int cpu) +{ + PVOP_VCALLEE1(pv_lock_ops.kick_cpu, cpu); +} + +static __always_inline void +pv_lockwait(u8 *lockbyte) +{ + PVOP_VCALLEE1(pv_lock_ops.lockwait, lockbyte); +} + +static __always_inline void pv_lockstat(enum pv_lock_stats type) +{ + PVOP_VCALLEE1(pv_lock_ops.lockstat, type); +} + +#else static __always_inline void __ticket_lock_spinning(struct arch_spinlock *lock, __ticket_t ticket) {
@@ -723,6 +742,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, {PVOP_VCALL2 (pv_lock_ops.unlock_kick, lock, ticket); } +#endif #endif
arch/x86/include/asm/paravirt_types.h +20 -0
@@ -326,6 +326,9 @@ struct pv_mmu_ops { phys_addr_t phys, pgprot_t flags); }; +struct mcs_spinlock ; +struct qspinlock ; +struct arch_spinlock ;#ifdef CONFIG_SMP #include <asm/spinlock_types.h>
@@ -333,9 +336,26 @@ struct arch_spinlock;typedef u16 __ticket_t ;#endif +#ifdef CONFIG_QUEUE_SPINLOCK +enum pv_lock_stats { +PV_HALT_QHEAD ,/* Queue head halting */ +PV_HALT_QNODE ,/* Other queue node halting */ +PV_HALT_ABORT ,/* Halting aborted */ +PV_WAKE_KICKED ,/* Wakeup by kicking */ +PV_WAKE_SPURIOUS ,/* Spurious wakeup */ +PV_KICK_NOHALT /* Kick but CPU not halted */ +}; +#endif +struct pv_lock_ops { +#ifdef CONFIG_QUEUE_SPINLOCK +struct paravirt_callee_save kick_cpu; +struct paravirt_callee_save lockstat; +struct paravirt_callee_save lockwait; +#else struct paravirt_callee_save lock_spinning;void (*unlock_kick)(struct arch_spinlock *lock,__ticket_t ticket); +#endif };/* This contains all the paravirt structures: we get a convenient
arch/x86/include/asm/pvqspinlock.h new file +403 -0
403 lines not on this page · raw message
arch/x86/include/asm/qspinlock.h +41 -3
62 lines not on this page · raw message
arch/x86/kernel/paravirt-spinlocks.c +6 -0
@@ -10,9 +10,15 @@ struct pv_lock_ops pv_lock_ops = {#ifdef CONFIG_SMP +#ifdef CONFIG_QUEUE_SPINLOCK + .kick_cpu = __PV_IS_CALLEE_SAVE(paravirt_nop), + .lockstat = __PV_IS_CALLEE_SAVE(paravirt_nop), + .lockwait = __PV_IS_CALLEE_SAVE(paravirt_nop), +#else .lock_spinning = __PV_IS_CALLEE_SAVE(paravirt_nop), .unlock_kick = paravirt_nop,#endif +#endif };EXPORT_SYMBOL (pv_lock_ops);
kernel/locking/qspinlock.c +68 -4
121 lines not on this page · raw message
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 17 lines · Waiman Long
arch/x86/include/asm/pvqspinlock.h new file +14
-0+static inline void pv_init_node (struct mcs_spinlock *node) +{ +struct pv_qnode *pn = (struct pv_qnode *)node; + +BUILD_BUG_ON (sizeof (struct pv_qnode ) >5 *sizeof (struct mcs_spinlock )); + +if (!pv_enabled ()) +return ; + + pn->cpustate =PV_CPU_ACTIVE ; + pn->mayhalt = false; + pn->mycpu =smp_processor_id (); + pn->head =PV_INVALID_HEAD ; +}
arch/x86/include/asm/pvqspinlock.h queue: · line 399
Quoted 7 lines
arch/x86/include/asm/pvqspinlock.h new file +1
-0@@ -333,6 +393,7 @@ queue: node += idx; node->locked =0 ; node->next =NULL ; +pv_init_node (node);/* * We touched a (possibly) cold cacheline in the per-cpu queue node;
So even if !pv_enabled() the compiler will still have to emit the code for that inline, which will generate additional register pressure, icache pressure and lovely stuff like that.
The patch I had used pv-ops for these things that would turn into NOPs in the regular case and callee-saved function calls for the PV case.
That still does not entirely eliminate cost, but does reduce it significant. Please consider using that.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 29 lines · Peter Zijlstra
Quoted 16 lines · Waiman Long
arch/x86/include/asm/pvqspinlock.h new file +14
-0+static inline void pv_init_node (struct mcs_spinlock *node) +{ +struct pv_qnode *pn = (struct pv_qnode *)node; + +BUILD_BUG_ON (sizeof (struct pv_qnode )>5 *sizeof (struct mcs_spinlock )); + +if (!pv_enabled ()) +return ; + + pn->cpustate =PV_CPU_ACTIVE ; + pn->mayhalt = false; + pn->mycpu =smp_processor_id (); + pn->head =PV_INVALID_HEAD ; +}arch/x86/include/asm/pvqspinlock.h queue: · line 399
Quoted 7 lines
arch/x86/include/asm/pvqspinlock.h new file +1
-0@@ -333,6 +393,7 @@ queue: node += idx; node->locked =0 ; node->next =NULL ; +pv_init_node (node);/* * We touched a (possibly) cold cacheline in the per-cpu queue node; So even if !pv_enabled() the compiler will still have to emit the code for that inline, which will generate additional register pressure, icache pressure and lovely stuff like that.
Quoted 5 lines
The patch I had used pv-ops for these things that would turn into NOPs in the regular case and callee-saved function calls for the PV case.
That still does not entirely eliminate cost, but does reduce it significant. Please consider using that.
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Another alternative that I can think of is to generate 2 versions of the slowpath code - one pv and one non-pv out of the same source code. The non-pv code will call into the pv code once if pv is enabled. In this way, it won't increase the icache and register pressure of the non-pv code. However, this may make the source code a bit harder to read.
Please let me know your thought on this alternate approach.
-Longman
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 10 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I tried some benching recently.. where did they hide the fastpaths? :)
-Mike
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I tried some benching recently.. where did they hide the fastpaths? :)
-Mike
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I tried some benching recently.. where did they hide the fastpaths? :)
-Mike
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anyway, I had done some measurements. In my test system, the queue_spin_lock_slowpath() function has a text size of about 400 bytes without PV, but 1120 bytes with PV. I made some changes to create separate versions of PV and non-PV slowpath functions as shown by the diff below. The text size is now about 430 bytes for the non-PV version and 925 bytes for the PV version. The overall object size increases by a bit more than 200 bytes, but the icache footprint should be reduced no matter which version is used.
-Longman
----------------------------------------
arch/x86/include/asm/pvqspinlock.h +0 -0
@@ -79,9 +79,6 @@ static inline void pv_init_node(struct mcs_spinlock *node) BUILD_BUG_ON(sizeof(struct pv_qnode) > 5*sizeof(struct
mcs_spinlock));
- if (!pv_enabled())
- return;
-
pn->cpustate = PV_CPU_ACTIVE;
pn->mayhalt = false;
pn->mycpu = smp_processor_id();
@@ -132,9 +129,6 @@ static inline bool pv_link_and_wait_node(u32 old, struct mcs
struct pv_qnode *ppn, *pn = (struct pv_qnode *)node;
unsigned int count;- if (!pv_enabled())
- return false;
-
if (!(old & _Q_TAIL_MASK)) {
node->locked = true; /* At queue head now */
goto ret;
@@ -236,9 +230,6 @@ pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *no
{
struct pv_qnode *pn = (struct pv_qnode *)node;- if (!pv_enabled())
- return smp_load_acquire(&lock->val.counter);
-
for (;;) {
unsigned int count;
s8 oldstate;
@@ -328,8 +319,6 @@ static inline void pv_wait_check(struct qspinlock *lock,
struct pv_qnode *pnxt = (struct pv_qnode *)next;
struct pv_qnode *pcur = (struct pv_qnode *)node;- if (!pv_enabled())
- return;
/*
* Clear the locked and head values of lock holder
*/kernel/locking/qspinlock.c +57 -9
99 lines not on this page · raw message
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 9 lines · Waiman Long
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Quoted 6 lines
Quoted 3 lines
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anything that avoids the lock holder preemption nonsense is a _massive_ win for them, a few function calls should not even register on that scale.
kernel/locking/qspinlock.c static inline int pv_wait_head(struct qspinlock *lock, · line 332
Quoted 5 lines
kernel/locking/qspinlock.c +4
-0+#ifdef _GEN_PV_LOCK_SLOWPATH +static void pv_queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#else void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#endif
If you have two functions you might as well use the PV stuff to patch in the right function call at the usage sites and avoid:
kernel/locking/qspinlock.c void queue_spin_lock_slowpath(struct qspinlock *lock, u32 v · line 343
Quoted 4 lines
kernel/locking/qspinlock.c +4
-0+if (pv_enabled ()) { +pv_queue_spin_lock_slowpath (lock, val); +return ; + }
this alltogether.
Quoted 13 lines
this_cpu_dec(mcs_nodes[0].count);}
EXPORT_SYMBOL(queue_spin_lock_slowpath);kernel/locking/qspinlock.c +10
-0+ +#if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS ) +/* +* Generate the PV version of the queue_spin_lock_slowpath function +*/ +#undef pv_init_node +#undef pv_wait_check +#undef pv_link_and_wait_node +#undef pv_wait_head +#undef EXPORT_SYMBOL
kernel/locking/qspinlock.c release: · line 524
Quoted 10 lines
kernel/locking/qspinlock.c +10
-0+#undef in_pv_code + +#define _GEN_PV_LOCK_SLOWPATH +#define EXPORT_SYMBOL (x) +#define in_pv_code return_true +#define pv_enabled return_false + +#include "qspinlock.c" + +#endif
That's properly disgusting :-) But a lot better than actually duplicating everything I suppose.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Waiman Long
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anything that avoids the lock holder preemption nonsense is a _massive_ win for them, a few function calls should not even register on that scale.
I would say all the PV stuffs are mostly useful for a over-committed guest where a single CPU is shared in more than one guest. When the guests are not overcommitted, the PV code seldom get triggered. In those cases, the overhead of the extra function call and register saving/restoring will show up.
Quoted 8 lines
kernel/locking/qspinlock.c static inline int pv_wait_head(struct qspinlock *lock, · line 332
Quoted 5 lines
kernel/locking/qspinlock.c +4
-0+#ifdef _GEN_PV_LOCK_SLOWPATH +static void pv_queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#else void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#endif If you have two functions you might as well use the PV stuff to patch in the right function call at the usage sites and avoid:
Quoted 5 lines
kernel/locking/qspinlock.c void queue_spin_lock_slowpath(struct qspinlock *lock, u32 v · line 343
Quoted 4 lines
kernel/locking/qspinlock.c +4
-0+if (pv_enabled ()) { +pv_queue_spin_lock_slowpath (lock, val); +return ; + }this alltogether.
Good point! I will do some investigation on how to do this kind of function address patching and eliminate the extra function call overhead.
Quoted 17 lines
Quoted 17 lines
this_cpu_dec(mcs_nodes[0].count);}
EXPORT_SYMBOL(queue_spin_lock_slowpath);kernel/locking/qspinlock.c +14
-0+ +#if !defined(_GEN_PV_LOCK_SLOWPATH)&& defined(CONFIG_PARAVIRT_SPINLOCKS ) +/* +* Generate the PV version of the queue_spin_lock_slowpath function +*/ +#undef pv_init_node +#undef pv_wait_check +#undef pv_link_and_wait_node +#undef pv_wait_head +#undef EXPORT_SYMBOL +#undef in_pv_code + +#define _GEN_PV_LOCK_SLOWPATH +#define EXPORT_SYMBOL (x)
Quoted 8 lines
kernel/locking/qspinlock.c release: · line 524
Quoted 6 lines
kernel/locking/qspinlock.c +6
-0+#define in_pv_code return_true +#define pv_enabled return_false + +#include "qspinlock.c" + +#endif That's properly disgusting :-) But a lot better than actually duplicating everything I suppose.
I know you don't like this kind of preprocessor trick, but this is the easiest way that I can think of to generate two separate functions from the same source code.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 10 lines · Peter Zijlstra
Quoted 9 lines · Waiman Long
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anything that avoids the lock holder preemption nonsense is a _massive_ win for them, a few function calls should not even register on that scale.
I would say all the PV stuffs are mostly useful for a over-committed guest where a single CPU is shared in more than one guest. When the guests are not overcommitted, the PV code seldom get triggered. In those cases, the overhead of the extra function call and register saving/restoring will show up.
Quoted 8 lines
kernel/locking/qspinlock.c static inline int pv_wait_head(struct qspinlock *lock, · line 332
Quoted 5 lines
kernel/locking/qspinlock.c +4
-0+#ifdef _GEN_PV_LOCK_SLOWPATH +static void pv_queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#else void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#endif If you have two functions you might as well use the PV stuff to patch in the right function call at the usage sites and avoid:
Quoted 5 lines
kernel/locking/qspinlock.c void queue_spin_lock_slowpath(struct qspinlock *lock, u32 v · line 343
Quoted 4 lines
kernel/locking/qspinlock.c +4
-0+if (pv_enabled ()) { +pv_queue_spin_lock_slowpath (lock, val); +return ; + }this alltogether.
Good point! I will do some investigation on how to do this kind of function address patching and eliminate the extra function call overhead.
Quoted 17 lines
Quoted 17 lines
this_cpu_dec(mcs_nodes[0].count);}
EXPORT_SYMBOL(queue_spin_lock_slowpath);kernel/locking/qspinlock.c +14
-0+ +#if !defined(_GEN_PV_LOCK_SLOWPATH)&& defined(CONFIG_PARAVIRT_SPINLOCKS ) +/* +* Generate the PV version of the queue_spin_lock_slowpath function +*/ +#undef pv_init_node +#undef pv_wait_check +#undef pv_link_and_wait_node +#undef pv_wait_head +#undef EXPORT_SYMBOL +#undef in_pv_code + +#define _GEN_PV_LOCK_SLOWPATH +#define EXPORT_SYMBOL (x)
Quoted 8 lines
kernel/locking/qspinlock.c release: · line 524
Quoted 6 lines
kernel/locking/qspinlock.c +6
-0+#define in_pv_code return_true +#define pv_enabled return_false + +#include "qspinlock.c" + +#endif That's properly disgusting :-) But a lot better than actually duplicating everything I suppose.
I know you don't like this kind of preprocessor trick, but this is the easiest way that I can think of to generate two separate functions from the same source code.
-Longman
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 9 lines · Waiman Long
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Quoted 6 lines
Quoted 3 lines
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anything that avoids the lock holder preemption nonsense is a _massive_ win for them, a few function calls should not even register on that scale.
kernel/locking/qspinlock.c static inline int pv_wait_head(struct qspinlock *lock, · line 332
Quoted 5 lines
kernel/locking/qspinlock.c +4
-0+#ifdef _GEN_PV_LOCK_SLOWPATH +static void pv_queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#else void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#endif
If you have two functions you might as well use the PV stuff to patch in the right function call at the usage sites and avoid:
kernel/locking/qspinlock.c void queue_spin_lock_slowpath(struct qspinlock *lock, u32 v · line 343
Quoted 4 lines
kernel/locking/qspinlock.c +4
-0+if (pv_enabled ()) { +pv_queue_spin_lock_slowpath (lock, val); +return ; + }
this alltogether.
Quoted 13 lines
this_cpu_dec(mcs_nodes[0].count);}
EXPORT_SYMBOL(queue_spin_lock_slowpath);kernel/locking/qspinlock.c +10
-0+ +#if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS ) +/* +* Generate the PV version of the queue_spin_lock_slowpath function +*/ +#undef pv_init_node +#undef pv_wait_check +#undef pv_link_and_wait_node +#undef pv_wait_head +#undef EXPORT_SYMBOL
kernel/locking/qspinlock.c release: · line 524
Quoted 10 lines
kernel/locking/qspinlock.c +10
-0+#undef in_pv_code + +#define _GEN_PV_LOCK_SLOWPATH +#define EXPORT_SYMBOL (x) +#define in_pv_code return_true +#define pv_enabled return_false + +#include "qspinlock.c" + +#endif
That's properly disgusting :-) But a lot better than actually duplicating everything I suppose.
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 9 lines · Waiman Long
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Quoted 6 lines
Quoted 3 lines
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anything that avoids the lock holder preemption nonsense is a _massive_ win for them, a few function calls should not even register on that scale.
kernel/locking/qspinlock.c static inline int pv_wait_head(struct qspinlock *lock, · line 332
Quoted 5 lines
kernel/locking/qspinlock.c +4
-0+#ifdef _GEN_PV_LOCK_SLOWPATH +static void pv_queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#else void queue_spin_lock_slowpath(struct qspinlock *lock, u32 val) +#endif
If you have two functions you might as well use the PV stuff to patch in the right function call at the usage sites and avoid:
kernel/locking/qspinlock.c void queue_spin_lock_slowpath(struct qspinlock *lock, u32 v · line 343
Quoted 4 lines
kernel/locking/qspinlock.c +4
-0+if (pv_enabled ()) { +pv_queue_spin_lock_slowpath (lock, val); +return ; + }
this alltogether.
Quoted 13 lines
this_cpu_dec(mcs_nodes[0].count);}
EXPORT_SYMBOL(queue_spin_lock_slowpath);kernel/locking/qspinlock.c +10
-0+ +#if !defined(_GEN_PV_LOCK_SLOWPATH) && defined(CONFIG_PARAVIRT_SPINLOCKS ) +/* +* Generate the PV version of the queue_spin_lock_slowpath function +*/ +#undef pv_init_node +#undef pv_wait_check +#undef pv_link_and_wait_node +#undef pv_wait_head +#undef EXPORT_SYMBOL
kernel/locking/qspinlock.c release: · line 524
Quoted 10 lines
kernel/locking/qspinlock.c +10
-0+#undef in_pv_code + +#define _GEN_PV_LOCK_SLOWPATH +#define EXPORT_SYMBOL (x) +#define in_pv_code return_true +#define pv_enabled return_false + +#include "qspinlock.c" + +#endif
That's properly disgusting :-) But a lot better than actually duplicating everything I suppose.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 11 lines · Peter Zijlstra
Quoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Yes, I am aware of that. However, the whole point of doing PV spinlock is to improve performance in a virtual guest.
Anyway, I had done some measurements. In my test system, the queue_spin_lock_slowpath() function has a text size of about 400 bytes without PV, but 1120 bytes with PV. I made some changes to create separate versions of PV and non-PV slowpath functions as shown by the diff below. The text size is now about 430 bytes for the non-PV version and 925 bytes for the PV version. The overall object size increases by a bit more than 200 bytes, but the icache footprint should be reduced no matter which version is used.
-Longman
----------------------------------------
arch/x86/include/asm/pvqspinlock.h +0 -0
@@ -79,9 +79,6 @@ static inline void pv_init_node(struct mcs_spinlock *node) BUILD_BUG_ON(sizeof(struct pv_qnode) > 5*sizeof(struct
mcs_spinlock));
- if (!pv_enabled())
- return;
-
pn->cpustate = PV_CPU_ACTIVE;
pn->mayhalt = false;
pn->mycpu = smp_processor_id();
@@ -132,9 +129,6 @@ static inline bool pv_link_and_wait_node(u32 old, struct mcs
struct pv_qnode *ppn, *pn = (struct pv_qnode *)node;
unsigned int count;- if (!pv_enabled())
- return false;
-
if (!(old & _Q_TAIL_MASK)) {
node->locked = true; /* At queue head now */
goto ret;
@@ -236,9 +230,6 @@ pv_wait_head(struct qspinlock *lock, struct mcs_spinlock *no
{
struct pv_qnode *pn = (struct pv_qnode *)node;- if (!pv_enabled())
- return smp_load_acquire(&lock->val.counter);
-
for (;;) {
unsigned int count;
s8 oldstate;
@@ -328,8 +319,6 @@ static inline void pv_wait_check(struct qspinlock *lock,
struct pv_qnode *pnxt = (struct pv_qnode *)next;
struct pv_qnode *pcur = (struct pv_qnode *)node;- if (!pv_enabled())
- return;
/*
* Clear the locked and head values of lock holder
*/kernel/locking/qspinlock.c +57 -9
99 lines not on this page · raw message
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 7 lines · Waiman Long
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
If people care about performance they should not be using virt crap :-)
I only really care about bare metal.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 29 lines · Peter Zijlstra
Quoted 16 lines · Waiman Long
arch/x86/include/asm/pvqspinlock.h new file +14
-0+static inline void pv_init_node (struct mcs_spinlock *node) +{ +struct pv_qnode *pn = (struct pv_qnode *)node; + +BUILD_BUG_ON (sizeof (struct pv_qnode )>5 *sizeof (struct mcs_spinlock )); + +if (!pv_enabled ()) +return ; + + pn->cpustate =PV_CPU_ACTIVE ; + pn->mayhalt = false; + pn->mycpu =smp_processor_id (); + pn->head =PV_INVALID_HEAD ; +}arch/x86/include/asm/pvqspinlock.h queue: · line 399
Quoted 7 lines
arch/x86/include/asm/pvqspinlock.h new file +1
-0@@ -333,6 +393,7 @@ queue: node += idx; node->locked =0 ; node->next =NULL ; +pv_init_node (node);/* * We touched a (possibly) cold cacheline in the per-cpu queue node; So even if !pv_enabled() the compiler will still have to emit the code for that inline, which will generate additional register pressure, icache pressure and lovely stuff like that.
Quoted 5 lines
The patch I had used pv-ops for these things that would turn into NOPs in the regular case and callee-saved function calls for the PV case.
That still does not entirely eliminate cost, but does reduce it significant. Please consider using that.
The additional register pressure may just cause a few more register moves which should be negligible in the overall performance . The additional icache pressure, however, may have some impact on performance. I was trying to balance the performance of the pv and non-pv versions so that we won't penalize the pv code too much for a bit more performance in the non-pv code. Doing it your way will add a lot of function call and register saving/restoring to the pv code.
Another alternative that I can think of is to generate 2 versions of the slowpath code - one pv and one non-pv out of the same source code. The non-pv code will call into the pv code once if pv is enabled. In this way, it won't increase the icache and register pressure of the non-pv code. However, this may make the source code a bit harder to read.
Please let me know your thought on this alternate approach.
-Longman
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 17 lines · Waiman Long
arch/x86/include/asm/pvqspinlock.h new file +14
-0+static inline void pv_init_node (struct mcs_spinlock *node) +{ +struct pv_qnode *pn = (struct pv_qnode *)node; + +BUILD_BUG_ON (sizeof (struct pv_qnode ) >5 *sizeof (struct mcs_spinlock )); + +if (!pv_enabled ()) +return ; + + pn->cpustate =PV_CPU_ACTIVE ; + pn->mayhalt = false; + pn->mycpu =smp_processor_id (); + pn->head =PV_INVALID_HEAD ; +}
arch/x86/include/asm/pvqspinlock.h queue: · line 399
Quoted 7 lines
arch/x86/include/asm/pvqspinlock.h new file +1
-0@@ -333,6 +393,7 @@ queue: node += idx; node->locked =0 ; node->next =NULL ; +pv_init_node (node);/* * We touched a (possibly) cold cacheline in the per-cpu queue node;
So even if !pv_enabled() the compiler will still have to emit the code for that inline, which will generate additional register pressure, icache pressure and lovely stuff like that.
The patch I had used pv-ops for these things that would turn into NOPs in the regular case and callee-saved function calls for the PV case.
That still does not entirely eliminate cost, but does reduce it significant. Please consider using that.
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 17 lines · Waiman Long
arch/x86/include/asm/pvqspinlock.h new file +14
-0+static inline void pv_init_node (struct mcs_spinlock *node) +{ +struct pv_qnode *pn = (struct pv_qnode *)node; + +BUILD_BUG_ON (sizeof (struct pv_qnode ) >5 *sizeof (struct mcs_spinlock )); + +if (!pv_enabled ()) +return ; + + pn->cpustate =PV_CPU_ACTIVE ; + pn->mayhalt = false; + pn->mycpu =smp_processor_id (); + pn->head =PV_INVALID_HEAD ; +}
arch/x86/include/asm/pvqspinlock.h queue: · line 399
Quoted 7 lines
arch/x86/include/asm/pvqspinlock.h new file +1
-0@@ -333,6 +393,7 @@ queue: node += idx; node->locked =0 ; node->next =NULL ; +pv_init_node (node);/* * We touched a (possibly) cold cacheline in the per-cpu queue node;
So even if !pv_enabled() the compiler will still have to emit the code for that inline, which will generate additional register pressure, icache pressure and lovely stuff like that.
The patch I had used pv-ops for these things that would turn into NOPs in the regular case and callee-saved function calls for the PV case.
That still does not entirely eliminate cost, but does reduce it significant. Please consider using that.
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
arch/x86/kernel/paravirt_patch_64.c +7 -0
@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) && defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,
@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) && defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);
That makes sure to overwrite the callee-saved call to the pv_lock_ops::queue_unlock with the immediate asm "movb $0, (%rdi)".
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 42 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);
Quoted 7 lines
That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that spin_unlock() can be called in many places, including loadable kernel modules. Can the paravirt_patch_ident_32() function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
Regards,
Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".
Quoted 8 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.
Quoted 3 lines
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
No need. Inline should (And is) working just fine.
Quoted 3 lines
Regards,
Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 55 lines · Konrad Rzeszutek Wilk
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 10 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.Quoted 2 lines
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
No need. Inline should (And is) working just fine.
Quoted 2 lines
Regards,
Longman
Thanks for letting me know about the paravirt patching capability available in the kernel. In this case, I would say we should use Peter's way of doing unlock without disabling unlock function inlining. That will further reduce the performance difference of kernels with and without PV.
Cheer,
Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Konrad Rzeszutek WilkQuoted 55 lines · Konrad Rzeszutek Wilk
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 10 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.Quoted 2 lines
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
No need. Inline should (And is) working just fine.
Quoted 2 lines
Regards,
Longman
Thanks for letting me know about the paravirt patching capability available in the kernel. In this case, I would say we should use Peter's way of doing unlock without disabling unlock function inlining. That will further reduce the performance difference of kernels with and without PV.
Cheer,
Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Konrad Rzeszutek WilkQuoted 55 lines · Konrad Rzeszutek Wilk
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 10 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.Quoted 2 lines
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
No need. Inline should (And is) working just fine.
Quoted 2 lines
Regards,
Longman
Thanks for letting me know about the paravirt patching capability available in the kernel. In this case, I would say we should use Peter's way of doing unlock without disabling unlock function inlining. That will further reduce the performance difference of kernels with and without PV.
Cheer,
Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Konrad Rzeszutek WilkQuoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Waiman Long
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 6 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.
I have been changing the patching code to patch the unlock call sites and it seems to be working now. However, when I manually inserted a kernel module using insmod and run the code in the newly inserted module, I got memory access violation as follows:
BUG: unable to handle kernel NULL pointer dereference at (null) IP: [< (null)>] (null) PGD 18d62f3067 PUD 18d476f067 PMD 0 Oops: 0010 [#1] SMP Modules linked in: locktest(OE) ebtable_nat ebtables xt_CHECKSUM iptable_mangle bridge autofs4 8021q garp stp llc ipt_REJECT nf_conntrack_ipv4 nf_defrag_ipv4 iptable_filter ip_tables ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 xt_state nf_conntrack ip6table_filter ip6_tables ipv6 vhost_net macvtap macvlan vhost tun uinput ppdev parport_pc parport sg microcode pcspkr virtio_balloon snd_hda_codec_generic virtio_console snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore virtio_net i2c_piix4 i2c_core ext4(E) jbd2(E) mbcache(E) floppy(E) virtio_blk(E) sr_mod(E) cdrom(E) virtio_pci(E) virtio_ring(E) virtio(E) pata_acpi(E) ata_generic(E) ata_piix(E) dm_mirror(E) dm_region_hash(E) dm_log(E) dm_mod(E) [last unloaded: speedstep_lib] CPU: 1 PID: 3907 Comm: run-locktest Tainted: G W OE 3.17.0-pvqlock #3 Hardware name: Red Hat KVM, BIOS Bochs 01/01/2011 task: ffff8818cc5baf90 ti: ffff8818b7094000 task.ti: ffff8818b7094000 RIP: 0010:[<0000000000000000>] [< (null)>] (null) RSP: 0018:ffff8818b7097db0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 00000000004c4b40 RCX: 0000000000000000 RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffff8818d3f052c0 RBP: ffff8818b7097dd8 R08: 0000000080522014 R09: 0000000000000000 R10: 0000000000001000 R11: 0000000000000001 R12: 0000000000000001 R13: 0000000000000000 R14: 0000000000000001 R15: ffff8818b7097ea0 FS: 00007fb828ece700(0000) GS:ffff88193ec20000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 0000000000000000 CR3: 00000018cc7e9000 CR4: 00000000000006e0 Stack: ffffffffa06ff395 ffff8818d465e000 ffffffff8164bec0 0000000000000001 0000000000000050 ffff8818b7097e18 ffffffffa06ff785 ffff8818b7097e38 0000000000000246 0000000054755e3a 0000000039f8ba72 ffff8818c174f000 Call Trace: [<ffffffffa06ff395>] ? test_spinlock+0x65/0x90 [locktest] [<ffffffffa06ff785>] etime_show+0xd5/0x120 [locktest] [<ffffffff812a2dc6>] kobj_attr_show+0x16/0x20 [<ffffffff8121a7fa>] sysfs_kf_seq_show+0xca/0x1b0 [<ffffffff81218a13>] kernfs_seq_show+0x23/0x30 [<ffffffff811c82db>] seq_read+0xbb/0x400 [<ffffffff812197e5>] kernfs_fop_read+0x35/0x40 [<ffffffff811a4223>] vfs_read+0xa3/0x110 [<ffffffff811a47e6>] SyS_read+0x56/0xd0 [<ffffffff810f3e16>] ? __audit_syscall_exit+0x216/0x2c0 [<ffffffff815b3ca9>] system_call_fastpath+0x16/0x1b Code: Bad RIP value. RSP <ffff8818b7097db0> CR2: 0000000000000000 ---[ end trace 69d0e259c9ec632f ]---
It seems like call site patching isn't properly done or the kernel module that I built was missing some critical information necessary for the proper linking. Anyway, I will include the unlock call patching code as a separate patch as it seems there may be problem under certain circumstance.
BTW, the kernel panic problem that your team reported had been fixed. The fix will be in the next version of the patch.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 70 lines · Waiman Long
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Waiman Long
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?Quoted 6 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.I have been changing the patching code to patch the unlock call sites and it seems to be working now. However, when I manually inserted a kernel module using insmod and run the code in the newly inserted module, I got memory access violation as follows:
BUG: unable to handle kernel NULL pointer dereference at (null) IP: [< (null)>] (null) PGD 18d62f3067 PUD 18d476f067 PMD 0 Oops: 0010 [#1] SMP Modules linked in: locktest(OE) ebtable_nat ebtables xt_CHECKSUM
iptable_manglebridge autofs4 8021q garp stp llcipt_REJECTnf_conntrack_ipv4nf_defrag_ipv4iptable_filterip_tablesip6t_REJECTnf_conntrack_ipv6nf_defrag_ipv6xt_statenf_conntrackip6table_filterip6_tablesipv6vhost_netmacvtap macvlan vhost tun uinput ppdevparport_pcparport sg microcode pcspkrvirtio_balloonsnd_hda_codec_genericvirtio_consolesnd_hda_intelsnd_hda_controllersnd_hda_codecsnd_hwdepsnd_seqsnd_seq_devicesnd_pcmsnd_timersnd soundcorevirtio_neti2c_piix4i2c_coreext4(E) jbd2(E) mbcache(E) floppy(E)virtio_blk(E)sr_mod(E)cdrom(E)virtio_pci(E)virtio_ring(E)virtio(E)pata_acpi(E)ata_generic(E)ata_piix(E)dm_mirror(E)dm_region_hash(E)dm_log(E)dm_mod(E)[last
unloaded:speedstep_lib]CPU: 1 PID: 3907 Comm: run-locktest Tainted: G W OE 3.17.0-pvqlock #3 Hardware name: Red Hat KVM, BIOS Bochs 01/01/2011 task: ffff8818cc5baf90 ti: ffff8818b7094000 task.ti: ffff8818b7094000 RIP: 0010:[<0000000000000000>] [< (null)>] (null) RSP: 0018:ffff8818b7097db0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 00000000004c4b40 RCX: 0000000000000000 RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffff8818d3f052c0 RBP: ffff8818b7097dd8 R08: 0000000080522014 R09: 0000000000000000 R10: 0000000000001000 R11: 0000000000000001 R12: 0000000000000001 R13: 0000000000000000 R14: 0000000000000001 R15: ffff8818b7097ea0 FS: 00007fb828ece700(0000) GS:ffff88193ec20000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 0000000000000000 CR3: 00000018cc7e9000 CR4: 00000000000006e0 Stack: ffffffffa06ff395 ffff8818d465e000 ffffffff8164bec0 0000000000000001 0000000000000050 ffff8818b7097e18 ffffffffa06ff785 ffff8818b7097e38 0000000000000246 0000000054755e3a 0000000039f8ba72 ffff8818c174f000 Call Trace: [<ffffffffa06ff395>] ? test_spinlock+0x65/0x90 [locktest] [<ffffffffa06ff785>] etime_show+0xd5/0x120 [locktest] [<ffffffff812a2dc6>] kobj_attr_show+0x16/0x20 [<ffffffff8121a7fa>] sysfs_kf_seq_show+0xca/0x1b0 [<ffffffff81218a13>] kernfs_seq_show+0x23/0x30 [<ffffffff811c82db>] seq_read+0xbb/0x400 [<ffffffff812197e5>] kernfs_fop_read+0x35/0x40 [<ffffffff811a4223>] vfs_read+0xa3/0x110 [<ffffffff811a47e6>] SyS_read+0x56/0xd0 [<ffffffff810f3e16>] ? __audit_syscall_exit+0x216/0x2c0 [<ffffffff815b3ca9>] system_call_fastpath+0x16/0x1b Code: Bad RIP value. RSP <ffff8818b7097db0> CR2: 0000000000000000 ---[ end trace 69d0e259c9ec632f ]---
Quoted 2 lines
It seems like call site patching isn't properly done or the kernel module that I built was missing some critical information necessary for the proper
Did the readelf give you the paravirt note section?
Quoted 2 lines
linking. Anyway, I will include the unlock call patching code as a separate patch as it seems there may be problem under certain circumstance.
one way to troubleshoot those is to enable the paravirt patching code to actually print where it is patching the code. That way when you load the module you can confirm it has done its job.
Then you can verify that the address where the code is called:
ffffffffa06ff395
is indeed patched. You might as well also do a hexdump in the module loading to confim that the patching had been done correctly.
Quoted 5 lines
BTW, the kernel panic problem that your team reported had been fixed. The fix will be in the next version of the patch.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 70 lines · Waiman Long
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Waiman Long
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?Quoted 6 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.I have been changing the patching code to patch the unlock call sites and it seems to be working now. However, when I manually inserted a kernel module using insmod and run the code in the newly inserted module, I got memory access violation as follows:
BUG: unable to handle kernel NULL pointer dereference at (null) IP: [< (null)>] (null) PGD 18d62f3067 PUD 18d476f067 PMD 0 Oops: 0010 [#1] SMP Modules linked in: locktest(OE) ebtable_nat ebtables xt_CHECKSUM
iptable_manglebridge autofs4 8021q garp stp llcipt_REJECTnf_conntrack_ipv4nf_defrag_ipv4iptable_filterip_tablesip6t_REJECTnf_conntrack_ipv6nf_defrag_ipv6xt_statenf_conntrackip6table_filterip6_tablesipv6vhost_netmacvtap macvlan vhost tun uinput ppdevparport_pcparport sg microcode pcspkrvirtio_balloonsnd_hda_codec_genericvirtio_consolesnd_hda_intelsnd_hda_controllersnd_hda_codecsnd_hwdepsnd_seqsnd_seq_devicesnd_pcmsnd_timersnd soundcorevirtio_neti2c_piix4i2c_coreext4(E) jbd2(E) mbcache(E) floppy(E)virtio_blk(E)sr_mod(E)cdrom(E)virtio_pci(E)virtio_ring(E)virtio(E)pata_acpi(E)ata_generic(E)ata_piix(E)dm_mirror(E)dm_region_hash(E)dm_log(E)dm_mod(E)[last
unloaded:speedstep_lib]CPU: 1 PID: 3907 Comm: run-locktest Tainted: G W OE 3.17.0-pvqlock #3 Hardware name: Red Hat KVM, BIOS Bochs 01/01/2011 task: ffff8818cc5baf90 ti: ffff8818b7094000 task.ti: ffff8818b7094000 RIP: 0010:[<0000000000000000>] [< (null)>] (null) RSP: 0018:ffff8818b7097db0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 00000000004c4b40 RCX: 0000000000000000 RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffff8818d3f052c0 RBP: ffff8818b7097dd8 R08: 0000000080522014 R09: 0000000000000000 R10: 0000000000001000 R11: 0000000000000001 R12: 0000000000000001 R13: 0000000000000000 R14: 0000000000000001 R15: ffff8818b7097ea0 FS: 00007fb828ece700(0000) GS:ffff88193ec20000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 0000000000000000 CR3: 00000018cc7e9000 CR4: 00000000000006e0 Stack: ffffffffa06ff395 ffff8818d465e000 ffffffff8164bec0 0000000000000001 0000000000000050 ffff8818b7097e18 ffffffffa06ff785 ffff8818b7097e38 0000000000000246 0000000054755e3a 0000000039f8ba72 ffff8818c174f000 Call Trace: [<ffffffffa06ff395>] ? test_spinlock+0x65/0x90 [locktest] [<ffffffffa06ff785>] etime_show+0xd5/0x120 [locktest] [<ffffffff812a2dc6>] kobj_attr_show+0x16/0x20 [<ffffffff8121a7fa>] sysfs_kf_seq_show+0xca/0x1b0 [<ffffffff81218a13>] kernfs_seq_show+0x23/0x30 [<ffffffff811c82db>] seq_read+0xbb/0x400 [<ffffffff812197e5>] kernfs_fop_read+0x35/0x40 [<ffffffff811a4223>] vfs_read+0xa3/0x110 [<ffffffff811a47e6>] SyS_read+0x56/0xd0 [<ffffffff810f3e16>] ? __audit_syscall_exit+0x216/0x2c0 [<ffffffff815b3ca9>] system_call_fastpath+0x16/0x1b Code: Bad RIP value. RSP <ffff8818b7097db0> CR2: 0000000000000000 ---[ end trace 69d0e259c9ec632f ]---
Quoted 2 lines
It seems like call site patching isn't properly done or the kernel module that I built was missing some critical information necessary for the proper
Did the readelf give you the paravirt note section?
Quoted 2 lines
linking. Anyway, I will include the unlock call patching code as a separate patch as it seems there may be problem under certain circumstance.
one way to troubleshoot those is to enable the paravirt patching code to actually print where it is patching the code. That way when you load the module you can confirm it has done its job.
Then you can verify that the address where the code is called:
ffffffffa06ff395
is indeed patched. You might as well also do a hexdump in the module loading to confim that the patching had been done correctly.
Quoted 5 lines
BTW, the kernel panic problem that your team reported had been fixed. The fix will be in the next version of the patch.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Konrad Rzeszutek WilkQuoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Waiman Long
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 6 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.
I have been changing the patching code to patch the unlock call sites and it seems to be working now. However, when I manually inserted a kernel module using insmod and run the code in the newly inserted module, I got memory access violation as follows:
BUG: unable to handle kernel NULL pointer dereference at (null) IP: [< (null)>] (null) PGD 18d62f3067 PUD 18d476f067 PMD 0 Oops: 0010 [#1] SMP Modules linked in: locktest(OE) ebtable_nat ebtables xt_CHECKSUM iptable_mangle bridge autofs4 8021q garp stp llc ipt_REJECT nf_conntrack_ipv4 nf_defrag_ipv4 iptable_filter ip_tables ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 xt_state nf_conntrack ip6table_filter ip6_tables ipv6 vhost_net macvtap macvlan vhost tun uinput ppdev parport_pc parport sg microcode pcspkr virtio_balloon snd_hda_codec_generic virtio_console snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore virtio_net i2c_piix4 i2c_core ext4(E) jbd2(E) mbcache(E) floppy(E) virtio_blk(E) sr_mod(E) cdrom(E) virtio_pci(E) virtio_ring(E) virtio(E) pata_acpi(E) ata_generic(E) ata_piix(E) dm_mirror(E) dm_region_hash(E) dm_log(E) dm_mod(E) [last unloaded: speedstep_lib] CPU: 1 PID: 3907 Comm: run-locktest Tainted: G W OE 3.17.0-pvqlock #3 Hardware name: Red Hat KVM, BIOS Bochs 01/01/2011 task: ffff8818cc5baf90 ti: ffff8818b7094000 task.ti: ffff8818b7094000 RIP: 0010:[<0000000000000000>] [< (null)>] (null) RSP: 0018:ffff8818b7097db0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 00000000004c4b40 RCX: 0000000000000000 RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffff8818d3f052c0 RBP: ffff8818b7097dd8 R08: 0000000080522014 R09: 0000000000000000 R10: 0000000000001000 R11: 0000000000000001 R12: 0000000000000001 R13: 0000000000000000 R14: 0000000000000001 R15: ffff8818b7097ea0 FS: 00007fb828ece700(0000) GS:ffff88193ec20000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 0000000000000000 CR3: 00000018cc7e9000 CR4: 00000000000006e0 Stack: ffffffffa06ff395 ffff8818d465e000 ffffffff8164bec0 0000000000000001 0000000000000050 ffff8818b7097e18 ffffffffa06ff785 ffff8818b7097e38 0000000000000246 0000000054755e3a 0000000039f8ba72 ffff8818c174f000 Call Trace: [<ffffffffa06ff395>] ? test_spinlock+0x65/0x90 [locktest] [<ffffffffa06ff785>] etime_show+0xd5/0x120 [locktest] [<ffffffff812a2dc6>] kobj_attr_show+0x16/0x20 [<ffffffff8121a7fa>] sysfs_kf_seq_show+0xca/0x1b0 [<ffffffff81218a13>] kernfs_seq_show+0x23/0x30 [<ffffffff811c82db>] seq_read+0xbb/0x400 [<ffffffff812197e5>] kernfs_fop_read+0x35/0x40 [<ffffffff811a4223>] vfs_read+0xa3/0x110 [<ffffffff811a47e6>] SyS_read+0x56/0xd0 [<ffffffff810f3e16>] ? __audit_syscall_exit+0x216/0x2c0 [<ffffffff815b3ca9>] system_call_fastpath+0x16/0x1b Code: Bad RIP value. RSP <ffff8818b7097db0> CR2: 0000000000000000 ---[ end trace 69d0e259c9ec632f ]---
It seems like call site patching isn't properly done or the kernel module that I built was missing some critical information necessary for the proper linking. Anyway, I will include the unlock call patching code as a separate patch as it seems there may be problem under certain circumstance.
BTW, the kernel panic problem that your team reported had been fixed. The fix will be in the next version of the patch.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Konrad Rzeszutek WilkQuoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Waiman Long
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 6 lines
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.
I have been changing the patching code to patch the unlock call sites and it seems to be working now. However, when I manually inserted a kernel module using insmod and run the code in the newly inserted module, I got memory access violation as follows:
BUG: unable to handle kernel NULL pointer dereference at (null) IP: [< (null)>] (null) PGD 18d62f3067 PUD 18d476f067 PMD 0 Oops: 0010 [#1] SMP Modules linked in: locktest(OE) ebtable_nat ebtables xt_CHECKSUM iptable_mangle bridge autofs4 8021q garp stp llc ipt_REJECT nf_conntrack_ipv4 nf_defrag_ipv4 iptable_filter ip_tables ip6t_REJECT nf_conntrack_ipv6 nf_defrag_ipv6 xt_state nf_conntrack ip6table_filter ip6_tables ipv6 vhost_net macvtap macvlan vhost tun uinput ppdev parport_pc parport sg microcode pcspkr virtio_balloon snd_hda_codec_generic virtio_console snd_hda_intel snd_hda_controller snd_hda_codec snd_hwdep snd_seq snd_seq_device snd_pcm snd_timer snd soundcore virtio_net i2c_piix4 i2c_core ext4(E) jbd2(E) mbcache(E) floppy(E) virtio_blk(E) sr_mod(E) cdrom(E) virtio_pci(E) virtio_ring(E) virtio(E) pata_acpi(E) ata_generic(E) ata_piix(E) dm_mirror(E) dm_region_hash(E) dm_log(E) dm_mod(E) [last unloaded: speedstep_lib] CPU: 1 PID: 3907 Comm: run-locktest Tainted: G W OE 3.17.0-pvqlock #3 Hardware name: Red Hat KVM, BIOS Bochs 01/01/2011 task: ffff8818cc5baf90 ti: ffff8818b7094000 task.ti: ffff8818b7094000 RIP: 0010:[<0000000000000000>] [< (null)>] (null) RSP: 0018:ffff8818b7097db0 EFLAGS: 00010246 RAX: 0000000000000000 RBX: 00000000004c4b40 RCX: 0000000000000000 RDX: 0000000000000001 RSI: 0000000000000000 RDI: ffff8818d3f052c0 RBP: ffff8818b7097dd8 R08: 0000000080522014 R09: 0000000000000000 R10: 0000000000001000 R11: 0000000000000001 R12: 0000000000000001 R13: 0000000000000000 R14: 0000000000000001 R15: ffff8818b7097ea0 FS: 00007fb828ece700(0000) GS:ffff88193ec20000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 000000008005003b CR2: 0000000000000000 CR3: 00000018cc7e9000 CR4: 00000000000006e0 Stack: ffffffffa06ff395 ffff8818d465e000 ffffffff8164bec0 0000000000000001 0000000000000050 ffff8818b7097e18 ffffffffa06ff785 ffff8818b7097e38 0000000000000246 0000000054755e3a 0000000039f8ba72 ffff8818c174f000 Call Trace: [<ffffffffa06ff395>] ? test_spinlock+0x65/0x90 [locktest] [<ffffffffa06ff785>] etime_show+0xd5/0x120 [locktest] [<ffffffff812a2dc6>] kobj_attr_show+0x16/0x20 [<ffffffff8121a7fa>] sysfs_kf_seq_show+0xca/0x1b0 [<ffffffff81218a13>] kernfs_seq_show+0x23/0x30 [<ffffffff811c82db>] seq_read+0xbb/0x400 [<ffffffff812197e5>] kernfs_fop_read+0x35/0x40 [<ffffffff811a4223>] vfs_read+0xa3/0x110 [<ffffffff811a47e6>] SyS_read+0x56/0xd0 [<ffffffff810f3e16>] ? __audit_syscall_exit+0x216/0x2c0 [<ffffffff815b3ca9>] system_call_fastpath+0x16/0x1b Code: Bad RIP value. RSP <ffff8818b7097db0> CR2: 0000000000000000 ---[ end trace 69d0e259c9ec632f ]---
It seems like call site patching isn't properly done or the kernel module that I built was missing some critical information necessary for the proper linking. Anyway, I will include the unlock call patching code as a separate patch as it seems there may be problem under certain circumstance.
BTW, the kernel panic problem that your team reported had been fixed. The fix will be in the next version of the patch.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".
Quoted 8 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
It has too. When the modules are loaded the .paravirt symbols are exposed and the module loader patches that.
And during bootup time (before modules are loaded) it also patches everything
- when it only runs on one CPU.
Quoted 3 lines
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
No need. Inline should (And is) working just fine.
Quoted 3 lines
Regards,
Longman
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".
Quoted 8 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
modules should be fine, see arch/x86/kernel/module.c:module_finalize() -> apply_paravirt().
Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 55 lines · Peter Zijlstra
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 6 lines
modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when paravirt_spinlock is enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 58 lines · Waiman Long
Quoted 57 lines · Peter Zijlstra
Quoted 56 lines · Waiman Long
Quoted 44 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +4
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) {return paravirt_patch_insns(insnbuf, len,
arch/x86/kernel/paravirt_patch_64.c +3
-0@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site:ret = paravirt_patch_insns(ibuf, len, start, end);Quoted 7 lines
That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 8 lines
Quoted 6 lines
modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when
paravirt_spinlockis enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.
I am sorry that the unlock call sites patching code doesn't work in a virtual guest. Your pvqspinlock patch did an unconditional patching even in a virtual guest. I added check for the paravirt_spinlocks_enabled, but it turned out that some spin_unlock() seemed to be called before paravirt_spinlocks_enabled is set. As a result, some call sites were still patched resulting in missed wake up's and system hang.
At this point, I am going to leave out that change from my patch set until we can figure out a better way of doing that.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
Quoted 66 lines · Waiman Long
Quoted 65 lines · Waiman Long
Quoted 63 lines · Peter Zijlstra
Quoted 49 lines · Waiman Long
Quoted 48 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +4
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) {return paravirt_patch_insns(insnbuf, len,
arch/x86/kernel/paravirt_patch_64.c +3
-0@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site:ret = paravirt_patch_insns(ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Quoted 6 lines
Quoted 1 line
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when
paravirt_spinlockis enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.I am sorry that the unlock call sites patching code doesn't work in a virtual guest. Your pvqspinlock patch did an unconditional patching even in a virtual guest. I added check for the
paravirt_spinlocks_enabled, but it turned out that somespin_unlock()seemed to be called beforeparavirt_spinlocks_enabledis set. As a result, some call sites were still patched resulting in missed wake up's and system hang.At this point, I am going to leave out that change from my patch set until we can figure out a better way of doing that.
Below was a partial kernel log with the unlock call site patch code in a KVM guest:
[ 0.438006] native_patch: patch out pv_queue_unlock! [ 0.438565] native_patch: patch out pv_queue_unlock! [ 0.439006] native_patch: patch out pv_queue_unlock! [ 0.439638] native_patch: patch out pv_queue_unlock! [ 0.440052] native_patch: patch out pv_queue_unlock! [ 0.441006] native_patch: patch out pv_queue_unlock! [ 0.441566] native_patch: patch out pv_queue_unlock! [ 0.442035] ftrace: allocating 24168 entries in 95 pages [ 0.451208] Switched APIC routing to physical flat. [ 0.453202] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1 [ 0.454002] smpboot: CPU0: Intel QEMU Virtual CPU version 1.5.3 (fam: 06, model: 06, stepping: 03) [ 0.456000] Performance Events: Broken PMU hardware detected, using software events only. [ 0.456003] Failed to access perfctr msr (MSR c1 is 0) [ 0.457151] KVM setup paravirtual spinlock [ 0.460039] NMI watchdog: disabled (cpu0): hardware events not enabled
It could be seen that some unlock call sites were patched before the KVM setup code set the paravirt_spinlocks_enabled flag.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 66 lines · Waiman Long
Quoted 65 lines · Waiman Long
Quoted 63 lines · Peter Zijlstra
Quoted 49 lines · Waiman Long
Quoted 48 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +4
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) {return paravirt_patch_insns(insnbuf, len,
arch/x86/kernel/paravirt_patch_64.c +3
-0@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site:ret = paravirt_patch_insns(ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Quoted 6 lines
Quoted 1 line
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when
paravirt_spinlockis enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.I am sorry that the unlock call sites patching code doesn't work in a virtual guest. Your pvqspinlock patch did an unconditional patching even in a virtual guest. I added check for the
paravirt_spinlocks_enabled, but it turned out that somespin_unlock()seemed to be called beforeparavirt_spinlocks_enabledis set. As a result, some call sites were still patched resulting in missed wake up's and system hang.At this point, I am going to leave out that change from my patch set until we can figure out a better way of doing that.
Below was a partial kernel log with the unlock call site patch code in a KVM guest:
[ 0.438006] native_patch: patch out pv_queue_unlock! [ 0.438565] native_patch: patch out pv_queue_unlock! [ 0.439006] native_patch: patch out pv_queue_unlock! [ 0.439638] native_patch: patch out pv_queue_unlock! [ 0.440052] native_patch: patch out pv_queue_unlock! [ 0.441006] native_patch: patch out pv_queue_unlock! [ 0.441566] native_patch: patch out pv_queue_unlock! [ 0.442035] ftrace: allocating 24168 entries in 95 pages [ 0.451208] Switched APIC routing to physical flat. [ 0.453202] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1 [ 0.454002] smpboot: CPU0: Intel QEMU Virtual CPU version 1.5.3 (fam: 06, model: 06, stepping: 03) [ 0.456000] Performance Events: Broken PMU hardware detected, using software events only. [ 0.456003] Failed to access perfctr msr (MSR c1 is 0) [ 0.457151] KVM setup paravirtual spinlock [ 0.460039] NMI watchdog: disabled (cpu0): hardware events not enabled
It could be seen that some unlock call sites were patched before the KVM setup code set the paravirt_spinlocks_enabled flag.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 66 lines · Waiman Long
Quoted 65 lines · Waiman Long
Quoted 63 lines · Peter Zijlstra
Quoted 49 lines · Waiman Long
Quoted 48 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +4
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) {return paravirt_patch_insns(insnbuf, len,
arch/x86/kernel/paravirt_patch_64.c +3
-0@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site:ret = paravirt_patch_insns(ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Quoted 6 lines
Quoted 1 line
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when
paravirt_spinlockis enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.I am sorry that the unlock call sites patching code doesn't work in a virtual guest. Your pvqspinlock patch did an unconditional patching even in a virtual guest. I added check for the
paravirt_spinlocks_enabled, but it turned out that somespin_unlock()seemed to be called beforeparavirt_spinlocks_enabledis set. As a result, some call sites were still patched resulting in missed wake up's and system hang.At this point, I am going to leave out that change from my patch set until we can figure out a better way of doing that.
Below was a partial kernel log with the unlock call site patch code in a KVM guest:
[ 0.438006] native_patch: patch out pv_queue_unlock! [ 0.438565] native_patch: patch out pv_queue_unlock! [ 0.439006] native_patch: patch out pv_queue_unlock! [ 0.439638] native_patch: patch out pv_queue_unlock! [ 0.440052] native_patch: patch out pv_queue_unlock! [ 0.441006] native_patch: patch out pv_queue_unlock! [ 0.441566] native_patch: patch out pv_queue_unlock! [ 0.442035] ftrace: allocating 24168 entries in 95 pages [ 0.451208] Switched APIC routing to physical flat. [ 0.453202] ..TIMER: vector=0x30 apic1=0 pin1=2 apic2=-1 pin2=-1 [ 0.454002] smpboot: CPU0: Intel QEMU Virtual CPU version 1.5.3 (fam: 06, model: 06, stepping: 03) [ 0.456000] Performance Events: Broken PMU hardware detected, using software events only. [ 0.456003] Failed to access perfctr msr (MSR c1 is 0) [ 0.457151] KVM setup paravirtual spinlock [ 0.460039] NMI watchdog: disabled (cpu0): hardware events not enabled
It could be seen that some unlock call sites were patched before the KVM setup code set the paravirt_spinlocks_enabled flag.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 58 lines · Waiman Long
Quoted 57 lines · Peter Zijlstra
Quoted 56 lines · Waiman Long
Quoted 44 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +4
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) {return paravirt_patch_insns(insnbuf, len,
arch/x86/kernel/paravirt_patch_64.c +3
-0@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site:ret = paravirt_patch_insns(ibuf, len, start, end);Quoted 7 lines
That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 8 lines
Quoted 6 lines
modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when
paravirt_spinlockis enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.
I am sorry that the unlock call sites patching code doesn't work in a virtual guest. Your pvqspinlock patch did an unconditional patching even in a virtual guest. I added check for the paravirt_spinlocks_enabled, but it turned out that some spin_unlock() seemed to be called before paravirt_spinlocks_enabled is set. As a result, some call sites were still patched resulting in missed wake up's and system hang.
At this point, I am going to leave out that change from my patch set until we can figure out a better way of doing that.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 55 lines · Peter Zijlstra
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 6 lines
modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when paravirt_spinlock is enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.
-Longman
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 55 lines · Peter Zijlstra
Quoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Quoted 7 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
Quoted 6 lines
modules should be fine, see arch/x86/kernel/module.c:module_finalize() ->
apply_paravirt().Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Thanks for letting me know about that. I have this concern because your patch didn't change the current configuration of disabling unlock inlining when paravirt_spinlock is enabled. With that, I think it is worthwhile to reduce the performance delta between the PV and non-PV kernel on bare metal.
-Longman
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".
Quoted 8 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
modules should be fine, see arch/x86/kernel/module.c:module_finalize() -> apply_paravirt().
Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 47 lines · Waiman Long
Quoted 46 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".
Quoted 8 lines
Quoted 3 lines
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that
spin_unlock()can be called in many places, including loadable kernel modules. Can theparavirt_patch_ident_32()function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
modules should be fine, see arch/x86/kernel/module.c:module_finalize() -> apply_paravirt().
Also note that the 'default' text is an indirect call into the paravirt ops table which routes to the 'right' function, so even if the text patching would be 'late' calls would 'work' as expected, just slower.
Re: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Peter ZijlstraQuoted 42 lines · Peter Zijlstra
Quoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]In particular this hunk:
Index:
linux-2.6/arch/x86/kernel/paravirt_patch_64.c
===================================================================arch/x86/kernel/paravirt_patch_64.c +7
-0@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS )&& defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);
Quoted 7 lines
That makes sure to overwrite the callee-saved call to the
pv_lock_ops::queue_unlockwith the immediate asm "movb $0, (%rdi)".Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
My concern is that spin_unlock() can be called in many places, including loadable kernel modules. Can the paravirt_patch_ident_32() function able to patch all of them in reasonable time? How about a kernel module loaded later at run time?
So I think we may still need to disable unlock function inlining even if we used your way kernel site patching.
Regards,
Longman
Peter Zijlstra
maintainerRe: [PATCH v12 09/11] pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongQuoted 3 lines · Waiman Long
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
But you don't have to. My patches allowed for the inline to remain, again reducing the overhead of enabling PV spinlocks while running on a real machine.
Look at:
http://lkml.kernel.org/r/[email protected]
In particular this hunk:
arch/x86/kernel/paravirt_patch_64.c +7 -0
@@ -22,6 +22,10 @@ DEF_NATIVE(pv_cpu_ops, swapgs, "swapgs") DEF_NATIVE(, mov32, "mov %edi, %eax"); DEF_NATIVE(, mov64, "mov %rdi, %rax"); +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) && defined(CONFIG_QUEUE_SPINLOCK ) +DEF_NATIVE (pv_lock_ops, queue_unlock,"movb $0, (%rdi)" ); +#endif +unsigned paravirt_patch_ident_32 (void *insnbuf,unsigned len) { return paravirt_patch_insns(insnbuf, len,
@@ -61,6 +65,9 @@ unsigned native_patch(u8 type, u16 clobbPATCH_SITE (pv_cpu_ops, clts);PATCH_SITE (pv_mmu_ops, flush_tlb_single);PATCH_SITE (pv_cpu_ops, wbinvd); +#if defined(CONFIG_PARAVIRT_SPINLOCKS ) && defined(CONFIG_QUEUE_SPINLOCK ) +PATCH_SITE (pv_lock_ops, queue_unlock); +#endif patch_site: ret =paravirt_patch_insns (ibuf, len, start, end);
That makes sure to overwrite the callee-saved call to the pv_lock_ops::queue_unlock with the immediate asm "movb $0, (%rdi)".
Therefore you can retain the inlined unlock with hardly (there might be some NOP padding) any overhead at all. On PV it reverts to a callee saved function call.
PATCH v12 9/11 pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongThis patch adds para-virtualization support to the queue spinlock code base with minimal impact to the native case. There are some minor code changes in the generic qspinlock.c file which should be usable in other architectures. The other code changes are specific to x86 processors and so are all put under the arch/x86 directory.
On the lock side, there are a couple of jump labels and 2 paravirt callee saved calls that defaults to NOPs and some registered move instructions. So the performance impact should be minimal.
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
The actual paravirt code comes in 5 parts;
- init_node; this initializes the extra data members required for PV state. PV state data is kept 1 cacheline ahead of the regular data.
- link_and_wait_node; this replaces the regular MCS queuing code. CPU halting can happen if the wait is too long.
- wait_head; this waits until the lock is avialable and the CPU will be halted if the wait is too long.
- wait_check; this is called after acquiring the lock to see if the next queue head CPU is halted. If this is the case, the lock bit is changed to indicate the queue head will have to be kicked on unlock.
- queue_unlock; this routine has a jump label to check if paravirt is enabled. If yes, it has to do an atomic cmpxchg to clear the lock bit or call the slowpath function to kick the queue head cpu.
Tracking the head is done in two parts, firstly the pv_wait_head will store its cpu number in whichever node is pointed to by the tail part of the lock word. Secondly, pv_link_and_wait_node() will propagate the existing head from the old to the new tail node.
--- arch/x86/include/asm/paravirt.h | 20 ++ arch/x86/include/asm/paravirt_types.h | 20 ++ arch/x86/include/asm/pvqspinlock.h | 403 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/qspinlock.h | 44 ++++- arch/x86/kernel/paravirt-spinlocks.c | 6 + kernel/locking/qspinlock.c | 72 ++++++- 6 files changed, 558 insertions(+), 7 deletions(-) create mode 100644 arch/x86/include/asm/pvqspinlock.h
arch/x86/include/asm/paravirt.h +20 -0
@@ -712,6 +712,25 @@ static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx, #if defined(CONFIG_SMP) && defined(CONFIG_PARAVIRT_SPINLOCKS) +#ifdef CONFIG_QUEUE_SPINLOCK + +static __always_inline void pv_kick_cpu(int cpu) +{ + PVOP_VCALLEE1(pv_lock_ops.kick_cpu, cpu); +} + +static __always_inline void +pv_lockwait(u8 *lockbyte) +{ + PVOP_VCALLEE1(pv_lock_ops.lockwait, lockbyte); +} + +static __always_inline void pv_lockstat(enum pv_lock_stats type) +{ + PVOP_VCALLEE1(pv_lock_ops.lockstat, type); +} + +#else static __always_inline void __ticket_lock_spinning(struct arch_spinlock *lock, __ticket_t ticket) {
@@ -723,6 +742,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, {PVOP_VCALL2 (pv_lock_ops.unlock_kick, lock, ticket); } +#endif #endif
arch/x86/include/asm/paravirt_types.h +20 -0
@@ -326,6 +326,9 @@ struct pv_mmu_ops { phys_addr_t phys, pgprot_t flags); }; +struct mcs_spinlock ; +struct qspinlock ; +struct arch_spinlock ;#ifdef CONFIG_SMP #include <asm/spinlock_types.h>
@@ -333,9 +336,26 @@ struct arch_spinlock;typedef u16 __ticket_t ;#endif +#ifdef CONFIG_QUEUE_SPINLOCK +enum pv_lock_stats { +PV_HALT_QHEAD ,/* Queue head halting */ +PV_HALT_QNODE ,/* Other queue node halting */ +PV_HALT_ABORT ,/* Halting aborted */ +PV_WAKE_KICKED ,/* Wakeup by kicking */ +PV_WAKE_SPURIOUS ,/* Spurious wakeup */ +PV_KICK_NOHALT /* Kick but CPU not halted */ +}; +#endif +struct pv_lock_ops { +#ifdef CONFIG_QUEUE_SPINLOCK +struct paravirt_callee_save kick_cpu; +struct paravirt_callee_save lockstat; +struct paravirt_callee_save lockwait; +#else struct paravirt_callee_save lock_spinning;void (*unlock_kick)(struct arch_spinlock *lock,__ticket_t ticket); +#endif };/* This contains all the paravirt structures: we get a convenient
arch/x86/include/asm/pvqspinlock.h new file +403 -0
403 lines not on this page · raw message
arch/x86/include/asm/qspinlock.h +41 -3
62 lines not on this page · raw message
arch/x86/kernel/paravirt-spinlocks.c +6 -0
@@ -10,9 +10,15 @@ struct pv_lock_ops pv_lock_ops = {#ifdef CONFIG_SMP +#ifdef CONFIG_QUEUE_SPINLOCK + .kick_cpu = __PV_IS_CALLEE_SAVE(paravirt_nop), + .lockstat = __PV_IS_CALLEE_SAVE(paravirt_nop), + .lockwait = __PV_IS_CALLEE_SAVE(paravirt_nop), +#else .lock_spinning = __PV_IS_CALLEE_SAVE(paravirt_nop), .unlock_kick = paravirt_nop,#endif +#endif };EXPORT_SYMBOL (pv_lock_ops);
kernel/locking/qspinlock.c +68 -4
121 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 9/11 pvqspinlock, x86: Add para-virtualization support
In reply to Waiman LongThis patch adds para-virtualization support to the queue spinlock code base with minimal impact to the native case. There are some minor code changes in the generic qspinlock.c file which should be usable in other architectures. The other code changes are specific to x86 processors and so are all put under the arch/x86 directory.
On the lock side, there are a couple of jump labels and 2 paravirt callee saved calls that defaults to NOPs and some registered move instructions. So the performance impact should be minimal.
Since enabling paravirt spinlock will disable unlock function inlining, a jump label can be added to the unlock function without adding patch sites all over the kernel.
The actual paravirt code comes in 5 parts;
- init_node; this initializes the extra data members required for PV state. PV state data is kept 1 cacheline ahead of the regular data.
- link_and_wait_node; this replaces the regular MCS queuing code. CPU halting can happen if the wait is too long.
- wait_head; this waits until the lock is avialable and the CPU will be halted if the wait is too long.
- wait_check; this is called after acquiring the lock to see if the next queue head CPU is halted. If this is the case, the lock bit is changed to indicate the queue head will have to be kicked on unlock.
- queue_unlock; this routine has a jump label to check if paravirt is enabled. If yes, it has to do an atomic cmpxchg to clear the lock bit or call the slowpath function to kick the queue head cpu.
Tracking the head is done in two parts, firstly the pv_wait_head will store its cpu number in whichever node is pointed to by the tail part of the lock word. Secondly, pv_link_and_wait_node() will propagate the existing head from the old to the new tail node.
--- arch/x86/include/asm/paravirt.h | 20 ++ arch/x86/include/asm/paravirt_types.h | 20 ++ arch/x86/include/asm/pvqspinlock.h | 403 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/qspinlock.h | 44 ++++- arch/x86/kernel/paravirt-spinlocks.c | 6 + kernel/locking/qspinlock.c | 72 ++++++- 6 files changed, 558 insertions(+), 7 deletions(-) create mode 100644 arch/x86/include/asm/pvqspinlock.h
arch/x86/include/asm/paravirt.h +20 -0
@@ -712,6 +712,25 @@ static inline void __set_fixmap(unsigned /* enum fixed_addresses */ idx, #if defined(CONFIG_SMP) && defined(CONFIG_PARAVIRT_SPINLOCKS) +#ifdef CONFIG_QUEUE_SPINLOCK + +static __always_inline void pv_kick_cpu(int cpu) +{ + PVOP_VCALLEE1(pv_lock_ops.kick_cpu, cpu); +} + +static __always_inline void +pv_lockwait(u8 *lockbyte) +{ + PVOP_VCALLEE1(pv_lock_ops.lockwait, lockbyte); +} + +static __always_inline void pv_lockstat(enum pv_lock_stats type) +{ + PVOP_VCALLEE1(pv_lock_ops.lockstat, type); +} + +#else static __always_inline void __ticket_lock_spinning(struct arch_spinlock *lock, __ticket_t ticket) {
@@ -723,6 +742,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, {PVOP_VCALL2 (pv_lock_ops.unlock_kick, lock, ticket); } +#endif #endif
arch/x86/include/asm/paravirt_types.h +20 -0
@@ -326,6 +326,9 @@ struct pv_mmu_ops { phys_addr_t phys, pgprot_t flags); }; +struct mcs_spinlock ; +struct qspinlock ; +struct arch_spinlock ;#ifdef CONFIG_SMP #include <asm/spinlock_types.h>
@@ -333,9 +336,26 @@ struct arch_spinlock;typedef u16 __ticket_t ;#endif +#ifdef CONFIG_QUEUE_SPINLOCK +enum pv_lock_stats { +PV_HALT_QHEAD ,/* Queue head halting */ +PV_HALT_QNODE ,/* Other queue node halting */ +PV_HALT_ABORT ,/* Halting aborted */ +PV_WAKE_KICKED ,/* Wakeup by kicking */ +PV_WAKE_SPURIOUS ,/* Spurious wakeup */ +PV_KICK_NOHALT /* Kick but CPU not halted */ +}; +#endif +struct pv_lock_ops { +#ifdef CONFIG_QUEUE_SPINLOCK +struct paravirt_callee_save kick_cpu; +struct paravirt_callee_save lockstat; +struct paravirt_callee_save lockwait; +#else struct paravirt_callee_save lock_spinning;void (*unlock_kick)(struct arch_spinlock *lock,__ticket_t ticket); +#endif };/* This contains all the paravirt structures: we get a convenient
arch/x86/include/asm/pvqspinlock.h new file +403 -0
403 lines not on this page · raw message
arch/x86/include/asm/qspinlock.h +41 -3
62 lines not on this page · raw message
arch/x86/kernel/paravirt-spinlocks.c +6 -0
@@ -10,9 +10,15 @@ struct pv_lock_ops pv_lock_ops = {#ifdef CONFIG_SMP +#ifdef CONFIG_QUEUE_SPINLOCK + .kick_cpu = __PV_IS_CALLEE_SAVE(paravirt_nop), + .lockstat = __PV_IS_CALLEE_SAVE(paravirt_nop), + .lockwait = __PV_IS_CALLEE_SAVE(paravirt_nop), +#else .lock_spinning = __PV_IS_CALLEE_SAVE(paravirt_nop), .unlock_kick = paravirt_nop,#endif +#endif };EXPORT_SYMBOL (pv_lock_ops);
kernel/locking/qspinlock.c +68 -4
121 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v12 10/11 pvqspinlock, x86: Enable PV qspinlock for KVM
In reply to Waiman LongThis patch adds the necessary KVM specific code to allow KVM to support the CPU halting and kicking operations needed by the queue spinlock PV code.
Two KVM guests of 20 CPU cores (2 nodes) were created for performance testing in one of the following three configurations:
1) Only 1 VM is active
2) Both VMs are active and they share the same 20 physical CPUs
(200% overcommit)
The tests run included the disk workload of the AIM7 benchmark on both ext4 and xfs RAM disks at 3000 users on a 3.17 based kernel. The "ebizzy -m" test and futextest was was also run and its performance data were recorded. With two VMs running, the "idle=poll" kernel option was added to simulate a busy guest. If PV qspinlock is not enabled, unfairlock will be used automically in a guest.
AIM7 XFS Disk Test (no overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 2542373 7.08 98.95 5.44 PV qspinlock 2549575 7.06 98.63 5.40
unfairlock 2616279 6.91 97.05 5.42
AIM7 XFS Disk Test (200% overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 644468 27.93 415.22 6.33 PV qspinlock 645624 27.88 419.84 0.39
unfairlock 695518 25.88 377.40 4.09
AIM7 EXT4 Disk Test (no overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 1995565 9.02 103.67 5.76 PV qspinlock 2011173 8.95 102.15 5.40
unfairlock 2066590 8.71 98.13 5.46
AIM7 EXT4 Disk Test (200% overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 478341 37.63 495.81 30.78 PV qspinlock 474058 37.97 475.74 30.95
unfairlock 560224 32.13 398.43 26.27
For the AIM7 disk workload, both PV ticketlock and qspinlock have about the same performance. The unfairlock performs slightly better than the PV lock.
EBIZZY-m Test (no overcommit)
kernel Rec/s Real Time Sys Time Usr Time ----- ----- --------- -------- -------- PV ticketlock 3255 10.00 60.65 3.62 PV qspinlock 3318 10.00 54.27 3.60
unfairlock 2833 10.00 26.66 3.09
EBIZZY-m Test (200% overcommit)
kernel Rec/s Real Time Sys Time Usr Time ----- ----- --------- -------- -------- PV ticketlock 841 10.00 71.03 2.37 PV qspinlock 834 10.00 68.27 2.39
unfairlock 865 10.00 27.08 1.51
futextest (no overcommit)
kernel kops/s ----- ------ PV ticketlock 11523 PV qspinlock 12328
unfairlock 9478
futextest (200% overcommit)
kernel kops/s
----- ------
PV ticketlock 7276
PV qspinlock 7095
unfairlock 5614
The ebizzy and futextest have much higher spinlock contention than the AIM7 disk workload. In this case, the unfairlock performs worse than both the PV ticketlock and qspinlock. The performance of the 2 PV locks are comparable.
--- arch/x86/kernel/kvm.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++- kernel/Kconfig.locks | 2 +- 2 files changed, 138 insertions(+), 2 deletions(-)
arch/x86/kernel/kvm.c +137 -1
@@ -560,7 +560,7 @@ arch_initcall(activate_jump_labels);#ifdef CONFIG_PARAVIRT_SPINLOCKS /* Kick a cpu by its apicid. Used to wake up a halted vcpu */ -static void kvm_kick_cpu (int cpu) +void kvm_kick_cpu (int cpu) {int apicid;unsigned long flags =0 ;
@@ -568,7 +568,9 @@ static void kvm_kick_cpu(int cpu) apicid =per_cpu (x86_cpu_to_apicid, cpu);kvm_hypercall2 (KVM_HC_KICK_CPU , flags, apicid); } +PV_CALLEE_SAVE_REGS_THUNK (kvm_kick_cpu); +#ifndef CONFIG_QUEUE_SPINLOCK enum kvm_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -796,6 +798,132 @@ static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket) } } } +#else /* !CONFIG_QUEUE_SPINLOCK */ + +#ifdef CONFIG_KVM_DEBUG_FS +static struct dentry *d_spin_debug; +static struct dentry *d_kvm_debug; +static u32 kick_nohlt_stats;/* Kick but not halt count */ +static u32 halt_qhead_stats;/* Queue head halting count */ +static u32 halt_qnode_stats;/* Queue node halting count */ +static u32 halt_abort_stats;/* Halting abort count */ +static u32 wake_kick_stats;/* Wakeup by kicking count */ +static u32 wake_spur_stats;/* Spurious wakeup count */ +static u64 time_blocked;/* Total blocking time */ + +static int __init kvm_spinlock_debugfs (void ) +{ + d_kvm_debug =debugfs_create_dir ("kvm-guest" ,NULL ); +if (!d_kvm_debug) { +printk (KERN_WARNING +"Could not create 'kvm' debugfs directory\n" ); +return -ENOMEM ; + } + d_spin_debug =debugfs_create_dir ("spinlocks" , d_kvm_debug); + +debugfs_create_u32 ("kick_nohlt_stats" , +0644 , d_spin_debug, &kick_nohlt_stats); +debugfs_create_u32 ("halt_qhead_stats" , +0644 , d_spin_debug, &halt_qhead_stats); +debugfs_create_u32 ("halt_qnode_stats" , +0644 , d_spin_debug, &halt_qnode_stats); +debugfs_create_u32 ("halt_abort_stats" , +0644 , d_spin_debug, &halt_abort_stats); +debugfs_create_u32 ("wake_kick_stats" , +0644 , d_spin_debug, &wake_kick_stats); +debugfs_create_u32 ("wake_spur_stats" , +0644 , d_spin_debug, &wake_spur_stats); +debugfs_create_u64 ("time_blocked" , +0644 , d_spin_debug, &time_blocked); +return 0 ; +} + +static inline void kvm_halt_stats (enum pv_lock_stats type) +{ +if (type ==PV_HALT_QHEAD ) +add_smp (&halt_qhead_stats,1 ); +else if (type ==PV_HALT_QNODE ) +add_smp (&halt_qnode_stats,1 ); +else /* type == PV_HALT_ABORT */ +add_smp (&halt_abort_stats,1 ); +} + +void kvm_lock_stats (enum pv_lock_stats type) +{ +if (type ==PV_WAKE_KICKED ) +add_smp (&wake_kick_stats,1 ); +else if (type ==PV_WAKE_SPURIOUS ) +add_smp (&wake_spur_stats,1 ); +else /* type == PV_KICK_NOHALT */ +add_smp (&kick_nohlt_stats,1 ); +} +PV_CALLEE_SAVE_REGS_THUNK (kvm_lock_stats); + +static inline u64 spin_time_start (void ) +{ +return sched_clock (); +} + +static inline void spin_time_accum_blocked (u64 start) +{ +u64 delta; + + delta =sched_clock () - start; +add_smp (&time_blocked, delta); +} + +fs_initcall (kvm_spinlock_debugfs); + +#else /* CONFIG_KVM_DEBUG_FS */ +static inline void kvm_halt_stats (enum pv_lock_stats type) +{ +} + +static inline u64 spin_time_start (void ) +{ +return 0 ; +} + +static inline void spin_time_accum_blocked (u64 start) +{ +} +#endif /* CONFIG_KVM_DEBUG_FS */ + +/* +* Halt the current CPU & release it back to the host +*/ +void kvm_halt_cpu (u8 *lockbyte) +{ +unsigned long flags; +u64 start; + +if (in_nmi ()) +return ; + +/* +* Make sure an interrupt handler can't upset things in a +* partially setup state. +*/ +local_irq_save (flags); +/* +* Don't halt if the lock byte is defined and is free +*/ +if (lockbyte && !ACCESS_ONCE (*lockbyte)) { +kvm_halt_stats (PV_HALT_ABORT ); + goto out; + } + start =spin_time_start (); +kvm_halt_stats (lockbyte ?PV_HALT_QHEAD :PV_HALT_QNODE ); +if (arch_irqs_disabled_flags (flags)) +halt (); +else +safe_halt (); +spin_time_accum_blocked (start); +out: +local_irq_restore (flags); +} +PV_CALLEE_SAVE_REGS_THUNK (kvm_halt_cpu); +#endif /* !CONFIG_QUEUE_SPINLOCK */ /* * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
@@ -808,8 +936,16 @@ void __init kvm_spinlock_init(void)if (!kvm_para_has_feature (KVM_FEATURE_PV_UNHALT ))return ; +#ifdef CONFIG_QUEUE_SPINLOCK + pv_lock_ops.kick_cpu =PV_CALLEE_SAVE (kvm_kick_cpu); + pv_lock_ops.lockwait =PV_CALLEE_SAVE (kvm_halt_cpu); +#ifdef CONFIG_KVM_DEBUG_FS + pv_lock_ops.lockstat =PV_CALLEE_SAVE (kvm_lock_stats); +#endif +#else pv_lock_ops.lock_spinning =PV_CALLEE_SAVE (kvm_lock_spinning); pv_lock_ops.unlock_kick = kvm_unlock_kick; +#endif }static __init int kvm_spinlock_init_jump (void )
kernel/Kconfig.locks +1 -1
@@ -236,7 +236,7 @@ config ARCH_USE_QUEUE_SPINLOCK config QUEUE_SPINLOCK def_bool y if ARCH_USE_QUEUE_SPINLOCK - depends on SMP && !PARAVIRT_SPINLOCKS + depends on SMP && (!PARAVIRT_SPINLOCKS || !XEN) config ARCH_USE_QUEUE_RWLOCK bool
Signature · 1 line
1.7.1
PATCH v12 10/11 pvqspinlock, x86: Enable PV qspinlock for KVM
In reply to Waiman LongThis patch adds the necessary KVM specific code to allow KVM to support the CPU halting and kicking operations needed by the queue spinlock PV code.
Two KVM guests of 20 CPU cores (2 nodes) were created for performance testing in one of the following three configurations:
1) Only 1 VM is active
2) Both VMs are active and they share the same 20 physical CPUs
(200% overcommit)
The tests run included the disk workload of the AIM7 benchmark on both ext4 and xfs RAM disks at 3000 users on a 3.17 based kernel. The "ebizzy -m" test and futextest was was also run and its performance data were recorded. With two VMs running, the "idle=poll" kernel option was added to simulate a busy guest. If PV qspinlock is not enabled, unfairlock will be used automically in a guest.
AIM7 XFS Disk Test (no overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 2542373 7.08 98.95 5.44 PV qspinlock 2549575 7.06 98.63 5.40
unfairlock 2616279 6.91 97.05 5.42
AIM7 XFS Disk Test (200% overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 644468 27.93 415.22 6.33 PV qspinlock 645624 27.88 419.84 0.39
unfairlock 695518 25.88 377.40 4.09
AIM7 EXT4 Disk Test (no overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 1995565 9.02 103.67 5.76 PV qspinlock 2011173 8.95 102.15 5.40
unfairlock 2066590 8.71 98.13 5.46
AIM7 EXT4 Disk Test (200% overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 478341 37.63 495.81 30.78 PV qspinlock 474058 37.97 475.74 30.95
unfairlock 560224 32.13 398.43 26.27
For the AIM7 disk workload, both PV ticketlock and qspinlock have about the same performance. The unfairlock performs slightly better than the PV lock.
EBIZZY-m Test (no overcommit)
kernel Rec/s Real Time Sys Time Usr Time ----- ----- --------- -------- -------- PV ticketlock 3255 10.00 60.65 3.62 PV qspinlock 3318 10.00 54.27 3.60
unfairlock 2833 10.00 26.66 3.09
EBIZZY-m Test (200% overcommit)
kernel Rec/s Real Time Sys Time Usr Time ----- ----- --------- -------- -------- PV ticketlock 841 10.00 71.03 2.37 PV qspinlock 834 10.00 68.27 2.39
unfairlock 865 10.00 27.08 1.51
futextest (no overcommit)
kernel kops/s ----- ------ PV ticketlock 11523 PV qspinlock 12328
unfairlock 9478
futextest (200% overcommit)
kernel kops/s
----- ------
PV ticketlock 7276
PV qspinlock 7095
unfairlock 5614
The ebizzy and futextest have much higher spinlock contention than the AIM7 disk workload. In this case, the unfairlock performs worse than both the PV ticketlock and qspinlock. The performance of the 2 PV locks are comparable.
--- arch/x86/kernel/kvm.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++- kernel/Kconfig.locks | 2 +- 2 files changed, 138 insertions(+), 2 deletions(-)
arch/x86/kernel/kvm.c +137 -1
@@ -560,7 +560,7 @@ arch_initcall(activate_jump_labels);#ifdef CONFIG_PARAVIRT_SPINLOCKS /* Kick a cpu by its apicid. Used to wake up a halted vcpu */ -static void kvm_kick_cpu (int cpu) +void kvm_kick_cpu (int cpu) {int apicid;unsigned long flags =0 ;
@@ -568,7 +568,9 @@ static void kvm_kick_cpu(int cpu) apicid =per_cpu (x86_cpu_to_apicid, cpu);kvm_hypercall2 (KVM_HC_KICK_CPU , flags, apicid); } +PV_CALLEE_SAVE_REGS_THUNK (kvm_kick_cpu); +#ifndef CONFIG_QUEUE_SPINLOCK enum kvm_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -796,6 +798,132 @@ static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket) } } } +#else /* !CONFIG_QUEUE_SPINLOCK */ + +#ifdef CONFIG_KVM_DEBUG_FS +static struct dentry *d_spin_debug; +static struct dentry *d_kvm_debug; +static u32 kick_nohlt_stats;/* Kick but not halt count */ +static u32 halt_qhead_stats;/* Queue head halting count */ +static u32 halt_qnode_stats;/* Queue node halting count */ +static u32 halt_abort_stats;/* Halting abort count */ +static u32 wake_kick_stats;/* Wakeup by kicking count */ +static u32 wake_spur_stats;/* Spurious wakeup count */ +static u64 time_blocked;/* Total blocking time */ + +static int __init kvm_spinlock_debugfs (void ) +{ + d_kvm_debug =debugfs_create_dir ("kvm-guest" ,NULL ); +if (!d_kvm_debug) { +printk (KERN_WARNING +"Could not create 'kvm' debugfs directory\n" ); +return -ENOMEM ; + } + d_spin_debug =debugfs_create_dir ("spinlocks" , d_kvm_debug); + +debugfs_create_u32 ("kick_nohlt_stats" , +0644 , d_spin_debug, &kick_nohlt_stats); +debugfs_create_u32 ("halt_qhead_stats" , +0644 , d_spin_debug, &halt_qhead_stats); +debugfs_create_u32 ("halt_qnode_stats" , +0644 , d_spin_debug, &halt_qnode_stats); +debugfs_create_u32 ("halt_abort_stats" , +0644 , d_spin_debug, &halt_abort_stats); +debugfs_create_u32 ("wake_kick_stats" , +0644 , d_spin_debug, &wake_kick_stats); +debugfs_create_u32 ("wake_spur_stats" , +0644 , d_spin_debug, &wake_spur_stats); +debugfs_create_u64 ("time_blocked" , +0644 , d_spin_debug, &time_blocked); +return 0 ; +} + +static inline void kvm_halt_stats (enum pv_lock_stats type) +{ +if (type ==PV_HALT_QHEAD ) +add_smp (&halt_qhead_stats,1 ); +else if (type ==PV_HALT_QNODE ) +add_smp (&halt_qnode_stats,1 ); +else /* type == PV_HALT_ABORT */ +add_smp (&halt_abort_stats,1 ); +} + +void kvm_lock_stats (enum pv_lock_stats type) +{ +if (type ==PV_WAKE_KICKED ) +add_smp (&wake_kick_stats,1 ); +else if (type ==PV_WAKE_SPURIOUS ) +add_smp (&wake_spur_stats,1 ); +else /* type == PV_KICK_NOHALT */ +add_smp (&kick_nohlt_stats,1 ); +} +PV_CALLEE_SAVE_REGS_THUNK (kvm_lock_stats); + +static inline u64 spin_time_start (void ) +{ +return sched_clock (); +} + +static inline void spin_time_accum_blocked (u64 start) +{ +u64 delta; + + delta =sched_clock () - start; +add_smp (&time_blocked, delta); +} + +fs_initcall (kvm_spinlock_debugfs); + +#else /* CONFIG_KVM_DEBUG_FS */ +static inline void kvm_halt_stats (enum pv_lock_stats type) +{ +} + +static inline u64 spin_time_start (void ) +{ +return 0 ; +} + +static inline void spin_time_accum_blocked (u64 start) +{ +} +#endif /* CONFIG_KVM_DEBUG_FS */ + +/* +* Halt the current CPU & release it back to the host +*/ +void kvm_halt_cpu (u8 *lockbyte) +{ +unsigned long flags; +u64 start; + +if (in_nmi ()) +return ; + +/* +* Make sure an interrupt handler can't upset things in a +* partially setup state. +*/ +local_irq_save (flags); +/* +* Don't halt if the lock byte is defined and is free +*/ +if (lockbyte && !ACCESS_ONCE (*lockbyte)) { +kvm_halt_stats (PV_HALT_ABORT ); + goto out; + } + start =spin_time_start (); +kvm_halt_stats (lockbyte ?PV_HALT_QHEAD :PV_HALT_QNODE ); +if (arch_irqs_disabled_flags (flags)) +halt (); +else +safe_halt (); +spin_time_accum_blocked (start); +out: +local_irq_restore (flags); +} +PV_CALLEE_SAVE_REGS_THUNK (kvm_halt_cpu); +#endif /* !CONFIG_QUEUE_SPINLOCK */ /* * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
@@ -808,8 +936,16 @@ void __init kvm_spinlock_init(void)if (!kvm_para_has_feature (KVM_FEATURE_PV_UNHALT ))return ; +#ifdef CONFIG_QUEUE_SPINLOCK + pv_lock_ops.kick_cpu =PV_CALLEE_SAVE (kvm_kick_cpu); + pv_lock_ops.lockwait =PV_CALLEE_SAVE (kvm_halt_cpu); +#ifdef CONFIG_KVM_DEBUG_FS + pv_lock_ops.lockstat =PV_CALLEE_SAVE (kvm_lock_stats); +#endif +#else pv_lock_ops.lock_spinning =PV_CALLEE_SAVE (kvm_lock_spinning); pv_lock_ops.unlock_kick = kvm_unlock_kick; +#endif }static __init int kvm_spinlock_init_jump (void )
kernel/Kconfig.locks +1 -1
@@ -236,7 +236,7 @@ config ARCH_USE_QUEUE_SPINLOCK config QUEUE_SPINLOCK def_bool y if ARCH_USE_QUEUE_SPINLOCK - depends on SMP && !PARAVIRT_SPINLOCKS + depends on SMP && (!PARAVIRT_SPINLOCKS || !XEN) config ARCH_USE_QUEUE_RWLOCK bool
Signature · 1 line
1.7.1
PATCH v12 10/11 pvqspinlock, x86: Enable PV qspinlock for KVM
In reply to Waiman LongThis patch adds the necessary KVM specific code to allow KVM to support the CPU halting and kicking operations needed by the queue spinlock PV code.
Two KVM guests of 20 CPU cores (2 nodes) were created for performance testing in one of the following three configurations:
1) Only 1 VM is active
2) Both VMs are active and they share the same 20 physical CPUs
(200% overcommit)
The tests run included the disk workload of the AIM7 benchmark on both ext4 and xfs RAM disks at 3000 users on a 3.17 based kernel. The "ebizzy -m" test and futextest was was also run and its performance data were recorded. With two VMs running, the "idle=poll" kernel option was added to simulate a busy guest. If PV qspinlock is not enabled, unfairlock will be used automically in a guest.
AIM7 XFS Disk Test (no overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 2542373 7.08 98.95 5.44 PV qspinlock 2549575 7.06 98.63 5.40
unfairlock 2616279 6.91 97.05 5.42
AIM7 XFS Disk Test (200% overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 644468 27.93 415.22 6.33 PV qspinlock 645624 27.88 419.84 0.39
unfairlock 695518 25.88 377.40 4.09
AIM7 EXT4 Disk Test (no overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 1995565 9.02 103.67 5.76 PV qspinlock 2011173 8.95 102.15 5.40
unfairlock 2066590 8.71 98.13 5.46
AIM7 EXT4 Disk Test (200% overcommit)
kernel JPM Real Time Sys Time Usr Time ----- --- --------- -------- -------- PV ticketlock 478341 37.63 495.81 30.78 PV qspinlock 474058 37.97 475.74 30.95
unfairlock 560224 32.13 398.43 26.27
For the AIM7 disk workload, both PV ticketlock and qspinlock have about the same performance. The unfairlock performs slightly better than the PV lock.
EBIZZY-m Test (no overcommit)
kernel Rec/s Real Time Sys Time Usr Time ----- ----- --------- -------- -------- PV ticketlock 3255 10.00 60.65 3.62 PV qspinlock 3318 10.00 54.27 3.60
unfairlock 2833 10.00 26.66 3.09
EBIZZY-m Test (200% overcommit)
kernel Rec/s Real Time Sys Time Usr Time ----- ----- --------- -------- -------- PV ticketlock 841 10.00 71.03 2.37 PV qspinlock 834 10.00 68.27 2.39
unfairlock 865 10.00 27.08 1.51
futextest (no overcommit)
kernel kops/s ----- ------ PV ticketlock 11523 PV qspinlock 12328
unfairlock 9478
futextest (200% overcommit)
kernel kops/s
----- ------
PV ticketlock 7276
PV qspinlock 7095
unfairlock 5614
The ebizzy and futextest have much higher spinlock contention than the AIM7 disk workload. In this case, the unfairlock performs worse than both the PV ticketlock and qspinlock. The performance of the 2 PV locks are comparable.
--- arch/x86/kernel/kvm.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++- kernel/Kconfig.locks | 2 +- 2 files changed, 138 insertions(+), 2 deletions(-)
arch/x86/kernel/kvm.c +137 -1
@@ -560,7 +560,7 @@ arch_initcall(activate_jump_labels);#ifdef CONFIG_PARAVIRT_SPINLOCKS /* Kick a cpu by its apicid. Used to wake up a halted vcpu */ -static void kvm_kick_cpu (int cpu) +void kvm_kick_cpu (int cpu) {int apicid;unsigned long flags =0 ;
@@ -568,7 +568,9 @@ static void kvm_kick_cpu(int cpu) apicid =per_cpu (x86_cpu_to_apicid, cpu);kvm_hypercall2 (KVM_HC_KICK_CPU , flags, apicid); } +PV_CALLEE_SAVE_REGS_THUNK (kvm_kick_cpu); +#ifndef CONFIG_QUEUE_SPINLOCK enum kvm_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -796,6 +798,132 @@ static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket) } } } +#else /* !CONFIG_QUEUE_SPINLOCK */ + +#ifdef CONFIG_KVM_DEBUG_FS +static struct dentry *d_spin_debug; +static struct dentry *d_kvm_debug; +static u32 kick_nohlt_stats;/* Kick but not halt count */ +static u32 halt_qhead_stats;/* Queue head halting count */ +static u32 halt_qnode_stats;/* Queue node halting count */ +static u32 halt_abort_stats;/* Halting abort count */ +static u32 wake_kick_stats;/* Wakeup by kicking count */ +static u32 wake_spur_stats;/* Spurious wakeup count */ +static u64 time_blocked;/* Total blocking time */ + +static int __init kvm_spinlock_debugfs (void ) +{ + d_kvm_debug =debugfs_create_dir ("kvm-guest" ,NULL ); +if (!d_kvm_debug) { +printk (KERN_WARNING +"Could not create 'kvm' debugfs directory\n" ); +return -ENOMEM ; + } + d_spin_debug =debugfs_create_dir ("spinlocks" , d_kvm_debug); + +debugfs_create_u32 ("kick_nohlt_stats" , +0644 , d_spin_debug, &kick_nohlt_stats); +debugfs_create_u32 ("halt_qhead_stats" , +0644 , d_spin_debug, &halt_qhead_stats); +debugfs_create_u32 ("halt_qnode_stats" , +0644 , d_spin_debug, &halt_qnode_stats); +debugfs_create_u32 ("halt_abort_stats" , +0644 , d_spin_debug, &halt_abort_stats); +debugfs_create_u32 ("wake_kick_stats" , +0644 , d_spin_debug, &wake_kick_stats); +debugfs_create_u32 ("wake_spur_stats" , +0644 , d_spin_debug, &wake_spur_stats); +debugfs_create_u64 ("time_blocked" , +0644 , d_spin_debug, &time_blocked); +return 0 ; +} + +static inline void kvm_halt_stats (enum pv_lock_stats type) +{ +if (type ==PV_HALT_QHEAD ) +add_smp (&halt_qhead_stats,1 ); +else if (type ==PV_HALT_QNODE ) +add_smp (&halt_qnode_stats,1 ); +else /* type == PV_HALT_ABORT */ +add_smp (&halt_abort_stats,1 ); +} + +void kvm_lock_stats (enum pv_lock_stats type) +{ +if (type ==PV_WAKE_KICKED ) +add_smp (&wake_kick_stats,1 ); +else if (type ==PV_WAKE_SPURIOUS ) +add_smp (&wake_spur_stats,1 ); +else /* type == PV_KICK_NOHALT */ +add_smp (&kick_nohlt_stats,1 ); +} +PV_CALLEE_SAVE_REGS_THUNK (kvm_lock_stats); + +static inline u64 spin_time_start (void ) +{ +return sched_clock (); +} + +static inline void spin_time_accum_blocked (u64 start) +{ +u64 delta; + + delta =sched_clock () - start; +add_smp (&time_blocked, delta); +} + +fs_initcall (kvm_spinlock_debugfs); + +#else /* CONFIG_KVM_DEBUG_FS */ +static inline void kvm_halt_stats (enum pv_lock_stats type) +{ +} + +static inline u64 spin_time_start (void ) +{ +return 0 ; +} + +static inline void spin_time_accum_blocked (u64 start) +{ +} +#endif /* CONFIG_KVM_DEBUG_FS */ + +/* +* Halt the current CPU & release it back to the host +*/ +void kvm_halt_cpu (u8 *lockbyte) +{ +unsigned long flags; +u64 start; + +if (in_nmi ()) +return ; + +/* +* Make sure an interrupt handler can't upset things in a +* partially setup state. +*/ +local_irq_save (flags); +/* +* Don't halt if the lock byte is defined and is free +*/ +if (lockbyte && !ACCESS_ONCE (*lockbyte)) { +kvm_halt_stats (PV_HALT_ABORT ); + goto out; + } + start =spin_time_start (); +kvm_halt_stats (lockbyte ?PV_HALT_QHEAD :PV_HALT_QNODE ); +if (arch_irqs_disabled_flags (flags)) +halt (); +else +safe_halt (); +spin_time_accum_blocked (start); +out: +local_irq_restore (flags); +} +PV_CALLEE_SAVE_REGS_THUNK (kvm_halt_cpu); +#endif /* !CONFIG_QUEUE_SPINLOCK */ /* * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
@@ -808,8 +936,16 @@ void __init kvm_spinlock_init(void)if (!kvm_para_has_feature (KVM_FEATURE_PV_UNHALT ))return ; +#ifdef CONFIG_QUEUE_SPINLOCK + pv_lock_ops.kick_cpu =PV_CALLEE_SAVE (kvm_kick_cpu); + pv_lock_ops.lockwait =PV_CALLEE_SAVE (kvm_halt_cpu); +#ifdef CONFIG_KVM_DEBUG_FS + pv_lock_ops.lockstat =PV_CALLEE_SAVE (kvm_lock_stats); +#endif +#else pv_lock_ops.lock_spinning =PV_CALLEE_SAVE (kvm_lock_spinning); pv_lock_ops.unlock_kick = kvm_unlock_kick; +#endif }static __init int kvm_spinlock_init_jump (void )
kernel/Kconfig.locks +1 -1
@@ -236,7 +236,7 @@ config ARCH_USE_QUEUE_SPINLOCK config QUEUE_SPINLOCK def_bool y if ARCH_USE_QUEUE_SPINLOCK - depends on SMP && !PARAVIRT_SPINLOCKS + depends on SMP && (!PARAVIRT_SPINLOCKS || !XEN) config ARCH_USE_QUEUE_RWLOCK bool
Signature · 1 line
1.7.1
PATCH v12 11/11 pvqspinlock, x86: Enable PV qspinlock for XEN
In reply to Waiman LongThis patch adds the necessary XEN specific code to allow XEN to support the CPU halting and kicking operations needed by the queue spinlock PV code.
--- arch/x86/xen/spinlock.c | 149 +++++++++++++++++++++++++++++++++++++++++++++-- kernel/Kconfig.locks | 2 +- 2 files changed, 145 insertions(+), 6 deletions(-)
arch/x86/xen/spinlock.c +144 -5
@@ -17,6 +17,12 @@#include "xen-ops.h" #include "debugfs.h" +static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; +static DEFINE_PER_CPU(char *, irq_name); +static bool xen_pvspin = true; + +#ifndef CONFIG_QUEUE_SPINLOCK +enum xen_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -100,12 +106,9 @@ struct xen_lock_waiting {__ticket_t want; }; -static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; -static DEFINE_PER_CPU(char *, irq_name); static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);static cpumask_t waiting_cpus; -static bool xen_pvspin = true; __visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want) {int irq =__this_cpu_read (lock_kicker_irq);
@@ -213,6 +216,118 @@ static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next) } } +#else /* CONFIG_QUEUE_SPINLOCK */ + +#ifdef CONFIG_XEN_DEBUG_FS +static u32 kick_nohlt_stats;/* Kick but not halt count */ +static u32 halt_qhead_stats;/* Queue head halting count */ +static u32 halt_qnode_stats;/* Queue node halting count */ +static u32 halt_abort_stats;/* Halting abort count */ +static u32 wake_kick_stats;/* Wakeup by kicking count */ +static u32 wake_spur_stats;/* Spurious wakeup count */ +static u64 time_blocked;/* Total blocking time */ + +static inline void xen_halt_stats (enum pv_lock_stats type) +{ +if (type ==PV_HALT_QHEAD ) +add_smp (&halt_qhead_stats,1 ); +else if (type ==PV_HALT_QNODE ) +add_smp (&halt_qnode_stats,1 ); +else /* type == PV_HALT_ABORT */ +add_smp (&halt_abort_stats,1 ); +} + +void xen_lock_stats (enum pv_lock_stats type) +{ +if (type ==PV_WAKE_KICKED ) +add_smp (&wake_kick_stats,1 ); +else if (type ==PV_WAKE_SPURIOUS ) +add_smp (&wake_spur_stats,1 ); +else /* type == PV_KICK_NOHALT */ +add_smp (&kick_nohlt_stats,1 ); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_lock_stats); + +static inline u64 spin_time_start (void ) +{ +return sched_clock (); +} + +static inline void spin_time_accum_blocked (u64 start) +{ +u64 delta; + + delta =sched_clock () - start; +add_smp (&time_blocked, delta); +} +#else /* CONFIG_XEN_DEBUG_FS */ +static inline void xen_halt_stats (enum pv_lock_stats type) +{ +} + +static inline u64 spin_time_start (void ) +{ +return 0 ; +} + +static inline void spin_time_accum_blocked (u64 start) +{ +} +#endif /* CONFIG_XEN_DEBUG_FS */ + +void xen_kick_cpu (int cpu) +{ +xen_send_IPI_one (cpu,XEN_SPIN_UNLOCK_VECTOR ); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_kick_cpu); + +/* +* Halt the current CPU & release it back to the host +*/ +void xen_halt_cpu (u8 *lockbyte) +{ +int irq =__this_cpu_read (lock_kicker_irq); +unsigned long flags; +u64 start; + +/* If kicker interrupts not initialized yet, just spin */ +if (irq ==-1 ) +return ; + +/* +* Make sure an interrupt handler can't upset things in a +* partially setup state. +*/ +local_irq_save (flags); + start =spin_time_start (); + +xen_halt_stats (lockbyte ?PV_HALT_QHEAD :PV_HALT_QNODE ); +/* clear pending */ +xen_clear_irq_pending (irq); + +/* Allow interrupts while blocked */ +local_irq_restore (flags); +/* +* Don't halt if the lock is now available +*/ +if (lockbyte && !ACCESS_ONCE (*lockbyte)) { +xen_halt_stats (PV_HALT_ABORT ); +return ; + } +/* +* If an interrupt happens here, it will leave the wakeup irq +* pending, which will cause xen_poll_irq() to return +* immediately. +*/ + +/* Block until irq becomes pending (or perhaps a spurious wakeup) */ +xen_poll_irq (irq); +spin_time_accum_blocked (start); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_halt_cpu); + +#endif /* CONFIG_QUEUE_SPINLOCK */ +static irqreturn_t dummy_handler (int irq,void *dev_id) {BUG ();
@@ -258,7 +373,6 @@ void xen_uninit_lock_cpu(int cpu)per_cpu (irq_name, cpu) =NULL ; } -/* * Our init of PV spinlocks is split in two init functions due to us * using paravirt patching and jump labels patching and having to do
@@ -275,8 +389,17 @@ void __init xen_init_spinlocks(void)return ; }printk (KERN_DEBUG "xen: PV spinlocks enabled\n" ); + +#ifdef CONFIG_QUEUE_SPINLOCK + pv_lock_ops.kick_cpu =PV_CALLEE_SAVE (xen_kick_cpu); + pv_lock_ops.lockwait =PV_CALLEE_SAVE (xen_halt_cpu); +#ifdef CONFIG_XEN_DEBUG_FS + pv_lock_ops.lockstat =PV_CALLEE_SAVE (xen_lock_stats); +#endif +#else pv_lock_ops.lock_spinning =PV_CALLEE_SAVE (xen_lock_spinning); pv_lock_ops.unlock_kick = xen_unlock_kick; +#endif }/*
@@ -321,6 +444,7 @@ static int __init xen_spinlock_debugfs(void) d_spin_debug =debugfs_create_dir ("spinlocks" , d_xen); +#ifndef CONFIG_QUEUE_SPINLOCK debugfs_create_u8 ("zero_stats" ,0644 , d_spin_debug, &zero_stats); debugfs_create_u32("taken_slow", 0444, d_spin_debug,
@@ -340,7 +464,22 @@ static int __init xen_spinlock_debugfs(void)debugfs_create_u32_array ("histo_blocked" ,0444 , d_spin_debug, spinlock_stats.histo_spin_blocked,HISTO_BUCKETS +1 ); - +#else /* CONFIG_QUEUE_SPINLOCK */ +debugfs_create_u32 ("kick_nohlt_stats" , +0644 , d_spin_debug, &kick_nohlt_stats); +debugfs_create_u32 ("halt_qhead_stats" , +0644 , d_spin_debug, &halt_qhead_stats); +debugfs_create_u32 ("halt_qnode_stats" , +0644 , d_spin_debug, &halt_qnode_stats); +debugfs_create_u32 ("halt_abort_stats" , +0644 , d_spin_debug, &halt_abort_stats); +debugfs_create_u32 ("wake_kick_stats" , +0644 , d_spin_debug, &wake_kick_stats); +debugfs_create_u32 ("wake_spur_stats" , +0644 , d_spin_debug, &wake_spur_stats); +debugfs_create_u64 ("time_blocked" , +0644 , d_spin_debug, &time_blocked); +#endif /* CONFIG_QUEUE_SPINLOCK */ return 0 ; }fs_initcall (xen_spinlock_debugfs);
kernel/Kconfig.locks +1 -1
@@ -236,7 +236,7 @@ config ARCH_USE_QUEUE_SPINLOCK config QUEUE_SPINLOCK def_bool y if ARCH_USE_QUEUE_SPINLOCK - depends on SMP && (!PARAVIRT_SPINLOCKS || !XEN) + depends on SMP config ARCH_USE_QUEUE_RWLOCK bool
Signature · 1 line
1.7.1
PATCH v12 11/11 pvqspinlock, x86: Enable PV qspinlock for XEN
In reply to Waiman LongThis patch adds the necessary XEN specific code to allow XEN to support the CPU halting and kicking operations needed by the queue spinlock PV code.
--- arch/x86/xen/spinlock.c | 149 +++++++++++++++++++++++++++++++++++++++++++++-- kernel/Kconfig.locks | 2 +- 2 files changed, 145 insertions(+), 6 deletions(-)
arch/x86/xen/spinlock.c +144 -5
@@ -17,6 +17,12 @@#include "xen-ops.h" #include "debugfs.h" +static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; +static DEFINE_PER_CPU(char *, irq_name); +static bool xen_pvspin = true; + +#ifndef CONFIG_QUEUE_SPINLOCK +enum xen_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -100,12 +106,9 @@ struct xen_lock_waiting {__ticket_t want; }; -static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; -static DEFINE_PER_CPU(char *, irq_name); static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);static cpumask_t waiting_cpus; -static bool xen_pvspin = true; __visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want) {int irq =__this_cpu_read (lock_kicker_irq);
@@ -213,6 +216,118 @@ static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next) } } +#else /* CONFIG_QUEUE_SPINLOCK */ + +#ifdef CONFIG_XEN_DEBUG_FS +static u32 kick_nohlt_stats;/* Kick but not halt count */ +static u32 halt_qhead_stats;/* Queue head halting count */ +static u32 halt_qnode_stats;/* Queue node halting count */ +static u32 halt_abort_stats;/* Halting abort count */ +static u32 wake_kick_stats;/* Wakeup by kicking count */ +static u32 wake_spur_stats;/* Spurious wakeup count */ +static u64 time_blocked;/* Total blocking time */ + +static inline void xen_halt_stats (enum pv_lock_stats type) +{ +if (type ==PV_HALT_QHEAD ) +add_smp (&halt_qhead_stats,1 ); +else if (type ==PV_HALT_QNODE ) +add_smp (&halt_qnode_stats,1 ); +else /* type == PV_HALT_ABORT */ +add_smp (&halt_abort_stats,1 ); +} + +void xen_lock_stats (enum pv_lock_stats type) +{ +if (type ==PV_WAKE_KICKED ) +add_smp (&wake_kick_stats,1 ); +else if (type ==PV_WAKE_SPURIOUS ) +add_smp (&wake_spur_stats,1 ); +else /* type == PV_KICK_NOHALT */ +add_smp (&kick_nohlt_stats,1 ); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_lock_stats); + +static inline u64 spin_time_start (void ) +{ +return sched_clock (); +} + +static inline void spin_time_accum_blocked (u64 start) +{ +u64 delta; + + delta =sched_clock () - start; +add_smp (&time_blocked, delta); +} +#else /* CONFIG_XEN_DEBUG_FS */ +static inline void xen_halt_stats (enum pv_lock_stats type) +{ +} + +static inline u64 spin_time_start (void ) +{ +return 0 ; +} + +static inline void spin_time_accum_blocked (u64 start) +{ +} +#endif /* CONFIG_XEN_DEBUG_FS */ + +void xen_kick_cpu (int cpu) +{ +xen_send_IPI_one (cpu,XEN_SPIN_UNLOCK_VECTOR ); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_kick_cpu); + +/* +* Halt the current CPU & release it back to the host +*/ +void xen_halt_cpu (u8 *lockbyte) +{ +int irq =__this_cpu_read (lock_kicker_irq); +unsigned long flags; +u64 start; + +/* If kicker interrupts not initialized yet, just spin */ +if (irq ==-1 ) +return ; + +/* +* Make sure an interrupt handler can't upset things in a +* partially setup state. +*/ +local_irq_save (flags); + start =spin_time_start (); + +xen_halt_stats (lockbyte ?PV_HALT_QHEAD :PV_HALT_QNODE ); +/* clear pending */ +xen_clear_irq_pending (irq); + +/* Allow interrupts while blocked */ +local_irq_restore (flags); +/* +* Don't halt if the lock is now available +*/ +if (lockbyte && !ACCESS_ONCE (*lockbyte)) { +xen_halt_stats (PV_HALT_ABORT ); +return ; + } +/* +* If an interrupt happens here, it will leave the wakeup irq +* pending, which will cause xen_poll_irq() to return +* immediately. +*/ + +/* Block until irq becomes pending (or perhaps a spurious wakeup) */ +xen_poll_irq (irq); +spin_time_accum_blocked (start); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_halt_cpu); + +#endif /* CONFIG_QUEUE_SPINLOCK */ +static irqreturn_t dummy_handler (int irq,void *dev_id) {BUG ();
@@ -258,7 +373,6 @@ void xen_uninit_lock_cpu(int cpu)per_cpu (irq_name, cpu) =NULL ; } -/* * Our init of PV spinlocks is split in two init functions due to us * using paravirt patching and jump labels patching and having to do
@@ -275,8 +389,17 @@ void __init xen_init_spinlocks(void)return ; }printk (KERN_DEBUG "xen: PV spinlocks enabled\n" ); + +#ifdef CONFIG_QUEUE_SPINLOCK + pv_lock_ops.kick_cpu =PV_CALLEE_SAVE (xen_kick_cpu); + pv_lock_ops.lockwait =PV_CALLEE_SAVE (xen_halt_cpu); +#ifdef CONFIG_XEN_DEBUG_FS + pv_lock_ops.lockstat =PV_CALLEE_SAVE (xen_lock_stats); +#endif +#else pv_lock_ops.lock_spinning =PV_CALLEE_SAVE (xen_lock_spinning); pv_lock_ops.unlock_kick = xen_unlock_kick; +#endif }/*
@@ -321,6 +444,7 @@ static int __init xen_spinlock_debugfs(void) d_spin_debug =debugfs_create_dir ("spinlocks" , d_xen); +#ifndef CONFIG_QUEUE_SPINLOCK debugfs_create_u8 ("zero_stats" ,0644 , d_spin_debug, &zero_stats); debugfs_create_u32("taken_slow", 0444, d_spin_debug,
@@ -340,7 +464,22 @@ static int __init xen_spinlock_debugfs(void)debugfs_create_u32_array ("histo_blocked" ,0444 , d_spin_debug, spinlock_stats.histo_spin_blocked,HISTO_BUCKETS +1 ); - +#else /* CONFIG_QUEUE_SPINLOCK */ +debugfs_create_u32 ("kick_nohlt_stats" , +0644 , d_spin_debug, &kick_nohlt_stats); +debugfs_create_u32 ("halt_qhead_stats" , +0644 , d_spin_debug, &halt_qhead_stats); +debugfs_create_u32 ("halt_qnode_stats" , +0644 , d_spin_debug, &halt_qnode_stats); +debugfs_create_u32 ("halt_abort_stats" , +0644 , d_spin_debug, &halt_abort_stats); +debugfs_create_u32 ("wake_kick_stats" , +0644 , d_spin_debug, &wake_kick_stats); +debugfs_create_u32 ("wake_spur_stats" , +0644 , d_spin_debug, &wake_spur_stats); +debugfs_create_u64 ("time_blocked" , +0644 , d_spin_debug, &time_blocked); +#endif /* CONFIG_QUEUE_SPINLOCK */ return 0 ; }fs_initcall (xen_spinlock_debugfs);
kernel/Kconfig.locks +1 -1
@@ -236,7 +236,7 @@ config ARCH_USE_QUEUE_SPINLOCK config QUEUE_SPINLOCK def_bool y if ARCH_USE_QUEUE_SPINLOCK - depends on SMP && (!PARAVIRT_SPINLOCKS || !XEN) + depends on SMP config ARCH_USE_QUEUE_RWLOCK bool
Signature · 1 line
1.7.1
PATCH v12 11/11 pvqspinlock, x86: Enable PV qspinlock for XEN
In reply to Waiman LongThis patch adds the necessary XEN specific code to allow XEN to support the CPU halting and kicking operations needed by the queue spinlock PV code.
--- arch/x86/xen/spinlock.c | 149 +++++++++++++++++++++++++++++++++++++++++++++-- kernel/Kconfig.locks | 2 +- 2 files changed, 145 insertions(+), 6 deletions(-)
arch/x86/xen/spinlock.c +144 -5
@@ -17,6 +17,12 @@#include "xen-ops.h" #include "debugfs.h" +static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; +static DEFINE_PER_CPU(char *, irq_name); +static bool xen_pvspin = true; + +#ifndef CONFIG_QUEUE_SPINLOCK +enum xen_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -100,12 +106,9 @@ struct xen_lock_waiting {__ticket_t want; }; -static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; -static DEFINE_PER_CPU(char *, irq_name); static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);static cpumask_t waiting_cpus; -static bool xen_pvspin = true; __visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want) {int irq =__this_cpu_read (lock_kicker_irq);
@@ -213,6 +216,118 @@ static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next) } } +#else /* CONFIG_QUEUE_SPINLOCK */ + +#ifdef CONFIG_XEN_DEBUG_FS +static u32 kick_nohlt_stats;/* Kick but not halt count */ +static u32 halt_qhead_stats;/* Queue head halting count */ +static u32 halt_qnode_stats;/* Queue node halting count */ +static u32 halt_abort_stats;/* Halting abort count */ +static u32 wake_kick_stats;/* Wakeup by kicking count */ +static u32 wake_spur_stats;/* Spurious wakeup count */ +static u64 time_blocked;/* Total blocking time */ + +static inline void xen_halt_stats (enum pv_lock_stats type) +{ +if (type ==PV_HALT_QHEAD ) +add_smp (&halt_qhead_stats,1 ); +else if (type ==PV_HALT_QNODE ) +add_smp (&halt_qnode_stats,1 ); +else /* type == PV_HALT_ABORT */ +add_smp (&halt_abort_stats,1 ); +} + +void xen_lock_stats (enum pv_lock_stats type) +{ +if (type ==PV_WAKE_KICKED ) +add_smp (&wake_kick_stats,1 ); +else if (type ==PV_WAKE_SPURIOUS ) +add_smp (&wake_spur_stats,1 ); +else /* type == PV_KICK_NOHALT */ +add_smp (&kick_nohlt_stats,1 ); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_lock_stats); + +static inline u64 spin_time_start (void ) +{ +return sched_clock (); +} + +static inline void spin_time_accum_blocked (u64 start) +{ +u64 delta; + + delta =sched_clock () - start; +add_smp (&time_blocked, delta); +} +#else /* CONFIG_XEN_DEBUG_FS */ +static inline void xen_halt_stats (enum pv_lock_stats type) +{ +} + +static inline u64 spin_time_start (void ) +{ +return 0 ; +} + +static inline void spin_time_accum_blocked (u64 start) +{ +} +#endif /* CONFIG_XEN_DEBUG_FS */ + +void xen_kick_cpu (int cpu) +{ +xen_send_IPI_one (cpu,XEN_SPIN_UNLOCK_VECTOR ); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_kick_cpu); + +/* +* Halt the current CPU & release it back to the host +*/ +void xen_halt_cpu (u8 *lockbyte) +{ +int irq =__this_cpu_read (lock_kicker_irq); +unsigned long flags; +u64 start; + +/* If kicker interrupts not initialized yet, just spin */ +if (irq ==-1 ) +return ; + +/* +* Make sure an interrupt handler can't upset things in a +* partially setup state. +*/ +local_irq_save (flags); + start =spin_time_start (); + +xen_halt_stats (lockbyte ?PV_HALT_QHEAD :PV_HALT_QNODE ); +/* clear pending */ +xen_clear_irq_pending (irq); + +/* Allow interrupts while blocked */ +local_irq_restore (flags); +/* +* Don't halt if the lock is now available +*/ +if (lockbyte && !ACCESS_ONCE (*lockbyte)) { +xen_halt_stats (PV_HALT_ABORT ); +return ; + } +/* +* If an interrupt happens here, it will leave the wakeup irq +* pending, which will cause xen_poll_irq() to return +* immediately. +*/ + +/* Block until irq becomes pending (or perhaps a spurious wakeup) */ +xen_poll_irq (irq); +spin_time_accum_blocked (start); +} +PV_CALLEE_SAVE_REGS_THUNK (xen_halt_cpu); + +#endif /* CONFIG_QUEUE_SPINLOCK */ +static irqreturn_t dummy_handler (int irq,void *dev_id) {BUG ();
@@ -258,7 +373,6 @@ void xen_uninit_lock_cpu(int cpu)per_cpu (irq_name, cpu) =NULL ; } -/* * Our init of PV spinlocks is split in two init functions due to us * using paravirt patching and jump labels patching and having to do
@@ -275,8 +389,17 @@ void __init xen_init_spinlocks(void)return ; }printk (KERN_DEBUG "xen: PV spinlocks enabled\n" ); + +#ifdef CONFIG_QUEUE_SPINLOCK + pv_lock_ops.kick_cpu =PV_CALLEE_SAVE (xen_kick_cpu); + pv_lock_ops.lockwait =PV_CALLEE_SAVE (xen_halt_cpu); +#ifdef CONFIG_XEN_DEBUG_FS + pv_lock_ops.lockstat =PV_CALLEE_SAVE (xen_lock_stats); +#endif +#else pv_lock_ops.lock_spinning =PV_CALLEE_SAVE (xen_lock_spinning); pv_lock_ops.unlock_kick = xen_unlock_kick; +#endif }/*
@@ -321,6 +444,7 @@ static int __init xen_spinlock_debugfs(void) d_spin_debug =debugfs_create_dir ("spinlocks" , d_xen); +#ifndef CONFIG_QUEUE_SPINLOCK debugfs_create_u8 ("zero_stats" ,0644 , d_spin_debug, &zero_stats); debugfs_create_u32("taken_slow", 0444, d_spin_debug,
@@ -340,7 +464,22 @@ static int __init xen_spinlock_debugfs(void)debugfs_create_u32_array ("histo_blocked" ,0444 , d_spin_debug, spinlock_stats.histo_spin_blocked,HISTO_BUCKETS +1 ); - +#else /* CONFIG_QUEUE_SPINLOCK */ +debugfs_create_u32 ("kick_nohlt_stats" , +0644 , d_spin_debug, &kick_nohlt_stats); +debugfs_create_u32 ("halt_qhead_stats" , +0644 , d_spin_debug, &halt_qhead_stats); +debugfs_create_u32 ("halt_qnode_stats" , +0644 , d_spin_debug, &halt_qnode_stats); +debugfs_create_u32 ("halt_abort_stats" , +0644 , d_spin_debug, &halt_abort_stats); +debugfs_create_u32 ("wake_kick_stats" , +0644 , d_spin_debug, &wake_kick_stats); +debugfs_create_u32 ("wake_spur_stats" , +0644 , d_spin_debug, &wake_spur_stats); +debugfs_create_u64 ("time_blocked" , +0644 , d_spin_debug, &time_blocked); +#endif /* CONFIG_QUEUE_SPINLOCK */ return 0 ; }fs_initcall (xen_spinlock_debugfs);
kernel/Kconfig.locks +1 -1
@@ -236,7 +236,7 @@ config ARCH_USE_QUEUE_SPINLOCK config QUEUE_SPINLOCK def_bool y if ARCH_USE_QUEUE_SPINLOCK - depends on SMP && (!PARAVIRT_SPINLOCKS || !XEN) + depends on SMP config ARCH_USE_QUEUE_RWLOCK bool
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerQuoted 8 lines · Waiman Long
v11->v12:
- Based on PeterZ's version of the qspinlock patch
(https://lkml.org/lkml/2014/6/15/63).
- Incorporated many of the review comments from Konrad Wilk and Paolo Bonzini.
- The pvqspinlock code is largely from my previous version with PeterZ's way of going from queue tail to head and his idea of using callee saved calls to KVM and XEN codes.
Thanks for taking the time to refresh this.. I would prefer you use a little more of the PV techniques I outlined in my latest PV patch to further reduce the overhead of PV enabled kernels on real hardware.
This is an important use case, because distro kernels will have to enable PV support while their majority of installations will be on physical hardware.
Other than that I see no reason not to move this forward.
Quoted 13 lines · Peter Zijlstra
Quoted 8 lines · Waiman Long
v11->v12:
- Based on PeterZ's version of the qspinlock patch(https://lkml.org/lkml/2014/6/15/63).- Incorporated many of the review comments from Konrad Wilk and Paolo Bonzini.
- The pvqspinlock code is largely from my previous version with PeterZ's way of going from queue tail to head and his idea of using callee saved calls to KVM and XEN codes.Thanks for taking the time to refresh this.. I would prefer you use a little more of the PV techniques I outlined in my latest PV patch to further reduce the overhead of PV enabled kernels on real hardware.
Quoted 5 lines
This is an important use case, because distro kernels will have to enable PV support while their majority of installations will be on physical hardware.
Other than that I see no reason not to move this forward.
Thanks for reviewing the patch and agree to move forward. Currently, I am thinking of separating out a PV and non-PV versions of the lock slowpath functions as shown in my previous mail. That should also minimize the performance impact on bare metal even more than what can be done with the PV techniques used in your patch while not penalizing PV performance.
As for the unlock function, if the site patching function can handle all the possible call sites of spin_unlock() without disabling function inlining, I will be glad to use your way of handing unlock function. Otherwise, I will prefer my current approach as it is simpler and more easy to understand as well as similar to what has been done in the pv ticket spinlock code.
-Longman
Quoted 13 lines · Peter Zijlstra
Quoted 8 lines · Waiman Long
v11->v12:
- Based on PeterZ's version of the qspinlock patch(https://lkml.org/lkml/2014/6/15/63).- Incorporated many of the review comments from Konrad Wilk and Paolo Bonzini.
- The pvqspinlock code is largely from my previous version with PeterZ's way of going from queue tail to head and his idea of using callee saved calls to KVM and XEN codes.Thanks for taking the time to refresh this.. I would prefer you use a little more of the PV techniques I outlined in my latest PV patch to further reduce the overhead of PV enabled kernels on real hardware.
Quoted 5 lines
This is an important use case, because distro kernels will have to enable PV support while their majority of installations will be on physical hardware.
Other than that I see no reason not to move this forward.
Thanks for reviewing the patch and agree to move forward. Currently, I am thinking of separating out a PV and non-PV versions of the lock slowpath functions as shown in my previous mail. That should also minimize the performance impact on bare metal even more than what can be done with the PV techniques used in your patch while not penalizing PV performance.
As for the unlock function, if the site patching function can handle all the possible call sites of spin_unlock() without disabling function inlining, I will be glad to use your way of handing unlock function. Otherwise, I will prefer my current approach as it is simpler and more easy to understand as well as similar to what has been done in the pv ticket spinlock code.
-Longman
Peter Zijlstra
maintainerQuoted 8 lines · Waiman Long
v11->v12:
- Based on PeterZ's version of the qspinlock patch
(https://lkml.org/lkml/2014/6/15/63).
- Incorporated many of the review comments from Konrad Wilk and Paolo Bonzini.
- The pvqspinlock code is largely from my previous version with PeterZ's way of going from queue tail to head and his idea of using callee saved calls to KVM and XEN codes.
Thanks for taking the time to refresh this.. I would prefer you use a little more of the PV techniques I outlined in my latest PV patch to further reduce the overhead of PV enabled kernels on real hardware.
This is an important use case, because distro kernels will have to enable PV support while their majority of installations will be on physical hardware.
Other than that I see no reason not to move this forward.