rtmutex.c 48 KB

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