rtmutex.c 26 KB

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