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
35 #include <sys/queue.h>
36 #include <sys/_lock.h>
38 struct lock_list_entry;
42 * Lock classes. Each lock has a class which describes characteristics
43 * common to all types of locks of a given class.
45 * Spin locks in general must always protect against preemption, as it is
46 * an error to perform any type of context switch while holding a spin lock.
47 * Also, for an individual lock to be recursable, its class must allow
48 * recursion and the lock itself must explicitly allow recursion.
50 * The 'lc_ddb_show' function pointer is used to dump class-specific
51 * data for the 'show lock' DDB command. The 'lc_lock' and
52 * 'lc_unlock' function pointers are used in sleep(9) and cv_wait(9)
53 * to lock and unlock locks while blocking on a sleep queue. The
54 * return value of 'lc_unlock' will be passed to 'lc_lock' on resume
55 * to allow communication of state between the two routines.
61 void (*lc_assert)(const struct lock_object *lock, int what);
62 void (*lc_ddb_show)(const struct lock_object *lock);
63 void (*lc_lock)(struct lock_object *lock, uintptr_t how);
64 int (*lc_owner)(const struct lock_object *lock,
65 struct thread **owner);
66 uintptr_t (*lc_unlock)(struct lock_object *lock);
69 #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */
70 #define LC_SPINLOCK 0x00000002 /* Spin lock. */
71 #define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */
72 #define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */
73 #define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */
75 #define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */
76 #define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
77 #define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */
78 #define LO_QUIET 0x00040000 /* Don't log locking operations. */
79 #define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
80 #define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */
81 #define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */
82 #define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */
83 #define LO_IS_VNODE 0x00800000 /* Tell WITNESS about a VNODE lock */
84 #define LO_CLASSMASK 0x0f000000 /* Class index bitmask. */
85 #define LO_NOPROFILE 0x10000000 /* Don't profile this lock */
86 #define LO_NEW 0x20000000 /* Don't check for double-init */
89 * Lock classes are statically assigned an index into the gobal lock_classes
90 * array. Debugging code looks up the lock class for a given lock object
91 * by indexing the array.
93 #define LO_CLASSSHIFT 24
94 #define LO_CLASSINDEX(lock) ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT)
95 #define LOCK_CLASS(lock) (lock_classes[LO_CLASSINDEX((lock))])
96 #define LOCK_CLASS_MAX (LO_CLASSMASK >> LO_CLASSSHIFT)
99 * Option flags passed to lock operations that witness also needs to know
100 * about or that are generic across all locks.
102 #define LOP_NEWORDER 0x00000001 /* Define a new lock order. */
103 #define LOP_QUIET 0x00000002 /* Don't log locking operations. */
104 #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */
105 #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */
106 #define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */
108 /* Flags passed to witness_assert. */
109 #define LA_MASKASSERT 0x000000ff /* Mask for witness defined asserts. */
110 #define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */
111 #define LA_LOCKED 0x00000001 /* Lock is at least share locked. */
112 #define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */
113 #define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */
114 #define LA_RECURSED 0x00000008 /* Lock is recursed. */
115 #define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */
119 * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
120 * then turn on LOCK_DEBUG. When this option is on, extra debugging
121 * facilities such as tracking the file and line number of lock operations
122 * are enabled. Also, mutex locking operations are not inlined to avoid
123 * bloat from all the extra debugging code. We also have to turn on all the
124 * calling conventions for this debugging code in modules so that modules can
125 * work with both debug and non-debug kernels.
127 #if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(LOCK_PROFILING)
134 * In the LOCK_DEBUG case, use the filename and line numbers for debugging
135 * operations. Otherwise, use default values to avoid the unneeded bloat.
138 #define LOCK_FILE __FILE__
139 #define LOCK_LINE __LINE__
141 #define LOCK_FILE NULL
146 * Macros for KTR_LOCK tracing.
148 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
149 * lo - struct lock_object * for this lock
150 * flags - flags passed to the lock operation
151 * recurse - this locks recursion level (or 0 if class is not recursable)
152 * result - result of a try lock operation
156 #define LOCK_LOG_TEST(lo, flags) \
157 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
159 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \
160 if (LOCK_LOG_TEST((lo), (flags))) \
161 CTR6(KTR_LOCK, opname " (%s) %s %p r = %d at %s:%d", \
162 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
163 (lo), (u_int)(recurse), (file), (line)); \
166 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \
167 if (LOCK_LOG_TEST((lo), (flags))) \
168 CTR6(KTR_LOCK, "TRY_" opname " (%s) %s %p result=%d at %s:%d",\
169 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
170 (lo), (u_int)(result), (file), (line)); \
173 #define LOCK_LOG_INIT(lo, flags) do { \
174 if (LOCK_LOG_TEST((lo), (flags))) \
175 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \
176 LOCK_CLASS(lo)->lc_name, (lo)->lo_name); \
179 #define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags)
181 #define lock_initalized(lo) ((lo)->lo_flags & LO_INITIALIZED)
184 * Helpful macros for quickly coming up with assertions with informative
187 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
188 #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__)
189 #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line)
190 #define MPASS4(ex, what, file, line) \
191 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
193 extern struct lock_class lock_class_mtx_sleep;
194 extern struct lock_class lock_class_mtx_spin;
195 extern struct lock_class lock_class_sx;
196 extern struct lock_class lock_class_rw;
197 extern struct lock_class lock_class_rm;
198 extern struct lock_class lock_class_rm_sleepable;
199 extern struct lock_class lock_class_lockmgr;
201 extern struct lock_class *lock_classes[];
203 struct lock_delay_config {
210 struct lock_delay_arg {
211 struct lock_delay_config *config;
217 lock_delay_arg_init(struct lock_delay_arg *la, struct lock_delay_config *lc) {
223 #define LOCK_DELAY_SYSINIT(func) \
224 SYSINIT(func##_ld, SI_SUB_LOCK, SI_ORDER_ANY, func, NULL)
226 void lock_init(struct lock_object *, struct lock_class *,
227 const char *, const char *, int);
228 void lock_destroy(struct lock_object *);
229 void lock_delay(struct lock_delay_arg *);
230 void spinlock_enter(void);
231 void spinlock_exit(void);
232 void witness_init(struct lock_object *, const char *);
233 void witness_destroy(struct lock_object *);
234 int witness_defineorder(struct lock_object *, struct lock_object *);
235 void witness_checkorder(struct lock_object *, int, const char *, int,
236 struct lock_object *);
237 void witness_lock(struct lock_object *, int, const char *, int);
238 void witness_upgrade(struct lock_object *, int, const char *, int);
239 void witness_downgrade(struct lock_object *, int, const char *, int);
240 void witness_unlock(struct lock_object *, int, const char *, int);
241 void witness_save(struct lock_object *, const char **, int *);
242 void witness_restore(struct lock_object *, const char *, int);
243 int witness_list_locks(struct lock_list_entry **,
244 int (*)(const char *, ...));
245 int witness_warn(int, struct lock_object *, const char *, ...);
246 void witness_assert(const struct lock_object *, int, const char *, int);
247 void witness_display_spinlock(struct lock_object *, struct thread *,
248 int (*)(const char *, ...));
249 int witness_line(struct lock_object *);
250 void witness_norelease(struct lock_object *);
251 void witness_releaseok(struct lock_object *);
252 const char *witness_file(struct lock_object *);
253 void witness_thread_exit(struct thread *);
257 /* Flags for witness_warn(). */
258 #define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */
259 #define WARN_PANIC 0x02 /* Panic if check fails. */
260 #define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */
262 #define WITNESS_INIT(lock, type) \
263 witness_init((lock), (type))
265 #define WITNESS_DESTROY(lock) \
266 witness_destroy(lock)
268 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock) \
269 witness_checkorder((lock), (flags), (file), (line), (interlock))
271 #define WITNESS_DEFINEORDER(lock1, lock2) \
272 witness_defineorder((struct lock_object *)(lock1), \
273 (struct lock_object *)(lock2))
275 #define WITNESS_LOCK(lock, flags, file, line) \
276 witness_lock((lock), (flags), (file), (line))
278 #define WITNESS_UPGRADE(lock, flags, file, line) \
279 witness_upgrade((lock), (flags), (file), (line))
281 #define WITNESS_DOWNGRADE(lock, flags, file, line) \
282 witness_downgrade((lock), (flags), (file), (line))
284 #define WITNESS_UNLOCK(lock, flags, file, line) \
285 witness_unlock((lock), (flags), (file), (line))
287 #define WITNESS_CHECK(flags, lock, fmt, ...) \
288 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
290 #define WITNESS_WARN(flags, lock, fmt, ...) \
291 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
293 #define WITNESS_SAVE_DECL(n) \
294 const char * __CONCAT(n, __wf); \
295 int __CONCAT(n, __wl)
297 #define WITNESS_SAVE(lock, n) \
298 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
300 #define WITNESS_RESTORE(lock, n) \
301 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
303 #define WITNESS_NORELEASE(lock) \
304 witness_norelease(&(lock)->lock_object)
306 #define WITNESS_RELEASEOK(lock) \
307 witness_releaseok(&(lock)->lock_object)
309 #define WITNESS_FILE(lock) \
312 #define WITNESS_LINE(lock) \
316 #define WITNESS_INIT(lock, type) (void)0
317 #define WITNESS_DESTROY(lock) (void)0
318 #define WITNESS_DEFINEORDER(lock1, lock2) 0
319 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock) (void)0
320 #define WITNESS_LOCK(lock, flags, file, line) (void)0
321 #define WITNESS_UPGRADE(lock, flags, file, line) (void)0
322 #define WITNESS_DOWNGRADE(lock, flags, file, line) (void)0
323 #define WITNESS_UNLOCK(lock, flags, file, line) (void)0
324 #define WITNESS_CHECK(flags, lock, fmt, ...) 0
325 #define WITNESS_WARN(flags, lock, fmt, ...) (void)0
326 #define WITNESS_SAVE_DECL(n) (void)0
327 #define WITNESS_SAVE(lock, n) (void)0
328 #define WITNESS_RESTORE(lock, n) (void)0
329 #define WITNESS_NORELEASE(lock) (void)0
330 #define WITNESS_RELEASEOK(lock) (void)0
331 #define WITNESS_FILE(lock) ("?")
332 #define WITNESS_LINE(lock) (0)
336 #endif /* _SYS_LOCK_H_ */