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: releng/5.2/sys/sys/lock.h 120246 2003-09-19 22:01:56Z sam $
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
50 struct lock_class {
51 const char *lc_name;
52 u_int lc_flags;
53 };
54
55 #define LC_SLEEPLOCK 0x00000001 /* Sleep lock. */
56 #define LC_SPINLOCK 0x00000002 /* Spin lock. */
57 #define LC_SLEEPABLE 0x00000004 /* Sleeping allowed with this lock. */
58 #define LC_RECURSABLE 0x00000008 /* Locks of this type may recurse. */
59 #define LC_UPGRADABLE 0x00000010 /* Upgrades and downgrades permitted. */
60
61 #define LO_CLASSFLAGS 0x0000ffff /* Class specific flags. */
62 #define LO_INITIALIZED 0x00010000 /* Lock has been initialized. */
63 #define LO_WITNESS 0x00020000 /* Should witness monitor this lock. */
64 #define LO_QUIET 0x00040000 /* Don't log locking operations. */
65 #define LO_RECURSABLE 0x00080000 /* Lock may recurse. */
66 #define LO_SLEEPABLE 0x00100000 /* Lock may be held while sleeping. */
67 #define LO_UPGRADABLE 0x00200000 /* Lock may be upgraded/downgraded. */
68 #define LO_DUPOK 0x00400000 /* Don't check for duplicate acquires */
69
70 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
71 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */
72
73 /*
74 * Option flags passed to lock operations that witness also needs to know
75 * about or that are generic across all locks.
76 */
77 #define LOP_QUIET 0x00000002 /* Don't log locking operations. */
78 #define LOP_TRYLOCK 0x00000004 /* Don't check lock order. */
79 #define LOP_EXCLUSIVE 0x00000008 /* Exclusive lock. */
80
81 /* Flags passed to witness_assert. */
82 #define LA_UNLOCKED 0x00000000 /* Lock is unlocked. */
83 #define LA_LOCKED 0x00000001 /* Lock is at least share locked. */
84 #define LA_SLOCKED 0x00000002 /* Lock is exactly share locked. */
85 #define LA_XLOCKED 0x00000004 /* Lock is exclusively locked. */
86 #define LA_RECURSED 0x00000008 /* Lock is recursed. */
87 #define LA_NOTRECURSED 0x00000010 /* Lock is not recursed. */
88
89 #ifdef _KERNEL
90 /*
91 * Lock instances. A lock instance is the data associated with a lock while
92 * it is held by witness. For example, a lock instance will hold the
93 * recursion count of a lock. Lock instances are held in lists. Spin locks
94 * are held in a per-cpu list while sleep locks are held in per-process list.
95 */
96 struct lock_instance {
97 struct lock_object *li_lock;
98 const char *li_file; /* File and line of last acquire. */
99 int li_line;
100 u_int li_flags; /* Recursion count and LI_* flags. */
101 };
102
103 /*
104 * A simple list type used to build the list of locks held by a process
105 * or CPU. We can't simply embed the list in struct lock_object since a
106 * lock may be held by more than one process if it is a shared lock. Locks
107 * are added to the head of the list, so we fill up each list entry from
108 * "the back" logically. To ease some of the arithmetic, we actually fill
109 * in each list entry the normal way (childer[0] then children[1], etc.) but
110 * when we traverse the list we read children[count-1] as the first entry
111 * down to children[0] as the final entry.
112 */
113 #define LOCK_NCHILDREN 3
114
115 struct lock_list_entry {
116 struct lock_list_entry *ll_next;
117 struct lock_instance ll_children[LOCK_NCHILDREN];
118 u_int ll_count;
119 };
120
121 /*
122 * If any of WITNESS, INVARIANTS, or KTR_LOCK KTR tracing has been enabled,
123 * then turn on LOCK_DEBUG. When this option is on, extra debugging
124 * facilities such as tracking the file and line number of lock operations
125 * are enabled. Also, mutex locking operations are not inlined to avoid
126 * bloat from all the extra debugging code. We also have to turn on all the
127 * calling conventions for this debugging code in modules so that modules can
128 * work with both debug and non-debug kernels.
129 */
130 #if defined(KLD_MODULE) || defined(WITNESS) || defined(INVARIANTS) || defined(INVARIANT_SUPPORT) || defined(KTR) || defined(MUTEX_PROFILING)
131 #define LOCK_DEBUG 1
132 #else
133 #define LOCK_DEBUG 0
134 #endif
135
136 /*
137 * In the LOCK_DEBUG case, use the filename and line numbers for debugging
138 * operations. Otherwise, use default values to avoid the unneeded bloat.
139 */
140 #if LOCK_DEBUG > 0
141 #define LOCK_FILE __FILE__
142 #define LOCK_LINE __LINE__
143 #else
144 #define LOCK_FILE NULL
145 #define LOCK_LINE 0
146 #endif
147
148 /*
149 * Macros for KTR_LOCK tracing.
150 *
151 * opname - name of this operation (LOCK/UNLOCK/SLOCK, etc.)
152 * lo - struct lock_object * for this lock
153 * flags - flags passed to the lock operation
154 * recurse - this locks recursion level (or 0 if class is not recursable)
155 * result - result of a try lock operation
156 * file - file name
157 * line - line number
158 */
159 #define LOCK_LOG_TEST(lo, flags) \
160 (((flags) & LOP_QUIET) == 0 && ((lo)->lo_flags & LO_QUIET) == 0)
161
162 #define LOCK_LOG_LOCK(opname, lo, flags, recurse, file, line) do { \
163 if (LOCK_LOG_TEST((lo), (flags))) \
164 CTR5(KTR_LOCK, opname " (%s) %s r = %d at %s:%d", \
165 (lo)->lo_class->lc_name, (lo)->lo_name, \
166 (u_int)(recurse), (file), (line)); \
167 } while (0)
168
169 #define LOCK_LOG_TRY(opname, lo, flags, result, file, line) do { \
170 if (LOCK_LOG_TEST((lo), (flags))) \
171 CTR5(KTR_LOCK, "TRY_" opname " (%s) %s result=%d at %s:%d",\
172 (lo)->lo_class->lc_name, (lo)->lo_name, \
173 (u_int)(result), (file), (line)); \
174 } while (0)
175
176 #define LOCK_LOG_INIT(lo, flags) do { \
177 if (LOCK_LOG_TEST((lo), (flags))) \
178 CTR4(KTR_LOCK, "%s: %p (%s) %s", __func__, (lo), \
179 (lo)->lo_class->lc_name, (lo)->lo_name); \
180 } while (0)
181
182 #define LOCK_LOG_DESTROY(lo, flags) LOCK_LOG_INIT(lo, flags)
183
184 /*
185 * Helpful macros for quickly coming up with assertions with informative
186 * panic messages.
187 */
188 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
189 #define MPASS2(ex, what) MPASS4(ex, what, __FILE__, __LINE__)
190 #define MPASS3(ex, file, line) MPASS4(ex, #ex, file, line)
191 #define MPASS4(ex, what, file, line) \
192 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
193
194 extern struct lock_class lock_class_mtx_sleep;
195 extern struct lock_class lock_class_mtx_spin;
196 extern struct lock_class lock_class_sx;
197
198 void witness_init(struct lock_object *);
199 void witness_destroy(struct lock_object *);
200 void witness_lock(struct lock_object *, int, const char *, int);
201 void witness_upgrade(struct lock_object *, int, const char *, int);
202 void witness_downgrade(struct lock_object *, int, const char *, int);
203 void witness_unlock(struct lock_object *, int, const char *, int);
204 void witness_save(struct lock_object *, const char **, int *);
205 void witness_restore(struct lock_object *, const char *, int);
206 int witness_list_locks(struct lock_list_entry **);
207 int witness_warn(int, struct lock_object *, const char *, ...);
208 void witness_assert(struct lock_object *, int, const char *, int);
209 void witness_display_spinlock(struct lock_object *, struct thread *);
210 int witness_line(struct lock_object *);
211 const char *witness_file(struct lock_object *);
212
213 #ifdef WITNESS
214
215 /* Flags for witness_warn(). */
216 #define WARN_GIANTOK 0x01 /* Giant is exempt from this check. */
217 #define WARN_PANIC 0x02 /* Panic if check fails. */
218 #define WARN_SLEEPOK 0x04 /* Sleepable locks are exempt from check. */
219
220 #define WITNESS_INIT(lock) \
221 witness_init((lock))
222
223 #define WITNESS_DESTROY(lock) \
224 witness_destroy(lock)
225
226 #define WITNESS_LOCK(lock, flags, file, line) \
227 witness_lock((lock), (flags), (file), (line))
228
229 #define WITNESS_UPGRADE(lock, flags, file, line) \
230 witness_upgrade((lock), (flags), (file), (line))
231
232 #define WITNESS_DOWNGRADE(lock, flags, file, line) \
233 witness_downgrade((lock), (flags), (file), (line))
234
235 #define WITNESS_UNLOCK(lock, flags, file, line) \
236 witness_unlock((lock), (flags), (file), (line))
237
238 #define WITNESS_WARN(flags, lock, fmt, ...) \
239 witness_warn((flags), (lock), (fmt), ## __VA_ARGS__)
240
241 #define WITNESS_SAVE_DECL(n) \
242 const char * __CONCAT(n, __wf); \
243 int __CONCAT(n, __wl)
244
245 #define WITNESS_SAVE(lock, n) \
246 witness_save((lock), &__CONCAT(n, __wf), &__CONCAT(n, __wl))
247
248 #define WITNESS_RESTORE(lock, n) \
249 witness_restore((lock), __CONCAT(n, __wf), __CONCAT(n, __wl))
250
251 #define WITNESS_FILE(lock) \
252 witness_file(lock)
253
254 #define WITNESS_LINE(lock) \
255 witness_line(lock)
256
257 #else /* WITNESS */
258 #define WITNESS_INIT(lock) ((lock)->lo_flags |= LO_INITIALIZED)
259 #define WITNESS_DESTROY(lock) ((lock)->lo_flags &= ~LO_INITIALIZED)
260 #define WITNESS_LOCK(lock, flags, file, line)
261 #define WITNESS_UPGRADE(lock, flags, file, line)
262 #define WITNESS_DOWNGRADE(lock, flags, file, line)
263 #define WITNESS_UNLOCK(lock, flags, file, line)
264 #define WITNESS_WARN(flags, lock, fmt, ...)
265 #define WITNESS_SAVE_DECL(n)
266 #define WITNESS_SAVE(lock, n)
267 #define WITNESS_RESTORE(lock, n)
268 #define WITNESS_FILE(lock) ("?")
269 #define WITNESS_LINE(lock) (0)
270 #endif /* WITNESS */
271
272 #endif /* _KERNEL */
273 #endif /* _SYS_LOCK_H_ */
Cache object: 42d751d8601ce9baa28c4968be4837e2
|