1 // SPDX-License-Identifier: GPL-2.0-only
3 * kernel/locking/mutex.c
5 * Mutexes: blocking mutual exclusion locks
7 * Started by Ingo Molnar:
11 * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
12 * David Howells for suggestions and improvements.
14 * - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
15 * from the -rt tree, where it was originally implemented for rtmutexes
16 * by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
19 * Also see Documentation/locking/mutex-design.rst.
21 #include <linux/mutex.h>
22 #include <linux/ww_mutex.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched/rt.h>
25 #include <linux/sched/wake_q.h>
26 #include <linux/sched/debug.h>
27 #include <linux/export.h>
28 #include <linux/spinlock.h>
29 #include <linux/interrupt.h>
30 #include <linux/debug_locks.h>
31 #include <linux/osq_lock.h>
33 #ifndef CONFIG_PREEMPT_RT
36 #ifdef CONFIG_DEBUG_MUTEXES
37 # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond)
39 # define MUTEX_WARN_ON(cond)
43 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
45 atomic_long_set(&lock->owner, 0);
46 raw_spin_lock_init(&lock->wait_lock);
47 INIT_LIST_HEAD(&lock->wait_list);
48 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
49 osq_lock_init(&lock->osq);
52 debug_mutex_init(lock, name, key);
54 EXPORT_SYMBOL(__mutex_init);
57 * @owner: contains: 'struct task_struct *' to the current lock owner,
58 * NULL means not owned. Since task_struct pointers are aligned at
59 * at least L1_CACHE_BYTES, we have low bits to store extra state.
61 * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
62 * Bit1 indicates unlock needs to hand the lock to the top-waiter
63 * Bit2 indicates handoff has been done and we're waiting for pickup.
65 #define MUTEX_FLAG_WAITERS 0x01
66 #define MUTEX_FLAG_HANDOFF 0x02
67 #define MUTEX_FLAG_PICKUP 0x04
69 #define MUTEX_FLAGS 0x07
72 * Internal helper function; C doesn't allow us to hide it :/
74 * DO NOT USE (outside of mutex code).
76 static inline struct task_struct *__mutex_owner(struct mutex *lock)
78 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
81 static inline struct task_struct *__owner_task(unsigned long owner)
83 return (struct task_struct *)(owner & ~MUTEX_FLAGS);
86 bool mutex_is_locked(struct mutex *lock)
88 return __mutex_owner(lock) != NULL;
90 EXPORT_SYMBOL(mutex_is_locked);
92 static inline unsigned long __owner_flags(unsigned long owner)
94 return owner & MUTEX_FLAGS;
97 static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff)
99 unsigned long owner, curr = (unsigned long)current;
101 owner = atomic_long_read(&lock->owner);
102 for (;;) { /* must loop, can race against a flag */
103 unsigned long flags = __owner_flags(owner);
104 unsigned long task = owner & ~MUTEX_FLAGS;
107 if (flags & MUTEX_FLAG_PICKUP) {
110 flags &= ~MUTEX_FLAG_PICKUP;
111 } else if (handoff) {
112 if (flags & MUTEX_FLAG_HANDOFF)
114 flags |= MUTEX_FLAG_HANDOFF;
119 MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
123 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
130 return __owner_task(owner);
134 * Trylock or set HANDOFF
136 static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
138 return !__mutex_trylock_common(lock, handoff);
142 * Actual trylock that will work on any unlocked state.
144 static inline bool __mutex_trylock(struct mutex *lock)
146 return !__mutex_trylock_common(lock, false);
149 #ifndef CONFIG_DEBUG_LOCK_ALLOC
151 * Lockdep annotations are contained to the slow paths for simplicity.
152 * There is nothing that would stop spreading the lockdep annotations outwards
157 * Optimistic trylock that only works in the uncontended case. Make sure to
158 * follow with a __mutex_trylock() before failing.
160 static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
162 unsigned long curr = (unsigned long)current;
163 unsigned long zero = 0UL;
165 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
171 static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
173 unsigned long curr = (unsigned long)current;
175 return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
179 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
181 atomic_long_or(flag, &lock->owner);
184 static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
186 atomic_long_andnot(flag, &lock->owner);
189 static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
191 return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
195 * Add @waiter to a given location in the lock wait_list and set the
196 * FLAG_WAITERS flag if it's the first waiter.
199 __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
200 struct list_head *list)
202 debug_mutex_add_waiter(lock, waiter, current);
204 list_add_tail(&waiter->list, list);
205 if (__mutex_waiter_is_first(lock, waiter))
206 __mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
210 __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)
212 list_del(&waiter->list);
213 if (likely(list_empty(&lock->wait_list)))
214 __mutex_clear_flag(lock, MUTEX_FLAGS);
216 debug_mutex_remove_waiter(lock, waiter, current);
220 * Give up ownership to a specific task, when @task = NULL, this is equivalent
221 * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
222 * WAITERS. Provides RELEASE semantics like a regular unlock, the
223 * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
225 static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
227 unsigned long owner = atomic_long_read(&lock->owner);
232 MUTEX_WARN_ON(__owner_task(owner) != current);
233 MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
235 new = (owner & MUTEX_FLAG_WAITERS);
236 new |= (unsigned long)task;
238 new |= MUTEX_FLAG_PICKUP;
240 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new))
245 #ifndef CONFIG_DEBUG_LOCK_ALLOC
247 * We split the mutex lock/unlock logic into separate fastpath and
248 * slowpath functions, to reduce the register pressure on the fastpath.
249 * We also put the fastpath first in the kernel image, to make sure the
250 * branch is predicted by the CPU as default-untaken.
252 static void __sched __mutex_lock_slowpath(struct mutex *lock);
255 * mutex_lock - acquire the mutex
256 * @lock: the mutex to be acquired
258 * Lock the mutex exclusively for this task. If the mutex is not
259 * available right now, it will sleep until it can get it.
261 * The mutex must later on be released by the same task that
262 * acquired it. Recursive locking is not allowed. The task
263 * may not exit without first unlocking the mutex. Also, kernel
264 * memory where the mutex resides must not be freed with
265 * the mutex still locked. The mutex must first be initialized
266 * (or statically defined) before it can be locked. memset()-ing
267 * the mutex to 0 is not allowed.
269 * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
270 * checks that will enforce the restrictions and will also do
271 * deadlock debugging)
273 * This function is similar to (but not equivalent to) down().
275 void __sched mutex_lock(struct mutex *lock)
279 if (!__mutex_trylock_fast(lock))
280 __mutex_lock_slowpath(lock);
282 EXPORT_SYMBOL(mutex_lock);
285 #include "ww_mutex.h"
287 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
290 * Trylock variant that returns the owning task on failure.
292 static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
294 return __mutex_trylock_common(lock, false);
298 bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
299 struct mutex_waiter *waiter)
303 ww = container_of(lock, struct ww_mutex, base);
306 * If ww->ctx is set the contents are undefined, only
307 * by acquiring wait_lock there is a guarantee that
308 * they are not invalid when reading.
310 * As such, when deadlock detection needs to be
311 * performed the optimistic spinning cannot be done.
313 * Check this in every inner iteration because we may
314 * be racing against another thread's ww_mutex_lock.
316 if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
320 * If we aren't on the wait list yet, cancel the spin
321 * if there are waiters. We want to avoid stealing the
322 * lock from a waiter with an earlier stamp, since the
323 * other thread may already own a lock that we also
326 if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
330 * Similarly, stop spinning if we are no longer the
333 if (waiter && !__mutex_waiter_is_first(lock, waiter))
340 * Look out! "owner" is an entirely speculative pointer access and not
343 * "noinline" so that this function shows up on perf profiles.
346 bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
347 struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
352 while (__mutex_owner(lock) == owner) {
354 * Ensure we emit the owner->on_cpu, dereference _after_
355 * checking lock->owner still matches owner. If that fails,
356 * owner might point to freed memory. If it still matches,
357 * the rcu_read_lock() ensures the memory stays valid.
362 * Use vcpu_is_preempted to detect lock holder preemption issue.
364 if (!owner->on_cpu || need_resched() ||
365 vcpu_is_preempted(task_cpu(owner))) {
370 if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
383 * Initial check for entering the mutex spinning loop
385 static inline int mutex_can_spin_on_owner(struct mutex *lock)
387 struct task_struct *owner;
394 owner = __mutex_owner(lock);
397 * As lock holder preemption issue, we both skip spinning if task is not
398 * on cpu or its cpu is preempted
401 retval = owner->on_cpu && !vcpu_is_preempted(task_cpu(owner));
405 * If lock->owner is not set, the mutex has been released. Return true
406 * such that we'll trylock in the spin path, which is a faster option
407 * than the blocking slow path.
413 * Optimistic spinning.
415 * We try to spin for acquisition when we find that the lock owner
416 * is currently running on a (different) CPU and while we don't
417 * need to reschedule. The rationale is that if the lock owner is
418 * running, it is likely to release the lock soon.
420 * The mutex spinners are queued up using MCS lock so that only one
421 * spinner can compete for the mutex. However, if mutex spinning isn't
422 * going to happen, there is no point in going through the lock/unlock
425 * Returns true when the lock was taken, otherwise false, indicating
426 * that we need to jump to the slowpath and sleep.
428 * The waiter flag is set to true if the spinner is a waiter in the wait
429 * queue. The waiter-spinner will spin on the lock directly and concurrently
430 * with the spinner at the head of the OSQ, if present, until the owner is
433 static __always_inline bool
434 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
435 struct mutex_waiter *waiter)
439 * The purpose of the mutex_can_spin_on_owner() function is
440 * to eliminate the overhead of osq_lock() and osq_unlock()
441 * in case spinning isn't possible. As a waiter-spinner
442 * is not going to take OSQ lock anyway, there is no need
443 * to call mutex_can_spin_on_owner().
445 if (!mutex_can_spin_on_owner(lock))
449 * In order to avoid a stampede of mutex spinners trying to
450 * acquire the mutex all at once, the spinners need to take a
451 * MCS (queued) lock first before spinning on the owner field.
453 if (!osq_lock(&lock->osq))
458 struct task_struct *owner;
460 /* Try to acquire the mutex... */
461 owner = __mutex_trylock_or_owner(lock);
466 * There's an owner, wait for it to either
467 * release the lock or go to sleep.
469 if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
473 * The cpu_relax() call is a compiler barrier which forces
474 * everything in this loop to be re-loaded. We don't need
475 * memory barriers as we'll eventually observe the right
476 * values at the cost of a few extra spins.
482 osq_unlock(&lock->osq);
489 osq_unlock(&lock->osq);
493 * If we fell out of the spin path because of need_resched(),
494 * reschedule now, before we try-lock the mutex. This avoids getting
495 * scheduled out right after we obtained the mutex.
497 if (need_resched()) {
499 * We _should_ have TASK_RUNNING here, but just in case
500 * we do not, make it so, otherwise we might get stuck.
502 __set_current_state(TASK_RUNNING);
503 schedule_preempt_disabled();
509 static __always_inline bool
510 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
511 struct mutex_waiter *waiter)
517 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
520 * mutex_unlock - release the mutex
521 * @lock: the mutex to be released
523 * Unlock a mutex that has been locked by this task previously.
525 * This function must not be used in interrupt context. Unlocking
526 * of a not locked mutex is not allowed.
528 * This function is similar to (but not equivalent to) up().
530 void __sched mutex_unlock(struct mutex *lock)
532 #ifndef CONFIG_DEBUG_LOCK_ALLOC
533 if (__mutex_unlock_fast(lock))
536 __mutex_unlock_slowpath(lock, _RET_IP_);
538 EXPORT_SYMBOL(mutex_unlock);
541 * ww_mutex_unlock - release the w/w mutex
542 * @lock: the mutex to be released
544 * Unlock a mutex that has been locked by this task previously with any of the
545 * ww_mutex_lock* functions (with or without an acquire context). It is
546 * forbidden to release the locks after releasing the acquire context.
548 * This function must not be used in interrupt context. Unlocking
549 * of a unlocked mutex is not allowed.
551 void __sched ww_mutex_unlock(struct ww_mutex *lock)
553 __ww_mutex_unlock(lock);
554 mutex_unlock(&lock->base);
556 EXPORT_SYMBOL(ww_mutex_unlock);
559 * Lock a mutex (possibly interruptible), slowpath:
561 static __always_inline int __sched
562 __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
563 struct lockdep_map *nest_lock, unsigned long ip,
564 struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
566 struct mutex_waiter waiter;
575 MUTEX_WARN_ON(lock->magic != lock);
577 ww = container_of(lock, struct ww_mutex, base);
579 if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
583 * Reset the wounded flag after a kill. No other process can
584 * race and wound us here since they can't have a valid owner
585 * pointer if we don't have any locks held.
587 if (ww_ctx->acquired == 0)
590 #ifdef CONFIG_DEBUG_LOCK_ALLOC
591 nest_lock = &ww_ctx->dep_map;
596 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
598 if (__mutex_trylock(lock) ||
599 mutex_optimistic_spin(lock, ww_ctx, NULL)) {
600 /* got the lock, yay! */
601 lock_acquired(&lock->dep_map, ip);
603 ww_mutex_set_context_fastpath(ww, ww_ctx);
608 raw_spin_lock(&lock->wait_lock);
610 * After waiting to acquire the wait_lock, try again.
612 if (__mutex_trylock(lock)) {
614 __ww_mutex_check_waiters(lock, ww_ctx);
619 debug_mutex_lock_common(lock, &waiter);
620 waiter.task = current;
622 waiter.ww_ctx = ww_ctx;
624 lock_contended(&lock->dep_map, ip);
627 /* add waiting tasks to the end of the waitqueue (FIFO): */
628 __mutex_add_waiter(lock, &waiter, &lock->wait_list);
631 * Add in stamp order, waking up waiters that must kill
634 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
639 set_current_state(state);
644 * Once we hold wait_lock, we're serialized against
645 * mutex_unlock() handing the lock off to us, do a trylock
646 * before testing the error conditions to make sure we pick up
649 if (__mutex_trylock(lock))
653 * Check for signals and kill conditions while holding
654 * wait_lock. This ensures the lock cancellation is ordered
655 * against mutex_unlock() and wake-ups do not go missing.
657 if (signal_pending_state(state, current)) {
663 ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
668 raw_spin_unlock(&lock->wait_lock);
669 schedule_preempt_disabled();
671 first = __mutex_waiter_is_first(lock, &waiter);
673 set_current_state(state);
675 * Here we order against unlock; we must either see it change
676 * state back to RUNNING and fall through the next schedule(),
677 * or we must see its unlock and acquire.
679 if (__mutex_trylock_or_handoff(lock, first) ||
680 (first && mutex_optimistic_spin(lock, ww_ctx, &waiter)))
683 raw_spin_lock(&lock->wait_lock);
685 raw_spin_lock(&lock->wait_lock);
687 __set_current_state(TASK_RUNNING);
691 * Wound-Wait; we stole the lock (!first_waiter), check the
692 * waiters as anyone might want to wound us.
694 if (!ww_ctx->is_wait_die &&
695 !__mutex_waiter_is_first(lock, &waiter))
696 __ww_mutex_check_waiters(lock, ww_ctx);
699 __mutex_remove_waiter(lock, &waiter);
701 debug_mutex_free_waiter(&waiter);
704 /* got the lock - cleanup and rejoice! */
705 lock_acquired(&lock->dep_map, ip);
708 ww_mutex_lock_acquired(ww, ww_ctx);
710 raw_spin_unlock(&lock->wait_lock);
715 __set_current_state(TASK_RUNNING);
716 __mutex_remove_waiter(lock, &waiter);
718 raw_spin_unlock(&lock->wait_lock);
719 debug_mutex_free_waiter(&waiter);
720 mutex_release(&lock->dep_map, ip);
726 __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
727 struct lockdep_map *nest_lock, unsigned long ip)
729 return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
733 __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
734 unsigned long ip, struct ww_acquire_ctx *ww_ctx)
736 return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
739 #ifdef CONFIG_DEBUG_LOCK_ALLOC
741 mutex_lock_nested(struct mutex *lock, unsigned int subclass)
743 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
746 EXPORT_SYMBOL_GPL(mutex_lock_nested);
749 _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
751 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
753 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
756 mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
758 return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
760 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
763 mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
765 return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
767 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
770 mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
776 token = io_schedule_prepare();
777 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
778 subclass, NULL, _RET_IP_, NULL, 0);
779 io_schedule_finish(token);
781 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
784 ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
786 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
789 if (ctx->deadlock_inject_countdown-- == 0) {
790 tmp = ctx->deadlock_inject_interval;
791 if (tmp > UINT_MAX/4)
794 tmp = tmp*2 + tmp + tmp/2;
796 ctx->deadlock_inject_interval = tmp;
797 ctx->deadlock_inject_countdown = tmp;
798 ctx->contending_lock = lock;
800 ww_mutex_unlock(lock);
810 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
815 ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
817 if (!ret && ctx && ctx->acquired > 1)
818 return ww_mutex_deadlock_injection(lock, ctx);
822 EXPORT_SYMBOL_GPL(ww_mutex_lock);
825 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
830 ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
833 if (!ret && ctx && ctx->acquired > 1)
834 return ww_mutex_deadlock_injection(lock, ctx);
838 EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
843 * Release the lock, slowpath:
845 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
847 struct task_struct *next = NULL;
848 DEFINE_WAKE_Q(wake_q);
851 mutex_release(&lock->dep_map, ip);
854 * Release the lock before (potentially) taking the spinlock such that
855 * other contenders can get on with things ASAP.
857 * Except when HANDOFF, in that case we must not clear the owner field,
858 * but instead set it to the top waiter.
860 owner = atomic_long_read(&lock->owner);
862 MUTEX_WARN_ON(__owner_task(owner) != current);
863 MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
865 if (owner & MUTEX_FLAG_HANDOFF)
868 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
869 if (owner & MUTEX_FLAG_WAITERS)
876 raw_spin_lock(&lock->wait_lock);
877 debug_mutex_unlock(lock);
878 if (!list_empty(&lock->wait_list)) {
879 /* get the first entry from the wait-list: */
880 struct mutex_waiter *waiter =
881 list_first_entry(&lock->wait_list,
882 struct mutex_waiter, list);
886 debug_mutex_wake_waiter(lock, waiter);
887 wake_q_add(&wake_q, next);
890 if (owner & MUTEX_FLAG_HANDOFF)
891 __mutex_handoff(lock, next);
893 raw_spin_unlock(&lock->wait_lock);
898 #ifndef CONFIG_DEBUG_LOCK_ALLOC
900 * Here come the less common (and hence less performance-critical) APIs:
901 * mutex_lock_interruptible() and mutex_trylock().
903 static noinline int __sched
904 __mutex_lock_killable_slowpath(struct mutex *lock);
906 static noinline int __sched
907 __mutex_lock_interruptible_slowpath(struct mutex *lock);
910 * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
911 * @lock: The mutex to be acquired.
913 * Lock the mutex like mutex_lock(). If a signal is delivered while the
914 * process is sleeping, this function will return without acquiring the
917 * Context: Process context.
918 * Return: 0 if the lock was successfully acquired or %-EINTR if a
921 int __sched mutex_lock_interruptible(struct mutex *lock)
925 if (__mutex_trylock_fast(lock))
928 return __mutex_lock_interruptible_slowpath(lock);
931 EXPORT_SYMBOL(mutex_lock_interruptible);
934 * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
935 * @lock: The mutex to be acquired.
937 * Lock the mutex like mutex_lock(). If a signal which will be fatal to
938 * the current process is delivered while the process is sleeping, this
939 * function will return without acquiring the mutex.
941 * Context: Process context.
942 * Return: 0 if the lock was successfully acquired or %-EINTR if a
943 * fatal signal arrived.
945 int __sched mutex_lock_killable(struct mutex *lock)
949 if (__mutex_trylock_fast(lock))
952 return __mutex_lock_killable_slowpath(lock);
954 EXPORT_SYMBOL(mutex_lock_killable);
957 * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
958 * @lock: The mutex to be acquired.
960 * Lock the mutex like mutex_lock(). While the task is waiting for this
961 * mutex, it will be accounted as being in the IO wait state by the
964 * Context: Process context.
966 void __sched mutex_lock_io(struct mutex *lock)
970 token = io_schedule_prepare();
972 io_schedule_finish(token);
974 EXPORT_SYMBOL_GPL(mutex_lock_io);
976 static noinline void __sched
977 __mutex_lock_slowpath(struct mutex *lock)
979 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
982 static noinline int __sched
983 __mutex_lock_killable_slowpath(struct mutex *lock)
985 return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
988 static noinline int __sched
989 __mutex_lock_interruptible_slowpath(struct mutex *lock)
991 return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
994 static noinline int __sched
995 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
997 return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
1001 static noinline int __sched
1002 __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
1003 struct ww_acquire_ctx *ctx)
1005 return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
1012 * mutex_trylock - try to acquire the mutex, without waiting
1013 * @lock: the mutex to be acquired
1015 * Try to acquire the mutex atomically. Returns 1 if the mutex
1016 * has been acquired successfully, and 0 on contention.
1018 * NOTE: this function follows the spin_trylock() convention, so
1019 * it is negated from the down_trylock() return values! Be careful
1020 * about this when converting semaphore users to mutexes.
1022 * This function must not be used in interrupt context. The
1023 * mutex must be released by the same task that acquired it.
1025 int __sched mutex_trylock(struct mutex *lock)
1029 MUTEX_WARN_ON(lock->magic != lock);
1031 locked = __mutex_trylock(lock);
1033 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1037 EXPORT_SYMBOL(mutex_trylock);
1039 #ifndef CONFIG_DEBUG_LOCK_ALLOC
1041 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1045 if (__mutex_trylock_fast(&lock->base)) {
1047 ww_mutex_set_context_fastpath(lock, ctx);
1051 return __ww_mutex_lock_slowpath(lock, ctx);
1053 EXPORT_SYMBOL(ww_mutex_lock);
1056 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1060 if (__mutex_trylock_fast(&lock->base)) {
1062 ww_mutex_set_context_fastpath(lock, ctx);
1066 return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
1068 EXPORT_SYMBOL(ww_mutex_lock_interruptible);
1070 #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1071 #endif /* !CONFIG_PREEMPT_RT */
1074 * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
1075 * @cnt: the atomic which we are to dec
1076 * @lock: the mutex to return holding if we dec to 0
1078 * return true and hold lock if we dec to 0, return false otherwise
1080 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
1082 /* dec if we can't possibly hit 0 */
1083 if (atomic_add_unless(cnt, -1, 1))
1085 /* we might hit 0, so take the lock */
1087 if (!atomic_dec_and_test(cnt)) {
1088 /* when we actually did the dec, we didn't hit 0 */
1092 /* we hit 0, and we hold the lock */
1095 EXPORT_SYMBOL(atomic_dec_and_mutex_lock);