Skip to content

Commit c516df9

Browse files
nhaehnleingomolnar
authored andcommitted
locking/ww_mutex: Optimize ww-mutexes by yielding to other waiters from optimistic spin
Lock stealing is less beneficial for w/w mutexes since we may just end up backing off if we stole from a thread with an earlier acquire stamp that already holds another w/w mutex that we also need. So don't spin optimistically unless we are sure that there is no other waiter that might cause us to back off. Median timings taken of a contention-heavy GPU workload: Before: real 0m52.946s user 0m7.272s sys 1m55.964s After: real 0m53.086s user 0m7.360s sys 1m46.204s This particular workload still spends 20%-25% of CPU in mutex_spin_on_owner according to perf, but my attempts to further reduce this spinning based on various heuristics all lead to an increase in measured wall time despite the decrease in sys time. 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-11-git-send-email-nhaehnle@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 25f13b4 commit c516df9

1 file changed

Lines changed: 52 additions & 26 deletions

File tree

‎kernel/locking/mutex.c‎

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,49 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
371371
}
372372

373373
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
374+
375+
static inline
376+
bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
377+
struct mutex_waiter *waiter)
378+
{
379+
struct ww_mutex *ww;
380+
381+
ww = container_of(lock, struct ww_mutex, base);
382+
383+
/*
384+
* If ww->ctx is set the contents are undefined, only
385+
* by acquiring wait_lock there is a guarantee that
386+
* they are not invalid when reading.
387+
*
388+
* As such, when deadlock detection needs to be
389+
* performed the optimistic spinning cannot be done.
390+
*
391+
* Check this in every inner iteration because we may
392+
* be racing against another thread's ww_mutex_lock.
393+
*/
394+
if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
395+
return false;
396+
397+
/*
398+
* If we aren't on the wait list yet, cancel the spin
399+
* if there are waiters. We want to avoid stealing the
400+
* lock from a waiter with an earlier stamp, since the
401+
* other thread may already own a lock that we also
402+
* need.
403+
*/
404+
if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
405+
return false;
406+
407+
/*
408+
* Similarly, stop spinning if we are no longer the
409+
* first waiter.
410+
*/
411+
if (waiter && !__mutex_waiter_is_first(lock, waiter))
412+
return false;
413+
414+
return true;
415+
}
416+
374417
/*
375418
* Look out! "owner" is an entirely speculative pointer access and not
376419
* reliable.
@@ -379,7 +422,7 @@ ww_mutex_set_context_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
379422
*/
380423
static noinline
381424
bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
382-
struct ww_acquire_ctx *ww_ctx)
425+
struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
383426
{
384427
bool ret = true;
385428

@@ -402,26 +445,9 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
402445
break;
403446
}
404447

405-
if (ww_ctx && ww_ctx->acquired > 0) {
406-
struct ww_mutex *ww;
407-
408-
ww = container_of(lock, struct ww_mutex, base);
409-
410-
/*
411-
* If ww->ctx is set the contents are undefined, only
412-
* by acquiring wait_lock there is a guarantee that
413-
* they are not invalid when reading.
414-
*
415-
* As such, when deadlock detection needs to be
416-
* performed the optimistic spinning cannot be done.
417-
*
418-
* Check this in every inner iteration because we may
419-
* be racing against another thread's ww_mutex_lock.
420-
*/
421-
if (READ_ONCE(ww->ctx)) {
422-
ret = false;
423-
break;
424-
}
448+
if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
449+
ret = false;
450+
break;
425451
}
426452

427453
cpu_relax();
@@ -484,7 +510,7 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock)
484510
*/
485511
static __always_inline bool
486512
mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
487-
const bool use_ww_ctx, const bool waiter)
513+
const bool use_ww_ctx, struct mutex_waiter *waiter)
488514
{
489515
if (!waiter) {
490516
/*
@@ -518,7 +544,7 @@ mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
518544
* There's an owner, wait for it to either
519545
* release the lock or go to sleep.
520546
*/
521-
if (!mutex_spin_on_owner(lock, owner, ww_ctx))
547+
if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
522548
goto fail_unlock;
523549

524550
/*
@@ -560,7 +586,7 @@ mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
560586
#else
561587
static __always_inline bool
562588
mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
563-
const bool use_ww_ctx, const bool waiter)
589+
const bool use_ww_ctx, struct mutex_waiter *waiter)
564590
{
565591
return false;
566592
}
@@ -731,7 +757,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
731757
mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
732758

733759
if (__mutex_trylock(lock) ||
734-
mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, false)) {
760+
mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, NULL)) {
735761
/* got the lock, yay! */
736762
lock_acquired(&lock->dep_map, ip);
737763
if (use_ww_ctx && ww_ctx)
@@ -820,7 +846,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
820846
* or we must see its unlock and acquire.
821847
*/
822848
if (__mutex_trylock(lock) ||
823-
(first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, true)))
849+
(first && mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx, &waiter)))
824850
break;
825851

826852
spin_lock_mutex(&lock->wait_lock, flags);

0 commit comments

Comments
 (0)