rtmutex.c 29 KB

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