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 | */ |
Thomas Gleixner | 531ae4b | 2021-08-15 23:27:57 +0200 | [diff] [blame] | 14 | #include <linux/sched.h> |
| 15 | #include <linux/sched/debug.h> |
| 16 | #include <linux/sched/deadline.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 17 | #include <linux/sched/signal.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 18 | #include <linux/sched/rt.h> |
Ingo Molnar | 84f001e | 2017-02-01 16:36:40 +0100 | [diff] [blame] | 19 | #include <linux/sched/wake_q.h> |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 20 | |
| 21 | #include "rtmutex_common.h" |
| 22 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 23 | /* |
| 24 | * lock->owner state tracking: |
| 25 | * |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 26 | * lock->owner holds the task_struct pointer of the owner. Bit 0 |
| 27 | * is used to keep track of the "lock has waiters" state. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 28 | * |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 29 | * owner bit0 |
| 30 | * NULL 0 lock is free (fast acquire possible) |
| 31 | * NULL 1 lock is free and has waiters and the top waiter |
| 32 | * is going to take the lock* |
| 33 | * taskpointer 0 lock is held (fast release possible) |
| 34 | * taskpointer 1 lock is held and has waiters** |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 35 | * |
| 36 | * The fast atomic compare exchange based acquire and release is only |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 37 | * possible when bit 0 of lock->owner is 0. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 38 | * |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 39 | * (*) It also can be a transitional state when grabbing the lock |
| 40 | * with ->wait_lock is held. To prevent any fast path cmpxchg to the lock, |
| 41 | * we need to set the bit0 before looking at the lock, and the owner may be |
| 42 | * NULL in this small time, hence this can be a transitional state. |
| 43 | * |
| 44 | * (**) There is a small time when bit 0 is set but there are no |
| 45 | * waiters. This can happen when grabbing the lock in the slow path. |
| 46 | * To prevent a cmpxchg of the owner releasing the lock, we need to |
| 47 | * set this bit before looking at the lock. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 48 | */ |
| 49 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 50 | static __always_inline void |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 51 | rt_mutex_set_owner(struct rt_mutex_base *lock, struct task_struct *owner) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 52 | { |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 53 | unsigned long val = (unsigned long)owner; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 54 | |
| 55 | if (rt_mutex_has_waiters(lock)) |
| 56 | val |= RT_MUTEX_HAS_WAITERS; |
| 57 | |
Paul E. McKenney | 0050c7b | 2020-01-03 15:59:12 -0800 | [diff] [blame] | 58 | WRITE_ONCE(lock->owner, (struct task_struct *)val); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 61 | static __always_inline void clear_rt_mutex_waiters(struct rt_mutex_base *lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 62 | { |
| 63 | lock->owner = (struct task_struct *) |
| 64 | ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS); |
| 65 | } |
| 66 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 67 | static __always_inline void fixup_rt_mutex_waiters(struct rt_mutex_base *lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 68 | { |
Thomas Gleixner | dbb2605 | 2016-11-30 21:04:41 +0000 | [diff] [blame] | 69 | unsigned long owner, *p = (unsigned long *) &lock->owner; |
| 70 | |
| 71 | if (rt_mutex_has_waiters(lock)) |
| 72 | return; |
| 73 | |
| 74 | /* |
| 75 | * The rbtree has no waiters enqueued, now make sure that the |
| 76 | * lock->owner still has the waiters bit set, otherwise the |
| 77 | * following can happen: |
| 78 | * |
| 79 | * CPU 0 CPU 1 CPU2 |
| 80 | * l->owner=T1 |
| 81 | * rt_mutex_lock(l) |
| 82 | * lock(l->lock) |
| 83 | * l->owner = T1 | HAS_WAITERS; |
| 84 | * enqueue(T2) |
| 85 | * boost() |
| 86 | * unlock(l->lock) |
| 87 | * block() |
| 88 | * |
| 89 | * rt_mutex_lock(l) |
| 90 | * lock(l->lock) |
| 91 | * l->owner = T1 | HAS_WAITERS; |
| 92 | * enqueue(T3) |
| 93 | * boost() |
| 94 | * unlock(l->lock) |
| 95 | * block() |
| 96 | * signal(->T2) signal(->T3) |
| 97 | * lock(l->lock) |
| 98 | * dequeue(T2) |
| 99 | * deboost() |
| 100 | * unlock(l->lock) |
| 101 | * lock(l->lock) |
| 102 | * dequeue(T3) |
| 103 | * ==> wait list is empty |
| 104 | * deboost() |
| 105 | * unlock(l->lock) |
| 106 | * lock(l->lock) |
| 107 | * fixup_rt_mutex_waiters() |
| 108 | * if (wait_list_empty(l) { |
| 109 | * l->owner = owner |
| 110 | * owner = l->owner & ~HAS_WAITERS; |
| 111 | * ==> l->owner = T1 |
| 112 | * } |
| 113 | * lock(l->lock) |
| 114 | * rt_mutex_unlock(l) fixup_rt_mutex_waiters() |
| 115 | * if (wait_list_empty(l) { |
| 116 | * owner = l->owner & ~HAS_WAITERS; |
| 117 | * cmpxchg(l->owner, T1, NULL) |
| 118 | * ===> Success (l->owner = NULL) |
| 119 | * |
| 120 | * l->owner = owner |
| 121 | * ==> l->owner = T1 |
| 122 | * } |
| 123 | * |
| 124 | * With the check for the waiter bit in place T3 on CPU2 will not |
| 125 | * overwrite. All tasks fiddling with the waiters bit are |
| 126 | * serialized by l->lock, so nothing else can modify the waiters |
| 127 | * bit. If the bit is set then nothing can change l->owner either |
| 128 | * so the simple RMW is safe. The cmpxchg() will simply fail if it |
| 129 | * happens in the middle of the RMW because the waiters bit is |
| 130 | * still set. |
| 131 | */ |
| 132 | owner = READ_ONCE(*p); |
| 133 | if (owner & RT_MUTEX_HAS_WAITERS) |
| 134 | WRITE_ONCE(*p, owner & ~RT_MUTEX_HAS_WAITERS); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* |
Sebastian Andrzej Siewior | cede884 | 2015-02-25 18:56:13 +0100 | [diff] [blame] | 138 | * We can speed up the acquire/release, if there's no debugging state to be |
| 139 | * set up. |
Thomas Gleixner | bd19723 | 2007-06-17 21:11:10 +0200 | [diff] [blame] | 140 | */ |
Sebastian Andrzej Siewior | cede884 | 2015-02-25 18:56:13 +0100 | [diff] [blame] | 141 | #ifndef CONFIG_DEBUG_RT_MUTEXES |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 142 | static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock, |
Sebastian Andrzej Siewior | 7851593 | 2021-08-15 23:27:54 +0200 | [diff] [blame] | 143 | struct task_struct *old, |
| 144 | struct task_struct *new) |
| 145 | { |
Thomas Gleixner | 709e0b6 | 2021-08-15 23:27:55 +0200 | [diff] [blame] | 146 | return try_cmpxchg_acquire(&lock->owner, &old, new); |
Sebastian Andrzej Siewior | 7851593 | 2021-08-15 23:27:54 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 149 | static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock, |
Sebastian Andrzej Siewior | 7851593 | 2021-08-15 23:27:54 +0200 | [diff] [blame] | 150 | struct task_struct *old, |
| 151 | struct task_struct *new) |
| 152 | { |
Thomas Gleixner | 709e0b6 | 2021-08-15 23:27:55 +0200 | [diff] [blame] | 153 | return try_cmpxchg_release(&lock->owner, &old, new); |
Sebastian Andrzej Siewior | 7851593 | 2021-08-15 23:27:54 +0200 | [diff] [blame] | 154 | } |
Davidlohr Bueso | 700318d | 2015-09-30 13:03:13 -0700 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * Callers must hold the ->wait_lock -- which is the whole purpose as we force |
| 158 | * all future threads that attempt to [Rmw] the lock to the slowpath. As such |
| 159 | * relaxed semantics suffice. |
| 160 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 161 | static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock) |
Thomas Gleixner | bd19723 | 2007-06-17 21:11:10 +0200 | [diff] [blame] | 162 | { |
| 163 | unsigned long owner, *p = (unsigned long *) &lock->owner; |
| 164 | |
| 165 | do { |
| 166 | owner = *p; |
Davidlohr Bueso | 700318d | 2015-09-30 13:03:13 -0700 | [diff] [blame] | 167 | } while (cmpxchg_relaxed(p, owner, |
| 168 | owner | RT_MUTEX_HAS_WAITERS) != owner); |
Thomas Gleixner | bd19723 | 2007-06-17 21:11:10 +0200 | [diff] [blame] | 169 | } |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Safe fastpath aware unlock: |
| 173 | * 1) Clear the waiters bit |
| 174 | * 2) Drop lock->wait_lock |
| 175 | * 3) Try to unlock the lock with cmpxchg |
| 176 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 177 | static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock, |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 178 | unsigned long flags) |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 179 | __releases(lock->wait_lock) |
| 180 | { |
| 181 | struct task_struct *owner = rt_mutex_owner(lock); |
| 182 | |
| 183 | clear_rt_mutex_waiters(lock); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 184 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 185 | /* |
| 186 | * If a new waiter comes in between the unlock and the cmpxchg |
| 187 | * we have two situations: |
| 188 | * |
| 189 | * unlock(wait_lock); |
| 190 | * lock(wait_lock); |
| 191 | * cmpxchg(p, owner, 0) == owner |
| 192 | * mark_rt_mutex_waiters(lock); |
| 193 | * acquire(lock); |
| 194 | * or: |
| 195 | * |
| 196 | * unlock(wait_lock); |
| 197 | * lock(wait_lock); |
| 198 | * mark_rt_mutex_waiters(lock); |
| 199 | * |
| 200 | * cmpxchg(p, owner, 0) != owner |
| 201 | * enqueue_waiter(); |
| 202 | * unlock(wait_lock); |
| 203 | * lock(wait_lock); |
| 204 | * wake waiter(); |
| 205 | * unlock(wait_lock); |
| 206 | * lock(wait_lock); |
| 207 | * acquire(lock); |
| 208 | */ |
Davidlohr Bueso | 700318d | 2015-09-30 13:03:13 -0700 | [diff] [blame] | 209 | return rt_mutex_cmpxchg_release(lock, owner, NULL); |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Thomas Gleixner | bd19723 | 2007-06-17 21:11:10 +0200 | [diff] [blame] | 212 | #else |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 213 | static __always_inline bool rt_mutex_cmpxchg_acquire(struct rt_mutex_base *lock, |
Sebastian Andrzej Siewior | 7851593 | 2021-08-15 23:27:54 +0200 | [diff] [blame] | 214 | struct task_struct *old, |
| 215 | struct task_struct *new) |
| 216 | { |
| 217 | return false; |
| 218 | |
| 219 | } |
| 220 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 221 | static __always_inline bool rt_mutex_cmpxchg_release(struct rt_mutex_base *lock, |
Sebastian Andrzej Siewior | 7851593 | 2021-08-15 23:27:54 +0200 | [diff] [blame] | 222 | struct task_struct *old, |
| 223 | struct task_struct *new) |
| 224 | { |
| 225 | return false; |
| 226 | } |
Davidlohr Bueso | 700318d | 2015-09-30 13:03:13 -0700 | [diff] [blame] | 227 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 228 | static __always_inline void mark_rt_mutex_waiters(struct rt_mutex_base *lock) |
Thomas Gleixner | bd19723 | 2007-06-17 21:11:10 +0200 | [diff] [blame] | 229 | { |
| 230 | lock->owner = (struct task_struct *) |
| 231 | ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS); |
| 232 | } |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * Simple slow path only version: lock->owner is protected by lock->wait_lock. |
| 236 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 237 | static __always_inline bool unlock_rt_mutex_safe(struct rt_mutex_base *lock, |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 238 | unsigned long flags) |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 239 | __releases(lock->wait_lock) |
| 240 | { |
| 241 | lock->owner = NULL; |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 242 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
Thomas Gleixner | bd19723 | 2007-06-17 21:11:10 +0200 | [diff] [blame] | 245 | #endif |
| 246 | |
Peter Zijlstra | 19830e5 | 2017-03-23 15:56:14 +0100 | [diff] [blame] | 247 | /* |
| 248 | * Only use with rt_mutex_waiter_{less,equal}() |
| 249 | */ |
| 250 | #define task_to_waiter(p) \ |
| 251 | &(struct rt_mutex_waiter){ .prio = (p)->prio, .deadline = (p)->dl.deadline } |
| 252 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 253 | static __always_inline int rt_mutex_waiter_less(struct rt_mutex_waiter *left, |
| 254 | struct rt_mutex_waiter *right) |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 255 | { |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 256 | if (left->prio < right->prio) |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 257 | return 1; |
| 258 | |
| 259 | /* |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 260 | * If both waiters have dl_prio(), we check the deadlines of the |
| 261 | * associated tasks. |
| 262 | * If left waiter has a dl_prio(), and we didn't return 1 above, |
| 263 | * then right waiter has a dl_prio() too. |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 264 | */ |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 265 | if (dl_prio(left->prio)) |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 266 | return dl_time_before(left->deadline, right->deadline); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 271 | static __always_inline int rt_mutex_waiter_equal(struct rt_mutex_waiter *left, |
| 272 | struct rt_mutex_waiter *right) |
Peter Zijlstra | 19830e5 | 2017-03-23 15:56:14 +0100 | [diff] [blame] | 273 | { |
| 274 | if (left->prio != right->prio) |
| 275 | return 0; |
| 276 | |
| 277 | /* |
| 278 | * If both waiters have dl_prio(), we check the deadlines of the |
| 279 | * associated tasks. |
| 280 | * If left waiter has a dl_prio(), and we didn't return 0 above, |
| 281 | * then right waiter has a dl_prio() too. |
| 282 | */ |
| 283 | if (dl_prio(left->prio)) |
| 284 | return left->deadline == right->deadline; |
| 285 | |
| 286 | return 1; |
| 287 | } |
| 288 | |
Peter Zijlstra | 5a79872 | 2020-04-29 17:29:58 +0200 | [diff] [blame] | 289 | #define __node_2_waiter(node) \ |
| 290 | rb_entry((node), struct rt_mutex_waiter, tree_entry) |
| 291 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 292 | static __always_inline bool __waiter_less(struct rb_node *a, const struct rb_node *b) |
Peter Zijlstra | 5a79872 | 2020-04-29 17:29:58 +0200 | [diff] [blame] | 293 | { |
| 294 | return rt_mutex_waiter_less(__node_2_waiter(a), __node_2_waiter(b)); |
| 295 | } |
| 296 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 297 | static __always_inline void |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 298 | rt_mutex_enqueue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter) |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 299 | { |
Peter Zijlstra | 5a79872 | 2020-04-29 17:29:58 +0200 | [diff] [blame] | 300 | rb_add_cached(&waiter->tree_entry, &lock->waiters, __waiter_less); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 303 | static __always_inline void |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 304 | rt_mutex_dequeue(struct rt_mutex_base *lock, struct rt_mutex_waiter *waiter) |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 305 | { |
| 306 | if (RB_EMPTY_NODE(&waiter->tree_entry)) |
| 307 | return; |
| 308 | |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 309 | rb_erase_cached(&waiter->tree_entry, &lock->waiters); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 310 | RB_CLEAR_NODE(&waiter->tree_entry); |
| 311 | } |
| 312 | |
Peter Zijlstra | 5a79872 | 2020-04-29 17:29:58 +0200 | [diff] [blame] | 313 | #define __node_2_pi_waiter(node) \ |
| 314 | rb_entry((node), struct rt_mutex_waiter, pi_tree_entry) |
| 315 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 316 | static __always_inline bool |
| 317 | __pi_waiter_less(struct rb_node *a, const struct rb_node *b) |
Peter Zijlstra | 5a79872 | 2020-04-29 17:29:58 +0200 | [diff] [blame] | 318 | { |
| 319 | return rt_mutex_waiter_less(__node_2_pi_waiter(a), __node_2_pi_waiter(b)); |
| 320 | } |
| 321 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 322 | static __always_inline void |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 323 | rt_mutex_enqueue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter) |
| 324 | { |
Peter Zijlstra | 5a79872 | 2020-04-29 17:29:58 +0200 | [diff] [blame] | 325 | 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] | 326 | } |
| 327 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 328 | static __always_inline void |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 329 | rt_mutex_dequeue_pi(struct task_struct *task, struct rt_mutex_waiter *waiter) |
| 330 | { |
| 331 | if (RB_EMPTY_NODE(&waiter->pi_tree_entry)) |
| 332 | return; |
| 333 | |
Davidlohr Bueso | a23ba90 | 2017-09-08 16:15:01 -0700 | [diff] [blame] | 334 | rb_erase_cached(&waiter->pi_tree_entry, &task->pi_waiters); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 335 | RB_CLEAR_NODE(&waiter->pi_tree_entry); |
| 336 | } |
| 337 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 338 | static __always_inline void rt_mutex_adjust_prio(struct task_struct *p) |
Xunlei Pang | e96a7705 | 2017-03-23 15:56:08 +0100 | [diff] [blame] | 339 | { |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 340 | struct task_struct *pi_task = NULL; |
Xunlei Pang | e96a7705 | 2017-03-23 15:56:08 +0100 | [diff] [blame] | 341 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 342 | lockdep_assert_held(&p->pi_lock); |
Xunlei Pang | e96a7705 | 2017-03-23 15:56:08 +0100 | [diff] [blame] | 343 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 344 | if (task_has_pi_waiters(p)) |
| 345 | pi_task = task_top_pi_waiter(p)->task; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 346 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 347 | rt_mutex_setprio(p, pi_task); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Thomas Gleixner | b576e64 | 2021-08-15 23:28:08 +0200 | [diff] [blame] | 350 | /* RT mutex specific wake_q wrappers */ |
| 351 | static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh, |
| 352 | struct rt_mutex_waiter *w) |
| 353 | { |
| 354 | wake_q_add(&wqh->head, w->task); |
| 355 | } |
| 356 | |
| 357 | static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh) |
| 358 | { |
| 359 | wake_up_q(&wqh->head); |
| 360 | |
| 361 | /* Pairs with preempt_disable() in mark_wakeup_next_waiter() */ |
| 362 | preempt_enable(); |
| 363 | } |
| 364 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 365 | /* |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 366 | * Deadlock detection is conditional: |
| 367 | * |
| 368 | * If CONFIG_DEBUG_RT_MUTEXES=n, deadlock detection is only conducted |
| 369 | * if the detect argument is == RT_MUTEX_FULL_CHAINWALK. |
| 370 | * |
| 371 | * If CONFIG_DEBUG_RT_MUTEXES=y, deadlock detection is always |
| 372 | * conducted independent of the detect argument. |
| 373 | * |
| 374 | * If the waiter argument is NULL this indicates the deboost path and |
| 375 | * deadlock detection is disabled independent of the detect argument |
| 376 | * and the config settings. |
| 377 | */ |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 378 | static __always_inline bool |
| 379 | rt_mutex_cond_detect_deadlock(struct rt_mutex_waiter *waiter, |
| 380 | enum rtmutex_chainwalk chwalk) |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 381 | { |
Zhen Lei | 07d2597 | 2021-07-31 20:30:11 +0800 | [diff] [blame] | 382 | if (IS_ENABLED(CONFIG_DEBUG_RT_MUTEXES)) |
Thomas Gleixner | f7efc47 | 2021-03-26 16:29:36 +0100 | [diff] [blame] | 383 | return waiter != NULL; |
| 384 | return chwalk == RT_MUTEX_FULL_CHAINWALK; |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 387 | static __always_inline struct rt_mutex_base *task_blocked_on_lock(struct task_struct *p) |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 388 | { |
| 389 | return p->pi_blocked_on ? p->pi_blocked_on->lock : NULL; |
| 390 | } |
| 391 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 392 | /* |
| 393 | * Adjust the priority chain. Also used for deadlock detection. |
| 394 | * Decreases task's usage by one - may thus free the task. |
Juri Lelli | 0c10617 | 2013-05-15 11:04:10 +0200 | [diff] [blame] | 395 | * |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 396 | * @task: the task owning the mutex (owner) for which a chain walk is |
| 397 | * probably needed |
Tom(JeHyeon) Yeon | e6beaa36 | 2015-03-18 14:03:30 +0900 | [diff] [blame] | 398 | * @chwalk: do we have to carry out deadlock detection? |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 399 | * @orig_lock: the mutex (can be NULL if we are walking the chain to recheck |
| 400 | * things for a task that has just got its priority adjusted, and |
| 401 | * is waiting on a mutex) |
| 402 | * @next_lock: the mutex on which the owner of @orig_lock was blocked before |
| 403 | * we dropped its pi_lock. Is never dereferenced, only used for |
| 404 | * comparison to detect lock chain changes. |
Juri Lelli | 0c10617 | 2013-05-15 11:04:10 +0200 | [diff] [blame] | 405 | * @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] | 406 | * its priority to the mutex owner (can be NULL in the case |
| 407 | * depicted above or if the top waiter is gone away and we are |
| 408 | * actually deboosting the owner) |
| 409 | * @top_task: the current top waiter |
Juri Lelli | 0c10617 | 2013-05-15 11:04:10 +0200 | [diff] [blame] | 410 | * |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 411 | * Returns 0 or -EDEADLK. |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 412 | * |
| 413 | * Chain walk basics and protection scope |
| 414 | * |
| 415 | * [R] refcount on task |
| 416 | * [P] task->pi_lock held |
| 417 | * [L] rtmutex->wait_lock held |
| 418 | * |
| 419 | * Step Description Protected by |
| 420 | * function arguments: |
| 421 | * @task [R] |
| 422 | * @orig_lock if != NULL @top_task is blocked on it |
| 423 | * @next_lock Unprotected. Cannot be |
| 424 | * dereferenced. Only used for |
| 425 | * comparison. |
| 426 | * @orig_waiter if != NULL @top_task is blocked on it |
| 427 | * @top_task current, or in case of proxy |
| 428 | * locking protected by calling |
| 429 | * code |
| 430 | * again: |
| 431 | * loop_sanity_check(); |
| 432 | * retry: |
| 433 | * [1] lock(task->pi_lock); [R] acquire [P] |
| 434 | * [2] waiter = task->pi_blocked_on; [P] |
| 435 | * [3] check_exit_conditions_1(); [P] |
| 436 | * [4] lock = waiter->lock; [P] |
| 437 | * [5] if (!try_lock(lock->wait_lock)) { [P] try to acquire [L] |
| 438 | * unlock(task->pi_lock); release [P] |
| 439 | * goto retry; |
| 440 | * } |
| 441 | * [6] check_exit_conditions_2(); [P] + [L] |
| 442 | * [7] requeue_lock_waiter(lock, waiter); [P] + [L] |
| 443 | * [8] unlock(task->pi_lock); release [P] |
| 444 | * put_task_struct(task); release [R] |
| 445 | * [9] check_exit_conditions_3(); [L] |
| 446 | * [10] task = owner(lock); [L] |
| 447 | * get_task_struct(task); [L] acquire [R] |
| 448 | * lock(task->pi_lock); [L] acquire [P] |
| 449 | * [11] requeue_pi_waiter(tsk, waiters(lock));[P] + [L] |
| 450 | * [12] check_exit_conditions_4(); [P] + [L] |
| 451 | * [13] unlock(task->pi_lock); release [P] |
| 452 | * unlock(lock->wait_lock); release [L] |
| 453 | * goto again; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 454 | */ |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 455 | static int __sched rt_mutex_adjust_prio_chain(struct task_struct *task, |
| 456 | enum rtmutex_chainwalk chwalk, |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 457 | struct rt_mutex_base *orig_lock, |
| 458 | struct rt_mutex_base *next_lock, |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 459 | struct rt_mutex_waiter *orig_waiter, |
| 460 | struct task_struct *top_task) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 461 | { |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 462 | struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter; |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 463 | struct rt_mutex_waiter *prerequeue_top_waiter; |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 464 | int ret = 0, depth = 0; |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 465 | struct rt_mutex_base *lock; |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 466 | bool detect_deadlock; |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 467 | bool requeue = true; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 468 | |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 469 | detect_deadlock = rt_mutex_cond_detect_deadlock(orig_waiter, chwalk); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 470 | |
| 471 | /* |
| 472 | * The (de)boosting is a step by step approach with a lot of |
| 473 | * pitfalls. We want this to be preemptible and we want hold a |
| 474 | * maximum of two locks per step. So we have to check |
| 475 | * carefully whether things change under us. |
| 476 | */ |
| 477 | again: |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 478 | /* |
| 479 | * We limit the lock chain length for each invocation. |
| 480 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 481 | if (++depth > max_lock_depth) { |
| 482 | static int prev_max; |
| 483 | |
| 484 | /* |
| 485 | * Print this only once. If the admin changes the limit, |
| 486 | * print a new message when reaching the limit again. |
| 487 | */ |
| 488 | if (prev_max != max_lock_depth) { |
| 489 | prev_max = max_lock_depth; |
| 490 | printk(KERN_WARNING "Maximum lock depth %d reached " |
| 491 | "task: %s (%d)\n", max_lock_depth, |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 492 | top_task->comm, task_pid_nr(top_task)); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 493 | } |
| 494 | put_task_struct(task); |
| 495 | |
Thomas Gleixner | 3d5c934 | 2014-06-05 12:34:23 +0200 | [diff] [blame] | 496 | return -EDEADLK; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 497 | } |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * We are fully preemptible here and only hold the refcount on |
| 501 | * @task. So everything can have changed under us since the |
| 502 | * caller or our own code below (goto retry/again) dropped all |
| 503 | * locks. |
| 504 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 505 | retry: |
| 506 | /* |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 507 | * [1] Task cannot go away as we did a get_task() before ! |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 508 | */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 509 | raw_spin_lock_irq(&task->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 510 | |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 511 | /* |
| 512 | * [2] Get the waiter on which @task is blocked on. |
| 513 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 514 | waiter = task->pi_blocked_on; |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 515 | |
| 516 | /* |
| 517 | * [3] check_exit_conditions_1() protected by task->pi_lock. |
| 518 | */ |
| 519 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 520 | /* |
| 521 | * Check whether the end of the boosting chain has been |
| 522 | * reached or the state of the chain has changed while we |
| 523 | * dropped the locks. |
| 524 | */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 525 | if (!waiter) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 526 | goto out_unlock_pi; |
| 527 | |
Thomas Gleixner | 1a539a8 | 2007-06-08 13:46:58 -0700 | [diff] [blame] | 528 | /* |
| 529 | * Check the orig_waiter state. After we dropped the locks, |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 530 | * the previous owner of the lock might have released the lock. |
Thomas Gleixner | 1a539a8 | 2007-06-08 13:46:58 -0700 | [diff] [blame] | 531 | */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 532 | if (orig_waiter && !rt_mutex_owner(orig_lock)) |
Thomas Gleixner | 1a539a8 | 2007-06-08 13:46:58 -0700 | [diff] [blame] | 533 | goto out_unlock_pi; |
| 534 | |
| 535 | /* |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 536 | * We dropped all locks after taking a refcount on @task, so |
| 537 | * the task might have moved on in the lock chain or even left |
| 538 | * the chain completely and blocks now on an unrelated lock or |
| 539 | * on @orig_lock. |
| 540 | * |
| 541 | * We stored the lock on which @task was blocked in @next_lock, |
| 542 | * so we can detect the chain change. |
| 543 | */ |
| 544 | if (next_lock != waiter->lock) |
| 545 | goto out_unlock_pi; |
| 546 | |
| 547 | /* |
Thomas Gleixner | 1a539a8 | 2007-06-08 13:46:58 -0700 | [diff] [blame] | 548 | * Drop out, when the task has no waiters. Note, |
| 549 | * top_waiter can be NULL, when we are in the deboosting |
| 550 | * mode! |
| 551 | */ |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 552 | if (top_waiter) { |
| 553 | if (!task_has_pi_waiters(task)) |
| 554 | goto out_unlock_pi; |
| 555 | /* |
| 556 | * If deadlock detection is off, we stop here if we |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 557 | * are not the top pi waiter of the task. If deadlock |
| 558 | * detection is enabled we continue, but stop the |
| 559 | * requeueing in the chain walk. |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 560 | */ |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 561 | if (top_waiter != task_top_pi_waiter(task)) { |
| 562 | if (!detect_deadlock) |
| 563 | goto out_unlock_pi; |
| 564 | else |
| 565 | requeue = false; |
| 566 | } |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 567 | } |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 568 | |
| 569 | /* |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 570 | * If the waiter priority is the same as the task priority |
| 571 | * then there is no further priority adjustment necessary. If |
| 572 | * deadlock detection is off, we stop the chain walk. If its |
| 573 | * enabled we continue, but stop the requeueing in the chain |
| 574 | * walk. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 575 | */ |
Peter Zijlstra | 19830e5 | 2017-03-23 15:56:14 +0100 | [diff] [blame] | 576 | if (rt_mutex_waiter_equal(waiter, task_to_waiter(task))) { |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 577 | if (!detect_deadlock) |
| 578 | goto out_unlock_pi; |
| 579 | else |
| 580 | requeue = false; |
| 581 | } |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 582 | |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 583 | /* |
| 584 | * [4] Get the next lock |
| 585 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 586 | lock = waiter->lock; |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 587 | /* |
| 588 | * [5] We need to trylock here as we are holding task->pi_lock, |
| 589 | * which is the reverse lock order versus the other rtmutex |
| 590 | * operations. |
| 591 | */ |
Thomas Gleixner | d209d74 | 2009-11-17 18:22:11 +0100 | [diff] [blame] | 592 | if (!raw_spin_trylock(&lock->wait_lock)) { |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 593 | raw_spin_unlock_irq(&task->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 594 | cpu_relax(); |
| 595 | goto retry; |
| 596 | } |
| 597 | |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 598 | /* |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 599 | * [6] check_exit_conditions_2() protected by task->pi_lock and |
| 600 | * lock->wait_lock. |
| 601 | * |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 602 | * Deadlock detection. If the lock is the same as the original |
| 603 | * lock which caused us to walk the lock chain or if the |
| 604 | * current lock is owned by the task which initiated the chain |
| 605 | * walk, we detected a deadlock. |
| 606 | */ |
Thomas Gleixner | 95e02ca | 2006-06-27 02:55:02 -0700 | [diff] [blame] | 607 | if (lock == orig_lock || rt_mutex_owner(lock) == top_task) { |
Thomas Gleixner | d209d74 | 2009-11-17 18:22:11 +0100 | [diff] [blame] | 608 | raw_spin_unlock(&lock->wait_lock); |
Thomas Gleixner | 3d5c934 | 2014-06-05 12:34:23 +0200 | [diff] [blame] | 609 | ret = -EDEADLK; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 610 | goto out_unlock_pi; |
| 611 | } |
| 612 | |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 613 | /* |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 614 | * If we just follow the lock chain for deadlock detection, no |
| 615 | * need to do all the requeue operations. To avoid a truckload |
| 616 | * of conditionals around the various places below, just do the |
| 617 | * minimum chain walk checks. |
| 618 | */ |
| 619 | if (!requeue) { |
| 620 | /* |
| 621 | * No requeue[7] here. Just release @task [8] |
| 622 | */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 623 | raw_spin_unlock(&task->pi_lock); |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 624 | put_task_struct(task); |
| 625 | |
| 626 | /* |
| 627 | * [9] check_exit_conditions_3 protected by lock->wait_lock. |
| 628 | * If there is no owner of the lock, end of chain. |
| 629 | */ |
| 630 | if (!rt_mutex_owner(lock)) { |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 631 | raw_spin_unlock_irq(&lock->wait_lock); |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | /* [10] Grab the next task, i.e. owner of @lock */ |
Matthew Wilcox (Oracle) | 7b3c92b | 2019-07-04 15:13:23 -0700 | [diff] [blame] | 636 | task = get_task_struct(rt_mutex_owner(lock)); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 637 | raw_spin_lock(&task->pi_lock); |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | * No requeue [11] here. We just do deadlock detection. |
| 641 | * |
| 642 | * [12] Store whether owner is blocked |
| 643 | * itself. Decision is made after dropping the locks |
| 644 | */ |
| 645 | next_lock = task_blocked_on_lock(task); |
| 646 | /* |
| 647 | * Get the top waiter for the next iteration |
| 648 | */ |
| 649 | top_waiter = rt_mutex_top_waiter(lock); |
| 650 | |
| 651 | /* [13] Drop locks */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 652 | raw_spin_unlock(&task->pi_lock); |
| 653 | raw_spin_unlock_irq(&lock->wait_lock); |
Thomas Gleixner | 67792e2 | 2014-05-22 03:25:57 +0000 | [diff] [blame] | 654 | |
| 655 | /* If owner is not blocked, end of chain. */ |
| 656 | if (!next_lock) |
| 657 | goto out_put_task; |
| 658 | goto again; |
| 659 | } |
| 660 | |
| 661 | /* |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 662 | * Store the current top waiter before doing the requeue |
| 663 | * operation on @lock. We need it for the boost/deboost |
| 664 | * decision below. |
| 665 | */ |
| 666 | prerequeue_top_waiter = rt_mutex_top_waiter(lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 667 | |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 668 | /* [7] Requeue the waiter in the lock waiter tree. */ |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 669 | rt_mutex_dequeue(lock, waiter); |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 670 | |
| 671 | /* |
| 672 | * Update the waiter prio fields now that we're dequeued. |
| 673 | * |
| 674 | * These values can have changed through either: |
| 675 | * |
| 676 | * sys_sched_set_scheduler() / sys_sched_setattr() |
| 677 | * |
| 678 | * or |
| 679 | * |
| 680 | * DL CBS enforcement advancing the effective deadline. |
| 681 | * |
| 682 | * Even though pi_waiters also uses these fields, and that tree is only |
| 683 | * updated in [11], we can do this here, since we hold [L], which |
| 684 | * serializes all pi_waiters access and rb_erase() does not care about |
| 685 | * the values of the node being removed. |
| 686 | */ |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 687 | waiter->prio = task->prio; |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 688 | waiter->deadline = task->dl.deadline; |
| 689 | |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 690 | rt_mutex_enqueue(lock, waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 691 | |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 692 | /* [8] Release the task */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 693 | raw_spin_unlock(&task->pi_lock); |
Thomas Gleixner | 2ffa5a5 | 2014-06-07 12:10:36 +0200 | [diff] [blame] | 694 | put_task_struct(task); |
| 695 | |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 696 | /* |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 697 | * [9] check_exit_conditions_3 protected by lock->wait_lock. |
| 698 | * |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 699 | * We must abort the chain walk if there is no lock owner even |
| 700 | * in the dead lock detection case, as we have nothing to |
| 701 | * follow here. This is the end of the chain we are walking. |
| 702 | */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 703 | if (!rt_mutex_owner(lock)) { |
| 704 | /* |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 705 | * If the requeue [7] above changed the top waiter, |
| 706 | * then we need to wake the new top waiter up to try |
| 707 | * to get the lock. |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 708 | */ |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 709 | if (prerequeue_top_waiter != rt_mutex_top_waiter(lock)) |
Thomas Gleixner | c014ef6 | 2021-08-15 23:28:06 +0200 | [diff] [blame] | 710 | wake_up_state(waiter->task, waiter->wake_state); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 711 | raw_spin_unlock_irq(&lock->wait_lock); |
Thomas Gleixner | 2ffa5a5 | 2014-06-07 12:10:36 +0200 | [diff] [blame] | 712 | return 0; |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 713 | } |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 714 | |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 715 | /* [10] Grab the next task, i.e. the owner of @lock */ |
Matthew Wilcox (Oracle) | 7b3c92b | 2019-07-04 15:13:23 -0700 | [diff] [blame] | 716 | task = get_task_struct(rt_mutex_owner(lock)); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 717 | raw_spin_lock(&task->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 718 | |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 719 | /* [11] requeue the pi waiters if necessary */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 720 | if (waiter == rt_mutex_top_waiter(lock)) { |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 721 | /* |
| 722 | * The waiter became the new top (highest priority) |
| 723 | * waiter on the lock. Replace the previous top waiter |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 724 | * in the owner tasks pi waiters tree with this waiter |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 725 | * and adjust the priority of the owner. |
| 726 | */ |
| 727 | rt_mutex_dequeue_pi(task, prerequeue_top_waiter); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 728 | rt_mutex_enqueue_pi(task, waiter); |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 729 | rt_mutex_adjust_prio(task); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 730 | |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 731 | } else if (prerequeue_top_waiter == waiter) { |
| 732 | /* |
| 733 | * The waiter was the top waiter on the lock, but is |
Ingo Molnar | e2db759 | 2021-03-22 02:35:05 +0100 | [diff] [blame] | 734 | * no longer the top priority waiter. Replace waiter in |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 735 | * the owner tasks pi waiters tree with the new top |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 736 | * (highest priority) waiter and adjust the priority |
| 737 | * of the owner. |
| 738 | * The new top waiter is stored in @waiter so that |
| 739 | * @waiter == @top_waiter evaluates to true below and |
| 740 | * we continue to deboost the rest of the chain. |
| 741 | */ |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 742 | rt_mutex_dequeue_pi(task, waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 743 | waiter = rt_mutex_top_waiter(lock); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 744 | rt_mutex_enqueue_pi(task, waiter); |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 745 | rt_mutex_adjust_prio(task); |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 746 | } else { |
| 747 | /* |
| 748 | * Nothing changed. No need to do any priority |
| 749 | * adjustment. |
| 750 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 753 | /* |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 754 | * [12] check_exit_conditions_4() protected by task->pi_lock |
| 755 | * and lock->wait_lock. The actual decisions are made after we |
| 756 | * dropped the locks. |
| 757 | * |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 758 | * Check whether the task which owns the current lock is pi |
| 759 | * blocked itself. If yes we store a pointer to the lock for |
| 760 | * the lock chain change detection above. After we dropped |
| 761 | * task->pi_lock next_lock cannot be dereferenced anymore. |
| 762 | */ |
| 763 | next_lock = task_blocked_on_lock(task); |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 764 | /* |
| 765 | * Store the top waiter of @lock for the end of chain walk |
| 766 | * decision below. |
| 767 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 768 | top_waiter = rt_mutex_top_waiter(lock); |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 769 | |
| 770 | /* [13] Drop the locks */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 771 | raw_spin_unlock(&task->pi_lock); |
| 772 | raw_spin_unlock_irq(&lock->wait_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 773 | |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 774 | /* |
Thomas Gleixner | 3eb65ae | 2014-06-09 19:40:34 +0200 | [diff] [blame] | 775 | * Make the actual exit decisions [12], based on the stored |
| 776 | * values. |
| 777 | * |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 778 | * We reached the end of the lock chain. Stop right here. No |
| 779 | * point to go back just to figure that out. |
| 780 | */ |
| 781 | if (!next_lock) |
| 782 | goto out_put_task; |
| 783 | |
Thomas Gleixner | a57594a | 2014-05-22 03:25:54 +0000 | [diff] [blame] | 784 | /* |
| 785 | * If the current waiter is not the top waiter on the lock, |
| 786 | * then we can stop the chain walk here if we are not in full |
| 787 | * deadlock detection mode. |
| 788 | */ |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 789 | if (!detect_deadlock && waiter != top_waiter) |
| 790 | goto out_put_task; |
| 791 | |
| 792 | goto again; |
| 793 | |
| 794 | out_unlock_pi: |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 795 | raw_spin_unlock_irq(&task->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 796 | out_put_task: |
| 797 | put_task_struct(task); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 798 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | /* |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 803 | * Try to take an rt-mutex |
| 804 | * |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 805 | * Must be called with lock->wait_lock held and interrupts disabled |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 806 | * |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 807 | * @lock: The lock to be acquired. |
| 808 | * @task: The task which wants to acquire the lock |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 809 | * @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] | 810 | * callsite called task_blocked_on_lock(), otherwise NULL |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 811 | */ |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 812 | static int __sched |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 813 | try_to_take_rt_mutex(struct rt_mutex_base *lock, struct task_struct *task, |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 814 | struct rt_mutex_waiter *waiter) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 815 | { |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 816 | lockdep_assert_held(&lock->wait_lock); |
| 817 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 818 | /* |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 819 | * Before testing whether we can acquire @lock, we set the |
| 820 | * RT_MUTEX_HAS_WAITERS bit in @lock->owner. This forces all |
| 821 | * other tasks which try to modify @lock into the slow path |
| 822 | * and they serialize on @lock->wait_lock. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 823 | * |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 824 | * The RT_MUTEX_HAS_WAITERS bit can have a transitional state |
| 825 | * as explained at the top of this file if and only if: |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 826 | * |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 827 | * - There is a lock owner. The caller must fixup the |
| 828 | * transient state if it does a trylock or leaves the lock |
| 829 | * function due to a signal or timeout. |
| 830 | * |
| 831 | * - @task acquires the lock and there are no other |
| 832 | * waiters. This is undone in rt_mutex_set_owner(@task) at |
| 833 | * the end of this function. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 834 | */ |
| 835 | mark_rt_mutex_waiters(lock); |
| 836 | |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 837 | /* |
| 838 | * If @lock has an owner, give up. |
| 839 | */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 840 | if (rt_mutex_owner(lock)) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 841 | return 0; |
| 842 | |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 843 | /* |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 844 | * If @waiter != NULL, @task has already enqueued the waiter |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 845 | * into @lock waiter tree. If @waiter == NULL then this is a |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 846 | * trylock attempt. |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 847 | */ |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 848 | if (waiter) { |
| 849 | /* |
| 850 | * If waiter is not the highest priority waiter of |
| 851 | * @lock, give up. |
| 852 | */ |
| 853 | if (waiter != rt_mutex_top_waiter(lock)) |
| 854 | return 0; |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 855 | |
| 856 | /* |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 857 | * We can acquire the lock. Remove the waiter from the |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 858 | * lock waiters tree. |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 859 | */ |
| 860 | rt_mutex_dequeue(lock, waiter); |
| 861 | |
| 862 | } else { |
| 863 | /* |
| 864 | * If the lock has waiters already we check whether @task is |
| 865 | * eligible to take over the lock. |
| 866 | * |
| 867 | * If there are no other waiters, @task can acquire |
| 868 | * the lock. @task->pi_blocked_on is NULL, so it does |
| 869 | * not need to be dequeued. |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 870 | */ |
| 871 | if (rt_mutex_has_waiters(lock)) { |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 872 | /* |
| 873 | * If @task->prio is greater than or equal to |
| 874 | * the top waiter priority (kernel view), |
| 875 | * @task lost. |
| 876 | */ |
Peter Zijlstra | 19830e5 | 2017-03-23 15:56:14 +0100 | [diff] [blame] | 877 | if (!rt_mutex_waiter_less(task_to_waiter(task), |
| 878 | rt_mutex_top_waiter(lock))) |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 879 | return 0; |
| 880 | |
| 881 | /* |
| 882 | * The current top waiter stays enqueued. We |
| 883 | * don't have to change anything in the lock |
| 884 | * waiters order. |
| 885 | */ |
| 886 | } else { |
| 887 | /* |
| 888 | * No waiters. Take the lock without the |
| 889 | * pi_lock dance.@task->pi_blocked_on is NULL |
| 890 | * and we have no waiters to enqueue in @task |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 891 | * pi waiters tree. |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 892 | */ |
| 893 | goto takeit; |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 894 | } |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 895 | } |
| 896 | |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 897 | /* |
| 898 | * Clear @task->pi_blocked_on. Requires protection by |
| 899 | * @task->pi_lock. Redundant operation for the @waiter == NULL |
| 900 | * case, but conditionals are more expensive than a redundant |
| 901 | * store. |
| 902 | */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 903 | raw_spin_lock(&task->pi_lock); |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 904 | task->pi_blocked_on = NULL; |
| 905 | /* |
| 906 | * Finish the lock acquisition. @task is the new owner. If |
| 907 | * other waiters exist we have to insert the highest priority |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 908 | * waiter into @task->pi_waiters tree. |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 909 | */ |
| 910 | if (rt_mutex_has_waiters(lock)) |
| 911 | rt_mutex_enqueue_pi(task, rt_mutex_top_waiter(lock)); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 912 | raw_spin_unlock(&task->pi_lock); |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 913 | |
| 914 | takeit: |
Thomas Gleixner | 358c331 | 2014-06-11 01:01:13 +0200 | [diff] [blame] | 915 | /* |
| 916 | * This either preserves the RT_MUTEX_HAS_WAITERS bit if there |
| 917 | * are still waiters or clears it. |
| 918 | */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 919 | rt_mutex_set_owner(lock, task); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 920 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 921 | return 1; |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Task blocks on lock. |
| 926 | * |
| 927 | * Prepare waiter and propagate pi chain |
| 928 | * |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 929 | * This must be called with lock->wait_lock held and interrupts disabled |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 930 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 931 | static int __sched task_blocks_on_rt_mutex(struct rt_mutex_base *lock, |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 932 | struct rt_mutex_waiter *waiter, |
| 933 | struct task_struct *task, |
| 934 | enum rtmutex_chainwalk chwalk) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 935 | { |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 936 | struct task_struct *owner = rt_mutex_owner(lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 937 | struct rt_mutex_waiter *top_waiter = waiter; |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 938 | struct rt_mutex_base *next_lock; |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 939 | int chain_walk = 0, res; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 940 | |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 941 | lockdep_assert_held(&lock->wait_lock); |
| 942 | |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 943 | /* |
| 944 | * Early deadlock detection. We really don't want the task to |
| 945 | * enqueue on itself just to untangle the mess later. It's not |
| 946 | * only an optimization. We drop the locks, so another waiter |
| 947 | * can come in before the chain walk detects the deadlock. So |
| 948 | * the other will detect the deadlock and return -EDEADLOCK, |
| 949 | * which is wrong, as the other waiter is not in a deadlock |
| 950 | * situation. |
| 951 | */ |
Thomas Gleixner | 3d5c934 | 2014-06-05 12:34:23 +0200 | [diff] [blame] | 952 | if (owner == task) |
Thomas Gleixner | 397335f | 2014-05-22 03:25:39 +0000 | [diff] [blame] | 953 | return -EDEADLK; |
| 954 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 955 | raw_spin_lock(&task->pi_lock); |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 956 | waiter->task = task; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 957 | waiter->lock = lock; |
Dario Faggioli | 2d3d891 | 2013-11-07 14:43:44 +0100 | [diff] [blame] | 958 | waiter->prio = task->prio; |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 959 | waiter->deadline = task->dl.deadline; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 960 | |
| 961 | /* Get the top priority waiter on the lock */ |
| 962 | if (rt_mutex_has_waiters(lock)) |
| 963 | top_waiter = rt_mutex_top_waiter(lock); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 964 | rt_mutex_enqueue(lock, waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 965 | |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 966 | task->pi_blocked_on = waiter; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 967 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 968 | raw_spin_unlock(&task->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 969 | |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 970 | if (!owner) |
| 971 | return 0; |
| 972 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 973 | raw_spin_lock(&owner->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 974 | if (waiter == rt_mutex_top_waiter(lock)) { |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 975 | rt_mutex_dequeue_pi(owner, top_waiter); |
| 976 | rt_mutex_enqueue_pi(owner, waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 977 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 978 | rt_mutex_adjust_prio(owner); |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 979 | if (owner->pi_blocked_on) |
| 980 | chain_walk = 1; |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 981 | } else if (rt_mutex_cond_detect_deadlock(waiter, chwalk)) { |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 982 | chain_walk = 1; |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 983 | } |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 984 | |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 985 | /* Store the lock on which owner is blocked or NULL */ |
| 986 | next_lock = task_blocked_on_lock(owner); |
| 987 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 988 | raw_spin_unlock(&owner->pi_lock); |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 989 | /* |
| 990 | * Even if full deadlock detection is on, if the owner is not |
| 991 | * blocked itself, we can avoid finding this out in the chain |
| 992 | * walk. |
| 993 | */ |
| 994 | if (!chain_walk || !next_lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 995 | return 0; |
| 996 | |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 997 | /* |
| 998 | * The owner can't disappear while holding a lock, |
| 999 | * so the owner struct is protected by wait_lock. |
| 1000 | * Gets dropped in rt_mutex_adjust_prio_chain()! |
| 1001 | */ |
| 1002 | get_task_struct(owner); |
| 1003 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1004 | raw_spin_unlock_irq(&lock->wait_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1005 | |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 1006 | res = rt_mutex_adjust_prio_chain(owner, chwalk, lock, |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 1007 | next_lock, waiter, task); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1008 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1009 | raw_spin_lock_irq(&lock->wait_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1010 | |
| 1011 | return res; |
| 1012 | } |
| 1013 | |
| 1014 | /* |
Davidlohr Bueso | 9f40a51 | 2015-05-19 10:24:57 -0700 | [diff] [blame] | 1015 | * Remove the top waiter from the current tasks pi waiter tree and |
Davidlohr Bueso | 45ab4ef | 2015-05-19 10:24:55 -0700 | [diff] [blame] | 1016 | * queue it up. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1017 | * |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1018 | * Called with lock->wait_lock held and interrupts disabled. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1019 | */ |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame^] | 1020 | static void __sched mark_wakeup_next_waiter(struct rt_wake_q_head *wqh, |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1021 | struct rt_mutex_base *lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1022 | { |
| 1023 | struct rt_mutex_waiter *waiter; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1024 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1025 | raw_spin_lock(¤t->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1026 | |
| 1027 | waiter = rt_mutex_top_waiter(lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1028 | |
| 1029 | /* |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 1030 | * Remove it from current->pi_waiters and deboost. |
| 1031 | * |
| 1032 | * We must in fact deboost here in order to ensure we call |
| 1033 | * rt_mutex_setprio() to update p->pi_top_task before the |
| 1034 | * task unblocks. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1035 | */ |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 1036 | rt_mutex_dequeue_pi(current, waiter); |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 1037 | rt_mutex_adjust_prio(current); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1038 | |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 1039 | /* |
| 1040 | * As we are waking up the top waiter, and the waiter stays |
| 1041 | * queued on the lock until it gets the lock, this lock |
| 1042 | * obviously has waiters. Just set the bit here and this has |
| 1043 | * the added benefit of forcing all new tasks into the |
| 1044 | * slow path making sure no task of lower priority than |
| 1045 | * the top waiter can steal this lock. |
| 1046 | */ |
| 1047 | lock->owner = (void *) RT_MUTEX_HAS_WAITERS; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1048 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 1049 | /* |
| 1050 | * We deboosted before waking the top waiter task such that we don't |
| 1051 | * run two tasks with the 'same' priority (and ensure the |
| 1052 | * p->pi_top_task pointer points to a blocked task). This however can |
| 1053 | * lead to priority inversion if we would get preempted after the |
| 1054 | * deboost but before waking our donor task, hence the preempt_disable() |
| 1055 | * before unlock. |
| 1056 | * |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame^] | 1057 | * Pairs with preempt_enable() in rt_mutex_wake_up_q(); |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 1058 | */ |
| 1059 | preempt_disable(); |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame^] | 1060 | rt_mutex_wake_q_add(wqh, waiter); |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 1061 | raw_spin_unlock(¤t->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 1065 | * Remove a waiter from a lock and give up |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1066 | * |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1067 | * 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] | 1068 | * have just failed to try_to_take_rt_mutex(). |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1069 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1070 | static void __sched remove_waiter(struct rt_mutex_base *lock, |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 1071 | struct rt_mutex_waiter *waiter) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1072 | { |
Thomas Gleixner | 1ca7b86 | 2014-06-07 09:36:13 +0200 | [diff] [blame] | 1073 | bool is_top_waiter = (waiter == rt_mutex_top_waiter(lock)); |
Ingo Molnar | 36c8b58 | 2006-07-03 00:25:41 -0700 | [diff] [blame] | 1074 | struct task_struct *owner = rt_mutex_owner(lock); |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1075 | struct rt_mutex_base *next_lock; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1076 | |
Peter Zijlstra | e0aad5b | 2017-03-23 15:56:13 +0100 | [diff] [blame] | 1077 | lockdep_assert_held(&lock->wait_lock); |
| 1078 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1079 | raw_spin_lock(¤t->pi_lock); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 1080 | rt_mutex_dequeue(lock, waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1081 | current->pi_blocked_on = NULL; |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1082 | raw_spin_unlock(¤t->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1083 | |
Thomas Gleixner | 1ca7b86 | 2014-06-07 09:36:13 +0200 | [diff] [blame] | 1084 | /* |
| 1085 | * Only update priority if the waiter was the highest priority |
| 1086 | * waiter of the lock and there is an owner to update. |
| 1087 | */ |
| 1088 | if (!owner || !is_top_waiter) |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 1089 | return; |
| 1090 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1091 | raw_spin_lock(&owner->pi_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1092 | |
Thomas Gleixner | 1ca7b86 | 2014-06-07 09:36:13 +0200 | [diff] [blame] | 1093 | rt_mutex_dequeue_pi(owner, waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1094 | |
Thomas Gleixner | 1ca7b86 | 2014-06-07 09:36:13 +0200 | [diff] [blame] | 1095 | if (rt_mutex_has_waiters(lock)) |
| 1096 | rt_mutex_enqueue_pi(owner, rt_mutex_top_waiter(lock)); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1097 | |
Peter Zijlstra | acd5862 | 2017-03-23 15:56:11 +0100 | [diff] [blame] | 1098 | rt_mutex_adjust_prio(owner); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1099 | |
Thomas Gleixner | 1ca7b86 | 2014-06-07 09:36:13 +0200 | [diff] [blame] | 1100 | /* Store the lock on which owner is blocked or NULL */ |
| 1101 | next_lock = task_blocked_on_lock(owner); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1102 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1103 | raw_spin_unlock(&owner->pi_lock); |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 1104 | |
Thomas Gleixner | 1ca7b86 | 2014-06-07 09:36:13 +0200 | [diff] [blame] | 1105 | /* |
| 1106 | * Don't walk the chain, if the owner task is not blocked |
| 1107 | * itself. |
| 1108 | */ |
Thomas Gleixner | 8208498 | 2014-06-05 11:16:12 +0200 | [diff] [blame] | 1109 | if (!next_lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1110 | return; |
| 1111 | |
Steven Rostedt | db63063 | 2006-09-29 01:59:44 -0700 | [diff] [blame] | 1112 | /* gets dropped in rt_mutex_adjust_prio_chain()! */ |
| 1113 | get_task_struct(owner); |
| 1114 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1115 | raw_spin_unlock_irq(&lock->wait_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1116 | |
Thomas Gleixner | 8930ed8 | 2014-05-22 03:25:47 +0000 | [diff] [blame] | 1117 | rt_mutex_adjust_prio_chain(owner, RT_MUTEX_MIN_CHAINWALK, lock, |
| 1118 | next_lock, NULL, current); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1119 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1120 | raw_spin_lock_irq(&lock->wait_lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1123 | /** |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1124 | * rt_mutex_slowlock_block() - Perform the wait-wake-try-to-take loop |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1125 | * @lock: the rt_mutex to take |
| 1126 | * @state: the state the task should block in (TASK_INTERRUPTIBLE |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1127 | * or TASK_UNINTERRUPTIBLE) |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1128 | * @timeout: the pre-initialized and started timer, or NULL for none |
| 1129 | * @waiter: the pre-initialized rt_mutex_waiter |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1130 | * |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1131 | * Must be called with lock->wait_lock held and interrupts disabled |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1132 | */ |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1133 | static int __sched rt_mutex_slowlock_block(struct rt_mutex_base *lock, |
| 1134 | unsigned int state, |
| 1135 | struct hrtimer_sleeper *timeout, |
| 1136 | struct rt_mutex_waiter *waiter) |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1137 | { |
| 1138 | int ret = 0; |
| 1139 | |
| 1140 | for (;;) { |
| 1141 | /* Try to acquire the lock: */ |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 1142 | if (try_to_take_rt_mutex(lock, current, waiter)) |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1143 | break; |
| 1144 | |
Thomas Gleixner | a51a327 | 2021-03-26 16:29:44 +0100 | [diff] [blame] | 1145 | if (timeout && !timeout->task) { |
| 1146 | ret = -ETIMEDOUT; |
| 1147 | break; |
| 1148 | } |
| 1149 | if (signal_pending_state(state, current)) { |
| 1150 | ret = -EINTR; |
| 1151 | break; |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1154 | raw_spin_unlock_irq(&lock->wait_lock); |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1155 | |
Davidlohr Bueso | 1b0b7c1 | 2015-07-01 13:29:48 -0700 | [diff] [blame] | 1156 | schedule(); |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1157 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1158 | raw_spin_lock_irq(&lock->wait_lock); |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1159 | set_current_state(state); |
| 1160 | } |
| 1161 | |
Davidlohr Bueso | afffc6c | 2015-02-01 22:16:24 -0800 | [diff] [blame] | 1162 | __set_current_state(TASK_RUNNING); |
Darren Hart | 8dac456 | 2009-04-03 13:40:12 -0700 | [diff] [blame] | 1163 | return ret; |
| 1164 | } |
| 1165 | |
Thomas Gleixner | d7a2edb | 2021-03-26 16:29:40 +0100 | [diff] [blame] | 1166 | static void __sched rt_mutex_handle_deadlock(int res, int detect_deadlock, |
| 1167 | struct rt_mutex_waiter *w) |
Thomas Gleixner | 3d5c934 | 2014-06-05 12:34:23 +0200 | [diff] [blame] | 1168 | { |
| 1169 | /* |
| 1170 | * If the result is not -EDEADLOCK or the caller requested |
| 1171 | * deadlock detection, nothing to do here. |
| 1172 | */ |
| 1173 | if (res != -EDEADLOCK || detect_deadlock) |
| 1174 | return; |
| 1175 | |
| 1176 | /* |
Ingo Molnar | e2db759 | 2021-03-22 02:35:05 +0100 | [diff] [blame] | 1177 | * Yell loudly and stop the task right here. |
Thomas Gleixner | 3d5c934 | 2014-06-05 12:34:23 +0200 | [diff] [blame] | 1178 | */ |
Sebastian Andrzej Siewior | 6d41c67 | 2021-03-26 16:29:32 +0100 | [diff] [blame] | 1179 | WARN(1, "rtmutex deadlock detected\n"); |
Thomas Gleixner | 3d5c934 | 2014-06-05 12:34:23 +0200 | [diff] [blame] | 1180 | while (1) { |
| 1181 | set_current_state(TASK_INTERRUPTIBLE); |
| 1182 | schedule(); |
| 1183 | } |
| 1184 | } |
| 1185 | |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1186 | /** |
| 1187 | * __rt_mutex_slowlock - Locking slowpath invoked with lock::wait_lock held |
| 1188 | * @lock: The rtmutex to block lock |
| 1189 | * @state: The task state for sleeping |
| 1190 | * @chwalk: Indicator whether full or partial chainwalk is requested |
| 1191 | * @waiter: Initializer waiter for blocking |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1192 | */ |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1193 | static int __sched __rt_mutex_slowlock(struct rt_mutex_base *lock, |
| 1194 | unsigned int state, |
| 1195 | enum rtmutex_chainwalk chwalk, |
| 1196 | struct rt_mutex_waiter *waiter) |
| 1197 | { |
| 1198 | int ret; |
| 1199 | |
| 1200 | lockdep_assert_held(&lock->wait_lock); |
| 1201 | |
| 1202 | /* Try to acquire the lock again: */ |
| 1203 | if (try_to_take_rt_mutex(lock, current, NULL)) |
| 1204 | return 0; |
| 1205 | |
| 1206 | set_current_state(state); |
| 1207 | |
| 1208 | ret = task_blocks_on_rt_mutex(lock, waiter, current, chwalk); |
| 1209 | |
| 1210 | if (likely(!ret)) |
| 1211 | ret = rt_mutex_slowlock_block(lock, state, NULL, waiter); |
| 1212 | |
| 1213 | if (unlikely(ret)) { |
| 1214 | __set_current_state(TASK_RUNNING); |
| 1215 | remove_waiter(lock, waiter); |
| 1216 | rt_mutex_handle_deadlock(ret, chwalk, waiter); |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * try_to_take_rt_mutex() sets the waiter bit |
| 1221 | * unconditionally. We might have to fix that up. |
| 1222 | */ |
| 1223 | fixup_rt_mutex_waiters(lock); |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
| 1227 | static inline int __rt_mutex_slowlock_locked(struct rt_mutex_base *lock, |
| 1228 | unsigned int state) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1229 | { |
| 1230 | struct rt_mutex_waiter waiter; |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1231 | int ret; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1232 | |
Peter Zijlstra | 5080935 | 2017-03-22 11:35:56 +0100 | [diff] [blame] | 1233 | rt_mutex_init_waiter(&waiter); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1234 | |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1235 | ret = __rt_mutex_slowlock(lock, state, RT_MUTEX_MIN_CHAINWALK, &waiter); |
| 1236 | |
| 1237 | debug_rt_mutex_free_waiter(&waiter); |
| 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * rt_mutex_slowlock - Locking slowpath invoked when fast path fails |
| 1243 | * @lock: The rtmutex to block lock |
| 1244 | * @state: The task state for sleeping |
| 1245 | */ |
| 1246 | static int __sched rt_mutex_slowlock(struct rt_mutex_base *lock, |
| 1247 | unsigned int state) |
| 1248 | { |
| 1249 | unsigned long flags; |
| 1250 | int ret; |
| 1251 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1252 | /* |
| 1253 | * Technically we could use raw_spin_[un]lock_irq() here, but this can |
| 1254 | * be called in early boot if the cmpxchg() fast path is disabled |
| 1255 | * (debug, no architecture support). In this case we will acquire the |
| 1256 | * rtmutex with lock->wait_lock held. But we cannot unconditionally |
| 1257 | * enable interrupts in that early boot case. So we need to use the |
| 1258 | * irqsave/restore variants. |
| 1259 | */ |
| 1260 | raw_spin_lock_irqsave(&lock->wait_lock, flags); |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1261 | ret = __rt_mutex_slowlock_locked(lock, state); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1262 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1263 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1264 | return ret; |
| 1265 | } |
| 1266 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1267 | static __always_inline int __rt_mutex_lock(struct rt_mutex_base *lock, |
Thomas Gleixner | 531ae4b | 2021-08-15 23:27:57 +0200 | [diff] [blame] | 1268 | unsigned int state) |
| 1269 | { |
| 1270 | if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) |
| 1271 | return 0; |
| 1272 | |
Thomas Gleixner | ebbdc41 | 2021-08-15 23:28:00 +0200 | [diff] [blame] | 1273 | return rt_mutex_slowlock(lock, state); |
Thomas Gleixner | 531ae4b | 2021-08-15 23:27:57 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1276 | static int __sched __rt_mutex_slowtrylock(struct rt_mutex_base *lock) |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 1277 | { |
| 1278 | int ret = try_to_take_rt_mutex(lock, current, NULL); |
| 1279 | |
| 1280 | /* |
| 1281 | * try_to_take_rt_mutex() sets the lock waiters bit |
| 1282 | * unconditionally. Clean this up. |
| 1283 | */ |
| 1284 | fixup_rt_mutex_waiters(lock); |
| 1285 | |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1289 | /* |
| 1290 | * Slow path try-lock function: |
| 1291 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1292 | static int __sched rt_mutex_slowtrylock(struct rt_mutex_base *lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1293 | { |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1294 | unsigned long flags; |
Thomas Gleixner | 88f2b4c | 2014-06-10 22:53:40 +0200 | [diff] [blame] | 1295 | int ret; |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1296 | |
Thomas Gleixner | 88f2b4c | 2014-06-10 22:53:40 +0200 | [diff] [blame] | 1297 | /* |
| 1298 | * If the lock already has an owner we fail to get the lock. |
| 1299 | * This can be done without taking the @lock->wait_lock as |
| 1300 | * it is only being read, and this is a trylock anyway. |
| 1301 | */ |
| 1302 | if (rt_mutex_owner(lock)) |
| 1303 | return 0; |
| 1304 | |
| 1305 | /* |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1306 | * The mutex has currently no owner. Lock the wait lock and try to |
| 1307 | * acquire the lock. We use irqsave here to support early boot calls. |
Thomas Gleixner | 88f2b4c | 2014-06-10 22:53:40 +0200 | [diff] [blame] | 1308 | */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1309 | raw_spin_lock_irqsave(&lock->wait_lock, flags); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1310 | |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 1311 | ret = __rt_mutex_slowtrylock(lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1312 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1313 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1314 | |
| 1315 | return ret; |
| 1316 | } |
| 1317 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1318 | static __always_inline int __rt_mutex_trylock(struct rt_mutex_base *lock) |
Thomas Gleixner | 70c8010 | 2021-03-26 16:29:41 +0100 | [diff] [blame] | 1319 | { |
Thomas Gleixner | 531ae4b | 2021-08-15 23:27:57 +0200 | [diff] [blame] | 1320 | if (likely(rt_mutex_cmpxchg_acquire(lock, NULL, current))) |
| 1321 | return 1; |
Thomas Gleixner | 70c8010 | 2021-03-26 16:29:41 +0100 | [diff] [blame] | 1322 | |
Thomas Gleixner | 531ae4b | 2021-08-15 23:27:57 +0200 | [diff] [blame] | 1323 | return rt_mutex_slowtrylock(lock); |
Thomas Gleixner | 70c8010 | 2021-03-26 16:29:41 +0100 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | /* |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 1327 | * Slow path to release a rt-mutex. |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1328 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1329 | static void __sched rt_mutex_slowunlock(struct rt_mutex_base *lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1330 | { |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame^] | 1331 | DEFINE_RT_WAKE_Q(wqh); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1332 | unsigned long flags; |
| 1333 | |
| 1334 | /* irqsave required to support early boot calls */ |
| 1335 | raw_spin_lock_irqsave(&lock->wait_lock, flags); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1336 | |
| 1337 | debug_rt_mutex_unlock(lock); |
| 1338 | |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 1339 | /* |
| 1340 | * We must be careful here if the fast path is enabled. If we |
| 1341 | * have no waiters queued we cannot set owner to NULL here |
| 1342 | * because of: |
| 1343 | * |
| 1344 | * foo->lock->owner = NULL; |
| 1345 | * rtmutex_lock(foo->lock); <- fast path |
| 1346 | * free = atomic_dec_and_test(foo->refcnt); |
| 1347 | * rtmutex_unlock(foo->lock); <- fast path |
| 1348 | * if (free) |
| 1349 | * kfree(foo); |
| 1350 | * raw_spin_unlock(foo->lock->wait_lock); |
| 1351 | * |
| 1352 | * So for the fastpath enabled kernel: |
| 1353 | * |
| 1354 | * Nothing can set the waiters bit as long as we hold |
| 1355 | * lock->wait_lock. So we do the following sequence: |
| 1356 | * |
| 1357 | * owner = rt_mutex_owner(lock); |
| 1358 | * clear_rt_mutex_waiters(lock); |
| 1359 | * raw_spin_unlock(&lock->wait_lock); |
| 1360 | * if (cmpxchg(&lock->owner, owner, 0) == owner) |
| 1361 | * return; |
| 1362 | * goto retry; |
| 1363 | * |
| 1364 | * The fastpath disabled variant is simple as all access to |
| 1365 | * lock->owner is serialized by lock->wait_lock: |
| 1366 | * |
| 1367 | * lock->owner = NULL; |
| 1368 | * raw_spin_unlock(&lock->wait_lock); |
| 1369 | */ |
| 1370 | while (!rt_mutex_has_waiters(lock)) { |
| 1371 | /* Drops lock->wait_lock ! */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1372 | if (unlock_rt_mutex_safe(lock, flags) == true) |
Thomas Gleixner | 70c8010 | 2021-03-26 16:29:41 +0100 | [diff] [blame] | 1373 | return; |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 1374 | /* Relock the rtmutex and try again */ |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1375 | raw_spin_lock_irqsave(&lock->wait_lock, flags); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 1378 | /* |
| 1379 | * The wakeup next waiter path does not suffer from the above |
| 1380 | * race. See the comments there. |
Davidlohr Bueso | 45ab4ef | 2015-05-19 10:24:55 -0700 | [diff] [blame] | 1381 | * |
| 1382 | * Queue the next waiter for wakeup once we release the wait_lock. |
Thomas Gleixner | 27e3571 | 2014-06-11 18:44:04 +0000 | [diff] [blame] | 1383 | */ |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame^] | 1384 | mark_wakeup_next_waiter(&wqh, lock); |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1385 | raw_spin_unlock_irqrestore(&lock->wait_lock, flags); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1386 | |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame^] | 1387 | rt_mutex_wake_up_q(&wqh); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 1390 | static __always_inline void __rt_mutex_unlock(struct rt_mutex_base *lock) |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1391 | { |
Thomas Gleixner | 70c8010 | 2021-03-26 16:29:41 +0100 | [diff] [blame] | 1392 | if (likely(rt_mutex_cmpxchg_release(lock, current, NULL))) |
| 1393 | return; |
| 1394 | |
| 1395 | rt_mutex_slowunlock(lock); |
Ingo Molnar | 23f78d4a | 2006-06-27 02:54:53 -0700 | [diff] [blame] | 1396 | } |