diff options
Diffstat (limited to 'kernel/rtmutex.c')
| -rw-r--r-- | kernel/rtmutex.c | 990 |
1 files changed, 990 insertions, 0 deletions
diff --git a/kernel/rtmutex.c b/kernel/rtmutex.c new file mode 100644 index 000000000000..4ab17da46fd8 --- /dev/null +++ b/kernel/rtmutex.c | |||
| @@ -0,0 +1,990 @@ | |||
| 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 | ||
| 10 | * | ||
| 11 | * See Documentation/rt-mutex-design.txt for details. | ||
| 12 | */ | ||
| 13 | #include <linux/spinlock.h> | ||
| 14 | #include <linux/module.h> | ||
| 15 | #include <linux/sched.h> | ||
| 16 | #include <linux/timer.h> | ||
| 17 | |||
| 18 | #include "rtmutex_common.h" | ||
| 19 | |||
| 20 | #ifdef CONFIG_DEBUG_RT_MUTEXES | ||
| 21 | # include "rtmutex-debug.h" | ||
| 22 | #else | ||
| 23 | # include "rtmutex.h" | ||
| 24 | #endif | ||
| 25 | |||
| 26 | /* | ||
| 27 | * lock->owner state tracking: | ||
| 28 | * | ||
| 29 | * lock->owner holds the task_struct pointer of the owner. Bit 0 and 1 | ||
| 30 | * are used to keep track of the "owner is pending" and "lock has | ||
| 31 | * waiters" state. | ||
| 32 | * | ||
| 33 | * owner bit1 bit0 | ||
| 34 | * NULL 0 0 lock is free (fast acquire possible) | ||
| 35 | * NULL 0 1 invalid state | ||
| 36 | * NULL 1 0 Transitional State* | ||
| 37 | * NULL 1 1 invalid state | ||
| 38 | * taskpointer 0 0 lock is held (fast release possible) | ||
| 39 | * taskpointer 0 1 task is pending owner | ||
| 40 | * taskpointer 1 0 lock is held and has waiters | ||
| 41 | * taskpointer 1 1 task is pending owner and lock has more waiters | ||
| 42 | * | ||
| 43 | * Pending ownership is assigned to the top (highest priority) | ||
| 44 | * waiter of the lock, when the lock is released. The thread is woken | ||
| 45 | * up and can now take the lock. Until the lock is taken (bit 0 | ||
| 46 | * cleared) a competing higher priority thread can steal the lock | ||
| 47 | * which puts the woken up thread back on the waiters list. | ||
| 48 | * | ||
| 49 | * The fast atomic compare exchange based acquire and release is only | ||
| 50 | * possible when bit 0 and 1 of lock->owner are 0. | ||
| 51 | * | ||
| 52 | * (*) There's a small time where the owner can be NULL and the | ||
| 53 | * "lock has waiters" bit is set. This can happen when grabbing the lock. | ||
| 54 | * To prevent a cmpxchg of the owner releasing the lock, we need to set this | ||
| 55 | * bit before looking at the lock, hence the reason this is a transitional | ||
| 56 | * state. | ||
| 57 | */ | ||
| 58 | |||
| 59 | static void | ||
| 60 | rt_mutex_set_owner(struct rt_mutex *lock, struct task_struct *owner, | ||
| 61 | unsigned long mask) | ||
| 62 | { | ||
| 63 | unsigned long val = (unsigned long)owner | mask; | ||
| 64 | |||
| 65 | if (rt_mutex_has_waiters(lock)) | ||
| 66 | val |= RT_MUTEX_HAS_WAITERS; | ||
| 67 | |||
| 68 | lock->owner = (struct task_struct *)val; | ||
| 69 | } | ||
| 70 | |||
| 71 | static inline void clear_rt_mutex_waiters(struct rt_mutex *lock) | ||
| 72 | { | ||
| 73 | lock->owner = (struct task_struct *) | ||
| 74 | ((unsigned long)lock->owner & ~RT_MUTEX_HAS_WAITERS); | ||
| 75 | } | ||
| 76 | |||
| 77 | static void fixup_rt_mutex_waiters(struct rt_mutex *lock) | ||
| 78 | { | ||
| 79 | if (!rt_mutex_has_waiters(lock)) | ||
| 80 | clear_rt_mutex_waiters(lock); | ||
| 81 | } | ||
| 82 | |||
| 83 | /* | ||
| 84 | * We can speed up the acquire/release, if the architecture | ||
| 85 | * supports cmpxchg and if there's no debugging state to be set up | ||
| 86 | */ | ||
| 87 | #if defined(__HAVE_ARCH_CMPXCHG) && !defined(CONFIG_DEBUG_RT_MUTEXES) | ||
| 88 | # define rt_mutex_cmpxchg(l,c,n) (cmpxchg(&l->owner, c, n) == c) | ||
| 89 | static inline void mark_rt_mutex_waiters(struct rt_mutex *lock) | ||
| 90 | { | ||
| 91 | unsigned long owner, *p = (unsigned long *) &lock->owner; | ||
| 92 | |||
| 93 | do { | ||
| 94 | owner = *p; | ||
| 95 | } while (cmpxchg(p, owner, owner | RT_MUTEX_HAS_WAITERS) != owner); | ||
| 96 | } | ||
| 97 | #else | ||
| 98 | # define rt_mutex_cmpxchg(l,c,n) (0) | ||
| 99 | static inline void mark_rt_mutex_waiters(struct rt_mutex *lock) | ||
| 100 | { | ||
| 101 | lock->owner = (struct task_struct *) | ||
| 102 | ((unsigned long)lock->owner | RT_MUTEX_HAS_WAITERS); | ||
| 103 | } | ||
| 104 | #endif | ||
| 105 | |||
| 106 | /* | ||
| 107 | * Calculate task priority from the waiter list priority | ||
| 108 | * | ||
| 109 | * Return task->normal_prio when the waiter list is empty or when | ||
| 110 | * the waiter is not allowed to do priority boosting | ||
| 111 | */ | ||
| 112 | int rt_mutex_getprio(struct task_struct *task) | ||
| 113 | { | ||
| 114 | if (likely(!task_has_pi_waiters(task))) | ||
| 115 | return task->normal_prio; | ||
| 116 | |||
| 117 | return min(task_top_pi_waiter(task)->pi_list_entry.prio, | ||
| 118 | task->normal_prio); | ||
| 119 | } | ||
| 120 | |||
| 121 | /* | ||
| 122 | * Adjust the priority of a task, after its pi_waiters got modified. | ||
| 123 | * | ||
| 124 | * This can be both boosting and unboosting. task->pi_lock must be held. | ||
| 125 | */ | ||
| 126 | static void __rt_mutex_adjust_prio(struct task_struct *task) | ||
| 127 | { | ||
| 128 | int prio = rt_mutex_getprio(task); | ||
| 129 | |||
| 130 | if (task->prio != prio) | ||
| 131 | rt_mutex_setprio(task, prio); | ||
| 132 | } | ||
| 133 | |||
| 134 | /* | ||
| 135 | * Adjust task priority (undo boosting). Called from the exit path of | ||
| 136 | * rt_mutex_slowunlock() and rt_mutex_slowlock(). | ||
| 137 | * | ||
| 138 | * (Note: We do this outside of the protection of lock->wait_lock to | ||
| 139 | * allow the lock to be taken while or before we readjust the priority | ||
| 140 | * of task. We do not use the spin_xx_mutex() variants here as we are | ||
| 141 | * outside of the debug path.) | ||
| 142 | */ | ||
| 143 | static void rt_mutex_adjust_prio(struct task_struct *task) | ||
| 144 | { | ||
| 145 | unsigned long flags; | ||
| 146 | |||
| 147 | spin_lock_irqsave(&task->pi_lock, flags); | ||
| 148 | __rt_mutex_adjust_prio(task); | ||
| 149 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 150 | } | ||
| 151 | |||
| 152 | /* | ||
| 153 | * Max number of times we'll walk the boosting chain: | ||
| 154 | */ | ||
| 155 | int max_lock_depth = 1024; | ||
| 156 | |||
| 157 | /* | ||
| 158 | * Adjust the priority chain. Also used for deadlock detection. | ||
| 159 | * Decreases task's usage by one - may thus free the task. | ||
| 160 | * Returns 0 or -EDEADLK. | ||
| 161 | */ | ||
| 162 | static int rt_mutex_adjust_prio_chain(struct task_struct *task, | ||
| 163 | int deadlock_detect, | ||
| 164 | struct rt_mutex *orig_lock, | ||
| 165 | struct rt_mutex_waiter *orig_waiter, | ||
| 166 | struct task_struct *top_task) | ||
| 167 | { | ||
| 168 | struct rt_mutex *lock; | ||
| 169 | struct rt_mutex_waiter *waiter, *top_waiter = orig_waiter; | ||
| 170 | int detect_deadlock, ret = 0, depth = 0; | ||
| 171 | unsigned long flags; | ||
| 172 | |||
| 173 | detect_deadlock = debug_rt_mutex_detect_deadlock(orig_waiter, | ||
| 174 | deadlock_detect); | ||
| 175 | |||
| 176 | /* | ||
| 177 | * The (de)boosting is a step by step approach with a lot of | ||
| 178 | * pitfalls. We want this to be preemptible and we want hold a | ||
| 179 | * maximum of two locks per step. So we have to check | ||
| 180 | * carefully whether things change under us. | ||
| 181 | */ | ||
| 182 | again: | ||
| 183 | if (++depth > max_lock_depth) { | ||
| 184 | static int prev_max; | ||
| 185 | |||
| 186 | /* | ||
| 187 | * Print this only once. If the admin changes the limit, | ||
| 188 | * print a new message when reaching the limit again. | ||
| 189 | */ | ||
| 190 | if (prev_max != max_lock_depth) { | ||
| 191 | prev_max = max_lock_depth; | ||
| 192 | printk(KERN_WARNING "Maximum lock depth %d reached " | ||
| 193 | "task: %s (%d)\n", max_lock_depth, | ||
| 194 | top_task->comm, top_task->pid); | ||
| 195 | } | ||
| 196 | put_task_struct(task); | ||
| 197 | |||
| 198 | return deadlock_detect ? -EDEADLK : 0; | ||
| 199 | } | ||
| 200 | retry: | ||
| 201 | /* | ||
| 202 | * Task can not go away as we did a get_task() before ! | ||
| 203 | */ | ||
| 204 | spin_lock_irqsave(&task->pi_lock, flags); | ||
| 205 | |||
| 206 | waiter = task->pi_blocked_on; | ||
| 207 | /* | ||
| 208 | * Check whether the end of the boosting chain has been | ||
| 209 | * reached or the state of the chain has changed while we | ||
| 210 | * dropped the locks. | ||
| 211 | */ | ||
| 212 | if (!waiter || !waiter->task) | ||
| 213 | goto out_unlock_pi; | ||
| 214 | |||
| 215 | if (top_waiter && (!task_has_pi_waiters(task) || | ||
| 216 | top_waiter != task_top_pi_waiter(task))) | ||
| 217 | goto out_unlock_pi; | ||
| 218 | |||
| 219 | /* | ||
| 220 | * When deadlock detection is off then we check, if further | ||
| 221 | * priority adjustment is necessary. | ||
| 222 | */ | ||
| 223 | if (!detect_deadlock && waiter->list_entry.prio == task->prio) | ||
| 224 | goto out_unlock_pi; | ||
| 225 | |||
| 226 | lock = waiter->lock; | ||
| 227 | if (!spin_trylock(&lock->wait_lock)) { | ||
| 228 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 229 | cpu_relax(); | ||
| 230 | goto retry; | ||
| 231 | } | ||
| 232 | |||
| 233 | /* Deadlock detection */ | ||
| 234 | if (lock == orig_lock || rt_mutex_owner(lock) == top_task) { | ||
| 235 | debug_rt_mutex_deadlock(deadlock_detect, orig_waiter, lock); | ||
| 236 | spin_unlock(&lock->wait_lock); | ||
| 237 | ret = deadlock_detect ? -EDEADLK : 0; | ||
| 238 | goto out_unlock_pi; | ||
| 239 | } | ||
| 240 | |||
| 241 | top_waiter = rt_mutex_top_waiter(lock); | ||
| 242 | |||
| 243 | /* Requeue the waiter */ | ||
| 244 | plist_del(&waiter->list_entry, &lock->wait_list); | ||
| 245 | waiter->list_entry.prio = task->prio; | ||
| 246 | plist_add(&waiter->list_entry, &lock->wait_list); | ||
| 247 | |||
| 248 | /* Release the task */ | ||
| 249 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 250 | put_task_struct(task); | ||
| 251 | |||
| 252 | /* Grab the next task */ | ||
| 253 | task = rt_mutex_owner(lock); | ||
| 254 | get_task_struct(task); | ||
| 255 | spin_lock_irqsave(&task->pi_lock, flags); | ||
| 256 | |||
| 257 | if (waiter == rt_mutex_top_waiter(lock)) { | ||
| 258 | /* Boost the owner */ | ||
| 259 | plist_del(&top_waiter->pi_list_entry, &task->pi_waiters); | ||
| 260 | waiter->pi_list_entry.prio = waiter->list_entry.prio; | ||
| 261 | plist_add(&waiter->pi_list_entry, &task->pi_waiters); | ||
| 262 | __rt_mutex_adjust_prio(task); | ||
| 263 | |||
| 264 | } else if (top_waiter == waiter) { | ||
| 265 | /* Deboost the owner */ | ||
| 266 | plist_del(&waiter->pi_list_entry, &task->pi_waiters); | ||
| 267 | waiter = rt_mutex_top_waiter(lock); | ||
| 268 | waiter->pi_list_entry.prio = waiter->list_entry.prio; | ||
| 269 | plist_add(&waiter->pi_list_entry, &task->pi_waiters); | ||
| 270 | __rt_mutex_adjust_prio(task); | ||
| 271 | } | ||
| 272 | |||
| 273 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 274 | |||
| 275 | top_waiter = rt_mutex_top_waiter(lock); | ||
| 276 | spin_unlock(&lock->wait_lock); | ||
| 277 | |||
| 278 | if (!detect_deadlock && waiter != top_waiter) | ||
| 279 | goto out_put_task; | ||
| 280 | |||
| 281 | goto again; | ||
| 282 | |||
| 283 | out_unlock_pi: | ||
| 284 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 285 | out_put_task: | ||
| 286 | put_task_struct(task); | ||
| 287 | |||
| 288 | return ret; | ||
| 289 | } | ||
| 290 | |||
| 291 | /* | ||
| 292 | * Optimization: check if we can steal the lock from the | ||
| 293 | * assigned pending owner [which might not have taken the | ||
| 294 | * lock yet]: | ||
| 295 | */ | ||
| 296 | static inline int try_to_steal_lock(struct rt_mutex *lock) | ||
| 297 | { | ||
| 298 | struct task_struct *pendowner = rt_mutex_owner(lock); | ||
| 299 | struct rt_mutex_waiter *next; | ||
| 300 | unsigned long flags; | ||
| 301 | |||
| 302 | if (!rt_mutex_owner_pending(lock)) | ||
| 303 | return 0; | ||
| 304 | |||
| 305 | if (pendowner == current) | ||
| 306 | return 1; | ||
| 307 | |||
| 308 | spin_lock_irqsave(&pendowner->pi_lock, flags); | ||
| 309 | if (current->prio >= pendowner->prio) { | ||
| 310 | spin_unlock_irqrestore(&pendowner->pi_lock, flags); | ||
| 311 | return 0; | ||
| 312 | } | ||
| 313 | |||
| 314 | /* | ||
| 315 | * Check if a waiter is enqueued on the pending owners | ||
| 316 | * pi_waiters list. Remove it and readjust pending owners | ||
| 317 | * priority. | ||
| 318 | */ | ||
| 319 | if (likely(!rt_mutex_has_waiters(lock))) { | ||
| 320 | spin_unlock_irqrestore(&pendowner->pi_lock, flags); | ||
| 321 | return 1; | ||
| 322 | } | ||
| 323 | |||
| 324 | /* No chain handling, pending owner is not blocked on anything: */ | ||
| 325 | next = rt_mutex_top_waiter(lock); | ||
| 326 | plist_del(&next->pi_list_entry, &pendowner->pi_waiters); | ||
| 327 | __rt_mutex_adjust_prio(pendowner); | ||
| 328 | spin_unlock_irqrestore(&pendowner->pi_lock, flags); | ||
| 329 | |||
| 330 | /* | ||
| 331 | * We are going to steal the lock and a waiter was | ||
| 332 | * enqueued on the pending owners pi_waiters queue. So | ||
| 333 | * we have to enqueue this waiter into | ||
| 334 | * current->pi_waiters list. This covers the case, | ||
| 335 | * where current is boosted because it holds another | ||
| 336 | * lock and gets unboosted because the booster is | ||
| 337 | * interrupted, so we would delay a waiter with higher | ||
| 338 | * priority as current->normal_prio. | ||
| 339 | * | ||
| 340 | * Note: in the rare case of a SCHED_OTHER task changing | ||
| 341 | * its priority and thus stealing the lock, next->task | ||
| 342 | * might be current: | ||
| 343 | */ | ||
| 344 | if (likely(next->task != current)) { | ||
| 345 | spin_lock_irqsave(¤t->pi_lock, flags); | ||
| 346 | plist_add(&next->pi_list_entry, ¤t->pi_waiters); | ||
| 347 | __rt_mutex_adjust_prio(current); | ||
| 348 | spin_unlock_irqrestore(¤t->pi_lock, flags); | ||
| 349 | } | ||
| 350 | return 1; | ||
| 351 | } | ||
| 352 | |||
| 353 | /* | ||
| 354 | * Try to take an rt-mutex | ||
| 355 | * | ||
| 356 | * This fails | ||
| 357 | * - when the lock has a real owner | ||
| 358 | * - when a different pending owner exists and has higher priority than current | ||
| 359 | * | ||
| 360 | * Must be called with lock->wait_lock held. | ||
| 361 | */ | ||
| 362 | static int try_to_take_rt_mutex(struct rt_mutex *lock) | ||
| 363 | { | ||
| 364 | /* | ||
| 365 | * We have to be careful here if the atomic speedups are | ||
| 366 | * enabled, such that, when | ||
| 367 | * - no other waiter is on the lock | ||
| 368 | * - the lock has been released since we did the cmpxchg | ||
| 369 | * the lock can be released or taken while we are doing the | ||
| 370 | * checks and marking the lock with RT_MUTEX_HAS_WAITERS. | ||
| 371 | * | ||
| 372 | * The atomic acquire/release aware variant of | ||
| 373 | * mark_rt_mutex_waiters uses a cmpxchg loop. After setting | ||
| 374 | * the WAITERS bit, the atomic release / acquire can not | ||
| 375 | * happen anymore and lock->wait_lock protects us from the | ||
| 376 | * non-atomic case. | ||
| 377 | * | ||
| 378 | * Note, that this might set lock->owner = | ||
| 379 | * RT_MUTEX_HAS_WAITERS in the case the lock is not contended | ||
| 380 | * any more. This is fixed up when we take the ownership. | ||
| 381 | * This is the transitional state explained at the top of this file. | ||
| 382 | */ | ||
| 383 | mark_rt_mutex_waiters(lock); | ||
| 384 | |||
| 385 | if (rt_mutex_owner(lock) && !try_to_steal_lock(lock)) | ||
| 386 | return 0; | ||
| 387 | |||
| 388 | /* We got the lock. */ | ||
| 389 | debug_rt_mutex_lock(lock); | ||
| 390 | |||
| 391 | rt_mutex_set_owner(lock, current, 0); | ||
| 392 | |||
| 393 | rt_mutex_deadlock_account_lock(lock, current); | ||
| 394 | |||
| 395 | return 1; | ||
| 396 | } | ||
| 397 | |||
| 398 | /* | ||
| 399 | * Task blocks on lock. | ||
| 400 | * | ||
| 401 | * Prepare waiter and propagate pi chain | ||
| 402 | * | ||
| 403 | * This must be called with lock->wait_lock held. | ||
| 404 | */ | ||
| 405 | static int task_blocks_on_rt_mutex(struct rt_mutex *lock, | ||
| 406 | struct rt_mutex_waiter *waiter, | ||
| 407 | int detect_deadlock) | ||
| 408 | { | ||
| 409 | struct task_struct *owner = rt_mutex_owner(lock); | ||
| 410 | struct rt_mutex_waiter *top_waiter = waiter; | ||
| 411 | unsigned long flags; | ||
| 412 | int chain_walk = 0, res; | ||
| 413 | |||
| 414 | spin_lock_irqsave(¤t->pi_lock, flags); | ||
| 415 | __rt_mutex_adjust_prio(current); | ||
| 416 | waiter->task = current; | ||
| 417 | waiter->lock = lock; | ||
| 418 | plist_node_init(&waiter->list_entry, current->prio); | ||
| 419 | plist_node_init(&waiter->pi_list_entry, current->prio); | ||
| 420 | |||
| 421 | /* Get the top priority waiter on the lock */ | ||
| 422 | if (rt_mutex_has_waiters(lock)) | ||
| 423 | top_waiter = rt_mutex_top_waiter(lock); | ||
| 424 | plist_add(&waiter->list_entry, &lock->wait_list); | ||
| 425 | |||
| 426 | current->pi_blocked_on = waiter; | ||
| 427 | |||
| 428 | spin_unlock_irqrestore(¤t->pi_lock, flags); | ||
| 429 | |||
| 430 | if (waiter == rt_mutex_top_waiter(lock)) { | ||
| 431 | spin_lock_irqsave(&owner->pi_lock, flags); | ||
| 432 | plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters); | ||
| 433 | plist_add(&waiter->pi_list_entry, &owner->pi_waiters); | ||
| 434 | |||
| 435 | __rt_mutex_adjust_prio(owner); | ||
| 436 | if (owner->pi_blocked_on) | ||
| 437 | chain_walk = 1; | ||
| 438 | spin_unlock_irqrestore(&owner->pi_lock, flags); | ||
| 439 | } | ||
| 440 | else if (debug_rt_mutex_detect_deadlock(waiter, detect_deadlock)) | ||
| 441 | chain_walk = 1; | ||
| 442 | |||
| 443 | if (!chain_walk) | ||
| 444 | return 0; | ||
| 445 | |||
| 446 | /* | ||
| 447 | * The owner can't disappear while holding a lock, | ||
| 448 | * so the owner struct is protected by wait_lock. | ||
| 449 | * Gets dropped in rt_mutex_adjust_prio_chain()! | ||
| 450 | */ | ||
| 451 | get_task_struct(owner); | ||
| 452 | |||
| 453 | spin_unlock(&lock->wait_lock); | ||
| 454 | |||
| 455 | res = rt_mutex_adjust_prio_chain(owner, detect_deadlock, lock, waiter, | ||
| 456 | current); | ||
| 457 | |||
| 458 | spin_lock(&lock->wait_lock); | ||
| 459 | |||
| 460 | return res; | ||
| 461 | } | ||
| 462 | |||
| 463 | /* | ||
| 464 | * Wake up the next waiter on the lock. | ||
| 465 | * | ||
| 466 | * Remove the top waiter from the current tasks waiter list and from | ||
| 467 | * the lock waiter list. Set it as pending owner. Then wake it up. | ||
| 468 | * | ||
| 469 | * Called with lock->wait_lock held. | ||
| 470 | */ | ||
| 471 | static void wakeup_next_waiter(struct rt_mutex *lock) | ||
| 472 | { | ||
| 473 | struct rt_mutex_waiter *waiter; | ||
| 474 | struct task_struct *pendowner; | ||
| 475 | unsigned long flags; | ||
| 476 | |||
| 477 | spin_lock_irqsave(¤t->pi_lock, flags); | ||
| 478 | |||
| 479 | waiter = rt_mutex_top_waiter(lock); | ||
| 480 | plist_del(&waiter->list_entry, &lock->wait_list); | ||
| 481 | |||
| 482 | /* | ||
| 483 | * Remove it from current->pi_waiters. We do not adjust a | ||
| 484 | * possible priority boost right now. We execute wakeup in the | ||
| 485 | * boosted mode and go back to normal after releasing | ||
| 486 | * lock->wait_lock. | ||
| 487 | */ | ||
| 488 | plist_del(&waiter->pi_list_entry, ¤t->pi_waiters); | ||
| 489 | pendowner = waiter->task; | ||
| 490 | waiter->task = NULL; | ||
| 491 | |||
| 492 | rt_mutex_set_owner(lock, pendowner, RT_MUTEX_OWNER_PENDING); | ||
| 493 | |||
| 494 | spin_unlock_irqrestore(¤t->pi_lock, flags); | ||
| 495 | |||
| 496 | /* | ||
| 497 | * Clear the pi_blocked_on variable and enqueue a possible | ||
| 498 | * waiter into the pi_waiters list of the pending owner. This | ||
| 499 | * prevents that in case the pending owner gets unboosted a | ||
| 500 | * waiter with higher priority than pending-owner->normal_prio | ||
| 501 | * is blocked on the unboosted (pending) owner. | ||
| 502 | */ | ||
| 503 | spin_lock_irqsave(&pendowner->pi_lock, flags); | ||
| 504 | |||
| 505 | WARN_ON(!pendowner->pi_blocked_on); | ||
| 506 | WARN_ON(pendowner->pi_blocked_on != waiter); | ||
| 507 | WARN_ON(pendowner->pi_blocked_on->lock != lock); | ||
| 508 | |||
| 509 | pendowner->pi_blocked_on = NULL; | ||
| 510 | |||
| 511 | if (rt_mutex_has_waiters(lock)) { | ||
| 512 | struct rt_mutex_waiter *next; | ||
| 513 | |||
| 514 | next = rt_mutex_top_waiter(lock); | ||
| 515 | plist_add(&next->pi_list_entry, &pendowner->pi_waiters); | ||
| 516 | } | ||
| 517 | spin_unlock_irqrestore(&pendowner->pi_lock, flags); | ||
| 518 | |||
| 519 | wake_up_process(pendowner); | ||
| 520 | } | ||
| 521 | |||
| 522 | /* | ||
| 523 | * Remove a waiter from a lock | ||
| 524 | * | ||
| 525 | * Must be called with lock->wait_lock held | ||
| 526 | */ | ||
| 527 | static void remove_waiter(struct rt_mutex *lock, | ||
| 528 | struct rt_mutex_waiter *waiter) | ||
| 529 | { | ||
| 530 | int first = (waiter == rt_mutex_top_waiter(lock)); | ||
| 531 | struct task_struct *owner = rt_mutex_owner(lock); | ||
| 532 | unsigned long flags; | ||
| 533 | int chain_walk = 0; | ||
| 534 | |||
| 535 | spin_lock_irqsave(¤t->pi_lock, flags); | ||
| 536 | plist_del(&waiter->list_entry, &lock->wait_list); | ||
| 537 | waiter->task = NULL; | ||
| 538 | current->pi_blocked_on = NULL; | ||
| 539 | spin_unlock_irqrestore(¤t->pi_lock, flags); | ||
| 540 | |||
| 541 | if (first && owner != current) { | ||
| 542 | |||
| 543 | spin_lock_irqsave(&owner->pi_lock, flags); | ||
| 544 | |||
| 545 | plist_del(&waiter->pi_list_entry, &owner->pi_waiters); | ||
| 546 | |||
| 547 | if (rt_mutex_has_waiters(lock)) { | ||
| 548 | struct rt_mutex_waiter *next; | ||
| 549 | |||
| 550 | next = rt_mutex_top_waiter(lock); | ||
| 551 | plist_add(&next->pi_list_entry, &owner->pi_waiters); | ||
| 552 | } | ||
| 553 | __rt_mutex_adjust_prio(owner); | ||
| 554 | |||
| 555 | if (owner->pi_blocked_on) | ||
| 556 | chain_walk = 1; | ||
| 557 | |||
| 558 | spin_unlock_irqrestore(&owner->pi_lock, flags); | ||
| 559 | } | ||
| 560 | |||
| 561 | WARN_ON(!plist_node_empty(&waiter->pi_list_entry)); | ||
| 562 | |||
| 563 | if (!chain_walk) | ||
| 564 | return; | ||
| 565 | |||
| 566 | /* gets dropped in rt_mutex_adjust_prio_chain()! */ | ||
| 567 | get_task_struct(owner); | ||
| 568 | |||
| 569 | spin_unlock(&lock->wait_lock); | ||
| 570 | |||
| 571 | rt_mutex_adjust_prio_chain(owner, 0, lock, NULL, current); | ||
| 572 | |||
| 573 | spin_lock(&lock->wait_lock); | ||
| 574 | } | ||
| 575 | |||
| 576 | /* | ||
| 577 | * Recheck the pi chain, in case we got a priority setting | ||
| 578 | * | ||
| 579 | * Called from sched_setscheduler | ||
| 580 | */ | ||
| 581 | void rt_mutex_adjust_pi(struct task_struct *task) | ||
| 582 | { | ||
| 583 | struct rt_mutex_waiter *waiter; | ||
| 584 | unsigned long flags; | ||
| 585 | |||
| 586 | spin_lock_irqsave(&task->pi_lock, flags); | ||
| 587 | |||
| 588 | waiter = task->pi_blocked_on; | ||
| 589 | if (!waiter || waiter->list_entry.prio == task->prio) { | ||
| 590 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 591 | return; | ||
| 592 | } | ||
| 593 | |||
| 594 | spin_unlock_irqrestore(&task->pi_lock, flags); | ||
| 595 | |||
| 596 | /* gets dropped in rt_mutex_adjust_prio_chain()! */ | ||
| 597 | get_task_struct(task); | ||
| 598 | rt_mutex_adjust_prio_chain(task, 0, NULL, NULL, task); | ||
| 599 | } | ||
| 600 | |||
| 601 | /* | ||
| 602 | * Slow path lock function: | ||
| 603 | */ | ||
| 604 | static int __sched | ||
| 605 | rt_mutex_slowlock(struct rt_mutex *lock, int state, | ||
| 606 | struct hrtimer_sleeper *timeout, | ||
| 607 | int detect_deadlock) | ||
| 608 | { | ||
| 609 | struct rt_mutex_waiter waiter; | ||
| 610 | int ret = 0; | ||
| 611 | |||
| 612 | debug_rt_mutex_init_waiter(&waiter); | ||
| 613 | waiter.task = NULL; | ||
| 614 | |||
| 615 | spin_lock(&lock->wait_lock); | ||
| 616 | |||
| 617 | /* Try to acquire the lock again: */ | ||
| 618 | if (try_to_take_rt_mutex(lock)) { | ||
| 619 | spin_unlock(&lock->wait_lock); | ||
| 620 | return 0; | ||
| 621 | } | ||
| 622 | |||
| 623 | set_current_state(state); | ||
| 624 | |||
| 625 | /* Setup the timer, when timeout != NULL */ | ||
| 626 | if (unlikely(timeout)) | ||
| 627 | hrtimer_start(&timeout->timer, timeout->timer.expires, | ||
| 628 | HRTIMER_ABS); | ||
| 629 | |||
| 630 | for (;;) { | ||
| 631 | /* Try to acquire the lock: */ | ||
| 632 | if (try_to_take_rt_mutex(lock)) | ||
| 633 | break; | ||
| 634 | |||
| 635 | /* | ||
| 636 | * TASK_INTERRUPTIBLE checks for signals and | ||
| 637 | * timeout. Ignored otherwise. | ||
| 638 | */ | ||
| 639 | if (unlikely(state == TASK_INTERRUPTIBLE)) { | ||
| 640 | /* Signal pending? */ | ||
| 641 | if (signal_pending(current)) | ||
| 642 | ret = -EINTR; | ||
| 643 | if (timeout && !timeout->task) | ||
| 644 | ret = -ETIMEDOUT; | ||
| 645 | if (ret) | ||
| 646 | break; | ||
| 647 | } | ||
| 648 | |||
| 649 | /* | ||
| 650 | * waiter.task is NULL the first time we come here and | ||
| 651 | * when we have been woken up by the previous owner | ||
| 652 | * but the lock got stolen by a higher prio task. | ||
| 653 | */ | ||
| 654 | if (!waiter.task) { | ||
| 655 | ret = task_blocks_on_rt_mutex(lock, &waiter, | ||
| 656 | detect_deadlock); | ||
| 657 | /* | ||
| 658 | * If we got woken up by the owner then start loop | ||
| 659 | * all over without going into schedule to try | ||
| 660 | * to get the lock now: | ||
| 661 | */ | ||
| 662 | if (unlikely(!waiter.task)) | ||
| 663 | continue; | ||
| 664 | |||
| 665 | if (unlikely(ret)) | ||
| 666 | break; | ||
| 667 | } | ||
| 668 | |||
| 669 | spin_unlock(&lock->wait_lock); | ||
| 670 | |||
| 671 | debug_rt_mutex_print_deadlock(&waiter); | ||
| 672 | |||
| 673 | if (waiter.task) | ||
| 674 | schedule_rt_mutex(lock); | ||
| 675 | |||
| 676 | spin_lock(&lock->wait_lock); | ||
| 677 | set_current_state(state); | ||
| 678 | } | ||
| 679 | |||
| 680 | set_current_state(TASK_RUNNING); | ||
| 681 | |||
| 682 | if (unlikely(waiter.task)) | ||
| 683 | remove_waiter(lock, &waiter); | ||
| 684 | |||
| 685 | /* | ||
| 686 | * try_to_take_rt_mutex() sets the waiter bit | ||
| 687 | * unconditionally. We might have to fix that up. | ||
| 688 | */ | ||
| 689 | fixup_rt_mutex_waiters(lock); | ||
| 690 | |||
| 691 | spin_unlock(&lock->wait_lock); | ||
| 692 | |||
| 693 | /* Remove pending timer: */ | ||
| 694 | if (unlikely(timeout)) | ||
| 695 | hrtimer_cancel(&timeout->timer); | ||
| 696 | |||
| 697 | /* | ||
| 698 | * Readjust priority, when we did not get the lock. We might | ||
| 699 | * have been the pending owner and boosted. Since we did not | ||
| 700 | * take the lock, the PI boost has to go. | ||
| 701 | */ | ||
| 702 | if (unlikely(ret)) | ||
| 703 | rt_mutex_adjust_prio(current); | ||
| 704 | |||
| 705 | debug_rt_mutex_free_waiter(&waiter); | ||
| 706 | |||
| 707 | return ret; | ||
| 708 | } | ||
| 709 | |||
| 710 | /* | ||
| 711 | * Slow path try-lock function: | ||
| 712 | */ | ||
| 713 | static inline int | ||
| 714 | rt_mutex_slowtrylock(struct rt_mutex *lock) | ||
| 715 | { | ||
| 716 | int ret = 0; | ||
| 717 | |||
| 718 | spin_lock(&lock->wait_lock); | ||
| 719 | |||
| 720 | if (likely(rt_mutex_owner(lock) != current)) { | ||
| 721 | |||
| 722 | ret = try_to_take_rt_mutex(lock); | ||
| 723 | /* | ||
| 724 | * try_to_take_rt_mutex() sets the lock waiters | ||
| 725 | * bit unconditionally. Clean this up. | ||
| 726 | */ | ||
| 727 | fixup_rt_mutex_waiters(lock); | ||
| 728 | } | ||
| 729 | |||
| 730 | spin_unlock(&lock->wait_lock); | ||
| 731 | |||
| 732 | return ret; | ||
| 733 | } | ||
| 734 | |||
| 735 | /* | ||
| 736 | * Slow path to release a rt-mutex: | ||
| 737 | */ | ||
| 738 | static void __sched | ||
| 739 | rt_mutex_slowunlock(struct rt_mutex *lock) | ||
| 740 | { | ||
| 741 | spin_lock(&lock->wait_lock); | ||
| 742 | |||
| 743 | debug_rt_mutex_unlock(lock); | ||
| 744 | |||
| 745 | rt_mutex_deadlock_account_unlock(current); | ||
| 746 | |||
| 747 | if (!rt_mutex_has_waiters(lock)) { | ||
| 748 | lock->owner = NULL; | ||
| 749 | spin_unlock(&lock->wait_lock); | ||
| 750 | return; | ||
| 751 | } | ||
| 752 | |||
| 753 | wakeup_next_waiter(lock); | ||
| 754 | |||
| 755 | spin_unlock(&lock->wait_lock); | ||
| 756 | |||
| 757 | /* Undo pi boosting if necessary: */ | ||
| 758 | rt_mutex_adjust_prio(current); | ||
| 759 | } | ||
| 760 | |||
| 761 | /* | ||
| 762 | * debug aware fast / slowpath lock,trylock,unlock | ||
| 763 | * | ||
| 764 | * The atomic acquire/release ops are compiled away, when either the | ||
| 765 | * architecture does not support cmpxchg or when debugging is enabled. | ||
| 766 | */ | ||
| 767 | static inline int | ||
| 768 | rt_mutex_fastlock(struct rt_mutex *lock, int state, | ||
| 769 | int detect_deadlock, | ||
| 770 | int (*slowfn)(struct rt_mutex *lock, int state, | ||
| 771 | struct hrtimer_sleeper *timeout, | ||
| 772 | int detect_deadlock)) | ||
| 773 | { | ||
| 774 | if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) { | ||
| 775 | rt_mutex_deadlock_account_lock(lock, current); | ||
| 776 | return 0; | ||
| 777 | } else | ||
| 778 | return slowfn(lock, state, NULL, detect_deadlock); | ||
| 779 | } | ||
| 780 | |||
| 781 | static inline int | ||
| 782 | rt_mutex_timed_fastlock(struct rt_mutex *lock, int state, | ||
| 783 | struct hrtimer_sleeper *timeout, int detect_deadlock, | ||
| 784 | int (*slowfn)(struct rt_mutex *lock, int state, | ||
| 785 | struct hrtimer_sleeper *timeout, | ||
| 786 | int detect_deadlock)) | ||
| 787 | { | ||
| 788 | if (!detect_deadlock && likely(rt_mutex_cmpxchg(lock, NULL, current))) { | ||
| 789 | rt_mutex_deadlock_account_lock(lock, current); | ||
| 790 | return 0; | ||
| 791 | } else | ||
| 792 | return slowfn(lock, state, timeout, detect_deadlock); | ||
| 793 | } | ||
| 794 | |||
| 795 | static inline int | ||
| 796 | rt_mutex_fasttrylock(struct rt_mutex *lock, | ||
| 797 | int (*slowfn)(struct rt_mutex *lock)) | ||
| 798 | { | ||
| 799 | if (likely(rt_mutex_cmpxchg(lock, NULL, current))) { | ||
| 800 | rt_mutex_deadlock_account_lock(lock, current); | ||
| 801 | return 1; | ||
| 802 | } | ||
| 803 | return slowfn(lock); | ||
| 804 | } | ||
| 805 | |||
| 806 | static inline void | ||
| 807 | rt_mutex_fastunlock(struct rt_mutex *lock, | ||
| 808 | void (*slowfn)(struct rt_mutex *lock)) | ||
| 809 | { | ||
| 810 | if (likely(rt_mutex_cmpxchg(lock, current, NULL))) | ||
| 811 | rt_mutex_deadlock_account_unlock(current); | ||
| 812 | else | ||
| 813 | slowfn(lock); | ||
| 814 | } | ||
| 815 | |||
| 816 | /** | ||
| 817 | * rt_mutex_lock - lock a rt_mutex | ||
| 818 | * | ||
| 819 | * @lock: the rt_mutex to be locked | ||
| 820 | */ | ||
| 821 | void __sched rt_mutex_lock(struct rt_mutex *lock) | ||
| 822 | { | ||
| 823 | might_sleep(); | ||
| 824 | |||
| 825 | rt_mutex_fastlock(lock, TASK_UNINTERRUPTIBLE, 0, rt_mutex_slowlock); | ||
| 826 | } | ||
| 827 | EXPORT_SYMBOL_GPL(rt_mutex_lock); | ||
| 828 | |||
| 829 | /** | ||
| 830 | * rt_mutex_lock_interruptible - lock a rt_mutex interruptible | ||
| 831 | * | ||
| 832 | * @lock: the rt_mutex to be locked | ||
| 833 | * @detect_deadlock: deadlock detection on/off | ||
| 834 | * | ||
| 835 | * Returns: | ||
| 836 | * 0 on success | ||
| 837 | * -EINTR when interrupted by a signal | ||
| 838 | * -EDEADLK when the lock would deadlock (when deadlock detection is on) | ||
| 839 | */ | ||
| 840 | int __sched rt_mutex_lock_interruptible(struct rt_mutex *lock, | ||
| 841 | int detect_deadlock) | ||
| 842 | { | ||
| 843 | might_sleep(); | ||
| 844 | |||
| 845 | return rt_mutex_fastlock(lock, TASK_INTERRUPTIBLE, | ||
| 846 | detect_deadlock, rt_mutex_slowlock); | ||
| 847 | } | ||
| 848 | EXPORT_SYMBOL_GPL(rt_mutex_lock_interruptible); | ||
| 849 | |||
| 850 | /** | ||
| 851 | * rt_mutex_lock_interruptible_ktime - lock a rt_mutex interruptible | ||
| 852 | * the timeout structure is provided | ||
| 853 | * by the caller | ||
| 854 | * | ||
| 855 | * @lock: the rt_mutex to be locked | ||
| 856 | * @timeout: timeout structure or NULL (no timeout) | ||
| 857 | * @detect_deadlock: deadlock detection on/off | ||
| 858 | * | ||
| 859 | * Returns: | ||
| 860 | * 0 on success | ||
| 861 | * -EINTR when interrupted by a signal | ||
| 862 | * -ETIMEOUT when the timeout expired | ||
| 863 | * -EDEADLK when the lock would deadlock (when deadlock detection is on) | ||
| 864 | */ | ||
| 865 | int | ||
| 866 | rt_mutex_timed_lock(struct rt_mutex *lock, struct hrtimer_sleeper *timeout, | ||
| 867 | int detect_deadlock) | ||
| 868 | { | ||
| 869 | might_sleep(); | ||
| 870 | |||
| 871 | return rt_mutex_timed_fastlock(lock, TASK_INTERRUPTIBLE, timeout, | ||
| 872 | detect_deadlock, rt_mutex_slowlock); | ||
| 873 | } | ||
| 874 | EXPORT_SYMBOL_GPL(rt_mutex_timed_lock); | ||
| 875 | |||
| 876 | /** | ||
| 877 | * rt_mutex_trylock - try to lock a rt_mutex | ||
| 878 | * | ||
| 879 | * @lock: the rt_mutex to be locked | ||
| 880 | * | ||
| 881 | * Returns 1 on success and 0 on contention | ||
| 882 | */ | ||
| 883 | int __sched rt_mutex_trylock(struct rt_mutex *lock) | ||
| 884 | { | ||
| 885 | return rt_mutex_fasttrylock(lock, rt_mutex_slowtrylock); | ||
| 886 | } | ||
| 887 | EXPORT_SYMBOL_GPL(rt_mutex_trylock); | ||
| 888 | |||
| 889 | /** | ||
| 890 | * rt_mutex_unlock - unlock a rt_mutex | ||
| 891 | * | ||
| 892 | * @lock: the rt_mutex to be unlocked | ||
| 893 | */ | ||
| 894 | void __sched rt_mutex_unlock(struct rt_mutex *lock) | ||
| 895 | { | ||
| 896 | rt_mutex_fastunlock(lock, rt_mutex_slowunlock); | ||
| 897 | } | ||
| 898 | EXPORT_SYMBOL_GPL(rt_mutex_unlock); | ||
| 899 | |||
| 900 | /*** | ||
| 901 | * rt_mutex_destroy - mark a mutex unusable | ||
| 902 | * @lock: the mutex to be destroyed | ||
| 903 | * | ||
| 904 | * This function marks the mutex uninitialized, and any subsequent | ||
| 905 | * use of the mutex is forbidden. The mutex must not be locked when | ||
| 906 | * this function is called. | ||
| 907 | */ | ||
| 908 | void rt_mutex_destroy(struct rt_mutex *lock) | ||
| 909 | { | ||
| 910 | WARN_ON(rt_mutex_is_locked(lock)); | ||
| 911 | #ifdef CONFIG_DEBUG_RT_MUTEXES | ||
| 912 | lock->magic = NULL; | ||
| 913 | #endif | ||
| 914 | } | ||
| 915 | |||
| 916 | EXPORT_SYMBOL_GPL(rt_mutex_destroy); | ||
| 917 | |||
| 918 | /** | ||
| 919 | * __rt_mutex_init - initialize the rt lock | ||
| 920 | * | ||
| 921 | * @lock: the rt lock to be initialized | ||
| 922 | * | ||
| 923 | * Initialize the rt lock to unlocked state. | ||
| 924 | * | ||
| 925 | * Initializing of a locked rt lock is not allowed | ||
| 926 | */ | ||
| 927 | void __rt_mutex_init(struct rt_mutex *lock, const char *name) | ||
| 928 | { | ||
| 929 | lock->owner = NULL; | ||
| 930 | spin_lock_init(&lock->wait_lock); | ||
| 931 | plist_head_init(&lock->wait_list, &lock->wait_lock); | ||
| 932 | |||
| 933 | debug_rt_mutex_init(lock, name); | ||
| 934 | } | ||
| 935 | EXPORT_SYMBOL_GPL(__rt_mutex_init); | ||
| 936 | |||
| 937 | /** | ||
| 938 | * rt_mutex_init_proxy_locked - initialize and lock a rt_mutex on behalf of a | ||
| 939 | * proxy owner | ||
| 940 | * | ||
| 941 | * @lock: the rt_mutex to be locked | ||
| 942 | * @proxy_owner:the task to set as owner | ||
| 943 | * | ||
| 944 | * No locking. Caller has to do serializing itself | ||
| 945 | * Special API call for PI-futex support | ||
| 946 | */ | ||
| 947 | void rt_mutex_init_proxy_locked(struct rt_mutex *lock, | ||
| 948 | struct task_struct *proxy_owner) | ||
| 949 | { | ||
| 950 | __rt_mutex_init(lock, NULL); | ||
| 951 | debug_rt_mutex_proxy_lock(lock, proxy_owner); | ||
| 952 | rt_mutex_set_owner(lock, proxy_owner, 0); | ||
| 953 | rt_mutex_deadlock_account_lock(lock, proxy_owner); | ||
| 954 | } | ||
| 955 | |||
| 956 | /** | ||
| 957 | * rt_mutex_proxy_unlock - release a lock on behalf of owner | ||
| 958 | * | ||
| 959 | * @lock: the rt_mutex to be locked | ||
| 960 | * | ||
| 961 | * No locking. Caller has to do serializing itself | ||
| 962 | * Special API call for PI-futex support | ||
| 963 | */ | ||
| 964 | void rt_mutex_proxy_unlock(struct rt_mutex *lock, | ||
| 965 | struct task_struct *proxy_owner) | ||
| 966 | { | ||
| 967 | debug_rt_mutex_proxy_unlock(lock); | ||
| 968 | rt_mutex_set_owner(lock, NULL, 0); | ||
| 969 | rt_mutex_deadlock_account_unlock(proxy_owner); | ||
| 970 | } | ||
| 971 | |||
| 972 | /** | ||
| 973 | * rt_mutex_next_owner - return the next owner of the lock | ||
| 974 | * | ||
| 975 | * @lock: the rt lock query | ||
| 976 | * | ||
| 977 | * Returns the next owner of the lock or NULL | ||
| 978 | * | ||
| 979 | * Caller has to serialize against other accessors to the lock | ||
| 980 | * itself. | ||
| 981 | * | ||
| 982 | * Special API call for PI-futex support | ||
| 983 | */ | ||
| 984 | struct task_struct *rt_mutex_next_owner(struct rt_mutex *lock) | ||
| 985 | { | ||
| 986 | if (!rt_mutex_has_waiters(lock)) | ||
| 987 | return NULL; | ||
| 988 | |||
| 989 | return rt_mutex_top_waiter(lock)->task; | ||
| 990 | } | ||
