rtmutex.c 51 KB

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