Lines Matching refs:lock

43 __mutex_init(struct mutex *lock, const char *name, struct lock_class_key *key)  in __mutex_init()  argument
45 atomic_long_set(&lock->owner, 0); in __mutex_init()
46 raw_spin_lock_init(&lock->wait_lock); in __mutex_init()
47 INIT_LIST_HEAD(&lock->wait_list); in __mutex_init()
49 osq_lock_init(&lock->osq); in __mutex_init()
52 debug_mutex_init(lock, name, key); in __mutex_init()
76 static inline struct task_struct *__mutex_owner(struct mutex *lock) in __mutex_owner() argument
78 return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS); in __mutex_owner()
86 bool mutex_is_locked(struct mutex *lock) in mutex_is_locked() argument
88 return __mutex_owner(lock) != NULL; in mutex_is_locked()
100 static inline struct task_struct *__mutex_trylock_common(struct mutex *lock, bool handoff) in __mutex_trylock_common() argument
104 owner = atomic_long_read(&lock->owner); in __mutex_trylock_common()
126 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &owner, task | flags)) { in __mutex_trylock_common()
139 static inline bool __mutex_trylock_or_handoff(struct mutex *lock, bool handoff) in __mutex_trylock_or_handoff() argument
141 return !__mutex_trylock_common(lock, handoff); in __mutex_trylock_or_handoff()
147 static inline bool __mutex_trylock(struct mutex *lock) in __mutex_trylock() argument
149 return !__mutex_trylock_common(lock, false); in __mutex_trylock()
163 static __always_inline bool __mutex_trylock_fast(struct mutex *lock) in __mutex_trylock_fast() argument
168 if (atomic_long_try_cmpxchg_acquire(&lock->owner, &zero, curr)) in __mutex_trylock_fast()
174 static __always_inline bool __mutex_unlock_fast(struct mutex *lock) in __mutex_unlock_fast() argument
178 return atomic_long_try_cmpxchg_release(&lock->owner, &curr, 0UL); in __mutex_unlock_fast()
182 static inline void __mutex_set_flag(struct mutex *lock, unsigned long flag) in __mutex_set_flag() argument
184 atomic_long_or(flag, &lock->owner); in __mutex_set_flag()
187 static inline void __mutex_clear_flag(struct mutex *lock, unsigned long flag) in __mutex_clear_flag() argument
189 atomic_long_andnot(flag, &lock->owner); in __mutex_clear_flag()
192 static inline bool __mutex_waiter_is_first(struct mutex *lock, struct mutex_waiter *waiter) in __mutex_waiter_is_first() argument
194 return list_first_entry(&lock->wait_list, struct mutex_waiter, list) == waiter; in __mutex_waiter_is_first()
202 __mutex_add_waiter(struct mutex *lock, struct mutex_waiter *waiter, in __mutex_add_waiter() argument
205 debug_mutex_add_waiter(lock, waiter, current); in __mutex_add_waiter()
208 if (__mutex_waiter_is_first(lock, waiter)) in __mutex_add_waiter()
209 __mutex_set_flag(lock, MUTEX_FLAG_WAITERS); in __mutex_add_waiter()
213 __mutex_remove_waiter(struct mutex *lock, struct mutex_waiter *waiter) in __mutex_remove_waiter() argument
216 if (likely(list_empty(&lock->wait_list))) in __mutex_remove_waiter()
217 __mutex_clear_flag(lock, MUTEX_FLAGS); in __mutex_remove_waiter()
219 debug_mutex_remove_waiter(lock, waiter, current); in __mutex_remove_waiter()
228 static void __mutex_handoff(struct mutex *lock, struct task_struct *task) in __mutex_handoff() argument
230 unsigned long owner = atomic_long_read(&lock->owner); in __mutex_handoff()
243 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, new)) in __mutex_handoff()
255 static void __sched __mutex_lock_slowpath(struct mutex *lock);
278 void __sched mutex_lock(struct mutex *lock) in mutex_lock() argument
282 if (!__mutex_trylock_fast(lock)) in mutex_lock()
283 __mutex_lock_slowpath(lock); in mutex_lock()
295 static inline struct task_struct *__mutex_trylock_or_owner(struct mutex *lock) in __mutex_trylock_or_owner() argument
297 return __mutex_trylock_common(lock, false); in __mutex_trylock_or_owner()
301 bool ww_mutex_spin_on_owner(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, in ww_mutex_spin_on_owner() argument
306 ww = container_of(lock, struct ww_mutex, base); in ww_mutex_spin_on_owner()
329 if (!waiter && (atomic_long_read(&lock->owner) & MUTEX_FLAG_WAITERS)) in ww_mutex_spin_on_owner()
336 if (waiter && !__mutex_waiter_is_first(lock, waiter)) in ww_mutex_spin_on_owner()
349 bool mutex_spin_on_owner(struct mutex *lock, struct task_struct *owner, in mutex_spin_on_owner() argument
356 while (__mutex_owner(lock) == owner) { in mutex_spin_on_owner()
376 if (ww_ctx && !ww_mutex_spin_on_owner(lock, ww_ctx, waiter)) { in mutex_spin_on_owner()
390 static inline int mutex_can_spin_on_owner(struct mutex *lock) in mutex_can_spin_on_owner() argument
405 owner = __mutex_owner(lock); in mutex_can_spin_on_owner()
445 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, in mutex_optimistic_spin() argument
456 if (!mutex_can_spin_on_owner(lock)) in mutex_optimistic_spin()
464 if (!osq_lock(&lock->osq)) in mutex_optimistic_spin()
472 owner = __mutex_trylock_or_owner(lock); in mutex_optimistic_spin()
480 if (!mutex_spin_on_owner(lock, owner, ww_ctx, waiter)) in mutex_optimistic_spin()
493 osq_unlock(&lock->osq); in mutex_optimistic_spin()
500 osq_unlock(&lock->osq); in mutex_optimistic_spin()
521 mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx, in mutex_optimistic_spin() argument
528 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip);
541 void __sched mutex_unlock(struct mutex *lock) in mutex_unlock() argument
544 if (__mutex_unlock_fast(lock)) in mutex_unlock()
547 __mutex_unlock_slowpath(lock, _RET_IP_); in mutex_unlock()
562 void __sched ww_mutex_unlock(struct ww_mutex *lock) in ww_mutex_unlock() argument
564 __ww_mutex_unlock(lock); in ww_mutex_unlock()
565 mutex_unlock(&lock->base); in ww_mutex_unlock()
573 __mutex_lock_common(struct mutex *lock, unsigned int state, unsigned int subclass, in __mutex_lock_common() argument
586 MUTEX_WARN_ON(lock->magic != lock); in __mutex_lock_common()
588 ww = container_of(lock, struct ww_mutex, base); in __mutex_lock_common()
607 mutex_acquire_nest(&lock->dep_map, subclass, 0, nest_lock, ip); in __mutex_lock_common()
609 if (__mutex_trylock(lock) || in __mutex_lock_common()
610 mutex_optimistic_spin(lock, ww_ctx, NULL)) { in __mutex_lock_common()
612 lock_acquired(&lock->dep_map, ip); in __mutex_lock_common()
619 raw_spin_lock(&lock->wait_lock); in __mutex_lock_common()
623 if (__mutex_trylock(lock)) { in __mutex_lock_common()
625 __ww_mutex_check_waiters(lock, ww_ctx); in __mutex_lock_common()
630 debug_mutex_lock_common(lock, &waiter); in __mutex_lock_common()
635 lock_contended(&lock->dep_map, ip); in __mutex_lock_common()
639 __mutex_add_waiter(lock, &waiter, &lock->wait_list); in __mutex_lock_common()
645 ret = __ww_mutex_add_waiter(&waiter, lock, ww_ctx); in __mutex_lock_common()
660 if (__mutex_trylock(lock)) in __mutex_lock_common()
674 ret = __ww_mutex_check_kill(lock, &waiter, ww_ctx); in __mutex_lock_common()
679 raw_spin_unlock(&lock->wait_lock); in __mutex_lock_common()
682 first = __mutex_waiter_is_first(lock, &waiter); in __mutex_lock_common()
690 if (__mutex_trylock_or_handoff(lock, first) || in __mutex_lock_common()
691 (first && mutex_optimistic_spin(lock, ww_ctx, &waiter))) in __mutex_lock_common()
694 raw_spin_lock(&lock->wait_lock); in __mutex_lock_common()
696 raw_spin_lock(&lock->wait_lock); in __mutex_lock_common()
706 !__mutex_waiter_is_first(lock, &waiter)) in __mutex_lock_common()
707 __ww_mutex_check_waiters(lock, ww_ctx); in __mutex_lock_common()
710 __mutex_remove_waiter(lock, &waiter); in __mutex_lock_common()
716 lock_acquired(&lock->dep_map, ip); in __mutex_lock_common()
721 raw_spin_unlock(&lock->wait_lock); in __mutex_lock_common()
727 __mutex_remove_waiter(lock, &waiter); in __mutex_lock_common()
729 raw_spin_unlock(&lock->wait_lock); in __mutex_lock_common()
731 mutex_release(&lock->dep_map, ip); in __mutex_lock_common()
737 __mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, in __mutex_lock() argument
740 return __mutex_lock_common(lock, state, subclass, nest_lock, ip, NULL, false); in __mutex_lock()
744 __ww_mutex_lock(struct mutex *lock, unsigned int state, unsigned int subclass, in __ww_mutex_lock() argument
747 return __mutex_lock_common(lock, state, subclass, NULL, ip, ww_ctx, true); in __ww_mutex_lock()
790 mutex_lock_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_nested() argument
792 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, subclass, NULL, _RET_IP_); in mutex_lock_nested()
798 _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest) in _mutex_lock_nest_lock() argument
800 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, nest, _RET_IP_); in _mutex_lock_nest_lock()
805 mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_killable_nested() argument
807 return __mutex_lock(lock, TASK_KILLABLE, subclass, NULL, _RET_IP_); in mutex_lock_killable_nested()
812 mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_interruptible_nested() argument
814 return __mutex_lock(lock, TASK_INTERRUPTIBLE, subclass, NULL, _RET_IP_); in mutex_lock_interruptible_nested()
819 mutex_lock_io_nested(struct mutex *lock, unsigned int subclass) in mutex_lock_io_nested() argument
826 __mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, in mutex_lock_io_nested()
833 ww_mutex_deadlock_injection(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_deadlock_injection() argument
847 ctx->contending_lock = lock; in ww_mutex_deadlock_injection()
849 ww_mutex_unlock(lock); in ww_mutex_deadlock_injection()
859 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock() argument
864 ret = __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, in ww_mutex_lock()
867 return ww_mutex_deadlock_injection(lock, ctx); in ww_mutex_lock()
874 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock_interruptible() argument
879 ret = __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, in ww_mutex_lock_interruptible()
883 return ww_mutex_deadlock_injection(lock, ctx); in ww_mutex_lock_interruptible()
894 static noinline void __sched __mutex_unlock_slowpath(struct mutex *lock, unsigned long ip) in __mutex_unlock_slowpath() argument
900 mutex_release(&lock->dep_map, ip); in __mutex_unlock_slowpath()
909 owner = atomic_long_read(&lock->owner); in __mutex_unlock_slowpath()
917 if (atomic_long_try_cmpxchg_release(&lock->owner, &owner, __owner_flags(owner))) { in __mutex_unlock_slowpath()
925 raw_spin_lock(&lock->wait_lock); in __mutex_unlock_slowpath()
926 debug_mutex_unlock(lock); in __mutex_unlock_slowpath()
927 if (!list_empty(&lock->wait_list)) { in __mutex_unlock_slowpath()
930 list_first_entry(&lock->wait_list, in __mutex_unlock_slowpath()
935 debug_mutex_wake_waiter(lock, waiter); in __mutex_unlock_slowpath()
940 __mutex_handoff(lock, next); in __mutex_unlock_slowpath()
942 raw_spin_unlock(&lock->wait_lock); in __mutex_unlock_slowpath()
953 __mutex_lock_killable_slowpath(struct mutex *lock);
956 __mutex_lock_interruptible_slowpath(struct mutex *lock);
970 int __sched mutex_lock_interruptible(struct mutex *lock) in mutex_lock_interruptible() argument
974 if (__mutex_trylock_fast(lock)) in mutex_lock_interruptible()
977 return __mutex_lock_interruptible_slowpath(lock); in mutex_lock_interruptible()
994 int __sched mutex_lock_killable(struct mutex *lock) in mutex_lock_killable() argument
998 if (__mutex_trylock_fast(lock)) in mutex_lock_killable()
1001 return __mutex_lock_killable_slowpath(lock); in mutex_lock_killable()
1015 void __sched mutex_lock_io(struct mutex *lock) in mutex_lock_io() argument
1020 mutex_lock(lock); in mutex_lock_io()
1026 __mutex_lock_slowpath(struct mutex *lock) in __mutex_lock_slowpath() argument
1028 __mutex_lock(lock, TASK_UNINTERRUPTIBLE, 0, NULL, _RET_IP_); in __mutex_lock_slowpath()
1032 __mutex_lock_killable_slowpath(struct mutex *lock) in __mutex_lock_killable_slowpath() argument
1034 return __mutex_lock(lock, TASK_KILLABLE, 0, NULL, _RET_IP_); in __mutex_lock_killable_slowpath()
1038 __mutex_lock_interruptible_slowpath(struct mutex *lock) in __mutex_lock_interruptible_slowpath() argument
1040 return __mutex_lock(lock, TASK_INTERRUPTIBLE, 0, NULL, _RET_IP_); in __mutex_lock_interruptible_slowpath()
1044 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in __ww_mutex_lock_slowpath() argument
1046 return __ww_mutex_lock(&lock->base, TASK_UNINTERRUPTIBLE, 0, in __ww_mutex_lock_slowpath()
1051 __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock, in __ww_mutex_lock_interruptible_slowpath() argument
1054 return __ww_mutex_lock(&lock->base, TASK_INTERRUPTIBLE, 0, in __ww_mutex_lock_interruptible_slowpath()
1074 int __sched mutex_trylock(struct mutex *lock) in mutex_trylock() argument
1078 MUTEX_WARN_ON(lock->magic != lock); in mutex_trylock()
1080 locked = __mutex_trylock(lock); in mutex_trylock()
1082 mutex_acquire(&lock->dep_map, 0, 1, _RET_IP_); in mutex_trylock()
1090 ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock() argument
1094 if (__mutex_trylock_fast(&lock->base)) { in ww_mutex_lock()
1096 ww_mutex_set_context_fastpath(lock, ctx); in ww_mutex_lock()
1100 return __ww_mutex_lock_slowpath(lock, ctx); in ww_mutex_lock()
1105 ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx) in ww_mutex_lock_interruptible() argument
1109 if (__mutex_trylock_fast(&lock->base)) { in ww_mutex_lock_interruptible()
1111 ww_mutex_set_context_fastpath(lock, ctx); in ww_mutex_lock_interruptible()
1115 return __ww_mutex_lock_interruptible_slowpath(lock, ctx); in ww_mutex_lock_interruptible()
1129 int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock) in atomic_dec_and_mutex_lock() argument
1135 mutex_lock(lock); in atomic_dec_and_mutex_lock()
1138 mutex_unlock(lock); in atomic_dec_and_mutex_lock()