aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/linux/mutex-debug.h24
-rw-r--r--include/linux/mutex.h46
-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
7 files changed, 187 insertions, 305 deletions
diff --git a/include/linux/mutex-debug.h b/include/linux/mutex-debug.h
deleted file mode 100644
index 4ac8b1977b73..000000000000
--- a/include/linux/mutex-debug.h
+++ /dev/null
@@ -1,24 +0,0 @@
1#ifndef __LINUX_MUTEX_DEBUG_H
2#define __LINUX_MUTEX_DEBUG_H
3
4#include <linux/linkage.h>
5#include <linux/lockdep.h>
6#include <linux/debug_locks.h>
7
8/*
9 * Mutexes - debugging helpers:
10 */
11
12#define __DEBUG_MUTEX_INITIALIZER(lockname) \
13 , .magic = &lockname
14
15#define mutex_init(mutex) \
16do { \
17 static struct lock_class_key __key; \
18 \
19 __mutex_init((mutex), #mutex, &__key); \
20} while (0)
21
22extern void mutex_destroy(struct mutex *lock);
23
24#endif
diff --git a/include/linux/mutex.h b/include/linux/mutex.h
index 2cb7531e7d7a..4d3bccabbea5 100644
--- a/include/linux/mutex.h
+++ b/include/linux/mutex.h
@@ -18,6 +18,7 @@
18#include <linux/atomic.h> 18#include <linux/atomic.h>
19#include <asm/processor.h> 19#include <asm/processor.h>
20#include <linux/osq_lock.h> 20#include <linux/osq_lock.h>
21#include <linux/debug_locks.h>
21 22
22/* 23/*
23 * Simple, straightforward mutexes with strict semantics: 24 * Simple, straightforward mutexes with strict semantics:
@@ -48,16 +49,12 @@
48 * locks and tasks (and only those tasks) 49 * locks and tasks (and only those tasks)
49 */ 50 */
50struct mutex { 51struct mutex {
51 /* 1: unlocked, 0: locked, negative: locked, possible waiters */ 52 atomic_long_t owner;
52 atomic_t count;
53 spinlock_t wait_lock; 53 spinlock_t wait_lock;
54 struct list_head wait_list;
55#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_MUTEX_SPIN_ON_OWNER)
56 struct task_struct *owner;
57#endif
58#ifdef CONFIG_MUTEX_SPIN_ON_OWNER 54#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
59 struct optimistic_spin_queue osq; /* Spinner MCS lock */ 55 struct optimistic_spin_queue osq; /* Spinner MCS lock */
60#endif 56#endif
57 struct list_head wait_list;
61#ifdef CONFIG_DEBUG_MUTEXES 58#ifdef CONFIG_DEBUG_MUTEXES
62 void *magic; 59 void *magic;
63#endif 60#endif
@@ -66,6 +63,11 @@ struct mutex {
66#endif 63#endif
67}; 64};
68 65
66static inline struct task_struct *__mutex_owner(struct mutex *lock)
67{
68 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~0x03);
69}
70
69/* 71/*
70 * This is the control structure for tasks blocked on mutex, 72 * This is the control structure for tasks blocked on mutex,
71 * which resides on the blocked task's kernel stack: 73 * which resides on the blocked task's kernel stack:
@@ -79,9 +81,20 @@ struct mutex_waiter {
79}; 81};
80 82
81#ifdef CONFIG_DEBUG_MUTEXES 83#ifdef CONFIG_DEBUG_MUTEXES
82# include <linux/mutex-debug.h> 84
85#define __DEBUG_MUTEX_INITIALIZER(lockname) \
86 , .magic = &lockname
87
88extern void mutex_destroy(struct mutex *lock);
89
83#else 90#else
91
84# define __DEBUG_MUTEX_INITIALIZER(lockname) 92# define __DEBUG_MUTEX_INITIALIZER(lockname)
93
94static inline void mutex_destroy(struct mutex *lock) {}
95
96#endif
97
85/** 98/**
86 * mutex_init - initialize the mutex 99 * mutex_init - initialize the mutex
87 * @mutex: the mutex to be initialized 100 * @mutex: the mutex to be initialized
@@ -90,14 +103,12 @@ struct mutex_waiter {
90 * 103 *
91 * It is not allowed to initialize an already locked mutex. 104 * It is not allowed to initialize an already locked mutex.
92 */ 105 */
93# define mutex_init(mutex) \ 106#define mutex_init(mutex) \
94do { \ 107do { \
95 static struct lock_class_key __key; \ 108 static struct lock_class_key __key; \
96 \ 109 \
97 __mutex_init((mutex), #mutex, &__key); \ 110 __mutex_init((mutex), #mutex, &__key); \
98} while (0) 111} while (0)
99static inline void mutex_destroy(struct mutex *lock) {}
100#endif
101 112
102#ifdef CONFIG_DEBUG_LOCK_ALLOC 113#ifdef CONFIG_DEBUG_LOCK_ALLOC
103# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \ 114# define __DEP_MAP_MUTEX_INITIALIZER(lockname) \
@@ -107,7 +118,7 @@ static inline void mutex_destroy(struct mutex *lock) {}
107#endif 118#endif
108 119
109#define __MUTEX_INITIALIZER(lockname) \ 120#define __MUTEX_INITIALIZER(lockname) \
110 { .count = ATOMIC_INIT(1) \ 121 { .owner = ATOMIC_LONG_INIT(0) \
111 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \ 122 , .wait_lock = __SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
112 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \ 123 , .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
113 __DEBUG_MUTEX_INITIALIZER(lockname) \ 124 __DEBUG_MUTEX_INITIALIZER(lockname) \
@@ -127,7 +138,10 @@ extern void __mutex_init(struct mutex *lock, const char *name,
127 */ 138 */
128static inline int mutex_is_locked(struct mutex *lock) 139static inline int mutex_is_locked(struct mutex *lock)
129{ 140{
130 return atomic_read(&lock->count) != 1; 141 /*
142 * XXX think about spin_is_locked
143 */
144 return __mutex_owner(lock) != NULL;
131} 145}
132 146
133/* 147/*
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