rtmutex.c 50 KB

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