Skip to content

locking/rwsem: Rwsem rearchitecture part 2

Waiman Long lkml 8 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v8 Latest19 patches 46 repliesIn review
  2. v720 patches 19 repliesSuperseded
  3. v620 patches 5 repliesSuperseded
Earlier versions · 5
VersionPostedPatchesRepliesStatus
v518 patches5 repliesSuperseded
v416 patches82 repliesSuperseded
v314 patches20 repliesSuperseded
v212 patches17 repliesSuperseded
v112 patches7 repliesSuperseded

Changes in v8 from the cover letter

- Remove v7 patch 1 as it has been merged.
- Add patch 14 to make rwsem->owner an atomic_long_t as suggested by PeterZ.
- Incorporates other changes suggested by PeterZ like removing *_is_spinnable() and owner_without_flags() helpers.

Patches in v8 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/19locking/rwsem: Make owner available even if !CONFIG_RWSEM_SPIN_ON_OWNERinclude/linux/rwsem.h kernel/locking/rwsem-xadd.c kernel/locking/rwsem.h +1 more+10 -320 replies
2/19locking/rwsem: Remove rwsem_wake() wakeup optimizationkernel/locking/rwsem-xadd.c+0 -720 replies
3/19locking/rwsem: Implement a new locking schemekernel/locking/rwsem-xadd.c kernel/locking/rwsem.h+85 -1360 replies
4/19locking/rwsem: Merge rwsem.h and rwsem-xadd.c into rwsem.ckernel/locking/Makefile kernel/locking/rwsem-xadd.c kernel/locking/rwsem.c +1 more+891 -9000 replies
5/19locking/rwsem: Code cleanup after files mergingkernel/locking/lock_events_list.h kernel/locking/rwsem.c+42 -950 replies
6/19locking/rwsem: Make rwsem_spin_on_owner() return owner statekernel/locking/rwsem.c+47 -180 replies
7/19locking/rwsem: Implement lock handoff to prevent lock starvationkernel/locking/lock_events_list.h kernel/locking/rwsem.c+173 -546 replies
8/19locking/rwsem: Always release wait_lock before waking up tasksinclude/linux/sched/wake_q.h kernel/locking/rwsem.c+20 -160 replies
9/19locking/rwsem: More optimal RT task handling of null ownerkernel/locking/rwsem.c+44 -70 replies
10/19locking/rwsem: Wake up almost all readers in wait queuekernel/locking/rwsem.c+26 -50 replies
11/19locking/rwsem: Clarify usage of owner's nonspinaable bitinclude/linux/rwsem.h kernel/locking/rwsem.c+22 -230 replies
12/19locking/rwsem: Enable readers spinning on writerkernel/locking/lock_events_list.h kernel/locking/rwsem.c+75 -120 replies
13/19locking/rwsem: Make rwsem->owner an atomic_long_tinclude/linux/percpu-rwsem.h include/linux/rwsem.h kernel/locking/rwsem.c+88 -5214 replies
14/19locking/rwsem: Enable time-based spinning on reader-owned rwsemkernel/locking/lock_events_list.h kernel/locking/rwsem.c+144 -302 replies
15/19locking/rwsem: Adaptive disabling of reader optimistic spinningkernel/locking/lock_events_list.h kernel/locking/rwsem.c+136 -816 replies
16/19locking/rwsem: Guard against making count negativekernel/locking/rwsem.c+80 -153 replies
17/19locking/rwsem: Merge owner into count on x86-64arch/x86/Kconfig kernel/Kconfig.locks kernel/locking/rwsem.c+171 -95 replies
18/19locking/rwsem: Remove redundant computation of writer lock wordkernel/locking/rwsem.c+12 -80 replies
19/19locking/rwsem: Disable preemption in down_read*() if owner in countkernel/locking/rwsem.c+34 -40 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v8 yet.

Read the v8 thread

Cover letter [PATCH v8 00/19] locking/rwsem: Rwsem rearchitecture part 2

v8:
- Remove v7 patch 1 as it has been merged.
- Add patch 14 to make rwsem->owner an atomic_long_t as suggested by PeterZ.
- Incorporates other changes suggested by PeterZ like removing *_is_spinnable() and owner_without_flags() helpers.

v7:
- Fix a bug in patch 1 and include changes suggested by Linus.
- Refresh the other patches accordingly.

v6:
- Add a new patch 1 to fix an existing rwsem bug that allows both readers and writer to become rwsem owners simultaneously.
- Fix a missed wakeup bug (in patch 9) of the v5 series.
- Fix boot up hang problem on some kernel configurations caused by the patch that merge owner into count.
- Make the adaptive disabling of reader optimistic spinning more aggressive to further improve workloads that don't benefit from that.
- Add another patch to disable preemption during down_read() when owner is merged into count to prevent the possiblity of reader count overflow.
- Don't allow merging of owner into count if NR_CPUS is greater that the max supported reader count.
- Allow readers dropped out from the optimistic spinning loop to attempt a trylock if it is still in the right read phase.
- Fix incorrect DEBUG_RWSEMS_WARN_ON() warning.

