2 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
28 * from BSDI $Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp Exp $
35 #include <sys/queue.h>
36 #include <sys/_lock.h>
41 * Lock classes. Each lock has a class which describes characteristics
42 * common to all types of locks of a given class.
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.
53 void (*lc_ddb_show)(struct lock_object *lock);
56 #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */
57 #define LC_SPINLOCK 0x00000002 /* Spin lock. */
58 #define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */
59 #define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */
60 #define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */
62 #define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */
63 #define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
64 #define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */
65 #define LO_QUIET 0x00040000 /* Don't log locking operations. */
66 #define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
67 #define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */
68 #define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */
69 #define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */
70 #define LO_ENROLLPEND 0x00800000 /* On the pending enroll list. */
71 #define LO_CLASSMASK 0x0f000000 /* Class index bitmask. */
74 * Lock classes are statically assigned an index into the gobal lock_classes
75 * array. Debugging code looks up the lock class for a given lock object
76 * by indexing the array.
78 #define LO_CLASSSHIFT 24
79 #define LO_CLASSINDEX(lock) ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT)
80 #define LOCK_CLASS(lock) (lock_classes[LO_CLASSINDEX((lock))])
81 #define LOCK_CLASS_MAX (LO_CLASSMASK >> LO_CLASSSHIFT)
83 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
84 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */
87 * Option flags passed to lock operations that witness also needs to know
88 * about or that are generic across all locks.
90 #define LOP_NEWORDER 0x00000001 /* Define a new lock order. */
91 #define LOP_QUIET 0x00000002 /* Don't log locking operations. */
92 #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */
93 #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */
94 #define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */
96 /* Flags passed to witness_assert. */
97 #define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */
98 #define LA_LOCKED 0x00000001 /* Lock is at least share locked. */
99 #define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */
100 #define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */
101 #define LA_RECURSED 0x00000008 /* Lock is recursed. */
102 #define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */
106 * Lock instances. A lock instance is the data associated with a lock while
107 * it is held by witness. For example, a lock instance will hold the
108 * recursion count of a lock. Lock instances are held in lists. Spin locks
109 * are held in a per-cpu list while sleep locks are held in per-thread list.
111 struct lock_instance {
112 struct lock_object *li_lock;
113 const char *li_file; /* File and line of last acquire. */
115 u_int li_flags; /* Recursion count and LI_* flags. */
119 * A simple list type used to build the list of locks held by a thread
120 * or CPU. We can't simply embed the list in struct lock_object since a
121 * lock may be held by more than one thread if it is a shared lock. Locks
122 * are added to the head of the list, so we fill up each list entry from
123 * "the back" logically. To ease some of the arithmetic, we actually fill
124 * in each list entry the normal way (childer[0] then children[1], etc.) but
125 * when we traverse the list we read children[count-1] as the first entry
126 * down to children[0] as the final entry.
128 #define LOCK_NCHILDREN 3
130 struct lock_list_entry {
131 struct lock_list_entry *ll_next;
132 struct lock_instance ll_children[LOCK_NCHILDREN];
137 * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
138 * then turn on LOCK_DEBUG. When this option is on, extra debugging
139 * facilities such as tracking the file and line number of lock operations
140 * are enabled. Also, mutex locking operations are not inlined to avoid
141 * bloat from all the extra debugging code. We also have to turn on all the
142 * calling conventions for this debugging code in modules so that modules can
143 * work with both debug and non-debug kernels.
145 #if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(MUTEX_PROFILING)
152 * In the LOCK_DEBUG case, use the filename and line numbers for debugging
153 * operations. Otherwise, use default values to avoid the unneeded bloat.
156 #define LOCK_FILE __FILE__
157 #define LOCK_LINE __LINE__
159 #define LOCK_FILE NULL
164 * Macros for KTR_LOCK tracing.
166 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
167 * lo - struct lock_object * for this lock
168 * flags - flags passed to the lock operation
169 * recurse - this locks recursion level (or 0 if class is not recursable)
170 * result - result of a try lock operation
174 #define LOCK_LOG_TEST(lo, flags) \
175 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
177 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \
178 if (LOCK_LOG_TEST((lo), (flags))) \
179 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d", \
180 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
181 (u_int)(recurse), (file), (line)); \
184 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \
185 if (LOCK_LOG_TEST((lo), (flags))) \
186 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\
187 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
188 (u_int)(result), (file), (line)); \
191 #define LOCK_LOG_INIT(lo, flags) do { \
192 if (LOCK_LOG_TEST((lo), (flags))) \
193 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \
194 LOCK_CLASS(lo)->lc_name, (lo)->lo_name); \
197 #define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags)
199 #define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED)
202 * Helpful macros for quickly coming up with assertions with informative
205 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
206 #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__)
207 #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line)
208 #define MPASS4(ex, what, file, line) \
209 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
211 extern struct lock_class lock_class_mtx_sleep;
212 extern struct lock_class lock_class_mtx_spin;
213 extern struct lock_class lock_class_sx;
214 extern struct lock_class lock_class_rw;
216 extern struct lock_class *lock_classes[];
218 void lock_init(struct lock_object *lock, struct lock_class *class,
219 const char *name, const char *type, int flags);
220 void lock_destroy(struct lock_object *lock);
221 void spinlock_enter(void);
222 void spinlock_exit(void);
223 void witness_init(struct lock_object *);
224 void witness_destroy(struct lock_object *);
225 int witness_defineorder(struct lock_object *, struct lock_object *);
226 void witness_checkorder(struct lock_object *, int, const char *, int);
227 void witness_lock(struct lock_object *, int, const char *, int);
228 void witness_upgrade(struct lock_object *, int, const char *, int);
229 void witness_downgrade(struct lock_object *, int, const char *, int);
230 void witness_unlock(struct lock_object *, int, const char *, int);
231 void witness_save(struct lock_object *, const char **, int *);
232 void witness_restore(struct lock_object *, const char *, int);
233 int witness_list_locks(struct lock_list_entry **);
234 int witness_warn(int, struct lock_object *, const char *, ...);
235 void witness_assert(struct lock_object *, int, const char *, int);
236 void witness_display_spinlock(struct lock_object *, struct thread *);
237 int witness_line(struct lock_object *);
238 const char *witness_file(struct lock_object *);
242 /* Flags for witness_warn(). */
243 #define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */
244 #define WARN_PANIC 0x02 /* Panic if check fails. */
245 #define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */
247 #define WITNESS_INIT(lock) \
250 #define WITNESS_DESTROY(lock) \
251 witness_destroy(lock)
253 #define WITNESS_CHECKORDER(lock, flags, file, line) \
254 witness_checkorder((lock), (flags), (file), (line))
256 #define WITNESS_DEFINEORDER(lock1, lock2) \
257 witness_defineorder((struct lock_object *)(lock1), \
258 (struct lock_object *)(lock2))
260 #define WITNESS_LOCK(lock, flags, file, line) \
261 witness_lock((lock), (flags), (file), (line))
263 #define WITNESS_UPGRADE(lock, flags, file, line) \
264 witness_upgrade((lock), (flags), (file), (line))
266 #define WITNESS_DOWNGRADE(lock, flags, file, line) \
267 witness_downgrade((lock), (flags), (file), (line))
269 #define WITNESS_UNLOCK(lock, flags, file, line) \
270 witness_unlock((lock), (flags), (file), (line))
272 #define WITNESS_CHECK(flags, lock, fmt, ...) \
273 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
275 #define WITNESS_WARN(flags, lock, fmt, ...) \
276 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
278 #define WITNESS_SAVE_DECL(n) \
279 const char * __CONCAT(n, __wf); \
280 int __CONCAT(n, __wl)
282 #define WITNESS_SAVE(lock, n) \
283 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
285 #define WITNESS_RESTORE(lock, n) \
286 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
288 #define WITNESS_FILE(lock) \
291 #define WITNESS_LINE(lock) \
295 #define WITNESS_INIT(lock)
296 #define WITNESS_DESTROY(lock)
297 #define WITNESS_DEFINEORDER(lock1, lock2) 0
298 #define WITNESS_CHECKORDER(lock, flags, file, line)
299 #define WITNESS_LOCK(lock, flags, file, line)
300 #define WITNESS_UPGRADE(lock, flags, file, line)
301 #define WITNESS_DOWNGRADE(lock, flags, file, line)
302 #define WITNESS_UNLOCK(lock, flags, file, line)
303 #define WITNESS_CHECK(flags, lock, fmt, ...) 0
304 #define WITNESS_WARN(flags, lock, fmt, ...)
305 #define WITNESS_SAVE_DECL(n)
306 #define WITNESS_SAVE(lock, n)
307 #define WITNESS_RESTORE(lock, n)
308 #define WITNESS_FILE(lock) ("?")
309 #define WITNESS_LINE(lock) (0)
313 * Helper macros to allow developers to add explicit lock order checks
314 * wherever they please without having to actually grab a lock to do so.
316 #define witness_check_mutex(m) \
317 WITNESS_CHECKORDER(&(m)->mtx_object, LOP_EXCLUSIVE, LOCK_FILE, \
320 #define witness_check_shared_sx(sx) \
321 WITNESS_CHECKORDER(&(sx)->sx_object, 0, LOCK_FILE, LOCK_LINE)
323 #define witness_check_exclusive_sx(sx) \
324 WITNESS_CHECKORDER(&(sx)->sx_object, LOP_EXCLUSIVE, LOCK_FILE, \
328 #endif /* _SYS_LOCK_H_ */