1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Berkeley Software Design Inc's name may not be used to endorse or
15 * promote products derived from this software without specific prior
16 * written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * from BSDI Id: mutex.h,v 2.7.2.35 2000/04/27 03:10:26 cp
31 */
32
33 #ifndef _SYS_LOCK_H_
34 #define _SYS_LOCK_H_
35
36 #include <sys/queue.h>
37 #include <sys/_lock.h>
38 #include <sys/ktr_class.h>
39
40 struct lock_list_entry;
41 struct thread;
42
43 /*
44 * Lock classes. Each lock has a class which describes characteristics
45 * common to all types of locks of a given class.
46 *
47 * Spin locks in general must always protect against preemption, as it is
48 * an error to perform any type of context switch while holding a spin lock.
49 * Also, for an individual lock to be recursable, its class must allow
50 * recursion and the lock itself must explicitly allow recursion.
51 *
52 * The 'lc_ddb_show' function pointer is used to dump class-specific
53 * data for the 'show lock' DDB command. The 'lc_lock' and
54 * 'lc_unlock' function pointers are used in sleep(9) and cv_wait(9)
55 * to lock and unlock locks while blocking on a sleep queue. The
56 * return value of 'lc_unlock' will be passed to 'lc_lock' on resume
57 * to allow communication of state between the two routines.
58 */
59
60 struct lock_class {
61 const char *lc_name;
62 u_int lc_flags;
63 void (*lc_assert)(const struct lock_object *lock, int what);
64 void (*lc_ddb_show)(const struct lock_object *lock);
65 void (*lc_lock)(struct lock_object *lock, uintptr_t how);
66 int (*lc_owner)(const struct lock_object *lock,
67 struct thread **owner);
68 uintptr_t (*lc_unlock)(struct lock_object *lock);
69 };
70
71 #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */
72 #define LC_SPINLOCK 0x00000002 /* Spin lock. */
73 #define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */
74 #define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */
75 #define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */
76
77 #define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */
78 #define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
79 #define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */
80 #define LO_QUIET 0x00040000 /* Don't log locking operations. */
81 #define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
82 #define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */
83 #define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */
84 #define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */
85 #define LO_IS_VNODE 0x00800000 /* Tell WITNESS about a VNODE lock */
86 #define LO_CLASSMASK 0x0f000000 /* Class index bitmask. */
87 #define LO_NOPROFILE 0x10000000 /* Don't profile this lock */
88 #define LO_NEW 0x20000000 /* Don't check for double-init */
89
90 /*
91 * Lock classes are statically assigned an index into the gobal lock_classes
92 * array. Debugging code looks up the lock class for a given lock object
93 * by indexing the array.
94 */
95 #define LO_CLASSSHIFT 24
96 #define LO_CLASSINDEX(lock) ((((lock)->lo_flags) & LO_CLASSMASK) >> LO_CLASSSHIFT)
97 #define LOCK_CLASS(lock) (lock_classes[LO_CLASSINDEX((lock))])
98 #define LOCK_CLASS_MAX (LO_CLASSMASK >> LO_CLASSSHIFT)
99
100 /*
101 * Option flags passed to lock operations that witness also needs to know
102 * about or that are generic across all locks.
103 */
104 #define LOP_NEWORDER 0x00000001 /* Define a new lock order. */
105 #define LOP_QUIET 0x00000002 /* Don't log locking operations. */
106 #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */
107 #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */
108 #define LOP_DUPOK 0x00000010 /* Don't check for duplicate acquires */
109 #define LOP_NOSLEEP 0x00000020 /* Non-sleepable despite LO_SLEEPABLE */
110
111 /* Flags passed to witness_assert. */
112 #define LA_MASKASSERT 0x000000ff /* Mask for witness defined asserts. */
113 #define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */
114 #define LA_LOCKED 0x00000001 /* Lock is at least share locked. */
115 #define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */
116 #define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */
117 #define LA_RECURSED 0x00000008 /* Lock is recursed. */
118 #define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */
119
120 #ifdef _KERNEL
121 /*
122 * Macros for KTR_LOCK tracing.
123 *
124 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
125 * lo - struct lock_object * for this lock
126 * flags - flags passed to the lock operation
127 * recurse - this locks recursion level (or 0 if class is not recursable)
128 * result - result of a try lock operation
129 * file - file name
130 * line - line number
131 */
132 #if LOCK_DEBUG > 0
133 #define LOCK_LOG_TEST(lo, flags) \
134 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
135 #else
136 #define LOCK_LOG_TEST(lo, flags) 0
137 #endif
138
139 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \
140 if (LOCK_LOG_TEST((lo), (flags))) \
141 CTR6(KTR_LOCK, opname " (%s) %s %p r = %d at %s:%d", \
142 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
143 (lo), (u_int)(recurse), (file), (line)); \
144 } while (0)
145
146 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \
147 if (LOCK_LOG_TEST((lo), (flags))) \
148 CTR6(KTR_LOCK, "TRY_" opname " (%s) %s %p result=%d at %s:%d",\
149 LOCK_CLASS(lo)->lc_name, (lo)->lo_name, \
150 (lo), (u_int)(result), (file), (line)); \
151 } while (0)
152
153 #define LOCK_LOG_INIT(lo, flags) do { \
154 if (LOCK_LOG_TEST((lo), (flags))) \
155 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \
156 LOCK_CLASS(lo)->lc_name, (lo)->lo_name); \
157 } while (0)
158
159 #define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags)
160
161 #define lock_initialized(lo) ((lo)->lo_flags & LO_INITIALIZED)
162
163 extern struct lock_class lock_class_mtx_sleep;
164 extern struct lock_class lock_class_mtx_spin;
165 extern struct lock_class lock_class_sx;
166 extern struct lock_class lock_class_rw;
167 extern struct lock_class lock_class_rm;
168 extern struct lock_class lock_class_rm_sleepable;
169 extern struct lock_class lock_class_lockmgr;
170
171 extern struct lock_class *lock_classes[];
172
173 struct lock_delay_config {
174 u_short base;
175 u_short max;
176 };
177
178 extern struct lock_delay_config locks_delay;
179 extern u_short locks_delay_retries;
180 extern u_short locks_delay_loops;
181
182 struct lock_delay_arg {
183 struct lock_delay_config *config;
184 u_int delay;
185 u_int spin_cnt;
186 };
187
188 static inline void
lock_delay_arg_init(struct lock_delay_arg * la,struct lock_delay_config * lc)189 lock_delay_arg_init(struct lock_delay_arg *la, struct lock_delay_config *lc)
190 {
191 la->config = lc;
192 la->delay = lc->base;
193 la->spin_cnt = 0;
194 }
195
196 static inline void
lock_delay_arg_init_noadapt(struct lock_delay_arg * la)197 lock_delay_arg_init_noadapt(struct lock_delay_arg *la)
198 {
199 la->delay = 0;
200 la->spin_cnt = 0;
201 }
202
203 #define lock_delay_spin(n) do { \
204 u_int _i; \
205 \
206 for (_i = (n); _i > 0; _i--) \
207 cpu_spinwait(); \
208 } while (0)
209
210 #define LOCK_DELAY_SYSINIT(func) \
211 SYSINIT(func##_ld, SI_SUB_LOCK, SI_ORDER_ANY, func, NULL)
212
213 #define LOCK_DELAY_SYSINIT_DEFAULT(lc) \
214 SYSINIT(lock_delay_##lc##_ld, SI_SUB_LOCK, SI_ORDER_ANY, \
215 lock_delay_default_init, &lc)
216
217 void lock_init(struct lock_object *, struct lock_class *,
218 const char *, const char *, int);
219 void lock_destroy(struct lock_object *);
220 void lock_delay(struct lock_delay_arg *);
221 void lock_delay_default_init(struct lock_delay_config *);
222 void spinlock_enter(void);
223 void spinlock_exit(void);
224 void witness_init(struct lock_object *, const char *);
225 void witness_destroy(struct lock_object *);
226 int witness_defineorder(struct lock_object *, struct lock_object *);
227 void witness_checkorder(struct lock_object *, int, const char *, int,
228 struct lock_object *);
229 void witness_lock(struct lock_object *, int, const char *, int);
230 void witness_upgrade(struct lock_object *, int, const char *, int);
231 void witness_downgrade(struct lock_object *, int, const char *, int);
232 void witness_unlock(struct lock_object *, int, const char *, int);
233 void witness_save(struct lock_object *, const char **, int *);
234 void witness_restore(struct lock_object *, const char *, int);
235 int witness_list_locks(struct lock_list_entry **,
236 int (*)(const char *, ...));
237 int witness_warn(int, struct lock_object *, const char *, ...);
238 void witness_assert(const struct lock_object *, int, const char *, int);
239 int witness_is_owned(const struct lock_object *lock);
240 void witness_display_spinlock(struct lock_object *, struct thread *,
241 int (*)(const char *, ...));
242 int witness_line(struct lock_object *);
243 void witness_norelease(struct lock_object *);
244 void witness_releaseok(struct lock_object *);
245 const char *witness_file(struct lock_object *);
246 void witness_thread_exit(struct thread *);
247
248 #ifdef WITNESS
249 int witness_startup_count(void);
250 void witness_startup(void *);
251
252 /* Flags for witness_warn(). */
253 #define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */
254 #define WARN_PANIC 0x02 /* Panic if check fails. */
255 #define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */
256
257 #define WITNESS_INIT(lock, type) \
258 witness_init((lock), (type))
259
260 #define WITNESS_DESTROY(lock) \
261 witness_destroy(lock)
262
263 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock) \
264 witness_checkorder((lock), (flags), (file), (line), (interlock))
265
266 #define WITNESS_DEFINEORDER(lock1, lock2) \
267 witness_defineorder((struct lock_object *)(lock1), \
268 (struct lock_object *)(lock2))
269
270 #define WITNESS_LOCK(lock, flags, file, line) \
271 witness_lock((lock), (flags), (file), (line))
272
273 #define WITNESS_UPGRADE(lock, flags, file, line) \
274 witness_upgrade((lock), (flags), (file), (line))
275
276 #define WITNESS_DOWNGRADE(lock, flags, file, line) \
277 witness_downgrade((lock), (flags), (file), (line))
278
279 #define WITNESS_UNLOCK(lock, flags, file, line) \
280 witness_unlock((lock), (flags), (file), (line))
281
282 #define WITNESS_CHECK(flags, lock, fmt, ...) \
283 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
284
285 #define WITNESS_WARN(flags, lock, fmt, ...) \
286 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
287
288 #define WITNESS_SAVE_DECL(n) \
289 const char * __CONCAT(n, __wf); \
290 int __CONCAT(n, __wl)
291
292 #define WITNESS_SAVE(lock, n) \
293 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
294
295 #define WITNESS_RESTORE(lock, n) \
296 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
297
298 #define WITNESS_NORELEASE(lock) \
299 witness_norelease(&(lock)->lock_object)
300
301 #define WITNESS_RELEASEOK(lock) \
302 witness_releaseok(&(lock)->lock_object)
303
304 #define WITNESS_FILE(lock) \
305 witness_file(lock)
306
307 #define WITNESS_LINE(lock) \
308 witness_line(lock)
309
310 #else /* WITNESS */
311 #define WITNESS_INIT(lock, type) (void)0
312 #define WITNESS_DESTROY(lock) (void)0
313 #define WITNESS_DEFINEORDER(lock1, lock2) 0
314 #define WITNESS_CHECKORDER(lock, flags, file, line, interlock) (void)0
315 #define WITNESS_LOCK(lock, flags, file, line) (void)0
316 #define WITNESS_UPGRADE(lock, flags, file, line) (void)0
317 #define WITNESS_DOWNGRADE(lock, flags, file, line) (void)0
318 #define WITNESS_UNLOCK(lock, flags, file, line) (void)0
319 #define WITNESS_CHECK(flags, lock, fmt, ...) 0
320 #define WITNESS_WARN(flags, lock, fmt, ...) (void)0
321 #define WITNESS_SAVE_DECL(n) (void)0
322 #define WITNESS_SAVE(lock, n) (void)0
323 #define WITNESS_RESTORE(lock, n) (void)0
324 #define WITNESS_NORELEASE(lock) (void)0
325 #define WITNESS_RELEASEOK(lock) (void)0
326 #define WITNESS_FILE(lock) ("?")
327 #define WITNESS_LINE(lock) (0)
328 #endif /* WITNESS */
329
330 #endif /* _KERNEL */
331 #endif /* _SYS_LOCK_H_ */
332