This is part 2 of a 3-part (0/1/2) series to rearchitect the internal operation of rwsem. Both part 0 and part 1 are merged into tip.

This patchset revamps the current rwsem-xadd implementation to make it saner and easier to work with. It also implements the following 3 new features:

1) Waiter lock handoff
2) Reader optimistic spinning with adapative disabling
3) Store write-lock owner in the atomic count (x86-64 only for now)

Waiter lock handoff is similar to the mechanism currently in the mutex code. This ensures that lock starvation won't happen.

Reader optimistic spinning enables readers to acquire the lock more quickly. So workloads that use a mix of readers and writers should see an increase in performance as long as the reader critical sections are short. For those workloads that have long reader critical sections reader optimistic spinning may hurt performance, so an adaptive disabling mechanism is also implemented to disable it when reader-owned lock spinning timeouts happen.

Finally, storing the write-lock owner into the count will allow optimistic spinners to get to the lock holder's task structure more quickly and eliminating the timing gap where the write lock is acquired but the owner isn't known yet. This is important for RT tasks where spinning on a lock with an unknown owner is not allowed.

Because of the fact that multiple readers can share the same lock, there is a natural preference for readers when measuring in term of locking throughput as more readers are likely to get into the locking fast path than the writers. With waiter lock handoff, we are not going to starve the writers.

On a 2-socket 40-core 80-thread Skylake system with 40 reader and writer locking threads, the min/mean/max locking operations done in a 5-second testing window before the patchset were:

40 readers, Iterations Min/Mean/Max = 1,807/1,808/1,810 40 writers, Iterations Min/Mean/Max = 1,807/50,344/151,255

After the patchset, they became:

40 readers, Iterations Min/Mean/Max = 30,057/31,359/32,741 40 writers, Iterations Min/Mean/Max = 94,466/95,845/97,098

It can be seen that the performance improves for both readers and writers. It also makes rwsem fairer to readers as well as among different threads within the reader and writer groups.

Patch 1 makes owner a permanent member of the rw_semaphore structure and set it irrespective of CONFIG_RWSEM_SPIN_ON_OWNER.

Patch 2 removes rwsem_wake() wakeup optimization as it doesn't work with lock handoff.

Patch 3 implements a new rwsem locking scheme similar to what qrwlock is current doing. Write lock is done by atomic_cmpxchg() while read lock is still being done by atomic_add().

Patch 4 merges the content of rwsem.h and rwsem-xadd.c into rwsem.c just like the mutex. The rwsem-xadd.c is removed and a bare-bone rwsem.h is left for internal function declaration needed by percpu-rwsem.c.

Patch 5 optimizes the merged rwsem.c file to generate smaller object file and performs other miscellaneous code cleanups.

Patch 6 makes rwsem_spin_on_owner() returns owner state.

Patch 7 implments lock handoff to prevent lock starvation. It is expected that throughput will be lower on workloads with highly contended rwsems for better fairness.

Patch 8 makes sure that all wake_up_q() calls happened after dropping the wait_lock.

Patch 9 makes RT task's handling of NULL owner more optimal.

Patch 10 makes reader wakeup to wake up almost all the readers in the wait queue instead of just those in the front.

Patch 11 renames the RWSEM_ANONYMOUSLY_OWNED bit to RWSEM_NONSPINNABLE.

Patch 12 enables reader to spin on a writer-owned rwsem.

Patch 13 makes rwsem->owner an atomic_long_t as it is now holding some flags that may need to be atomically updated.

Patch 14 enables a writer to spin on a reader-owned rwsem for at most 25us and extends the RWSEM_NONSPINNABLE bit to 2 separate ones - one for readers and one for writers.

Patch 15 implements the adaptive disabling of reader optimistic spinning when the reader-owned rwsem spinning timeouts happen.

Patch 16 handles the case of too many readers by reserving the sign bit to designate that a reader lock attempt will fail and the locking reader will be put to sleep. This will ensure that we will not overflow the reader count.

Patch 17 merges the write-lock owner task pointer into the count. Only 64-bit count has enough space to provide a reasonable number of bits for reader count. This is for x86-64 only for the time being.

Patch 18 eliminates redundant computation of the merged owner-count.

Patch 19 disable preemption during the down_read() call to eliminate the remote possibility that the reader count may overflow.

With a locking microbenchmark running on 5.1 based kernel, the total locking rates (in kops/s) on a 2-socket Skylake system with equal numbers of readers and writers (mixed) before and after this patchset were:

   # of Threads   Before Patch      After Patch
   ------------   ------------      -----------
        2            2,618             4,193
        4            1,202             3,726
        8              802             3,622
       16              729             3,359
       32              319             2,826
       64              102             2,744

