mutex.c 24 KB

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