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 33bb630f9fSThomas Gleixner #ifndef CONFIG_PREEMPT_RT 34a321fb90SThomas Gleixner #include "mutex.h" 35a321fb90SThomas Gleixner 3601768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 37e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond) 3801768b42SPeter Zijlstra #else 39e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) 4001768b42SPeter Zijlstra #endif 4101768b42SPeter Zijlstra 4201768b42SPeter Zijlstra void 4301768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 4401768b42SPeter Zijlstra { 453ca0ff57SPeter Zijlstra atomic_long_set(&lock->owner, 0); 46ebf4c55cSThomas Gleixner raw_spin_lock_init(&lock->wait_lock); 4701768b42SPeter Zijlstra INIT_LIST_HEAD(&lock->wait_list); 4801768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 494d9d951eSJason Low osq_lock_init(&lock->osq); 5001768b42SPeter Zijlstra #endif 5101768b42SPeter Zijlstra 5201768b42SPeter Zijlstra debug_mutex_init(lock, name, key); 5301768b42SPeter Zijlstra } 5401768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init); 5501768b42SPeter Zijlstra 563ca0ff57SPeter Zijlstra /* 573ca0ff57SPeter Zijlstra * @owner: contains: 'struct task_struct *' to the current lock owner, 583ca0ff57SPeter Zijlstra * NULL means not owned. Since task_struct pointers are aligned at 59e274795eSPeter Zijlstra * at least L1_CACHE_BYTES, we have low bits to store extra state. 603ca0ff57SPeter Zijlstra * 613ca0ff57SPeter Zijlstra * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. 629d659ae1SPeter Zijlstra * Bit1 indicates unlock needs to hand the lock to the top-waiter 63e274795eSPeter Zijlstra * Bit2 indicates handoff has been done and we're waiting for pickup. 643ca0ff57SPeter Zijlstra */ 653ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS 0x01 669d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF 0x02 67e274795eSPeter Zijlstra #define MUTEX_FLAG_PICKUP 0x04 683ca0ff57SPeter Zijlstra 69e274795eSPeter Zijlstra #define MUTEX_FLAGS 0x07 703ca0ff57SPeter Zijlstra 715f35d5a6SMukesh Ojha /* 725f35d5a6SMukesh Ojha * Internal helper function; C doesn't allow us to hide it :/ 735f35d5a6SMukesh Ojha * 745f35d5a6SMukesh Ojha * DO NOT USE (outside of mutex code). 755f35d5a6SMukesh Ojha */ 765f35d5a6SMukesh Ojha static inline struct task_struct *__mutex_owner(struct mutex *lock) 775f35d5a6SMukesh Ojha { 78a037d269SMukesh Ojha return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS); 795f35d5a6SMukesh Ojha } 805f35d5a6SMukesh Ojha 813ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner) 823ca0ff57SPeter Zijlstra { 833ca0ff57SPeter Zijlstra return (struct task_struct *)(owner & ~MUTEX_FLAGS); 843ca0ff57SPeter Zijlstra } 853ca0ff57SPeter Zijlstra 865f35d5a6SMukesh Ojha bool mutex_is_locked(struct mutex *lock) 875f35d5a6SMukesh Ojha { 885f35d5a6SMukesh Ojha return __mutex_owner(lock) != NULL; 895f35d5a6SMukesh Ojha } 905f35d5a6SMukesh Ojha EXPORT_SYMBOL(mutex_is_locked); 915f35d5a6SMukesh Ojha 923ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner) 933ca0ff57SPeter Zijlstra { 943ca0ff57SPeter Zijlstra return owner & MUTEX_FLAGS; 953ca0ff57SPeter Zijlstra } 963ca0ff57SPeter Zijlstra 97*12235da8SMaarten Lankhorst /* 98*12235da8SMaarten Lankhorst * Returns: __mutex_owner(lock) on failure or NULL on success. 99*12235da8SMaarten Lankhorst */ 100ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff) 1013ca0ff57SPeter Zijlstra { 1023ca0ff57SPeter Zijlstra unsigned long owner, curr = (unsigned long)current; 1033ca0ff57SPeter Zijlstra 1043ca0ff57SPeter Zijlstra owner = atomic_long_read(&lock->owner); 1053ca0ff57SPeter Zijlstra for (;;) { /* must loop, can race against a flag */ 106ab4e4d9fSPeter Zijlstra unsigned long flags = __owner_flags(owner); 107e274795eSPeter Zijlstra unsigned long task = owner & ~MUTEX_FLAGS; 1083ca0ff57SPeter Zijlstra 109e274795eSPeter Zijlstra if (task) { 110ad90880dSPeter Zijlstra if (flags & MUTEX_FLAG_PICKUP) { 111ad90880dSPeter Zijlstra if (task != curr) 112e274795eSPeter Zijlstra break; 113e274795eSPeter Zijlstra flags &= ~MUTEX_FLAG_PICKUP; 114ad90880dSPeter Zijlstra } else if (handoff) { 115ad90880dSPeter Zijlstra if (flags & MUTEX_FLAG_HANDOFF) 116ad90880dSPeter Zijlstra break; 117ad90880dSPeter Zijlstra flags |= MUTEX_FLAG_HANDOFF; 118ad90880dSPeter Zijlstra } else { 119ad90880dSPeter Zijlstra break; 120ad90880dSPeter Zijlstra } 121e274795eSPeter Zijlstra } else { 122e6b4457bSPeter Zijlstra MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP)); 123ad90880dSPeter Zijlstra task = curr; 1249d659ae1SPeter Zijlstra } 1253ca0ff57SPeter Zijlstra 126ad90880dSPeter Zijlstra if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) { 127ad90880dSPeter Zijlstra if (task == curr) 128e274795eSPeter Zijlstra return NULL; 129ad90880dSPeter Zijlstra break; 130ad90880dSPeter Zijlstra } 1313ca0ff57SPeter Zijlstra } 132e274795eSPeter Zijlstra 133e274795eSPeter Zijlstra return __owner_task(owner); 134e274795eSPeter Zijlstra } 135e274795eSPeter Zijlstra 136e274795eSPeter Zijlstra /* 137ad90880dSPeter Zijlstra * Trylock or set HANDOFF 138ad90880dSPeter Zijlstra */ 139ad90880dSPeter Zijlstra static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff) 140ad90880dSPeter Zijlstra { 141ad90880dSPeter Zijlstra return !__mutex_trylock_common(lock, handoff); 142ad90880dSPeter Zijlstra } 143ad90880dSPeter Zijlstra 144ad90880dSPeter Zijlstra /* 145e274795eSPeter Zijlstra * Actual trylock that will work on any unlocked state. 146e274795eSPeter Zijlstra */ 147e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock) 148e274795eSPeter Zijlstra { 149ad90880dSPeter Zijlstra return !__mutex_trylock_common(lock, false); 1503ca0ff57SPeter Zijlstra } 1513ca0ff57SPeter Zijlstra 1523ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 1533ca0ff57SPeter Zijlstra /* 1543ca0ff57SPeter Zijlstra * Lockdep annotations are contained to the slow paths for simplicity. 1553ca0ff57SPeter Zijlstra * There is nothing that would stop spreading the lockdep annotations outwards 1563ca0ff57SPeter Zijlstra * except more code. 1573ca0ff57SPeter Zijlstra */ 1583ca0ff57SPeter Zijlstra 1593ca0ff57SPeter Zijlstra /* 1603ca0ff57SPeter Zijlstra * Optimistic trylock that only works in the uncontended case. Make sure to 1613ca0ff57SPeter Zijlstra * follow with a __mutex_trylock() before failing. 1623ca0ff57SPeter Zijlstra */ 1633ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock) 1643ca0ff57SPeter Zijlstra { 1653ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 166c427f695SPeter Zijlstra unsigned long zero = 0UL; 1673ca0ff57SPeter Zijlstra 168c427f695SPeter Zijlstra if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr)) 1693ca0ff57SPeter Zijlstra return true; 1703ca0ff57SPeter Zijlstra 1713ca0ff57SPeter Zijlstra return false; 1723ca0ff57SPeter Zijlstra } 1733ca0ff57SPeter Zijlstra 1743ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock) 1753ca0ff57SPeter Zijlstra { 1763ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 1773ca0ff57SPeter Zijlstra 178ab4e4d9fSPeter Zijlstra return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); 1793ca0ff57SPeter Zijlstra } 1803ca0ff57SPeter Zijlstra #endif 1813ca0ff57SPeter Zijlstra 1823ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 1833ca0ff57SPeter Zijlstra { 1843ca0ff57SPeter Zijlstra atomic_long_or(flag, &lock->owner); 1853ca0ff57SPeter Zijlstra } 1863ca0ff57SPeter Zijlstra 1873ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) 1883ca0ff57SPeter Zijlstra { 1893ca0ff57SPeter Zijlstra atomic_long_andnot(flag, &lock->owner); 1903ca0ff57SPeter Zijlstra } 1913ca0ff57SPeter Zijlstra 1929d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) 1939d659ae1SPeter Zijlstra { 1949d659ae1SPeter Zijlstra return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; 1959d659ae1SPeter Zijlstra } 1969d659ae1SPeter Zijlstra 1979d659ae1SPeter Zijlstra /* 19808295b3bSThomas Hellstrom * Add @waiter to a given location in the lock wait_list and set the 19908295b3bSThomas Hellstrom * FLAG_WAITERS flag if it's the first waiter. 20008295b3bSThomas Hellstrom */ 2013a010c49SZqiang static void 20208295b3bSThomas Hellstrom __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, 20308295b3bSThomas Hellstrom struct list_head *list) 20408295b3bSThomas Hellstrom { 20508295b3bSThomas Hellstrom debug_mutex_add_waiter(lock, waiter, current); 20608295b3bSThomas Hellstrom 20708295b3bSThomas Hellstrom list_add_tail(&waiter->list, list); 20808295b3bSThomas Hellstrom if (__mutex_waiter_is_first(lock, waiter)) 20908295b3bSThomas Hellstrom __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); 21008295b3bSThomas Hellstrom } 21108295b3bSThomas Hellstrom 2123a010c49SZqiang static void 2133a010c49SZqiang __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter) 2143a010c49SZqiang { 2153a010c49SZqiang list_del(&waiter->list); 2163a010c49SZqiang if (likely(list_empty(&lock->wait_list))) 2173a010c49SZqiang __mutex_clear_flag(lock, MUTEX_FLAGS); 2183a010c49SZqiang 2193a010c49SZqiang debug_mutex_remove_waiter(lock, waiter, current); 2203a010c49SZqiang } 2213a010c49SZqiang 22208295b3bSThomas Hellstrom /* 2239d659ae1SPeter Zijlstra * Give up ownership to a specific task, when @task = NULL, this is equivalent 224e2db7592SIngo Molnar * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves 225e274795eSPeter Zijlstra * WAITERS. Provides RELEASE semantics like a regular unlock, the 226e274795eSPeter Zijlstra * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff. 2279d659ae1SPeter Zijlstra */ 2289d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task) 2299d659ae1SPeter Zijlstra { 2309d659ae1SPeter Zijlstra unsigned long owner = atomic_long_read(&lock->owner); 2319d659ae1SPeter Zijlstra 2329d659ae1SPeter Zijlstra for (;;) { 233ab4e4d9fSPeter Zijlstra unsigned long new; 2349d659ae1SPeter Zijlstra 235e6b4457bSPeter Zijlstra MUTEX_WARN_ON(__owner_task(owner) != current); 236e6b4457bSPeter Zijlstra MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 2379d659ae1SPeter Zijlstra 2389d659ae1SPeter Zijlstra new = (owner & MUTEX_FLAG_WAITERS); 2399d659ae1SPeter Zijlstra new |= (unsigned long)task; 240e274795eSPeter Zijlstra if (task) 241e274795eSPeter Zijlstra new |= MUTEX_FLAG_PICKUP; 2429d659ae1SPeter Zijlstra 243ab4e4d9fSPeter Zijlstra if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new)) 2449d659ae1SPeter Zijlstra break; 2459d659ae1SPeter Zijlstra } 2469d659ae1SPeter Zijlstra } 2479d659ae1SPeter Zijlstra 24801768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 24901768b42SPeter Zijlstra /* 25001768b42SPeter Zijlstra * We split the mutex lock/unlock logic into separate fastpath and 25101768b42SPeter Zijlstra * slowpath functions, to reduce the register pressure on the fastpath. 25201768b42SPeter Zijlstra * We also put the fastpath first in the kernel image, to make sure the 25301768b42SPeter Zijlstra * branch is predicted by the CPU as default-untaken. 25401768b42SPeter Zijlstra */ 2553ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock); 25601768b42SPeter Zijlstra 25701768b42SPeter Zijlstra /** 25801768b42SPeter Zijlstra * mutex_lock - acquire the mutex 25901768b42SPeter Zijlstra * @lock: the mutex to be acquired 26001768b42SPeter Zijlstra * 26101768b42SPeter Zijlstra * Lock the mutex exclusively for this task. If the mutex is not 26201768b42SPeter Zijlstra * available right now, it will sleep until it can get it. 26301768b42SPeter Zijlstra * 26401768b42SPeter Zijlstra * The mutex must later on be released by the same task that 26501768b42SPeter Zijlstra * acquired it. Recursive locking is not allowed. The task 26601768b42SPeter Zijlstra * may not exit without first unlocking the mutex. Also, kernel 267139b6fd2SSharon Dvir * memory where the mutex resides must not be freed with 26801768b42SPeter Zijlstra * the mutex still locked. The mutex must first be initialized 26901768b42SPeter Zijlstra * (or statically defined) before it can be locked. memset()-ing 27001768b42SPeter Zijlstra * the mutex to 0 is not allowed. 27101768b42SPeter Zijlstra * 27201768b42SPeter Zijlstra * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging 27301768b42SPeter Zijlstra * checks that will enforce the restrictions and will also do 2747b4ff1adSMauro Carvalho Chehab * deadlock debugging) 27501768b42SPeter Zijlstra * 27601768b42SPeter Zijlstra * This function is similar to (but not equivalent to) down(). 27701768b42SPeter Zijlstra */ 27801768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock) 27901768b42SPeter Zijlstra { 28001768b42SPeter Zijlstra might_sleep(); 28101768b42SPeter Zijlstra 2823ca0ff57SPeter Zijlstra if (!__mutex_trylock_fast(lock)) 2833ca0ff57SPeter Zijlstra __mutex_lock_slowpath(lock); 2843ca0ff57SPeter Zijlstra } 28501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock); 28601768b42SPeter Zijlstra #endif 28701768b42SPeter Zijlstra 2882674bd18SPeter Zijlstra (Intel) #include "ww_mutex.h" 28976916515SDavidlohr Bueso 29001768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 291c516df97SNicolai Hähnle 292ad90880dSPeter Zijlstra /* 293ad90880dSPeter Zijlstra * Trylock variant that returns the owning task on failure. 294ad90880dSPeter Zijlstra */ 295ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock) 296ad90880dSPeter Zijlstra { 297ad90880dSPeter Zijlstra return __mutex_trylock_common(lock, false); 298ad90880dSPeter Zijlstra } 299ad90880dSPeter Zijlstra 300c516df97SNicolai Hähnle static inline 301c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 302c516df97SNicolai Hähnle struct mutex_waiter *waiter) 303c516df97SNicolai Hähnle { 304c516df97SNicolai Hähnle struct ww_mutex *ww; 305c516df97SNicolai Hähnle 306c516df97SNicolai Hähnle ww = container_of(lock, struct ww_mutex, base); 307c516df97SNicolai Hähnle 30801768b42SPeter Zijlstra /* 309c516df97SNicolai Hähnle * If ww->ctx is set the contents are undefined, only 310c516df97SNicolai Hähnle * by acquiring wait_lock there is a guarantee that 311c516df97SNicolai Hähnle * they are not invalid when reading. 312c516df97SNicolai Hähnle * 313c516df97SNicolai Hähnle * As such, when deadlock detection needs to be 314c516df97SNicolai Hähnle * performed the optimistic spinning cannot be done. 315c516df97SNicolai Hähnle * 316c516df97SNicolai Hähnle * Check this in every inner iteration because we may 317c516df97SNicolai Hähnle * be racing against another thread's ww_mutex_lock. 318c516df97SNicolai Hähnle */ 319c516df97SNicolai Hähnle if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx)) 320c516df97SNicolai Hähnle return false; 321c516df97SNicolai Hähnle 322c516df97SNicolai Hähnle /* 323c516df97SNicolai Hähnle * If we aren't on the wait list yet, cancel the spin 324c516df97SNicolai Hähnle * if there are waiters. We want to avoid stealing the 325c516df97SNicolai Hähnle * lock from a waiter with an earlier stamp, since the 326c516df97SNicolai Hähnle * other thread may already own a lock that we also 327c516df97SNicolai Hähnle * need. 328c516df97SNicolai Hähnle */ 329c516df97SNicolai Hähnle if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS)) 330c516df97SNicolai Hähnle return false; 331c516df97SNicolai Hähnle 332c516df97SNicolai Hähnle /* 333c516df97SNicolai Hähnle * Similarly, stop spinning if we are no longer the 334c516df97SNicolai Hähnle * first waiter. 335c516df97SNicolai Hähnle */ 336c516df97SNicolai Hähnle if (waiter && !__mutex_waiter_is_first(lock, waiter)) 337c516df97SNicolai Hähnle return false; 338c516df97SNicolai Hähnle 339c516df97SNicolai Hähnle return true; 340c516df97SNicolai Hähnle } 341c516df97SNicolai Hähnle 34201768b42SPeter Zijlstra /* 34325f13b40SNicolai Hähnle * Look out! "owner" is an entirely speculative pointer access and not 34425f13b40SNicolai Hähnle * reliable. 34525f13b40SNicolai Hähnle * 34625f13b40SNicolai Hähnle * "noinline" so that this function shows up on perf profiles. 34701768b42SPeter Zijlstra */ 34801768b42SPeter Zijlstra static noinline 34925f13b40SNicolai Hähnle bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, 350c516df97SNicolai Hähnle struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter) 35101768b42SPeter Zijlstra { 35201ac33c1SJason Low bool ret = true; 353be1f7bf2SJason Low 35401768b42SPeter Zijlstra rcu_read_lock(); 3553ca0ff57SPeter Zijlstra while (__mutex_owner(lock) == owner) { 356be1f7bf2SJason Low /* 357be1f7bf2SJason Low * Ensure we emit the owner->on_cpu, dereference _after_ 35801ac33c1SJason Low * checking lock->owner still matches owner. If that fails, 35901ac33c1SJason Low * owner might point to freed memory. If it still matches, 360be1f7bf2SJason Low * the rcu_read_lock() ensures the memory stays valid. 361be1f7bf2SJason Low */ 362be1f7bf2SJason Low barrier(); 363be1f7bf2SJason Low 36405ffc951SPan Xinhui /* 36505ffc951SPan Xinhui * Use vcpu_is_preempted to detect lock holder preemption issue. 36605ffc951SPan Xinhui */ 36705ffc951SPan Xinhui if (!owner->on_cpu || need_resched() || 36805ffc951SPan Xinhui vcpu_is_preempted(task_cpu(owner))) { 369be1f7bf2SJason Low ret = false; 370be1f7bf2SJason Low break; 371be1f7bf2SJason Low } 37201768b42SPeter Zijlstra 373c516df97SNicolai Hähnle if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) { 37425f13b40SNicolai Hähnle ret = false; 37525f13b40SNicolai Hähnle break; 37625f13b40SNicolai Hähnle } 37725f13b40SNicolai Hähnle 378f2f09a4cSChristian Borntraeger cpu_relax(); 37901768b42SPeter Zijlstra } 38001768b42SPeter Zijlstra rcu_read_unlock(); 38101768b42SPeter Zijlstra 382be1f7bf2SJason Low return ret; 38301768b42SPeter Zijlstra } 38401768b42SPeter Zijlstra 38501768b42SPeter Zijlstra /* 38601768b42SPeter Zijlstra * Initial check for entering the mutex spinning loop 38701768b42SPeter Zijlstra */ 38801768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock) 38901768b42SPeter Zijlstra { 39001768b42SPeter Zijlstra struct task_struct *owner; 39101768b42SPeter Zijlstra int retval = 1; 39201768b42SPeter Zijlstra 39346af29e4SJason Low if (need_resched()) 39446af29e4SJason Low return 0; 39546af29e4SJason Low 39601768b42SPeter Zijlstra rcu_read_lock(); 3973ca0ff57SPeter Zijlstra owner = __mutex_owner(lock); 39805ffc951SPan Xinhui 39905ffc951SPan Xinhui /* 40005ffc951SPan Xinhui * As lock holder preemption issue, we both skip spinning if task is not 40105ffc951SPan Xinhui * on cpu or its cpu is preempted 40205ffc951SPan Xinhui */ 40301768b42SPeter Zijlstra if (owner) 40405ffc951SPan Xinhui retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner)); 40501768b42SPeter Zijlstra rcu_read_unlock(); 40676916515SDavidlohr Bueso 40776916515SDavidlohr Bueso /* 4083ca0ff57SPeter Zijlstra * If lock->owner is not set, the mutex has been released. Return true 4093ca0ff57SPeter Zijlstra * such that we'll trylock in the spin path, which is a faster option 4103ca0ff57SPeter Zijlstra * than the blocking slow path. 41176916515SDavidlohr Bueso */ 4123ca0ff57SPeter Zijlstra return retval; 41376916515SDavidlohr Bueso } 41476916515SDavidlohr Bueso 41576916515SDavidlohr Bueso /* 41676916515SDavidlohr Bueso * Optimistic spinning. 41776916515SDavidlohr Bueso * 41876916515SDavidlohr Bueso * We try to spin for acquisition when we find that the lock owner 41976916515SDavidlohr Bueso * is currently running on a (different) CPU and while we don't 42076916515SDavidlohr Bueso * need to reschedule. The rationale is that if the lock owner is 42176916515SDavidlohr Bueso * running, it is likely to release the lock soon. 42276916515SDavidlohr Bueso * 42376916515SDavidlohr Bueso * The mutex spinners are queued up using MCS lock so that only one 42476916515SDavidlohr Bueso * spinner can compete for the mutex. However, if mutex spinning isn't 42576916515SDavidlohr Bueso * going to happen, there is no point in going through the lock/unlock 42676916515SDavidlohr Bueso * overhead. 42776916515SDavidlohr Bueso * 42876916515SDavidlohr Bueso * Returns true when the lock was taken, otherwise false, indicating 42976916515SDavidlohr Bueso * that we need to jump to the slowpath and sleep. 430b341afb3SWaiman Long * 431b341afb3SWaiman Long * The waiter flag is set to true if the spinner is a waiter in the wait 432b341afb3SWaiman Long * queue. The waiter-spinner will spin on the lock directly and concurrently 433b341afb3SWaiman Long * with the spinner at the head of the OSQ, if present, until the owner is 434b341afb3SWaiman Long * changed to itself. 43576916515SDavidlohr Bueso */ 436427b1820SPeter Zijlstra static __always_inline bool 437427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 4385de2055dSWaiman Long struct mutex_waiter *waiter) 43976916515SDavidlohr Bueso { 440b341afb3SWaiman Long if (!waiter) { 441b341afb3SWaiman Long /* 442b341afb3SWaiman Long * The purpose of the mutex_can_spin_on_owner() function is 443b341afb3SWaiman Long * to eliminate the overhead of osq_lock() and osq_unlock() 444b341afb3SWaiman Long * in case spinning isn't possible. As a waiter-spinner 445b341afb3SWaiman Long * is not going to take OSQ lock anyway, there is no need 446b341afb3SWaiman Long * to call mutex_can_spin_on_owner(). 447b341afb3SWaiman Long */ 44876916515SDavidlohr Bueso if (!mutex_can_spin_on_owner(lock)) 449b341afb3SWaiman Long goto fail; 45076916515SDavidlohr Bueso 451e42f678aSDavidlohr Bueso /* 452e42f678aSDavidlohr Bueso * In order to avoid a stampede of mutex spinners trying to 453e42f678aSDavidlohr Bueso * acquire the mutex all at once, the spinners need to take a 454e42f678aSDavidlohr Bueso * MCS (queued) lock first before spinning on the owner field. 455e42f678aSDavidlohr Bueso */ 45676916515SDavidlohr Bueso if (!osq_lock(&lock->osq)) 457b341afb3SWaiman Long goto fail; 458b341afb3SWaiman Long } 45976916515SDavidlohr Bueso 460b341afb3SWaiman Long for (;;) { 46176916515SDavidlohr Bueso struct task_struct *owner; 46276916515SDavidlohr Bueso 463e274795eSPeter Zijlstra /* Try to acquire the mutex... */ 464e274795eSPeter Zijlstra owner = __mutex_trylock_or_owner(lock); 465e274795eSPeter Zijlstra if (!owner) 466e274795eSPeter Zijlstra break; 46776916515SDavidlohr Bueso 46876916515SDavidlohr Bueso /* 469e274795eSPeter Zijlstra * There's an owner, wait for it to either 47076916515SDavidlohr Bueso * release the lock or go to sleep. 47176916515SDavidlohr Bueso */ 472c516df97SNicolai Hähnle if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter)) 473b341afb3SWaiman Long goto fail_unlock; 47476916515SDavidlohr Bueso 47576916515SDavidlohr Bueso /* 47676916515SDavidlohr Bueso * The cpu_relax() call is a compiler barrier which forces 47776916515SDavidlohr Bueso * everything in this loop to be re-loaded. We don't need 47876916515SDavidlohr Bueso * memory barriers as we'll eventually observe the right 47976916515SDavidlohr Bueso * values at the cost of a few extra spins. 48076916515SDavidlohr Bueso */ 481f2f09a4cSChristian Borntraeger cpu_relax(); 48276916515SDavidlohr Bueso } 48376916515SDavidlohr Bueso 484b341afb3SWaiman Long if (!waiter) 48576916515SDavidlohr Bueso osq_unlock(&lock->osq); 486b341afb3SWaiman Long 487b341afb3SWaiman Long return true; 488b341afb3SWaiman Long 489b341afb3SWaiman Long 490b341afb3SWaiman Long fail_unlock: 491b341afb3SWaiman Long if (!waiter) 492b341afb3SWaiman Long osq_unlock(&lock->osq); 493b341afb3SWaiman Long 494b341afb3SWaiman Long fail: 49576916515SDavidlohr Bueso /* 49676916515SDavidlohr Bueso * If we fell out of the spin path because of need_resched(), 49776916515SDavidlohr Bueso * reschedule now, before we try-lock the mutex. This avoids getting 49876916515SDavidlohr Bueso * scheduled out right after we obtained the mutex. 49976916515SDavidlohr Bueso */ 5006f942a1fSPeter Zijlstra if (need_resched()) { 5016f942a1fSPeter Zijlstra /* 5026f942a1fSPeter Zijlstra * We _should_ have TASK_RUNNING here, but just in case 5036f942a1fSPeter Zijlstra * we do not, make it so, otherwise we might get stuck. 5046f942a1fSPeter Zijlstra */ 5056f942a1fSPeter Zijlstra __set_current_state(TASK_RUNNING); 50676916515SDavidlohr Bueso schedule_preempt_disabled(); 5076f942a1fSPeter Zijlstra } 50876916515SDavidlohr Bueso 50976916515SDavidlohr Bueso return false; 51076916515SDavidlohr Bueso } 51176916515SDavidlohr Bueso #else 512427b1820SPeter Zijlstra static __always_inline bool 513427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 5145de2055dSWaiman Long struct mutex_waiter *waiter) 51576916515SDavidlohr Bueso { 51676916515SDavidlohr Bueso return false; 51776916515SDavidlohr Bueso } 51801768b42SPeter Zijlstra #endif 51901768b42SPeter Zijlstra 5203ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip); 52101768b42SPeter Zijlstra 52201768b42SPeter Zijlstra /** 52301768b42SPeter Zijlstra * mutex_unlock - release the mutex 52401768b42SPeter Zijlstra * @lock: the mutex to be released 52501768b42SPeter Zijlstra * 52601768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously. 52701768b42SPeter Zijlstra * 52801768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 52901768b42SPeter Zijlstra * of a not locked mutex is not allowed. 53001768b42SPeter Zijlstra * 53101768b42SPeter Zijlstra * This function is similar to (but not equivalent to) up(). 53201768b42SPeter Zijlstra */ 53301768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock) 53401768b42SPeter Zijlstra { 5353ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 5363ca0ff57SPeter Zijlstra if (__mutex_unlock_fast(lock)) 5373ca0ff57SPeter Zijlstra return; 53801768b42SPeter Zijlstra #endif 5393ca0ff57SPeter Zijlstra __mutex_unlock_slowpath(lock, _RET_IP_); 54001768b42SPeter Zijlstra } 54101768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock); 54201768b42SPeter Zijlstra 54301768b42SPeter Zijlstra /** 54401768b42SPeter Zijlstra * ww_mutex_unlock - release the w/w mutex 54501768b42SPeter Zijlstra * @lock: the mutex to be released 54601768b42SPeter Zijlstra * 54701768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously with any of the 54801768b42SPeter Zijlstra * ww_mutex_lock* functions (with or without an acquire context). It is 54901768b42SPeter Zijlstra * forbidden to release the locks after releasing the acquire context. 55001768b42SPeter Zijlstra * 55101768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 55201768b42SPeter Zijlstra * of a unlocked mutex is not allowed. 55301768b42SPeter Zijlstra */ 55401768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock) 55501768b42SPeter Zijlstra { 556aaa77de1SPeter Zijlstra (Intel) __ww_mutex_unlock(lock); 5573ca0ff57SPeter Zijlstra mutex_unlock(&lock->base); 55801768b42SPeter Zijlstra } 55901768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock); 56001768b42SPeter Zijlstra 56101768b42SPeter Zijlstra /* 56201768b42SPeter Zijlstra * Lock a mutex (possibly interruptible), slowpath: 56301768b42SPeter Zijlstra */ 56401768b42SPeter Zijlstra static __always_inline int __sched 5652f064a59SPeter Zijlstra __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass, 56601768b42SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip, 56701768b42SPeter Zijlstra struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 56801768b42SPeter Zijlstra { 56901768b42SPeter Zijlstra struct mutex_waiter waiter; 570a40ca565SWaiman Long struct ww_mutex *ww; 57101768b42SPeter Zijlstra int ret; 57201768b42SPeter Zijlstra 5735de2055dSWaiman Long if (!use_ww_ctx) 5745de2055dSWaiman Long ww_ctx = NULL; 5755de2055dSWaiman Long 576427b1820SPeter Zijlstra might_sleep(); 577ea9e0fb8SNicolai Hähnle 578e6b4457bSPeter Zijlstra MUTEX_WARN_ON(lock->magic != lock); 5796c11c6e3SSebastian Andrzej Siewior 580a40ca565SWaiman Long ww = container_of(lock, struct ww_mutex, base); 5815de2055dSWaiman Long if (ww_ctx) { 5820422e83dSChris Wilson if (unlikely(ww_ctx == READ_ONCE(ww->ctx))) 5830422e83dSChris Wilson return -EALREADY; 58408295b3bSThomas Hellstrom 58508295b3bSThomas Hellstrom /* 58608295b3bSThomas Hellstrom * Reset the wounded flag after a kill. No other process can 58708295b3bSThomas Hellstrom * race and wound us here since they can't have a valid owner 58808295b3bSThomas Hellstrom * pointer if we don't have any locks held. 58908295b3bSThomas Hellstrom */ 59008295b3bSThomas Hellstrom if (ww_ctx->acquired == 0) 59108295b3bSThomas Hellstrom ww_ctx->wounded = 0; 592cf702eddSPeter Zijlstra 593cf702eddSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 594cf702eddSPeter Zijlstra nest_lock = &ww_ctx->dep_map; 595cf702eddSPeter Zijlstra #endif 5960422e83dSChris Wilson } 5970422e83dSChris Wilson 59801768b42SPeter Zijlstra preempt_disable(); 59901768b42SPeter Zijlstra mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 60001768b42SPeter Zijlstra 601e274795eSPeter Zijlstra if (__mutex_trylock(lock) || 6025de2055dSWaiman Long mutex_optimistic_spin(lock, ww_ctx, NULL)) { 60376916515SDavidlohr Bueso /* got the lock, yay! */ 6043ca0ff57SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 6055de2055dSWaiman Long if (ww_ctx) 6063ca0ff57SPeter Zijlstra ww_mutex_set_context_fastpath(ww, ww_ctx); 60701768b42SPeter Zijlstra preempt_enable(); 60801768b42SPeter Zijlstra return 0; 60901768b42SPeter Zijlstra } 61001768b42SPeter Zijlstra 611ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 6121e820c96SJason Low /* 6133ca0ff57SPeter Zijlstra * After waiting to acquire the wait_lock, try again. 6141e820c96SJason Low */ 615659cf9f5SNicolai Hähnle if (__mutex_trylock(lock)) { 6165de2055dSWaiman Long if (ww_ctx) 61755f036caSPeter Ziljstra __ww_mutex_check_waiters(lock, ww_ctx); 618659cf9f5SNicolai Hähnle 61901768b42SPeter Zijlstra goto skip_wait; 620659cf9f5SNicolai Hähnle } 62101768b42SPeter Zijlstra 62201768b42SPeter Zijlstra debug_mutex_lock_common(lock, &waiter); 623c0afb0ffSPeter Zijlstra waiter.task = current; 624b857174eSSebastian Andrzej Siewior if (use_ww_ctx) 625c0afb0ffSPeter Zijlstra waiter.ww_ctx = ww_ctx; 62601768b42SPeter Zijlstra 6276baa5c60SNicolai Hähnle lock_contended(&lock->dep_map, ip); 6286baa5c60SNicolai Hähnle 6296baa5c60SNicolai Hähnle if (!use_ww_ctx) { 63001768b42SPeter Zijlstra /* add waiting tasks to the end of the waitqueue (FIFO): */ 63108295b3bSThomas Hellstrom __mutex_add_waiter(lock, &waiter, &lock->wait_list); 6326baa5c60SNicolai Hähnle } else { 63355f036caSPeter Ziljstra /* 63455f036caSPeter Ziljstra * Add in stamp order, waking up waiters that must kill 63555f036caSPeter Ziljstra * themselves. 63655f036caSPeter Ziljstra */ 6376baa5c60SNicolai Hähnle ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); 6386baa5c60SNicolai Hähnle if (ret) 63955f036caSPeter Ziljstra goto err_early_kill; 6406baa5c60SNicolai Hähnle } 6416baa5c60SNicolai Hähnle 642642fa448SDavidlohr Bueso set_current_state(state); 64301768b42SPeter Zijlstra for (;;) { 644048661a1SPeter Zijlstra bool first; 645048661a1SPeter Zijlstra 6465bbd7e64SPeter Zijlstra /* 6475bbd7e64SPeter Zijlstra * Once we hold wait_lock, we're serialized against 6485bbd7e64SPeter Zijlstra * mutex_unlock() handing the lock off to us, do a trylock 6495bbd7e64SPeter Zijlstra * before testing the error conditions to make sure we pick up 6505bbd7e64SPeter Zijlstra * the handoff. 6515bbd7e64SPeter Zijlstra */ 652e274795eSPeter Zijlstra if (__mutex_trylock(lock)) 6535bbd7e64SPeter Zijlstra goto acquired; 65401768b42SPeter Zijlstra 65501768b42SPeter Zijlstra /* 65655f036caSPeter Ziljstra * Check for signals and kill conditions while holding 6575bbd7e64SPeter Zijlstra * wait_lock. This ensures the lock cancellation is ordered 6585bbd7e64SPeter Zijlstra * against mutex_unlock() and wake-ups do not go missing. 65901768b42SPeter Zijlstra */ 6603bb5f4acSDavidlohr Bueso if (signal_pending_state(state, current)) { 66101768b42SPeter Zijlstra ret = -EINTR; 66201768b42SPeter Zijlstra goto err; 66301768b42SPeter Zijlstra } 66401768b42SPeter Zijlstra 6655de2055dSWaiman Long if (ww_ctx) { 66655f036caSPeter Ziljstra ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx); 66701768b42SPeter Zijlstra if (ret) 66801768b42SPeter Zijlstra goto err; 66901768b42SPeter Zijlstra } 67001768b42SPeter Zijlstra 671ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 67201768b42SPeter Zijlstra schedule_preempt_disabled(); 6739d659ae1SPeter Zijlstra 6746baa5c60SNicolai Hähnle first = __mutex_waiter_is_first(lock, &waiter); 6755bbd7e64SPeter Zijlstra 676642fa448SDavidlohr Bueso set_current_state(state); 6775bbd7e64SPeter Zijlstra /* 6785bbd7e64SPeter Zijlstra * Here we order against unlock; we must either see it change 6795bbd7e64SPeter Zijlstra * state back to RUNNING and fall through the next schedule(), 6805bbd7e64SPeter Zijlstra * or we must see its unlock and acquire. 6815bbd7e64SPeter Zijlstra */ 682ad90880dSPeter Zijlstra if (__mutex_trylock_or_handoff(lock, first) || 6835de2055dSWaiman Long (first && mutex_optimistic_spin(lock, ww_ctx, &waiter))) 6845bbd7e64SPeter Zijlstra break; 6855bbd7e64SPeter Zijlstra 686ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 68701768b42SPeter Zijlstra } 688ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 6895bbd7e64SPeter Zijlstra acquired: 690642fa448SDavidlohr Bueso __set_current_state(TASK_RUNNING); 69151587bcfSDavidlohr Bueso 6925de2055dSWaiman Long if (ww_ctx) { 69308295b3bSThomas Hellstrom /* 69408295b3bSThomas Hellstrom * Wound-Wait; we stole the lock (!first_waiter), check the 69508295b3bSThomas Hellstrom * waiters as anyone might want to wound us. 69608295b3bSThomas Hellstrom */ 69708295b3bSThomas Hellstrom if (!ww_ctx->is_wait_die && 69808295b3bSThomas Hellstrom !__mutex_waiter_is_first(lock, &waiter)) 69908295b3bSThomas Hellstrom __ww_mutex_check_waiters(lock, ww_ctx); 70008295b3bSThomas Hellstrom } 70108295b3bSThomas Hellstrom 7023a010c49SZqiang __mutex_remove_waiter(lock, &waiter); 7033ca0ff57SPeter Zijlstra 70401768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 70501768b42SPeter Zijlstra 70601768b42SPeter Zijlstra skip_wait: 70701768b42SPeter Zijlstra /* got the lock - cleanup and rejoice! */ 70801768b42SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 70901768b42SPeter Zijlstra 7105de2055dSWaiman Long if (ww_ctx) 71155f036caSPeter Ziljstra ww_mutex_lock_acquired(ww, ww_ctx); 71201768b42SPeter Zijlstra 713ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 71401768b42SPeter Zijlstra preempt_enable(); 71501768b42SPeter Zijlstra return 0; 71601768b42SPeter Zijlstra 71701768b42SPeter Zijlstra err: 718642fa448SDavidlohr Bueso __set_current_state(TASK_RUNNING); 7193a010c49SZqiang __mutex_remove_waiter(lock, &waiter); 72055f036caSPeter Ziljstra err_early_kill: 721ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 72201768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 7235facae4fSQian Cai mutex_release(&lock->dep_map, ip); 72401768b42SPeter Zijlstra preempt_enable(); 72501768b42SPeter Zijlstra return ret; 72601768b42SPeter Zijlstra } 72701768b42SPeter Zijlstra 728427b1820SPeter Zijlstra static int __sched 7292f064a59SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 730427b1820SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip) 731427b1820SPeter Zijlstra { 732427b1820SPeter Zijlstra return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false); 733427b1820SPeter Zijlstra } 734427b1820SPeter Zijlstra 735427b1820SPeter Zijlstra static int __sched 7362f064a59SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 737cf702eddSPeter Zijlstra unsigned long ip, struct ww_acquire_ctx *ww_ctx) 738427b1820SPeter Zijlstra { 739cf702eddSPeter Zijlstra return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true); 740427b1820SPeter Zijlstra } 741427b1820SPeter Zijlstra 742*12235da8SMaarten Lankhorst /** 743*12235da8SMaarten Lankhorst * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context 744*12235da8SMaarten Lankhorst * @ww: mutex to lock 745*12235da8SMaarten Lankhorst * @ww_ctx: optional w/w acquire context 746*12235da8SMaarten Lankhorst * 747*12235da8SMaarten Lankhorst * Trylocks a mutex with the optional acquire context; no deadlock detection is 748*12235da8SMaarten Lankhorst * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise. 749*12235da8SMaarten Lankhorst * 750*12235da8SMaarten Lankhorst * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is 751*12235da8SMaarten Lankhorst * specified, -EALREADY handling may happen in calls to ww_mutex_trylock. 752*12235da8SMaarten Lankhorst * 753*12235da8SMaarten Lankhorst * A mutex acquired with this function must be released with ww_mutex_unlock. 754*12235da8SMaarten Lankhorst */ 755*12235da8SMaarten Lankhorst int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx) 756*12235da8SMaarten Lankhorst { 757*12235da8SMaarten Lankhorst if (!ww_ctx) 758*12235da8SMaarten Lankhorst return mutex_trylock(&ww->base); 759*12235da8SMaarten Lankhorst 760*12235da8SMaarten Lankhorst MUTEX_WARN_ON(ww->base.magic != &ww->base); 761*12235da8SMaarten Lankhorst 762*12235da8SMaarten Lankhorst /* 763*12235da8SMaarten Lankhorst * Reset the wounded flag after a kill. No other process can 764*12235da8SMaarten Lankhorst * race and wound us here, since they can't have a valid owner 765*12235da8SMaarten Lankhorst * pointer if we don't have any locks held. 766*12235da8SMaarten Lankhorst */ 767*12235da8SMaarten Lankhorst if (ww_ctx->acquired == 0) 768*12235da8SMaarten Lankhorst ww_ctx->wounded = 0; 769*12235da8SMaarten Lankhorst 770*12235da8SMaarten Lankhorst if (__mutex_trylock(&ww->base)) { 771*12235da8SMaarten Lankhorst ww_mutex_set_context_fastpath(ww, ww_ctx); 772*12235da8SMaarten Lankhorst mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_); 773*12235da8SMaarten Lankhorst return 1; 774*12235da8SMaarten Lankhorst } 775*12235da8SMaarten Lankhorst 776*12235da8SMaarten Lankhorst return 0; 777*12235da8SMaarten Lankhorst } 778*12235da8SMaarten Lankhorst EXPORT_SYMBOL(ww_mutex_trylock); 779*12235da8SMaarten Lankhorst 78001768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 78101768b42SPeter Zijlstra void __sched 78201768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass) 78301768b42SPeter Zijlstra { 784427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); 78501768b42SPeter Zijlstra } 78601768b42SPeter Zijlstra 78701768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested); 78801768b42SPeter Zijlstra 78901768b42SPeter Zijlstra void __sched 79001768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) 79101768b42SPeter Zijlstra { 792427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_); 79301768b42SPeter Zijlstra } 79401768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 79501768b42SPeter Zijlstra 79601768b42SPeter Zijlstra int __sched 79701768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) 79801768b42SPeter Zijlstra { 799427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_); 80001768b42SPeter Zijlstra } 80101768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested); 80201768b42SPeter Zijlstra 80301768b42SPeter Zijlstra int __sched 80401768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) 80501768b42SPeter Zijlstra { 806427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_); 80701768b42SPeter Zijlstra } 80801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 80901768b42SPeter Zijlstra 8101460cb65STejun Heo void __sched 8111460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass) 8121460cb65STejun Heo { 8131460cb65STejun Heo int token; 8141460cb65STejun Heo 8151460cb65STejun Heo might_sleep(); 8161460cb65STejun Heo 8171460cb65STejun Heo token = io_schedule_prepare(); 8181460cb65STejun Heo __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 8191460cb65STejun Heo subclass, NULL, _RET_IP_, NULL, 0); 8201460cb65STejun Heo io_schedule_finish(token); 8211460cb65STejun Heo } 8221460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested); 8231460cb65STejun Heo 82401768b42SPeter Zijlstra static inline int 82501768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 82601768b42SPeter Zijlstra { 82701768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 82801768b42SPeter Zijlstra unsigned tmp; 82901768b42SPeter Zijlstra 83001768b42SPeter Zijlstra if (ctx->deadlock_inject_countdown-- == 0) { 83101768b42SPeter Zijlstra tmp = ctx->deadlock_inject_interval; 83201768b42SPeter Zijlstra if (tmp > UINT_MAX/4) 83301768b42SPeter Zijlstra tmp = UINT_MAX; 83401768b42SPeter Zijlstra else 83501768b42SPeter Zijlstra tmp = tmp*2 + tmp + tmp/2; 83601768b42SPeter Zijlstra 83701768b42SPeter Zijlstra ctx->deadlock_inject_interval = tmp; 83801768b42SPeter Zijlstra ctx->deadlock_inject_countdown = tmp; 83901768b42SPeter Zijlstra ctx->contending_lock = lock; 84001768b42SPeter Zijlstra 84101768b42SPeter Zijlstra ww_mutex_unlock(lock); 84201768b42SPeter Zijlstra 84301768b42SPeter Zijlstra return -EDEADLK; 84401768b42SPeter Zijlstra } 84501768b42SPeter Zijlstra #endif 84601768b42SPeter Zijlstra 84701768b42SPeter Zijlstra return 0; 84801768b42SPeter Zijlstra } 84901768b42SPeter Zijlstra 85001768b42SPeter Zijlstra int __sched 851c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 85201768b42SPeter Zijlstra { 85301768b42SPeter Zijlstra int ret; 85401768b42SPeter Zijlstra 85501768b42SPeter Zijlstra might_sleep(); 856427b1820SPeter Zijlstra ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 857cf702eddSPeter Zijlstra 0, _RET_IP_, ctx); 858ea9e0fb8SNicolai Hähnle if (!ret && ctx && ctx->acquired > 1) 85901768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 86001768b42SPeter Zijlstra 86101768b42SPeter Zijlstra return ret; 86201768b42SPeter Zijlstra } 863c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock); 86401768b42SPeter Zijlstra 86501768b42SPeter Zijlstra int __sched 866c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 86701768b42SPeter Zijlstra { 86801768b42SPeter Zijlstra int ret; 86901768b42SPeter Zijlstra 87001768b42SPeter Zijlstra might_sleep(); 871427b1820SPeter Zijlstra ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 872cf702eddSPeter Zijlstra 0, _RET_IP_, ctx); 87301768b42SPeter Zijlstra 874ea9e0fb8SNicolai Hähnle if (!ret && ctx && ctx->acquired > 1) 87501768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 87601768b42SPeter Zijlstra 87701768b42SPeter Zijlstra return ret; 87801768b42SPeter Zijlstra } 879c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); 88001768b42SPeter Zijlstra 88101768b42SPeter Zijlstra #endif 88201768b42SPeter Zijlstra 88301768b42SPeter Zijlstra /* 88401768b42SPeter Zijlstra * Release the lock, slowpath: 88501768b42SPeter Zijlstra */ 8863ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) 88701768b42SPeter Zijlstra { 8889d659ae1SPeter Zijlstra struct task_struct *next = NULL; 889194a6b5bSWaiman Long DEFINE_WAKE_Q(wake_q); 890b9c16a0eSPeter Zijlstra unsigned long owner; 89101768b42SPeter Zijlstra 8925facae4fSQian Cai mutex_release(&lock->dep_map, ip); 8933ca0ff57SPeter Zijlstra 89401768b42SPeter Zijlstra /* 8959d659ae1SPeter Zijlstra * Release the lock before (potentially) taking the spinlock such that 8969d659ae1SPeter Zijlstra * other contenders can get on with things ASAP. 8979d659ae1SPeter Zijlstra * 8989d659ae1SPeter Zijlstra * Except when HANDOFF, in that case we must not clear the owner field, 8999d659ae1SPeter Zijlstra * but instead set it to the top waiter. 90001768b42SPeter Zijlstra */ 9019d659ae1SPeter Zijlstra owner = atomic_long_read(&lock->owner); 9029d659ae1SPeter Zijlstra for (;;) { 903e6b4457bSPeter Zijlstra MUTEX_WARN_ON(__owner_task(owner) != current); 904e6b4457bSPeter Zijlstra MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 9059d659ae1SPeter Zijlstra 9069d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 9079d659ae1SPeter Zijlstra break; 9089d659ae1SPeter Zijlstra 909ab4e4d9fSPeter Zijlstra if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) { 9109d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_WAITERS) 9119d659ae1SPeter Zijlstra break; 9129d659ae1SPeter Zijlstra 9133ca0ff57SPeter Zijlstra return; 9149d659ae1SPeter Zijlstra } 9159d659ae1SPeter Zijlstra } 91601768b42SPeter Zijlstra 917ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 9181d8fe7dcSJason Low debug_mutex_unlock(lock); 91901768b42SPeter Zijlstra if (!list_empty(&lock->wait_list)) { 92001768b42SPeter Zijlstra /* get the first entry from the wait-list: */ 92101768b42SPeter Zijlstra struct mutex_waiter *waiter = 9229d659ae1SPeter Zijlstra list_first_entry(&lock->wait_list, 92301768b42SPeter Zijlstra struct mutex_waiter, list); 92401768b42SPeter Zijlstra 9259d659ae1SPeter Zijlstra next = waiter->task; 9269d659ae1SPeter Zijlstra 92701768b42SPeter Zijlstra debug_mutex_wake_waiter(lock, waiter); 9289d659ae1SPeter Zijlstra wake_q_add(&wake_q, next); 92901768b42SPeter Zijlstra } 93001768b42SPeter Zijlstra 9319d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 9329d659ae1SPeter Zijlstra __mutex_handoff(lock, next); 9339d659ae1SPeter Zijlstra 934ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 9359d659ae1SPeter Zijlstra 9361329ce6fSDavidlohr Bueso wake_up_q(&wake_q); 93701768b42SPeter Zijlstra } 93801768b42SPeter Zijlstra 93901768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 94001768b42SPeter Zijlstra /* 94101768b42SPeter Zijlstra * Here come the less common (and hence less performance-critical) APIs: 94201768b42SPeter Zijlstra * mutex_lock_interruptible() and mutex_trylock(). 94301768b42SPeter Zijlstra */ 94401768b42SPeter Zijlstra static noinline int __sched 94501768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock); 94601768b42SPeter Zijlstra 94701768b42SPeter Zijlstra static noinline int __sched 94801768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock); 94901768b42SPeter Zijlstra 95001768b42SPeter Zijlstra /** 95145dbac0eSMatthew Wilcox * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals. 95245dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 95301768b42SPeter Zijlstra * 95445dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). If a signal is delivered while the 95545dbac0eSMatthew Wilcox * process is sleeping, this function will return without acquiring the 95645dbac0eSMatthew Wilcox * mutex. 95701768b42SPeter Zijlstra * 95845dbac0eSMatthew Wilcox * Context: Process context. 95945dbac0eSMatthew Wilcox * Return: 0 if the lock was successfully acquired or %-EINTR if a 96045dbac0eSMatthew Wilcox * signal arrived. 96101768b42SPeter Zijlstra */ 96201768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock) 96301768b42SPeter Zijlstra { 96401768b42SPeter Zijlstra might_sleep(); 9653ca0ff57SPeter Zijlstra 9663ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 96701768b42SPeter Zijlstra return 0; 9683ca0ff57SPeter Zijlstra 96901768b42SPeter Zijlstra return __mutex_lock_interruptible_slowpath(lock); 97001768b42SPeter Zijlstra } 97101768b42SPeter Zijlstra 97201768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible); 97301768b42SPeter Zijlstra 97445dbac0eSMatthew Wilcox /** 97545dbac0eSMatthew Wilcox * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals. 97645dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 97745dbac0eSMatthew Wilcox * 97845dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). If a signal which will be fatal to 97945dbac0eSMatthew Wilcox * the current process is delivered while the process is sleeping, this 98045dbac0eSMatthew Wilcox * function will return without acquiring the mutex. 98145dbac0eSMatthew Wilcox * 98245dbac0eSMatthew Wilcox * Context: Process context. 98345dbac0eSMatthew Wilcox * Return: 0 if the lock was successfully acquired or %-EINTR if a 98445dbac0eSMatthew Wilcox * fatal signal arrived. 98545dbac0eSMatthew Wilcox */ 98601768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock) 98701768b42SPeter Zijlstra { 98801768b42SPeter Zijlstra might_sleep(); 9893ca0ff57SPeter Zijlstra 9903ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 99101768b42SPeter Zijlstra return 0; 9923ca0ff57SPeter Zijlstra 99301768b42SPeter Zijlstra return __mutex_lock_killable_slowpath(lock); 99401768b42SPeter Zijlstra } 99501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable); 99601768b42SPeter Zijlstra 99745dbac0eSMatthew Wilcox /** 99845dbac0eSMatthew Wilcox * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O 99945dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 100045dbac0eSMatthew Wilcox * 100145dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). While the task is waiting for this 100245dbac0eSMatthew Wilcox * mutex, it will be accounted as being in the IO wait state by the 100345dbac0eSMatthew Wilcox * scheduler. 100445dbac0eSMatthew Wilcox * 100545dbac0eSMatthew Wilcox * Context: Process context. 100645dbac0eSMatthew Wilcox */ 10071460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock) 10081460cb65STejun Heo { 10091460cb65STejun Heo int token; 10101460cb65STejun Heo 10111460cb65STejun Heo token = io_schedule_prepare(); 10121460cb65STejun Heo mutex_lock(lock); 10131460cb65STejun Heo io_schedule_finish(token); 10141460cb65STejun Heo } 10151460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io); 10161460cb65STejun Heo 10173ca0ff57SPeter Zijlstra static noinline void __sched 10183ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock) 101901768b42SPeter Zijlstra { 1020427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_); 102101768b42SPeter Zijlstra } 102201768b42SPeter Zijlstra 102301768b42SPeter Zijlstra static noinline int __sched 102401768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock) 102501768b42SPeter Zijlstra { 1026427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_); 102701768b42SPeter Zijlstra } 102801768b42SPeter Zijlstra 102901768b42SPeter Zijlstra static noinline int __sched 103001768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock) 103101768b42SPeter Zijlstra { 1032427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_); 103301768b42SPeter Zijlstra } 103401768b42SPeter Zijlstra 103501768b42SPeter Zijlstra static noinline int __sched 103601768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 103701768b42SPeter Zijlstra { 1038cf702eddSPeter Zijlstra return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, 1039427b1820SPeter Zijlstra _RET_IP_, ctx); 104001768b42SPeter Zijlstra } 104101768b42SPeter Zijlstra 104201768b42SPeter Zijlstra static noinline int __sched 104301768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, 104401768b42SPeter Zijlstra struct ww_acquire_ctx *ctx) 104501768b42SPeter Zijlstra { 1046cf702eddSPeter Zijlstra return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, 1047427b1820SPeter Zijlstra _RET_IP_, ctx); 104801768b42SPeter Zijlstra } 104901768b42SPeter Zijlstra 105001768b42SPeter Zijlstra #endif 105101768b42SPeter Zijlstra 105201768b42SPeter Zijlstra /** 105301768b42SPeter Zijlstra * mutex_trylock - try to acquire the mutex, without waiting 105401768b42SPeter Zijlstra * @lock: the mutex to be acquired 105501768b42SPeter Zijlstra * 105601768b42SPeter Zijlstra * Try to acquire the mutex atomically. Returns 1 if the mutex 105701768b42SPeter Zijlstra * has been acquired successfully, and 0 on contention. 105801768b42SPeter Zijlstra * 105901768b42SPeter Zijlstra * NOTE: this function follows the spin_trylock() convention, so 106001768b42SPeter Zijlstra * it is negated from the down_trylock() return values! Be careful 106101768b42SPeter Zijlstra * about this when converting semaphore users to mutexes. 106201768b42SPeter Zijlstra * 106301768b42SPeter Zijlstra * This function must not be used in interrupt context. The 106401768b42SPeter Zijlstra * mutex must be released by the same task that acquired it. 106501768b42SPeter Zijlstra */ 106601768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock) 106701768b42SPeter Zijlstra { 10686c11c6e3SSebastian Andrzej Siewior bool locked; 106901768b42SPeter Zijlstra 1070e6b4457bSPeter Zijlstra MUTEX_WARN_ON(lock->magic != lock); 10716c11c6e3SSebastian Andrzej Siewior 10726c11c6e3SSebastian Andrzej Siewior locked = __mutex_trylock(lock); 10733ca0ff57SPeter Zijlstra if (locked) 10743ca0ff57SPeter Zijlstra mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 107501768b42SPeter Zijlstra 10763ca0ff57SPeter Zijlstra return locked; 107701768b42SPeter Zijlstra } 107801768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock); 107901768b42SPeter Zijlstra 108001768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 108101768b42SPeter Zijlstra int __sched 1082c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 108301768b42SPeter Zijlstra { 108401768b42SPeter Zijlstra might_sleep(); 108501768b42SPeter Zijlstra 10863ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 1087ea9e0fb8SNicolai Hähnle if (ctx) 108801768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 10893ca0ff57SPeter Zijlstra return 0; 10903ca0ff57SPeter Zijlstra } 10913ca0ff57SPeter Zijlstra 10923ca0ff57SPeter Zijlstra return __ww_mutex_lock_slowpath(lock, ctx); 109301768b42SPeter Zijlstra } 1094c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock); 109501768b42SPeter Zijlstra 109601768b42SPeter Zijlstra int __sched 1097c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 109801768b42SPeter Zijlstra { 109901768b42SPeter Zijlstra might_sleep(); 110001768b42SPeter Zijlstra 11013ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 1102ea9e0fb8SNicolai Hähnle if (ctx) 110301768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 11043ca0ff57SPeter Zijlstra return 0; 11053ca0ff57SPeter Zijlstra } 11063ca0ff57SPeter Zijlstra 11073ca0ff57SPeter Zijlstra return __ww_mutex_lock_interruptible_slowpath(lock, ctx); 110801768b42SPeter Zijlstra } 1109c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible); 111001768b42SPeter Zijlstra 1111bb630f9fSThomas Gleixner #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ 1112bb630f9fSThomas Gleixner #endif /* !CONFIG_PREEMPT_RT */ 111301768b42SPeter Zijlstra 111401768b42SPeter Zijlstra /** 111501768b42SPeter Zijlstra * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 111601768b42SPeter Zijlstra * @cnt: the atomic which we are to dec 111701768b42SPeter Zijlstra * @lock: the mutex to return holding if we dec to 0 111801768b42SPeter Zijlstra * 111901768b42SPeter Zijlstra * return true and hold lock if we dec to 0, return false otherwise 112001768b42SPeter Zijlstra */ 112101768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) 112201768b42SPeter Zijlstra { 112301768b42SPeter Zijlstra /* dec if we can't possibly hit 0 */ 112401768b42SPeter Zijlstra if (atomic_add_unless(cnt, -1, 1)) 112501768b42SPeter Zijlstra return 0; 112601768b42SPeter Zijlstra /* we might hit 0, so take the lock */ 112701768b42SPeter Zijlstra mutex_lock(lock); 112801768b42SPeter Zijlstra if (!atomic_dec_and_test(cnt)) { 112901768b42SPeter Zijlstra /* when we actually did the dec, we didn't hit 0 */ 113001768b42SPeter Zijlstra mutex_unlock(lock); 113101768b42SPeter Zijlstra return 0; 113201768b42SPeter Zijlstra } 113301768b42SPeter Zijlstra /* we hit 0, and we hold the lock */ 113401768b42SPeter Zijlstra return 1; 113501768b42SPeter Zijlstra } 113601768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock); 1137