xref: /linux-6.15/include/linux/mutex.h (revision 3cf67d61)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
26053ee3bSIngo Molnar /*
36053ee3bSIngo Molnar  * Mutexes: blocking mutual exclusion locks
46053ee3bSIngo Molnar  *
56053ee3bSIngo Molnar  * started by Ingo Molnar:
66053ee3bSIngo Molnar  *
76053ee3bSIngo Molnar  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <[email protected]>
86053ee3bSIngo Molnar  *
96053ee3bSIngo Molnar  * This file contains the main data structure and API definitions.
106053ee3bSIngo Molnar  */
116053ee3bSIngo Molnar #ifndef __LINUX_MUTEX_H
126053ee3bSIngo Molnar #define __LINUX_MUTEX_H
136053ee3bSIngo Molnar 
14040a0a37SMaarten Lankhorst #include <asm/current.h>
156053ee3bSIngo Molnar #include <linux/list.h>
166053ee3bSIngo Molnar #include <linux/spinlock_types.h>
17ef5d4707SIngo Molnar #include <linux/lockdep.h>
1860063497SArun Sharma #include <linux/atomic.h>
19083986e8SHeiko Carstens #include <asm/processor.h>
2090631822SJason Low #include <linux/osq_lock.h>
213ca0ff57SPeter Zijlstra #include <linux/debug_locks.h>
2254da6a09SPeter Zijlstra #include <linux/cleanup.h>
23d84f3179SKent Overstreet #include <linux/mutex_types.h>
246053ee3bSIngo Molnar 
254cd47222SGeorge Stark struct device;
264cd47222SGeorge Stark 
27bb630f9fSThomas Gleixner #ifdef CONFIG_DEBUG_LOCK_ALLOC
28bb630f9fSThomas Gleixner # define __DEP_MAP_MUTEX_INITIALIZER(lockname)			\
29bb630f9fSThomas Gleixner 		, .dep_map = {					\
30bb630f9fSThomas Gleixner 			.name = #lockname,			\
31bb630f9fSThomas Gleixner 			.wait_type_inner = LD_WAIT_SLEEP,	\
32bb630f9fSThomas Gleixner 		}
33bb630f9fSThomas Gleixner #else
34bb630f9fSThomas Gleixner # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
35bb630f9fSThomas Gleixner #endif
36bb630f9fSThomas Gleixner 
376053ee3bSIngo Molnar #ifdef CONFIG_DEBUG_MUTEXES
383ca0ff57SPeter Zijlstra 
393ca0ff57SPeter Zijlstra # define __DEBUG_MUTEX_INITIALIZER(lockname)				\
403ca0ff57SPeter Zijlstra 	, .magic = &lockname
413ca0ff57SPeter Zijlstra 
423ca0ff57SPeter Zijlstra extern void mutex_destroy(struct mutex *lock);
433ca0ff57SPeter Zijlstra 
446053ee3bSIngo Molnar #else
453ca0ff57SPeter Zijlstra 
466053ee3bSIngo Molnar # define __DEBUG_MUTEX_INITIALIZER(lockname)
473ca0ff57SPeter Zijlstra 
mutex_destroy(struct mutex * lock)483ca0ff57SPeter Zijlstra static inline void mutex_destroy(struct mutex *lock) {}
493ca0ff57SPeter Zijlstra 
503ca0ff57SPeter Zijlstra #endif
513ca0ff57SPeter Zijlstra 
52ef5dc121SRandy Dunlap /**
53ef5dc121SRandy Dunlap  * mutex_init - initialize the mutex
54ef5dc121SRandy Dunlap  * @mutex: the mutex to be initialized
55ef5dc121SRandy Dunlap  *
56ef5dc121SRandy Dunlap  * Initialize the mutex to unlocked state.
57ef5dc121SRandy Dunlap  *
58ef5dc121SRandy Dunlap  * It is not allowed to initialize an already locked mutex.
59ef5dc121SRandy Dunlap  */
60ef5d4707SIngo Molnar #define mutex_init(mutex)						\
61ef5d4707SIngo Molnar do {									\
62ef5d4707SIngo Molnar 	static struct lock_class_key __key;				\
63ef5d4707SIngo Molnar 									\
64ef5d4707SIngo Molnar 	__mutex_init((mutex), #mutex, &__key);				\
65ef5d4707SIngo Molnar } while (0)
666053ee3bSIngo Molnar 
67e837d833SBart Van Assche /**
68e837d833SBart Van Assche  * mutex_init_with_key - initialize a mutex with a given lockdep key
69e837d833SBart Van Assche  * @mutex: the mutex to be initialized
70e837d833SBart Van Assche  * @key: the lockdep key to be associated with the mutex
71e837d833SBart Van Assche  *
72e837d833SBart Van Assche  * Initialize the mutex to the unlocked state.
73e837d833SBart Van Assche  *
74e837d833SBart Van Assche  * It is not allowed to initialize an already locked mutex.
75e837d833SBart Van Assche  */
76e837d833SBart Van Assche #define mutex_init_with_key(mutex, key) __mutex_init((mutex), #mutex, (key))
77e837d833SBart Van Assche 
7896f80528SBart Van Assche #ifndef CONFIG_PREEMPT_RT
796053ee3bSIngo Molnar #define __MUTEX_INITIALIZER(lockname) \
803ca0ff57SPeter Zijlstra 		{ .owner = ATOMIC_LONG_INIT(0) \
81ebf4c55cSThomas Gleixner 		, .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
826053ee3bSIngo Molnar 		, .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
83ef5d4707SIngo Molnar 		__DEBUG_MUTEX_INITIALIZER(lockname) \
84ef5d4707SIngo Molnar 		__DEP_MAP_MUTEX_INITIALIZER(lockname) }
856053ee3bSIngo Molnar 
866053ee3bSIngo Molnar #define DEFINE_MUTEX(mutexname) \
876053ee3bSIngo Molnar 	struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
886053ee3bSIngo Molnar 
89ef5d4707SIngo Molnar extern void __mutex_init(struct mutex *lock, const char *name,
90ef5d4707SIngo Molnar 			 struct lock_class_key *key);
916053ee3bSIngo Molnar 
9245f8bde0SRobert P. J. Day /**
936053ee3bSIngo Molnar  * mutex_is_locked - is the mutex locked
946053ee3bSIngo Molnar  * @lock: the mutex to be queried
956053ee3bSIngo Molnar  *
96db076befSYaowei Bai  * Returns true if the mutex is locked, false if unlocked.
976053ee3bSIngo Molnar  */
985f35d5a6SMukesh Ojha extern bool mutex_is_locked(struct mutex *lock);
996053ee3bSIngo Molnar 
100bb630f9fSThomas Gleixner #else /* !CONFIG_PREEMPT_RT */
101bb630f9fSThomas Gleixner /*
102bb630f9fSThomas Gleixner  * Preempt-RT variant based on rtmutexes.
103bb630f9fSThomas Gleixner  */
104bb630f9fSThomas Gleixner 
105bb630f9fSThomas Gleixner #define __MUTEX_INITIALIZER(mutexname)					\
106bb630f9fSThomas Gleixner {									\
107bb630f9fSThomas Gleixner 	.rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex)	\
108bb630f9fSThomas Gleixner 	__DEP_MAP_MUTEX_INITIALIZER(mutexname)				\
109bb630f9fSThomas Gleixner }
110bb630f9fSThomas Gleixner 
111bb630f9fSThomas Gleixner #define DEFINE_MUTEX(mutexname)						\
112bb630f9fSThomas Gleixner 	struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
113bb630f9fSThomas Gleixner 
114bb630f9fSThomas Gleixner extern void __mutex_rt_init(struct mutex *lock, const char *name,
115bb630f9fSThomas Gleixner 			    struct lock_class_key *key);
116bb630f9fSThomas Gleixner 
117bb630f9fSThomas Gleixner #define mutex_is_locked(l)	rt_mutex_base_is_locked(&(l)->rtmutex)
118bb630f9fSThomas Gleixner 
119bb630f9fSThomas Gleixner #define __mutex_init(mutex, name, key)			\
120bb630f9fSThomas Gleixner do {							\
121bb630f9fSThomas Gleixner 	rt_mutex_base_init(&(mutex)->rtmutex);		\
122bb630f9fSThomas Gleixner 	__mutex_rt_init((mutex), name, key);		\
123bb630f9fSThomas Gleixner } while (0)
124bb630f9fSThomas Gleixner 
125bb630f9fSThomas Gleixner #endif /* CONFIG_PREEMPT_RT */
126bb630f9fSThomas Gleixner 
1274cd47222SGeorge Stark #ifdef CONFIG_DEBUG_MUTEXES
1284cd47222SGeorge Stark 
1294cd47222SGeorge Stark int __devm_mutex_init(struct device *dev, struct mutex *lock);
1304cd47222SGeorge Stark 
1314cd47222SGeorge Stark #else
1324cd47222SGeorge Stark 
__devm_mutex_init(struct device * dev,struct mutex * lock)1334cd47222SGeorge Stark static inline int __devm_mutex_init(struct device *dev, struct mutex *lock)
1344cd47222SGeorge Stark {
1354cd47222SGeorge Stark 	/*
1364cd47222SGeorge Stark 	 * When CONFIG_DEBUG_MUTEXES is off mutex_destroy() is just a nop so
1374cd47222SGeorge Stark 	 * no really need to register it in the devm subsystem.
1384cd47222SGeorge Stark 	 */
1394cd47222SGeorge Stark 	return 0;
1404cd47222SGeorge Stark }
1414cd47222SGeorge Stark 
1424cd47222SGeorge Stark #endif
1434cd47222SGeorge Stark 
1444cd47222SGeorge Stark #define devm_mutex_init(dev, mutex)			\
1454cd47222SGeorge Stark ({							\
1464cd47222SGeorge Stark 	typeof(mutex) mutex_ = (mutex);			\
1474cd47222SGeorge Stark 							\
1484cd47222SGeorge Stark 	mutex_init(mutex_);				\
1494cd47222SGeorge Stark 	__devm_mutex_init(dev, mutex_);			\
1504cd47222SGeorge Stark })
1514cd47222SGeorge Stark 
1526053ee3bSIngo Molnar /*
15367a6de49SPeter Zijlstra  * See kernel/locking/mutex.c for detailed documentation of these APIs.
154387b1468SMauro Carvalho Chehab  * Also see Documentation/locking/mutex-design.rst.
1556053ee3bSIngo Molnar  */
156ef5d4707SIngo Molnar #ifdef CONFIG_DEBUG_LOCK_ALLOC
157ef5d4707SIngo Molnar extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
158e4c70a66SPeter Zijlstra extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
159040a0a37SMaarten Lankhorst 
16018d8362dSAndrew Morton extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
16118d8362dSAndrew Morton 					unsigned int subclass);
162ad776537SLiam R. Howlett extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
163ad776537SLiam R. Howlett 					unsigned int subclass);
1641460cb65STejun Heo extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
165e4564f79SPeter Zijlstra 
166e4564f79SPeter Zijlstra #define mutex_lock(lock) mutex_lock_nested(lock, 0)
167e4564f79SPeter Zijlstra #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
168ad776537SLiam R. Howlett #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
1691460cb65STejun Heo #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
170e4c70a66SPeter Zijlstra 
171e4c70a66SPeter Zijlstra #define mutex_lock_nest_lock(lock, nest_lock)				\
172e4c70a66SPeter Zijlstra do {									\
173e4c70a66SPeter Zijlstra 	typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\
174e4c70a66SPeter Zijlstra 	_mutex_lock_nest_lock(lock, &(nest_lock)->dep_map);		\
175e4c70a66SPeter Zijlstra } while (0)
176e4c70a66SPeter Zijlstra 
177ef5d4707SIngo Molnar #else
178ec701584SHarvey Harrison extern void mutex_lock(struct mutex *lock);
179ec701584SHarvey Harrison extern int __must_check mutex_lock_interruptible(struct mutex *lock);
180ec701584SHarvey Harrison extern int __must_check mutex_lock_killable(struct mutex *lock);
1811460cb65STejun Heo extern void mutex_lock_io(struct mutex *lock);
182e4564f79SPeter Zijlstra 
183ef5d4707SIngo Molnar # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
184d63a5a74SNeilBrown # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
185ad776537SLiam R. Howlett # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
186e4c70a66SPeter Zijlstra # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
187291da9d4SThomas Gleixner # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
188ef5d4707SIngo Molnar #endif
189ef5d4707SIngo Molnar 
1906053ee3bSIngo Molnar /*
1916053ee3bSIngo Molnar  * NOTE: mutex_trylock() follows the spin_trylock() convention,
1926053ee3bSIngo Molnar  *       not the down_trylock() convention!
193d98d38f2SArjan van de Ven  *
194d98d38f2SArjan van de Ven  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
1956053ee3bSIngo Molnar  */
196ec701584SHarvey Harrison extern int mutex_trylock(struct mutex *lock);
197ec701584SHarvey Harrison extern void mutex_unlock(struct mutex *lock);
198040a0a37SMaarten Lankhorst 
199a511e3f9SAndrew Morton extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
200b1fca266SEric Paris 
20154da6a09SPeter Zijlstra DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
202e4ab322fSPeter Zijlstra DEFINE_GUARD_COND(mutex, _try, mutex_trylock(_T))
203e4ab322fSPeter Zijlstra DEFINE_GUARD_COND(mutex, _intr, mutex_lock_interruptible(_T) == 0)
20454da6a09SPeter Zijlstra 
205*3cf67d61SMasami Hiramatsu (Google) extern unsigned long mutex_get_owner(struct mutex *lock);
206*3cf67d61SMasami Hiramatsu (Google) 
207e7224674STim Chen #endif /* __LINUX_MUTEX_H */
208