rtmutex.c 47 KB

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