On workloads where the rwsem reader critical section is relatively long (longer than the spinning period), optimistic of writer on reader-owned rwsem may not be that helpful. In fact, the performance may regress in some cases like the will-it-sclae page_fault1 microbenchmark. This is likely due to the fact that larger reader groups where the readers acquire the lock together are broken into smaller ones. So more work will be needed to better tune the rwsem code to that kind of workload.

The v7-to-v8 diff is attached at the end.

Waiman Long (19):
locking/rwsem: Make owner available even if
!CONFIG_RWSEM_SPIN_ON_OWNER
locking/rwsem: Remove rwsem_wake() wakeup optimization locking/rwsem: Implement a new locking scheme locking/rwsem: Merge rwsem.h and rwsem-xadd.c into rwsem.c locking/rwsem: Code cleanup after files merging locking/rwsem: Make rwsem_spin_on_owner() return owner state locking/rwsem: Implement lock handoff to prevent lock starvation locking/rwsem: Always release wait_lock before waking up tasks locking/rwsem: More optimal RT task handling of null owner locking/rwsem: Wake up almost all readers in wait queue locking/rwsem: Clarify usage of owner's nonspinaable bit locking/rwsem: Enable readers spinning on writer locking/rwsem: Make rwsem->owner an atomic_long_t locking/rwsem: Enable time-based spinning on reader-owned rwsem locking/rwsem: Adaptive disabling of reader optimistic spinning locking/rwsem: Guard against making count negative locking/rwsem: Merge owner into count on x86-64 locking/rwsem: Remove redundant computation of writer lock word locking/rwsem: Disable preemption in down_read*() if owner in count

 arch/x86/Kconfig                  |    6 +
 include/linux/percpu-rwsem.h      |    4 +-
 include/linux/rwsem.h             |   16 +-
 include/linux/sched/wake_q.h      |    5 +
 kernel/Kconfig.locks              |   12 +
 kernel/locking/Makefile           |    2 +-
 kernel/locking/lock_events_list.h |   12 +-
 kernel/locking/rwsem-xadd.c       |  745 -------------
 kernel/locking/rwsem.c            | 1668 ++++++++++++++++++++++++++++-
 kernel/locking/rwsem.h            |  306 +-----
 lib/Kconfig.debug                 |    8 +-
 11 files changed, 1694 insertions(+), 1090 deletions(-)
 delete mode 100644 kernel/locking/rwsem-xadd.c
include/linux/percpu-rwsem.h +2 -2
index 03cb4b6f842e..0a43830f1932 100644
@@ -117,7 +117,7 @@ static inline void percpu_rwsem_release(struct percpu_rw_semaphore *sem,
 	lock_release(&sem->rw_sem.dep_map, 1, ip);
 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 	if (!read)
-		sem->rw_sem.owner = RWSEM_OWNER_UNKNOWN;
+		atomic_long_set(&sem->rw_sem.owner, RWSEM_OWNER_UNKNOWN);
 #endif
 }
 
@@ -127,7 +127,7 @@ static inline void percpu_rwsem_acquire(struct percpu_rw_semaphore *sem,
 	lock_acquire(&sem->rw_sem.dep_map, 0, 1, read, 1, NULL, ip);
 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 	if (!read)
-		sem->rw_sem.owner = current;
+		atomic_long_set(&sem->rw_sem.owner, (long)current);
 #endif
 }
 
include/linux/rwsem.h +6 -5
index bb76e82398b2..e401358c4e7e 100644
@@ -35,10 +35,11 @@
 struct rw_semaphore {
 	atomic_long_t count;
 	/*
-	 * Write owner or one of the read owners. Can be used as a
-	 * speculative check to see if the owner is running on the cpu.
+	 * Write owner or one of the read owners as well flags regarding
+	 * the current state of the rwsem. Can be used as a speculative
+	 * check to see if the write owner is running on the cpu.
 	 */
-	struct task_struct *owner;
+	atomic_long_t owner;
 #ifdef CONFIG_RWSEM_SPIN_ON_OWNER
 	struct optimistic_spin_queue osq; /* spinner MCS lock */
 #endif
@@ -53,7 +54,7 @@ struct rw_semaphore {
  * Setting all bits of the owner field except bit 0 will indicate
  * that the rwsem is writer-owned with an unknown owner.
  */
-#define RWSEM_OWNER_UNKNOWN	((struct task_struct *)-2L)
+#define RWSEM_OWNER_UNKNOWN	(-2L)
 
 /* In all implementations count != 0 means locked */
 static inline int rwsem_is_locked(struct rw_semaphore *sem)
@@ -80,7 +81,7 @@ static inline int rwsem_is_locked(struct rw_semaphore *sem)
 
 #define __RWSEM_INITIALIZER(name)				\
 	{ __RWSEM_INIT_COUNT(name),				\
-	  .owner = NULL,					\
+	  .owner = ATOMIC_LONG_INIT(0),				\
 	  .wait_list = LIST_HEAD_INIT((name).wait_list),	\
 	  .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(name.wait_lock)	\
 	  __RWSEM_OPT_INIT(name)				\
kernel/locking/rwsem.c +147 -157

631 lines not on this page · raw message