rtmutex.c 44 KB

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