rtmutex.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  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/locking/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 there's no debugging state to be
  66. * set up.
  67. */
  68. #ifndef 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. /*
  78. * Safe fastpath aware unlock:
  79. * 1) Clear the waiters bit
  80. * 2) Drop lock->wait_lock
  81. * 3) Try to unlock the lock with cmpxchg
  82. */
  83. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  84. __releases(lock->wait_lock)
  85. {
  86. struct task_struct *owner = rt_mutex_owner(lock);
  87. clear_rt_mutex_waiters(lock);
  88. raw_spin_unlock(&lock->wait_lock);
  89. /*
  90. * If a new waiter comes in between the unlock and the cmpxchg
  91. * we have two situations:
  92. *
  93. * unlock(wait_lock);
  94. * lock(wait_lock);
  95. * cmpxchg(p, owner, 0) == owner
  96. * mark_rt_mutex_waiters(lock);
  97. * acquire(lock);
  98. * or:
  99. *
  100. * unlock(wait_lock);
  101. * lock(wait_lock);
  102. * mark_rt_mutex_waiters(lock);
  103. *
  104. * cmpxchg(p, owner, 0) != owner
  105. * enqueue_waiter();
  106. * unlock(wait_lock);
  107. * lock(wait_lock);
  108. * wake waiter();
  109. * unlock(wait_lock);
  110. * lock(wait_lock);
  111. * acquire(lock);
  112. */
  113. return rt_mutex_cmpxchg(lock, owner, NULL);
  114. }
  115. #else
  116. # define rt_mutex_cmpxchg(l,c,n) (0)
  117. static inline void mark_rt_mutex_waiters(struct rt_mutex *lock)
  118. {
  119. lock->owner = (struct task_struct *)
  120. ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS);
  121. }
  122. /*
  123. * Simple slow path only version: lock->owner is protected by lock->wait_lock.
  124. */
  125. static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock)
  126. __releases(lock->wait_lock)
  127. {
  128. lock->owner = NULL;
  129. raw_spin_unlock(&lock->wait_lock);
  130. return true;
  131. }
  132. #endif
  133. static inline int
  134. rt_mutex_waiter_less(struct rt_mutex_waiter *left,
  135. struct rt_mutex_waiter *right)
  136. {
  137. if (left->prio < right->prio)
  138. return 1;
  139. /*
  140. * If both waiters have dl_prio(), we check the deadlines of the
  141. * associated tasks.
  142. * If left waiter has a dl_prio(), and we didn't return 1 above,
  143. * then right waiter has a dl_prio() too.
  144. */
  145. if (dl_prio(left->prio))
  146. return (left->task->dl.deadline < right->task->dl.deadline);
  147. return 0;
  148. }
  149. static void
  150. rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  151. {
  152. struct rb_node **link = &lock->waiters.rb_node;
  153. struct rb_node *parent = NULL;
  154. struct rt_mutex_waiter *entry;
  155. int leftmost = 1;
  156. while (*link) {
  157. parent = *link;
  158. entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry);
  159. if (rt_mutex_waiter_less(waiter, entry)) {
  160. link = &parent->rb_left;
  161. } else {
  162. link = &parent->rb_right;
  163. leftmost = 0;
  164. }
  165. }
  166. if (leftmost)
  167. lock->waiters_leftmost = &waiter->tree_entry;
  168. rb_link_node(&waiter->tree_entry, parent, link);
  169. rb_insert_color(&waiter->tree_entry, &lock->waiters);
  170. }
  171. static void
  172. rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter)
  173. {
  174. if (RB_EMPTY_NODE(&waiter->tree_entry))
  175. return;
  176. if (lock->waiters_leftmost == &waiter->tree_entry)
  177. lock->waiters_leftmost = rb_next(&waiter->tree_entry);
  178. rb_erase(&waiter->tree_entry, &lock->waiters);
  179. RB_CLEAR_NODE(&waiter->tree_entry);
  180. }
  181. static void
  182. rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  183. {
  184. struct rb_node **link = &task->pi_waiters.rb_node;
  185. struct rb_node *parent = NULL;
  186. struct rt_mutex_waiter *entry;
  187. int leftmost = 1;
  188. while (*link) {
  189. parent = *link;
  190. entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry);
  191. if (rt_mutex_waiter_less(waiter, entry)) {
  192. link = &parent->rb_left;
  193. } else {
  194. link = &parent->rb_right;
  195. leftmost = 0;
  196. }
  197. }
  198. if (leftmost)
  199. task->pi_waiters_leftmost = &waiter->pi_tree_entry;
  200. rb_link_node(&waiter->pi_tree_entry, parent, link);
  201. rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters);
  202. }
  203. static void
  204. rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter)
  205. {
  206. if (RB_EMPTY_NODE(&waiter->pi_tree_entry))
  207. return;
  208. if (task->pi_waiters_leftmost == &waiter->pi_tree_entry)
  209. task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry);
  210. rb_erase(&waiter->pi_tree_entry, &task->pi_waiters);
  211. RB_CLEAR_NODE(&waiter->pi_tree_entry);
  212. }
  213. /*
  214. * Calculate task priority from the waiter tree priority
  215. *
  216. * Return task->normal_prio when the waiter tree is empty or when
  217. * the waiter is not allowed to do priority boosting
  218. */
  219. int rt_mutex_getprio(struct task_struct *task)
  220. {
  221. if (likely(!task_has_pi_waiters(task)))
  222. return task->normal_prio;
  223. return min(task_top_pi_waiter(task)->prio,
  224. task->normal_prio);
  225. }
  226. struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
  227. {
  228. if (likely(!task_has_pi_waiters(task)))
  229. return NULL;
  230. return task_top_pi_waiter(task)->task;
  231. }
  232. /*
  233. * Called by sched_setscheduler() to check whether the priority change
  234. * is overruled by a possible priority boosting.
  235. */
  236. int rt_mutex_check_prio(struct task_struct *task, int newprio)
  237. {
  238. if (!task_has_pi_waiters(task))
  239. return 0;
  240. return task_top_pi_waiter(task)->task->prio <= newprio;
  241. }
  242. /*
  243. * Adjust the priority of a task, after its pi_waiters got modified.
  244. *
  245. * This can be both boosting and unboosting. task->pi_lock must be held.
  246. */
  247. static void __rt_mutex_adjust_prio(struct task_struct *task)
  248. {
  249. int prio = rt_mutex_getprio(task);
  250. if (task->prio != prio || dl_prio(prio))
  251. rt_mutex_setprio(task, prio);
  252. }
  253. /*
  254. * Adjust task priority (undo boosting). Called from the exit path of
  255. * rt_mutex_slowunlock() and rt_mutex_slowlock().
  256. *
  257. * (Note: We do this outside of the protection of lock->wait_lock to
  258. * allow the lock to be taken while or before we readjust the priority
  259. * of task. We do not use the spin_xx_mutex() variants here as we are
  260. * outside of the debug path.)
  261. */
  262. static void rt_mutex_adjust_prio(struct task_struct *task)
  263. {
  264. unsigned long flags;
  265. raw_spin_lock_irqsave(&task->pi_lock, flags);
  266. __rt_mutex_adjust_prio(task);
  267. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  268. }
  269. /*
  270. * Deadlock detection is conditional:
  271. *
  272. * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted
  273. * if the detect argument is == RT_MUTEX_FULL_CHAINWALK.
  274. *
  275. * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always
  276. * conducted independent of the detect argument.
  277. *
  278. * If the waiter argument is NULL this indicates the deboost path and
  279. * deadlock detection is disabled independent of the detect argument
  280. * and the config settings.
  281. */
  282. static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter,
  283. enum rtmutex_chainwalk chwalk)
  284. {
  285. /*
  286. * This is just a wrapper function for the following call,
  287. * because debug_rt_mutex_detect_deadlock() smells like a magic
  288. * debug feature and I wanted to keep the cond function in the
  289. * main source file along with the comments instead of having
  290. * two of the same in the headers.
  291. */
  292. return debug_rt_mutex_detect_deadlock(waiter, chwalk);
  293. }
  294. /*
  295. * Max number of times we'll walk the boosting chain:
  296. */
  297. int max_lock_depth = 1024;
  298. static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p)
  299. {
  300. return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL;
  301. }
  302. /*
  303. * Adjust the priority chain. Also used for deadlock detection.
  304. * Decreases task's usage by one - may thus free the task.
  305. *
  306. * @task: the task owning the mutex (owner) for which a chain walk is
  307. * probably needed
  308. * @chwalk: do we have to carry out deadlock detection?
  309. * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck
  310. * things for a task that has just got its priority adjusted, and
  311. * is waiting on a mutex)
  312. * @next_lock: the mutex on which the owner of @orig_lock was blocked before
  313. * we dropped its pi_lock. Is never dereferenced, only used for
  314. * comparison to detect lock chain changes.
  315. * @orig_waiter: rt_mutex_waiter struct for the task that has just donated
  316. * its priority to the mutex owner (can be NULL in the case
  317. * depicted above or if the top waiter is gone away and we are
  318. * actually deboosting the owner)
  319. * @top_task: the current top waiter
  320. *
  321. * Returns 0 or -EDEADLK.
  322. *
  323. * Chain walk basics and protection scope
  324. *
  325. * [R] refcount on task
  326. * [P] task->pi_lock held
  327. * [L] rtmutex->wait_lock held
  328. *
  329. * Step Description Protected by
  330. * function arguments:
  331. * @task [R]
  332. * @orig_lock if != NULL @top_task is blocked on it
  333. * @next_lock Unprotected. Cannot be
  334. * dereferenced. Only used for
  335. * comparison.
  336. * @orig_waiter if != NULL @top_task is blocked on it
  337. * @top_task current, or in case of proxy
  338. * locking protected by calling
  339. * code
  340. * again:
  341. * loop_sanity_check();
  342. * retry:
  343. * [1] lock(task->pi_lock); [R] acquire [P]
  344. * [2] waiter = task->pi_blocked_on; [P]
  345. * [3] check_exit_conditions_1(); [P]
  346. * [4] lock = waiter->lock; [P]
  347. * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L]
  348. * unlock(task->pi_lock); release [P]
  349. * goto retry;
  350. * }
  351. * [6] check_exit_conditions_2(); [P] + [L]
  352. * [7] requeue_lock_waiter(lock, waiter); [P] + [L]
  353. * [8] unlock(task->pi_lock); release [P]
  354. * put_task_struct(task); release [R]
  355. * [9] check_exit_conditions_3(); [L]
  356. * [10] task = owner(lock); [L]
  357. * get_task_struct(task); [L] acquire [R]
  358. * lock(task->pi_lock); [L] acquire [P]
  359. * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L]
  360. * [12] check_exit_conditions_4(); [P] + [L]
  361. * [13] unlock(task->pi_lock); release [P]
  362. * unlock(lock->wait_lock); release [L]
  363. * goto again;
  364. */
  365. static int rt_mutex_adjust_prio_chain(struct task_struct *task,
  366. enum rtmutex_chainwalk chwalk,
  367. struct rt_mutex *orig_lock,
  368. struct rt_mutex *next_lock,
  369. struct rt_mutex_waiter *orig_waiter,
  370. struct task_struct *top_task)
  371. {
  372. struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter;
  373. struct rt_mutex_waiter *prerequeue_top_waiter;
  374. int ret = 0, depth = 0;
  375. struct rt_mutex *lock;
  376. bool detect_deadlock;
  377. unsigned long flags;
  378. bool requeue = true;
  379. detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk);
  380. /*
  381. * The (de)boosting is a step by step approach with a lot of
  382. * pitfalls. We want this to be preemptible and we want hold a
  383. * maximum of two locks per step. So we have to check
  384. * carefully whether things change under us.
  385. */
  386. again:
  387. /*
  388. * We limit the lock chain length for each invocation.
  389. */
  390. if (++depth > max_lock_depth) {
  391. static int prev_max;
  392. /*
  393. * Print this only once. If the admin changes the limit,
  394. * print a new message when reaching the limit again.
  395. */
  396. if (prev_max != max_lock_depth) {
  397. prev_max = max_lock_depth;
  398. printk(KERN_WARNING "Maximum lock depth %d reached "
  399. "task: %s (%d)\n", max_lock_depth,
  400. top_task->comm, task_pid_nr(top_task));
  401. }
  402. put_task_struct(task);
  403. return -EDEADLK;
  404. }
  405. /*
  406. * We are fully preemptible here and only hold the refcount on
  407. * @task. So everything can have changed under us since the
  408. * caller or our own code below (goto retry/again) dropped all
  409. * locks.
  410. */
  411. retry:
  412. /*
  413. * [1] Task cannot go away as we did a get_task() before !
  414. */
  415. raw_spin_lock_irqsave(&task->pi_lock, flags);
  416. /*
  417. * [2] Get the waiter on which @task is blocked on.
  418. */
  419. waiter = task->pi_blocked_on;
  420. /*
  421. * [3] check_exit_conditions_1() protected by task->pi_lock.
  422. */
  423. /*
  424. * Check whether the end of the boosting chain has been
  425. * reached or the state of the chain has changed while we
  426. * dropped the locks.
  427. */
  428. if (!waiter)
  429. goto out_unlock_pi;
  430. /*
  431. * Check the orig_waiter state. After we dropped the locks,
  432. * the previous owner of the lock might have released the lock.
  433. */
  434. if (orig_waiter && !rt_mutex_owner(orig_lock))
  435. goto out_unlock_pi;
  436. /*
  437. * We dropped all locks after taking a refcount on @task, so
  438. * the task might have moved on in the lock chain or even left
  439. * the chain completely and blocks now on an unrelated lock or
  440. * on @orig_lock.
  441. *
  442. * We stored the lock on which @task was blocked in @next_lock,
  443. * so we can detect the chain change.
  444. */
  445. if (next_lock != waiter->lock)
  446. goto out_unlock_pi;
  447. /*
  448. * Drop out, when the task has no waiters. Note,
  449. * top_waiter can be NULL, when we are in the deboosting
  450. * mode!
  451. */
  452. if (top_waiter) {
  453. if (!task_has_pi_waiters(task))
  454. goto out_unlock_pi;
  455. /*
  456. * If deadlock detection is off, we stop here if we
  457. * are not the top pi waiter of the task. If deadlock
  458. * detection is enabled we continue, but stop the
  459. * requeueing in the chain walk.
  460. */
  461. if (top_waiter != task_top_pi_waiter(task)) {
  462. if (!detect_deadlock)
  463. goto out_unlock_pi;
  464. else
  465. requeue = false;
  466. }
  467. }
  468. /*
  469. * If the waiter priority is the same as the task priority
  470. * then there is no further priority adjustment necessary. If
  471. * deadlock detection is off, we stop the chain walk. If its
  472. * enabled we continue, but stop the requeueing in the chain
  473. * walk.
  474. */
  475. if (waiter->prio == task->prio) {
  476. if (!detect_deadlock)
  477. goto out_unlock_pi;
  478. else
  479. requeue = false;
  480. }
  481. /*
  482. * [4] Get the next lock
  483. */
  484. lock = waiter->lock;
  485. /*
  486. * [5] We need to trylock here as we are holding task->pi_lock,
  487. * which is the reverse lock order versus the other rtmutex
  488. * operations.
  489. */
  490. if (!raw_spin_trylock(&lock->wait_lock)) {
  491. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  492. cpu_relax();
  493. goto retry;
  494. }
  495. /*
  496. * [6] check_exit_conditions_2() protected by task->pi_lock and
  497. * lock->wait_lock.
  498. *
  499. * Deadlock detection. If the lock is the same as the original
  500. * lock which caused us to walk the lock chain or if the
  501. * current lock is owned by the task which initiated the chain
  502. * walk, we detected a deadlock.
  503. */
  504. if (lock == orig_lock || rt_mutex_owner(lock) == top_task) {
  505. debug_rt_mutex_deadlock(chwalk, orig_waiter, lock);
  506. raw_spin_unlock(&lock->wait_lock);
  507. ret = -EDEADLK;
  508. goto out_unlock_pi;
  509. }
  510. /*
  511. * If we just follow the lock chain for deadlock detection, no
  512. * need to do all the requeue operations. To avoid a truckload
  513. * of conditionals around the various places below, just do the
  514. * minimum chain walk checks.
  515. */
  516. if (!requeue) {
  517. /*
  518. * No requeue[7] here. Just release @task [8]
  519. */
  520. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  521. put_task_struct(task);
  522. /*
  523. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  524. * If there is no owner of the lock, end of chain.
  525. */
  526. if (!rt_mutex_owner(lock)) {
  527. raw_spin_unlock(&lock->wait_lock);
  528. return 0;
  529. }
  530. /* [10] Grab the next task, i.e. owner of @lock */
  531. task = rt_mutex_owner(lock);
  532. get_task_struct(task);
  533. raw_spin_lock_irqsave(&task->pi_lock, flags);
  534. /*
  535. * No requeue [11] here. We just do deadlock detection.
  536. *
  537. * [12] Store whether owner is blocked
  538. * itself. Decision is made after dropping the locks
  539. */
  540. next_lock = task_blocked_on_lock(task);
  541. /*
  542. * Get the top waiter for the next iteration
  543. */
  544. top_waiter = rt_mutex_top_waiter(lock);
  545. /* [13] Drop locks */
  546. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  547. raw_spin_unlock(&lock->wait_lock);
  548. /* If owner is not blocked, end of chain. */
  549. if (!next_lock)
  550. goto out_put_task;
  551. goto again;
  552. }
  553. /*
  554. * Store the current top waiter before doing the requeue
  555. * operation on @lock. We need it for the boost/deboost
  556. * decision below.
  557. */
  558. prerequeue_top_waiter = rt_mutex_top_waiter(lock);
  559. /* [7] Requeue the waiter in the lock waiter list. */
  560. rt_mutex_dequeue(lock, waiter);
  561. waiter->prio = task->prio;
  562. rt_mutex_enqueue(lock, waiter);
  563. /* [8] Release the task */
  564. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  565. put_task_struct(task);
  566. /*
  567. * [9] check_exit_conditions_3 protected by lock->wait_lock.
  568. *
  569. * We must abort the chain walk if there is no lock owner even
  570. * in the dead lock detection case, as we have nothing to
  571. * follow here. This is the end of the chain we are walking.
  572. */
  573. if (!rt_mutex_owner(lock)) {
  574. /*
  575. * If the requeue [7] above changed the top waiter,
  576. * then we need to wake the new top waiter up to try
  577. * to get the lock.
  578. */
  579. if (prerequeue_top_waiter != rt_mutex_top_waiter(lock))
  580. wake_up_process(rt_mutex_top_waiter(lock)->task);
  581. raw_spin_unlock(&lock->wait_lock);
  582. return 0;
  583. }
  584. /* [10] Grab the next task, i.e. the owner of @lock */
  585. task = rt_mutex_owner(lock);
  586. get_task_struct(task);
  587. raw_spin_lock_irqsave(&task->pi_lock, flags);
  588. /* [11] requeue the pi waiters if necessary */
  589. if (waiter == rt_mutex_top_waiter(lock)) {
  590. /*
  591. * The waiter became the new top (highest priority)
  592. * waiter on the lock. Replace the previous top waiter
  593. * in the owner tasks pi waiters list with this waiter
  594. * and adjust the priority of the owner.
  595. */
  596. rt_mutex_dequeue_pi(task, prerequeue_top_waiter);
  597. rt_mutex_enqueue_pi(task, waiter);
  598. __rt_mutex_adjust_prio(task);
  599. } else if (prerequeue_top_waiter == waiter) {
  600. /*
  601. * The waiter was the top waiter on the lock, but is
  602. * no longer the top prority waiter. Replace waiter in
  603. * the owner tasks pi waiters list with the new top
  604. * (highest priority) waiter and adjust the priority
  605. * of the owner.
  606. * The new top waiter is stored in @waiter so that
  607. * @waiter == @top_waiter evaluates to true below and
  608. * we continue to deboost the rest of the chain.
  609. */
  610. rt_mutex_dequeue_pi(task, waiter);
  611. waiter = rt_mutex_top_waiter(lock);
  612. rt_mutex_enqueue_pi(task, waiter);
  613. __rt_mutex_adjust_prio(task);
  614. } else {
  615. /*
  616. * Nothing changed. No need to do any priority
  617. * adjustment.
  618. */
  619. }
  620. /*
  621. * [12] check_exit_conditions_4() protected by task->pi_lock
  622. * and lock->wait_lock. The actual decisions are made after we
  623. * dropped the locks.
  624. *
  625. * Check whether the task which owns the current lock is pi
  626. * blocked itself. If yes we store a pointer to the lock for
  627. * the lock chain change detection above. After we dropped
  628. * task->pi_lock next_lock cannot be dereferenced anymore.
  629. */
  630. next_lock = task_blocked_on_lock(task);
  631. /*
  632. * Store the top waiter of @lock for the end of chain walk
  633. * decision below.
  634. */
  635. top_waiter = rt_mutex_top_waiter(lock);
  636. /* [13] Drop the locks */
  637. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  638. raw_spin_unlock(&lock->wait_lock);
  639. /*
  640. * Make the actual exit decisions [12], based on the stored
  641. * values.
  642. *
  643. * We reached the end of the lock chain. Stop right here. No
  644. * point to go back just to figure that out.
  645. */
  646. if (!next_lock)
  647. goto out_put_task;
  648. /*
  649. * If the current waiter is not the top waiter on the lock,
  650. * then we can stop the chain walk here if we are not in full
  651. * deadlock detection mode.
  652. */
  653. if (!detect_deadlock && waiter != top_waiter)
  654. goto out_put_task;
  655. goto again;
  656. out_unlock_pi:
  657. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  658. out_put_task:
  659. put_task_struct(task);
  660. return ret;
  661. }
  662. /*
  663. * Try to take an rt-mutex
  664. *
  665. * Must be called with lock->wait_lock held.
  666. *
  667. * @lock: The lock to be acquired.
  668. * @task: The task which wants to acquire the lock
  669. * @waiter: The waiter that is queued to the lock's wait list if the
  670. * callsite called task_blocked_on_lock(), otherwise NULL
  671. */
  672. static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
  673. struct rt_mutex_waiter *waiter)
  674. {
  675. unsigned long flags;
  676. /*
  677. * Before testing whether we can acquire @lock, we set the
  678. * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all
  679. * other tasks which try to modify @lock into the slow path
  680. * and they serialize on @lock->wait_lock.
  681. *
  682. * The RT_MUTEX_HAS_WAITERS bit can have a transitional state
  683. * as explained at the top of this file if and only if:
  684. *
  685. * - There is a lock owner. The caller must fixup the
  686. * transient state if it does a trylock or leaves the lock
  687. * function due to a signal or timeout.
  688. *
  689. * - @task acquires the lock and there are no other
  690. * waiters. This is undone in rt_mutex_set_owner(@task) at
  691. * the end of this function.
  692. */
  693. mark_rt_mutex_waiters(lock);
  694. /*
  695. * If @lock has an owner, give up.
  696. */
  697. if (rt_mutex_owner(lock))
  698. return 0;
  699. /*
  700. * If @waiter != NULL, @task has already enqueued the waiter
  701. * into @lock waiter list. If @waiter == NULL then this is a
  702. * trylock attempt.
  703. */
  704. if (waiter) {
  705. /*
  706. * If waiter is not the highest priority waiter of
  707. * @lock, give up.
  708. */
  709. if (waiter != rt_mutex_top_waiter(lock))
  710. return 0;
  711. /*
  712. * We can acquire the lock. Remove the waiter from the
  713. * lock waiters list.
  714. */
  715. rt_mutex_dequeue(lock, waiter);
  716. } else {
  717. /*
  718. * If the lock has waiters already we check whether @task is
  719. * eligible to take over the lock.
  720. *
  721. * If there are no other waiters, @task can acquire
  722. * the lock. @task->pi_blocked_on is NULL, so it does
  723. * not need to be dequeued.
  724. */
  725. if (rt_mutex_has_waiters(lock)) {
  726. /*
  727. * If @task->prio is greater than or equal to
  728. * the top waiter priority (kernel view),
  729. * @task lost.
  730. */
  731. if (task->prio >= rt_mutex_top_waiter(lock)->prio)
  732. return 0;
  733. /*
  734. * The current top waiter stays enqueued. We
  735. * don't have to change anything in the lock
  736. * waiters order.
  737. */
  738. } else {
  739. /*
  740. * No waiters. Take the lock without the
  741. * pi_lock dance.@task->pi_blocked_on is NULL
  742. * and we have no waiters to enqueue in @task
  743. * pi waiters list.
  744. */
  745. goto takeit;
  746. }
  747. }
  748. /*
  749. * Clear @task->pi_blocked_on. Requires protection by
  750. * @task->pi_lock. Redundant operation for the @waiter == NULL
  751. * case, but conditionals are more expensive than a redundant
  752. * store.
  753. */
  754. raw_spin_lock_irqsave(&task->pi_lock, flags);
  755. task->pi_blocked_on = NULL;
  756. /*
  757. * Finish the lock acquisition. @task is the new owner. If
  758. * other waiters exist we have to insert the highest priority
  759. * waiter into @task->pi_waiters list.
  760. */
  761. if (rt_mutex_has_waiters(lock))
  762. rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock));
  763. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  764. takeit:
  765. /* We got the lock. */
  766. debug_rt_mutex_lock(lock);
  767. /*
  768. * This either preserves the RT_MUTEX_HAS_WAITERS bit if there
  769. * are still waiters or clears it.
  770. */
  771. rt_mutex_set_owner(lock, task);
  772. rt_mutex_deadlock_account_lock(lock, task);
  773. return 1;
  774. }
  775. /*
  776. * Task blocks on lock.
  777. *
  778. * Prepare waiter and propagate pi chain
  779. *
  780. * This must be called with lock->wait_lock held.
  781. */
  782. static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
  783. struct rt_mutex_waiter *waiter,
  784. struct task_struct *task,
  785. enum rtmutex_chainwalk chwalk)
  786. {
  787. struct task_struct *owner = rt_mutex_owner(lock);
  788. struct rt_mutex_waiter *top_waiter = waiter;
  789. struct rt_mutex *next_lock;
  790. int chain_walk = 0, res;
  791. unsigned long flags;
  792. /*
  793. * Early deadlock detection. We really don't want the task to
  794. * enqueue on itself just to untangle the mess later. It's not
  795. * only an optimization. We drop the locks, so another waiter
  796. * can come in before the chain walk detects the deadlock. So
  797. * the other will detect the deadlock and return -EDEADLOCK,
  798. * which is wrong, as the other waiter is not in a deadlock
  799. * situation.
  800. */
  801. if (owner == task)
  802. return -EDEADLK;
  803. raw_spin_lock_irqsave(&task->pi_lock, flags);
  804. __rt_mutex_adjust_prio(task);
  805. waiter->task = task;
  806. waiter->lock = lock;
  807. waiter->prio = task->prio;
  808. /* Get the top priority waiter on the lock */
  809. if (rt_mutex_has_waiters(lock))
  810. top_waiter = rt_mutex_top_waiter(lock);
  811. rt_mutex_enqueue(lock, waiter);
  812. task->pi_blocked_on = waiter;
  813. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  814. if (!owner)
  815. return 0;
  816. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  817. if (waiter == rt_mutex_top_waiter(lock)) {
  818. rt_mutex_dequeue_pi(owner, top_waiter);
  819. rt_mutex_enqueue_pi(owner, waiter);
  820. __rt_mutex_adjust_prio(owner);
  821. if (owner->pi_blocked_on)
  822. chain_walk = 1;
  823. } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) {
  824. chain_walk = 1;
  825. }
  826. /* Store the lock on which owner is blocked or NULL */
  827. next_lock = task_blocked_on_lock(owner);
  828. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  829. /*
  830. * Even if full deadlock detection is on, if the owner is not
  831. * blocked itself, we can avoid finding this out in the chain
  832. * walk.
  833. */
  834. if (!chain_walk || !next_lock)
  835. return 0;
  836. /*
  837. * The owner can't disappear while holding a lock,
  838. * so the owner struct is protected by wait_lock.
  839. * Gets dropped in rt_mutex_adjust_prio_chain()!
  840. */
  841. get_task_struct(owner);
  842. raw_spin_unlock(&lock->wait_lock);
  843. res = rt_mutex_adjust_prio_chain(owner, chwalk, lock,
  844. next_lock, waiter, task);
  845. raw_spin_lock(&lock->wait_lock);
  846. return res;
  847. }
  848. /*
  849. * Wake up the next waiter on the lock.
  850. *
  851. * Remove the top waiter from the current tasks pi waiter list and
  852. * wake it up.
  853. *
  854. * Called with lock->wait_lock held.
  855. */
  856. static void wakeup_next_waiter(struct rt_mutex *lock)
  857. {
  858. struct rt_mutex_waiter *waiter;
  859. unsigned long flags;
  860. raw_spin_lock_irqsave(&current->pi_lock, flags);
  861. waiter = rt_mutex_top_waiter(lock);
  862. /*
  863. * Remove it from current->pi_waiters. We do not adjust a
  864. * possible priority boost right now. We execute wakeup in the
  865. * boosted mode and go back to normal after releasing
  866. * lock->wait_lock.
  867. */
  868. rt_mutex_dequeue_pi(current, waiter);
  869. /*
  870. * As we are waking up the top waiter, and the waiter stays
  871. * queued on the lock until it gets the lock, this lock
  872. * obviously has waiters. Just set the bit here and this has
  873. * the added benefit of forcing all new tasks into the
  874. * slow path making sure no task of lower priority than
  875. * the top waiter can steal this lock.
  876. */
  877. lock->owner = (void *) RT_MUTEX_HAS_WAITERS;
  878. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  879. /*
  880. * It's safe to dereference waiter as it cannot go away as
  881. * long as we hold lock->wait_lock. The waiter task needs to
  882. * acquire it in order to dequeue the waiter.
  883. */
  884. wake_up_process(waiter->task);
  885. }
  886. /*
  887. * Remove a waiter from a lock and give up
  888. *
  889. * Must be called with lock->wait_lock held and
  890. * have just failed to try_to_take_rt_mutex().
  891. */
  892. static void remove_waiter(struct rt_mutex *lock,
  893. struct rt_mutex_waiter *waiter)
  894. {
  895. bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock));
  896. struct task_struct *owner = rt_mutex_owner(lock);
  897. struct rt_mutex *next_lock;
  898. unsigned long flags;
  899. raw_spin_lock_irqsave(&current->pi_lock, flags);
  900. rt_mutex_dequeue(lock, waiter);
  901. current->pi_blocked_on = NULL;
  902. raw_spin_unlock_irqrestore(&current->pi_lock, flags);
  903. /*
  904. * Only update priority if the waiter was the highest priority
  905. * waiter of the lock and there is an owner to update.
  906. */
  907. if (!owner || !is_top_waiter)
  908. return;
  909. raw_spin_lock_irqsave(&owner->pi_lock, flags);
  910. rt_mutex_dequeue_pi(owner, waiter);
  911. if (rt_mutex_has_waiters(lock))
  912. rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock));
  913. __rt_mutex_adjust_prio(owner);
  914. /* Store the lock on which owner is blocked or NULL */
  915. next_lock = task_blocked_on_lock(owner);
  916. raw_spin_unlock_irqrestore(&owner->pi_lock, flags);
  917. /*
  918. * Don't walk the chain, if the owner task is not blocked
  919. * itself.
  920. */
  921. if (!next_lock)
  922. return;
  923. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  924. get_task_struct(owner);
  925. raw_spin_unlock(&lock->wait_lock);
  926. rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock,
  927. next_lock, NULL, current);
  928. raw_spin_lock(&lock->wait_lock);
  929. }
  930. /*
  931. * Recheck the pi chain, in case we got a priority setting
  932. *
  933. * Called from sched_setscheduler
  934. */
  935. void rt_mutex_adjust_pi(struct task_struct *task)
  936. {
  937. struct rt_mutex_waiter *waiter;
  938. struct rt_mutex *next_lock;
  939. unsigned long flags;
  940. raw_spin_lock_irqsave(&task->pi_lock, flags);
  941. waiter = task->pi_blocked_on;
  942. if (!waiter || (waiter->prio == task->prio &&
  943. !dl_prio(task->prio))) {
  944. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  945. return;
  946. }
  947. next_lock = waiter->lock;
  948. raw_spin_unlock_irqrestore(&task->pi_lock, flags);
  949. /* gets dropped in rt_mutex_adjust_prio_chain()! */
  950. get_task_struct(task);
  951. rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL,
  952. next_lock, NULL, task);
  953. }
  954. /**
  955. * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop
  956. * @lock: the rt_mutex to take
  957. * @state: the state the task should block in (TASK_INTERRUPTIBLE
  958. * or TASK_UNINTERRUPTIBLE)
  959. * @timeout: the pre-initialized and started timer, or NULL for none
  960. * @waiter: the pre-initialized rt_mutex_waiter
  961. *
  962. * lock->wait_lock must be held by the caller.
  963. */
  964. static int __sched
  965. __rt_mutex_slowlock(struct rt_mutex *lock, int state,
  966. struct hrtimer_sleeper *timeout,
  967. struct rt_mutex_waiter *waiter)
  968. {
  969. int ret = 0;
  970. for (;;) {
  971. /* Try to acquire the lock: */
  972. if (try_to_take_rt_mutex(lock, current, waiter))
  973. break;
  974. /*
  975. * TASK_INTERRUPTIBLE checks for signals and
  976. * timeout. Ignored otherwise.
  977. */
  978. if (unlikely(state == TASK_INTERRUPTIBLE)) {
  979. /* Signal pending? */
  980. if (signal_pending(current))
  981. ret = -EINTR;
  982. if (timeout && !timeout->task)
  983. ret = -ETIMEDOUT;
  984. if (ret)
  985. break;
  986. }
  987. raw_spin_unlock(&lock->wait_lock);
  988. debug_rt_mutex_print_deadlock(waiter);
  989. schedule_rt_mutex(lock);
  990. raw_spin_lock(&lock->wait_lock);
  991. set_current_state(state);
  992. }
  993. __set_current_state(TASK_RUNNING);
  994. return ret;
  995. }
  996. static void rt_mutex_handle_deadlock(int res, int detect_deadlock,
  997. struct rt_mutex_waiter *w)
  998. {
  999. /*
  1000. * If the result is not -EDEADLOCK or the caller requested
  1001. * deadlock detection, nothing to do here.
  1002. */
  1003. if (res != -EDEADLOCK || detect_deadlock)
  1004. return;
  1005. /*
  1006. * Yell lowdly and stop the task right here.
  1007. */
  1008. rt_mutex_print_deadlock(w);
  1009. while (1) {
  1010. set_current_state(TASK_INTERRUPTIBLE);
  1011. schedule();
  1012. }
  1013. }
  1014. /*
  1015. * Slow path lock function:
  1016. */
  1017. static int __sched
  1018. rt_mutex_slowlock(struct rt_mutex *lock, int state,
  1019. struct hrtimer_sleeper *timeout,
  1020. enum rtmutex_chainwalk chwalk)
  1021. {
  1022. struct rt_mutex_waiter waiter;
  1023. int ret = 0;
  1024. debug_rt_mutex_init_waiter(&waiter);
  1025. RB_CLEAR_NODE(&waiter.pi_tree_entry);
  1026. RB_CLEAR_NODE(&waiter.tree_entry);
  1027. raw_spin_lock(&lock->wait_lock);
  1028. /* Try to acquire the lock again: */
  1029. if (try_to_take_rt_mutex(lock, current, NULL)) {
  1030. raw_spin_unlock(&lock->wait_lock);
  1031. return 0;
  1032. }
  1033. set_current_state(state);
  1034. /* Setup the timer, when timeout != NULL */
  1035. if (unlikely(timeout)) {
  1036. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1037. if (!hrtimer_active(&timeout->timer))
  1038. timeout->task = NULL;
  1039. }
  1040. ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk);
  1041. if (likely(!ret))
  1042. /* sleep on the mutex */
  1043. ret = __rt_mutex_slowlock(lock, state, timeout, &waiter);
  1044. if (unlikely(ret)) {
  1045. __set_current_state(TASK_RUNNING);
  1046. if (rt_mutex_has_waiters(lock))
  1047. remove_waiter(lock, &waiter);
  1048. rt_mutex_handle_deadlock(ret, chwalk, &waiter);
  1049. }
  1050. /*
  1051. * try_to_take_rt_mutex() sets the waiter bit
  1052. * unconditionally. We might have to fix that up.
  1053. */
  1054. fixup_rt_mutex_waiters(lock);
  1055. raw_spin_unlock(&lock->wait_lock);
  1056. /* Remove pending timer: */
  1057. if (unlikely(timeout))
  1058. hrtimer_cancel(&timeout->timer);
  1059. debug_rt_mutex_free_waiter(&waiter);
  1060. return ret;
  1061. }
  1062. /*
  1063. * Slow path try-lock function:
  1064. */
  1065. static inline int rt_mutex_slowtrylock(struct rt_mutex *lock)
  1066. {
  1067. int ret;
  1068. /*
  1069. * If the lock already has an owner we fail to get the lock.
  1070. * This can be done without taking the @lock->wait_lock as
  1071. * it is only being read, and this is a trylock anyway.
  1072. */
  1073. if (rt_mutex_owner(lock))
  1074. return 0;
  1075. /*
  1076. * The mutex has currently no owner. Lock the wait lock and
  1077. * try to acquire the lock.
  1078. */
  1079. raw_spin_lock(&lock->wait_lock);
  1080. ret = try_to_take_rt_mutex(lock, current, NULL);
  1081. /*
  1082. * try_to_take_rt_mutex() sets the lock waiters bit
  1083. * unconditionally. Clean this up.
  1084. */
  1085. fixup_rt_mutex_waiters(lock);
  1086. raw_spin_unlock(&lock->wait_lock);
  1087. return ret;
  1088. }
  1089. /*
  1090. * Slow path to release a rt-mutex:
  1091. */
  1092. static void __sched
  1093. rt_mutex_slowunlock(struct rt_mutex *lock)
  1094. {
  1095. raw_spin_lock(&lock->wait_lock);
  1096. debug_rt_mutex_unlock(lock);
  1097. rt_mutex_deadlock_account_unlock(current);
  1098. /*
  1099. * We must be careful here if the fast path is enabled. If we
  1100. * have no waiters queued we cannot set owner to NULL here
  1101. * because of:
  1102. *
  1103. * foo->lock->owner = NULL;
  1104. * rtmutex_lock(foo->lock); <- fast path
  1105. * free = atomic_dec_and_test(foo->refcnt);
  1106. * rtmutex_unlock(foo->lock); <- fast path
  1107. * if (free)
  1108. * kfree(foo);
  1109. * raw_spin_unlock(foo->lock->wait_lock);
  1110. *
  1111. * So for the fastpath enabled kernel:
  1112. *
  1113. * Nothing can set the waiters bit as long as we hold
  1114. * lock->wait_lock. So we do the following sequence:
  1115. *
  1116. * owner = rt_mutex_owner(lock);
  1117. * clear_rt_mutex_waiters(lock);
  1118. * raw_spin_unlock(&lock->wait_lock);
  1119. * if (cmpxchg(&lock->owner, owner, 0) == owner)
  1120. * return;
  1121. * goto retry;
  1122. *
  1123. * The fastpath disabled variant is simple as all access to
  1124. * lock->owner is serialized by lock->wait_lock:
  1125. *
  1126. * lock->owner = NULL;
  1127. * raw_spin_unlock(&lock->wait_lock);
  1128. */
  1129. while (!rt_mutex_has_waiters(lock)) {
  1130. /* Drops lock->wait_lock ! */
  1131. if (unlock_rt_mutex_safe(lock) == true)
  1132. return;
  1133. /* Relock the rtmutex and try again */
  1134. raw_spin_lock(&lock->wait_lock);
  1135. }
  1136. /*
  1137. * The wakeup next waiter path does not suffer from the above
  1138. * race. See the comments there.
  1139. */
  1140. wakeup_next_waiter(lock);
  1141. raw_spin_unlock(&lock->wait_lock);
  1142. /* Undo pi boosting if necessary: */
  1143. rt_mutex_adjust_prio(current);
  1144. }
  1145. /*
  1146. * debug aware fast / slowpath lock,trylock,unlock
  1147. *
  1148. * The atomic acquire/release ops are compiled away, when either the
  1149. * architecture does not support cmpxchg or when debugging is enabled.
  1150. */
  1151. static inline int
  1152. rt_mutex_fastlock(struct rt_mutex *lock, int state,
  1153. int (*slowfn)(struct rt_mutex *lock, int state,
  1154. struct hrtimer_sleeper *timeout,
  1155. enum rtmutex_chainwalk chwalk))
  1156. {
  1157. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1158. rt_mutex_deadlock_account_lock(lock, current);
  1159. return 0;
  1160. } else
  1161. return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK);
  1162. }
  1163. static inline int
  1164. rt_mutex_timed_fastlock(struct rt_mutex *lock, int state,
  1165. struct hrtimer_sleeper *timeout,
  1166. enum rtmutex_chainwalk chwalk,
  1167. int (*slowfn)(struct rt_mutex *lock, int state,
  1168. struct hrtimer_sleeper *timeout,
  1169. enum rtmutex_chainwalk chwalk))
  1170. {
  1171. if (chwalk == RT_MUTEX_MIN_CHAINWALK &&
  1172. likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1173. rt_mutex_deadlock_account_lock(lock, current);
  1174. return 0;
  1175. } else
  1176. return slowfn(lock, state, timeout, chwalk);
  1177. }
  1178. static inline int
  1179. rt_mutex_fasttrylock(struct rt_mutex *lock,
  1180. int (*slowfn)(struct rt_mutex *lock))
  1181. {
  1182. if (likely(rt_mutex_cmpxchg(lock, NULL, current))) {
  1183. rt_mutex_deadlock_account_lock(lock, current);
  1184. return 1;
  1185. }
  1186. return slowfn(lock);
  1187. }
  1188. static inline void
  1189. rt_mutex_fastunlock(struct rt_mutex *lock,
  1190. void (*slowfn)(struct rt_mutex *lock))
  1191. {
  1192. if (likely(rt_mutex_cmpxchg(lock, current, NULL)))
  1193. rt_mutex_deadlock_account_unlock(current);
  1194. else
  1195. slowfn(lock);
  1196. }
  1197. /**
  1198. * rt_mutex_lock - lock a rt_mutex
  1199. *
  1200. * @lock: the rt_mutex to be locked
  1201. */
  1202. void __sched rt_mutex_lock(struct rt_mutex *lock)
  1203. {
  1204. might_sleep();
  1205. rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock);
  1206. }
  1207. EXPORT_SYMBOL_GPL(rt_mutex_lock);
  1208. /**
  1209. * rt_mutex_lock_interruptible - lock a rt_mutex interruptible
  1210. *
  1211. * @lock: the rt_mutex to be locked
  1212. *
  1213. * Returns:
  1214. * 0 on success
  1215. * -EINTR when interrupted by a signal
  1216. */
  1217. int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock)
  1218. {
  1219. might_sleep();
  1220. return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock);
  1221. }
  1222. EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible);
  1223. /*
  1224. * Futex variant with full deadlock detection.
  1225. */
  1226. int rt_mutex_timed_futex_lock(struct rt_mutex *lock,
  1227. struct hrtimer_sleeper *timeout)
  1228. {
  1229. might_sleep();
  1230. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1231. RT_MUTEX_FULL_CHAINWALK,
  1232. rt_mutex_slowlock);
  1233. }
  1234. /**
  1235. * rt_mutex_timed_lock - lock a rt_mutex interruptible
  1236. * the timeout structure is provided
  1237. * by the caller
  1238. *
  1239. * @lock: the rt_mutex to be locked
  1240. * @timeout: timeout structure or NULL (no timeout)
  1241. *
  1242. * Returns:
  1243. * 0 on success
  1244. * -EINTR when interrupted by a signal
  1245. * -ETIMEDOUT when the timeout expired
  1246. */
  1247. int
  1248. rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout)
  1249. {
  1250. might_sleep();
  1251. return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout,
  1252. RT_MUTEX_MIN_CHAINWALK,
  1253. rt_mutex_slowlock);
  1254. }
  1255. EXPORT_SYMBOL_GPL(rt_mutex_timed_lock);
  1256. /**
  1257. * rt_mutex_trylock - try to lock a rt_mutex
  1258. *
  1259. * @lock: the rt_mutex to be locked
  1260. *
  1261. * Returns 1 on success and 0 on contention
  1262. */
  1263. int __sched rt_mutex_trylock(struct rt_mutex *lock)
  1264. {
  1265. return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock);
  1266. }
  1267. EXPORT_SYMBOL_GPL(rt_mutex_trylock);
  1268. /**
  1269. * rt_mutex_unlock - unlock a rt_mutex
  1270. *
  1271. * @lock: the rt_mutex to be unlocked
  1272. */
  1273. void __sched rt_mutex_unlock(struct rt_mutex *lock)
  1274. {
  1275. rt_mutex_fastunlock(lock, rt_mutex_slowunlock);
  1276. }
  1277. EXPORT_SYMBOL_GPL(rt_mutex_unlock);
  1278. /**
  1279. * rt_mutex_destroy - mark a mutex unusable
  1280. * @lock: the mutex to be destroyed
  1281. *
  1282. * This function marks the mutex uninitialized, and any subsequent
  1283. * use of the mutex is forbidden. The mutex must not be locked when
  1284. * this function is called.
  1285. */
  1286. void rt_mutex_destroy(struct rt_mutex *lock)
  1287. {
  1288. WARN_ON(rt_mutex_is_locked(lock));
  1289. #ifdef CONFIG_DEBUG_RT_MUTEXES
  1290. lock->magic = NULL;
  1291. #endif
  1292. }
  1293. EXPORT_SYMBOL_GPL(rt_mutex_destroy);
  1294. /**
  1295. * __rt_mutex_init - initialize the rt lock
  1296. *
  1297. * @lock: the rt lock to be initialized
  1298. *
  1299. * Initialize the rt lock to unlocked state.
  1300. *
  1301. * Initializing of a locked rt lock is not allowed
  1302. */
  1303. void __rt_mutex_init(struct rt_mutex *lock, const char *name)
  1304. {
  1305. lock->owner = NULL;
  1306. raw_spin_lock_init(&lock->wait_lock);
  1307. lock->waiters = RB_ROOT;
  1308. lock->waiters_leftmost = NULL;
  1309. debug_rt_mutex_init(lock, name);
  1310. }
  1311. EXPORT_SYMBOL_GPL(__rt_mutex_init);
  1312. /**
  1313. * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a
  1314. * proxy owner
  1315. *
  1316. * @lock: the rt_mutex to be locked
  1317. * @proxy_owner:the task to set as owner
  1318. *
  1319. * No locking. Caller has to do serializing itself
  1320. * Special API call for PI-futex support
  1321. */
  1322. void rt_mutex_init_proxy_locked(struct rt_mutex *lock,
  1323. struct task_struct *proxy_owner)
  1324. {
  1325. __rt_mutex_init(lock, NULL);
  1326. debug_rt_mutex_proxy_lock(lock, proxy_owner);
  1327. rt_mutex_set_owner(lock, proxy_owner);
  1328. rt_mutex_deadlock_account_lock(lock, proxy_owner);
  1329. }
  1330. /**
  1331. * rt_mutex_proxy_unlock - release a lock on behalf of owner
  1332. *
  1333. * @lock: the rt_mutex to be locked
  1334. *
  1335. * No locking. Caller has to do serializing itself
  1336. * Special API call for PI-futex support
  1337. */
  1338. void rt_mutex_proxy_unlock(struct rt_mutex *lock,
  1339. struct task_struct *proxy_owner)
  1340. {
  1341. debug_rt_mutex_proxy_unlock(lock);
  1342. rt_mutex_set_owner(lock, NULL);
  1343. rt_mutex_deadlock_account_unlock(proxy_owner);
  1344. }
  1345. /**
  1346. * rt_mutex_start_proxy_lock() - Start lock acquisition for another task
  1347. * @lock: the rt_mutex to take
  1348. * @waiter: the pre-initialized rt_mutex_waiter
  1349. * @task: the task to prepare
  1350. *
  1351. * Returns:
  1352. * 0 - task blocked on lock
  1353. * 1 - acquired the lock for task, caller should wake it up
  1354. * <0 - error
  1355. *
  1356. * Special API call for FUTEX_REQUEUE_PI support.
  1357. */
  1358. int rt_mutex_start_proxy_lock(struct rt_mutex *lock,
  1359. struct rt_mutex_waiter *waiter,
  1360. struct task_struct *task)
  1361. {
  1362. int ret;
  1363. raw_spin_lock(&lock->wait_lock);
  1364. if (try_to_take_rt_mutex(lock, task, NULL)) {
  1365. raw_spin_unlock(&lock->wait_lock);
  1366. return 1;
  1367. }
  1368. /* We enforce deadlock detection for futexes */
  1369. ret = task_blocks_on_rt_mutex(lock, waiter, task,
  1370. RT_MUTEX_FULL_CHAINWALK);
  1371. if (ret && !rt_mutex_owner(lock)) {
  1372. /*
  1373. * Reset the return value. We might have
  1374. * returned with -EDEADLK and the owner
  1375. * released the lock while we were walking the
  1376. * pi chain. Let the waiter sort it out.
  1377. */
  1378. ret = 0;
  1379. }
  1380. if (unlikely(ret))
  1381. remove_waiter(lock, waiter);
  1382. raw_spin_unlock(&lock->wait_lock);
  1383. debug_rt_mutex_print_deadlock(waiter);
  1384. return ret;
  1385. }
  1386. /**
  1387. * rt_mutex_next_owner - return the next owner of the lock
  1388. *
  1389. * @lock: the rt lock query
  1390. *
  1391. * Returns the next owner of the lock or NULL
  1392. *
  1393. * Caller has to serialize against other accessors to the lock
  1394. * itself.
  1395. *
  1396. * Special API call for PI-futex support
  1397. */
  1398. struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock)
  1399. {
  1400. if (!rt_mutex_has_waiters(lock))
  1401. return NULL;
  1402. return rt_mutex_top_waiter(lock)->task;
  1403. }
  1404. /**
  1405. * rt_mutex_finish_proxy_lock() - Complete lock acquisition
  1406. * @lock: the rt_mutex we were woken on
  1407. * @to: the timeout, null if none. hrtimer should already have
  1408. * been started.
  1409. * @waiter: the pre-initialized rt_mutex_waiter
  1410. *
  1411. * Complete the lock acquisition started our behalf by another thread.
  1412. *
  1413. * Returns:
  1414. * 0 - success
  1415. * <0 - error, one of -EINTR, -ETIMEDOUT
  1416. *
  1417. * Special API call for PI-futex requeue support
  1418. */
  1419. int rt_mutex_finish_proxy_lock(struct rt_mutex *lock,
  1420. struct hrtimer_sleeper *to,
  1421. struct rt_mutex_waiter *waiter)
  1422. {
  1423. int ret;
  1424. raw_spin_lock(&lock->wait_lock);
  1425. set_current_state(TASK_INTERRUPTIBLE);
  1426. /* sleep on the mutex */
  1427. ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter);
  1428. if (unlikely(ret))
  1429. remove_waiter(lock, waiter);
  1430. /*
  1431. * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might
  1432. * have to fix that up.
  1433. */
  1434. fixup_rt_mutex_waiters(lock);
  1435. raw_spin_unlock(&lock->wait_lock);
  1436. return ret;
  1437. }