rtmutex.c 26 KB

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