qspinlock: a 4-byte queue spinlock with PV support
Subsystems (8)
v5 cover qspinlock: a 4-byte queue spinlock with PV support
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-3: 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 breaking data alignments.
2) Patches 4 and 5: Enables the use of unfair queue spinlock in a real para-virtualized execution environment. 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 6-8: Enable qspinlock para-virtualization support by making sure that the lock holder and the queue head stay alive as long as possible.
Patches 1-3 are fully tested and ready for production. Patches 4-8, on the other hands, are not fully tested. They have undergone compilation tests with various combinations of kernel config settings, boot-up tests in a bare-metal as well as simple performance test on a KVM guest. Further tests and performance characterization are still needed to be done. So comments on them are welcomed. Suggestions or recommendations on how to add PV support in the Xen environment are also needed.
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 noticeable performance benefit probably won't show up in machines with less than 4 sockets.
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.
The performance data in bare metal were discussed in the patch descriptions. For PV support, some simple performance test was performed on a 2-node 20-CPU KVM guest running 3.14-rc4 kernel in a larger 8-node machine. The disk workload of the AIM7 benchmark was run on both ext4 and xfs RAM disks at 2000 users. The JPM (jobs/minute) data of the test run were:
kernel XFS FS %change ext4 FS %change
------ ------ ------- ------- -------
PV ticketlock (baseline) 2390438 - 1366743 -
qspinlock 1775148 -26% 1336303 -2.2%
PV qspinlock 2264151 -5.3% 1351351 -1.1%
unfair qspinlock 2404810 +0.6% 1612903 +18%
unfair + PV qspinlock 2419355 +1.2% 1612903 +18%
The XFS test had moderate spinlock contention of 1.6% whereas the ext4 test had heavy spinlock contention of 15.4% as reported by perf. It seems like the PV qspinlock support still has room for improvement compared with the current PV ticketlock implementation.
Waiman Long (8):
qspinlock: Introducing a 4-byte queue spinlock implementation qspinlock, x86: Enable x86-64 to use queue spinlock qspinlock, x86: Add x86 specific optimization for 2 contending tasks pvqspinlock, x86: Allow unfair spinlock in a real PV environment pvqspinlock, x86: Enable unfair queue spinlock in a KVM guest pvqspinlock, x86: Rename paravirt_ticketlocks_enabled pvqspinlock, x86: Add qspinlock para-virtualization support pvqspinlock, x86: Enable KVM to use qspinlock's PV support
arch/x86/Kconfig | 12 + arch/x86/include/asm/paravirt.h | 9 +- arch/x86/include/asm/paravirt_types.h | 12 + arch/x86/include/asm/pvqspinlock.h | 176 ++++++++++ arch/x86/include/asm/qspinlock.h | 133 +++++++ arch/x86/include/asm/spinlock.h | 9 +- arch/x86/include/asm/spinlock_types.h | 4 + arch/x86/kernel/Makefile | 1 + arch/x86/kernel/kvm.c | 73 ++++- arch/x86/kernel/paravirt-spinlocks.c | 15 +- arch/x86/xen/spinlock.c | 2 +- include/asm-generic/qspinlock.h | 122 +++++++ include/asm-generic/qspinlock_types.h | 61 ++++ kernel/Kconfig.locks | 7 + kernel/locking/Makefile | 1 + kernel/locking/qspinlock.c | 610 +++++++++++++++++++++++++++++++++ 16 files changed, 1239 insertions(+), 8 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 v5 1/8 qspinlock: Introducing a 4-byte queue spinlock implementation
This patch introduces a new queue spinlock implementation that can serve as an alternative to the default ticket spinlock. Compared with the ticket spinlock, this queue spinlock should be almost as fair as the ticket spinlock. It has about the same speed in single-thread and it can be much faster in high contention situations. Only in light to moderate contention where the average queue depth is around 1-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.
The idea behind this spinlock implementation is the fact that spinlocks are acquired with preemption disabled. In other words, the process will not be migrated to another CPU while it is trying to get a spinlock. Ignoring interrupt handling, a CPU can only be contending in one spinlock at any one time. Of course, interrupt handler can try to acquire one spinlock while the interrupted user process is in the process of getting another spinlock. By allocating a set of per-cpu queue nodes and used them to form a waiting queue, we can encode the queue node address into a much smaller 16-bit size. Together with the 1-byte lock bit, this queue spinlock implementation will only need 4 bytes to hold all the information that it needs.
The current queue node address encoding of the 4-byte word is as follows:
Bits 0-7 : the locked byte
Bits 8-9 : queue node index in the per-cpu array (4 entries) Bits 10-31: cpu number + 1 (max cpus = 4M -1)
In the extremely unlikely case that all the queue node entries are used up, the current code will fall back to busy spinning without waiting in a queue with warning message.
For single-thread performance (no contention), a 256K lock/unlock loop was run on a 2.4Ghz Westmere x86-64 CPU. The following table shows the average time (in ns) for a single lock/unlock sequence (including the looping and timing overhead):
Lock Type Time (ns)
--------- ---------
Ticket spinlock 14.1
Queue spinlock 8.8
So the queue spinlock is much faster than the ticket spinlock, even though the overhead of locking and unlocking should be pretty small when there is no contention. The performance advantage is mainly due to the fact that ticket spinlock does a read-modify-write (add) instruction in unlock whereas queue spinlock only does a simple write in unlock which can be much faster in a pipelined CPU.
The AIM7 benchmark was run on a 8-socket 80-core DL980 with Westmere x86-64 CPUs with XFS filesystem on a ramdisk and HT off to evaluate the performance impact of this patch on a 3.13 kernel.
+------------+----------+-----------------+---------+ | Kernel | 3.13 JPM | 3.13 with | %Change | | | | qspinlock patch | | +------------+----------+-----------------+---------+ | 10-100 users | +------------+----------+-----------------+---------+ |custom | 357459 | 363109 | +1.58% | |dbase | 496847 | 498801 | +0.39% | |disk | 2925312 | 2771387 | -5.26% | |five_sec | 166612 | 169215 | +1.56% | |fserver | 382129 | 383279 | +0.30% | |high_systime| 16356 | 16380 | +0.15% | |short | 4521978 | 4257363 | -5.85% | +------------+----------+-----------------+---------+ | 200-1000 users | +------------+----------+-----------------+---------+ |custom | 449070 | 447711 | -0.30% | |dbase | 845029 | 853362 | +0.99% | |disk | 2725249 | 4892907 | +79.54% | |five_sec | 169410 | 170638 | +0.72% | |fserver | 489662 | 491828 | +0.44% | |high_systime| 142823 | 143790 | +0.68% | |short | 7435288 | 9016171 | +21.26% | +------------+----------+-----------------+---------+ | 1100-2000 users | +------------+----------+-----------------+---------+ |custom | 432470 | 432570 | +0.02% | |dbase | 889289 | 890026 | +0.08% | |disk | 2565138 | 5008732 | +95.26% | |five_sec | 169141 | 170034 | +0.53% | |fserver | 498569 | 500701 | +0.43% | |high_systime| 229913 | 245866 | +6.94% | |short | 8496794 | 8281918 | -2.53% | +------------+----------+-----------------+---------+
The workload with the most gain was the disk workload. Without the patch, the perf profile at 1500 users looked like:
26.19% reaim [kernel.kallsyms] [k] _raw_spin_lock
|--47.28%-- evict
|--46.87%-- inode_sb_list_add
|--1.24%-- xlog_cil_insert_items
|--0.68%-- __remove_inode_hash
|--0.67%-- inode_wait_for_writeback
--3.26%-- [...]
22.96% swapper [kernel.kallsyms] [k] cpu_idle_loop
5.56% reaim [kernel.kallsyms] [k] mutex_spin_on_owner
4.87% reaim [kernel.kallsyms] [k] update_cfs_rq_blocked_load
2.04% reaim [kernel.kallsyms] [k] mspin_lock
1.30% reaim [kernel.kallsyms] [k] memcpy
1.08% reaim [unknown] [.] 0x0000003c52009447There was pretty high spinlock contention on the inode_sb_list_lock and maybe the inode's i_lock.
With the patch, the perf profile at 1500 users became:
26.82% swapper [kernel.kallsyms] [k] cpu_idle_loop
4.66% reaim [kernel.kallsyms] [k] mutex_spin_on_owner
3.97% reaim [kernel.kallsyms] [k] update_cfs_rq_blocked_load
2.40% reaim [kernel.kallsyms] [k] queue_spin_lock_slowpath
|--88.31%-- _raw_spin_lock
| |--36.02%-- inode_sb_list_add
| |--35.09%-- evict
| |--16.89%-- xlog_cil_insert_items
| |--6.30%-- try_to_wake_up
| |--2.20%-- _xfs_buf_find
| |--0.75%-- __remove_inode_hash
| |--0.72%-- __mutex_lock_slowpath
| |--0.53%-- load_balance
|--6.02%-- _raw_spin_lock_irqsave
| |--74.75%-- down_trylock
| |--9.69%-- rcu_check_quiescent_state
| |--7.47%-- down
| |--3.57%-- up
| |--1.67%-- rwsem_wake
| |--1.00%-- remove_wait_queue
| |--0.56%-- pagevec_lru_move_fn
|--5.39%-- _raw_spin_lock_irq
| |--82.05%-- rwsem_down_read_failed
| |--10.48%-- rwsem_down_write_failed
| |--4.24%-- __down
| |--2.74%-- __schedule
--0.28%-- [...]
2.20% reaim [kernel.kallsyms] [k] memcpy
1.84% reaim [unknown] [.] 0x000000000041517b
1.77% reaim [kernel.kallsyms] [k] _raw_spin_lock
|--21.08%-- xlog_cil_insert_items
|--10.14%-- xfs_icsb_modify_counters
|--7.20%-- xfs_iget_cache_hit
|--6.56%-- inode_sb_list_add
|--5.49%-- _xfs_buf_find
|--5.25%-- evict
|--5.03%-- __remove_inode_hash
|--4.64%-- __mutex_lock_slowpath
|--3.78%-- selinux_inode_free_security
|--2.95%-- xfs_inode_is_filestream
|--2.35%-- try_to_wake_up
|--2.07%-- xfs_inode_set_reclaim_tag
|--1.52%-- list_lru_add
|--1.16%-- xfs_inode_clear_eofblocks_tag
:
1.30% reaim [kernel.kallsyms] [k] effective_load
1.27% reaim [kernel.kallsyms] [k] mspin_lock
1.10% reaim [kernel.kallsyms] [k] security_compute_sidOn the ext4 filesystem, the disk workload improved from 416281 JPM to 899101 JPM (+116%) with the patch. In this case, the contended spinlock is the mb_cache_spinlock.
--- include/asm-generic/qspinlock.h | 122 ++++++++++ include/asm-generic/qspinlock_types.h | 55 +++++ kernel/Kconfig.locks | 7 + kernel/locking/Makefile | 1 + kernel/locking/qspinlock.c | 393 +++++++++++++++++++++++++++++++++ 5 files changed, 578 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 +122 -0
@@ -0,0 +1,122 @@ +/* +* 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> + +/* +* External function declarations +*/ +extern void queue_spin_lock_slowpath (struct qspinlock *lock,int qsval); + +/** +* 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->qlcode) & _QSPINLOCK_LOCKED; +} + +/** +* queue_spin_value_unlocked - is the spinlock structure unlocked? +* @lock: queue spinlock structure +* Return: 1 if it is unlocked, 0 otherwise +*/ +static __always_inline int queue_spin_value_unlocked(struct qspinlock lock) +{ +return !(atomic_read (&lock.qlcode) & _QSPINLOCK_LOCKED); +} + +/** +* queue_spin_is_contended - check if the lock is contended +* @lock : Pointer to queue spinlock structure +* Return: 1 if lock contended, 0 otherwise +*/ +static __always_inline int queue_spin_is_contended(struct qspinlock *lock) +{ +return atomic_read (&lock->qlcode) & ~_QSPINLOCK_LOCK_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->qlcode) && + (atomic_cmpxchg (&lock->qlcode,0 , _QSPINLOCK_LOCKED) ==0 )) +return 1 ; +return 0 ; +} + +/** +* queue_spin_lock - acquire a queue spinlock +* @lock: Pointer to queue spinlock structure +*/ +static __always_inline void queue_spin_lock(struct qspinlock *lock) +{ +int qsval; + +/* +* To reduce memory access to only once for the cold cache case, +* a direct cmpxchg() is performed in the fastpath to optimize the +* uncontended case. The contended performance, however, may suffer +* a bit because of that. +*/ + qsval =atomic_cmpxchg (&lock->qlcode,0 , _QSPINLOCK_LOCKED); +if (likely (qsval ==0 )) +return ; +queue_spin_lock_slowpath (lock, qsval); +} + +#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) +{ +/* +* Use an atomic subtraction to clear the lock bit. +*/ +smp_mb__before_atomic_dec (); +atomic_sub (_QSPINLOCK_LOCKED, &lock->qlcode); +} +#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 +55 -0
55 lines not on this page · raw message
kernel/Kconfig.locks +7 -0
@@ -223,3 +223,10 @@ endif config MUTEX_SPIN_ON_OWNER def_bool y depends on SMP && !DEBUG_MUTEXES + +config ARCH_USE_QUEUE_SPINLOCK + bool + +config QUEUE_SPINLOCK + def_bool y if ARCH_USE_QUEUE_SPINLOCK + depends on SMP && !PARAVIRT_SPINLOCKS
kernel/locking/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_LOCKDEP) += lockdep_proc.o endif obj-$(CONFIG_SMP) += spinlock.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/qspinlock.c new file +393 -0
393 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
Quoted 27 lines · Waiman Long
kernel/locking/qspinlock.c new file +26
-0+void queue_spin_lock_slowpath (struct qspinlock *lock,int qsval) +{ +unsigned int cpu_nr, qn_idx; +struct qnode *node, *next; +u32 prev_qcode, my_qcode; + +/* +* Get the queue node +*/ + cpu_nr =smp_processor_id (); + node =get_qnode (&qn_idx); + +/* +* It should never happen that all the queue nodes are being used. +*/ +BUG_ON (!node); + +/* +* Set up the new cpu code to be exchanged +*/ + my_qcode =queue_encode_qcode (cpu_nr, qn_idx); + +/* +* Initialize the queue node +*/ + node->wait = true;
kernel/locking/qspinlock.c line 297
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+ node->next =NULL ; + +/* +* The lock may be available at this point, try again if no task was +* waiting in the queue. +*/ +if (!(qsval >> _QCODE_OFFSET) &&queue_spin_trylock (lock)) { +put_qnode (); +return ; + }
Cosmetic, but probably "goto release_node" would be more consistent.
And I am wondering how much this "qsval >> _QCODE_OFFSET" check can help. Note that this is the only usage of this arg, perhaps it would be better to simply remove it and shrink the caller's code a bit? It is also used in 3/8, but we can read the "fresh" value of ->qlcode (trylock does this anyway), and perhaps it can actually help if it is already unlocked.
Quoted 19 lines
kernel/locking/qspinlock.c new file +19
-0+ prev_qcode =atomic_xchg (&lock->qlcode, my_qcode); +/* +* It is possible that we may accidentally steal the lock. If this is +* the case, we need to either release it if not the head of the queue +* or get the lock and be done with it. +*/ +if (unlikely (!(prev_qcode & _QSPINLOCK_LOCKED))) { +if (prev_qcode ==0 ) { +/* +* Got the lock since it is at the head of the queue +* Now try to atomically clear the queue code. +*/ +if (atomic_cmpxchg (&lock->qlcode, my_qcode, + _QSPINLOCK_LOCKED) == my_qcode) + goto release_node; +/* +* The cmpxchg fails only if one or more tasks +* are added to the queue. In this case, we need to +* notify the next one to be the head of the queue.
kernel/locking/qspinlock.c line 330
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+*/ + goto notify_next; + } +/* +* Accidentally steal the lock, release the lock and +* let the queue head get it. +*/ +queue_spin_unlock (lock); + }else + prev_qcode &= ~_QSPINLOCK_LOCKED;/* Clear the lock bit */
You know, actually I started this email because I thought that "goto notify_next" is wrong, I misread the patch as if this "goto" can happen even if prev_qcode != 0.
So feel free to ignore, all my comments are cosmetic/subjective, but to me it would be more clean/clear to rewrite the code above as
if (prev_qcode == 0) {
if (atomic_cmpxchg(..., _QSPINLOCK_LOCKED) == my_qcode)
goto release_node;
goto notify_next;
}
if (prev_qcode & _QSPINLOCK_LOCKED)
prev_qcode &= ~_QSPINLOCK_LOCKED;
else
queue_spin_unlock(lock);kernel/locking/qspinlock.c line 360
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+while (true) { +u32 qcode; +int retval; + + retval =queue_get_lock_qcode (lock, &qcode, my_qcode); +if (retval >0 ) + ;/* Lock not available yet */ +else if (retval <0 ) +/* Lock taken, can release the node & return */ + goto release_node;
I guess this is for 3/8which adds the optimized version of queue_get_lock_qcode(), so perhaps this "retval < 0" block can go into 3/8 as well.
kernel/locking/qspinlock.c line 367
Quoted 7 lines
kernel/locking/qspinlock.c new file +7
-0+else if (qcode != my_qcode) { +/* +* Just get the lock with other spinners waiting +* in the queue. +*/ +if (queue_spin_setlock (lock)) + goto notify_next;
OTOH, at least the generic (non-optimized) version of queue_spin_setlock() could probably accept "qcode" and avoid atomic_read() + _QSPINLOCK_LOCKED check.
But once again, please feel free to ignore.
Oleg.
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
Quoted 30 lines · Oleg Nesterov
Quoted 29 lines · Waiman Long
kernel/locking/qspinlock.c new file +29
-0+void queue_spin_lock_slowpath (struct qspinlock *lock,int qsval) +{ +unsigned int cpu_nr, qn_idx; +struct qnode *node, *next; +u32 prev_qcode, my_qcode; + +/* +* Get the queue node +*/ + cpu_nr =smp_processor_id (); + node =get_qnode (&qn_idx); + +/* +* It should never happen that all the queue nodes are being used. +*/ +BUG_ON (!node); + +/* +* Set up the new cpu code to be exchanged +*/ + my_qcode =queue_encode_qcode (cpu_nr, qn_idx); + +/* +* Initialize the queue node +*/ + node->wait = true; + node->next =NULL ; + +/*
Quoted 8 lines
kernel/locking/qspinlock.c line 297
Quoted 7 lines
kernel/locking/qspinlock.c new file +7
-0+* The lock may be available at this point, try again if no task was +* waiting in the queue. +*/ +if (!(qsval>> _QCODE_OFFSET)&&queue_spin_trylock (lock)) { +put_qnode (); +return ; + }Cosmetic, but probably "goto
release_node" would be more consistent.
Yes, that is true.
Quoted 5 lines
And I am wondering how much this "qsval>>
_QCODE_OFFSET" check can help. Note that this is the only usage of this arg, perhaps it would be better to simply remove it and shrink the caller's code a bit? It is also used in 3/8, but we can read the "fresh" value of ->qlcode (trylock does this anyway), and perhaps it can actually help if it is already unlocked.
First of all, there is no shrinkage in the caller code even if the qsval argument is removed, at least for x86. The caller just targets the return register of the cmpxchg instruction to be the 2nd function parameter register.
When the lock is lightly contended, there isn't much difference on whether to check qsval or a fresh copy of qlcode. However, when the lock is heavily contended, every additional read or write will contribute to the cacheline bouncing traffic. The code was written to minimize those optional read request.
Quoted 35 lines
Quoted 19 lines
kernel/locking/qspinlock.c new file +19
-0+ prev_qcode =atomic_xchg (&lock->qlcode, my_qcode); +/* +* It is possible that we may accidentally steal the lock. If this is +* the case, we need to either release it if not the head of the queue +* or get the lock and be done with it. +*/ +if (unlikely (!(prev_qcode& _QSPINLOCK_LOCKED))) { +if (prev_qcode ==0 ) { +/* +* Got the lock since it is at the head of the queue +* Now try to atomically clear the queue code. +*/ +if (atomic_cmpxchg (&lock->qlcode, my_qcode, + _QSPINLOCK_LOCKED) == my_qcode) + goto release_node; +/* +* The cmpxchg fails only if one or more tasks +* are added to the queue. In this case, we need to +* notify the next one to be the head of the queue. kernel/locking/qspinlock.c
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+*/ + goto notify_next; + } +/* +* Accidentally steal the lock, release the lock and +* let the queue head get it. +*/ +queue_spin_unlock (lock); + }else + prev_qcode&= ~_QSPINLOCK_LOCKED;/* Clear the lock bit */ You know, actually I started this email because I thought that "goto
notify_next" is wrong, I misread the patch as if this "goto" can happen even ifprev_qcode!= 0.So feel free to ignore, all my comments are cosmetic/subjective, but to me it would be more clean/clear to rewrite the code above as
Quoted 11 lines
if (prev_qcode == 0) {
if (atomic_cmpxchg(..., _QSPINLOCK_LOCKED) == my_qcode)
goto release_node;
goto notify_next;
}
if (prev_qcode& _QSPINLOCK_LOCKED)
prev_qcode&= ~_QSPINLOCK_LOCKED;
else
queue_spin_unlock(lock);This part of the code cause confusion and make it harder to read. I am planning to rewrite it to use cmpxchg to make sure that it won't accidentally steal the lock. That should make the code easier to understand and make it possible to write better optimized code in other part of the function.
Quoted 5 lines
Quoted 5 lines
kernel/locking/qspinlock.c new file +5
-0+while (true) { +u32 qcode; +int retval; + + retval =queue_get_lock_qcode (lock,&qcode, my_qcode);
Quoted 9 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if (retval> 0) + ; /* Lock not available yet */ + else if (retval< 0) + /* Lock taken, can release the node& return */ + goto release_node;I guess this is for 3/8which adds the optimized version of
queue_get_lock_qcode(), so perhaps this "retval< 0" block can go into 3/8 as well.
Yes, that is true.
Quoted 11 lines
kernel/locking/qspinlock.c line 367
Quoted 7 lines
kernel/locking/qspinlock.c new file +7
-0+else if (qcode != my_qcode) { +/* +* Just get the lock with other spinners waiting +* in the queue. +*/ +if (queue_spin_setlock (lock)) + goto notify_next;OTOH, at least the generic (non-optimized) version of
queue_spin_setlock()could probably accept "qcode" and avoidatomic_read()+_QSPINLOCK_LOCKEDcheck.
Will do so.
Thank for the comments.
-Longman
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
In reply to Oleg NesterovQuoted 30 lines · Oleg Nesterov
Quoted 29 lines · Waiman Long
kernel/locking/qspinlock.c new file +29
-0+void queue_spin_lock_slowpath (struct qspinlock *lock,int qsval) +{ +unsigned int cpu_nr, qn_idx; +struct qnode *node, *next; +u32 prev_qcode, my_qcode; + +/* +* Get the queue node +*/ + cpu_nr =smp_processor_id (); + node =get_qnode (&qn_idx); + +/* +* It should never happen that all the queue nodes are being used. +*/ +BUG_ON (!node); + +/* +* Set up the new cpu code to be exchanged +*/ + my_qcode =queue_encode_qcode (cpu_nr, qn_idx); + +/* +* Initialize the queue node +*/ + node->wait = true; + node->next =NULL ; + +/*
Quoted 8 lines
kernel/locking/qspinlock.c line 297
Quoted 7 lines
kernel/locking/qspinlock.c new file +7
-0+* The lock may be available at this point, try again if no task was +* waiting in the queue. +*/ +if (!(qsval>> _QCODE_OFFSET)&&queue_spin_trylock (lock)) { +put_qnode (); +return ; + }Cosmetic, but probably "goto
release_node" would be more consistent.
Yes, that is true.
Quoted 5 lines
And I am wondering how much this "qsval>>
_QCODE_OFFSET" check can help. Note that this is the only usage of this arg, perhaps it would be better to simply remove it and shrink the caller's code a bit? It is also used in 3/8, but we can read the "fresh" value of ->qlcode (trylock does this anyway), and perhaps it can actually help if it is already unlocked.
First of all, there is no shrinkage in the caller code even if the qsval argument is removed, at least for x86. The caller just targets the return register of the cmpxchg instruction to be the 2nd function parameter register.
When the lock is lightly contended, there isn't much difference on whether to check qsval or a fresh copy of qlcode. However, when the lock is heavily contended, every additional read or write will contribute to the cacheline bouncing traffic. The code was written to minimize those optional read request.
Quoted 35 lines
Quoted 19 lines
kernel/locking/qspinlock.c new file +19
-0+ prev_qcode =atomic_xchg (&lock->qlcode, my_qcode); +/* +* It is possible that we may accidentally steal the lock. If this is +* the case, we need to either release it if not the head of the queue +* or get the lock and be done with it. +*/ +if (unlikely (!(prev_qcode& _QSPINLOCK_LOCKED))) { +if (prev_qcode ==0 ) { +/* +* Got the lock since it is at the head of the queue +* Now try to atomically clear the queue code. +*/ +if (atomic_cmpxchg (&lock->qlcode, my_qcode, + _QSPINLOCK_LOCKED) == my_qcode) + goto release_node; +/* +* The cmpxchg fails only if one or more tasks +* are added to the queue. In this case, we need to +* notify the next one to be the head of the queue. kernel/locking/qspinlock.c
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+*/ + goto notify_next; + } +/* +* Accidentally steal the lock, release the lock and +* let the queue head get it. +*/ +queue_spin_unlock (lock); + }else + prev_qcode&= ~_QSPINLOCK_LOCKED;/* Clear the lock bit */ You know, actually I started this email because I thought that "goto
notify_next" is wrong, I misread the patch as if this "goto" can happen even ifprev_qcode!= 0.So feel free to ignore, all my comments are cosmetic/subjective, but to me it would be more clean/clear to rewrite the code above as
Quoted 11 lines
if (prev_qcode == 0) {
if (atomic_cmpxchg(..., _QSPINLOCK_LOCKED) == my_qcode)
goto release_node;
goto notify_next;
}
if (prev_qcode& _QSPINLOCK_LOCKED)
prev_qcode&= ~_QSPINLOCK_LOCKED;
else
queue_spin_unlock(lock);This part of the code cause confusion and make it harder to read. I am planning to rewrite it to use cmpxchg to make sure that it won't accidentally steal the lock. That should make the code easier to understand and make it possible to write better optimized code in other part of the function.
Quoted 5 lines
Quoted 5 lines
kernel/locking/qspinlock.c new file +5
-0+while (true) { +u32 qcode; +int retval; + + retval =queue_get_lock_qcode (lock,&qcode, my_qcode);
Quoted 9 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ if (retval> 0) + ; /* Lock not available yet */ + else if (retval< 0) + /* Lock taken, can release the node& return */ + goto release_node;I guess this is for 3/8which adds the optimized version of
queue_get_lock_qcode(), so perhaps this "retval< 0" block can go into 3/8 as well.
Yes, that is true.
Quoted 11 lines
kernel/locking/qspinlock.c line 367
Quoted 7 lines
kernel/locking/qspinlock.c new file +7
-0+else if (qcode != my_qcode) { +/* +* Just get the lock with other spinners waiting +* in the queue. +*/ +if (queue_spin_setlock (lock)) + goto notify_next;OTOH, at least the generic (non-optimized) version of
queue_spin_setlock()could probably accept "qcode" and avoidatomic_read()+_QSPINLOCK_LOCKEDcheck.
Will do so.
Thank for the comments.
-Longman
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
In reply to Waiman LongQuoted 27 lines · Waiman Long
kernel/locking/qspinlock.c new file +26
-0+void queue_spin_lock_slowpath (struct qspinlock *lock,int qsval) +{ +unsigned int cpu_nr, qn_idx; +struct qnode *node, *next; +u32 prev_qcode, my_qcode; + +/* +* Get the queue node +*/ + cpu_nr =smp_processor_id (); + node =get_qnode (&qn_idx); + +/* +* It should never happen that all the queue nodes are being used. +*/ +BUG_ON (!node); + +/* +* Set up the new cpu code to be exchanged +*/ + my_qcode =queue_encode_qcode (cpu_nr, qn_idx); + +/* +* Initialize the queue node +*/ + node->wait = true;
kernel/locking/qspinlock.c line 297
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+ node->next =NULL ; + +/* +* The lock may be available at this point, try again if no task was +* waiting in the queue. +*/ +if (!(qsval >> _QCODE_OFFSET) &&queue_spin_trylock (lock)) { +put_qnode (); +return ; + }
Cosmetic, but probably "goto release_node" would be more consistent.
And I am wondering how much this "qsval >> _QCODE_OFFSET" check can help. Note that this is the only usage of this arg, perhaps it would be better to simply remove it and shrink the caller's code a bit? It is also used in 3/8, but we can read the "fresh" value of ->qlcode (trylock does this anyway), and perhaps it can actually help if it is already unlocked.
Quoted 19 lines
kernel/locking/qspinlock.c new file +19
-0+ prev_qcode =atomic_xchg (&lock->qlcode, my_qcode); +/* +* It is possible that we may accidentally steal the lock. If this is +* the case, we need to either release it if not the head of the queue +* or get the lock and be done with it. +*/ +if (unlikely (!(prev_qcode & _QSPINLOCK_LOCKED))) { +if (prev_qcode ==0 ) { +/* +* Got the lock since it is at the head of the queue +* Now try to atomically clear the queue code. +*/ +if (atomic_cmpxchg (&lock->qlcode, my_qcode, + _QSPINLOCK_LOCKED) == my_qcode) + goto release_node; +/* +* The cmpxchg fails only if one or more tasks +* are added to the queue. In this case, we need to +* notify the next one to be the head of the queue.
kernel/locking/qspinlock.c line 330
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+*/ + goto notify_next; + } +/* +* Accidentally steal the lock, release the lock and +* let the queue head get it. +*/ +queue_spin_unlock (lock); + }else + prev_qcode &= ~_QSPINLOCK_LOCKED;/* Clear the lock bit */
You know, actually I started this email because I thought that "goto notify_next" is wrong, I misread the patch as if this "goto" can happen even if prev_qcode != 0.
So feel free to ignore, all my comments are cosmetic/subjective, but to me it would be more clean/clear to rewrite the code above as
if (prev_qcode == 0) {
if (atomic_cmpxchg(..., _QSPINLOCK_LOCKED) == my_qcode)
goto release_node;
goto notify_next;
}
if (prev_qcode & _QSPINLOCK_LOCKED)
prev_qcode &= ~_QSPINLOCK_LOCKED;
else
queue_spin_unlock(lock);kernel/locking/qspinlock.c line 360
Quoted 10 lines
kernel/locking/qspinlock.c new file +10
-0+while (true) { +u32 qcode; +int retval; + + retval =queue_get_lock_qcode (lock, &qcode, my_qcode); +if (retval >0 ) + ;/* Lock not available yet */ +else if (retval <0 ) +/* Lock taken, can release the node & return */ + goto release_node;
I guess this is for 3/8which adds the optimized version of queue_get_lock_qcode(), so perhaps this "retval < 0" block can go into 3/8 as well.
kernel/locking/qspinlock.c line 367
Quoted 7 lines
kernel/locking/qspinlock.c new file +7
-0+else if (qcode != my_qcode) { +/* +* Just get the lock with other spinners waiting +* in the queue. +*/ +if (queue_spin_setlock (lock)) + goto notify_next;
OTOH, at least the generic (non-optimized) version of queue_spin_setlock() could probably accept "qcode" and avoid atomic_read() + _QSPINLOCK_LOCKED check.
But once again, please feel free to ignore.
Oleg.
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
In reply to Waiman LongForgot to ask...
kernel/locking/qspinlock.c line 388
Quoted 11 lines · Waiman Long
kernel/locking/qspinlock.c new file +10
-0+notify_next: +/* +* Wait, if needed, until the next one in queue set up the next field +*/ +while (!(next =ACCESS_ONCE (node->next))) +arch_mutex_cpu_relax (); +/* +* The next one in queue is now at the head +*/ +smp_store_release (&next->wait, false);
Do we really need smp_store_release()? It seems that we can rely on the control dependency here. And afaics there is no need to serialise this store with other changes in *lock, plus they all have mb's anyway.
Oleg.
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
Quoted 17 lines · Oleg Nesterov
Forgot to ask...
kernel/locking/qspinlock.c line 388
Quoted 10 lines · Waiman Long
kernel/locking/qspinlock.c new file +10
-0+notify_next: +/* +* Wait, if needed, until the next one in queue set up the next field +*/ +while (!(next =ACCESS_ONCE (node->next))) +arch_mutex_cpu_relax (); +/* +* The next one in queue is now at the head +*/ +smp_store_release (&next->wait, false);Do we really need
smp_store_release()? It seems that we can rely on the control dependency here. And afaics there is no need to serialise this store with other changes in *lock, plus they all have mb's anyway.
Quoted 2 lines
Oleg.
I am just following the current logic in the mcs_spin_unlock function. It is probably true that we don't need the release semantic in this particular case.
-Longman
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
In reply to Oleg NesterovQuoted 17 lines · Oleg Nesterov
Forgot to ask...
kernel/locking/qspinlock.c line 388
Quoted 10 lines · Waiman Long
kernel/locking/qspinlock.c new file +10
-0+notify_next: +/* +* Wait, if needed, until the next one in queue set up the next field +*/ +while (!(next =ACCESS_ONCE (node->next))) +arch_mutex_cpu_relax (); +/* +* The next one in queue is now at the head +*/ +smp_store_release (&next->wait, false);Do we really need
smp_store_release()? It seems that we can rely on the control dependency here. And afaics there is no need to serialise this store with other changes in *lock, plus they all have mb's anyway.
Quoted 2 lines
Oleg.
I am just following the current logic in the mcs_spin_unlock function. It is probably true that we don't need the release semantic in this particular case.
-Longman
Re: [PATCH v5 1/8] qspinlock: Introducing a 4-byte queue spinlock implementation
In reply to Waiman LongForgot to ask...
kernel/locking/qspinlock.c line 388
Quoted 11 lines · Waiman Long
kernel/locking/qspinlock.c new file +10
-0+notify_next: +/* +* Wait, if needed, until the next one in queue set up the next field +*/ +while (!(next =ACCESS_ONCE (node->next))) +arch_mutex_cpu_relax (); +/* +* The next one in queue is now at the head +*/ +smp_store_release (&next->wait, false);
Do we really need smp_store_release()? It seems that we can rely on the control dependency here. And afaics there is no need to serialise this store with other changes in *lock, plus they all have mb's anyway.
Oleg.
PATCH v5 1/8 qspinlock: Introducing a 4-byte queue spinlock implementation
In reply to Waiman LongThis patch introduces a new queue spinlock implementation that can serve as an alternative to the default ticket spinlock. Compared with the ticket spinlock, this queue spinlock should be almost as fair as the ticket spinlock. It has about the same speed in single-thread and it can be much faster in high contention situations. Only in light to moderate contention where the average queue depth is around 1-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.
The idea behind this spinlock implementation is the fact that spinlocks are acquired with preemption disabled. In other words, the process will not be migrated to another CPU while it is trying to get a spinlock. Ignoring interrupt handling, a CPU can only be contending in one spinlock at any one time. Of course, interrupt handler can try to acquire one spinlock while the interrupted user process is in the process of getting another spinlock. By allocating a set of per-cpu queue nodes and used them to form a waiting queue, we can encode the queue node address into a much smaller 16-bit size. Together with the 1-byte lock bit, this queue spinlock implementation will only need 4 bytes to hold all the information that it needs.
The current queue node address encoding of the 4-byte word is as follows:
Bits 0-7 : the locked byte
Bits 8-9 : queue node index in the per-cpu array (4 entries) Bits 10-31: cpu number + 1 (max cpus = 4M -1)
In the extremely unlikely case that all the queue node entries are used up, the current code will fall back to busy spinning without waiting in a queue with warning message.
For single-thread performance (no contention), a 256K lock/unlock loop was run on a 2.4Ghz Westmere x86-64 CPU. The following table shows the average time (in ns) for a single lock/unlock sequence (including the looping and timing overhead):
Lock Type Time (ns)
--------- ---------
Ticket spinlock 14.1
Queue spinlock 8.8
So the queue spinlock is much faster than the ticket spinlock, even though the overhead of locking and unlocking should be pretty small when there is no contention. The performance advantage is mainly due to the fact that ticket spinlock does a read-modify-write (add) instruction in unlock whereas queue spinlock only does a simple write in unlock which can be much faster in a pipelined CPU.
The AIM7 benchmark was run on a 8-socket 80-core DL980 with Westmere x86-64 CPUs with XFS filesystem on a ramdisk and HT off to evaluate the performance impact of this patch on a 3.13 kernel.
+------------+----------+-----------------+---------+ | Kernel | 3.13 JPM | 3.13 with | %Change | | | | qspinlock patch | | +------------+----------+-----------------+---------+ | 10-100 users | +------------+----------+-----------------+---------+ |custom | 357459 | 363109 | +1.58% | |dbase | 496847 | 498801 | +0.39% | |disk | 2925312 | 2771387 | -5.26% | |five_sec | 166612 | 169215 | +1.56% | |fserver | 382129 | 383279 | +0.30% | |high_systime| 16356 | 16380 | +0.15% | |short | 4521978 | 4257363 | -5.85% | +------------+----------+-----------------+---------+ | 200-1000 users | +------------+----------+-----------------+---------+ |custom | 449070 | 447711 | -0.30% | |dbase | 845029 | 853362 | +0.99% | |disk | 2725249 | 4892907 | +79.54% | |five_sec | 169410 | 170638 | +0.72% | |fserver | 489662 | 491828 | +0.44% | |high_systime| 142823 | 143790 | +0.68% | |short | 7435288 | 9016171 | +21.26% | +------------+----------+-----------------+---------+ | 1100-2000 users | +------------+----------+-----------------+---------+ |custom | 432470 | 432570 | +0.02% | |dbase | 889289 | 890026 | +0.08% | |disk | 2565138 | 5008732 | +95.26% | |five_sec | 169141 | 170034 | +0.53% | |fserver | 498569 | 500701 | +0.43% | |high_systime| 229913 | 245866 | +6.94% | |short | 8496794 | 8281918 | -2.53% | +------------+----------+-----------------+---------+
The workload with the most gain was the disk workload. Without the patch, the perf profile at 1500 users looked like:
26.19% reaim [kernel.kallsyms] [k] _raw_spin_lock
|--47.28%-- evict
|--46.87%-- inode_sb_list_add
|--1.24%-- xlog_cil_insert_items
|--0.68%-- __remove_inode_hash
|--0.67%-- inode_wait_for_writeback
--3.26%-- [...]
22.96% swapper [kernel.kallsyms] [k] cpu_idle_loop
5.56% reaim [kernel.kallsyms] [k] mutex_spin_on_owner
4.87% reaim [kernel.kallsyms] [k] update_cfs_rq_blocked_load
2.04% reaim [kernel.kallsyms] [k] mspin_lock
1.30% reaim [kernel.kallsyms] [k] memcpy
1.08% reaim [unknown] [.] 0x0000003c52009447There was pretty high spinlock contention on the inode_sb_list_lock and maybe the inode's i_lock.
With the patch, the perf profile at 1500 users became:
26.82% swapper [kernel.kallsyms] [k] cpu_idle_loop
4.66% reaim [kernel.kallsyms] [k] mutex_spin_on_owner
3.97% reaim [kernel.kallsyms] [k] update_cfs_rq_blocked_load
2.40% reaim [kernel.kallsyms] [k] queue_spin_lock_slowpath
|--88.31%-- _raw_spin_lock
| |--36.02%-- inode_sb_list_add
| |--35.09%-- evict
| |--16.89%-- xlog_cil_insert_items
| |--6.30%-- try_to_wake_up
| |--2.20%-- _xfs_buf_find
| |--0.75%-- __remove_inode_hash
| |--0.72%-- __mutex_lock_slowpath
| |--0.53%-- load_balance
|--6.02%-- _raw_spin_lock_irqsave
| |--74.75%-- down_trylock
| |--9.69%-- rcu_check_quiescent_state
| |--7.47%-- down
| |--3.57%-- up
| |--1.67%-- rwsem_wake
| |--1.00%-- remove_wait_queue
| |--0.56%-- pagevec_lru_move_fn
|--5.39%-- _raw_spin_lock_irq
| |--82.05%-- rwsem_down_read_failed
| |--10.48%-- rwsem_down_write_failed
| |--4.24%-- __down
| |--2.74%-- __schedule
--0.28%-- [...]
2.20% reaim [kernel.kallsyms] [k] memcpy
1.84% reaim [unknown] [.] 0x000000000041517b
1.77% reaim [kernel.kallsyms] [k] _raw_spin_lock
|--21.08%-- xlog_cil_insert_items
|--10.14%-- xfs_icsb_modify_counters
|--7.20%-- xfs_iget_cache_hit
|--6.56%-- inode_sb_list_add
|--5.49%-- _xfs_buf_find
|--5.25%-- evict
|--5.03%-- __remove_inode_hash
|--4.64%-- __mutex_lock_slowpath
|--3.78%-- selinux_inode_free_security
|--2.95%-- xfs_inode_is_filestream
|--2.35%-- try_to_wake_up
|--2.07%-- xfs_inode_set_reclaim_tag
|--1.52%-- list_lru_add
|--1.16%-- xfs_inode_clear_eofblocks_tag
:
1.30% reaim [kernel.kallsyms] [k] effective_load
1.27% reaim [kernel.kallsyms] [k] mspin_lock
1.10% reaim [kernel.kallsyms] [k] security_compute_sidOn the ext4 filesystem, the disk workload improved from 416281 JPM to 899101 JPM (+116%) with the patch. In this case, the contended spinlock is the mb_cache_spinlock.
--- include/asm-generic/qspinlock.h | 122 ++++++++++ include/asm-generic/qspinlock_types.h | 55 +++++ kernel/Kconfig.locks | 7 + kernel/locking/Makefile | 1 + kernel/locking/qspinlock.c | 393 +++++++++++++++++++++++++++++++++ 5 files changed, 578 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 +122 -0
@@ -0,0 +1,122 @@ +/* +* 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> + +/* +* External function declarations +*/ +extern void queue_spin_lock_slowpath (struct qspinlock *lock,int qsval); + +/** +* 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->qlcode) & _QSPINLOCK_LOCKED; +} + +/** +* queue_spin_value_unlocked - is the spinlock structure unlocked? +* @lock: queue spinlock structure +* Return: 1 if it is unlocked, 0 otherwise +*/ +static __always_inline int queue_spin_value_unlocked(struct qspinlock lock) +{ +return !(atomic_read (&lock.qlcode) & _QSPINLOCK_LOCKED); +} + +/** +* queue_spin_is_contended - check if the lock is contended +* @lock : Pointer to queue spinlock structure +* Return: 1 if lock contended, 0 otherwise +*/ +static __always_inline int queue_spin_is_contended(struct qspinlock *lock) +{ +return atomic_read (&lock->qlcode) & ~_QSPINLOCK_LOCK_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->qlcode) && + (atomic_cmpxchg (&lock->qlcode,0 , _QSPINLOCK_LOCKED) ==0 )) +return 1 ; +return 0 ; +} + +/** +* queue_spin_lock - acquire a queue spinlock +* @lock: Pointer to queue spinlock structure +*/ +static __always_inline void queue_spin_lock(struct qspinlock *lock) +{ +int qsval; + +/* +* To reduce memory access to only once for the cold cache case, +* a direct cmpxchg() is performed in the fastpath to optimize the +* uncontended case. The contended performance, however, may suffer +* a bit because of that. +*/ + qsval =atomic_cmpxchg (&lock->qlcode,0 , _QSPINLOCK_LOCKED); +if (likely (qsval ==0 )) +return ; +queue_spin_lock_slowpath (lock, qsval); +} + +#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) +{ +/* +* Use an atomic subtraction to clear the lock bit. +*/ +smp_mb__before_atomic_dec (); +atomic_sub (_QSPINLOCK_LOCKED, &lock->qlcode); +} +#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 +55 -0
55 lines not on this page · raw message
kernel/Kconfig.locks +7 -0
@@ -223,3 +223,10 @@ endif config MUTEX_SPIN_ON_OWNER def_bool y depends on SMP && !DEBUG_MUTEXES + +config ARCH_USE_QUEUE_SPINLOCK + bool + +config QUEUE_SPINLOCK + def_bool y if ARCH_USE_QUEUE_SPINLOCK + depends on SMP && !PARAVIRT_SPINLOCKS
kernel/locking/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_LOCKDEP) += lockdep_proc.o endif obj-$(CONFIG_SMP) += spinlock.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/qspinlock.c new file +393 -0
393 lines not on this page · raw message
Signature · 1 line
1.7.1
PATCH v5 2/8 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 | 41 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/spinlock.h | 5 ++++ arch/x86/include/asm/spinlock_types.h | 4 +++ 4 files changed, 51 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/qspinlock.h
arch/x86/Kconfig +1 -0
@@ -17,6 +17,7 @@ config X86_64 depends on 64BIT select X86_DEV_DMA_OPS select ARCH_USE_CMPXCHG_LOCKREF + select ARCH_USE_QUEUE_SPINLOCK ### Arch settings config X86
arch/x86/include/asm/qspinlock.h new file +41 -0
41 lines not on this page · raw message
arch/x86/include/asm/spinlock.h +5 -0
@@ -43,6 +43,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)
@@ -181,6 +185,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/rwlock.h>
Signature · 1 line
1.7.1
Re: [PATCH v5 2/8] qspinlock, x86: Enable x86-64 to use queue spinlock
arch/x86/include/asm/qspinlock.h line 16
Quoted 10 lines · Waiman Long
arch/x86/include/asm/qspinlock.h new file +9
-0+#define _ARCH_SUPPORTS_ATOMIC_8_16_BITS_OPS + +/* +* x86-64 specific queue spinlock union structure +*/ +union arch_qspinlock { +struct qspinlock slock; +u8 lock;/* Lock bit */ +};
And this enables the optimized version of queue_spin_setlock().
But why does it check ACCESS_ONCE(qlock->lock) == 0 ? This is called right after queue_get_lock_qcode() returns 0, this locked should be likely unlocked.
Oleg.
Re: [PATCH v5 2/8] qspinlock, x86: Enable x86-64 to use queue spinlock
Quoted 12 lines · Oleg Nesterov
arch/x86/include/asm/qspinlock.h line 16
Quoted 9 lines · Waiman Long
arch/x86/include/asm/qspinlock.h new file +9
-0+#define _ARCH_SUPPORTS_ATOMIC_8_16_BITS_OPS + +/* +* x86-64 specific queue spinlock union structure +*/ +union arch_qspinlock { +struct qspinlock slock; +u8 lock;/* Lock bit */ +};And this enables the optimized version of
queue_spin_setlock().
Quoted 6 lines
But why does it check
ACCESS_ONCE(qlock->lock)== 0 ? This is called right afterqueue_get_lock_qcode()returns 0, this locked should be likely unlocked.Oleg.
Yes, the read check in queue_spin_setlock is probably redundant. Will remove that.
-Longman
Re: [PATCH v5 2/8] qspinlock, x86: Enable x86-64 to use queue spinlock
In reply to Oleg NesterovQuoted 12 lines · Oleg Nesterov
arch/x86/include/asm/qspinlock.h line 16
Quoted 9 lines · Waiman Long
arch/x86/include/asm/qspinlock.h new file +9
-0+#define _ARCH_SUPPORTS_ATOMIC_8_16_BITS_OPS + +/* +* x86-64 specific queue spinlock union structure +*/ +union arch_qspinlock { +struct qspinlock slock; +u8 lock;/* Lock bit */ +};And this enables the optimized version of
queue_spin_setlock().
Quoted 6 lines
But why does it check
ACCESS_ONCE(qlock->lock)== 0 ? This is called right afterqueue_get_lock_qcode()returns 0, this locked should be likely unlocked.Oleg.
Yes, the read check in queue_spin_setlock is probably redundant. Will remove that.
-Longman
Re: [PATCH v5 2/8] qspinlock, x86: Enable x86-64 to use queue spinlock
In reply to Waiman Longarch/x86/include/asm/qspinlock.h line 16
Quoted 10 lines · Waiman Long
arch/x86/include/asm/qspinlock.h new file +9
-0+#define _ARCH_SUPPORTS_ATOMIC_8_16_BITS_OPS + +/* +* x86-64 specific queue spinlock union structure +*/ +union arch_qspinlock { +struct qspinlock slock; +u8 lock;/* Lock bit */ +};
And this enables the optimized version of queue_spin_setlock().
But why does it check ACCESS_ONCE(qlock->lock) == 0 ? This is called right after queue_get_lock_qcode() returns 0, this locked should be likely unlocked.
Oleg.
PATCH v5 2/8 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 | 41 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/spinlock.h | 5 ++++ arch/x86/include/asm/spinlock_types.h | 4 +++ 4 files changed, 51 insertions(+), 0 deletions(-) create mode 100644 arch/x86/include/asm/qspinlock.h
arch/x86/Kconfig +1 -0
@@ -17,6 +17,7 @@ config X86_64 depends on 64BIT select X86_DEV_DMA_OPS select ARCH_USE_CMPXCHG_LOCKREF + select ARCH_USE_QUEUE_SPINLOCK ### Arch settings config X86
arch/x86/include/asm/qspinlock.h new file +41 -0
41 lines not on this page · raw message
arch/x86/include/asm/spinlock.h +5 -0
@@ -43,6 +43,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)
@@ -181,6 +185,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/rwlock.h>
Signature · 1 line
1.7.1
PATCH v5 3/8 qspinlock, x86: Add x86 specific optimization for 2 contending tasks
In reply to Waiman LongA major problem with the queue spinlock patch is its performance at low contention level (2-4 contending tasks) where it is slower than the corresponding ticket spinlock code path. The following table shows the execution time (in ms) of a micro-benchmark where 5M iterations of the lock/unlock cycles were run on a 10-core Westere-EX CPU with 2 different types loads - standalone (lock and protected data in different cachelines) and embedded (lock and protected data in the same cacheline).
[Standalone/Embedded]
# of tasks Ticket lock Queue lock %Change
---------- ----------- ---------- -------
1 135/111 135/102 0%/-8%
2 732/950 1315/1573 +80%/+66%
3 1827/1783 2372/2428 +30%/+36%
4 2689/2725 2934/2934 +9%/+8%
5 3736/3748 3658/3652 -2%/-3%
6 4942/4984 4434/4428 -10%/-11%
7 6304/6319 5176/5163 -18%/-18%
8 7736/7629 5955/5944 -23%/-22%It can be seen that the performance degradation is particular bad with 2 and 3 contending tasks. To reduce that performance deficit at low contention level, a special x86 specific optimized code path for 2 contending tasks was added. This special code path will only be activated with less than 16K of configured CPUs because it uses a byte in the 32-bit lock word to hold a waiting bit for the 2nd contending tasks instead of queuing the waiting task in the queue.
With the change, the performance data became:
[Standalone/Embedded]
# of tasks Ticket lock Queue lock %Change
---------- ----------- ---------- -------
2 732/950 523/528 -29%/-44%
3 1827/1783 2366/2384 +30%/+34%The queue spinlock code path is now a bit faster with 2 contending tasks. There is also a very slight improvement with 3 contending tasks.
The performance of the optimized code path can vary depending on which of the several different code paths is taken. It is also not as fair as the ticket spinlock and some variations on the execution times of the 2 contending tasks. Testing with different pairs of cores within the same CPUs shows an execution time that varies from 400ms to 1194ms. The ticket spinlock code also shows a variation of 718-1146ms which is probably due to the CPU topology within a socket.
In a multi-socketed server, the optimized code path also seems to produce a big performance improvement in cross-node contention traffic at low contention level. The table below show the performance with 1 contending task per node:
[Standalone]
# of nodes Ticket lock Queue lock %Change
---------- ----------- ---------- -------
1 135 135 0%
2 4452 528 -88%
3 10767 2369 -78%
4 20835 2921 -86%The micro-benchmark was also run on a 4-core Ivy-Bridge PC. The table below shows the collected performance data:
[Standalone/Embedded]
# of tasks Ticket lock Queue lock %Change
---------- ----------- ---------- -------
1 197/178 181/150 -8%/-16%
2 1109/928 435-1417/697-2125
3 1836/1702 1372-3112/1379-3138
4 2717/2429 1842-4158/1846-4170The performance of the queue lock patch varied from run to run whereas the performance of the ticket lock was more consistent. The queue lock figures above were the range of values that were reported.
This optimization can also be easily used by other architectures as long as they support 8 and 16 bits atomic operations.
--- arch/x86/include/asm/qspinlock.h | 20 ++++- include/asm-generic/qspinlock_types.h | 8 ++- kernel/locking/qspinlock.c | 192 ++++++++++++++++++++++++++++++++- 3 files changed, 215 insertions(+), 5 deletions(-)
arch/x86/include/asm/qspinlock.h +19 -1
@@ -7,12 +7,30 @@#define _ARCH_SUPPORTS_ATOMIC_8_16_BITS_OPS +#define smp_u8_store_release (p, v) \ +do { \ + barrier(); \ + ACCESS_ONCE(*p) = (v); \ +} while (0) + +/* +* As the qcode will be accessed as a 16-bit word, no offset is needed +*/ +#define _QCODE_VAL_OFFSET 0 +/* * x86-64 specific queue spinlock union structure +* Besides the slock and lock fields, the other fields are only +* valid with less than 16K CPUs. */ union arch_qspinlock {struct qspinlock slock; -u8 lock;/* Lock bit */ +struct { +u8 lock;/* Lock bit */ +u8 wait;/* Waiting bit */ +u16 qcode;/* Queue code */ + }; +u16 lock_wait;/* Lock and wait bits */ };#define queue_spin_unlock queue_spin_unlock
include/asm-generic/qspinlock_types.h +7 -1
@@ -48,7 +48,13 @@ typedef struct qspinlock {atomic_t qlcode;/* Lock + queue code */ } arch_spinlock_t; -#define _QCODE_OFFSET 8 +#if CONFIG_NR_CPUS >= (1 <<14 ) +# define _Q_MANY_CPUS +# define _QCODE_OFFSET 8 +#else +# define _QCODE_OFFSET 16 +#endif +#define _QSPINLOCK_LOCKED 1U#define _QSPINLOCK_LOCK_MASK 0xff
kernel/locking/qspinlock.c +189 -3
234 lines not on this page · raw message
Signature · 1 line
1.7.1
Re: [PATCH v5 3/8] qspinlock, x86: Add x86 specific optimization for 2 contending tasks
kernel/locking/qspinlock.c static __always_inline int queue_spin_setlock(struct qspinlock *lock) · line 320
Quoted 7 lines · Waiman Long
kernel/locking/qspinlock.c +1
-1@@ -144,7 +317,7 @@ static __always_inline int queue_spin_setlock(struct qspinlock *lock)int qlcode =atomic_read (lock->qlcode);if (!(qlcode & _QSPINLOCK_LOCKED) && (atomic_cmpxchg (&lock->qlcode, - qlcode, qlcode|_QSPINLOCK_LOCKED) == qlcode)) + qlcode, code|_QSPINLOCK_LOCKED) == qlcode))
Hmm. didn't read the patch, but this change looks like accidental typo...
Oleg.
Re: [PATCH v5 3/8] qspinlock, x86: Add x86 specific optimization for 2 contending tasks
Quoted 11 lines · Oleg Nesterov
kernel/locking/qspinlock.c static __always_inline int queue_spin_setlock(struct qspinlock *lock) · line 320
Quoted 6 lines · Waiman Long
kernel/locking/qspinlock.c +1
-1@@ -144,7 +317,7 @@ static __always_inline int queue_spin_setlock(struct qspinlock *lock)int qlcode =atomic_read (lock->qlcode);if (!(qlcode& _QSPINLOCK_LOCKED)&& (atomic_cmpxchg (&lock->qlcode, - qlcode, qlcode|_QSPINLOCK_LOCKED) == qlcode)) + qlcode, code|_QSPINLOCK_LOCKED) == qlcode))Hmm. didn't read the patch, but this change looks like accidental typo...
Oleg.
Thank for catching that typo error. That generic code wasn't compiled in and so I missed it. I will fix that in the next version.
-Longman
Re: [PATCH v5 3/8] qspinlock, x86: Add x86 specific optimization for 2 contending tasks
In reply to Oleg NesterovQuoted 11 lines · Oleg Nesterov
kernel/locking/qspinlock.c static __always_inline int queue_spin_setlock(struct qspinlock *lock) · line 320
Quoted 6 lines · Waiman Long
kernel/locking/qspinlock.c +1
-1@@ -144,7 +317,7 @@ static __always_inline int queue_spin_setlock(struct qspinlock *lock)int qlcode =atomic_read (lock->qlcode);if (!(qlcode& _QSPINLOCK_LOCKED)&& (atomic_cmpxchg (&lock->qlcode, - qlcode, qlcode|_QSPINLOCK_LOCKED) == qlcode)) + qlcode, code|_QSPINLOCK_LOCKED) == qlcode))Hmm. didn't read the patch, but this change looks like accidental typo...
Oleg.
Thank for catching that typo error. That generic code wasn't compiled in and so I missed it. I will fix that in the next version.
-Longman
Re: [PATCH v5 3/8] qspinlock, x86: Add x86 specific optimization for 2 contending tasks
In reply to Waiman Longkernel/locking/qspinlock.c static __always_inline int queue_spin_setlock(struct qspinlock *lock) · line 320
Quoted 7 lines · Waiman Long
kernel/locking/qspinlock.c +1
-1@@ -144,7 +317,7 @@ static __always_inline int queue_spin_setlock(struct qspinlock *lock)int qlcode =atomic_read (lock->qlcode);if (!(qlcode & _QSPINLOCK_LOCKED) && (atomic_cmpxchg (&lock->qlcode, - qlcode, qlcode|_QSPINLOCK_LOCKED) == qlcode)) + qlcode, code|_QSPINLOCK_LOCKED) == qlcode))
Hmm. didn't read the patch, but this change looks like accidental typo...
Oleg.
PATCH v5 3/8 qspinlock, x86: Add x86 specific optimization for 2 contending tasks
In reply to Waiman LongA major problem with the queue spinlock patch is its performance at low contention level (2-4 contending tasks) where it is slower than the corresponding ticket spinlock code path. The following table shows the execution time (in ms) of a micro-benchmark where 5M iterations of the lock/unlock cycles were run on a 10-core Westere-EX CPU with 2 different types loads - standalone (lock and protected data in different cachelines) and embedded (lock and protected data in the same cacheline).
[Standalone/Embedded]
# of tasks Ticket lock Queue lock %Change
---------- ----------- ---------- -------
1 135/111 135/102 0%/-8%
2 732/950 1315/1573 +80%/+66%
3 1827/1783 2372/2428 +30%/+36%
4 2689/2725 2934/2934 +9%/+8%
5 3736/3748 3658/3652 -2%/-3%
6 4942/4984 4434/4428 -10%/-11%
7 6304/6319 5176/5163 -18%/-18%
8 7736/7629 5955/5944 -23%/-22%It can be seen that the performance degradation is particular bad with 2 and 3 contending tasks. To reduce that performance deficit at low contention level, a special x86 specific optimized code path for 2 contending tasks was added. This special code path will only be activated with less than 16K of configured CPUs because it uses a byte in the 32-bit lock word to hold a waiting bit for the 2nd contending tasks instead of queuing the waiting task in the queue.
With the change, the performance data became:
[Standalone/Embedded]
# of tasks Ticket lock Queue lock %Change
---------- ----------- ---------- -------
2 732/950 523/528 -29%/-44%
3 1827/1783 2366/2384 +30%/+34%The queue spinlock code path is now a bit faster with 2 contending tasks. There is also a very slight improvement with 3 contending tasks.
The performance of the optimized code path can vary depending on which of the several different code paths is taken. It is also not as fair as the ticket spinlock and some variations on the execution times of the 2 contending tasks. Testing with different pairs of cores within the same CPUs shows an execution time that varies from 400ms to 1194ms. The ticket spinlock code also shows a variation of 718-1146ms which is probably due to the CPU topology within a socket.
In a multi-socketed server, the optimized code path also seems to produce a big performance improvement in cross-node contention traffic at low contention level. The table below show the performance with 1 contending task per node:
[Standalone]
# of nodes Ticket lock Queue lock %Change
---------- ----------- ---------- -------
1 135 135 0%
2 4452 528 -88%
3 10767 2369 -78%
4 20835 2921 -86%The micro-benchmark was also run on a 4-core Ivy-Bridge PC. The table below shows the collected performance data:
[Standalone/Embedded]
# of tasks Ticket lock Queue lock %Change
---------- ----------- ---------- -------
1 197/178 181/150 -8%/-16%
2 1109/928 435-1417/697-2125
3 1836/1702 1372-3112/1379-3138
4 2717/2429 1842-4158/1846-4170The performance of the queue lock patch varied from run to run whereas the performance of the ticket lock was more consistent. The queue lock figures above were the range of values that were reported.
This optimization can also be easily used by other architectures as long as they support 8 and 16 bits atomic operations.
--- arch/x86/include/asm/qspinlock.h | 20 ++++- include/asm-generic/qspinlock_types.h | 8 ++- kernel/locking/qspinlock.c | 192 ++++++++++++++++++++++++++++++++- 3 files changed, 215 insertions(+), 5 deletions(-)
arch/x86/include/asm/qspinlock.h +19 -1
@@ -7,12 +7,30 @@#define _ARCH_SUPPORTS_ATOMIC_8_16_BITS_OPS +#define smp_u8_store_release (p, v) \ +do { \ + barrier(); \ + ACCESS_ONCE(*p) = (v); \ +} while (0) + +/* +* As the qcode will be accessed as a 16-bit word, no offset is needed +*/ +#define _QCODE_VAL_OFFSET 0 +/* * x86-64 specific queue spinlock union structure +* Besides the slock and lock fields, the other fields are only +* valid with less than 16K CPUs. */ union arch_qspinlock {struct qspinlock slock; -u8 lock;/* Lock bit */ +struct { +u8 lock;/* Lock bit */ +u8 wait;/* Waiting bit */ +u16 qcode;/* Queue code */ + }; +u16 lock_wait;/* Lock and wait bits */ };#define queue_spin_unlock queue_spin_unlock
include/asm-generic/qspinlock_types.h +7 -1
@@ -48,7 +48,13 @@ typedef struct qspinlock {atomic_t qlcode;/* Lock + queue code */ } arch_spinlock_t; -#define _QCODE_OFFSET 8 +#if CONFIG_NR_CPUS >= (1 <<14 ) +# define _Q_MANY_CPUS +# define _QCODE_OFFSET 8 +#else +# define _QCODE_OFFSET 16 +#endif +#define _QSPINLOCK_LOCKED 1U#define _QSPINLOCK_LOCK_MASK 0xff
kernel/locking/qspinlock.c +189 -3
234 lines not on this page · raw message
Signature · 1 line
1.7.1
RFC PATCH v5 4/8 pvqspinlock, x86: Allow unfair spinlock in a real PV environment
In reply to Waiman LongLocking is always an issue in a virtualized environment as the virtual CPU that is waiting on a lock may get scheduled out and hence block any progress in lock acquisition even when the lock has been freed.
One solution to this problem is to allow unfair lock in a para-virtualized environment. In this case, a new lock acquirer can come and steal the lock if the next-in-line CPU to get the lock is scheduled out. Unfair lock in a native environment is generally not a good idea as there is a possibility of lock starvation for a heavily contended lock.
This patch add a new configuration option for the x86 architecture to enable the use of unfair queue spinlock (PARAVIRT_UNFAIR_LOCKS) in a real para-virtualized guest. A jump label (paravirt_unfairlocks_enabled) is used to switch between a fair and an unfair version of the spinlock code. This jump label will only be enabled in a real PV guest.
Enabling this configuration feature decreases the performance of an uncontended lock-unlock operation by about 1-2%.
--- arch/x86/Kconfig | 11 +++++ arch/x86/include/asm/qspinlock.h | 74 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/paravirt-spinlocks.c | 7 +++ 4 files changed, 93 insertions(+), 0 deletions(-)
arch/x86/Kconfig +11 -0
@@ -645,6 +645,17 @@ config PARAVIRT_SPINLOCKS If you are unsure how to answer this question, answer Y. +config PARAVIRT_UNFAIR_LOCKS + bool "Enable unfair locks in a para-virtualized guest" + depends on PARAVIRT && SMP && QUEUE_SPINLOCK + depends on !CONFIG_X86_OOSTORE && !CONFIG_X86_PPRO_FENCE + ---help--- + This changes the kernel to use unfair locks in a real + para-virtualized guest system. This will help performance + in most cases. However, there is a possibility of lock + starvation on a heavily contended lock especially in a + large guest with many virtual CPUs. + source "arch/x86/xen/Kconfig" config KVM_GUEST
arch/x86/include/asm/qspinlock.h +74 -0
78 lines not on this page · raw message
arch/x86/kernel/Makefile +1 -0
@@ -88,6 +88,7 @@ obj-$(CONFIG_DEBUG_NMI_SELFTEST) += nmi_selftest.o obj-$(CONFIG_KVM_GUEST) += kvm.o kvmclock.o obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt_patch_$(BITS).o obj-$(CONFIG_PARAVIRT_SPINLOCKS)+= paravirt-spinlocks.o +obj-$(CONFIG_PARAVIRT_UNFAIR_LOCKS)+= paravirt-spinlocks.o obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/paravirt-spinlocks.c +7 -0
@@ -8,6 +8,7 @@#include <asm/paravirt.h> +#ifdef CONFIG_PARAVIRT_SPINLOCKS struct pv_lock_ops pv_lock_ops = { #ifdef CONFIG_SMP .lock_spinning = __PV_IS_CALLEE_SAVE(paravirt_nop),
@@ -18,3 +19,9 @@ EXPORT_SYMBOL(pv_lock_ops);struct static_key paravirt_ticketlocks_enabled =STATIC_KEY_INIT_FALSE ;EXPORT_SYMBOL (paravirt_ticketlocks_enabled); +#endif + +#ifdef CONFIG_PARAVIRT_UNFAIR_LOCKS +struct static_key paravirt_unfairlocks_enabled =STATIC_KEY_INIT_FALSE ; +EXPORT_SYMBOL (paravirt_unfairlocks_enabled); +#endif
Signature · 1 line
1.7.1
RFC PATCH v5 4/8 pvqspinlock, x86: Allow unfair spinlock in a real PV environment
In reply to Waiman LongLocking is always an issue in a virtualized environment as the virtual CPU that is waiting on a lock may get scheduled out and hence block any progress in lock acquisition even when the lock has been freed.
One solution to this problem is to allow unfair lock in a para-virtualized environment. In this case, a new lock acquirer can come and steal the lock if the next-in-line CPU to get the lock is scheduled out. Unfair lock in a native environment is generally not a good idea as there is a possibility of lock starvation for a heavily contended lock.
This patch add a new configuration option for the x86 architecture to enable the use of unfair queue spinlock (PARAVIRT_UNFAIR_LOCKS) in a real para-virtualized guest. A jump label (paravirt_unfairlocks_enabled) is used to switch between a fair and an unfair version of the spinlock code. This jump label will only be enabled in a real PV guest.
Enabling this configuration feature decreases the performance of an uncontended lock-unlock operation by about 1-2%.
--- arch/x86/Kconfig | 11 +++++ arch/x86/include/asm/qspinlock.h | 74 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/paravirt-spinlocks.c | 7 +++ 4 files changed, 93 insertions(+), 0 deletions(-)
arch/x86/Kconfig +11 -0
@@ -645,6 +645,17 @@ config PARAVIRT_SPINLOCKS If you are unsure how to answer this question, answer Y. +config PARAVIRT_UNFAIR_LOCKS + bool "Enable unfair locks in a para-virtualized guest" + depends on PARAVIRT && SMP && QUEUE_SPINLOCK + depends on !CONFIG_X86_OOSTORE && !CONFIG_X86_PPRO_FENCE + ---help--- + This changes the kernel to use unfair locks in a real + para-virtualized guest system. This will help performance + in most cases. However, there is a possibility of lock + starvation on a heavily contended lock especially in a + large guest with many virtual CPUs. + source "arch/x86/xen/Kconfig" config KVM_GUEST
arch/x86/include/asm/qspinlock.h +74 -0
78 lines not on this page · raw message
arch/x86/kernel/Makefile +1 -0
@@ -88,6 +88,7 @@ obj-$(CONFIG_DEBUG_NMI_SELFTEST) += nmi_selftest.o obj-$(CONFIG_KVM_GUEST) += kvm.o kvmclock.o obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt_patch_$(BITS).o obj-$(CONFIG_PARAVIRT_SPINLOCKS)+= paravirt-spinlocks.o +obj-$(CONFIG_PARAVIRT_UNFAIR_LOCKS)+= paravirt-spinlocks.o obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o
arch/x86/kernel/paravirt-spinlocks.c +7 -0
@@ -8,6 +8,7 @@#include <asm/paravirt.h> +#ifdef CONFIG_PARAVIRT_SPINLOCKS struct pv_lock_ops pv_lock_ops = { #ifdef CONFIG_SMP .lock_spinning = __PV_IS_CALLEE_SAVE(paravirt_nop),
@@ -18,3 +19,9 @@ EXPORT_SYMBOL(pv_lock_ops);struct static_key paravirt_ticketlocks_enabled =STATIC_KEY_INIT_FALSE ;EXPORT_SYMBOL (paravirt_ticketlocks_enabled); +#endif + +#ifdef CONFIG_PARAVIRT_UNFAIR_LOCKS +struct static_key paravirt_unfairlocks_enabled =STATIC_KEY_INIT_FALSE ; +EXPORT_SYMBOL (paravirt_unfairlocks_enabled); +#endif
Signature · 1 line
1.7.1
RFC PATCH v5 5/8 pvqspinlock, x86: Enable unfair queue spinlock in a KVM guest
In reply to Waiman LongThis patch adds a KVM init function to activate the unfair queue spinlock in a KVM guest when the PARAVIRT_UNFAIR_LOCKS kernel config option is selected.
--- arch/x86/kernel/kvm.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
arch/x86/kernel/kvm.c +17 -0
@@ -826,3 +826,20 @@ static __init int kvm_spinlock_init_jump(void)early_initcall (kvm_spinlock_init_jump);#endif /* CONFIG_PARAVIRT_SPINLOCKS */ + +#ifdef CONFIG_PARAVIRT_UNFAIR_LOCKS +/* +* Enable unfair lock if running in a real para-virtualized environment +*/ +static __init int kvm_unfair_locks_init_jump (void ) +{ +if (!kvm_para_available ()) +return 0 ; + +static_key_slow_inc (¶virt_unfairlocks_enabled); +printk (KERN_INFO "KVM setup unfair spinlock\n" ); + +return 0 ; +} +early_initcall (kvm_unfair_locks_init_jump); +#endif
Signature · 1 line
1.7.1
RFC PATCH v5 5/8 pvqspinlock, x86: Enable unfair queue spinlock in a KVM guest
In reply to Waiman LongThis patch adds a KVM init function to activate the unfair queue spinlock in a KVM guest when the PARAVIRT_UNFAIR_LOCKS kernel config option is selected.
--- arch/x86/kernel/kvm.c | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-)
arch/x86/kernel/kvm.c +17 -0
@@ -826,3 +826,20 @@ static __init int kvm_spinlock_init_jump(void)early_initcall (kvm_spinlock_init_jump);#endif /* CONFIG_PARAVIRT_SPINLOCKS */ + +#ifdef CONFIG_PARAVIRT_UNFAIR_LOCKS +/* +* Enable unfair lock if running in a real para-virtualized environment +*/ +static __init int kvm_unfair_locks_init_jump (void ) +{ +if (!kvm_para_available ()) +return 0 ; + +static_key_slow_inc (¶virt_unfairlocks_enabled); +printk (KERN_INFO "KVM setup unfair spinlock\n" ); + +return 0 ; +} +early_initcall (kvm_unfair_locks_init_jump); +#endif
Signature · 1 line
1.7.1
RFC PATCH v5 6/8 pvqspinlock, 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
@@ -40,7 +40,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
@@ -151,7 +151,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
@@ -818,7 +818,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
@@ -17,8 +17,8 @@ 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);#endif #ifdef CONFIG_PARAVIRT_UNFAIR_LOCKS
arch/x86/xen/spinlock.c +1 -1
@@ -290,7 +290,7 @@ static __init int xen_init_spinlocks_jump(void)if (!xen_pvspin)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
RFC PATCH v5 6/8 pvqspinlock, 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
@@ -40,7 +40,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
@@ -151,7 +151,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
@@ -818,7 +818,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
@@ -17,8 +17,8 @@ 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);#endif #ifdef CONFIG_PARAVIRT_UNFAIR_LOCKS
arch/x86/xen/spinlock.c +1 -1
@@ -290,7 +290,7 @@ static __init int xen_init_spinlocks_jump(void)if (!xen_pvspin)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
RFC PATCH v5 7/8 pvqspinlock, x86: Add qspinlock para-virtualization support
In reply to Waiman LongThis patch adds para-virtualization support to the queue spinlock code by enabling the queue head to kick the lock holder CPU, if known, in when the lock isn't released for a certain amount of time. It also enables the mutual monitoring of the queue head CPU and the following node CPU in the queue to make sure that their CPUs will stay scheduled in.
--- arch/x86/include/asm/paravirt.h | 9 ++- arch/x86/include/asm/paravirt_types.h | 12 +++ arch/x86/include/asm/pvqspinlock.h | 176 +++++++++++++++++++++++++++++++++ arch/x86/kernel/paravirt-spinlocks.c | 4 + kernel/locking/qspinlock.c | 41 +++++++- 5 files changed, 235 insertions(+), 7 deletions(-) create mode 100644 arch/x86/include/asm/pvqspinlock.h
arch/x86/include/asm/paravirt.h +7 -2
@@ -711,7 +711,12 @@ 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 __queue_kick_cpu(int cpu, enum pv_kick_type type) +{ + PVOP_VCALL2(pv_lock_ops.kick_cpu, cpu, type); +} +#else static __always_inline void __ticket_lock_spinning(struct arch_spinlock *lock, __ticket_t ticket) {
@@ -723,7 +728,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, {PVOP_VCALL2 (pv_lock_ops.unlock_kick, lock, ticket); } - +#endif #endif #ifdef CONFIG_X86_32
arch/x86/include/asm/paravirt_types.h +12 -0
@@ -333,9 +333,21 @@ struct arch_spinlock;typedef u16 __ticket_t ;#endif +#ifdef CONFIG_QUEUE_SPINLOCK +enum pv_kick_type { +PV_KICK_LOCK_HOLDER , +PV_KICK_QUEUE_HEAD , +PV_KICK_NEXT_NODE +}; +#endif +struct pv_lock_ops { +#ifdef CONFIG_QUEUE_SPINLOCK +void (*kick_cpu)(int cpu,enum pv_kick_type ); +#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 +176 -0
176 lines not on this page · raw message
arch/x86/kernel/paravirt-spinlocks.c +4 -0
@@ -11,9 +11,13 @@#ifdef CONFIG_PARAVIRT_SPINLOCKS struct pv_lock_ops pv_lock_ops = {#ifdef CONFIG_SMP +#ifdef CONFIG_QUEUE_SPINLOCK + .kick_cpu = 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 +36 -5
88 lines not on this page · raw message
Signature · 1 line
1.7.1
RFC PATCH v5 7/8 pvqspinlock, x86: Add qspinlock para-virtualization support
In reply to Waiman LongThis patch adds para-virtualization support to the queue spinlock code by enabling the queue head to kick the lock holder CPU, if known, in when the lock isn't released for a certain amount of time. It also enables the mutual monitoring of the queue head CPU and the following node CPU in the queue to make sure that their CPUs will stay scheduled in.
--- arch/x86/include/asm/paravirt.h | 9 ++- arch/x86/include/asm/paravirt_types.h | 12 +++ arch/x86/include/asm/pvqspinlock.h | 176 +++++++++++++++++++++++++++++++++ arch/x86/kernel/paravirt-spinlocks.c | 4 + kernel/locking/qspinlock.c | 41 +++++++- 5 files changed, 235 insertions(+), 7 deletions(-) create mode 100644 arch/x86/include/asm/pvqspinlock.h
arch/x86/include/asm/paravirt.h +7 -2
@@ -711,7 +711,12 @@ 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 __queue_kick_cpu(int cpu, enum pv_kick_type type) +{ + PVOP_VCALL2(pv_lock_ops.kick_cpu, cpu, type); +} +#else static __always_inline void __ticket_lock_spinning(struct arch_spinlock *lock, __ticket_t ticket) {
@@ -723,7 +728,7 @@ static __always_inline void __ticket_unlock_kick(struct arch_spinlock *lock, {PVOP_VCALL2 (pv_lock_ops.unlock_kick, lock, ticket); } - +#endif #endif #ifdef CONFIG_X86_32
arch/x86/include/asm/paravirt_types.h +12 -0
@@ -333,9 +333,21 @@ struct arch_spinlock;typedef u16 __ticket_t ;#endif +#ifdef CONFIG_QUEUE_SPINLOCK +enum pv_kick_type { +PV_KICK_LOCK_HOLDER , +PV_KICK_QUEUE_HEAD , +PV_KICK_NEXT_NODE +}; +#endif +struct pv_lock_ops { +#ifdef CONFIG_QUEUE_SPINLOCK +void (*kick_cpu)(int cpu,enum pv_kick_type ); +#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 +176 -0
176 lines not on this page · raw message
arch/x86/kernel/paravirt-spinlocks.c +4 -0
@@ -11,9 +11,13 @@#ifdef CONFIG_PARAVIRT_SPINLOCKS struct pv_lock_ops pv_lock_ops = {#ifdef CONFIG_SMP +#ifdef CONFIG_QUEUE_SPINLOCK + .kick_cpu = 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 +36 -5
88 lines not on this page · raw message
Signature · 1 line
1.7.1
RFC PATCH v5 8/8 pvqspinlock, x86: Enable KVM to use qspinlock's PV support
In reply to Waiman LongThis patch enables KVM to use the queue spinlock's PV support code when the PARAVIRT_SPINLOCKS kernel config option is set. However, PV support for Xen is not ready yet and so the queue spinlock will still have to be disabled when PARAVIRT_SPINLOCKS config option is on with Xen.
--- arch/x86/kernel/kvm.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/Kconfig.locks | 2 +- 2 files changed, 55 insertions(+), 1 deletions(-)
arch/x86/kernel/kvm.c +54 -0
@@ -568,6 +568,7 @@ static void kvm_kick_cpu(int cpu)kvm_hypercall2 (KVM_HC_KICK_CPU , flags, apicid); } +#ifndef CONFIG_QUEUE_SPINLOCK enum kvm_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -795,6 +796,55 @@ 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 lh_kick_stats;/* Lock holder kick count */ +static u32 qh_kick_stats;/* Queue head kick count */ +static u32 nn_kick_stats;/* Next node kick count */ + +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 ("lh_kick_stats" ,0644 , d_spin_debug, &lh_kick_stats); +debugfs_create_u32 ("qh_kick_stats" ,0644 , d_spin_debug, &qh_kick_stats); +debugfs_create_u32 ("nn_kick_stats" ,0644 , d_spin_debug, &nn_kick_stats); + +return 0 ; +} + +static inline void inc_kick_stats (enum pv_kick_type type) +{ +if (type ==PV_KICK_LOCK_HOLDER ) +add_smp (&lh_kick_stats,1 ); +else if (type ==PV_KICK_QUEUE_HEAD ) +add_smp (&qh_kick_stats,1 ); +else +add_smp (&nn_kick_stats,1 ); +} +fs_initcall (kvm_spinlock_debugfs); + +#else /* CONFIG_KVM_DEBUG_FS */ +static inline void inc_kick_stats (enum pv_kick_type type) +{ +} +#endif /* CONFIG_KVM_DEBUG_FS */ + +static void kvm_kick_cpu_type (int cpu,enum pv_kick_type type) +{ +kvm_kick_cpu (cpu); +inc_kick_stats (type); +} +#endif /* !CONFIG_QUEUE_SPINLOCK */ /* * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
@@ -807,8 +857,12 @@ 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 = kvm_kick_cpu_type; +#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
@@ -229,4 +229,4 @@ 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)
Signature · 1 line
1.7.1
RFC PATCH v5 8/8 pvqspinlock, x86: Enable KVM to use qspinlock's PV support
In reply to Waiman LongThis patch enables KVM to use the queue spinlock's PV support code when the PARAVIRT_SPINLOCKS kernel config option is set. However, PV support for Xen is not ready yet and so the queue spinlock will still have to be disabled when PARAVIRT_SPINLOCKS config option is on with Xen.
--- arch/x86/kernel/kvm.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ kernel/Kconfig.locks | 2 +- 2 files changed, 55 insertions(+), 1 deletions(-)
arch/x86/kernel/kvm.c +54 -0
@@ -568,6 +568,7 @@ static void kvm_kick_cpu(int cpu)kvm_hypercall2 (KVM_HC_KICK_CPU , flags, apicid); } +#ifndef CONFIG_QUEUE_SPINLOCK enum kvm_contention_stat {TAKEN_SLOW , TAKEN_SLOW_PICKUP,
@@ -795,6 +796,55 @@ 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 lh_kick_stats;/* Lock holder kick count */ +static u32 qh_kick_stats;/* Queue head kick count */ +static u32 nn_kick_stats;/* Next node kick count */ + +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 ("lh_kick_stats" ,0644 , d_spin_debug, &lh_kick_stats); +debugfs_create_u32 ("qh_kick_stats" ,0644 , d_spin_debug, &qh_kick_stats); +debugfs_create_u32 ("nn_kick_stats" ,0644 , d_spin_debug, &nn_kick_stats); + +return 0 ; +} + +static inline void inc_kick_stats (enum pv_kick_type type) +{ +if (type ==PV_KICK_LOCK_HOLDER ) +add_smp (&lh_kick_stats,1 ); +else if (type ==PV_KICK_QUEUE_HEAD ) +add_smp (&qh_kick_stats,1 ); +else +add_smp (&nn_kick_stats,1 ); +} +fs_initcall (kvm_spinlock_debugfs); + +#else /* CONFIG_KVM_DEBUG_FS */ +static inline void inc_kick_stats (enum pv_kick_type type) +{ +} +#endif /* CONFIG_KVM_DEBUG_FS */ + +static void kvm_kick_cpu_type (int cpu,enum pv_kick_type type) +{ +kvm_kick_cpu (cpu); +inc_kick_stats (type); +} +#endif /* !CONFIG_QUEUE_SPINLOCK */ /* * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
@@ -807,8 +857,12 @@ 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 = kvm_kick_cpu_type; +#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
@@ -229,4 +229,4 @@ 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)
Signature · 1 line
1.7.1
Peter Zijlstra
maintainerIs this the same 8 patches you send yesterday?
Quoted 2 lines · Peter Zijlstra
Is this the same 8 patches you send yesterday?
Sorry for the duplication. It was the same patch. It has some minor update in the cover-letter to include some KVM guest test results. I was having problem locating the patch from the LKML list and so I thought they had failed to be sent out properly.
-Longman
Quoted 2 lines · Peter Zijlstra
Is this the same 8 patches you send yesterday?
Sorry for the duplication. It was the same patch. It has some minor update in the cover-letter to include some KVM guest test results. I was having problem locating the patch from the LKML list and so I thought they had failed to be sent out properly.
-Longman
Peter Zijlstra
maintainerIs this the same 8 patches you send yesterday?