mutex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/sched.h>
  22. #include <linux/sched/rt.h>
  23. #include <linux/export.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/debug_locks.h>
  27. /*
  28. * In the DEBUG case we are using the "NULL fastpath" for mutexes,
  29. * which forces all calls into the slowpath:
  30. */
  31. #ifdef CONFIG_DEBUG_MUTEXES
  32. # include "mutex-debug.h"
  33. # include <asm-generic/mutex-null.h>
  34. #else
  35. # include "mutex.h"
  36. # include <asm/mutex.h>
  37. #endif
  38. void
  39. __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
  40. {
  41. atomic_set(&lock->count, 1);
  42. spin_lock_init(&lock->wait_lock);
  43. INIT_LIST_HEAD(&lock->wait_list);
  44. mutex_clear_owner(lock);
  45. debug_mutex_init(lock, name, key);
  46. }
  47. EXPORT_SYMBOL(__mutex_init);
  48. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  49. /*
  50. * We split the mutex lock/unlock logic into separate fastpath and
  51. * slowpath functions, to reduce the register pressure on the fastpath.
  52. * We also put the fastpath first in the kernel image, to make sure the
  53. * branch is predicted by the CPU as default-untaken.
  54. */
  55. static __used noinline void __sched
  56. __mutex_lock_slowpath(atomic_t *lock_count);
  57. /**
  58. * mutex_lock - acquire the mutex
  59. * @lock: the mutex to be acquired
  60. *
  61. * Lock the mutex exclusively for this task. If the mutex is not
  62. * available right now, it will sleep until it can get it.
  63. *
  64. * The mutex must later on be released by the same task that
  65. * acquired it. Recursive locking is not allowed. The task
  66. * may not exit without first unlocking the mutex. Also, kernel
  67. * memory where the mutex resides mutex must not be freed with
  68. * the mutex still locked. The mutex must first be initialized
  69. * (or statically defined) before it can be locked. memset()-ing
  70. * the mutex to 0 is not allowed.
  71. *
  72. * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging
  73. * checks that will enforce the restrictions and will also do
  74. * deadlock debugging. )
  75. *
  76. * This function is similar to (but not equivalent to) down().
  77. */
  78. void __sched mutex_lock(struct mutex *lock)
  79. {
  80. might_sleep();
  81. /*
  82. * The locking fastpath is the 1->0 transition from
  83. * 'unlocked' into 'locked' state.
  84. */
  85. __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
  86. mutex_set_owner(lock);
  87. }
  88. EXPORT_SYMBOL(mutex_lock);
  89. #endif
  90. static __used noinline void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
  91. /**
  92. * mutex_unlock - release the mutex
  93. * @lock: the mutex to be released
  94. *
  95. * Unlock a mutex that has been locked by this task previously.
  96. *
  97. * This function must not be used in interrupt context. Unlocking
  98. * of a not locked mutex is not allowed.
  99. *
  100. * This function is similar to (but not equivalent to) up().
  101. */
  102. void __sched mutex_unlock(struct mutex *lock)
  103. {
  104. /*
  105. * The unlocking fastpath is the 0->1 transition from 'locked'
  106. * into 'unlocked' state:
  107. */
  108. #ifndef CONFIG_DEBUG_MUTEXES
  109. /*
  110. * When debugging is enabled we must not clear the owner before time,
  111. * the slow path will always be taken, and that clears the owner field
  112. * after verifying that it was indeed current.
  113. */
  114. mutex_clear_owner(lock);
  115. #endif
  116. __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
  117. }
  118. EXPORT_SYMBOL(mutex_unlock);
  119. /*
  120. * Lock a mutex (possibly interruptible), slowpath:
  121. */
  122. static inline int __sched
  123. __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
  124. struct lockdep_map *nest_lock, unsigned long ip)
  125. {
  126. struct task_struct *task = current;
  127. struct mutex_waiter waiter;
  128. unsigned long flags;
  129. preempt_disable();
  130. mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
  131. #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
  132. /*
  133. * Optimistic spinning.
  134. *
  135. * We try to spin for acquisition when we find that there are no
  136. * pending waiters and the lock owner is currently running on a
  137. * (different) CPU.
  138. *
  139. * The rationale is that if the lock owner is running, it is likely to
  140. * release the lock soon.
  141. *
  142. * Since this needs the lock owner, and this mutex implementation
  143. * doesn't track the owner atomically in the lock field, we need to
  144. * track it non-atomically.
  145. *
  146. * We can't do this for DEBUG_MUTEXES because that relies on wait_lock
  147. * to serialize everything.
  148. */
  149. for (;;) {
  150. struct task_struct *owner;
  151. /*
  152. * If there's an owner, wait for it to either
  153. * release the lock or go to sleep.
  154. */
  155. owner = ACCESS_ONCE(lock->owner);
  156. if (owner && !mutex_spin_on_owner(lock, owner))
  157. break;
  158. if (atomic_cmpxchg(&lock->count, 1, 0) == 1) {
  159. lock_acquired(&lock->dep_map, ip);
  160. mutex_set_owner(lock);
  161. preempt_enable();
  162. return 0;
  163. }
  164. /*
  165. * When there's no owner, we might have preempted between the
  166. * owner acquiring the lock and setting the owner field. If
  167. * we're an RT task that will live-lock because we won't let
  168. * the owner complete.
  169. */
  170. if (!owner && (need_resched() || rt_task(task)))
  171. break;
  172. /*
  173. * The cpu_relax() call is a compiler barrier which forces
  174. * everything in this loop to be re-loaded. We don't need
  175. * memory barriers as we'll eventually observe the right
  176. * values at the cost of a few extra spins.
  177. */
  178. arch_mutex_cpu_relax();
  179. }
  180. #endif
  181. spin_lock_mutex(&lock->wait_lock, flags);
  182. debug_mutex_lock_common(lock, &waiter);
  183. debug_mutex_add_waiter(lock, &waiter, task_thread_info(task));
  184. /* add waiting tasks to the end of the waitqueue (FIFO): */
  185. list_add_tail(&waiter.list, &lock->wait_list);
  186. waiter.task = task;
  187. if (atomic_xchg(&lock->count, -1) == 1)
  188. goto done;
  189. lock_contended(&lock->dep_map, ip);
  190. for (;;) {
  191. /*
  192. * Lets try to take the lock again - this is needed even if
  193. * we get here for the first time (shortly after failing to
  194. * acquire the lock), to make sure that we get a wakeup once
  195. * it's unlocked. Later on, if we sleep, this is the
  196. * operation that gives us the lock. We xchg it to -1, so
  197. * that when we release the lock, we properly wake up the
  198. * other waiters:
  199. */
  200. if (atomic_xchg(&lock->count, -1) == 1)
  201. break;
  202. /*
  203. * got a signal? (This code gets eliminated in the
  204. * TASK_UNINTERRUPTIBLE case.)
  205. */
  206. if (unlikely(signal_pending_state(state, task))) {
  207. mutex_remove_waiter(lock, &waiter,
  208. task_thread_info(task));
  209. mutex_release(&lock->dep_map, 1, ip);
  210. spin_unlock_mutex(&lock->wait_lock, flags);
  211. debug_mutex_free_waiter(&waiter);
  212. preempt_enable();
  213. return -EINTR;
  214. }
  215. __set_task_state(task, state);
  216. /* didn't get the lock, go to sleep: */
  217. spin_unlock_mutex(&lock->wait_lock, flags);
  218. schedule_preempt_disabled();
  219. spin_lock_mutex(&lock->wait_lock, flags);
  220. }
  221. done:
  222. lock_acquired(&lock->dep_map, ip);
  223. /* got the lock - rejoice! */
  224. mutex_remove_waiter(lock, &waiter, current_thread_info());
  225. mutex_set_owner(lock);
  226. /* set it to 0 if there are no waiters left: */
  227. if (likely(list_empty(&lock->wait_list)))
  228. atomic_set(&lock->count, 0);
  229. spin_unlock_mutex(&lock->wait_lock, flags);
  230. debug_mutex_free_waiter(&waiter);
  231. preempt_enable();
  232. return 0;
  233. }
  234. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  235. void __sched
  236. mutex_lock_nested(struct mutex *lock, unsigned int subclass)
  237. {
  238. might_sleep();
  239. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
  240. }
  241. EXPORT_SYMBOL_GPL(mutex_lock_nested);
  242. void __sched
  243. _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
  244. {
  245. might_sleep();
  246. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
  247. }
  248. EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
  249. int __sched
  250. mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
  251. {
  252. might_sleep();
  253. return __mutex_lock_common(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
  254. }
  255. EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
  256. int __sched
  257. mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
  258. {
  259. might_sleep();
  260. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
  261. subclass, NULL, _RET_IP_);
  262. }
  263. EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
  264. #endif
  265. /*
  266. * Release the lock, slowpath:
  267. */
  268. static inline void
  269. __mutex_unlock_common_slowpath(atomic_t *lock_count, int nested)
  270. {
  271. struct mutex *lock = container_of(lock_count, struct mutex, count);
  272. unsigned long flags;
  273. spin_lock_mutex(&lock->wait_lock, flags);
  274. mutex_release(&lock->dep_map, nested, _RET_IP_);
  275. debug_mutex_unlock(lock);
  276. /*
  277. * some architectures leave the lock unlocked in the fastpath failure
  278. * case, others need to leave it locked. In the later case we have to
  279. * unlock it here
  280. */
  281. if (__mutex_slowpath_needs_to_unlock())
  282. atomic_set(&lock->count, 1);
  283. if (!list_empty(&lock->wait_list)) {
  284. /* get the first entry from the wait-list: */
  285. struct mutex_waiter *waiter =
  286. list_entry(lock->wait_list.next,
  287. struct mutex_waiter, list);
  288. debug_mutex_wake_waiter(lock, waiter);
  289. wake_up_process(waiter->task);
  290. }
  291. spin_unlock_mutex(&lock->wait_lock, flags);
  292. }
  293. /*
  294. * Release the lock, slowpath:
  295. */
  296. static __used noinline void
  297. __mutex_unlock_slowpath(atomic_t *lock_count)
  298. {
  299. __mutex_unlock_common_slowpath(lock_count, 1);
  300. }
  301. #ifndef CONFIG_DEBUG_LOCK_ALLOC
  302. /*
  303. * Here come the less common (and hence less performance-critical) APIs:
  304. * mutex_lock_interruptible() and mutex_trylock().
  305. */
  306. static noinline int __sched
  307. __mutex_lock_killable_slowpath(atomic_t *lock_count);
  308. static noinline int __sched
  309. __mutex_lock_interruptible_slowpath(atomic_t *lock_count);
  310. /**
  311. * mutex_lock_interruptible - acquire the mutex, interruptible
  312. * @lock: the mutex to be acquired
  313. *
  314. * Lock the mutex like mutex_lock(), and return 0 if the mutex has
  315. * been acquired or sleep until the mutex becomes available. If a
  316. * signal arrives while waiting for the lock then this function
  317. * returns -EINTR.
  318. *
  319. * This function is similar to (but not equivalent to) down_interruptible().
  320. */
  321. int __sched mutex_lock_interruptible(struct mutex *lock)
  322. {
  323. int ret;
  324. might_sleep();
  325. ret = __mutex_fastpath_lock_retval
  326. (&lock->count, __mutex_lock_interruptible_slowpath);
  327. if (!ret)
  328. mutex_set_owner(lock);
  329. return ret;
  330. }
  331. EXPORT_SYMBOL(mutex_lock_interruptible);
  332. int __sched mutex_lock_killable(struct mutex *lock)
  333. {
  334. int ret;
  335. might_sleep();
  336. ret = __mutex_fastpath_lock_retval
  337. (&lock->count, __mutex_lock_killable_slowpath);
  338. if (!ret)
  339. mutex_set_owner(lock);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL(mutex_lock_killable);
  343. static __used noinline void __sched
  344. __mutex_lock_slowpath(atomic_t *lock_count)
  345. {
  346. struct mutex *lock = container_of(lock_count, struct mutex, count);
  347. __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
  348. }
  349. static noinline int __sched
  350. __mutex_lock_killable_slowpath(atomic_t *lock_count)
  351. {
  352. struct mutex *lock = container_of(lock_count, struct mutex, count);
  353. return __mutex_lock_common(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
  354. }
  355. static noinline int __sched
  356. __mutex_lock_interruptible_slowpath(atomic_t *lock_count)
  357. {
  358. struct mutex *lock = container_of(lock_count, struct mutex, count);
  359. return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
  360. }
  361. #endif
  362. /*
  363. * Spinlock based trylock, we take the spinlock and check whether we
  364. * can get the lock:
  365. */
  366. static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
  367. {
  368. struct mutex *lock = container_of(lock_count, struct mutex, count);
  369. unsigned long flags;
  370. int prev;
  371. spin_lock_mutex(&lock->wait_lock, flags);
  372. prev = atomic_xchg(&lock->count, -1);
  373. if (likely(prev == 1)) {
  374. mutex_set_owner(lock);
  375. mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
  376. }
  377. /* Set it back to 0 if there are no waiters: */
  378. if (likely(list_empty(&lock->wait_list)))
  379. atomic_set(&lock->count, 0);
  380. spin_unlock_mutex(&lock->wait_lock, flags);
  381. return prev == 1;
  382. }
  383. /**
  384. * mutex_trylock - try to acquire the mutex, without waiting
  385. * @lock: the mutex to be acquired
  386. *
  387. * Try to acquire the mutex atomically. Returns 1 if the mutex
  388. * has been acquired successfully, and 0 on contention.
  389. *
  390. * NOTE: this function follows the spin_trylock() convention, so
  391. * it is negated from the down_trylock() return values! Be careful
  392. * about this when converting semaphore users to mutexes.
  393. *
  394. * This function must not be used in interrupt context. The
  395. * mutex must be released by the same task that acquired it.
  396. */
  397. int __sched mutex_trylock(struct mutex *lock)
  398. {
  399. int ret;
  400. ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath);
  401. if (ret)
  402. mutex_set_owner(lock);
  403. return ret;
  404. }
  405. EXPORT_SYMBOL(mutex_trylock);
  406. /**
  407. * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
  408. * @cnt: the atomic which we are to dec
  409. * @lock: the mutex to return holding if we dec to 0
  410. *
  411. * return true and hold lock if we dec to 0, return false otherwise
  412. */
  413. int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
  414. {
  415. /* dec if we can't possibly hit 0 */
  416. if (atomic_add_unless(cnt, -1, 1))
  417. return 0;
  418. /* we might hit 0, so take the lock */
  419. mutex_lock(lock);
  420. if (!atomic_dec_and_test(cnt)) {
  421. /* when we actually did the dec, we didn't hit 0 */
  422. mutex_unlock(lock);
  423. return 0;
  424. }
  425. /* we hit 0, and we hold the lock */
  426. return 1;
  427. }
  428. EXPORT_SYMBOL(atomic_dec_and_mutex_lock);