rtmutex.c 46 KB

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