diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-10-13 09:51:40 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-10-13 09:51:40 -0400 |
commit | 6d5f0ebfc0be9cbfeaafdd9258d5fa24b7975a36 (patch) | |
tree | 3b7a5851a3d9f02441e2dcbaf22785d131544544 /kernel/locking | |
parent | dbb885fecc1b1b35e93416bedd24d21bd20f60ed (diff) | |
parent | 8acd91e8620836a56ff62028ed28ba629f2881a0 (diff) |
Merge branch 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull core locking updates from Ingo Molnar:
"The main updates in this cycle were:
- mutex MCS refactoring finishing touches: improve comments, refactor
and clean up code, reduce debug data structure footprint, etc.
- qrwlock finishing touches: remove old code, self-test updates.
- small rwsem optimization
- various smaller fixes/cleanups"
* 'locking-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
locking/lockdep: Revert qrwlock recusive stuff
locking/rwsem: Avoid double checking before try acquiring write lock
locking/rwsem: Move EXPORT_SYMBOL() lines to follow function definition
locking/rwlock, x86: Delete unused asm/rwlock.h and rwlock.S
locking/rwlock, x86: Clean up asm/spinlock*.h to remove old rwlock code
locking/semaphore: Resolve some shadow warnings
locking/selftest: Support queued rwlock
locking/lockdep: Restrict the use of recursive read_lock() with qrwlock
locking/spinlocks: Always evaluate the second argument of spin_lock_nested()
locking/Documentation: Update locking/mutex-design.txt disadvantages
locking/Documentation: Move locking related docs into Documentation/locking/
locking/mutexes: Use MUTEX_SPIN_ON_OWNER when appropriate
locking/mutexes: Refactor optimistic spinning code
locking/mcs: Remove obsolete comment
locking/mutexes: Document quick lock release when unlocking
locking/mutexes: Standardize arguments in lock/unlock slowpaths
locking: Remove deprecated smp_mb__() barriers
Diffstat (limited to 'kernel/locking')
-rw-r--r-- | kernel/locking/mcs_spinlock.h | 3 | ||||
-rw-r--r-- | kernel/locking/mutex.c | 416 | ||||
-rw-r--r-- | kernel/locking/mutex.h | 2 | ||||
-rw-r--r-- | kernel/locking/rtmutex.c | 2 | ||||
-rw-r--r-- | kernel/locking/rwsem-xadd.c | 27 | ||||
-rw-r--r-- | kernel/locking/semaphore.c | 12 |
6 files changed, 250 insertions, 212 deletions
diff --git a/kernel/locking/mcs_spinlock.h b/kernel/locking/mcs_spinlock.h index 23e89c5930e9..4d60986fcbee 100644 --- a/kernel/locking/mcs_spinlock.h +++ b/kernel/locking/mcs_spinlock.h | |||
@@ -56,9 +56,6 @@ do { \ | |||
56 | * If the lock has already been acquired, then this will proceed to spin | 56 | * If the lock has already been acquired, then this will proceed to spin |
57 | * on this node->locked until the previous lock holder sets the node->locked | 57 | * on this node->locked until the previous lock holder sets the node->locked |
58 | * in mcs_spin_unlock(). | 58 | * in mcs_spin_unlock(). |
59 | * | ||
60 | * We don't inline mcs_spin_lock() so that perf can correctly account for the | ||
61 | * time spent in this lock function. | ||
62 | */ | 59 | */ |
63 | static inline | 60 | static inline |
64 | void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node) | 61 | void mcs_spin_lock(struct mcs_spinlock **lock, struct mcs_spinlock *node) |
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index ae712b25e492..dadbf88c22c4 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c | |||
@@ -15,7 +15,7 @@ | |||
15 | * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale | 15 | * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale |
16 | * and Sven Dietrich. | 16 | * and Sven Dietrich. |
17 | * | 17 | * |
18 | * Also see Documentation/mutex-design.txt. | 18 | * Also see Documentation/locking/mutex-design.txt. |
19 | */ | 19 | */ |
20 | #include <linux/mutex.h> | 20 | #include <linux/mutex.h> |
21 | #include <linux/ww_mutex.h> | 21 | #include <linux/ww_mutex.h> |
@@ -106,6 +106,92 @@ void __sched mutex_lock(struct mutex *lock) | |||
106 | EXPORT_SYMBOL(mutex_lock); | 106 | EXPORT_SYMBOL(mutex_lock); |
107 | #endif | 107 | #endif |
108 | 108 | ||
109 | static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww, | ||
110 | struct ww_acquire_ctx *ww_ctx) | ||
111 | { | ||
112 | #ifdef CONFIG_DEBUG_MUTEXES | ||
113 | /* | ||
114 | * If this WARN_ON triggers, you used ww_mutex_lock to acquire, | ||
115 | * but released with a normal mutex_unlock in this call. | ||
116 | * | ||
117 | * This should never happen, always use ww_mutex_unlock. | ||
118 | */ | ||
119 | DEBUG_LOCKS_WARN_ON(ww->ctx); | ||
120 | |||
121 | /* | ||
122 | * Not quite done after calling ww_acquire_done() ? | ||
123 | */ | ||
124 | DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire); | ||
125 | |||
126 | if (ww_ctx->contending_lock) { | ||
127 | /* | ||
128 | * After -EDEADLK you tried to | ||
129 | * acquire a different ww_mutex? Bad! | ||
130 | */ | ||
131 | DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww); | ||
132 | |||
133 | /* | ||
134 | * You called ww_mutex_lock after receiving -EDEADLK, | ||
135 | * but 'forgot' to unlock everything else first? | ||
136 | */ | ||
137 | DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0); | ||
138 | ww_ctx->contending_lock = NULL; | ||
139 | } | ||
140 | |||
141 | /* | ||
142 | * Naughty, using a different class will lead to undefined behavior! | ||
143 | */ | ||
144 | DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class); | ||
145 | #endif | ||
146 | ww_ctx->acquired++; | ||
147 | } | ||
148 | |||
149 | /* | ||
150 | * 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. | ||
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 | */ | ||
156 | static __always_inline void | ||
157 | ww_mutex_set_context_fastpath(struct ww_mutex *lock, | ||
158 | struct ww_acquire_ctx *ctx) | ||
159 | { | ||
160 | unsigned long flags; | ||
161 | struct mutex_waiter *cur; | ||
162 | |||
163 | ww_mutex_lock_acquired(lock, ctx); | ||
164 | |||
165 | lock->ctx = ctx; | ||
166 | |||
167 | /* | ||
168 | * The lock->ctx update should be visible on all cores before | ||
169 | * the atomic read is done, otherwise contended waiters might be | ||
170 | * missed. The contended waiters will either see ww_ctx == NULL | ||
171 | * and keep spinning, or it will acquire wait_lock, add itself | ||
172 | * to waiter list and sleep. | ||
173 | */ | ||
174 | smp_mb(); /* ^^^ */ | ||
175 | |||
176 | /* | ||
177 | * Check if lock is contended, if not there is nobody to wake up | ||
178 | */ | ||
179 | if (likely(atomic_read(&lock->base.count) == 0)) | ||
180 | return; | ||
181 | |||
182 | /* | ||
183 | * Uh oh, we raced in fastpath, wake up everyone in this case, | ||
184 | * so they can see the new lock->ctx. | ||
185 | */ | ||
186 | spin_lock_mutex(&lock->base.wait_lock, flags); | ||
187 | list_for_each_entry(cur, &lock->base.wait_list, list) { | ||
188 | debug_mutex_wake_waiter(&lock->base, cur); | ||
189 | wake_up_process(cur->task); | ||
190 | } | ||
191 | spin_unlock_mutex(&lock->base.wait_lock, flags); | ||
192 | } | ||
193 | |||
194 | |||
109 | #ifdef CONFIG_MUTEX_SPIN_ON_OWNER | 195 | #ifdef CONFIG_MUTEX_SPIN_ON_OWNER |
110 | /* | 196 | /* |
111 | * In order to avoid a stampede of mutex spinners from acquiring the mutex | 197 | * In order to avoid a stampede of mutex spinners from acquiring the mutex |
@@ -180,6 +266,129 @@ static inline int mutex_can_spin_on_owner(struct mutex *lock) | |||
180 | */ | 266 | */ |
181 | return retval; | 267 | return retval; |
182 | } | 268 | } |
269 | |||
270 | /* | ||
271 | * Atomically try to take the lock when it is available | ||
272 | */ | ||
273 | static inline bool mutex_try_to_acquire(struct mutex *lock) | ||
274 | { | ||
275 | return !mutex_is_locked(lock) && | ||
276 | (atomic_cmpxchg(&lock->count, 1, 0) == 1); | ||
277 | } | ||
278 | |||
279 | /* | ||
280 | * Optimistic spinning. | ||
281 | * | ||
282 | * We try to spin for acquisition when we find that the lock owner | ||
283 | * is currently running on a (different) CPU and while we don't | ||
284 | * need to reschedule. The rationale is that if the lock owner is | ||
285 | * running, it is likely to release the lock soon. | ||
286 | * | ||
287 | * Since this needs the lock owner, and this mutex implementation | ||
288 | * doesn't track the owner atomically in the lock field, we need to | ||
289 | * track it non-atomically. | ||
290 | * | ||
291 | * We can't do this for DEBUG_MUTEXES because that relies on wait_lock | ||
292 | * to serialize everything. | ||
293 | * | ||
294 | * The mutex spinners are queued up using MCS lock so that only one | ||
295 | * spinner can compete for the mutex. However, if mutex spinning isn't | ||
296 | * going to happen, there is no point in going through the lock/unlock | ||
297 | * overhead. | ||
298 | * | ||
299 | * Returns true when the lock was taken, otherwise false, indicating | ||
300 | * that we need to jump to the slowpath and sleep. | ||
301 | */ | ||
302 | static bool mutex_optimistic_spin(struct mutex *lock, | ||
303 | struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) | ||
304 | { | ||
305 | struct task_struct *task = current; | ||
306 | |||
307 | if (!mutex_can_spin_on_owner(lock)) | ||
308 | goto done; | ||
309 | |||
310 | if (!osq_lock(&lock->osq)) | ||
311 | goto done; | ||
312 | |||
313 | while (true) { | ||
314 | struct task_struct *owner; | ||
315 | |||
316 | if (use_ww_ctx && ww_ctx->acquired > 0) { | ||
317 | struct ww_mutex *ww; | ||
318 | |||
319 | ww = container_of(lock, struct ww_mutex, base); | ||
320 | /* | ||
321 | * If ww->ctx is set the contents are undefined, only | ||
322 | * by acquiring wait_lock there is a guarantee that | ||
323 | * they are not invalid when reading. | ||
324 | * | ||
325 | * As such, when deadlock detection needs to be | ||
326 | * performed the optimistic spinning cannot be done. | ||
327 | */ | ||
328 | if (ACCESS_ONCE(ww->ctx)) | ||
329 | break; | ||
330 | } | ||
331 | |||
332 | /* | ||
333 | * If there's an owner, wait for it to either | ||
334 | * release the lock or go to sleep. | ||
335 | */ | ||
336 | owner = ACCESS_ONCE(lock->owner); | ||
337 | if (owner && !mutex_spin_on_owner(lock, owner)) | ||
338 | break; | ||
339 | |||
340 | /* Try to acquire the mutex if it is unlocked. */ | ||
341 | if (mutex_try_to_acquire(lock)) { | ||
342 | lock_acquired(&lock->dep_map, ip); | ||
343 | |||
344 | if (use_ww_ctx) { | ||
345 | struct ww_mutex *ww; | ||
346 | ww = container_of(lock, struct ww_mutex, base); | ||
347 | |||
348 | ww_mutex_set_context_fastpath(ww, ww_ctx); | ||
349 | } | ||
350 | |||
351 | mutex_set_owner(lock); | ||
352 | osq_unlock(&lock->osq); | ||
353 | return true; | ||
354 | } | ||
355 | |||
356 | /* | ||
357 | * When there's no owner, we might have preempted between the | ||
358 | * owner acquiring the lock and setting the owner field. If | ||
359 | * we're an RT task that will live-lock because we won't let | ||
360 | * the owner complete. | ||
361 | */ | ||
362 | if (!owner && (need_resched() || rt_task(task))) | ||
363 | break; | ||
364 | |||
365 | /* | ||
366 | * The cpu_relax() call is a compiler barrier which forces | ||
367 | * everything in this loop to be re-loaded. We don't need | ||
368 | * memory barriers as we'll eventually observe the right | ||
369 | * values at the cost of a few extra spins. | ||
370 | */ | ||
371 | cpu_relax_lowlatency(); | ||
372 | } | ||
373 | |||
374 | osq_unlock(&lock->osq); | ||
375 | done: | ||
376 | /* | ||
377 | * If we fell out of the spin path because of need_resched(), | ||
378 | * reschedule now, before we try-lock the mutex. This avoids getting | ||
379 | * scheduled out right after we obtained the mutex. | ||
380 | */ | ||
381 | if (need_resched()) | ||
382 | schedule_preempt_disabled(); | ||
383 | |||
384 | return false; | ||
385 | } | ||
386 | #else | ||
387 | static bool mutex_optimistic_spin(struct mutex *lock, | ||
388 | struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) | ||
389 | { | ||
390 | return false; | ||
391 | } | ||
183 | #endif | 392 | #endif |
184 | 393 | ||
185 | __visible __used noinline | 394 | __visible __used noinline |
@@ -277,91 +486,6 @@ __mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx) | |||
277 | return 0; | 486 | return 0; |
278 | } | 487 | } |
279 | 488 | ||
280 | static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww, | ||
281 | struct ww_acquire_ctx *ww_ctx) | ||
282 | { | ||
283 | #ifdef CONFIG_DEBUG_MUTEXES | ||
284 | /* | ||
285 | * If this WARN_ON triggers, you used ww_mutex_lock to acquire, | ||
286 | * but released with a normal mutex_unlock in this call. | ||
287 | * | ||
288 | * This should never happen, always use ww_mutex_unlock. | ||
289 | */ | ||
290 | DEBUG_LOCKS_WARN_ON(ww->ctx); | ||
291 | |||
292 | /* | ||
293 | * Not quite done after calling ww_acquire_done() ? | ||
294 | */ | ||
295 | DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire); | ||
296 | |||
297 | if (ww_ctx->contending_lock) { | ||
298 | /* | ||
299 | * After -EDEADLK you tried to | ||
300 | * acquire a different ww_mutex? Bad! | ||
301 | */ | ||
302 | DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww); | ||
303 | |||
304 | /* | ||
305 | * You called ww_mutex_lock after receiving -EDEADLK, | ||
306 | * but 'forgot' to unlock everything else first? | ||
307 | */ | ||
308 | DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0); | ||
309 | ww_ctx->contending_lock = NULL; | ||
310 | } | ||
311 | |||
312 | /* | ||
313 | * Naughty, using a different class will lead to undefined behavior! | ||
314 | */ | ||
315 | DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class); | ||
316 | #endif | ||
317 | ww_ctx->acquired++; | ||
318 | } | ||
319 | |||
320 | /* | ||
321 | * after acquiring lock with fastpath or when we lost out in contested | ||
322 | * slowpath, set ctx and wake up any waiters so they can recheck. | ||
323 | * | ||
324 | * This function is never called when CONFIG_DEBUG_LOCK_ALLOC is set, | ||
325 | * as the fastpath and opportunistic spinning are disabled in that case. | ||
326 | */ | ||
327 | static __always_inline void | ||
328 | ww_mutex_set_context_fastpath(struct ww_mutex *lock, | ||
329 | struct ww_acquire_ctx *ctx) | ||
330 | { | ||
331 | unsigned long flags; | ||
332 | struct mutex_waiter *cur; | ||
333 | |||
334 | ww_mutex_lock_acquired(lock, ctx); | ||
335 | |||
336 | lock->ctx = ctx; | ||
337 | |||
338 | /* | ||
339 | * The lock->ctx update should be visible on all cores before | ||
340 | * the atomic read is done, otherwise contended waiters might be | ||
341 | * missed. The contended waiters will either see ww_ctx == NULL | ||
342 | * and keep spinning, or it will acquire wait_lock, add itself | ||
343 | * to waiter list and sleep. | ||
344 | */ | ||
345 | smp_mb(); /* ^^^ */ | ||
346 | |||
347 | /* | ||
348 | * Check if lock is contended, if not there is nobody to wake up | ||
349 | */ | ||
350 | if (likely(atomic_read(&lock->base.count) == 0)) | ||
351 | return; | ||
352 | |||
353 | /* | ||
354 | * Uh oh, we raced in fastpath, wake up everyone in this case, | ||
355 | * so they can see the new lock->ctx. | ||
356 | */ | ||
357 | spin_lock_mutex(&lock->base.wait_lock, flags); | ||
358 | list_for_each_entry(cur, &lock->base.wait_list, list) { | ||
359 | debug_mutex_wake_waiter(&lock->base, cur); | ||
360 | wake_up_process(cur->task); | ||
361 | } | ||
362 | spin_unlock_mutex(&lock->base.wait_lock, flags); | ||
363 | } | ||
364 | |||
365 | /* | 489 | /* |
366 | * Lock a mutex (possibly interruptible), slowpath: | 490 | * Lock a mutex (possibly interruptible), slowpath: |
367 | */ | 491 | */ |
@@ -378,104 +502,12 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, | |||
378 | preempt_disable(); | 502 | preempt_disable(); |
379 | mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); | 503 | mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); |
380 | 504 | ||
381 | #ifdef CONFIG_MUTEX_SPIN_ON_OWNER | 505 | if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) { |
382 | /* | 506 | /* got the lock, yay! */ |
383 | * Optimistic spinning. | 507 | preempt_enable(); |
384 | * | 508 | return 0; |
385 | * We try to spin for acquisition when we find that the lock owner | ||
386 | * is currently running on a (different) CPU and while we don't | ||
387 | * need to reschedule. The rationale is that if the lock owner is | ||
388 | * running, it is likely to release the lock soon. | ||
389 | * | ||
390 | * Since this needs the lock owner, and this mutex implementation | ||
391 | * doesn't track the owner atomically in the lock field, we need to | ||
392 | * track it non-atomically. | ||
393 | * | ||
394 | * We can't do this for DEBUG_MUTEXES because that relies on wait_lock | ||
395 | * to serialize everything. | ||
396 | * | ||
397 | * The mutex spinners are queued up using MCS lock so that only one | ||
398 | * spinner can compete for the mutex. However, if mutex spinning isn't | ||
399 | * going to happen, there is no point in going through the lock/unlock | ||
400 | * overhead. | ||
401 | */ | ||
402 | if (!mutex_can_spin_on_owner(lock)) | ||
403 | goto slowpath; | ||
404 | |||
405 | if (!osq_lock(&lock->osq)) | ||
406 | goto slowpath; | ||
407 | |||
408 | for (;;) { | ||
409 | struct task_struct *owner; | ||
410 | |||
411 | if (use_ww_ctx && ww_ctx->acquired > 0) { | ||
412 | struct ww_mutex *ww; | ||
413 | |||
414 | ww = container_of(lock, struct ww_mutex, base); | ||
415 | /* | ||
416 | * If ww->ctx is set the contents are undefined, only | ||
417 | * by acquiring wait_lock there is a guarantee that | ||
418 | * they are not invalid when reading. | ||
419 | * | ||
420 | * As such, when deadlock detection needs to be | ||
421 | * performed the optimistic spinning cannot be done. | ||
422 | */ | ||
423 | if (ACCESS_ONCE(ww->ctx)) | ||
424 | break; | ||
425 | } | ||
426 | |||
427 | /* | ||
428 | * If there's an owner, wait for it to either | ||
429 | * release the lock or go to sleep. | ||
430 | */ | ||
431 | owner = ACCESS_ONCE(lock->owner); | ||
432 | if (owner && !mutex_spin_on_owner(lock, owner)) | ||
433 | break; | ||
434 | |||
435 | /* Try to acquire the mutex if it is unlocked. */ | ||
436 | if (!mutex_is_locked(lock) && | ||
437 | (atomic_cmpxchg(&lock->count, 1, 0) == 1)) { | ||
438 | lock_acquired(&lock->dep_map, ip); | ||
439 | if (use_ww_ctx) { | ||
440 | struct ww_mutex *ww; | ||
441 | ww = container_of(lock, struct ww_mutex, base); | ||
442 | |||
443 | ww_mutex_set_context_fastpath(ww, ww_ctx); | ||
444 | } | ||
445 | |||
446 | mutex_set_owner(lock); | ||
447 | osq_unlock(&lock->osq); | ||
448 | preempt_enable(); | ||
449 | return 0; | ||
450 | } | ||
451 | |||
452 | /* | ||
453 | * When there's no owner, we might have preempted between the | ||
454 | * owner acquiring the lock and setting the owner field. If | ||
455 | * we're an RT task that will live-lock because we won't let | ||
456 | * the owner complete. | ||
457 | */ | ||
458 | if (!owner && (need_resched() || rt_task(task))) | ||
459 | break; | ||
460 | |||
461 | /* | ||
462 | * The cpu_relax() call is a compiler barrier which forces | ||
463 | * everything in this loop to be re-loaded. We don't need | ||
464 | * memory barriers as we'll eventually observe the right | ||
465 | * values at the cost of a few extra spins. | ||
466 | */ | ||
467 | cpu_relax_lowlatency(); | ||
468 | } | 509 | } |
469 | osq_unlock(&lock->osq); | 510 | |
470 | slowpath: | ||
471 | /* | ||
472 | * If we fell out of the spin path because of need_resched(), | ||
473 | * reschedule now, before we try-lock the mutex. This avoids getting | ||
474 | * scheduled out right after we obtained the mutex. | ||
475 | */ | ||
476 | if (need_resched()) | ||
477 | schedule_preempt_disabled(); | ||
478 | #endif | ||
479 | spin_lock_mutex(&lock->wait_lock, flags); | 511 | spin_lock_mutex(&lock->wait_lock, flags); |
480 | 512 | ||
481 | /* | 513 | /* |
@@ -679,15 +711,21 @@ EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible); | |||
679 | * Release the lock, slowpath: | 711 | * Release the lock, slowpath: |
680 | */ | 712 | */ |
681 | static inline void | 713 | static inline void |
682 | __mutex_unlock_common_slowpath(atomic_t *lock_count, int nested) | 714 | __mutex_unlock_common_slowpath(struct mutex *lock, int nested) |
683 | { | 715 | { |
684 | struct mutex *lock = container_of(lock_count, struct mutex, count); | ||
685 | unsigned long flags; | 716 | unsigned long flags; |
686 | 717 | ||
687 | /* | 718 | /* |
688 | * some architectures leave the lock unlocked in the fastpath failure | 719 | * As a performance measurement, release the lock before doing other |
720 | * wakeup related duties to follow. This allows other tasks to acquire | ||
721 | * the lock sooner, while still handling cleanups in past unlock calls. | ||
722 | * This can be done as we do not enforce strict equivalence between the | ||
723 | * mutex counter and wait_list. | ||
724 | * | ||
725 | * | ||
726 | * Some architectures leave the lock unlocked in the fastpath failure | ||
689 | * case, others need to leave it locked. In the later case we have to | 727 | * case, others need to leave it locked. In the later case we have to |
690 | * unlock it here | 728 | * unlock it here - as the lock counter is currently 0 or negative. |
691 | */ | 729 | */ |
692 | if (__mutex_slowpath_needs_to_unlock()) | 730 | if (__mutex_slowpath_needs_to_unlock()) |
693 | atomic_set(&lock->count, 1); | 731 | atomic_set(&lock->count, 1); |
@@ -716,7 +754,9 @@ __mutex_unlock_common_slowpath(atomic_t *lock_count, int nested) | |||
716 | __visible void | 754 | __visible void |
717 | __mutex_unlock_slowpath(atomic_t *lock_count) | 755 | __mutex_unlock_slowpath(atomic_t *lock_count) |
718 | { | 756 | { |
719 | __mutex_unlock_common_slowpath(lock_count, 1); | 757 | struct mutex *lock = container_of(lock_count, struct mutex, count); |
758 | |||
759 | __mutex_unlock_common_slowpath(lock, 1); | ||
720 | } | 760 | } |
721 | 761 | ||
722 | #ifndef CONFIG_DEBUG_LOCK_ALLOC | 762 | #ifndef CONFIG_DEBUG_LOCK_ALLOC |
diff --git a/kernel/locking/mutex.h b/kernel/locking/mutex.h index 4115fbf83b12..5cda397607f2 100644 --- a/kernel/locking/mutex.h +++ b/kernel/locking/mutex.h | |||
@@ -16,7 +16,7 @@ | |||
16 | #define mutex_remove_waiter(lock, waiter, ti) \ | 16 | #define mutex_remove_waiter(lock, waiter, ti) \ |
17 | __list_del((waiter)->list.prev, (waiter)->list.next) | 17 | __list_del((waiter)->list.prev, (waiter)->list.next) |
18 | 18 | ||
19 | #ifdef CONFIG_SMP | 19 | #ifdef CONFIG_MUTEX_SPIN_ON_OWNER |
20 | static inline void mutex_set_owner(struct mutex *lock) | 20 | static inline void mutex_set_owner(struct mutex *lock) |
21 | { | 21 | { |
22 | lock->owner = current; | 22 | lock->owner = current; |
diff --git a/kernel/locking/rtmutex.c b/kernel/locking/rtmutex.c index a0ea2a141b3b..7c98873a3077 100644 --- a/kernel/locking/rtmutex.c +++ b/kernel/locking/rtmutex.c | |||
@@ -8,7 +8,7 @@ | |||
8 | * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt | 8 | * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt |
9 | * Copyright (C) 2006 Esben Nielsen | 9 | * Copyright (C) 2006 Esben Nielsen |
10 | * | 10 | * |
11 | * See Documentation/rt-mutex-design.txt for details. | 11 | * See Documentation/locking/rt-mutex-design.txt for details. |
12 | */ | 12 | */ |
13 | #include <linux/spinlock.h> | 13 | #include <linux/spinlock.h> |
14 | #include <linux/export.h> | 14 | #include <linux/export.h> |
diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c index d6203faf2eb1..7628c3fc37ca 100644 --- a/kernel/locking/rwsem-xadd.c +++ b/kernel/locking/rwsem-xadd.c | |||
@@ -246,19 +246,22 @@ struct rw_semaphore __sched *rwsem_down_read_failed(struct rw_semaphore *sem) | |||
246 | 246 | ||
247 | return sem; | 247 | return sem; |
248 | } | 248 | } |
249 | EXPORT_SYMBOL(rwsem_down_read_failed); | ||
249 | 250 | ||
250 | static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem) | 251 | static inline bool rwsem_try_write_lock(long count, struct rw_semaphore *sem) |
251 | { | 252 | { |
252 | if (!(count & RWSEM_ACTIVE_MASK)) { | 253 | /* |
253 | /* try acquiring the write lock */ | 254 | * Try acquiring the write lock. Check count first in order |
254 | if (sem->count == RWSEM_WAITING_BIAS && | 255 | * to reduce unnecessary expensive cmpxchg() operations. |
255 | cmpxchg(&sem->count, RWSEM_WAITING_BIAS, | 256 | */ |
256 | RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) { | 257 | if (count == RWSEM_WAITING_BIAS && |
257 | if (!list_is_singular(&sem->wait_list)) | 258 | cmpxchg(&sem->count, RWSEM_WAITING_BIAS, |
258 | rwsem_atomic_update(RWSEM_WAITING_BIAS, sem); | 259 | RWSEM_ACTIVE_WRITE_BIAS) == RWSEM_WAITING_BIAS) { |
259 | return true; | 260 | if (!list_is_singular(&sem->wait_list)) |
260 | } | 261 | rwsem_atomic_update(RWSEM_WAITING_BIAS, sem); |
262 | return true; | ||
261 | } | 263 | } |
264 | |||
262 | return false; | 265 | return false; |
263 | } | 266 | } |
264 | 267 | ||
@@ -465,6 +468,7 @@ struct rw_semaphore __sched *rwsem_down_write_failed(struct rw_semaphore *sem) | |||
465 | 468 | ||
466 | return sem; | 469 | return sem; |
467 | } | 470 | } |
471 | EXPORT_SYMBOL(rwsem_down_write_failed); | ||
468 | 472 | ||
469 | /* | 473 | /* |
470 | * handle waking up a waiter on the semaphore | 474 | * handle waking up a waiter on the semaphore |
@@ -485,6 +489,7 @@ struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) | |||
485 | 489 | ||
486 | return sem; | 490 | return sem; |
487 | } | 491 | } |
492 | EXPORT_SYMBOL(rwsem_wake); | ||
488 | 493 | ||
489 | /* | 494 | /* |
490 | * downgrade a write lock into a read lock | 495 | * downgrade a write lock into a read lock |
@@ -506,8 +511,4 @@ struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) | |||
506 | 511 | ||
507 | return sem; | 512 | return sem; |
508 | } | 513 | } |
509 | |||
510 | EXPORT_SYMBOL(rwsem_down_read_failed); | ||
511 | EXPORT_SYMBOL(rwsem_down_write_failed); | ||
512 | EXPORT_SYMBOL(rwsem_wake); | ||
513 | EXPORT_SYMBOL(rwsem_downgrade_wake); | 514 | EXPORT_SYMBOL(rwsem_downgrade_wake); |
diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index 6815171a4fff..b8120abe594b 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c | |||
@@ -36,7 +36,7 @@ | |||
36 | static noinline void __down(struct semaphore *sem); | 36 | static noinline void __down(struct semaphore *sem); |
37 | static noinline int __down_interruptible(struct semaphore *sem); | 37 | static noinline int __down_interruptible(struct semaphore *sem); |
38 | static noinline int __down_killable(struct semaphore *sem); | 38 | static noinline int __down_killable(struct semaphore *sem); |
39 | static noinline int __down_timeout(struct semaphore *sem, long jiffies); | 39 | static noinline int __down_timeout(struct semaphore *sem, long timeout); |
40 | static noinline void __up(struct semaphore *sem); | 40 | static noinline void __up(struct semaphore *sem); |
41 | 41 | ||
42 | /** | 42 | /** |
@@ -145,14 +145,14 @@ EXPORT_SYMBOL(down_trylock); | |||
145 | /** | 145 | /** |
146 | * down_timeout - acquire the semaphore within a specified time | 146 | * down_timeout - acquire the semaphore within a specified time |
147 | * @sem: the semaphore to be acquired | 147 | * @sem: the semaphore to be acquired |
148 | * @jiffies: how long to wait before failing | 148 | * @timeout: how long to wait before failing |
149 | * | 149 | * |
150 | * Attempts to acquire the semaphore. If no more tasks are allowed to | 150 | * Attempts to acquire the semaphore. If no more tasks are allowed to |
151 | * acquire the semaphore, calling this function will put the task to sleep. | 151 | * acquire the semaphore, calling this function will put the task to sleep. |
152 | * If the semaphore is not released within the specified number of jiffies, | 152 | * If the semaphore is not released within the specified number of jiffies, |
153 | * this function returns -ETIME. It returns 0 if the semaphore was acquired. | 153 | * this function returns -ETIME. It returns 0 if the semaphore was acquired. |
154 | */ | 154 | */ |
155 | int down_timeout(struct semaphore *sem, long jiffies) | 155 | int down_timeout(struct semaphore *sem, long timeout) |
156 | { | 156 | { |
157 | unsigned long flags; | 157 | unsigned long flags; |
158 | int result = 0; | 158 | int result = 0; |
@@ -161,7 +161,7 @@ int down_timeout(struct semaphore *sem, long jiffies) | |||
161 | if (likely(sem->count > 0)) | 161 | if (likely(sem->count > 0)) |
162 | sem->count--; | 162 | sem->count--; |
163 | else | 163 | else |
164 | result = __down_timeout(sem, jiffies); | 164 | result = __down_timeout(sem, timeout); |
165 | raw_spin_unlock_irqrestore(&sem->lock, flags); | 165 | raw_spin_unlock_irqrestore(&sem->lock, flags); |
166 | 166 | ||
167 | return result; | 167 | return result; |
@@ -248,9 +248,9 @@ static noinline int __sched __down_killable(struct semaphore *sem) | |||
248 | return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT); | 248 | return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT); |
249 | } | 249 | } |
250 | 250 | ||
251 | static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies) | 251 | static noinline int __sched __down_timeout(struct semaphore *sem, long timeout) |
252 | { | 252 | { |
253 | return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies); | 253 | return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout); |
254 | } | 254 | } |
255 | 255 | ||
256 | static noinline void __sched __up(struct semaphore *sem) | 256 | static noinline void __sched __up(struct semaphore *sem) |