mutex.c 24 KB

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