mutex.c 23 KB

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