mutex.c 24 KB

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