rtmutex.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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/locking/rt-mutex-design.txt for details.
  12. */
  13. #include <linux/spinlock.h>
  14. #include <linux/export.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/rt.h>
  17. #include <linux/sched/deadline.h>
  18. #include <linux/sched/wake_q.h>
  19. #include <linux/timer.h>
  20. #include "rtmutex_common.h"
  21. /*
  22. * lock->owner state tracking:
  23. *
  24. * lock->owner holds the task_struct pointer of the owner. Bit 0
  25. * is used to keep track of the "lock has waiters" state.
  26. *
  27. * owner bit0
  28. * NULL 0 lock is free (fast acquire possible)
  29. * NULL 1 lock is free and has waiters and the top waiter
  30. * is going to take the lock*
  31. * taskpointer 0 lock is held (fast release possible)
  32. * taskpointer 1 lock is held and has waiters**
  33. *
  34. * The fast atomic compare exchange based acquire and release is only
  35. * possible when bit 0 of lock->owner is 0.
  36. *
  37. * (*) It also can be a transitional state when grabbing the lock
  38. * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock,
  39. * we need to set the bit0 before looking at the lock, and the owner may be
  40. * NULL in this small time, hence this can be a transitional state.
  41. *
  42. * (**) There is a small time when bit 0 is set but there are no
  43. * waiters. This can happen when grabbing the lock in the slow path.
  44. * To prevent a cmpxchg of the owner releasing the lock, we need to
  45. * set this bit before looking at the lock.
  46. */
  47. static void
  48. rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner)
  49. {
  50. unsigned long val = (unsigned long)owner;
  51. if (rt_mutex_has_waiters(lock))
  52. val |= RT_MUTEX_HAS_WAITERS;
  53. lock->owner = (struct task_struct *)val;
  54. }
  55. static inline void clear_rt_mutex_waiters(struct rt_mutex *lock)
  56. {
  57. lock->owner = (struct task_struct *)
  58. ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS);
  59. }
  60. static void fixup_rt_mutex_waiters(struct rt_mutex *lock)
  61. {
  62. unsigned long owner, *p = (unsigned long *) &lock->owner;
  63. if (rt_mutex_has_waiters(lock))
  64. return;
  65. /*
  66. * The rbtree has no waiters enqueued, now make sure that the
  67. * lock->owner still has the waiters bit set, otherwise the
  68. * following can happen:
  69. *
  70. * CPU 0 CPU 1 CPU2
  71. * l->owner=T1
  72. * rt_mutex_lock(l)
  73. * lock(l->lock)
  74. * l->owner = T1 | HAS_WAITERS;
  75. * enqueue(T2)
  76. * boost()
  77. * unlock(l->lock)
  78. * block()
  79. *
  80. * rt_mutex_lock(l)
  81. * lock(l->lock)
  82. * l->owner = T1 | HAS_WAITERS;
  83. * enqueue(T3)
  84. * boost()
  85. * unlock(l->lock)
  86. * block()
  87. * signal(->T2) signal(->T3)
  88. * lock(l->lock)
  89. * dequeue(T2)
  90. * deboost()
  91. * unlock(l->lock)
  92. * lock(l->lock)
  93. * dequeue(T3)
  94. * ==> wait list is empty
  95. * deboost()
  96. * unlock(l->lock)
  97. * lock(l->lock)
  98. * fixup_rt_mutex_waiters()
  99. * if (wait_list_empty(l) {
  100. * l->owner = owner
  101. * owner = l->owner & ~HAS_WAITERS;
  102. * ==> l->owner = T1
  103. * }
  104. * lock(l->lock)
  105. * rt_mutex_unlock(l) fixup_rt_mutex_waiters()
  106. * if (wait_list_empty(l) {
  107. * owner = l->owner & ~HAS_WAITERS;
  108. * cmpxchg(l->owner, T1, NULL)
  109. * ===> Success (l->owner = NULL)
  110. *
  111. * l->owner = owner
  112. * ==> l->owner = T1
  113. * }
  114. *
  115. * With the check for the waiter bit in place T3 on CPU2 will not
  116. * overwrite. All tasks fiddling with the waiters bit are
  117. * serialized by l->lock, so nothing else can modify the waiters
  118. * bit. If the bit is set then nothing can change l->owner either
  119. * so the simple RMW is safe. The cmpxchg() will simply fail if it
  120. * happens in the middle of the RMW because the waiters bit is
  121. * still set.
  122. */
  123. owner = READ_ONCE(*p);
  124. if (owner & RT_MUTEX_HAS_WAITERS)
  125. WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS);
  126. }
  127. /*
  128. * We can speed up the acquire/release, if there's no debugging state to be
  129. * set up.
  130. */
  131. #ifndef CONFIG_DEBUG_RT_MUTEXES
  132. # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c)
  133. # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c)
  134. # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c)
  135. /*
  136. * Callers must hold the ->wait_lock -- which is the whole purpose as we force
  137. * all future threads that attempt to [Rmw] the lock to the slowpath. As such
  138. * relaxed semantics suffice.
  139. */
  140. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  141. {
  142. unsigned long owner, *p = (unsigned long *) &lock->owner;
  143. do {
  144. owner = *p;
  145. } while (cmpxchg_relaxed(p, owner,
  146. owner | RT_MUTEX_HAS_WAITERS) != owner);
  147. }
  148. /*
  149. * Safe fastpath aware unlock:
  150. * 1) Clear the waiters bit
  151. * 2) Drop lock->wait_lock
  152. * 3) Try to unlock the lock with cmpxchg
  153. */
  154. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  155. unsigned long flags)
  156. __releases(lock->wait_lock)
  157. {
  158. struct task_struct *owner = rt_mutex_owner(lock);
  159. clear_rt_mutex_waiters(lock);
  160. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  161. /*
  162. * If a new waiter comes in between the unlock and the cmpxchg
  163. * we have two situations:
  164. *
  165. * unlock(wait_lock);
  166. * lock(wait_lock);
  167. * cmpxchg(p, owner, 0) == owner
  168. * mark_rt_mutex_waiters(lock);
  169. * acquire(lock);
  170. * or:
  171. *
  172. * unlock(wait_lock);
  173. * lock(wait_lock);
  174. * mark_rt_mutex_waiters(lock);
  175. *
  176. * cmpxchg(p, owner, 0) != owner
  177. * enqueue_waiter();
  178. * unlock(wait_lock);
  179. * lock(wait_lock);
  180. * wake waiter();
  181. * unlock(wait_lock);
  182. * lock(wait_lock);
  183. * acquire(lock);
  184. */
  185. return rt_mutex_cmpxchg_release(lock, owner, NULL);
  186. }
  187. #else
  188. # define rt_mutex_cmpxchg_relaxed(l,c,n) (0)
  189. # define rt_mutex_cmpxchg_acquire(l,c,n) (0)
  190. # define rt_mutex_cmpxchg_release(l,c,n) (0)
  191. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  192. {
  193. lock->owner = (struct task_struct *)
  194. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  195. }
  196. /*
  197. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  198. */
  199. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock,
  200. unsigned long flags)
  201. __releases(lock->wait_lock)
  202. {
  203. lock->owner = NULL;
  204. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  205. return true;
  206. }
  207. #endif
  208. static inline int
  209. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  210. struct rt_mutex_waiter *right)
  211. {
  212. if (left->prio < right->prio)
  213. return 1;
  214. /*
  215. * If both waiters have dl_prio(), we check the deadlines of the
  216. * associated tasks.
  217. * If left waiter has a dl_prio(), and we didn't return 1 above,
  218. * then right waiter has a dl_prio() too.
  219. */
  220. if (dl_prio(left->prio))
  221. return dl_time_before(left->task->dl.deadline,
  222. right->task->dl.deadline);
  223. return 0;
  224. }
  225. static void
  226. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  227. {
  228. struct rb_node **link = &lock->waiters.rb_node;
  229. struct rb_node *parent = NULL;
  230. struct rt_mutex_waiter *entry;
  231. int leftmost = 1;
  232. while (*link) {
  233. parent = *link;
  234. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  235. if (rt_mutex_waiter_less(waiter, entry)) {
  236. link = &parent->rb_left;
  237. } else {
  238. link = &parent->rb_right;
  239. leftmost = 0;
  240. }
  241. }
  242. if (leftmost)
  243. lock->waiters_leftmost = &waiter->tree_entry;
  244. rb_link_node(&waiter->tree_entry, parent, link);
  245. rb_insert_color(&waiter->tree_entry, &lock->waiters);
  246. }
  247. static void
  248. rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  249. {
  250. if (RB_EMPTY_NODE(&waiter->tree_entry))
  251. return;
  252. if (lock->waiters_leftmost == &waiter->tree_entry)
  253. lock->waiters_leftmost = rb_next(&waiter->tree_entry);
  254. rb_erase(&waiter->tree_entry, &lock->waiters);
  255. RB_CLEAR_NODE(&waiter->tree_entry);
  256. }
  257. static void
  258. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  259. {
  260. struct rb_node **link = &task->pi_waiters.rb_node;
  261. struct rb_node *parent = NULL;
  262. struct rt_mutex_waiter *entry;
  263. int leftmost = 1;
  264. while (*link) {
  265. parent = *link;
  266. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  267. if (rt_mutex_waiter_less(waiter, entry)) {
  268. link = &parent->rb_left;
  269. } else {
  270. link = &parent->rb_right;
  271. leftmost = 0;
  272. }
  273. }
  274. if (leftmost)
  275. task->pi_waiters_leftmost = &waiter->pi_tree_entry;
  276. rb_link_node(&waiter->pi_tree_entry, parent, link);
  277. rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
  278. }
  279. static void
  280. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  281. {
  282. if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
  283. return;
  284. if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
  285. task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
  286. rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
  287. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  288. }
  289. /*
  290. * Calculate task priority from the waiter tree priority
  291. *
  292. * Return task->normal_prio when the waiter tree is empty or when
  293. * the waiter is not allowed to do priority boosting
  294. */
  295. int rt_mutex_getprio(struct task_struct *task)
  296. {
  297. if (likely(!task_has_pi_waiters(task)))
  298. return task->normal_prio;
  299. return min(task_top_pi_waiter(task)->prio,
  300. task->normal_prio);
  301. }
  302. struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  303. {
  304. if (likely(!task_has_pi_waiters(task)))
  305. return NULL;
  306. return task_top_pi_waiter(task)->task;
  307. }
  308. /*
  309. * Called by sched_setscheduler() to get the priority which will be
  310. * effective after the change.
  311. */
  312. int rt_mutex_get_effective_prio(struct task_struct *task, int newprio)
  313. {
  314. if (!task_has_pi_waiters(task))
  315. return newprio;
  316. if (task_top_pi_waiter(task)->task->prio <= newprio)
  317. return task_top_pi_waiter(task)->task->prio;
  318. return newprio;
  319. }
  320. /*
  321. * Adjust the priority of a task, after its pi_waiters got modified.
  322. *
  323. * This can be both boosting and unboosting. task->pi_lock must be held.
  324. */
  325. static void __rt_mutex_adjust_prio(struct task_struct *task)
  326. {
  327. int prio = rt_mutex_getprio(task);
  328. if (task->prio != prio || dl_prio(prio))
  329. rt_mutex_setprio(task, prio);
  330. }
  331. /*
  332. * Adjust task priority (undo boosting). Called from the exit path of
  333. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  334. *
  335. * (Note: We do this outside of the protection of lock->wait_lock to
  336. * allow the lock to be taken while or before we readjust the priority
  337. * of task. We do not use the spin_xx_mutex() variants here as we are
  338. * outside of the debug path.)
  339. */
  340. void rt_mutex_adjust_prio(struct task_struct *task)
  341. {
  342. unsigned long flags;
  343. raw_spin_lock_irqsave(&task->pi_lock, flags);
  344. __rt_mutex_adjust_prio(task);
  345. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  346. }
  347. /*
  348. * Deadlock detection is conditional:
  349. *
  350. * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
  351. * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
  352. *
  353. * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
  354. * conducted independent of the detect argument.
  355. *
  356. * If the waiter argument is NULL this indicates the deboost path and
  357. * deadlock detection is disabled independent of the detect argument
  358. * and the config settings.
  359. */
  360. static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  361. enum rtmutex_chainwalk chwalk)
  362. {
  363. /*
  364. * This is just a wrapper function for the following call,
  365. * because debug_rt_mutex_detect_deadlock() smells like a magic
  366. * debug feature and I wanted to keep the cond function in the
  367. * main source file along with the comments instead of having
  368. * two of the same in the headers.
  369. */
  370. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  371. }
  372. /*
  373. * Max number of times we'll walk the boosting chain:
  374. */
  375. int max_lock_depth = 1024;
  376. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  377. {
  378. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  379. }
  380. /*
  381. * Adjust the priority chain. Also used for deadlock detection.
  382. * Decreases task's usage by one - may thus free the task.
  383. *
  384. * @task: the task owning the mutex (owner) for which a chain walk is
  385. * probably needed
  386. * @chwalk: do we have to carry out deadlock detection?
  387. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  388. * things for a task that has just got its priority adjusted, and
  389. * is waiting on a mutex)
  390. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  391. * we dropped its pi_lock. Is never dereferenced, only used for
  392. * comparison to detect lock chain changes.
  393. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  394. * its priority to the mutex owner (can be NULL in the case
  395. * depicted above or if the top waiter is gone away and we are
  396. * actually deboosting the owner)
  397. * @top_task: the current top waiter
  398. *
  399. * Returns 0 or -EDEADLK.
  400. *
  401. * Chain walk basics and protection scope
  402. *
  403. * [R] refcount on task
  404. * [P] task->pi_lock held
  405. * [L] rtmutex->wait_lock held
  406. *
  407. * Step Description Protected by
  408. * function arguments:
  409. * @task [R]
  410. * @orig_lock if != NULL @top_task is blocked on it
  411. * @next_lock Unprotected. Cannot be
  412. * dereferenced. Only used for
  413. * comparison.
  414. * @orig_waiter if != NULL @top_task is blocked on it
  415. * @top_task current, or in case of proxy
  416. * locking protected by calling
  417. * code
  418. * again:
  419. * loop_sanity_check();
  420. * retry:
  421. * [1] lock(task->pi_lock); [R] acquire [P]
  422. * [2] waiter = task->pi_blocked_on; [P]
  423. * [3] check_exit_conditions_1(); [P]
  424. * [4] lock = waiter->lock; [P]
  425. * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
  426. * unlock(task->pi_lock); release [P]
  427. * goto retry;
  428. * }
  429. * [6] check_exit_conditions_2(); [P] + [L]
  430. * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
  431. * [8] unlock(task->pi_lock); release [P]
  432. * put_task_struct(task); release [R]
  433. * [9] check_exit_conditions_3(); [L]
  434. * [10] task = owner(lock); [L]
  435. * get_task_struct(task); [L] acquire [R]
  436. * lock(task->pi_lock); [L] acquire [P]
  437. * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
  438. * [12] check_exit_conditions_4(); [P] + [L]
  439. * [13] unlock(task->pi_lock); release [P]
  440. * unlock(lock->wait_lock); release [L]
  441. * goto again;
  442. */
  443. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  444. enum rtmutex_chainwalk chwalk,
  445. struct rt_mutex *orig_lock,
  446. struct rt_mutex *next_lock,
  447. struct rt_mutex_waiter *orig_waiter,
  448. struct task_struct *top_task)
  449. {
  450. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  451. struct rt_mutex_waiter *prerequeue_top_waiter;
  452. int ret = 0, depth = 0;
  453. struct rt_mutex *lock;
  454. bool detect_deadlock;
  455. bool requeue = true;
  456. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  457. /*
  458. * The (de)boosting is a step by step approach with a lot of
  459. * pitfalls. We want this to be preemptible and we want hold a
  460. * maximum of two locks per step. So we have to check
  461. * carefully whether things change under us.
  462. */
  463. again:
  464. /*
  465. * We limit the lock chain length for each invocation.
  466. */
  467. if (++depth > max_lock_depth) {
  468. static int prev_max;
  469. /*
  470. * Print this only once. If the admin changes the limit,
  471. * print a new message when reaching the limit again.
  472. */
  473. if (prev_max != max_lock_depth) {
  474. prev_max = max_lock_depth;
  475. printk(KERN_WARNING "Maximum lock depth %d reached "
  476. "task: %s (%d)\n", max_lock_depth,
  477. top_task->comm, task_pid_nr(top_task));
  478. }
  479. put_task_struct(task);
  480. return -EDEADLK;
  481. }
  482. /*
  483. * We are fully preemptible here and only hold the refcount on
  484. * @task. So everything can have changed under us since the
  485. * caller or our own code below (goto retry/again) dropped all
  486. * locks.
  487. */
  488. retry:
  489. /*
  490. * [1] Task cannot go away as we did a get_task() before !
  491. */
  492. raw_spin_lock_irq(&task->pi_lock);
  493. /*
  494. * [2] Get the waiter on which @task is blocked on.
  495. */
  496. waiter = task->pi_blocked_on;
  497. /*
  498. * [3] check_exit_conditions_1() protected by task->pi_lock.
  499. */
  500. /*
  501. * Check whether the end of the boosting chain has been
  502. * reached or the state of the chain has changed while we
  503. * dropped the locks.
  504. */
  505. if (!waiter)
  506. goto out_unlock_pi;
  507. /*
  508. * Check the orig_waiter state. After we dropped the locks,
  509. * the previous owner of the lock might have released the lock.
  510. */
  511. if (orig_waiter && !rt_mutex_owner(orig_lock))
  512. goto out_unlock_pi;
  513. /*
  514. * We dropped all locks after taking a refcount on @task, so
  515. * the task might have moved on in the lock chain or even left
  516. * the chain completely and blocks now on an unrelated lock or
  517. * on @orig_lock.
  518. *
  519. * We stored the lock on which @task was blocked in @next_lock,
  520. * so we can detect the chain change.
  521. */
  522. if (next_lock != waiter->lock)
  523. goto out_unlock_pi;
  524. /*
  525. * Drop out, when the task has no waiters. Note,
  526. * top_waiter can be NULL, when we are in the deboosting
  527. * mode!
  528. */
  529. if (top_waiter) {
  530. if (!task_has_pi_waiters(task))
  531. goto out_unlock_pi;
  532. /*
  533. * If deadlock detection is off, we stop here if we
  534. * are not the top pi waiter of the task. If deadlock
  535. * detection is enabled we continue, but stop the
  536. * requeueing in the chain walk.
  537. */
  538. if (top_waiter != task_top_pi_waiter(task)) {
  539. if (!detect_deadlock)
  540. goto out_unlock_pi;
  541. else
  542. requeue = false;
  543. }
  544. }
  545. /*
  546. * If the waiter priority is the same as the task priority
  547. * then there is no further priority adjustment necessary. If
  548. * deadlock detection is off, we stop the chain walk. If its
  549. * enabled we continue, but stop the requeueing in the chain
  550. * walk.
  551. */
  552. if (waiter->prio == task->prio) {
  553. if (!detect_deadlock)
  554. goto out_unlock_pi;
  555. else
  556. requeue = false;
  557. }
  558. /*
  559. * [4] Get the next lock
  560. */
  561. lock = waiter->lock;
  562. /*
  563. * [5] We need to trylock here as we are holding task->pi_lock,
  564. * which is the reverse lock order versus the other rtmutex
  565. * operations.
  566. */
  567. if (!raw_spin_trylock(&lock->wait_lock)) {
  568. raw_spin_unlock_irq(&task->pi_lock);
  569. cpu_relax();
  570. goto retry;
  571. }
  572. /*
  573. * [6] check_exit_conditions_2() protected by task->pi_lock and
  574. * lock->wait_lock.
  575. *
  576. * Deadlock detection. If the lock is the same as the original
  577. * lock which caused us to walk the lock chain or if the
  578. * current lock is owned by the task which initiated the chain
  579. * walk, we detected a deadlock.
  580. */
  581. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  582. debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
  583. raw_spin_unlock(&lock->wait_lock);
  584. ret = -EDEADLK;
  585. goto out_unlock_pi;
  586. }
  587. /*
  588. * If we just follow the lock chain for deadlock detection, no
  589. * need to do all the requeue operations. To avoid a truckload
  590. * of conditionals around the various places below, just do the
  591. * minimum chain walk checks.
  592. */
  593. if (!requeue) {
  594. /*
  595. * No requeue[7] here. Just release @task [8]
  596. */
  597. raw_spin_unlock(&task->pi_lock);
  598. put_task_struct(task);
  599. /*
  600. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  601. * If there is no owner of the lock, end of chain.
  602. */
  603. if (!rt_mutex_owner(lock)) {
  604. raw_spin_unlock_irq(&lock->wait_lock);
  605. return 0;
  606. }
  607. /* [10] Grab the next task, i.e. owner of @lock */
  608. task = rt_mutex_owner(lock);
  609. get_task_struct(task);
  610. raw_spin_lock(&task->pi_lock);
  611. /*
  612. * No requeue [11] here. We just do deadlock detection.
  613. *
  614. * [12] Store whether owner is blocked
  615. * itself. Decision is made after dropping the locks
  616. */
  617. next_lock = task_blocked_on_lock(task);
  618. /*
  619. * Get the top waiter for the next iteration
  620. */
  621. top_waiter = rt_mutex_top_waiter(lock);
  622. /* [13] Drop locks */
  623. raw_spin_unlock(&task->pi_lock);
  624. raw_spin_unlock_irq(&lock->wait_lock);
  625. /* If owner is not blocked, end of chain. */
  626. if (!next_lock)
  627. goto out_put_task;
  628. goto again;
  629. }
  630. /*
  631. * Store the current top waiter before doing the requeue
  632. * operation on @lock. We need it for the boost/deboost
  633. * decision below.
  634. */
  635. prerequeue_top_waiter = rt_mutex_top_waiter(lock);
  636. /* [7] Requeue the waiter in the lock waiter tree. */
  637. rt_mutex_dequeue(lock, waiter);
  638. waiter->prio = task->prio;
  639. rt_mutex_enqueue(lock, waiter);
  640. /* [8] Release the task */
  641. raw_spin_unlock(&task->pi_lock);
  642. put_task_struct(task);
  643. /*
  644. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  645. *
  646. * We must abort the chain walk if there is no lock owner even
  647. * in the dead lock detection case, as we have nothing to
  648. * follow here. This is the end of the chain we are walking.
  649. */
  650. if (!rt_mutex_owner(lock)) {
  651. /*
  652. * If the requeue [7] above changed the top waiter,
  653. * then we need to wake the new top waiter up to try
  654. * to get the lock.
  655. */
  656. if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  657. wake_up_process(rt_mutex_top_waiter(lock)->task);
  658. raw_spin_unlock_irq(&lock->wait_lock);
  659. return 0;
  660. }
  661. /* [10] Grab the next task, i.e. the owner of @lock */
  662. task = rt_mutex_owner(lock);
  663. get_task_struct(task);
  664. raw_spin_lock(&task->pi_lock);
  665. /* [11] requeue the pi waiters if necessary */
  666. if (waiter == rt_mutex_top_waiter(lock)) {
  667. /*
  668. * The waiter became the new top (highest priority)
  669. * waiter on the lock. Replace the previous top waiter
  670. * in the owner tasks pi waiters tree with this waiter
  671. * and adjust the priority of the owner.
  672. */
  673. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  674. rt_mutex_enqueue_pi(task, waiter);
  675. __rt_mutex_adjust_prio(task);
  676. } else if (prerequeue_top_waiter == waiter) {
  677. /*
  678. * The waiter was the top waiter on the lock, but is
  679. * no longer the top prority waiter. Replace waiter in
  680. * the owner tasks pi waiters tree with the new top
  681. * (highest priority) waiter and adjust the priority
  682. * of the owner.
  683. * The new top waiter is stored in @waiter so that
  684. * @waiter == @top_waiter evaluates to true below and
  685. * we continue to deboost the rest of the chain.
  686. */
  687. rt_mutex_dequeue_pi(task, waiter);
  688. waiter = rt_mutex_top_waiter(lock);
  689. rt_mutex_enqueue_pi(task, waiter);
  690. __rt_mutex_adjust_prio(task);
  691. } else {
  692. /*
  693. * Nothing changed. No need to do any priority
  694. * adjustment.
  695. */
  696. }
  697. /*
  698. * [12] check_exit_conditions_4() protected by task->pi_lock
  699. * and lock->wait_lock. The actual decisions are made after we
  700. * dropped the locks.
  701. *
  702. * Check whether the task which owns the current lock is pi
  703. * blocked itself. If yes we store a pointer to the lock for
  704. * the lock chain change detection above. After we dropped
  705. * task->pi_lock next_lock cannot be dereferenced anymore.
  706. */
  707. next_lock = task_blocked_on_lock(task);
  708. /*
  709. * Store the top waiter of @lock for the end of chain walk
  710. * decision below.
  711. */
  712. top_waiter = rt_mutex_top_waiter(lock);
  713. /* [13] Drop the locks */
  714. raw_spin_unlock(&task->pi_lock);
  715. raw_spin_unlock_irq(&lock->wait_lock);
  716. /*
  717. * Make the actual exit decisions [12], based on the stored
  718. * values.
  719. *
  720. * We reached the end of the lock chain. Stop right here. No
  721. * point to go back just to figure that out.
  722. */
  723. if (!next_lock)
  724. goto out_put_task;
  725. /*
  726. * If the current waiter is not the top waiter on the lock,
  727. * then we can stop the chain walk here if we are not in full
  728. * deadlock detection mode.
  729. */
  730. if (!detect_deadlock && waiter != top_waiter)
  731. goto out_put_task;
  732. goto again;
  733. out_unlock_pi:
  734. raw_spin_unlock_irq(&task->pi_lock);
  735. out_put_task:
  736. put_task_struct(task);
  737. return ret;
  738. }
  739. /*
  740. * Try to take an rt-mutex
  741. *
  742. * Must be called with lock->wait_lock held and interrupts disabled
  743. *
  744. * @lock: The lock to be acquired.
  745. * @task: The task which wants to acquire the lock
  746. * @waiter: The waiter that is queued to the lock's wait tree if the
  747. * callsite called task_blocked_on_lock(), otherwise NULL
  748. */
  749. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  750. struct rt_mutex_waiter *waiter)
  751. {
  752. /*
  753. * Before testing whether we can acquire @lock, we set the
  754. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  755. * other tasks which try to modify @lock into the slow path
  756. * and they serialize on @lock->wait_lock.
  757. *
  758. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  759. * as explained at the top of this file if and only if:
  760. *
  761. * - There is a lock owner. The caller must fixup the
  762. * transient state if it does a trylock or leaves the lock
  763. * function due to a signal or timeout.
  764. *
  765. * - @task acquires the lock and there are no other
  766. * waiters. This is undone in rt_mutex_set_owner(@task) at
  767. * the end of this function.
  768. */
  769. mark_rt_mutex_waiters(lock);
  770. /*
  771. * If @lock has an owner, give up.
  772. */
  773. if (rt_mutex_owner(lock))
  774. return 0;
  775. /*
  776. * If @waiter != NULL, @task has already enqueued the waiter
  777. * into @lock waiter tree. If @waiter == NULL then this is a
  778. * trylock attempt.
  779. */
  780. if (waiter) {
  781. /*
  782. * If waiter is not the highest priority waiter of
  783. * @lock, give up.
  784. */
  785. if (waiter != rt_mutex_top_waiter(lock))
  786. return 0;
  787. /*
  788. * We can acquire the lock. Remove the waiter from the
  789. * lock waiters tree.
  790. */
  791. rt_mutex_dequeue(lock, waiter);
  792. } else {
  793. /*
  794. * If the lock has waiters already we check whether @task is
  795. * eligible to take over the lock.
  796. *
  797. * If there are no other waiters, @task can acquire
  798. * the lock. @task->pi_blocked_on is NULL, so it does
  799. * not need to be dequeued.
  800. */
  801. if (rt_mutex_has_waiters(lock)) {
  802. /*
  803. * If @task->prio is greater than or equal to
  804. * the top waiter priority (kernel view),
  805. * @task lost.
  806. */
  807. if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  808. return 0;
  809. /*
  810. * The current top waiter stays enqueued. We
  811. * don't have to change anything in the lock
  812. * waiters order.
  813. */
  814. } else {
  815. /*
  816. * No waiters. Take the lock without the
  817. * pi_lock dance.@task->pi_blocked_on is NULL
  818. * and we have no waiters to enqueue in @task
  819. * pi waiters tree.
  820. */
  821. goto takeit;
  822. }
  823. }
  824. /*
  825. * Clear @task->pi_blocked_on. Requires protection by
  826. * @task->pi_lock. Redundant operation for the @waiter == NULL
  827. * case, but conditionals are more expensive than a redundant
  828. * store.
  829. */
  830. raw_spin_lock(&task->pi_lock);
  831. task->pi_blocked_on = NULL;
  832. /*
  833. * Finish the lock acquisition. @task is the new owner. If
  834. * other waiters exist we have to insert the highest priority
  835. * waiter into @task->pi_waiters tree.
  836. */
  837. if (rt_mutex_has_waiters(lock))
  838. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  839. raw_spin_unlock(&task->pi_lock);
  840. takeit:
  841. /* We got the lock. */
  842. debug_rt_mutex_lock(lock);
  843. /*
  844. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  845. * are still waiters or clears it.
  846. */
  847. rt_mutex_set_owner(lock, task);
  848. rt_mutex_deadlock_account_lock(lock, task);
  849. return 1;
  850. }
  851. /*
  852. * Task blocks on lock.
  853. *
  854. * Prepare waiter and propagate pi chain
  855. *
  856. * This must be called with lock->wait_lock held and interrupts disabled
  857. */
  858. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  859. struct rt_mutex_waiter *waiter,
  860. struct task_struct *task,
  861. enum rtmutex_chainwalk chwalk)
  862. {
  863. struct task_struct *owner = rt_mutex_owner(lock);
  864. struct rt_mutex_waiter *top_waiter = waiter;
  865. struct rt_mutex *next_lock;
  866. int chain_walk = 0, res;
  867. /*
  868. * Early deadlock detection. We really don't want the task to
  869. * enqueue on itself just to untangle the mess later. It's not
  870. * only an optimization. We drop the locks, so another waiter
  871. * can come in before the chain walk detects the deadlock. So
  872. * the other will detect the deadlock and return -EDEADLOCK,
  873. * which is wrong, as the other waiter is not in a deadlock
  874. * situation.
  875. */
  876. if (owner == task)
  877. return -EDEADLK;
  878. raw_spin_lock(&task->pi_lock);
  879. __rt_mutex_adjust_prio(task);
  880. waiter->task = task;
  881. waiter->lock = lock;
  882. waiter->prio = task->prio;
  883. /* Get the top priority waiter on the lock */
  884. if (rt_mutex_has_waiters(lock))
  885. top_waiter = rt_mutex_top_waiter(lock);
  886. rt_mutex_enqueue(lock, waiter);
  887. task->pi_blocked_on = waiter;
  888. raw_spin_unlock(&task->pi_lock);
  889. if (!owner)
  890. return 0;
  891. raw_spin_lock(&owner->pi_lock);
  892. if (waiter == rt_mutex_top_waiter(lock)) {
  893. rt_mutex_dequeue_pi(owner, top_waiter);
  894. rt_mutex_enqueue_pi(owner, waiter);
  895. __rt_mutex_adjust_prio(owner);
  896. if (owner->pi_blocked_on)
  897. chain_walk = 1;
  898. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  899. chain_walk = 1;
  900. }
  901. /* Store the lock on which owner is blocked or NULL */
  902. next_lock = task_blocked_on_lock(owner);
  903. raw_spin_unlock(&owner->pi_lock);
  904. /*
  905. * Even if full deadlock detection is on, if the owner is not
  906. * blocked itself, we can avoid finding this out in the chain
  907. * walk.
  908. */
  909. if (!chain_walk || !next_lock)
  910. return 0;
  911. /*
  912. * The owner can't disappear while holding a lock,
  913. * so the owner struct is protected by wait_lock.
  914. * Gets dropped in rt_mutex_adjust_prio_chain()!
  915. */
  916. get_task_struct(owner);
  917. raw_spin_unlock_irq(&lock->wait_lock);
  918. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  919. next_lock, waiter, task);
  920. raw_spin_lock_irq(&lock->wait_lock);
  921. return res;
  922. }
  923. /*
  924. * Remove the top waiter from the current tasks pi waiter tree and
  925. * queue it up.
  926. *
  927. * Called with lock->wait_lock held and interrupts disabled.
  928. */
  929. static void mark_wakeup_next_waiter(struct wake_q_head *wake_q,
  930. struct rt_mutex *lock)
  931. {
  932. struct rt_mutex_waiter *waiter;
  933. raw_spin_lock(&current->pi_lock);
  934. waiter = rt_mutex_top_waiter(lock);
  935. /*
  936. * Remove it from current->pi_waiters. We do not adjust a
  937. * possible priority boost right now. We execute wakeup in the
  938. * boosted mode and go back to normal after releasing
  939. * lock->wait_lock.
  940. */
  941. rt_mutex_dequeue_pi(current, waiter);
  942. /*
  943. * As we are waking up the top waiter, and the waiter stays
  944. * queued on the lock until it gets the lock, this lock
  945. * obviously has waiters. Just set the bit here and this has
  946. * the added benefit of forcing all new tasks into the
  947. * slow path making sure no task of lower priority than
  948. * the top waiter can steal this lock.
  949. */
  950. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  951. raw_spin_unlock(&current->pi_lock);
  952. wake_q_add(wake_q, waiter->task);
  953. }
  954. /*
  955. * Remove a waiter from a lock and give up
  956. *
  957. * Must be called with lock->wait_lock held and interrupts disabled. I must
  958. * have just failed to try_to_take_rt_mutex().
  959. */
  960. static void remove_waiter(struct rt_mutex *lock,
  961. struct rt_mutex_waiter *waiter)
  962. {
  963. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  964. struct task_struct *owner = rt_mutex_owner(lock);
  965. struct rt_mutex *next_lock;
  966. raw_spin_lock(&current->pi_lock);
  967. rt_mutex_dequeue(lock, waiter);
  968. current->pi_blocked_on = NULL;
  969. raw_spin_unlock(&current->pi_lock);
  970. /*
  971. * Only update priority if the waiter was the highest priority
  972. * waiter of the lock and there is an owner to update.
  973. */
  974. if (!owner || !is_top_waiter)
  975. return;
  976. raw_spin_lock(&owner->pi_lock);
  977. rt_mutex_dequeue_pi(owner, waiter);
  978. if (rt_mutex_has_waiters(lock))
  979. rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  980. __rt_mutex_adjust_prio(owner);
  981. /* Store the lock on which owner is blocked or NULL */
  982. next_lock = task_blocked_on_lock(owner);
  983. raw_spin_unlock(&owner->pi_lock);
  984. /*
  985. * Don't walk the chain, if the owner task is not blocked
  986. * itself.
  987. */
  988. if (!next_lock)
  989. return;
  990. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  991. get_task_struct(owner);
  992. raw_spin_unlock_irq(&lock->wait_lock);
  993. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  994. next_lock, NULL, current);
  995. raw_spin_lock_irq(&lock->wait_lock);
  996. }
  997. /*
  998. * Recheck the pi chain, in case we got a priority setting
  999. *
  1000. * Called from sched_setscheduler
  1001. */
  1002. void rt_mutex_adjust_pi(struct task_struct *task)
  1003. {
  1004. struct rt_mutex_waiter *waiter;
  1005. struct rt_mutex *next_lock;
  1006. unsigned long flags;
  1007. raw_spin_lock_irqsave(&task->pi_lock, flags);
  1008. waiter = task->pi_blocked_on;
  1009. if (!waiter || (waiter->prio == task->prio &&
  1010. !dl_prio(task->prio))) {
  1011. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  1012. return;
  1013. }
  1014. next_lock = waiter->lock;
  1015. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  1016. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  1017. get_task_struct(task);
  1018. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  1019. next_lock, NULL, task);
  1020. }
  1021. /**
  1022. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  1023. * @lock: the rt_mutex to take
  1024. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  1025. * or TASK_UNINTERRUPTIBLE)
  1026. * @timeout: the pre-initialized and started timer, or NULL for none
  1027. * @waiter: the pre-initialized rt_mutex_waiter
  1028. *
  1029. * Must be called with lock->wait_lock held and interrupts disabled
  1030. */
  1031. static int __sched
  1032. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  1033. struct hrtimer_sleeper *timeout,
  1034. struct rt_mutex_waiter *waiter)
  1035. {
  1036. int ret = 0;
  1037. for (;;) {
  1038. /* Try to acquire the lock: */
  1039. if (try_to_take_rt_mutex(lock, current, waiter))
  1040. break;
  1041. /*
  1042. * TASK_INTERRUPTIBLE checks for signals and
  1043. * timeout. Ignored otherwise.
  1044. */
  1045. if (likely(state == TASK_INTERRUPTIBLE)) {
  1046. /* Signal pending? */
  1047. if (signal_pending(current))
  1048. ret = -EINTR;
  1049. if (timeout && !timeout->task)
  1050. ret = -ETIMEDOUT;
  1051. if (ret)
  1052. break;
  1053. }
  1054. raw_spin_unlock_irq(&lock->wait_lock);
  1055. debug_rt_mutex_print_deadlock(waiter);
  1056. schedule();
  1057. raw_spin_lock_irq(&lock->wait_lock);
  1058. set_current_state(state);
  1059. }
  1060. __set_current_state(TASK_RUNNING);
  1061. return ret;
  1062. }
  1063. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  1064. struct rt_mutex_waiter *w)
  1065. {
  1066. /*
  1067. * If the result is not -EDEADLOCK or the caller requested
  1068. * deadlock detection, nothing to do here.
  1069. */
  1070. if (res != -EDEADLOCK || detect_deadlock)
  1071. return;
  1072. /*
  1073. * Yell lowdly and stop the task right here.
  1074. */
  1075. rt_mutex_print_deadlock(w);
  1076. while (1) {
  1077. set_current_state(TASK_INTERRUPTIBLE);
  1078. schedule();
  1079. }
  1080. }
  1081. /*
  1082. * Slow path lock function:
  1083. */
  1084. static int __sched
  1085. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  1086. struct hrtimer_sleeper *timeout,
  1087. enum rtmutex_chainwalk chwalk)
  1088. {
  1089. struct rt_mutex_waiter waiter;
  1090. unsigned long flags;
  1091. int ret = 0;
  1092. debug_rt_mutex_init_waiter(&waiter);
  1093. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  1094. RB_CLEAR_NODE(&waiter.tree_entry);
  1095. /*
  1096. * Technically we could use raw_spin_[un]lock_irq() here, but this can
  1097. * be called in early boot if the cmpxchg() fast path is disabled
  1098. * (debug, no architecture support). In this case we will acquire the
  1099. * rtmutex with lock->wait_lock held. But we cannot unconditionally
  1100. * enable interrupts in that early boot case. So we need to use the
  1101. * irqsave/restore variants.
  1102. */
  1103. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1104. /* Try to acquire the lock again: */
  1105. if (try_to_take_rt_mutex(lock, current, NULL)) {
  1106. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1107. return 0;
  1108. }
  1109. set_current_state(state);
  1110. /* Setup the timer, when timeout != NULL */
  1111. if (unlikely(timeout))
  1112. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1113. ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
  1114. if (likely(!ret))
  1115. /* sleep on the mutex */
  1116. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  1117. if (unlikely(ret)) {
  1118. __set_current_state(TASK_RUNNING);
  1119. if (rt_mutex_has_waiters(lock))
  1120. remove_waiter(lock, &waiter);
  1121. rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  1122. }
  1123. /*
  1124. * try_to_take_rt_mutex() sets the waiter bit
  1125. * unconditionally. We might have to fix that up.
  1126. */
  1127. fixup_rt_mutex_waiters(lock);
  1128. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1129. /* Remove pending timer: */
  1130. if (unlikely(timeout))
  1131. hrtimer_cancel(&timeout->timer);
  1132. debug_rt_mutex_free_waiter(&waiter);
  1133. return ret;
  1134. }
  1135. /*
  1136. * Slow path try-lock function:
  1137. */
  1138. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  1139. {
  1140. unsigned long flags;
  1141. int ret;
  1142. /*
  1143. * If the lock already has an owner we fail to get the lock.
  1144. * This can be done without taking the @lock->wait_lock as
  1145. * it is only being read, and this is a trylock anyway.
  1146. */
  1147. if (rt_mutex_owner(lock))
  1148. return 0;
  1149. /*
  1150. * The mutex has currently no owner. Lock the wait lock and try to
  1151. * acquire the lock. We use irqsave here to support early boot calls.
  1152. */
  1153. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1154. ret = try_to_take_rt_mutex(lock, current, NULL);
  1155. /*
  1156. * try_to_take_rt_mutex() sets the lock waiters bit
  1157. * unconditionally. Clean this up.
  1158. */
  1159. fixup_rt_mutex_waiters(lock);
  1160. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1161. return ret;
  1162. }
  1163. /*
  1164. * Slow path to release a rt-mutex.
  1165. * Return whether the current task needs to undo a potential priority boosting.
  1166. */
  1167. static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock,
  1168. struct wake_q_head *wake_q)
  1169. {
  1170. unsigned long flags;
  1171. /* irqsave required to support early boot calls */
  1172. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1173. debug_rt_mutex_unlock(lock);
  1174. rt_mutex_deadlock_account_unlock(current);
  1175. /*
  1176. * We must be careful here if the fast path is enabled. If we
  1177. * have no waiters queued we cannot set owner to NULL here
  1178. * because of:
  1179. *
  1180. * foo->lock->owner = NULL;
  1181. * rtmutex_lock(foo->lock); <- fast path
  1182. * free = atomic_dec_and_test(foo->refcnt);
  1183. * rtmutex_unlock(foo->lock); <- fast path
  1184. * if (free)
  1185. * kfree(foo);
  1186. * raw_spin_unlock(foo->lock->wait_lock);
  1187. *
  1188. * So for the fastpath enabled kernel:
  1189. *
  1190. * Nothing can set the waiters bit as long as we hold
  1191. * lock->wait_lock. So we do the following sequence:
  1192. *
  1193. * owner = rt_mutex_owner(lock);
  1194. * clear_rt_mutex_waiters(lock);
  1195. * raw_spin_unlock(&lock->wait_lock);
  1196. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1197. * return;
  1198. * goto retry;
  1199. *
  1200. * The fastpath disabled variant is simple as all access to
  1201. * lock->owner is serialized by lock->wait_lock:
  1202. *
  1203. * lock->owner = NULL;
  1204. * raw_spin_unlock(&lock->wait_lock);
  1205. */
  1206. while (!rt_mutex_has_waiters(lock)) {
  1207. /* Drops lock->wait_lock ! */
  1208. if (unlock_rt_mutex_safe(lock, flags) == true)
  1209. return false;
  1210. /* Relock the rtmutex and try again */
  1211. raw_spin_lock_irqsave(&lock->wait_lock, flags);
  1212. }
  1213. /*
  1214. * The wakeup next waiter path does not suffer from the above
  1215. * race. See the comments there.
  1216. *
  1217. * Queue the next waiter for wakeup once we release the wait_lock.
  1218. */
  1219. mark_wakeup_next_waiter(wake_q, lock);
  1220. raw_spin_unlock_irqrestore(&lock->wait_lock, flags);
  1221. /* check PI boosting */
  1222. return true;
  1223. }
  1224. /*
  1225. * debug aware fast / slowpath lock,trylock,unlock
  1226. *
  1227. * The atomic acquire/release ops are compiled away, when either the
  1228. * architecture does not support cmpxchg or when debugging is enabled.
  1229. */
  1230. static inline int
  1231. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  1232. int (*slowfn)(struct rt_mutex *lock, int state,
  1233. struct hrtimer_sleeper *timeout,
  1234. enum rtmutex_chainwalk chwalk))
  1235. {
  1236. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  1237. rt_mutex_deadlock_account_lock(lock, current);
  1238. return 0;
  1239. } else
  1240. return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  1241. }
  1242. static inline int
  1243. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  1244. struct hrtimer_sleeper *timeout,
  1245. enum rtmutex_chainwalk chwalk,
  1246. int (*slowfn)(struct rt_mutex *lock, int state,
  1247. struct hrtimer_sleeper *timeout,
  1248. enum rtmutex_chainwalk chwalk))
  1249. {
  1250. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  1251. likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  1252. rt_mutex_deadlock_account_lock(lock, current);
  1253. return 0;
  1254. } else
  1255. return slowfn(lock, state, timeout, chwalk);
  1256. }
  1257. static inline int
  1258. rt_mutex_fasttrylock(struct rt_mutex *lock,
  1259. int (*slowfn)(struct rt_mutex *lock))
  1260. {
  1261. if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) {
  1262. rt_mutex_deadlock_account_lock(lock, current);
  1263. return 1;
  1264. }
  1265. return slowfn(lock);
  1266. }
  1267. static inline void
  1268. rt_mutex_fastunlock(struct rt_mutex *lock,
  1269. bool (*slowfn)(struct rt_mutex *lock,
  1270. struct wake_q_head *wqh))
  1271. {
  1272. DEFINE_WAKE_Q(wake_q);
  1273. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  1274. rt_mutex_deadlock_account_unlock(current);
  1275. } else {
  1276. bool deboost = slowfn(lock, &wake_q);
  1277. wake_up_q(&wake_q);
  1278. /* Undo pi boosting if necessary: */
  1279. if (deboost)
  1280. rt_mutex_adjust_prio(current);
  1281. }
  1282. }
  1283. /**
  1284. * rt_mutex_lock - lock a rt_mutex
  1285. *
  1286. * @lock: the rt_mutex to be locked
  1287. */
  1288. void __sched rt_mutex_lock(struct rt_mutex *lock)
  1289. {
  1290. might_sleep();
  1291. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  1292. }
  1293. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  1294. /**
  1295. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1296. *
  1297. * @lock: the rt_mutex to be locked
  1298. *
  1299. * Returns:
  1300. * 0 on success
  1301. * -EINTR when interrupted by a signal
  1302. */
  1303. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  1304. {
  1305. might_sleep();
  1306. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  1307. }
  1308. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1309. /*
  1310. * Futex variant with full deadlock detection.
  1311. */
  1312. int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  1313. struct hrtimer_sleeper *timeout)
  1314. {
  1315. might_sleep();
  1316. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1317. RT_MUTEX_FULL_CHAINWALK,
  1318. rt_mutex_slowlock);
  1319. }
  1320. /**
  1321. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1322. * the timeout structure is provided
  1323. * by the caller
  1324. *
  1325. * @lock: the rt_mutex to be locked
  1326. * @timeout: timeout structure or NULL (no timeout)
  1327. *
  1328. * Returns:
  1329. * 0 on success
  1330. * -EINTR when interrupted by a signal
  1331. * -ETIMEDOUT when the timeout expired
  1332. */
  1333. int
  1334. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  1335. {
  1336. might_sleep();
  1337. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1338. RT_MUTEX_MIN_CHAINWALK,
  1339. rt_mutex_slowlock);
  1340. }
  1341. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1342. /**
  1343. * rt_mutex_trylock - try to lock a rt_mutex
  1344. *
  1345. * @lock: the rt_mutex to be locked
  1346. *
  1347. * This function can only be called in thread context. It's safe to
  1348. * call it from atomic regions, but not from hard interrupt or soft
  1349. * interrupt context.
  1350. *
  1351. * Returns 1 on success and 0 on contention
  1352. */
  1353. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1354. {
  1355. if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq()))
  1356. return 0;
  1357. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1358. }
  1359. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1360. /**
  1361. * rt_mutex_unlock - unlock a rt_mutex
  1362. *
  1363. * @lock: the rt_mutex to be unlocked
  1364. */
  1365. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1366. {
  1367. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1368. }
  1369. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1370. /**
  1371. * rt_mutex_futex_unlock - Futex variant of rt_mutex_unlock
  1372. * @lock: the rt_mutex to be unlocked
  1373. *
  1374. * Returns: true/false indicating whether priority adjustment is
  1375. * required or not.
  1376. */
  1377. bool __sched rt_mutex_futex_unlock(struct rt_mutex *lock,
  1378. struct wake_q_head *wqh)
  1379. {
  1380. if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) {
  1381. rt_mutex_deadlock_account_unlock(current);
  1382. return false;
  1383. }
  1384. return rt_mutex_slowunlock(lock, wqh);
  1385. }
  1386. /**
  1387. * rt_mutex_destroy - mark a mutex unusable
  1388. * @lock: the mutex to be destroyed
  1389. *
  1390. * This function marks the mutex uninitialized, and any subsequent
  1391. * use of the mutex is forbidden. The mutex must not be locked when
  1392. * this function is called.
  1393. */
  1394. void rt_mutex_destroy(struct rt_mutex *lock)
  1395. {
  1396. WARN_ON(rt_mutex_is_locked(lock));
  1397. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1398. lock->magic = NULL;
  1399. #endif
  1400. }
  1401. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1402. /**
  1403. * __rt_mutex_init - initialize the rt lock
  1404. *
  1405. * @lock: the rt lock to be initialized
  1406. *
  1407. * Initialize the rt lock to unlocked state.
  1408. *
  1409. * Initializing of a locked rt lock is not allowed
  1410. */
  1411. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  1412. {
  1413. lock->owner = NULL;
  1414. raw_spin_lock_init(&lock->wait_lock);
  1415. lock->waiters = RB_ROOT;
  1416. lock->waiters_leftmost = NULL;
  1417. debug_rt_mutex_init(lock, name);
  1418. }
  1419. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1420. /**
  1421. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1422. * proxy owner
  1423. *
  1424. * @lock: the rt_mutex to be locked
  1425. * @proxy_owner:the task to set as owner
  1426. *
  1427. * No locking. Caller has to do serializing itself
  1428. *
  1429. * Special API call for PI-futex support. This initializes the rtmutex and
  1430. * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not
  1431. * possible at this point because the pi_state which contains the rtmutex
  1432. * is not yet visible to other tasks.
  1433. */
  1434. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1435. struct task_struct *proxy_owner)
  1436. {
  1437. __rt_mutex_init(lock, NULL);
  1438. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1439. rt_mutex_set_owner(lock, proxy_owner);
  1440. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1441. }
  1442. /**
  1443. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1444. *
  1445. * @lock: the rt_mutex to be locked
  1446. *
  1447. * No locking. Caller has to do serializing itself
  1448. *
  1449. * Special API call for PI-futex support. This merrily cleans up the rtmutex
  1450. * (debugging) state. Concurrent operations on this rt_mutex are not
  1451. * possible because it belongs to the pi_state which is about to be freed
  1452. * and it is not longer visible to other tasks.
  1453. */
  1454. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1455. struct task_struct *proxy_owner)
  1456. {
  1457. debug_rt_mutex_proxy_unlock(lock);
  1458. rt_mutex_set_owner(lock, NULL);
  1459. rt_mutex_deadlock_account_unlock(proxy_owner);
  1460. }
  1461. /**
  1462. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1463. * @lock: the rt_mutex to take
  1464. * @waiter: the pre-initialized rt_mutex_waiter
  1465. * @task: the task to prepare
  1466. *
  1467. * Returns:
  1468. * 0 - task blocked on lock
  1469. * 1 - acquired the lock for task, caller should wake it up
  1470. * <0 - error
  1471. *
  1472. * Special API call for FUTEX_REQUEUE_PI support.
  1473. */
  1474. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1475. struct rt_mutex_waiter *waiter,
  1476. struct task_struct *task)
  1477. {
  1478. int ret;
  1479. raw_spin_lock_irq(&lock->wait_lock);
  1480. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1481. raw_spin_unlock_irq(&lock->wait_lock);
  1482. return 1;
  1483. }
  1484. /* We enforce deadlock detection for futexes */
  1485. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  1486. RT_MUTEX_FULL_CHAINWALK);
  1487. if (ret && !rt_mutex_owner(lock)) {
  1488. /*
  1489. * Reset the return value. We might have
  1490. * returned with -EDEADLK and the owner
  1491. * released the lock while we were walking the
  1492. * pi chain. Let the waiter sort it out.
  1493. */
  1494. ret = 0;
  1495. }
  1496. if (unlikely(ret))
  1497. remove_waiter(lock, waiter);
  1498. raw_spin_unlock_irq(&lock->wait_lock);
  1499. debug_rt_mutex_print_deadlock(waiter);
  1500. return ret;
  1501. }
  1502. /**
  1503. * rt_mutex_next_owner - return the next owner of the lock
  1504. *
  1505. * @lock: the rt lock query
  1506. *
  1507. * Returns the next owner of the lock or NULL
  1508. *
  1509. * Caller has to serialize against other accessors to the lock
  1510. * itself.
  1511. *
  1512. * Special API call for PI-futex support
  1513. */
  1514. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1515. {
  1516. if (!rt_mutex_has_waiters(lock))
  1517. return NULL;
  1518. return rt_mutex_top_waiter(lock)->task;
  1519. }
  1520. /**
  1521. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1522. * @lock: the rt_mutex we were woken on
  1523. * @to: the timeout, null if none. hrtimer should already have
  1524. * been started.
  1525. * @waiter: the pre-initialized rt_mutex_waiter
  1526. *
  1527. * Complete the lock acquisition started our behalf by another thread.
  1528. *
  1529. * Returns:
  1530. * 0 - success
  1531. * <0 - error, one of -EINTR, -ETIMEDOUT
  1532. *
  1533. * Special API call for PI-futex requeue support
  1534. */
  1535. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1536. struct hrtimer_sleeper *to,
  1537. struct rt_mutex_waiter *waiter)
  1538. {
  1539. int ret;
  1540. raw_spin_lock_irq(&lock->wait_lock);
  1541. set_current_state(TASK_INTERRUPTIBLE);
  1542. /* sleep on the mutex */
  1543. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1544. if (unlikely(ret))
  1545. remove_waiter(lock, waiter);
  1546. /*
  1547. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1548. * have to fix that up.
  1549. */
  1550. fixup_rt_mutex_waiters(lock);
  1551. raw_spin_unlock_irq(&lock->wait_lock);
  1552. return ret;
  1553. }