rtmutex.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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. /*
  171. * Check the orig_waiter state. After we dropped the locks,
  172. * the previous owner of the lock might have released the lock
  173. * and made us the pending owner:
  174. */
  175. if (orig_waiter && !orig_waiter->task)
  176. goto out_unlock_pi;
  177. /*
  178. * Drop out, when the task has no waiters. Note,
  179. * top_waiter can be NULL, when we are in the deboosting
  180. * mode!
  181. */
  182. if (top_waiter && (!task_has_pi_waiters(task) ||
  183. top_waiter != task_top_pi_waiter(task)))
  184. goto out_unlock_pi;
  185. /*
  186. * When deadlock detection is off then we check, if further
  187. * priority adjustment is necessary.
  188. */
  189. if (!detect_deadlock && waiter->list_entry.prio == task->prio)
  190. goto out_unlock_pi;
  191. lock = waiter->lock;
  192. if (!spin_trylock(&lock->wait_lock)) {
  193. spin_unlock_irqrestore(&task->pi_lock, flags);
  194. cpu_relax();
  195. goto retry;
  196. }
  197. /* Deadlock detection */
  198. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  199. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  200. spin_unlock(&lock->wait_lock);
  201. ret = deadlock_detect ? -EDEADLK : 0;
  202. goto out_unlock_pi;
  203. }
  204. top_waiter = rt_mutex_top_waiter(lock);
  205. /* Requeue the waiter */
  206. plist_del(&waiter->list_entry, &lock->wait_list);
  207. waiter->list_entry.prio = task->prio;
  208. plist_add(&waiter->list_entry, &lock->wait_list);
  209. /* Release the task */
  210. spin_unlock_irqrestore(&task->pi_lock, flags);
  211. put_task_struct(task);
  212. /* Grab the next task */
  213. task = rt_mutex_owner(lock);
  214. get_task_struct(task);
  215. spin_lock_irqsave(&task->pi_lock, flags);
  216. if (waiter == rt_mutex_top_waiter(lock)) {
  217. /* Boost the owner */
  218. plist_del(&top_waiter->pi_list_entry, &task->pi_waiters);
  219. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  220. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  221. __rt_mutex_adjust_prio(task);
  222. } else if (top_waiter == waiter) {
  223. /* Deboost the owner */
  224. plist_del(&waiter->pi_list_entry, &task->pi_waiters);
  225. waiter = rt_mutex_top_waiter(lock);
  226. waiter->pi_list_entry.prio = waiter->list_entry.prio;
  227. plist_add(&waiter->pi_list_entry, &task->pi_waiters);
  228. __rt_mutex_adjust_prio(task);
  229. }
  230. spin_unlock_irqrestore(&task->pi_lock, flags);
  231. top_waiter = rt_mutex_top_waiter(lock);
  232. spin_unlock(&lock->wait_lock);
  233. if (!detect_deadlock && waiter != top_waiter)
  234. goto out_put_task;
  235. goto again;
  236. out_unlock_pi:
  237. spin_unlock_irqrestore(&task->pi_lock, flags);
  238. out_put_task:
  239. put_task_struct(task);
  240. return ret;
  241. }
  242. /*
  243. * Optimization: check if we can steal the lock from the
  244. * assigned pending owner [which might not have taken the
  245. * lock yet]:
  246. */
  247. static inline int try_to_steal_lock(struct rt_mutex *lock)
  248. {
  249. struct task_struct *pendowner = rt_mutex_owner(lock);
  250. struct rt_mutex_waiter *next;
  251. unsigned long flags;
  252. if (!rt_mutex_owner_pending(lock))
  253. return 0;
  254. if (pendowner == current)
  255. return 1;
  256. spin_lock_irqsave(&pendowner->pi_lock, flags);
  257. if (current->prio >= pendowner->prio) {
  258. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  259. return 0;
  260. }
  261. /*
  262. * Check if a waiter is enqueued on the pending owners
  263. * pi_waiters list. Remove it and readjust pending owners
  264. * priority.
  265. */
  266. if (likely(!rt_mutex_has_waiters(lock))) {
  267. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  268. return 1;
  269. }
  270. /* No chain handling, pending owner is not blocked on anything: */
  271. next = rt_mutex_top_waiter(lock);
  272. plist_del(&next->pi_list_entry, &pendowner->pi_waiters);
  273. __rt_mutex_adjust_prio(pendowner);
  274. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  275. /*
  276. * We are going to steal the lock and a waiter was
  277. * enqueued on the pending owners pi_waiters queue. So
  278. * we have to enqueue this waiter into
  279. * current->pi_waiters list. This covers the case,
  280. * where current is boosted because it holds another
  281. * lock and gets unboosted because the booster is
  282. * interrupted, so we would delay a waiter with higher
  283. * priority as current->normal_prio.
  284. *
  285. * Note: in the rare case of a SCHED_OTHER task changing
  286. * its priority and thus stealing the lock, next->task
  287. * might be current:
  288. */
  289. if (likely(next->task != current)) {
  290. spin_lock_irqsave(&current->pi_lock, flags);
  291. plist_add(&next->pi_list_entry, &current->pi_waiters);
  292. __rt_mutex_adjust_prio(current);
  293. spin_unlock_irqrestore(&current->pi_lock, flags);
  294. }
  295. return 1;
  296. }
  297. /*
  298. * Try to take an rt-mutex
  299. *
  300. * This fails
  301. * - when the lock has a real owner
  302. * - when a different pending owner exists and has higher priority than current
  303. *
  304. * Must be called with lock->wait_lock held.
  305. */
  306. static int try_to_take_rt_mutex(struct rt_mutex *lock)
  307. {
  308. /*
  309. * We have to be careful here if the atomic speedups are
  310. * enabled, such that, when
  311. * - no other waiter is on the lock
  312. * - the lock has been released since we did the cmpxchg
  313. * the lock can be released or taken while we are doing the
  314. * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
  315. *
  316. * The atomic acquire/release aware variant of
  317. * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
  318. * the WAITERS bit, the atomic release / acquire can not
  319. * happen anymore and lock->wait_lock protects us from the
  320. * non-atomic case.
  321. *
  322. * Note, that this might set lock->owner =
  323. * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
  324. * any more. This is fixed up when we take the ownership.
  325. * This is the transitional state explained at the top of this file.
  326. */
  327. mark_rt_mutex_waiters(lock);
  328. if (rt_mutex_owner(lock) && !try_to_steal_lock(lock))
  329. return 0;
  330. /* We got the lock. */
  331. debug_rt_mutex_lock(lock);
  332. rt_mutex_set_owner(lock, current, 0);
  333. rt_mutex_deadlock_account_lock(lock, current);
  334. return 1;
  335. }
  336. /*
  337. * Task blocks on lock.
  338. *
  339. * Prepare waiter and propagate pi chain
  340. *
  341. * This must be called with lock->wait_lock held.
  342. */
  343. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  344. struct rt_mutex_waiter *waiter,
  345. int detect_deadlock)
  346. {
  347. struct task_struct *owner = rt_mutex_owner(lock);
  348. struct rt_mutex_waiter *top_waiter = waiter;
  349. unsigned long flags;
  350. int chain_walk = 0, res;
  351. spin_lock_irqsave(&current->pi_lock, flags);
  352. __rt_mutex_adjust_prio(current);
  353. waiter->task = current;
  354. waiter->lock = lock;
  355. plist_node_init(&waiter->list_entry, current->prio);
  356. plist_node_init(&waiter->pi_list_entry, current->prio);
  357. /* Get the top priority waiter on the lock */
  358. if (rt_mutex_has_waiters(lock))
  359. top_waiter = rt_mutex_top_waiter(lock);
  360. plist_add(&waiter->list_entry, &lock->wait_list);
  361. current->pi_blocked_on = waiter;
  362. spin_unlock_irqrestore(&current->pi_lock, flags);
  363. if (waiter == rt_mutex_top_waiter(lock)) {
  364. spin_lock_irqsave(&owner->pi_lock, flags);
  365. plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
  366. plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
  367. __rt_mutex_adjust_prio(owner);
  368. if (owner->pi_blocked_on)
  369. chain_walk = 1;
  370. spin_unlock_irqrestore(&owner->pi_lock, flags);
  371. }
  372. else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
  373. chain_walk = 1;
  374. if (!chain_walk)
  375. return 0;
  376. /*
  377. * The owner can't disappear while holding a lock,
  378. * so the owner struct is protected by wait_lock.
  379. * Gets dropped in rt_mutex_adjust_prio_chain()!
  380. */
  381. get_task_struct(owner);
  382. spin_unlock(&lock->wait_lock);
  383. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
  384. current);
  385. spin_lock(&lock->wait_lock);
  386. return res;
  387. }
  388. /*
  389. * Wake up the next waiter on the lock.
  390. *
  391. * Remove the top waiter from the current tasks waiter list and from
  392. * the lock waiter list. Set it as pending owner. Then wake it up.
  393. *
  394. * Called with lock->wait_lock held.
  395. */
  396. static void wakeup_next_waiter(struct rt_mutex *lock)
  397. {
  398. struct rt_mutex_waiter *waiter;
  399. struct task_struct *pendowner;
  400. unsigned long flags;
  401. spin_lock_irqsave(&current->pi_lock, flags);
  402. waiter = rt_mutex_top_waiter(lock);
  403. plist_del(&waiter->list_entry, &lock->wait_list);
  404. /*
  405. * Remove it from current->pi_waiters. We do not adjust a
  406. * possible priority boost right now. We execute wakeup in the
  407. * boosted mode and go back to normal after releasing
  408. * lock->wait_lock.
  409. */
  410. plist_del(&waiter->pi_list_entry, &current->pi_waiters);
  411. pendowner = waiter->task;
  412. waiter->task = NULL;
  413. rt_mutex_set_owner(lock, pendowner, RT_MUTEX_OWNER_PENDING);
  414. spin_unlock_irqrestore(&current->pi_lock, flags);
  415. /*
  416. * Clear the pi_blocked_on variable and enqueue a possible
  417. * waiter into the pi_waiters list of the pending owner. This
  418. * prevents that in case the pending owner gets unboosted a
  419. * waiter with higher priority than pending-owner->normal_prio
  420. * is blocked on the unboosted (pending) owner.
  421. */
  422. spin_lock_irqsave(&pendowner->pi_lock, flags);
  423. WARN_ON(!pendowner->pi_blocked_on);
  424. WARN_ON(pendowner->pi_blocked_on != waiter);
  425. WARN_ON(pendowner->pi_blocked_on->lock != lock);
  426. pendowner->pi_blocked_on = NULL;
  427. if (rt_mutex_has_waiters(lock)) {
  428. struct rt_mutex_waiter *next;
  429. next = rt_mutex_top_waiter(lock);
  430. plist_add(&next->pi_list_entry, &pendowner->pi_waiters);
  431. }
  432. spin_unlock_irqrestore(&pendowner->pi_lock, flags);
  433. wake_up_process(pendowner);
  434. }
  435. /*
  436. * Remove a waiter from a lock
  437. *
  438. * Must be called with lock->wait_lock held
  439. */
  440. void remove_waiter(struct rt_mutex *lock,
  441. struct rt_mutex_waiter *waiter)
  442. {
  443. int first = (waiter == rt_mutex_top_waiter(lock));
  444. struct task_struct *owner = rt_mutex_owner(lock);
  445. unsigned long flags;
  446. int chain_walk = 0;
  447. spin_lock_irqsave(&current->pi_lock, flags);
  448. plist_del(&waiter->list_entry, &lock->wait_list);
  449. waiter->task = NULL;
  450. current->pi_blocked_on = NULL;
  451. spin_unlock_irqrestore(&current->pi_lock, flags);
  452. if (first && owner != current) {
  453. spin_lock_irqsave(&owner->pi_lock, flags);
  454. plist_del(&waiter->pi_list_entry, &owner->pi_waiters);
  455. if (rt_mutex_has_waiters(lock)) {
  456. struct rt_mutex_waiter *next;
  457. next = rt_mutex_top_waiter(lock);
  458. plist_add(&next->pi_list_entry, &owner->pi_waiters);
  459. }
  460. __rt_mutex_adjust_prio(owner);
  461. if (owner->pi_blocked_on)
  462. chain_walk = 1;
  463. spin_unlock_irqrestore(&owner->pi_lock, flags);
  464. }
  465. WARN_ON(!plist_node_empty(&waiter->pi_list_entry));
  466. if (!chain_walk)
  467. return;
  468. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  469. get_task_struct(owner);
  470. spin_unlock(&lock->wait_lock);
  471. rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
  472. spin_lock(&lock->wait_lock);
  473. }
  474. /*
  475. * Recheck the pi chain, in case we got a priority setting
  476. *
  477. * Called from sched_setscheduler
  478. */
  479. void rt_mutex_adjust_pi(struct task_struct *task)
  480. {
  481. struct rt_mutex_waiter *waiter;
  482. unsigned long flags;
  483. spin_lock_irqsave(&task->pi_lock, flags);
  484. waiter = task->pi_blocked_on;
  485. if (!waiter || waiter->list_entry.prio == task->prio) {
  486. spin_unlock_irqrestore(&task->pi_lock, flags);
  487. return;
  488. }
  489. spin_unlock_irqrestore(&task->pi_lock, flags);
  490. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  491. get_task_struct(task);
  492. rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
  493. }
  494. /*
  495. * Slow path lock function:
  496. */
  497. static int __sched
  498. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  499. struct hrtimer_sleeper *timeout,
  500. int detect_deadlock)
  501. {
  502. struct rt_mutex_waiter waiter;
  503. int ret = 0;
  504. debug_rt_mutex_init_waiter(&waiter);
  505. waiter.task = NULL;
  506. spin_lock(&lock->wait_lock);
  507. /* Try to acquire the lock again: */
  508. if (try_to_take_rt_mutex(lock)) {
  509. spin_unlock(&lock->wait_lock);
  510. return 0;
  511. }
  512. set_current_state(state);
  513. /* Setup the timer, when timeout != NULL */
  514. if (unlikely(timeout))
  515. hrtimer_start(&timeout->timer, timeout->timer.expires,
  516. HRTIMER_MODE_ABS);
  517. for (;;) {
  518. /* Try to acquire the lock: */
  519. if (try_to_take_rt_mutex(lock))
  520. break;
  521. /*
  522. * TASK_INTERRUPTIBLE checks for signals and
  523. * timeout. Ignored otherwise.
  524. */
  525. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  526. /* Signal pending? */
  527. if (signal_pending(current))
  528. ret = -EINTR;
  529. if (timeout && !timeout->task)
  530. ret = -ETIMEDOUT;
  531. if (ret)
  532. break;
  533. }
  534. /*
  535. * waiter.task is NULL the first time we come here and
  536. * when we have been woken up by the previous owner
  537. * but the lock got stolen by a higher prio task.
  538. */
  539. if (!waiter.task) {
  540. ret = task_blocks_on_rt_mutex(lock, &waiter,
  541. detect_deadlock);
  542. /*
  543. * If we got woken up by the owner then start loop
  544. * all over without going into schedule to try
  545. * to get the lock now:
  546. */
  547. if (unlikely(!waiter.task)) {
  548. /*
  549. * Reset the return value. We might
  550. * have returned with -EDEADLK and the
  551. * owner released the lock while we
  552. * were walking the pi chain.
  553. */
  554. ret = 0;
  555. continue;
  556. }
  557. if (unlikely(ret))
  558. break;
  559. }
  560. spin_unlock(&lock->wait_lock);
  561. debug_rt_mutex_print_deadlock(&waiter);
  562. if (waiter.task)
  563. schedule_rt_mutex(lock);
  564. spin_lock(&lock->wait_lock);
  565. set_current_state(state);
  566. }
  567. set_current_state(TASK_RUNNING);
  568. if (unlikely(waiter.task))
  569. remove_waiter(lock, &waiter);
  570. /*
  571. * try_to_take_rt_mutex() sets the waiter bit
  572. * unconditionally. We might have to fix that up.
  573. */
  574. fixup_rt_mutex_waiters(lock);
  575. spin_unlock(&lock->wait_lock);
  576. /* Remove pending timer: */
  577. if (unlikely(timeout))
  578. hrtimer_cancel(&timeout->timer);
  579. /*
  580. * Readjust priority, when we did not get the lock. We might
  581. * have been the pending owner and boosted. Since we did not
  582. * take the lock, the PI boost has to go.
  583. */
  584. if (unlikely(ret))
  585. rt_mutex_adjust_prio(current);
  586. debug_rt_mutex_free_waiter(&waiter);
  587. return ret;
  588. }
  589. /*
  590. * Slow path try-lock function:
  591. */
  592. static inline int
  593. rt_mutex_slowtrylock(struct rt_mutex *lock)
  594. {
  595. int ret = 0;
  596. spin_lock(&lock->wait_lock);
  597. if (likely(rt_mutex_owner(lock) != current)) {
  598. ret = try_to_take_rt_mutex(lock);
  599. /*
  600. * try_to_take_rt_mutex() sets the lock waiters
  601. * bit unconditionally. Clean this up.
  602. */
  603. fixup_rt_mutex_waiters(lock);
  604. }
  605. spin_unlock(&lock->wait_lock);
  606. return ret;
  607. }
  608. /*
  609. * Slow path to release a rt-mutex:
  610. */
  611. static void __sched
  612. rt_mutex_slowunlock(struct rt_mutex *lock)
  613. {
  614. spin_lock(&lock->wait_lock);
  615. debug_rt_mutex_unlock(lock);
  616. rt_mutex_deadlock_account_unlock(current);
  617. if (!rt_mutex_has_waiters(lock)) {
  618. lock->owner = NULL;
  619. spin_unlock(&lock->wait_lock);
  620. return;
  621. }
  622. wakeup_next_waiter(lock);
  623. spin_unlock(&lock->wait_lock);
  624. /* Undo pi boosting if necessary: */
  625. rt_mutex_adjust_prio(current);
  626. }
  627. /*
  628. * debug aware fast / slowpath lock,trylock,unlock
  629. *
  630. * The atomic acquire/release ops are compiled away, when either the
  631. * architecture does not support cmpxchg or when debugging is enabled.
  632. */
  633. static inline int
  634. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  635. int detect_deadlock,
  636. int (*slowfn)(struct rt_mutex *lock, int state,
  637. struct hrtimer_sleeper *timeout,
  638. int detect_deadlock))
  639. {
  640. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  641. rt_mutex_deadlock_account_lock(lock, current);
  642. return 0;
  643. } else
  644. return slowfn(lock, state, NULL, detect_deadlock);
  645. }
  646. static inline int
  647. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  648. struct hrtimer_sleeper *timeout, int detect_deadlock,
  649. int (*slowfn)(struct rt_mutex *lock, int state,
  650. struct hrtimer_sleeper *timeout,
  651. int detect_deadlock))
  652. {
  653. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  654. rt_mutex_deadlock_account_lock(lock, current);
  655. return 0;
  656. } else
  657. return slowfn(lock, state, timeout, detect_deadlock);
  658. }
  659. static inline int
  660. rt_mutex_fasttrylock(struct rt_mutex *lock,
  661. int (*slowfn)(struct rt_mutex *lock))
  662. {
  663. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  664. rt_mutex_deadlock_account_lock(lock, current);
  665. return 1;
  666. }
  667. return slowfn(lock);
  668. }
  669. static inline void
  670. rt_mutex_fastunlock(struct rt_mutex *lock,
  671. void (*slowfn)(struct rt_mutex *lock))
  672. {
  673. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  674. rt_mutex_deadlock_account_unlock(current);
  675. else
  676. slowfn(lock);
  677. }
  678. /**
  679. * rt_mutex_lock - lock a rt_mutex
  680. *
  681. * @lock: the rt_mutex to be locked
  682. */
  683. void __sched rt_mutex_lock(struct rt_mutex *lock)
  684. {
  685. might_sleep();
  686. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  687. }
  688. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  689. /**
  690. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  691. *
  692. * @lock: the rt_mutex to be locked
  693. * @detect_deadlock: deadlock detection on/off
  694. *
  695. * Returns:
  696. * 0 on success
  697. * -EINTR when interrupted by a signal
  698. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  699. */
  700. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  701. int detect_deadlock)
  702. {
  703. might_sleep();
  704. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  705. detect_deadlock, rt_mutex_slowlock);
  706. }
  707. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  708. /**
  709. * rt_mutex_lock_interruptible_ktime - lock a rt_mutex interruptible
  710. * the timeout structure is provided
  711. * by the caller
  712. *
  713. * @lock: the rt_mutex to be locked
  714. * @timeout: timeout structure or NULL (no timeout)
  715. * @detect_deadlock: deadlock detection on/off
  716. *
  717. * Returns:
  718. * 0 on success
  719. * -EINTR when interrupted by a signal
  720. * -ETIMEOUT when the timeout expired
  721. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  722. */
  723. int
  724. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  725. int detect_deadlock)
  726. {
  727. might_sleep();
  728. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  729. detect_deadlock, rt_mutex_slowlock);
  730. }
  731. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  732. /**
  733. * rt_mutex_trylock - try to lock a rt_mutex
  734. *
  735. * @lock: the rt_mutex to be locked
  736. *
  737. * Returns 1 on success and 0 on contention
  738. */
  739. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  740. {
  741. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  742. }
  743. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  744. /**
  745. * rt_mutex_unlock - unlock a rt_mutex
  746. *
  747. * @lock: the rt_mutex to be unlocked
  748. */
  749. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  750. {
  751. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  752. }
  753. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  754. /***
  755. * rt_mutex_destroy - mark a mutex unusable
  756. * @lock: the mutex to be destroyed
  757. *
  758. * This function marks the mutex uninitialized, and any subsequent
  759. * use of the mutex is forbidden. The mutex must not be locked when
  760. * this function is called.
  761. */
  762. void rt_mutex_destroy(struct rt_mutex *lock)
  763. {
  764. WARN_ON(rt_mutex_is_locked(lock));
  765. #ifdef CONFIG_DEBUG_RT_MUTEXES
  766. lock->magic = NULL;
  767. #endif
  768. }
  769. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  770. /**
  771. * __rt_mutex_init - initialize the rt lock
  772. *
  773. * @lock: the rt lock to be initialized
  774. *
  775. * Initialize the rt lock to unlocked state.
  776. *
  777. * Initializing of a locked rt lock is not allowed
  778. */
  779. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  780. {
  781. lock->owner = NULL;
  782. spin_lock_init(&lock->wait_lock);
  783. plist_head_init(&lock->wait_list, &lock->wait_lock);
  784. debug_rt_mutex_init(lock, name);
  785. }
  786. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  787. /**
  788. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  789. * proxy owner
  790. *
  791. * @lock: the rt_mutex to be locked
  792. * @proxy_owner:the task to set as owner
  793. *
  794. * No locking. Caller has to do serializing itself
  795. * Special API call for PI-futex support
  796. */
  797. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  798. struct task_struct *proxy_owner)
  799. {
  800. __rt_mutex_init(lock, NULL);
  801. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  802. rt_mutex_set_owner(lock, proxy_owner, 0);
  803. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  804. }
  805. /**
  806. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  807. *
  808. * @lock: the rt_mutex to be locked
  809. *
  810. * No locking. Caller has to do serializing itself
  811. * Special API call for PI-futex support
  812. */
  813. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  814. struct task_struct *proxy_owner)
  815. {
  816. debug_rt_mutex_proxy_unlock(lock);
  817. rt_mutex_set_owner(lock, NULL, 0);
  818. rt_mutex_deadlock_account_unlock(proxy_owner);
  819. }
  820. /**
  821. * rt_mutex_next_owner - return the next owner of the lock
  822. *
  823. * @lock: the rt lock query
  824. *
  825. * Returns the next owner of the lock or NULL
  826. *
  827. * Caller has to serialize against other accessors to the lock
  828. * itself.
  829. *
  830. * Special API call for PI-futex support
  831. */
  832. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  833. {
  834. if (!rt_mutex_has_waiters(lock))
  835. return NULL;
  836. return rt_mutex_top_waiter(lock)->task;
  837. }