rtmutex.c 43 KB

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