Re: [PATCH -v3 07/10] mutex: Restructure wait loop
From: Peter Zijlstra
Date: Wed Sep 14 2016 - 06:34:29 EST
On Fri, Sep 09, 2016 at 04:32:39PM -0400, Waiman Long wrote:
> >I think you need to remove the HANDOFF flag here in the error path too.
> >Maybe you should fix that in patch 6.
> >
> >It is also possible that __mutex_handoff() has been called at this point.
> >So this task may now be the owner of the lock. Maybe you should do a final
> >trylock here.
>
> Alternatively, we can move the trylock in the main loop after the
> spin_lock_mutex(). In this case, we don't need an extra trylock in the error
> path.
I did something like so..
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -629,12 +629,12 @@ __mutex_lock_common(struct mutex *lock,
if (__mutex_waiter_is_first(lock, &waiter))
__mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
- if (__mutex_trylock(lock, false))
- goto remove_waiter;
-
lock_contended(&lock->dep_map, ip);
for (;;) {
+ if (__mutex_trylock(lock, first))
+ break;
+
/*
* got a signal? (This code gets eliminated in the
* TASK_UNINTERRUPTIBLE case.)
@@ -659,13 +659,9 @@ __mutex_lock_common(struct mutex *lock,
first = true;
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
}
-
- if (__mutex_trylock(lock, first))
- break;
}
__set_task_state(task, TASK_RUNNING);
-remove_waiter:
mutex_remove_waiter(lock, &waiter, task);
if (likely(list_empty(&lock->wait_list)))
__mutex_clear_flag(lock, MUTEX_FLAGS);