rtmutex.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. * RT-Mutexes: simple blocking mutual exclusion locks with PI support
  3. *
  4. * started by Ingo Molnar and Thomas Gleixner.
  5. *
  6. * Copyright (C) 2004-2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  7. * Copyright (C) 2005-2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  8. * Copyright (C) 2005 Kihon Technologies Inc., Steven Rostedt
  9. * Copyright (C) 2006 Esben Nielsen
  10. */
  11. #include <linux/spinlock.h>
  12. #include <linux/module.h>
  13. #include <linux/sched.h>
  14. #include <linux/timer.h>
  15. #include "rtmutex_common.h"
  16. #ifdef CONFIG_DEBUG_RT_MUTEXES
  17. # include "rtmutex-debug.h"
  18. #else
  19. # include "rtmutex.h"
  20. #endif
  21. /*
  22. * lock->owner state tracking:
  23. *
  24. * lock->owner holds the task_struct pointer of the owner. Bit 0 and 1
  25. * are used to keep track of the "owner is pending" and "lock has
  26. * waiters" state.
  27. *
  28. * owner bit1 bit0
  29. * NULL 0 0 lock is free (fast acquire possible)
  30. * NULL 0 1 invalid state
  31. * NULL 1 0 Transitional State*
  32. * NULL 1 1 invalid state
  33. * taskpointer 0 0 lock is held (fast release possible)
  34. * taskpointer 0 1 task is pending owner
  35. * taskpointer 1 0 lock is held and has waiters
  36. * taskpointer 1 1 task is pending owner and lock has more waiters
  37. *
  38. * Pending ownership is assigned to the top (highest priority)
  39. * waiter of the lock, when the lock is released. The thread is woken
  40. * up and can now take the lock. Until the lock is taken (bit 0
  41. * cleared) a competing higher priority thread can steal the lock
  42. * which puts the woken up thread back on the waiters list.
  43. *
  44. * The fast atomic compare exchange based acquire and release is only
  45. * possible when bit 0 and 1 of lock->owner are 0.
  46. *
  47. * (*) There's a small time where the owner can be NULL and the
  48. * "lock has waiters" bit is set. This can happen when grabbing the lock.
  49. * To prevent a cmpxchg of the owner releasing the lock, we need to set this
  50. * bit before looking at the lock, hence the reason this is a transitional
  51. * state.
  52. */
  53. static void
  54. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner,
  55. unsigned long mask)
  56. {
  57. unsigned long val = (unsigned long)owner | mask;
  58. if (rt_mutex_has_waiters(lock))
  59. val |= RT_MUTEX_HAS_WAITERS;
  60. lock->owner = (struct task_struct *)val;
  61. }
  62. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  63. {
  64. lock->owner = (struct task_struct *)
  65. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  66. }
  67. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  68. {
  69. if (!rt_mutex_has_waiters(lock))
  70. clear_rt_mutex_waiters(lock);
  71. }
  72. /*
  73. * We can speed up the acquire/release, if the architecture
  74. * supports cmpxchg and if there's no debugging state to be set up
  75. */
  76. #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES)
  77. # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c)
  78. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  79. {
  80. unsigned long owner, *p = (unsigned long *) &lock->owner;
  81. do {
  82. owner = *p;
  83. } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner);
  84. }
  85. #else
  86. # define rt_mutex_cmpxchg(l,c,n) (0)
  87. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  88. {
  89. lock->owner = (struct task_struct *)
  90. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  91. }
  92. #endif
  93. /*
  94. * Calculate task priority from the waiter list priority
  95. *
  96. * Return task->normal_prio when the waiter list is empty or when
  97. * the waiter is not allowed to do priority boosting
  98. */
  99. int rt_mutex_getprio(struct task_struct *task)
  100. {
  101. if (likely(!task_has_pi_waiters(task)))
  102. return task->normal_prio;
  103. return min(task_top_pi_waiter(task)->pi_list_entry.prio,
  104. task->normal_prio);
  105. }
  106. /*
  107. * Adjust the priority of a task, after its pi_waiters got modified.
  108. *
  109. * This can be both boosting and unboosting. task->pi_lock must be held.
  110. */
  111. static void __rt_mutex_adjust_prio(struct task_struct *task)
  112. {
  113. int prio = rt_mutex_getprio(task);
  114. if (task->prio != prio)
  115. rt_mutex_setprio(task, prio);
  116. }
  117. /*
  118. * Adjust task priority (undo boosting). Called from the exit path of
  119. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  120. *
  121. * (Note: We do this outside of the protection of lock->wait_lock to
  122. * allow the lock to be taken while or before we readjust the priority
  123. * of task. We do not use the spin_xx_mutex() variants here as we are
  124. * outside of the debug path.)
  125. */
  126. static void rt_mutex_adjust_prio(struct task_struct *task)
  127. {
  128. unsigned long flags;
  129. spin_lock_irqsave(&task->pi_lock, flags);
  130. __rt_mutex_adjust_prio(task);
  131. spin_unlock_irqrestore(&task->pi_lock, flags);
  132. }
  133. /*
  134. * Max number of times we'll walk the boosting chain:
  135. */
  136. int max_lock_depth = 1024;
  137. /*
  138. * Adjust the priority chain. Also used for deadlock detection.
  139. * Decreases task's usage by one - may thus free the task.
  140. * Returns 0 or -EDEADLK.
  141. */
  142. static int rt_mutex_adjust_prio_chain(task_t *task,
  143. int deadlock_detect,
  144. struct rt_mutex *orig_lock,
  145. struct rt_mutex_waiter *orig_waiter
  146. __IP_DECL__)
  147. {
  148. struct rt_mutex *lock;
  149. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  150. int detect_deadlock, ret = 0, depth = 0;
  151. unsigned long flags;
  152. detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
  153. deadlock_detect);
  154. /*
  155. * The (de)boosting is a step by step approach with a lot of
  156. * pitfalls. We want this to be preemptible and we want hold a
  157. * maximum of two locks per step. So we have to check
  158. * carefully whether things change under us.
  159. */
  160. again:
  161. if (++depth > max_lock_depth) {
  162. static int prev_max;
  163. /*
  164. * Print this only once. If the admin changes the limit,
  165. * print a new message when reaching the limit again.
  166. */
  167. if (prev_max != max_lock_depth) {
  168. prev_max = max_lock_depth;
  169. printk(KERN_WARNING "Maximum lock depth %d reached "
  170. "task: %s (%d)\n", max_lock_depth,
  171. current->comm, current->pid);
  172. }
  173. put_task_struct(task);
  174. return deadlock_detect ? -EDEADLK : 0;
  175. }
  176. retry:
  177. /*
  178. * Task can not go away as we did a get_task() before !
  179. */
  180. spin_lock_irqsave(&task->pi_lock, flags);
  181. waiter = task->pi_blocked_on;
  182. /*
  183. * Check whether the end of the boosting chain has been
  184. * reached or the state of the chain has changed while we
  185. * dropped the locks.
  186. */
  187. if (!waiter || !waiter->task)
  188. goto out_unlock_pi;
  189. if (top_waiter && (!task_has_pi_waiters(task) ||
  190. top_waiter != task_top_pi_waiter(task)))
  191. goto out_unlock_pi;
  192. /*
  193. * When deadlock detection is off then we check, if further
  194. * priority adjustment is necessary.
  195. */
  196. if (!detect_deadlock && waiter->list_entry.prio == task->prio)
  197. goto out_unlock_pi;
  198. lock = waiter->lock;
  199. if (!spin_trylock(&lock->wait_lock)) {
  200. spin_unlock_irqrestore(&task->pi_lock, flags);
  201. cpu_relax();
  202. goto retry;
  203. }
  204. /* Deadlock detection */
  205. if (lock == orig_lock || rt_mutex_owner(lock) == current) {
  206. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  207. spin_unlock(&lock->wait_lock);
  208. ret = deadlock_detect ? -EDEADLK : 0;
  209. goto out_unlock_pi;
  210. }
  211. top_waiter = rt_mutex_top_waiter(lock);
  212. /* Requeue the waiter */
  213. plist_del(&waiter->list_entry, &lock->wait_list);
  214. waiter->list_entry.prio = task->prio;
  215. plist_add(&waiter->list_entry, &lock->wait_list);
  216. /* Release the task */
  217. spin_unlock_irqrestore(&task->pi_lock, flags);
  218. put_task_struct(task);
  219. /* Grab the next task */
  220. task = rt_mutex_owner(lock);
  221. spin_lock_irqsave(&task->pi_lock, flags);
  222. if (waiter == rt_mutex_top_waiter(lock)) {
  223. /* Boost the owner */
  224. plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
  225. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  226. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  227. __rt_mutex_adjust_prio(task);
  228. } else if (top_waiter == waiter) {
  229. /* Deboost the owner */
  230. plist_del(&waiter->pi_list_entry, &task->pi_waiters);
  231. waiter = rt_mutex_top_waiter(lock);
  232. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  233. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  234. __rt_mutex_adjust_prio(task);
  235. }
  236. get_task_struct(task);
  237. spin_unlock_irqrestore(&task->pi_lock, flags);
  238. top_waiter = rt_mutex_top_waiter(lock);
  239. spin_unlock(&lock->wait_lock);
  240. if (!detect_deadlock && waiter != top_waiter)
  241. goto out_put_task;
  242. goto again;
  243. out_unlock_pi:
  244. spin_unlock_irqrestore(&task->pi_lock, flags);
  245. out_put_task:
  246. put_task_struct(task);
  247. return ret;
  248. }
  249. /*
  250. * Optimization: check if we can steal the lock from the
  251. * assigned pending owner [which might not have taken the
  252. * lock yet]:
  253. */
  254. static inline int try_to_steal_lock(struct rt_mutex *lock)
  255. {
  256. struct task_struct *pendowner = rt_mutex_owner(lock);
  257. struct rt_mutex_waiter *next;
  258. unsigned long flags;
  259. if (!rt_mutex_owner_pending(lock))
  260. return 0;
  261. if (pendowner == current)
  262. return 1;
  263. spin_lock_irqsave(&pendowner->pi_lock, flags);
  264. if (current->prio >= pendowner->prio) {
  265. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  266. return 0;
  267. }
  268. /*
  269. * Check if a waiter is enqueued on the pending owners
  270. * pi_waiters list. Remove it and readjust pending owners
  271. * priority.
  272. */
  273. if (likely(!rt_mutex_has_waiters(lock))) {
  274. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  275. return 1;
  276. }
  277. /* No chain handling, pending owner is not blocked on anything: */
  278. next = rt_mutex_top_waiter(lock);
  279. plist_del(&next->pi_list_entry, &pendowner->pi_waiters);
  280. __rt_mutex_adjust_prio(pendowner);
  281. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  282. /*
  283. * We are going to steal the lock and a waiter was
  284. * enqueued on the pending owners pi_waiters queue. So
  285. * we have to enqueue this waiter into
  286. * current->pi_waiters list. This covers the case,
  287. * where current is boosted because it holds another
  288. * lock and gets unboosted because the booster is
  289. * interrupted, so we would delay a waiter with higher
  290. * priority as current->normal_prio.
  291. *
  292. * Note: in the rare case of a SCHED_OTHER task changing
  293. * its priority and thus stealing the lock, next->task
  294. * might be current:
  295. */
  296. if (likely(next->task != current)) {
  297. spin_lock_irqsave(&current->pi_lock, flags);
  298. plist_add(&next->pi_list_entry, &current->pi_waiters);
  299. __rt_mutex_adjust_prio(current);
  300. spin_unlock_irqrestore(&current->pi_lock, flags);
  301. }
  302. return 1;
  303. }
  304. /*
  305. * Try to take an rt-mutex
  306. *
  307. * This fails
  308. * - when the lock has a real owner
  309. * - when a different pending owner exists and has higher priority than current
  310. *
  311. * Must be called with lock->wait_lock held.
  312. */
  313. static int try_to_take_rt_mutex(struct rt_mutex *lock __IP_DECL__)
  314. {
  315. /*
  316. * We have to be careful here if the atomic speedups are
  317. * enabled, such that, when
  318. * - no other waiter is on the lock
  319. * - the lock has been released since we did the cmpxchg
  320. * the lock can be released or taken while we are doing the
  321. * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
  322. *
  323. * The atomic acquire/release aware variant of
  324. * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
  325. * the WAITERS bit, the atomic release / acquire can not
  326. * happen anymore and lock->wait_lock protects us from the
  327. * non-atomic case.
  328. *
  329. * Note, that this might set lock->owner =
  330. * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
  331. * any more. This is fixed up when we take the ownership.
  332. * This is the transitional state explained at the top of this file.
  333. */
  334. mark_rt_mutex_waiters(lock);
  335. if (rt_mutex_owner(lock) && !try_to_steal_lock(lock))
  336. return 0;
  337. /* We got the lock. */
  338. debug_rt_mutex_lock(lock __IP__);
  339. rt_mutex_set_owner(lock, current, 0);
  340. rt_mutex_deadlock_account_lock(lock, current);
  341. return 1;
  342. }
  343. /*
  344. * Task blocks on lock.
  345. *
  346. * Prepare waiter and propagate pi chain
  347. *
  348. * This must be called with lock->wait_lock held.
  349. */
  350. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  351. struct rt_mutex_waiter *waiter,
  352. int detect_deadlock
  353. __IP_DECL__)
  354. {
  355. struct rt_mutex_waiter *top_waiter = waiter;
  356. task_t *owner = rt_mutex_owner(lock);
  357. int boost = 0, res;
  358. unsigned long flags;
  359. spin_lock_irqsave(&current->pi_lock, flags);
  360. __rt_mutex_adjust_prio(current);
  361. waiter->task = current;
  362. waiter->lock = lock;
  363. plist_node_init(&waiter->list_entry, current->prio);
  364. plist_node_init(&waiter->pi_list_entry, current->prio);
  365. /* Get the top priority waiter on the lock */
  366. if (rt_mutex_has_waiters(lock))
  367. top_waiter = rt_mutex_top_waiter(lock);
  368. plist_add(&waiter->list_entry, &lock->wait_list);
  369. current->pi_blocked_on = waiter;
  370. spin_unlock_irqrestore(&current->pi_lock, flags);
  371. if (waiter == rt_mutex_top_waiter(lock)) {
  372. spin_lock_irqsave(&owner->pi_lock, flags);
  373. plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
  374. plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
  375. __rt_mutex_adjust_prio(owner);
  376. if (owner->pi_blocked_on) {
  377. boost = 1;
  378. get_task_struct(owner);
  379. }
  380. spin_unlock_irqrestore(&owner->pi_lock, flags);
  381. }
  382. else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) {
  383. spin_lock_irqsave(&owner->pi_lock, flags);
  384. if (owner->pi_blocked_on) {
  385. boost = 1;
  386. get_task_struct(owner);
  387. }
  388. spin_unlock_irqrestore(&owner->pi_lock, flags);
  389. }
  390. if (!boost)
  391. return 0;
  392. spin_unlock(&lock->wait_lock);
  393. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock,
  394. waiter __IP__);
  395. spin_lock(&lock->wait_lock);
  396. return res;
  397. }
  398. /*
  399. * Wake up the next waiter on the lock.
  400. *
  401. * Remove the top waiter from the current tasks waiter list and from
  402. * the lock waiter list. Set it as pending owner. Then wake it up.
  403. *
  404. * Called with lock->wait_lock held.
  405. */
  406. static void wakeup_next_waiter(struct rt_mutex *lock)
  407. {
  408. struct rt_mutex_waiter *waiter;
  409. struct task_struct *pendowner;
  410. unsigned long flags;
  411. spin_lock_irqsave(&current->pi_lock, flags);
  412. waiter = rt_mutex_top_waiter(lock);
  413. plist_del(&waiter->list_entry, &lock->wait_list);
  414. /*
  415. * Remove it from current->pi_waiters. We do not adjust a
  416. * possible priority boost right now. We execute wakeup in the
  417. * boosted mode and go back to normal after releasing
  418. * lock->wait_lock.
  419. */
  420. plist_del(&waiter->pi_list_entry, &current->pi_waiters);
  421. pendowner = waiter->task;
  422. waiter->task = NULL;
  423. rt_mutex_set_owner(lock, pendowner, RT_MUTEX_OWNER_PENDING);
  424. spin_unlock_irqrestore(&current->pi_lock, flags);
  425. /*
  426. * Clear the pi_blocked_on variable and enqueue a possible
  427. * waiter into the pi_waiters list of the pending owner. This
  428. * prevents that in case the pending owner gets unboosted a
  429. * waiter with higher priority than pending-owner->normal_prio
  430. * is blocked on the unboosted (pending) owner.
  431. */
  432. spin_lock_irqsave(&pendowner->pi_lock, flags);
  433. WARN_ON(!pendowner->pi_blocked_on);
  434. WARN_ON(pendowner->pi_blocked_on != waiter);
  435. WARN_ON(pendowner->pi_blocked_on->lock != lock);
  436. pendowner->pi_blocked_on = NULL;
  437. if (rt_mutex_has_waiters(lock)) {
  438. struct rt_mutex_waiter *next;
  439. next = rt_mutex_top_waiter(lock);
  440. plist_add(&next->pi_list_entry, &pendowner->pi_waiters);
  441. }
  442. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  443. wake_up_process(pendowner);
  444. }
  445. /*
  446. * Remove a waiter from a lock
  447. *
  448. * Must be called with lock->wait_lock held
  449. */
  450. static void remove_waiter(struct rt_mutex *lock,
  451. struct rt_mutex_waiter *waiter __IP_DECL__)
  452. {
  453. int first = (waiter == rt_mutex_top_waiter(lock));
  454. int boost = 0;
  455. task_t *owner = rt_mutex_owner(lock);
  456. unsigned long flags;
  457. spin_lock_irqsave(&current->pi_lock, flags);
  458. plist_del(&waiter->list_entry, &lock->wait_list);
  459. waiter->task = NULL;
  460. current->pi_blocked_on = NULL;
  461. spin_unlock_irqrestore(&current->pi_lock, flags);
  462. if (first && owner != current) {
  463. spin_lock_irqsave(&owner->pi_lock, flags);
  464. plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
  465. if (rt_mutex_has_waiters(lock)) {
  466. struct rt_mutex_waiter *next;
  467. next = rt_mutex_top_waiter(lock);
  468. plist_add(&next->pi_list_entry, &owner->pi_waiters);
  469. }
  470. __rt_mutex_adjust_prio(owner);
  471. if (owner->pi_blocked_on) {
  472. boost = 1;
  473. get_task_struct(owner);
  474. }
  475. spin_unlock_irqrestore(&owner->pi_lock, flags);
  476. }
  477. WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  478. if (!boost)
  479. return;
  480. spin_unlock(&lock->wait_lock);
  481. rt_mutex_adjust_prio_chain(owner, 0, lock, NULL __IP__);
  482. spin_lock(&lock->wait_lock);
  483. }
  484. /*
  485. * Slow path lock function:
  486. */
  487. static int __sched
  488. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  489. struct hrtimer_sleeper *timeout,
  490. int detect_deadlock __IP_DECL__)
  491. {
  492. struct rt_mutex_waiter waiter;
  493. int ret = 0;
  494. debug_rt_mutex_init_waiter(&waiter);
  495. waiter.task = NULL;
  496. spin_lock(&lock->wait_lock);
  497. /* Try to acquire the lock again: */
  498. if (try_to_take_rt_mutex(lock __IP__)) {
  499. spin_unlock(&lock->wait_lock);
  500. return 0;
  501. }
  502. set_current_state(state);
  503. /* Setup the timer, when timeout != NULL */
  504. if (unlikely(timeout))
  505. hrtimer_start(&timeout->timer, timeout->timer.expires,
  506. HRTIMER_ABS);
  507. for (;;) {
  508. /* Try to acquire the lock: */
  509. if (try_to_take_rt_mutex(lock __IP__))
  510. break;
  511. /*
  512. * TASK_INTERRUPTIBLE checks for signals and
  513. * timeout. Ignored otherwise.
  514. */
  515. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  516. /* Signal pending? */
  517. if (signal_pending(current))
  518. ret = -EINTR;
  519. if (timeout && !timeout->task)
  520. ret = -ETIMEDOUT;
  521. if (ret)
  522. break;
  523. }
  524. /*
  525. * waiter.task is NULL the first time we come here and
  526. * when we have been woken up by the previous owner
  527. * but the lock got stolen by a higher prio task.
  528. */
  529. if (!waiter.task) {
  530. ret = task_blocks_on_rt_mutex(lock, &waiter,
  531. detect_deadlock __IP__);
  532. /*
  533. * If we got woken up by the owner then start loop
  534. * all over without going into schedule to try
  535. * to get the lock now:
  536. */
  537. if (unlikely(!waiter.task))
  538. continue;
  539. if (unlikely(ret))
  540. break;
  541. }
  542. spin_unlock(&lock->wait_lock);
  543. debug_rt_mutex_print_deadlock(&waiter);
  544. if (waiter.task)
  545. schedule_rt_mutex(lock);
  546. spin_lock(&lock->wait_lock);
  547. set_current_state(state);
  548. }
  549. set_current_state(TASK_RUNNING);
  550. if (unlikely(waiter.task))
  551. remove_waiter(lock, &waiter __IP__);
  552. /*
  553. * try_to_take_rt_mutex() sets the waiter bit
  554. * unconditionally. We might have to fix that up.
  555. */
  556. fixup_rt_mutex_waiters(lock);
  557. spin_unlock(&lock->wait_lock);
  558. /* Remove pending timer: */
  559. if (unlikely(timeout))
  560. hrtimer_cancel(&timeout->timer);
  561. /*
  562. * Readjust priority, when we did not get the lock. We might
  563. * have been the pending owner and boosted. Since we did not
  564. * take the lock, the PI boost has to go.
  565. */
  566. if (unlikely(ret))
  567. rt_mutex_adjust_prio(current);
  568. debug_rt_mutex_free_waiter(&waiter);
  569. return ret;
  570. }
  571. /*
  572. * Slow path try-lock function:
  573. */
  574. static inline int
  575. rt_mutex_slowtrylock(struct rt_mutex *lock __IP_DECL__)
  576. {
  577. int ret = 0;
  578. spin_lock(&lock->wait_lock);
  579. if (likely(rt_mutex_owner(lock) != current)) {
  580. ret = try_to_take_rt_mutex(lock __IP__);
  581. /*
  582. * try_to_take_rt_mutex() sets the lock waiters
  583. * bit unconditionally. Clean this up.
  584. */
  585. fixup_rt_mutex_waiters(lock);
  586. }
  587. spin_unlock(&lock->wait_lock);
  588. return ret;
  589. }
  590. /*
  591. * Slow path to release a rt-mutex:
  592. */
  593. static void __sched
  594. rt_mutex_slowunlock(struct rt_mutex *lock)
  595. {
  596. spin_lock(&lock->wait_lock);
  597. debug_rt_mutex_unlock(lock);
  598. rt_mutex_deadlock_account_unlock(current);
  599. if (!rt_mutex_has_waiters(lock)) {
  600. lock->owner = NULL;
  601. spin_unlock(&lock->wait_lock);
  602. return;
  603. }
  604. wakeup_next_waiter(lock);
  605. spin_unlock(&lock->wait_lock);
  606. /* Undo pi boosting if necessary: */
  607. rt_mutex_adjust_prio(current);
  608. }
  609. /*
  610. * debug aware fast / slowpath lock,trylock,unlock
  611. *
  612. * The atomic acquire/release ops are compiled away, when either the
  613. * architecture does not support cmpxchg or when debugging is enabled.
  614. */
  615. static inline int
  616. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  617. int detect_deadlock,
  618. int (*slowfn)(struct rt_mutex *lock, int state,
  619. struct hrtimer_sleeper *timeout,
  620. int detect_deadlock __IP_DECL__))
  621. {
  622. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  623. rt_mutex_deadlock_account_lock(lock, current);
  624. return 0;
  625. } else
  626. return slowfn(lock, state, NULL, detect_deadlock __RET_IP__);
  627. }
  628. static inline int
  629. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  630. struct hrtimer_sleeper *timeout, int detect_deadlock,
  631. int (*slowfn)(struct rt_mutex *lock, int state,
  632. struct hrtimer_sleeper *timeout,
  633. int detect_deadlock __IP_DECL__))
  634. {
  635. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  636. rt_mutex_deadlock_account_lock(lock, current);
  637. return 0;
  638. } else
  639. return slowfn(lock, state, timeout, detect_deadlock __RET_IP__);
  640. }
  641. static inline int
  642. rt_mutex_fasttrylock(struct rt_mutex *lock,
  643. int (*slowfn)(struct rt_mutex *lock __IP_DECL__))
  644. {
  645. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  646. rt_mutex_deadlock_account_lock(lock, current);
  647. return 1;
  648. }
  649. return slowfn(lock __RET_IP__);
  650. }
  651. static inline void
  652. rt_mutex_fastunlock(struct rt_mutex *lock,
  653. void (*slowfn)(struct rt_mutex *lock))
  654. {
  655. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  656. rt_mutex_deadlock_account_unlock(current);
  657. else
  658. slowfn(lock);
  659. }
  660. /**
  661. * rt_mutex_lock - lock a rt_mutex
  662. *
  663. * @lock: the rt_mutex to be locked
  664. */
  665. void __sched rt_mutex_lock(struct rt_mutex *lock)
  666. {
  667. might_sleep();
  668. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  669. }
  670. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  671. /**
  672. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  673. *
  674. * @lock: the rt_mutex to be locked
  675. * @detect_deadlock: deadlock detection on/off
  676. *
  677. * Returns:
  678. * 0 on success
  679. * -EINTR when interrupted by a signal
  680. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  681. */
  682. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  683. int detect_deadlock)
  684. {
  685. might_sleep();
  686. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  687. detect_deadlock, rt_mutex_slowlock);
  688. }
  689. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  690. /**
  691. * rt_mutex_lock_interruptible_ktime - lock a rt_mutex interruptible
  692. * the timeout structure is provided
  693. * by the caller
  694. *
  695. * @lock: the rt_mutex to be locked
  696. * @timeout: timeout structure or NULL (no timeout)
  697. * @detect_deadlock: deadlock detection on/off
  698. *
  699. * Returns:
  700. * 0 on success
  701. * -EINTR when interrupted by a signal
  702. * -ETIMEOUT when the timeout expired
  703. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  704. */
  705. int
  706. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  707. int detect_deadlock)
  708. {
  709. might_sleep();
  710. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  711. detect_deadlock, rt_mutex_slowlock);
  712. }
  713. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  714. /**
  715. * rt_mutex_trylock - try to lock a rt_mutex
  716. *
  717. * @lock: the rt_mutex to be locked
  718. *
  719. * Returns 1 on success and 0 on contention
  720. */
  721. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  722. {
  723. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  724. }
  725. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  726. /**
  727. * rt_mutex_unlock - unlock a rt_mutex
  728. *
  729. * @lock: the rt_mutex to be unlocked
  730. */
  731. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  732. {
  733. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  734. }
  735. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  736. /***
  737. * rt_mutex_destroy - mark a mutex unusable
  738. * @lock: the mutex to be destroyed
  739. *
  740. * This function marks the mutex uninitialized, and any subsequent
  741. * use of the mutex is forbidden. The mutex must not be locked when
  742. * this function is called.
  743. */
  744. void rt_mutex_destroy(struct rt_mutex *lock)
  745. {
  746. WARN_ON(rt_mutex_is_locked(lock));
  747. #ifdef CONFIG_DEBUG_RT_MUTEXES
  748. lock->magic = NULL;
  749. #endif
  750. }
  751. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  752. /**
  753. * __rt_mutex_init - initialize the rt lock
  754. *
  755. * @lock: the rt lock to be initialized
  756. *
  757. * Initialize the rt lock to unlocked state.
  758. *
  759. * Initializing of a locked rt lock is not allowed
  760. */
  761. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  762. {
  763. lock->owner = NULL;
  764. spin_lock_init(&lock->wait_lock);
  765. plist_head_init(&lock->wait_list, &lock->wait_lock);
  766. debug_rt_mutex_init(lock, name);
  767. }
  768. EXPORT_SYMBOL_GPL(__rt_mutex_init);