rtmutex.c 29 KB

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