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