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