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