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