Lines Matching refs:lock
46 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key) in __mutex_init() argument
48 atomic_long_set(&lock->owner, 0); in __mutex_init()
49 raw_spin_lock_init(&lock->wait_lock); in __mutex_init()
50 INIT_LIST_HEAD(&lock->wait_list); in __mutex_init()
52 osq_lock_init(&lock->osq); in __mutex_init()
55 debug_mutex_init(lock, name, key); in __mutex_init()
79 static inline struct task_struct *__mutex_owner(struct mutex *lock) in __mutex_owner() argument
81 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS); in __mutex_owner()
89 bool mutex_is_locked(struct mutex *lock) in mutex_is_locked() argument
91 return __mutex_owner(lock) != NULL; in mutex_is_locked()
103 static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff) in __mutex_trylock_common() argument
107 owner = atomic_long_read(&lock->owner); in __mutex_trylock_common()
129 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) { in __mutex_trylock_common()
142 static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff) in __mutex_trylock_or_handoff() argument
144 return !__mutex_trylock_common(lock, handoff); in __mutex_trylock_or_handoff()
150 static inline bool __mutex_trylock(struct mutex *lock) in __mutex_trylock() argument
152 return !__mutex_trylock_common(lock, false); in __mutex_trylock()
166 static __always_inline bool __mutex_trylock_fast(struct mutex *lock) in __mutex_trylock_fast() argument
171 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr)) in __mutex_trylock_fast()
177 static __always_inline bool __mutex_unlock_fast(struct mutex *lock) in __mutex_unlock_fast() argument
181 return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); in __mutex_unlock_fast()
185 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) in __mutex_set_flag() argument
187 atomic_long_or(flag, &lock->owner); in __mutex_set_flag()
190 static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) in __mutex_clear_flag() argument
192 atomic_long_andnot(flag, &lock->owner); in __mutex_clear_flag()
195 static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) in __mutex_waiter_is_first() argument
197 return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; in __mutex_waiter_is_first()
205 __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, in __mutex_add_waiter() argument
208 debug_mutex_add_waiter(lock, waiter, current); in __mutex_add_waiter()
211 if (__mutex_waiter_is_first(lock, waiter)) in __mutex_add_waiter()
212 __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); in __mutex_add_waiter()
216 __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter) in __mutex_remove_waiter() argument
219 if (likely(list_empty(&lock->wait_list))) in __mutex_remove_waiter()
220 __mutex_clear_flag(lock, MUTEX_FLAGS); in __mutex_remove_waiter()
222 debug_mutex_remove_waiter(lock, waiter, current); in __mutex_remove_waiter()
231 static void __mutex_handoff(struct mutex *lock, struct task_struct *task) in __mutex_handoff() argument
233 unsigned long owner = atomic_long_read(&lock->owner); in __mutex_handoff()
246 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new)) in __mutex_handoff()
258 static void __sched __mutex_lock_slowpath(struct mutex *lock);
281 void __sched mutex_lock(struct mutex *lock) in mutex_lock() argument
285 if (!__mutex_trylock_fast(lock)) in mutex_lock()
286 __mutex_lock_slowpath(lock); in mutex_lock()
298 static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock) in __mutex_trylock_or_owner() argument
300 return __mutex_trylock_common(lock, false); in __mutex_trylock_or_owner()
304 bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, in ww_mutex_spin_on_owner() argument
309 ww = container_of(lock, struct ww_mutex, base); in ww_mutex_spin_on_owner()
332 if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS)) in ww_mutex_spin_on_owner()
339 if (waiter && !__mutex_waiter_is_first(lock, waiter)) in ww_mutex_spin_on_owner()
352 bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, in mutex_spin_on_owner() argument
359 while (__mutex_owner(lock) == owner) { in mutex_spin_on_owner()
378 if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) { in mutex_spin_on_owner()
392 static inline int mutex_can_spin_on_owner(struct mutex *lock) in mutex_can_spin_on_owner() argument
407 owner = __mutex_owner(lock); in mutex_can_spin_on_owner()
441 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, in mutex_optimistic_spin() argument
452 if (!mutex_can_spin_on_owner(lock)) in mutex_optimistic_spin()
460 if (!osq_lock(&lock->osq)) in mutex_optimistic_spin()
468 owner = __mutex_trylock_or_owner(lock); in mutex_optimistic_spin()
476 if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter)) in mutex_optimistic_spin()
489 osq_unlock(&lock->osq); in mutex_optimistic_spin()
496 osq_unlock(&lock->osq); in mutex_optimistic_spin()
517 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, in mutex_optimistic_spin() argument
524 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
537 void __sched mutex_unlock(struct mutex *lock) in mutex_unlock() argument
540 if (__mutex_unlock_fast(lock)) in mutex_unlock()
543 __mutex_unlock_slowpath(lock, _RET_IP_); in mutex_unlock()
558 void __sched ww_mutex_unlock(struct ww_mutex *lock) in ww_mutex_unlock() argument
560 __ww_mutex_unlock(lock); in ww_mutex_unlock()
561 mutex_unlock(&lock->base); in ww_mutex_unlock()
569 __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass, in __mutex_lock_common() argument
582 MUTEX_WARN_ON(lock->magic != lock); in __mutex_lock_common()
584 ww = container_of(lock, struct ww_mutex, base); in __mutex_lock_common()
603 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); in __mutex_lock_common()
605 trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); in __mutex_lock_common()
606 if (__mutex_trylock(lock) || in __mutex_lock_common()
607 mutex_optimistic_spin(lock, ww_ctx, NULL)) { in __mutex_lock_common()
609 lock_acquired(&lock->dep_map, ip); in __mutex_lock_common()
612 trace_contention_end(lock, 0); in __mutex_lock_common()
617 raw_spin_lock(&lock->wait_lock); in __mutex_lock_common()
621 if (__mutex_trylock(lock)) { in __mutex_lock_common()
623 __ww_mutex_check_waiters(lock, ww_ctx); in __mutex_lock_common()
628 debug_mutex_lock_common(lock, &waiter); in __mutex_lock_common()
633 lock_contended(&lock->dep_map, ip); in __mutex_lock_common()
637 __mutex_add_waiter(lock, &waiter, &lock->wait_list); in __mutex_lock_common()
643 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); in __mutex_lock_common()
649 trace_contention_begin(lock, LCB_F_MUTEX); in __mutex_lock_common()
659 if (__mutex_trylock(lock)) in __mutex_lock_common()
673 ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx); in __mutex_lock_common()
678 raw_spin_unlock(&lock->wait_lock); in __mutex_lock_common()
681 first = __mutex_waiter_is_first(lock, &waiter); in __mutex_lock_common()
689 if (__mutex_trylock_or_handoff(lock, first)) in __mutex_lock_common()
693 trace_contention_begin(lock, LCB_F_MUTEX | LCB_F_SPIN); in __mutex_lock_common()
694 if (mutex_optimistic_spin(lock, ww_ctx, &waiter)) in __mutex_lock_common()
696 trace_contention_begin(lock, LCB_F_MUTEX); in __mutex_lock_common()
699 raw_spin_lock(&lock->wait_lock); in __mutex_lock_common()
701 raw_spin_lock(&lock->wait_lock); in __mutex_lock_common()
711 !__mutex_waiter_is_first(lock, &waiter)) in __mutex_lock_common()
712 __ww_mutex_check_waiters(lock, ww_ctx); in __mutex_lock_common()
715 __mutex_remove_waiter(lock, &waiter); in __mutex_lock_common()
721 lock_acquired(&lock->dep_map, ip); in __mutex_lock_common()
722 trace_contention_end(lock, 0); in __mutex_lock_common()
727 raw_spin_unlock(&lock->wait_lock); in __mutex_lock_common()
733 __mutex_remove_waiter(lock, &waiter); in __mutex_lock_common()
735 trace_contention_end(lock, ret); in __mutex_lock_common()
736 raw_spin_unlock(&lock->wait_lock); in __mutex_lock_common()
738 mutex_release(&lock->dep_map, ip); in __mutex_lock_common()
744 __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, in __mutex_lock() argument
747 return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false); in __mutex_lock()
751 __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, in __ww_mutex_lock() argument
754 return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true); in __ww_mutex_lock()
797 mutex_lock_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_nested() argument
799 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); in mutex_lock_nested()
805 _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) in _mutex_lock_nest_lock() argument
807 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_); in _mutex_lock_nest_lock()
812 mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_killable_nested() argument
814 return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_); in mutex_lock_killable_nested()
819 mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_interruptible_nested() argument
821 return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_); in mutex_lock_interruptible_nested()
826 mutex_lock_io_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_io_nested() argument
833 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, in mutex_lock_io_nested()
840 ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_deadlock_injection() argument
854 ctx->contending_lock = lock; in ww_mutex_deadlock_injection()
856 ww_mutex_unlock(lock); in ww_mutex_deadlock_injection()
866 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock() argument
871 ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, in ww_mutex_lock()
874 return ww_mutex_deadlock_injection(lock, ctx); in ww_mutex_lock()
881 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock_interruptible() argument
886 ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, in ww_mutex_lock_interruptible()
890 return ww_mutex_deadlock_injection(lock, ctx); in ww_mutex_lock_interruptible()
901 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) in __mutex_unlock_slowpath() argument
907 mutex_release(&lock->dep_map, ip); in __mutex_unlock_slowpath()
916 owner = atomic_long_read(&lock->owner); in __mutex_unlock_slowpath()
924 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) { in __mutex_unlock_slowpath()
932 raw_spin_lock(&lock->wait_lock); in __mutex_unlock_slowpath()
933 debug_mutex_unlock(lock); in __mutex_unlock_slowpath()
934 if (!list_empty(&lock->wait_list)) { in __mutex_unlock_slowpath()
937 list_first_entry(&lock->wait_list, in __mutex_unlock_slowpath()
942 debug_mutex_wake_waiter(lock, waiter); in __mutex_unlock_slowpath()
947 __mutex_handoff(lock, next); in __mutex_unlock_slowpath()
949 raw_spin_unlock(&lock->wait_lock); in __mutex_unlock_slowpath()
960 __mutex_lock_killable_slowpath(struct mutex *lock);
963 __mutex_lock_interruptible_slowpath(struct mutex *lock);
977 int __sched mutex_lock_interruptible(struct mutex *lock) in mutex_lock_interruptible() argument
981 if (__mutex_trylock_fast(lock)) in mutex_lock_interruptible()
984 return __mutex_lock_interruptible_slowpath(lock); in mutex_lock_interruptible()
1001 int __sched mutex_lock_killable(struct mutex *lock) in mutex_lock_killable() argument
1005 if (__mutex_trylock_fast(lock)) in mutex_lock_killable()
1008 return __mutex_lock_killable_slowpath(lock); in mutex_lock_killable()
1022 void __sched mutex_lock_io(struct mutex *lock) in mutex_lock_io() argument
1027 mutex_lock(lock); in mutex_lock_io()
1033 __mutex_lock_slowpath(struct mutex *lock) in __mutex_lock_slowpath() argument
1035 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_); in __mutex_lock_slowpath()
1039 __mutex_lock_killable_slowpath(struct mutex *lock) in __mutex_lock_killable_slowpath() argument
1041 return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_); in __mutex_lock_killable_slowpath()
1045 __mutex_lock_interruptible_slowpath(struct mutex *lock) in __mutex_lock_interruptible_slowpath() argument
1047 return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_); in __mutex_lock_interruptible_slowpath()
1051 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in __ww_mutex_lock_slowpath() argument
1053 return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, in __ww_mutex_lock_slowpath()
1058 __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, in __ww_mutex_lock_interruptible_slowpath() argument
1061 return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, in __ww_mutex_lock_interruptible_slowpath()
1081 int __sched mutex_trylock(struct mutex *lock) in mutex_trylock() argument
1085 MUTEX_WARN_ON(lock->magic != lock); in mutex_trylock()
1087 locked = __mutex_trylock(lock); in mutex_trylock()
1089 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); in mutex_trylock()
1097 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock() argument
1101 if (__mutex_trylock_fast(&lock->base)) { in ww_mutex_lock()
1103 ww_mutex_set_context_fastpath(lock, ctx); in ww_mutex_lock()
1107 return __ww_mutex_lock_slowpath(lock, ctx); in ww_mutex_lock()
1112 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock_interruptible() argument
1116 if (__mutex_trylock_fast(&lock->base)) { in ww_mutex_lock_interruptible()
1118 ww_mutex_set_context_fastpath(lock, ctx); in ww_mutex_lock_interruptible()
1122 return __ww_mutex_lock_interruptible_slowpath(lock, ctx); in ww_mutex_lock_interruptible()
1136 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) in atomic_dec_and_mutex_lock() argument
1142 mutex_lock(lock); in atomic_dec_and_mutex_lock()
1145 mutex_unlock(lock); in atomic_dec_and_mutex_lock()