mutex.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * kernel/locking/mutex.c
  3. *
  4. * Mutexes: blocking mutual exclusion locks
  5. *
  6. * Started by Ingo Molnar:
  7. *
  8. * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  9. *
  10. * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
  11. * David Howells for suggestions and improvements.
  12. *
  13. * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
  14. * from the -rt tree, where it was originally implemented for rtmutexes
  15. * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
  16. * and Sven Dietrich.
  17. *
  18. * Also see Documentation/locking/mutex-design.txt.
  19. */
  20. #include <linux/mutex.h>
  21. #include <linux/ww_mutex.h>
  22. #include <linux/sched.h>
  23. #include <linux/sched/rt.h>
  24. #include <linux/export.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/debug_locks.h>
  28. #include "mcs_spinlock.h"
  29. /*
  30. * In the DEBUG case we are using the "NULL fastpath" for mutexes,
  31. * which forces all calls into the slowpath:
  32. */
  33. #ifdef CONFIG_DEBUG_MUTEXES
  34. # include "mutex-debug.h"
  35. # include <asm-generic/mutex-null.h>
  36. # ifndef CONFIG_LOCKDEP
  37. # define CREATE_TRACE_POINTS
  38. # endif
  39. # include <trace/events/lock.h>
  40. /*
  41. * Must be 0 for the debug case so we do not do the unlock outside of the
  42. * wait_lock region. debug_mutex_unlock() will do the actual unlock in this
  43. * case.
  44. */
  45. # undef __mutex_slowpath_needs_to_unlock
  46. # define __mutex_slowpath_needs_to_unlock() 0
  47. #else
  48. # include "mutex.h"
  49. # include <asm/mutex.h>
  50. #endif
  51. void
  52. __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
  53. {
  54. atomic_set(&lock->count, 1);
  55. spin_lock_init(&lock->wait_lock);
  56. INIT_LIST_HEAD(&lock->wait_list);
  57. mutex_clear_owner(lock);
  58. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  59. osq_lock_init(&lock->osq);
  60. #endif
  61. debug_mutex_init(lock, name, key);
  62. }
  63. EXPORT_SYMBOL(__mutex_init);
  64. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  65. /*
  66. * We split the mutex lock/unlock logic into separate fastpath and
  67. * slowpath functions, to reduce the register pressure on the fastpath.
  68. * We also put the fastpath first in the kernel image, to make sure the
  69. * branch is predicted by the CPU as default-untaken.
  70. */
  71. __visible void __sched __mutex_lock_slowpath(atomic_t *lock_count);
  72. /**
  73. * mutex_lock - acquire the mutex
  74. * @lock: the mutex to be acquired
  75. *
  76. * Lock the mutex exclusively for this task. If the mutex is not
  77. * available right now, it will sleep until it can get it.
  78. *
  79. * The mutex must later on be released by the same task that
  80. * acquired it. Recursive locking is not allowed. The task
  81. * may not exit without first unlocking the mutex. Also, kernel
  82. * memory where the mutex resides mutex must not be freed with
  83. * the mutex still locked. The mutex must first be initialized
  84. * (or statically defined) before it can be locked. memset()-ing
  85. * the mutex to 0 is not allowed.
  86. *
  87. * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging
  88. * checks that will enforce the restrictions and will also do
  89. * deadlock debugging. )
  90. *
  91. * This function is similar to (but not equivalent to) down().
  92. */
  93. void __sched mutex_lock(struct mutex *lock)
  94. {
  95. might_sleep();
  96. /*
  97. * The locking fastpath is the 1->0 transition from
  98. * 'unlocked' into 'locked' state.
  99. */
  100. __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
  101. mutex_set_owner(lock);
  102. }
  103. EXPORT_SYMBOL(mutex_lock);
  104. #endif
  105. static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
  106. struct ww_acquire_ctx *ww_ctx)
  107. {
  108. #ifdef CONFIG_DEBUG_MUTEXES
  109. /*
  110. * If this WARN_ON triggers, you used ww_mutex_lock to acquire,
  111. * but released with a normal mutex_unlock in this call.
  112. *
  113. * This should never happen, always use ww_mutex_unlock.
  114. */
  115. DEBUG_LOCKS_WARN_ON(ww->ctx);
  116. /*
  117. * Not quite done after calling ww_acquire_done() ?
  118. */
  119. DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
  120. if (ww_ctx->contending_lock) {
  121. /*
  122. * After -EDEADLK you tried to
  123. * acquire a different ww_mutex? Bad!
  124. */
  125. DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
  126. /*
  127. * You called ww_mutex_lock after receiving -EDEADLK,
  128. * but 'forgot' to unlock everything else first?
  129. */
  130. DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
  131. ww_ctx->contending_lock = NULL;
  132. }
  133. /*
  134. * Naughty, using a different class will lead to undefined behavior!
  135. */
  136. DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
  137. #endif
  138. ww_ctx->acquired++;
  139. }
  140. /*
  141. * after acquiring lock with fastpath or when we lost out in contested
  142. * slowpath, set ctx and wake up any waiters so they can recheck.
  143. *
  144. * This function is never called when CONFIG_DEBUG_LOCK_ALLOC is set,
  145. * as the fastpath and opportunistic spinning are disabled in that case.
  146. */
  147. static __always_inline void
  148. ww_mutex_set_context_fastpath(struct ww_mutex *lock,
  149. struct ww_acquire_ctx *ctx)
  150. {
  151. unsigned long flags;
  152. struct mutex_waiter *cur;
  153. ww_mutex_lock_acquired(lock, ctx);
  154. lock->ctx = ctx;
  155. /*
  156. * The lock->ctx update should be visible on all cores before
  157. * the atomic read is done, otherwise contended waiters might be
  158. * missed. The contended waiters will either see ww_ctx == NULL
  159. * and keep spinning, or it will acquire wait_lock, add itself
  160. * to waiter list and sleep.
  161. */
  162. smp_mb(); /* ^^^ */
  163. /*
  164. * Check if lock is contended, if not there is nobody to wake up
  165. */
  166. if (likely(atomic_read(&lock->base.count) == 0))
  167. return;
  168. /*
  169. * Uh oh, we raced in fastpath, wake up everyone in this case,
  170. * so they can see the new lock->ctx.
  171. */
  172. spin_lock_mutex(&lock->base.wait_lock, flags);
  173. list_for_each_entry(cur, &lock->base.wait_list, list) {
  174. debug_mutex_wake_waiter(&lock->base, cur);
  175. wake_up_process(cur->task);
  176. }
  177. spin_unlock_mutex(&lock->base.wait_lock, flags);
  178. }
  179. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  180. /*
  181. * In order to avoid a stampede of mutex spinners from acquiring the mutex
  182. * more or less simultaneously, the spinners need to acquire a MCS lock
  183. * first before spinning on the owner field.
  184. *
  185. */
  186. /*
  187. * Mutex spinning code migrated from kernel/sched/core.c
  188. */
  189. static inline bool owner_running(struct mutex *lock, struct task_struct *owner)
  190. {
  191. if (lock->owner != owner)
  192. return false;
  193. /*
  194. * Ensure we emit the owner->on_cpu, dereference _after_ checking
  195. * lock->owner still matches owner, if that fails, owner might
  196. * point to free()d memory, if it still matches, the rcu_read_lock()
  197. * ensures the memory stays valid.
  198. */
  199. barrier();
  200. return owner->on_cpu;
  201. }
  202. /*
  203. * Look out! "owner" is an entirely speculative pointer
  204. * access and not reliable.
  205. */
  206. static noinline
  207. int mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
  208. {
  209. rcu_read_lock();
  210. while (owner_running(lock, owner)) {
  211. if (need_resched())
  212. break;
  213. cpu_relax_lowlatency();
  214. }
  215. rcu_read_unlock();
  216. /*
  217. * We break out the loop above on need_resched() and when the
  218. * owner changed, which is a sign for heavy contention. Return
  219. * success only when lock->owner is NULL.
  220. */
  221. return lock->owner == NULL;
  222. }
  223. /*
  224. * Initial check for entering the mutex spinning loop
  225. */
  226. static inline int mutex_can_spin_on_owner(struct mutex *lock)
  227. {
  228. struct task_struct *owner;
  229. int retval = 1;
  230. if (need_resched())
  231. return 0;
  232. rcu_read_lock();
  233. owner = ACCESS_ONCE(lock->owner);
  234. if (owner)
  235. retval = owner->on_cpu;
  236. rcu_read_unlock();
  237. /*
  238. * if lock->owner is not set, the mutex owner may have just acquired
  239. * it and not set the owner yet or the mutex has been released.
  240. */
  241. return retval;
  242. }
  243. /*
  244. * Atomically try to take the lock when it is available
  245. */
  246. static inline bool mutex_try_to_acquire(struct mutex *lock)
  247. {
  248. return !mutex_is_locked(lock) &&
  249. (atomic_cmpxchg(&lock->count, 1, 0) == 1);
  250. }
  251. /*
  252. * Optimistic spinning.
  253. *
  254. * We try to spin for acquisition when we find that the lock owner
  255. * is currently running on a (different) CPU and while we don't
  256. * need to reschedule. The rationale is that if the lock owner is
  257. * running, it is likely to release the lock soon.
  258. *
  259. * Since this needs the lock owner, and this mutex implementation
  260. * doesn't track the owner atomically in the lock field, we need to
  261. * track it non-atomically.
  262. *
  263. * We can't do this for DEBUG_MUTEXES because that relies on wait_lock
  264. * to serialize everything.
  265. *
  266. * The mutex spinners are queued up using MCS lock so that only one
  267. * spinner can compete for the mutex. However, if mutex spinning isn't
  268. * going to happen, there is no point in going through the lock/unlock
  269. * overhead.
  270. *
  271. * Returns true when the lock was taken, otherwise false, indicating
  272. * that we need to jump to the slowpath and sleep.
  273. */
  274. static bool mutex_optimistic_spin(struct mutex *lock,
  275. struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
  276. {
  277. struct task_struct *task = current;
  278. if (!mutex_can_spin_on_owner(lock))
  279. goto done;
  280. if (!osq_lock(&lock->osq))
  281. goto done;
  282. while (true) {
  283. struct task_struct *owner;
  284. if (use_ww_ctx && ww_ctx->acquired > 0) {
  285. struct ww_mutex *ww;
  286. ww = container_of(lock, struct ww_mutex, base);
  287. /*
  288. * If ww->ctx is set the contents are undefined, only
  289. * by acquiring wait_lock there is a guarantee that
  290. * they are not invalid when reading.
  291. *
  292. * As such, when deadlock detection needs to be
  293. * performed the optimistic spinning cannot be done.
  294. */
  295. if (ACCESS_ONCE(ww->ctx))
  296. break;
  297. }
  298. /*
  299. * If there's an owner, wait for it to either
  300. * release the lock or go to sleep.
  301. */
  302. owner = ACCESS_ONCE(lock->owner);
  303. if (owner && !mutex_spin_on_owner(lock, owner))
  304. break;
  305. /* Try to acquire the mutex if it is unlocked. */
  306. if (mutex_try_to_acquire(lock)) {
  307. lock_acquired(&lock->dep_map, ip);
  308. if (use_ww_ctx) {
  309. struct ww_mutex *ww;
  310. ww = container_of(lock, struct ww_mutex, base);
  311. ww_mutex_set_context_fastpath(ww, ww_ctx);
  312. }
  313. mutex_set_owner(lock);
  314. osq_unlock(&lock->osq);
  315. return true;
  316. }
  317. /*
  318. * When there's no owner, we might have preempted between the
  319. * owner acquiring the lock and setting the owner field. If
  320. * we're an RT task that will live-lock because we won't let
  321. * the owner complete.
  322. */
  323. if (!owner && (need_resched() || rt_task(task)))
  324. break;
  325. /*
  326. * The cpu_relax() call is a compiler barrier which forces
  327. * everything in this loop to be re-loaded. We don't need
  328. * memory barriers as we'll eventually observe the right
  329. * values at the cost of a few extra spins.
  330. */
  331. cpu_relax_lowlatency();
  332. }
  333. osq_unlock(&lock->osq);
  334. done:
  335. /*
  336. * If we fell out of the spin path because of need_resched(),
  337. * reschedule now, before we try-lock the mutex. This avoids getting
  338. * scheduled out right after we obtained the mutex.
  339. */
  340. if (need_resched()) {
  341. /*
  342. * We _should_ have TASK_RUNNING here, but just in case
  343. * we do not, make it so, otherwise we might get stuck.
  344. * 6f942a1f264e875c5f3ad6f505d7b500a3e7fa82 (patch)
  345. */
  346. __set_current_state(TASK_RUNNING);
  347. schedule_preempt_disabled();
  348. }
  349. return false;
  350. }
  351. #else
  352. static bool mutex_optimistic_spin(struct mutex *lock,
  353. struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
  354. {
  355. return false;
  356. }
  357. #endif
  358. __visible __used noinline
  359. void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
  360. /**
  361. * mutex_unlock - release the mutex
  362. * @lock: the mutex to be released
  363. *
  364. * Unlock a mutex that has been locked by this task previously.
  365. *
  366. * This function must not be used in interrupt context. Unlocking
  367. * of a not locked mutex is not allowed.
  368. *
  369. * This function is similar to (but not equivalent to) up().
  370. */
  371. void __sched mutex_unlock(struct mutex *lock)
  372. {
  373. /*
  374. * The unlocking fastpath is the 0->1 transition from 'locked'
  375. * into 'unlocked' state:
  376. */
  377. #ifndef CONFIG_DEBUG_MUTEXES
  378. /*
  379. * When debugging is enabled we must not clear the owner before time,
  380. * the slow path will always be taken, and that clears the owner field
  381. * after verifying that it was indeed current.
  382. */
  383. mutex_clear_owner(lock);
  384. #endif
  385. __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
  386. }
  387. EXPORT_SYMBOL(mutex_unlock);
  388. /**
  389. * ww_mutex_unlock - release the w/w mutex
  390. * @lock: the mutex to be released
  391. *
  392. * Unlock a mutex that has been locked by this task previously with any of the
  393. * ww_mutex_lock* functions (with or without an acquire context). It is
  394. * forbidden to release the locks after releasing the acquire context.
  395. *
  396. * This function must not be used in interrupt context. Unlocking
  397. * of a unlocked mutex is not allowed.
  398. */
  399. void __sched ww_mutex_unlock(struct ww_mutex *lock)
  400. {
  401. /*
  402. * The unlocking fastpath is the 0->1 transition from 'locked'
  403. * into 'unlocked' state:
  404. */
  405. if (lock->ctx) {
  406. #ifdef CONFIG_DEBUG_MUTEXES
  407. DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
  408. #endif
  409. if (lock->ctx->acquired > 0)
  410. lock->ctx->acquired--;
  411. lock->ctx = NULL;
  412. }
  413. #ifndef CONFIG_DEBUG_MUTEXES
  414. /*
  415. * When debugging is enabled we must not clear the owner before time,
  416. * the slow path will always be taken, and that clears the owner field
  417. * after verifying that it was indeed current.
  418. */
  419. mutex_clear_owner(&lock->base);
  420. #endif
  421. __mutex_fastpath_unlock(&lock->base.count, __mutex_unlock_slowpath);
  422. }
  423. EXPORT_SYMBOL(ww_mutex_unlock);
  424. static inline int __sched
  425. __mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx)
  426. {
  427. struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
  428. struct ww_acquire_ctx *hold_ctx = ACCESS_ONCE(ww->ctx);
  429. if (!hold_ctx)
  430. return 0;
  431. if (unlikely(ctx == hold_ctx))
  432. return -EALREADY;
  433. if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
  434. (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
  435. #ifdef CONFIG_DEBUG_MUTEXES
  436. DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
  437. ctx->contending_lock = ww;
  438. #endif
  439. return -EDEADLK;
  440. }
  441. return 0;
  442. }
  443. /*
  444. * Lock a mutex (possibly interruptible), slowpath:
  445. */
  446. static __always_inline int __sched
  447. __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
  448. struct lockdep_map *nest_lock, unsigned long ip,
  449. struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
  450. {
  451. struct task_struct *task = current;
  452. struct mutex_waiter waiter;
  453. unsigned long flags;
  454. int ret;
  455. #ifdef CONFIG_DEBUG_MUTEXES
  456. bool __mutex_contended = false;
  457. #endif
  458. preempt_disable();
  459. mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
  460. if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) {
  461. /* got the lock, yay! */
  462. preempt_enable();
  463. return 0;
  464. }
  465. spin_lock_mutex(&lock->wait_lock, flags);
  466. /*
  467. * Once more, try to acquire the lock. Only try-lock the mutex if
  468. * it is unlocked to reduce unnecessary xchg() operations.
  469. */
  470. if (!mutex_is_locked(lock) && (atomic_xchg(&lock->count, 0) == 1))
  471. goto skip_wait;
  472. debug_mutex_lock_common(lock, &waiter);
  473. debug_mutex_add_waiter(lock, &waiter, task_thread_info(task));
  474. /* add waiting tasks to the end of the waitqueue (FIFO): */
  475. list_add_tail(&waiter.list, &lock->wait_list);
  476. waiter.task = task;
  477. lock_contended(&lock->dep_map, ip);
  478. #ifdef CONFIG_DEBUG_MUTEXES
  479. trace_mutex_contended(lock, ip);
  480. __mutex_contended = true; /* to pair mutex_contended & mutex_acquired */
  481. #endif
  482. for (;;) {
  483. /*
  484. * Lets try to take the lock again - this is needed even if
  485. * we get here for the first time (shortly after failing to
  486. * acquire the lock), to make sure that we get a wakeup once
  487. * it's unlocked. Later on, if we sleep, this is the
  488. * operation that gives us the lock. We xchg it to -1, so
  489. * that when we release the lock, we properly wake up the
  490. * other waiters. We only attempt the xchg if the count is
  491. * non-negative in order to avoid unnecessary xchg operations:
  492. */
  493. if (atomic_read(&lock->count) >= 0 &&
  494. (atomic_xchg(&lock->count, -1) == 1))
  495. break;
  496. /*
  497. * got a signal? (This code gets eliminated in the
  498. * TASK_UNINTERRUPTIBLE case.)
  499. */
  500. if (unlikely(signal_pending_state(state, task))) {
  501. ret = -EINTR;
  502. goto err;
  503. }
  504. if (use_ww_ctx && ww_ctx->acquired > 0) {
  505. ret = __mutex_lock_check_stamp(lock, ww_ctx);
  506. if (ret)
  507. goto err;
  508. }
  509. __set_task_state(task, state);
  510. /* didn't get the lock, go to sleep: */
  511. spin_unlock_mutex(&lock->wait_lock, flags);
  512. schedule_preempt_disabled();
  513. spin_lock_mutex(&lock->wait_lock, flags);
  514. }
  515. mutex_remove_waiter(lock, &waiter, current_thread_info());
  516. /* set it to 0 if there are no waiters left: */
  517. if (likely(list_empty(&lock->wait_list)))
  518. atomic_set(&lock->count, 0);
  519. debug_mutex_free_waiter(&waiter);
  520. skip_wait:
  521. #ifdef CONFIG_DEBUG_MUTEXES
  522. if (unlikely(__mutex_contended))
  523. trace_mutex_acquired(lock, ip);
  524. #endif
  525. /* got the lock - cleanup and rejoice! */
  526. lock_acquired(&lock->dep_map, ip);
  527. mutex_set_owner(lock);
  528. if (use_ww_ctx) {
  529. struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
  530. struct mutex_waiter *cur;
  531. /*
  532. * This branch gets optimized out for the common case,
  533. * and is only important for ww_mutex_lock.
  534. */
  535. ww_mutex_lock_acquired(ww, ww_ctx);
  536. ww->ctx = ww_ctx;
  537. /*
  538. * Give any possible sleeping processes the chance to wake up,
  539. * so they can recheck if they have to back off.
  540. */
  541. list_for_each_entry(cur, &lock->wait_list, list) {
  542. debug_mutex_wake_waiter(lock, cur);
  543. wake_up_process(cur->task);
  544. }
  545. }
  546. spin_unlock_mutex(&lock->wait_lock, flags);
  547. preempt_enable();
  548. return 0;
  549. err:
  550. mutex_remove_waiter(lock, &waiter, task_thread_info(task));
  551. spin_unlock_mutex(&lock->wait_lock, flags);
  552. debug_mutex_free_waiter(&waiter);
  553. mutex_release(&lock->dep_map, 1, ip);
  554. preempt_enable();
  555. return ret;
  556. }
  557. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  558. void __sched
  559. mutex_lock_nested(struct mutex *lock, unsigned int subclass)
  560. {
  561. might_sleep();
  562. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
  563. subclass, NULL, _RET_IP_, NULL, 0);
  564. }
  565. EXPORT_SYMBOL_GPL(mutex_lock_nested);
  566. void __sched
  567. _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  568. {
  569. might_sleep();
  570. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
  571. 0, nest, _RET_IP_, NULL, 0);
  572. }
  573. EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
  574. int __sched
  575. mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
  576. {
  577. might_sleep();
  578. return __mutex_lock_common(lock, TASK_KILLABLE,
  579. subclass, NULL, _RET_IP_, NULL, 0);
  580. }
  581. EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
  582. int __sched
  583. mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
  584. {
  585. might_sleep();
  586. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
  587. subclass, NULL, _RET_IP_, NULL, 0);
  588. }
  589. EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
  590. static inline int
  591. ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  592. {
  593. #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
  594. unsigned tmp;
  595. if (ctx->deadlock_inject_countdown-- == 0) {
  596. tmp = ctx->deadlock_inject_interval;
  597. if (tmp > UINT_MAX/4)
  598. tmp = UINT_MAX;
  599. else
  600. tmp = tmp*2 + tmp + tmp/2;
  601. ctx->deadlock_inject_interval = tmp;
  602. ctx->deadlock_inject_countdown = tmp;
  603. ctx->contending_lock = lock;
  604. ww_mutex_unlock(lock);
  605. return -EDEADLK;
  606. }
  607. #endif
  608. return 0;
  609. }
  610. int __sched
  611. __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  612. {
  613. int ret;
  614. might_sleep();
  615. ret = __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE,
  616. 0, &ctx->dep_map, _RET_IP_, ctx, 1);
  617. if (!ret && ctx->acquired > 1)
  618. return ww_mutex_deadlock_injection(lock, ctx);
  619. return ret;
  620. }
  621. EXPORT_SYMBOL_GPL(__ww_mutex_lock);
  622. int __sched
  623. __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  624. {
  625. int ret;
  626. might_sleep();
  627. ret = __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE,
  628. 0, &ctx->dep_map, _RET_IP_, ctx, 1);
  629. if (!ret && ctx->acquired > 1)
  630. return ww_mutex_deadlock_injection(lock, ctx);
  631. return ret;
  632. }
  633. EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
  634. #endif
  635. /*
  636. * Release the lock, slowpath:
  637. */
  638. static inline void
  639. __mutex_unlock_common_slowpath(struct mutex *lock, int nested)
  640. {
  641. unsigned long flags;
  642. /*
  643. * As a performance measurement, release the lock before doing other
  644. * wakeup related duties to follow. This allows other tasks to acquire
  645. * the lock sooner, while still handling cleanups in past unlock calls.
  646. * This can be done as we do not enforce strict equivalence between the
  647. * mutex counter and wait_list.
  648. *
  649. *
  650. * Some architectures leave the lock unlocked in the fastpath failure
  651. * case, others need to leave it locked. In the later case we have to
  652. * unlock it here - as the lock counter is currently 0 or negative.
  653. */
  654. if (__mutex_slowpath_needs_to_unlock())
  655. atomic_set(&lock->count, 1);
  656. spin_lock_mutex(&lock->wait_lock, flags);
  657. mutex_release(&lock->dep_map, nested, _RET_IP_);
  658. debug_mutex_unlock(lock);
  659. if (!list_empty(&lock->wait_list)) {
  660. /* get the first entry from the wait-list: */
  661. struct mutex_waiter *waiter =
  662. list_entry(lock->wait_list.next,
  663. struct mutex_waiter, list);
  664. debug_mutex_wake_waiter(lock, waiter);
  665. wake_up_process(waiter->task);
  666. }
  667. spin_unlock_mutex(&lock->wait_lock, flags);
  668. }
  669. /*
  670. * Release the lock, slowpath:
  671. */
  672. __visible void
  673. __mutex_unlock_slowpath(atomic_t *lock_count)
  674. {
  675. struct mutex *lock = container_of(lock_count, struct mutex, count);
  676. __mutex_unlock_common_slowpath(lock, 1);
  677. }
  678. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  679. /*
  680. * Here come the less common (and hence less performance-critical) APIs:
  681. * mutex_lock_interruptible() and mutex_trylock().
  682. */
  683. static noinline int __sched
  684. __mutex_lock_killable_slowpath(struct mutex *lock);
  685. static noinline int __sched
  686. __mutex_lock_interruptible_slowpath(struct mutex *lock);
  687. /**
  688. * mutex_lock_interruptible - acquire the mutex, interruptible
  689. * @lock: the mutex to be acquired
  690. *
  691. * Lock the mutex like mutex_lock(), and return 0 if the mutex has
  692. * been acquired or sleep until the mutex becomes available. If a
  693. * signal arrives while waiting for the lock then this function
  694. * returns -EINTR.
  695. *
  696. * This function is similar to (but not equivalent to) down_interruptible().
  697. */
  698. int __sched mutex_lock_interruptible(struct mutex *lock)
  699. {
  700. int ret;
  701. might_sleep();
  702. ret = __mutex_fastpath_lock_retval(&lock->count);
  703. if (likely(!ret)) {
  704. mutex_set_owner(lock);
  705. return 0;
  706. } else
  707. return __mutex_lock_interruptible_slowpath(lock);
  708. }
  709. EXPORT_SYMBOL(mutex_lock_interruptible);
  710. int __sched mutex_lock_killable(struct mutex *lock)
  711. {
  712. int ret;
  713. might_sleep();
  714. ret = __mutex_fastpath_lock_retval(&lock->count);
  715. if (likely(!ret)) {
  716. mutex_set_owner(lock);
  717. return 0;
  718. } else
  719. return __mutex_lock_killable_slowpath(lock);
  720. }
  721. EXPORT_SYMBOL(mutex_lock_killable);
  722. __visible void __sched
  723. __mutex_lock_slowpath(atomic_t *lock_count)
  724. {
  725. struct mutex *lock = container_of(lock_count, struct mutex, count);
  726. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
  727. NULL, _RET_IP_, NULL, 0);
  728. }
  729. static noinline int __sched
  730. __mutex_lock_killable_slowpath(struct mutex *lock)
  731. {
  732. return __mutex_lock_common(lock, TASK_KILLABLE, 0,
  733. NULL, _RET_IP_, NULL, 0);
  734. }
  735. static noinline int __sched
  736. __mutex_lock_interruptible_slowpath(struct mutex *lock)
  737. {
  738. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0,
  739. NULL, _RET_IP_, NULL, 0);
  740. }
  741. static noinline int __sched
  742. __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  743. {
  744. return __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 0,
  745. NULL, _RET_IP_, ctx, 1);
  746. }
  747. static noinline int __sched
  748. __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
  749. struct ww_acquire_ctx *ctx)
  750. {
  751. return __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 0,
  752. NULL, _RET_IP_, ctx, 1);
  753. }
  754. #endif
  755. /*
  756. * Spinlock based trylock, we take the spinlock and check whether we
  757. * can get the lock:
  758. */
  759. static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
  760. {
  761. struct mutex *lock = container_of(lock_count, struct mutex, count);
  762. unsigned long flags;
  763. int prev;
  764. /* No need to trylock if the mutex is locked. */
  765. if (mutex_is_locked(lock))
  766. return 0;
  767. spin_lock_mutex(&lock->wait_lock, flags);
  768. prev = atomic_xchg(&lock->count, -1);
  769. if (likely(prev == 1)) {
  770. mutex_set_owner(lock);
  771. mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  772. }
  773. /* Set it back to 0 if there are no waiters: */
  774. if (likely(list_empty(&lock->wait_list)))
  775. atomic_set(&lock->count, 0);
  776. spin_unlock_mutex(&lock->wait_lock, flags);
  777. return prev == 1;
  778. }
  779. /**
  780. * mutex_trylock - try to acquire the mutex, without waiting
  781. * @lock: the mutex to be acquired
  782. *
  783. * Try to acquire the mutex atomically. Returns 1 if the mutex
  784. * has been acquired successfully, and 0 on contention.
  785. *
  786. * NOTE: this function follows the spin_trylock() convention, so
  787. * it is negated from the down_trylock() return values! Be careful
  788. * about this when converting semaphore users to mutexes.
  789. *
  790. * This function must not be used in interrupt context. The
  791. * mutex must be released by the same task that acquired it.
  792. */
  793. int __sched mutex_trylock(struct mutex *lock)
  794. {
  795. int ret;
  796. ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath);
  797. if (ret)
  798. mutex_set_owner(lock);
  799. return ret;
  800. }
  801. EXPORT_SYMBOL(mutex_trylock);
  802. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  803. int __sched
  804. __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  805. {
  806. int ret;
  807. might_sleep();
  808. ret = __mutex_fastpath_lock_retval(&lock->base.count);
  809. if (likely(!ret)) {
  810. ww_mutex_set_context_fastpath(lock, ctx);
  811. mutex_set_owner(&lock->base);
  812. } else
  813. ret = __ww_mutex_lock_slowpath(lock, ctx);
  814. return ret;
  815. }
  816. EXPORT_SYMBOL(__ww_mutex_lock);
  817. int __sched
  818. __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
  819. {
  820. int ret;
  821. might_sleep();
  822. ret = __mutex_fastpath_lock_retval(&lock->base.count);
  823. if (likely(!ret)) {
  824. ww_mutex_set_context_fastpath(lock, ctx);
  825. mutex_set_owner(&lock->base);
  826. } else
  827. ret = __ww_mutex_lock_interruptible_slowpath(lock, ctx);
  828. return ret;
  829. }
  830. EXPORT_SYMBOL(__ww_mutex_lock_interruptible);
  831. #endif
  832. /**
  833. * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  834. * @cnt: the atomic which we are to dec
  835. * @lock: the mutex to return holding if we dec to 0
  836. *
  837. * return true and hold lock if we dec to 0, return false otherwise
  838. */
  839. int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  840. {
  841. /* dec if we can't possibly hit 0 */
  842. if (atomic_add_unless(cnt, -1, 1))
  843. return 0;
  844. /* we might hit 0, so take the lock */
  845. mutex_lock(lock);
  846. if (!atomic_dec_and_test(cnt)) {
  847. /* when we actually did the dec, we didn't hit 0 */
  848. mutex_unlock(lock);
  849. return 0;
  850. }
  851. /* we hit 0, and we hold the lock */
  852. return 1;
  853. }
  854. EXPORT_SYMBOL(atomic_dec_and_mutex_lock);