rtmutex.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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 -EDEADLK;
  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 = -EDEADLK;
  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 (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. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  644. struct rt_mutex_waiter *w)
  645. {
  646. /*
  647. * If the result is not -EDEADLOCK or the caller requested
  648. * deadlock detection, nothing to do here.
  649. */
  650. if (res != -EDEADLOCK || detect_deadlock)
  651. return;
  652. /*
  653. * Yell lowdly and stop the task right here.
  654. */
  655. rt_mutex_print_deadlock(w);
  656. while (1) {
  657. set_current_state(TASK_INTERRUPTIBLE);
  658. schedule();
  659. }
  660. }
  661. /*
  662. * Slow path lock function:
  663. */
  664. static int __sched
  665. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  666. struct hrtimer_sleeper *timeout,
  667. int detect_deadlock)
  668. {
  669. struct rt_mutex_waiter waiter;
  670. int ret = 0;
  671. debug_rt_mutex_init_waiter(&waiter);
  672. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  673. RB_CLEAR_NODE(&waiter.tree_entry);
  674. raw_spin_lock(&lock->wait_lock);
  675. /* Try to acquire the lock again: */
  676. if (try_to_take_rt_mutex(lock, current, NULL)) {
  677. raw_spin_unlock(&lock->wait_lock);
  678. return 0;
  679. }
  680. set_current_state(state);
  681. /* Setup the timer, when timeout != NULL */
  682. if (unlikely(timeout)) {
  683. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  684. if (!hrtimer_active(&timeout->timer))
  685. timeout->task = NULL;
  686. }
  687. ret = task_blocks_on_rt_mutex(lock, &waiter, current, detect_deadlock);
  688. if (likely(!ret))
  689. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  690. set_current_state(TASK_RUNNING);
  691. if (unlikely(ret)) {
  692. remove_waiter(lock, &waiter);
  693. rt_mutex_handle_deadlock(ret, detect_deadlock, &waiter);
  694. }
  695. /*
  696. * try_to_take_rt_mutex() sets the waiter bit
  697. * unconditionally. We might have to fix that up.
  698. */
  699. fixup_rt_mutex_waiters(lock);
  700. raw_spin_unlock(&lock->wait_lock);
  701. /* Remove pending timer: */
  702. if (unlikely(timeout))
  703. hrtimer_cancel(&timeout->timer);
  704. debug_rt_mutex_free_waiter(&waiter);
  705. return ret;
  706. }
  707. /*
  708. * Slow path try-lock function:
  709. */
  710. static inline int
  711. rt_mutex_slowtrylock(struct rt_mutex *lock)
  712. {
  713. int ret = 0;
  714. raw_spin_lock(&lock->wait_lock);
  715. if (likely(rt_mutex_owner(lock) != current)) {
  716. ret = try_to_take_rt_mutex(lock, current, NULL);
  717. /*
  718. * try_to_take_rt_mutex() sets the lock waiters
  719. * bit unconditionally. Clean this up.
  720. */
  721. fixup_rt_mutex_waiters(lock);
  722. }
  723. raw_spin_unlock(&lock->wait_lock);
  724. return ret;
  725. }
  726. /*
  727. * Slow path to release a rt-mutex:
  728. */
  729. static void __sched
  730. rt_mutex_slowunlock(struct rt_mutex *lock)
  731. {
  732. raw_spin_lock(&lock->wait_lock);
  733. debug_rt_mutex_unlock(lock);
  734. rt_mutex_deadlock_account_unlock(current);
  735. if (!rt_mutex_has_waiters(lock)) {
  736. lock->owner = NULL;
  737. raw_spin_unlock(&lock->wait_lock);
  738. return;
  739. }
  740. wakeup_next_waiter(lock);
  741. raw_spin_unlock(&lock->wait_lock);
  742. /* Undo pi boosting if necessary: */
  743. rt_mutex_adjust_prio(current);
  744. }
  745. /*
  746. * debug aware fast / slowpath lock,trylock,unlock
  747. *
  748. * The atomic acquire/release ops are compiled away, when either the
  749. * architecture does not support cmpxchg or when debugging is enabled.
  750. */
  751. static inline int
  752. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  753. int detect_deadlock,
  754. int (*slowfn)(struct rt_mutex *lock, int state,
  755. struct hrtimer_sleeper *timeout,
  756. int detect_deadlock))
  757. {
  758. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  759. rt_mutex_deadlock_account_lock(lock, current);
  760. return 0;
  761. } else
  762. return slowfn(lock, state, NULL, detect_deadlock);
  763. }
  764. static inline int
  765. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  766. struct hrtimer_sleeper *timeout, int detect_deadlock,
  767. int (*slowfn)(struct rt_mutex *lock, int state,
  768. struct hrtimer_sleeper *timeout,
  769. int detect_deadlock))
  770. {
  771. if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  772. rt_mutex_deadlock_account_lock(lock, current);
  773. return 0;
  774. } else
  775. return slowfn(lock, state, timeout, detect_deadlock);
  776. }
  777. static inline int
  778. rt_mutex_fasttrylock(struct rt_mutex *lock,
  779. int (*slowfn)(struct rt_mutex *lock))
  780. {
  781. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  782. rt_mutex_deadlock_account_lock(lock, current);
  783. return 1;
  784. }
  785. return slowfn(lock);
  786. }
  787. static inline void
  788. rt_mutex_fastunlock(struct rt_mutex *lock,
  789. void (*slowfn)(struct rt_mutex *lock))
  790. {
  791. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  792. rt_mutex_deadlock_account_unlock(current);
  793. else
  794. slowfn(lock);
  795. }
  796. /**
  797. * rt_mutex_lock - lock a rt_mutex
  798. *
  799. * @lock: the rt_mutex to be locked
  800. */
  801. void __sched rt_mutex_lock(struct rt_mutex *lock)
  802. {
  803. might_sleep();
  804. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock);
  805. }
  806. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  807. /**
  808. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  809. *
  810. * @lock: the rt_mutex to be locked
  811. * @detect_deadlock: deadlock detection on/off
  812. *
  813. * Returns:
  814. * 0 on success
  815. * -EINTR when interrupted by a signal
  816. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  817. */
  818. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock,
  819. int detect_deadlock)
  820. {
  821. might_sleep();
  822. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE,
  823. detect_deadlock, rt_mutex_slowlock);
  824. }
  825. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  826. /**
  827. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  828. * the timeout structure is provided
  829. * by the caller
  830. *
  831. * @lock: the rt_mutex to be locked
  832. * @timeout: timeout structure or NULL (no timeout)
  833. * @detect_deadlock: deadlock detection on/off
  834. *
  835. * Returns:
  836. * 0 on success
  837. * -EINTR when interrupted by a signal
  838. * -ETIMEDOUT when the timeout expired
  839. * -EDEADLK when the lock would deadlock (when deadlock detection is on)
  840. */
  841. int
  842. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout,
  843. int detect_deadlock)
  844. {
  845. might_sleep();
  846. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  847. detect_deadlock, rt_mutex_slowlock);
  848. }
  849. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  850. /**
  851. * rt_mutex_trylock - try to lock a rt_mutex
  852. *
  853. * @lock: the rt_mutex to be locked
  854. *
  855. * Returns 1 on success and 0 on contention
  856. */
  857. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  858. {
  859. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  860. }
  861. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  862. /**
  863. * rt_mutex_unlock - unlock a rt_mutex
  864. *
  865. * @lock: the rt_mutex to be unlocked
  866. */
  867. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  868. {
  869. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  870. }
  871. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  872. /**
  873. * rt_mutex_destroy - mark a mutex unusable
  874. * @lock: the mutex to be destroyed
  875. *
  876. * This function marks the mutex uninitialized, and any subsequent
  877. * use of the mutex is forbidden. The mutex must not be locked when
  878. * this function is called.
  879. */
  880. void rt_mutex_destroy(struct rt_mutex *lock)
  881. {
  882. WARN_ON(rt_mutex_is_locked(lock));
  883. #ifdef CONFIG_DEBUG_RT_MUTEXES
  884. lock->magic = NULL;
  885. #endif
  886. }
  887. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  888. /**
  889. * __rt_mutex_init - initialize the rt lock
  890. *
  891. * @lock: the rt lock to be initialized
  892. *
  893. * Initialize the rt lock to unlocked state.
  894. *
  895. * Initializing of a locked rt lock is not allowed
  896. */
  897. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  898. {
  899. lock->owner = NULL;
  900. raw_spin_lock_init(&lock->wait_lock);
  901. lock->waiters = RB_ROOT;
  902. lock->waiters_leftmost = NULL;
  903. debug_rt_mutex_init(lock, name);
  904. }
  905. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  906. /**
  907. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  908. * proxy owner
  909. *
  910. * @lock: the rt_mutex to be locked
  911. * @proxy_owner:the task to set as owner
  912. *
  913. * No locking. Caller has to do serializing itself
  914. * Special API call for PI-futex support
  915. */
  916. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  917. struct task_struct *proxy_owner)
  918. {
  919. __rt_mutex_init(lock, NULL);
  920. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  921. rt_mutex_set_owner(lock, proxy_owner);
  922. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  923. }
  924. /**
  925. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  926. *
  927. * @lock: the rt_mutex to be locked
  928. *
  929. * No locking. Caller has to do serializing itself
  930. * Special API call for PI-futex support
  931. */
  932. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  933. struct task_struct *proxy_owner)
  934. {
  935. debug_rt_mutex_proxy_unlock(lock);
  936. rt_mutex_set_owner(lock, NULL);
  937. rt_mutex_deadlock_account_unlock(proxy_owner);
  938. }
  939. /**
  940. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  941. * @lock: the rt_mutex to take
  942. * @waiter: the pre-initialized rt_mutex_waiter
  943. * @task: the task to prepare
  944. * @detect_deadlock: perform deadlock detection (1) or not (0)
  945. *
  946. * Returns:
  947. * 0 - task blocked on lock
  948. * 1 - acquired the lock for task, caller should wake it up
  949. * <0 - error
  950. *
  951. * Special API call for FUTEX_REQUEUE_PI support.
  952. */
  953. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  954. struct rt_mutex_waiter *waiter,
  955. struct task_struct *task, int detect_deadlock)
  956. {
  957. int ret;
  958. raw_spin_lock(&lock->wait_lock);
  959. if (try_to_take_rt_mutex(lock, task, NULL)) {
  960. raw_spin_unlock(&lock->wait_lock);
  961. return 1;
  962. }
  963. /* We enforce deadlock detection for futexes */
  964. ret = task_blocks_on_rt_mutex(lock, waiter, task, 1);
  965. if (ret && !rt_mutex_owner(lock)) {
  966. /*
  967. * Reset the return value. We might have
  968. * returned with -EDEADLK and the owner
  969. * released the lock while we were walking the
  970. * pi chain. Let the waiter sort it out.
  971. */
  972. ret = 0;
  973. }
  974. if (unlikely(ret))
  975. remove_waiter(lock, waiter);
  976. raw_spin_unlock(&lock->wait_lock);
  977. debug_rt_mutex_print_deadlock(waiter);
  978. return ret;
  979. }
  980. /**
  981. * rt_mutex_next_owner - return the next owner of the lock
  982. *
  983. * @lock: the rt lock query
  984. *
  985. * Returns the next owner of the lock or NULL
  986. *
  987. * Caller has to serialize against other accessors to the lock
  988. * itself.
  989. *
  990. * Special API call for PI-futex support
  991. */
  992. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  993. {
  994. if (!rt_mutex_has_waiters(lock))
  995. return NULL;
  996. return rt_mutex_top_waiter(lock)->task;
  997. }
  998. /**
  999. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1000. * @lock: the rt_mutex we were woken on
  1001. * @to: the timeout, null if none. hrtimer should already have
  1002. * been started.
  1003. * @waiter: the pre-initialized rt_mutex_waiter
  1004. * @detect_deadlock: perform deadlock detection (1) or not (0)
  1005. *
  1006. * Complete the lock acquisition started our behalf by another thread.
  1007. *
  1008. * Returns:
  1009. * 0 - success
  1010. * <0 - error, one of -EINTR, -ETIMEDOUT, or -EDEADLK
  1011. *
  1012. * Special API call for PI-futex requeue support
  1013. */
  1014. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1015. struct hrtimer_sleeper *to,
  1016. struct rt_mutex_waiter *waiter,
  1017. int detect_deadlock)
  1018. {
  1019. int ret;
  1020. raw_spin_lock(&lock->wait_lock);
  1021. set_current_state(TASK_INTERRUPTIBLE);
  1022. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1023. set_current_state(TASK_RUNNING);
  1024. if (unlikely(ret))
  1025. remove_waiter(lock, waiter);
  1026. /*
  1027. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1028. * have to fix that up.
  1029. */
  1030. fixup_rt_mutex_waiters(lock);
  1031. raw_spin_unlock(&lock->wait_lock);
  1032. return ret;
  1033. }