• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Mutexes: blocking mutual exclusion locks
4  *
5  * started by Ingo Molnar:
6  *
7  *  Copyright (C) 2004, 2005, 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
8  *
9  * This file contains the main data structure and API definitions.
10  */
11 #ifndef __LINUX_MUTEX_H
12 #define __LINUX_MUTEX_H
13 
14 #include <asm/current.h>
15 #include <linux/list.h>
16 #include <linux/spinlock_types.h>
17 #include <linux/lockdep.h>
18 #include <linux/atomic.h>
19 #include <asm/processor.h>
20 #include <linux/osq_lock.h>
21 #include <linux/debug_locks.h>
22 #include <linux/cleanup.h>
23 #include <linux/android_vendor.h>
24 
25 #ifdef CONFIG_DEBUG_LOCK_ALLOC
26 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)			\
27 		, .dep_map = {					\
28 			.name = #lockname,			\
29 			.wait_type_inner = LD_WAIT_SLEEP,	\
30 		}
31 #else
32 # define __DEP_MAP_MUTEX_INITIALIZER(lockname)
33 #endif
34 
35 #ifndef CONFIG_PREEMPT_RT
36 
37 /*
38  * Simple, straightforward mutexes with strict semantics:
39  *
40  * - only one task can hold the mutex at a time
41  * - only the owner can unlock the mutex
42  * - multiple unlocks are not permitted
43  * - recursive locking is not permitted
44  * - a mutex object must be initialized via the API
45  * - a mutex object must not be initialized via memset or copying
46  * - task may not exit with mutex held
47  * - memory areas where held locks reside must not be freed
48  * - held mutexes must not be reinitialized
49  * - mutexes may not be used in hardware or software interrupt
50  *   contexts such as tasklets and timers
51  *
52  * These semantics are fully enforced when DEBUG_MUTEXES is
53  * enabled. Furthermore, besides enforcing the above rules, the mutex
54  * debugging code also implements a number of additional features
55  * that make lock debugging easier and faster:
56  *
57  * - uses symbolic names of mutexes, whenever they are printed in debug output
58  * - point-of-acquire tracking, symbolic lookup of function names
59  * - list of all locks held in the system, printout of them
60  * - owner tracking
61  * - detects self-recursing locks and prints out all relevant info
62  * - detects multi-task circular deadlocks and prints out all affected
63  *   locks and tasks (and only those tasks)
64  */
65 struct mutex {
66 	atomic_long_t		owner;
67 	raw_spinlock_t		wait_lock;
68 #ifdef CONFIG_MUTEX_SPIN_ON_OWNER
69 	struct optimistic_spin_queue osq; /* Spinner MCS lock */
70 #endif
71 	struct list_head	wait_list;
72 #ifdef CONFIG_DEBUG_MUTEXES
73 	void			*magic;
74 #endif
75 #ifdef CONFIG_DEBUG_LOCK_ALLOC
76 	struct lockdep_map	dep_map;
77 #endif
78 	ANDROID_OEM_DATA_ARRAY(1, 2);
79 };
80 
81 #ifdef CONFIG_DEBUG_MUTEXES
82 
83 #define __DEBUG_MUTEX_INITIALIZER(lockname)				\
84 	, .magic = &lockname
85 
86 extern void mutex_destroy(struct mutex *lock);
87 
88 #else
89 
90 # define __DEBUG_MUTEX_INITIALIZER(lockname)
91 
mutex_destroy(struct mutex * lock)92 static inline void mutex_destroy(struct mutex *lock) {}
93 
94 #endif
95 
96 /**
97  * mutex_init - initialize the mutex
98  * @mutex: the mutex to be initialized
99  *
100  * Initialize the mutex to unlocked state.
101  *
102  * It is not allowed to initialize an already locked mutex.
103  */
104 #define mutex_init(mutex)						\
105 do {									\
106 	static struct lock_class_key __key;				\
107 									\
108 	__mutex_init((mutex), #mutex, &__key);				\
109 } while (0)
110 
111 #define __MUTEX_INITIALIZER(lockname) \
112 		{ .owner = ATOMIC_LONG_INIT(0) \
113 		, .wait_lock = __RAW_SPIN_LOCK_UNLOCKED(lockname.wait_lock) \
114 		, .wait_list = LIST_HEAD_INIT(lockname.wait_list) \
115 		__DEBUG_MUTEX_INITIALIZER(lockname) \
116 		__DEP_MAP_MUTEX_INITIALIZER(lockname) }
117 
118 #define DEFINE_MUTEX(mutexname) \
119 	struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
120 
121 extern void __mutex_init(struct mutex *lock, const char *name,
122 			 struct lock_class_key *key);
123 
124 /**
125  * mutex_is_locked - is the mutex locked
126  * @lock: the mutex to be queried
127  *
128  * Returns true if the mutex is locked, false if unlocked.
129  */
130 extern bool mutex_is_locked(struct mutex *lock);
131 
132 #else /* !CONFIG_PREEMPT_RT */
133 /*
134  * Preempt-RT variant based on rtmutexes.
135  */
136 #include <linux/rtmutex.h>
137 
138 struct mutex {
139 	struct rt_mutex_base	rtmutex;
140 #ifdef CONFIG_DEBUG_LOCK_ALLOC
141 	struct lockdep_map	dep_map;
142 #endif
143 };
144 
145 #define __MUTEX_INITIALIZER(mutexname)					\
146 {									\
147 	.rtmutex = __RT_MUTEX_BASE_INITIALIZER(mutexname.rtmutex)	\
148 	__DEP_MAP_MUTEX_INITIALIZER(mutexname)				\
149 }
150 
151 #define DEFINE_MUTEX(mutexname)						\
152 	struct mutex mutexname = __MUTEX_INITIALIZER(mutexname)
153 
154 extern void __mutex_rt_init(struct mutex *lock, const char *name,
155 			    struct lock_class_key *key);
156 extern int mutex_trylock(struct mutex *lock);
157 
mutex_destroy(struct mutex * lock)158 static inline void mutex_destroy(struct mutex *lock) { }
159 
160 #define mutex_is_locked(l)	rt_mutex_base_is_locked(&(l)->rtmutex)
161 
162 #define __mutex_init(mutex, name, key)			\
163 do {							\
164 	rt_mutex_base_init(&(mutex)->rtmutex);		\
165 	__mutex_rt_init((mutex), name, key);		\
166 } while (0)
167 
168 #define mutex_init(mutex)				\
169 do {							\
170 	static struct lock_class_key __key;		\
171 							\
172 	__mutex_init((mutex), #mutex, &__key);		\
173 } while (0)
174 #endif /* CONFIG_PREEMPT_RT */
175 
176 /*
177  * See kernel/locking/mutex.c for detailed documentation of these APIs.
178  * Also see Documentation/locking/mutex-design.rst.
179  */
180 #ifdef CONFIG_DEBUG_LOCK_ALLOC
181 extern void mutex_lock_nested(struct mutex *lock, unsigned int subclass);
182 extern void _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest_lock);
183 
184 extern int __must_check mutex_lock_interruptible_nested(struct mutex *lock,
185 					unsigned int subclass);
186 extern int __must_check mutex_lock_killable_nested(struct mutex *lock,
187 					unsigned int subclass);
188 extern void mutex_lock_io_nested(struct mutex *lock, unsigned int subclass);
189 
190 #define mutex_lock(lock) mutex_lock_nested(lock, 0)
191 #define mutex_lock_interruptible(lock) mutex_lock_interruptible_nested(lock, 0)
192 #define mutex_lock_killable(lock) mutex_lock_killable_nested(lock, 0)
193 #define mutex_lock_io(lock) mutex_lock_io_nested(lock, 0)
194 
195 #define mutex_lock_nest_lock(lock, nest_lock)				\
196 do {									\
197 	typecheck(struct lockdep_map *, &(nest_lock)->dep_map);	\
198 	_mutex_lock_nest_lock(lock, &(nest_lock)->dep_map);		\
199 } while (0)
200 
201 #else
202 extern void mutex_lock(struct mutex *lock);
203 extern int __must_check mutex_lock_interruptible(struct mutex *lock);
204 extern int __must_check mutex_lock_killable(struct mutex *lock);
205 extern void mutex_lock_io(struct mutex *lock);
206 
207 # define mutex_lock_nested(lock, subclass) mutex_lock(lock)
208 # define mutex_lock_interruptible_nested(lock, subclass) mutex_lock_interruptible(lock)
209 # define mutex_lock_killable_nested(lock, subclass) mutex_lock_killable(lock)
210 # define mutex_lock_nest_lock(lock, nest_lock) mutex_lock(lock)
211 # define mutex_lock_io_nested(lock, subclass) mutex_lock_io(lock)
212 #endif
213 
214 /*
215  * NOTE: mutex_trylock() follows the spin_trylock() convention,
216  *       not the down_trylock() convention!
217  *
218  * Returns 1 if the mutex has been acquired successfully, and 0 on contention.
219  */
220 extern int mutex_trylock(struct mutex *lock);
221 extern void mutex_unlock(struct mutex *lock);
222 
223 extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock);
224 
225 DEFINE_GUARD(mutex, struct mutex *, mutex_lock(_T), mutex_unlock(_T))
226 DEFINE_FREE(mutex, struct mutex *, if (_T) mutex_unlock(_T))
227 
228 #endif /* __LINUX_MUTEX_H */
229