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