123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977 |
- #include <linux/mutex.h>
- #include <linux/ww_mutex.h>
- #include <linux/sched.h>
- #include <linux/sched/rt.h>
- #include <linux/export.h>
- #include <linux/spinlock.h>
- #include <linux/interrupt.h>
- #include <linux/debug_locks.h>
- #include <linux/osq_lock.h>
- #ifdef CONFIG_DEBUG_MUTEXES
- # include "mutex-debug.h"
- # include <asm-generic/mutex-null.h>
- # undef __mutex_slowpath_needs_to_unlock
- # define __mutex_slowpath_needs_to_unlock() 0
- #else
- # include "mutex.h"
- # include <asm/mutex.h>
- #endif
- void
- __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)
- {
- atomic_set(&lock->count, 1);
- spin_lock_init(&lock->wait_lock);
- INIT_LIST_HEAD(&lock->wait_list);
- mutex_clear_owner(lock);
- #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
- osq_lock_init(&lock->osq);
- #endif
- debug_mutex_init(lock, name, key);
- }
- EXPORT_SYMBOL(__mutex_init);
- #ifndef CONFIG_DEBUG_LOCK_ALLOC
- __visible void __sched __mutex_lock_slowpath(atomic_t *lock_count);
- void __sched mutex_lock(struct mutex *lock)
- {
- might_sleep();
-
- __mutex_fastpath_lock(&lock->count, __mutex_lock_slowpath);
- mutex_set_owner(lock);
- }
- EXPORT_SYMBOL(mutex_lock);
- #endif
- static __always_inline void ww_mutex_lock_acquired(struct ww_mutex *ww,
- struct ww_acquire_ctx *ww_ctx)
- {
- #ifdef CONFIG_DEBUG_MUTEXES
-
- DEBUG_LOCKS_WARN_ON(ww->ctx);
-
- DEBUG_LOCKS_WARN_ON(ww_ctx->done_acquire);
- if (ww_ctx->contending_lock) {
-
- DEBUG_LOCKS_WARN_ON(ww_ctx->contending_lock != ww);
-
- DEBUG_LOCKS_WARN_ON(ww_ctx->acquired > 0);
- ww_ctx->contending_lock = NULL;
- }
-
- DEBUG_LOCKS_WARN_ON(ww_ctx->ww_class != ww->ww_class);
- #endif
- ww_ctx->acquired++;
- }
- static __always_inline void
- ww_mutex_set_context_fastpath(struct ww_mutex *lock,
- struct ww_acquire_ctx *ctx)
- {
- unsigned long flags;
- struct mutex_waiter *cur;
- ww_mutex_lock_acquired(lock, ctx);
- lock->ctx = ctx;
-
- smp_mb();
-
- if (likely(atomic_read(&lock->base.count) == 0))
- return;
-
- spin_lock_mutex(&lock->base.wait_lock, flags);
- list_for_each_entry(cur, &lock->base.wait_list, list) {
- debug_mutex_wake_waiter(&lock->base, cur);
- wake_up_process(cur->task);
- }
- spin_unlock_mutex(&lock->base.wait_lock, flags);
- }
- static __always_inline void
- ww_mutex_set_context_slowpath(struct ww_mutex *lock,
- struct ww_acquire_ctx *ctx)
- {
- struct mutex_waiter *cur;
- ww_mutex_lock_acquired(lock, ctx);
- lock->ctx = ctx;
-
- list_for_each_entry(cur, &lock->base.wait_list, list) {
- debug_mutex_wake_waiter(&lock->base, cur);
- wake_up_process(cur->task);
- }
- }
- #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
- static noinline
- bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner)
- {
- bool ret = true;
- rcu_read_lock();
- while (lock->owner == owner) {
-
- barrier();
- if (!owner->on_cpu || need_resched()) {
- ret = false;
- break;
- }
- cpu_relax_lowlatency();
- }
- rcu_read_unlock();
- return ret;
- }
- static inline int mutex_can_spin_on_owner(struct mutex *lock)
- {
- struct task_struct *owner;
- int retval = 1;
- if (need_resched())
- return 0;
- rcu_read_lock();
- owner = READ_ONCE(lock->owner);
- if (owner)
- retval = owner->on_cpu;
- rcu_read_unlock();
-
- return retval;
- }
- static inline bool mutex_try_to_acquire(struct mutex *lock)
- {
- return !mutex_is_locked(lock) &&
- (atomic_cmpxchg_acquire(&lock->count, 1, 0) == 1);
- }
- static bool mutex_optimistic_spin(struct mutex *lock,
- struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
- {
- struct task_struct *task = current;
- if (!mutex_can_spin_on_owner(lock))
- goto done;
-
- if (!osq_lock(&lock->osq))
- goto done;
- while (true) {
- struct task_struct *owner;
- if (use_ww_ctx && ww_ctx->acquired > 0) {
- struct ww_mutex *ww;
- ww = container_of(lock, struct ww_mutex, base);
-
- if (READ_ONCE(ww->ctx))
- break;
- }
-
- owner = READ_ONCE(lock->owner);
- if (owner && !mutex_spin_on_owner(lock, owner))
- break;
-
- if (mutex_try_to_acquire(lock)) {
- lock_acquired(&lock->dep_map, ip);
- if (use_ww_ctx) {
- struct ww_mutex *ww;
- ww = container_of(lock, struct ww_mutex, base);
- ww_mutex_set_context_fastpath(ww, ww_ctx);
- }
- mutex_set_owner(lock);
- osq_unlock(&lock->osq);
- return true;
- }
-
- if (!owner && (need_resched() || rt_task(task)))
- break;
-
- cpu_relax_lowlatency();
- }
- osq_unlock(&lock->osq);
- done:
-
- if (need_resched()) {
-
- __set_current_state(TASK_RUNNING);
- schedule_preempt_disabled();
- }
- return false;
- }
- #else
- static bool mutex_optimistic_spin(struct mutex *lock,
- struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
- {
- return false;
- }
- #endif
- __visible __used noinline
- void __sched __mutex_unlock_slowpath(atomic_t *lock_count);
- void __sched mutex_unlock(struct mutex *lock)
- {
-
- #ifndef CONFIG_DEBUG_MUTEXES
-
- mutex_clear_owner(lock);
- #endif
- __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
- }
- EXPORT_SYMBOL(mutex_unlock);
- void __sched ww_mutex_unlock(struct ww_mutex *lock)
- {
-
- if (lock->ctx) {
- #ifdef CONFIG_DEBUG_MUTEXES
- DEBUG_LOCKS_WARN_ON(!lock->ctx->acquired);
- #endif
- if (lock->ctx->acquired > 0)
- lock->ctx->acquired--;
- lock->ctx = NULL;
- }
- #ifndef CONFIG_DEBUG_MUTEXES
-
- mutex_clear_owner(&lock->base);
- #endif
- __mutex_fastpath_unlock(&lock->base.count, __mutex_unlock_slowpath);
- }
- EXPORT_SYMBOL(ww_mutex_unlock);
- static inline int __sched
- __ww_mutex_lock_check_stamp(struct mutex *lock, struct ww_acquire_ctx *ctx)
- {
- struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
- struct ww_acquire_ctx *hold_ctx = READ_ONCE(ww->ctx);
- if (!hold_ctx)
- return 0;
- if (ctx->stamp - hold_ctx->stamp <= LONG_MAX &&
- (ctx->stamp != hold_ctx->stamp || ctx > hold_ctx)) {
- #ifdef CONFIG_DEBUG_MUTEXES
- DEBUG_LOCKS_WARN_ON(ctx->contending_lock);
- ctx->contending_lock = ww;
- #endif
- return -EDEADLK;
- }
- return 0;
- }
- static __always_inline int __sched
- __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
- struct lockdep_map *nest_lock, unsigned long ip,
- struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
- {
- struct task_struct *task = current;
- struct mutex_waiter waiter;
- unsigned long flags;
- int ret;
- if (use_ww_ctx) {
- struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
- if (unlikely(ww_ctx == READ_ONCE(ww->ctx)))
- return -EALREADY;
- }
- preempt_disable();
- mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip);
- if (mutex_optimistic_spin(lock, ww_ctx, use_ww_ctx)) {
-
- preempt_enable();
- return 0;
- }
- spin_lock_mutex(&lock->wait_lock, flags);
-
- if (!mutex_is_locked(lock) &&
- (atomic_xchg_acquire(&lock->count, 0) == 1))
- goto skip_wait;
- debug_mutex_lock_common(lock, &waiter);
- debug_mutex_add_waiter(lock, &waiter, task);
-
- list_add_tail(&waiter.list, &lock->wait_list);
- waiter.task = task;
- lock_contended(&lock->dep_map, ip);
- for (;;) {
-
- if (atomic_read(&lock->count) >= 0 &&
- (atomic_xchg_acquire(&lock->count, -1) == 1))
- break;
-
- if (unlikely(signal_pending_state(state, task))) {
- ret = -EINTR;
- goto err;
- }
- if (use_ww_ctx && ww_ctx->acquired > 0) {
- ret = __ww_mutex_lock_check_stamp(lock, ww_ctx);
- if (ret)
- goto err;
- }
- __set_task_state(task, state);
-
- spin_unlock_mutex(&lock->wait_lock, flags);
- schedule_preempt_disabled();
- spin_lock_mutex(&lock->wait_lock, flags);
- }
- __set_task_state(task, TASK_RUNNING);
- mutex_remove_waiter(lock, &waiter, task);
-
- if (likely(list_empty(&lock->wait_list)))
- atomic_set(&lock->count, 0);
- debug_mutex_free_waiter(&waiter);
- skip_wait:
-
- lock_acquired(&lock->dep_map, ip);
- mutex_set_owner(lock);
- if (use_ww_ctx) {
- struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
- ww_mutex_set_context_slowpath(ww, ww_ctx);
- }
- spin_unlock_mutex(&lock->wait_lock, flags);
- preempt_enable();
- return 0;
- err:
- mutex_remove_waiter(lock, &waiter, task);
- spin_unlock_mutex(&lock->wait_lock, flags);
- debug_mutex_free_waiter(&waiter);
- mutex_release(&lock->dep_map, 1, ip);
- preempt_enable();
- return ret;
- }
- #ifdef CONFIG_DEBUG_LOCK_ALLOC
- void __sched
- mutex_lock_nested(struct mutex *lock, unsigned int subclass)
- {
- might_sleep();
- __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
- subclass, NULL, _RET_IP_, NULL, 0);
- }
- EXPORT_SYMBOL_GPL(mutex_lock_nested);
- void __sched
- _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
- {
- might_sleep();
- __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
- 0, nest, _RET_IP_, NULL, 0);
- }
- EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
- int __sched
- mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
- {
- might_sleep();
- return __mutex_lock_common(lock, TASK_KILLABLE,
- subclass, NULL, _RET_IP_, NULL, 0);
- }
- EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
- int __sched
- mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
- {
- might_sleep();
- return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
- subclass, NULL, _RET_IP_, NULL, 0);
- }
- EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
- static inline int
- ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
- {
- #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH
- unsigned tmp;
- if (ctx->deadlock_inject_countdown-- == 0) {
- tmp = ctx->deadlock_inject_interval;
- if (tmp > UINT_MAX/4)
- tmp = UINT_MAX;
- else
- tmp = tmp*2 + tmp + tmp/2;
- ctx->deadlock_inject_interval = tmp;
- ctx->deadlock_inject_countdown = tmp;
- ctx->contending_lock = lock;
- ww_mutex_unlock(lock);
- return -EDEADLK;
- }
- #endif
- return 0;
- }
- int __sched
- __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
- {
- int ret;
- might_sleep();
- ret = __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE,
- 0, &ctx->dep_map, _RET_IP_, ctx, 1);
- if (!ret && ctx->acquired > 1)
- return ww_mutex_deadlock_injection(lock, ctx);
- return ret;
- }
- EXPORT_SYMBOL_GPL(__ww_mutex_lock);
- int __sched
- __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
- {
- int ret;
- might_sleep();
- ret = __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE,
- 0, &ctx->dep_map, _RET_IP_, ctx, 1);
- if (!ret && ctx->acquired > 1)
- return ww_mutex_deadlock_injection(lock, ctx);
- return ret;
- }
- EXPORT_SYMBOL_GPL(__ww_mutex_lock_interruptible);
- #endif
- static inline void
- __mutex_unlock_common_slowpath(struct mutex *lock, int nested)
- {
- unsigned long flags;
- WAKE_Q(wake_q);
-
- if (__mutex_slowpath_needs_to_unlock())
- atomic_set(&lock->count, 1);
- spin_lock_mutex(&lock->wait_lock, flags);
- mutex_release(&lock->dep_map, nested, _RET_IP_);
- debug_mutex_unlock(lock);
- if (!list_empty(&lock->wait_list)) {
-
- struct mutex_waiter *waiter =
- list_entry(lock->wait_list.next,
- struct mutex_waiter, list);
- debug_mutex_wake_waiter(lock, waiter);
- wake_q_add(&wake_q, waiter->task);
- }
- spin_unlock_mutex(&lock->wait_lock, flags);
- wake_up_q(&wake_q);
- }
- __visible void
- __mutex_unlock_slowpath(atomic_t *lock_count)
- {
- struct mutex *lock = container_of(lock_count, struct mutex, count);
- __mutex_unlock_common_slowpath(lock, 1);
- }
- #ifndef CONFIG_DEBUG_LOCK_ALLOC
- static noinline int __sched
- __mutex_lock_killable_slowpath(struct mutex *lock);
- static noinline int __sched
- __mutex_lock_interruptible_slowpath(struct mutex *lock);
- int __sched mutex_lock_interruptible(struct mutex *lock)
- {
- int ret;
- might_sleep();
- ret = __mutex_fastpath_lock_retval(&lock->count);
- if (likely(!ret)) {
- mutex_set_owner(lock);
- return 0;
- } else
- return __mutex_lock_interruptible_slowpath(lock);
- }
- EXPORT_SYMBOL(mutex_lock_interruptible);
- int __sched mutex_lock_killable(struct mutex *lock)
- {
- int ret;
- might_sleep();
- ret = __mutex_fastpath_lock_retval(&lock->count);
- if (likely(!ret)) {
- mutex_set_owner(lock);
- return 0;
- } else
- return __mutex_lock_killable_slowpath(lock);
- }
- EXPORT_SYMBOL(mutex_lock_killable);
- __visible void __sched
- __mutex_lock_slowpath(atomic_t *lock_count)
- {
- struct mutex *lock = container_of(lock_count, struct mutex, count);
- __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
- NULL, _RET_IP_, NULL, 0);
- }
- static noinline int __sched
- __mutex_lock_killable_slowpath(struct mutex *lock)
- {
- return __mutex_lock_common(lock, TASK_KILLABLE, 0,
- NULL, _RET_IP_, NULL, 0);
- }
- static noinline int __sched
- __mutex_lock_interruptible_slowpath(struct mutex *lock)
- {
- return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0,
- NULL, _RET_IP_, NULL, 0);
- }
- static noinline int __sched
- __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
- {
- return __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 0,
- NULL, _RET_IP_, ctx, 1);
- }
- static noinline int __sched
- __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
- struct ww_acquire_ctx *ctx)
- {
- return __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 0,
- NULL, _RET_IP_, ctx, 1);
- }
- #endif
- static inline int __mutex_trylock_slowpath(atomic_t *lock_count)
- {
- struct mutex *lock = container_of(lock_count, struct mutex, count);
- unsigned long flags;
- int prev;
-
- if (mutex_is_locked(lock))
- return 0;
- spin_lock_mutex(&lock->wait_lock, flags);
- prev = atomic_xchg_acquire(&lock->count, -1);
- if (likely(prev == 1)) {
- mutex_set_owner(lock);
- mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_);
- }
-
- if (likely(list_empty(&lock->wait_list)))
- atomic_set(&lock->count, 0);
- spin_unlock_mutex(&lock->wait_lock, flags);
- return prev == 1;
- }
- int __sched mutex_trylock(struct mutex *lock)
- {
- int ret;
- ret = __mutex_fastpath_trylock(&lock->count, __mutex_trylock_slowpath);
- if (ret)
- mutex_set_owner(lock);
- return ret;
- }
- EXPORT_SYMBOL(mutex_trylock);
- #ifndef CONFIG_DEBUG_LOCK_ALLOC
- int __sched
- __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
- {
- int ret;
- might_sleep();
- ret = __mutex_fastpath_lock_retval(&lock->base.count);
- if (likely(!ret)) {
- ww_mutex_set_context_fastpath(lock, ctx);
- mutex_set_owner(&lock->base);
- } else
- ret = __ww_mutex_lock_slowpath(lock, ctx);
- return ret;
- }
- EXPORT_SYMBOL(__ww_mutex_lock);
- int __sched
- __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
- {
- int ret;
- might_sleep();
- ret = __mutex_fastpath_lock_retval(&lock->base.count);
- if (likely(!ret)) {
- ww_mutex_set_context_fastpath(lock, ctx);
- mutex_set_owner(&lock->base);
- } else
- ret = __ww_mutex_lock_interruptible_slowpath(lock, ctx);
- return ret;
- }
- EXPORT_SYMBOL(__ww_mutex_lock_interruptible);
- #endif
- int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock)
- {
-
- if (atomic_add_unless(cnt, -1, 1))
- return 0;
-
- mutex_lock(lock);
- if (!atomic_dec_and_test(cnt)) {
-
- mutex_unlock(lock);
- return 0;
- }
-
- return 1;
- }
- EXPORT_SYMBOL(atomic_dec_and_mutex_lock);
|