rtmutex.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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/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. #else
  78. # define rt_mutex_cmpxchg(l,c,n) (0)
  79. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  80. {
  81. lock->owner = (struct task_struct *)
  82. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  83. }
  84. #endif
  85. static inline int
  86. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  87. struct rt_mutex_waiter *right)
  88. {
  89. if (left->prio < right->prio)
  90. return 1;
  91. /*
  92. * If both waiters have dl_prio(), we check the deadlines of the
  93. * associated tasks.
  94. * If left waiter has a dl_prio(), and we didn't return 1 above,
  95. * then right waiter has a dl_prio() too.
  96. */
  97. if (dl_prio(left->prio))
  98. return (left->task->dl.deadline < right->task->dl.deadline);
  99. return 0;
  100. }
  101. static void
  102. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  103. {
  104. struct rb_node **link = &lock->waiters.rb_node;
  105. struct rb_node *parent = NULL;
  106. struct rt_mutex_waiter *entry;
  107. int leftmost = 1;
  108. while (*link) {
  109. parent = *link;
  110. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  111. if (rt_mutex_waiter_less(waiter, entry)) {
  112. link = &parent->rb_left;
  113. } else {
  114. link = &parent->rb_right;
  115. leftmost = 0;
  116. }
  117. }
  118. if (leftmost)
  119. lock->waiters_leftmost = &waiter->tree_entry;
  120. rb_link_node(&waiter->tree_entry, parent, link);
  121. rb_insert_color(&waiter->tree_entry, &lock->waiters);
  122. }
  123. static void
  124. rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  125. {
  126. if (RB_EMPTY_NODE(&waiter->tree_entry))
  127. return;
  128. if (lock->waiters_leftmost == &waiter->tree_entry)
  129. lock->waiters_leftmost = rb_next(&waiter->tree_entry);
  130. rb_erase(&waiter->tree_entry, &lock->waiters);
  131. RB_CLEAR_NODE(&waiter->tree_entry);
  132. }
  133. static void
  134. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  135. {
  136. struct rb_node **link = &task->pi_waiters.rb_node;
  137. struct rb_node *parent = NULL;
  138. struct rt_mutex_waiter *entry;
  139. int leftmost = 1;
  140. while (*link) {
  141. parent = *link;
  142. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  143. if (rt_mutex_waiter_less(waiter, entry)) {
  144. link = &parent->rb_left;
  145. } else {
  146. link = &parent->rb_right;
  147. leftmost = 0;
  148. }
  149. }
  150. if (leftmost)
  151. task->pi_waiters_leftmost = &waiter->pi_tree_entry;
  152. rb_link_node(&waiter->pi_tree_entry, parent, link);
  153. rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
  154. }
  155. static void
  156. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  157. {
  158. if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
  159. return;
  160. if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
  161. task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
  162. rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
  163. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  164. }
  165. /*
  166. * Calculate task priority from the waiter tree priority
  167. *
  168. * Return task->normal_prio when the waiter tree is empty or when
  169. * the waiter is not allowed to do priority boosting
  170. */
  171. int rt_mutex_getprio(struct task_struct *task)
  172. {
  173. if (likely(!task_has_pi_waiters(task)))
  174. return task->normal_prio;
  175. return min(task_top_pi_waiter(task)->prio,
  176. task->normal_prio);
  177. }
  178. struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  179. {
  180. if (likely(!task_has_pi_waiters(task)))
  181. return NULL;
  182. return task_top_pi_waiter(task)->task;
  183. }
  184. /*
  185. * Called by sched_setscheduler() to check whether the priority change
  186. * is overruled by a possible priority boosting.
  187. */
  188. int rt_mutex_check_prio(struct task_struct *task, int newprio)
  189. {
  190. if (!task_has_pi_waiters(task))
  191. return 0;
  192. return task_top_pi_waiter(task)->task->prio <= newprio;
  193. }
  194. /*
  195. * Adjust the priority of a task, after its pi_waiters got modified.
  196. *
  197. * This can be both boosting and unboosting. task->pi_lock must be held.
  198. */
  199. static void __rt_mutex_adjust_prio(struct task_struct *task)
  200. {
  201. int prio = rt_mutex_getprio(task);
  202. if (task->prio != prio || dl_prio(prio))
  203. rt_mutex_setprio(task, prio);
  204. }
  205. /*
  206. * Adjust task priority (undo boosting). Called from the exit path of
  207. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  208. *
  209. * (Note: We do this outside of the protection of lock->wait_lock to
  210. * allow the lock to be taken while or before we readjust the priority
  211. * of task. We do not use the spin_xx_mutex() variants here as we are
  212. * outside of the debug path.)
  213. */
  214. static void rt_mutex_adjust_prio(struct task_struct *task)
  215. {
  216. unsigned long flags;
  217. raw_spin_lock_irqsave(&task->pi_lock, flags);
  218. __rt_mutex_adjust_prio(task);
  219. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  220. }
  221. /*
  222. * Max number of times we'll walk the boosting chain:
  223. */
  224. int max_lock_depth = 1024;
  225. /*
  226. * Adjust the priority chain. Also used for deadlock detection.
  227. * Decreases task's usage by one - may thus free the task.
  228. *
  229. * @task: the task owning the mutex (owner) for which a chain walk is probably
  230. * needed
  231. * @deadlock_detect: do we have to carry out deadlock detection?
  232. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  233. * things for a task that has just got its priority adjusted, and
  234. * is waiting on a mutex)
  235. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  236. * its priority to the mutex owner (can be NULL in the case
  237. * depicted above or if the top waiter is gone away and we are
  238. * actually deboosting the owner)
  239. * @top_task: the current top waiter
  240. *
  241. * Returns 0 or -EDEADLK.
  242. */
  243. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  244. int deadlock_detect,
  245. struct rt_mutex *orig_lock,
  246. struct rt_mutex_waiter *orig_waiter,
  247. struct task_struct *top_task)
  248. {
  249. struct rt_mutex *lock;
  250. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  251. int detect_deadlock, ret = 0, depth = 0;
  252. unsigned long flags;
  253. detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter,
  254. deadlock_detect);
  255. /*
  256. * The (de)boosting is a step by step approach with a lot of
  257. * pitfalls. We want this to be preemptible and we want hold a
  258. * maximum of two locks per step. So we have to check
  259. * carefully whether things change under us.
  260. */
  261. again:
  262. if (++depth > max_lock_depth) {
  263. static int prev_max;
  264. /*
  265. * Print this only once. If the admin changes the limit,
  266. * print a new message when reaching the limit again.
  267. */
  268. if (prev_max != max_lock_depth) {
  269. prev_max = max_lock_depth;
  270. printk(KERN_WARNING "Maximum lock depth %d reached "
  271. "task: %s (%d)\n", max_lock_depth,
  272. top_task->comm, task_pid_nr(top_task));
  273. }
  274. put_task_struct(task);
  275. return deadlock_detect ? -EDEADLK : 0;
  276. }
  277. retry:
  278. /*
  279. * Task can not go away as we did a get_task() before !
  280. */
  281. raw_spin_lock_irqsave(&task->pi_lock, flags);
  282. waiter = task->pi_blocked_on;
  283. /*
  284. * Check whether the end of the boosting chain has been
  285. * reached or the state of the chain has changed while we
  286. * dropped the locks.
  287. */
  288. if (!waiter)
  289. goto out_unlock_pi;
  290. /*
  291. * Check the orig_waiter state. After we dropped the locks,
  292. * the previous owner of the lock might have released the lock.
  293. */
  294. if (orig_waiter && !rt_mutex_owner(orig_lock))
  295. goto out_unlock_pi;
  296. /*
  297. * Drop out, when the task has no waiters. Note,
  298. * top_waiter can be NULL, when we are in the deboosting
  299. * mode!
  300. */
  301. if (top_waiter && (!task_has_pi_waiters(task) ||
  302. top_waiter != task_top_pi_waiter(task)))
  303. goto out_unlock_pi;
  304. /*
  305. * When deadlock detection is off then we check, if further
  306. * priority adjustment is necessary.
  307. */
  308. if (!detect_deadlock && waiter->prio == task->prio)
  309. goto out_unlock_pi;
  310. lock = waiter->lock;
  311. if (!raw_spin_trylock(&lock->wait_lock)) {
  312. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  313. cpu_relax();
  314. goto retry;
  315. }
  316. /* Deadlock detection */
  317. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  318. debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock);
  319. raw_spin_unlock(&lock->wait_lock);
  320. ret = deadlock_detect ? -EDEADLK : 0;
  321. goto out_unlock_pi;
  322. }
  323. top_waiter = rt_mutex_top_waiter(lock);
  324. /* Requeue the waiter */
  325. rt_mutex_dequeue(lock, waiter);
  326. waiter->prio = task->prio;
  327. rt_mutex_enqueue(lock, waiter);
  328. /* Release the task */
  329. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  330. if (!rt_mutex_owner(lock)) {
  331. /*
  332. * If the requeue above changed the top waiter, then we need
  333. * to wake the new top waiter up to try to get the lock.
  334. */
  335. if (top_waiter != rt_mutex_top_waiter(lock))
  336. wake_up_process(rt_mutex_top_waiter(lock)->task);
  337. raw_spin_unlock(&lock->wait_lock);
  338. goto out_put_task;
  339. }
  340. put_task_struct(task);
  341. /* Grab the next task */
  342. task = rt_mutex_owner(lock);
  343. get_task_struct(task);
  344. raw_spin_lock_irqsave(&task->pi_lock, flags);
  345. if (waiter == rt_mutex_top_waiter(lock)) {
  346. /* Boost the owner */
  347. rt_mutex_dequeue_pi(task, top_waiter);
  348. rt_mutex_enqueue_pi(task, waiter);
  349. __rt_mutex_adjust_prio(task);
  350. } else if (top_waiter == waiter) {
  351. /* Deboost the owner */
  352. rt_mutex_dequeue_pi(task, waiter);
  353. waiter = rt_mutex_top_waiter(lock);
  354. rt_mutex_enqueue_pi(task, waiter);
  355. __rt_mutex_adjust_prio(task);
  356. }
  357. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  358. top_waiter = rt_mutex_top_waiter(lock);
  359. raw_spin_unlock(&lock->wait_lock);
  360. if (!detect_deadlock && waiter != top_waiter)
  361. goto out_put_task;
  362. goto again;
  363. out_unlock_pi:
  364. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  365. out_put_task:
  366. put_task_struct(task);
  367. return ret;
  368. }
  369. /*
  370. * Try to take an rt-mutex
  371. *
  372. * Must be called with lock->wait_lock held.
  373. *
  374. * @lock: the lock to be acquired.
  375. * @task: the task which wants to acquire the lock
  376. * @waiter: the waiter that is queued to the lock's wait list. (could be NULL)
  377. */
  378. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  379. struct rt_mutex_waiter *waiter)
  380. {
  381. /*
  382. * We have to be careful here if the atomic speedups are
  383. * enabled, such that, when
  384. * - no other waiter is on the lock
  385. * - the lock has been released since we did the cmpxchg
  386. * the lock can be released or taken while we are doing the
  387. * checks and marking the lock with RT_MUTEX_HAS_WAITERS.
  388. *
  389. * The atomic acquire/release aware variant of
  390. * mark_rt_mutex_waiters uses a cmpxchg loop. After setting
  391. * the WAITERS bit, the atomic release / acquire can not
  392. * happen anymore and lock->wait_lock protects us from the
  393. * non-atomic case.
  394. *
  395. * Note, that this might set lock->owner =
  396. * RT_MUTEX_HAS_WAITERS in the case the lock is not contended
  397. * any more. This is fixed up when we take the ownership.
  398. * This is the transitional state explained at the top of this file.
  399. */
  400. mark_rt_mutex_waiters(lock);
  401. if (rt_mutex_owner(lock))
  402. return 0;
  403. /*
  404. * It will get the lock because of one of these conditions:
  405. * 1) there is no waiter
  406. * 2) higher priority than waiters
  407. * 3) it is top waiter
  408. */
  409. if (rt_mutex_has_waiters(lock)) {
  410. if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
  411. if (!waiter || waiter != rt_mutex_top_waiter(lock))
  412. return 0;
  413. }
  414. }
  415. if (waiter || rt_mutex_has_waiters(lock)) {
  416. unsigned long flags;
  417. struct rt_mutex_waiter *top;
  418. raw_spin_lock_irqsave(&task->pi_lock, flags);
  419. /* remove the queued waiter. */
  420. if (waiter) {
  421. rt_mutex_dequeue(lock, waiter);
  422. task->pi_blocked_on = NULL;
  423. }
  424. /*
  425. * We have to enqueue the top waiter(if it exists) into
  426. * task->pi_waiters list.
  427. */
  428. if (rt_mutex_has_waiters(lock)) {
  429. top = rt_mutex_top_waiter(lock);
  430. rt_mutex_enqueue_pi(task, top);
  431. }
  432. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  433. }
  434. /* We got the lock. */
  435. debug_rt_mutex_lock(lock);
  436. rt_mutex_set_owner(lock, task);
  437. rt_mutex_deadlock_account_lock(lock, task);
  438. return 1;
  439. }
  440. /*
  441. * Task blocks on lock.
  442. *
  443. * Prepare waiter and propagate pi chain
  444. *
  445. * This must be called with lock->wait_lock held.
  446. */
  447. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  448. struct rt_mutex_waiter *waiter,
  449. struct task_struct *task,
  450. int detect_deadlock)
  451. {
  452. struct task_struct *owner = rt_mutex_owner(lock);
  453. struct rt_mutex_waiter *top_waiter = waiter;
  454. unsigned long flags;
  455. int chain_walk = 0, res;
  456. raw_spin_lock_irqsave(&task->pi_lock, flags);
  457. __rt_mutex_adjust_prio(task);
  458. waiter->task = task;
  459. waiter->lock = lock;
  460. waiter->prio = task->prio;
  461. /* Get the top priority waiter on the lock */
  462. if (rt_mutex_has_waiters(lock))
  463. top_waiter = rt_mutex_top_waiter(lock);
  464. rt_mutex_enqueue(lock, waiter);
  465. task->pi_blocked_on = waiter;
  466. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  467. if (!owner)
  468. return 0;
  469. if (waiter == rt_mutex_top_waiter(lock)) {
  470. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  471. rt_mutex_dequeue_pi(owner, top_waiter);
  472. rt_mutex_enqueue_pi(owner, waiter);
  473. __rt_mutex_adjust_prio(owner);
  474. if (owner->pi_blocked_on)
  475. chain_walk = 1;
  476. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  477. }
  478. else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock))
  479. chain_walk = 1;
  480. if (!chain_walk)
  481. return 0;
  482. /*
  483. * The owner can't disappear while holding a lock,
  484. * so the owner struct is protected by wait_lock.
  485. * Gets dropped in rt_mutex_adjust_prio_chain()!
  486. */
  487. get_task_struct(owner);
  488. raw_spin_unlock(&lock->wait_lock);
  489. res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter,
  490. task);
  491. raw_spin_lock(&lock->wait_lock);
  492. return res;
  493. }
  494. /*
  495. * Wake up the next waiter on the lock.
  496. *
  497. * Remove the top waiter from the current tasks waiter list and wake it up.
  498. *
  499. * Called with lock->wait_lock held.
  500. */
  501. static void wakeup_next_waiter(struct rt_mutex *lock)
  502. {
  503. struct rt_mutex_waiter *waiter;
  504. unsigned long flags;
  505. raw_spin_lock_irqsave(&current->pi_lock, flags);
  506. waiter = rt_mutex_top_waiter(lock);
  507. /*
  508. * Remove it from current->pi_waiters. We do not adjust a
  509. * possible priority boost right now. We execute wakeup in the
  510. * boosted mode and go back to normal after releasing
  511. * lock->wait_lock.
  512. */
  513. rt_mutex_dequeue_pi(current, waiter);
  514. rt_mutex_set_owner(lock, NULL);
  515. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  516. wake_up_process(waiter->task);
  517. }
  518. /*
  519. * Remove a waiter from a lock and give up
  520. *
  521. * Must be called with lock->wait_lock held and
  522. * have just failed to try_to_take_rt_mutex().
  523. */
  524. static void remove_waiter(struct rt_mutex *lock,
  525. struct rt_mutex_waiter *waiter)
  526. {
  527. int first = (waiter == rt_mutex_top_waiter(lock));
  528. struct task_struct *owner = rt_mutex_owner(lock);
  529. unsigned long flags;
  530. int chain_walk = 0;
  531. raw_spin_lock_irqsave(&current->pi_lock, flags);
  532. rt_mutex_dequeue(lock, waiter);
  533. current->pi_blocked_on = NULL;
  534. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  535. if (!owner)
  536. return;
  537. if (first) {
  538. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  539. rt_mutex_dequeue_pi(owner, waiter);
  540. if (rt_mutex_has_waiters(lock)) {
  541. struct rt_mutex_waiter *next;
  542. next = rt_mutex_top_waiter(lock);
  543. rt_mutex_enqueue_pi(owner, next);
  544. }
  545. __rt_mutex_adjust_prio(owner);
  546. if (owner->pi_blocked_on)
  547. chain_walk = 1;
  548. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  549. }
  550. if (!chain_walk)
  551. return;
  552. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  553. get_task_struct(owner);
  554. raw_spin_unlock(&lock->wait_lock);
  555. rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current);
  556. raw_spin_lock(&lock->wait_lock);
  557. }
  558. /*
  559. * Recheck the pi chain, in case we got a priority setting
  560. *
  561. * Called from sched_setscheduler
  562. */
  563. void rt_mutex_adjust_pi(struct task_struct *task)
  564. {
  565. struct rt_mutex_waiter *waiter;
  566. unsigned long flags;
  567. raw_spin_lock_irqsave(&task->pi_lock, flags);
  568. waiter = task->pi_blocked_on;
  569. if (!waiter || (waiter->prio == task->prio &&
  570. !dl_prio(task->prio))) {
  571. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  572. return;
  573. }
  574. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  575. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  576. get_task_struct(task);
  577. rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task);
  578. }
  579. /**
  580. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  581. * @lock: the rt_mutex to take
  582. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  583. * or TASK_UNINTERRUPTIBLE)
  584. * @timeout: the pre-initialized and started timer, or NULL for none
  585. * @waiter: the pre-initialized rt_mutex_waiter
  586. *
  587. * lock->wait_lock must be held by the caller.
  588. */
  589. static int __sched
  590. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  591. struct hrtimer_sleeper *timeout,
  592. struct rt_mutex_waiter *waiter)
  593. {
  594. int ret = 0;
  595. for (;;) {
  596. /* Try to acquire the lock: */
  597. if (try_to_take_rt_mutex(lock, current, waiter))
  598. break;
  599. /*
  600. * TASK_INTERRUPTIBLE checks for signals and
  601. * timeout. Ignored otherwise.
  602. */
  603. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  604. /* Signal pending? */
  605. if (signal_pending(current))
  606. ret = -EINTR;
  607. if (timeout && !timeout->task)
  608. ret = -ETIMEDOUT;
  609. if (ret)
  610. break;
  611. }
  612. raw_spin_unlock(&lock->wait_lock);
  613. debug_rt_mutex_print_deadlock(waiter);
  614. schedule_rt_mutex(lock);
  615. raw_spin_lock(&lock->wait_lock);
  616. set_current_state(state);
  617. }
  618. return ret;
  619. }
  620. /*
  621. * Slow path lock function:
  622. */
  623. static int __sched
  624. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  625. struct hrtimer_sleeper *timeout,
  626. int detect_deadlock)
  627. {
  628. struct rt_mutex_waiter waiter;
  629. int ret = 0;
  630. debug_rt_mutex_init_waiter(&waiter);
  631. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  632. RB_CLEAR_NODE(&waiter.tree_entry);
  633. raw_spin_lock(&lock->wait_lock);
  634. /* Try to acquire the lock again: */
  635. if (try_to_take_rt_mutex(lock, current, NULL)) {
  636. raw_spin_unlock(&lock->wait_lock);
  637. return 0;
  638. }
  639. set_current_state(state);
  640. /* Setup the timer, when timeout != NULL */
  641. if (unlikely(timeout)) {
  642. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  643. if (!hrtimer_active(&timeout->timer))
  644. timeout->task = NULL;
  645. }
  646. ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
  647. if (likely(!ret))
  648. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  649. set_current_state(TASK_RUNNING);
  650. if (unlikely(ret))
  651. remove_waiter(lock, &waiter);
  652. /*
  653. * try_to_take_rt_mutex() sets the waiter bit
  654. * unconditionally. We might have to fix that up.
  655. */
  656. fixup_rt_mutex_waiters(lock);
  657. raw_spin_unlock(&lock->wait_lock);
  658. /* Remove pending timer: */
  659. if (unlikely(timeout))
  660. hrtimer_cancel(&timeout->timer);
  661. debug_rt_mutex_free_waiter(&waiter);
  662. return ret;
  663. }
  664. /*
  665. * Slow path try-lock function:
  666. */
  667. static inline int
  668. rt_mutex_slowtrylock(struct rt_mutex *lock)
  669. {
  670. int ret = 0;
  671. raw_spin_lock(&lock->wait_lock);
  672. if (likely(rt_mutex_owner(lock) != current)) {
  673. ret = try_to_take_rt_mutex(lock, current, NULL);
  674. /*
  675. * try_to_take_rt_mutex() sets the lock waiters
  676. * bit unconditionally. Clean this up.
  677. */
  678. fixup_rt_mutex_waiters(lock);
  679. }
  680. raw_spin_unlock(&lock->wait_lock);
  681. return ret;
  682. }
  683. /*
  684. * Slow path to release a rt-mutex:
  685. */
  686. static void __sched
  687. rt_mutex_slowunlock(struct rt_mutex *lock)
  688. {
  689. raw_spin_lock(&lock->wait_lock);
  690. debug_rt_mutex_unlock(lock);
  691. rt_mutex_deadlock_account_unlock(current);
  692. if (!rt_mutex_has_waiters(lock)) {
  693. lock->owner = NULL;
  694. raw_spin_unlock(&lock->wait_lock);
  695. return;
  696. }
  697. wakeup_next_waiter(lock);
  698. raw_spin_unlock(&lock->wait_lock);
  699. /* Undo pi boosting if necessary: */
  700. rt_mutex_adjust_prio(current);
  701. }
  702. /*
  703. * debug aware fast / slowpath lock,trylock,unlock
  704. *
  705. * The atomic acquire/release ops are compiled away, when either the
  706. * architecture does not support cmpxchg or when debugging is enabled.
  707. */
  708. static inline int
  709. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  710. int detect_deadlock,
  711. int (*slowfn)(struct rt_mutex *lock, int state,
  712. struct hrtimer_sleeper *timeout,
  713. int detect_deadlock))
  714. {
  715. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  716. rt_mutex_deadlock_account_lock(lock, current);
  717. return 0;
  718. } else
  719. return slowfn(lock, state, NULL, detect_deadlock);
  720. }
  721. static inline int
  722. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  723. struct hrtimer_sleeper *timeout, int detect_deadlock,
  724. int (*slowfn)(struct rt_mutex *lock, int state,
  725. struct hrtimer_sleeper *timeout,
  726. int detect_deadlock))
  727. {
  728. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  729. rt_mutex_deadlock_account_lock(lock, current);
  730. return 0;
  731. } else
  732. return slowfn(lock, state, timeout, detect_deadlock);
  733. }
  734. static inline int
  735. rt_mutex_fasttrylock(struct rt_mutex *lock,
  736. int (*slowfn)(struct rt_mutex *lock))
  737. {
  738. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  739. rt_mutex_deadlock_account_lock(lock, current);
  740. return 1;
  741. }
  742. return slowfn(lock);
  743. }
  744. static inline void
  745. rt_mutex_fastunlock(struct rt_mutex *lock,
  746. void (*slowfn)(struct rt_mutex *lock))
  747. {
  748. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  749. rt_mutex_deadlock_account_unlock(current);
  750. else
  751. slowfn(lock);
  752. }
  753. /**
  754. * rt_mutex_lock - lock a rt_mutex
  755. *
  756. * @lock: the rt_mutex to be locked
  757. */
  758. void __sched rt_mutex_lock(struct rt_mutex *lock)
  759. {
  760. might_sleep();
  761. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  762. }
  763. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  764. /**
  765. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  766. *
  767. * @lock: the rt_mutex to be locked
  768. * @detect_deadlock: deadlock detection on/off
  769. *
  770. * Returns:
  771. * 0 on success
  772. * -EINTR when interrupted by a signal
  773. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  774. */
  775. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  776. int detect_deadlock)
  777. {
  778. might_sleep();
  779. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  780. detect_deadlock, rt_mutex_slowlock);
  781. }
  782. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  783. /**
  784. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  785. * the timeout structure is provided
  786. * by the caller
  787. *
  788. * @lock: the rt_mutex to be locked
  789. * @timeout: timeout structure or NULL (no timeout)
  790. * @detect_deadlock: deadlock detection on/off
  791. *
  792. * Returns:
  793. * 0 on success
  794. * -EINTR when interrupted by a signal
  795. * -ETIMEDOUT when the timeout expired
  796. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  797. */
  798. int
  799. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  800. int detect_deadlock)
  801. {
  802. might_sleep();
  803. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  804. detect_deadlock, rt_mutex_slowlock);
  805. }
  806. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  807. /**
  808. * rt_mutex_trylock - try to lock a rt_mutex
  809. *
  810. * @lock: the rt_mutex to be locked
  811. *
  812. * Returns 1 on success and 0 on contention
  813. */
  814. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  815. {
  816. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  817. }
  818. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  819. /**
  820. * rt_mutex_unlock - unlock a rt_mutex
  821. *
  822. * @lock: the rt_mutex to be unlocked
  823. */
  824. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  825. {
  826. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  827. }
  828. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  829. /**
  830. * rt_mutex_destroy - mark a mutex unusable
  831. * @lock: the mutex to be destroyed
  832. *
  833. * This function marks the mutex uninitialized, and any subsequent
  834. * use of the mutex is forbidden. The mutex must not be locked when
  835. * this function is called.
  836. */
  837. void rt_mutex_destroy(struct rt_mutex *lock)
  838. {
  839. WARN_ON(rt_mutex_is_locked(lock));
  840. #ifdef CONFIG_DEBUG_RT_MUTEXES
  841. lock->magic = NULL;
  842. #endif
  843. }
  844. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  845. /**
  846. * __rt_mutex_init - initialize the rt lock
  847. *
  848. * @lock: the rt lock to be initialized
  849. *
  850. * Initialize the rt lock to unlocked state.
  851. *
  852. * Initializing of a locked rt lock is not allowed
  853. */
  854. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  855. {
  856. lock->owner = NULL;
  857. raw_spin_lock_init(&lock->wait_lock);
  858. lock->waiters = RB_ROOT;
  859. lock->waiters_leftmost = NULL;
  860. debug_rt_mutex_init(lock, name);
  861. }
  862. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  863. /**
  864. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  865. * proxy owner
  866. *
  867. * @lock: the rt_mutex to be locked
  868. * @proxy_owner:the task to set as owner
  869. *
  870. * No locking. Caller has to do serializing itself
  871. * Special API call for PI-futex support
  872. */
  873. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  874. struct task_struct *proxy_owner)
  875. {
  876. __rt_mutex_init(lock, NULL);
  877. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  878. rt_mutex_set_owner(lock, proxy_owner);
  879. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  880. }
  881. /**
  882. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  883. *
  884. * @lock: the rt_mutex to be locked
  885. *
  886. * No locking. Caller has to do serializing itself
  887. * Special API call for PI-futex support
  888. */
  889. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  890. struct task_struct *proxy_owner)
  891. {
  892. debug_rt_mutex_proxy_unlock(lock);
  893. rt_mutex_set_owner(lock, NULL);
  894. rt_mutex_deadlock_account_unlock(proxy_owner);
  895. }
  896. /**
  897. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  898. * @lock: the rt_mutex to take
  899. * @waiter: the pre-initialized rt_mutex_waiter
  900. * @task: the task to prepare
  901. * @detect_deadlock: perform deadlock detection (1) or not (0)
  902. *
  903. * Returns:
  904. * 0 - task blocked on lock
  905. * 1 - acquired the lock for task, caller should wake it up
  906. * <0 - error
  907. *
  908. * Special API call for FUTEX_REQUEUE_PI support.
  909. */
  910. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  911. struct rt_mutex_waiter *waiter,
  912. struct task_struct *task, int detect_deadlock)
  913. {
  914. int ret;
  915. raw_spin_lock(&lock->wait_lock);
  916. if (try_to_take_rt_mutex(lock, task, NULL)) {
  917. raw_spin_unlock(&lock->wait_lock);
  918. return 1;
  919. }
  920. ret = task_blocks_on_rt_mutex(lock, waiter, task, detect_deadlock);
  921. if (ret && !rt_mutex_owner(lock)) {
  922. /*
  923. * Reset the return value. We might have
  924. * returned with -EDEADLK and the owner
  925. * released the lock while we were walking the
  926. * pi chain. Let the waiter sort it out.
  927. */
  928. ret = 0;
  929. }
  930. if (unlikely(ret))
  931. remove_waiter(lock, waiter);
  932. raw_spin_unlock(&lock->wait_lock);
  933. debug_rt_mutex_print_deadlock(waiter);
  934. return ret;
  935. }
  936. /**
  937. * rt_mutex_next_owner - return the next owner of the lock
  938. *
  939. * @lock: the rt lock query
  940. *
  941. * Returns the next owner of the lock or NULL
  942. *
  943. * Caller has to serialize against other accessors to the lock
  944. * itself.
  945. *
  946. * Special API call for PI-futex support
  947. */
  948. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  949. {
  950. if (!rt_mutex_has_waiters(lock))
  951. return NULL;
  952. return rt_mutex_top_waiter(lock)->task;
  953. }
  954. /**
  955. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  956. * @lock: the rt_mutex we were woken on
  957. * @to: the timeout, null if none. hrtimer should already have
  958. * been started.
  959. * @waiter: the pre-initialized rt_mutex_waiter
  960. * @detect_deadlock: perform deadlock detection (1) or not (0)
  961. *
  962. * Complete the lock acquisition started our behalf by another thread.
  963. *
  964. * Returns:
  965. * 0 - success
  966. * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
  967. *
  968. * Special API call for PI-futex requeue support
  969. */
  970. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  971. struct hrtimer_sleeper *to,
  972. struct rt_mutex_waiter *waiter,
  973. int detect_deadlock)
  974. {
  975. int ret;
  976. raw_spin_lock(&lock->wait_lock);
  977. set_current_state(TASK_INTERRUPTIBLE);
  978. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  979. set_current_state(TASK_RUNNING);
  980. if (unlikely(ret))
  981. remove_waiter(lock, waiter);
  982. /*
  983. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  984. * have to fix that up.
  985. */
  986. fixup_rt_mutex_waiters(lock);
  987. raw_spin_unlock(&lock->wait_lock);
  988. return ret;
  989. }