locking/rwsem: Rework rwsem-xadd & enable new rwsem features
Subsystems (12)
Waiman Long
reviewerv6 cover locking/rwsem: Rework rwsem-xadd & enable new rwsem features
v5->v6:
- Reworked the locking algorithm to make it similar to qrwlock.
- Removed all the architecture specific code & use only generic code.
- Added waiter lock handoff and time-based reader lock stealing.
v4->v5:
- Drop the OSQ patch, the need to increase the size of the rwsem structure and the autotuning mechanism.
- Add an intermediate patch to enable readers spinning on writer.
- Other miscellaneous changes and optimizations.
v3->v4:
- Rebased to the latest tip tree due to changes to rwsem-xadd.c.
- Update the OSQ patch to fix race condition.
v2->v3:
- Used smp_acquire__after_ctrl_dep() to provide acquire barrier.
- Added the following new patches:
1) make rwsem_spin_on_owner() return a tristate value.
2) reactivate reader spinning when there is a large number of favorable writer-on-writer spinnings.
3) move all the rwsem macros in arch-specific rwsem.h files into a common asm-generic/rwsem_types.h file.
4) add a boot parameter to specify the reader spinning threshold.
- Updated some of the patches as suggested by PeterZ and adjusted some of the reader spinning parameters.
v1->v2:
- Fixed a 0day build error.
- Added a new patch 1 to make osq_lock() a proper acquire memory barrier.
- Replaced the explicit enabling of reader spinning by an autotuning mechanism that disable reader spinning for those rwsems that may not benefit from reader spinning.
- Remove the last xfs patch as it is no longer necessary.
v4: https://lkml.org/lkml/2016/8/18/1039
v5: https://lkml.org/lkml/2017/6/1/841
This patchset revamps the current rwsem-xadd implmentation to make it saner and easier to work with. This patchset also implements the following 3 new features:
1) Waiter lock handoff
2) Reader optimistic spinning
3) Time-based reader lock stealing
With these changes, performance on workloads with a mix of readers and writers will improve substantially. Now rwsem will also become reader-preferring instead of writer-preferring, which is usually good for performance purpose.
This patchset also uses generic code for all architectures, thus all the architecture specific assembly codes can be removed easing maintenance.
Patch 1 moves down the rwsem_down_read_failed() function for later patches.
Patch 2 reworks the rwsem-xadd locking and unlocking codes to use an algorithm somewhat similar to what qrwlock is doing today. All the fastpath codes are moved to a new kernel/locking/rwsem-xadd.h header file.
Patch 3 moves all the owner setting code to the fastpath in the rwsem-xadd.h file as well.
Patch 4 moves content of kernel/locking/rwsem.h to rwsem-xadd.h and removes it.
Patch 5 moves rwsem internal functions from include/linux/rwsem.h to rwsem-xadd.h.
Patch 6 removes all the architecture specific rwsem files.
Patch 7 enables forced lock handoff to the first waiter in the wait queue when it has waited for too long without acquiring the lock. This prevents lock starvation and makes rwsem more fair.
Patch 8 enables readers to optimistically spin on a writer owned lock.
Patch 9 enables time-based reader lock stealing, thus making rwsem reader-preferring instead of writer-preferring.
Patch 10 modifies rwsem_spin_on_owner() to return a tri-state value that can be used in later patch.
Patch 11 enables writers to optimistically spin on reader-owned lock using a fixed iteration count.
In term of rwsem performance, a rwsem microbenchmark and fio randrw test with a xfs filesystem on a ramdisk were used to verify the performance changes due to these patches. Both tests were run on a 2-socket, 40-core Gold 6148 system. The rwsem microbenchmark (1:1 reader/writer ratio) has short critical section while the fio randrw test has long critical section (4k read/write).
The following table shows the performance of the rwsem microbenchmark and fio radrw test with different number of patches applied on 4.14 based kernels:
# of Patches Locking Rate FIO Bandwidth FIO Bandwidth
Applied 40 threads 32 threads 16 threads
------------ ------------ ------------- -------------
0 38.7 kop/s 706 MB/s 704 MB/s
7 38.6 kop/s 668 MB/s 663 MB/s
8 38.9 kop/s 704 MB/s 701 MB/s
9 39.1 kop/s 702 MB/s 707 MB/s
11 3218.0 kop/s 2594 MB/s 2614 MB/sSo this patchset improves mixed read/write rwsem microbench by 83X and randrw fio bandwidth by about 3.7X.
With separate reader and writer threads (20 each), the following table shows the per-thread locking rates (min/mean/max) of the rwsem microbenchmark with various patch level.
# of Patches Reader Writer
Applied Locking Rate Locking Rate
------------ ------------ ------------
0 5,155/ 5,155/ 5,155 5,154/248,852/346,281
7 5,696/ 5,697/ 5,698 113,500/215,826/320,872
8 4,827/ 5,047/ 5,215 4,826/176,797/284,069
9 211,276/ 509,712/1,134,007 4,894/221,839/246,818
11 884,513/1,043,989/1,252,533 9,604/ 11,105/ 25,225It can be seen that rwsem changes from writer-preferring to reader-preferring.
Waiman Long (11):
locking/rwsem: relocate rwsem_down_read_failed() locking/rwsem: Implement a new locking scheme locking/rwsem: Move owner setting code from rwsem.c to rwsem-xadd.h locking/rwsem: Remove kernel/locking/rwsem.h locking/rwsem: Move rwsem internal function declarations to
rwsem-xadd.h
locking/rwsem: Remove arch specific rwsem files locking/rwsem: Implement lock handoff to prevent lock starvation locking/rwsem: Enable readers spinning on writer locking/rwsem: Enable time-based reader lock stealing locking/rwsem: Make rwsem_spin_on_owner() return a tri-state value locking/rwsem: Enable count-based spinning on reader
arch/alpha/include/asm/rwsem.h | 195 -------------- arch/arm/include/asm/Kbuild | 1 - arch/arm64/include/asm/Kbuild | 1 - arch/hexagon/include/asm/Kbuild | 1 - arch/ia64/include/asm/rwsem.h | 154 ------------ arch/powerpc/include/asm/Kbuild | 1 - arch/s390/include/asm/rwsem.h | 210 ---------------- arch/sh/include/asm/Kbuild | 1 - arch/sparc/include/asm/Kbuild | 1 - arch/x86/include/asm/rwsem.h | 221 ---------------- arch/x86/lib/Makefile | 1 - arch/x86/lib/rwsem.S | 144 ----------- arch/xtensa/include/asm/Kbuild | 1 - include/asm-generic/rwsem.h | 129 ---------- include/linux/rwsem.h | 19 +- kernel/locking/percpu-rwsem.c | 4 + kernel/locking/rwsem-xadd.c | 544 ++++++++++++++++++++++++++-------------- kernel/locking/rwsem-xadd.h | 272 ++++++++++++++++++++ kernel/locking/rwsem.c | 21 +- kernel/locking/rwsem.h | 68 ----- 20 files changed, 638 insertions(+), 1351 deletions(-) delete mode 100644 arch/alpha/include/asm/rwsem.h delete mode 100644 arch/ia64/include/asm/rwsem.h delete mode 100644 arch/s390/include/asm/rwsem.h delete mode 100644 arch/x86/include/asm/rwsem.h delete mode 100644 arch/x86/lib/rwsem.S delete mode 100644 include/asm-generic/rwsem.h create mode 100644 kernel/locking/rwsem-xadd.h delete mode 100644 kernel/locking/rwsem.h
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 1/11 locking/rwsem: relocate rwsem_down_read_failed()
The rwsem_down_read_failed*() functions were relocted from above the optimistic spinning section to below that section. This enables them to use functions in that section in future patches. There is no code change.
--- kernel/locking/rwsem-xadd.c | 150 ++++++++++++++++++++++---------------------- 1 file changed, 75 insertions(+), 75 deletions(-)
kernel/locking/rwsem-xadd.c +75 -75
@@ -219,81 +219,6 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem, }/* -* Wait for the read lock to be granted -*/ -static inline struct rw_semaphore __sched * -__rwsem_down_read_failed_common (struct rw_semaphore *sem,int state) -{ -long count, adjustment = -RWSEM_ACTIVE_READ_BIAS ; -struct rwsem_waiter waiter; -DEFINE_WAKE_Q (wake_q); - - waiter.task = current; - waiter.type =RWSEM_WAITING_FOR_READ ; - -raw_spin_lock_irq (&sem->wait_lock); -if (list_empty (&sem->wait_list)) - adjustment +=RWSEM_WAITING_BIAS ; -list_add_tail (&waiter.list, &sem->wait_list); - -/* we're now waiting on the lock, but no longer actively locking */ - count =atomic_long_add_return (adjustment, &sem->count); - -/* -* If there are no active locks, wake the front queued process(es). -* -* If there are no writers and we are first in the queue, -* wake our own waiter to join the existing active readers ! -*/ -if (count ==RWSEM_WAITING_BIAS || - (count >RWSEM_WAITING_BIAS && - adjustment != -RWSEM_ACTIVE_READ_BIAS )) -__rwsem_mark_wake (sem,RWSEM_WAKE_ANY , &wake_q); - -raw_spin_unlock_irq (&sem->wait_lock); -wake_up_q (&wake_q); - -/* wait to be given the lock */ -while (true) { -set_current_state (state); -if (!waiter.task) -break ; -if (signal_pending_state (state, current)) { -raw_spin_lock_irq (&sem->wait_lock); -if (waiter.task) - goto out_nolock; -raw_spin_unlock_irq (&sem->wait_lock); -break ; - } -schedule (); - } - -__set_current_state (TASK_RUNNING ); -return sem; -out_nolock: -list_del (&waiter.list); -if (list_empty (&sem->wait_list)) -atomic_long_add (-RWSEM_WAITING_BIAS , &sem->count); -raw_spin_unlock_irq (&sem->wait_lock); -__set_current_state (TASK_RUNNING ); -return ERR_PTR (-EINTR ); -} - -__visible struct rw_semaphore * __sched -rwsem_down_read_failed(struct rw_semaphore *sem) -{ -return __rwsem_down_read_failed_common (sem,TASK_UNINTERRUPTIBLE ); -} -EXPORT_SYMBOL (rwsem_down_read_failed); - -__visible struct rw_semaphore * __sched -rwsem_down_read_failed_killable(struct rw_semaphore *sem) -{ -return __rwsem_down_read_failed_common (sem,TASK_KILLABLE ); -} -EXPORT_SYMBOL (rwsem_down_read_failed_killable); - -/* * This function must be called with the sem->wait_lock held to prevent * race conditions between checking the rwsem wait list and setting the * sem->count accordingly.
@@ -488,6 +413,81 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)#endif /* +* Wait for the read lock to be granted +*/ +static inline struct rw_semaphore __sched * +__rwsem_down_read_failed_common (struct rw_semaphore *sem,int state) +{ +long count, adjustment = -RWSEM_ACTIVE_READ_BIAS ; +struct rwsem_waiter waiter; +DEFINE_WAKE_Q (wake_q); + + waiter.task = current; + waiter.type =RWSEM_WAITING_FOR_READ ; + +raw_spin_lock_irq (&sem->wait_lock); +if (list_empty (&sem->wait_list)) + adjustment +=RWSEM_WAITING_BIAS ; +list_add_tail (&waiter.list, &sem->wait_list); + +/* we're now waiting on the lock, but no longer actively locking */ + count =atomic_long_add_return (adjustment, &sem->count); + +/* +* If there are no active locks, wake the front queued process(es). +* +* If there are no writers and we are first in the queue, +* wake our own waiter to join the existing active readers ! +*/ +if (count ==RWSEM_WAITING_BIAS || + (count >RWSEM_WAITING_BIAS && + adjustment != -RWSEM_ACTIVE_READ_BIAS )) +__rwsem_mark_wake (sem,RWSEM_WAKE_ANY , &wake_q); + +raw_spin_unlock_irq (&sem->wait_lock); +wake_up_q (&wake_q); + +/* wait to be given the lock */ +while (true) { +set_current_state (state); +if (!waiter.task) +break ; +if (signal_pending_state (state, current)) { +raw_spin_lock_irq (&sem->wait_lock); +if (waiter.task) + goto out_nolock; +raw_spin_unlock_irq (&sem->wait_lock); +break ; + } +schedule (); + } + +__set_current_state (TASK_RUNNING ); +return sem; +out_nolock: +list_del (&waiter.list); +if (list_empty (&sem->wait_list)) +atomic_long_add (-RWSEM_WAITING_BIAS , &sem->count); +raw_spin_unlock_irq (&sem->wait_lock); +__set_current_state (TASK_RUNNING ); +return ERR_PTR (-EINTR ); +} + +__visible struct rw_semaphore * __sched +rwsem_down_read_failed(struct rw_semaphore *sem) +{ +return __rwsem_down_read_failed_common (sem,TASK_UNINTERRUPTIBLE ); +} +EXPORT_SYMBOL (rwsem_down_read_failed); + +__visible struct rw_semaphore * __sched +rwsem_down_read_failed_killable(struct rw_semaphore *sem) +{ +return __rwsem_down_read_failed_common (sem,TASK_KILLABLE ); +} +EXPORT_SYMBOL (rwsem_down_read_failed_killable); + +/* * Wait until we successfully acquire the write lock */ static inline struct rw_semaphore *
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 2/11 locking/rwsem: Implement a new locking scheme
In reply to Waiman LongThe current way of using various reader, writer and waiting biases in the rwsem code are confusing and hard to understand. I have to reread the rwsem count guide in the rwsem-xadd.c file from time to time to remind myself how this whole thing works. It also makes the rwsem code harder to be optimized.
To make rwsem more sane, a new locking scheme similar to the one in qrwlock is now being used. The count is now a 32-bit atomic value in all architectures. The current bit definitions are:
Bit 0 - writer locked bit Bit 1 - waiters present bit Bits 2-7 - reserved for future extension
Bits 8-31 - reader count
Now the cmpxchg instruction is used to acquire the write lock. The read lock is still acquired with xadd instruction, so there is no change here. This scheme will allow up to 16M active readers which should be more than enough. We can always use some more reserved bits if necessary.
The same generic locking code will be used for all the architectures and the architecture specific files will be retired.
This patch also hide the fastpath implementation of rwsem (now in kernel/locking/rwsem-xadd.h) from the other kernel code as include/linux/rwsem.h will not include it.
With a locking microbenchmark running on 3.13 based kernel, the total locking rates (in Mops/s) of the benchmark on a 2-socket 36-core x86-64 system before and after the patch were as follows:
Before Patch After Patch
# of Threads wlock rlock wlock rlock
------------ ----- ----- ----- -----
1 39.039 33.401 40.432 33.093
2 9.767 17.250 11.424 18.763
4 9.069 17.580 10.085 17.372
8 9.390 15.372 11.733 14.507The locking rates of the benchmark on a 16-processor Power8 system were as follows:
Before Patch After Patch
# of Threads wlock rlock wlock rlock
------------ ----- ----- ----- -----
1 15.086 13.738 9.373 13.597
2 4.864 6.280 5.514 6.309
4 3.286 4.932 4.153 5.011
8 2.637 2.248 3.528 2.189The locking rates of the benchmark on a 32-core Cavium ARM64 system were as follows:
Before Patch After Patch
# of Threads wlock rlock wlock rlock
------------ ----- ----- ----- -----
1 4.849 3.972 5.194 4.223
2 3.165 4.628 3.077 4.885
4 0.742 3.856 0.716 4.136
8 1.639 2.443 1.330 2.475For read lock, locking performance was about the same before and after the patch. For write lock, the new code had better contended performance (2 or more threads) for both x86 and ppc, but it seemed to slow down a bit in arm64. The uncontended performance, however, suffers quite a bit in ppc, but not in x86 and arm64. So cmpxchg does have a noticeable higher cost than xadd in ppc, the elimination of the atomic count reversal in slowpath helps the contended performance, though.
--- include/asm-generic/rwsem.h | 129 ------------------------------------- include/linux/rwsem.h | 12 ++-- kernel/locking/percpu-rwsem.c | 2 + kernel/locking/rwsem-xadd.c | 145 ++++++++++++++---------------------------- kernel/locking/rwsem-xadd.h | 117 ++++++++++++++++++++++++++++++++++ kernel/locking/rwsem.h | 4 ++ 6 files changed, 174 insertions(+), 235 deletions(-) delete mode 100644 include/asm-generic/rwsem.h create mode 100644 kernel/locking/rwsem-xadd.h
include/asm-generic/rwsem.h deleted +0 -129
@@ -1,129 +0,0 @@ -#ifndef _ASM_GENERIC_RWSEM_H -#define _ASM_GENERIC_RWSEM_H - -#ifndef _LINUX_RWSEM_H -#error "Please don't include <asm/rwsem.h> directly, use <linux/rwsem.h> instead." -#endif - -#ifdef __KERNEL__ - -/* -* R/W semaphores originally for PPC using the stuff in lib/rwsem.c. -* Adapted largely from include/asm-i386/rwsem.h -* by Paul Mackerras <[email protected]>. -*/ - -/* -* the semaphore definition -*/ -#ifdef CONFIG_64BIT -# define RWSEM_ACTIVE_MASK 0xffffffffL -#else -# define RWSEM_ACTIVE_MASK 0x0000ffffL -#endif - -#define RWSEM_UNLOCKED_VALUE 0x00000000L -#define RWSEM_ACTIVE_BIAS 0x00000001L -#define RWSEM_WAITING_BIAS (-RWSEM_ACTIVE_MASK-1) -#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS -#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) - -/* -* lock for reading -*/ -static inline void __down_read (struct rw_semaphore *sem) -{ -if (unlikely (atomic_long_inc_return_acquire (&sem->count) <=0 )) -rwsem_down_read_failed (sem); -} - -static inline int __down_read_trylock (struct rw_semaphore *sem) -{ -long tmp; - -while ((tmp =atomic_long_read (&sem->count)) >=0 ) { -if (tmp ==atomic_long_cmpxchg_acquire (&sem->count, tmp, - tmp +RWSEM_ACTIVE_READ_BIAS )) { -return 1 ; - } - } -return 0 ; -} - -/* -* lock for writing -*/ -static inline void __down_write (struct rw_semaphore *sem) -{ -long tmp; - - tmp =atomic_long_add_return_acquire (RWSEM_ACTIVE_WRITE_BIAS , - &sem->count); -if (unlikely (tmp !=RWSEM_ACTIVE_WRITE_BIAS )) -rwsem_down_write_failed (sem); -} - -static inline int __down_write_killable (struct rw_semaphore *sem) -{ -long tmp; - - tmp =atomic_long_add_return_acquire (RWSEM_ACTIVE_WRITE_BIAS , - &sem->count); -if (unlikely (tmp !=RWSEM_ACTIVE_WRITE_BIAS )) -if (IS_ERR (rwsem_down_write_failed_killable (sem))) -return -EINTR ; -return 0 ; -} - -static inline int __down_write_trylock (struct rw_semaphore *sem) -{ -long tmp; - - tmp =atomic_long_cmpxchg_acquire (&sem->count,RWSEM_UNLOCKED_VALUE , -RWSEM_ACTIVE_WRITE_BIAS ); -return tmp ==RWSEM_UNLOCKED_VALUE ; -} - -/* -* unlock after reading -*/ -static inline void __up_read (struct rw_semaphore *sem) -{ -long tmp; - - tmp =atomic_long_dec_return_release (&sem->count); -if (unlikely (tmp <-1 && (tmp &RWSEM_ACTIVE_MASK ) ==0 )) -rwsem_wake (sem); -} - -/* -* unlock after writing -*/ -static inline void __up_write (struct rw_semaphore *sem) -{ -if (unlikely (atomic_long_sub_return_release (RWSEM_ACTIVE_WRITE_BIAS , - &sem->count) <0 )) -rwsem_wake (sem); -} - -/* -* downgrade write lock to read lock -*/ -static inline void __downgrade_write (struct rw_semaphore *sem) -{ -long tmp; - -/* -* When downgrading from exclusive to shared ownership, -* anything inside the write-locked region cannot leak -* into the read side. In contrast, anything in the -* read-locked region is ok to be re-ordered into the -* write side. As such, rely on RELEASE semantics. -*/ - tmp =atomic_long_add_return_release (-RWSEM_WAITING_BIAS , &sem->count); -if (tmp <0 ) -rwsem_downgrade_wake (sem); -} - -#endif /* __KERNEL__ */ -#endif /* _ASM_GENERIC_RWSEM_H */
include/linux/rwsem.h +5 -7
@@ -25,11 +25,10 @@ #include <linux/rwsem-spinlock.h> /* use a generic implementation */ #define __RWSEM_INIT_COUNT(name) .count = RWSEM_UNLOCKED_VALUE #else -/* All arch specific implementations share the same struct */ struct rw_semaphore { - atomic_long_t count; - struct list_head wait_list; + atomic_t count; raw_spinlock_t wait_lock; + struct list_head wait_list; #ifdef CONFIG_RWSEM_SPIN_ON_OWNER struct optimistic_spin_queue osq; /* spinner MCS lock */ /*
@@ -50,16 +49,15 @@ struct rw_semaphore {extern struct rw_semaphore *rwsem_wake (struct rw_semaphore *);extern struct rw_semaphore *rwsem_downgrade_wake (struct rw_semaphore *sem); -/* Include the arch specific part */ -#include <asm/rwsem.h> +#define RWSEM_UNLOCKED_VALUE 0/* In all implementations count != 0 means locked */ static inline int rwsem_is_locked (struct rw_semaphore *sem) { -return atomic_long_read (&sem->count) !=0 ; +return atomic_read (&sem->count) !=RWSEM_UNLOCKED_VALUE ; } -#define __RWSEM_INIT_COUNT (name) .count = ATOMIC_LONG_INIT(RWSEM_UNLOCKED_VALUE) +#define __RWSEM_INIT_COUNT (name) .count = ATOMIC_INIT(RWSEM_UNLOCKED_VALUE)#endif /* Common initializer macros and functions */
kernel/locking/percpu-rwsem.c +2 -0
@@ -7,6 +7,8 @@#include <linux/sched.h> #include <linux/errno.h> +#include "rwsem.h" +int __percpu_init_rwsem (struct percpu_rw_semaphore *sem,const char *name,struct lock_class_key *rwsem_key) {
kernel/locking/rwsem-xadd.c +46 -99
271 lines not on this page · raw message
kernel/locking/rwsem-xadd.h new file +117 -0
117 lines not on this page · raw message
kernel/locking/rwsem.h +4 -0
@@ -66,3 +66,7 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem) { }#endif + +#ifdef CONFIG_RWSEM_XCHGADD_ALGORITHM +#include "rwsem-xadd.h" +#endif
Signature · 1 line
1.8.3.1
Peter Zijlstra
maintainerRe: [PATCH v6 02/11] locking/rwsem: Implement a new locking scheme
Quoted 20 lines · Waiman Long
kernel/locking/rwsem-xadd.h new file +20
-0+/* +* The definition of the atomic counter in the semaphore: +* +* Bit 0 - writer locked bit +* Bit 1 - waiters present bit +* Bits 2-7 - reserved +* Bits 8-31 - 24-bit reader count +* +* atomic_fetch_add() is used to obtain reader lock, whereas atomic_cmpxchg() +* will be used to obtain writer lock. +*/ +#define RWSEM_WRITER_LOCKED 0X00000001 +#define RWSEM_FLAG_WAITERS 0X00000002 +#define RWSEM_READER_BIAS 0x00000100 +#define RWSEM_READER_SHIFT 8 +#define RWSEM_READER_MASK (~((1U << RWSEM_READER_SHIFT) - 1)) +#define RWSEM_LOCK_MASK (RWSEM_WRITER_LOCKED|RWSEM_READER_MASK) +#define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_LOCKED|RWSEM_FLAG_WAITERS) + +#define RWSEM_COUNT_IS_LOCKED (c) ((c) & RWSEM_LOCK_MASK)
kernel/locking/rwsem-xadd.h line 35
Quoted 10 lines
kernel/locking/rwsem-xadd.h new file +10
-0+ +/* +* lock for reading +*/ +static inline void __down_read (struct rw_semaphore *sem) +{ +if (unlikely (atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count) + &RWSEM_READ_FAILED_MASK )) +rwsem_down_read_failed (sem); +}
So I implemented rwsem-mutex (also qrwlock based) that puts
(unsigned long)current | RWSEM_WRITER
in the atomic_long_t rw_semaphore::owner field. The down-side is that you can't do fetch_add based __down_read, because that would clobber the pointer. The up-side is that we have a stable owner pointer (which is what I needed for PI like things).
I've yet to do performance tests -- and I've not done a bunch of the obvious optimisations either.
Waiman Long
reviewerRe: [PATCH v6 02/11] locking/rwsem: Implement a new locking scheme
Quoted 33 lines · Peter Zijlstra
Quoted 20 lines · Waiman Long
kernel/locking/rwsem-xadd.h new file +20
-0+/* +* The definition of the atomic counter in the semaphore: +* +* Bit 0 - writer locked bit +* Bit 1 - waiters present bit +* Bits 2-7 - reserved +* Bits 8-31 - 24-bit reader count +* +* atomic_fetch_add() is used to obtain reader lock, whereas atomic_cmpxchg() +* will be used to obtain writer lock. +*/ +#define RWSEM_WRITER_LOCKED 0X00000001 +#define RWSEM_FLAG_WAITERS 0X00000002 +#define RWSEM_READER_BIAS 0x00000100 +#define RWSEM_READER_SHIFT 8 +#define RWSEM_READER_MASK (~((1U << RWSEM_READER_SHIFT) - 1)) +#define RWSEM_LOCK_MASK (RWSEM_WRITER_LOCKED|RWSEM_READER_MASK) +#define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_LOCKED|RWSEM_FLAG_WAITERS) + +#define RWSEM_COUNT_IS_LOCKED (c) ((c) & RWSEM_LOCK_MASK)kernel/locking/rwsem-xadd.h line 35
Quoted 10 lines
kernel/locking/rwsem-xadd.h new file +10
-0+ +/* +* lock for reading +*/ +static inline void __down_read (struct rw_semaphore *sem) +{ +if (unlikely (atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count) + &RWSEM_READ_FAILED_MASK )) +rwsem_down_read_failed (sem); +}So I implemented rwsem-mutex (also qrwlock based) that puts
Quoted 6 lines
(unsigned long)current |
RWSEM_WRITERin the
atomic_long_trw_semaphore::ownerfield. The down-side is that you can't dofetch_addbased__down_read, because that would clobber the pointer. The up-side is that we have a stable owner pointer (which is what I needed for PI like things).
Without fetch_add for readers, it could lead to reduced performance for reader heavy workloads.
Are you trying to do a PI version of rwsem? It can work when the lock is writer owned, but not when it is reader owned.
Cheers,
Longman
Waiman Long
reviewerRe: [PATCH v6 02/11] locking/rwsem: Implement a new locking scheme
Quoted 40 lines · Waiman Long
Quoted 33 lines · Peter Zijlstra
Quoted 20 lines · Waiman Long
kernel/locking/rwsem-xadd.h new file +20
-0+/* +* The definition of the atomic counter in the semaphore: +* +* Bit 0 - writer locked bit +* Bit 1 - waiters present bit +* Bits 2-7 - reserved +* Bits 8-31 - 24-bit reader count +* +* atomic_fetch_add() is used to obtain reader lock, whereas atomic_cmpxchg() +* will be used to obtain writer lock. +*/ +#define RWSEM_WRITER_LOCKED 0X00000001 +#define RWSEM_FLAG_WAITERS 0X00000002 +#define RWSEM_READER_BIAS 0x00000100 +#define RWSEM_READER_SHIFT 8 +#define RWSEM_READER_MASK (~((1U << RWSEM_READER_SHIFT) - 1)) +#define RWSEM_LOCK_MASK (RWSEM_WRITER_LOCKED|RWSEM_READER_MASK) +#define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_LOCKED|RWSEM_FLAG_WAITERS) + +#define RWSEM_COUNT_IS_LOCKED (c) ((c) & RWSEM_LOCK_MASK)kernel/locking/rwsem-xadd.h line 35
Quoted 10 lines
kernel/locking/rwsem-xadd.h new file +10
-0+ +/* +* lock for reading +*/ +static inline void __down_read (struct rw_semaphore *sem) +{ +if (unlikely (atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count) + &RWSEM_READ_FAILED_MASK )) +rwsem_down_read_failed (sem); +}So I implemented rwsem-mutex (also qrwlock based) that puts
Quoted 6 lines
(unsigned long)current |
RWSEM_WRITERin the
atomic_long_trw_semaphore::ownerfield. The down-side is that you can't dofetch_addbased__down_read, because that would clobber the pointer. The up-side is that we have a stable owner pointer (which is what I needed for PI like things).
Quoted 5 lines
Without
fetch_addfor readers, it could lead to reduced performance for reader heavy workloads.Are you trying to do a PI version of rwsem? It can work when the lock is writer owned, but not when it is reader owned.
I have actually been thinking about giving priority to RT or DL task by putting the task in front of the wait queue and assert the lock handoff bit, for example. There are extra reserved bits left that can be useful for adding these additional features. That will be later when I am done with the current patch.
Cheers,
Longman
Peter Zijlstra
maintainerRe: [PATCH v6 02/11] locking/rwsem: Implement a new locking scheme
In reply to Waiman LongQuoted 5 lines · Waiman Long
Quoted 4 lines · Peter Zijlstra
So I implemented rwsem-mutex (also qrwlock based) that puts
(unsigned long)current |
RWSEM_WRITER
Quoted 7 lines
Quoted 4 lines
in the
atomic_long_trw_semaphore::ownerfield. The down-side is that you can't dofetch_addbased__down_read, because that would clobber the pointer. The up-side is that we have a stable owner pointer (which is what I needed for PI like things).Without
fetch_addfor readers, it could lead to reduced performance for reader heavy workloads.
Yeah I know.. :-)
Quoted 2 lines
Are you trying to do a PI version of rwsem? It can work when the lock is writer owned, but not when it is reader owned.
Not classical PI; there's one of those in -rt btw:
I'm implementing proxy-execution (or rather, playing with it in a few spare moments here and there). But yes, it will only be able to boost write owners. But in order to make that happen I need the lock state and owner thing in the same field, like mutex.
In any case, I'll try and have a look at these patches.
Waiman Long
reviewerPATCH v6 3/11 locking/rwsem: Move owner setting code from rwsem.c to rwsem-xadd.h
In reply to Waiman LongThe setting of owner field is specific to rwsem-xadd, it is not needed for rwsem-spinlock. This patch moves all the owner setting code to the fast paths directly within rwsem-add.h file.
The rwsem_set_reader_owned() is now only called by the first reader. So there is no need to do a read to check if the owner is properly set first.
--- kernel/locking/rwsem-xadd.c | 7 +++---- kernel/locking/rwsem-xadd.h | 19 ++++++++++++++++--- kernel/locking/rwsem.c | 17 ++--------------- kernel/locking/rwsem.h | 11 ++++------- 4 files changed, 25 insertions(+), 29 deletions(-)
kernel/locking/rwsem-xadd.c +3 -4
@@ -132,11 +132,10 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,return ; }/* -* It is not really necessary to set it to reader-owned here, -* but it gives the spinners an early indication that the -* readers now have the lock. +* Set it to reader-owned for first reader. */ -rwsem_set_reader_owned (sem); +if (!(oldcount >>RWSEM_READER_SHIFT )) +rwsem_set_reader_owned (sem); }/*
kernel/locking/rwsem-xadd.h +16 -3
@@ -29,9 +29,12 @@*/ static inline void __down_read (struct rw_semaphore *sem) { -if (unlikely (atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count) - &RWSEM_READ_FAILED_MASK )) +int count =atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count); + +if (unlikely (count &RWSEM_READ_FAILED_MASK ))rwsem_down_read_failed (sem); +else if ((count >>RWSEM_READER_SHIFT ) ==1 ) +rwsem_set_reader_owned (sem); }static inline int __down_read_trylock (struct rw_semaphore *sem)
@@ -41,6 +44,8 @@ static inline int __down_read_trylock(struct rw_semaphore *sem)while (!((tmp =atomic_read (&sem->count)) &RWSEM_READ_FAILED_MASK )) {if (tmp ==atomic_cmpxchg_acquire (&sem->count, tmp, tmp +RWSEM_READER_BIAS )) { +if (!(tmp >>RWSEM_READER_SHIFT )) +rwsem_set_reader_owned (sem);return 1 ; } }
@@ -55,6 +60,7 @@ static inline void __down_write(struct rw_semaphore *sem)if (unlikely (atomic_cmpxchg_acquire (&sem->count,0 ,RWSEM_WRITER_LOCKED )))rwsem_down_write_failed (sem); +rwsem_set_owner (sem); }static inline int __down_write_killable (struct rw_semaphore *sem)
@@ -63,12 +69,17 @@ static inline int __down_write_killable(struct rw_semaphore *sem) RWSEM_WRITER_LOCKED)))if (IS_ERR (rwsem_down_write_failed_killable (sem)))return -EINTR ; +rwsem_set_owner (sem);return 0 ; }static inline int __down_write_trylock (struct rw_semaphore *sem) { -return !atomic_cmpxchg_acquire (&sem->count,0 ,RWSEM_WRITER_LOCKED ); +bool taken = !atomic_cmpxchg_acquire (&sem->count,0 , +RWSEM_WRITER_LOCKED ); +if (taken) +rwsem_set_owner (sem); +return taken; }/*
@@ -89,6 +100,7 @@ static inline void __up_read(struct rw_semaphore *sem)*/ static inline void __up_write (struct rw_semaphore *sem) { +rwsem_clear_owner (sem);if (unlikely (atomic_fetch_add_release (-RWSEM_WRITER_LOCKED , &sem->count) &RWSEM_FLAG_WAITERS ))rwsem_wake (sem);
@@ -110,6 +122,7 @@ static inline void __downgrade_write(struct rw_semaphore *sem)*/ tmp =atomic_fetch_add_release (-RWSEM_WRITER_LOCKED +RWSEM_READER_BIAS , &sem->count); +rwsem_set_reader_owned (sem);if (tmp &RWSEM_FLAG_WAITERS )rwsem_downgrade_wake (sem); }
kernel/locking/rwsem.c +2 -15
@@ -23,7 +23,6 @@ void __sched down_read(struct rw_semaphore *sem)rwsem_acquire_read (&sem->dep_map,0 ,0 , _RET_IP_);LOCK_CONTENDED (sem, __down_read_trylock, __down_read); -rwsem_set_reader_owned (sem); }EXPORT_SYMBOL (down_read);
@@ -35,10 +34,8 @@ int down_read_trylock(struct rw_semaphore *sem) {int ret =__down_read_trylock (sem); -if (ret ==1 ) { +if (ret ==1 )rwsem_acquire_read (&sem->dep_map,0 ,1 , _RET_IP_); -rwsem_set_reader_owned (sem); - }return ret; }
@@ -53,7 +50,6 @@ void __sched down_write(struct rw_semaphore *sem)rwsem_acquire (&sem->dep_map,0 ,0 , _RET_IP_);LOCK_CONTENDED (sem, __down_write_trylock, __down_write); -rwsem_set_owner (sem); }EXPORT_SYMBOL (down_write);
@@ -71,7 +67,6 @@ int __sched down_write_killable(struct rw_semaphore *sem)return -EINTR ; } -rwsem_set_owner (sem);return 0 ; }
@@ -84,10 +79,8 @@ int down_write_trylock(struct rw_semaphore *sem) {int ret =__down_write_trylock (sem); -if (ret ==1 ) { +if (ret ==1 )rwsem_acquire (&sem->dep_map,0 ,1 , _RET_IP_); -rwsem_set_owner (sem); - }return ret; }
@@ -113,7 +106,6 @@ void up_write(struct rw_semaphore *sem) {rwsem_release (&sem->dep_map,1 , _RET_IP_); -rwsem_clear_owner (sem);__up_write (sem); }
@@ -126,7 +118,6 @@ void downgrade_write(struct rw_semaphore *sem) {lock_downgrade (&sem->dep_map, _RET_IP_); -rwsem_set_reader_owned (sem);__downgrade_write (sem); }
@@ -140,7 +131,6 @@ void down_read_nested(struct rw_semaphore *sem, int subclass)rwsem_acquire_read (&sem->dep_map, subclass,0 , _RET_IP_);LOCK_CONTENDED (sem, __down_read_trylock, __down_read); -rwsem_set_reader_owned (sem); }EXPORT_SYMBOL (down_read_nested);
@@ -151,7 +141,6 @@ void _down_write_nest_lock(struct rw_semaphore *sem, struct lockdep_map *nest)rwsem_acquire_nest (&sem->dep_map,0 ,0 , nest, _RET_IP_);LOCK_CONTENDED (sem, __down_write_trylock, __down_write); -rwsem_set_owner (sem); }EXPORT_SYMBOL (_down_write_nest_lock);
@@ -171,7 +160,6 @@ void down_write_nested(struct rw_semaphore *sem, int subclass)rwsem_acquire (&sem->dep_map, subclass,0 , _RET_IP_);LOCK_CONTENDED (sem, __down_write_trylock, __down_write); -rwsem_set_owner (sem); }EXPORT_SYMBOL (down_write_nested);
@@ -186,7 +174,6 @@ int __sched down_write_killable_nested(struct rw_semaphore *sem, int subclass)return -EINTR ; } -rwsem_set_owner (sem);return 0 ; }
kernel/locking/rwsem.h +4 -7
@@ -33,15 +33,12 @@ static inline void rwsem_clear_owner(struct rw_semaphore *sem)WRITE_ONCE (sem->owner,NULL ); } +/* +* This should only be called by the first reader that acquires the read lock. +*/ static inline void rwsem_set_reader_owned (struct rw_semaphore *sem) { -/* -* We check the owner value first to make sure that we will only -* do a write to the rwsem cacheline when it is really necessary -* to minimize cacheline contention. -*/ -if (sem->owner !=RWSEM_READER_OWNED ) -WRITE_ONCE (sem->owner,RWSEM_READER_OWNED ); +WRITE_ONCE (sem->owner,RWSEM_READER_OWNED ); }static inline bool rwsem_owner_is_writer (struct task_struct *owner)
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 4/11 locking/rwsem: Remove kernel/locking/rwsem.h
In reply to Waiman LongThe content of kernel/locking/rwsem.h is now specific to rwsem-xadd. So we can just move the its content into rwsem-xadd.h and remove it.
--- kernel/locking/percpu-rwsem.c | 4 ++- kernel/locking/rwsem-xadd.c | 2 +- kernel/locking/rwsem-xadd.h | 66 +++++++++++++++++++++++++++++++++++++++++ kernel/locking/rwsem.c | 4 ++- kernel/locking/rwsem.h | 69 ------------------------------------------- 5 files changed, 73 insertions(+), 72 deletions(-) delete mode 100644 kernel/locking/rwsem.h
kernel/locking/percpu-rwsem.c +3 -1
@@ -7,7 +7,9 @@#include <linux/sched.h> #include <linux/errno.h> -#include "rwsem.h" +#ifdef CONFIG_RWSEM_XCHGADD_ALGORITHM +#include "rwsem-xadd.h" +#endif int __percpu_init_rwsem (struct percpu_rw_semaphore *sem,const char *name,struct lock_class_key *rwsem_key)
kernel/locking/rwsem-xadd.c +1 -1
@@ -18,7 +18,7 @@#include <linux/sched/debug.h> #include <linux/osq_lock.h> -#include "rwsem.h" +#include "rwsem-xadd.h" /* * Guide to the rw_semaphore's count field.
kernel/locking/rwsem-xadd.h +66 -0
72 lines not on this page · raw message
kernel/locking/rwsem.c +3 -1
@@ -12,7 +12,9 @@#include <linux/rwsem.h> #include <linux/atomic.h> -#include "rwsem.h" +#ifdef CONFIG_RWSEM_XCHGADD_ALGORITHM +#include "rwsem-xadd.h" +#endif /* * lock for reading
kernel/locking/rwsem.h deleted +0 -69
69 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 5/11 locking/rwsem: Move rwsem internal function declarations to rwsem-xadd.h
In reply to Waiman LongWe don't need to expose rwsem internal functions which are not supposed to be called directly from other kernel code.
--- include/linux/rwsem.h | 7 ------- kernel/locking/rwsem-xadd.h | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-)
include/linux/rwsem.h +0 -7
@@ -42,13 +42,6 @@ struct rw_semaphore {#endif }; -extern struct rw_semaphore *rwsem_down_read_failed (struct rw_semaphore *sem); -extern struct rw_semaphore *rwsem_down_read_failed_killable (struct rw_semaphore *sem); -extern struct rw_semaphore *rwsem_down_write_failed (struct rw_semaphore *sem); -extern struct rw_semaphore *rwsem_down_write_failed_killable (struct rw_semaphore *sem); -extern struct rw_semaphore *rwsem_wake (struct rw_semaphore *); -extern struct rw_semaphore *rwsem_downgrade_wake (struct rw_semaphore *sem); -#define RWSEM_UNLOCKED_VALUE 0/* In all implementations count != 0 means locked */
kernel/locking/rwsem-xadd.h +7 -0
@@ -90,6 +90,13 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)#define RWSEM_COUNT_IS_LOCKED (c) ((c) & RWSEM_LOCK_MASK) +extern struct rw_semaphore *rwsem_down_read_failed (struct rw_semaphore *sem); +extern struct rw_semaphore *rwsem_down_read_failed_killable (struct rw_semaphore *sem); +extern struct rw_semaphore *rwsem_down_write_failed (struct rw_semaphore *sem); +extern struct rw_semaphore *rwsem_down_write_failed_killable (struct rw_semaphore *sem); +extern struct rw_semaphore *rwsem_wake (struct rw_semaphore *); +extern struct rw_semaphore *rwsem_downgrade_wake (struct rw_semaphore *sem); +/* * lock for reading */
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 6/11 locking/rwsem: Remove arch specific rwsem files
In reply to Waiman LongAs the generic rwsem-xadd code is using the appropriate acquire and release versions of the atomic operations, the arch specific rwsem.h files will not that much faster than the generic code. So we can remove those arch specific rwsem.h and stop building asm/rwsem.h to reduce maintenance effort.
--- arch/alpha/include/asm/rwsem.h | 195 ----------------------------------- arch/arm/include/asm/Kbuild | 1 - arch/arm64/include/asm/Kbuild | 1 - arch/hexagon/include/asm/Kbuild | 1 - arch/ia64/include/asm/rwsem.h | 154 ---------------------------- arch/powerpc/include/asm/Kbuild | 1 - arch/s390/include/asm/rwsem.h | 210 -------------------------------------- arch/sh/include/asm/Kbuild | 1 - arch/sparc/include/asm/Kbuild | 1 - arch/x86/include/asm/rwsem.h | 221 ---------------------------------------- arch/x86/lib/Makefile | 1 - arch/x86/lib/rwsem.S | 144 -------------------------- arch/xtensa/include/asm/Kbuild | 1 - 13 files changed, 932 deletions(-) delete mode 100644 arch/alpha/include/asm/rwsem.h delete mode 100644 arch/ia64/include/asm/rwsem.h delete mode 100644 arch/s390/include/asm/rwsem.h delete mode 100644 arch/x86/include/asm/rwsem.h delete mode 100644 arch/x86/lib/rwsem.S
arch/alpha/include/asm/rwsem.h deleted +0 -195
@@ -1,195 +0,0 @@ -#ifndef _ALPHA_RWSEM_H -#define _ALPHA_RWSEM_H - -/* -* Written by Ivan Kokshaysky <[email protected]>, 2001. -* Based on asm-alpha/semaphore.h and asm-i386/rwsem.h -*/ - -#ifndef _LINUX_RWSEM_H -#error "please don't include asm/rwsem.h directly, use linux/rwsem.h instead" -#endif - -#ifdef __KERNEL__ - -#include <linux/compiler.h> - -#define RWSEM_UNLOCKED_VALUE 0x0000000000000000L -#define RWSEM_ACTIVE_BIAS 0x0000000000000001L -#define RWSEM_ACTIVE_MASK 0x00000000ffffffffL -#define RWSEM_WAITING_BIAS (-0x0000000100000000L) -#define RWSEM_ACTIVE_READ_BIAS RWSEM_ACTIVE_BIAS -#define RWSEM_ACTIVE_WRITE_BIAS (RWSEM_WAITING_BIAS + RWSEM_ACTIVE_BIAS) - -static inline void __down_read (struct rw_semaphore *sem) -{ -long oldcount; -#ifndef CONFIG_SMP - oldcount = sem->count.counter; - sem->count.counter +=RWSEM_ACTIVE_READ_BIAS ; -#else -long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%3,%2\n" -" stq_c %2,%1\n" -" beq %2,2f\n" -" mb\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (oldcount),"=m" (sem->count),"=&r" (temp) - :"Ir" (RWSEM_ACTIVE_READ_BIAS ),"m" (sem->count) :"memory" ); -#endif -if (unlikely (oldcount <0 )) -rwsem_down_read_failed (sem); -} - -/* -* trylock for reading -- returns 1 if successful, 0 if contention -*/ -static inline int __down_read_trylock (struct rw_semaphore *sem) -{ -long old, new, res; - - res =atomic_long_read (&sem->count); -do { - new = res +RWSEM_ACTIVE_READ_BIAS ; -if (new <=0 ) -break ; - old = res; - res =atomic_long_cmpxchg (&sem->count, old, new); - }while (res != old); -return res >=0 ?1 :0 ; -} - -static inline long ___down_write (struct rw_semaphore *sem) -{ -long oldcount; -#ifndef CONFIG_SMP - oldcount = sem->count.counter; - sem->count.counter +=RWSEM_ACTIVE_WRITE_BIAS ; -#else -long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%3,%2\n" -" stq_c %2,%1\n" -" beq %2,2f\n" -" mb\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (oldcount),"=m" (sem->count),"=&r" (temp) - :"Ir" (RWSEM_ACTIVE_WRITE_BIAS ),"m" (sem->count) :"memory" ); -#endif -return oldcount; -} - -static inline void __down_write (struct rw_semaphore *sem) -{ -if (unlikely (___down_write (sem))) -rwsem_down_write_failed (sem); -} - -static inline int __down_write_killable (struct rw_semaphore *sem) -{ -if (unlikely (___down_write (sem))) -if (IS_ERR (rwsem_down_write_failed_killable (sem))) -return -EINTR ; - -return 0 ; -} - -/* -* trylock for writing -- returns 1 if successful, 0 if contention -*/ -static inline int __down_write_trylock (struct rw_semaphore *sem) -{ -long ret =atomic_long_cmpxchg (&sem->count,RWSEM_UNLOCKED_VALUE , -RWSEM_ACTIVE_WRITE_BIAS ); -if (ret ==RWSEM_UNLOCKED_VALUE ) -return 1 ; -return 0 ; -} - -static inline void __up_read (struct rw_semaphore *sem) -{ -long oldcount; -#ifndef CONFIG_SMP - oldcount = sem->count.counter; - sem->count.counter -=RWSEM_ACTIVE_READ_BIAS ; -#else -long temp; - __asm__ __volatile__( -" mb\n" -"1: ldq_l %0,%1\n" -" subq %0,%3,%2\n" -" stq_c %2,%1\n" -" beq %2,2f\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (oldcount),"=m" (sem->count),"=&r" (temp) - :"Ir" (RWSEM_ACTIVE_READ_BIAS ),"m" (sem->count) :"memory" ); -#endif -if (unlikely (oldcount <0 )) -if ((int )oldcount -RWSEM_ACTIVE_READ_BIAS ==0 ) -rwsem_wake (sem); -} - -static inline void __up_write (struct rw_semaphore *sem) -{ -long count; -#ifndef CONFIG_SMP - sem->count.counter -=RWSEM_ACTIVE_WRITE_BIAS ; - count = sem->count.counter; -#else -long temp; - __asm__ __volatile__( -" mb\n" -"1: ldq_l %0,%1\n" -" subq %0,%3,%2\n" -" stq_c %2,%1\n" -" beq %2,2f\n" -" subq %0,%3,%0\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (count),"=m" (sem->count),"=&r" (temp) - :"Ir" (RWSEM_ACTIVE_WRITE_BIAS ),"m" (sem->count) :"memory" ); -#endif -if (unlikely (count)) -if ((int )count ==0 ) -rwsem_wake (sem); -} - -/* -* downgrade write lock to read lock -*/ -static inline void __downgrade_write (struct rw_semaphore *sem) -{ -long oldcount; -#ifndef CONFIG_SMP - oldcount = sem->count.counter; - sem->count.counter -=RWSEM_WAITING_BIAS ; -#else -long temp; - __asm__ __volatile__( -"1: ldq_l %0,%1\n" -" addq %0,%3,%2\n" -" stq_c %2,%1\n" -" beq %2,2f\n" -" mb\n" -".subsection 2\n" -"2: br 1b\n" -".previous" - :"=&r" (oldcount),"=m" (sem->count),"=&r" (temp) - :"Ir" (-RWSEM_WAITING_BIAS ),"m" (sem->count) :"memory" ); -#endif -if (unlikely (oldcount <0 )) -rwsem_downgrade_wake (sem); -} - -#endif /* __KERNEL__ */ -#endif /* _ALPHA_RWSEM_H */
arch/arm/include/asm/Kbuild +0 -1
@@ -12,7 +12,6 @@ generic-y += mm-arch-hooks.h generic-y += msi.h generic-y += parport.h generic-y += preempt.h -generic-y += rwsem.h generic-y += seccomp.h generic-y += segment.h generic-y += serial.h
arch/arm64/include/asm/Kbuild +0 -1
@@ -16,7 +16,6 @@ generic-y += mcs_spinlock.h generic-y += mm-arch-hooks.h generic-y += msi.h generic-y += preempt.h -generic-y += rwsem.h generic-y += segment.h generic-y += serial.h generic-y += set_memory.h
arch/hexagon/include/asm/Kbuild +0 -1
@@ -24,7 +24,6 @@ generic-y += mm-arch-hooks.h generic-y += pci.h generic-y += percpu.h generic-y += preempt.h -generic-y += rwsem.h generic-y += sections.h generic-y += segment.h generic-y += serial.h
arch/ia64/include/asm/rwsem.h deleted +0 -154
154 lines not on this page · raw message
arch/powerpc/include/asm/Kbuild +0 -1
@@ -6,6 +6,5 @@ generic-y += irq_work.h generic-y += local64.h generic-y += mcs_spinlock.h generic-y += preempt.h -generic-y += rwsem.h generic-y += vtime.h generic-y += msi.h
arch/s390/include/asm/rwsem.h deleted +0 -210
210 lines not on this page · raw message
arch/sh/include/asm/Kbuild +0 -1
@@ -13,7 +13,6 @@ generic-y += mm-arch-hooks.h generic-y += parport.h generic-y += percpu.h generic-y += preempt.h -generic-y += rwsem.h generic-y += serial.h generic-y += sizes.h generic-y += trace_clock.h
arch/sparc/include/asm/Kbuild +0 -1
@@ -15,7 +15,6 @@ generic-y += mcs_spinlock.h generic-y += mm-arch-hooks.h generic-y += module.h generic-y += preempt.h -generic-y += rwsem.h generic-y += serial.h generic-y += trace_clock.h generic-y += word-at-a-time.h
arch/x86/include/asm/rwsem.h deleted +0 -221
221 lines not on this page · raw message
arch/x86/lib/Makefile +0 -1
@@ -22,7 +22,6 @@ obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o lib-y := delay.o misc.o cmdline.o cpu.o lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o lib-y += memcpy_$(BITS).o -lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
arch/x86/lib/rwsem.S deleted +0 -144
144 lines not on this page · raw message
arch/xtensa/include/asm/Kbuild +0 -1
@@ -21,7 +21,6 @@ generic-y += mm-arch-hooks.h generic-y += param.h generic-y += percpu.h generic-y += preempt.h -generic-y += rwsem.h generic-y += sections.h generic-y += topology.h generic-y += trace_clock.h
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 7/11 locking/rwsem: Implement lock handoff to prevent lock starvation
In reply to Waiman LongBecause of writer lock stealing, it is possible that a constant stream of incoming writers will cause a waiting writer or reader to wait indefinitely leading to lock starvation.
The mutex code has a lock handoff mechanism to prevent lock starvation. This patch implements a similar lock handoff mechanism to disable lock stealing and force lock handoff to the first waiter in the queue after at least a 10ms waiting period. The waiting period is used to avoid discouraging lock stealing too much to affect performance.
A rwsem microbenchmark was run for 5 seconds on a 2-socket 40-core 80-thread x86-64 system with a 4.14-rc2 based kernel and 60 write_lock threads with 1us sleep critical section.
For the unpatched kernel, the locking rate was 15,519 kop/s. However there were 28 threads with only 1 locking operation done (practically starved). The thread with the highest locking rate had done more than 646k of them.
For the patched kernel, the locking rate dropped to 12,590 kop/s. The number of locking operations done per thread had a range of 14,450 - 22,648. The rwsem became much more fair with the tradeoff of lower overall throughput.
--- kernel/locking/rwsem-xadd.c | 98 +++++++++++++++++++++++++++++++++++++-------- kernel/locking/rwsem-xadd.h | 22 ++++++---- 2 files changed, 96 insertions(+), 24 deletions(-)
kernel/locking/rwsem-xadd.c +81 -17
@@ -70,6 +70,7 @@ struct rwsem_waiter {struct list_head list;struct task_struct *task;enum rwsem_waiter_type type; +unsigned long timeout; };enum rwsem_wake_type {
@@ -79,6 +80,16 @@ enum rwsem_wake_type { };/* +* The minimum waiting time (10ms) in the wait queue before initiating the +* handoff protocol. +* +* The queue head waiter that is aborted (killed) will pass the handoff +* bit, if set, to the next waiter, but the bit has to be cleared when +* the wait queue becomes empty. +*/ +#define RWSEM_WAIT_TIMEOUT ((HZ - 1)/100 + 1) + +/* * handle the lock release when processes blocked on it that can now run * - if we come here from up_xxxx(), then: * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
@@ -128,6 +139,13 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem, adjustment =RWSEM_READER_BIAS ; oldcount =atomic_fetch_add (adjustment, &sem->count);if (unlikely (oldcount &RWSEM_WRITER_LOCKED )) { +/* +* Initiate handoff to reader, if applicable. +*/ +if (!(oldcount &RWSEM_FLAG_HANDOFF ) && +time_after (jiffies, waiter->timeout)) + adjustment -=RWSEM_FLAG_HANDOFF ; +atomic_sub (adjustment, &sem->count);return ; }
@@ -170,6 +188,12 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem, adjustment -=RWSEM_FLAG_WAITERS ; } +/* +* Clear the handoff flag +*/ +if (woken &&RWSEM_COUNT_IS_HANDOFF (atomic_read (&sem->count))) + adjustment -=RWSEM_FLAG_HANDOFF ; +if (adjustment)atomic_add (adjustment, &sem->count); }
@@ -179,15 +203,20 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,* race conditions between checking the rwsem wait list and setting the * sem->count accordingly. */ -static inline bool rwsem_try_write_lock (int count,struct rw_semaphore *sem) +static inline bool +rwsem_try_write_lock (int count,struct rw_semaphore *sem,bool first) {int new;if (RWSEM_COUNT_IS_LOCKED (count))return false; +if (!first &&RWSEM_COUNT_IS_HANDOFF (count)) +return false; + new = count +RWSEM_WRITER_LOCKED - - (list_is_singular (&sem->wait_list) ?RWSEM_FLAG_WAITERS :0 ); + (list_is_singular (&sem->wait_list) ?RWSEM_FLAG_WAITERS :0 ) - + (count &RWSEM_FLAG_HANDOFF );if (atomic_cmpxchg_acquire (&sem->count, count, new) == count) {rwsem_set_owner (sem);
@@ -206,7 +235,7 @@ static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)int old, count =atomic_read (&sem->count);while (true) { -if (RWSEM_COUNT_IS_LOCKED (count)) +if (RWSEM_COUNT_IS_LOCKED_OR_HANDOFF (count))return false; old = atomic_cmpxchg_acquire(&sem->count, count,
@@ -362,6 +391,16 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)#endif /* +* This is safe to be called without holding the wait_lock. +*/ +static inline bool +rwsem_waiter_is_first (struct rw_semaphore *sem,struct rwsem_waiter *waiter) +{ + return list_first_entry(&sem->wait_list, struct rwsem_waiter, list) + == waiter; +} + +/* * Wait for the read lock to be granted */ static inline struct rw_semaphore __sched *
@@ -373,6 +412,7 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) waiter.task = current; waiter.type =RWSEM_WAITING_FOR_READ ; + waiter.timeout = jiffies +RWSEM_WAIT_TIMEOUT ;raw_spin_lock_irq (&sem->wait_lock);if (list_empty (&sem->wait_list))
@@ -413,8 +453,13 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)return sem; out_nolock:list_del (&waiter.list); -if (list_empty (&sem->wait_list)) -atomic_add (-RWSEM_FLAG_WAITERS , &sem->count); +if (list_empty (&sem->wait_list)) { +int adjustment = -RWSEM_FLAG_WAITERS ; + +if (RWSEM_COUNT_IS_HANDOFF (atomic_read (&sem->count))) + adjustment -=RWSEM_FLAG_HANDOFF ; +atomic_add (adjustment, &sem->count); + }raw_spin_unlock_irq (&sem->wait_lock);__set_current_state (TASK_RUNNING );return ERR_PTR (-EINTR );
@@ -441,7 +486,7 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) __rwsem_down_write_failed_common(struct rw_semaphore *sem, int state) { int count; -bool waiting = true;/* any queued threads before us */ +bool first = false;/* First one in queue */ struct rwsem_waiter waiter;struct rw_semaphore *ret = sem;DEFINE_WAKE_Q (wake_q);
@@ -456,17 +501,18 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)*/ waiter.task = current; waiter.type =RWSEM_WAITING_FOR_WRITE ; + waiter.timeout = jiffies +RWSEM_WAIT_TIMEOUT ;raw_spin_lock_irq (&sem->wait_lock);/* account for this before adding a new element to the list */ if (list_empty (&sem->wait_list)) - waiting = false; + first = true;list_add_tail (&waiter.list, &sem->wait_list);/* we're now waiting on the lock, but no longer actively locking */ -if (waiting) { +if (!first) { count =atomic_read (&sem->count);/*
@@ -498,19 +544,30 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)/* wait until we successfully acquire the lock */ set_current_state (state);while (true) { -if (rwsem_try_write_lock (count, sem)) +if (rwsem_try_write_lock (count, sem, first))break ; +raw_spin_unlock_irq (&sem->wait_lock);/* Block until there are no active lockers. */ -do { +for (;;) {if (signal_pending_state (state, current)) goto out_nolock;schedule ();set_current_state (state); count =atomic_read (&sem->count); - }while (RWSEM_COUNT_IS_LOCKED (count)); + +if (!first) + first =rwsem_waiter_is_first (sem, &waiter); + +if (!RWSEM_COUNT_IS_LOCKED (count)) +break ; + +if (first && !RWSEM_COUNT_IS_HANDOFF (count) && +time_after (jiffies, waiter.timeout)) +atomic_or (RWSEM_FLAG_HANDOFF , &sem->count); + }raw_spin_lock_irq (&sem->wait_lock); }
@@ -524,10 +581,15 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)__set_current_state (TASK_RUNNING );raw_spin_lock_irq (&sem->wait_lock);list_del (&waiter.list); -if (list_empty (&sem->wait_list)) -atomic_add (-RWSEM_FLAG_WAITERS , &sem->count); -else +if (list_empty (&sem->wait_list)) { +int adjustment = -RWSEM_FLAG_WAITERS ; + +if (RWSEM_COUNT_IS_HANDOFF (atomic_read (&sem->count))) + adjustment -=RWSEM_FLAG_HANDOFF ; +atomic_add (adjustment, &sem->count); + }else {__rwsem_mark_wake (sem,RWSEM_WAKE_ANY , &wake_q); + }raw_spin_unlock_irq (&sem->wait_lock);wake_up_q (&wake_q);
@@ -553,7 +615,7 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)* - up_read/up_write has decremented the active part of count if we come here */ __visible -struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) +struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem, int count) {unsigned long flags;DEFINE_WAKE_Q (wake_q);
@@ -586,7 +648,9 @@ struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)smp_rmb ();/* -* If a spinner is present, it is not necessary to do the wakeup. +* If a spinner is present and the handoff flag isn't set, it is +* not necessary to do the wakeup. +* * Try to do wakeup only if the trylock succeeds to minimize * spinlock contention which may introduce too much delay in the * unlock operation.
@@ -605,7 +669,7 @@ struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem)* rwsem_has_spinner() is true, it will guarantee at least one * trylock attempt on the rwsem later on. */ -if (rwsem_has_spinner (sem)) { +if (rwsem_has_spinner (sem) && !(count &RWSEM_FLAG_HANDOFF )) {/* * The smp_rmb() here is to make sure that the spinner * state is consulted before reading the wait_lock.
kernel/locking/rwsem-xadd.h +15 -7
@@ -74,7 +74,8 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)* * Bit 0 - writer locked bit * Bit 1 - waiters present bit -* Bits 2-7 - reserved +* Bit 2 - lock handoff bit +* Bits 3-7 - reserved * Bits 8-31 - 24-bit reader count * * atomic_fetch_add() is used to obtain reader lock, whereas atomic_cmpxchg()
@@ -82,19 +83,24 @@ static inline void rwsem_set_reader_owned(struct rw_semaphore *sem)*/ #define RWSEM_WRITER_LOCKED 0X00000001#define RWSEM_FLAG_WAITERS 0X00000002 +#define RWSEM_FLAG_HANDOFF 0X00000004#define RWSEM_READER_BIAS 0x00000100#define RWSEM_READER_SHIFT 8#define RWSEM_READER_MASK (~((1U << RWSEM_READER_SHIFT) - 1))#define RWSEM_LOCK_MASK (RWSEM_WRITER_LOCKED|RWSEM_READER_MASK) -#define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_LOCKED|RWSEM_FLAG_WAITERS) +#define RWSEM_READ_FAILED_MASK (RWSEM_WRITER_LOCKED|RWSEM_FLAG_WAITERS|\ + RWSEM_FLAG_HANDOFF)#define RWSEM_COUNT_IS_LOCKED (c) ((c) & RWSEM_LOCK_MASK) +#define RWSEM_COUNT_IS_HANDOFF (c) ((c) & RWSEM_FLAG_HANDOFF) +#define RWSEM_COUNT_IS_LOCKED_OR_HANDOFF (c) \ + ((c) & (RWSEM_LOCK_MASK|RWSEM_FLAG_HANDOFF))extern struct rw_semaphore *rwsem_down_read_failed (struct rw_semaphore *sem);extern struct rw_semaphore *rwsem_down_read_failed_killable (struct rw_semaphore *sem);extern struct rw_semaphore *rwsem_down_write_failed (struct rw_semaphore *sem);extern struct rw_semaphore *rwsem_down_write_failed_killable (struct rw_semaphore *sem); -extern struct rw_semaphore *rwsem_wake (struct rw_semaphore *); +extern struct rw_semaphore *rwsem_wake (struct rw_semaphore *,int count);extern struct rw_semaphore *rwsem_downgrade_wake (struct rw_semaphore *sem);/*
@@ -165,7 +171,7 @@ static inline void __up_read(struct rw_semaphore *sem) tmp =atomic_add_return_release (-RWSEM_READER_BIAS , &sem->count);if (unlikely ((tmp & (RWSEM_LOCK_MASK |RWSEM_FLAG_WAITERS )) ==RWSEM_FLAG_WAITERS )) -rwsem_wake (sem); +rwsem_wake (sem, tmp); }/*
@@ -173,10 +179,12 @@ static inline void __up_read(struct rw_semaphore *sem)*/ static inline void __up_write (struct rw_semaphore *sem) { +int tmp; +rwsem_clear_owner (sem); -if (unlikely (atomic_fetch_add_release (-RWSEM_WRITER_LOCKED , - &sem->count) &RWSEM_FLAG_WAITERS )) -rwsem_wake (sem); + tmp =atomic_fetch_add_release (-RWSEM_WRITER_LOCKED , &sem->count); +if (unlikely (tmp &RWSEM_FLAG_WAITERS )) +rwsem_wake (sem, tmp); }/*
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 8/11 locking/rwsem: Enable readers spinning on writer
In reply to Waiman LongThis patch enables readers to optimistically spin on a rwsem when it is owned by a writer instead of going to sleep directly. The rwsem_can_spin_on_owner() function is extracted out of rwsem_optimistic_spin() and is called directly by rwsem_down_read_failed() and rwsem_down_write_failed().
This patch may actually reduce performance under certain circumstances as the readers may not be grouped together in the wait queue anymore. So we may have a number of small reader groups among writers instead of a large reader group. However, this change is needed for some of the subsequent patches.
--- kernel/locking/rwsem-xadd.c | 66 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 9 deletions(-)
kernel/locking/rwsem-xadd.c +57 -9
@@ -228,6 +228,28 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,#ifdef CONFIG_RWSEM_SPIN_ON_OWNER /* +* Try to acquire read lock before the reader is put on wait queue. +*/ +static inline bool rwsem_try_read_lock_unqueued (struct rw_semaphore *sem) +{ +int count =atomic_read (&sem->count); + +if (count & (RWSEM_FLAG_HANDOFF |RWSEM_WRITER_LOCKED )) +return false; + + count =atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count); +if (!(count & (RWSEM_FLAG_HANDOFF |RWSEM_WRITER_LOCKED ))) { +if (!(count >>RWSEM_READER_SHIFT )) +rwsem_set_reader_owned (sem); +return true; + } + +/* Back out the change */ +atomic_add (-RWSEM_READER_BIAS , &sem->count); +return false; +} + +/* * Try to acquire write lock before the writer has been put on wait queue. */ static inline bool rwsem_try_write_lock_unqueued (struct rw_semaphore *sem)
@@ -318,16 +340,14 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)return !rwsem_owner_is_reader (READ_ONCE (sem->owner)); } -static bool rwsem_optimistic_spin (struct rw_semaphore *sem) +static bool +rwsem_optimistic_spin (struct rw_semaphore *sem,enum rwsem_waiter_type type) {bool taken = false;preempt_disable ();/* sem->wait_lock should not be held when doing optimistic spinning */ -if (!rwsem_can_spin_on_owner (sem)) - goto done; -if (!osq_lock (&sem->osq)) goto done;
@@ -342,10 +362,12 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem)/* * Try to acquire the lock */ -if (rwsem_try_write_lock_unqueued (sem)) { - taken = true; + taken = (type ==RWSEM_WAITING_FOR_WRITE ) + ?rwsem_try_write_lock_unqueued (sem) + :rwsem_try_read_lock_unqueued (sem); + +if (taken)break ; - }/* * When there's no owner, we might have preempted between the
@@ -379,7 +401,13 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) }#else -static bool rwsem_optimistic_spin (struct rw_semaphore *sem) +static inline bool rwsem_can_spin_on_owner (struct rw_semaphore *sem) +{ +return false; +} + +static inline bool +rwsem_optimistic_spin (struct rw_semaphore *sem,enum rwsem_waiter_type type) {return false; }
@@ -406,10 +434,29 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) static inline struct rw_semaphore __sched *__rwsem_down_read_failed_common (struct rw_semaphore *sem,int state) { +bool can_spin;int count, adjustment = -RWSEM_READER_BIAS ;struct rwsem_waiter waiter;DEFINE_WAKE_Q (wake_q); +/* +* Undo read bias from down_read operation to stop active locking if: +* 1) Optimistic spinners are present; or +* 2) optimistic spinning is allowed. +*/ + can_spin =rwsem_can_spin_on_owner (sem); +if (can_spin ||rwsem_has_spinner (sem)) { +atomic_add (-RWSEM_READER_BIAS , &sem->count); + adjustment =0 ; + +/* +* Do optimistic spinning and steal lock if possible. +*/ +if (can_spin && +rwsem_optimistic_spin (sem,RWSEM_WAITING_FOR_READ )) +return sem; + } + waiter.task = current; waiter.type =RWSEM_WAITING_FOR_READ ; waiter.timeout = jiffies +RWSEM_WAIT_TIMEOUT ;
@@ -492,7 +539,8 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)DEFINE_WAKE_Q (wake_q);/* do optimistic spinning and steal lock if possible */ -if (rwsem_optimistic_spin (sem)) +if (rwsem_can_spin_on_owner (sem) && +rwsem_optimistic_spin (sem,RWSEM_WAITING_FOR_WRITE ))return sem;/*
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 9/11 locking/rwsem: Enable time-based reader lock stealing
In reply to Waiman LongBecause of writer lock stealing and optimistic spinning, writers were preferred over readers in rwsem. However, preferring readers is generally good for performance. However, we need to do it in a way that won't starve writers as an incoming stream of readers will not allow a wakeup call to happen which is needed to allow a waiting writer to set the handoff bit to initiate the handoff protocol.
Now the owner field is extended to hold a timestamp put in by the first reader that acquires the lock. An incoming reader is allowed to steal the lock if the lock is reader owned, the handoff bit isn't set and the owner timestamp matches the current time.
--- kernel/locking/rwsem-xadd.c | 45 +++++++++++++++++++++++++++------- kernel/locking/rwsem-xadd.h | 60 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 22 deletions(-)
kernel/locking/rwsem-xadd.c +36 -9
@@ -150,10 +150,11 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,return ; }/* -* Set it to reader-owned for first reader. +* Since the wait queue head is a reader, we can set a +* new timestamp even if it is not the first reader to +* acquire the current lock. */ -if (!(oldcount >>RWSEM_READER_SHIFT )) -rwsem_set_reader_owned (sem); +rwsem_set_reader_owned (sem); }/*
@@ -400,6 +401,22 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) return osq_is_locked(&sem->osq); } +static inline bool +rwsem_reader_steal_lock (struct rw_semaphore *sem,int count) +{ +struct task_struct *owner =READ_ONCE (sem->owner); + +/* +* Reader can steal the lock if: +* 1) the lock is reader-owned; +* 2) the handoff bit isn't set; and +* 3) the time stamp in the owner field matches the current time +* when it is properly initialized. +*/ +return !(count & (RWSEM_WRITER_LOCKED |RWSEM_FLAG_HANDOFF )) && + (!rwsem_owner_is_reader (owner) || +rwsem_owner_timestamp_match (owner)); +}#else static inline bool rwsem_can_spin_on_owner (struct rw_semaphore *sem) {
@@ -416,6 +433,12 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) {return false; } + +static inline bool +rwsem_reader_steal_lock (struct rw_semaphore *sem,int count) +{ +return false; +}#endif /*
@@ -432,13 +455,16 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)* Wait for the read lock to be granted */ static inline struct rw_semaphore __sched * -__rwsem_down_read_failed_common (struct rw_semaphore *sem,int state) +__rwsem_down_read_failed_common (struct rw_semaphore *sem,int count,int state) {bool can_spin; -int count, adjustment = -RWSEM_READER_BIAS ; +int adjustment = -RWSEM_READER_BIAS ;struct rwsem_waiter waiter;DEFINE_WAKE_Q (wake_q); +if (rwsem_reader_steal_lock (sem, count)) +return sem; +/* * Undo read bias from down_read operation to stop active locking if: * 1) Optimistic spinners are present; or
@@ -513,16 +539,17 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem) } __visible struct rw_semaphore * __sched -rwsem_down_read_failed(struct rw_semaphore *sem) +rwsem_down_read_failed(struct rw_semaphore *sem, int count) { -return __rwsem_down_read_failed_common (sem,TASK_UNINTERRUPTIBLE ); +return __rwsem_down_read_failed_common (sem, count, +TASK_UNINTERRUPTIBLE ); }EXPORT_SYMBOL (rwsem_down_read_failed); __visible struct rw_semaphore * __sched -rwsem_down_read_failed_killable(struct rw_semaphore *sem) +rwsem_down_read_failed_killable(struct rw_semaphore *sem, int count) { -return __rwsem_down_read_failed_common (sem,TASK_KILLABLE ); +return __rwsem_down_read_failed_common (sem, count,TASK_KILLABLE ); }EXPORT_SYMBOL (rwsem_down_read_failed_killable);
kernel/locking/rwsem-xadd.h +47 -13
106 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 10/11 locking/rwsem: Make rwsem_spin_on_owner() return a tri-state value
In reply to Waiman LongThis patch modifies rwsem_spin_on_owner() to return a tri-state value to better reflect the state of lock holder which enables us to make a better decision of what to do next.
--- kernel/locking/rwsem-xadd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
kernel/locking/rwsem-xadd.c +9 -5
@@ -301,9 +301,13 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) }/* -* Return true only if we can still spin on the owner field of the rwsem. +* Return the folowing three values depending on the lock owner state. +* 1 when owner has changed and no reader is detected yet. +* 0 when owner has change and/or owner is a reader. +* -1 when optimistic spinning has to stop because either the owner stops +* running or its timeslice has been used up. */ -static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem) +static noinline int rwsem_spin_on_owner(struct rw_semaphore *sem) {struct task_struct *owner =READ_ONCE (sem->owner);
@@ -327,7 +331,7 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)if (!owner->on_cpu ||need_resched () ||vcpu_is_preempted (task_cpu (owner))) {rcu_read_unlock (); -return false; +return -1 ; }cpu_relax ();
@@ -338,7 +342,7 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)* If there is a new owner or the owner is not set, we continue * spinning. */ -return !rwsem_owner_is_reader (READ_ONCE (sem->owner)); +return rwsem_owner_is_reader (READ_ONCE (sem->owner)) ?0 :1 ; } static bool
@@ -359,7 +363,7 @@ static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)* 2) readers own the lock as we can't determine if they are * actively running or not. */ -while (rwsem_spin_on_owner (sem)) { +while (rwsem_spin_on_owner (sem) >0 ) {/* * Try to acquire the lock */
Signature · 1 line
1.8.3.1
Waiman Long
reviewerPATCH v6 11/11 locking/rwsem: Enable count-based spinning on reader
In reply to Waiman LongWhen the rwsem is owned by reader, writers stop optimistic spinning simply because there is no easy way to figure out if all the readers are actively running or not. However, there are scenarios where the readers are unlikely to sleep and optimistic spinning can help performance.
This patch provides a simple mechanism for spinning on a reader-owned rwsem. It is a loop count threshold based spinning where the count will get reset whenenver the the rwsem reader count value changes indicating that the rwsem is still active. There is another maximum count value that limits that maximum number of spinnings that can happen.
When the loop or max counts reach 0, a bit will be set in the owner field to indicate that no more optimistic spinning should be done on this rwsem until it becomes writer owned again.
The spinning threshold and maximum values can be overridden by architecture specific rwsem.h header file, if necessary. The current default threshold value is 512 iterations.
On a 2-socket 40-core x86-64 Gold 6148 system, a rwsem microbenchmark was run with 40 locking threads (one/core) doing 10s of equal number of reader and writer lock/unlock operations on the same rwsem alternatively, the resulting locking total rates on a 4.14 based kernel were 927 kop/s and 3218 kop/s without and with the patch respectively. That was an increase of about 247%.
--- kernel/locking/rwsem-xadd.c | 97 +++++++++++++++++++++++++++++++++++++++------ kernel/locking/rwsem-xadd.h | 27 +++++++++++++ 2 files changed, 111 insertions(+), 13 deletions(-)
kernel/locking/rwsem-xadd.c +84 -13
@@ -90,6 +90,22 @@ enum rwsem_wake_type {#define RWSEM_WAIT_TIMEOUT ((HZ - 1)/100 + 1)/* +* Reader-owned rwsem spinning threshold and maximum value +* +* This threshold and maximum values can be overridden by architecture +* specific value. The loop count will be reset whenenver the rwsem count +* value changes. The max value constrains the total number of reader-owned +* lock spinnings that can happen. +*/ +#ifdef ARCH_RWSEM_RSPIN_THRESHOLD +# define RWSEM_RSPIN_THRESHOLD ARCH_RWSEM_RSPIN_THRESHOLD +# define RWSEM_RSPIN_MAX ARCH_RWSEM_RSPIN_MAX +#else +# define RWSEM_RSPIN_THRESHOLD (1 << 9) +# define RWSEM_RSPIN_MAX (1 << 12) +#endif + +/* * handle the lock release when processes blocked on it that can now run * - if we come here from up_xxxx(), then: * - the 'active part' of count (&0x0000ffff) reached 0 (but may have changed)
@@ -230,8 +246,17 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,#ifdef CONFIG_RWSEM_SPIN_ON_OWNER /* * Try to acquire read lock before the reader is put on wait queue. +* +* To avoid writer starvation, it cannot acquire the lock if the reader +* count isn't 0 and there is a timestamp mismatch. In this case, the +* reader has to stop spinning. +* +* This will at least allow the reader count to go to 0 and wake up the +* first one in the wait queue which can initiate the handoff protocol, +* if necessary. */ -static inline bool rwsem_try_read_lock_unqueued (struct rw_semaphore *sem) +static inline bool rwsem_try_read_lock_unqueued (struct rw_semaphore *sem, +bool *stop_spin) {int count =atomic_read (&sem->count);
@@ -240,11 +265,21 @@ static inline bool rwsem_try_read_lock_unqueued(struct rw_semaphore *sem) count =atomic_fetch_add_acquire (RWSEM_READER_BIAS , &sem->count);if (!(count & (RWSEM_FLAG_HANDOFF |RWSEM_WRITER_LOCKED ))) { -if (!(count >>RWSEM_READER_SHIFT )) +if (!(count >>RWSEM_READER_SHIFT )) {rwsem_set_reader_owned (sem); + }else { +struct task_struct *owner =READ_ONCE (sem->owner); + +if (rwsem_owner_is_reader (owner) && + !rwsem_owner_timestamp_match (owner)) { + *stop_spin = true; + goto backout; + } + }return true; } +backout:/* Back out the change */ atomic_add (-RWSEM_READER_BIAS , &sem->count);return false;
@@ -272,7 +307,8 @@ static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem) } } -static inline bool rwsem_can_spin_on_owner (struct rw_semaphore *sem) +static inline bool rwsem_can_spin_on_owner (struct rw_semaphore *sem, +bool reader) {struct task_struct *owner;bool ret = true;
@@ -284,9 +320,9 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem) owner =READ_ONCE (sem->owner);if (!rwsem_owner_is_writer (owner)) {/* -* Don't spin if the rwsem is readers owned. +* Don't spin if the rspin disable bit is set for writer. */ - ret = !rwsem_owner_is_reader (owner); + ret = reader || !rwsem_owner_is_spin_disabled (owner); goto done; }
@@ -349,6 +385,11 @@ static noinline int rwsem_spin_on_owner(struct rw_semaphore *sem) rwsem_optimistic_spin(struct rw_semaphore *sem, enum rwsem_waiter_type type) { bool taken = false; +bool stop_spin = false; +int owner_state;/* Lock owner state */ +int rspin_cnt =RWSEM_RSPIN_THRESHOLD ; +int rspin_max =RWSEM_RSPIN_MAX ; +int old_count =0 ;preempt_disable ();
@@ -356,25 +397,55 @@ static noinline int rwsem_spin_on_owner(struct rw_semaphore *sem)if (!osq_lock (&sem->osq)) goto done; +if (rwsem_is_spin_disabled (sem)) + rspin_cnt =0 ; +/* * Optimistically spin on the owner field and attempt to acquire the * lock whenever the owner changes. Spinning will be stopped when: -* 1) the owning writer isn't running; or -* 2) readers own the lock as we can't determine if they are -* actively running or not. +* 1) the owning writer isn't running; +* 2) writer: readers own the lock and spinning count has reached 0; +* 3) reader: timestamp mismatch. */ -while (rwsem_spin_on_owner (sem) >0 ) { +while ((owner_state =rwsem_spin_on_owner (sem)) >=0 ) {/* * Try to acquire the lock */ taken = (type ==RWSEM_WAITING_FOR_WRITE ) ?rwsem_try_write_lock_unqueued (sem) - :rwsem_try_read_lock_unqueued (sem); + :rwsem_try_read_lock_unqueued (sem, &stop_spin); -if (taken) +if (taken || stop_spin)break ;/* +* We only decremnt the rspin_cnt when the lock is owned +* by readers (owner_state == 0). In which case, +* rwsem_spin_on_owner() will essentially be a no-op +* and we will be spinning in this main loop. The spinning +* count will be reset whenever the rwsem count value +* changes. +*/ +if (!owner_state) { +int count; + +if (!rspin_cnt || !rspin_max) { +if (!rwsem_is_spin_disabled (sem)) +rwsem_set_spin_disabled (sem); +break ; + } + + count =atomic_read (&sem->count) >>RWSEM_READER_SHIFT ; +if (count != old_count) { + old_count = count; + rspin_cnt =RWSEM_RSPIN_THRESHOLD ; + }else { + rspin_cnt--; + } + rspin_max--; + } + +/* * When there's no owner, we might have preempted between the * owner acquiring the lock and setting the owner field. If * we're an RT task that will live-lock because we won't let
@@ -474,7 +545,7 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)* 1) Optimistic spinners are present; or * 2) optimistic spinning is allowed. */ - can_spin =rwsem_can_spin_on_owner (sem); + can_spin =rwsem_can_spin_on_owner (sem, true);if (can_spin ||rwsem_has_spinner (sem)) {atomic_add (-RWSEM_READER_BIAS , &sem->count); adjustment =0 ;
@@ -570,7 +641,7 @@ static inline bool rwsem_has_spinner(struct rw_semaphore *sem)DEFINE_WAKE_Q (wake_q);/* do optimistic spinning and steal lock if possible */ -if (rwsem_can_spin_on_owner (sem) && +if (rwsem_can_spin_on_owner (sem, false) &&rwsem_optimistic_spin (sem,RWSEM_WAITING_FOR_WRITE ))return sem;
kernel/locking/rwsem-xadd.h +27 -0
@@ -27,6 +27,7 @@* - a writer owns the lock */ #define RWSEM_READER_OWNED (1UL) +#define RWSEM_SPIN_DISABLED (2UL)#define RWSEM_READER_TIMESTAMP_SHIFT 8#define RWSEM_READER_TIMESTAMP_MASK \ ~((1UL << RWSEM_READER_TIMESTAMP_SHIFT) - 1)
@@ -81,6 +82,32 @@ static inline bool rwsem_is_reader_owned(struct rw_semaphore *sem) {return rwsem_owner_is_reader (READ_ONCE (sem->owner)); } + +static inline bool rwsem_owner_is_spin_disabled (struct task_struct *owner) +{ +return (unsigned long )owner &RWSEM_SPIN_DISABLED ; +} + +/* +* Try to set an optimistic spinning disable bit while it is reader-owned. +*/ +static inline void rwsem_set_spin_disabled (struct rw_semaphore *sem) +{ +struct task_struct *owner =READ_ONCE (sem->owner); + +/* +* Failure in cmpxchg() will be ignored, and the caller is expected +* to retry later. +*/ +if (rwsem_owner_is_reader (owner)) +cmpxchg (&sem->owner, owner, + (void *)((unsigned long )owner|RWSEM_SPIN_DISABLED )); +} + +static inline bool rwsem_is_spin_disabled (struct rw_semaphore *sem) +{ +return rwsem_owner_is_spin_disabled (READ_ONCE (sem->owner)); +}#else static inline void rwsem_set_owner (struct rw_semaphore *sem) {
Signature · 1 line
1.8.3.1
Peter Zijlstra
maintainerQuoted 11 lines · Waiman Long
# of Patches Reader Writer Applied Locking Rate Locking Rate ------------ ------------ ------------ 0 5,155/ 5,155/ 5,155 5,154/248,852/346,281 7 5,696/ 5,697/ 5,698 113,500/215,826/320,872 8 4,827/ 5,047/ 5,215 4,826/176,797/284,069 9 211,276/ 509,712/1,134,007 4,894/221,839/246,818 11 884,513/1,043,989/1,252,533 9,604/ 11,105/ 25,225It can be seen that rwsem changes from writer-preferring to reader-preferring.
A bit radically so, you almost starve the writers there.
Waiman Long
reviewerQuoted 10 lines · Peter Zijlstra
Quoted 9 lines · Waiman Long
# of Patches Reader Writer Applied Locking Rate Locking Rate ------------ ------------ ------------ 0 5,155/ 5,155/ 5,155 5,154/248,852/346,281 7 5,696/ 5,697/ 5,698 113,500/215,826/320,872 8 4,827/ 5,047/ 5,215 4,826/176,797/284,069 9 211,276/ 509,712/1,134,007 4,894/221,839/246,818 11 884,513/1,043,989/1,252,533 9,604/ 11,105/ 25,225
Quoted 3 lines
Quoted 2 lines
It can be seen that rwsem changes from writer-preferring to reader-preferring.
A bit radically so, you almost starve the writers there.
Yes, almost, but the lock handoff code will make sure that it won't actually get starved. That is why I added aggressive reader lock stealing after the lock handoff code.
Cheers,
Longman
Quoted 10 lines · Peter Zijlstra
Quoted 9 lines · Waiman Long
# of Patches Reader Writer Applied Locking Rate Locking Rate ------------ ------------ ------------ 0 5,155/ 5,155/ 5,155 5,154/248,852/346,281 7 5,696/ 5,697/ 5,698 113,500/215,826/320,872 8 4,827/ 5,047/ 5,215 4,826/176,797/284,069 9 211,276/ 509,712/1,134,007 4,894/221,839/246,818 11 884,513/1,043,989/1,252,533 9,604/ 11,105/ 25,225
Quoted 4 lines
Quoted 2 lines
It can be seen that rwsem changes from writer-preferring to reader-preferring.
A bit radically so, you almost starve the writers there.
Which is a bit of a problem for us, because we often use the write locks as an IO barrier for operations like truncate, fallocate, etc. i.e. we want it to immediately block readers.
That's going to be a bit of a problem if, for example, we have so many AIO-based direct IO writers on a file we can't get fallocate to run in a timely fashion to preallocate the space the writers are soon going to write into.
Not to mention the AIO-DIO append case where we have multiple concurrent writers at EOF, and so every so often one of the many IOs needs to take the write lock extending EOF safely. Blocking that for 10ms waiting for a hand-off is going to make all the people who care about deterministic IO latency go nuts....
So from my perspective on the IO side, I'd much prefer a write bias. Indeed, if we go back to the Irix XFS code, all these locks we defined as "MR_BARRIER" locks, which meant the XFS rwsems were specifically intended to have writer bias.
I think we can live with a fair r/w bias, but swinging from a 50:1 write bias to a 100:1 read bias is going change behaviour dramatically, and in many cases it won't be an improvement...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Quoted 20 lines · Waiman Long
In term of rwsem performance, a rwsem microbenchmark and fio randrw test with a xfs filesystem on a ramdisk were used to verify the performance changes due to these patches. Both tests were run on a 2-socket, 40-core Gold 6148 system. The rwsem microbenchmark (1:1 reader/writer ratio) has short critical section while the fio randrw test has long critical section (4k read/write).
The following table shows the performance of the rwsem microbenchmark and fio radrw test with different number of patches applied on 4.14 based kernels:
# of Patches Locking Rate FIO Bandwidth FIO Bandwidth Applied 40 threads 32 threads 16 threads ------------ ------------ ------------- ------------- 0 38.7 kop/s 706 MB/s 704 MB/s 7 38.6 kop/s 668 MB/s 663 MB/s 8 38.9 kop/s 704 MB/s 701 MB/s 9 39.1 kop/s 702 MB/s 707 MB/s 11 3218.0 kop/s 2594 MB/s 2614 MB/s
Quoted 2 lines
So this patchset improves mixed read/write rwsem microbench by 83X and randrw fio bandwidth by about 3.7X.
Overall improvement in bandwidth is not necessarily a good thing - this could simply demonstrate total write bandwidth starvation and so it's only reporting read bandwith. It's much more important to look at the change in read bandwidth vs write bandwidth in the fio test. i.e. exactly how did the IO balance change as a result of changing the locking bias?
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Waiman Long
reviewerQuoted 21 lines · Dave Chinner
Quoted 20 lines · Waiman Long
In term of rwsem performance, a rwsem microbenchmark and fio randrw test with a xfs filesystem on a ramdisk were used to verify the performance changes due to these patches. Both tests were run on a 2-socket, 40-core Gold 6148 system. The rwsem microbenchmark (1:1 reader/writer ratio) has short critical section while the fio randrw test has long critical section (4k read/write).
The following table shows the performance of the rwsem microbenchmark and fio radrw test with different number of patches applied on 4.14 based kernels:
# of Patches Locking Rate FIO Bandwidth FIO Bandwidth Applied 40 threads 32 threads 16 threads ------------ ------------ ------------- ------------- 0 38.7 kop/s 706 MB/s 704 MB/s 7 38.6 kop/s 668 MB/s 663 MB/s 8 38.9 kop/s 704 MB/s 701 MB/s 9 39.1 kop/s 702 MB/s 707 MB/s 11 3218.0 kop/s 2594 MB/s 2614 MB/s
Quoted 8 lines
Quoted 2 lines
So this patchset improves mixed read/write rwsem microbench by 83X and randrw fio bandwidth by about 3.7X.
Overall improvement in bandwidth is not necessarily a good thing - this could simply demonstrate total write bandwidth starvation and so it's only reporting read bandwith. It's much more important to look at the change in read bandwidth vs write bandwidth in the fio test. i.e. exactly how did the IO balance change as a result of changing the locking bias?
Thanks for the input. I can take out the reader lock stealing part. That will give it a more fair reader/writer bias. It can also be an option that be set when the rwsem is inited.
Cheers,
Longman