]>
Commit | Line | Data |
---|---|---|
23f78d4a IM |
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 | |
d07fe82c | 10 | * |
214e0aed | 11 | * See Documentation/locking/rt-mutex-design.txt for details. |
23f78d4a IM |
12 | */ |
13 | #include <linux/spinlock.h> | |
9984de1a | 14 | #include <linux/export.h> |
174cd4b1 | 15 | #include <linux/sched/signal.h> |
8bd75c77 | 16 | #include <linux/sched/rt.h> |
fb00aca4 | 17 | #include <linux/sched/deadline.h> |
84f001e1 | 18 | #include <linux/sched/wake_q.h> |
b17b0153 | 19 | #include <linux/sched/debug.h> |
23f78d4a IM |
20 | #include <linux/timer.h> |
21 | ||
22 | #include "rtmutex_common.h" | |
23 | ||
23f78d4a IM |
24 | /* |
25 | * lock->owner state tracking: | |
26 | * | |
8161239a LJ |
27 | * lock->owner holds the task_struct pointer of the owner. Bit 0 |
28 | * is used to keep track of the "lock has waiters" state. | |
23f78d4a | 29 | * |
8161239a LJ |
30 | * owner bit0 |
31 | * NULL 0 lock is free (fast acquire possible) | |
32 | * NULL 1 lock is free and has waiters and the top waiter | |
33 | * is going to take the lock* | |
34 | * taskpointer 0 lock is held (fast release possible) | |
35 | * taskpointer 1 lock is held and has waiters** | |
23f78d4a IM |
36 | * |
37 | * The fast atomic compare exchange based acquire and release is only | |
8161239a LJ |
38 | * possible when bit 0 of lock->owner is 0. |
39 | * | |
40 | * (*) It also can be a transitional state when grabbing the lock | |
41 | * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock, | |
42 | * we need to set the bit0 before looking at the lock, and the owner may be | |
43 | * NULL in this small time, hence this can be a transitional state. | |
23f78d4a | 44 | * |
8161239a LJ |
45 | * (**) There is a small time when bit 0 is set but there are no |
46 | * waiters. This can happen when grabbing the lock in the slow path. | |
47 | * To prevent a cmpxchg of the owner releasing the lock, we need to | |
48 | * set this bit before looking at the lock. | |
23f78d4a IM |
49 | */ |
50 | ||
bd197234 | 51 | static void |
8161239a | 52 | rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner) |
23f78d4a | 53 | { |
8161239a | 54 | unsigned long val = (unsigned long)owner; |
23f78d4a IM |
55 | |
56 | if (rt_mutex_has_waiters(lock)) | |
57 | val |= RT_MUTEX_HAS_WAITERS; | |
58 | ||
59 | lock->owner = (struct task_struct *)val; | |
60 | } | |
61 | ||
62 | static inline void clear_rt_mutex_waiters(struct rt_mutex *lock) | |
63 | { | |
64 | lock->owner = (struct task_struct *) | |
65 | ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS); | |
66 | } | |
67 | ||
68 | static void fixup_rt_mutex_waiters(struct rt_mutex *lock) | |
69 | { | |
dbb26055 TG |
70 | unsigned long owner, *p = (unsigned long *) &lock->owner; |
71 | ||
72 | if (rt_mutex_has_waiters(lock)) | |
73 | return; | |
74 | ||
75 | /* | |
76 | * The rbtree has no waiters enqueued, now make sure that the | |
77 | * lock->owner still has the waiters bit set, otherwise the | |
78 | * following can happen: | |
79 | * | |
80 | * CPU 0 CPU 1 CPU2 | |
81 | * l->owner=T1 | |
82 | * rt_mutex_lock(l) | |
83 | * lock(l->lock) | |
84 | * l->owner = T1 | HAS_WAITERS; | |
85 | * enqueue(T2) | |
86 | * boost() | |
87 | * unlock(l->lock) | |
88 | * block() | |
89 | * | |
90 | * rt_mutex_lock(l) | |
91 | * lock(l->lock) | |
92 | * l->owner = T1 | HAS_WAITERS; | |
93 | * enqueue(T3) | |
94 | * boost() | |
95 | * unlock(l->lock) | |
96 | * block() | |
97 | * signal(->T2) signal(->T3) | |
98 | * lock(l->lock) | |
99 | * dequeue(T2) | |
100 | * deboost() | |
101 | * unlock(l->lock) | |
102 | * lock(l->lock) | |
103 | * dequeue(T3) | |
104 | * ==> wait list is empty | |
105 | * deboost() | |
106 | * unlock(l->lock) | |
107 | * lock(l->lock) | |
108 | * fixup_rt_mutex_waiters() | |
109 | * if (wait_list_empty(l) { | |
110 | * l->owner = owner | |
111 | * owner = l->owner & ~HAS_WAITERS; | |
112 | * ==> l->owner = T1 | |
113 | * } | |
114 | * lock(l->lock) | |
115 | * rt_mutex_unlock(l) fixup_rt_mutex_waiters() | |
116 | * if (wait_list_empty(l) { | |
117 | * owner = l->owner & ~HAS_WAITERS; | |
118 | * cmpxchg(l->owner, T1, NULL) | |
119 | * ===> Success (l->owner = NULL) | |
120 | * | |
121 | * l->owner = owner | |
122 | * ==> l->owner = T1 | |
123 | * } | |
124 | * | |
125 | * With the check for the waiter bit in place T3 on CPU2 will not | |
126 | * overwrite. All tasks fiddling with the waiters bit are | |
127 | * serialized by l->lock, so nothing else can modify the waiters | |
128 | * bit. If the bit is set then nothing can change l->owner either | |
129 | * so the simple RMW is safe. The cmpxchg() will simply fail if it | |
130 | * happens in the middle of the RMW because the waiters bit is | |
131 | * still set. | |
132 | */ | |
133 | owner = READ_ONCE(*p); | |
134 | if (owner & RT_MUTEX_HAS_WAITERS) | |
135 | WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS); | |
23f78d4a IM |
136 | } |
137 | ||
bd197234 | 138 | /* |
cede8841 SAS |
139 | * We can speed up the acquire/release, if there's no debugging state to be |
140 | * set up. | |
bd197234 | 141 | */ |
cede8841 | 142 | #ifndef CONFIG_DEBUG_RT_MUTEXES |
700318d1 DB |
143 | # define rt_mutex_cmpxchg_relaxed(l,c,n) (cmpxchg_relaxed(&l->owner, c, n) == c) |
144 | # define rt_mutex_cmpxchg_acquire(l,c,n) (cmpxchg_acquire(&l->owner, c, n) == c) | |
145 | # define rt_mutex_cmpxchg_release(l,c,n) (cmpxchg_release(&l->owner, c, n) == c) | |
146 | ||
147 | /* | |
148 | * Callers must hold the ->wait_lock -- which is the whole purpose as we force | |
149 | * all future threads that attempt to [Rmw] the lock to the slowpath. As such | |
150 | * relaxed semantics suffice. | |
151 | */ | |
bd197234 TG |
152 | static inline void mark_rt_mutex_waiters(struct rt_mutex *lock) |
153 | { | |
154 | unsigned long owner, *p = (unsigned long *) &lock->owner; | |
155 | ||
156 | do { | |
157 | owner = *p; | |
700318d1 DB |
158 | } while (cmpxchg_relaxed(p, owner, |
159 | owner | RT_MUTEX_HAS_WAITERS) != owner); | |
bd197234 | 160 | } |
27e35715 TG |
161 | |
162 | /* | |
163 | * Safe fastpath aware unlock: | |
164 | * 1) Clear the waiters bit | |
165 | * 2) Drop lock->wait_lock | |
166 | * 3) Try to unlock the lock with cmpxchg | |
167 | */ | |
b4abf910 TG |
168 | static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock, |
169 | unsigned long flags) | |
27e35715 TG |
170 | __releases(lock->wait_lock) |
171 | { | |
172 | struct task_struct *owner = rt_mutex_owner(lock); | |
173 | ||
174 | clear_rt_mutex_waiters(lock); | |
b4abf910 | 175 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
27e35715 TG |
176 | /* |
177 | * If a new waiter comes in between the unlock and the cmpxchg | |
178 | * we have two situations: | |
179 | * | |
180 | * unlock(wait_lock); | |
181 | * lock(wait_lock); | |
182 | * cmpxchg(p, owner, 0) == owner | |
183 | * mark_rt_mutex_waiters(lock); | |
184 | * acquire(lock); | |
185 | * or: | |
186 | * | |
187 | * unlock(wait_lock); | |
188 | * lock(wait_lock); | |
189 | * mark_rt_mutex_waiters(lock); | |
190 | * | |
191 | * cmpxchg(p, owner, 0) != owner | |
192 | * enqueue_waiter(); | |
193 | * unlock(wait_lock); | |
194 | * lock(wait_lock); | |
195 | * wake waiter(); | |
196 | * unlock(wait_lock); | |
197 | * lock(wait_lock); | |
198 | * acquire(lock); | |
199 | */ | |
700318d1 | 200 | return rt_mutex_cmpxchg_release(lock, owner, NULL); |
27e35715 TG |
201 | } |
202 | ||
bd197234 | 203 | #else |
700318d1 DB |
204 | # define rt_mutex_cmpxchg_relaxed(l,c,n) (0) |
205 | # define rt_mutex_cmpxchg_acquire(l,c,n) (0) | |
206 | # define rt_mutex_cmpxchg_release(l,c,n) (0) | |
207 | ||
bd197234 TG |
208 | static inline void mark_rt_mutex_waiters(struct rt_mutex *lock) |
209 | { | |
210 | lock->owner = (struct task_struct *) | |
211 | ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS); | |
212 | } | |
27e35715 TG |
213 | |
214 | /* | |
215 | * Simple slow path only version: lock->owner is protected by lock->wait_lock. | |
216 | */ | |
b4abf910 TG |
217 | static inline bool unlock_rt_mutex_safe(struct rt_mutex *lock, |
218 | unsigned long flags) | |
27e35715 TG |
219 | __releases(lock->wait_lock) |
220 | { | |
221 | lock->owner = NULL; | |
b4abf910 | 222 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
27e35715 TG |
223 | return true; |
224 | } | |
bd197234 TG |
225 | #endif |
226 | ||
fb00aca4 PZ |
227 | static inline int |
228 | rt_mutex_waiter_less(struct rt_mutex_waiter *left, | |
229 | struct rt_mutex_waiter *right) | |
230 | { | |
2d3d891d | 231 | if (left->prio < right->prio) |
fb00aca4 PZ |
232 | return 1; |
233 | ||
234 | /* | |
2d3d891d DF |
235 | * If both waiters have dl_prio(), we check the deadlines of the |
236 | * associated tasks. | |
237 | * If left waiter has a dl_prio(), and we didn't return 1 above, | |
238 | * then right waiter has a dl_prio() too. | |
fb00aca4 | 239 | */ |
2d3d891d | 240 | if (dl_prio(left->prio)) |
f5240575 JL |
241 | return dl_time_before(left->task->dl.deadline, |
242 | right->task->dl.deadline); | |
fb00aca4 PZ |
243 | |
244 | return 0; | |
245 | } | |
246 | ||
247 | static void | |
248 | rt_mutex_enqueue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter) | |
249 | { | |
250 | struct rb_node **link = &lock->waiters.rb_node; | |
251 | struct rb_node *parent = NULL; | |
252 | struct rt_mutex_waiter *entry; | |
253 | int leftmost = 1; | |
254 | ||
255 | while (*link) { | |
256 | parent = *link; | |
257 | entry = rb_entry(parent, struct rt_mutex_waiter, tree_entry); | |
258 | if (rt_mutex_waiter_less(waiter, entry)) { | |
259 | link = &parent->rb_left; | |
260 | } else { | |
261 | link = &parent->rb_right; | |
262 | leftmost = 0; | |
263 | } | |
264 | } | |
265 | ||
266 | if (leftmost) | |
267 | lock->waiters_leftmost = &waiter->tree_entry; | |
268 | ||
269 | rb_link_node(&waiter->tree_entry, parent, link); | |
270 | rb_insert_color(&waiter->tree_entry, &lock->waiters); | |
271 | } | |
272 | ||
273 | static void | |
274 | rt_mutex_dequeue(struct rt_mutex *lock, struct rt_mutex_waiter *waiter) | |
275 | { | |
276 | if (RB_EMPTY_NODE(&waiter->tree_entry)) | |
277 | return; | |
278 | ||
279 | if (lock->waiters_leftmost == &waiter->tree_entry) | |
280 | lock->waiters_leftmost = rb_next(&waiter->tree_entry); | |
281 | ||
282 | rb_erase(&waiter->tree_entry, &lock->waiters); | |
283 | RB_CLEAR_NODE(&waiter->tree_entry); | |
284 | } | |
285 | ||
286 | static void | |
287 | rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter) | |
288 | { | |
289 | struct rb_node **link = &task->pi_waiters.rb_node; | |
290 | struct rb_node *parent = NULL; | |
291 | struct rt_mutex_waiter *entry; | |
292 | int leftmost = 1; | |
293 | ||
294 | while (*link) { | |
295 | parent = *link; | |
296 | entry = rb_entry(parent, struct rt_mutex_waiter, pi_tree_entry); | |
297 | if (rt_mutex_waiter_less(waiter, entry)) { | |
298 | link = &parent->rb_left; | |
299 | } else { | |
300 | link = &parent->rb_right; | |
301 | leftmost = 0; | |
302 | } | |
303 | } | |
304 | ||
305 | if (leftmost) | |
306 | task->pi_waiters_leftmost = &waiter->pi_tree_entry; | |
307 | ||
308 | rb_link_node(&waiter->pi_tree_entry, parent, link); | |
309 | rb_insert_color(&waiter->pi_tree_entry, &task->pi_waiters); | |
310 | } | |
311 | ||
312 | static void | |
313 | rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter) | |
314 | { | |
315 | if (RB_EMPTY_NODE(&waiter->pi_tree_entry)) | |
316 | return; | |
317 | ||
318 | if (task->pi_waiters_leftmost == &waiter->pi_tree_entry) | |
319 | task->pi_waiters_leftmost = rb_next(&waiter->pi_tree_entry); | |
320 | ||
321 | rb_erase(&waiter->pi_tree_entry, &task->pi_waiters); | |
322 | RB_CLEAR_NODE(&waiter->pi_tree_entry); | |
323 | } | |
324 | ||
23f78d4a | 325 | /* |
fb00aca4 | 326 | * Calculate task priority from the waiter tree priority |
23f78d4a | 327 | * |
fb00aca4 | 328 | * Return task->normal_prio when the waiter tree is empty or when |
23f78d4a IM |
329 | * the waiter is not allowed to do priority boosting |
330 | */ | |
331 | int rt_mutex_getprio(struct task_struct *task) | |
332 | { | |
333 | if (likely(!task_has_pi_waiters(task))) | |
334 | return task->normal_prio; | |
335 | ||
2d3d891d | 336 | return min(task_top_pi_waiter(task)->prio, |
23f78d4a IM |
337 | task->normal_prio); |
338 | } | |
339 | ||
2d3d891d DF |
340 | struct task_struct *rt_mutex_get_top_task(struct task_struct *task) |
341 | { | |
342 | if (likely(!task_has_pi_waiters(task))) | |
343 | return NULL; | |
344 | ||
345 | return task_top_pi_waiter(task)->task; | |
346 | } | |
347 | ||
c365c292 | 348 | /* |
0782e63b TG |
349 | * Called by sched_setscheduler() to get the priority which will be |
350 | * effective after the change. | |
c365c292 | 351 | */ |
0782e63b | 352 | int rt_mutex_get_effective_prio(struct task_struct *task, int newprio) |
c365c292 TG |
353 | { |
354 | if (!task_has_pi_waiters(task)) | |
0782e63b | 355 | return newprio; |
c365c292 | 356 | |
0782e63b TG |
357 | if (task_top_pi_waiter(task)->task->prio <= newprio) |
358 | return task_top_pi_waiter(task)->task->prio; | |
359 | return newprio; | |
c365c292 TG |
360 | } |
361 | ||
23f78d4a IM |
362 | /* |
363 | * Adjust the priority of a task, after its pi_waiters got modified. | |
364 | * | |
365 | * This can be both boosting and unboosting. task->pi_lock must be held. | |
366 | */ | |
bd197234 | 367 | static void __rt_mutex_adjust_prio(struct task_struct *task) |
23f78d4a IM |
368 | { |
369 | int prio = rt_mutex_getprio(task); | |
370 | ||
2d3d891d | 371 | if (task->prio != prio || dl_prio(prio)) |
23f78d4a IM |
372 | rt_mutex_setprio(task, prio); |
373 | } | |
374 | ||
375 | /* | |
376 | * Adjust task priority (undo boosting). Called from the exit path of | |
377 | * rt_mutex_slowunlock() and rt_mutex_slowlock(). | |
378 | * | |
379 | * (Note: We do this outside of the protection of lock->wait_lock to | |
380 | * allow the lock to be taken while or before we readjust the priority | |
381 | * of task. We do not use the spin_xx_mutex() variants here as we are | |
382 | * outside of the debug path.) | |
383 | */ | |
802ab58d | 384 | void rt_mutex_adjust_prio(struct task_struct *task) |
23f78d4a IM |
385 | { |
386 | unsigned long flags; | |
387 | ||
1d615482 | 388 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
23f78d4a | 389 | __rt_mutex_adjust_prio(task); |
1d615482 | 390 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
23f78d4a IM |
391 | } |
392 | ||
8930ed80 TG |
393 | /* |
394 | * Deadlock detection is conditional: | |
395 | * | |
396 | * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted | |
397 | * if the detect argument is == RT_MUTEX_FULL_CHAINWALK. | |
398 | * | |
399 | * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always | |
400 | * conducted independent of the detect argument. | |
401 | * | |
402 | * If the waiter argument is NULL this indicates the deboost path and | |
403 | * deadlock detection is disabled independent of the detect argument | |
404 | * and the config settings. | |
405 | */ | |
406 | static bool rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter, | |
407 | enum rtmutex_chainwalk chwalk) | |
408 | { | |
409 | /* | |
410 | * This is just a wrapper function for the following call, | |
411 | * because debug_rt_mutex_detect_deadlock() smells like a magic | |
412 | * debug feature and I wanted to keep the cond function in the | |
413 | * main source file along with the comments instead of having | |
414 | * two of the same in the headers. | |
415 | */ | |
416 | return debug_rt_mutex_detect_deadlock(waiter, chwalk); | |
417 | } | |
418 | ||
23f78d4a IM |
419 | /* |
420 | * Max number of times we'll walk the boosting chain: | |
421 | */ | |
422 | int max_lock_depth = 1024; | |
423 | ||
82084984 TG |
424 | static inline struct rt_mutex *task_blocked_on_lock(struct task_struct *p) |
425 | { | |
426 | return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL; | |
427 | } | |
428 | ||
23f78d4a IM |
429 | /* |
430 | * Adjust the priority chain. Also used for deadlock detection. | |
431 | * Decreases task's usage by one - may thus free the task. | |
0c106173 | 432 | * |
82084984 TG |
433 | * @task: the task owning the mutex (owner) for which a chain walk is |
434 | * probably needed | |
e6beaa36 | 435 | * @chwalk: do we have to carry out deadlock detection? |
82084984 TG |
436 | * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck |
437 | * things for a task that has just got its priority adjusted, and | |
438 | * is waiting on a mutex) | |
439 | * @next_lock: the mutex on which the owner of @orig_lock was blocked before | |
440 | * we dropped its pi_lock. Is never dereferenced, only used for | |
441 | * comparison to detect lock chain changes. | |
0c106173 | 442 | * @orig_waiter: rt_mutex_waiter struct for the task that has just donated |
82084984 TG |
443 | * its priority to the mutex owner (can be NULL in the case |
444 | * depicted above or if the top waiter is gone away and we are | |
445 | * actually deboosting the owner) | |
446 | * @top_task: the current top waiter | |
0c106173 | 447 | * |
23f78d4a | 448 | * Returns 0 or -EDEADLK. |
3eb65aea TG |
449 | * |
450 | * Chain walk basics and protection scope | |
451 | * | |
452 | * [R] refcount on task | |
453 | * [P] task->pi_lock held | |
454 | * [L] rtmutex->wait_lock held | |
455 | * | |
456 | * Step Description Protected by | |
457 | * function arguments: | |
458 | * @task [R] | |
459 | * @orig_lock if != NULL @top_task is blocked on it | |
460 | * @next_lock Unprotected. Cannot be | |
461 | * dereferenced. Only used for | |
462 | * comparison. | |
463 | * @orig_waiter if != NULL @top_task is blocked on it | |
464 | * @top_task current, or in case of proxy | |
465 | * locking protected by calling | |
466 | * code | |
467 | * again: | |
468 | * loop_sanity_check(); | |
469 | * retry: | |
470 | * [1] lock(task->pi_lock); [R] acquire [P] | |
471 | * [2] waiter = task->pi_blocked_on; [P] | |
472 | * [3] check_exit_conditions_1(); [P] | |
473 | * [4] lock = waiter->lock; [P] | |
474 | * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L] | |
475 | * unlock(task->pi_lock); release [P] | |
476 | * goto retry; | |
477 | * } | |
478 | * [6] check_exit_conditions_2(); [P] + [L] | |
479 | * [7] requeue_lock_waiter(lock, waiter); [P] + [L] | |
480 | * [8] unlock(task->pi_lock); release [P] | |
481 | * put_task_struct(task); release [R] | |
482 | * [9] check_exit_conditions_3(); [L] | |
483 | * [10] task = owner(lock); [L] | |
484 | * get_task_struct(task); [L] acquire [R] | |
485 | * lock(task->pi_lock); [L] acquire [P] | |
486 | * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L] | |
487 | * [12] check_exit_conditions_4(); [P] + [L] | |
488 | * [13] unlock(task->pi_lock); release [P] | |
489 | * unlock(lock->wait_lock); release [L] | |
490 | * goto again; | |
23f78d4a | 491 | */ |
bd197234 | 492 | static int rt_mutex_adjust_prio_chain(struct task_struct *task, |
8930ed80 | 493 | enum rtmutex_chainwalk chwalk, |
bd197234 | 494 | struct rt_mutex *orig_lock, |
82084984 | 495 | struct rt_mutex *next_lock, |
bd197234 TG |
496 | struct rt_mutex_waiter *orig_waiter, |
497 | struct task_struct *top_task) | |
23f78d4a | 498 | { |
23f78d4a | 499 | struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter; |
a57594a1 | 500 | struct rt_mutex_waiter *prerequeue_top_waiter; |
8930ed80 | 501 | int ret = 0, depth = 0; |
a57594a1 | 502 | struct rt_mutex *lock; |
8930ed80 | 503 | bool detect_deadlock; |
67792e2c | 504 | bool requeue = true; |
23f78d4a | 505 | |
8930ed80 | 506 | detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk); |
23f78d4a IM |
507 | |
508 | /* | |
509 | * The (de)boosting is a step by step approach with a lot of | |
510 | * pitfalls. We want this to be preemptible and we want hold a | |
511 | * maximum of two locks per step. So we have to check | |
512 | * carefully whether things change under us. | |
513 | */ | |
514 | again: | |
3eb65aea TG |
515 | /* |
516 | * We limit the lock chain length for each invocation. | |
517 | */ | |
23f78d4a IM |
518 | if (++depth > max_lock_depth) { |
519 | static int prev_max; | |
520 | ||
521 | /* | |
522 | * Print this only once. If the admin changes the limit, | |
523 | * print a new message when reaching the limit again. | |
524 | */ | |
525 | if (prev_max != max_lock_depth) { | |
526 | prev_max = max_lock_depth; | |
527 | printk(KERN_WARNING "Maximum lock depth %d reached " | |
528 | "task: %s (%d)\n", max_lock_depth, | |
ba25f9dc | 529 | top_task->comm, task_pid_nr(top_task)); |
23f78d4a IM |
530 | } |
531 | put_task_struct(task); | |
532 | ||
3d5c9340 | 533 | return -EDEADLK; |
23f78d4a | 534 | } |
3eb65aea TG |
535 | |
536 | /* | |
537 | * We are fully preemptible here and only hold the refcount on | |
538 | * @task. So everything can have changed under us since the | |
539 | * caller or our own code below (goto retry/again) dropped all | |
540 | * locks. | |
541 | */ | |
23f78d4a IM |
542 | retry: |
543 | /* | |
3eb65aea | 544 | * [1] Task cannot go away as we did a get_task() before ! |
23f78d4a | 545 | */ |
b4abf910 | 546 | raw_spin_lock_irq(&task->pi_lock); |
23f78d4a | 547 | |
3eb65aea TG |
548 | /* |
549 | * [2] Get the waiter on which @task is blocked on. | |
550 | */ | |
23f78d4a | 551 | waiter = task->pi_blocked_on; |
3eb65aea TG |
552 | |
553 | /* | |
554 | * [3] check_exit_conditions_1() protected by task->pi_lock. | |
555 | */ | |
556 | ||
23f78d4a IM |
557 | /* |
558 | * Check whether the end of the boosting chain has been | |
559 | * reached or the state of the chain has changed while we | |
560 | * dropped the locks. | |
561 | */ | |
8161239a | 562 | if (!waiter) |
23f78d4a IM |
563 | goto out_unlock_pi; |
564 | ||
1a539a87 TG |
565 | /* |
566 | * Check the orig_waiter state. After we dropped the locks, | |
8161239a | 567 | * the previous owner of the lock might have released the lock. |
1a539a87 | 568 | */ |
8161239a | 569 | if (orig_waiter && !rt_mutex_owner(orig_lock)) |
1a539a87 TG |
570 | goto out_unlock_pi; |
571 | ||
82084984 TG |
572 | /* |
573 | * We dropped all locks after taking a refcount on @task, so | |
574 | * the task might have moved on in the lock chain or even left | |
575 | * the chain completely and blocks now on an unrelated lock or | |
576 | * on @orig_lock. | |
577 | * | |
578 | * We stored the lock on which @task was blocked in @next_lock, | |
579 | * so we can detect the chain change. | |
580 | */ | |
581 | if (next_lock != waiter->lock) | |
582 | goto out_unlock_pi; | |
583 | ||
1a539a87 TG |
584 | /* |
585 | * Drop out, when the task has no waiters. Note, | |
586 | * top_waiter can be NULL, when we are in the deboosting | |
587 | * mode! | |
588 | */ | |
397335f0 TG |
589 | if (top_waiter) { |
590 | if (!task_has_pi_waiters(task)) | |
591 | goto out_unlock_pi; | |
592 | /* | |
593 | * If deadlock detection is off, we stop here if we | |
67792e2c TG |
594 | * are not the top pi waiter of the task. If deadlock |
595 | * detection is enabled we continue, but stop the | |
596 | * requeueing in the chain walk. | |
397335f0 | 597 | */ |
67792e2c TG |
598 | if (top_waiter != task_top_pi_waiter(task)) { |
599 | if (!detect_deadlock) | |
600 | goto out_unlock_pi; | |
601 | else | |
602 | requeue = false; | |
603 | } | |
397335f0 | 604 | } |
23f78d4a IM |
605 | |
606 | /* | |
67792e2c TG |
607 | * If the waiter priority is the same as the task priority |
608 | * then there is no further priority adjustment necessary. If | |
609 | * deadlock detection is off, we stop the chain walk. If its | |
610 | * enabled we continue, but stop the requeueing in the chain | |
611 | * walk. | |
23f78d4a | 612 | */ |
67792e2c TG |
613 | if (waiter->prio == task->prio) { |
614 | if (!detect_deadlock) | |
615 | goto out_unlock_pi; | |
616 | else | |
617 | requeue = false; | |
618 | } | |
23f78d4a | 619 | |
3eb65aea TG |
620 | /* |
621 | * [4] Get the next lock | |
622 | */ | |
23f78d4a | 623 | lock = waiter->lock; |
3eb65aea TG |
624 | /* |
625 | * [5] We need to trylock here as we are holding task->pi_lock, | |
626 | * which is the reverse lock order versus the other rtmutex | |
627 | * operations. | |
628 | */ | |
d209d74d | 629 | if (!raw_spin_trylock(&lock->wait_lock)) { |
b4abf910 | 630 | raw_spin_unlock_irq(&task->pi_lock); |
23f78d4a IM |
631 | cpu_relax(); |
632 | goto retry; | |
633 | } | |
634 | ||
397335f0 | 635 | /* |
3eb65aea TG |
636 | * [6] check_exit_conditions_2() protected by task->pi_lock and |
637 | * lock->wait_lock. | |
638 | * | |
397335f0 TG |
639 | * Deadlock detection. If the lock is the same as the original |
640 | * lock which caused us to walk the lock chain or if the | |
641 | * current lock is owned by the task which initiated the chain | |
642 | * walk, we detected a deadlock. | |
643 | */ | |
95e02ca9 | 644 | if (lock == orig_lock || rt_mutex_owner(lock) == top_task) { |
8930ed80 | 645 | debug_rt_mutex_deadlock(chwalk, orig_waiter, lock); |
d209d74d | 646 | raw_spin_unlock(&lock->wait_lock); |
3d5c9340 | 647 | ret = -EDEADLK; |
23f78d4a IM |
648 | goto out_unlock_pi; |
649 | } | |
650 | ||
67792e2c TG |
651 | /* |
652 | * If we just follow the lock chain for deadlock detection, no | |
653 | * need to do all the requeue operations. To avoid a truckload | |
654 | * of conditionals around the various places below, just do the | |
655 | * minimum chain walk checks. | |
656 | */ | |
657 | if (!requeue) { | |
658 | /* | |
659 | * No requeue[7] here. Just release @task [8] | |
660 | */ | |
b4abf910 | 661 | raw_spin_unlock(&task->pi_lock); |
67792e2c TG |
662 | put_task_struct(task); |
663 | ||
664 | /* | |
665 | * [9] check_exit_conditions_3 protected by lock->wait_lock. | |
666 | * If there is no owner of the lock, end of chain. | |
667 | */ | |
668 | if (!rt_mutex_owner(lock)) { | |
b4abf910 | 669 | raw_spin_unlock_irq(&lock->wait_lock); |
67792e2c TG |
670 | return 0; |
671 | } | |
672 | ||
673 | /* [10] Grab the next task, i.e. owner of @lock */ | |
674 | task = rt_mutex_owner(lock); | |
675 | get_task_struct(task); | |
b4abf910 | 676 | raw_spin_lock(&task->pi_lock); |
67792e2c TG |
677 | |
678 | /* | |
679 | * No requeue [11] here. We just do deadlock detection. | |
680 | * | |
681 | * [12] Store whether owner is blocked | |
682 | * itself. Decision is made after dropping the locks | |
683 | */ | |
684 | next_lock = task_blocked_on_lock(task); | |
685 | /* | |
686 | * Get the top waiter for the next iteration | |
687 | */ | |
688 | top_waiter = rt_mutex_top_waiter(lock); | |
689 | ||
690 | /* [13] Drop locks */ | |
b4abf910 TG |
691 | raw_spin_unlock(&task->pi_lock); |
692 | raw_spin_unlock_irq(&lock->wait_lock); | |
67792e2c TG |
693 | |
694 | /* If owner is not blocked, end of chain. */ | |
695 | if (!next_lock) | |
696 | goto out_put_task; | |
697 | goto again; | |
698 | } | |
699 | ||
a57594a1 TG |
700 | /* |
701 | * Store the current top waiter before doing the requeue | |
702 | * operation on @lock. We need it for the boost/deboost | |
703 | * decision below. | |
704 | */ | |
705 | prerequeue_top_waiter = rt_mutex_top_waiter(lock); | |
23f78d4a | 706 | |
9f40a51a | 707 | /* [7] Requeue the waiter in the lock waiter tree. */ |
fb00aca4 | 708 | rt_mutex_dequeue(lock, waiter); |
2d3d891d | 709 | waiter->prio = task->prio; |
fb00aca4 | 710 | rt_mutex_enqueue(lock, waiter); |
23f78d4a | 711 | |
3eb65aea | 712 | /* [8] Release the task */ |
b4abf910 | 713 | raw_spin_unlock(&task->pi_lock); |
2ffa5a5c TG |
714 | put_task_struct(task); |
715 | ||
a57594a1 | 716 | /* |
3eb65aea TG |
717 | * [9] check_exit_conditions_3 protected by lock->wait_lock. |
718 | * | |
a57594a1 TG |
719 | * We must abort the chain walk if there is no lock owner even |
720 | * in the dead lock detection case, as we have nothing to | |
721 | * follow here. This is the end of the chain we are walking. | |
722 | */ | |
8161239a LJ |
723 | if (!rt_mutex_owner(lock)) { |
724 | /* | |
3eb65aea TG |
725 | * If the requeue [7] above changed the top waiter, |
726 | * then we need to wake the new top waiter up to try | |
727 | * to get the lock. | |
8161239a | 728 | */ |
a57594a1 | 729 | if (prerequeue_top_waiter != rt_mutex_top_waiter(lock)) |
8161239a | 730 | wake_up_process(rt_mutex_top_waiter(lock)->task); |
b4abf910 | 731 | raw_spin_unlock_irq(&lock->wait_lock); |
2ffa5a5c | 732 | return 0; |
8161239a | 733 | } |
23f78d4a | 734 | |
3eb65aea | 735 | /* [10] Grab the next task, i.e. the owner of @lock */ |
23f78d4a | 736 | task = rt_mutex_owner(lock); |
db630637 | 737 | get_task_struct(task); |
b4abf910 | 738 | raw_spin_lock(&task->pi_lock); |
23f78d4a | 739 | |
3eb65aea | 740 | /* [11] requeue the pi waiters if necessary */ |
23f78d4a | 741 | if (waiter == rt_mutex_top_waiter(lock)) { |
a57594a1 TG |
742 | /* |
743 | * The waiter became the new top (highest priority) | |
744 | * waiter on the lock. Replace the previous top waiter | |
9f40a51a | 745 | * in the owner tasks pi waiters tree with this waiter |
a57594a1 TG |
746 | * and adjust the priority of the owner. |
747 | */ | |
748 | rt_mutex_dequeue_pi(task, prerequeue_top_waiter); | |
fb00aca4 | 749 | rt_mutex_enqueue_pi(task, waiter); |
23f78d4a IM |
750 | __rt_mutex_adjust_prio(task); |
751 | ||
a57594a1 TG |
752 | } else if (prerequeue_top_waiter == waiter) { |
753 | /* | |
754 | * The waiter was the top waiter on the lock, but is | |
755 | * no longer the top prority waiter. Replace waiter in | |
9f40a51a | 756 | * the owner tasks pi waiters tree with the new top |
a57594a1 TG |
757 | * (highest priority) waiter and adjust the priority |
758 | * of the owner. | |
759 | * The new top waiter is stored in @waiter so that | |
760 | * @waiter == @top_waiter evaluates to true below and | |
761 | * we continue to deboost the rest of the chain. | |
762 | */ | |
fb00aca4 | 763 | rt_mutex_dequeue_pi(task, waiter); |
23f78d4a | 764 | waiter = rt_mutex_top_waiter(lock); |
fb00aca4 | 765 | rt_mutex_enqueue_pi(task, waiter); |
23f78d4a | 766 | __rt_mutex_adjust_prio(task); |
a57594a1 TG |
767 | } else { |
768 | /* | |
769 | * Nothing changed. No need to do any priority | |
770 | * adjustment. | |
771 | */ | |
23f78d4a IM |
772 | } |
773 | ||
82084984 | 774 | /* |
3eb65aea TG |
775 | * [12] check_exit_conditions_4() protected by task->pi_lock |
776 | * and lock->wait_lock. The actual decisions are made after we | |
777 | * dropped the locks. | |
778 | * | |
82084984 TG |
779 | * Check whether the task which owns the current lock is pi |
780 | * blocked itself. If yes we store a pointer to the lock for | |
781 | * the lock chain change detection above. After we dropped | |
782 | * task->pi_lock next_lock cannot be dereferenced anymore. | |
783 | */ | |
784 | next_lock = task_blocked_on_lock(task); | |
a57594a1 TG |
785 | /* |
786 | * Store the top waiter of @lock for the end of chain walk | |
787 | * decision below. | |
788 | */ | |
23f78d4a | 789 | top_waiter = rt_mutex_top_waiter(lock); |
3eb65aea TG |
790 | |
791 | /* [13] Drop the locks */ | |
b4abf910 TG |
792 | raw_spin_unlock(&task->pi_lock); |
793 | raw_spin_unlock_irq(&lock->wait_lock); | |
23f78d4a | 794 | |
82084984 | 795 | /* |
3eb65aea TG |
796 | * Make the actual exit decisions [12], based on the stored |
797 | * values. | |
798 | * | |
82084984 TG |
799 | * We reached the end of the lock chain. Stop right here. No |
800 | * point to go back just to figure that out. | |
801 | */ | |
802 | if (!next_lock) | |
803 | goto out_put_task; | |
804 | ||
a57594a1 TG |
805 | /* |
806 | * If the current waiter is not the top waiter on the lock, | |
807 | * then we can stop the chain walk here if we are not in full | |
808 | * deadlock detection mode. | |
809 | */ | |
23f78d4a IM |
810 | if (!detect_deadlock && waiter != top_waiter) |
811 | goto out_put_task; | |
812 | ||
813 | goto again; | |
814 | ||
815 | out_unlock_pi: | |
b4abf910 | 816 | raw_spin_unlock_irq(&task->pi_lock); |
23f78d4a IM |
817 | out_put_task: |
818 | put_task_struct(task); | |
36c8b586 | 819 | |
23f78d4a IM |
820 | return ret; |
821 | } | |
822 | ||
23f78d4a IM |
823 | /* |
824 | * Try to take an rt-mutex | |
825 | * | |
b4abf910 | 826 | * Must be called with lock->wait_lock held and interrupts disabled |
8161239a | 827 | * |
358c331f TG |
828 | * @lock: The lock to be acquired. |
829 | * @task: The task which wants to acquire the lock | |
9f40a51a | 830 | * @waiter: The waiter that is queued to the lock's wait tree if the |
358c331f | 831 | * callsite called task_blocked_on_lock(), otherwise NULL |
23f78d4a | 832 | */ |
8161239a | 833 | static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task, |
358c331f | 834 | struct rt_mutex_waiter *waiter) |
23f78d4a IM |
835 | { |
836 | /* | |
358c331f TG |
837 | * Before testing whether we can acquire @lock, we set the |
838 | * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all | |
839 | * other tasks which try to modify @lock into the slow path | |
840 | * and they serialize on @lock->wait_lock. | |
23f78d4a | 841 | * |
358c331f TG |
842 | * The RT_MUTEX_HAS_WAITERS bit can have a transitional state |
843 | * as explained at the top of this file if and only if: | |
23f78d4a | 844 | * |
358c331f TG |
845 | * - There is a lock owner. The caller must fixup the |
846 | * transient state if it does a trylock or leaves the lock | |
847 | * function due to a signal or timeout. | |
848 | * | |
849 | * - @task acquires the lock and there are no other | |
850 | * waiters. This is undone in rt_mutex_set_owner(@task) at | |
851 | * the end of this function. | |
23f78d4a IM |
852 | */ |
853 | mark_rt_mutex_waiters(lock); | |
854 | ||
358c331f TG |
855 | /* |
856 | * If @lock has an owner, give up. | |
857 | */ | |
8161239a | 858 | if (rt_mutex_owner(lock)) |
23f78d4a IM |
859 | return 0; |
860 | ||
8161239a | 861 | /* |
358c331f | 862 | * If @waiter != NULL, @task has already enqueued the waiter |
9f40a51a | 863 | * into @lock waiter tree. If @waiter == NULL then this is a |
358c331f | 864 | * trylock attempt. |
8161239a | 865 | */ |
358c331f TG |
866 | if (waiter) { |
867 | /* | |
868 | * If waiter is not the highest priority waiter of | |
869 | * @lock, give up. | |
870 | */ | |
871 | if (waiter != rt_mutex_top_waiter(lock)) | |
872 | return 0; | |
8161239a | 873 | |
358c331f TG |
874 | /* |
875 | * We can acquire the lock. Remove the waiter from the | |
9f40a51a | 876 | * lock waiters tree. |
358c331f TG |
877 | */ |
878 | rt_mutex_dequeue(lock, waiter); | |
8161239a | 879 | |
358c331f | 880 | } else { |
8161239a | 881 | /* |
358c331f TG |
882 | * If the lock has waiters already we check whether @task is |
883 | * eligible to take over the lock. | |
884 | * | |
885 | * If there are no other waiters, @task can acquire | |
886 | * the lock. @task->pi_blocked_on is NULL, so it does | |
887 | * not need to be dequeued. | |
8161239a LJ |
888 | */ |
889 | if (rt_mutex_has_waiters(lock)) { | |
358c331f TG |
890 | /* |
891 | * If @task->prio is greater than or equal to | |
892 | * the top waiter priority (kernel view), | |
893 | * @task lost. | |
894 | */ | |
895 | if (task->prio >= rt_mutex_top_waiter(lock)->prio) | |
896 | return 0; | |
897 | ||
898 | /* | |
899 | * The current top waiter stays enqueued. We | |
900 | * don't have to change anything in the lock | |
901 | * waiters order. | |
902 | */ | |
903 | } else { | |
904 | /* | |
905 | * No waiters. Take the lock without the | |
906 | * pi_lock dance.@task->pi_blocked_on is NULL | |
907 | * and we have no waiters to enqueue in @task | |
9f40a51a | 908 | * pi waiters tree. |
358c331f TG |
909 | */ |
910 | goto takeit; | |
8161239a | 911 | } |
8161239a LJ |
912 | } |
913 | ||
358c331f TG |
914 | /* |
915 | * Clear @task->pi_blocked_on. Requires protection by | |
916 | * @task->pi_lock. Redundant operation for the @waiter == NULL | |
917 | * case, but conditionals are more expensive than a redundant | |
918 | * store. | |
919 | */ | |
b4abf910 | 920 | raw_spin_lock(&task->pi_lock); |
358c331f TG |
921 | task->pi_blocked_on = NULL; |
922 | /* | |
923 | * Finish the lock acquisition. @task is the new owner. If | |
924 | * other waiters exist we have to insert the highest priority | |
9f40a51a | 925 | * waiter into @task->pi_waiters tree. |
358c331f TG |
926 | */ |
927 | if (rt_mutex_has_waiters(lock)) | |
928 | rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock)); | |
b4abf910 | 929 | raw_spin_unlock(&task->pi_lock); |
358c331f TG |
930 | |
931 | takeit: | |
23f78d4a | 932 | /* We got the lock. */ |
9a11b49a | 933 | debug_rt_mutex_lock(lock); |
23f78d4a | 934 | |
358c331f TG |
935 | /* |
936 | * This either preserves the RT_MUTEX_HAS_WAITERS bit if there | |
937 | * are still waiters or clears it. | |
938 | */ | |
8161239a | 939 | rt_mutex_set_owner(lock, task); |
23f78d4a | 940 | |
23f78d4a IM |
941 | return 1; |
942 | } | |
943 | ||
944 | /* | |
945 | * Task blocks on lock. | |
946 | * | |
947 | * Prepare waiter and propagate pi chain | |
948 | * | |
b4abf910 | 949 | * This must be called with lock->wait_lock held and interrupts disabled |
23f78d4a IM |
950 | */ |
951 | static int task_blocks_on_rt_mutex(struct rt_mutex *lock, | |
952 | struct rt_mutex_waiter *waiter, | |
8dac456a | 953 | struct task_struct *task, |
8930ed80 | 954 | enum rtmutex_chainwalk chwalk) |
23f78d4a | 955 | { |
36c8b586 | 956 | struct task_struct *owner = rt_mutex_owner(lock); |
23f78d4a | 957 | struct rt_mutex_waiter *top_waiter = waiter; |
82084984 | 958 | struct rt_mutex *next_lock; |
db630637 | 959 | int chain_walk = 0, res; |
23f78d4a | 960 | |
397335f0 TG |
961 | /* |
962 | * Early deadlock detection. We really don't want the task to | |
963 | * enqueue on itself just to untangle the mess later. It's not | |
964 | * only an optimization. We drop the locks, so another waiter | |
965 | * can come in before the chain walk detects the deadlock. So | |
966 | * the other will detect the deadlock and return -EDEADLOCK, | |
967 | * which is wrong, as the other waiter is not in a deadlock | |
968 | * situation. | |
969 | */ | |
3d5c9340 | 970 | if (owner == task) |
397335f0 TG |
971 | return -EDEADLK; |
972 | ||
b4abf910 | 973 | raw_spin_lock(&task->pi_lock); |
8dac456a DH |
974 | __rt_mutex_adjust_prio(task); |
975 | waiter->task = task; | |
23f78d4a | 976 | waiter->lock = lock; |
2d3d891d | 977 | waiter->prio = task->prio; |
23f78d4a IM |
978 | |
979 | /* Get the top priority waiter on the lock */ | |
980 | if (rt_mutex_has_waiters(lock)) | |
981 | top_waiter = rt_mutex_top_waiter(lock); | |
fb00aca4 | 982 | rt_mutex_enqueue(lock, waiter); |
23f78d4a | 983 | |
8dac456a | 984 | task->pi_blocked_on = waiter; |
23f78d4a | 985 | |
b4abf910 | 986 | raw_spin_unlock(&task->pi_lock); |
23f78d4a | 987 | |
8161239a LJ |
988 | if (!owner) |
989 | return 0; | |
990 | ||
b4abf910 | 991 | raw_spin_lock(&owner->pi_lock); |
23f78d4a | 992 | if (waiter == rt_mutex_top_waiter(lock)) { |
fb00aca4 PZ |
993 | rt_mutex_dequeue_pi(owner, top_waiter); |
994 | rt_mutex_enqueue_pi(owner, waiter); | |
23f78d4a IM |
995 | |
996 | __rt_mutex_adjust_prio(owner); | |
db630637 SR |
997 | if (owner->pi_blocked_on) |
998 | chain_walk = 1; | |
8930ed80 | 999 | } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) { |
db630637 | 1000 | chain_walk = 1; |
82084984 | 1001 | } |
db630637 | 1002 | |
82084984 TG |
1003 | /* Store the lock on which owner is blocked or NULL */ |
1004 | next_lock = task_blocked_on_lock(owner); | |
1005 | ||
b4abf910 | 1006 | raw_spin_unlock(&owner->pi_lock); |
82084984 TG |
1007 | /* |
1008 | * Even if full deadlock detection is on, if the owner is not | |
1009 | * blocked itself, we can avoid finding this out in the chain | |
1010 | * walk. | |
1011 | */ | |
1012 | if (!chain_walk || !next_lock) | |
23f78d4a IM |
1013 | return 0; |
1014 | ||
db630637 SR |
1015 | /* |
1016 | * The owner can't disappear while holding a lock, | |
1017 | * so the owner struct is protected by wait_lock. | |
1018 | * Gets dropped in rt_mutex_adjust_prio_chain()! | |
1019 | */ | |
1020 | get_task_struct(owner); | |
1021 | ||
b4abf910 | 1022 | raw_spin_unlock_irq(&lock->wait_lock); |
23f78d4a | 1023 | |
8930ed80 | 1024 | res = rt_mutex_adjust_prio_chain(owner, chwalk, lock, |
82084984 | 1025 | next_lock, waiter, task); |
23f78d4a | 1026 | |
b4abf910 | 1027 | raw_spin_lock_irq(&lock->wait_lock); |
23f78d4a IM |
1028 | |
1029 | return res; | |
1030 | } | |
1031 | ||
1032 | /* | |
9f40a51a | 1033 | * Remove the top waiter from the current tasks pi waiter tree and |
45ab4eff | 1034 | * queue it up. |
23f78d4a | 1035 | * |
b4abf910 | 1036 | * Called with lock->wait_lock held and interrupts disabled. |
23f78d4a | 1037 | */ |
45ab4eff DB |
1038 | static void mark_wakeup_next_waiter(struct wake_q_head *wake_q, |
1039 | struct rt_mutex *lock) | |
23f78d4a IM |
1040 | { |
1041 | struct rt_mutex_waiter *waiter; | |
23f78d4a | 1042 | |
b4abf910 | 1043 | raw_spin_lock(¤t->pi_lock); |
23f78d4a IM |
1044 | |
1045 | waiter = rt_mutex_top_waiter(lock); | |
23f78d4a IM |
1046 | |
1047 | /* | |
1048 | * Remove it from current->pi_waiters. We do not adjust a | |
1049 | * possible priority boost right now. We execute wakeup in the | |
1050 | * boosted mode and go back to normal after releasing | |
1051 | * lock->wait_lock. | |
1052 | */ | |
fb00aca4 | 1053 | rt_mutex_dequeue_pi(current, waiter); |
23f78d4a | 1054 | |
27e35715 TG |
1055 | /* |
1056 | * As we are waking up the top waiter, and the waiter stays | |
1057 | * queued on the lock until it gets the lock, this lock | |
1058 | * obviously has waiters. Just set the bit here and this has | |
1059 | * the added benefit of forcing all new tasks into the | |
1060 | * slow path making sure no task of lower priority than | |
1061 | * the top waiter can steal this lock. | |
1062 | */ | |
1063 | lock->owner = (void *) RT_MUTEX_HAS_WAITERS; | |
23f78d4a | 1064 | |
b4abf910 | 1065 | raw_spin_unlock(¤t->pi_lock); |
23f78d4a | 1066 | |
45ab4eff | 1067 | wake_q_add(wake_q, waiter->task); |
23f78d4a IM |
1068 | } |
1069 | ||
1070 | /* | |
8161239a | 1071 | * Remove a waiter from a lock and give up |
23f78d4a | 1072 | * |
b4abf910 | 1073 | * Must be called with lock->wait_lock held and interrupts disabled. I must |
8161239a | 1074 | * have just failed to try_to_take_rt_mutex(). |
23f78d4a | 1075 | */ |
bd197234 TG |
1076 | static void remove_waiter(struct rt_mutex *lock, |
1077 | struct rt_mutex_waiter *waiter) | |
23f78d4a | 1078 | { |
1ca7b860 | 1079 | bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock)); |
36c8b586 | 1080 | struct task_struct *owner = rt_mutex_owner(lock); |
1ca7b860 | 1081 | struct rt_mutex *next_lock; |
23f78d4a | 1082 | |
b4abf910 | 1083 | raw_spin_lock(¤t->pi_lock); |
fb00aca4 | 1084 | rt_mutex_dequeue(lock, waiter); |
23f78d4a | 1085 | current->pi_blocked_on = NULL; |
b4abf910 | 1086 | raw_spin_unlock(¤t->pi_lock); |
23f78d4a | 1087 | |
1ca7b860 TG |
1088 | /* |
1089 | * Only update priority if the waiter was the highest priority | |
1090 | * waiter of the lock and there is an owner to update. | |
1091 | */ | |
1092 | if (!owner || !is_top_waiter) | |
8161239a LJ |
1093 | return; |
1094 | ||
b4abf910 | 1095 | raw_spin_lock(&owner->pi_lock); |
23f78d4a | 1096 | |
1ca7b860 | 1097 | rt_mutex_dequeue_pi(owner, waiter); |
23f78d4a | 1098 | |
1ca7b860 TG |
1099 | if (rt_mutex_has_waiters(lock)) |
1100 | rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock)); | |
23f78d4a | 1101 | |
1ca7b860 | 1102 | __rt_mutex_adjust_prio(owner); |
23f78d4a | 1103 | |
1ca7b860 TG |
1104 | /* Store the lock on which owner is blocked or NULL */ |
1105 | next_lock = task_blocked_on_lock(owner); | |
db630637 | 1106 | |
b4abf910 | 1107 | raw_spin_unlock(&owner->pi_lock); |
23f78d4a | 1108 | |
1ca7b860 TG |
1109 | /* |
1110 | * Don't walk the chain, if the owner task is not blocked | |
1111 | * itself. | |
1112 | */ | |
82084984 | 1113 | if (!next_lock) |
23f78d4a IM |
1114 | return; |
1115 | ||
db630637 SR |
1116 | /* gets dropped in rt_mutex_adjust_prio_chain()! */ |
1117 | get_task_struct(owner); | |
1118 | ||
b4abf910 | 1119 | raw_spin_unlock_irq(&lock->wait_lock); |
23f78d4a | 1120 | |
8930ed80 TG |
1121 | rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock, |
1122 | next_lock, NULL, current); | |
23f78d4a | 1123 | |
b4abf910 | 1124 | raw_spin_lock_irq(&lock->wait_lock); |
23f78d4a IM |
1125 | } |
1126 | ||
95e02ca9 TG |
1127 | /* |
1128 | * Recheck the pi chain, in case we got a priority setting | |
1129 | * | |
1130 | * Called from sched_setscheduler | |
1131 | */ | |
1132 | void rt_mutex_adjust_pi(struct task_struct *task) | |
1133 | { | |
1134 | struct rt_mutex_waiter *waiter; | |
82084984 | 1135 | struct rt_mutex *next_lock; |
95e02ca9 TG |
1136 | unsigned long flags; |
1137 | ||
1d615482 | 1138 | raw_spin_lock_irqsave(&task->pi_lock, flags); |
95e02ca9 TG |
1139 | |
1140 | waiter = task->pi_blocked_on; | |
2d3d891d DF |
1141 | if (!waiter || (waiter->prio == task->prio && |
1142 | !dl_prio(task->prio))) { | |
1d615482 | 1143 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
95e02ca9 TG |
1144 | return; |
1145 | } | |
82084984 | 1146 | next_lock = waiter->lock; |
1d615482 | 1147 | raw_spin_unlock_irqrestore(&task->pi_lock, flags); |
95e02ca9 | 1148 | |
db630637 SR |
1149 | /* gets dropped in rt_mutex_adjust_prio_chain()! */ |
1150 | get_task_struct(task); | |
82084984 | 1151 | |
8930ed80 TG |
1152 | rt_mutex_adjust_prio_chain(task, RT_MUTEX_MIN_CHAINWALK, NULL, |
1153 | next_lock, NULL, task); | |
95e02ca9 TG |
1154 | } |
1155 | ||
50809358 PZ |
1156 | void rt_mutex_init_waiter(struct rt_mutex_waiter *waiter) |
1157 | { | |
1158 | debug_rt_mutex_init_waiter(waiter); | |
1159 | RB_CLEAR_NODE(&waiter->pi_tree_entry); | |
1160 | RB_CLEAR_NODE(&waiter->tree_entry); | |
1161 | waiter->task = NULL; | |
1162 | } | |
1163 | ||
8dac456a DH |
1164 | /** |
1165 | * __rt_mutex_slowlock() - Perform the wait-wake-try-to-take loop | |
1166 | * @lock: the rt_mutex to take | |
1167 | * @state: the state the task should block in (TASK_INTERRUPTIBLE | |
b4abf910 | 1168 | * or TASK_UNINTERRUPTIBLE) |
8dac456a DH |
1169 | * @timeout: the pre-initialized and started timer, or NULL for none |
1170 | * @waiter: the pre-initialized rt_mutex_waiter | |
8dac456a | 1171 | * |
b4abf910 | 1172 | * Must be called with lock->wait_lock held and interrupts disabled |
23f78d4a IM |
1173 | */ |
1174 | static int __sched | |
8dac456a DH |
1175 | __rt_mutex_slowlock(struct rt_mutex *lock, int state, |
1176 | struct hrtimer_sleeper *timeout, | |
8161239a | 1177 | struct rt_mutex_waiter *waiter) |
23f78d4a | 1178 | { |
23f78d4a IM |
1179 | int ret = 0; |
1180 | ||
23f78d4a IM |
1181 | for (;;) { |
1182 | /* Try to acquire the lock: */ | |
8161239a | 1183 | if (try_to_take_rt_mutex(lock, current, waiter)) |
23f78d4a IM |
1184 | break; |
1185 | ||
1186 | /* | |
1187 | * TASK_INTERRUPTIBLE checks for signals and | |
1188 | * timeout. Ignored otherwise. | |
1189 | */ | |
4009f4b3 | 1190 | if (likely(state == TASK_INTERRUPTIBLE)) { |
23f78d4a IM |
1191 | /* Signal pending? */ |
1192 | if (signal_pending(current)) | |
1193 | ret = -EINTR; | |
1194 | if (timeout && !timeout->task) | |
1195 | ret = -ETIMEDOUT; | |
1196 | if (ret) | |
1197 | break; | |
1198 | } | |
1199 | ||
b4abf910 | 1200 | raw_spin_unlock_irq(&lock->wait_lock); |
23f78d4a | 1201 | |
8dac456a | 1202 | debug_rt_mutex_print_deadlock(waiter); |
23f78d4a | 1203 | |
1b0b7c17 | 1204 | schedule(); |
23f78d4a | 1205 | |
b4abf910 | 1206 | raw_spin_lock_irq(&lock->wait_lock); |
23f78d4a IM |
1207 | set_current_state(state); |
1208 | } | |
1209 | ||
afffc6c1 | 1210 | __set_current_state(TASK_RUNNING); |
8dac456a DH |
1211 | return ret; |
1212 | } | |
1213 | ||
3d5c9340 TG |
1214 | static void rt_mutex_handle_deadlock(int res, int detect_deadlock, |
1215 | struct rt_mutex_waiter *w) | |
1216 | { | |
1217 | /* | |
1218 | * If the result is not -EDEADLOCK or the caller requested | |
1219 | * deadlock detection, nothing to do here. | |
1220 | */ | |
1221 | if (res != -EDEADLOCK || detect_deadlock) | |
1222 | return; | |
1223 | ||
1224 | /* | |
1225 | * Yell lowdly and stop the task right here. | |
1226 | */ | |
1227 | rt_mutex_print_deadlock(w); | |
1228 | while (1) { | |
1229 | set_current_state(TASK_INTERRUPTIBLE); | |
1230 | schedule(); | |
1231 | } | |
1232 | } | |
1233 | ||
8dac456a DH |
1234 | /* |
1235 | * Slow path lock function: | |
1236 | */ | |
1237 | static int __sched | |
1238 | rt_mutex_slowlock(struct rt_mutex *lock, int state, | |
1239 | struct hrtimer_sleeper *timeout, | |
8930ed80 | 1240 | enum rtmutex_chainwalk chwalk) |
8dac456a DH |
1241 | { |
1242 | struct rt_mutex_waiter waiter; | |
b4abf910 | 1243 | unsigned long flags; |
8dac456a DH |
1244 | int ret = 0; |
1245 | ||
50809358 | 1246 | rt_mutex_init_waiter(&waiter); |
8dac456a | 1247 | |
b4abf910 TG |
1248 | /* |
1249 | * Technically we could use raw_spin_[un]lock_irq() here, but this can | |
1250 | * be called in early boot if the cmpxchg() fast path is disabled | |
1251 | * (debug, no architecture support). In this case we will acquire the | |
1252 | * rtmutex with lock->wait_lock held. But we cannot unconditionally | |
1253 | * enable interrupts in that early boot case. So we need to use the | |
1254 | * irqsave/restore variants. | |
1255 | */ | |
1256 | raw_spin_lock_irqsave(&lock->wait_lock, flags); | |
8dac456a DH |
1257 | |
1258 | /* Try to acquire the lock again: */ | |
8161239a | 1259 | if (try_to_take_rt_mutex(lock, current, NULL)) { |
b4abf910 | 1260 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
8dac456a DH |
1261 | return 0; |
1262 | } | |
1263 | ||
1264 | set_current_state(state); | |
1265 | ||
1266 | /* Setup the timer, when timeout != NULL */ | |
ccdd92c1 | 1267 | if (unlikely(timeout)) |
8dac456a | 1268 | hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS); |
8dac456a | 1269 | |
8930ed80 | 1270 | ret = task_blocks_on_rt_mutex(lock, &waiter, current, chwalk); |
8161239a LJ |
1271 | |
1272 | if (likely(!ret)) | |
afffc6c1 | 1273 | /* sleep on the mutex */ |
8161239a | 1274 | ret = __rt_mutex_slowlock(lock, state, timeout, &waiter); |
8dac456a | 1275 | |
3d5c9340 | 1276 | if (unlikely(ret)) { |
9d3e2d02 | 1277 | __set_current_state(TASK_RUNNING); |
8d1e5a1a SAS |
1278 | if (rt_mutex_has_waiters(lock)) |
1279 | remove_waiter(lock, &waiter); | |
8930ed80 | 1280 | rt_mutex_handle_deadlock(ret, chwalk, &waiter); |
3d5c9340 | 1281 | } |
23f78d4a IM |
1282 | |
1283 | /* | |
1284 | * try_to_take_rt_mutex() sets the waiter bit | |
1285 | * unconditionally. We might have to fix that up. | |
1286 | */ | |
1287 | fixup_rt_mutex_waiters(lock); | |
1288 | ||
b4abf910 | 1289 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
23f78d4a IM |
1290 | |
1291 | /* Remove pending timer: */ | |
1292 | if (unlikely(timeout)) | |
1293 | hrtimer_cancel(&timeout->timer); | |
1294 | ||
23f78d4a IM |
1295 | debug_rt_mutex_free_waiter(&waiter); |
1296 | ||
1297 | return ret; | |
1298 | } | |
1299 | ||
1300 | /* | |
1301 | * Slow path try-lock function: | |
1302 | */ | |
88f2b4c1 | 1303 | static inline int rt_mutex_slowtrylock(struct rt_mutex *lock) |
23f78d4a | 1304 | { |
b4abf910 | 1305 | unsigned long flags; |
88f2b4c1 TG |
1306 | int ret; |
1307 | ||
1308 | /* | |
1309 | * If the lock already has an owner we fail to get the lock. | |
1310 | * This can be done without taking the @lock->wait_lock as | |
1311 | * it is only being read, and this is a trylock anyway. | |
1312 | */ | |
1313 | if (rt_mutex_owner(lock)) | |
1314 | return 0; | |
23f78d4a | 1315 | |
88f2b4c1 | 1316 | /* |
b4abf910 TG |
1317 | * The mutex has currently no owner. Lock the wait lock and try to |
1318 | * acquire the lock. We use irqsave here to support early boot calls. | |
88f2b4c1 | 1319 | */ |
b4abf910 | 1320 | raw_spin_lock_irqsave(&lock->wait_lock, flags); |
23f78d4a | 1321 | |
88f2b4c1 | 1322 | ret = try_to_take_rt_mutex(lock, current, NULL); |
23f78d4a | 1323 | |
88f2b4c1 TG |
1324 | /* |
1325 | * try_to_take_rt_mutex() sets the lock waiters bit | |
1326 | * unconditionally. Clean this up. | |
1327 | */ | |
1328 | fixup_rt_mutex_waiters(lock); | |
23f78d4a | 1329 | |
b4abf910 | 1330 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
23f78d4a IM |
1331 | |
1332 | return ret; | |
1333 | } | |
1334 | ||
1335 | /* | |
802ab58d SAS |
1336 | * Slow path to release a rt-mutex. |
1337 | * Return whether the current task needs to undo a potential priority boosting. | |
23f78d4a | 1338 | */ |
802ab58d SAS |
1339 | static bool __sched rt_mutex_slowunlock(struct rt_mutex *lock, |
1340 | struct wake_q_head *wake_q) | |
23f78d4a | 1341 | { |
b4abf910 TG |
1342 | unsigned long flags; |
1343 | ||
1344 | /* irqsave required to support early boot calls */ | |
1345 | raw_spin_lock_irqsave(&lock->wait_lock, flags); | |
23f78d4a IM |
1346 | |
1347 | debug_rt_mutex_unlock(lock); | |
1348 | ||
27e35715 TG |
1349 | /* |
1350 | * We must be careful here if the fast path is enabled. If we | |
1351 | * have no waiters queued we cannot set owner to NULL here | |
1352 | * because of: | |
1353 | * | |
1354 | * foo->lock->owner = NULL; | |
1355 | * rtmutex_lock(foo->lock); <- fast path | |
1356 | * free = atomic_dec_and_test(foo->refcnt); | |
1357 | * rtmutex_unlock(foo->lock); <- fast path | |
1358 | * if (free) | |
1359 | * kfree(foo); | |
1360 | * raw_spin_unlock(foo->lock->wait_lock); | |
1361 | * | |
1362 | * So for the fastpath enabled kernel: | |
1363 | * | |
1364 | * Nothing can set the waiters bit as long as we hold | |
1365 | * lock->wait_lock. So we do the following sequence: | |
1366 | * | |
1367 | * owner = rt_mutex_owner(lock); | |
1368 | * clear_rt_mutex_waiters(lock); | |
1369 | * raw_spin_unlock(&lock->wait_lock); | |
1370 | * if (cmpxchg(&lock->owner, owner, 0) == owner) | |
1371 | * return; | |
1372 | * goto retry; | |
1373 | * | |
1374 | * The fastpath disabled variant is simple as all access to | |
1375 | * lock->owner is serialized by lock->wait_lock: | |
1376 | * | |
1377 | * lock->owner = NULL; | |
1378 | * raw_spin_unlock(&lock->wait_lock); | |
1379 | */ | |
1380 | while (!rt_mutex_has_waiters(lock)) { | |
1381 | /* Drops lock->wait_lock ! */ | |
b4abf910 | 1382 | if (unlock_rt_mutex_safe(lock, flags) == true) |
802ab58d | 1383 | return false; |
27e35715 | 1384 | /* Relock the rtmutex and try again */ |
b4abf910 | 1385 | raw_spin_lock_irqsave(&lock->wait_lock, flags); |
23f78d4a IM |
1386 | } |
1387 | ||
27e35715 TG |
1388 | /* |
1389 | * The wakeup next waiter path does not suffer from the above | |
1390 | * race. See the comments there. | |
45ab4eff DB |
1391 | * |
1392 | * Queue the next waiter for wakeup once we release the wait_lock. | |
27e35715 | 1393 | */ |
802ab58d | 1394 | mark_wakeup_next_waiter(wake_q, lock); |
23f78d4a | 1395 | |
b4abf910 | 1396 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
23f78d4a | 1397 | |
802ab58d SAS |
1398 | /* check PI boosting */ |
1399 | return true; | |
23f78d4a IM |
1400 | } |
1401 | ||
1402 | /* | |
1403 | * debug aware fast / slowpath lock,trylock,unlock | |
1404 | * | |
1405 | * The atomic acquire/release ops are compiled away, when either the | |
1406 | * architecture does not support cmpxchg or when debugging is enabled. | |
1407 | */ | |
1408 | static inline int | |
1409 | rt_mutex_fastlock(struct rt_mutex *lock, int state, | |
23f78d4a IM |
1410 | int (*slowfn)(struct rt_mutex *lock, int state, |
1411 | struct hrtimer_sleeper *timeout, | |
8930ed80 | 1412 | enum rtmutex_chainwalk chwalk)) |
23f78d4a | 1413 | { |
fffa954f | 1414 | if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) |
23f78d4a | 1415 | return 0; |
fffa954f PZ |
1416 | |
1417 | return slowfn(lock, state, NULL, RT_MUTEX_MIN_CHAINWALK); | |
23f78d4a IM |
1418 | } |
1419 | ||
1420 | static inline int | |
1421 | rt_mutex_timed_fastlock(struct rt_mutex *lock, int state, | |
8930ed80 TG |
1422 | struct hrtimer_sleeper *timeout, |
1423 | enum rtmutex_chainwalk chwalk, | |
23f78d4a IM |
1424 | int (*slowfn)(struct rt_mutex *lock, int state, |
1425 | struct hrtimer_sleeper *timeout, | |
8930ed80 | 1426 | enum rtmutex_chainwalk chwalk)) |
23f78d4a | 1427 | { |
8930ed80 | 1428 | if (chwalk == RT_MUTEX_MIN_CHAINWALK && |
fffa954f | 1429 | likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) |
23f78d4a | 1430 | return 0; |
fffa954f PZ |
1431 | |
1432 | return slowfn(lock, state, timeout, chwalk); | |
23f78d4a IM |
1433 | } |
1434 | ||
1435 | static inline int | |
1436 | rt_mutex_fasttrylock(struct rt_mutex *lock, | |
9a11b49a | 1437 | int (*slowfn)(struct rt_mutex *lock)) |
23f78d4a | 1438 | { |
fffa954f | 1439 | if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) |
23f78d4a | 1440 | return 1; |
fffa954f | 1441 | |
9a11b49a | 1442 | return slowfn(lock); |
23f78d4a IM |
1443 | } |
1444 | ||
1445 | static inline void | |
1446 | rt_mutex_fastunlock(struct rt_mutex *lock, | |
802ab58d SAS |
1447 | bool (*slowfn)(struct rt_mutex *lock, |
1448 | struct wake_q_head *wqh)) | |
23f78d4a | 1449 | { |
194a6b5b | 1450 | DEFINE_WAKE_Q(wake_q); |
fffa954f | 1451 | bool deboost; |
802ab58d | 1452 | |
fffa954f PZ |
1453 | if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) |
1454 | return; | |
802ab58d | 1455 | |
fffa954f | 1456 | deboost = slowfn(lock, &wake_q); |
802ab58d | 1457 | |
fffa954f | 1458 | wake_up_q(&wake_q); |
802ab58d | 1459 | |
fffa954f PZ |
1460 | /* Undo pi boosting if necessary: */ |
1461 | if (deboost) | |
1462 | rt_mutex_adjust_prio(current); | |
23f78d4a IM |
1463 | } |
1464 | ||
1465 | /** | |
1466 | * rt_mutex_lock - lock a rt_mutex | |
1467 | * | |
1468 | * @lock: the rt_mutex to be locked | |
1469 | */ | |
1470 | void __sched rt_mutex_lock(struct rt_mutex *lock) | |
1471 | { | |
1472 | might_sleep(); | |
1473 | ||
c051b21f | 1474 | rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, rt_mutex_slowlock); |
23f78d4a IM |
1475 | } |
1476 | EXPORT_SYMBOL_GPL(rt_mutex_lock); | |
1477 | ||
1478 | /** | |
1479 | * rt_mutex_lock_interruptible - lock a rt_mutex interruptible | |
1480 | * | |
c051b21f | 1481 | * @lock: the rt_mutex to be locked |
23f78d4a IM |
1482 | * |
1483 | * Returns: | |
c051b21f TG |
1484 | * 0 on success |
1485 | * -EINTR when interrupted by a signal | |
23f78d4a | 1486 | */ |
c051b21f | 1487 | int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock) |
23f78d4a IM |
1488 | { |
1489 | might_sleep(); | |
1490 | ||
c051b21f | 1491 | return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, rt_mutex_slowlock); |
23f78d4a IM |
1492 | } |
1493 | EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible); | |
1494 | ||
c051b21f TG |
1495 | /* |
1496 | * Futex variant with full deadlock detection. | |
5293c2ef | 1497 | * Futex variants must not use the fast-path, see __rt_mutex_futex_unlock(). |
c051b21f | 1498 | */ |
5293c2ef | 1499 | int __sched rt_mutex_timed_futex_lock(struct rt_mutex *lock, |
c051b21f TG |
1500 | struct hrtimer_sleeper *timeout) |
1501 | { | |
1502 | might_sleep(); | |
1503 | ||
5293c2ef PZ |
1504 | return rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, |
1505 | timeout, RT_MUTEX_FULL_CHAINWALK); | |
1506 | } | |
1507 | ||
1508 | /* | |
1509 | * Futex variant, must not use fastpath. | |
1510 | */ | |
1511 | int __sched rt_mutex_futex_trylock(struct rt_mutex *lock) | |
1512 | { | |
1513 | return rt_mutex_slowtrylock(lock); | |
c051b21f TG |
1514 | } |
1515 | ||
23f78d4a | 1516 | /** |
23b94b96 LH |
1517 | * rt_mutex_timed_lock - lock a rt_mutex interruptible |
1518 | * the timeout structure is provided | |
1519 | * by the caller | |
23f78d4a | 1520 | * |
c051b21f | 1521 | * @lock: the rt_mutex to be locked |
23f78d4a | 1522 | * @timeout: timeout structure or NULL (no timeout) |
23f78d4a IM |
1523 | * |
1524 | * Returns: | |
c051b21f TG |
1525 | * 0 on success |
1526 | * -EINTR when interrupted by a signal | |
3ac49a1c | 1527 | * -ETIMEDOUT when the timeout expired |
23f78d4a IM |
1528 | */ |
1529 | int | |
c051b21f | 1530 | rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout) |
23f78d4a IM |
1531 | { |
1532 | might_sleep(); | |
1533 | ||
8930ed80 TG |
1534 | return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout, |
1535 | RT_MUTEX_MIN_CHAINWALK, | |
c051b21f | 1536 | rt_mutex_slowlock); |
23f78d4a IM |
1537 | } |
1538 | EXPORT_SYMBOL_GPL(rt_mutex_timed_lock); | |
1539 | ||
1540 | /** | |
1541 | * rt_mutex_trylock - try to lock a rt_mutex | |
1542 | * | |
1543 | * @lock: the rt_mutex to be locked | |
1544 | * | |
6ce47fd9 TG |
1545 | * This function can only be called in thread context. It's safe to |
1546 | * call it from atomic regions, but not from hard interrupt or soft | |
1547 | * interrupt context. | |
1548 | * | |
23f78d4a IM |
1549 | * Returns 1 on success and 0 on contention |
1550 | */ | |
1551 | int __sched rt_mutex_trylock(struct rt_mutex *lock) | |
1552 | { | |
a461d587 | 1553 | if (WARN_ON_ONCE(in_irq() || in_nmi() || in_serving_softirq())) |
6ce47fd9 TG |
1554 | return 0; |
1555 | ||
23f78d4a IM |
1556 | return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock); |
1557 | } | |
1558 | EXPORT_SYMBOL_GPL(rt_mutex_trylock); | |
1559 | ||
1560 | /** | |
1561 | * rt_mutex_unlock - unlock a rt_mutex | |
1562 | * | |
1563 | * @lock: the rt_mutex to be unlocked | |
1564 | */ | |
1565 | void __sched rt_mutex_unlock(struct rt_mutex *lock) | |
1566 | { | |
1567 | rt_mutex_fastunlock(lock, rt_mutex_slowunlock); | |
1568 | } | |
1569 | EXPORT_SYMBOL_GPL(rt_mutex_unlock); | |
1570 | ||
802ab58d | 1571 | /** |
5293c2ef PZ |
1572 | * Futex variant, that since futex variants do not use the fast-path, can be |
1573 | * simple and will not need to retry. | |
802ab58d | 1574 | */ |
5293c2ef PZ |
1575 | bool __sched __rt_mutex_futex_unlock(struct rt_mutex *lock, |
1576 | struct wake_q_head *wake_q) | |
802ab58d | 1577 | { |
5293c2ef PZ |
1578 | lockdep_assert_held(&lock->wait_lock); |
1579 | ||
1580 | debug_rt_mutex_unlock(lock); | |
1581 | ||
1582 | if (!rt_mutex_has_waiters(lock)) { | |
1583 | lock->owner = NULL; | |
1584 | return false; /* done */ | |
1585 | } | |
1586 | ||
1587 | mark_wakeup_next_waiter(wake_q, lock); | |
1588 | return true; /* deboost and wakeups */ | |
1589 | } | |
fffa954f | 1590 | |
5293c2ef PZ |
1591 | void __sched rt_mutex_futex_unlock(struct rt_mutex *lock) |
1592 | { | |
1593 | DEFINE_WAKE_Q(wake_q); | |
1594 | bool deboost; | |
1595 | ||
1596 | raw_spin_lock_irq(&lock->wait_lock); | |
1597 | deboost = __rt_mutex_futex_unlock(lock, &wake_q); | |
1598 | raw_spin_unlock_irq(&lock->wait_lock); | |
1599 | ||
1600 | if (deboost) { | |
1601 | wake_up_q(&wake_q); | |
1602 | rt_mutex_adjust_prio(current); | |
1603 | } | |
802ab58d SAS |
1604 | } |
1605 | ||
23b94b96 | 1606 | /** |
23f78d4a IM |
1607 | * rt_mutex_destroy - mark a mutex unusable |
1608 | * @lock: the mutex to be destroyed | |
1609 | * | |
1610 | * This function marks the mutex uninitialized, and any subsequent | |
1611 | * use of the mutex is forbidden. The mutex must not be locked when | |
1612 | * this function is called. | |
1613 | */ | |
1614 | void rt_mutex_destroy(struct rt_mutex *lock) | |
1615 | { | |
1616 | WARN_ON(rt_mutex_is_locked(lock)); | |
1617 | #ifdef CONFIG_DEBUG_RT_MUTEXES | |
1618 | lock->magic = NULL; | |
1619 | #endif | |
1620 | } | |
1621 | ||
1622 | EXPORT_SYMBOL_GPL(rt_mutex_destroy); | |
1623 | ||
1624 | /** | |
1625 | * __rt_mutex_init - initialize the rt lock | |
1626 | * | |
1627 | * @lock: the rt lock to be initialized | |
1628 | * | |
1629 | * Initialize the rt lock to unlocked state. | |
1630 | * | |
1631 | * Initializing of a locked rt lock is not allowed | |
1632 | */ | |
1633 | void __rt_mutex_init(struct rt_mutex *lock, const char *name) | |
1634 | { | |
1635 | lock->owner = NULL; | |
d209d74d | 1636 | raw_spin_lock_init(&lock->wait_lock); |
fb00aca4 PZ |
1637 | lock->waiters = RB_ROOT; |
1638 | lock->waiters_leftmost = NULL; | |
23f78d4a IM |
1639 | |
1640 | debug_rt_mutex_init(lock, name); | |
1641 | } | |
1642 | EXPORT_SYMBOL_GPL(__rt_mutex_init); | |
0cdbee99 IM |
1643 | |
1644 | /** | |
1645 | * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a | |
1646 | * proxy owner | |
1647 | * | |
84d82ec5 | 1648 | * @lock: the rt_mutex to be locked |
0cdbee99 IM |
1649 | * @proxy_owner:the task to set as owner |
1650 | * | |
1651 | * No locking. Caller has to do serializing itself | |
84d82ec5 TG |
1652 | * |
1653 | * Special API call for PI-futex support. This initializes the rtmutex and | |
1654 | * assigns it to @proxy_owner. Concurrent operations on the rtmutex are not | |
1655 | * possible at this point because the pi_state which contains the rtmutex | |
1656 | * is not yet visible to other tasks. | |
0cdbee99 IM |
1657 | */ |
1658 | void rt_mutex_init_proxy_locked(struct rt_mutex *lock, | |
1659 | struct task_struct *proxy_owner) | |
1660 | { | |
1661 | __rt_mutex_init(lock, NULL); | |
9a11b49a | 1662 | debug_rt_mutex_proxy_lock(lock, proxy_owner); |
8161239a | 1663 | rt_mutex_set_owner(lock, proxy_owner); |
0cdbee99 IM |
1664 | } |
1665 | ||
1666 | /** | |
1667 | * rt_mutex_proxy_unlock - release a lock on behalf of owner | |
1668 | * | |
84d82ec5 | 1669 | * @lock: the rt_mutex to be locked |
0cdbee99 IM |
1670 | * |
1671 | * No locking. Caller has to do serializing itself | |
84d82ec5 TG |
1672 | * |
1673 | * Special API call for PI-futex support. This merrily cleans up the rtmutex | |
1674 | * (debugging) state. Concurrent operations on this rt_mutex are not | |
1675 | * possible because it belongs to the pi_state which is about to be freed | |
1676 | * and it is not longer visible to other tasks. | |
0cdbee99 IM |
1677 | */ |
1678 | void rt_mutex_proxy_unlock(struct rt_mutex *lock, | |
1679 | struct task_struct *proxy_owner) | |
1680 | { | |
1681 | debug_rt_mutex_proxy_unlock(lock); | |
8161239a | 1682 | rt_mutex_set_owner(lock, NULL); |
0cdbee99 IM |
1683 | } |
1684 | ||
8dac456a DH |
1685 | /** |
1686 | * rt_mutex_start_proxy_lock() - Start lock acquisition for another task | |
1687 | * @lock: the rt_mutex to take | |
1688 | * @waiter: the pre-initialized rt_mutex_waiter | |
1689 | * @task: the task to prepare | |
8dac456a DH |
1690 | * |
1691 | * Returns: | |
1692 | * 0 - task blocked on lock | |
1693 | * 1 - acquired the lock for task, caller should wake it up | |
1694 | * <0 - error | |
1695 | * | |
1696 | * Special API call for FUTEX_REQUEUE_PI support. | |
1697 | */ | |
1698 | int rt_mutex_start_proxy_lock(struct rt_mutex *lock, | |
1699 | struct rt_mutex_waiter *waiter, | |
c051b21f | 1700 | struct task_struct *task) |
8dac456a DH |
1701 | { |
1702 | int ret; | |
1703 | ||
b4abf910 | 1704 | raw_spin_lock_irq(&lock->wait_lock); |
8dac456a | 1705 | |
8161239a | 1706 | if (try_to_take_rt_mutex(lock, task, NULL)) { |
b4abf910 | 1707 | raw_spin_unlock_irq(&lock->wait_lock); |
8dac456a DH |
1708 | return 1; |
1709 | } | |
1710 | ||
3d5c9340 | 1711 | /* We enforce deadlock detection for futexes */ |
8930ed80 TG |
1712 | ret = task_blocks_on_rt_mutex(lock, waiter, task, |
1713 | RT_MUTEX_FULL_CHAINWALK); | |
8dac456a | 1714 | |
8161239a | 1715 | if (ret && !rt_mutex_owner(lock)) { |
8dac456a DH |
1716 | /* |
1717 | * Reset the return value. We might have | |
1718 | * returned with -EDEADLK and the owner | |
1719 | * released the lock while we were walking the | |
1720 | * pi chain. Let the waiter sort it out. | |
1721 | */ | |
1722 | ret = 0; | |
1723 | } | |
8161239a LJ |
1724 | |
1725 | if (unlikely(ret)) | |
1726 | remove_waiter(lock, waiter); | |
1727 | ||
b4abf910 | 1728 | raw_spin_unlock_irq(&lock->wait_lock); |
8dac456a DH |
1729 | |
1730 | debug_rt_mutex_print_deadlock(waiter); | |
1731 | ||
1732 | return ret; | |
1733 | } | |
1734 | ||
0cdbee99 IM |
1735 | /** |
1736 | * rt_mutex_next_owner - return the next owner of the lock | |
1737 | * | |
1738 | * @lock: the rt lock query | |
1739 | * | |
1740 | * Returns the next owner of the lock or NULL | |
1741 | * | |
1742 | * Caller has to serialize against other accessors to the lock | |
1743 | * itself. | |
1744 | * | |
1745 | * Special API call for PI-futex support | |
1746 | */ | |
1747 | struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock) | |
1748 | { | |
1749 | if (!rt_mutex_has_waiters(lock)) | |
1750 | return NULL; | |
1751 | ||
1752 | return rt_mutex_top_waiter(lock)->task; | |
1753 | } | |
8dac456a DH |
1754 | |
1755 | /** | |
38d589f2 | 1756 | * rt_mutex_wait_proxy_lock() - Wait for lock acquisition |
8dac456a DH |
1757 | * @lock: the rt_mutex we were woken on |
1758 | * @to: the timeout, null if none. hrtimer should already have | |
c051b21f | 1759 | * been started. |
8dac456a | 1760 | * @waiter: the pre-initialized rt_mutex_waiter |
8dac456a | 1761 | * |
38d589f2 PZ |
1762 | * Wait for the the lock acquisition started on our behalf by |
1763 | * rt_mutex_start_proxy_lock(). Upon failure, the caller must call | |
1764 | * rt_mutex_cleanup_proxy_lock(). | |
8dac456a DH |
1765 | * |
1766 | * Returns: | |
1767 | * 0 - success | |
c051b21f | 1768 | * <0 - error, one of -EINTR, -ETIMEDOUT |
8dac456a | 1769 | * |
38d589f2 | 1770 | * Special API call for PI-futex support |
8dac456a | 1771 | */ |
38d589f2 | 1772 | int rt_mutex_wait_proxy_lock(struct rt_mutex *lock, |
8dac456a | 1773 | struct hrtimer_sleeper *to, |
c051b21f | 1774 | struct rt_mutex_waiter *waiter) |
8dac456a DH |
1775 | { |
1776 | int ret; | |
1777 | ||
b4abf910 | 1778 | raw_spin_lock_irq(&lock->wait_lock); |
8dac456a DH |
1779 | |
1780 | set_current_state(TASK_INTERRUPTIBLE); | |
1781 | ||
afffc6c1 | 1782 | /* sleep on the mutex */ |
8161239a | 1783 | ret = __rt_mutex_slowlock(lock, TASK_INTERRUPTIBLE, to, waiter); |
8dac456a | 1784 | |
8dac456a DH |
1785 | /* |
1786 | * try_to_take_rt_mutex() sets the waiter bit unconditionally. We might | |
1787 | * have to fix that up. | |
1788 | */ | |
1789 | fixup_rt_mutex_waiters(lock); | |
1790 | ||
b4abf910 | 1791 | raw_spin_unlock_irq(&lock->wait_lock); |
8dac456a | 1792 | |
8dac456a DH |
1793 | return ret; |
1794 | } | |
38d589f2 PZ |
1795 | |
1796 | /** | |
1797 | * rt_mutex_cleanup_proxy_lock() - Cleanup failed lock acquisition | |
1798 | * @lock: the rt_mutex we were woken on | |
1799 | * @waiter: the pre-initialized rt_mutex_waiter | |
1800 | * | |
1801 | * Attempt to clean up after a failed rt_mutex_wait_proxy_lock(). | |
1802 | * | |
1803 | * Unless we acquired the lock; we're still enqueued on the wait-list and can | |
1804 | * in fact still be granted ownership until we're removed. Therefore we can | |
1805 | * find we are in fact the owner and must disregard the | |
1806 | * rt_mutex_wait_proxy_lock() failure. | |
1807 | * | |
1808 | * Returns: | |
1809 | * true - did the cleanup, we done. | |
1810 | * false - we acquired the lock after rt_mutex_wait_proxy_lock() returned, | |
1811 | * caller should disregards its return value. | |
1812 | * | |
1813 | * Special API call for PI-futex support | |
1814 | */ | |
1815 | bool rt_mutex_cleanup_proxy_lock(struct rt_mutex *lock, | |
1816 | struct rt_mutex_waiter *waiter) | |
1817 | { | |
1818 | bool cleanup = false; | |
1819 | ||
1820 | raw_spin_lock_irq(&lock->wait_lock); | |
1821 | /* | |
1822 | * Unless we're the owner; we're still enqueued on the wait_list. | |
1823 | * So check if we became owner, if not, take us off the wait_list. | |
1824 | */ | |
1825 | if (rt_mutex_owner(lock) != current) { | |
1826 | remove_waiter(lock, waiter); | |
1827 | fixup_rt_mutex_waiters(lock); | |
1828 | cleanup = true; | |
1829 | } | |
1830 | raw_spin_unlock_irq(&lock->wait_lock); | |
1831 | ||
1832 | return cleanup; | |
1833 | } |