mutex.c 25 KB

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