diff options
| author | Jason Low <jason.low2@hpe.com> | 2016-05-16 20:38:00 -0400 |
|---|---|---|
| committer | Ingo Molnar <mingo@kernel.org> | 2016-06-03 03:47:13 -0400 |
| commit | c0fcb6c2d332041256dc55d8a1ec3c0a2d0befb8 (patch) | |
| tree | eaef72a2761e059aed127bf863e1b606c44a5da7 /kernel/locking | |
| parent | e38513905eeaae59056eac2c9ac55a43b1fc41b2 (diff) | |
locking/rwsem: Optimize write lock by reducing operations in slowpath
When acquiring the rwsem write lock in the slowpath, we first try
to set count to RWSEM_WAITING_BIAS. When that is successful,
we then atomically add the RWSEM_WAITING_BIAS in cases where
there are other tasks on the wait list. This causes write lock
operations to often issue multiple atomic operations.
We can instead make the list_is_singular() check first, and then
set the count accordingly, so that we issue at most 1 atomic
operation when acquiring the write lock and reduce unnecessary
cacheline contention.
Signed-off-by: Jason Low <jason.low2@hpe.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Waiman Long<Waiman.Long@hpe.com>
Acked-by: Davidlohr Bueso <dave@stgolabs.net>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Christoph Lameter <cl@linux.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Cc: Jason Low <jason.low2@hp.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Martin Schwidefsky <schwidefsky@de.ibm.com>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Peter Hurley <peter@hurleysoftware.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Terry Rudd <terry.rudd@hpe.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Cc: Tony Luck <tony.luck@intel.com>
Link: http://lkml.kernel.org/r/1463445486-16078-2-git-send-email-jason.low2@hpe.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'kernel/locking')
| -rw-r--r-- | kernel/locking/rwsem-xadd.c | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c index fcbf75ac3dcb..b957da7fcb19 100644 --- a/kernel/locking/rwsem-xadd.c +++ b/kernel/locking/rwsem-xadd.c | |||
| @@ -261,17 +261,28 @@ struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) | |||
| 261 | } | 261 | } |
| 262 | EXPORT_SYMBOL(rwsem_down_read_failed); | 262 | EXPORT_SYMBOL(rwsem_down_read_failed); |
| 263 | 263 | ||
| 264 | /* | ||
| 265 | * This function must be called with the sem->wait_lock held to prevent | ||
| 266 | * race conditions between checking the rwsem wait list and setting the | ||
| 267 | * sem->count accordingly. | ||
| 268 | */ | ||
| 264 | static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem) | 269 | static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem) |
| 265 | { | 270 | { |
| 266 | /* | 271 | /* |
| 267 | * Try acquiring the write lock. Check count first in order | 272 | * Avoid trying to acquire write lock if count isn't RWSEM_WAITING_BIAS. |
| 268 | * to reduce unnecessary expensive cmpxchg() operations. | ||
| 269 | */ | 273 | */ |
| 270 | if (count == RWSEM_WAITING_BIAS && | 274 | if (count != RWSEM_WAITING_BIAS) |
| 271 | cmpxchg_acquire(&sem->count, RWSEM_WAITING_BIAS, | 275 | return false; |
| 272 | RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) { | 276 | |
| 273 | if (!list_is_singular(&sem->wait_list)) | 277 | /* |
| 274 | rwsem_atomic_update(RWSEM_WAITING_BIAS, sem); | 278 | * Acquire the lock by trying to set it to ACTIVE_WRITE_BIAS. If there |
| 279 | * are other tasks on the wait list, we need to add on WAITING_BIAS. | ||
| 280 | */ | ||
| 281 | count = list_is_singular(&sem->wait_list) ? | ||
| 282 | RWSEM_ACTIVE_WRITE_BIAS : | ||
| 283 | RWSEM_ACTIVE_WRITE_BIAS + RWSEM_WAITING_BIAS; | ||
| 284 | |||
| 285 | if (cmpxchg_acquire(&sem->count, RWSEM_WAITING_BIAS, count) == RWSEM_WAITING_BIAS) { | ||
| 275 | rwsem_set_owner(sem); | 286 | rwsem_set_owner(sem); |
| 276 | return true; | 287 | return true; |
| 277 | } | 288 | } |
