1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 201768b42SPeter Zijlstra /* 367a6de49SPeter Zijlstra * kernel/locking/mutex.c 401768b42SPeter Zijlstra * 501768b42SPeter Zijlstra * Mutexes: blocking mutual exclusion locks 601768b42SPeter Zijlstra * 701768b42SPeter Zijlstra * Started by Ingo Molnar: 801768b42SPeter Zijlstra * 901768b42SPeter Zijlstra * Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> 1001768b42SPeter Zijlstra * 1101768b42SPeter Zijlstra * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and 1201768b42SPeter Zijlstra * David Howells for suggestions and improvements. 1301768b42SPeter Zijlstra * 1401768b42SPeter Zijlstra * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline 1501768b42SPeter Zijlstra * from the -rt tree, where it was originally implemented for rtmutexes 1601768b42SPeter Zijlstra * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale 1701768b42SPeter Zijlstra * and Sven Dietrich. 1801768b42SPeter Zijlstra * 19387b1468SMauro Carvalho Chehab * Also see Documentation/locking/mutex-design.rst. 2001768b42SPeter Zijlstra */ 2101768b42SPeter Zijlstra #include <linux/mutex.h> 2201768b42SPeter Zijlstra #include <linux/ww_mutex.h> 23174cd4b1SIngo Molnar #include <linux/sched/signal.h> 2401768b42SPeter Zijlstra #include <linux/sched/rt.h> 2584f001e1SIngo Molnar #include <linux/sched/wake_q.h> 26b17b0153SIngo Molnar #include <linux/sched/debug.h> 2701768b42SPeter Zijlstra #include <linux/export.h> 2801768b42SPeter Zijlstra #include <linux/spinlock.h> 2901768b42SPeter Zijlstra #include <linux/interrupt.h> 3001768b42SPeter Zijlstra #include <linux/debug_locks.h> 317a215f89SDavidlohr Bueso #include <linux/osq_lock.h> 3201768b42SPeter Zijlstra 33a321fb90SThomas Gleixner #include "mutex.h" 34a321fb90SThomas Gleixner 3501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 36e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond) 3701768b42SPeter Zijlstra #else 38e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) 3901768b42SPeter Zijlstra #endif 4001768b42SPeter Zijlstra 4101768b42SPeter Zijlstra void 4201768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 4301768b42SPeter Zijlstra { 443ca0ff57SPeter Zijlstra atomic_long_set(&lock->owner, 0); 45ebf4c55cSThomas Gleixner raw_spin_lock_init(&lock->wait_lock); 4601768b42SPeter Zijlstra INIT_LIST_HEAD(&lock->wait_list); 4701768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 484d9d951eSJason Low osq_lock_init(&lock->osq); 4901768b42SPeter Zijlstra #endif 5001768b42SPeter Zijlstra 5101768b42SPeter Zijlstra debug_mutex_init(lock, name, key); 5201768b42SPeter Zijlstra } 5301768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init); 5401768b42SPeter Zijlstra 553ca0ff57SPeter Zijlstra /* 563ca0ff57SPeter Zijlstra * @owner: contains: 'struct task_struct *' to the current lock owner, 573ca0ff57SPeter Zijlstra * NULL means not owned. Since task_struct pointers are aligned at 58e274795eSPeter Zijlstra * at least L1_CACHE_BYTES, we have low bits to store extra state. 593ca0ff57SPeter Zijlstra * 603ca0ff57SPeter Zijlstra * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. 619d659ae1SPeter Zijlstra * Bit1 indicates unlock needs to hand the lock to the top-waiter 62e274795eSPeter Zijlstra * Bit2 indicates handoff has been done and we're waiting for pickup. 633ca0ff57SPeter Zijlstra */ 643ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS 0x01 659d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF 0x02 66e274795eSPeter Zijlstra #define MUTEX_FLAG_PICKUP 0x04 673ca0ff57SPeter Zijlstra 68e274795eSPeter Zijlstra #define MUTEX_FLAGS 0x07 693ca0ff57SPeter Zijlstra 705f35d5a6SMukesh Ojha /* 715f35d5a6SMukesh Ojha * Internal helper function; C doesn't allow us to hide it :/ 725f35d5a6SMukesh Ojha * 735f35d5a6SMukesh Ojha * DO NOT USE (outside of mutex code). 745f35d5a6SMukesh Ojha */ 755f35d5a6SMukesh Ojha static inline struct task_struct *__mutex_owner(struct mutex *lock) 765f35d5a6SMukesh Ojha { 77a037d269SMukesh Ojha return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS); 785f35d5a6SMukesh Ojha } 795f35d5a6SMukesh Ojha 803ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner) 813ca0ff57SPeter Zijlstra { 823ca0ff57SPeter Zijlstra return (struct task_struct *)(owner & ~MUTEX_FLAGS); 833ca0ff57SPeter Zijlstra } 843ca0ff57SPeter Zijlstra 855f35d5a6SMukesh Ojha bool mutex_is_locked(struct mutex *lock) 865f35d5a6SMukesh Ojha { 875f35d5a6SMukesh Ojha return __mutex_owner(lock) != NULL; 885f35d5a6SMukesh Ojha } 895f35d5a6SMukesh Ojha EXPORT_SYMBOL(mutex_is_locked); 905f35d5a6SMukesh Ojha 913ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner) 923ca0ff57SPeter Zijlstra { 933ca0ff57SPeter Zijlstra return owner & MUTEX_FLAGS; 943ca0ff57SPeter Zijlstra } 953ca0ff57SPeter Zijlstra 96ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff) 973ca0ff57SPeter Zijlstra { 983ca0ff57SPeter Zijlstra unsigned long owner, curr = (unsigned long)current; 993ca0ff57SPeter Zijlstra 1003ca0ff57SPeter Zijlstra owner = atomic_long_read(&lock->owner); 1013ca0ff57SPeter Zijlstra for (;;) { /* must loop, can race against a flag */ 102ab4e4d9fSPeter Zijlstra unsigned long flags = __owner_flags(owner); 103e274795eSPeter Zijlstra unsigned long task = owner & ~MUTEX_FLAGS; 1043ca0ff57SPeter Zijlstra 105e274795eSPeter Zijlstra if (task) { 106ad90880dSPeter Zijlstra if (flags & MUTEX_FLAG_PICKUP) { 107ad90880dSPeter Zijlstra if (task != curr) 108e274795eSPeter Zijlstra break; 109e274795eSPeter Zijlstra flags &= ~MUTEX_FLAG_PICKUP; 110ad90880dSPeter Zijlstra } else if (handoff) { 111ad90880dSPeter Zijlstra if (flags & MUTEX_FLAG_HANDOFF) 112ad90880dSPeter Zijlstra break; 113ad90880dSPeter Zijlstra flags |= MUTEX_FLAG_HANDOFF; 114ad90880dSPeter Zijlstra } else { 115ad90880dSPeter Zijlstra break; 116ad90880dSPeter Zijlstra } 117e274795eSPeter Zijlstra } else { 118e6b4457bSPeter Zijlstra MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP)); 119ad90880dSPeter Zijlstra task = curr; 1209d659ae1SPeter Zijlstra } 1213ca0ff57SPeter Zijlstra 122ad90880dSPeter Zijlstra if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) { 123ad90880dSPeter Zijlstra if (task == curr) 124e274795eSPeter Zijlstra return NULL; 125ad90880dSPeter Zijlstra break; 126ad90880dSPeter Zijlstra } 1273ca0ff57SPeter Zijlstra } 128e274795eSPeter Zijlstra 129e274795eSPeter Zijlstra return __owner_task(owner); 130e274795eSPeter Zijlstra } 131e274795eSPeter Zijlstra 132e274795eSPeter Zijlstra /* 133ad90880dSPeter Zijlstra * Trylock or set HANDOFF 134ad90880dSPeter Zijlstra */ 135ad90880dSPeter Zijlstra static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff) 136ad90880dSPeter Zijlstra { 137ad90880dSPeter Zijlstra return !__mutex_trylock_common(lock, handoff); 138ad90880dSPeter Zijlstra } 139ad90880dSPeter Zijlstra 140ad90880dSPeter Zijlstra /* 141e274795eSPeter Zijlstra * Actual trylock that will work on any unlocked state. 142e274795eSPeter Zijlstra */ 143e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock) 144e274795eSPeter Zijlstra { 145ad90880dSPeter Zijlstra return !__mutex_trylock_common(lock, false); 1463ca0ff57SPeter Zijlstra } 1473ca0ff57SPeter Zijlstra 1483ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 1493ca0ff57SPeter Zijlstra /* 1503ca0ff57SPeter Zijlstra * Lockdep annotations are contained to the slow paths for simplicity. 1513ca0ff57SPeter Zijlstra * There is nothing that would stop spreading the lockdep annotations outwards 1523ca0ff57SPeter Zijlstra * except more code. 1533ca0ff57SPeter Zijlstra */ 1543ca0ff57SPeter Zijlstra 1553ca0ff57SPeter Zijlstra /* 1563ca0ff57SPeter Zijlstra * Optimistic trylock that only works in the uncontended case. Make sure to 1573ca0ff57SPeter Zijlstra * follow with a __mutex_trylock() before failing. 1583ca0ff57SPeter Zijlstra */ 1593ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock) 1603ca0ff57SPeter Zijlstra { 1613ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 162c427f695SPeter Zijlstra unsigned long zero = 0UL; 1633ca0ff57SPeter Zijlstra 164c427f695SPeter Zijlstra if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr)) 1653ca0ff57SPeter Zijlstra return true; 1663ca0ff57SPeter Zijlstra 1673ca0ff57SPeter Zijlstra return false; 1683ca0ff57SPeter Zijlstra } 1693ca0ff57SPeter Zijlstra 1703ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock) 1713ca0ff57SPeter Zijlstra { 1723ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 1733ca0ff57SPeter Zijlstra 174ab4e4d9fSPeter Zijlstra return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); 1753ca0ff57SPeter Zijlstra } 1763ca0ff57SPeter Zijlstra #endif 1773ca0ff57SPeter Zijlstra 1783ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 1793ca0ff57SPeter Zijlstra { 1803ca0ff57SPeter Zijlstra atomic_long_or(flag, &lock->owner); 1813ca0ff57SPeter Zijlstra } 1823ca0ff57SPeter Zijlstra 1833ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) 1843ca0ff57SPeter Zijlstra { 1853ca0ff57SPeter Zijlstra atomic_long_andnot(flag, &lock->owner); 1863ca0ff57SPeter Zijlstra } 1873ca0ff57SPeter Zijlstra 1889d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) 1899d659ae1SPeter Zijlstra { 1909d659ae1SPeter Zijlstra return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; 1919d659ae1SPeter Zijlstra } 1929d659ae1SPeter Zijlstra 1939d659ae1SPeter Zijlstra /* 19408295b3bSThomas Hellstrom * Add @waiter to a given location in the lock wait_list and set the 19508295b3bSThomas Hellstrom * FLAG_WAITERS flag if it's the first waiter. 19608295b3bSThomas Hellstrom */ 1973a010c49SZqiang static void 19808295b3bSThomas Hellstrom __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, 19908295b3bSThomas Hellstrom struct list_head *list) 20008295b3bSThomas Hellstrom { 20108295b3bSThomas Hellstrom debug_mutex_add_waiter(lock, waiter, current); 20208295b3bSThomas Hellstrom 20308295b3bSThomas Hellstrom list_add_tail(&waiter->list, list); 20408295b3bSThomas Hellstrom if (__mutex_waiter_is_first(lock, waiter)) 20508295b3bSThomas Hellstrom __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); 20608295b3bSThomas Hellstrom } 20708295b3bSThomas Hellstrom 2083a010c49SZqiang static void 2093a010c49SZqiang __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter) 2103a010c49SZqiang { 2113a010c49SZqiang list_del(&waiter->list); 2123a010c49SZqiang if (likely(list_empty(&lock->wait_list))) 2133a010c49SZqiang __mutex_clear_flag(lock, MUTEX_FLAGS); 2143a010c49SZqiang 2153a010c49SZqiang debug_mutex_remove_waiter(lock, waiter, current); 2163a010c49SZqiang } 2173a010c49SZqiang 21808295b3bSThomas Hellstrom /* 2199d659ae1SPeter Zijlstra * Give up ownership to a specific task, when @task = NULL, this is equivalent 220e2db7592SIngo Molnar * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves 221e274795eSPeter Zijlstra * WAITERS. Provides RELEASE semantics like a regular unlock, the 222e274795eSPeter Zijlstra * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff. 2239d659ae1SPeter Zijlstra */ 2249d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task) 2259d659ae1SPeter Zijlstra { 2269d659ae1SPeter Zijlstra unsigned long owner = atomic_long_read(&lock->owner); 2279d659ae1SPeter Zijlstra 2289d659ae1SPeter Zijlstra for (;;) { 229ab4e4d9fSPeter Zijlstra unsigned long new; 2309d659ae1SPeter Zijlstra 231e6b4457bSPeter Zijlstra MUTEX_WARN_ON(__owner_task(owner) != current); 232e6b4457bSPeter Zijlstra MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 2339d659ae1SPeter Zijlstra 2349d659ae1SPeter Zijlstra new = (owner & MUTEX_FLAG_WAITERS); 2359d659ae1SPeter Zijlstra new |= (unsigned long)task; 236e274795eSPeter Zijlstra if (task) 237e274795eSPeter Zijlstra new |= MUTEX_FLAG_PICKUP; 2389d659ae1SPeter Zijlstra 239ab4e4d9fSPeter Zijlstra if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new)) 2409d659ae1SPeter Zijlstra break; 2419d659ae1SPeter Zijlstra } 2429d659ae1SPeter Zijlstra } 2439d659ae1SPeter Zijlstra 24401768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 24501768b42SPeter Zijlstra /* 24601768b42SPeter Zijlstra * We split the mutex lock/unlock logic into separate fastpath and 24701768b42SPeter Zijlstra * slowpath functions, to reduce the register pressure on the fastpath. 24801768b42SPeter Zijlstra * We also put the fastpath first in the kernel image, to make sure the 24901768b42SPeter Zijlstra * branch is predicted by the CPU as default-untaken. 25001768b42SPeter Zijlstra */ 2513ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock); 25201768b42SPeter Zijlstra 25301768b42SPeter Zijlstra /** 25401768b42SPeter Zijlstra * mutex_lock - acquire the mutex 25501768b42SPeter Zijlstra * @lock: the mutex to be acquired 25601768b42SPeter Zijlstra * 25701768b42SPeter Zijlstra * Lock the mutex exclusively for this task. If the mutex is not 25801768b42SPeter Zijlstra * available right now, it will sleep until it can get it. 25901768b42SPeter Zijlstra * 26001768b42SPeter Zijlstra * The mutex must later on be released by the same task that 26101768b42SPeter Zijlstra * acquired it. Recursive locking is not allowed. The task 26201768b42SPeter Zijlstra * may not exit without first unlocking the mutex. Also, kernel 263139b6fd2SSharon Dvir * memory where the mutex resides must not be freed with 26401768b42SPeter Zijlstra * the mutex still locked. The mutex must first be initialized 26501768b42SPeter Zijlstra * (or statically defined) before it can be locked. memset()-ing 26601768b42SPeter Zijlstra * the mutex to 0 is not allowed. 26701768b42SPeter Zijlstra * 26801768b42SPeter Zijlstra * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging 26901768b42SPeter Zijlstra * checks that will enforce the restrictions and will also do 2707b4ff1adSMauro Carvalho Chehab * deadlock debugging) 27101768b42SPeter Zijlstra * 27201768b42SPeter Zijlstra * This function is similar to (but not equivalent to) down(). 27301768b42SPeter Zijlstra */ 27401768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock) 27501768b42SPeter Zijlstra { 27601768b42SPeter Zijlstra might_sleep(); 27701768b42SPeter Zijlstra 2783ca0ff57SPeter Zijlstra if (!__mutex_trylock_fast(lock)) 2793ca0ff57SPeter Zijlstra __mutex_lock_slowpath(lock); 2803ca0ff57SPeter Zijlstra } 28101768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock); 28201768b42SPeter Zijlstra #endif 28301768b42SPeter Zijlstra 28455f036caSPeter Ziljstra /* 28555f036caSPeter Ziljstra * Wait-Die: 28655f036caSPeter Ziljstra * The newer transactions are killed when: 28755f036caSPeter Ziljstra * It (the new transaction) makes a request for a lock being held 28855f036caSPeter Ziljstra * by an older transaction. 28908295b3bSThomas Hellstrom * 29008295b3bSThomas Hellstrom * Wound-Wait: 29108295b3bSThomas Hellstrom * The newer transactions are wounded when: 29208295b3bSThomas Hellstrom * An older transaction makes a request for a lock being held by 29308295b3bSThomas Hellstrom * the newer transaction. 29455f036caSPeter Ziljstra */ 29555f036caSPeter Ziljstra 29655f036caSPeter Ziljstra /* 29755f036caSPeter Ziljstra * Associate the ww_mutex @ww with the context @ww_ctx under which we acquired 29855f036caSPeter Ziljstra * it. 29955f036caSPeter Ziljstra */ 300427b1820SPeter Zijlstra static __always_inline void 301427b1820SPeter Zijlstra ww_mutex_lock_acquired(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx) 30276916515SDavidlohr Bueso { 30376916515SDavidlohr Bueso #ifdef CONFIG_DEBUG_MUTEXES 30476916515SDavidlohr Bueso /* 30576916515SDavidlohr Bueso * If this WARN_ON triggers, you used ww_mutex_lock to acquire, 30676916515SDavidlohr Bueso * but released with a normal mutex_unlock in this call. 30776916515SDavidlohr Bueso * 30876916515SDavidlohr Bueso * This should never happen, always use ww_mutex_unlock. 30976916515SDavidlohr Bueso */ 31076916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww->ctx); 31176916515SDavidlohr Bueso 31276916515SDavidlohr Bueso /* 31376916515SDavidlohr Bueso * Not quite done after calling ww_acquire_done() ? 31476916515SDavidlohr Bueso */ 31576916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire); 31676916515SDavidlohr Bueso 31776916515SDavidlohr Bueso if (ww_ctx->contending_lock) { 31876916515SDavidlohr Bueso /* 31976916515SDavidlohr Bueso * After -EDEADLK you tried to 32076916515SDavidlohr Bueso * acquire a different ww_mutex? Bad! 32176916515SDavidlohr Bueso */ 32276916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww); 32376916515SDavidlohr Bueso 32476916515SDavidlohr Bueso /* 32576916515SDavidlohr Bueso * You called ww_mutex_lock after receiving -EDEADLK, 32676916515SDavidlohr Bueso * but 'forgot' to unlock everything else first? 32776916515SDavidlohr Bueso */ 32876916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0); 32976916515SDavidlohr Bueso ww_ctx->contending_lock = NULL; 33076916515SDavidlohr Bueso } 33176916515SDavidlohr Bueso 33276916515SDavidlohr Bueso /* 33376916515SDavidlohr Bueso * Naughty, using a different class will lead to undefined behavior! 33476916515SDavidlohr Bueso */ 33576916515SDavidlohr Bueso DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class); 33676916515SDavidlohr Bueso #endif 33776916515SDavidlohr Bueso ww_ctx->acquired++; 33855f036caSPeter Ziljstra ww->ctx = ww_ctx; 3393822da3eSNicolai Hähnle } 3403822da3eSNicolai Hähnle 34176916515SDavidlohr Bueso /* 34255f036caSPeter Ziljstra * Determine if context @a is 'after' context @b. IOW, @a is a younger 34355f036caSPeter Ziljstra * transaction than @b and depending on algorithm either needs to wait for 34455f036caSPeter Ziljstra * @b or die. 34555f036caSPeter Ziljstra */ 34655f036caSPeter Ziljstra static inline bool __sched 34755f036caSPeter Ziljstra __ww_ctx_stamp_after(struct ww_acquire_ctx *a, struct ww_acquire_ctx *b) 34855f036caSPeter Ziljstra { 34955f036caSPeter Ziljstra 35055f036caSPeter Ziljstra return (signed long)(a->stamp - b->stamp) > 0; 35155f036caSPeter Ziljstra } 35255f036caSPeter Ziljstra 35355f036caSPeter Ziljstra /* 35455f036caSPeter Ziljstra * Wait-Die; wake a younger waiter context (when locks held) such that it can 35555f036caSPeter Ziljstra * die. 356659cf9f5SNicolai Hähnle * 35755f036caSPeter Ziljstra * Among waiters with context, only the first one can have other locks acquired 35855f036caSPeter Ziljstra * already (ctx->acquired > 0), because __ww_mutex_add_waiter() and 35955f036caSPeter Ziljstra * __ww_mutex_check_kill() wake any but the earliest context. 36055f036caSPeter Ziljstra */ 36155f036caSPeter Ziljstra static bool __sched 36255f036caSPeter Ziljstra __ww_mutex_die(struct mutex *lock, struct mutex_waiter *waiter, 36355f036caSPeter Ziljstra struct ww_acquire_ctx *ww_ctx) 36455f036caSPeter Ziljstra { 36508295b3bSThomas Hellstrom if (!ww_ctx->is_wait_die) 36608295b3bSThomas Hellstrom return false; 36708295b3bSThomas Hellstrom 36855f036caSPeter Ziljstra if (waiter->ww_ctx->acquired > 0 && 36955f036caSPeter Ziljstra __ww_ctx_stamp_after(waiter->ww_ctx, ww_ctx)) { 37055f036caSPeter Ziljstra debug_mutex_wake_waiter(lock, waiter); 37155f036caSPeter Ziljstra wake_up_process(waiter->task); 37255f036caSPeter Ziljstra } 37355f036caSPeter Ziljstra 37455f036caSPeter Ziljstra return true; 37555f036caSPeter Ziljstra } 37655f036caSPeter Ziljstra 37755f036caSPeter Ziljstra /* 37808295b3bSThomas Hellstrom * Wound-Wait; wound a younger @hold_ctx if it holds the lock. 37908295b3bSThomas Hellstrom * 38008295b3bSThomas Hellstrom * Wound the lock holder if there are waiters with older transactions than 38108295b3bSThomas Hellstrom * the lock holders. Even if multiple waiters may wound the lock holder, 38208295b3bSThomas Hellstrom * it's sufficient that only one does. 38308295b3bSThomas Hellstrom */ 38408295b3bSThomas Hellstrom static bool __ww_mutex_wound(struct mutex *lock, 38508295b3bSThomas Hellstrom struct ww_acquire_ctx *ww_ctx, 38608295b3bSThomas Hellstrom struct ww_acquire_ctx *hold_ctx) 38708295b3bSThomas Hellstrom { 38808295b3bSThomas Hellstrom struct task_struct *owner = __mutex_owner(lock); 38908295b3bSThomas Hellstrom 39008295b3bSThomas Hellstrom lockdep_assert_held(&lock->wait_lock); 39108295b3bSThomas Hellstrom 39208295b3bSThomas Hellstrom /* 39308295b3bSThomas Hellstrom * Possible through __ww_mutex_add_waiter() when we race with 39408295b3bSThomas Hellstrom * ww_mutex_set_context_fastpath(). In that case we'll get here again 39508295b3bSThomas Hellstrom * through __ww_mutex_check_waiters(). 39608295b3bSThomas Hellstrom */ 39708295b3bSThomas Hellstrom if (!hold_ctx) 39808295b3bSThomas Hellstrom return false; 39908295b3bSThomas Hellstrom 40008295b3bSThomas Hellstrom /* 40108295b3bSThomas Hellstrom * Can have !owner because of __mutex_unlock_slowpath(), but if owner, 40208295b3bSThomas Hellstrom * it cannot go away because we'll have FLAG_WAITERS set and hold 40308295b3bSThomas Hellstrom * wait_lock. 40408295b3bSThomas Hellstrom */ 40508295b3bSThomas Hellstrom if (!owner) 40608295b3bSThomas Hellstrom return false; 40708295b3bSThomas Hellstrom 40808295b3bSThomas Hellstrom if (ww_ctx->acquired > 0 && __ww_ctx_stamp_after(hold_ctx, ww_ctx)) { 40908295b3bSThomas Hellstrom hold_ctx->wounded = 1; 41008295b3bSThomas Hellstrom 41108295b3bSThomas Hellstrom /* 41208295b3bSThomas Hellstrom * wake_up_process() paired with set_current_state() 41308295b3bSThomas Hellstrom * inserts sufficient barriers to make sure @owner either sees 414e13e2366SThomas Hellstrom * it's wounded in __ww_mutex_check_kill() or has a 41508295b3bSThomas Hellstrom * wakeup pending to re-read the wounded state. 41608295b3bSThomas Hellstrom */ 41708295b3bSThomas Hellstrom if (owner != current) 41808295b3bSThomas Hellstrom wake_up_process(owner); 41908295b3bSThomas Hellstrom 42008295b3bSThomas Hellstrom return true; 42108295b3bSThomas Hellstrom } 42208295b3bSThomas Hellstrom 42308295b3bSThomas Hellstrom return false; 42408295b3bSThomas Hellstrom } 42508295b3bSThomas Hellstrom 42608295b3bSThomas Hellstrom /* 42755f036caSPeter Ziljstra * We just acquired @lock under @ww_ctx, if there are later contexts waiting 42808295b3bSThomas Hellstrom * behind us on the wait-list, check if they need to die, or wound us. 42955f036caSPeter Ziljstra * 43055f036caSPeter Ziljstra * See __ww_mutex_add_waiter() for the list-order construction; basically the 43155f036caSPeter Ziljstra * list is ordered by stamp, smallest (oldest) first. 432659cf9f5SNicolai Hähnle * 43308295b3bSThomas Hellstrom * This relies on never mixing wait-die/wound-wait on the same wait-list; 43408295b3bSThomas Hellstrom * which is currently ensured by that being a ww_class property. 43508295b3bSThomas Hellstrom * 436659cf9f5SNicolai Hähnle * The current task must not be on the wait list. 437659cf9f5SNicolai Hähnle */ 438659cf9f5SNicolai Hähnle static void __sched 43955f036caSPeter Ziljstra __ww_mutex_check_waiters(struct mutex *lock, struct ww_acquire_ctx *ww_ctx) 440659cf9f5SNicolai Hähnle { 441659cf9f5SNicolai Hähnle struct mutex_waiter *cur; 442659cf9f5SNicolai Hähnle 443659cf9f5SNicolai Hähnle lockdep_assert_held(&lock->wait_lock); 444659cf9f5SNicolai Hähnle 445659cf9f5SNicolai Hähnle list_for_each_entry(cur, &lock->wait_list, list) { 446659cf9f5SNicolai Hähnle if (!cur->ww_ctx) 447659cf9f5SNicolai Hähnle continue; 448659cf9f5SNicolai Hähnle 44908295b3bSThomas Hellstrom if (__ww_mutex_die(lock, cur, ww_ctx) || 45008295b3bSThomas Hellstrom __ww_mutex_wound(lock, cur->ww_ctx, ww_ctx)) 451659cf9f5SNicolai Hähnle break; 452659cf9f5SNicolai Hähnle } 453659cf9f5SNicolai Hähnle } 454659cf9f5SNicolai Hähnle 45576916515SDavidlohr Bueso /* 45655f036caSPeter Ziljstra * After acquiring lock with fastpath, where we do not hold wait_lock, set ctx 45755f036caSPeter Ziljstra * and wake up any waiters so they can recheck. 45876916515SDavidlohr Bueso */ 45976916515SDavidlohr Bueso static __always_inline void 460427b1820SPeter Zijlstra ww_mutex_set_context_fastpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 46176916515SDavidlohr Bueso { 46276916515SDavidlohr Bueso ww_mutex_lock_acquired(lock, ctx); 46376916515SDavidlohr Bueso 46476916515SDavidlohr Bueso /* 46576916515SDavidlohr Bueso * The lock->ctx update should be visible on all cores before 46655f036caSPeter Ziljstra * the WAITERS check is done, otherwise contended waiters might be 46776916515SDavidlohr Bueso * missed. The contended waiters will either see ww_ctx == NULL 46876916515SDavidlohr Bueso * and keep spinning, or it will acquire wait_lock, add itself 46976916515SDavidlohr Bueso * to waiter list and sleep. 47076916515SDavidlohr Bueso */ 47108295b3bSThomas Hellstrom smp_mb(); /* See comments above and below. */ 47276916515SDavidlohr Bueso 47376916515SDavidlohr Bueso /* 47408295b3bSThomas Hellstrom * [W] ww->ctx = ctx [W] MUTEX_FLAG_WAITERS 47508295b3bSThomas Hellstrom * MB MB 47608295b3bSThomas Hellstrom * [R] MUTEX_FLAG_WAITERS [R] ww->ctx 47708295b3bSThomas Hellstrom * 47808295b3bSThomas Hellstrom * The memory barrier above pairs with the memory barrier in 47908295b3bSThomas Hellstrom * __ww_mutex_add_waiter() and makes sure we either observe ww->ctx 48008295b3bSThomas Hellstrom * and/or !empty list. 48176916515SDavidlohr Bueso */ 4823ca0ff57SPeter Zijlstra if (likely(!(atomic_long_read(&lock->base.owner) & MUTEX_FLAG_WAITERS))) 48376916515SDavidlohr Bueso return; 48476916515SDavidlohr Bueso 48576916515SDavidlohr Bueso /* 48655f036caSPeter Ziljstra * Uh oh, we raced in fastpath, check if any of the waiters need to 48708295b3bSThomas Hellstrom * die or wound us. 48876916515SDavidlohr Bueso */ 489ebf4c55cSThomas Gleixner raw_spin_lock(&lock->base.wait_lock); 49055f036caSPeter Ziljstra __ww_mutex_check_waiters(&lock->base, ctx); 491ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->base.wait_lock); 49276916515SDavidlohr Bueso } 49376916515SDavidlohr Bueso 49401768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 495c516df97SNicolai Hähnle 496ad90880dSPeter Zijlstra /* 497ad90880dSPeter Zijlstra * Trylock variant that returns the owning task on failure. 498ad90880dSPeter Zijlstra */ 499ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock) 500ad90880dSPeter Zijlstra { 501ad90880dSPeter Zijlstra return __mutex_trylock_common(lock, false); 502ad90880dSPeter Zijlstra } 503ad90880dSPeter Zijlstra 504c516df97SNicolai Hähnle static inline 505c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 506c516df97SNicolai Hähnle struct mutex_waiter *waiter) 507c516df97SNicolai Hähnle { 508c516df97SNicolai Hähnle struct ww_mutex *ww; 509c516df97SNicolai Hähnle 510c516df97SNicolai Hähnle ww = container_of(lock, struct ww_mutex, base); 511c516df97SNicolai Hähnle 51201768b42SPeter Zijlstra /* 513c516df97SNicolai Hähnle * If ww->ctx is set the contents are undefined, only 514c516df97SNicolai Hähnle * by acquiring wait_lock there is a guarantee that 515c516df97SNicolai Hähnle * they are not invalid when reading. 516c516df97SNicolai Hähnle * 517c516df97SNicolai Hähnle * As such, when deadlock detection needs to be 518c516df97SNicolai Hähnle * performed the optimistic spinning cannot be done. 519c516df97SNicolai Hähnle * 520c516df97SNicolai Hähnle * Check this in every inner iteration because we may 521c516df97SNicolai Hähnle * be racing against another thread's ww_mutex_lock. 522c516df97SNicolai Hähnle */ 523c516df97SNicolai Hähnle if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx)) 524c516df97SNicolai Hähnle return false; 525c516df97SNicolai Hähnle 526c516df97SNicolai Hähnle /* 527c516df97SNicolai Hähnle * If we aren't on the wait list yet, cancel the spin 528c516df97SNicolai Hähnle * if there are waiters. We want to avoid stealing the 529c516df97SNicolai Hähnle * lock from a waiter with an earlier stamp, since the 530c516df97SNicolai Hähnle * other thread may already own a lock that we also 531c516df97SNicolai Hähnle * need. 532c516df97SNicolai Hähnle */ 533c516df97SNicolai Hähnle if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS)) 534c516df97SNicolai Hähnle return false; 535c516df97SNicolai Hähnle 536c516df97SNicolai Hähnle /* 537c516df97SNicolai Hähnle * Similarly, stop spinning if we are no longer the 538c516df97SNicolai Hähnle * first waiter. 539c516df97SNicolai Hähnle */ 540c516df97SNicolai Hähnle if (waiter && !__mutex_waiter_is_first(lock, waiter)) 541c516df97SNicolai Hähnle return false; 542c516df97SNicolai Hähnle 543c516df97SNicolai Hähnle return true; 544c516df97SNicolai Hähnle } 545c516df97SNicolai Hähnle 54601768b42SPeter Zijlstra /* 54725f13b40SNicolai Hähnle * Look out! "owner" is an entirely speculative pointer access and not 54825f13b40SNicolai Hähnle * reliable. 54925f13b40SNicolai Hähnle * 55025f13b40SNicolai Hähnle * "noinline" so that this function shows up on perf profiles. 55101768b42SPeter Zijlstra */ 55201768b42SPeter Zijlstra static noinline 55325f13b40SNicolai Hähnle bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, 554c516df97SNicolai Hähnle struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter) 55501768b42SPeter Zijlstra { 55601ac33c1SJason Low bool ret = true; 557be1f7bf2SJason Low 55801768b42SPeter Zijlstra rcu_read_lock(); 5593ca0ff57SPeter Zijlstra while (__mutex_owner(lock) == owner) { 560be1f7bf2SJason Low /* 561be1f7bf2SJason Low * Ensure we emit the owner->on_cpu, dereference _after_ 56201ac33c1SJason Low * checking lock->owner still matches owner. If that fails, 56301ac33c1SJason Low * owner might point to freed memory. If it still matches, 564be1f7bf2SJason Low * the rcu_read_lock() ensures the memory stays valid. 565be1f7bf2SJason Low */ 566be1f7bf2SJason Low barrier(); 567be1f7bf2SJason Low 56805ffc951SPan Xinhui /* 56905ffc951SPan Xinhui * Use vcpu_is_preempted to detect lock holder preemption issue. 57005ffc951SPan Xinhui */ 57105ffc951SPan Xinhui if (!owner->on_cpu || need_resched() || 57205ffc951SPan Xinhui vcpu_is_preempted(task_cpu(owner))) { 573be1f7bf2SJason Low ret = false; 574be1f7bf2SJason Low break; 575be1f7bf2SJason Low } 57601768b42SPeter Zijlstra 577c516df97SNicolai Hähnle if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) { 57825f13b40SNicolai Hähnle ret = false; 57925f13b40SNicolai Hähnle break; 58025f13b40SNicolai Hähnle } 58125f13b40SNicolai Hähnle 582f2f09a4cSChristian Borntraeger cpu_relax(); 58301768b42SPeter Zijlstra } 58401768b42SPeter Zijlstra rcu_read_unlock(); 58501768b42SPeter Zijlstra 586be1f7bf2SJason Low return ret; 58701768b42SPeter Zijlstra } 58801768b42SPeter Zijlstra 58901768b42SPeter Zijlstra /* 59001768b42SPeter Zijlstra * Initial check for entering the mutex spinning loop 59101768b42SPeter Zijlstra */ 59201768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock) 59301768b42SPeter Zijlstra { 59401768b42SPeter Zijlstra struct task_struct *owner; 59501768b42SPeter Zijlstra int retval = 1; 59601768b42SPeter Zijlstra 59746af29e4SJason Low if (need_resched()) 59846af29e4SJason Low return 0; 59946af29e4SJason Low 60001768b42SPeter Zijlstra rcu_read_lock(); 6013ca0ff57SPeter Zijlstra owner = __mutex_owner(lock); 60205ffc951SPan Xinhui 60305ffc951SPan Xinhui /* 60405ffc951SPan Xinhui * As lock holder preemption issue, we both skip spinning if task is not 60505ffc951SPan Xinhui * on cpu or its cpu is preempted 60605ffc951SPan Xinhui */ 60701768b42SPeter Zijlstra if (owner) 60805ffc951SPan Xinhui retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner)); 60901768b42SPeter Zijlstra rcu_read_unlock(); 61076916515SDavidlohr Bueso 61176916515SDavidlohr Bueso /* 6123ca0ff57SPeter Zijlstra * If lock->owner is not set, the mutex has been released. Return true 6133ca0ff57SPeter Zijlstra * such that we'll trylock in the spin path, which is a faster option 6143ca0ff57SPeter Zijlstra * than the blocking slow path. 61576916515SDavidlohr Bueso */ 6163ca0ff57SPeter Zijlstra return retval; 61776916515SDavidlohr Bueso } 61876916515SDavidlohr Bueso 61976916515SDavidlohr Bueso /* 62076916515SDavidlohr Bueso * Optimistic spinning. 62176916515SDavidlohr Bueso * 62276916515SDavidlohr Bueso * We try to spin for acquisition when we find that the lock owner 62376916515SDavidlohr Bueso * is currently running on a (different) CPU and while we don't 62476916515SDavidlohr Bueso * need to reschedule. The rationale is that if the lock owner is 62576916515SDavidlohr Bueso * running, it is likely to release the lock soon. 62676916515SDavidlohr Bueso * 62776916515SDavidlohr Bueso * The mutex spinners are queued up using MCS lock so that only one 62876916515SDavidlohr Bueso * spinner can compete for the mutex. However, if mutex spinning isn't 62976916515SDavidlohr Bueso * going to happen, there is no point in going through the lock/unlock 63076916515SDavidlohr Bueso * overhead. 63176916515SDavidlohr Bueso * 63276916515SDavidlohr Bueso * Returns true when the lock was taken, otherwise false, indicating 63376916515SDavidlohr Bueso * that we need to jump to the slowpath and sleep. 634b341afb3SWaiman Long * 635b341afb3SWaiman Long * The waiter flag is set to true if the spinner is a waiter in the wait 636b341afb3SWaiman Long * queue. The waiter-spinner will spin on the lock directly and concurrently 637b341afb3SWaiman Long * with the spinner at the head of the OSQ, if present, until the owner is 638b341afb3SWaiman Long * changed to itself. 63976916515SDavidlohr Bueso */ 640427b1820SPeter Zijlstra static __always_inline bool 641427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 6425de2055dSWaiman Long struct mutex_waiter *waiter) 64376916515SDavidlohr Bueso { 644b341afb3SWaiman Long if (!waiter) { 645b341afb3SWaiman Long /* 646b341afb3SWaiman Long * The purpose of the mutex_can_spin_on_owner() function is 647b341afb3SWaiman Long * to eliminate the overhead of osq_lock() and osq_unlock() 648b341afb3SWaiman Long * in case spinning isn't possible. As a waiter-spinner 649b341afb3SWaiman Long * is not going to take OSQ lock anyway, there is no need 650b341afb3SWaiman Long * to call mutex_can_spin_on_owner(). 651b341afb3SWaiman Long */ 65276916515SDavidlohr Bueso if (!mutex_can_spin_on_owner(lock)) 653b341afb3SWaiman Long goto fail; 65476916515SDavidlohr Bueso 655e42f678aSDavidlohr Bueso /* 656e42f678aSDavidlohr Bueso * In order to avoid a stampede of mutex spinners trying to 657e42f678aSDavidlohr Bueso * acquire the mutex all at once, the spinners need to take a 658e42f678aSDavidlohr Bueso * MCS (queued) lock first before spinning on the owner field. 659e42f678aSDavidlohr Bueso */ 66076916515SDavidlohr Bueso if (!osq_lock(&lock->osq)) 661b341afb3SWaiman Long goto fail; 662b341afb3SWaiman Long } 66376916515SDavidlohr Bueso 664b341afb3SWaiman Long for (;;) { 66576916515SDavidlohr Bueso struct task_struct *owner; 66676916515SDavidlohr Bueso 667e274795eSPeter Zijlstra /* Try to acquire the mutex... */ 668e274795eSPeter Zijlstra owner = __mutex_trylock_or_owner(lock); 669e274795eSPeter Zijlstra if (!owner) 670e274795eSPeter Zijlstra break; 67176916515SDavidlohr Bueso 67276916515SDavidlohr Bueso /* 673e274795eSPeter Zijlstra * There's an owner, wait for it to either 67476916515SDavidlohr Bueso * release the lock or go to sleep. 67576916515SDavidlohr Bueso */ 676c516df97SNicolai Hähnle if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter)) 677b341afb3SWaiman Long goto fail_unlock; 67876916515SDavidlohr Bueso 67976916515SDavidlohr Bueso /* 68076916515SDavidlohr Bueso * The cpu_relax() call is a compiler barrier which forces 68176916515SDavidlohr Bueso * everything in this loop to be re-loaded. We don't need 68276916515SDavidlohr Bueso * memory barriers as we'll eventually observe the right 68376916515SDavidlohr Bueso * values at the cost of a few extra spins. 68476916515SDavidlohr Bueso */ 685f2f09a4cSChristian Borntraeger cpu_relax(); 68676916515SDavidlohr Bueso } 68776916515SDavidlohr Bueso 688b341afb3SWaiman Long if (!waiter) 68976916515SDavidlohr Bueso osq_unlock(&lock->osq); 690b341afb3SWaiman Long 691b341afb3SWaiman Long return true; 692b341afb3SWaiman Long 693b341afb3SWaiman Long 694b341afb3SWaiman Long fail_unlock: 695b341afb3SWaiman Long if (!waiter) 696b341afb3SWaiman Long osq_unlock(&lock->osq); 697b341afb3SWaiman Long 698b341afb3SWaiman Long fail: 69976916515SDavidlohr Bueso /* 70076916515SDavidlohr Bueso * If we fell out of the spin path because of need_resched(), 70176916515SDavidlohr Bueso * reschedule now, before we try-lock the mutex. This avoids getting 70276916515SDavidlohr Bueso * scheduled out right after we obtained the mutex. 70376916515SDavidlohr Bueso */ 7046f942a1fSPeter Zijlstra if (need_resched()) { 7056f942a1fSPeter Zijlstra /* 7066f942a1fSPeter Zijlstra * We _should_ have TASK_RUNNING here, but just in case 7076f942a1fSPeter Zijlstra * we do not, make it so, otherwise we might get stuck. 7086f942a1fSPeter Zijlstra */ 7096f942a1fSPeter Zijlstra __set_current_state(TASK_RUNNING); 71076916515SDavidlohr Bueso schedule_preempt_disabled(); 7116f942a1fSPeter Zijlstra } 71276916515SDavidlohr Bueso 71376916515SDavidlohr Bueso return false; 71476916515SDavidlohr Bueso } 71576916515SDavidlohr Bueso #else 716427b1820SPeter Zijlstra static __always_inline bool 717427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 7185de2055dSWaiman Long struct mutex_waiter *waiter) 71976916515SDavidlohr Bueso { 72076916515SDavidlohr Bueso return false; 72176916515SDavidlohr Bueso } 72201768b42SPeter Zijlstra #endif 72301768b42SPeter Zijlstra 7243ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip); 72501768b42SPeter Zijlstra 72601768b42SPeter Zijlstra /** 72701768b42SPeter Zijlstra * mutex_unlock - release the mutex 72801768b42SPeter Zijlstra * @lock: the mutex to be released 72901768b42SPeter Zijlstra * 73001768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously. 73101768b42SPeter Zijlstra * 73201768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 73301768b42SPeter Zijlstra * of a not locked mutex is not allowed. 73401768b42SPeter Zijlstra * 73501768b42SPeter Zijlstra * This function is similar to (but not equivalent to) up(). 73601768b42SPeter Zijlstra */ 73701768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock) 73801768b42SPeter Zijlstra { 7393ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 7403ca0ff57SPeter Zijlstra if (__mutex_unlock_fast(lock)) 7413ca0ff57SPeter Zijlstra return; 74201768b42SPeter Zijlstra #endif 7433ca0ff57SPeter Zijlstra __mutex_unlock_slowpath(lock, _RET_IP_); 74401768b42SPeter Zijlstra } 74501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock); 74601768b42SPeter Zijlstra 747*aaa77de1SPeter Zijlstra (Intel) static void __ww_mutex_unlock(struct ww_mutex *lock) 748*aaa77de1SPeter Zijlstra (Intel) { 749*aaa77de1SPeter Zijlstra (Intel) /* 750*aaa77de1SPeter Zijlstra (Intel) * The unlocking fastpath is the 0->1 transition from 'locked' 751*aaa77de1SPeter Zijlstra (Intel) * into 'unlocked' state: 752*aaa77de1SPeter Zijlstra (Intel) */ 753*aaa77de1SPeter Zijlstra (Intel) if (lock->ctx) { 754*aaa77de1SPeter Zijlstra (Intel) MUTEX_WARN_ON(!lock->ctx->acquired); 755*aaa77de1SPeter Zijlstra (Intel) if (lock->ctx->acquired > 0) 756*aaa77de1SPeter Zijlstra (Intel) lock->ctx->acquired--; 757*aaa77de1SPeter Zijlstra (Intel) lock->ctx = NULL; 758*aaa77de1SPeter Zijlstra (Intel) } 759*aaa77de1SPeter Zijlstra (Intel) } 760*aaa77de1SPeter Zijlstra (Intel) 76101768b42SPeter Zijlstra /** 76201768b42SPeter Zijlstra * ww_mutex_unlock - release the w/w mutex 76301768b42SPeter Zijlstra * @lock: the mutex to be released 76401768b42SPeter Zijlstra * 76501768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously with any of the 76601768b42SPeter Zijlstra * ww_mutex_lock* functions (with or without an acquire context). It is 76701768b42SPeter Zijlstra * forbidden to release the locks after releasing the acquire context. 76801768b42SPeter Zijlstra * 76901768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 77001768b42SPeter Zijlstra * of a unlocked mutex is not allowed. 77101768b42SPeter Zijlstra */ 77201768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock) 77301768b42SPeter Zijlstra { 774*aaa77de1SPeter Zijlstra (Intel) __ww_mutex_unlock(lock); 7753ca0ff57SPeter Zijlstra mutex_unlock(&lock->base); 77601768b42SPeter Zijlstra } 77701768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock); 77801768b42SPeter Zijlstra 77955f036caSPeter Ziljstra 78055f036caSPeter Ziljstra static __always_inline int __sched 78155f036caSPeter Ziljstra __ww_mutex_kill(struct mutex *lock, struct ww_acquire_ctx *ww_ctx) 78255f036caSPeter Ziljstra { 78355f036caSPeter Ziljstra if (ww_ctx->acquired > 0) { 78455f036caSPeter Ziljstra #ifdef CONFIG_DEBUG_MUTEXES 78555f036caSPeter Ziljstra struct ww_mutex *ww; 78655f036caSPeter Ziljstra 78755f036caSPeter Ziljstra ww = container_of(lock, struct ww_mutex, base); 78855f036caSPeter Ziljstra DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock); 78955f036caSPeter Ziljstra ww_ctx->contending_lock = ww; 79055f036caSPeter Ziljstra #endif 79155f036caSPeter Ziljstra return -EDEADLK; 79255f036caSPeter Ziljstra } 79355f036caSPeter Ziljstra 79455f036caSPeter Ziljstra return 0; 79555f036caSPeter Ziljstra } 79655f036caSPeter Ziljstra 79755f036caSPeter Ziljstra 79855f036caSPeter Ziljstra /* 79908295b3bSThomas Hellstrom * Check the wound condition for the current lock acquire. 80008295b3bSThomas Hellstrom * 80108295b3bSThomas Hellstrom * Wound-Wait: If we're wounded, kill ourself. 80255f036caSPeter Ziljstra * 80355f036caSPeter Ziljstra * Wait-Die: If we're trying to acquire a lock already held by an older 80455f036caSPeter Ziljstra * context, kill ourselves. 80555f036caSPeter Ziljstra * 80655f036caSPeter Ziljstra * Since __ww_mutex_add_waiter() orders the wait-list on stamp, we only have to 80755f036caSPeter Ziljstra * look at waiters before us in the wait-list. 80855f036caSPeter Ziljstra */ 80901768b42SPeter Zijlstra static inline int __sched 81055f036caSPeter Ziljstra __ww_mutex_check_kill(struct mutex *lock, struct mutex_waiter *waiter, 811200b1874SNicolai Hähnle struct ww_acquire_ctx *ctx) 81201768b42SPeter Zijlstra { 81301768b42SPeter Zijlstra struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 8144d3199e4SDavidlohr Bueso struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx); 815200b1874SNicolai Hähnle struct mutex_waiter *cur; 81601768b42SPeter Zijlstra 81755f036caSPeter Ziljstra if (ctx->acquired == 0) 81855f036caSPeter Ziljstra return 0; 81955f036caSPeter Ziljstra 82008295b3bSThomas Hellstrom if (!ctx->is_wait_die) { 82108295b3bSThomas Hellstrom if (ctx->wounded) 82208295b3bSThomas Hellstrom return __ww_mutex_kill(lock, ctx); 82308295b3bSThomas Hellstrom 82408295b3bSThomas Hellstrom return 0; 82508295b3bSThomas Hellstrom } 82608295b3bSThomas Hellstrom 827200b1874SNicolai Hähnle if (hold_ctx && __ww_ctx_stamp_after(ctx, hold_ctx)) 82855f036caSPeter Ziljstra return __ww_mutex_kill(lock, ctx); 829200b1874SNicolai Hähnle 830200b1874SNicolai Hähnle /* 831200b1874SNicolai Hähnle * If there is a waiter in front of us that has a context, then its 83255f036caSPeter Ziljstra * stamp is earlier than ours and we must kill ourself. 833200b1874SNicolai Hähnle */ 834200b1874SNicolai Hähnle cur = waiter; 835200b1874SNicolai Hähnle list_for_each_entry_continue_reverse(cur, &lock->wait_list, list) { 83655f036caSPeter Ziljstra if (!cur->ww_ctx) 83755f036caSPeter Ziljstra continue; 83855f036caSPeter Ziljstra 83955f036caSPeter Ziljstra return __ww_mutex_kill(lock, ctx); 840200b1874SNicolai Hähnle } 841200b1874SNicolai Hähnle 84201768b42SPeter Zijlstra return 0; 84301768b42SPeter Zijlstra } 84401768b42SPeter Zijlstra 84555f036caSPeter Ziljstra /* 84655f036caSPeter Ziljstra * Add @waiter to the wait-list, keep the wait-list ordered by stamp, smallest 84755f036caSPeter Ziljstra * first. Such that older contexts are preferred to acquire the lock over 84855f036caSPeter Ziljstra * younger contexts. 84955f036caSPeter Ziljstra * 85055f036caSPeter Ziljstra * Waiters without context are interspersed in FIFO order. 85155f036caSPeter Ziljstra * 85255f036caSPeter Ziljstra * Furthermore, for Wait-Die kill ourself immediately when possible (there are 85308295b3bSThomas Hellstrom * older contexts already waiting) to avoid unnecessary waiting and for 85408295b3bSThomas Hellstrom * Wound-Wait ensure we wound the owning context when it is younger. 85555f036caSPeter Ziljstra */ 8566baa5c60SNicolai Hähnle static inline int __sched 8576baa5c60SNicolai Hähnle __ww_mutex_add_waiter(struct mutex_waiter *waiter, 8586baa5c60SNicolai Hähnle struct mutex *lock, 8596baa5c60SNicolai Hähnle struct ww_acquire_ctx *ww_ctx) 8606baa5c60SNicolai Hähnle { 8616baa5c60SNicolai Hähnle struct mutex_waiter *cur; 8626baa5c60SNicolai Hähnle struct list_head *pos; 86308295b3bSThomas Hellstrom bool is_wait_die; 8646baa5c60SNicolai Hähnle 8656baa5c60SNicolai Hähnle if (!ww_ctx) { 86608295b3bSThomas Hellstrom __mutex_add_waiter(lock, waiter, &lock->wait_list); 8676baa5c60SNicolai Hähnle return 0; 8686baa5c60SNicolai Hähnle } 8696baa5c60SNicolai Hähnle 87008295b3bSThomas Hellstrom is_wait_die = ww_ctx->is_wait_die; 87108295b3bSThomas Hellstrom 8726baa5c60SNicolai Hähnle /* 8736baa5c60SNicolai Hähnle * Add the waiter before the first waiter with a higher stamp. 8746baa5c60SNicolai Hähnle * Waiters without a context are skipped to avoid starving 87508295b3bSThomas Hellstrom * them. Wait-Die waiters may die here. Wound-Wait waiters 87608295b3bSThomas Hellstrom * never die here, but they are sorted in stamp order and 87708295b3bSThomas Hellstrom * may wound the lock holder. 8786baa5c60SNicolai Hähnle */ 8796baa5c60SNicolai Hähnle pos = &lock->wait_list; 8806baa5c60SNicolai Hähnle list_for_each_entry_reverse(cur, &lock->wait_list, list) { 8816baa5c60SNicolai Hähnle if (!cur->ww_ctx) 8826baa5c60SNicolai Hähnle continue; 8836baa5c60SNicolai Hähnle 8846baa5c60SNicolai Hähnle if (__ww_ctx_stamp_after(ww_ctx, cur->ww_ctx)) { 88555f036caSPeter Ziljstra /* 88655f036caSPeter Ziljstra * Wait-Die: if we find an older context waiting, there 88755f036caSPeter Ziljstra * is no point in queueing behind it, as we'd have to 88855f036caSPeter Ziljstra * die the moment it would acquire the lock. 88955f036caSPeter Ziljstra */ 89008295b3bSThomas Hellstrom if (is_wait_die) { 89155f036caSPeter Ziljstra int ret = __ww_mutex_kill(lock, ww_ctx); 8926baa5c60SNicolai Hähnle 89355f036caSPeter Ziljstra if (ret) 89455f036caSPeter Ziljstra return ret; 89508295b3bSThomas Hellstrom } 8966baa5c60SNicolai Hähnle 8976baa5c60SNicolai Hähnle break; 8986baa5c60SNicolai Hähnle } 8996baa5c60SNicolai Hähnle 9006baa5c60SNicolai Hähnle pos = &cur->list; 901200b1874SNicolai Hähnle 90255f036caSPeter Ziljstra /* Wait-Die: ensure younger waiters die. */ 90355f036caSPeter Ziljstra __ww_mutex_die(lock, cur, ww_ctx); 9046baa5c60SNicolai Hähnle } 9056baa5c60SNicolai Hähnle 90608295b3bSThomas Hellstrom __mutex_add_waiter(lock, waiter, pos); 90708295b3bSThomas Hellstrom 90808295b3bSThomas Hellstrom /* 90908295b3bSThomas Hellstrom * Wound-Wait: if we're blocking on a mutex owned by a younger context, 91008295b3bSThomas Hellstrom * wound that such that we might proceed. 91108295b3bSThomas Hellstrom */ 91208295b3bSThomas Hellstrom if (!is_wait_die) { 91308295b3bSThomas Hellstrom struct ww_mutex *ww = container_of(lock, struct ww_mutex, base); 91408295b3bSThomas Hellstrom 91508295b3bSThomas Hellstrom /* 91608295b3bSThomas Hellstrom * See ww_mutex_set_context_fastpath(). Orders setting 91708295b3bSThomas Hellstrom * MUTEX_FLAG_WAITERS vs the ww->ctx load, 91808295b3bSThomas Hellstrom * such that either we or the fastpath will wound @ww->ctx. 91908295b3bSThomas Hellstrom */ 92008295b3bSThomas Hellstrom smp_mb(); 92108295b3bSThomas Hellstrom __ww_mutex_wound(lock, ww_ctx, ww->ctx); 92208295b3bSThomas Hellstrom } 92355f036caSPeter Ziljstra 92401768b42SPeter Zijlstra return 0; 92501768b42SPeter Zijlstra } 92601768b42SPeter Zijlstra 92701768b42SPeter Zijlstra /* 92801768b42SPeter Zijlstra * Lock a mutex (possibly interruptible), slowpath: 92901768b42SPeter Zijlstra */ 93001768b42SPeter Zijlstra static __always_inline int __sched 9312f064a59SPeter Zijlstra __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass, 93201768b42SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip, 93301768b42SPeter Zijlstra struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 93401768b42SPeter Zijlstra { 93501768b42SPeter Zijlstra struct mutex_waiter waiter; 936a40ca565SWaiman Long struct ww_mutex *ww; 93701768b42SPeter Zijlstra int ret; 93801768b42SPeter Zijlstra 9395de2055dSWaiman Long if (!use_ww_ctx) 9405de2055dSWaiman Long ww_ctx = NULL; 9415de2055dSWaiman Long 942427b1820SPeter Zijlstra might_sleep(); 943ea9e0fb8SNicolai Hähnle 944e6b4457bSPeter Zijlstra MUTEX_WARN_ON(lock->magic != lock); 9456c11c6e3SSebastian Andrzej Siewior 946a40ca565SWaiman Long ww = container_of(lock, struct ww_mutex, base); 9475de2055dSWaiman Long if (ww_ctx) { 9480422e83dSChris Wilson if (unlikely(ww_ctx == READ_ONCE(ww->ctx))) 9490422e83dSChris Wilson return -EALREADY; 95008295b3bSThomas Hellstrom 95108295b3bSThomas Hellstrom /* 95208295b3bSThomas Hellstrom * Reset the wounded flag after a kill. No other process can 95308295b3bSThomas Hellstrom * race and wound us here since they can't have a valid owner 95408295b3bSThomas Hellstrom * pointer if we don't have any locks held. 95508295b3bSThomas Hellstrom */ 95608295b3bSThomas Hellstrom if (ww_ctx->acquired == 0) 95708295b3bSThomas Hellstrom ww_ctx->wounded = 0; 958cf702eddSPeter Zijlstra 959cf702eddSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 960cf702eddSPeter Zijlstra nest_lock = &ww_ctx->dep_map; 961cf702eddSPeter Zijlstra #endif 9620422e83dSChris Wilson } 9630422e83dSChris Wilson 96401768b42SPeter Zijlstra preempt_disable(); 96501768b42SPeter Zijlstra mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 96601768b42SPeter Zijlstra 967e274795eSPeter Zijlstra if (__mutex_trylock(lock) || 9685de2055dSWaiman Long mutex_optimistic_spin(lock, ww_ctx, NULL)) { 96976916515SDavidlohr Bueso /* got the lock, yay! */ 9703ca0ff57SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 9715de2055dSWaiman Long if (ww_ctx) 9723ca0ff57SPeter Zijlstra ww_mutex_set_context_fastpath(ww, ww_ctx); 97301768b42SPeter Zijlstra preempt_enable(); 97401768b42SPeter Zijlstra return 0; 97501768b42SPeter Zijlstra } 97601768b42SPeter Zijlstra 977ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 9781e820c96SJason Low /* 9793ca0ff57SPeter Zijlstra * After waiting to acquire the wait_lock, try again. 9801e820c96SJason Low */ 981659cf9f5SNicolai Hähnle if (__mutex_trylock(lock)) { 9825de2055dSWaiman Long if (ww_ctx) 98355f036caSPeter Ziljstra __ww_mutex_check_waiters(lock, ww_ctx); 984659cf9f5SNicolai Hähnle 98501768b42SPeter Zijlstra goto skip_wait; 986659cf9f5SNicolai Hähnle } 98701768b42SPeter Zijlstra 98801768b42SPeter Zijlstra debug_mutex_lock_common(lock, &waiter); 989c0afb0ffSPeter Zijlstra waiter.task = current; 990c0afb0ffSPeter Zijlstra if (ww_ctx) 991c0afb0ffSPeter Zijlstra waiter.ww_ctx = ww_ctx; 99201768b42SPeter Zijlstra 9936baa5c60SNicolai Hähnle lock_contended(&lock->dep_map, ip); 9946baa5c60SNicolai Hähnle 9956baa5c60SNicolai Hähnle if (!use_ww_ctx) { 99601768b42SPeter Zijlstra /* add waiting tasks to the end of the waitqueue (FIFO): */ 99708295b3bSThomas Hellstrom __mutex_add_waiter(lock, &waiter, &lock->wait_list); 9986baa5c60SNicolai Hähnle } else { 99955f036caSPeter Ziljstra /* 100055f036caSPeter Ziljstra * Add in stamp order, waking up waiters that must kill 100155f036caSPeter Ziljstra * themselves. 100255f036caSPeter Ziljstra */ 10036baa5c60SNicolai Hähnle ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); 10046baa5c60SNicolai Hähnle if (ret) 100555f036caSPeter Ziljstra goto err_early_kill; 10066baa5c60SNicolai Hähnle } 10076baa5c60SNicolai Hähnle 1008642fa448SDavidlohr Bueso set_current_state(state); 100901768b42SPeter Zijlstra for (;;) { 1010048661a1SPeter Zijlstra bool first; 1011048661a1SPeter Zijlstra 10125bbd7e64SPeter Zijlstra /* 10135bbd7e64SPeter Zijlstra * Once we hold wait_lock, we're serialized against 10145bbd7e64SPeter Zijlstra * mutex_unlock() handing the lock off to us, do a trylock 10155bbd7e64SPeter Zijlstra * before testing the error conditions to make sure we pick up 10165bbd7e64SPeter Zijlstra * the handoff. 10175bbd7e64SPeter Zijlstra */ 1018e274795eSPeter Zijlstra if (__mutex_trylock(lock)) 10195bbd7e64SPeter Zijlstra goto acquired; 102001768b42SPeter Zijlstra 102101768b42SPeter Zijlstra /* 102255f036caSPeter Ziljstra * Check for signals and kill conditions while holding 10235bbd7e64SPeter Zijlstra * wait_lock. This ensures the lock cancellation is ordered 10245bbd7e64SPeter Zijlstra * against mutex_unlock() and wake-ups do not go missing. 102501768b42SPeter Zijlstra */ 10263bb5f4acSDavidlohr Bueso if (signal_pending_state(state, current)) { 102701768b42SPeter Zijlstra ret = -EINTR; 102801768b42SPeter Zijlstra goto err; 102901768b42SPeter Zijlstra } 103001768b42SPeter Zijlstra 10315de2055dSWaiman Long if (ww_ctx) { 103255f036caSPeter Ziljstra ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx); 103301768b42SPeter Zijlstra if (ret) 103401768b42SPeter Zijlstra goto err; 103501768b42SPeter Zijlstra } 103601768b42SPeter Zijlstra 1037ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 103801768b42SPeter Zijlstra schedule_preempt_disabled(); 10399d659ae1SPeter Zijlstra 10406baa5c60SNicolai Hähnle first = __mutex_waiter_is_first(lock, &waiter); 10415bbd7e64SPeter Zijlstra 1042642fa448SDavidlohr Bueso set_current_state(state); 10435bbd7e64SPeter Zijlstra /* 10445bbd7e64SPeter Zijlstra * Here we order against unlock; we must either see it change 10455bbd7e64SPeter Zijlstra * state back to RUNNING and fall through the next schedule(), 10465bbd7e64SPeter Zijlstra * or we must see its unlock and acquire. 10475bbd7e64SPeter Zijlstra */ 1048ad90880dSPeter Zijlstra if (__mutex_trylock_or_handoff(lock, first) || 10495de2055dSWaiman Long (first && mutex_optimistic_spin(lock, ww_ctx, &waiter))) 10505bbd7e64SPeter Zijlstra break; 10515bbd7e64SPeter Zijlstra 1052ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 105301768b42SPeter Zijlstra } 1054ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 10555bbd7e64SPeter Zijlstra acquired: 1056642fa448SDavidlohr Bueso __set_current_state(TASK_RUNNING); 105751587bcfSDavidlohr Bueso 10585de2055dSWaiman Long if (ww_ctx) { 105908295b3bSThomas Hellstrom /* 106008295b3bSThomas Hellstrom * Wound-Wait; we stole the lock (!first_waiter), check the 106108295b3bSThomas Hellstrom * waiters as anyone might want to wound us. 106208295b3bSThomas Hellstrom */ 106308295b3bSThomas Hellstrom if (!ww_ctx->is_wait_die && 106408295b3bSThomas Hellstrom !__mutex_waiter_is_first(lock, &waiter)) 106508295b3bSThomas Hellstrom __ww_mutex_check_waiters(lock, ww_ctx); 106608295b3bSThomas Hellstrom } 106708295b3bSThomas Hellstrom 10683a010c49SZqiang __mutex_remove_waiter(lock, &waiter); 10693ca0ff57SPeter Zijlstra 107001768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 107101768b42SPeter Zijlstra 107201768b42SPeter Zijlstra skip_wait: 107301768b42SPeter Zijlstra /* got the lock - cleanup and rejoice! */ 107401768b42SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 107501768b42SPeter Zijlstra 10765de2055dSWaiman Long if (ww_ctx) 107755f036caSPeter Ziljstra ww_mutex_lock_acquired(ww, ww_ctx); 107801768b42SPeter Zijlstra 1079ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 108001768b42SPeter Zijlstra preempt_enable(); 108101768b42SPeter Zijlstra return 0; 108201768b42SPeter Zijlstra 108301768b42SPeter Zijlstra err: 1084642fa448SDavidlohr Bueso __set_current_state(TASK_RUNNING); 10853a010c49SZqiang __mutex_remove_waiter(lock, &waiter); 108655f036caSPeter Ziljstra err_early_kill: 1087ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 108801768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 10895facae4fSQian Cai mutex_release(&lock->dep_map, ip); 109001768b42SPeter Zijlstra preempt_enable(); 109101768b42SPeter Zijlstra return ret; 109201768b42SPeter Zijlstra } 109301768b42SPeter Zijlstra 1094427b1820SPeter Zijlstra static int __sched 10952f064a59SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 1096427b1820SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip) 1097427b1820SPeter Zijlstra { 1098427b1820SPeter Zijlstra return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false); 1099427b1820SPeter Zijlstra } 1100427b1820SPeter Zijlstra 1101427b1820SPeter Zijlstra static int __sched 11022f064a59SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 1103cf702eddSPeter Zijlstra unsigned long ip, struct ww_acquire_ctx *ww_ctx) 1104427b1820SPeter Zijlstra { 1105cf702eddSPeter Zijlstra return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true); 1106427b1820SPeter Zijlstra } 1107427b1820SPeter Zijlstra 110801768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 110901768b42SPeter Zijlstra void __sched 111001768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass) 111101768b42SPeter Zijlstra { 1112427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); 111301768b42SPeter Zijlstra } 111401768b42SPeter Zijlstra 111501768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested); 111601768b42SPeter Zijlstra 111701768b42SPeter Zijlstra void __sched 111801768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) 111901768b42SPeter Zijlstra { 1120427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_); 112101768b42SPeter Zijlstra } 112201768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 112301768b42SPeter Zijlstra 112401768b42SPeter Zijlstra int __sched 112501768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) 112601768b42SPeter Zijlstra { 1127427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_); 112801768b42SPeter Zijlstra } 112901768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested); 113001768b42SPeter Zijlstra 113101768b42SPeter Zijlstra int __sched 113201768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) 113301768b42SPeter Zijlstra { 1134427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_); 113501768b42SPeter Zijlstra } 113601768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 113701768b42SPeter Zijlstra 11381460cb65STejun Heo void __sched 11391460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass) 11401460cb65STejun Heo { 11411460cb65STejun Heo int token; 11421460cb65STejun Heo 11431460cb65STejun Heo might_sleep(); 11441460cb65STejun Heo 11451460cb65STejun Heo token = io_schedule_prepare(); 11461460cb65STejun Heo __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 11471460cb65STejun Heo subclass, NULL, _RET_IP_, NULL, 0); 11481460cb65STejun Heo io_schedule_finish(token); 11491460cb65STejun Heo } 11501460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested); 11511460cb65STejun Heo 115201768b42SPeter Zijlstra static inline int 115301768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 115401768b42SPeter Zijlstra { 115501768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 115601768b42SPeter Zijlstra unsigned tmp; 115701768b42SPeter Zijlstra 115801768b42SPeter Zijlstra if (ctx->deadlock_inject_countdown-- == 0) { 115901768b42SPeter Zijlstra tmp = ctx->deadlock_inject_interval; 116001768b42SPeter Zijlstra if (tmp > UINT_MAX/4) 116101768b42SPeter Zijlstra tmp = UINT_MAX; 116201768b42SPeter Zijlstra else 116301768b42SPeter Zijlstra tmp = tmp*2 + tmp + tmp/2; 116401768b42SPeter Zijlstra 116501768b42SPeter Zijlstra ctx->deadlock_inject_interval = tmp; 116601768b42SPeter Zijlstra ctx->deadlock_inject_countdown = tmp; 116701768b42SPeter Zijlstra ctx->contending_lock = lock; 116801768b42SPeter Zijlstra 116901768b42SPeter Zijlstra ww_mutex_unlock(lock); 117001768b42SPeter Zijlstra 117101768b42SPeter Zijlstra return -EDEADLK; 117201768b42SPeter Zijlstra } 117301768b42SPeter Zijlstra #endif 117401768b42SPeter Zijlstra 117501768b42SPeter Zijlstra return 0; 117601768b42SPeter Zijlstra } 117701768b42SPeter Zijlstra 117801768b42SPeter Zijlstra int __sched 1179c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 118001768b42SPeter Zijlstra { 118101768b42SPeter Zijlstra int ret; 118201768b42SPeter Zijlstra 118301768b42SPeter Zijlstra might_sleep(); 1184427b1820SPeter Zijlstra ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 1185cf702eddSPeter Zijlstra 0, _RET_IP_, ctx); 1186ea9e0fb8SNicolai Hähnle if (!ret && ctx && ctx->acquired > 1) 118701768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 118801768b42SPeter Zijlstra 118901768b42SPeter Zijlstra return ret; 119001768b42SPeter Zijlstra } 1191c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock); 119201768b42SPeter Zijlstra 119301768b42SPeter Zijlstra int __sched 1194c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 119501768b42SPeter Zijlstra { 119601768b42SPeter Zijlstra int ret; 119701768b42SPeter Zijlstra 119801768b42SPeter Zijlstra might_sleep(); 1199427b1820SPeter Zijlstra ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 1200cf702eddSPeter Zijlstra 0, _RET_IP_, ctx); 120101768b42SPeter Zijlstra 1202ea9e0fb8SNicolai Hähnle if (!ret && ctx && ctx->acquired > 1) 120301768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 120401768b42SPeter Zijlstra 120501768b42SPeter Zijlstra return ret; 120601768b42SPeter Zijlstra } 1207c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); 120801768b42SPeter Zijlstra 120901768b42SPeter Zijlstra #endif 121001768b42SPeter Zijlstra 121101768b42SPeter Zijlstra /* 121201768b42SPeter Zijlstra * Release the lock, slowpath: 121301768b42SPeter Zijlstra */ 12143ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) 121501768b42SPeter Zijlstra { 12169d659ae1SPeter Zijlstra struct task_struct *next = NULL; 1217194a6b5bSWaiman Long DEFINE_WAKE_Q(wake_q); 1218b9c16a0eSPeter Zijlstra unsigned long owner; 121901768b42SPeter Zijlstra 12205facae4fSQian Cai mutex_release(&lock->dep_map, ip); 12213ca0ff57SPeter Zijlstra 122201768b42SPeter Zijlstra /* 12239d659ae1SPeter Zijlstra * Release the lock before (potentially) taking the spinlock such that 12249d659ae1SPeter Zijlstra * other contenders can get on with things ASAP. 12259d659ae1SPeter Zijlstra * 12269d659ae1SPeter Zijlstra * Except when HANDOFF, in that case we must not clear the owner field, 12279d659ae1SPeter Zijlstra * but instead set it to the top waiter. 122801768b42SPeter Zijlstra */ 12299d659ae1SPeter Zijlstra owner = atomic_long_read(&lock->owner); 12309d659ae1SPeter Zijlstra for (;;) { 1231e6b4457bSPeter Zijlstra MUTEX_WARN_ON(__owner_task(owner) != current); 1232e6b4457bSPeter Zijlstra MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 12339d659ae1SPeter Zijlstra 12349d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 12359d659ae1SPeter Zijlstra break; 12369d659ae1SPeter Zijlstra 1237ab4e4d9fSPeter Zijlstra if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) { 12389d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_WAITERS) 12399d659ae1SPeter Zijlstra break; 12409d659ae1SPeter Zijlstra 12413ca0ff57SPeter Zijlstra return; 12429d659ae1SPeter Zijlstra } 12439d659ae1SPeter Zijlstra } 124401768b42SPeter Zijlstra 1245ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 12461d8fe7dcSJason Low debug_mutex_unlock(lock); 124701768b42SPeter Zijlstra if (!list_empty(&lock->wait_list)) { 124801768b42SPeter Zijlstra /* get the first entry from the wait-list: */ 124901768b42SPeter Zijlstra struct mutex_waiter *waiter = 12509d659ae1SPeter Zijlstra list_first_entry(&lock->wait_list, 125101768b42SPeter Zijlstra struct mutex_waiter, list); 125201768b42SPeter Zijlstra 12539d659ae1SPeter Zijlstra next = waiter->task; 12549d659ae1SPeter Zijlstra 125501768b42SPeter Zijlstra debug_mutex_wake_waiter(lock, waiter); 12569d659ae1SPeter Zijlstra wake_q_add(&wake_q, next); 125701768b42SPeter Zijlstra } 125801768b42SPeter Zijlstra 12599d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 12609d659ae1SPeter Zijlstra __mutex_handoff(lock, next); 12619d659ae1SPeter Zijlstra 1262ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 12639d659ae1SPeter Zijlstra 12641329ce6fSDavidlohr Bueso wake_up_q(&wake_q); 126501768b42SPeter Zijlstra } 126601768b42SPeter Zijlstra 126701768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 126801768b42SPeter Zijlstra /* 126901768b42SPeter Zijlstra * Here come the less common (and hence less performance-critical) APIs: 127001768b42SPeter Zijlstra * mutex_lock_interruptible() and mutex_trylock(). 127101768b42SPeter Zijlstra */ 127201768b42SPeter Zijlstra static noinline int __sched 127301768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock); 127401768b42SPeter Zijlstra 127501768b42SPeter Zijlstra static noinline int __sched 127601768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock); 127701768b42SPeter Zijlstra 127801768b42SPeter Zijlstra /** 127945dbac0eSMatthew Wilcox * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals. 128045dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 128101768b42SPeter Zijlstra * 128245dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). If a signal is delivered while the 128345dbac0eSMatthew Wilcox * process is sleeping, this function will return without acquiring the 128445dbac0eSMatthew Wilcox * mutex. 128501768b42SPeter Zijlstra * 128645dbac0eSMatthew Wilcox * Context: Process context. 128745dbac0eSMatthew Wilcox * Return: 0 if the lock was successfully acquired or %-EINTR if a 128845dbac0eSMatthew Wilcox * signal arrived. 128901768b42SPeter Zijlstra */ 129001768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock) 129101768b42SPeter Zijlstra { 129201768b42SPeter Zijlstra might_sleep(); 12933ca0ff57SPeter Zijlstra 12943ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 129501768b42SPeter Zijlstra return 0; 12963ca0ff57SPeter Zijlstra 129701768b42SPeter Zijlstra return __mutex_lock_interruptible_slowpath(lock); 129801768b42SPeter Zijlstra } 129901768b42SPeter Zijlstra 130001768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible); 130101768b42SPeter Zijlstra 130245dbac0eSMatthew Wilcox /** 130345dbac0eSMatthew Wilcox * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals. 130445dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 130545dbac0eSMatthew Wilcox * 130645dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). If a signal which will be fatal to 130745dbac0eSMatthew Wilcox * the current process is delivered while the process is sleeping, this 130845dbac0eSMatthew Wilcox * function will return without acquiring the mutex. 130945dbac0eSMatthew Wilcox * 131045dbac0eSMatthew Wilcox * Context: Process context. 131145dbac0eSMatthew Wilcox * Return: 0 if the lock was successfully acquired or %-EINTR if a 131245dbac0eSMatthew Wilcox * fatal signal arrived. 131345dbac0eSMatthew Wilcox */ 131401768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock) 131501768b42SPeter Zijlstra { 131601768b42SPeter Zijlstra might_sleep(); 13173ca0ff57SPeter Zijlstra 13183ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 131901768b42SPeter Zijlstra return 0; 13203ca0ff57SPeter Zijlstra 132101768b42SPeter Zijlstra return __mutex_lock_killable_slowpath(lock); 132201768b42SPeter Zijlstra } 132301768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable); 132401768b42SPeter Zijlstra 132545dbac0eSMatthew Wilcox /** 132645dbac0eSMatthew Wilcox * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O 132745dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 132845dbac0eSMatthew Wilcox * 132945dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). While the task is waiting for this 133045dbac0eSMatthew Wilcox * mutex, it will be accounted as being in the IO wait state by the 133145dbac0eSMatthew Wilcox * scheduler. 133245dbac0eSMatthew Wilcox * 133345dbac0eSMatthew Wilcox * Context: Process context. 133445dbac0eSMatthew Wilcox */ 13351460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock) 13361460cb65STejun Heo { 13371460cb65STejun Heo int token; 13381460cb65STejun Heo 13391460cb65STejun Heo token = io_schedule_prepare(); 13401460cb65STejun Heo mutex_lock(lock); 13411460cb65STejun Heo io_schedule_finish(token); 13421460cb65STejun Heo } 13431460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io); 13441460cb65STejun Heo 13453ca0ff57SPeter Zijlstra static noinline void __sched 13463ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock) 134701768b42SPeter Zijlstra { 1348427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_); 134901768b42SPeter Zijlstra } 135001768b42SPeter Zijlstra 135101768b42SPeter Zijlstra static noinline int __sched 135201768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock) 135301768b42SPeter Zijlstra { 1354427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_); 135501768b42SPeter Zijlstra } 135601768b42SPeter Zijlstra 135701768b42SPeter Zijlstra static noinline int __sched 135801768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock) 135901768b42SPeter Zijlstra { 1360427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_); 136101768b42SPeter Zijlstra } 136201768b42SPeter Zijlstra 136301768b42SPeter Zijlstra static noinline int __sched 136401768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 136501768b42SPeter Zijlstra { 1366cf702eddSPeter Zijlstra return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, 1367427b1820SPeter Zijlstra _RET_IP_, ctx); 136801768b42SPeter Zijlstra } 136901768b42SPeter Zijlstra 137001768b42SPeter Zijlstra static noinline int __sched 137101768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, 137201768b42SPeter Zijlstra struct ww_acquire_ctx *ctx) 137301768b42SPeter Zijlstra { 1374cf702eddSPeter Zijlstra return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, 1375427b1820SPeter Zijlstra _RET_IP_, ctx); 137601768b42SPeter Zijlstra } 137701768b42SPeter Zijlstra 137801768b42SPeter Zijlstra #endif 137901768b42SPeter Zijlstra 138001768b42SPeter Zijlstra /** 138101768b42SPeter Zijlstra * mutex_trylock - try to acquire the mutex, without waiting 138201768b42SPeter Zijlstra * @lock: the mutex to be acquired 138301768b42SPeter Zijlstra * 138401768b42SPeter Zijlstra * Try to acquire the mutex atomically. Returns 1 if the mutex 138501768b42SPeter Zijlstra * has been acquired successfully, and 0 on contention. 138601768b42SPeter Zijlstra * 138701768b42SPeter Zijlstra * NOTE: this function follows the spin_trylock() convention, so 138801768b42SPeter Zijlstra * it is negated from the down_trylock() return values! Be careful 138901768b42SPeter Zijlstra * about this when converting semaphore users to mutexes. 139001768b42SPeter Zijlstra * 139101768b42SPeter Zijlstra * This function must not be used in interrupt context. The 139201768b42SPeter Zijlstra * mutex must be released by the same task that acquired it. 139301768b42SPeter Zijlstra */ 139401768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock) 139501768b42SPeter Zijlstra { 13966c11c6e3SSebastian Andrzej Siewior bool locked; 139701768b42SPeter Zijlstra 1398e6b4457bSPeter Zijlstra MUTEX_WARN_ON(lock->magic != lock); 13996c11c6e3SSebastian Andrzej Siewior 14006c11c6e3SSebastian Andrzej Siewior locked = __mutex_trylock(lock); 14013ca0ff57SPeter Zijlstra if (locked) 14023ca0ff57SPeter Zijlstra mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 140301768b42SPeter Zijlstra 14043ca0ff57SPeter Zijlstra return locked; 140501768b42SPeter Zijlstra } 140601768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock); 140701768b42SPeter Zijlstra 140801768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 140901768b42SPeter Zijlstra int __sched 1410c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 141101768b42SPeter Zijlstra { 141201768b42SPeter Zijlstra might_sleep(); 141301768b42SPeter Zijlstra 14143ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 1415ea9e0fb8SNicolai Hähnle if (ctx) 141601768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 14173ca0ff57SPeter Zijlstra return 0; 14183ca0ff57SPeter Zijlstra } 14193ca0ff57SPeter Zijlstra 14203ca0ff57SPeter Zijlstra return __ww_mutex_lock_slowpath(lock, ctx); 142101768b42SPeter Zijlstra } 1422c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock); 142301768b42SPeter Zijlstra 142401768b42SPeter Zijlstra int __sched 1425c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 142601768b42SPeter Zijlstra { 142701768b42SPeter Zijlstra might_sleep(); 142801768b42SPeter Zijlstra 14293ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 1430ea9e0fb8SNicolai Hähnle if (ctx) 143101768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 14323ca0ff57SPeter Zijlstra return 0; 14333ca0ff57SPeter Zijlstra } 14343ca0ff57SPeter Zijlstra 14353ca0ff57SPeter Zijlstra return __ww_mutex_lock_interruptible_slowpath(lock, ctx); 143601768b42SPeter Zijlstra } 1437c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible); 143801768b42SPeter Zijlstra 143901768b42SPeter Zijlstra #endif 144001768b42SPeter Zijlstra 144101768b42SPeter Zijlstra /** 144201768b42SPeter Zijlstra * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 144301768b42SPeter Zijlstra * @cnt: the atomic which we are to dec 144401768b42SPeter Zijlstra * @lock: the mutex to return holding if we dec to 0 144501768b42SPeter Zijlstra * 144601768b42SPeter Zijlstra * return true and hold lock if we dec to 0, return false otherwise 144701768b42SPeter Zijlstra */ 144801768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) 144901768b42SPeter Zijlstra { 145001768b42SPeter Zijlstra /* dec if we can't possibly hit 0 */ 145101768b42SPeter Zijlstra if (atomic_add_unless(cnt, -1, 1)) 145201768b42SPeter Zijlstra return 0; 145301768b42SPeter Zijlstra /* we might hit 0, so take the lock */ 145401768b42SPeter Zijlstra mutex_lock(lock); 145501768b42SPeter Zijlstra if (!atomic_dec_and_test(cnt)) { 145601768b42SPeter Zijlstra /* when we actually did the dec, we didn't hit 0 */ 145701768b42SPeter Zijlstra mutex_unlock(lock); 145801768b42SPeter Zijlstra return 0; 145901768b42SPeter Zijlstra } 146001768b42SPeter Zijlstra /* we hit 0, and we hold the lock */ 146101768b42SPeter Zijlstra return 1; 146201768b42SPeter Zijlstra } 146301768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock); 1464