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 33*16edd9b5SNamhyung Kim #define CREATE_TRACE_POINTS 34*16edd9b5SNamhyung Kim #include <trace/events/lock.h> 35*16edd9b5SNamhyung Kim 36bb630f9fSThomas Gleixner #ifndef CONFIG_PREEMPT_RT 37a321fb90SThomas Gleixner #include "mutex.h" 38a321fb90SThomas Gleixner 3901768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_MUTEXES 40e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond) 4101768b42SPeter Zijlstra #else 42e6b4457bSPeter Zijlstra # define MUTEX_WARN_ON(cond) 4301768b42SPeter Zijlstra #endif 4401768b42SPeter Zijlstra 4501768b42SPeter Zijlstra void 4601768b42SPeter Zijlstra __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) 4701768b42SPeter Zijlstra { 483ca0ff57SPeter Zijlstra atomic_long_set(&lock->owner, 0); 49ebf4c55cSThomas Gleixner raw_spin_lock_init(&lock->wait_lock); 5001768b42SPeter Zijlstra INIT_LIST_HEAD(&lock->wait_list); 5101768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 524d9d951eSJason Low osq_lock_init(&lock->osq); 5301768b42SPeter Zijlstra #endif 5401768b42SPeter Zijlstra 5501768b42SPeter Zijlstra debug_mutex_init(lock, name, key); 5601768b42SPeter Zijlstra } 5701768b42SPeter Zijlstra EXPORT_SYMBOL(__mutex_init); 5801768b42SPeter Zijlstra 593ca0ff57SPeter Zijlstra /* 603ca0ff57SPeter Zijlstra * @owner: contains: 'struct task_struct *' to the current lock owner, 613ca0ff57SPeter Zijlstra * NULL means not owned. Since task_struct pointers are aligned at 62e274795eSPeter Zijlstra * at least L1_CACHE_BYTES, we have low bits to store extra state. 633ca0ff57SPeter Zijlstra * 643ca0ff57SPeter Zijlstra * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup. 659d659ae1SPeter Zijlstra * Bit1 indicates unlock needs to hand the lock to the top-waiter 66e274795eSPeter Zijlstra * Bit2 indicates handoff has been done and we're waiting for pickup. 673ca0ff57SPeter Zijlstra */ 683ca0ff57SPeter Zijlstra #define MUTEX_FLAG_WAITERS 0x01 699d659ae1SPeter Zijlstra #define MUTEX_FLAG_HANDOFF 0x02 70e274795eSPeter Zijlstra #define MUTEX_FLAG_PICKUP 0x04 713ca0ff57SPeter Zijlstra 72e274795eSPeter Zijlstra #define MUTEX_FLAGS 0x07 733ca0ff57SPeter Zijlstra 745f35d5a6SMukesh Ojha /* 755f35d5a6SMukesh Ojha * Internal helper function; C doesn't allow us to hide it :/ 765f35d5a6SMukesh Ojha * 775f35d5a6SMukesh Ojha * DO NOT USE (outside of mutex code). 785f35d5a6SMukesh Ojha */ 795f35d5a6SMukesh Ojha static inline struct task_struct *__mutex_owner(struct mutex *lock) 805f35d5a6SMukesh Ojha { 81a037d269SMukesh Ojha return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS); 825f35d5a6SMukesh Ojha } 835f35d5a6SMukesh Ojha 843ca0ff57SPeter Zijlstra static inline struct task_struct *__owner_task(unsigned long owner) 853ca0ff57SPeter Zijlstra { 863ca0ff57SPeter Zijlstra return (struct task_struct *)(owner & ~MUTEX_FLAGS); 873ca0ff57SPeter Zijlstra } 883ca0ff57SPeter Zijlstra 895f35d5a6SMukesh Ojha bool mutex_is_locked(struct mutex *lock) 905f35d5a6SMukesh Ojha { 915f35d5a6SMukesh Ojha return __mutex_owner(lock) != NULL; 925f35d5a6SMukesh Ojha } 935f35d5a6SMukesh Ojha EXPORT_SYMBOL(mutex_is_locked); 945f35d5a6SMukesh Ojha 953ca0ff57SPeter Zijlstra static inline unsigned long __owner_flags(unsigned long owner) 963ca0ff57SPeter Zijlstra { 973ca0ff57SPeter Zijlstra return owner & MUTEX_FLAGS; 983ca0ff57SPeter Zijlstra } 993ca0ff57SPeter Zijlstra 10012235da8SMaarten Lankhorst /* 10112235da8SMaarten Lankhorst * Returns: __mutex_owner(lock) on failure or NULL on success. 10212235da8SMaarten Lankhorst */ 103ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff) 1043ca0ff57SPeter Zijlstra { 1053ca0ff57SPeter Zijlstra unsigned long owner, curr = (unsigned long)current; 1063ca0ff57SPeter Zijlstra 1073ca0ff57SPeter Zijlstra owner = atomic_long_read(&lock->owner); 1083ca0ff57SPeter Zijlstra for (;;) { /* must loop, can race against a flag */ 109ab4e4d9fSPeter Zijlstra unsigned long flags = __owner_flags(owner); 110e274795eSPeter Zijlstra unsigned long task = owner & ~MUTEX_FLAGS; 1113ca0ff57SPeter Zijlstra 112e274795eSPeter Zijlstra if (task) { 113ad90880dSPeter Zijlstra if (flags & MUTEX_FLAG_PICKUP) { 114ad90880dSPeter Zijlstra if (task != curr) 115e274795eSPeter Zijlstra break; 116e274795eSPeter Zijlstra flags &= ~MUTEX_FLAG_PICKUP; 117ad90880dSPeter Zijlstra } else if (handoff) { 118ad90880dSPeter Zijlstra if (flags & MUTEX_FLAG_HANDOFF) 119ad90880dSPeter Zijlstra break; 120ad90880dSPeter Zijlstra flags |= MUTEX_FLAG_HANDOFF; 121ad90880dSPeter Zijlstra } else { 122ad90880dSPeter Zijlstra break; 123ad90880dSPeter Zijlstra } 124e274795eSPeter Zijlstra } else { 125e6b4457bSPeter Zijlstra MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP)); 126ad90880dSPeter Zijlstra task = curr; 1279d659ae1SPeter Zijlstra } 1283ca0ff57SPeter Zijlstra 129ad90880dSPeter Zijlstra if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) { 130ad90880dSPeter Zijlstra if (task == curr) 131e274795eSPeter Zijlstra return NULL; 132ad90880dSPeter Zijlstra break; 133ad90880dSPeter Zijlstra } 1343ca0ff57SPeter Zijlstra } 135e274795eSPeter Zijlstra 136e274795eSPeter Zijlstra return __owner_task(owner); 137e274795eSPeter Zijlstra } 138e274795eSPeter Zijlstra 139e274795eSPeter Zijlstra /* 140ad90880dSPeter Zijlstra * Trylock or set HANDOFF 141ad90880dSPeter Zijlstra */ 142ad90880dSPeter Zijlstra static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff) 143ad90880dSPeter Zijlstra { 144ad90880dSPeter Zijlstra return !__mutex_trylock_common(lock, handoff); 145ad90880dSPeter Zijlstra } 146ad90880dSPeter Zijlstra 147ad90880dSPeter Zijlstra /* 148e274795eSPeter Zijlstra * Actual trylock that will work on any unlocked state. 149e274795eSPeter Zijlstra */ 150e274795eSPeter Zijlstra static inline bool __mutex_trylock(struct mutex *lock) 151e274795eSPeter Zijlstra { 152ad90880dSPeter Zijlstra return !__mutex_trylock_common(lock, false); 1533ca0ff57SPeter Zijlstra } 1543ca0ff57SPeter Zijlstra 1553ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 1563ca0ff57SPeter Zijlstra /* 1573ca0ff57SPeter Zijlstra * Lockdep annotations are contained to the slow paths for simplicity. 1583ca0ff57SPeter Zijlstra * There is nothing that would stop spreading the lockdep annotations outwards 1593ca0ff57SPeter Zijlstra * except more code. 1603ca0ff57SPeter Zijlstra */ 1613ca0ff57SPeter Zijlstra 1623ca0ff57SPeter Zijlstra /* 1633ca0ff57SPeter Zijlstra * Optimistic trylock that only works in the uncontended case. Make sure to 1643ca0ff57SPeter Zijlstra * follow with a __mutex_trylock() before failing. 1653ca0ff57SPeter Zijlstra */ 1663ca0ff57SPeter Zijlstra static __always_inline bool __mutex_trylock_fast(struct mutex *lock) 1673ca0ff57SPeter Zijlstra { 1683ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 169c427f695SPeter Zijlstra unsigned long zero = 0UL; 1703ca0ff57SPeter Zijlstra 171c427f695SPeter Zijlstra if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr)) 1723ca0ff57SPeter Zijlstra return true; 1733ca0ff57SPeter Zijlstra 1743ca0ff57SPeter Zijlstra return false; 1753ca0ff57SPeter Zijlstra } 1763ca0ff57SPeter Zijlstra 1773ca0ff57SPeter Zijlstra static __always_inline bool __mutex_unlock_fast(struct mutex *lock) 1783ca0ff57SPeter Zijlstra { 1793ca0ff57SPeter Zijlstra unsigned long curr = (unsigned long)current; 1803ca0ff57SPeter Zijlstra 181ab4e4d9fSPeter Zijlstra return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); 1823ca0ff57SPeter Zijlstra } 1833ca0ff57SPeter Zijlstra #endif 1843ca0ff57SPeter Zijlstra 1853ca0ff57SPeter Zijlstra static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) 1863ca0ff57SPeter Zijlstra { 1873ca0ff57SPeter Zijlstra atomic_long_or(flag, &lock->owner); 1883ca0ff57SPeter Zijlstra } 1893ca0ff57SPeter Zijlstra 1903ca0ff57SPeter Zijlstra static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) 1913ca0ff57SPeter Zijlstra { 1923ca0ff57SPeter Zijlstra atomic_long_andnot(flag, &lock->owner); 1933ca0ff57SPeter Zijlstra } 1943ca0ff57SPeter Zijlstra 1959d659ae1SPeter Zijlstra static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) 1969d659ae1SPeter Zijlstra { 1979d659ae1SPeter Zijlstra return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; 1989d659ae1SPeter Zijlstra } 1999d659ae1SPeter Zijlstra 2009d659ae1SPeter Zijlstra /* 20108295b3bSThomas Hellstrom * Add @waiter to a given location in the lock wait_list and set the 20208295b3bSThomas Hellstrom * FLAG_WAITERS flag if it's the first waiter. 20308295b3bSThomas Hellstrom */ 2043a010c49SZqiang static void 20508295b3bSThomas Hellstrom __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, 20608295b3bSThomas Hellstrom struct list_head *list) 20708295b3bSThomas Hellstrom { 20808295b3bSThomas Hellstrom debug_mutex_add_waiter(lock, waiter, current); 20908295b3bSThomas Hellstrom 21008295b3bSThomas Hellstrom list_add_tail(&waiter->list, list); 21108295b3bSThomas Hellstrom if (__mutex_waiter_is_first(lock, waiter)) 21208295b3bSThomas Hellstrom __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); 21308295b3bSThomas Hellstrom } 21408295b3bSThomas Hellstrom 2153a010c49SZqiang static void 2163a010c49SZqiang __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter) 2173a010c49SZqiang { 2183a010c49SZqiang list_del(&waiter->list); 2193a010c49SZqiang if (likely(list_empty(&lock->wait_list))) 2203a010c49SZqiang __mutex_clear_flag(lock, MUTEX_FLAGS); 2213a010c49SZqiang 2223a010c49SZqiang debug_mutex_remove_waiter(lock, waiter, current); 2233a010c49SZqiang } 2243a010c49SZqiang 22508295b3bSThomas Hellstrom /* 2269d659ae1SPeter Zijlstra * Give up ownership to a specific task, when @task = NULL, this is equivalent 227e2db7592SIngo Molnar * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves 228e274795eSPeter Zijlstra * WAITERS. Provides RELEASE semantics like a regular unlock, the 229e274795eSPeter Zijlstra * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff. 2309d659ae1SPeter Zijlstra */ 2319d659ae1SPeter Zijlstra static void __mutex_handoff(struct mutex *lock, struct task_struct *task) 2329d659ae1SPeter Zijlstra { 2339d659ae1SPeter Zijlstra unsigned long owner = atomic_long_read(&lock->owner); 2349d659ae1SPeter Zijlstra 2359d659ae1SPeter Zijlstra for (;;) { 236ab4e4d9fSPeter Zijlstra unsigned long new; 2379d659ae1SPeter Zijlstra 238e6b4457bSPeter Zijlstra MUTEX_WARN_ON(__owner_task(owner) != current); 239e6b4457bSPeter Zijlstra MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 2409d659ae1SPeter Zijlstra 2419d659ae1SPeter Zijlstra new = (owner & MUTEX_FLAG_WAITERS); 2429d659ae1SPeter Zijlstra new |= (unsigned long)task; 243e274795eSPeter Zijlstra if (task) 244e274795eSPeter Zijlstra new |= MUTEX_FLAG_PICKUP; 2459d659ae1SPeter Zijlstra 246ab4e4d9fSPeter Zijlstra if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new)) 2479d659ae1SPeter Zijlstra break; 2489d659ae1SPeter Zijlstra } 2499d659ae1SPeter Zijlstra } 2509d659ae1SPeter Zijlstra 25101768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 25201768b42SPeter Zijlstra /* 25301768b42SPeter Zijlstra * We split the mutex lock/unlock logic into separate fastpath and 25401768b42SPeter Zijlstra * slowpath functions, to reduce the register pressure on the fastpath. 25501768b42SPeter Zijlstra * We also put the fastpath first in the kernel image, to make sure the 25601768b42SPeter Zijlstra * branch is predicted by the CPU as default-untaken. 25701768b42SPeter Zijlstra */ 2583ca0ff57SPeter Zijlstra static void __sched __mutex_lock_slowpath(struct mutex *lock); 25901768b42SPeter Zijlstra 26001768b42SPeter Zijlstra /** 26101768b42SPeter Zijlstra * mutex_lock - acquire the mutex 26201768b42SPeter Zijlstra * @lock: the mutex to be acquired 26301768b42SPeter Zijlstra * 26401768b42SPeter Zijlstra * Lock the mutex exclusively for this task. If the mutex is not 26501768b42SPeter Zijlstra * available right now, it will sleep until it can get it. 26601768b42SPeter Zijlstra * 26701768b42SPeter Zijlstra * The mutex must later on be released by the same task that 26801768b42SPeter Zijlstra * acquired it. Recursive locking is not allowed. The task 26901768b42SPeter Zijlstra * may not exit without first unlocking the mutex. Also, kernel 270139b6fd2SSharon Dvir * memory where the mutex resides must not be freed with 27101768b42SPeter Zijlstra * the mutex still locked. The mutex must first be initialized 27201768b42SPeter Zijlstra * (or statically defined) before it can be locked. memset()-ing 27301768b42SPeter Zijlstra * the mutex to 0 is not allowed. 27401768b42SPeter Zijlstra * 27501768b42SPeter Zijlstra * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging 27601768b42SPeter Zijlstra * checks that will enforce the restrictions and will also do 2777b4ff1adSMauro Carvalho Chehab * deadlock debugging) 27801768b42SPeter Zijlstra * 27901768b42SPeter Zijlstra * This function is similar to (but not equivalent to) down(). 28001768b42SPeter Zijlstra */ 28101768b42SPeter Zijlstra void __sched mutex_lock(struct mutex *lock) 28201768b42SPeter Zijlstra { 28301768b42SPeter Zijlstra might_sleep(); 28401768b42SPeter Zijlstra 2853ca0ff57SPeter Zijlstra if (!__mutex_trylock_fast(lock)) 2863ca0ff57SPeter Zijlstra __mutex_lock_slowpath(lock); 2873ca0ff57SPeter Zijlstra } 28801768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock); 28901768b42SPeter Zijlstra #endif 29001768b42SPeter Zijlstra 2912674bd18SPeter Zijlstra (Intel) #include "ww_mutex.h" 29276916515SDavidlohr Bueso 29301768b42SPeter Zijlstra #ifdef CONFIG_MUTEX_SPIN_ON_OWNER 294c516df97SNicolai Hähnle 295ad90880dSPeter Zijlstra /* 296ad90880dSPeter Zijlstra * Trylock variant that returns the owning task on failure. 297ad90880dSPeter Zijlstra */ 298ad90880dSPeter Zijlstra static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock) 299ad90880dSPeter Zijlstra { 300ad90880dSPeter Zijlstra return __mutex_trylock_common(lock, false); 301ad90880dSPeter Zijlstra } 302ad90880dSPeter Zijlstra 303c516df97SNicolai Hähnle static inline 304c516df97SNicolai Hähnle bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 305c516df97SNicolai Hähnle struct mutex_waiter *waiter) 306c516df97SNicolai Hähnle { 307c516df97SNicolai Hähnle struct ww_mutex *ww; 308c516df97SNicolai Hähnle 309c516df97SNicolai Hähnle ww = container_of(lock, struct ww_mutex, base); 310c516df97SNicolai Hähnle 31101768b42SPeter Zijlstra /* 312c516df97SNicolai Hähnle * If ww->ctx is set the contents are undefined, only 313c516df97SNicolai Hähnle * by acquiring wait_lock there is a guarantee that 314c516df97SNicolai Hähnle * they are not invalid when reading. 315c516df97SNicolai Hähnle * 316c516df97SNicolai Hähnle * As such, when deadlock detection needs to be 317c516df97SNicolai Hähnle * performed the optimistic spinning cannot be done. 318c516df97SNicolai Hähnle * 319c516df97SNicolai Hähnle * Check this in every inner iteration because we may 320c516df97SNicolai Hähnle * be racing against another thread's ww_mutex_lock. 321c516df97SNicolai Hähnle */ 322c516df97SNicolai Hähnle if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx)) 323c516df97SNicolai Hähnle return false; 324c516df97SNicolai Hähnle 325c516df97SNicolai Hähnle /* 326c516df97SNicolai Hähnle * If we aren't on the wait list yet, cancel the spin 327c516df97SNicolai Hähnle * if there are waiters. We want to avoid stealing the 328c516df97SNicolai Hähnle * lock from a waiter with an earlier stamp, since the 329c516df97SNicolai Hähnle * other thread may already own a lock that we also 330c516df97SNicolai Hähnle * need. 331c516df97SNicolai Hähnle */ 332c516df97SNicolai Hähnle if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS)) 333c516df97SNicolai Hähnle return false; 334c516df97SNicolai Hähnle 335c516df97SNicolai Hähnle /* 336c516df97SNicolai Hähnle * Similarly, stop spinning if we are no longer the 337c516df97SNicolai Hähnle * first waiter. 338c516df97SNicolai Hähnle */ 339c516df97SNicolai Hähnle if (waiter && !__mutex_waiter_is_first(lock, waiter)) 340c516df97SNicolai Hähnle return false; 341c516df97SNicolai Hähnle 342c516df97SNicolai Hähnle return true; 343c516df97SNicolai Hähnle } 344c516df97SNicolai Hähnle 34501768b42SPeter Zijlstra /* 34625f13b40SNicolai Hähnle * Look out! "owner" is an entirely speculative pointer access and not 34725f13b40SNicolai Hähnle * reliable. 34825f13b40SNicolai Hähnle * 34925f13b40SNicolai Hähnle * "noinline" so that this function shows up on perf profiles. 35001768b42SPeter Zijlstra */ 35101768b42SPeter Zijlstra static noinline 35225f13b40SNicolai Hähnle bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, 353c516df97SNicolai Hähnle struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter) 35401768b42SPeter Zijlstra { 35501ac33c1SJason Low bool ret = true; 356be1f7bf2SJason Low 3576c2787f2SYanfei Xu lockdep_assert_preemption_disabled(); 3586c2787f2SYanfei Xu 3593ca0ff57SPeter Zijlstra while (__mutex_owner(lock) == owner) { 360be1f7bf2SJason Low /* 361be1f7bf2SJason Low * Ensure we emit the owner->on_cpu, dereference _after_ 3626c2787f2SYanfei Xu * checking lock->owner still matches owner. And we already 3636c2787f2SYanfei Xu * disabled preemption which is equal to the RCU read-side 3646c2787f2SYanfei Xu * crital section in optimistic spinning code. Thus the 3656c2787f2SYanfei Xu * task_strcut structure won't go away during the spinning 3666c2787f2SYanfei Xu * period 367be1f7bf2SJason Low */ 368be1f7bf2SJason Low barrier(); 369be1f7bf2SJason Low 37005ffc951SPan Xinhui /* 37105ffc951SPan Xinhui * Use vcpu_is_preempted to detect lock holder preemption issue. 37205ffc951SPan Xinhui */ 373c0bed69dSKefeng Wang if (!owner_on_cpu(owner) || need_resched()) { 374be1f7bf2SJason Low ret = false; 375be1f7bf2SJason Low break; 376be1f7bf2SJason Low } 37701768b42SPeter Zijlstra 378c516df97SNicolai Hähnle if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) { 37925f13b40SNicolai Hähnle ret = false; 38025f13b40SNicolai Hähnle break; 38125f13b40SNicolai Hähnle } 38225f13b40SNicolai Hähnle 383f2f09a4cSChristian Borntraeger cpu_relax(); 38401768b42SPeter Zijlstra } 38501768b42SPeter Zijlstra 386be1f7bf2SJason Low return ret; 38701768b42SPeter Zijlstra } 38801768b42SPeter Zijlstra 38901768b42SPeter Zijlstra /* 39001768b42SPeter Zijlstra * Initial check for entering the mutex spinning loop 39101768b42SPeter Zijlstra */ 39201768b42SPeter Zijlstra static inline int mutex_can_spin_on_owner(struct mutex *lock) 39301768b42SPeter Zijlstra { 39401768b42SPeter Zijlstra struct task_struct *owner; 39501768b42SPeter Zijlstra int retval = 1; 39601768b42SPeter Zijlstra 3976c2787f2SYanfei Xu lockdep_assert_preemption_disabled(); 3986c2787f2SYanfei Xu 39946af29e4SJason Low if (need_resched()) 40046af29e4SJason Low return 0; 40146af29e4SJason Low 4026c2787f2SYanfei Xu /* 4036c2787f2SYanfei Xu * We already disabled preemption which is equal to the RCU read-side 4046c2787f2SYanfei Xu * crital section in optimistic spinning code. Thus the task_strcut 4056c2787f2SYanfei Xu * structure won't go away during the spinning period. 4066c2787f2SYanfei Xu */ 4073ca0ff57SPeter Zijlstra owner = __mutex_owner(lock); 40801768b42SPeter Zijlstra if (owner) 409c0bed69dSKefeng Wang retval = owner_on_cpu(owner); 41076916515SDavidlohr Bueso 41176916515SDavidlohr Bueso /* 4123ca0ff57SPeter Zijlstra * If lock->owner is not set, the mutex has been released. Return true 4133ca0ff57SPeter Zijlstra * such that we'll trylock in the spin path, which is a faster option 4143ca0ff57SPeter Zijlstra * than the blocking slow path. 41576916515SDavidlohr Bueso */ 4163ca0ff57SPeter Zijlstra return retval; 41776916515SDavidlohr Bueso } 41876916515SDavidlohr Bueso 41976916515SDavidlohr Bueso /* 42076916515SDavidlohr Bueso * Optimistic spinning. 42176916515SDavidlohr Bueso * 42276916515SDavidlohr Bueso * We try to spin for acquisition when we find that the lock owner 42376916515SDavidlohr Bueso * is currently running on a (different) CPU and while we don't 42476916515SDavidlohr Bueso * need to reschedule. The rationale is that if the lock owner is 42576916515SDavidlohr Bueso * running, it is likely to release the lock soon. 42676916515SDavidlohr Bueso * 42776916515SDavidlohr Bueso * The mutex spinners are queued up using MCS lock so that only one 42876916515SDavidlohr Bueso * spinner can compete for the mutex. However, if mutex spinning isn't 42976916515SDavidlohr Bueso * going to happen, there is no point in going through the lock/unlock 43076916515SDavidlohr Bueso * overhead. 43176916515SDavidlohr Bueso * 43276916515SDavidlohr Bueso * Returns true when the lock was taken, otherwise false, indicating 43376916515SDavidlohr Bueso * that we need to jump to the slowpath and sleep. 434b341afb3SWaiman Long * 435b341afb3SWaiman Long * The waiter flag is set to true if the spinner is a waiter in the wait 436b341afb3SWaiman Long * queue. The waiter-spinner will spin on the lock directly and concurrently 437b341afb3SWaiman Long * with the spinner at the head of the OSQ, if present, until the owner is 438b341afb3SWaiman Long * changed to itself. 43976916515SDavidlohr Bueso */ 440427b1820SPeter Zijlstra static __always_inline bool 441427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 4425de2055dSWaiman Long struct mutex_waiter *waiter) 44376916515SDavidlohr Bueso { 444b341afb3SWaiman Long if (!waiter) { 445b341afb3SWaiman Long /* 446b341afb3SWaiman Long * The purpose of the mutex_can_spin_on_owner() function is 447b341afb3SWaiman Long * to eliminate the overhead of osq_lock() and osq_unlock() 448b341afb3SWaiman Long * in case spinning isn't possible. As a waiter-spinner 449b341afb3SWaiman Long * is not going to take OSQ lock anyway, there is no need 450b341afb3SWaiman Long * to call mutex_can_spin_on_owner(). 451b341afb3SWaiman Long */ 45276916515SDavidlohr Bueso if (!mutex_can_spin_on_owner(lock)) 453b341afb3SWaiman Long goto fail; 45476916515SDavidlohr Bueso 455e42f678aSDavidlohr Bueso /* 456e42f678aSDavidlohr Bueso * In order to avoid a stampede of mutex spinners trying to 457e42f678aSDavidlohr Bueso * acquire the mutex all at once, the spinners need to take a 458e42f678aSDavidlohr Bueso * MCS (queued) lock first before spinning on the owner field. 459e42f678aSDavidlohr Bueso */ 46076916515SDavidlohr Bueso if (!osq_lock(&lock->osq)) 461b341afb3SWaiman Long goto fail; 462b341afb3SWaiman Long } 46376916515SDavidlohr Bueso 464b341afb3SWaiman Long for (;;) { 46576916515SDavidlohr Bueso struct task_struct *owner; 46676916515SDavidlohr Bueso 467e274795eSPeter Zijlstra /* Try to acquire the mutex... */ 468e274795eSPeter Zijlstra owner = __mutex_trylock_or_owner(lock); 469e274795eSPeter Zijlstra if (!owner) 470e274795eSPeter Zijlstra break; 47176916515SDavidlohr Bueso 47276916515SDavidlohr Bueso /* 473e274795eSPeter Zijlstra * There's an owner, wait for it to either 47476916515SDavidlohr Bueso * release the lock or go to sleep. 47576916515SDavidlohr Bueso */ 476c516df97SNicolai Hähnle if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter)) 477b341afb3SWaiman Long goto fail_unlock; 47876916515SDavidlohr Bueso 47976916515SDavidlohr Bueso /* 48076916515SDavidlohr Bueso * The cpu_relax() call is a compiler barrier which forces 48176916515SDavidlohr Bueso * everything in this loop to be re-loaded. We don't need 48276916515SDavidlohr Bueso * memory barriers as we'll eventually observe the right 48376916515SDavidlohr Bueso * values at the cost of a few extra spins. 48476916515SDavidlohr Bueso */ 485f2f09a4cSChristian Borntraeger cpu_relax(); 48676916515SDavidlohr Bueso } 48776916515SDavidlohr Bueso 488b341afb3SWaiman Long if (!waiter) 48976916515SDavidlohr Bueso osq_unlock(&lock->osq); 490b341afb3SWaiman Long 491b341afb3SWaiman Long return true; 492b341afb3SWaiman Long 493b341afb3SWaiman Long 494b341afb3SWaiman Long fail_unlock: 495b341afb3SWaiman Long if (!waiter) 496b341afb3SWaiman Long osq_unlock(&lock->osq); 497b341afb3SWaiman Long 498b341afb3SWaiman Long fail: 49976916515SDavidlohr Bueso /* 50076916515SDavidlohr Bueso * If we fell out of the spin path because of need_resched(), 50176916515SDavidlohr Bueso * reschedule now, before we try-lock the mutex. This avoids getting 50276916515SDavidlohr Bueso * scheduled out right after we obtained the mutex. 50376916515SDavidlohr Bueso */ 5046f942a1fSPeter Zijlstra if (need_resched()) { 5056f942a1fSPeter Zijlstra /* 5066f942a1fSPeter Zijlstra * We _should_ have TASK_RUNNING here, but just in case 5076f942a1fSPeter Zijlstra * we do not, make it so, otherwise we might get stuck. 5086f942a1fSPeter Zijlstra */ 5096f942a1fSPeter Zijlstra __set_current_state(TASK_RUNNING); 51076916515SDavidlohr Bueso schedule_preempt_disabled(); 5116f942a1fSPeter Zijlstra } 51276916515SDavidlohr Bueso 51376916515SDavidlohr Bueso return false; 51476916515SDavidlohr Bueso } 51576916515SDavidlohr Bueso #else 516427b1820SPeter Zijlstra static __always_inline bool 517427b1820SPeter Zijlstra mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, 5185de2055dSWaiman Long struct mutex_waiter *waiter) 51976916515SDavidlohr Bueso { 52076916515SDavidlohr Bueso return false; 52176916515SDavidlohr Bueso } 52201768b42SPeter Zijlstra #endif 52301768b42SPeter Zijlstra 5243ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip); 52501768b42SPeter Zijlstra 52601768b42SPeter Zijlstra /** 52701768b42SPeter Zijlstra * mutex_unlock - release the mutex 52801768b42SPeter Zijlstra * @lock: the mutex to be released 52901768b42SPeter Zijlstra * 53001768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously. 53101768b42SPeter Zijlstra * 53201768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 53301768b42SPeter Zijlstra * of a not locked mutex is not allowed. 53401768b42SPeter Zijlstra * 53501768b42SPeter Zijlstra * This function is similar to (but not equivalent to) up(). 53601768b42SPeter Zijlstra */ 53701768b42SPeter Zijlstra void __sched mutex_unlock(struct mutex *lock) 53801768b42SPeter Zijlstra { 5393ca0ff57SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 5403ca0ff57SPeter Zijlstra if (__mutex_unlock_fast(lock)) 5413ca0ff57SPeter Zijlstra return; 54201768b42SPeter Zijlstra #endif 5433ca0ff57SPeter Zijlstra __mutex_unlock_slowpath(lock, _RET_IP_); 54401768b42SPeter Zijlstra } 54501768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_unlock); 54601768b42SPeter Zijlstra 54701768b42SPeter Zijlstra /** 54801768b42SPeter Zijlstra * ww_mutex_unlock - release the w/w mutex 54901768b42SPeter Zijlstra * @lock: the mutex to be released 55001768b42SPeter Zijlstra * 55101768b42SPeter Zijlstra * Unlock a mutex that has been locked by this task previously with any of the 55201768b42SPeter Zijlstra * ww_mutex_lock* functions (with or without an acquire context). It is 55301768b42SPeter Zijlstra * forbidden to release the locks after releasing the acquire context. 55401768b42SPeter Zijlstra * 55501768b42SPeter Zijlstra * This function must not be used in interrupt context. Unlocking 55601768b42SPeter Zijlstra * of a unlocked mutex is not allowed. 55701768b42SPeter Zijlstra */ 55801768b42SPeter Zijlstra void __sched ww_mutex_unlock(struct ww_mutex *lock) 55901768b42SPeter Zijlstra { 560aaa77de1SPeter Zijlstra (Intel) __ww_mutex_unlock(lock); 5613ca0ff57SPeter Zijlstra mutex_unlock(&lock->base); 56201768b42SPeter Zijlstra } 56301768b42SPeter Zijlstra EXPORT_SYMBOL(ww_mutex_unlock); 56401768b42SPeter Zijlstra 56501768b42SPeter Zijlstra /* 56601768b42SPeter Zijlstra * Lock a mutex (possibly interruptible), slowpath: 56701768b42SPeter Zijlstra */ 56801768b42SPeter Zijlstra static __always_inline int __sched 5692f064a59SPeter Zijlstra __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass, 57001768b42SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip, 57101768b42SPeter Zijlstra struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx) 57201768b42SPeter Zijlstra { 57301768b42SPeter Zijlstra struct mutex_waiter waiter; 574a40ca565SWaiman Long struct ww_mutex *ww; 57501768b42SPeter Zijlstra int ret; 57601768b42SPeter Zijlstra 5775de2055dSWaiman Long if (!use_ww_ctx) 5785de2055dSWaiman Long ww_ctx = NULL; 5795de2055dSWaiman Long 580427b1820SPeter Zijlstra might_sleep(); 581ea9e0fb8SNicolai Hähnle 582e6b4457bSPeter Zijlstra MUTEX_WARN_ON(lock->magic != lock); 5836c11c6e3SSebastian Andrzej Siewior 584a40ca565SWaiman Long ww = container_of(lock, struct ww_mutex, base); 5855de2055dSWaiman Long if (ww_ctx) { 5860422e83dSChris Wilson if (unlikely(ww_ctx == READ_ONCE(ww->ctx))) 5870422e83dSChris Wilson return -EALREADY; 58808295b3bSThomas Hellstrom 58908295b3bSThomas Hellstrom /* 59008295b3bSThomas Hellstrom * Reset the wounded flag after a kill. No other process can 59108295b3bSThomas Hellstrom * race and wound us here since they can't have a valid owner 59208295b3bSThomas Hellstrom * pointer if we don't have any locks held. 59308295b3bSThomas Hellstrom */ 59408295b3bSThomas Hellstrom if (ww_ctx->acquired == 0) 59508295b3bSThomas Hellstrom ww_ctx->wounded = 0; 596cf702eddSPeter Zijlstra 597cf702eddSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 598cf702eddSPeter Zijlstra nest_lock = &ww_ctx->dep_map; 599cf702eddSPeter Zijlstra #endif 6000422e83dSChris Wilson } 6010422e83dSChris Wilson 60201768b42SPeter Zijlstra preempt_disable(); 60301768b42SPeter Zijlstra mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); 60401768b42SPeter Zijlstra 605e274795eSPeter Zijlstra if (__mutex_trylock(lock) || 6065de2055dSWaiman Long mutex_optimistic_spin(lock, ww_ctx, NULL)) { 60776916515SDavidlohr Bueso /* got the lock, yay! */ 6083ca0ff57SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 6095de2055dSWaiman Long if (ww_ctx) 6103ca0ff57SPeter Zijlstra ww_mutex_set_context_fastpath(ww, ww_ctx); 61101768b42SPeter Zijlstra preempt_enable(); 61201768b42SPeter Zijlstra return 0; 61301768b42SPeter Zijlstra } 61401768b42SPeter Zijlstra 615ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 6161e820c96SJason Low /* 6173ca0ff57SPeter Zijlstra * After waiting to acquire the wait_lock, try again. 6181e820c96SJason Low */ 619659cf9f5SNicolai Hähnle if (__mutex_trylock(lock)) { 6205de2055dSWaiman Long if (ww_ctx) 62155f036caSPeter Ziljstra __ww_mutex_check_waiters(lock, ww_ctx); 622659cf9f5SNicolai Hähnle 62301768b42SPeter Zijlstra goto skip_wait; 624659cf9f5SNicolai Hähnle } 62501768b42SPeter Zijlstra 62601768b42SPeter Zijlstra debug_mutex_lock_common(lock, &waiter); 627c0afb0ffSPeter Zijlstra waiter.task = current; 628b857174eSSebastian Andrzej Siewior if (use_ww_ctx) 629c0afb0ffSPeter Zijlstra waiter.ww_ctx = ww_ctx; 63001768b42SPeter Zijlstra 6316baa5c60SNicolai Hähnle lock_contended(&lock->dep_map, ip); 6326baa5c60SNicolai Hähnle 6336baa5c60SNicolai Hähnle if (!use_ww_ctx) { 63401768b42SPeter Zijlstra /* add waiting tasks to the end of the waitqueue (FIFO): */ 63508295b3bSThomas Hellstrom __mutex_add_waiter(lock, &waiter, &lock->wait_list); 6366baa5c60SNicolai Hähnle } else { 63755f036caSPeter Ziljstra /* 63855f036caSPeter Ziljstra * Add in stamp order, waking up waiters that must kill 63955f036caSPeter Ziljstra * themselves. 64055f036caSPeter Ziljstra */ 6416baa5c60SNicolai Hähnle ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); 6426baa5c60SNicolai Hähnle if (ret) 64355f036caSPeter Ziljstra goto err_early_kill; 6446baa5c60SNicolai Hähnle } 6456baa5c60SNicolai Hähnle 646642fa448SDavidlohr Bueso set_current_state(state); 64701768b42SPeter Zijlstra for (;;) { 648048661a1SPeter Zijlstra bool first; 649048661a1SPeter Zijlstra 6505bbd7e64SPeter Zijlstra /* 6515bbd7e64SPeter Zijlstra * Once we hold wait_lock, we're serialized against 6525bbd7e64SPeter Zijlstra * mutex_unlock() handing the lock off to us, do a trylock 6535bbd7e64SPeter Zijlstra * before testing the error conditions to make sure we pick up 6545bbd7e64SPeter Zijlstra * the handoff. 6555bbd7e64SPeter Zijlstra */ 656e274795eSPeter Zijlstra if (__mutex_trylock(lock)) 6575bbd7e64SPeter Zijlstra goto acquired; 65801768b42SPeter Zijlstra 65901768b42SPeter Zijlstra /* 66055f036caSPeter Ziljstra * Check for signals and kill conditions while holding 6615bbd7e64SPeter Zijlstra * wait_lock. This ensures the lock cancellation is ordered 6625bbd7e64SPeter Zijlstra * against mutex_unlock() and wake-ups do not go missing. 66301768b42SPeter Zijlstra */ 6643bb5f4acSDavidlohr Bueso if (signal_pending_state(state, current)) { 66501768b42SPeter Zijlstra ret = -EINTR; 66601768b42SPeter Zijlstra goto err; 66701768b42SPeter Zijlstra } 66801768b42SPeter Zijlstra 6695de2055dSWaiman Long if (ww_ctx) { 67055f036caSPeter Ziljstra ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx); 67101768b42SPeter Zijlstra if (ret) 67201768b42SPeter Zijlstra goto err; 67301768b42SPeter Zijlstra } 67401768b42SPeter Zijlstra 675ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 67601768b42SPeter Zijlstra schedule_preempt_disabled(); 6779d659ae1SPeter Zijlstra 6786baa5c60SNicolai Hähnle first = __mutex_waiter_is_first(lock, &waiter); 6795bbd7e64SPeter Zijlstra 680642fa448SDavidlohr Bueso set_current_state(state); 6815bbd7e64SPeter Zijlstra /* 6825bbd7e64SPeter Zijlstra * Here we order against unlock; we must either see it change 6835bbd7e64SPeter Zijlstra * state back to RUNNING and fall through the next schedule(), 6845bbd7e64SPeter Zijlstra * or we must see its unlock and acquire. 6855bbd7e64SPeter Zijlstra */ 686ad90880dSPeter Zijlstra if (__mutex_trylock_or_handoff(lock, first) || 6875de2055dSWaiman Long (first && mutex_optimistic_spin(lock, ww_ctx, &waiter))) 6885bbd7e64SPeter Zijlstra break; 6895bbd7e64SPeter Zijlstra 690ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 69101768b42SPeter Zijlstra } 692ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 6935bbd7e64SPeter Zijlstra acquired: 694642fa448SDavidlohr Bueso __set_current_state(TASK_RUNNING); 69551587bcfSDavidlohr Bueso 6965de2055dSWaiman Long if (ww_ctx) { 69708295b3bSThomas Hellstrom /* 69808295b3bSThomas Hellstrom * Wound-Wait; we stole the lock (!first_waiter), check the 69908295b3bSThomas Hellstrom * waiters as anyone might want to wound us. 70008295b3bSThomas Hellstrom */ 70108295b3bSThomas Hellstrom if (!ww_ctx->is_wait_die && 70208295b3bSThomas Hellstrom !__mutex_waiter_is_first(lock, &waiter)) 70308295b3bSThomas Hellstrom __ww_mutex_check_waiters(lock, ww_ctx); 70408295b3bSThomas Hellstrom } 70508295b3bSThomas Hellstrom 7063a010c49SZqiang __mutex_remove_waiter(lock, &waiter); 7073ca0ff57SPeter Zijlstra 70801768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 70901768b42SPeter Zijlstra 71001768b42SPeter Zijlstra skip_wait: 71101768b42SPeter Zijlstra /* got the lock - cleanup and rejoice! */ 71201768b42SPeter Zijlstra lock_acquired(&lock->dep_map, ip); 71301768b42SPeter Zijlstra 7145de2055dSWaiman Long if (ww_ctx) 71555f036caSPeter Ziljstra ww_mutex_lock_acquired(ww, ww_ctx); 71601768b42SPeter Zijlstra 717ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 71801768b42SPeter Zijlstra preempt_enable(); 71901768b42SPeter Zijlstra return 0; 72001768b42SPeter Zijlstra 72101768b42SPeter Zijlstra err: 722642fa448SDavidlohr Bueso __set_current_state(TASK_RUNNING); 7233a010c49SZqiang __mutex_remove_waiter(lock, &waiter); 72455f036caSPeter Ziljstra err_early_kill: 725ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 72601768b42SPeter Zijlstra debug_mutex_free_waiter(&waiter); 7275facae4fSQian Cai mutex_release(&lock->dep_map, ip); 72801768b42SPeter Zijlstra preempt_enable(); 72901768b42SPeter Zijlstra return ret; 73001768b42SPeter Zijlstra } 73101768b42SPeter Zijlstra 732427b1820SPeter Zijlstra static int __sched 7332f064a59SPeter Zijlstra __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 734427b1820SPeter Zijlstra struct lockdep_map *nest_lock, unsigned long ip) 735427b1820SPeter Zijlstra { 736427b1820SPeter Zijlstra return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false); 737427b1820SPeter Zijlstra } 738427b1820SPeter Zijlstra 739427b1820SPeter Zijlstra static int __sched 7402f064a59SPeter Zijlstra __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, 741cf702eddSPeter Zijlstra unsigned long ip, struct ww_acquire_ctx *ww_ctx) 742427b1820SPeter Zijlstra { 743cf702eddSPeter Zijlstra return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true); 744427b1820SPeter Zijlstra } 745427b1820SPeter Zijlstra 74612235da8SMaarten Lankhorst /** 74712235da8SMaarten Lankhorst * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context 74812235da8SMaarten Lankhorst * @ww: mutex to lock 74912235da8SMaarten Lankhorst * @ww_ctx: optional w/w acquire context 75012235da8SMaarten Lankhorst * 75112235da8SMaarten Lankhorst * Trylocks a mutex with the optional acquire context; no deadlock detection is 75212235da8SMaarten Lankhorst * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise. 75312235da8SMaarten Lankhorst * 75412235da8SMaarten Lankhorst * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is 75512235da8SMaarten Lankhorst * specified, -EALREADY handling may happen in calls to ww_mutex_trylock. 75612235da8SMaarten Lankhorst * 75712235da8SMaarten Lankhorst * A mutex acquired with this function must be released with ww_mutex_unlock. 75812235da8SMaarten Lankhorst */ 75912235da8SMaarten Lankhorst int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx) 76012235da8SMaarten Lankhorst { 76112235da8SMaarten Lankhorst if (!ww_ctx) 76212235da8SMaarten Lankhorst return mutex_trylock(&ww->base); 76312235da8SMaarten Lankhorst 76412235da8SMaarten Lankhorst MUTEX_WARN_ON(ww->base.magic != &ww->base); 76512235da8SMaarten Lankhorst 76612235da8SMaarten Lankhorst /* 76712235da8SMaarten Lankhorst * Reset the wounded flag after a kill. No other process can 76812235da8SMaarten Lankhorst * race and wound us here, since they can't have a valid owner 76912235da8SMaarten Lankhorst * pointer if we don't have any locks held. 77012235da8SMaarten Lankhorst */ 77112235da8SMaarten Lankhorst if (ww_ctx->acquired == 0) 77212235da8SMaarten Lankhorst ww_ctx->wounded = 0; 77312235da8SMaarten Lankhorst 77412235da8SMaarten Lankhorst if (__mutex_trylock(&ww->base)) { 77512235da8SMaarten Lankhorst ww_mutex_set_context_fastpath(ww, ww_ctx); 77612235da8SMaarten Lankhorst mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_); 77712235da8SMaarten Lankhorst return 1; 77812235da8SMaarten Lankhorst } 77912235da8SMaarten Lankhorst 78012235da8SMaarten Lankhorst return 0; 78112235da8SMaarten Lankhorst } 78212235da8SMaarten Lankhorst EXPORT_SYMBOL(ww_mutex_trylock); 78312235da8SMaarten Lankhorst 78401768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC 78501768b42SPeter Zijlstra void __sched 78601768b42SPeter Zijlstra mutex_lock_nested(struct mutex *lock, unsigned int subclass) 78701768b42SPeter Zijlstra { 788427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); 78901768b42SPeter Zijlstra } 79001768b42SPeter Zijlstra 79101768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_nested); 79201768b42SPeter Zijlstra 79301768b42SPeter Zijlstra void __sched 79401768b42SPeter Zijlstra _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) 79501768b42SPeter Zijlstra { 796427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_); 79701768b42SPeter Zijlstra } 79801768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock); 79901768b42SPeter Zijlstra 80001768b42SPeter Zijlstra int __sched 80101768b42SPeter Zijlstra mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) 80201768b42SPeter Zijlstra { 803427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_); 80401768b42SPeter Zijlstra } 80501768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_killable_nested); 80601768b42SPeter Zijlstra 80701768b42SPeter Zijlstra int __sched 80801768b42SPeter Zijlstra mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) 80901768b42SPeter Zijlstra { 810427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_); 81101768b42SPeter Zijlstra } 81201768b42SPeter Zijlstra EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested); 81301768b42SPeter Zijlstra 8141460cb65STejun Heo void __sched 8151460cb65STejun Heo mutex_lock_io_nested(struct mutex *lock, unsigned int subclass) 8161460cb65STejun Heo { 8171460cb65STejun Heo int token; 8181460cb65STejun Heo 8191460cb65STejun Heo might_sleep(); 8201460cb65STejun Heo 8211460cb65STejun Heo token = io_schedule_prepare(); 8221460cb65STejun Heo __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 8231460cb65STejun Heo subclass, NULL, _RET_IP_, NULL, 0); 8241460cb65STejun Heo io_schedule_finish(token); 8251460cb65STejun Heo } 8261460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io_nested); 8271460cb65STejun Heo 82801768b42SPeter Zijlstra static inline int 82901768b42SPeter Zijlstra ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 83001768b42SPeter Zijlstra { 83101768b42SPeter Zijlstra #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH 83201768b42SPeter Zijlstra unsigned tmp; 83301768b42SPeter Zijlstra 83401768b42SPeter Zijlstra if (ctx->deadlock_inject_countdown-- == 0) { 83501768b42SPeter Zijlstra tmp = ctx->deadlock_inject_interval; 83601768b42SPeter Zijlstra if (tmp > UINT_MAX/4) 83701768b42SPeter Zijlstra tmp = UINT_MAX; 83801768b42SPeter Zijlstra else 83901768b42SPeter Zijlstra tmp = tmp*2 + tmp + tmp/2; 84001768b42SPeter Zijlstra 84101768b42SPeter Zijlstra ctx->deadlock_inject_interval = tmp; 84201768b42SPeter Zijlstra ctx->deadlock_inject_countdown = tmp; 84301768b42SPeter Zijlstra ctx->contending_lock = lock; 84401768b42SPeter Zijlstra 84501768b42SPeter Zijlstra ww_mutex_unlock(lock); 84601768b42SPeter Zijlstra 84701768b42SPeter Zijlstra return -EDEADLK; 84801768b42SPeter Zijlstra } 84901768b42SPeter Zijlstra #endif 85001768b42SPeter Zijlstra 85101768b42SPeter Zijlstra return 0; 85201768b42SPeter Zijlstra } 85301768b42SPeter Zijlstra 85401768b42SPeter Zijlstra int __sched 855c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 85601768b42SPeter Zijlstra { 85701768b42SPeter Zijlstra int ret; 85801768b42SPeter Zijlstra 85901768b42SPeter Zijlstra might_sleep(); 860427b1820SPeter Zijlstra ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 861cf702eddSPeter Zijlstra 0, _RET_IP_, ctx); 862ea9e0fb8SNicolai Hähnle if (!ret && ctx && ctx->acquired > 1) 86301768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 86401768b42SPeter Zijlstra 86501768b42SPeter Zijlstra return ret; 86601768b42SPeter Zijlstra } 867c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock); 86801768b42SPeter Zijlstra 86901768b42SPeter Zijlstra int __sched 870c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 87101768b42SPeter Zijlstra { 87201768b42SPeter Zijlstra int ret; 87301768b42SPeter Zijlstra 87401768b42SPeter Zijlstra might_sleep(); 875427b1820SPeter Zijlstra ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 876cf702eddSPeter Zijlstra 0, _RET_IP_, ctx); 87701768b42SPeter Zijlstra 878ea9e0fb8SNicolai Hähnle if (!ret && ctx && ctx->acquired > 1) 87901768b42SPeter Zijlstra return ww_mutex_deadlock_injection(lock, ctx); 88001768b42SPeter Zijlstra 88101768b42SPeter Zijlstra return ret; 88201768b42SPeter Zijlstra } 883c5470b22SNicolai Hähnle EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible); 88401768b42SPeter Zijlstra 88501768b42SPeter Zijlstra #endif 88601768b42SPeter Zijlstra 88701768b42SPeter Zijlstra /* 88801768b42SPeter Zijlstra * Release the lock, slowpath: 88901768b42SPeter Zijlstra */ 8903ca0ff57SPeter Zijlstra static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) 89101768b42SPeter Zijlstra { 8929d659ae1SPeter Zijlstra struct task_struct *next = NULL; 893194a6b5bSWaiman Long DEFINE_WAKE_Q(wake_q); 894b9c16a0eSPeter Zijlstra unsigned long owner; 89501768b42SPeter Zijlstra 8965facae4fSQian Cai mutex_release(&lock->dep_map, ip); 8973ca0ff57SPeter Zijlstra 89801768b42SPeter Zijlstra /* 8999d659ae1SPeter Zijlstra * Release the lock before (potentially) taking the spinlock such that 9009d659ae1SPeter Zijlstra * other contenders can get on with things ASAP. 9019d659ae1SPeter Zijlstra * 9029d659ae1SPeter Zijlstra * Except when HANDOFF, in that case we must not clear the owner field, 9039d659ae1SPeter Zijlstra * but instead set it to the top waiter. 90401768b42SPeter Zijlstra */ 9059d659ae1SPeter Zijlstra owner = atomic_long_read(&lock->owner); 9069d659ae1SPeter Zijlstra for (;;) { 907e6b4457bSPeter Zijlstra MUTEX_WARN_ON(__owner_task(owner) != current); 908e6b4457bSPeter Zijlstra MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP); 9099d659ae1SPeter Zijlstra 9109d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 9119d659ae1SPeter Zijlstra break; 9129d659ae1SPeter Zijlstra 913ab4e4d9fSPeter Zijlstra if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) { 9149d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_WAITERS) 9159d659ae1SPeter Zijlstra break; 9169d659ae1SPeter Zijlstra 9173ca0ff57SPeter Zijlstra return; 9189d659ae1SPeter Zijlstra } 9199d659ae1SPeter Zijlstra } 92001768b42SPeter Zijlstra 921ebf4c55cSThomas Gleixner raw_spin_lock(&lock->wait_lock); 9221d8fe7dcSJason Low debug_mutex_unlock(lock); 92301768b42SPeter Zijlstra if (!list_empty(&lock->wait_list)) { 92401768b42SPeter Zijlstra /* get the first entry from the wait-list: */ 92501768b42SPeter Zijlstra struct mutex_waiter *waiter = 9269d659ae1SPeter Zijlstra list_first_entry(&lock->wait_list, 92701768b42SPeter Zijlstra struct mutex_waiter, list); 92801768b42SPeter Zijlstra 9299d659ae1SPeter Zijlstra next = waiter->task; 9309d659ae1SPeter Zijlstra 93101768b42SPeter Zijlstra debug_mutex_wake_waiter(lock, waiter); 9329d659ae1SPeter Zijlstra wake_q_add(&wake_q, next); 93301768b42SPeter Zijlstra } 93401768b42SPeter Zijlstra 9359d659ae1SPeter Zijlstra if (owner & MUTEX_FLAG_HANDOFF) 9369d659ae1SPeter Zijlstra __mutex_handoff(lock, next); 9379d659ae1SPeter Zijlstra 938ebf4c55cSThomas Gleixner raw_spin_unlock(&lock->wait_lock); 9399d659ae1SPeter Zijlstra 9401329ce6fSDavidlohr Bueso wake_up_q(&wake_q); 94101768b42SPeter Zijlstra } 94201768b42SPeter Zijlstra 94301768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 94401768b42SPeter Zijlstra /* 94501768b42SPeter Zijlstra * Here come the less common (and hence less performance-critical) APIs: 94601768b42SPeter Zijlstra * mutex_lock_interruptible() and mutex_trylock(). 94701768b42SPeter Zijlstra */ 94801768b42SPeter Zijlstra static noinline int __sched 94901768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock); 95001768b42SPeter Zijlstra 95101768b42SPeter Zijlstra static noinline int __sched 95201768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock); 95301768b42SPeter Zijlstra 95401768b42SPeter Zijlstra /** 95545dbac0eSMatthew Wilcox * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals. 95645dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 95701768b42SPeter Zijlstra * 95845dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). If a signal is delivered while the 95945dbac0eSMatthew Wilcox * process is sleeping, this function will return without acquiring the 96045dbac0eSMatthew Wilcox * mutex. 96101768b42SPeter Zijlstra * 96245dbac0eSMatthew Wilcox * Context: Process context. 96345dbac0eSMatthew Wilcox * Return: 0 if the lock was successfully acquired or %-EINTR if a 96445dbac0eSMatthew Wilcox * signal arrived. 96501768b42SPeter Zijlstra */ 96601768b42SPeter Zijlstra int __sched mutex_lock_interruptible(struct mutex *lock) 96701768b42SPeter Zijlstra { 96801768b42SPeter Zijlstra might_sleep(); 9693ca0ff57SPeter Zijlstra 9703ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 97101768b42SPeter Zijlstra return 0; 9723ca0ff57SPeter Zijlstra 97301768b42SPeter Zijlstra return __mutex_lock_interruptible_slowpath(lock); 97401768b42SPeter Zijlstra } 97501768b42SPeter Zijlstra 97601768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_interruptible); 97701768b42SPeter Zijlstra 97845dbac0eSMatthew Wilcox /** 97945dbac0eSMatthew Wilcox * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals. 98045dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 98145dbac0eSMatthew Wilcox * 98245dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). If a signal which will be fatal to 98345dbac0eSMatthew Wilcox * the current process is delivered while the process is sleeping, this 98445dbac0eSMatthew Wilcox * function will return without acquiring the mutex. 98545dbac0eSMatthew Wilcox * 98645dbac0eSMatthew Wilcox * Context: Process context. 98745dbac0eSMatthew Wilcox * Return: 0 if the lock was successfully acquired or %-EINTR if a 98845dbac0eSMatthew Wilcox * fatal signal arrived. 98945dbac0eSMatthew Wilcox */ 99001768b42SPeter Zijlstra int __sched mutex_lock_killable(struct mutex *lock) 99101768b42SPeter Zijlstra { 99201768b42SPeter Zijlstra might_sleep(); 9933ca0ff57SPeter Zijlstra 9943ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(lock)) 99501768b42SPeter Zijlstra return 0; 9963ca0ff57SPeter Zijlstra 99701768b42SPeter Zijlstra return __mutex_lock_killable_slowpath(lock); 99801768b42SPeter Zijlstra } 99901768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_lock_killable); 100001768b42SPeter Zijlstra 100145dbac0eSMatthew Wilcox /** 100245dbac0eSMatthew Wilcox * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O 100345dbac0eSMatthew Wilcox * @lock: The mutex to be acquired. 100445dbac0eSMatthew Wilcox * 100545dbac0eSMatthew Wilcox * Lock the mutex like mutex_lock(). While the task is waiting for this 100645dbac0eSMatthew Wilcox * mutex, it will be accounted as being in the IO wait state by the 100745dbac0eSMatthew Wilcox * scheduler. 100845dbac0eSMatthew Wilcox * 100945dbac0eSMatthew Wilcox * Context: Process context. 101045dbac0eSMatthew Wilcox */ 10111460cb65STejun Heo void __sched mutex_lock_io(struct mutex *lock) 10121460cb65STejun Heo { 10131460cb65STejun Heo int token; 10141460cb65STejun Heo 10151460cb65STejun Heo token = io_schedule_prepare(); 10161460cb65STejun Heo mutex_lock(lock); 10171460cb65STejun Heo io_schedule_finish(token); 10181460cb65STejun Heo } 10191460cb65STejun Heo EXPORT_SYMBOL_GPL(mutex_lock_io); 10201460cb65STejun Heo 10213ca0ff57SPeter Zijlstra static noinline void __sched 10223ca0ff57SPeter Zijlstra __mutex_lock_slowpath(struct mutex *lock) 102301768b42SPeter Zijlstra { 1024427b1820SPeter Zijlstra __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_); 102501768b42SPeter Zijlstra } 102601768b42SPeter Zijlstra 102701768b42SPeter Zijlstra static noinline int __sched 102801768b42SPeter Zijlstra __mutex_lock_killable_slowpath(struct mutex *lock) 102901768b42SPeter Zijlstra { 1030427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_); 103101768b42SPeter Zijlstra } 103201768b42SPeter Zijlstra 103301768b42SPeter Zijlstra static noinline int __sched 103401768b42SPeter Zijlstra __mutex_lock_interruptible_slowpath(struct mutex *lock) 103501768b42SPeter Zijlstra { 1036427b1820SPeter Zijlstra return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_); 103701768b42SPeter Zijlstra } 103801768b42SPeter Zijlstra 103901768b42SPeter Zijlstra static noinline int __sched 104001768b42SPeter Zijlstra __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 104101768b42SPeter Zijlstra { 1042cf702eddSPeter Zijlstra return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, 1043427b1820SPeter Zijlstra _RET_IP_, ctx); 104401768b42SPeter Zijlstra } 104501768b42SPeter Zijlstra 104601768b42SPeter Zijlstra static noinline int __sched 104701768b42SPeter Zijlstra __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, 104801768b42SPeter Zijlstra struct ww_acquire_ctx *ctx) 104901768b42SPeter Zijlstra { 1050cf702eddSPeter Zijlstra return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, 1051427b1820SPeter Zijlstra _RET_IP_, ctx); 105201768b42SPeter Zijlstra } 105301768b42SPeter Zijlstra 105401768b42SPeter Zijlstra #endif 105501768b42SPeter Zijlstra 105601768b42SPeter Zijlstra /** 105701768b42SPeter Zijlstra * mutex_trylock - try to acquire the mutex, without waiting 105801768b42SPeter Zijlstra * @lock: the mutex to be acquired 105901768b42SPeter Zijlstra * 106001768b42SPeter Zijlstra * Try to acquire the mutex atomically. Returns 1 if the mutex 106101768b42SPeter Zijlstra * has been acquired successfully, and 0 on contention. 106201768b42SPeter Zijlstra * 106301768b42SPeter Zijlstra * NOTE: this function follows the spin_trylock() convention, so 106401768b42SPeter Zijlstra * it is negated from the down_trylock() return values! Be careful 106501768b42SPeter Zijlstra * about this when converting semaphore users to mutexes. 106601768b42SPeter Zijlstra * 106701768b42SPeter Zijlstra * This function must not be used in interrupt context. The 106801768b42SPeter Zijlstra * mutex must be released by the same task that acquired it. 106901768b42SPeter Zijlstra */ 107001768b42SPeter Zijlstra int __sched mutex_trylock(struct mutex *lock) 107101768b42SPeter Zijlstra { 10726c11c6e3SSebastian Andrzej Siewior bool locked; 107301768b42SPeter Zijlstra 1074e6b4457bSPeter Zijlstra MUTEX_WARN_ON(lock->magic != lock); 10756c11c6e3SSebastian Andrzej Siewior 10766c11c6e3SSebastian Andrzej Siewior locked = __mutex_trylock(lock); 10773ca0ff57SPeter Zijlstra if (locked) 10783ca0ff57SPeter Zijlstra mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); 107901768b42SPeter Zijlstra 10803ca0ff57SPeter Zijlstra return locked; 108101768b42SPeter Zijlstra } 108201768b42SPeter Zijlstra EXPORT_SYMBOL(mutex_trylock); 108301768b42SPeter Zijlstra 108401768b42SPeter Zijlstra #ifndef CONFIG_DEBUG_LOCK_ALLOC 108501768b42SPeter Zijlstra int __sched 1086c5470b22SNicolai Hähnle ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 108701768b42SPeter Zijlstra { 108801768b42SPeter Zijlstra might_sleep(); 108901768b42SPeter Zijlstra 10903ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 1091ea9e0fb8SNicolai Hähnle if (ctx) 109201768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 10933ca0ff57SPeter Zijlstra return 0; 10943ca0ff57SPeter Zijlstra } 10953ca0ff57SPeter Zijlstra 10963ca0ff57SPeter Zijlstra return __ww_mutex_lock_slowpath(lock, ctx); 109701768b42SPeter Zijlstra } 1098c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock); 109901768b42SPeter Zijlstra 110001768b42SPeter Zijlstra int __sched 1101c5470b22SNicolai Hähnle ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) 110201768b42SPeter Zijlstra { 110301768b42SPeter Zijlstra might_sleep(); 110401768b42SPeter Zijlstra 11053ca0ff57SPeter Zijlstra if (__mutex_trylock_fast(&lock->base)) { 1106ea9e0fb8SNicolai Hähnle if (ctx) 110701768b42SPeter Zijlstra ww_mutex_set_context_fastpath(lock, ctx); 11083ca0ff57SPeter Zijlstra return 0; 11093ca0ff57SPeter Zijlstra } 11103ca0ff57SPeter Zijlstra 11113ca0ff57SPeter Zijlstra return __ww_mutex_lock_interruptible_slowpath(lock, ctx); 111201768b42SPeter Zijlstra } 1113c5470b22SNicolai Hähnle EXPORT_SYMBOL(ww_mutex_lock_interruptible); 111401768b42SPeter Zijlstra 1115bb630f9fSThomas Gleixner #endif /* !CONFIG_DEBUG_LOCK_ALLOC */ 1116bb630f9fSThomas Gleixner #endif /* !CONFIG_PREEMPT_RT */ 111701768b42SPeter Zijlstra 111801768b42SPeter Zijlstra /** 111901768b42SPeter Zijlstra * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 112001768b42SPeter Zijlstra * @cnt: the atomic which we are to dec 112101768b42SPeter Zijlstra * @lock: the mutex to return holding if we dec to 0 112201768b42SPeter Zijlstra * 112301768b42SPeter Zijlstra * return true and hold lock if we dec to 0, return false otherwise 112401768b42SPeter Zijlstra */ 112501768b42SPeter Zijlstra int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) 112601768b42SPeter Zijlstra { 112701768b42SPeter Zijlstra /* dec if we can't possibly hit 0 */ 112801768b42SPeter Zijlstra if (atomic_add_unless(cnt, -1, 1)) 112901768b42SPeter Zijlstra return 0; 113001768b42SPeter Zijlstra /* we might hit 0, so take the lock */ 113101768b42SPeter Zijlstra mutex_lock(lock); 113201768b42SPeter Zijlstra if (!atomic_dec_and_test(cnt)) { 113301768b42SPeter Zijlstra /* when we actually did the dec, we didn't hit 0 */ 113401768b42SPeter Zijlstra mutex_unlock(lock); 113501768b42SPeter Zijlstra return 0; 113601768b42SPeter Zijlstra } 113701768b42SPeter Zijlstra /* we hit 0, and we hold the lock */ 113801768b42SPeter Zijlstra return 1; 113901768b42SPeter Zijlstra } 114001768b42SPeter Zijlstra EXPORT_SYMBOL(atomic_dec_and_mutex_lock); 1141