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