Skip to content

Commit 200b187

Browse files
nhaehnleingomolnar
authored andcommitted
locking/ww_mutex: Notify waiters that have to back off while adding tasks to wait list
While adding our task as a waiter, detect if another task should back off because of us. With this patch, we establish the invariant that the wait list contains at most one (sleeping) waiter with ww_ctx->acquired > 0, and this waiter will be the first waiter with a context. Since only waiters with ww_ctx->acquired > 0 have to back off, this allows us to be much more economical with wakeups. Signed-off-by: Nicolai Hähnle <Nicolai.Haehnle@amd.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Chris Wilson <chris@chris-wilson.co.uk> Cc: Daniel Vetter <daniel@ffwll.ch> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Maarten Lankhorst <dev@mblankhorst.nl> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: dri-devel@lists.freedesktop.org Link: http://lkml.kernel.org/r/1482346000-9927-8-git-send-email-nhaehnle@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 6baa5c6 commit 200b187

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

‎kernel/locking/mutex.c‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -596,23 +596,34 @@ void __sched ww_mutex_unlock(struct ww_mutex *lock)
596596
EXPORT_SYMBOL(ww_mutex_unlock);
597597

598598
static inline int __sched
599-
__ww_mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx)
599+
__ww_mutex_lock_check_stamp(struct mutex *lock, struct mutex_waiter *waiter,
600+
struct ww_acquire_ctx *ctx)
600601
{
601602
struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
602603
struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
604+
struct mutex_waiter *cur;
603605

604-
if (!hold_ctx)
605-
return 0;
606+
if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
607+
goto deadlock;
606608

607-
if (__ww_ctx_stamp_after(ctx, hold_ctx)) {
608-
#ifdef CONFIG_DEBUG_MUTEXES
609-
DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
610-
ctx->contending_lock = ww;
611-
#endif
612-
return -EDEADLK;
609+
/*
610+
* If there is a waiter in front of us that has a context, then its
611+
* stamp is earlier than ours and we must back off.
612+
*/
613+
cur = waiter;
614+
list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) {
615+
if (cur->ww_ctx)
616+
goto deadlock;
613617
}
614618

615619
return 0;
620+
621+
deadlock:
622+
#ifdef CONFIG_DEBUG_MUTEXES
623+
DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
624+
ctx->contending_lock = ww;
625+
#endif
626+
return -EDEADLK;
616627
}
617628

618629
static inline int __sched
@@ -655,6 +666,15 @@ __ww_mutex_add_waiter(struct mutex_waiter *waiter,
655666
}
656667

657668
pos = &cur->list;
669+
670+
/*
671+
* Wake up the waiter so that it gets a chance to back
672+
* off.
673+
*/
674+
if (cur->ww_ctx->acquired > 0) {
675+
debug_mutex_wake_waiter(lock, cur);
676+
wake_up_process(cur->task);
677+
}
658678
}
659679

660680
list_add_tail(&waiter->list, pos);
@@ -746,7 +766,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
746766
}
747767

748768
if (use_ww_ctx && ww_ctx && ww_ctx->acquired > 0) {
749-
ret = __ww_mutex_lock_check_stamp(lock, ww_ctx);
769+
ret = __ww_mutex_lock_check_stamp(lock, &waiter, ww_ctx);
750770
if (ret)
751771
goto err;
752772
}

0 commit comments

Comments
 (0)