Skip to content

Commit 659cf9f

Browse files
nhaehnleingomolnar
authored andcommitted
locking/ww_mutex: Optimize ww-mutexes by waking at most one waiter for backoff when acquiring the lock
The wait list is sorted by stamp order, and the only waiting task that may have to back off is the first waiter with a context. The regular slow path does not have to wake any other tasks at all, since all other waiters that would have to back off were either woken up when the waiter was added to the list, or detected the condition before they added themselves. Median timings taken of a contention-heavy GPU workload: Without this series: real 0m59.900s user 0m7.516s sys 2m16.076s With changes up to and including this patch: real 0m52.946s user 0m7.272s sys 1m55.964s 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-9-git-send-email-nhaehnle@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 200b187 commit 659cf9f

1 file changed

Lines changed: 40 additions & 19 deletions

File tree

‎kernel/locking/mutex.c‎

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,36 @@ __ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
288288
(a->stamp != b->stamp || a > b);
289289
}
290290

291+
/*
292+
* Wake up any waiters that may have to back off when the lock is held by the
293+
* given context.
294+
*
295+
* Due to the invariants on the wait list, this can only affect the first
296+
* waiter with a context.
297+
*
298+
* The current task must not be on the wait list.
299+
*/
300+
static void __sched
301+
__ww_mutex_wakeup_for_backoff(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
302+
{
303+
struct mutex_waiter *cur;
304+
305+
lockdep_assert_held(&lock->wait_lock);
306+
307+
list_for_each_entry(cur, &lock->wait_list, list) {
308+
if (!cur->ww_ctx)
309+
continue;
310+
311+
if (cur->ww_ctx->acquired > 0 &&
312+
__ww_ctx_stamp_after(cur->ww_ctx, ww_ctx)) {
313+
debug_mutex_wake_waiter(lock, cur);
314+
wake_up_process(cur->task);
315+
}
316+
317+
break;
318+
}
319+
}
320+
291321
/*
292322
* After acquiring lock with fastpath or when we lost out in contested
293323
* slowpath, set ctx and wake up any waiters so they can recheck.
@@ -297,7 +327,6 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock,
297327
struct ww_acquire_ctx *ctx)
298328
{
299329
unsigned long flags;
300-
struct mutex_waiter *cur;
301330

302331
ww_mutex_lock_acquired(lock, ctx);
303332

@@ -323,36 +352,24 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock,
323352
* so they can see the new lock->ctx.
324353
*/
325354
spin_lock_mutex(&lock->base.wait_lock, flags);
326-
list_for_each_entry(cur, &lock->base.wait_list, list) {
327-
debug_mutex_wake_waiter(&lock->base, cur);
328-
wake_up_process(cur->task);
329-
}
355+
__ww_mutex_wakeup_for_backoff(&lock->base, ctx);
330356
spin_unlock_mutex(&lock->base.wait_lock, flags);
331357
}
332358

333359
/*
334-
* After acquiring lock in the slowpath set ctx and wake up any
335-
* waiters so they can recheck.
360+
* After acquiring lock in the slowpath set ctx.
361+
*
362+
* Unlike for the fast path, the caller ensures that waiters are woken up where
363+
* necessary.
336364
*
337365
* Callers must hold the mutex wait_lock.
338366
*/
339367
static __always_inline void
340368
ww_mutex_set_context_slowpath(struct ww_mutex *lock,
341369
struct ww_acquire_ctx *ctx)
342370
{
343-
struct mutex_waiter *cur;
344-
345371
ww_mutex_lock_acquired(lock, ctx);
346372
lock->ctx = ctx;
347-
348-
/*
349-
* Give any possible sleeping processes the chance to wake up,
350-
* so they can recheck if they have to back off.
351-
*/
352-
list_for_each_entry(cur, &lock->base.wait_list, list) {
353-
debug_mutex_wake_waiter(&lock->base, cur);
354-
wake_up_process(cur->task);
355-
}
356373
}
357374

358375
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
@@ -719,8 +736,12 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
719736
/*
720737
* After waiting to acquire the wait_lock, try again.
721738
*/
722-
if (__mutex_trylock(lock))
739+
if (__mutex_trylock(lock)) {
740+
if (use_ww_ctx && ww_ctx)
741+
__ww_mutex_wakeup_for_backoff(lock, ww_ctx);
742+
723743
goto skip_wait;
744+
}
724745

725746
debug_mutex_lock_common(lock, &waiter);
726747
debug_mutex_add_waiter(lock, &waiter, current);

0 commit comments

Comments
 (0)