summaryrefslogtreecommitdiffstats
path: root/kernel/locking/mutex.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/locking/mutex.c')
-rw-r--r--kernel/locking/mutex.c202
1 files changed, 125 insertions, 77 deletions
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index f44f658ae629..cfe48419b7d0 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -244,6 +244,17 @@ void __sched mutex_lock(struct mutex *lock)
244EXPORT_SYMBOL(mutex_lock); 244EXPORT_SYMBOL(mutex_lock);
245#endif 245#endif
246 246
247/*
248 * Wait-Die:
249 * The newer transactions are killed when:
250 * It (the new transaction) makes a request for a lock being held
251 * by an older transaction.
252 */
253
254/*
255 * Associate the ww_mutex @ww with the context @ww_ctx under which we acquired
256 * it.
257 */
247static __always_inline void 258static __always_inline void
248ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx) 259ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
249{ 260{
@@ -282,26 +293,53 @@ ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
282 DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class); 293 DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
283#endif 294#endif
284 ww_ctx->acquired++; 295 ww_ctx->acquired++;
296 ww->ctx = ww_ctx;
285} 297}
286 298
299/*
300 * Determine if context @a is 'after' context @b. IOW, @a is a younger
301 * transaction than @b and depending on algorithm either needs to wait for
302 * @b or die.
303 */
287static inline bool __sched 304static inline bool __sched
288__ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b) 305__ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b)
289{ 306{
290 return a->stamp - b->stamp <= LONG_MAX && 307
291 (a->stamp != b->stamp || a > b); 308 return (signed long)(a->stamp - b->stamp) > 0;
309}
310
311/*
312 * Wait-Die; wake a younger waiter context (when locks held) such that it can
313 * die.
314 *
315 * Among waiters with context, only the first one can have other locks acquired
316 * already (ctx->acquired > 0), because __ww_mutex_add_waiter() and
317 * __ww_mutex_check_kill() wake any but the earliest context.
318 */
319static bool __sched
320__ww_mutex_die(struct mutex *lock, struct mutex_waiter *waiter,
321 struct ww_acquire_ctx *ww_ctx)
322{
323 if (waiter->ww_ctx->acquired > 0 &&
324 __ww_ctx_stamp_after(waiter->ww_ctx, ww_ctx)) {
325 debug_mutex_wake_waiter(lock, waiter);
326 wake_up_process(waiter->task);
327 }
328
329 return true;
292} 330}
293 331
294/* 332/*
295 * Wake up any waiters that may have to back off when the lock is held by the 333 * We just acquired @lock under @ww_ctx, if there are later contexts waiting
296 * given context. 334 * behind us on the wait-list, check if they need to die.
297 * 335 *
298 * Due to the invariants on the wait list, this can only affect the first 336 * See __ww_mutex_add_waiter() for the list-order construction; basically the
299 * waiter with a context. 337 * list is ordered by stamp, smallest (oldest) first.
300 * 338 *
301 * The current task must not be on the wait list. 339 * The current task must not be on the wait list.
302 */ 340 */
303static void __sched 341static void __sched
304__ww_mutex_wakeup_for_backoff(struct mutex *lock, struct ww_acquire_ctx *ww_ctx) 342__ww_mutex_check_waiters(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
305{ 343{
306 struct mutex_waiter *cur; 344 struct mutex_waiter *cur;
307 345
@@ -311,30 +349,23 @@ __ww_mutex_wakeup_for_backoff(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
311 if (!cur->ww_ctx) 349 if (!cur->ww_ctx)
312 continue; 350 continue;
313 351
314 if (cur->ww_ctx->acquired > 0 && 352 if (__ww_mutex_die(lock, cur, ww_ctx))
315 __ww_ctx_stamp_after(cur->ww_ctx, ww_ctx)) { 353 break;
316 debug_mutex_wake_waiter(lock, cur);
317 wake_up_process(cur->task);
318 }
319
320 break;
321 } 354 }
322} 355}
323 356
324/* 357/*
325 * After acquiring lock with fastpath or when we lost out in contested 358 * After acquiring lock with fastpath, where we do not hold wait_lock, set ctx
326 * slowpath, set ctx and wake up any waiters so they can recheck. 359 * and wake up any waiters so they can recheck.
327 */ 360 */
328static __always_inline void 361static __always_inline void
329ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 362ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
330{ 363{
331 ww_mutex_lock_acquired(lock, ctx); 364 ww_mutex_lock_acquired(lock, ctx);
332 365
333 lock->ctx = ctx;
334
335 /* 366 /*
336 * The lock->ctx update should be visible on all cores before 367 * The lock->ctx update should be visible on all cores before
337 * the atomic read is done, otherwise contended waiters might be 368 * the WAITERS check is done, otherwise contended waiters might be
338 * missed. The contended waiters will either see ww_ctx == NULL 369 * missed. The contended waiters will either see ww_ctx == NULL
339 * and keep spinning, or it will acquire wait_lock, add itself 370 * and keep spinning, or it will acquire wait_lock, add itself
340 * to waiter list and sleep. 371 * to waiter list and sleep.
@@ -348,29 +379,14 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
348 return; 379 return;
349 380
350 /* 381 /*
351 * Uh oh, we raced in fastpath, wake up everyone in this case, 382 * Uh oh, we raced in fastpath, check if any of the waiters need to
352 * so they can see the new lock->ctx. 383 * die.
353 */ 384 */
354 spin_lock(&lock->base.wait_lock); 385 spin_lock(&lock->base.wait_lock);
355 __ww_mutex_wakeup_for_backoff(&lock->base, ctx); 386 __ww_mutex_check_waiters(&lock->base, ctx);
356 spin_unlock(&lock->base.wait_lock); 387 spin_unlock(&lock->base.wait_lock);
357} 388}
358 389
359/*
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.
364 *
365 * Callers must hold the mutex wait_lock.
366 */
367static __always_inline void
368ww_mutex_set_context_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
369{
370 ww_mutex_lock_acquired(lock, ctx);
371 lock->ctx = ctx;
372}
373
374#ifdef CONFIG_MUTEX_SPIN_ON_OWNER 390#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
375 391
376static inline 392static inline
@@ -646,37 +662,73 @@ void __sched ww_mutex_unlock(struct ww_mutex *lock)
646} 662}
647EXPORT_SYMBOL(ww_mutex_unlock); 663EXPORT_SYMBOL(ww_mutex_unlock);
648 664
665
666static __always_inline int __sched
667__ww_mutex_kill(struct mutex *lock, struct ww_acquire_ctx *ww_ctx)
668{
669 if (ww_ctx->acquired > 0) {
670#ifdef CONFIG_DEBUG_MUTEXES
671 struct ww_mutex *ww;
672
673 ww = container_of(lock, struct ww_mutex, base);
674 DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock);
675 ww_ctx->contending_lock = ww;
676#endif
677 return -EDEADLK;
678 }
679
680 return 0;
681}
682
683
684/*
685 * Check whether we need to kill the transaction for the current lock acquire.
686 *
687 * Wait-Die: If we're trying to acquire a lock already held by an older
688 * context, kill ourselves.
689 *
690 * Since __ww_mutex_add_waiter() orders the wait-list on stamp, we only have to
691 * look at waiters before us in the wait-list.
692 */
649static inline int __sched 693static inline int __sched
650__ww_mutex_lock_check_stamp(struct mutex *lock, struct mutex_waiter *waiter, 694__ww_mutex_check_kill(struct mutex *lock, struct mutex_waiter *waiter,
651 struct ww_acquire_ctx *ctx) 695 struct ww_acquire_ctx *ctx)
652{ 696{
653 struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 697 struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
654 struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx); 698 struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
655 struct mutex_waiter *cur; 699 struct mutex_waiter *cur;
656 700
701 if (ctx->acquired == 0)
702 return 0;
703
657 if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx)) 704 if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx))
658 goto deadlock; 705 return __ww_mutex_kill(lock, ctx);
659 706
660 /* 707 /*
661 * If there is a waiter in front of us that has a context, then its 708 * If there is a waiter in front of us that has a context, then its
662 * stamp is earlier than ours and we must back off. 709 * stamp is earlier than ours and we must kill ourself.
663 */ 710 */
664 cur = waiter; 711 cur = waiter;
665 list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) { 712 list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) {
666 if (cur->ww_ctx) 713 if (!cur->ww_ctx)
667 goto deadlock; 714 continue;
715
716 return __ww_mutex_kill(lock, ctx);
668 } 717 }
669 718
670 return 0; 719 return 0;
671
672deadlock:
673#ifdef CONFIG_DEBUG_MUTEXES
674 DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
675 ctx->contending_lock = ww;
676#endif
677 return -EDEADLK;
678} 720}
679 721
722/*
723 * Add @waiter to the wait-list, keep the wait-list ordered by stamp, smallest
724 * first. Such that older contexts are preferred to acquire the lock over
725 * younger contexts.
726 *
727 * Waiters without context are interspersed in FIFO order.
728 *
729 * Furthermore, for Wait-Die kill ourself immediately when possible (there are
730 * older contexts already waiting) to avoid unnecessary waiting.
731 */
680static inline int __sched 732static inline int __sched
681__ww_mutex_add_waiter(struct mutex_waiter *waiter, 733__ww_mutex_add_waiter(struct mutex_waiter *waiter,
682 struct mutex *lock, 734 struct mutex *lock,
@@ -693,7 +745,7 @@ __ww_mutex_add_waiter(struct mutex_waiter *waiter,
693 /* 745 /*
694 * Add the waiter before the first waiter with a higher stamp. 746 * Add the waiter before the first waiter with a higher stamp.
695 * Waiters without a context are skipped to avoid starving 747 * Waiters without a context are skipped to avoid starving
696 * them. 748 * them. Wait-Die waiters may die here.
697 */ 749 */
698 pos = &lock->wait_list; 750 pos = &lock->wait_list;
699 list_for_each_entry_reverse(cur, &lock->wait_list, list) { 751 list_for_each_entry_reverse(cur, &lock->wait_list, list) {
@@ -701,34 +753,27 @@ __ww_mutex_add_waiter(struct mutex_waiter *waiter,
701 continue; 753 continue;
702 754
703 if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) { 755 if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) {
704 /* Back off immediately if necessary. */ 756 /*
705 if (ww_ctx->acquired > 0) { 757 * Wait-Die: if we find an older context waiting, there
706#ifdef CONFIG_DEBUG_MUTEXES 758 * is no point in queueing behind it, as we'd have to
707 struct ww_mutex *ww; 759 * die the moment it would acquire the lock.
760 */
761 int ret = __ww_mutex_kill(lock, ww_ctx);
708 762
709 ww = container_of(lock, struct ww_mutex, base); 763 if (ret)
710 DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock); 764 return ret;
711 ww_ctx->contending_lock = ww;
712#endif
713 return -EDEADLK;
714 }
715 765
716 break; 766 break;
717 } 767 }
718 768
719 pos = &cur->list; 769 pos = &cur->list;
720 770
721 /* 771 /* Wait-Die: ensure younger waiters die. */
722 * Wake up the waiter so that it gets a chance to back 772 __ww_mutex_die(lock, cur, ww_ctx);
723 * off.
724 */
725 if (cur->ww_ctx->acquired > 0) {
726 debug_mutex_wake_waiter(lock, cur);
727 wake_up_process(cur->task);
728 }
729 } 773 }
730 774
731 list_add_tail(&waiter->list, pos); 775 list_add_tail(&waiter->list, pos);
776
732 return 0; 777 return 0;
733} 778}
734 779
@@ -772,7 +817,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
772 */ 817 */
773 if (__mutex_trylock(lock)) { 818 if (__mutex_trylock(lock)) {
774 if (use_ww_ctx && ww_ctx) 819 if (use_ww_ctx && ww_ctx)
775 __ww_mutex_wakeup_for_backoff(lock, ww_ctx); 820 __ww_mutex_check_waiters(lock, ww_ctx);
776 821
777 goto skip_wait; 822 goto skip_wait;
778 } 823 }
@@ -790,10 +835,13 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
790 waiter.ww_ctx = MUTEX_POISON_WW_CTX; 835 waiter.ww_ctx = MUTEX_POISON_WW_CTX;
791#endif 836#endif
792 } else { 837 } else {
793 /* Add in stamp order, waking up waiters that must back off. */ 838 /*
839 * Add in stamp order, waking up waiters that must kill
840 * themselves.
841 */
794 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); 842 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
795 if (ret) 843 if (ret)
796 goto err_early_backoff; 844 goto err_early_kill;
797 845
798 waiter.ww_ctx = ww_ctx; 846 waiter.ww_ctx = ww_ctx;
799 } 847 }
@@ -815,7 +863,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
815 goto acquired; 863 goto acquired;
816 864
817 /* 865 /*
818 * Check for signals and wound conditions while holding 866 * Check for signals and kill conditions while holding
819 * wait_lock. This ensures the lock cancellation is ordered 867 * wait_lock. This ensures the lock cancellation is ordered
820 * against mutex_unlock() and wake-ups do not go missing. 868 * against mutex_unlock() and wake-ups do not go missing.
821 */ 869 */
@@ -824,8 +872,8 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
824 goto err; 872 goto err;
825 } 873 }
826 874
827 if (use_ww_ctx && ww_ctx && ww_ctx->acquired > 0) { 875 if (use_ww_ctx && ww_ctx) {
828 ret = __ww_mutex_lock_check_stamp(lock, &waiter, ww_ctx); 876 ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
829 if (ret) 877 if (ret)
830 goto err; 878 goto err;
831 } 879 }
@@ -870,7 +918,7 @@ skip_wait:
870 lock_acquired(&lock->dep_map, ip); 918 lock_acquired(&lock->dep_map, ip);
871 919
872 if (use_ww_ctx && ww_ctx) 920 if (use_ww_ctx && ww_ctx)
873 ww_mutex_set_context_slowpath(ww, ww_ctx); 921 ww_mutex_lock_acquired(ww, ww_ctx);
874 922
875 spin_unlock(&lock->wait_lock); 923 spin_unlock(&lock->wait_lock);
876 preempt_enable(); 924 preempt_enable();
@@ -879,7 +927,7 @@ skip_wait:
879err: 927err:
880 __set_current_state(TASK_RUNNING); 928 __set_current_state(TASK_RUNNING);
881 mutex_remove_waiter(lock, &waiter, current); 929 mutex_remove_waiter(lock, &waiter, current);
882err_early_backoff: 930err_early_kill:
883 spin_unlock(&lock->wait_lock); 931 spin_unlock(&lock->wait_lock);
884 debug_mutex_free_waiter(&waiter); 932 debug_mutex_free_waiter(&waiter);
885 mutex_release(&lock->dep_map, 1, ip); 933 mutex_release(&lock->dep_map, 1, ip);