提交 b3fd4f03 编写于 作者: D Davidlohr Bueso 提交者: Ingo Molnar

locking/rwsem: Avoid deceiving lock spinners

When readers hold the semaphore, the ->owner is nil. As such,
and unlike mutexes, '!owner' does not necessarily imply that
the lock is free. This will cause writers to potentially spin
excessively as they've been mislead to thinking they have a
chance of acquiring the lock, instead of blocking.

This patch therefore enhances the counter check when the owner
is not set by the time we've broken out of the loop. Otherwise
we can return true as a new owner has the lock and thus we want
to continue spinning. While at it, we can make rwsem_spin_on_owner()
less ambiguos and return right away under need_resched conditions.
Signed-off-by: NDavidlohr Bueso <dbueso@suse.de>
Signed-off-by: NPeter Zijlstra (Intel) <peterz@infradead.org>
Cc: Jason Low <jason.low2@hp.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Michel Lespinasse <walken@google.com>
Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
Cc: Tim Chen <tim.c.chen@linux.intel.com>
Link: http://lkml.kernel.org/r/1422609267-15102-5-git-send-email-dave@stgolabs.netSigned-off-by: NIngo Molnar <mingo@kernel.org>
上级 7a215f89
...@@ -337,21 +337,30 @@ static inline bool owner_running(struct rw_semaphore *sem, ...@@ -337,21 +337,30 @@ static inline bool owner_running(struct rw_semaphore *sem,
static noinline static noinline
bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner) bool rwsem_spin_on_owner(struct rw_semaphore *sem, struct task_struct *owner)
{ {
long count;
rcu_read_lock(); rcu_read_lock();
while (owner_running(sem, owner)) { while (owner_running(sem, owner)) {
if (need_resched()) /* abort spinning when need_resched */
break; if (need_resched()) {
rcu_read_unlock();
return false;
}
cpu_relax_lowlatency(); cpu_relax_lowlatency();
} }
rcu_read_unlock(); rcu_read_unlock();
if (READ_ONCE(sem->owner))
return true; /* new owner, continue spinning */
/* /*
* We break out the loop above on need_resched() or when the * When the owner is not set, the lock could be free or
* owner changed, which is a sign for heavy contention. Return * held by readers. Check the counter to verify the
* success only when sem->owner is NULL. * state.
*/ */
return sem->owner == NULL; count = READ_ONCE(sem->count);
return (count == 0 || count == RWSEM_WAITING_BIAS);
} }
static bool rwsem_optimistic_spin(struct rw_semaphore *sem) static bool rwsem_optimistic_spin(struct rw_semaphore *sem)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册