101768b42SPeter Zijlstra /* 267a6de49SPeter Zijlstra * kernel/locking/mutex.c 301768b42SPeter Zijlstra * 401768b42SPeter Zijlstra * Mutexes: blocking mutual exclusion locks 501768b42SPeter Zijlstra * 601768b42SPeter Zijlstra * Started by Ingo Molnar: 701768b42SPeter Zijlstra * 801768b42SPeter Zijlstra * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 901768b42SPeter Zijlstra * 1001768b42SPeter Zijlstra * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and 1101768b42SPeter Zijlstra * David Howells for suggestions and improvements. 1201768b42SPeter Zijlstra * 1301768b42SPeter Zijlstra * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline 1401768b42SPeter Zijlstra * from the -rt tree, where it was originally implemented for rtmutexes 1501768b42SPeter Zijlstra * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale 1601768b42SPeter Zijlstra * and Sven Dietrich. 1701768b42SPeter Zijlstra * 18214e0aedSDavidlohr Bueso * Also see Documentation/locking/mutex-design.txt. 1901768b42SPeter Zijlstra */ 2001768b42SPeter Zijlstra #include <linux/mutex.h> 2101768b42SPeter Zijlstra #include <linux/ww_mutex.h> 2201768b42SPeter Zijlstra #include <linux/sched.h> 2301768b42SPeter Zijlstra #include <linux/sched/rt.h> 2401768b42SPeter Zijlstra #include <linux/export.h> 2501768b42SPeter Zijlstra #include <linux/spinlock.h> 2601768b42SPeter Zijlstra #include <linux/interrupt.h> 2701768b42SPeter Zijlstra #include <linux/debug_locks.h> 287a215f89SDavidlohr Bueso #include <linux/osq_lock.h> 2901768b42SPeter Zijlstra 3001768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 3101768b42SPeter Zijlstra # include "mutex-debug.h" 3201768b42SPeter Zijlstra #else 3301768b42SPeter Zijlstra # include "mutex.h" 3401768b42SPeter Zijlstra #endif 3501768b42SPeter Zijlstra 3601768b42SPeter Zijlstra void 3701768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 3801768b42SPeter Zijlstra { 393ca0ff57SPeter Zijlstra atomic_long_set(&lock->owner, 0); 4001768b42SPeter Zijlstra spin_lock_init(&lock->wait_lock); 4101768b42SPeter Zijlstra INIT_LIST_HEAD(&lock->wait_list); 4201768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 434d9d951eSJason Low osq_lock_init(&lock->osq); 4401768b42SPeter Zijlstra #endif 4501768b42SPeter Zijlstra 4601768b42SPeter Zijlstra debug_mutex_init(lock, name, key); 4701768b42SPeter Zijlstra } 4801768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init); 4901768b42SPeter Zijlstra 503ca0ff57SPeter Zijlstra /* 513ca0ff57SPeter Zijlstra * @owner: contains: 'struct task_struct *' to the current lock owner, 523ca0ff57SPeter Zijlstra * NULL means not owned. Since task_struct pointers are aligned at 533ca0ff57SPeter Zijlstra * ARCH_MIN_TASKALIGN (which is at least sizeof(void *)), we have low 543ca0ff57SPeter Zijlstra * bits to store extra state. 553ca0ff57SPeter Zijlstra * 563ca0ff57SPeter Zijlstra * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. 57*9d659ae1SPeter Zijlstra * Bit1 indicates unlock needs to hand the lock to the top-waiter 583ca0ff57SPeter Zijlstra */ 593ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS 0x01 60*9d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF 0x02 613ca0ff57SPeter Zijlstra 623ca0ff57SPeter Zijlstra #define MUTEX_FLAGS 0x03 633ca0ff57SPeter Zijlstra 643ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner) 653ca0ff57SPeter Zijlstra { 663ca0ff57SPeter Zijlstra return (struct task_struct *)(owner & ~MUTEX_FLAGS); 673ca0ff57SPeter Zijlstra } 683ca0ff57SPeter Zijlstra 693ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner) 703ca0ff57SPeter Zijlstra { 713ca0ff57SPeter Zijlstra return owner & MUTEX_FLAGS; 723ca0ff57SPeter Zijlstra } 733ca0ff57SPeter Zijlstra 743ca0ff57SPeter Zijlstra /* 753ca0ff57SPeter Zijlstra * Actual trylock that will work on any unlocked state. 76*9d659ae1SPeter Zijlstra * 77*9d659ae1SPeter Zijlstra * When setting the owner field, we must preserve the low flag bits. 78*9d659ae1SPeter Zijlstra * 79*9d659ae1SPeter Zijlstra * Be careful with @handoff, only set that in a wait-loop (where you set 80*9d659ae1SPeter Zijlstra * HANDOFF) to avoid recursive lock attempts. 813ca0ff57SPeter Zijlstra */ 82*9d659ae1SPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock, const bool handoff) 833ca0ff57SPeter Zijlstra { 843ca0ff57SPeter Zijlstra unsigned long owner, curr = (unsigned long)current; 853ca0ff57SPeter Zijlstra 863ca0ff57SPeter Zijlstra owner = atomic_long_read(&lock->owner); 873ca0ff57SPeter Zijlstra for (;;) { /* must loop, can race against a flag */ 88*9d659ae1SPeter Zijlstra unsigned long old, flags = __owner_flags(owner); 893ca0ff57SPeter Zijlstra 90*9d659ae1SPeter Zijlstra if (__owner_task(owner)) { 91*9d659ae1SPeter Zijlstra if (handoff && unlikely(__owner_task(owner) == current)) { 92*9d659ae1SPeter Zijlstra /* 93*9d659ae1SPeter Zijlstra * Provide ACQUIRE semantics for the lock-handoff. 94*9d659ae1SPeter Zijlstra * 95*9d659ae1SPeter Zijlstra * We cannot easily use load-acquire here, since 96*9d659ae1SPeter Zijlstra * the actual load is a failed cmpxchg, which 97*9d659ae1SPeter Zijlstra * doesn't imply any barriers. 98*9d659ae1SPeter Zijlstra * 99*9d659ae1SPeter Zijlstra * Also, this is a fairly unlikely scenario, and 100*9d659ae1SPeter Zijlstra * this contains the cost. 101*9d659ae1SPeter Zijlstra */ 102*9d659ae1SPeter Zijlstra smp_mb(); /* ACQUIRE */ 103*9d659ae1SPeter Zijlstra return true; 104*9d659ae1SPeter Zijlstra } 105*9d659ae1SPeter Zijlstra 1063ca0ff57SPeter Zijlstra return false; 107*9d659ae1SPeter Zijlstra } 1083ca0ff57SPeter Zijlstra 109*9d659ae1SPeter Zijlstra /* 110*9d659ae1SPeter Zijlstra * We set the HANDOFF bit, we must make sure it doesn't live 111*9d659ae1SPeter Zijlstra * past the point where we acquire it. This would be possible 112*9d659ae1SPeter Zijlstra * if we (accidentally) set the bit on an unlocked mutex. 113*9d659ae1SPeter Zijlstra */ 114*9d659ae1SPeter Zijlstra if (handoff) 115*9d659ae1SPeter Zijlstra flags &= ~MUTEX_FLAG_HANDOFF; 116*9d659ae1SPeter Zijlstra 117*9d659ae1SPeter Zijlstra old = atomic_long_cmpxchg_acquire(&lock->owner, owner, curr | flags); 1183ca0ff57SPeter Zijlstra if (old == owner) 1193ca0ff57SPeter Zijlstra return true; 1203ca0ff57SPeter Zijlstra 1213ca0ff57SPeter Zijlstra owner = old; 1223ca0ff57SPeter Zijlstra } 1233ca0ff57SPeter Zijlstra } 1243ca0ff57SPeter Zijlstra 1253ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 1263ca0ff57SPeter Zijlstra /* 1273ca0ff57SPeter Zijlstra * Lockdep annotations are contained to the slow paths for simplicity. 1283ca0ff57SPeter Zijlstra * There is nothing that would stop spreading the lockdep annotations outwards 1293ca0ff57SPeter Zijlstra * except more code. 1303ca0ff57SPeter Zijlstra */ 1313ca0ff57SPeter Zijlstra 1323ca0ff57SPeter Zijlstra /* 1333ca0ff57SPeter Zijlstra * Optimistic trylock that only works in the uncontended case. Make sure to 1343ca0ff57SPeter Zijlstra * follow with a __mutex_trylock() before failing. 1353ca0ff57SPeter Zijlstra */ 1363ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock) 1373ca0ff57SPeter Zijlstra { 1383ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 1393ca0ff57SPeter Zijlstra 1403ca0ff57SPeter Zijlstra if (!atomic_long_cmpxchg_acquire(&lock->owner, 0UL, curr)) 1413ca0ff57SPeter Zijlstra return true; 1423ca0ff57SPeter Zijlstra 1433ca0ff57SPeter Zijlstra return false; 1443ca0ff57SPeter Zijlstra } 1453ca0ff57SPeter Zijlstra 1463ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock) 1473ca0ff57SPeter Zijlstra { 1483ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 1493ca0ff57SPeter Zijlstra 1503ca0ff57SPeter Zijlstra if (atomic_long_cmpxchg_release(&lock->owner, curr, 0UL) == curr) 1513ca0ff57SPeter Zijlstra return true; 1523ca0ff57SPeter Zijlstra 1533ca0ff57SPeter Zijlstra return false; 1543ca0ff57SPeter Zijlstra } 1553ca0ff57SPeter Zijlstra #endif 1563ca0ff57SPeter Zijlstra 1573ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 1583ca0ff57SPeter Zijlstra { 1593ca0ff57SPeter Zijlstra atomic_long_or(flag, &lock->owner); 1603ca0ff57SPeter Zijlstra } 1613ca0ff57SPeter Zijlstra 1623ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) 1633ca0ff57SPeter Zijlstra { 1643ca0ff57SPeter Zijlstra atomic_long_andnot(flag, &lock->owner); 1653ca0ff57SPeter Zijlstra } 1663ca0ff57SPeter Zijlstra 167*9d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) 168*9d659ae1SPeter Zijlstra { 169*9d659ae1SPeter Zijlstra return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; 170*9d659ae1SPeter Zijlstra } 171*9d659ae1SPeter Zijlstra 172*9d659ae1SPeter Zijlstra /* 173*9d659ae1SPeter Zijlstra * Give up ownership to a specific task, when @task = NULL, this is equivalent 174*9d659ae1SPeter Zijlstra * to a regular unlock. Clears HANDOFF, preserves WAITERS. Provides RELEASE 175*9d659ae1SPeter Zijlstra * semantics like a regular unlock, the __mutex_trylock() provides matching 176*9d659ae1SPeter Zijlstra * ACQUIRE semantics for the handoff. 177*9d659ae1SPeter Zijlstra */ 178*9d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task) 179*9d659ae1SPeter Zijlstra { 180*9d659ae1SPeter Zijlstra unsigned long owner = atomic_long_read(&lock->owner); 181*9d659ae1SPeter Zijlstra 182*9d659ae1SPeter Zijlstra for (;;) { 183*9d659ae1SPeter Zijlstra unsigned long old, new; 184*9d659ae1SPeter Zijlstra 185*9d659ae1SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 186*9d659ae1SPeter Zijlstra DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current); 187*9d659ae1SPeter Zijlstra #endif 188*9d659ae1SPeter Zijlstra 189*9d659ae1SPeter Zijlstra new = (owner & MUTEX_FLAG_WAITERS); 190*9d659ae1SPeter Zijlstra new |= (unsigned long)task; 191*9d659ae1SPeter Zijlstra 192*9d659ae1SPeter Zijlstra old = atomic_long_cmpxchg_release(&lock->owner, owner, new); 193*9d659ae1SPeter Zijlstra if (old == owner) 194*9d659ae1SPeter Zijlstra break; 195*9d659ae1SPeter Zijlstra 196*9d659ae1SPeter Zijlstra owner = old; 197*9d659ae1SPeter Zijlstra } 198*9d659ae1SPeter Zijlstra } 199*9d659ae1SPeter Zijlstra 20001768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 20101768b42SPeter Zijlstra /* 20201768b42SPeter Zijlstra * We split the mutex lock/unlock logic into separate fastpath and 20301768b42SPeter Zijlstra * slowpath functions, to reduce the register pressure on the fastpath. 20401768b42SPeter Zijlstra * We also put the fastpath first in the kernel image, to make sure the 20501768b42SPeter Zijlstra * branch is predicted by the CPU as default-untaken. 20601768b42SPeter Zijlstra */ 2073ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock); 20801768b42SPeter Zijlstra 20901768b42SPeter Zijlstra /** 21001768b42SPeter Zijlstra * mutex_lock - acquire the mutex 21101768b42SPeter Zijlstra * @lock: the mutex to be acquired 21201768b42SPeter Zijlstra * 21301768b42SPeter Zijlstra * Lock the mutex exclusively for this task. If the mutex is not 21401768b42SPeter Zijlstra * available right now, it will sleep until it can get it. 21501768b42SPeter Zijlstra * 21601768b42SPeter Zijlstra * The mutex must later on be released by the same task that 21701768b42SPeter Zijlstra * acquired it. Recursive locking is not allowed. The task 21801768b42SPeter Zijlstra * may not exit without first unlocking the mutex. Also, kernel 219139b6fd2SSharon Dvir * memory where the mutex resides must not be freed with 22001768b42SPeter Zijlstra * the mutex still locked. The mutex must first be initialized 22101768b42SPeter Zijlstra * (or statically defined) before it can be locked. memset()-ing 22201768b42SPeter Zijlstra * the mutex to 0 is not allowed. 22301768b42SPeter Zijlstra * 22401768b42SPeter Zijlstra * ( The CONFIG_DEBUG_MUTEXES .config option turns on debugging 22501768b42SPeter Zijlstra * checks that will enforce the restrictions and will also do 22601768b42SPeter Zijlstra * deadlock debugging. ) 22701768b42SPeter Zijlstra * 22801768b42SPeter Zijlstra * This function is similar to (but not equivalent to) down(). 22901768b42SPeter Zijlstra */ 23001768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock) 23101768b42SPeter Zijlstra { 23201768b42SPeter Zijlstra might_sleep(); 23301768b42SPeter Zijlstra 2343ca0ff57SPeter Zijlstra if (!__mutex_trylock_fast(lock)) 2353ca0ff57SPeter Zijlstra __mutex_lock_slowpath(lock); 2363ca0ff57SPeter Zijlstra } 23701768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock); 23801768b42SPeter Zijlstra #endif 23901768b42SPeter Zijlstra 24076916515SDavidlohr Bueso static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww, 24176916515SDavidlohr Bueso struct ww_acquire_ctx *ww_ctx) 24276916515SDavidlohr Bueso { 24376916515SDavidlohr Bueso #ifdef CONFIG_DEBUG_MUTEXES 24476916515SDavidlohr Bueso /* 24576916515SDavidlohr Bueso * If this WARN_ON triggers, you used ww_mutex_lock to acquire, 24676916515SDavidlohr Bueso * but released with a normal mutex_unlock in this call. 24776916515SDavidlohr Bueso * 24876916515SDavidlohr Bueso * This should never happen, always use ww_mutex_unlock. 24976916515SDavidlohr Bueso */ 25076916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww->ctx); 25176916515SDavidlohr Bueso 25276916515SDavidlohr Bueso /* 25376916515SDavidlohr Bueso * Not quite done after calling ww_acquire_done() ? 25476916515SDavidlohr Bueso */ 25576916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire); 25676916515SDavidlohr Bueso 25776916515SDavidlohr Bueso if (ww_ctx->contending_lock) { 25876916515SDavidlohr Bueso /* 25976916515SDavidlohr Bueso * After -EDEADLK you tried to 26076916515SDavidlohr Bueso * acquire a different ww_mutex? Bad! 26176916515SDavidlohr Bueso */ 26276916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww); 26376916515SDavidlohr Bueso 26476916515SDavidlohr Bueso /* 26576916515SDavidlohr Bueso * You called ww_mutex_lock after receiving -EDEADLK, 26676916515SDavidlohr Bueso * but 'forgot' to unlock everything else first? 26776916515SDavidlohr Bueso */ 26876916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0); 26976916515SDavidlohr Bueso ww_ctx->contending_lock = NULL; 27076916515SDavidlohr Bueso } 27176916515SDavidlohr Bueso 27276916515SDavidlohr Bueso /* 27376916515SDavidlohr Bueso * Naughty, using a different class will lead to undefined behavior! 27476916515SDavidlohr Bueso */ 27576916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class); 27676916515SDavidlohr Bueso #endif 27776916515SDavidlohr Bueso ww_ctx->acquired++; 27876916515SDavidlohr Bueso } 27976916515SDavidlohr Bueso 28076916515SDavidlohr Bueso /* 2814bd19084SDavidlohr Bueso * After acquiring lock with fastpath or when we lost out in contested 28276916515SDavidlohr Bueso * slowpath, set ctx and wake up any waiters so they can recheck. 28376916515SDavidlohr Bueso */ 28476916515SDavidlohr Bueso static __always_inline void 28576916515SDavidlohr Bueso ww_mutex_set_context_fastpath(struct ww_mutex *lock, 28676916515SDavidlohr Bueso struct ww_acquire_ctx *ctx) 28776916515SDavidlohr Bueso { 28876916515SDavidlohr Bueso unsigned long flags; 28976916515SDavidlohr Bueso struct mutex_waiter *cur; 29076916515SDavidlohr Bueso 29176916515SDavidlohr Bueso ww_mutex_lock_acquired(lock, ctx); 29276916515SDavidlohr Bueso 29376916515SDavidlohr Bueso lock->ctx = ctx; 29476916515SDavidlohr Bueso 29576916515SDavidlohr Bueso /* 29676916515SDavidlohr Bueso * The lock->ctx update should be visible on all cores before 29776916515SDavidlohr Bueso * the atomic read is done, otherwise contended waiters might be 29876916515SDavidlohr Bueso * missed. The contended waiters will either see ww_ctx == NULL 29976916515SDavidlohr Bueso * and keep spinning, or it will acquire wait_lock, add itself 30076916515SDavidlohr Bueso * to waiter list and sleep. 30176916515SDavidlohr Bueso */ 30276916515SDavidlohr Bueso smp_mb(); /* ^^^ */ 30376916515SDavidlohr Bueso 30476916515SDavidlohr Bueso /* 30576916515SDavidlohr Bueso * Check if lock is contended, if not there is nobody to wake up 30676916515SDavidlohr Bueso */ 3073ca0ff57SPeter Zijlstra if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS))) 30876916515SDavidlohr Bueso return; 30976916515SDavidlohr Bueso 31076916515SDavidlohr Bueso /* 31176916515SDavidlohr Bueso * Uh oh, we raced in fastpath, wake up everyone in this case, 31276916515SDavidlohr Bueso * so they can see the new lock->ctx. 31376916515SDavidlohr Bueso */ 31476916515SDavidlohr Bueso spin_lock_mutex(&lock->base.wait_lock, flags); 31576916515SDavidlohr Bueso list_for_each_entry(cur, &lock->base.wait_list, list) { 31676916515SDavidlohr Bueso debug_mutex_wake_waiter(&lock->base, cur); 31776916515SDavidlohr Bueso wake_up_process(cur->task); 31876916515SDavidlohr Bueso } 31976916515SDavidlohr Bueso spin_unlock_mutex(&lock->base.wait_lock, flags); 32076916515SDavidlohr Bueso } 32176916515SDavidlohr Bueso 3224bd19084SDavidlohr Bueso /* 3234bd19084SDavidlohr Bueso * After acquiring lock in the slowpath set ctx and wake up any 3244bd19084SDavidlohr Bueso * waiters so they can recheck. 3254bd19084SDavidlohr Bueso * 3264bd19084SDavidlohr Bueso * Callers must hold the mutex wait_lock. 3274bd19084SDavidlohr Bueso */ 3284bd19084SDavidlohr Bueso static __always_inline void 3294bd19084SDavidlohr Bueso ww_mutex_set_context_slowpath(struct ww_mutex *lock, 3304bd19084SDavidlohr Bueso struct ww_acquire_ctx *ctx) 3314bd19084SDavidlohr Bueso { 3324bd19084SDavidlohr Bueso struct mutex_waiter *cur; 3334bd19084SDavidlohr Bueso 3344bd19084SDavidlohr Bueso ww_mutex_lock_acquired(lock, ctx); 3354bd19084SDavidlohr Bueso lock->ctx = ctx; 3364bd19084SDavidlohr Bueso 3374bd19084SDavidlohr Bueso /* 3384bd19084SDavidlohr Bueso * Give any possible sleeping processes the chance to wake up, 3394bd19084SDavidlohr Bueso * so they can recheck if they have to back off. 3404bd19084SDavidlohr Bueso */ 3414bd19084SDavidlohr Bueso list_for_each_entry(cur, &lock->base.wait_list, list) { 3424bd19084SDavidlohr Bueso debug_mutex_wake_waiter(&lock->base, cur); 3434bd19084SDavidlohr Bueso wake_up_process(cur->task); 3444bd19084SDavidlohr Bueso } 3454bd19084SDavidlohr Bueso } 34676916515SDavidlohr Bueso 34701768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 34801768b42SPeter Zijlstra /* 34901768b42SPeter Zijlstra * Look out! "owner" is an entirely speculative pointer 35001768b42SPeter Zijlstra * access and not reliable. 35101768b42SPeter Zijlstra */ 35201768b42SPeter Zijlstra static noinline 353be1f7bf2SJason Low bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner) 35401768b42SPeter Zijlstra { 35501ac33c1SJason Low bool ret = true; 356be1f7bf2SJason Low 35701768b42SPeter Zijlstra rcu_read_lock(); 3583ca0ff57SPeter Zijlstra while (__mutex_owner(lock) == owner) { 359be1f7bf2SJason Low /* 360be1f7bf2SJason Low * Ensure we emit the owner->on_cpu, dereference _after_ 36101ac33c1SJason Low * checking lock->owner still matches owner. If that fails, 36201ac33c1SJason Low * owner might point to freed memory. If it still matches, 363be1f7bf2SJason Low * the rcu_read_lock() ensures the memory stays valid. 364be1f7bf2SJason Low */ 365be1f7bf2SJason Low barrier(); 366be1f7bf2SJason Low 367be1f7bf2SJason Low if (!owner->on_cpu || need_resched()) { 368be1f7bf2SJason Low ret = false; 369be1f7bf2SJason Low break; 370be1f7bf2SJason Low } 37101768b42SPeter Zijlstra 3723a6bfbc9SDavidlohr Bueso cpu_relax_lowlatency(); 37301768b42SPeter Zijlstra } 37401768b42SPeter Zijlstra rcu_read_unlock(); 37501768b42SPeter Zijlstra 376be1f7bf2SJason Low return ret; 37701768b42SPeter Zijlstra } 37801768b42SPeter Zijlstra 37901768b42SPeter Zijlstra /* 38001768b42SPeter Zijlstra * Initial check for entering the mutex spinning loop 38101768b42SPeter Zijlstra */ 38201768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock) 38301768b42SPeter Zijlstra { 38401768b42SPeter Zijlstra struct task_struct *owner; 38501768b42SPeter Zijlstra int retval = 1; 38601768b42SPeter Zijlstra 38746af29e4SJason Low if (need_resched()) 38846af29e4SJason Low return 0; 38946af29e4SJason Low 39001768b42SPeter Zijlstra rcu_read_lock(); 3913ca0ff57SPeter Zijlstra owner = __mutex_owner(lock); 39201768b42SPeter Zijlstra if (owner) 39301768b42SPeter Zijlstra retval = owner->on_cpu; 39401768b42SPeter Zijlstra rcu_read_unlock(); 39576916515SDavidlohr Bueso 39676916515SDavidlohr Bueso /* 3973ca0ff57SPeter Zijlstra * If lock->owner is not set, the mutex has been released. Return true 3983ca0ff57SPeter Zijlstra * such that we'll trylock in the spin path, which is a faster option 3993ca0ff57SPeter Zijlstra * than the blocking slow path. 40076916515SDavidlohr Bueso */ 4013ca0ff57SPeter Zijlstra return retval; 40276916515SDavidlohr Bueso } 40376916515SDavidlohr Bueso 40476916515SDavidlohr Bueso /* 40576916515SDavidlohr Bueso * Optimistic spinning. 40676916515SDavidlohr Bueso * 40776916515SDavidlohr Bueso * We try to spin for acquisition when we find that the lock owner 40876916515SDavidlohr Bueso * is currently running on a (different) CPU and while we don't 40976916515SDavidlohr Bueso * need to reschedule. The rationale is that if the lock owner is 41076916515SDavidlohr Bueso * running, it is likely to release the lock soon. 41176916515SDavidlohr Bueso * 41276916515SDavidlohr Bueso * The mutex spinners are queued up using MCS lock so that only one 41376916515SDavidlohr Bueso * spinner can compete for the mutex. However, if mutex spinning isn't 41476916515SDavidlohr Bueso * going to happen, there is no point in going through the lock/unlock 41576916515SDavidlohr Bueso * overhead. 41676916515SDavidlohr Bueso * 41776916515SDavidlohr Bueso * Returns true when the lock was taken, otherwise false, indicating 41876916515SDavidlohr Bueso * that we need to jump to the slowpath and sleep. 41976916515SDavidlohr Bueso */ 42076916515SDavidlohr Bueso static bool mutex_optimistic_spin(struct mutex *lock, 42176916515SDavidlohr Bueso struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 42276916515SDavidlohr Bueso { 42376916515SDavidlohr Bueso struct task_struct *task = current; 42476916515SDavidlohr Bueso 42576916515SDavidlohr Bueso if (!mutex_can_spin_on_owner(lock)) 42676916515SDavidlohr Bueso goto done; 42776916515SDavidlohr Bueso 428e42f678aSDavidlohr Bueso /* 429e42f678aSDavidlohr Bueso * In order to avoid a stampede of mutex spinners trying to 430e42f678aSDavidlohr Bueso * acquire the mutex all at once, the spinners need to take a 431e42f678aSDavidlohr Bueso * MCS (queued) lock first before spinning on the owner field. 432e42f678aSDavidlohr Bueso */ 43376916515SDavidlohr Bueso if (!osq_lock(&lock->osq)) 43476916515SDavidlohr Bueso goto done; 43576916515SDavidlohr Bueso 43676916515SDavidlohr Bueso while (true) { 43776916515SDavidlohr Bueso struct task_struct *owner; 43876916515SDavidlohr Bueso 43976916515SDavidlohr Bueso if (use_ww_ctx && ww_ctx->acquired > 0) { 44076916515SDavidlohr Bueso struct ww_mutex *ww; 44176916515SDavidlohr Bueso 44276916515SDavidlohr Bueso ww = container_of(lock, struct ww_mutex, base); 44376916515SDavidlohr Bueso /* 44476916515SDavidlohr Bueso * If ww->ctx is set the contents are undefined, only 44576916515SDavidlohr Bueso * by acquiring wait_lock there is a guarantee that 44676916515SDavidlohr Bueso * they are not invalid when reading. 44776916515SDavidlohr Bueso * 44876916515SDavidlohr Bueso * As such, when deadlock detection needs to be 44976916515SDavidlohr Bueso * performed the optimistic spinning cannot be done. 45076916515SDavidlohr Bueso */ 4514d3199e4SDavidlohr Bueso if (READ_ONCE(ww->ctx)) 45276916515SDavidlohr Bueso break; 45376916515SDavidlohr Bueso } 45476916515SDavidlohr Bueso 45576916515SDavidlohr Bueso /* 45676916515SDavidlohr Bueso * If there's an owner, wait for it to either 45776916515SDavidlohr Bueso * release the lock or go to sleep. 45876916515SDavidlohr Bueso */ 4593ca0ff57SPeter Zijlstra owner = __mutex_owner(lock); 46076916515SDavidlohr Bueso if (owner && !mutex_spin_on_owner(lock, owner)) 46176916515SDavidlohr Bueso break; 46276916515SDavidlohr Bueso 46376916515SDavidlohr Bueso /* Try to acquire the mutex if it is unlocked. */ 464*9d659ae1SPeter Zijlstra if (__mutex_trylock(lock, false)) { 46576916515SDavidlohr Bueso osq_unlock(&lock->osq); 46676916515SDavidlohr Bueso return true; 46776916515SDavidlohr Bueso } 46876916515SDavidlohr Bueso 46976916515SDavidlohr Bueso /* 47076916515SDavidlohr Bueso * The cpu_relax() call is a compiler barrier which forces 47176916515SDavidlohr Bueso * everything in this loop to be re-loaded. We don't need 47276916515SDavidlohr Bueso * memory barriers as we'll eventually observe the right 47376916515SDavidlohr Bueso * values at the cost of a few extra spins. 47476916515SDavidlohr Bueso */ 47576916515SDavidlohr Bueso cpu_relax_lowlatency(); 47676916515SDavidlohr Bueso } 47776916515SDavidlohr Bueso 47876916515SDavidlohr Bueso osq_unlock(&lock->osq); 47976916515SDavidlohr Bueso done: 48076916515SDavidlohr Bueso /* 48176916515SDavidlohr Bueso * If we fell out of the spin path because of need_resched(), 48276916515SDavidlohr Bueso * reschedule now, before we try-lock the mutex. This avoids getting 48376916515SDavidlohr Bueso * scheduled out right after we obtained the mutex. 48476916515SDavidlohr Bueso */ 4856f942a1fSPeter Zijlstra if (need_resched()) { 4866f942a1fSPeter Zijlstra /* 4876f942a1fSPeter Zijlstra * We _should_ have TASK_RUNNING here, but just in case 4886f942a1fSPeter Zijlstra * we do not, make it so, otherwise we might get stuck. 4896f942a1fSPeter Zijlstra */ 4906f942a1fSPeter Zijlstra __set_current_state(TASK_RUNNING); 49176916515SDavidlohr Bueso schedule_preempt_disabled(); 4926f942a1fSPeter Zijlstra } 49376916515SDavidlohr Bueso 49476916515SDavidlohr Bueso return false; 49576916515SDavidlohr Bueso } 49676916515SDavidlohr Bueso #else 49776916515SDavidlohr Bueso static bool mutex_optimistic_spin(struct mutex *lock, 49876916515SDavidlohr Bueso struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 49976916515SDavidlohr Bueso { 50076916515SDavidlohr Bueso return false; 50176916515SDavidlohr Bueso } 50201768b42SPeter Zijlstra #endif 50301768b42SPeter Zijlstra 5043ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip); 50501768b42SPeter Zijlstra 50601768b42SPeter Zijlstra /** 50701768b42SPeter Zijlstra * mutex_unlock - release the mutex 50801768b42SPeter Zijlstra * @lock: the mutex to be released 50901768b42SPeter Zijlstra * 51001768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously. 51101768b42SPeter Zijlstra * 51201768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 51301768b42SPeter Zijlstra * of a not locked mutex is not allowed. 51401768b42SPeter Zijlstra * 51501768b42SPeter Zijlstra * This function is similar to (but not equivalent to) up(). 51601768b42SPeter Zijlstra */ 51701768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock) 51801768b42SPeter Zijlstra { 5193ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 5203ca0ff57SPeter Zijlstra if (__mutex_unlock_fast(lock)) 5213ca0ff57SPeter Zijlstra return; 52201768b42SPeter Zijlstra #endif 5233ca0ff57SPeter Zijlstra __mutex_unlock_slowpath(lock, _RET_IP_); 52401768b42SPeter Zijlstra } 52501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock); 52601768b42SPeter Zijlstra 52701768b42SPeter Zijlstra /** 52801768b42SPeter Zijlstra * ww_mutex_unlock - release the w/w mutex 52901768b42SPeter Zijlstra * @lock: the mutex to be released 53001768b42SPeter Zijlstra * 53101768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously with any of the 53201768b42SPeter Zijlstra * ww_mutex_lock* functions (with or without an acquire context). It is 53301768b42SPeter Zijlstra * forbidden to release the locks after releasing the acquire context. 53401768b42SPeter Zijlstra * 53501768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 53601768b42SPeter Zijlstra * of a unlocked mutex is not allowed. 53701768b42SPeter Zijlstra */ 53801768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock) 53901768b42SPeter Zijlstra { 54001768b42SPeter Zijlstra /* 54101768b42SPeter Zijlstra * The unlocking fastpath is the 0->1 transition from 'locked' 54201768b42SPeter Zijlstra * into 'unlocked' state: 54301768b42SPeter Zijlstra */ 54401768b42SPeter Zijlstra if (lock->ctx) { 54501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 54601768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired); 54701768b42SPeter Zijlstra #endif 54801768b42SPeter Zijlstra if (lock->ctx->acquired > 0) 54901768b42SPeter Zijlstra lock->ctx->acquired--; 55001768b42SPeter Zijlstra lock->ctx = NULL; 55101768b42SPeter Zijlstra } 55201768b42SPeter Zijlstra 5533ca0ff57SPeter Zijlstra mutex_unlock(&lock->base); 55401768b42SPeter Zijlstra } 55501768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock); 55601768b42SPeter Zijlstra 55701768b42SPeter Zijlstra static inline int __sched 55863dc47e9SDavidlohr Bueso __ww_mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx) 55901768b42SPeter Zijlstra { 56001768b42SPeter Zijlstra struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 5614d3199e4SDavidlohr Bueso struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx); 56201768b42SPeter Zijlstra 56301768b42SPeter Zijlstra if (!hold_ctx) 56401768b42SPeter Zijlstra return 0; 56501768b42SPeter Zijlstra 56601768b42SPeter Zijlstra if (ctx->stamp - hold_ctx->stamp <= LONG_MAX && 56701768b42SPeter Zijlstra (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) { 56801768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 56901768b42SPeter Zijlstra DEBUG_LOCKS_WARN_ON(ctx->contending_lock); 57001768b42SPeter Zijlstra ctx->contending_lock = ww; 57101768b42SPeter Zijlstra #endif 57201768b42SPeter Zijlstra return -EDEADLK; 57301768b42SPeter Zijlstra } 57401768b42SPeter Zijlstra 57501768b42SPeter Zijlstra return 0; 57601768b42SPeter Zijlstra } 57701768b42SPeter Zijlstra 57801768b42SPeter Zijlstra /* 57901768b42SPeter Zijlstra * Lock a mutex (possibly interruptible), slowpath: 58001768b42SPeter Zijlstra */ 58101768b42SPeter Zijlstra static __always_inline int __sched 58201768b42SPeter Zijlstra __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, 58301768b42SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip, 58401768b42SPeter Zijlstra struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 58501768b42SPeter Zijlstra { 58601768b42SPeter Zijlstra struct task_struct *task = current; 58701768b42SPeter Zijlstra struct mutex_waiter waiter; 58801768b42SPeter Zijlstra unsigned long flags; 589*9d659ae1SPeter Zijlstra bool first = false; 59001768b42SPeter Zijlstra int ret; 59101768b42SPeter Zijlstra 5920422e83dSChris Wilson if (use_ww_ctx) { 5930422e83dSChris Wilson struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 5940422e83dSChris Wilson if (unlikely(ww_ctx == READ_ONCE(ww->ctx))) 5950422e83dSChris Wilson return -EALREADY; 5960422e83dSChris Wilson } 5970422e83dSChris Wilson 59801768b42SPeter Zijlstra preempt_disable(); 59901768b42SPeter Zijlstra mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 60001768b42SPeter Zijlstra 601*9d659ae1SPeter Zijlstra if (__mutex_trylock(lock, false) || 602*9d659ae1SPeter Zijlstra mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) { 60376916515SDavidlohr Bueso /* got the lock, yay! */ 6043ca0ff57SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 6053ca0ff57SPeter Zijlstra if (use_ww_ctx) { 6063ca0ff57SPeter Zijlstra struct ww_mutex *ww; 6073ca0ff57SPeter Zijlstra ww = container_of(lock, struct ww_mutex, base); 6083ca0ff57SPeter Zijlstra 6093ca0ff57SPeter Zijlstra ww_mutex_set_context_fastpath(ww, ww_ctx); 6103ca0ff57SPeter Zijlstra } 61101768b42SPeter Zijlstra preempt_enable(); 61201768b42SPeter Zijlstra return 0; 61301768b42SPeter Zijlstra } 61401768b42SPeter Zijlstra 61501768b42SPeter Zijlstra spin_lock_mutex(&lock->wait_lock, flags); 6161e820c96SJason Low /* 6173ca0ff57SPeter Zijlstra * After waiting to acquire the wait_lock, try again. 6181e820c96SJason Low */ 619*9d659ae1SPeter Zijlstra if (__mutex_trylock(lock, false)) 62001768b42SPeter Zijlstra goto skip_wait; 62101768b42SPeter Zijlstra 62201768b42SPeter Zijlstra debug_mutex_lock_common(lock, &waiter); 6236720a305SLinus Torvalds debug_mutex_add_waiter(lock, &waiter, task); 62401768b42SPeter Zijlstra 62501768b42SPeter Zijlstra /* add waiting tasks to the end of the waitqueue (FIFO): */ 62601768b42SPeter Zijlstra list_add_tail(&waiter.list, &lock->wait_list); 62701768b42SPeter Zijlstra waiter.task = task; 62801768b42SPeter Zijlstra 629*9d659ae1SPeter Zijlstra if (__mutex_waiter_is_first(lock, &waiter)) 6303ca0ff57SPeter Zijlstra __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); 6313ca0ff57SPeter Zijlstra 63201768b42SPeter Zijlstra lock_contended(&lock->dep_map, ip); 63301768b42SPeter Zijlstra 63401768b42SPeter Zijlstra for (;;) { 635*9d659ae1SPeter Zijlstra if (__mutex_trylock(lock, first)) 63601768b42SPeter Zijlstra break; 63701768b42SPeter Zijlstra 63801768b42SPeter Zijlstra /* 63901768b42SPeter Zijlstra * got a signal? (This code gets eliminated in the 64001768b42SPeter Zijlstra * TASK_UNINTERRUPTIBLE case.) 64101768b42SPeter Zijlstra */ 64201768b42SPeter Zijlstra if (unlikely(signal_pending_state(state, task))) { 64301768b42SPeter Zijlstra ret = -EINTR; 64401768b42SPeter Zijlstra goto err; 64501768b42SPeter Zijlstra } 64601768b42SPeter Zijlstra 64701768b42SPeter Zijlstra if (use_ww_ctx && ww_ctx->acquired > 0) { 64863dc47e9SDavidlohr Bueso ret = __ww_mutex_lock_check_stamp(lock, ww_ctx); 64901768b42SPeter Zijlstra if (ret) 65001768b42SPeter Zijlstra goto err; 65101768b42SPeter Zijlstra } 65201768b42SPeter Zijlstra 65301768b42SPeter Zijlstra __set_task_state(task, state); 65401768b42SPeter Zijlstra spin_unlock_mutex(&lock->wait_lock, flags); 65501768b42SPeter Zijlstra schedule_preempt_disabled(); 65601768b42SPeter Zijlstra spin_lock_mutex(&lock->wait_lock, flags); 657*9d659ae1SPeter Zijlstra 658*9d659ae1SPeter Zijlstra if (!first && __mutex_waiter_is_first(lock, &waiter)) { 659*9d659ae1SPeter Zijlstra first = true; 660*9d659ae1SPeter Zijlstra __mutex_set_flag(lock, MUTEX_FLAG_HANDOFF); 661*9d659ae1SPeter Zijlstra } 66201768b42SPeter Zijlstra } 66351587bcfSDavidlohr Bueso __set_task_state(task, TASK_RUNNING); 66451587bcfSDavidlohr Bueso 6656720a305SLinus Torvalds mutex_remove_waiter(lock, &waiter, task); 66601768b42SPeter Zijlstra if (likely(list_empty(&lock->wait_list))) 667*9d659ae1SPeter Zijlstra __mutex_clear_flag(lock, MUTEX_FLAGS); 6683ca0ff57SPeter Zijlstra 66901768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 67001768b42SPeter Zijlstra 67101768b42SPeter Zijlstra skip_wait: 67201768b42SPeter Zijlstra /* got the lock - cleanup and rejoice! */ 67301768b42SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 67401768b42SPeter Zijlstra 67501768b42SPeter Zijlstra if (use_ww_ctx) { 67601768b42SPeter Zijlstra struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 6774bd19084SDavidlohr Bueso ww_mutex_set_context_slowpath(ww, ww_ctx); 67801768b42SPeter Zijlstra } 67901768b42SPeter Zijlstra 68001768b42SPeter Zijlstra spin_unlock_mutex(&lock->wait_lock, flags); 68101768b42SPeter Zijlstra preempt_enable(); 68201768b42SPeter Zijlstra return 0; 68301768b42SPeter Zijlstra 68401768b42SPeter Zijlstra err: 6856720a305SLinus Torvalds mutex_remove_waiter(lock, &waiter, task); 68601768b42SPeter Zijlstra spin_unlock_mutex(&lock->wait_lock, flags); 68701768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 68801768b42SPeter Zijlstra mutex_release(&lock->dep_map, 1, ip); 68901768b42SPeter Zijlstra preempt_enable(); 69001768b42SPeter Zijlstra return ret; 69101768b42SPeter Zijlstra } 69201768b42SPeter Zijlstra 69301768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 69401768b42SPeter Zijlstra void __sched 69501768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass) 69601768b42SPeter Zijlstra { 69701768b42SPeter Zijlstra might_sleep(); 69801768b42SPeter Zijlstra __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 69901768b42SPeter Zijlstra subclass, NULL, _RET_IP_, NULL, 0); 70001768b42SPeter Zijlstra } 70101768b42SPeter Zijlstra 70201768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested); 70301768b42SPeter Zijlstra 70401768b42SPeter Zijlstra void __sched 70501768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) 70601768b42SPeter Zijlstra { 70701768b42SPeter Zijlstra might_sleep(); 70801768b42SPeter Zijlstra __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 70901768b42SPeter Zijlstra 0, nest, _RET_IP_, NULL, 0); 71001768b42SPeter Zijlstra } 71101768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 71201768b42SPeter Zijlstra 71301768b42SPeter Zijlstra int __sched 71401768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) 71501768b42SPeter Zijlstra { 71601768b42SPeter Zijlstra might_sleep(); 71701768b42SPeter Zijlstra return __mutex_lock_common(lock, TASK_KILLABLE, 71801768b42SPeter Zijlstra subclass, NULL, _RET_IP_, NULL, 0); 71901768b42SPeter Zijlstra } 72001768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested); 72101768b42SPeter Zijlstra 72201768b42SPeter Zijlstra int __sched 72301768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) 72401768b42SPeter Zijlstra { 72501768b42SPeter Zijlstra might_sleep(); 72601768b42SPeter Zijlstra return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 72701768b42SPeter Zijlstra subclass, NULL, _RET_IP_, NULL, 0); 72801768b42SPeter Zijlstra } 72901768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 73001768b42SPeter Zijlstra 73101768b42SPeter Zijlstra static inline int 73201768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 73301768b42SPeter Zijlstra { 73401768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 73501768b42SPeter Zijlstra unsigned tmp; 73601768b42SPeter Zijlstra 73701768b42SPeter Zijlstra if (ctx->deadlock_inject_countdown-- == 0) { 73801768b42SPeter Zijlstra tmp = ctx->deadlock_inject_interval; 73901768b42SPeter Zijlstra if (tmp > UINT_MAX/4) 74001768b42SPeter Zijlstra tmp = UINT_MAX; 74101768b42SPeter Zijlstra else 74201768b42SPeter Zijlstra tmp = tmp*2 + tmp + tmp/2; 74301768b42SPeter Zijlstra 74401768b42SPeter Zijlstra ctx->deadlock_inject_interval = tmp; 74501768b42SPeter Zijlstra ctx->deadlock_inject_countdown = tmp; 74601768b42SPeter Zijlstra ctx->contending_lock = lock; 74701768b42SPeter Zijlstra 74801768b42SPeter Zijlstra ww_mutex_unlock(lock); 74901768b42SPeter Zijlstra 75001768b42SPeter Zijlstra return -EDEADLK; 75101768b42SPeter Zijlstra } 75201768b42SPeter Zijlstra #endif 75301768b42SPeter Zijlstra 75401768b42SPeter Zijlstra return 0; 75501768b42SPeter Zijlstra } 75601768b42SPeter Zijlstra 75701768b42SPeter Zijlstra int __sched 75801768b42SPeter Zijlstra __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 75901768b42SPeter Zijlstra { 76001768b42SPeter Zijlstra int ret; 76101768b42SPeter Zijlstra 76201768b42SPeter Zijlstra might_sleep(); 76301768b42SPeter Zijlstra ret = __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 76401768b42SPeter Zijlstra 0, &ctx->dep_map, _RET_IP_, ctx, 1); 76501768b42SPeter Zijlstra if (!ret && ctx->acquired > 1) 76601768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 76701768b42SPeter Zijlstra 76801768b42SPeter Zijlstra return ret; 76901768b42SPeter Zijlstra } 77001768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(__ww_mutex_lock); 77101768b42SPeter Zijlstra 77201768b42SPeter Zijlstra int __sched 77301768b42SPeter Zijlstra __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 77401768b42SPeter Zijlstra { 77501768b42SPeter Zijlstra int ret; 77601768b42SPeter Zijlstra 77701768b42SPeter Zijlstra might_sleep(); 77801768b42SPeter Zijlstra ret = __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 77901768b42SPeter Zijlstra 0, &ctx->dep_map, _RET_IP_, ctx, 1); 78001768b42SPeter Zijlstra 78101768b42SPeter Zijlstra if (!ret && ctx->acquired > 1) 78201768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 78301768b42SPeter Zijlstra 78401768b42SPeter Zijlstra return ret; 78501768b42SPeter Zijlstra } 78601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible); 78701768b42SPeter Zijlstra 78801768b42SPeter Zijlstra #endif 78901768b42SPeter Zijlstra 79001768b42SPeter Zijlstra /* 79101768b42SPeter Zijlstra * Release the lock, slowpath: 79201768b42SPeter Zijlstra */ 7933ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) 79401768b42SPeter Zijlstra { 795*9d659ae1SPeter Zijlstra struct task_struct *next = NULL; 7963ca0ff57SPeter Zijlstra unsigned long owner, flags; 7971329ce6fSDavidlohr Bueso WAKE_Q(wake_q); 79801768b42SPeter Zijlstra 7993ca0ff57SPeter Zijlstra mutex_release(&lock->dep_map, 1, ip); 8003ca0ff57SPeter Zijlstra 80101768b42SPeter Zijlstra /* 802*9d659ae1SPeter Zijlstra * Release the lock before (potentially) taking the spinlock such that 803*9d659ae1SPeter Zijlstra * other contenders can get on with things ASAP. 804*9d659ae1SPeter Zijlstra * 805*9d659ae1SPeter Zijlstra * Except when HANDOFF, in that case we must not clear the owner field, 806*9d659ae1SPeter Zijlstra * but instead set it to the top waiter. 80701768b42SPeter Zijlstra */ 808*9d659ae1SPeter Zijlstra owner = atomic_long_read(&lock->owner); 809*9d659ae1SPeter Zijlstra for (;;) { 810*9d659ae1SPeter Zijlstra unsigned long old; 811*9d659ae1SPeter Zijlstra 812*9d659ae1SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 813*9d659ae1SPeter Zijlstra DEBUG_LOCKS_WARN_ON(__owner_task(owner) != current); 814*9d659ae1SPeter Zijlstra #endif 815*9d659ae1SPeter Zijlstra 816*9d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 817*9d659ae1SPeter Zijlstra break; 818*9d659ae1SPeter Zijlstra 819*9d659ae1SPeter Zijlstra old = atomic_long_cmpxchg_release(&lock->owner, owner, 820*9d659ae1SPeter Zijlstra __owner_flags(owner)); 821*9d659ae1SPeter Zijlstra if (old == owner) { 822*9d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_WAITERS) 823*9d659ae1SPeter Zijlstra break; 824*9d659ae1SPeter Zijlstra 8253ca0ff57SPeter Zijlstra return; 826*9d659ae1SPeter Zijlstra } 827*9d659ae1SPeter Zijlstra 828*9d659ae1SPeter Zijlstra owner = old; 829*9d659ae1SPeter Zijlstra } 83001768b42SPeter Zijlstra 8311d8fe7dcSJason Low spin_lock_mutex(&lock->wait_lock, flags); 8321d8fe7dcSJason Low debug_mutex_unlock(lock); 83301768b42SPeter Zijlstra if (!list_empty(&lock->wait_list)) { 83401768b42SPeter Zijlstra /* get the first entry from the wait-list: */ 83501768b42SPeter Zijlstra struct mutex_waiter *waiter = 836*9d659ae1SPeter Zijlstra list_first_entry(&lock->wait_list, 83701768b42SPeter Zijlstra struct mutex_waiter, list); 83801768b42SPeter Zijlstra 839*9d659ae1SPeter Zijlstra next = waiter->task; 840*9d659ae1SPeter Zijlstra 84101768b42SPeter Zijlstra debug_mutex_wake_waiter(lock, waiter); 842*9d659ae1SPeter Zijlstra wake_q_add(&wake_q, next); 84301768b42SPeter Zijlstra } 84401768b42SPeter Zijlstra 845*9d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 846*9d659ae1SPeter Zijlstra __mutex_handoff(lock, next); 847*9d659ae1SPeter Zijlstra 84801768b42SPeter Zijlstra spin_unlock_mutex(&lock->wait_lock, flags); 849*9d659ae1SPeter Zijlstra 8501329ce6fSDavidlohr Bueso wake_up_q(&wake_q); 85101768b42SPeter Zijlstra } 85201768b42SPeter Zijlstra 85301768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 85401768b42SPeter Zijlstra /* 85501768b42SPeter Zijlstra * Here come the less common (and hence less performance-critical) APIs: 85601768b42SPeter Zijlstra * mutex_lock_interruptible() and mutex_trylock(). 85701768b42SPeter Zijlstra */ 85801768b42SPeter Zijlstra static noinline int __sched 85901768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock); 86001768b42SPeter Zijlstra 86101768b42SPeter Zijlstra static noinline int __sched 86201768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock); 86301768b42SPeter Zijlstra 86401768b42SPeter Zijlstra /** 86501768b42SPeter Zijlstra * mutex_lock_interruptible - acquire the mutex, interruptible 86601768b42SPeter Zijlstra * @lock: the mutex to be acquired 86701768b42SPeter Zijlstra * 86801768b42SPeter Zijlstra * Lock the mutex like mutex_lock(), and return 0 if the mutex has 86901768b42SPeter Zijlstra * been acquired or sleep until the mutex becomes available. If a 87001768b42SPeter Zijlstra * signal arrives while waiting for the lock then this function 87101768b42SPeter Zijlstra * returns -EINTR. 87201768b42SPeter Zijlstra * 87301768b42SPeter Zijlstra * This function is similar to (but not equivalent to) down_interruptible(). 87401768b42SPeter Zijlstra */ 87501768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock) 87601768b42SPeter Zijlstra { 87701768b42SPeter Zijlstra might_sleep(); 8783ca0ff57SPeter Zijlstra 8793ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 88001768b42SPeter Zijlstra return 0; 8813ca0ff57SPeter Zijlstra 88201768b42SPeter Zijlstra return __mutex_lock_interruptible_slowpath(lock); 88301768b42SPeter Zijlstra } 88401768b42SPeter Zijlstra 88501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible); 88601768b42SPeter Zijlstra 88701768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock) 88801768b42SPeter Zijlstra { 88901768b42SPeter Zijlstra might_sleep(); 8903ca0ff57SPeter Zijlstra 8913ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 89201768b42SPeter Zijlstra return 0; 8933ca0ff57SPeter Zijlstra 89401768b42SPeter Zijlstra return __mutex_lock_killable_slowpath(lock); 89501768b42SPeter Zijlstra } 89601768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable); 89701768b42SPeter Zijlstra 8983ca0ff57SPeter Zijlstra static noinline void __sched 8993ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock) 90001768b42SPeter Zijlstra { 90101768b42SPeter Zijlstra __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0, 90201768b42SPeter Zijlstra NULL, _RET_IP_, NULL, 0); 90301768b42SPeter Zijlstra } 90401768b42SPeter Zijlstra 90501768b42SPeter Zijlstra static noinline int __sched 90601768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock) 90701768b42SPeter Zijlstra { 90801768b42SPeter Zijlstra return __mutex_lock_common(lock, TASK_KILLABLE, 0, 90901768b42SPeter Zijlstra NULL, _RET_IP_, NULL, 0); 91001768b42SPeter Zijlstra } 91101768b42SPeter Zijlstra 91201768b42SPeter Zijlstra static noinline int __sched 91301768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock) 91401768b42SPeter Zijlstra { 91501768b42SPeter Zijlstra return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0, 91601768b42SPeter Zijlstra NULL, _RET_IP_, NULL, 0); 91701768b42SPeter Zijlstra } 91801768b42SPeter Zijlstra 91901768b42SPeter Zijlstra static noinline int __sched 92001768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 92101768b42SPeter Zijlstra { 92201768b42SPeter Zijlstra return __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 0, 92301768b42SPeter Zijlstra NULL, _RET_IP_, ctx, 1); 92401768b42SPeter Zijlstra } 92501768b42SPeter Zijlstra 92601768b42SPeter Zijlstra static noinline int __sched 92701768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, 92801768b42SPeter Zijlstra struct ww_acquire_ctx *ctx) 92901768b42SPeter Zijlstra { 93001768b42SPeter Zijlstra return __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 0, 93101768b42SPeter Zijlstra NULL, _RET_IP_, ctx, 1); 93201768b42SPeter Zijlstra } 93301768b42SPeter Zijlstra 93401768b42SPeter Zijlstra #endif 93501768b42SPeter Zijlstra 93601768b42SPeter Zijlstra /** 93701768b42SPeter Zijlstra * mutex_trylock - try to acquire the mutex, without waiting 93801768b42SPeter Zijlstra * @lock: the mutex to be acquired 93901768b42SPeter Zijlstra * 94001768b42SPeter Zijlstra * Try to acquire the mutex atomically. Returns 1 if the mutex 94101768b42SPeter Zijlstra * has been acquired successfully, and 0 on contention. 94201768b42SPeter Zijlstra * 94301768b42SPeter Zijlstra * NOTE: this function follows the spin_trylock() convention, so 94401768b42SPeter Zijlstra * it is negated from the down_trylock() return values! Be careful 94501768b42SPeter Zijlstra * about this when converting semaphore users to mutexes. 94601768b42SPeter Zijlstra * 94701768b42SPeter Zijlstra * This function must not be used in interrupt context. The 94801768b42SPeter Zijlstra * mutex must be released by the same task that acquired it. 94901768b42SPeter Zijlstra */ 95001768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock) 95101768b42SPeter Zijlstra { 952*9d659ae1SPeter Zijlstra bool locked = __mutex_trylock(lock, false); 95301768b42SPeter Zijlstra 9543ca0ff57SPeter Zijlstra if (locked) 9553ca0ff57SPeter Zijlstra mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 95601768b42SPeter Zijlstra 9573ca0ff57SPeter Zijlstra return locked; 95801768b42SPeter Zijlstra } 95901768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock); 96001768b42SPeter Zijlstra 96101768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 96201768b42SPeter Zijlstra int __sched 96301768b42SPeter Zijlstra __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 96401768b42SPeter Zijlstra { 96501768b42SPeter Zijlstra might_sleep(); 96601768b42SPeter Zijlstra 9673ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 96801768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 9693ca0ff57SPeter Zijlstra return 0; 9703ca0ff57SPeter Zijlstra } 9713ca0ff57SPeter Zijlstra 9723ca0ff57SPeter Zijlstra return __ww_mutex_lock_slowpath(lock, ctx); 97301768b42SPeter Zijlstra } 97401768b42SPeter Zijlstra EXPORT_SYMBOL(__ww_mutex_lock); 97501768b42SPeter Zijlstra 97601768b42SPeter Zijlstra int __sched 97701768b42SPeter Zijlstra __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 97801768b42SPeter Zijlstra { 97901768b42SPeter Zijlstra might_sleep(); 98001768b42SPeter Zijlstra 9813ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 98201768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 9833ca0ff57SPeter Zijlstra return 0; 9843ca0ff57SPeter Zijlstra } 9853ca0ff57SPeter Zijlstra 9863ca0ff57SPeter Zijlstra return __ww_mutex_lock_interruptible_slowpath(lock, ctx); 98701768b42SPeter Zijlstra } 98801768b42SPeter Zijlstra EXPORT_SYMBOL(__ww_mutex_lock_interruptible); 98901768b42SPeter Zijlstra 99001768b42SPeter Zijlstra #endif 99101768b42SPeter Zijlstra 99201768b42SPeter Zijlstra /** 99301768b42SPeter Zijlstra * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 99401768b42SPeter Zijlstra * @cnt: the atomic which we are to dec 99501768b42SPeter Zijlstra * @lock: the mutex to return holding if we dec to 0 99601768b42SPeter Zijlstra * 99701768b42SPeter Zijlstra * return true and hold lock if we dec to 0, return false otherwise 99801768b42SPeter Zijlstra */ 99901768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) 100001768b42SPeter Zijlstra { 100101768b42SPeter Zijlstra /* dec if we can't possibly hit 0 */ 100201768b42SPeter Zijlstra if (atomic_add_unless(cnt, -1, 1)) 100301768b42SPeter Zijlstra return 0; 100401768b42SPeter Zijlstra /* we might hit 0, so take the lock */ 100501768b42SPeter Zijlstra mutex_lock(lock); 100601768b42SPeter Zijlstra if (!atomic_dec_and_test(cnt)) { 100701768b42SPeter Zijlstra /* when we actually did the dec, we didn't hit 0 */ 100801768b42SPeter Zijlstra mutex_unlock(lock); 100901768b42SPeter Zijlstra return 0; 101001768b42SPeter Zijlstra } 101101768b42SPeter Zijlstra /* we hit 0, and we hold the lock */ 101201768b42SPeter Zijlstra return 1; 101301768b42SPeter Zijlstra } 101401768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock); 1015