Skip to content

Commit ddd0fa7

Browse files
Waiman Longingomolnar
authored andcommitted
locking/rwsem: Streamline the rwsem_optimistic_spin() code
This patch moves the owner loading and checking code entirely inside of rwsem_spin_on_owner() to simplify the logic of rwsem_optimistic_spin() loop. Suggested-by: Peter Hurley <peter@hurleysoftware.com> Signed-off-by: Waiman Long <Waiman.Long@hpe.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Peter Hurley <peter@hurleysoftware.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Dave Chinner <david@fromorbit.com> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: Douglas Hatch <doug.hatch@hpe.com> Cc: Jason Low <jason.low2@hp.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Scott J Norton <scott.norton@hpe.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/1463534783-38814-6-git-send-email-Waiman.Long@hpe.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent bf7b4c4 commit ddd0fa7

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

‎kernel/locking/rwsem-xadd.c‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,16 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem)
350350
return ret;
351351
}
352352

353-
static noinline
354-
bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
353+
/*
354+
* Return true only if we can still spin on the owner field of the rwsem.
355+
*/
356+
static noinline bool rwsem_spin_on_owner(struct rw_semaphore *sem)
355357
{
358+
struct task_struct *owner = READ_ONCE(sem->owner);
359+
360+
if (!rwsem_owner_is_writer(owner))
361+
goto out;
362+
356363
rcu_read_lock();
357364
while (sem->owner == owner) {
358365
/*
@@ -372,7 +379,7 @@ bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
372379
cpu_relax_lowlatency();
373380
}
374381
rcu_read_unlock();
375-
382+
out:
376383
/*
377384
* If there is a new owner or the owner is not set, we continue
378385
* spinning.
@@ -382,7 +389,6 @@ bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
382389

383390
static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
384391
{
385-
struct task_struct *owner;
386392
bool taken = false;
387393

388394
preempt_disable();
@@ -394,21 +400,17 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
394400
if (!osq_lock(&sem->osq))
395401
goto done;
396402

397-
while (true) {
398-
owner = READ_ONCE(sem->owner);
403+
/*
404+
* Optimistically spin on the owner field and attempt to acquire the
405+
* lock whenever the owner changes. Spinning will be stopped when:
406+
* 1) the owning writer isn't running; or
407+
* 2) readers own the lock as we can't determine if they are
408+
* actively running or not.
409+
*/
410+
while (rwsem_spin_on_owner(sem)) {
399411
/*
400-
* Don't spin if
401-
* 1) the owner is a reader as we we can't determine if the
402-
* reader is actively running or not.
403-
* 2) The rwsem_spin_on_owner() returns false which means
404-
* the owner isn't running.
412+
* Try to acquire the lock
405413
*/
406-
if (rwsem_owner_is_reader(owner) ||
407-
(rwsem_owner_is_writer(owner) &&
408-
!rwsem_spin_on_owner(sem, owner)))
409-
break;
410-
411-
/* wait_lock will be acquired if write_lock is obtained */
412414
if (rwsem_try_write_lock_unqueued(sem)) {
413415
taken = true;
414416
break;
@@ -420,7 +422,7 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
420422
* we're an RT task that will live-lock because we won't let
421423
* the owner complete.
422424
*/
423-
if (!owner && (need_resched() || rt_task(current)))
425+
if (!sem->owner && (need_resched() || rt_task(current)))
424426
break;
425427

426428
/*

0 commit comments

Comments
 (0)