mutex.c 13 KB

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