On Fri, Aug 26, 2016 at 07:35:09PM -0400, Waiman Long wrote:
@@ -624,13 +649,24 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,True, but it took me a little while to figure out why
/* didn't get the lock, go to sleep: */
spin_unlock_mutex(&lock->wait_lock, flags);
schedule_preempt_disabled();
+ /*
+ * Both __mutex_trylock() and __mutex_waiter_is_first()
+ * can be done without the protection of wait_lock.
+ */
__mutex_waiter_is_first() is safe without the lock :-)
+ acquired = __mutex_trylock(lock);That said; I think there's a few problems with this. Since we now poke
+ if (!acquired&& __mutex_waiter_is_first(lock,&waiter)) {
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
+ /*
+ * Wait until the lock is handed off or the owner
+ * sleeps.
+ */
+ acquired = mutex_optimistic_spin(lock, ww_ctx,
+ use_ww_ctx, true);
+ }
at the loop termination conditions outside of the wait_lock, it becomes
important where we do the task->state vs wakeup bits.
Specifically, since we still have state==RUNNING here, its possible
we'll fail to acquire the lock _and_ miss the wakeup from
mutex_unlock(). Leaving us stuck forever more.
Also, we should do the __mutex_trylock _after_ we set the handoff,
otherwise its possible we get the lock handed (miss the wakeup as per
the above) and fail to notice, again going back to sleep forever more.
@@ -638,7 +636,8 @@ __mutex_lock_common(struct mutex *lock,
lock_contended(&lock->dep_map, ip);
- for (acquired = false; !acquired; ) {
+ set_task_state(task, state);
+ for (;;) {
/*
* got a signal? (This code gets eliminated in the
* TASK_UNINTERRUPTIBLE case.)
@@ -654,30 +653,23 @@ __mutex_lock_common(struct mutex *lock,
goto err;
}
- __set_task_state(task, state);
-
- /* didn't get the lock, go to sleep: */
spin_unlock_mutex(&lock->wait_lock, flags);
schedule_preempt_disabled();
- /*
- * Both __mutex_trylock() and __mutex_waiter_is_first()
- * can be done without the protection of wait_lock.
- */
- acquired = __mutex_trylock(lock, true);
+ set_task_state(task, state);
- if (!acquired&& __mutex_waiter_is_first(lock,&waiter)) {
+ if (__mutex_waiter_is_first(lock,&waiter)) {
__mutex_set_flag(lock, MUTEX_FLAG_HANDOFF);
- /*
- * Wait until the lock is handed off or the owner
- * sleeps.
- */
- acquired = mutex_optimistic_spin(lock, ww_ctx,
- use_ww_ctx, true);
+ if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, true))
+ break;
}
+ if (__mutex_trylock(lock, true))
+ break;
+