1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * kernel/locking/mutex.c 4 * 5 * Mutexes: blocking mutual exclusion locks 6 * 7 * Started by Ingo Molnar: 8 * 9 * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]> 10 * 11 * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and 12 * David Howells for suggestions and improvements. 13 * 14 * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline 15 * from the -rt tree, where it was originally implemented for rtmutexes 16 * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale 17 * and Sven Dietrich. 18 * 19 * Also see Documentation/locking/mutex-design.rst. 20 */ 21 #include <linux/mutex.h> 22 #include <linux/ww_mutex.h> 23 #include <linux/sched/signal.h> 24 #include <linux/sched/rt.h> 25 #include <linux/sched/wake_q.h> 26 #include <linux/sched/debug.h> 27 #include <linux/export.h> 28 #include <linux/spinlock.h> 29 #include <linux/interrupt.h> 30 #include <linux/debug_locks.h> 31 #include <linux/osq_lock.h> 32 33 #define CREATE_TRACE_POINTS 34 #include <trace/events/lock.h> 35 36 #ifndef CONFIG_PREEMPT_RT 37 #include "mutex.h" 38 39 #ifdef CONFIG_DEBUG_MUTEXES 40 # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond) 41 #else 42 # define MUTEX_WARN_ON(cond) 43 #endif 44 45 void 46 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 47 { 48 atomic_long_set(&lock->owner, 0); 49 raw_spin_lock_init(&lock->wait_lock); 50 INIT_LIST_HEAD(&lock->wait_list); 51 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 52 osq_lock_init(&lock->osq); 53 #endif 54 55 debug_mutex_init(lock, name, key); 56 } 57 EXPORT_SYMBOL(__mutex_init); 58 59 static inline struct task_struct *__owner_task(unsigned long owner) 60 { 61 return (struct task_struct *)(owner & ~MUTEX_FLAGS); 62 } 63 64 bool mutex_is_locked(struct mutex *lock) 65 { 66 return __mutex_owner(lock) != NULL; 67 } 68 EXPORT_SYMBOL(mutex_is_locked); 69 70 static inline unsigned long __owner_flags(unsigned long owner) 71 { 72 return owner & MUTEX_FLAGS; 73 } 74 75 /* Do not use the return value as a pointer directly. */ 76 unsigned long mutex_get_owner(struct mutex *lock) 77 { 78 unsigned long owner = atomic_long_read(&lock->owner); 79 80 return (unsigned long)__owner_task(owner); 81 } 82 83 /* 84 * Returns: __mutex_owner(lock) on failure or NULL on success. 85 */ 86 static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff) 87 { 88 unsigned long owner, curr = (unsigned long)current; 89 90 owner = atomic_long_read(&lock->owner); 91 for (;;) { /* must loop, can race against a flag */ 92 unsigned long flags = __owner_flags(owner); 93 unsigned long task = owner & ~MUTEX_FLAGS; 94 95 if (task) { 96 if (flags & MUTEX_FLAG_PICKUP) { 97 if (task != curr) 98 break; 99 flags &= ~MUTEX_FLAG_PICKUP; 100 } else if (handoff) { 101 if (flags & MUTEX_FLAG_HANDOFF) 102 break; 103 flags |= MUTEX_FLAG_HANDOFF; 104 } else { 105 break; 106 } 107 } else { 108 MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP)); 109 task = curr; 110 } 111 112 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) { 113 if (task == curr) 114 return NULL; 115 break; 116 } 117 } 118 119 return __owner_task(owner); 120 } 121 122 /* 123 * Trylock or set HANDOFF 124 */ 125 static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff) 126 { 127 return !__mutex_trylock_common(lock, handoff); 128 } 129 130 /* 131 * Actual trylock that will work on any unlocked state. 132 */ 133 static inline bool __mutex_trylock(struct mutex *lock) 134 { 135 return !__mutex_trylock_common(lock, false); 136 } 137 138 #ifndef CONFIG_DEBUG_LOCK_ALLOC 139 /* 140 * Lockdep annotations are contained to the slow paths for simplicity. 141 * There is nothing that would stop spreading the lockdep annotations outwards 142 * except more code. 143 */ 144 145 /* 146 * Optimistic trylock that only works in the uncontended case. Make sure to 147 * follow with a __mutex_trylock() before failing. 148 */ 149 static __always_inline bool __mutex_trylock_fast(struct mutex *lock) 150 { 151 unsigned long curr = (unsigned long)current; 152 unsigned long zero = 0UL; 153 154 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr)) 155 return true; 156 157 return false; 158 } 159 160 static __always_inline bool __mutex_unlock_fast(struct mutex *lock) 161 { 162 unsigned long curr = (unsigned long)current; 163 164 return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); 165 } 166 #endif 167 168 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 169 { 170 atomic_long_or(flag, &lock->owner); 171 } 172 173 static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) 174 { 175 atomic_long_andnot(flag, &lock->owner); 176 } 177 178 static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) 179 { 180 return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; 181 } 182 183 /* 184 * Add @waiter to a given location in the lock wait_list and set the 185 * FLAG_WAITERS flag if it's the first waiter. 186 */ 187 static void 188 __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, 189 struct list_head *list) 190 { 191 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER 192 WRITE_ONCE(current->blocker_mutex, lock); 193 #endif 194 debug_mutex_add_waiter(lock, waiter, current); 195 196 list_add_tail(&waiter->list, list); 197 if (__mutex_waiter_is_first(lock, waiter)) 198 __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); 199 } 200 201 static void 202 __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter) 203 { 204 list_del(&waiter->list); 205 if (likely(list_empty(&lock->wait_list))) 206 __mutex_clear_flag(lock, MUTEX_FLAGS); 207 208 debug_mutex_remove_waiter(lock, waiter, current); 209 #ifdef CONFIG_DETECT_HUNG_TASK_BLOCKER 210 WRITE_ONCE(current->blocker_mutex, NULL); 211 #endif 212 } 213 214 /* 215 * Give up ownership to a specific task, when @task = NULL, this is equivalent 216 * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves 217 * WAITERS. Provides RELEASE semantics like a regular unlock, the 218 * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff. 219 */ 220 static void __mutex_handoff(struct mutex *lock, struct task_struct *task) 221 { 222 unsigned long owner = atomic_long_read(&lock->owner); 223 224 for (;;) { 225 unsigned long new; 226 227 MUTEX_WARN_ON(__owner_task(owner) != current); 228 MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 229 230 new = (owner & MUTEX_FLAG_WAITERS); 231 new |= (unsigned long)task; 232 if (task) 233 new |= MUTEX_FLAG_PICKUP; 234 235 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new)) 236 break; 237 } 238 } 239 240 #ifndef CONFIG_DEBUG_LOCK_ALLOC 241 /* 242 * We split the mutex lock/unlock logic into separate fastpath and 243 * slowpath functions, to reduce the register pressure on the fastpath. 244 * We also put the fastpath first in the kernel image, to make sure the 245 * branch is predicted by the CPU as default-untaken. 246 */ 247 static void __sched __mutex_lock_slowpath(struct mutex *lock); 248 249 /** 250 * mutex_lock - acquire the mutex 251 * @lock: the mutex to be acquired 252 * 253 * Lock the mutex exclusively for this task. If the mutex is not 254 * available right now, it will sleep until it can get it. 255 * 256 * The mutex must later on be released by the same task that 257 * acquired it. Recursive locking is not allowed. The task 258 * may not exit without first unlocking the mutex. Also, kernel 259 * memory where the mutex resides must not be freed with 260 * the mutex still locked. The mutex must first be initialized 261 * (or statically defined) before it can be locked. memset()-ing 262 * the mutex to 0 is not allowed. 263 * 264 * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging 265 * checks that will enforce the restrictions and will also do 266 * deadlock debugging) 267 * 268 * This function is similar to (but not equivalent to) down(). 269 */ 270 void __sched mutex_lock(struct mutex *lock) 271 { 272 might_sleep(); 273 274 if (!__mutex_trylock_fast(lock)) 275 __mutex_lock_slowpath(lock); 276 } 277 EXPORT_SYMBOL(mutex_lock); 278 #endif 279 280 #include "ww_mutex.h" 281 282 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 283 284 /* 285 * Trylock variant that returns the owning task on failure. 286 */ 287 static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock) 288 { 289 return __mutex_trylock_common(lock, false); 290 } 291 292 static inline 293 bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 294 struct mutex_waiter *waiter) 295 { 296 struct ww_mutex *ww; 297 298 ww = container_of(lock, struct ww_mutex, base); 299 300 /* 301 * If ww->ctx is set the contents are undefined, only 302 * by acquiring wait_lock there is a guarantee that 303 * they are not invalid when reading. 304 * 305 * As such, when deadlock detection needs to be 306 * performed the optimistic spinning cannot be done. 307 * 308 * Check this in every inner iteration because we may 309 * be racing against another thread's ww_mutex_lock. 310 */ 311 if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx)) 312 return false; 313 314 /* 315 * If we aren't on the wait list yet, cancel the spin 316 * if there are waiters. We want to avoid stealing the 317 * lock from a waiter with an earlier stamp, since the 318 * other thread may already own a lock that we also 319 * need. 320 */ 321 if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS)) 322 return false; 323 324 /* 325 * Similarly, stop spinning if we are no longer the 326 * first waiter. 327 */ 328 if (waiter && !__mutex_waiter_is_first(lock, waiter)) 329 return false; 330 331 return true; 332 } 333 334 /* 335 * Look out! "owner" is an entirely speculative pointer access and not 336 * reliable. 337 * 338 * "noinline" so that this function shows up on perf profiles. 339 */ 340 static noinline 341 bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, 342 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter) 343 { 344 bool ret = true; 345 346 lockdep_assert_preemption_disabled(); 347 348 while (__mutex_owner(lock) == owner) { 349 /* 350 * Ensure we emit the owner->on_cpu, dereference _after_ 351 * checking lock->owner still matches owner. And we already 352 * disabled preemption which is equal to the RCU read-side 353 * crital section in optimistic spinning code. Thus the 354 * task_strcut structure won't go away during the spinning 355 * period 356 */ 357 barrier(); 358 359 /* 360 * Use vcpu_is_preempted to detect lock holder preemption issue. 361 */ 362 if (!owner_on_cpu(owner) || need_resched()) { 363 ret = false; 364 break; 365 } 366 367 if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) { 368 ret = false; 369 break; 370 } 371 372 cpu_relax(); 373 } 374 375 return ret; 376 } 377 378 /* 379 * Initial check for entering the mutex spinning loop 380 */ 381 static inline int mutex_can_spin_on_owner(struct mutex *lock) 382 { 383 struct task_struct *owner; 384 int retval = 1; 385 386 lockdep_assert_preemption_disabled(); 387 388 if (need_resched()) 389 return 0; 390 391 /* 392 * We already disabled preemption which is equal to the RCU read-side 393 * crital section in optimistic spinning code. Thus the task_strcut 394 * structure won't go away during the spinning period. 395 */ 396 owner = __mutex_owner(lock); 397 if (owner) 398 retval = owner_on_cpu(owner); 399 400 /* 401 * If lock->owner is not set, the mutex has been released. Return true 402 * such that we'll trylock in the spin path, which is a faster option 403 * than the blocking slow path. 404 */ 405 return retval; 406 } 407 408 /* 409 * Optimistic spinning. 410 * 411 * We try to spin for acquisition when we find that the lock owner 412 * is currently running on a (different) CPU and while we don't 413 * need to reschedule. The rationale is that if the lock owner is 414 * running, it is likely to release the lock soon. 415 * 416 * The mutex spinners are queued up using MCS lock so that only one 417 * spinner can compete for the mutex. However, if mutex spinning isn't 418 * going to happen, there is no point in going through the lock/unlock 419 * overhead. 420 * 421 * Returns true when the lock was taken, otherwise false, indicating 422 * that we need to jump to the slowpath and sleep. 423 * 424 * The waiter flag is set to true if the spinner is a waiter in the wait 425 * queue. The waiter-spinner will spin on the lock directly and concurrently 426 * with the spinner at the head of the OSQ, if present, until the owner is 427 * changed to itself. 428 */ 429 static __always_inline bool 430 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 431 struct mutex_waiter *waiter) 432 { 433 if (!waiter) { 434 /* 435 * The purpose of the mutex_can_spin_on_owner() function is 436 * to eliminate the overhead of osq_lock() and osq_unlock() 437 * in case spinning isn't possible. As a waiter-spinner 438 * is not going to take OSQ lock anyway, there is no need 439 * to call mutex_can_spin_on_owner(). 440 */ 441 if (!mutex_can_spin_on_owner(lock)) 442 goto fail; 443 444 /* 445 * In order to avoid a stampede of mutex spinners trying to 446 * acquire the mutex all at once, the spinners need to take a 447 * MCS (queued) lock first before spinning on the owner field. 448 */ 449 if (!osq_lock(&lock->osq)) 450 goto fail; 451 } 452 453 for (;;) { 454 struct task_struct *owner; 455 456 /* Try to acquire the mutex... */ 457 owner = __mutex_trylock_or_owner(lock); 458 if (!owner) 459 break; 460 461 /* 462 * There's an owner, wait for it to either 463 * release the lock or go to sleep. 464 */ 465 if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter)) 466 goto fail_unlock; 467 468 /* 469 * The cpu_relax() call is a compiler barrier which forces 470 * everything in this loop to be re-loaded. We don't need 471 * memory barriers as we'll eventually observe the right 472 * values at the cost of a few extra spins. 473 */ 474 cpu_relax(); 475 } 476 477 if (!waiter) 478 osq_unlock(&lock->osq); 479 480 return true; 481 482 483 fail_unlock: 484 if (!waiter) 485 osq_unlock(&lock->osq); 486 487 fail: 488 /* 489 * If we fell out of the spin path because of need_resched(), 490 * reschedule now, before we try-lock the mutex. This avoids getting 491 * scheduled out right after we obtained the mutex. 492 */ 493 if (need_resched()) { 494 /* 495 * We _should_ have TASK_RUNNING here, but just in case 496 * we do not, make it so, otherwise we might get stuck. 497 */ 498 __set_current_state(TASK_RUNNING); 499 schedule_preempt_disabled(); 500 } 501 502 return false; 503 } 504 #else 505 static __always_inline bool 506 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 507 struct mutex_waiter *waiter) 508 { 509 return false; 510 } 511 #endif 512 513 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip); 514 515 /** 516 * mutex_unlock - release the mutex 517 * @lock: the mutex to be released 518 * 519 * Unlock a mutex that has been locked by this task previously. 520 * 521 * This function must not be used in interrupt context. Unlocking 522 * of a not locked mutex is not allowed. 523 * 524 * The caller must ensure that the mutex stays alive until this function has 525 * returned - mutex_unlock() can NOT directly be used to release an object such 526 * that another concurrent task can free it. 527 * Mutexes are different from spinlocks & refcounts in this aspect. 528 * 529 * This function is similar to (but not equivalent to) up(). 530 */ 531 void __sched mutex_unlock(struct mutex *lock) 532 { 533 #ifndef CONFIG_DEBUG_LOCK_ALLOC 534 if (__mutex_unlock_fast(lock)) 535 return; 536 #endif 537 __mutex_unlock_slowpath(lock, _RET_IP_); 538 } 539 EXPORT_SYMBOL(mutex_unlock); 540 541 /** 542 * ww_mutex_unlock - release the w/w mutex 543 * @lock: the mutex to be released 544 * 545 * Unlock a mutex that has been locked by this task previously with any of the 546 * ww_mutex_lock* functions (with or without an acquire context). It is 547 * forbidden to release the locks after releasing the acquire context. 548 * 549 * This function must not be used in interrupt context. Unlocking 550 * of a unlocked mutex is not allowed. 551 */ 552 void __sched ww_mutex_unlock(struct ww_mutex *lock) 553 { 554 __ww_mutex_unlock(lock); 555 mutex_unlock(&lock->base); 556 } 557 EXPORT_SYMBOL(ww_mutex_unlock); 558 559 /* 560 * Lock a mutex (possibly interruptible), slowpath: 561 */ 562 static __always_inline int __sched 563 __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass, 564 struct lockdep_map *nest_lock, unsigned long ip, 565 struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 566 { 567 DEFINE_WAKE_Q(wake_q); 568 struct mutex_waiter waiter; 569 struct ww_mutex *ww; 570 unsigned long flags; 571 int ret; 572 573 if (!use_ww_ctx) 574 ww_ctx = NULL; 575 576 might_sleep(); 577 578 MUTEX_WARN_ON(lock->magic != lock); 579 580 ww = container_of(lock, struct ww_mutex, base); 581 if (ww_ctx) { 582 if (unlikely(ww_ctx == READ_ONCE(ww->ctx))) 583 return -EALREADY; 584 585 /* 586 * Reset the wounded flag after a kill. No other process can 587 * race and wound us here since they can't have a valid owner 588 * pointer if we don't have any locks held. 589 */ 590 if (ww_ctx->acquired == 0) 591 ww_ctx->wounded = 0; 592 593 #ifdef CONFIG_DEBUG_LOCK_ALLOC 594 nest_lock = &ww_ctx->dep_map; 595 #endif 596 } 597 598 preempt_disable(); 599 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 600 601 trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); 602 if (__mutex_trylock(lock) || 603 mutex_optimistic_spin(lock, ww_ctx, NULL)) { 604 /* got the lock, yay! */ 605 lock_acquired(&lock->dep_map, ip); 606 if (ww_ctx) 607 ww_mutex_set_context_fastpath(ww, ww_ctx); 608 trace_contention_end(lock, 0); 609 preempt_enable(); 610 return 0; 611 } 612 613 raw_spin_lock_irqsave(&lock->wait_lock, flags); 614 /* 615 * After waiting to acquire the wait_lock, try again. 616 */ 617 if (__mutex_trylock(lock)) { 618 if (ww_ctx) 619 __ww_mutex_check_waiters(lock, ww_ctx, &wake_q); 620 621 goto skip_wait; 622 } 623 624 debug_mutex_lock_common(lock, &waiter); 625 waiter.task = current; 626 if (use_ww_ctx) 627 waiter.ww_ctx = ww_ctx; 628 629 lock_contended(&lock->dep_map, ip); 630 631 if (!use_ww_ctx) { 632 /* add waiting tasks to the end of the waitqueue (FIFO): */ 633 __mutex_add_waiter(lock, &waiter, &lock->wait_list); 634 } else { 635 /* 636 * Add in stamp order, waking up waiters that must kill 637 * themselves. 638 */ 639 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx, &wake_q); 640 if (ret) 641 goto err_early_kill; 642 } 643 644 set_current_state(state); 645 trace_contention_begin(lock, LCB_F_MUTEX); 646 for (;;) { 647 bool first; 648 649 /* 650 * Once we hold wait_lock, we're serialized against 651 * mutex_unlock() handing the lock off to us, do a trylock 652 * before testing the error conditions to make sure we pick up 653 * the handoff. 654 */ 655 if (__mutex_trylock(lock)) 656 goto acquired; 657 658 /* 659 * Check for signals and kill conditions while holding 660 * wait_lock. This ensures the lock cancellation is ordered 661 * against mutex_unlock() and wake-ups do not go missing. 662 */ 663 if (signal_pending_state(state, current)) { 664 ret = -EINTR; 665 goto err; 666 } 667 668 if (ww_ctx) { 669 ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx); 670 if (ret) 671 goto err; 672 } 673 674 raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); 675 676 schedule_preempt_disabled(); 677 678 first = __mutex_waiter_is_first(lock, &waiter); 679 680 set_current_state(state); 681 /* 682 * Here we order against unlock; we must either see it change 683 * state back to RUNNING and fall through the next schedule(), 684 * or we must see its unlock and acquire. 685 */ 686 if (__mutex_trylock_or_handoff(lock, first)) 687 break; 688 689 if (first) { 690 trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); 691 if (mutex_optimistic_spin(lock, ww_ctx, &waiter)) 692 break; 693 trace_contention_begin(lock, LCB_F_MUTEX); 694 } 695 696 raw_spin_lock_irqsave(&lock->wait_lock, flags); 697 } 698 raw_spin_lock_irqsave(&lock->wait_lock, flags); 699 acquired: 700 __set_current_state(TASK_RUNNING); 701 702 if (ww_ctx) { 703 /* 704 * Wound-Wait; we stole the lock (!first_waiter), check the 705 * waiters as anyone might want to wound us. 706 */ 707 if (!ww_ctx->is_wait_die && 708 !__mutex_waiter_is_first(lock, &waiter)) 709 __ww_mutex_check_waiters(lock, ww_ctx, &wake_q); 710 } 711 712 __mutex_remove_waiter(lock, &waiter); 713 714 debug_mutex_free_waiter(&waiter); 715 716 skip_wait: 717 /* got the lock - cleanup and rejoice! */ 718 lock_acquired(&lock->dep_map, ip); 719 trace_contention_end(lock, 0); 720 721 if (ww_ctx) 722 ww_mutex_lock_acquired(ww, ww_ctx); 723 724 raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); 725 preempt_enable(); 726 return 0; 727 728 err: 729 __set_current_state(TASK_RUNNING); 730 __mutex_remove_waiter(lock, &waiter); 731 err_early_kill: 732 trace_contention_end(lock, ret); 733 raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); 734 debug_mutex_free_waiter(&waiter); 735 mutex_release(&lock->dep_map, ip); 736 preempt_enable(); 737 return ret; 738 } 739 740 static int __sched 741 __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 742 struct lockdep_map *nest_lock, unsigned long ip) 743 { 744 return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false); 745 } 746 747 static int __sched 748 __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 749 unsigned long ip, struct ww_acquire_ctx *ww_ctx) 750 { 751 return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true); 752 } 753 754 /** 755 * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context 756 * @ww: mutex to lock 757 * @ww_ctx: optional w/w acquire context 758 * 759 * Trylocks a mutex with the optional acquire context; no deadlock detection is 760 * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise. 761 * 762 * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is 763 * specified, -EALREADY handling may happen in calls to ww_mutex_trylock. 764 * 765 * A mutex acquired with this function must be released with ww_mutex_unlock. 766 */ 767 int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx) 768 { 769 if (!ww_ctx) 770 return mutex_trylock(&ww->base); 771 772 MUTEX_WARN_ON(ww->base.magic != &ww->base); 773 774 /* 775 * Reset the wounded flag after a kill. No other process can 776 * race and wound us here, since they can't have a valid owner 777 * pointer if we don't have any locks held. 778 */ 779 if (ww_ctx->acquired == 0) 780 ww_ctx->wounded = 0; 781 782 if (__mutex_trylock(&ww->base)) { 783 ww_mutex_set_context_fastpath(ww, ww_ctx); 784 mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_); 785 return 1; 786 } 787 788 return 0; 789 } 790 EXPORT_SYMBOL(ww_mutex_trylock); 791 792 #ifdef CONFIG_DEBUG_LOCK_ALLOC 793 void __sched 794 mutex_lock_nested(struct mutex *lock, unsigned int subclass) 795 { 796 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); 797 } 798 799 EXPORT_SYMBOL_GPL(mutex_lock_nested); 800 801 void __sched 802 _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) 803 { 804 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_); 805 } 806 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 807 808 int __sched 809 mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) 810 { 811 return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_); 812 } 813 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested); 814 815 int __sched 816 mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) 817 { 818 return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_); 819 } 820 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 821 822 void __sched 823 mutex_lock_io_nested(struct mutex *lock, unsigned int subclass) 824 { 825 int token; 826 827 might_sleep(); 828 829 token = io_schedule_prepare(); 830 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 831 subclass, NULL, _RET_IP_, NULL, 0); 832 io_schedule_finish(token); 833 } 834 EXPORT_SYMBOL_GPL(mutex_lock_io_nested); 835 836 static inline int 837 ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 838 { 839 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 840 unsigned tmp; 841 842 if (ctx->deadlock_inject_countdown-- == 0) { 843 tmp = ctx->deadlock_inject_interval; 844 if (tmp > UINT_MAX/4) 845 tmp = UINT_MAX; 846 else 847 tmp = tmp*2 + tmp + tmp/2; 848 849 ctx->deadlock_inject_interval = tmp; 850 ctx->deadlock_inject_countdown = tmp; 851 ctx->contending_lock = lock; 852 853 ww_mutex_unlock(lock); 854 855 return -EDEADLK; 856 } 857 #endif 858 859 return 0; 860 } 861 862 int __sched 863 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 864 { 865 int ret; 866 867 might_sleep(); 868 ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 869 0, _RET_IP_, ctx); 870 if (!ret && ctx && ctx->acquired > 1) 871 return ww_mutex_deadlock_injection(lock, ctx); 872 873 return ret; 874 } 875 EXPORT_SYMBOL_GPL(ww_mutex_lock); 876 877 int __sched 878 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 879 { 880 int ret; 881 882 might_sleep(); 883 ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 884 0, _RET_IP_, ctx); 885 886 if (!ret && ctx && ctx->acquired > 1) 887 return ww_mutex_deadlock_injection(lock, ctx); 888 889 return ret; 890 } 891 EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); 892 893 #endif 894 895 /* 896 * Release the lock, slowpath: 897 */ 898 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) 899 { 900 struct task_struct *next = NULL; 901 DEFINE_WAKE_Q(wake_q); 902 unsigned long owner; 903 unsigned long flags; 904 905 mutex_release(&lock->dep_map, ip); 906 907 /* 908 * Release the lock before (potentially) taking the spinlock such that 909 * other contenders can get on with things ASAP. 910 * 911 * Except when HANDOFF, in that case we must not clear the owner field, 912 * but instead set it to the top waiter. 913 */ 914 owner = atomic_long_read(&lock->owner); 915 for (;;) { 916 MUTEX_WARN_ON(__owner_task(owner) != current); 917 MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 918 919 if (owner & MUTEX_FLAG_HANDOFF) 920 break; 921 922 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) { 923 if (owner & MUTEX_FLAG_WAITERS) 924 break; 925 926 return; 927 } 928 } 929 930 raw_spin_lock_irqsave(&lock->wait_lock, flags); 931 debug_mutex_unlock(lock); 932 if (!list_empty(&lock->wait_list)) { 933 /* get the first entry from the wait-list: */ 934 struct mutex_waiter *waiter = 935 list_first_entry(&lock->wait_list, 936 struct mutex_waiter, list); 937 938 next = waiter->task; 939 940 debug_mutex_wake_waiter(lock, waiter); 941 wake_q_add(&wake_q, next); 942 } 943 944 if (owner & MUTEX_FLAG_HANDOFF) 945 __mutex_handoff(lock, next); 946 947 raw_spin_unlock_irqrestore_wake(&lock->wait_lock, flags, &wake_q); 948 } 949 950 #ifndef CONFIG_DEBUG_LOCK_ALLOC 951 /* 952 * Here come the less common (and hence less performance-critical) APIs: 953 * mutex_lock_interruptible() and mutex_trylock(). 954 */ 955 static noinline int __sched 956 __mutex_lock_killable_slowpath(struct mutex *lock); 957 958 static noinline int __sched 959 __mutex_lock_interruptible_slowpath(struct mutex *lock); 960 961 /** 962 * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals. 963 * @lock: The mutex to be acquired. 964 * 965 * Lock the mutex like mutex_lock(). If a signal is delivered while the 966 * process is sleeping, this function will return without acquiring the 967 * mutex. 968 * 969 * Context: Process context. 970 * Return: 0 if the lock was successfully acquired or %-EINTR if a 971 * signal arrived. 972 */ 973 int __sched mutex_lock_interruptible(struct mutex *lock) 974 { 975 might_sleep(); 976 977 if (__mutex_trylock_fast(lock)) 978 return 0; 979 980 return __mutex_lock_interruptible_slowpath(lock); 981 } 982 983 EXPORT_SYMBOL(mutex_lock_interruptible); 984 985 /** 986 * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals. 987 * @lock: The mutex to be acquired. 988 * 989 * Lock the mutex like mutex_lock(). If a signal which will be fatal to 990 * the current process is delivered while the process is sleeping, this 991 * function will return without acquiring the mutex. 992 * 993 * Context: Process context. 994 * Return: 0 if the lock was successfully acquired or %-EINTR if a 995 * fatal signal arrived. 996 */ 997 int __sched mutex_lock_killable(struct mutex *lock) 998 { 999 might_sleep(); 1000 1001 if (__mutex_trylock_fast(lock)) 1002 return 0; 1003 1004 return __mutex_lock_killable_slowpath(lock); 1005 } 1006 EXPORT_SYMBOL(mutex_lock_killable); 1007 1008 /** 1009 * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O 1010 * @lock: The mutex to be acquired. 1011 * 1012 * Lock the mutex like mutex_lock(). While the task is waiting for this 1013 * mutex, it will be accounted as being in the IO wait state by the 1014 * scheduler. 1015 * 1016 * Context: Process context. 1017 */ 1018 void __sched mutex_lock_io(struct mutex *lock) 1019 { 1020 int token; 1021 1022 token = io_schedule_prepare(); 1023 mutex_lock(lock); 1024 io_schedule_finish(token); 1025 } 1026 EXPORT_SYMBOL_GPL(mutex_lock_io); 1027 1028 static noinline void __sched 1029 __mutex_lock_slowpath(struct mutex *lock) 1030 { 1031 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_); 1032 } 1033 1034 static noinline int __sched 1035 __mutex_lock_killable_slowpath(struct mutex *lock) 1036 { 1037 return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_); 1038 } 1039 1040 static noinline int __sched 1041 __mutex_lock_interruptible_slowpath(struct mutex *lock) 1042 { 1043 return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_); 1044 } 1045 1046 static noinline int __sched 1047 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 1048 { 1049 return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, 1050 _RET_IP_, ctx); 1051 } 1052 1053 static noinline int __sched 1054 __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, 1055 struct ww_acquire_ctx *ctx) 1056 { 1057 return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, 1058 _RET_IP_, ctx); 1059 } 1060 1061 #endif 1062 1063 /** 1064 * mutex_trylock - try to acquire the mutex, without waiting 1065 * @lock: the mutex to be acquired 1066 * 1067 * Try to acquire the mutex atomically. Returns 1 if the mutex 1068 * has been acquired successfully, and 0 on contention. 1069 * 1070 * NOTE: this function follows the spin_trylock() convention, so 1071 * it is negated from the down_trylock() return values! Be careful 1072 * about this when converting semaphore users to mutexes. 1073 * 1074 * This function must not be used in interrupt context. The 1075 * mutex must be released by the same task that acquired it. 1076 */ 1077 int __sched mutex_trylock(struct mutex *lock) 1078 { 1079 bool locked; 1080 1081 MUTEX_WARN_ON(lock->magic != lock); 1082 1083 locked = __mutex_trylock(lock); 1084 if (locked) 1085 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 1086 1087 return locked; 1088 } 1089 EXPORT_SYMBOL(mutex_trylock); 1090 1091 #ifndef CONFIG_DEBUG_LOCK_ALLOC 1092 int __sched 1093 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 1094 { 1095 might_sleep(); 1096 1097 if (__mutex_trylock_fast(&lock->base)) { 1098 if (ctx) 1099 ww_mutex_set_context_fastpath(lock, ctx); 1100 return 0; 1101 } 1102 1103 return __ww_mutex_lock_slowpath(lock, ctx); 1104 } 1105 EXPORT_SYMBOL(ww_mutex_lock); 1106 1107 int __sched 1108 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 1109 { 1110 might_sleep(); 1111 1112 if (__mutex_trylock_fast(&lock->base)) { 1113 if (ctx) 1114 ww_mutex_set_context_fastpath(lock, ctx); 1115 return 0; 1116 } 1117 1118 return __ww_mutex_lock_interruptible_slowpath(lock, ctx); 1119 } 1120 EXPORT_SYMBOL(ww_mutex_lock_interruptible); 1121 1122 #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ 1123 #endif /* !CONFIG_PREEMPT_RT */ 1124 1125 EXPORT_TRACEPOINT_SYMBOL_GPL(contention_begin); 1126 EXPORT_TRACEPOINT_SYMBOL_GPL(contention_end); 1127 1128 /** 1129 * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 1130 * @cnt: the atomic which we are to dec 1131 * @lock: the mutex to return holding if we dec to 0 1132 * 1133 * return true and hold lock if we dec to 0, return false otherwise 1134 */ 1135 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) 1136 { 1137 /* dec if we can't possibly hit 0 */ 1138 if (atomic_add_unless(cnt, -1, 1)) 1139 return 0; 1140 /* we might hit 0, so take the lock */ 1141 mutex_lock(lock); 1142 if (!atomic_dec_and_test(cnt)) { 1143 /* when we actually did the dec, we didn't hit 0 */ 1144 mutex_unlock(lock); 1145 return 0; 1146 } 1147 /* we hit 0, and we hold the lock */ 1148 return 1; 1149 } 1150 EXPORT_SYMBOL(atomic_dec_and_mutex_lock); 1151