aboutsummaryrefslogtreecommitdiffstats
path: root/kernel
diff options
context:
space:
mode:
authorPeter Zijlstra <peterz@infradead.org>2016-08-23 07:36:04 -0400
committerIngo Molnar <mingo@kernel.org>2016-10-25 05:31:50 -0400
commit3ca0ff571b092ee4d807f1168caa428d95b0173b (patch)
treebde73cbec0c0fb44da58291546da58830384fb14 /kernel
parent3ab7c086d5ec72585ef0158dbc265cb03ddc682a (diff)
locking/mutex: Rework mutex::owner
The current mutex implementation has an atomic lock word and a non-atomic owner field. This disparity leads to a number of issues with the current mutex code as it means that we can have a locked mutex without an explicit owner (because the owner field has not been set, or already cleared). This leads to a number of weird corner cases, esp. between the optimistic spinning and debug code. Where the optimistic spinning code needs the owner field updated inside the lock region, the debug code is more relaxed because the whole lock is serialized by the wait_lock. Also, the spinning code itself has a few corner cases where we need to deal with a held lock without an owner field. Furthermore, it becomes even more of a problem when trying to fix starvation cases in the current code. We end up stacking special case on special case. To solve this rework the basic mutex implementation to be a single atomic word that contains the owner and uses the low bits for extra state. This matches how PI futexes and rt_mutex already work. By having the owner an integral part of the lock state a lot of the problems dissapear and we get a better option to deal with starvation cases, direct owner handoff. Changing the basic mutex does however invalidate all the arch specific mutex code; this patch leaves that unused in-place, a later patch will remove that. Tested-by: Jason Low <jason.low2@hpe.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Will Deacon <will.deacon@arm.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Paul E. McKenney <paulmck@linux.vnet.ibm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-kernel@vger.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/locking/mutex-debug.c13
-rw-r--r--kernel/locking/mutex-debug.h10
-rw-r--r--kernel/locking/mutex.c371
-rw-r--r--kernel/locking/mutex.h26
-rw-r--r--kernel/sched/core.c2
5 files changed, 157 insertions, 265 deletions
diff --git a/kernel/locking/mutex-debug.c b/kernel/locking/mutex-debug.c
index 9c951fade415..9aa713629387 100644
--- a/kernel/locking/mutex-debug.c
+++ b/kernel/locking/mutex-debug.c
@@ -73,21 +73,8 @@ void debug_mutex_unlock(struct mutex *lock)
73{ 73{
74 if (likely(debug_locks)) { 74 if (likely(debug_locks)) {
75 DEBUG_LOCKS_WARN_ON(lock->magic != lock); 75 DEBUG_LOCKS_WARN_ON(lock->magic != lock);
76
77 if (!lock->owner)
78 DEBUG_LOCKS_WARN_ON(!lock->owner);
79 else
80 DEBUG_LOCKS_WARN_ON(lock->owner != current);
81
82 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next); 76 DEBUG_LOCKS_WARN_ON(!lock->wait_list.prev && !lock->wait_list.next);
83 } 77 }
84
85 /*
86 * __mutex_slowpath_needs_to_unlock() is explicitly 0 for debug
87 * mutexes so that we can do it here after we've verified state.
88 */
89 mutex_clear_owner(lock);
90 atomic_set(&lock->count, 1);
91} 78}
92 79
93void debug_mutex_init(struct mutex *lock, const char *name, 80void debug_mutex_init(struct mutex *lock, const char *name,
diff --git a/kernel/locking/mutex-debug.h b/kernel/locking/mutex-debug.h
index 57a871ae3c81..a459faa48987 100644
--- a/kernel/locking/mutex-debug.h
+++ b/kernel/locking/mutex-debug.h
@@ -27,16 +27,6 @@ extern void debug_mutex_unlock(struct mutex *lock);
27extern void debug_mutex_init(struct mutex *lock, const char *name, 27extern void debug_mutex_init(struct mutex *lock, const char *name,
28 struct lock_class_key *key); 28 struct lock_class_key *key);
29 29
30static inline void mutex_set_owner(struct mutex *lock)
31{
32 WRITE_ONCE(lock->owner, current);
33}
34
35static inline void mutex_clear_owner(struct mutex *lock)
36{
37 WRITE_ONCE(lock->owner, NULL);
38}
39
40#define spin_lock_mutex(lock, flags) \ 30#define spin_lock_mutex(lock, flags) \
41 do { \ 31 do { \
42 struct mutex *l = container_of(lock, struct mutex, wait_lock); \ 32 struct mutex *l = container_of(lock, struct mutex, wait_lock); \
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c
index a70b90db3909..de1ce0bae0d5 100644
--- a/kernel/locking/mutex.c
+++ b/kernel/locking/mutex.c
@@ -27,41 +27,113 @@
27#include <linux/debug_locks.h> 27#include <linux/debug_locks.h>
28#include <linux/osq_lock.h> 28#include <linux/osq_lock.h>
29 29
30/*
31 * In the DEBUG case we are using the "NULL fastpath" for mutexes,
32 * which forces all calls into the slowpath:
33 */
34#ifdef CONFIG_DEBUG_MUTEXES 30#ifdef CONFIG_DEBUG_MUTEXES
35# include "mutex-debug.h" 31# include "mutex-debug.h"
36# include <asm-generic/mutex-null.h>
37/*
38 * Must be 0 for the debug case so we do not do the unlock outside of the
39 * wait_lock region. debug_mutex_unlock() will do the actual unlock in this
40 * case.
41 */
42# undef __mutex_slowpath_needs_to_unlock
43# define __mutex_slowpath_needs_to_unlock() 0
44#else 32#else
45# include "mutex.h" 33# include "mutex.h"
46# include <asm/mutex.h>
47#endif 34#endif
48 35
49void 36void
50__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 37__mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
51{ 38{
52 atomic_set(&lock->count, 1); 39 atomic_long_set(&lock->owner, 0);
53 spin_lock_init(&lock->wait_lock); 40 spin_lock_init(&lock->wait_lock);
54 INIT_LIST_HEAD(&lock->wait_list); 41 INIT_LIST_HEAD(&lock->wait_list);
55 mutex_clear_owner(lock);
56#ifdef CONFIG_MUTEX_SPIN_ON_OWNER 42#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
57 osq_lock_init(&lock->osq); 43 osq_lock_init(&lock->osq);
58#endif 44#endif
59 45
60 debug_mutex_init(lock, name, key); 46 debug_mutex_init(lock, name, key);
61} 47}
62
63EXPORT_SYMBOL(__mutex_init); 48EXPORT_SYMBOL(__mutex_init);
64 49
50/*
51 * @owner: contains: 'struct task_struct *' to the current lock owner,
52 * NULL means not owned. Since task_struct pointers are aligned at
53 * ARCH_MIN_TASKALIGN (which is at least sizeof(void *)), we have low
54 * bits to store extra state.
55 *
56 * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
57 */
58#define MUTEX_FLAG_WAITERS 0x01
59
60#define MUTEX_FLAGS 0x03
61
62static inline struct task_struct *__owner_task(unsigned long owner)
63{
64 return (struct task_struct *)(owner & ~MUTEX_FLAGS);
65}
66
67static inline unsigned long __owner_flags(unsigned long owner)
68{
69 return owner & MUTEX_FLAGS;
70}
71
72/*
73 * Actual trylock that will work on any unlocked state.
74 */
75static inline bool __mutex_trylock(struct mutex *lock)
76{
77 unsigned long owner, curr = (unsigned long)current;
78
79 owner = atomic_long_read(&lock->owner);
80 for (;;) { /* must loop, can race against a flag */
81 unsigned long old;
82
83 if (__owner_task(owner))
84 return false;
85
86 old = atomic_long_cmpxchg_acquire(&lock->owner, owner,
87 curr | __owner_flags(owner));
88 if (old == owner)
89 return true;
90
91 owner = old;
92 }
93}
94
95#ifndef CONFIG_DEBUG_LOCK_ALLOC
96/*
97 * Lockdep annotations are contained to the slow paths for simplicity.
98 * There is nothing that would stop spreading the lockdep annotations outwards
99 * except more code.
100 */
101
102/*
103 * Optimistic trylock that only works in the uncontended case. Make sure to
104 * follow with a __mutex_trylock() before failing.
105 */
106static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
107{
108 unsigned long curr = (unsigned long)current;
109
110 if (!atomic_long_cmpxchg_acquire(&lock->owner, 0UL, curr))
111 return true;
112
113 return false;
114}
115
116static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
117{
118 unsigned long curr = (unsigned long)current;
119
120 if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr)
121 return true;
122
123 return false;
124}
125#endif
126
127static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
128{
129 atomic_long_or(flag, &lock->owner);
130}
131
132static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
133{
134 atomic_long_andnot(flag, &lock->owner);
135}
136
65#ifndef CONFIG_DEBUG_LOCK_ALLOC 137#ifndef CONFIG_DEBUG_LOCK_ALLOC
66/* 138/*
67 * We split the mutex lock/unlock logic into separate fastpath and 139 * We split the mutex lock/unlock logic into separate fastpath and
@@ -69,7 +141,7 @@ EXPORT_SYMBOL(__mutex_init);
69 * We also put the fastpath first in the kernel image, to make sure the 141 * We also put the fastpath first in the kernel image, to make sure the
70 * branch is predicted by the CPU as default-untaken. 142 * branch is predicted by the CPU as default-untaken.
71 */ 143 */
72__visible void __sched __mutex_lock_slowpath(atomic_t *lock_count); 144static void __sched __mutex_lock_slowpath(struct mutex *lock);
73 145
74/** 146/**
75 * mutex_lock - acquire the mutex 147 * mutex_lock - acquire the mutex
@@ -95,14 +167,10 @@ __visible void __sched __mutex_lock_slowpath(atomic_t *lock_count);
95void __sched mutex_lock(struct mutex *lock) 167void __sched mutex_lock(struct mutex *lock)
96{ 168{
97 might_sleep(); 169 might_sleep();
98 /*
99 * The locking fastpath is the 1->0 transition from
100 * 'unlocked' into 'locked' state.
101 */
102 __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
103 mutex_set_owner(lock);
104}
105 170
171 if (!__mutex_trylock_fast(lock))
172 __mutex_lock_slowpath(lock);
173}
106EXPORT_SYMBOL(mutex_lock); 174EXPORT_SYMBOL(mutex_lock);
107#endif 175#endif
108 176
@@ -149,9 +217,6 @@ static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
149/* 217/*
150 * After acquiring lock with fastpath or when we lost out in contested 218 * After acquiring lock with fastpath or when we lost out in contested
151 * slowpath, set ctx and wake up any waiters so they can recheck. 219 * slowpath, set ctx and wake up any waiters so they can recheck.
152 *
153 * This function is never called when CONFIG_DEBUG_LOCK_ALLOC is set,
154 * as the fastpath and opportunistic spinning are disabled in that case.
155 */ 220 */
156static __always_inline void 221static __always_inline void
157ww_mutex_set_context_fastpath(struct ww_mutex *lock, 222ww_mutex_set_context_fastpath(struct ww_mutex *lock,
@@ -176,7 +241,7 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock,
176 /* 241 /*
177 * Check if lock is contended, if not there is nobody to wake up 242 * Check if lock is contended, if not there is nobody to wake up
178 */ 243 */
179 if (likely(atomic_read(&lock->base.count) == 0)) 244 if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS)))
180 return; 245 return;
181 246
182 /* 247 /*
@@ -227,7 +292,7 @@ bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
227 bool ret = true; 292 bool ret = true;
228 293
229 rcu_read_lock(); 294 rcu_read_lock();
230 while (lock->owner == owner) { 295 while (__mutex_owner(lock) == owner) {
231 /* 296 /*
232 * Ensure we emit the owner->on_cpu, dereference _after_ 297 * Ensure we emit the owner->on_cpu, dereference _after_
233 * checking lock->owner still matches owner. If that fails, 298 * checking lock->owner still matches owner. If that fails,
@@ -260,27 +325,20 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock)
260 return 0; 325 return 0;
261 326
262 rcu_read_lock(); 327 rcu_read_lock();
263 owner = READ_ONCE(lock->owner); 328 owner = __mutex_owner(lock);
264 if (owner) 329 if (owner)
265 retval = owner->on_cpu; 330 retval = owner->on_cpu;
266 rcu_read_unlock(); 331 rcu_read_unlock();
332
267 /* 333 /*
268 * if lock->owner is not set, the mutex owner may have just acquired 334 * If lock->owner is not set, the mutex has been released. Return true
269 * it and not set the owner yet or the mutex has been released. 335 * such that we'll trylock in the spin path, which is a faster option
336 * than the blocking slow path.
270 */ 337 */
271 return retval; 338 return retval;
272} 339}
273 340
274/* 341/*
275 * Atomically try to take the lock when it is available
276 */
277static inline bool mutex_try_to_acquire(struct mutex *lock)
278{
279 return !mutex_is_locked(lock) &&
280 (atomic_cmpxchg_acquire(&lock->count, 1, 0) == 1);
281}
282
283/*
284 * Optimistic spinning. 342 * Optimistic spinning.
285 * 343 *
286 * We try to spin for acquisition when we find that the lock owner 344 * We try to spin for acquisition when we find that the lock owner
@@ -288,13 +346,6 @@ static inline bool mutex_try_to_acquire(struct mutex *lock)
288 * need to reschedule. The rationale is that if the lock owner is 346 * need to reschedule. The rationale is that if the lock owner is
289 * running, it is likely to release the lock soon. 347 * running, it is likely to release the lock soon.
290 * 348 *
291 * Since this needs the lock owner, and this mutex implementation
292 * doesn't track the owner atomically in the lock field, we need to
293 * track it non-atomically.
294 *
295 * We can't do this for DEBUG_MUTEXES because that relies on wait_lock
296 * to serialize everything.
297 *
298 * The mutex spinners are queued up using MCS lock so that only one 349 * The mutex spinners are queued up using MCS lock so that only one
299 * spinner can compete for the mutex. However, if mutex spinning isn't 350 * spinner can compete for the mutex. However, if mutex spinning isn't
300 * going to happen, there is no point in going through the lock/unlock 351 * going to happen, there is no point in going through the lock/unlock
@@ -342,36 +393,17 @@ static bool mutex_optimistic_spin(struct mutex *lock,
342 * If there's an owner, wait for it to either 393 * If there's an owner, wait for it to either
343 * release the lock or go to sleep. 394 * release the lock or go to sleep.
344 */ 395 */
345 owner = READ_ONCE(lock->owner); 396 owner = __mutex_owner(lock);
346 if (owner && !mutex_spin_on_owner(lock, owner)) 397 if (owner && !mutex_spin_on_owner(lock, owner))
347 break; 398 break;
348 399
349 /* Try to acquire the mutex if it is unlocked. */ 400 /* Try to acquire the mutex if it is unlocked. */
350 if (mutex_try_to_acquire(lock)) { 401 if (__mutex_trylock(lock)) {
351 lock_acquired(&lock->dep_map, ip);
352
353 if (use_ww_ctx) {
354 struct ww_mutex *ww;
355 ww = container_of(lock, struct ww_mutex, base);
356
357 ww_mutex_set_context_fastpath(ww, ww_ctx);
358 }
359
360 mutex_set_owner(lock);
361 osq_unlock(&lock->osq); 402 osq_unlock(&lock->osq);
362 return true; 403 return true;
363 } 404 }
364 405
365 /* 406 /*
366 * When there's no owner, we might have preempted between the
367 * owner acquiring the lock and setting the owner field. If
368 * we're an RT task that will live-lock because we won't let
369 * the owner complete.
370 */
371 if (!owner && (need_resched() || rt_task(task)))
372 break;
373
374 /*
375 * The cpu_relax() call is a compiler barrier which forces 407 * The cpu_relax() call is a compiler barrier which forces
376 * everything in this loop to be re-loaded. We don't need 408 * everything in this loop to be re-loaded. We don't need
377 * memory barriers as we'll eventually observe the right 409 * memory barriers as we'll eventually observe the right
@@ -406,8 +438,7 @@ static bool mutex_optimistic_spin(struct mutex *lock,
406} 438}
407#endif 439#endif
408 440
409__visible __used noinline 441static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
410void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
411 442
412/** 443/**
413 * mutex_unlock - release the mutex 444 * mutex_unlock - release the mutex
@@ -422,21 +453,12 @@ void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
422 */ 453 */
423void __sched mutex_unlock(struct mutex *lock) 454void __sched mutex_unlock(struct mutex *lock)
424{ 455{
425 /* 456#ifndef CONFIG_DEBUG_LOCK_ALLOC
426 * The unlocking fastpath is the 0->1 transition from 'locked' 457 if (__mutex_unlock_fast(lock))
427 * into 'unlocked' state: 458 return;
428 */
429#ifndef CONFIG_DEBUG_MUTEXES
430 /*
431 * When debugging is enabled we must not clear the owner before time,
432 * the slow path will always be taken, and that clears the owner field
433 * after verifying that it was indeed current.
434 */
435 mutex_clear_owner(lock);
436#endif 459#endif
437 __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath); 460 __mutex_unlock_slowpath(lock, _RET_IP_);
438} 461}
439
440EXPORT_SYMBOL(mutex_unlock); 462EXPORT_SYMBOL(mutex_unlock);
441 463
442/** 464/**
@@ -465,15 +487,7 @@ void __sched ww_mutex_unlock(struct ww_mutex *lock)
465 lock->ctx = NULL; 487 lock->ctx = NULL;
466 } 488 }
467 489
468#ifndef CONFIG_DEBUG_MUTEXES 490 mutex_unlock(&lock->base);
469 /*
470 * When debugging is enabled we must not clear the owner before time,
471 * the slow path will always be taken, and that clears the owner field
472 * after verifying that it was indeed current.
473 */
474 mutex_clear_owner(&lock->base);
475#endif
476 __mutex_fastpath_unlock(&lock->base.count, __mutex_unlock_slowpath);
477} 491}
478EXPORT_SYMBOL(ww_mutex_unlock); 492EXPORT_SYMBOL(ww_mutex_unlock);
479 493
@@ -520,20 +534,24 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
520 preempt_disable(); 534 preempt_disable();
521 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 535 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
522 536
523 if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) { 537 if (__mutex_trylock(lock) || mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) {
524 /* got the lock, yay! */ 538 /* got the lock, yay! */
539 lock_acquired(&lock->dep_map, ip);
540 if (use_ww_ctx) {
541 struct ww_mutex *ww;
542 ww = container_of(lock, struct ww_mutex, base);
543
544 ww_mutex_set_context_fastpath(ww, ww_ctx);
545 }
525 preempt_enable(); 546 preempt_enable();
526 return 0; 547 return 0;
527 } 548 }
528 549
529 spin_lock_mutex(&lock->wait_lock, flags); 550 spin_lock_mutex(&lock->wait_lock, flags);
530
531 /* 551 /*
532 * Once more, try to acquire the lock. Only try-lock the mutex if 552 * After waiting to acquire the wait_lock, try again.
533 * it is unlocked to reduce unnecessary xchg() operations.
534 */ 553 */
535 if (!mutex_is_locked(lock) && 554 if (__mutex_trylock(lock))
536 (atomic_xchg_acquire(&lock->count, 0) == 1))
537 goto skip_wait; 555 goto skip_wait;
538 556
539 debug_mutex_lock_common(lock, &waiter); 557 debug_mutex_lock_common(lock, &waiter);
@@ -543,21 +561,13 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
543 list_add_tail(&waiter.list, &lock->wait_list); 561 list_add_tail(&waiter.list, &lock->wait_list);
544 waiter.task = task; 562 waiter.task = task;
545 563
564 if (list_first_entry(&lock->wait_list, struct mutex_waiter, list) == &waiter)
565 __mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
566
546 lock_contended(&lock->dep_map, ip); 567 lock_contended(&lock->dep_map, ip);
547 568
548 for (;;) { 569 for (;;) {
549 /* 570 if (__mutex_trylock(lock))
550 * Lets try to take the lock again - this is needed even if
551 * we get here for the first time (shortly after failing to
552 * acquire the lock), to make sure that we get a wakeup once
553 * it's unlocked. Later on, if we sleep, this is the
554 * operation that gives us the lock. We xchg it to -1, so
555 * that when we release the lock, we properly wake up the
556 * other waiters. We only attempt the xchg if the count is
557 * non-negative in order to avoid unnecessary xchg operations:
558 */
559 if (atomic_read(&lock->count) >= 0 &&
560 (atomic_xchg_acquire(&lock->count, -1) == 1))
561 break; 571 break;
562 572
563 /* 573 /*
@@ -585,15 +595,14 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
585 __set_task_state(task, TASK_RUNNING); 595 __set_task_state(task, TASK_RUNNING);
586 596
587 mutex_remove_waiter(lock, &waiter, task); 597 mutex_remove_waiter(lock, &waiter, task);
588 /* set it to 0 if there are no waiters left: */
589 if (likely(list_empty(&lock->wait_list))) 598 if (likely(list_empty(&lock->wait_list)))
590 atomic_set(&lock->count, 0); 599 __mutex_clear_flag(lock, MUTEX_FLAG_WAITERS);
600
591 debug_mutex_free_waiter(&waiter); 601 debug_mutex_free_waiter(&waiter);
592 602
593skip_wait: 603skip_wait:
594 /* got the lock - cleanup and rejoice! */ 604 /* got the lock - cleanup and rejoice! */
595 lock_acquired(&lock->dep_map, ip); 605 lock_acquired(&lock->dep_map, ip);
596 mutex_set_owner(lock);
597 606
598 if (use_ww_ctx) { 607 if (use_ww_ctx) {
599 struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 608 struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
@@ -631,7 +640,6 @@ _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
631 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 640 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
632 0, nest, _RET_IP_, NULL, 0); 641 0, nest, _RET_IP_, NULL, 0);
633} 642}
634
635EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 643EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
636 644
637int __sched 645int __sched
@@ -650,7 +658,6 @@ mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
650 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 658 return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
651 subclass, NULL, _RET_IP_, NULL, 0); 659 subclass, NULL, _RET_IP_, NULL, 0);
652} 660}
653
654EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 661EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
655 662
656static inline int 663static inline int
@@ -715,29 +722,22 @@ EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
715/* 722/*
716 * Release the lock, slowpath: 723 * Release the lock, slowpath:
717 */ 724 */
718static inline void 725static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
719__mutex_unlock_common_slowpath(struct mutex *lock, int nested)
720{ 726{
721 unsigned long flags; 727 unsigned long owner, flags;
722 WAKE_Q(wake_q); 728 WAKE_Q(wake_q);
723 729
730 mutex_release(&lock->dep_map, 1, ip);
731
724 /* 732 /*
725 * As a performance measurement, release the lock before doing other 733 * Release the lock before (potentially) taking the spinlock
726 * wakeup related duties to follow. This allows other tasks to acquire 734 * such that other contenders can get on with things ASAP.
727 * the lock sooner, while still handling cleanups in past unlock calls.
728 * This can be done as we do not enforce strict equivalence between the
729 * mutex counter and wait_list.
730 *
731 *
732 * Some architectures leave the lock unlocked in the fastpath failure
733 * case, others need to leave it locked. In the later case we have to
734 * unlock it here - as the lock counter is currently 0 or negative.
735 */ 735 */
736 if (__mutex_slowpath_needs_to_unlock()) 736 owner = atomic_long_fetch_and_release(MUTEX_FLAGS, &lock->owner);
737 atomic_set(&lock->count, 1); 737 if (!__owner_flags(owner))
738 return;
738 739
739 spin_lock_mutex(&lock->wait_lock, flags); 740 spin_lock_mutex(&lock->wait_lock, flags);
740 mutex_release(&lock->dep_map, nested, _RET_IP_);
741 debug_mutex_unlock(lock); 741 debug_mutex_unlock(lock);
742 742
743 if (!list_empty(&lock->wait_list)) { 743 if (!list_empty(&lock->wait_list)) {
@@ -754,17 +754,6 @@ __mutex_unlock_common_slowpath(struct mutex *lock, int nested)
754 wake_up_q(&wake_q); 754 wake_up_q(&wake_q);
755} 755}
756 756
757/*
758 * Release the lock, slowpath:
759 */
760__visible void
761__mutex_unlock_slowpath(atomic_t *lock_count)
762{
763 struct mutex *lock = container_of(lock_count, struct mutex, count);
764
765 __mutex_unlock_common_slowpath(lock, 1);
766}
767
768#ifndef CONFIG_DEBUG_LOCK_ALLOC 757#ifndef CONFIG_DEBUG_LOCK_ALLOC
769/* 758/*
770 * Here come the less common (and hence less performance-critical) APIs: 759 * Here come the less common (and hence less performance-critical) APIs:
@@ -789,38 +778,30 @@ __mutex_lock_interruptible_slowpath(struct mutex *lock);
789 */ 778 */
790int __sched mutex_lock_interruptible(struct mutex *lock) 779int __sched mutex_lock_interruptible(struct mutex *lock)
791{ 780{
792 int ret;
793
794 might_sleep(); 781 might_sleep();
795 ret = __mutex_fastpath_lock_retval(&lock->count); 782
796 if (likely(!ret)) { 783 if (__mutex_trylock_fast(lock))
797 mutex_set_owner(lock);
798 return 0; 784 return 0;
799 } else 785
800 return __mutex_lock_interruptible_slowpath(lock); 786 return __mutex_lock_interruptible_slowpath(lock);
801} 787}
802 788
803EXPORT_SYMBOL(mutex_lock_interruptible); 789EXPORT_SYMBOL(mutex_lock_interruptible);
804 790
805int __sched mutex_lock_killable(struct mutex *lock) 791int __sched mutex_lock_killable(struct mutex *lock)
806{ 792{
807 int ret;
808
809 might_sleep(); 793 might_sleep();
810 ret = __mutex_fastpath_lock_retval(&lock->count); 794
811 if (likely(!ret)) { 795 if (__mutex_trylock_fast(lock))
812 mutex_set_owner(lock);
813 return 0; 796 return 0;
814 } else 797
815 return __mutex_lock_killable_slowpath(lock); 798 return __mutex_lock_killable_slowpath(lock);
816} 799}
817EXPORT_SYMBOL(mutex_lock_killable); 800EXPORT_SYMBOL(mutex_lock_killable);
818 801
819__visible void __sched 802static noinline void __sched
820__mutex_lock_slowpath(atomic_t *lock_count) 803__mutex_lock_slowpath(struct mutex *lock)
821{ 804{
822 struct mutex *lock = container_of(lock_count, struct mutex, count);
823
824 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, 805 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
825 NULL, _RET_IP_, NULL, 0); 806 NULL, _RET_IP_, NULL, 0);
826} 807}
@@ -856,37 +837,6 @@ __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
856 837
857#endif 838#endif
858 839
859/*
860 * Spinlock based trylock, we take the spinlock and check whether we
861 * can get the lock:
862 */
863static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
864{
865 struct mutex *lock = container_of(lock_count, struct mutex, count);
866 unsigned long flags;
867 int prev;
868
869 /* No need to trylock if the mutex is locked. */
870 if (mutex_is_locked(lock))
871 return 0;
872
873 spin_lock_mutex(&lock->wait_lock, flags);
874
875 prev = atomic_xchg_acquire(&lock->count, -1);
876 if (likely(prev == 1)) {
877 mutex_set_owner(lock);
878 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
879 }
880
881 /* Set it back to 0 if there are no waiters: */
882 if (likely(list_empty(&lock->wait_list)))
883 atomic_set(&lock->count, 0);
884
885 spin_unlock_mutex(&lock->wait_lock, flags);
886
887 return prev == 1;
888}
889
890/** 840/**
891 * mutex_trylock - try to acquire the mutex, without waiting 841 * mutex_trylock - try to acquire the mutex, without waiting
892 * @lock: the mutex to be acquired 842 * @lock: the mutex to be acquired
@@ -903,13 +853,12 @@ static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
903 */ 853 */
904int __sched mutex_trylock(struct mutex *lock) 854int __sched mutex_trylock(struct mutex *lock)
905{ 855{
906 int ret; 856 bool locked = __mutex_trylock(lock);
907 857
908 ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath); 858 if (locked)
909 if (ret) 859 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
910 mutex_set_owner(lock);
911 860
912 return ret; 861 return locked;
913} 862}
914EXPORT_SYMBOL(mutex_trylock); 863EXPORT_SYMBOL(mutex_trylock);
915 864
@@ -917,36 +866,28 @@ EXPORT_SYMBOL(mutex_trylock);
917int __sched 866int __sched
918__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 867__ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
919{ 868{
920 int ret;
921
922 might_sleep(); 869 might_sleep();
923 870
924 ret = __mutex_fastpath_lock_retval(&lock->base.count); 871 if (__mutex_trylock_fast(&lock->base)) {
925
926 if (likely(!ret)) {
927 ww_mutex_set_context_fastpath(lock, ctx); 872 ww_mutex_set_context_fastpath(lock, ctx);
928 mutex_set_owner(&lock->base); 873 return 0;
929 } else 874 }
930 ret = __ww_mutex_lock_slowpath(lock, ctx); 875
931 return ret; 876 return __ww_mutex_lock_slowpath(lock, ctx);
932} 877}
933EXPORT_SYMBOL(__ww_mutex_lock); 878EXPORT_SYMBOL(__ww_mutex_lock);
934 879
935int __sched 880int __sched
936__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 881__ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
937{ 882{
938 int ret;
939
940 might_sleep(); 883 might_sleep();
941 884
942 ret = __mutex_fastpath_lock_retval(&lock->base.count); 885 if (__mutex_trylock_fast(&lock->base)) {
943
944 if (likely(!ret)) {
945 ww_mutex_set_context_fastpath(lock, ctx); 886 ww_mutex_set_context_fastpath(lock, ctx);
946 mutex_set_owner(&lock->base); 887 return 0;
947 } else 888 }
948 ret = __ww_mutex_lock_interruptible_slowpath(lock, ctx); 889
949 return ret; 890 return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
950} 891}
951EXPORT_SYMBOL(__ww_mutex_lock_interruptible); 892EXPORT_SYMBOL(__ww_mutex_lock_interruptible);
952 893
diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h
index 6cd6b8e9efd7..4410a4af42a3 100644
--- a/kernel/locking/mutex.h
+++ b/kernel/locking/mutex.h
@@ -16,32 +16,6 @@
16#define mutex_remove_waiter(lock, waiter, task) \ 16#define mutex_remove_waiter(lock, waiter, task) \
17 __list_del((waiter)->list.prev, (waiter)->list.next) 17 __list_del((waiter)->list.prev, (waiter)->list.next)
18 18
19#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
20/*
21 * The mutex owner can get read and written to locklessly.
22 * We should use WRITE_ONCE when writing the owner value to
23 * avoid store tearing, otherwise, a thread could potentially
24 * read a partially written and incomplete owner value.
25 */
26static inline void mutex_set_owner(struct mutex *lock)
27{
28 WRITE_ONCE(lock->owner, current);
29}
30
31static inline void mutex_clear_owner(struct mutex *lock)
32{
33 WRITE_ONCE(lock->owner, NULL);
34}
35#else
36static inline void mutex_set_owner(struct mutex *lock)
37{
38}
39
40static inline void mutex_clear_owner(struct mutex *lock)
41{
42}
43#endif
44
45#define debug_mutex_wake_waiter(lock, waiter) do { } while (0) 19#define debug_mutex_wake_waiter(lock, waiter) do { } while (0)
46#define debug_mutex_free_waiter(waiter) do { } while (0) 20#define debug_mutex_free_waiter(waiter) do { } while (0)
47#define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0) 21#define debug_mutex_add_waiter(lock, waiter, ti) do { } while (0)
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 94732d1ab00a..8912aafd09e1 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -75,11 +75,11 @@
75#include <linux/compiler.h> 75#include <linux/compiler.h>
76#include <linux/frame.h> 76#include <linux/frame.h>
77#include <linux/prefetch.h> 77#include <linux/prefetch.h>
78#include <linux/mutex.h>
78 79
79#include <asm/switch_to.h> 80#include <asm/switch_to.h>
80#include <asm/tlb.h> 81#include <asm/tlb.h>
81#include <asm/irq_regs.h> 82#include <asm/irq_regs.h>
82#include <asm/mutex.h>
83#ifdef CONFIG_PARAVIRT 83#ifdef CONFIG_PARAVIRT
84#include <asm/paravirt.h> 84#include <asm/paravirt.h>
85#endif 85#endif