Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * kernel/locking/mutex.c
0004  *
0005  * Mutexes: blocking mutual exclusion locks
0006  *
0007  * Started by Ingo Molnar:
0008  *
0009  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
0010  *
0011  * Many thanks to Arjan van de Ven, Thomas Gleixner, Steven Rostedt and
0012  * David Howells for suggestions and improvements.
0013  *
0014  *  - Adaptive spinning for mutexes by Peter Zijlstra. (Ported to mainline
0015  *    from the -rt tree, where it was originally implemented for rtmutexes
0016  *    by Steven Rostedt, based on work by Gregory Haskins, Peter Morreale
0017  *    and Sven Dietrich.
0018  *
0019  * Also see Documentation/locking/mutex-design.rst.
0020  */
0021 #include <linux/mutex.h>
0022 #include <linux/ww_mutex.h>
0023 #include <linux/sched/signal.h>
0024 #include <linux/sched/rt.h>
0025 #include <linux/sched/wake_q.h>
0026 #include <linux/sched/debug.h>
0027 #include <linux/export.h>
0028 #include <linux/spinlock.h>
0029 #include <linux/interrupt.h>
0030 #include <linux/debug_locks.h>
0031 #include <linux/osq_lock.h>
0032 
0033 #define CREATE_TRACE_POINTS
0034 #include <trace/events/lock.h>
0035 
0036 #ifndef CONFIG_PREEMPT_RT
0037 #include "mutex.h"
0038 
0039 #ifdef CONFIG_DEBUG_MUTEXES
0040 # define MUTEX_WARN_ON(cond) DEBUG_LOCKS_WARN_ON(cond)
0041 #else
0042 # define MUTEX_WARN_ON(cond)
0043 #endif
0044 
0045 void
0046 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
0047 {
0048     atomic_long_set(&lock->owner, 0);
0049     raw_spin_lock_init(&lock->wait_lock);
0050     INIT_LIST_HEAD(&lock->wait_list);
0051 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
0052     osq_lock_init(&lock->osq);
0053 #endif
0054 
0055     debug_mutex_init(lock, name, key);
0056 }
0057 EXPORT_SYMBOL(__mutex_init);
0058 
0059 /*
0060  * @owner: contains: 'struct task_struct *' to the current lock owner,
0061  * NULL means not owned. Since task_struct pointers are aligned at
0062  * at least L1_CACHE_BYTES, we have low bits to store extra state.
0063  *
0064  * Bit0 indicates a non-empty waiter list; unlock must issue a wakeup.
0065  * Bit1 indicates unlock needs to hand the lock to the top-waiter
0066  * Bit2 indicates handoff has been done and we're waiting for pickup.
0067  */
0068 #define MUTEX_FLAG_WAITERS  0x01
0069 #define MUTEX_FLAG_HANDOFF  0x02
0070 #define MUTEX_FLAG_PICKUP   0x04
0071 
0072 #define MUTEX_FLAGS     0x07
0073 
0074 /*
0075  * Internal helper function; C doesn't allow us to hide it :/
0076  *
0077  * DO NOT USE (outside of mutex code).
0078  */
0079 static inline struct task_struct *__mutex_owner(struct mutex *lock)
0080 {
0081     return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
0082 }
0083 
0084 static inline struct task_struct *__owner_task(unsigned long owner)
0085 {
0086     return (struct task_struct *)(owner & ~MUTEX_FLAGS);
0087 }
0088 
0089 bool mutex_is_locked(struct mutex *lock)
0090 {
0091     return __mutex_owner(lock) != NULL;
0092 }
0093 EXPORT_SYMBOL(mutex_is_locked);
0094 
0095 static inline unsigned long __owner_flags(unsigned long owner)
0096 {
0097     return owner & MUTEX_FLAGS;
0098 }
0099 
0100 /*
0101  * Returns: __mutex_owner(lock) on failure or NULL on success.
0102  */
0103 static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff)
0104 {
0105     unsigned long owner, curr = (unsigned long)current;
0106 
0107     owner = atomic_long_read(&lock->owner);
0108     for (;;) { /* must loop, can race against a flag */
0109         unsigned long flags = __owner_flags(owner);
0110         unsigned long task = owner & ~MUTEX_FLAGS;
0111 
0112         if (task) {
0113             if (flags & MUTEX_FLAG_PICKUP) {
0114                 if (task != curr)
0115                     break;
0116                 flags &= ~MUTEX_FLAG_PICKUP;
0117             } else if (handoff) {
0118                 if (flags & MUTEX_FLAG_HANDOFF)
0119                     break;
0120                 flags |= MUTEX_FLAG_HANDOFF;
0121             } else {
0122                 break;
0123             }
0124         } else {
0125             MUTEX_WARN_ON(flags & (MUTEX_FLAG_HANDOFF | MUTEX_FLAG_PICKUP));
0126             task = curr;
0127         }
0128 
0129         if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) {
0130             if (task == curr)
0131                 return NULL;
0132             break;
0133         }
0134     }
0135 
0136     return __owner_task(owner);
0137 }
0138 
0139 /*
0140  * Trylock or set HANDOFF
0141  */
0142 static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff)
0143 {
0144     return !__mutex_trylock_common(lock, handoff);
0145 }
0146 
0147 /*
0148  * Actual trylock that will work on any unlocked state.
0149  */
0150 static inline bool __mutex_trylock(struct mutex *lock)
0151 {
0152     return !__mutex_trylock_common(lock, false);
0153 }
0154 
0155 #ifndef CONFIG_DEBUG_LOCK_ALLOC
0156 /*
0157  * Lockdep annotations are contained to the slow paths for simplicity.
0158  * There is nothing that would stop spreading the lockdep annotations outwards
0159  * except more code.
0160  */
0161 
0162 /*
0163  * Optimistic trylock that only works in the uncontended case. Make sure to
0164  * follow with a __mutex_trylock() before failing.
0165  */
0166 static __always_inline bool __mutex_trylock_fast(struct mutex *lock)
0167 {
0168     unsigned long curr = (unsigned long)current;
0169     unsigned long zero = 0UL;
0170 
0171     if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr))
0172         return true;
0173 
0174     return false;
0175 }
0176 
0177 static __always_inline bool __mutex_unlock_fast(struct mutex *lock)
0178 {
0179     unsigned long curr = (unsigned long)current;
0180 
0181     return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL);
0182 }
0183 #endif
0184 
0185 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag)
0186 {
0187     atomic_long_or(flag, &lock->owner);
0188 }
0189 
0190 static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag)
0191 {
0192     atomic_long_andnot(flag, &lock->owner);
0193 }
0194 
0195 static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter)
0196 {
0197     return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter;
0198 }
0199 
0200 /*
0201  * Add @waiter to a given location in the lock wait_list and set the
0202  * FLAG_WAITERS flag if it's the first waiter.
0203  */
0204 static void
0205 __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter,
0206            struct list_head *list)
0207 {
0208     debug_mutex_add_waiter(lock, waiter, current);
0209 
0210     list_add_tail(&waiter->list, list);
0211     if (__mutex_waiter_is_first(lock, waiter))
0212         __mutex_set_flag(lock, MUTEX_FLAG_WAITERS);
0213 }
0214 
0215 static void
0216 __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter)
0217 {
0218     list_del(&waiter->list);
0219     if (likely(list_empty(&lock->wait_list)))
0220         __mutex_clear_flag(lock, MUTEX_FLAGS);
0221 
0222     debug_mutex_remove_waiter(lock, waiter, current);
0223 }
0224 
0225 /*
0226  * Give up ownership to a specific task, when @task = NULL, this is equivalent
0227  * to a regular unlock. Sets PICKUP on a handoff, clears HANDOFF, preserves
0228  * WAITERS. Provides RELEASE semantics like a regular unlock, the
0229  * __mutex_trylock() provides a matching ACQUIRE semantics for the handoff.
0230  */
0231 static void __mutex_handoff(struct mutex *lock, struct task_struct *task)
0232 {
0233     unsigned long owner = atomic_long_read(&lock->owner);
0234 
0235     for (;;) {
0236         unsigned long new;
0237 
0238         MUTEX_WARN_ON(__owner_task(owner) != current);
0239         MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
0240 
0241         new = (owner & MUTEX_FLAG_WAITERS);
0242         new |= (unsigned long)task;
0243         if (task)
0244             new |= MUTEX_FLAG_PICKUP;
0245 
0246         if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new))
0247             break;
0248     }
0249 }
0250 
0251 #ifndef CONFIG_DEBUG_LOCK_ALLOC
0252 /*
0253  * We split the mutex lock/unlock logic into separate fastpath and
0254  * slowpath functions, to reduce the register pressure on the fastpath.
0255  * We also put the fastpath first in the kernel image, to make sure the
0256  * branch is predicted by the CPU as default-untaken.
0257  */
0258 static void __sched __mutex_lock_slowpath(struct mutex *lock);
0259 
0260 /**
0261  * mutex_lock - acquire the mutex
0262  * @lock: the mutex to be acquired
0263  *
0264  * Lock the mutex exclusively for this task. If the mutex is not
0265  * available right now, it will sleep until it can get it.
0266  *
0267  * The mutex must later on be released by the same task that
0268  * acquired it. Recursive locking is not allowed. The task
0269  * may not exit without first unlocking the mutex. Also, kernel
0270  * memory where the mutex resides must not be freed with
0271  * the mutex still locked. The mutex must first be initialized
0272  * (or statically defined) before it can be locked. memset()-ing
0273  * the mutex to 0 is not allowed.
0274  *
0275  * (The CONFIG_DEBUG_MUTEXES .config option turns on debugging
0276  * checks that will enforce the restrictions and will also do
0277  * deadlock debugging)
0278  *
0279  * This function is similar to (but not equivalent to) down().
0280  */
0281 void __sched mutex_lock(struct mutex *lock)
0282 {
0283     might_sleep();
0284 
0285     if (!__mutex_trylock_fast(lock))
0286         __mutex_lock_slowpath(lock);
0287 }
0288 EXPORT_SYMBOL(mutex_lock);
0289 #endif
0290 
0291 #include "ww_mutex.h"
0292 
0293 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
0294 
0295 /*
0296  * Trylock variant that returns the owning task on failure.
0297  */
0298 static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock)
0299 {
0300     return __mutex_trylock_common(lock, false);
0301 }
0302 
0303 static inline
0304 bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
0305                 struct mutex_waiter *waiter)
0306 {
0307     struct ww_mutex *ww;
0308 
0309     ww = container_of(lock, struct ww_mutex, base);
0310 
0311     /*
0312      * If ww->ctx is set the contents are undefined, only
0313      * by acquiring wait_lock there is a guarantee that
0314      * they are not invalid when reading.
0315      *
0316      * As such, when deadlock detection needs to be
0317      * performed the optimistic spinning cannot be done.
0318      *
0319      * Check this in every inner iteration because we may
0320      * be racing against another thread's ww_mutex_lock.
0321      */
0322     if (ww_ctx->acquired > 0 && READ_ONCE(ww->ctx))
0323         return false;
0324 
0325     /*
0326      * If we aren't on the wait list yet, cancel the spin
0327      * if there are waiters. We want  to avoid stealing the
0328      * lock from a waiter with an earlier stamp, since the
0329      * other thread may already own a lock that we also
0330      * need.
0331      */
0332     if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS))
0333         return false;
0334 
0335     /*
0336      * Similarly, stop spinning if we are no longer the
0337      * first waiter.
0338      */
0339     if (waiter && !__mutex_waiter_is_first(lock, waiter))
0340         return false;
0341 
0342     return true;
0343 }
0344 
0345 /*
0346  * Look out! "owner" is an entirely speculative pointer access and not
0347  * reliable.
0348  *
0349  * "noinline" so that this function shows up on perf profiles.
0350  */
0351 static noinline
0352 bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner,
0353              struct ww_acquire_ctx *ww_ctx, struct mutex_waiter *waiter)
0354 {
0355     bool ret = true;
0356 
0357     lockdep_assert_preemption_disabled();
0358 
0359     while (__mutex_owner(lock) == owner) {
0360         /*
0361          * Ensure we emit the owner->on_cpu, dereference _after_
0362          * checking lock->owner still matches owner. And we already
0363          * disabled preemption which is equal to the RCU read-side
0364          * crital section in optimistic spinning code. Thus the
0365          * task_strcut structure won't go away during the spinning
0366          * period
0367          */
0368         barrier();
0369 
0370         /*
0371          * Use vcpu_is_preempted to detect lock holder preemption issue.
0372          */
0373         if (!owner_on_cpu(owner) || need_resched()) {
0374             ret = false;
0375             break;
0376         }
0377 
0378         if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) {
0379             ret = false;
0380             break;
0381         }
0382 
0383         cpu_relax();
0384     }
0385 
0386     return ret;
0387 }
0388 
0389 /*
0390  * Initial check for entering the mutex spinning loop
0391  */
0392 static inline int mutex_can_spin_on_owner(struct mutex *lock)
0393 {
0394     struct task_struct *owner;
0395     int retval = 1;
0396 
0397     lockdep_assert_preemption_disabled();
0398 
0399     if (need_resched())
0400         return 0;
0401 
0402     /*
0403      * We already disabled preemption which is equal to the RCU read-side
0404      * crital section in optimistic spinning code. Thus the task_strcut
0405      * structure won't go away during the spinning period.
0406      */
0407     owner = __mutex_owner(lock);
0408     if (owner)
0409         retval = owner_on_cpu(owner);
0410 
0411     /*
0412      * If lock->owner is not set, the mutex has been released. Return true
0413      * such that we'll trylock in the spin path, which is a faster option
0414      * than the blocking slow path.
0415      */
0416     return retval;
0417 }
0418 
0419 /*
0420  * Optimistic spinning.
0421  *
0422  * We try to spin for acquisition when we find that the lock owner
0423  * is currently running on a (different) CPU and while we don't
0424  * need to reschedule. The rationale is that if the lock owner is
0425  * running, it is likely to release the lock soon.
0426  *
0427  * The mutex spinners are queued up using MCS lock so that only one
0428  * spinner can compete for the mutex. However, if mutex spinning isn't
0429  * going to happen, there is no point in going through the lock/unlock
0430  * overhead.
0431  *
0432  * Returns true when the lock was taken, otherwise false, indicating
0433  * that we need to jump to the slowpath and sleep.
0434  *
0435  * The waiter flag is set to true if the spinner is a waiter in the wait
0436  * queue. The waiter-spinner will spin on the lock directly and concurrently
0437  * with the spinner at the head of the OSQ, if present, until the owner is
0438  * changed to itself.
0439  */
0440 static __always_inline bool
0441 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
0442               struct mutex_waiter *waiter)
0443 {
0444     if (!waiter) {
0445         /*
0446          * The purpose of the mutex_can_spin_on_owner() function is
0447          * to eliminate the overhead of osq_lock() and osq_unlock()
0448          * in case spinning isn't possible. As a waiter-spinner
0449          * is not going to take OSQ lock anyway, there is no need
0450          * to call mutex_can_spin_on_owner().
0451          */
0452         if (!mutex_can_spin_on_owner(lock))
0453             goto fail;
0454 
0455         /*
0456          * In order to avoid a stampede of mutex spinners trying to
0457          * acquire the mutex all at once, the spinners need to take a
0458          * MCS (queued) lock first before spinning on the owner field.
0459          */
0460         if (!osq_lock(&lock->osq))
0461             goto fail;
0462     }
0463 
0464     for (;;) {
0465         struct task_struct *owner;
0466 
0467         /* Try to acquire the mutex... */
0468         owner = __mutex_trylock_or_owner(lock);
0469         if (!owner)
0470             break;
0471 
0472         /*
0473          * There's an owner, wait for it to either
0474          * release the lock or go to sleep.
0475          */
0476         if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter))
0477             goto fail_unlock;
0478 
0479         /*
0480          * The cpu_relax() call is a compiler barrier which forces
0481          * everything in this loop to be re-loaded. We don't need
0482          * memory barriers as we'll eventually observe the right
0483          * values at the cost of a few extra spins.
0484          */
0485         cpu_relax();
0486     }
0487 
0488     if (!waiter)
0489         osq_unlock(&lock->osq);
0490 
0491     return true;
0492 
0493 
0494 fail_unlock:
0495     if (!waiter)
0496         osq_unlock(&lock->osq);
0497 
0498 fail:
0499     /*
0500      * If we fell out of the spin path because of need_resched(),
0501      * reschedule now, before we try-lock the mutex. This avoids getting
0502      * scheduled out right after we obtained the mutex.
0503      */
0504     if (need_resched()) {
0505         /*
0506          * We _should_ have TASK_RUNNING here, but just in case
0507          * we do not, make it so, otherwise we might get stuck.
0508          */
0509         __set_current_state(TASK_RUNNING);
0510         schedule_preempt_disabled();
0511     }
0512 
0513     return false;
0514 }
0515 #else
0516 static __always_inline bool
0517 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,
0518               struct mutex_waiter *waiter)
0519 {
0520     return false;
0521 }
0522 #endif
0523 
0524 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
0525 
0526 /**
0527  * mutex_unlock - release the mutex
0528  * @lock: the mutex to be released
0529  *
0530  * Unlock a mutex that has been locked by this task previously.
0531  *
0532  * This function must not be used in interrupt context. Unlocking
0533  * of a not locked mutex is not allowed.
0534  *
0535  * This function is similar to (but not equivalent to) up().
0536  */
0537 void __sched mutex_unlock(struct mutex *lock)
0538 {
0539 #ifndef CONFIG_DEBUG_LOCK_ALLOC
0540     if (__mutex_unlock_fast(lock))
0541         return;
0542 #endif
0543     __mutex_unlock_slowpath(lock, _RET_IP_);
0544 }
0545 EXPORT_SYMBOL(mutex_unlock);
0546 
0547 /**
0548  * ww_mutex_unlock - release the w/w mutex
0549  * @lock: the mutex to be released
0550  *
0551  * Unlock a mutex that has been locked by this task previously with any of the
0552  * ww_mutex_lock* functions (with or without an acquire context). It is
0553  * forbidden to release the locks after releasing the acquire context.
0554  *
0555  * This function must not be used in interrupt context. Unlocking
0556  * of a unlocked mutex is not allowed.
0557  */
0558 void __sched ww_mutex_unlock(struct ww_mutex *lock)
0559 {
0560     __ww_mutex_unlock(lock);
0561     mutex_unlock(&lock->base);
0562 }
0563 EXPORT_SYMBOL(ww_mutex_unlock);
0564 
0565 /*
0566  * Lock a mutex (possibly interruptible), slowpath:
0567  */
0568 static __always_inline int __sched
0569 __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass,
0570             struct lockdep_map *nest_lock, unsigned long ip,
0571             struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
0572 {
0573     struct mutex_waiter waiter;
0574     struct ww_mutex *ww;
0575     int ret;
0576 
0577     if (!use_ww_ctx)
0578         ww_ctx = NULL;
0579 
0580     might_sleep();
0581 
0582     MUTEX_WARN_ON(lock->magic != lock);
0583 
0584     ww = container_of(lock, struct ww_mutex, base);
0585     if (ww_ctx) {
0586         if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
0587             return -EALREADY;
0588 
0589         /*
0590          * Reset the wounded flag after a kill. No other process can
0591          * race and wound us here since they can't have a valid owner
0592          * pointer if we don't have any locks held.
0593          */
0594         if (ww_ctx->acquired == 0)
0595             ww_ctx->wounded = 0;
0596 
0597 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0598         nest_lock = &ww_ctx->dep_map;
0599 #endif
0600     }
0601 
0602     preempt_disable();
0603     mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
0604 
0605     trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
0606     if (__mutex_trylock(lock) ||
0607         mutex_optimistic_spin(lock, ww_ctx, NULL)) {
0608         /* got the lock, yay! */
0609         lock_acquired(&lock->dep_map, ip);
0610         if (ww_ctx)
0611             ww_mutex_set_context_fastpath(ww, ww_ctx);
0612         trace_contention_end(lock, 0);
0613         preempt_enable();
0614         return 0;
0615     }
0616 
0617     raw_spin_lock(&lock->wait_lock);
0618     /*
0619      * After waiting to acquire the wait_lock, try again.
0620      */
0621     if (__mutex_trylock(lock)) {
0622         if (ww_ctx)
0623             __ww_mutex_check_waiters(lock, ww_ctx);
0624 
0625         goto skip_wait;
0626     }
0627 
0628     debug_mutex_lock_common(lock, &waiter);
0629     waiter.task = current;
0630     if (use_ww_ctx)
0631         waiter.ww_ctx = ww_ctx;
0632 
0633     lock_contended(&lock->dep_map, ip);
0634 
0635     if (!use_ww_ctx) {
0636         /* add waiting tasks to the end of the waitqueue (FIFO): */
0637         __mutex_add_waiter(lock, &waiter, &lock->wait_list);
0638     } else {
0639         /*
0640          * Add in stamp order, waking up waiters that must kill
0641          * themselves.
0642          */
0643         ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx);
0644         if (ret)
0645             goto err_early_kill;
0646     }
0647 
0648     set_current_state(state);
0649     trace_contention_begin(lock, LCB_F_MUTEX);
0650     for (;;) {
0651         bool first;
0652 
0653         /*
0654          * Once we hold wait_lock, we're serialized against
0655          * mutex_unlock() handing the lock off to us, do a trylock
0656          * before testing the error conditions to make sure we pick up
0657          * the handoff.
0658          */
0659         if (__mutex_trylock(lock))
0660             goto acquired;
0661 
0662         /*
0663          * Check for signals and kill conditions while holding
0664          * wait_lock. This ensures the lock cancellation is ordered
0665          * against mutex_unlock() and wake-ups do not go missing.
0666          */
0667         if (signal_pending_state(state, current)) {
0668             ret = -EINTR;
0669             goto err;
0670         }
0671 
0672         if (ww_ctx) {
0673             ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx);
0674             if (ret)
0675                 goto err;
0676         }
0677 
0678         raw_spin_unlock(&lock->wait_lock);
0679         schedule_preempt_disabled();
0680 
0681         first = __mutex_waiter_is_first(lock, &waiter);
0682 
0683         set_current_state(state);
0684         /*
0685          * Here we order against unlock; we must either see it change
0686          * state back to RUNNING and fall through the next schedule(),
0687          * or we must see its unlock and acquire.
0688          */
0689         if (__mutex_trylock_or_handoff(lock, first))
0690             break;
0691 
0692         if (first) {
0693             trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN);
0694             if (mutex_optimistic_spin(lock, ww_ctx, &waiter))
0695                 break;
0696             trace_contention_begin(lock, LCB_F_MUTEX);
0697         }
0698 
0699         raw_spin_lock(&lock->wait_lock);
0700     }
0701     raw_spin_lock(&lock->wait_lock);
0702 acquired:
0703     __set_current_state(TASK_RUNNING);
0704 
0705     if (ww_ctx) {
0706         /*
0707          * Wound-Wait; we stole the lock (!first_waiter), check the
0708          * waiters as anyone might want to wound us.
0709          */
0710         if (!ww_ctx->is_wait_die &&
0711             !__mutex_waiter_is_first(lock, &waiter))
0712             __ww_mutex_check_waiters(lock, ww_ctx);
0713     }
0714 
0715     __mutex_remove_waiter(lock, &waiter);
0716 
0717     debug_mutex_free_waiter(&waiter);
0718 
0719 skip_wait:
0720     /* got the lock - cleanup and rejoice! */
0721     lock_acquired(&lock->dep_map, ip);
0722     trace_contention_end(lock, 0);
0723 
0724     if (ww_ctx)
0725         ww_mutex_lock_acquired(ww, ww_ctx);
0726 
0727     raw_spin_unlock(&lock->wait_lock);
0728     preempt_enable();
0729     return 0;
0730 
0731 err:
0732     __set_current_state(TASK_RUNNING);
0733     __mutex_remove_waiter(lock, &waiter);
0734 err_early_kill:
0735     trace_contention_end(lock, ret);
0736     raw_spin_unlock(&lock->wait_lock);
0737     debug_mutex_free_waiter(&waiter);
0738     mutex_release(&lock->dep_map, ip);
0739     preempt_enable();
0740     return ret;
0741 }
0742 
0743 static int __sched
0744 __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
0745          struct lockdep_map *nest_lock, unsigned long ip)
0746 {
0747     return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false);
0748 }
0749 
0750 static int __sched
0751 __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass,
0752         unsigned long ip, struct ww_acquire_ctx *ww_ctx)
0753 {
0754     return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true);
0755 }
0756 
0757 /**
0758  * ww_mutex_trylock - tries to acquire the w/w mutex with optional acquire context
0759  * @ww: mutex to lock
0760  * @ww_ctx: optional w/w acquire context
0761  *
0762  * Trylocks a mutex with the optional acquire context; no deadlock detection is
0763  * possible. Returns 1 if the mutex has been acquired successfully, 0 otherwise.
0764  *
0765  * Unlike ww_mutex_lock, no deadlock handling is performed. However, if a @ctx is
0766  * specified, -EALREADY handling may happen in calls to ww_mutex_trylock.
0767  *
0768  * A mutex acquired with this function must be released with ww_mutex_unlock.
0769  */
0770 int ww_mutex_trylock(struct ww_mutex *ww, struct ww_acquire_ctx *ww_ctx)
0771 {
0772     if (!ww_ctx)
0773         return mutex_trylock(&ww->base);
0774 
0775     MUTEX_WARN_ON(ww->base.magic != &ww->base);
0776 
0777     /*
0778      * Reset the wounded flag after a kill. No other process can
0779      * race and wound us here, since they can't have a valid owner
0780      * pointer if we don't have any locks held.
0781      */
0782     if (ww_ctx->acquired == 0)
0783         ww_ctx->wounded = 0;
0784 
0785     if (__mutex_trylock(&ww->base)) {
0786         ww_mutex_set_context_fastpath(ww, ww_ctx);
0787         mutex_acquire_nest(&ww->base.dep_map, 0, 1, &ww_ctx->dep_map, _RET_IP_);
0788         return 1;
0789     }
0790 
0791     return 0;
0792 }
0793 EXPORT_SYMBOL(ww_mutex_trylock);
0794 
0795 #ifdef CONFIG_DEBUG_LOCK_ALLOC
0796 void __sched
0797 mutex_lock_nested(struct mutex *lock, unsigned int subclass)
0798 {
0799     __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_);
0800 }
0801 
0802 EXPORT_SYMBOL_GPL(mutex_lock_nested);
0803 
0804 void __sched
0805 _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
0806 {
0807     __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_);
0808 }
0809 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
0810 
0811 int __sched
0812 mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
0813 {
0814     return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_);
0815 }
0816 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
0817 
0818 int __sched
0819 mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
0820 {
0821     return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_);
0822 }
0823 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
0824 
0825 void __sched
0826 mutex_lock_io_nested(struct mutex *lock, unsigned int subclass)
0827 {
0828     int token;
0829 
0830     might_sleep();
0831 
0832     token = io_schedule_prepare();
0833     __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
0834                 subclass, NULL, _RET_IP_, NULL, 0);
0835     io_schedule_finish(token);
0836 }
0837 EXPORT_SYMBOL_GPL(mutex_lock_io_nested);
0838 
0839 static inline int
0840 ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
0841 {
0842 #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
0843     unsigned tmp;
0844 
0845     if (ctx->deadlock_inject_countdown-- == 0) {
0846         tmp = ctx->deadlock_inject_interval;
0847         if (tmp > UINT_MAX/4)
0848             tmp = UINT_MAX;
0849         else
0850             tmp = tmp*2 + tmp + tmp/2;
0851 
0852         ctx->deadlock_inject_interval = tmp;
0853         ctx->deadlock_inject_countdown = tmp;
0854         ctx->contending_lock = lock;
0855 
0856         ww_mutex_unlock(lock);
0857 
0858         return -EDEADLK;
0859     }
0860 #endif
0861 
0862     return 0;
0863 }
0864 
0865 int __sched
0866 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
0867 {
0868     int ret;
0869 
0870     might_sleep();
0871     ret =  __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE,
0872                    0, _RET_IP_, ctx);
0873     if (!ret && ctx && ctx->acquired > 1)
0874         return ww_mutex_deadlock_injection(lock, ctx);
0875 
0876     return ret;
0877 }
0878 EXPORT_SYMBOL_GPL(ww_mutex_lock);
0879 
0880 int __sched
0881 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
0882 {
0883     int ret;
0884 
0885     might_sleep();
0886     ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE,
0887                   0, _RET_IP_, ctx);
0888 
0889     if (!ret && ctx && ctx->acquired > 1)
0890         return ww_mutex_deadlock_injection(lock, ctx);
0891 
0892     return ret;
0893 }
0894 EXPORT_SYMBOL_GPL(ww_mutex_lock_interruptible);
0895 
0896 #endif
0897 
0898 /*
0899  * Release the lock, slowpath:
0900  */
0901 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip)
0902 {
0903     struct task_struct *next = NULL;
0904     DEFINE_WAKE_Q(wake_q);
0905     unsigned long owner;
0906 
0907     mutex_release(&lock->dep_map, ip);
0908 
0909     /*
0910      * Release the lock before (potentially) taking the spinlock such that
0911      * other contenders can get on with things ASAP.
0912      *
0913      * Except when HANDOFF, in that case we must not clear the owner field,
0914      * but instead set it to the top waiter.
0915      */
0916     owner = atomic_long_read(&lock->owner);
0917     for (;;) {
0918         MUTEX_WARN_ON(__owner_task(owner) != current);
0919         MUTEX_WARN_ON(owner & MUTEX_FLAG_PICKUP);
0920 
0921         if (owner & MUTEX_FLAG_HANDOFF)
0922             break;
0923 
0924         if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) {
0925             if (owner & MUTEX_FLAG_WAITERS)
0926                 break;
0927 
0928             return;
0929         }
0930     }
0931 
0932     raw_spin_lock(&lock->wait_lock);
0933     debug_mutex_unlock(lock);
0934     if (!list_empty(&lock->wait_list)) {
0935         /* get the first entry from the wait-list: */
0936         struct mutex_waiter *waiter =
0937             list_first_entry(&lock->wait_list,
0938                      struct mutex_waiter, list);
0939 
0940         next = waiter->task;
0941 
0942         debug_mutex_wake_waiter(lock, waiter);
0943         wake_q_add(&wake_q, next);
0944     }
0945 
0946     if (owner & MUTEX_FLAG_HANDOFF)
0947         __mutex_handoff(lock, next);
0948 
0949     raw_spin_unlock(&lock->wait_lock);
0950 
0951     wake_up_q(&wake_q);
0952 }
0953 
0954 #ifndef CONFIG_DEBUG_LOCK_ALLOC
0955 /*
0956  * Here come the less common (and hence less performance-critical) APIs:
0957  * mutex_lock_interruptible() and mutex_trylock().
0958  */
0959 static noinline int __sched
0960 __mutex_lock_killable_slowpath(struct mutex *lock);
0961 
0962 static noinline int __sched
0963 __mutex_lock_interruptible_slowpath(struct mutex *lock);
0964 
0965 /**
0966  * mutex_lock_interruptible() - Acquire the mutex, interruptible by signals.
0967  * @lock: The mutex to be acquired.
0968  *
0969  * Lock the mutex like mutex_lock().  If a signal is delivered while the
0970  * process is sleeping, this function will return without acquiring the
0971  * mutex.
0972  *
0973  * Context: Process context.
0974  * Return: 0 if the lock was successfully acquired or %-EINTR if a
0975  * signal arrived.
0976  */
0977 int __sched mutex_lock_interruptible(struct mutex *lock)
0978 {
0979     might_sleep();
0980 
0981     if (__mutex_trylock_fast(lock))
0982         return 0;
0983 
0984     return __mutex_lock_interruptible_slowpath(lock);
0985 }
0986 
0987 EXPORT_SYMBOL(mutex_lock_interruptible);
0988 
0989 /**
0990  * mutex_lock_killable() - Acquire the mutex, interruptible by fatal signals.
0991  * @lock: The mutex to be acquired.
0992  *
0993  * Lock the mutex like mutex_lock().  If a signal which will be fatal to
0994  * the current process is delivered while the process is sleeping, this
0995  * function will return without acquiring the mutex.
0996  *
0997  * Context: Process context.
0998  * Return: 0 if the lock was successfully acquired or %-EINTR if a
0999  * fatal signal arrived.
1000  */
1001 int __sched mutex_lock_killable(struct mutex *lock)
1002 {
1003     might_sleep();
1004 
1005     if (__mutex_trylock_fast(lock))
1006         return 0;
1007 
1008     return __mutex_lock_killable_slowpath(lock);
1009 }
1010 EXPORT_SYMBOL(mutex_lock_killable);
1011 
1012 /**
1013  * mutex_lock_io() - Acquire the mutex and mark the process as waiting for I/O
1014  * @lock: The mutex to be acquired.
1015  *
1016  * Lock the mutex like mutex_lock().  While the task is waiting for this
1017  * mutex, it will be accounted as being in the IO wait state by the
1018  * scheduler.
1019  *
1020  * Context: Process context.
1021  */
1022 void __sched mutex_lock_io(struct mutex *lock)
1023 {
1024     int token;
1025 
1026     token = io_schedule_prepare();
1027     mutex_lock(lock);
1028     io_schedule_finish(token);
1029 }
1030 EXPORT_SYMBOL_GPL(mutex_lock_io);
1031 
1032 static noinline void __sched
1033 __mutex_lock_slowpath(struct mutex *lock)
1034 {
1035     __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_);
1036 }
1037 
1038 static noinline int __sched
1039 __mutex_lock_killable_slowpath(struct mutex *lock)
1040 {
1041     return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_);
1042 }
1043 
1044 static noinline int __sched
1045 __mutex_lock_interruptible_slowpath(struct mutex *lock)
1046 {
1047     return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_);
1048 }
1049 
1050 static noinline int __sched
1051 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1052 {
1053     return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0,
1054                    _RET_IP_, ctx);
1055 }
1056 
1057 static noinline int __sched
1058 __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
1059                         struct ww_acquire_ctx *ctx)
1060 {
1061     return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0,
1062                    _RET_IP_, ctx);
1063 }
1064 
1065 #endif
1066 
1067 /**
1068  * mutex_trylock - try to acquire the mutex, without waiting
1069  * @lock: the mutex to be acquired
1070  *
1071  * Try to acquire the mutex atomically. Returns 1 if the mutex
1072  * has been acquired successfully, and 0 on contention.
1073  *
1074  * NOTE: this function follows the spin_trylock() convention, so
1075  * it is negated from the down_trylock() return values! Be careful
1076  * about this when converting semaphore users to mutexes.
1077  *
1078  * This function must not be used in interrupt context. The
1079  * mutex must be released by the same task that acquired it.
1080  */
1081 int __sched mutex_trylock(struct mutex *lock)
1082 {
1083     bool locked;
1084 
1085     MUTEX_WARN_ON(lock->magic != lock);
1086 
1087     locked = __mutex_trylock(lock);
1088     if (locked)
1089         mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
1090 
1091     return locked;
1092 }
1093 EXPORT_SYMBOL(mutex_trylock);
1094 
1095 #ifndef CONFIG_DEBUG_LOCK_ALLOC
1096 int __sched
1097 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1098 {
1099     might_sleep();
1100 
1101     if (__mutex_trylock_fast(&lock->base)) {
1102         if (ctx)
1103             ww_mutex_set_context_fastpath(lock, ctx);
1104         return 0;
1105     }
1106 
1107     return __ww_mutex_lock_slowpath(lock, ctx);
1108 }
1109 EXPORT_SYMBOL(ww_mutex_lock);
1110 
1111 int __sched
1112 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
1113 {
1114     might_sleep();
1115 
1116     if (__mutex_trylock_fast(&lock->base)) {
1117         if (ctx)
1118             ww_mutex_set_context_fastpath(lock, ctx);
1119         return 0;
1120     }
1121 
1122     return __ww_mutex_lock_interruptible_slowpath(lock, ctx);
1123 }
1124 EXPORT_SYMBOL(ww_mutex_lock_interruptible);
1125 
1126 #endif /* !CONFIG_DEBUG_LOCK_ALLOC */
1127 #endif /* !CONFIG_PREEMPT_RT */
1128 
1129 /**
1130  * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0
1131  * @cnt: the atomic which we are to dec
1132  * @lock: the mutex to return holding if we dec to 0
1133  *
1134  * return true and hold lock if we dec to 0, return false otherwise
1135  */
1136 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
1137 {
1138     /* dec if we can't possibly hit 0 */
1139     if (atomic_add_unless(cnt, -1, 1))
1140         return 0;
1141     /* we might hit 0, so take the lock */
1142     mutex_lock(lock);
1143     if (!atomic_dec_and_test(cnt)) {
1144         /* when we actually did the dec, we didn't hit 0 */
1145         mutex_unlock(lock);
1146         return 0;
1147     }
1148     /* we hit 0, and we hold the lock */
1149     return 1;
1150 }
1151 EXPORT_SYMBOL(atomic_dec_and_mutex_lock);