diff options
author | Davidlohr Bueso <davidlohr@hp.com> | 2014-07-30 16:41:53 -0400 |
---|---|---|
committer | Ingo Molnar <mingo@kernel.org> | 2014-08-13 04:32:01 -0400 |
commit | 76916515d9d84e6552ee5e218e0ed566ad75e600 (patch) | |
tree | ca00b7122cfa29a458ec0bc27aae2c67b1b37013 /kernel/locking | |
parent | aa9fc0c19bee0cbc152e0e06488095fb69229236 (diff) |
locking/mutexes: Refactor optimistic spinning code
When we fail to acquire the mutex in the fastpath, we end up calling
__mutex_lock_common(). A *lot* goes on in this function. Move out the
optimistic spinning code into mutex_optimistic_spin() and simplify
the former a bit. Furthermore, this is similar to what we have in
rwsems. No logical changes.
Signed-off-by: Davidlohr Bueso <davidlohr@hp.com>
Acked-by: Jason Low <jason.low2@hp.com>
Signed-off-by: Peter Zijlstra <peterz@infradead.org>
Cc: aswin@hp.com
Cc: mingo@kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Link: http://lkml.kernel.org/r/1406752916-3341-4-git-send-email-davidlohr@hp.com
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Diffstat (limited to 'kernel/locking')
-rw-r--r-- | kernel/locking/mutex.c | 396 |
1 files changed, 214 insertions, 182 deletions
diff --git a/kernel/locking/mutex.c b/kernel/locking/mutex.c index 93bec48f09ed..0d8b6ed93874 100644 --- a/kernel/locking/mutex.c +++ b/kernel/locking/mutex.c | |||
@@ -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 | /* |