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