Skip to content

Commit a338ecb

Browse files
Waiman-Longingomolnar
authored andcommitted
locking/rwsem: Micro-optimize rwsem_try_read_lock_unqueued()
The atomic_long_cmpxchg_acquire() in rwsem_try_read_lock_unqueued() is replaced by atomic_long_try_cmpxchg_acquire() to simpify the code and generate slightly better assembly code. There is no functional change. Signed-off-by: Waiman Long <longman@redhat.com> Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Acked-by: Will Deacon <will.deacon@arm.com> Acked-by: Davidlohr Bueso <dbueso@suse.de> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Arnd Bergmann <arnd@arndb.de> Cc: Borislav Petkov <bp@alien8.de> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tim Chen <tim.c.chen@linux.intel.com> Link: http://lkml.kernel.org/r/20190404174320.22416-5-longman@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 12a30a7 commit a338ecb

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

‎kernel/locking/rwsem-xadd.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,21 +259,16 @@ static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem)
259259
*/
260260
static inline bool rwsem_try_write_lock_unqueued(struct rw_semaphore *sem)
261261
{
262-
long old, count = atomic_long_read(&sem->count);
262+
long count = atomic_long_read(&sem->count);
263263

264-
while (true) {
265-
if (!(count == 0 || count == RWSEM_WAITING_BIAS))
266-
return false;
267-
268-
old = atomic_long_cmpxchg_acquire(&sem->count, count,
269-
count + RWSEM_ACTIVE_WRITE_BIAS);
270-
if (old == count) {
264+
while (!count || count == RWSEM_WAITING_BIAS) {
265+
if (atomic_long_try_cmpxchg_acquire(&sem->count, &count,
266+
count + RWSEM_ACTIVE_WRITE_BIAS)) {
271267
rwsem_set_owner(sem);
272268
return true;
273269
}
274-
275-
count = old;
276270
}
271+
return false;
277272
}
278273

279274
static inline bool owner_on_cpu(struct task_struct *owner)

0 commit comments

Comments
 (0)