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