Skip to content

Commit 97bc540

Browse files
Peter Zijlstragregkh
authored andcommitted
locking/mutex: Fix HANDOFF condition
[ Upstream commit 048661a ] Yanfei reported that setting HANDOFF should not depend on recomputing @FIRST, only on @FIRST state. Which would then give: if (ww_ctx || !first) first = __mutex_waiter_is_first(lock, &waiter); if (first) __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); But because 'ww_ctx || !first' is basically 'always' and the test for first is relatively cheap, omit that first branch entirely. 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.896786297@infradead.org Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent cb83afd commit 97bc540

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

‎kernel/locking/mutex.c‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,6 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
938938
struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
939939
{
940940
struct mutex_waiter waiter;
941-
bool first = false;
942941
struct ww_mutex *ww;
943942
int ret;
944943

@@ -1017,6 +1016,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10171016

10181017
set_current_state(state);
10191018
for (;;) {
1019+
bool first;
1020+
10201021
/*
10211022
* Once we hold wait_lock, we're serialized against
10221023
* mutex_unlock() handing the lock off to us, do a trylock
@@ -1045,15 +1046,9 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
10451046
spin_unlock(&lock->wait_lock);
10461047
schedule_preempt_disabled();
10471048

1048-
/*
1049-
* ww_mutex needs to always recheck its position since its waiter
1050-
* list is not FIFO ordered.
1051-
*/
1052-
if (ww_ctx || !first) {
1053-
first = __mutex_waiter_is_first(lock, &waiter);
1054-
if (first)
1055-
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
1056-
}
1049+
first = __mutex_waiter_is_first(lock, &waiter);
1050+
if (first)
1051+
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
10571052

10581053
set_current_state(state);
10591054
/*

0 commit comments

Comments
 (0)