rtmutex.c 26 KB

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