locking/mutex: Introduce __mutex_trylock_or_handoff()
authorPeter Zijlstra <peterz@infradead.org>
Wed, 30 Jun 2021 15:35:19 +0000 (17:35 +0200)
committerPeter Zijlstra <peterz@infradead.org>
Wed, 7 Jul 2021 11:53:25 +0000 (13:53 +0200)
commitad90880dc9625682a58897cba2ecff657a2aa60b
tree961ea9c382b93450563bd307f960ee1616193141
parent048661a1f963e9517630f080687d48af79ed784c
locking/mutex: Introduce __mutex_trylock_or_handoff()

Yanfei reported that it is possible to loose HANDOFF when we race with
mutex_unlock() and end up setting HANDOFF on an unlocked mutex. At
that point anybody can steal it, losing HANDOFF in the process.

If this happens often enough, we can in fact starve the top waiter.

Solve this by folding the 'set HANDOFF' operation into the trylock
operation, such that either we acquire the lock, or it gets HANDOFF
set. This avoids having HANDOFF set on an unlocked mutex.

Reported-by: Yanfei Xu <yanfei.xu@windriver.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Reviewed-by: Waiman Long <longman@redhat.com>
Reviewed-by: Yanfei Xu <yanfei.xu@windriver.com>
Link: https://lore.kernel.org/r/20210630154114.958507900@infradead.org
kernel/locking/mutex.c