rtmutex.c 25 KB

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