Re: [PATCH v2 1/2] locking: Implement an algorithm choice for Wound-Wait mutexes

From: Thomas Hellstrom
Date: Thu Jun 14 2018 - 09:19:26 EST


On 06/14/2018 02:48 PM, Thomas Hellstrom wrote:
Hi, Peter,

On 06/14/2018 02:41 PM, Peter Zijlstra wrote:
On Thu, Jun 14, 2018 at 09:29:21AM +0200, Thomas Hellstrom wrote:
+static bool __ww_mutex_wound(struct mutex *lock,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct ww_acquire_ctx *ww_ctx,
+ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct ww_acquire_ctx *hold_ctx)
+{
+ÂÂÂ struct task_struct *owner = __mutex_owner(lock);
+
+ÂÂÂ lockdep_assert_held(&lock->wait_lock);
+
+ÂÂÂ if (owner && hold_ctx && __ww_ctx_stamp_after(hold_ctx, ww_ctx) &&
+ÂÂÂÂÂÂÂ ww_ctx->acquired > 0) {
+ÂÂÂÂÂÂÂ hold_ctx->wounded = 1;
+
+ÂÂÂÂÂÂÂ /*
+ÂÂÂÂÂÂÂÂ * wake_up_process() paired with set_current_state() inserts
+ÂÂÂÂÂÂÂÂ * sufficient barriers to make sure @owner either sees it's
+ÂÂÂÂÂÂÂÂ * wounded or has a wakeup pending to re-read the wounded
+ÂÂÂÂÂÂÂÂ * state.
+ÂÂÂÂÂÂÂÂ *
+ÂÂÂÂÂÂÂÂ * The value of hold_ctx->wounded in
+ÂÂÂÂÂÂÂÂ * __ww_mutex_lock_check_stamp();
+ÂÂÂÂÂÂÂÂ */
+ÂÂÂÂÂÂÂ if (owner != current)
+ÂÂÂÂÂÂÂÂÂÂÂ wake_up_process(owner);
+
+ÂÂÂÂÂÂÂ return true;
+ÂÂÂ }
+
+ÂÂÂ return false;
+}
@@ -338,12 +377,18 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
ÂÂÂÂÂÂ * and keep spinning, or it will acquire wait_lock, add itself
ÂÂÂÂÂÂ * to waiter list and sleep.
ÂÂÂÂÂÂ */
-ÂÂÂ smp_mb(); /* ^^^ */
+ÂÂÂ smp_mb(); /* See comments above and below. */
 Â /*
-ÂÂÂÂ * Check if lock is contended, if not there is nobody to wake up
+ÂÂÂÂ * Check if lock is contended, if not there is nobody to wake up.
+ÂÂÂÂ * We can use list_empty() unlocked here since it only compares a
+ÂÂÂÂ * list_head field pointer to the address of the list head
+ÂÂÂÂ * itself, similarly to how list_empty() can be considered RCU-safe.
+ÂÂÂÂ * The memory barrier above pairs with the memory barrier in
+ÂÂÂÂ * __ww_mutex_add_waiter and makes sure lock->ctx is visible before
+ÂÂÂÂ * we check for waiters.
ÂÂÂÂÂÂ */
-ÂÂÂ if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS)))
+ÂÂÂ if (likely(list_empty(&lock->base.wait_list)))
ÂÂÂÂÂÂÂÂÂ return;
OK, so what happens is that if we see !empty list, we take wait_lock,
if we end up in __ww_mutex_wound() we must really have !empty wait-list.

It can however still see !owner because __mutex_unlock_slowpath() can
clear the owner field. But if owner is set, it must stay valid because
FLAG_WAITERS and we're holding wait_lock.

If __ww_mutex_wound() is called from ww_mutex_set_context_fastpath() owner is the current process so we can never see !owner. However if __ww_mutex_wound() is called from __ww_mutex_add_waiter() then the above is true.

Or actually it was intended to be true, but FLAG_WAITERS is set too late. It needs to be moved to just after we actually add the waiter to the list.

Then the hunk that replaces a FLAG_WAITERS read with a lockless list_empty() can also be ditched.

/Thomas




So the wake_up_process() is in fact safe.

Let me put that in a comment.


Thanks,

Thomas