rtmutex.c 30 KB

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