FreeBSD/Linux Kernel Cross Reference
sys/sys/lock.h
1 /*-
2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
14 * written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
29 * $FreeBSD$
30 */
31
32 #ifndef _SYS_LOCK_H_
33 #define _SYS_LOCK_H_
34
35 #include <sys/queue.h>
36 #include <sys/_lock.h>
37
38 struct thread;
39
40 /*
41 * Lock classes. Each lock has a class which describes characteristics
42 * common to all types of locks of a given class.
43 *
44 * Spin locks in general must always protect against preemption, as it is
45 * an error to perform any type of context switch while holding a spin lock.
46 * Also, for an individual lock to be recursable, its class must allow
47 * recursion and the lock itself must explicitly allow recursion.
48 *
49 * The 'lc_ddb_show' function pointer is used to dump class-specific
50 * data for the 'show lock' DDB command. The 'lc_lock' and
51 * 'lc_unlock' function pointers are used in sleep(9) and cv_wait(9)
52 * to lock and unlock locks while blocking on a sleep queue. The
53 * return value of 'lc_unlock' will be passed to 'lc_lock' on resume
54 * to allow communication of state between the two routines.
55 */
56
57 struct lock_class {
58 const char *lc_name;
59 u_int lc_flags;
60 void (*lc_ddb_show)(struct lock_object *lock);
61 void (*lc_lock)(struct lock_object *lock, int how);
62 int (*lc_unlock)(struct lock_object *lock);
63 };
64
65 #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */
66 #define LC_SPINLOCK 0x00000002 /* Spin lock. */
67 #define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */
68 #define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */
69 #define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */
70
71 #define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */
72 #define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
73 #define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */
74 #define LO_QUIET 0x00040000 /* Don't log locking operations. */
75 #define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
76 #define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */
77 #define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */
78 #define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */
79 #define LO_ENROLLPEND 0x00800000 /* On the pending enroll list. */
80 #define LO_CLASSMASK 0x0f000000 /* Class index bitmask. */
81 #define LO_NOPROFILE 0x10000000 /* Don't profile this lock */
82
83 /*
84 * Lock classes are statically assigned an index into the gobal lock_classes
85 * array. Debugging code looks up the lock class for a given lock object
86 * by indexing the array.
87 */
88 #define LO_CLASSSHIFT 24
89 #define LO_CLASSINDEX(lock) ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT)
90 #define LOCK_CLASS(lock) (lock_classes[LO_CLASSINDEX((lock))])
91 #define LOCK_CLASS_MAX (LO_CLASSMASK >> LO_CLASSSHIFT)
92
93 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
94 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */
95
96 /*
97 * Option flags passed to lock operations that witness also needs to know
98 * about or that are generic across all locks.
99 */
100 #define LOP_NEWORDER 0x00000001 /* Define a new lock order. */
101 #define LOP_QUIET 0x00000002 /* Don't log locking operations. */
102 #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */
103 #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */
104 #define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */
105
106 /* Flags passed to witness_assert. */
107 #define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */
108 #define LA_LOCKED 0x00000001 /* Lock is at least share locked. */
109 #define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */
110 #define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */
111 #define LA_RECURSED 0x00000008 /* Lock is recursed. */
112 #define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */
113
114 #ifdef _KERNEL
115 /*
116 * Lock instances. A lock instance is the data associated with a lock while
117 * it is held by witness. For example, a lock instance will hold the
118 * recursion count of a lock. Lock instances are held in lists. Spin locks
119 * are held in a per-cpu list while sleep locks are held in per-thread list.
120 */
121 struct lock_instance {
122 struct lock_object *li_lock;
123 const char *li_file; /* File and line of last acquire. */
124 int li_line;
125 u_int li_flags; /* Recursion count and LI_* flags. */
126 };
127
128 /*
129 * A simple list type used to build the list of locks held by a thread
130 * or CPU. We can't simply embed the list in struct lock_object since a
131 * lock may be held by more than one thread if it is a shared lock. Locks
132 * are added to the head of the list, so we fill up each list entry from
133 * "the back" logically. To ease some of the arithmetic, we actually fill
134 * in each list entry the normal way (childer[0] then children[1], etc.) but
135 * when we traverse the list we read children[count-1] as the first entry
136 * down to children[0] as the final entry.
137 */
138 #define LOCK_NCHILDREN 3
139
140 struct lock_list_entry {
141 struct lock_list_entry *ll_next;
142 struct lock_instance ll_children[LOCK_NCHILDREN];
143 u_int ll_count;
144 };
145
146 /*
147 * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
148 * then turn on LOCK_DEBUG. When this option is on, extra debugging
149 * facilities such as tracking the file and line number of lock operations
150 * are enabled. Also, mutex locking operations are not inlined to avoid
151 * bloat from all the extra debugging code. We also have to turn on all the
152 * calling conventions for this debugging code in modules so that modules can
153 * work with both debug and non-debug kernels.
154 */
155 #if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(LOCK_PROFILING)
156 #define LOCK_DEBUG 1
157 #else
158 #define LOCK_DEBUG 0
159 #endif
160
161 /*
162 * In the LOCK_DEBUG case, use the filename and line numbers for debugging
163 * operations. Otherwise, use default values to avoid the unneeded bloat.
164 */
165 #if LOCK_DEBUG > 0
166 #define LOCK_FILE __FILE__
167 #define LOCK_LINE __LINE__
168 #else
169 #define LOCK_FILE NULL
170 #define LOCK_LINE 0
171 #endif
172
173 /*
174 * Macros for KTR_LOCK tracing.
175 *
176 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
177 * lo - struct lock_object * for this lock
178 * flags - flags passed to the lock operation
179 * recurse - this locks recursion level (or 0 if class is not recursable)
180 * result - result of a try lock operation
181 * file - file name
182 * line - line number
183 */
184 #define LOCK_LOG_TEST(lo, flags) \
185 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
186
187 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \
188 if (LOCK_LOG_TEST((lo), (flags))) \
189 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d", \
190 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
191 (u_int)(recurse), (file), (line)); \
192 } while (0)
193
194 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \
195 if (LOCK_LOG_TEST((lo), (flags))) \
196 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\
197 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
198 (u_int)(result), (file), (line)); \
199 } while (0)
200
201 #define LOCK_LOG_INIT(lo, flags) do { \
202 if (LOCK_LOG_TEST((lo), (flags))) \
203 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \
204 LOCK_CLASS(lo)->lc_name, (lo)->lo_name); \
205 } while (0)
206
207 #define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags)
208
209 #define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED)
210
211 /*
212 * Helpful macros for quickly coming up with assertions with informative
213 * panic messages.
214 */
215 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
216 #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__)
217 #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line)
218 #define MPASS4(ex, what, file, line) \
219 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
220
221 extern struct lock_class lock_class_mtx_sleep;
222 extern struct lock_class lock_class_mtx_spin;
223 extern struct lock_class lock_class_sx;
224 extern struct lock_class lock_class_rw;
225 extern struct lock_class lock_class_lockmgr;
226
227 extern struct lock_class *lock_classes[];
228
229 void lock_init(struct lock_object *, struct lock_class *,
230 const char *, const char *, int);
231 void lock_destroy(struct lock_object *);
232 void spinlock_enter(void);
233 void spinlock_exit(void);
234 void witness_init(struct lock_object *);
235 void witness_destroy(struct lock_object *);
236 int witness_defineorder(struct lock_object *, struct lock_object *);
237 void witness_checkorder(struct lock_object *, int, const char *, int);
238 void witness_lock(struct lock_object *, int, const char *, int);
239 void witness_upgrade(struct lock_object *, int, const char *, int);
240 void witness_downgrade(struct lock_object *, int, const char *, int);
241 void witness_unlock(struct lock_object *, int, const char *, int);
242 void witness_save(struct lock_object *, const char **, int *);
243 void witness_restore(struct lock_object *, const char *, int);
244 int witness_list_locks(struct lock_list_entry **);
245 int witness_warn(int, struct lock_object *, const char *, ...);
246 void witness_assert(struct lock_object *, int, const char *, int);
247 void witness_display_spinlock(struct lock_object *, struct thread *);
248 int witness_line(struct lock_object *);
249 const char *witness_file(struct lock_object *);
250
251 #ifdef WITNESS
252
253 /* Flags for witness_warn(). */
254 #define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */
255 #define WARN_PANIC 0x02 /* Panic if check fails. */
256 #define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */
257
258 #define WITNESS_INIT(lock) \
259 witness_init((lock))
260
261 #define WITNESS_DESTROY(lock) \
262 witness_destroy(lock)
263
264 #define WITNESS_CHECKORDER(lock, flags, file, line) \
265 witness_checkorder((lock), (flags), (file), (line))
266
267 #define WITNESS_DEFINEORDER(lock1, lock2) \
268 witness_defineorder((struct lock_object *)(lock1), \
269 (struct lock_object *)(lock2))
270
271 #define WITNESS_LOCK(lock, flags, file, line) \
272 witness_lock((lock), (flags), (file), (line))
273
274 #define WITNESS_UPGRADE(lock, flags, file, line) \
275 witness_upgrade((lock), (flags), (file), (line))
276
277 #define WITNESS_DOWNGRADE(lock, flags, file, line) \
278 witness_downgrade((lock), (flags), (file), (line))
279
280 #define WITNESS_UNLOCK(lock, flags, file, line) \
281 witness_unlock((lock), (flags), (file), (line))
282
283 #define WITNESS_CHECK(flags, lock, fmt, ...) \
284 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
285
286 #define WITNESS_WARN(flags, lock, fmt, ...) \
287 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
288
289 #define WITNESS_SAVE_DECL(n) \
290 const char * __CONCAT(n, __wf); \
291 int __CONCAT(n, __wl)
292
293 #define WITNESS_SAVE(lock, n) \
294 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
295
296 #define WITNESS_RESTORE(lock, n) \
297 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
298
299 #define WITNESS_FILE(lock) \
300 witness_file(lock)
301
302 #define WITNESS_LINE(lock) \
303 witness_line(lock)
304
305 #else /* WITNESS */
306 #define WITNESS_INIT(lock)
307 #define WITNESS_DESTROY(lock)
308 #define WITNESS_DEFINEORDER(lock1, lock2) 0
309 #define WITNESS_CHECKORDER(lock, flags, file, line)
310 #define WITNESS_LOCK(lock, flags, file, line)
311 #define WITNESS_UPGRADE(lock, flags, file, line)
312 #define WITNESS_DOWNGRADE(lock, flags, file, line)
313 #define WITNESS_UNLOCK(lock, flags, file, line)
314 #define WITNESS_CHECK(flags, lock, fmt, ...) 0
315 #define WITNESS_WARN(flags, lock, fmt, ...)
316 #define WITNESS_SAVE_DECL(n)
317 #define WITNESS_SAVE(lock, n)
318 #define WITNESS_RESTORE(lock, n)
319 #define WITNESS_FILE(lock) ("?")
320 #define WITNESS_LINE(lock) (0)
321 #endif /* WITNESS */
322
323 /*
324 * Helper macros to allow developers to add explicit lock order checks
325 * wherever they please without having to actually grab a lock to do so.
326 */
327 #define witness_check(l) \
328 WITNESS_CHECKORDER(&(l)->lock_object, LOP_EXCLUSIVE, LOCK_FILE, \
329 LOCK_LINE)
330
331 #define witness_check_shared(l) \
332 WITNESS_CHECKORDER(&(l)->lock_object, 0, LOCK_FILE, LOCK_LINE)
333
334 #endif /* _KERNEL */
335 #endif /* _SYS_LOCK_H_ */
Cache object: 1a090205f2b0264a09e30db4509fc680
|