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