kern_lock.c revision 1.99 1 1.99 ad /* $NetBSD: kern_lock.c,v 1.99 2006/09/07 02:06:47 ad Exp $ */
2 1.19 thorpej
3 1.19 thorpej /*-
4 1.28 thorpej * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 1.19 thorpej * All rights reserved.
6 1.19 thorpej *
7 1.19 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.19 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.19 thorpej * NASA Ames Research Center.
10 1.19 thorpej *
11 1.19 thorpej * This code is derived from software contributed to The NetBSD Foundation
12 1.19 thorpej * by Ross Harvey.
13 1.19 thorpej *
14 1.19 thorpej * Redistribution and use in source and binary forms, with or without
15 1.19 thorpej * modification, are permitted provided that the following conditions
16 1.19 thorpej * are met:
17 1.19 thorpej * 1. Redistributions of source code must retain the above copyright
18 1.19 thorpej * notice, this list of conditions and the following disclaimer.
19 1.19 thorpej * 2. Redistributions in binary form must reproduce the above copyright
20 1.19 thorpej * notice, this list of conditions and the following disclaimer in the
21 1.19 thorpej * documentation and/or other materials provided with the distribution.
22 1.19 thorpej * 3. All advertising materials mentioning features or use of this software
23 1.19 thorpej * must display the following acknowledgement:
24 1.19 thorpej * This product includes software developed by the NetBSD
25 1.19 thorpej * Foundation, Inc. and its contributors.
26 1.19 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
27 1.19 thorpej * contributors may be used to endorse or promote products derived
28 1.19 thorpej * from this software without specific prior written permission.
29 1.19 thorpej *
30 1.19 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 1.19 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 1.19 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 1.19 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 1.19 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 1.19 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 1.19 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 1.19 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 1.19 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.19 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.19 thorpej * POSSIBILITY OF SUCH DAMAGE.
41 1.19 thorpej */
42 1.2 fvdl
43 1.86 perry /*
44 1.1 fvdl * Copyright (c) 1995
45 1.1 fvdl * The Regents of the University of California. All rights reserved.
46 1.1 fvdl *
47 1.1 fvdl * This code contains ideas from software contributed to Berkeley by
48 1.1 fvdl * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
49 1.1 fvdl * System project at Carnegie-Mellon University.
50 1.1 fvdl *
51 1.1 fvdl * Redistribution and use in source and binary forms, with or without
52 1.1 fvdl * modification, are permitted provided that the following conditions
53 1.1 fvdl * are met:
54 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
55 1.1 fvdl * notice, this list of conditions and the following disclaimer.
56 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
57 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
58 1.1 fvdl * documentation and/or other materials provided with the distribution.
59 1.72 agc * 3. Neither the name of the University nor the names of its contributors
60 1.1 fvdl * may be used to endorse or promote products derived from this software
61 1.1 fvdl * without specific prior written permission.
62 1.1 fvdl *
63 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 fvdl * SUCH DAMAGE.
74 1.1 fvdl *
75 1.1 fvdl * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
76 1.1 fvdl */
77 1.60 lukem
78 1.60 lukem #include <sys/cdefs.h>
79 1.99 ad __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.99 2006/09/07 02:06:47 ad Exp $");
80 1.7 thorpej
81 1.21 thorpej #include "opt_multiprocessor.h"
82 1.7 thorpej #include "opt_lockdebug.h"
83 1.18 chs #include "opt_ddb.h"
84 1.1 fvdl
85 1.1 fvdl #include <sys/param.h>
86 1.1 fvdl #include <sys/proc.h>
87 1.1 fvdl #include <sys/lock.h>
88 1.2 fvdl #include <sys/systm.h>
89 1.1 fvdl #include <machine/cpu.h>
90 1.1 fvdl
91 1.98 ad #include <dev/lockstat.h>
92 1.98 ad
93 1.25 thorpej #if defined(LOCKDEBUG)
94 1.25 thorpej #include <sys/syslog.h>
95 1.25 thorpej /*
96 1.25 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
97 1.25 thorpej * ansi and traditional c compiles.
98 1.25 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
99 1.25 thorpej */
100 1.25 thorpej #include <machine/stdarg.h>
101 1.25 thorpej
102 1.36 thorpej void lock_printf(const char *fmt, ...)
103 1.37 eeh __attribute__((__format__(__printf__,1,2)));
104 1.25 thorpej
105 1.98 ad static int acquire(volatile struct lock **, int *, int, int, int, uintptr_t ra);
106 1.73 yamt
107 1.57 sommerfe int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
108 1.55 thorpej
109 1.55 thorpej #ifdef DDB
110 1.55 thorpej #include <ddb/ddbvar.h>
111 1.55 thorpej #include <machine/db_machdep.h>
112 1.55 thorpej #include <ddb/db_command.h>
113 1.55 thorpej #include <ddb/db_interface.h>
114 1.55 thorpej #endif
115 1.85 yamt #endif /* defined(LOCKDEBUG) */
116 1.85 yamt
117 1.85 yamt #if defined(MULTIPROCESSOR)
118 1.86 perry struct simplelock kernel_lock;
119 1.25 thorpej #endif
120 1.25 thorpej
121 1.1 fvdl /*
122 1.1 fvdl * Locking primitives implementation.
123 1.56 wiz * Locks provide shared/exclusive synchronization.
124 1.1 fvdl */
125 1.1 fvdl
126 1.21 thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
127 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
128 1.21 thorpej #define COUNT_CPU(cpu_id, x) \
129 1.47 sommerfe curcpu()->ci_spin_locks += (x)
130 1.21 thorpej #else
131 1.21 thorpej u_long spin_locks;
132 1.21 thorpej #define COUNT_CPU(cpu_id, x) spin_locks += (x)
133 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
134 1.21 thorpej
135 1.69 thorpej #define COUNT(lkp, l, cpu_id, x) \
136 1.21 thorpej do { \
137 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) \
138 1.21 thorpej COUNT_CPU((cpu_id), (x)); \
139 1.21 thorpej else \
140 1.69 thorpej (l)->l_locks += (x); \
141 1.30 thorpej } while (/*CONSTCOND*/0)
142 1.1 fvdl #else
143 1.22 mellon #define COUNT(lkp, p, cpu_id, x)
144 1.48 sommerfe #define COUNT_CPU(cpu_id, x)
145 1.21 thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
146 1.1 fvdl
147 1.43 thorpej #define INTERLOCK_ACQUIRE(lkp, flags, s) \
148 1.40 thorpej do { \
149 1.43 thorpej if ((flags) & LK_SPIN) \
150 1.65 fvdl s = spllock(); \
151 1.40 thorpej simple_lock(&(lkp)->lk_interlock); \
152 1.66 perry } while (/*CONSTCOND*/ 0)
153 1.40 thorpej
154 1.43 thorpej #define INTERLOCK_RELEASE(lkp, flags, s) \
155 1.40 thorpej do { \
156 1.40 thorpej simple_unlock(&(lkp)->lk_interlock); \
157 1.52 thorpej if ((flags) & LK_SPIN) \
158 1.40 thorpej splx(s); \
159 1.66 perry } while (/*CONSTCOND*/ 0)
160 1.40 thorpej
161 1.63 chs #ifdef DDB /* { */
162 1.89 chs #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
163 1.63 chs int simple_lock_debugger = 1; /* more serious on MP */
164 1.63 chs #else
165 1.63 chs int simple_lock_debugger = 0;
166 1.63 chs #endif
167 1.93 erh #define SLOCK_DEBUGGER() if (simple_lock_debugger && db_onpanic) Debugger()
168 1.63 chs #define SLOCK_TRACE() \
169 1.63 chs db_stack_trace_print((db_expr_t)__builtin_frame_address(0), \
170 1.71 pk TRUE, 65535, "", lock_printf);
171 1.63 chs #else
172 1.63 chs #define SLOCK_DEBUGGER() /* nothing */
173 1.63 chs #define SLOCK_TRACE() /* nothing */
174 1.63 chs #endif /* } */
175 1.63 chs
176 1.50 thorpej #if defined(LOCKDEBUG)
177 1.50 thorpej #if defined(DDB)
178 1.93 erh #define SPINLOCK_SPINCHECK_DEBUGGER if (db_onpanic) Debugger()
179 1.50 thorpej #else
180 1.50 thorpej #define SPINLOCK_SPINCHECK_DEBUGGER /* nothing */
181 1.50 thorpej #endif
182 1.50 thorpej
183 1.50 thorpej #define SPINLOCK_SPINCHECK_DECL \
184 1.50 thorpej /* 32-bits of count -- wrap constitutes a "spinout" */ \
185 1.50 thorpej uint32_t __spinc = 0
186 1.50 thorpej
187 1.50 thorpej #define SPINLOCK_SPINCHECK \
188 1.50 thorpej do { \
189 1.50 thorpej if (++__spinc == 0) { \
190 1.71 pk lock_printf("LK_SPIN spinout, excl %d, share %d\n", \
191 1.50 thorpej lkp->lk_exclusivecount, lkp->lk_sharecount); \
192 1.50 thorpej if (lkp->lk_exclusivecount) \
193 1.71 pk lock_printf("held by CPU %lu\n", \
194 1.50 thorpej (u_long) lkp->lk_cpu); \
195 1.50 thorpej if (lkp->lk_lock_file) \
196 1.71 pk lock_printf("last locked at %s:%d\n", \
197 1.50 thorpej lkp->lk_lock_file, lkp->lk_lock_line); \
198 1.50 thorpej if (lkp->lk_unlock_file) \
199 1.71 pk lock_printf("last unlocked at %s:%d\n", \
200 1.50 thorpej lkp->lk_unlock_file, lkp->lk_unlock_line); \
201 1.63 chs SLOCK_TRACE(); \
202 1.50 thorpej SPINLOCK_SPINCHECK_DEBUGGER; \
203 1.50 thorpej } \
204 1.66 perry } while (/*CONSTCOND*/ 0)
205 1.50 thorpej #else
206 1.50 thorpej #define SPINLOCK_SPINCHECK_DECL /* nothing */
207 1.50 thorpej #define SPINLOCK_SPINCHECK /* nothing */
208 1.50 thorpej #endif /* LOCKDEBUG && DDB */
209 1.50 thorpej
210 1.98 ad #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
211 1.98 ad
212 1.1 fvdl /*
213 1.1 fvdl * Acquire a resource.
214 1.1 fvdl */
215 1.73 yamt static int
216 1.91 perry acquire(volatile struct lock **lkpp, int *s, int extflags,
217 1.98 ad int drain, int wanted, uintptr_t ra)
218 1.73 yamt {
219 1.73 yamt int error;
220 1.91 perry volatile struct lock *lkp = *lkpp;
221 1.98 ad LOCKSTAT_TIMER(slptime);
222 1.73 yamt
223 1.73 yamt KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
224 1.73 yamt
225 1.73 yamt if (extflags & LK_SPIN) {
226 1.73 yamt int interlocked;
227 1.73 yamt
228 1.73 yamt SPINLOCK_SPINCHECK_DECL;
229 1.73 yamt
230 1.73 yamt if (!drain) {
231 1.73 yamt lkp->lk_waitcount++;
232 1.73 yamt lkp->lk_flags |= LK_WAIT_NONZERO;
233 1.73 yamt }
234 1.73 yamt for (interlocked = 1;;) {
235 1.73 yamt SPINLOCK_SPINCHECK;
236 1.73 yamt if ((lkp->lk_flags & wanted) != 0) {
237 1.73 yamt if (interlocked) {
238 1.74 hannken INTERLOCK_RELEASE(lkp, LK_SPIN, *s);
239 1.73 yamt interlocked = 0;
240 1.73 yamt }
241 1.73 yamt SPINLOCK_SPIN_HOOK;
242 1.73 yamt } else if (interlocked) {
243 1.73 yamt break;
244 1.73 yamt } else {
245 1.74 hannken INTERLOCK_ACQUIRE(lkp, LK_SPIN, *s);
246 1.73 yamt interlocked = 1;
247 1.73 yamt }
248 1.73 yamt }
249 1.73 yamt if (!drain) {
250 1.73 yamt lkp->lk_waitcount--;
251 1.73 yamt if (lkp->lk_waitcount == 0)
252 1.73 yamt lkp->lk_flags &= ~LK_WAIT_NONZERO;
253 1.73 yamt }
254 1.73 yamt KASSERT((lkp->lk_flags & wanted) == 0);
255 1.73 yamt error = 0; /* sanity */
256 1.73 yamt } else {
257 1.73 yamt for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
258 1.73 yamt if (drain)
259 1.73 yamt lkp->lk_flags |= LK_WAITDRAIN;
260 1.73 yamt else {
261 1.73 yamt lkp->lk_waitcount++;
262 1.73 yamt lkp->lk_flags |= LK_WAIT_NONZERO;
263 1.73 yamt }
264 1.73 yamt /* XXX Cast away volatile. */
265 1.98 ad LOCKSTAT_START_TIMER(slptime);
266 1.73 yamt error = ltsleep(drain ?
267 1.87 christos (volatile const void *)&lkp->lk_flags :
268 1.87 christos (volatile const void *)lkp, lkp->lk_prio,
269 1.73 yamt lkp->lk_wmesg, lkp->lk_timo, &lkp->lk_interlock);
270 1.98 ad LOCKSTAT_STOP_TIMER(slptime);
271 1.98 ad LOCKSTAT_EVENT_RA((void *)(uintptr_t)lkp,
272 1.98 ad LB_LOCKMGR | LB_SLEEP, 1, slptime, ra);
273 1.73 yamt if (!drain) {
274 1.73 yamt lkp->lk_waitcount--;
275 1.73 yamt if (lkp->lk_waitcount == 0)
276 1.73 yamt lkp->lk_flags &= ~LK_WAIT_NONZERO;
277 1.73 yamt }
278 1.73 yamt if (error)
279 1.73 yamt break;
280 1.73 yamt if (extflags & LK_SLEEPFAIL) {
281 1.73 yamt error = ENOLCK;
282 1.73 yamt break;
283 1.73 yamt }
284 1.78 hannken if (lkp->lk_newlock != NULL) {
285 1.78 hannken simple_lock(&lkp->lk_newlock->lk_interlock);
286 1.78 hannken simple_unlock(&lkp->lk_interlock);
287 1.78 hannken if (lkp->lk_waitcount == 0)
288 1.87 christos wakeup(&lkp->lk_newlock);
289 1.78 hannken *lkpp = lkp = lkp->lk_newlock;
290 1.78 hannken }
291 1.73 yamt }
292 1.1 fvdl }
293 1.1 fvdl
294 1.73 yamt return error;
295 1.73 yamt }
296 1.73 yamt
297 1.69 thorpej #define SETHOLDER(lkp, pid, lid, cpu_id) \
298 1.19 thorpej do { \
299 1.19 thorpej if ((lkp)->lk_flags & LK_SPIN) \
300 1.19 thorpej (lkp)->lk_cpu = cpu_id; \
301 1.69 thorpej else { \
302 1.19 thorpej (lkp)->lk_lockholder = pid; \
303 1.69 thorpej (lkp)->lk_locklwp = lid; \
304 1.69 thorpej } \
305 1.30 thorpej } while (/*CONSTCOND*/0)
306 1.19 thorpej
307 1.69 thorpej #define WEHOLDIT(lkp, pid, lid, cpu_id) \
308 1.19 thorpej (((lkp)->lk_flags & LK_SPIN) != 0 ? \
309 1.69 thorpej ((lkp)->lk_cpu == (cpu_id)) : \
310 1.69 thorpej ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid)))
311 1.19 thorpej
312 1.23 thorpej #define WAKEUP_WAITER(lkp) \
313 1.23 thorpej do { \
314 1.73 yamt if (((lkp)->lk_flags & (LK_SPIN | LK_WAIT_NONZERO)) == \
315 1.73 yamt LK_WAIT_NONZERO) { \
316 1.87 christos wakeup((lkp)); \
317 1.23 thorpej } \
318 1.30 thorpej } while (/*CONSTCOND*/0)
319 1.23 thorpej
320 1.21 thorpej #if defined(LOCKDEBUG) /* { */
321 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
322 1.21 thorpej struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
323 1.21 thorpej
324 1.27 thorpej #define SPINLOCK_LIST_LOCK() \
325 1.29 sommerfe __cpu_simple_lock(&spinlock_list_slock.lock_data)
326 1.21 thorpej
327 1.27 thorpej #define SPINLOCK_LIST_UNLOCK() \
328 1.29 sommerfe __cpu_simple_unlock(&spinlock_list_slock.lock_data)
329 1.21 thorpej #else
330 1.21 thorpej #define SPINLOCK_LIST_LOCK() /* nothing */
331 1.21 thorpej
332 1.21 thorpej #define SPINLOCK_LIST_UNLOCK() /* nothing */
333 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
334 1.21 thorpej
335 1.91 perry _TAILQ_HEAD(, struct lock, volatile) spinlock_list =
336 1.21 thorpej TAILQ_HEAD_INITIALIZER(spinlock_list);
337 1.21 thorpej
338 1.21 thorpej #define HAVEIT(lkp) \
339 1.21 thorpej do { \
340 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) { \
341 1.87 christos int sp = spllock(); \
342 1.21 thorpej SPINLOCK_LIST_LOCK(); \
343 1.87 christos TAILQ_INSERT_TAIL(&spinlock_list, (lkp), lk_list); \
344 1.21 thorpej SPINLOCK_LIST_UNLOCK(); \
345 1.87 christos splx(sp); \
346 1.21 thorpej } \
347 1.30 thorpej } while (/*CONSTCOND*/0)
348 1.21 thorpej
349 1.21 thorpej #define DONTHAVEIT(lkp) \
350 1.21 thorpej do { \
351 1.21 thorpej if ((lkp)->lk_flags & LK_SPIN) { \
352 1.87 christos int sp = spllock(); \
353 1.21 thorpej SPINLOCK_LIST_LOCK(); \
354 1.87 christos TAILQ_REMOVE(&spinlock_list, (lkp), lk_list); \
355 1.21 thorpej SPINLOCK_LIST_UNLOCK(); \
356 1.87 christos splx(sp); \
357 1.21 thorpej } \
358 1.30 thorpej } while (/*CONSTCOND*/0)
359 1.21 thorpej #else
360 1.21 thorpej #define HAVEIT(lkp) /* nothing */
361 1.21 thorpej
362 1.21 thorpej #define DONTHAVEIT(lkp) /* nothing */
363 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
364 1.21 thorpej
365 1.25 thorpej #if defined(LOCKDEBUG)
366 1.25 thorpej /*
367 1.25 thorpej * Lock debug printing routine; can be configured to print to console
368 1.25 thorpej * or log to syslog.
369 1.25 thorpej */
370 1.25 thorpej void
371 1.25 thorpej lock_printf(const char *fmt, ...)
372 1.25 thorpej {
373 1.68 pk char b[150];
374 1.25 thorpej va_list ap;
375 1.25 thorpej
376 1.25 thorpej va_start(ap, fmt);
377 1.25 thorpej if (lock_debug_syslog)
378 1.25 thorpej vlog(LOG_DEBUG, fmt, ap);
379 1.68 pk else {
380 1.68 pk vsnprintf(b, sizeof(b), fmt, ap);
381 1.68 pk printf_nolog("%s", b);
382 1.68 pk }
383 1.25 thorpej va_end(ap);
384 1.25 thorpej }
385 1.25 thorpej #endif /* LOCKDEBUG */
386 1.25 thorpej
387 1.1 fvdl /*
388 1.78 hannken * Transfer any waiting processes from one lock to another.
389 1.78 hannken */
390 1.78 hannken void
391 1.78 hannken transferlockers(struct lock *from, struct lock *to)
392 1.78 hannken {
393 1.78 hannken
394 1.78 hannken KASSERT(from != to);
395 1.78 hannken KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
396 1.78 hannken if (from->lk_waitcount == 0)
397 1.78 hannken return;
398 1.78 hannken from->lk_newlock = to;
399 1.78 hannken wakeup((void *)from);
400 1.78 hannken tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
401 1.78 hannken from->lk_newlock = NULL;
402 1.78 hannken from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
403 1.78 hannken KASSERT(from->lk_waitcount == 0);
404 1.78 hannken }
405 1.78 hannken
406 1.78 hannken
407 1.78 hannken /*
408 1.1 fvdl * Initialize a lock; required before use.
409 1.1 fvdl */
410 1.1 fvdl void
411 1.33 thorpej lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
412 1.1 fvdl {
413 1.1 fvdl
414 1.8 perry memset(lkp, 0, sizeof(struct lock));
415 1.1 fvdl simple_lock_init(&lkp->lk_interlock);
416 1.1 fvdl lkp->lk_flags = flags & LK_EXTFLG_MASK;
417 1.19 thorpej if (flags & LK_SPIN)
418 1.19 thorpej lkp->lk_cpu = LK_NOCPU;
419 1.19 thorpej else {
420 1.19 thorpej lkp->lk_lockholder = LK_NOPROC;
421 1.78 hannken lkp->lk_newlock = NULL;
422 1.19 thorpej lkp->lk_prio = prio;
423 1.19 thorpej lkp->lk_timo = timo;
424 1.19 thorpej }
425 1.19 thorpej lkp->lk_wmesg = wmesg; /* just a name for spin locks */
426 1.50 thorpej #if defined(LOCKDEBUG)
427 1.50 thorpej lkp->lk_lock_file = NULL;
428 1.50 thorpej lkp->lk_unlock_file = NULL;
429 1.50 thorpej #endif
430 1.1 fvdl }
431 1.1 fvdl
432 1.1 fvdl /*
433 1.1 fvdl * Determine the status of a lock.
434 1.1 fvdl */
435 1.1 fvdl int
436 1.33 thorpej lockstatus(struct lock *lkp)
437 1.1 fvdl {
438 1.76 yamt int s = 0; /* XXX: gcc */
439 1.76 yamt int lock_type = 0;
440 1.76 yamt struct lwp *l = curlwp; /* XXX */
441 1.76 yamt pid_t pid;
442 1.76 yamt lwpid_t lid;
443 1.88 blymn cpuid_t cpu_num;
444 1.76 yamt
445 1.76 yamt if ((lkp->lk_flags & LK_SPIN) || l == NULL) {
446 1.88 blymn cpu_num = cpu_number();
447 1.76 yamt pid = LK_KERNPROC;
448 1.76 yamt lid = 0;
449 1.76 yamt } else {
450 1.88 blymn cpu_num = LK_NOCPU;
451 1.76 yamt pid = l->l_proc->p_pid;
452 1.76 yamt lid = l->l_lid;
453 1.76 yamt }
454 1.1 fvdl
455 1.43 thorpej INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
456 1.76 yamt if (lkp->lk_exclusivecount != 0) {
457 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num))
458 1.76 yamt lock_type = LK_EXCLUSIVE;
459 1.76 yamt else
460 1.76 yamt lock_type = LK_EXCLOTHER;
461 1.76 yamt } else if (lkp->lk_sharecount != 0)
462 1.1 fvdl lock_type = LK_SHARED;
463 1.43 thorpej INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
464 1.1 fvdl return (lock_type);
465 1.1 fvdl }
466 1.35 thorpej
467 1.92 chs #if defined(LOCKDEBUG)
468 1.35 thorpej /*
469 1.35 thorpej * Make sure no spin locks are held by a CPU that is about
470 1.35 thorpej * to context switch.
471 1.35 thorpej */
472 1.35 thorpej void
473 1.35 thorpej spinlock_switchcheck(void)
474 1.35 thorpej {
475 1.35 thorpej u_long cnt;
476 1.35 thorpej int s;
477 1.35 thorpej
478 1.44 thorpej s = spllock();
479 1.35 thorpej #if defined(MULTIPROCESSOR)
480 1.35 thorpej cnt = curcpu()->ci_spin_locks;
481 1.35 thorpej #else
482 1.35 thorpej cnt = spin_locks;
483 1.35 thorpej #endif
484 1.35 thorpej splx(s);
485 1.35 thorpej
486 1.35 thorpej if (cnt != 0)
487 1.35 thorpej panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
488 1.35 thorpej (u_long) cpu_number(), cnt);
489 1.35 thorpej }
490 1.92 chs #endif /* LOCKDEBUG */
491 1.1 fvdl
492 1.1 fvdl /*
493 1.44 thorpej * Locks and IPLs (interrupt priority levels):
494 1.44 thorpej *
495 1.44 thorpej * Locks which may be taken from interrupt context must be handled
496 1.44 thorpej * very carefully; you must spl to the highest IPL where the lock
497 1.44 thorpej * is needed before acquiring the lock.
498 1.44 thorpej *
499 1.44 thorpej * It is also important to avoid deadlock, since certain (very high
500 1.44 thorpej * priority) interrupts are often needed to keep the system as a whole
501 1.44 thorpej * from deadlocking, and must not be blocked while you are spinning
502 1.44 thorpej * waiting for a lower-priority lock.
503 1.44 thorpej *
504 1.44 thorpej * In addition, the lock-debugging hooks themselves need to use locks!
505 1.44 thorpej *
506 1.44 thorpej * A raw __cpu_simple_lock may be used from interrupts are long as it
507 1.44 thorpej * is acquired and held at a single IPL.
508 1.44 thorpej *
509 1.44 thorpej * A simple_lock (which is a __cpu_simple_lock wrapped with some
510 1.44 thorpej * debugging hooks) may be used at or below spllock(), which is
511 1.44 thorpej * typically at or just below splhigh() (i.e. blocks everything
512 1.44 thorpej * but certain machine-dependent extremely high priority interrupts).
513 1.44 thorpej *
514 1.44 thorpej * spinlockmgr spinlocks should be used at or below splsched().
515 1.44 thorpej *
516 1.44 thorpej * Some platforms may have interrupts of higher priority than splsched(),
517 1.44 thorpej * including hard serial interrupts, inter-processor interrupts, and
518 1.44 thorpej * kernel debugger traps.
519 1.44 thorpej */
520 1.44 thorpej
521 1.44 thorpej /*
522 1.32 sommerfe * XXX XXX kludge around another kludge..
523 1.32 sommerfe *
524 1.32 sommerfe * vfs_shutdown() may be called from interrupt context, either as a result
525 1.32 sommerfe * of a panic, or from the debugger. It proceeds to call
526 1.32 sommerfe * sys_sync(&proc0, ...), pretending its running on behalf of proc0
527 1.32 sommerfe *
528 1.32 sommerfe * We would like to make an attempt to sync the filesystems in this case, so
529 1.32 sommerfe * if this happens, we treat attempts to acquire locks specially.
530 1.32 sommerfe * All locks are acquired on behalf of proc0.
531 1.32 sommerfe *
532 1.32 sommerfe * If we've already paniced, we don't block waiting for locks, but
533 1.32 sommerfe * just barge right ahead since we're already going down in flames.
534 1.32 sommerfe */
535 1.32 sommerfe
536 1.32 sommerfe /*
537 1.1 fvdl * Set, change, or release a lock.
538 1.1 fvdl *
539 1.1 fvdl * Shared requests increment the shared count. Exclusive requests set the
540 1.1 fvdl * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
541 1.1 fvdl * accepted shared locks and shared-to-exclusive upgrades to go away.
542 1.1 fvdl */
543 1.1 fvdl int
544 1.50 thorpej #if defined(LOCKDEBUG)
545 1.91 perry _lockmgr(volatile struct lock *lkp, u_int flags,
546 1.50 thorpej struct simplelock *interlkp, const char *file, int line)
547 1.50 thorpej #else
548 1.91 perry lockmgr(volatile struct lock *lkp, u_int flags,
549 1.33 thorpej struct simplelock *interlkp)
550 1.50 thorpej #endif
551 1.1 fvdl {
552 1.1 fvdl int error;
553 1.1 fvdl pid_t pid;
554 1.69 thorpej lwpid_t lid;
555 1.1 fvdl int extflags;
556 1.88 blymn cpuid_t cpu_num;
557 1.69 thorpej struct lwp *l = curlwp;
558 1.32 sommerfe int lock_shutdown_noblock = 0;
559 1.67 scw int s = 0;
560 1.1 fvdl
561 1.1 fvdl error = 0;
562 1.19 thorpej
563 1.80 yamt /* LK_RETRY is for vn_lock, not for lockmgr. */
564 1.79 yamt KASSERT((flags & LK_RETRY) == 0);
565 1.79 yamt
566 1.43 thorpej INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
567 1.1 fvdl if (flags & LK_INTERLOCK)
568 1.1 fvdl simple_unlock(interlkp);
569 1.1 fvdl extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
570 1.19 thorpej
571 1.21 thorpej #ifdef DIAGNOSTIC /* { */
572 1.19 thorpej /*
573 1.19 thorpej * Don't allow spins on sleep locks and don't allow sleeps
574 1.19 thorpej * on spin locks.
575 1.19 thorpej */
576 1.19 thorpej if ((flags ^ lkp->lk_flags) & LK_SPIN)
577 1.64 provos panic("lockmgr: sleep/spin mismatch");
578 1.21 thorpej #endif /* } */
579 1.19 thorpej
580 1.69 thorpej if (extflags & LK_SPIN) {
581 1.19 thorpej pid = LK_KERNPROC;
582 1.69 thorpej lid = 0;
583 1.69 thorpej } else {
584 1.69 thorpej if (l == NULL) {
585 1.32 sommerfe if (!doing_shutdown) {
586 1.32 sommerfe panic("lockmgr: no context");
587 1.32 sommerfe } else {
588 1.69 thorpej l = &lwp0;
589 1.32 sommerfe if (panicstr && (!(flags & LK_NOWAIT))) {
590 1.32 sommerfe flags |= LK_NOWAIT;
591 1.32 sommerfe lock_shutdown_noblock = 1;
592 1.32 sommerfe }
593 1.32 sommerfe }
594 1.32 sommerfe }
595 1.69 thorpej lid = l->l_lid;
596 1.69 thorpej pid = l->l_proc->p_pid;
597 1.19 thorpej }
598 1.88 blymn cpu_num = cpu_number();
599 1.19 thorpej
600 1.1 fvdl /*
601 1.1 fvdl * Once a lock has drained, the LK_DRAINING flag is set and an
602 1.1 fvdl * exclusive lock is returned. The only valid operation thereafter
603 1.1 fvdl * is a single release of that exclusive lock. This final release
604 1.1 fvdl * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
605 1.1 fvdl * further requests of any sort will result in a panic. The bits
606 1.1 fvdl * selected for these two flags are chosen so that they will be set
607 1.1 fvdl * in memory that is freed (freed memory is filled with 0xdeadbeef).
608 1.1 fvdl * The final release is permitted to give a new lease on life to
609 1.1 fvdl * the lock by specifying LK_REENABLE.
610 1.1 fvdl */
611 1.1 fvdl if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
612 1.28 thorpej #ifdef DIAGNOSTIC /* { */
613 1.1 fvdl if (lkp->lk_flags & LK_DRAINED)
614 1.1 fvdl panic("lockmgr: using decommissioned lock");
615 1.1 fvdl if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
616 1.88 blymn WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
617 1.64 provos panic("lockmgr: non-release on draining lock: %d",
618 1.1 fvdl flags & LK_TYPE_MASK);
619 1.28 thorpej #endif /* DIAGNOSTIC */ /* } */
620 1.1 fvdl lkp->lk_flags &= ~LK_DRAINING;
621 1.1 fvdl if ((flags & LK_REENABLE) == 0)
622 1.1 fvdl lkp->lk_flags |= LK_DRAINED;
623 1.1 fvdl }
624 1.1 fvdl
625 1.1 fvdl switch (flags & LK_TYPE_MASK) {
626 1.1 fvdl
627 1.1 fvdl case LK_SHARED:
628 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
629 1.1 fvdl /*
630 1.1 fvdl * If just polling, check to see if we will block.
631 1.1 fvdl */
632 1.1 fvdl if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
633 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
634 1.1 fvdl error = EBUSY;
635 1.1 fvdl break;
636 1.1 fvdl }
637 1.1 fvdl /*
638 1.1 fvdl * Wait for exclusive locks and upgrades to clear.
639 1.1 fvdl */
640 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
641 1.98 ad LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
642 1.98 ad RETURN_ADDRESS);
643 1.1 fvdl if (error)
644 1.1 fvdl break;
645 1.1 fvdl lkp->lk_sharecount++;
646 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
647 1.88 blymn COUNT(lkp, l, cpu_num, 1);
648 1.1 fvdl break;
649 1.1 fvdl }
650 1.1 fvdl /*
651 1.1 fvdl * We hold an exclusive lock, so downgrade it to shared.
652 1.1 fvdl * An alternative would be to fail with EDEADLK.
653 1.1 fvdl */
654 1.1 fvdl lkp->lk_sharecount++;
655 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
656 1.88 blymn COUNT(lkp, l, cpu_num, 1);
657 1.1 fvdl /* fall into downgrade */
658 1.1 fvdl
659 1.1 fvdl case LK_DOWNGRADE:
660 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
661 1.19 thorpej lkp->lk_exclusivecount == 0)
662 1.1 fvdl panic("lockmgr: not holding exclusive lock");
663 1.1 fvdl lkp->lk_sharecount += lkp->lk_exclusivecount;
664 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
665 1.1 fvdl lkp->lk_exclusivecount = 0;
666 1.15 fvdl lkp->lk_recurselevel = 0;
667 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
668 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
669 1.50 thorpej #if defined(LOCKDEBUG)
670 1.50 thorpej lkp->lk_unlock_file = file;
671 1.50 thorpej lkp->lk_unlock_line = line;
672 1.50 thorpej #endif
673 1.21 thorpej DONTHAVEIT(lkp);
674 1.23 thorpej WAKEUP_WAITER(lkp);
675 1.1 fvdl break;
676 1.1 fvdl
677 1.1 fvdl case LK_EXCLUPGRADE:
678 1.1 fvdl /*
679 1.1 fvdl * If another process is ahead of us to get an upgrade,
680 1.1 fvdl * then we want to fail rather than have an intervening
681 1.1 fvdl * exclusive access.
682 1.1 fvdl */
683 1.1 fvdl if (lkp->lk_flags & LK_WANT_UPGRADE) {
684 1.1 fvdl lkp->lk_sharecount--;
685 1.73 yamt if (lkp->lk_sharecount == 0)
686 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
687 1.88 blymn COUNT(lkp, l, cpu_num, -1);
688 1.1 fvdl error = EBUSY;
689 1.1 fvdl break;
690 1.1 fvdl }
691 1.1 fvdl /* fall into normal upgrade */
692 1.1 fvdl
693 1.1 fvdl case LK_UPGRADE:
694 1.1 fvdl /*
695 1.1 fvdl * Upgrade a shared lock to an exclusive one. If another
696 1.1 fvdl * shared lock has already requested an upgrade to an
697 1.1 fvdl * exclusive lock, our shared lock is released and an
698 1.1 fvdl * exclusive lock is requested (which will be granted
699 1.1 fvdl * after the upgrade). If we return an error, the file
700 1.1 fvdl * will always be unlocked.
701 1.1 fvdl */
702 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
703 1.1 fvdl panic("lockmgr: upgrade exclusive lock");
704 1.1 fvdl lkp->lk_sharecount--;
705 1.73 yamt if (lkp->lk_sharecount == 0)
706 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
707 1.88 blymn COUNT(lkp, l, cpu_num, -1);
708 1.1 fvdl /*
709 1.1 fvdl * If we are just polling, check to see if we will block.
710 1.1 fvdl */
711 1.1 fvdl if ((extflags & LK_NOWAIT) &&
712 1.1 fvdl ((lkp->lk_flags & LK_WANT_UPGRADE) ||
713 1.1 fvdl lkp->lk_sharecount > 1)) {
714 1.1 fvdl error = EBUSY;
715 1.1 fvdl break;
716 1.1 fvdl }
717 1.1 fvdl if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
718 1.1 fvdl /*
719 1.1 fvdl * We are first shared lock to request an upgrade, so
720 1.1 fvdl * request upgrade and wait for the shared count to
721 1.1 fvdl * drop to zero, then take exclusive lock.
722 1.1 fvdl */
723 1.1 fvdl lkp->lk_flags |= LK_WANT_UPGRADE;
724 1.98 ad error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
725 1.98 ad RETURN_ADDRESS);
726 1.1 fvdl lkp->lk_flags &= ~LK_WANT_UPGRADE;
727 1.83 yamt if (error) {
728 1.83 yamt WAKEUP_WAITER(lkp);
729 1.1 fvdl break;
730 1.83 yamt }
731 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
732 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
733 1.50 thorpej #if defined(LOCKDEBUG)
734 1.50 thorpej lkp->lk_lock_file = file;
735 1.50 thorpej lkp->lk_lock_line = line;
736 1.50 thorpej #endif
737 1.21 thorpej HAVEIT(lkp);
738 1.1 fvdl if (lkp->lk_exclusivecount != 0)
739 1.1 fvdl panic("lockmgr: non-zero exclusive count");
740 1.1 fvdl lkp->lk_exclusivecount = 1;
741 1.15 fvdl if (extflags & LK_SETRECURSE)
742 1.15 fvdl lkp->lk_recurselevel = 1;
743 1.88 blymn COUNT(lkp, l, cpu_num, 1);
744 1.1 fvdl break;
745 1.1 fvdl }
746 1.1 fvdl /*
747 1.1 fvdl * Someone else has requested upgrade. Release our shared
748 1.1 fvdl * lock, awaken upgrade requestor if we are the last shared
749 1.1 fvdl * lock, then request an exclusive lock.
750 1.1 fvdl */
751 1.23 thorpej if (lkp->lk_sharecount == 0)
752 1.23 thorpej WAKEUP_WAITER(lkp);
753 1.1 fvdl /* fall into exclusive request */
754 1.1 fvdl
755 1.1 fvdl case LK_EXCLUSIVE:
756 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
757 1.1 fvdl /*
758 1.19 thorpej * Recursive lock.
759 1.1 fvdl */
760 1.15 fvdl if ((extflags & LK_CANRECURSE) == 0 &&
761 1.16 sommerfe lkp->lk_recurselevel == 0) {
762 1.16 sommerfe if (extflags & LK_RECURSEFAIL) {
763 1.16 sommerfe error = EDEADLK;
764 1.16 sommerfe break;
765 1.16 sommerfe } else
766 1.16 sommerfe panic("lockmgr: locking against myself");
767 1.16 sommerfe }
768 1.1 fvdl lkp->lk_exclusivecount++;
769 1.15 fvdl if (extflags & LK_SETRECURSE &&
770 1.15 fvdl lkp->lk_recurselevel == 0)
771 1.15 fvdl lkp->lk_recurselevel = lkp->lk_exclusivecount;
772 1.88 blymn COUNT(lkp, l, cpu_num, 1);
773 1.1 fvdl break;
774 1.1 fvdl }
775 1.1 fvdl /*
776 1.1 fvdl * If we are just polling, check to see if we will sleep.
777 1.1 fvdl */
778 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
779 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
780 1.73 yamt LK_SHARE_NONZERO))) {
781 1.1 fvdl error = EBUSY;
782 1.1 fvdl break;
783 1.1 fvdl }
784 1.1 fvdl /*
785 1.1 fvdl * Try to acquire the want_exclusive flag.
786 1.1 fvdl */
787 1.82 yamt error = acquire(&lkp, &s, extflags, 0,
788 1.98 ad LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
789 1.1 fvdl if (error)
790 1.1 fvdl break;
791 1.1 fvdl lkp->lk_flags |= LK_WANT_EXCL;
792 1.1 fvdl /*
793 1.1 fvdl * Wait for shared locks and upgrades to finish.
794 1.1 fvdl */
795 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
796 1.98 ad LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
797 1.98 ad RETURN_ADDRESS);
798 1.1 fvdl lkp->lk_flags &= ~LK_WANT_EXCL;
799 1.83 yamt if (error) {
800 1.83 yamt WAKEUP_WAITER(lkp);
801 1.1 fvdl break;
802 1.83 yamt }
803 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
804 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
805 1.50 thorpej #if defined(LOCKDEBUG)
806 1.50 thorpej lkp->lk_lock_file = file;
807 1.50 thorpej lkp->lk_lock_line = line;
808 1.50 thorpej #endif
809 1.21 thorpej HAVEIT(lkp);
810 1.1 fvdl if (lkp->lk_exclusivecount != 0)
811 1.1 fvdl panic("lockmgr: non-zero exclusive count");
812 1.1 fvdl lkp->lk_exclusivecount = 1;
813 1.15 fvdl if (extflags & LK_SETRECURSE)
814 1.15 fvdl lkp->lk_recurselevel = 1;
815 1.88 blymn COUNT(lkp, l, cpu_num, 1);
816 1.1 fvdl break;
817 1.1 fvdl
818 1.1 fvdl case LK_RELEASE:
819 1.1 fvdl if (lkp->lk_exclusivecount != 0) {
820 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
821 1.19 thorpej if (lkp->lk_flags & LK_SPIN) {
822 1.19 thorpej panic("lockmgr: processor %lu, not "
823 1.19 thorpej "exclusive lock holder %lu "
824 1.88 blymn "unlocking", cpu_num, lkp->lk_cpu);
825 1.19 thorpej } else {
826 1.19 thorpej panic("lockmgr: pid %d, not "
827 1.19 thorpej "exclusive lock holder %d "
828 1.19 thorpej "unlocking", pid,
829 1.19 thorpej lkp->lk_lockholder);
830 1.19 thorpej }
831 1.19 thorpej }
832 1.15 fvdl if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
833 1.15 fvdl lkp->lk_recurselevel = 0;
834 1.1 fvdl lkp->lk_exclusivecount--;
835 1.88 blymn COUNT(lkp, l, cpu_num, -1);
836 1.1 fvdl if (lkp->lk_exclusivecount == 0) {
837 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
838 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
839 1.50 thorpej #if defined(LOCKDEBUG)
840 1.50 thorpej lkp->lk_unlock_file = file;
841 1.50 thorpej lkp->lk_unlock_line = line;
842 1.50 thorpej #endif
843 1.21 thorpej DONTHAVEIT(lkp);
844 1.1 fvdl }
845 1.1 fvdl } else if (lkp->lk_sharecount != 0) {
846 1.1 fvdl lkp->lk_sharecount--;
847 1.73 yamt if (lkp->lk_sharecount == 0)
848 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
849 1.88 blymn COUNT(lkp, l, cpu_num, -1);
850 1.1 fvdl }
851 1.39 thorpej #ifdef DIAGNOSTIC
852 1.39 thorpej else
853 1.39 thorpej panic("lockmgr: release of unlocked lock!");
854 1.39 thorpej #endif
855 1.23 thorpej WAKEUP_WAITER(lkp);
856 1.1 fvdl break;
857 1.1 fvdl
858 1.1 fvdl case LK_DRAIN:
859 1.1 fvdl /*
860 1.86 perry * Check that we do not already hold the lock, as it can
861 1.1 fvdl * never drain if we do. Unfortunately, we have no way to
862 1.1 fvdl * check for holding a shared lock, but at least we can
863 1.1 fvdl * check for an exclusive one.
864 1.1 fvdl */
865 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num))
866 1.1 fvdl panic("lockmgr: draining against myself");
867 1.1 fvdl /*
868 1.1 fvdl * If we are just polling, check to see if we will sleep.
869 1.1 fvdl */
870 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
871 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
872 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
873 1.1 fvdl error = EBUSY;
874 1.1 fvdl break;
875 1.1 fvdl }
876 1.78 hannken error = acquire(&lkp, &s, extflags, 1,
877 1.73 yamt LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
878 1.98 ad LK_SHARE_NONZERO | LK_WAIT_NONZERO,
879 1.98 ad RETURN_ADDRESS);
880 1.23 thorpej if (error)
881 1.23 thorpej break;
882 1.1 fvdl lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
883 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
884 1.50 thorpej #if defined(LOCKDEBUG)
885 1.50 thorpej lkp->lk_lock_file = file;
886 1.50 thorpej lkp->lk_lock_line = line;
887 1.50 thorpej #endif
888 1.21 thorpej HAVEIT(lkp);
889 1.1 fvdl lkp->lk_exclusivecount = 1;
890 1.15 fvdl /* XXX unlikely that we'd want this */
891 1.15 fvdl if (extflags & LK_SETRECURSE)
892 1.15 fvdl lkp->lk_recurselevel = 1;
893 1.88 blymn COUNT(lkp, l, cpu_num, 1);
894 1.1 fvdl break;
895 1.1 fvdl
896 1.1 fvdl default:
897 1.43 thorpej INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
898 1.1 fvdl panic("lockmgr: unknown locktype request %d",
899 1.1 fvdl flags & LK_TYPE_MASK);
900 1.1 fvdl /* NOTREACHED */
901 1.1 fvdl }
902 1.23 thorpej if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
903 1.23 thorpej ((lkp->lk_flags &
904 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
905 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
906 1.1 fvdl lkp->lk_flags &= ~LK_WAITDRAIN;
907 1.87 christos wakeup(&lkp->lk_flags);
908 1.1 fvdl }
909 1.32 sommerfe /*
910 1.32 sommerfe * Note that this panic will be a recursive panic, since
911 1.32 sommerfe * we only set lock_shutdown_noblock above if panicstr != NULL.
912 1.32 sommerfe */
913 1.32 sommerfe if (error && lock_shutdown_noblock)
914 1.32 sommerfe panic("lockmgr: deadlock (see previous panic)");
915 1.86 perry
916 1.43 thorpej INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
917 1.1 fvdl return (error);
918 1.1 fvdl }
919 1.1 fvdl
920 1.1 fvdl /*
921 1.47 sommerfe * For a recursive spinlock held one or more times by the current CPU,
922 1.47 sommerfe * release all N locks, and return N.
923 1.47 sommerfe * Intended for use in mi_switch() shortly before context switching.
924 1.47 sommerfe */
925 1.47 sommerfe
926 1.47 sommerfe int
927 1.50 thorpej #if defined(LOCKDEBUG)
928 1.91 perry _spinlock_release_all(volatile struct lock *lkp, const char *file, int line)
929 1.50 thorpej #else
930 1.91 perry spinlock_release_all(volatile struct lock *lkp)
931 1.50 thorpej #endif
932 1.47 sommerfe {
933 1.47 sommerfe int s, count;
934 1.88 blymn cpuid_t cpu_num;
935 1.86 perry
936 1.47 sommerfe KASSERT(lkp->lk_flags & LK_SPIN);
937 1.86 perry
938 1.47 sommerfe INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
939 1.47 sommerfe
940 1.88 blymn cpu_num = cpu_number();
941 1.47 sommerfe count = lkp->lk_exclusivecount;
942 1.86 perry
943 1.47 sommerfe if (count != 0) {
944 1.86 perry #ifdef DIAGNOSTIC
945 1.88 blymn if (WEHOLDIT(lkp, 0, 0, cpu_num) == 0) {
946 1.47 sommerfe panic("spinlock_release_all: processor %lu, not "
947 1.47 sommerfe "exclusive lock holder %lu "
948 1.88 blymn "unlocking", (long)cpu_num, lkp->lk_cpu);
949 1.47 sommerfe }
950 1.47 sommerfe #endif
951 1.47 sommerfe lkp->lk_recurselevel = 0;
952 1.47 sommerfe lkp->lk_exclusivecount = 0;
953 1.88 blymn COUNT_CPU(cpu_num, -count);
954 1.47 sommerfe lkp->lk_flags &= ~LK_HAVE_EXCL;
955 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
956 1.50 thorpej #if defined(LOCKDEBUG)
957 1.50 thorpej lkp->lk_unlock_file = file;
958 1.50 thorpej lkp->lk_unlock_line = line;
959 1.50 thorpej #endif
960 1.47 sommerfe DONTHAVEIT(lkp);
961 1.47 sommerfe }
962 1.47 sommerfe #ifdef DIAGNOSTIC
963 1.47 sommerfe else if (lkp->lk_sharecount != 0)
964 1.47 sommerfe panic("spinlock_release_all: release of shared lock!");
965 1.47 sommerfe else
966 1.47 sommerfe panic("spinlock_release_all: release of unlocked lock!");
967 1.47 sommerfe #endif
968 1.86 perry INTERLOCK_RELEASE(lkp, LK_SPIN, s);
969 1.47 sommerfe
970 1.47 sommerfe return (count);
971 1.47 sommerfe }
972 1.47 sommerfe
973 1.47 sommerfe /*
974 1.47 sommerfe * For a recursive spinlock held one or more times by the current CPU,
975 1.47 sommerfe * release all N locks, and return N.
976 1.47 sommerfe * Intended for use in mi_switch() right after resuming execution.
977 1.47 sommerfe */
978 1.47 sommerfe
979 1.47 sommerfe void
980 1.50 thorpej #if defined(LOCKDEBUG)
981 1.91 perry _spinlock_acquire_count(volatile struct lock *lkp, int count,
982 1.50 thorpej const char *file, int line)
983 1.50 thorpej #else
984 1.91 perry spinlock_acquire_count(volatile struct lock *lkp, int count)
985 1.50 thorpej #endif
986 1.47 sommerfe {
987 1.47 sommerfe int s, error;
988 1.88 blymn cpuid_t cpu_num;
989 1.86 perry
990 1.47 sommerfe KASSERT(lkp->lk_flags & LK_SPIN);
991 1.86 perry
992 1.47 sommerfe INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
993 1.47 sommerfe
994 1.88 blymn cpu_num = cpu_number();
995 1.47 sommerfe
996 1.47 sommerfe #ifdef DIAGNOSTIC
997 1.88 blymn if (WEHOLDIT(lkp, LK_NOPROC, 0, cpu_num))
998 1.88 blymn panic("spinlock_acquire_count: processor %lu already holds lock", (long)cpu_num);
999 1.47 sommerfe #endif
1000 1.47 sommerfe /*
1001 1.47 sommerfe * Try to acquire the want_exclusive flag.
1002 1.47 sommerfe */
1003 1.98 ad error = acquire(&lkp, &s, LK_SPIN, 0, LK_HAVE_EXCL | LK_WANT_EXCL,
1004 1.98 ad RETURN_ADDRESS);
1005 1.47 sommerfe lkp->lk_flags |= LK_WANT_EXCL;
1006 1.47 sommerfe /*
1007 1.47 sommerfe * Wait for shared locks and upgrades to finish.
1008 1.47 sommerfe */
1009 1.78 hannken error = acquire(&lkp, &s, LK_SPIN, 0,
1010 1.98 ad LK_HAVE_EXCL | LK_SHARE_NONZERO | LK_WANT_UPGRADE,
1011 1.98 ad RETURN_ADDRESS);
1012 1.47 sommerfe lkp->lk_flags &= ~LK_WANT_EXCL;
1013 1.47 sommerfe lkp->lk_flags |= LK_HAVE_EXCL;
1014 1.88 blymn SETHOLDER(lkp, LK_NOPROC, 0, cpu_num);
1015 1.50 thorpej #if defined(LOCKDEBUG)
1016 1.50 thorpej lkp->lk_lock_file = file;
1017 1.50 thorpej lkp->lk_lock_line = line;
1018 1.50 thorpej #endif
1019 1.47 sommerfe HAVEIT(lkp);
1020 1.47 sommerfe if (lkp->lk_exclusivecount != 0)
1021 1.47 sommerfe panic("lockmgr: non-zero exclusive count");
1022 1.47 sommerfe lkp->lk_exclusivecount = count;
1023 1.47 sommerfe lkp->lk_recurselevel = 1;
1024 1.88 blymn COUNT_CPU(cpu_num, count);
1025 1.47 sommerfe
1026 1.86 perry INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
1027 1.47 sommerfe }
1028 1.47 sommerfe
1029 1.47 sommerfe
1030 1.47 sommerfe
1031 1.47 sommerfe /*
1032 1.1 fvdl * Print out information about state of a lock. Used by VOP_PRINT
1033 1.1 fvdl * routines to display ststus about contained locks.
1034 1.1 fvdl */
1035 1.2 fvdl void
1036 1.91 perry lockmgr_printinfo(volatile struct lock *lkp)
1037 1.1 fvdl {
1038 1.1 fvdl
1039 1.1 fvdl if (lkp->lk_sharecount)
1040 1.1 fvdl printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
1041 1.1 fvdl lkp->lk_sharecount);
1042 1.19 thorpej else if (lkp->lk_flags & LK_HAVE_EXCL) {
1043 1.19 thorpej printf(" lock type %s: EXCL (count %d) by ",
1044 1.19 thorpej lkp->lk_wmesg, lkp->lk_exclusivecount);
1045 1.19 thorpej if (lkp->lk_flags & LK_SPIN)
1046 1.19 thorpej printf("processor %lu", lkp->lk_cpu);
1047 1.19 thorpej else
1048 1.69 thorpej printf("pid %d.%d", lkp->lk_lockholder,
1049 1.69 thorpej lkp->lk_locklwp);
1050 1.19 thorpej } else
1051 1.19 thorpej printf(" not locked");
1052 1.19 thorpej if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
1053 1.1 fvdl printf(" with %d pending", lkp->lk_waitcount);
1054 1.1 fvdl }
1055 1.1 fvdl
1056 1.21 thorpej #if defined(LOCKDEBUG) /* { */
1057 1.91 perry _TAILQ_HEAD(, struct simplelock, volatile) simplelock_list =
1058 1.21 thorpej TAILQ_HEAD_INITIALIZER(simplelock_list);
1059 1.21 thorpej
1060 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1061 1.21 thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
1062 1.21 thorpej
1063 1.21 thorpej #define SLOCK_LIST_LOCK() \
1064 1.29 sommerfe __cpu_simple_lock(&simplelock_list_slock.lock_data)
1065 1.21 thorpej
1066 1.21 thorpej #define SLOCK_LIST_UNLOCK() \
1067 1.29 sommerfe __cpu_simple_unlock(&simplelock_list_slock.lock_data)
1068 1.21 thorpej
1069 1.21 thorpej #define SLOCK_COUNT(x) \
1070 1.47 sommerfe curcpu()->ci_simple_locks += (x)
1071 1.21 thorpej #else
1072 1.21 thorpej u_long simple_locks;
1073 1.21 thorpej
1074 1.21 thorpej #define SLOCK_LIST_LOCK() /* nothing */
1075 1.21 thorpej
1076 1.21 thorpej #define SLOCK_LIST_UNLOCK() /* nothing */
1077 1.21 thorpej
1078 1.21 thorpej #define SLOCK_COUNT(x) simple_locks += (x)
1079 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
1080 1.21 thorpej
1081 1.26 sommerfe #ifdef MULTIPROCESSOR
1082 1.75 wiz #define SLOCK_MP() lock_printf("on CPU %ld\n", \
1083 1.46 thorpej (u_long) cpu_number())
1084 1.26 sommerfe #else
1085 1.26 sommerfe #define SLOCK_MP() /* nothing */
1086 1.26 sommerfe #endif
1087 1.26 sommerfe
1088 1.21 thorpej #define SLOCK_WHERE(str, alp, id, l) \
1089 1.21 thorpej do { \
1090 1.58 chs lock_printf("\n"); \
1091 1.25 thorpej lock_printf(str); \
1092 1.33 thorpej lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
1093 1.26 sommerfe SLOCK_MP(); \
1094 1.21 thorpej if ((alp)->lock_file != NULL) \
1095 1.25 thorpej lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
1096 1.21 thorpej (alp)->lock_line); \
1097 1.21 thorpej if ((alp)->unlock_file != NULL) \
1098 1.25 thorpej lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
1099 1.21 thorpej (alp)->unlock_line); \
1100 1.58 chs SLOCK_TRACE() \
1101 1.21 thorpej SLOCK_DEBUGGER(); \
1102 1.30 thorpej } while (/*CONSTCOND*/0)
1103 1.12 chs
1104 1.1 fvdl /*
1105 1.1 fvdl * Simple lock functions so that the debugger can see from whence
1106 1.1 fvdl * they are being called.
1107 1.1 fvdl */
1108 1.1 fvdl void
1109 1.91 perry simple_lock_init(volatile struct simplelock *alp)
1110 1.1 fvdl {
1111 1.21 thorpej
1112 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1113 1.27 thorpej __cpu_simple_lock_init(&alp->lock_data);
1114 1.21 thorpej #else
1115 1.27 thorpej alp->lock_data = __SIMPLELOCK_UNLOCKED;
1116 1.21 thorpej #endif /* } */
1117 1.5 chs alp->lock_file = NULL;
1118 1.5 chs alp->lock_line = 0;
1119 1.5 chs alp->unlock_file = NULL;
1120 1.5 chs alp->unlock_line = 0;
1121 1.41 thorpej alp->lock_holder = LK_NOCPU;
1122 1.1 fvdl }
1123 1.1 fvdl
1124 1.1 fvdl void
1125 1.91 perry _simple_lock(volatile struct simplelock *alp, const char *id, int l)
1126 1.1 fvdl {
1127 1.88 blymn cpuid_t cpu_num = cpu_number();
1128 1.12 chs int s;
1129 1.12 chs
1130 1.44 thorpej s = spllock();
1131 1.21 thorpej
1132 1.21 thorpej /*
1133 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1134 1.21 thorpej * don't take any action, and just fall into the normal spin case.
1135 1.21 thorpej */
1136 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1137 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1138 1.88 blymn if (alp->lock_holder == cpu_num) {
1139 1.21 thorpej SLOCK_WHERE("simple_lock: locking against myself\n",
1140 1.21 thorpej alp, id, l);
1141 1.21 thorpej goto out;
1142 1.1 fvdl }
1143 1.21 thorpej #else
1144 1.21 thorpej SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
1145 1.21 thorpej goto out;
1146 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
1147 1.1 fvdl }
1148 1.21 thorpej
1149 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1150 1.21 thorpej /* Acquire the lock before modifying any fields. */
1151 1.70 pk splx(s);
1152 1.27 thorpej __cpu_simple_lock(&alp->lock_data);
1153 1.70 pk s = spllock();
1154 1.21 thorpej #else
1155 1.27 thorpej alp->lock_data = __SIMPLELOCK_LOCKED;
1156 1.21 thorpej #endif /* } */
1157 1.21 thorpej
1158 1.45 sommerfe if (alp->lock_holder != LK_NOCPU) {
1159 1.45 sommerfe SLOCK_WHERE("simple_lock: uninitialized lock\n",
1160 1.45 sommerfe alp, id, l);
1161 1.45 sommerfe }
1162 1.5 chs alp->lock_file = id;
1163 1.5 chs alp->lock_line = l;
1164 1.88 blymn alp->lock_holder = cpu_num;
1165 1.21 thorpej
1166 1.21 thorpej SLOCK_LIST_LOCK();
1167 1.87 christos TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
1168 1.21 thorpej SLOCK_LIST_UNLOCK();
1169 1.21 thorpej
1170 1.21 thorpej SLOCK_COUNT(1);
1171 1.21 thorpej
1172 1.21 thorpej out:
1173 1.18 chs splx(s);
1174 1.38 thorpej }
1175 1.38 thorpej
1176 1.38 thorpej int
1177 1.91 perry _simple_lock_held(volatile struct simplelock *alp)
1178 1.38 thorpej {
1179 1.54 enami #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
1180 1.88 blymn cpuid_t cpu_num = cpu_number();
1181 1.54 enami #endif
1182 1.38 thorpej int s, locked = 0;
1183 1.38 thorpej
1184 1.44 thorpej s = spllock();
1185 1.42 thorpej
1186 1.42 thorpej #if defined(MULTIPROCESSOR)
1187 1.38 thorpej if (__cpu_simple_lock_try(&alp->lock_data) == 0)
1188 1.88 blymn locked = (alp->lock_holder == cpu_num);
1189 1.38 thorpej else
1190 1.38 thorpej __cpu_simple_unlock(&alp->lock_data);
1191 1.38 thorpej #else
1192 1.42 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1193 1.42 thorpej locked = 1;
1194 1.88 blymn KASSERT(alp->lock_holder == cpu_num);
1195 1.42 thorpej }
1196 1.42 thorpej #endif
1197 1.38 thorpej
1198 1.38 thorpej splx(s);
1199 1.42 thorpej
1200 1.38 thorpej return (locked);
1201 1.1 fvdl }
1202 1.1 fvdl
1203 1.1 fvdl int
1204 1.91 perry _simple_lock_try(volatile struct simplelock *alp, const char *id, int l)
1205 1.1 fvdl {
1206 1.88 blymn cpuid_t cpu_num = cpu_number();
1207 1.21 thorpej int s, rv = 0;
1208 1.1 fvdl
1209 1.44 thorpej s = spllock();
1210 1.21 thorpej
1211 1.21 thorpej /*
1212 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
1213 1.21 thorpej * don't take any action.
1214 1.21 thorpej */
1215 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1216 1.27 thorpej if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
1217 1.88 blymn if (alp->lock_holder == cpu_num)
1218 1.21 thorpej SLOCK_WHERE("simple_lock_try: locking against myself\n",
1219 1.26 sommerfe alp, id, l);
1220 1.21 thorpej goto out;
1221 1.21 thorpej }
1222 1.21 thorpej #else
1223 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
1224 1.21 thorpej SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
1225 1.21 thorpej goto out;
1226 1.18 chs }
1227 1.27 thorpej alp->lock_data = __SIMPLELOCK_LOCKED;
1228 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
1229 1.21 thorpej
1230 1.21 thorpej /*
1231 1.21 thorpej * At this point, we have acquired the lock.
1232 1.21 thorpej */
1233 1.21 thorpej
1234 1.21 thorpej rv = 1;
1235 1.18 chs
1236 1.5 chs alp->lock_file = id;
1237 1.5 chs alp->lock_line = l;
1238 1.88 blymn alp->lock_holder = cpu_num;
1239 1.21 thorpej
1240 1.21 thorpej SLOCK_LIST_LOCK();
1241 1.87 christos TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
1242 1.21 thorpej SLOCK_LIST_UNLOCK();
1243 1.21 thorpej
1244 1.21 thorpej SLOCK_COUNT(1);
1245 1.21 thorpej
1246 1.21 thorpej out:
1247 1.12 chs splx(s);
1248 1.21 thorpej return (rv);
1249 1.1 fvdl }
1250 1.1 fvdl
1251 1.1 fvdl void
1252 1.91 perry _simple_unlock(volatile struct simplelock *alp, const char *id, int l)
1253 1.1 fvdl {
1254 1.12 chs int s;
1255 1.1 fvdl
1256 1.44 thorpej s = spllock();
1257 1.21 thorpej
1258 1.21 thorpej /*
1259 1.21 thorpej * MULTIPROCESSOR case: This is `safe' because we think we hold
1260 1.21 thorpej * the lock, and if we don't, we don't take any action.
1261 1.21 thorpej */
1262 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
1263 1.21 thorpej SLOCK_WHERE("simple_unlock: lock not held\n",
1264 1.21 thorpej alp, id, l);
1265 1.21 thorpej goto out;
1266 1.21 thorpej }
1267 1.21 thorpej
1268 1.21 thorpej SLOCK_LIST_LOCK();
1269 1.21 thorpej TAILQ_REMOVE(&simplelock_list, alp, list);
1270 1.21 thorpej SLOCK_LIST_UNLOCK();
1271 1.21 thorpej
1272 1.21 thorpej SLOCK_COUNT(-1);
1273 1.21 thorpej
1274 1.21 thorpej alp->list.tqe_next = NULL; /* sanity */
1275 1.21 thorpej alp->list.tqe_prev = NULL; /* sanity */
1276 1.21 thorpej
1277 1.5 chs alp->unlock_file = id;
1278 1.5 chs alp->unlock_line = l;
1279 1.21 thorpej
1280 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1281 1.26 sommerfe alp->lock_holder = LK_NOCPU;
1282 1.21 thorpej /* Now that we've modified all fields, release the lock. */
1283 1.27 thorpej __cpu_simple_unlock(&alp->lock_data);
1284 1.21 thorpej #else
1285 1.27 thorpej alp->lock_data = __SIMPLELOCK_UNLOCKED;
1286 1.41 thorpej KASSERT(alp->lock_holder == cpu_number());
1287 1.41 thorpej alp->lock_holder = LK_NOCPU;
1288 1.21 thorpej #endif /* } */
1289 1.21 thorpej
1290 1.21 thorpej out:
1291 1.18 chs splx(s);
1292 1.12 chs }
1293 1.12 chs
1294 1.12 chs void
1295 1.33 thorpej simple_lock_dump(void)
1296 1.12 chs {
1297 1.91 perry volatile struct simplelock *alp;
1298 1.12 chs int s;
1299 1.12 chs
1300 1.44 thorpej s = spllock();
1301 1.21 thorpej SLOCK_LIST_LOCK();
1302 1.25 thorpej lock_printf("all simple locks:\n");
1303 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1304 1.25 thorpej lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
1305 1.21 thorpej alp->lock_file, alp->lock_line);
1306 1.12 chs }
1307 1.21 thorpej SLOCK_LIST_UNLOCK();
1308 1.12 chs splx(s);
1309 1.12 chs }
1310 1.12 chs
1311 1.12 chs void
1312 1.33 thorpej simple_lock_freecheck(void *start, void *end)
1313 1.12 chs {
1314 1.91 perry volatile struct simplelock *alp;
1315 1.12 chs int s;
1316 1.12 chs
1317 1.44 thorpej s = spllock();
1318 1.21 thorpej SLOCK_LIST_LOCK();
1319 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1320 1.91 perry if ((volatile void *)alp >= start &&
1321 1.91 perry (volatile void *)alp < end) {
1322 1.25 thorpej lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
1323 1.34 thorpej alp, alp->lock_holder, alp->lock_file,
1324 1.34 thorpej alp->lock_line);
1325 1.34 thorpej SLOCK_DEBUGGER();
1326 1.34 thorpej }
1327 1.34 thorpej }
1328 1.34 thorpej SLOCK_LIST_UNLOCK();
1329 1.34 thorpej splx(s);
1330 1.34 thorpej }
1331 1.34 thorpej
1332 1.55 thorpej /*
1333 1.55 thorpej * We must be holding exactly one lock: the sched_lock.
1334 1.55 thorpej */
1335 1.55 thorpej
1336 1.34 thorpej void
1337 1.34 thorpej simple_lock_switchcheck(void)
1338 1.34 thorpej {
1339 1.55 thorpej
1340 1.55 thorpej simple_lock_only_held(&sched_lock, "switching");
1341 1.55 thorpej }
1342 1.55 thorpej
1343 1.93 erh /*
1344 1.93 erh * Drop into the debugger if lp isn't the only lock held.
1345 1.93 erh * lp may be NULL.
1346 1.93 erh */
1347 1.55 thorpej void
1348 1.55 thorpej simple_lock_only_held(volatile struct simplelock *lp, const char *where)
1349 1.55 thorpej {
1350 1.91 perry volatile struct simplelock *alp;
1351 1.88 blymn cpuid_t cpu_num = cpu_number();
1352 1.34 thorpej int s;
1353 1.34 thorpej
1354 1.55 thorpej if (lp) {
1355 1.55 thorpej LOCK_ASSERT(simple_lock_held(lp));
1356 1.55 thorpej }
1357 1.44 thorpej s = spllock();
1358 1.34 thorpej SLOCK_LIST_LOCK();
1359 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1360 1.55 thorpej if (alp == lp)
1361 1.42 thorpej continue;
1362 1.85 yamt #if defined(MULTIPROCESSOR)
1363 1.85 yamt if (alp == &kernel_lock)
1364 1.85 yamt continue;
1365 1.85 yamt #endif /* defined(MULTIPROCESSOR) */
1366 1.88 blymn if (alp->lock_holder == cpu_num)
1367 1.55 thorpej break;
1368 1.12 chs }
1369 1.21 thorpej SLOCK_LIST_UNLOCK();
1370 1.12 chs splx(s);
1371 1.55 thorpej
1372 1.55 thorpej if (alp != NULL) {
1373 1.58 chs lock_printf("\n%s with held simple_lock %p "
1374 1.55 thorpej "CPU %lu %s:%d\n",
1375 1.55 thorpej where, alp, alp->lock_holder, alp->lock_file,
1376 1.55 thorpej alp->lock_line);
1377 1.58 chs SLOCK_TRACE();
1378 1.55 thorpej SLOCK_DEBUGGER();
1379 1.55 thorpej }
1380 1.1 fvdl }
1381 1.94 erh
1382 1.94 erh /*
1383 1.94 erh * Set to 1 by simple_lock_assert_*().
1384 1.94 erh * Can be cleared from ddb to avoid a panic.
1385 1.94 erh */
1386 1.94 erh int slock_assert_will_panic;
1387 1.94 erh
1388 1.94 erh /*
1389 1.94 erh * If the lock isn't held, print a traceback, optionally drop into the
1390 1.94 erh * debugger, then panic.
1391 1.94 erh * The panic can be avoided by clearing slock_assert_with_panic from the
1392 1.94 erh * debugger.
1393 1.94 erh */
1394 1.94 erh void
1395 1.94 erh _simple_lock_assert_locked(volatile struct simplelock *alp,
1396 1.94 erh const char *lockname, const char *id, int l)
1397 1.94 erh {
1398 1.94 erh if (simple_lock_held(alp) == 0) {
1399 1.94 erh slock_assert_will_panic = 1;
1400 1.94 erh lock_printf("%s lock not held\n", lockname);
1401 1.94 erh SLOCK_WHERE("lock not held", alp, id, l);
1402 1.94 erh if (slock_assert_will_panic)
1403 1.94 erh panic("%s: not locked", lockname);
1404 1.94 erh }
1405 1.94 erh }
1406 1.94 erh
1407 1.94 erh void
1408 1.94 erh _simple_lock_assert_unlocked(volatile struct simplelock *alp,
1409 1.94 erh const char *lockname, const char *id, int l)
1410 1.94 erh {
1411 1.94 erh if (simple_lock_held(alp)) {
1412 1.94 erh slock_assert_will_panic = 1;
1413 1.94 erh lock_printf("%s lock held\n", lockname);
1414 1.94 erh SLOCK_WHERE("lock held", alp, id, l);
1415 1.94 erh if (slock_assert_will_panic)
1416 1.94 erh panic("%s: locked", lockname);
1417 1.94 erh }
1418 1.94 erh }
1419 1.94 erh
1420 1.96 yamt void
1421 1.96 yamt assert_sleepable(struct simplelock *interlock, const char *msg)
1422 1.96 yamt {
1423 1.96 yamt
1424 1.97 yamt if (curlwp == NULL) {
1425 1.97 yamt panic("assert_sleepable: NULL curlwp");
1426 1.97 yamt }
1427 1.96 yamt spinlock_switchcheck();
1428 1.96 yamt simple_lock_only_held(interlock, msg);
1429 1.96 yamt }
1430 1.96 yamt
1431 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
1432 1.62 thorpej
1433 1.62 thorpej #if defined(MULTIPROCESSOR)
1434 1.62 thorpej /*
1435 1.62 thorpej * Functions for manipulating the kernel_lock. We put them here
1436 1.62 thorpej * so that they show up in profiles.
1437 1.62 thorpej */
1438 1.62 thorpej
1439 1.85 yamt /*
1440 1.85 yamt * splbiglock: block IPLs which need to grab kernel_lock.
1441 1.85 yamt * XXX splvm or splaudio should be enough.
1442 1.85 yamt */
1443 1.85 yamt #if !defined(__HAVE_SPLBIGLOCK)
1444 1.85 yamt #define splbiglock() splclock()
1445 1.85 yamt #endif
1446 1.62 thorpej
1447 1.62 thorpej void
1448 1.62 thorpej _kernel_lock_init(void)
1449 1.62 thorpej {
1450 1.62 thorpej
1451 1.85 yamt simple_lock_init(&kernel_lock);
1452 1.62 thorpej }
1453 1.62 thorpej
1454 1.62 thorpej /*
1455 1.62 thorpej * Acquire/release the kernel lock. Intended for use in the scheduler
1456 1.62 thorpej * and the lower half of the kernel.
1457 1.62 thorpej */
1458 1.62 thorpej void
1459 1.62 thorpej _kernel_lock(int flag)
1460 1.62 thorpej {
1461 1.85 yamt struct cpu_info *ci = curcpu();
1462 1.62 thorpej
1463 1.62 thorpej SCHED_ASSERT_UNLOCKED();
1464 1.85 yamt
1465 1.85 yamt if (ci->ci_data.cpu_biglock_count > 0) {
1466 1.85 yamt LOCK_ASSERT(simple_lock_held(&kernel_lock));
1467 1.85 yamt ci->ci_data.cpu_biglock_count++;
1468 1.85 yamt } else {
1469 1.85 yamt int s;
1470 1.85 yamt
1471 1.85 yamt s = splbiglock();
1472 1.85 yamt while (!simple_lock_try(&kernel_lock)) {
1473 1.85 yamt splx(s);
1474 1.85 yamt SPINLOCK_SPIN_HOOK;
1475 1.85 yamt s = splbiglock();
1476 1.85 yamt }
1477 1.85 yamt ci->ci_data.cpu_biglock_count++;
1478 1.85 yamt splx(s);
1479 1.85 yamt }
1480 1.62 thorpej }
1481 1.62 thorpej
1482 1.62 thorpej void
1483 1.62 thorpej _kernel_unlock(void)
1484 1.62 thorpej {
1485 1.85 yamt struct cpu_info *ci = curcpu();
1486 1.85 yamt int s;
1487 1.85 yamt
1488 1.85 yamt KASSERT(ci->ci_data.cpu_biglock_count > 0);
1489 1.62 thorpej
1490 1.85 yamt s = splbiglock();
1491 1.85 yamt if ((--ci->ci_data.cpu_biglock_count) == 0)
1492 1.85 yamt simple_unlock(&kernel_lock);
1493 1.85 yamt splx(s);
1494 1.62 thorpej }
1495 1.62 thorpej
1496 1.62 thorpej /*
1497 1.62 thorpej * Acquire/release the kernel_lock on behalf of a process. Intended for
1498 1.62 thorpej * use in the top half of the kernel.
1499 1.62 thorpej */
1500 1.62 thorpej void
1501 1.69 thorpej _kernel_proc_lock(struct lwp *l)
1502 1.62 thorpej {
1503 1.62 thorpej
1504 1.62 thorpej SCHED_ASSERT_UNLOCKED();
1505 1.85 yamt _kernel_lock(0);
1506 1.62 thorpej }
1507 1.62 thorpej
1508 1.62 thorpej void
1509 1.69 thorpej _kernel_proc_unlock(struct lwp *l)
1510 1.62 thorpej {
1511 1.62 thorpej
1512 1.85 yamt _kernel_unlock();
1513 1.62 thorpej }
1514 1.77 yamt
1515 1.77 yamt int
1516 1.77 yamt _kernel_lock_release_all()
1517 1.77 yamt {
1518 1.85 yamt struct cpu_info *ci = curcpu();
1519 1.77 yamt int hold_count;
1520 1.77 yamt
1521 1.85 yamt hold_count = ci->ci_data.cpu_biglock_count;
1522 1.85 yamt
1523 1.85 yamt if (hold_count) {
1524 1.85 yamt int s;
1525 1.85 yamt
1526 1.85 yamt s = splbiglock();
1527 1.85 yamt ci->ci_data.cpu_biglock_count = 0;
1528 1.85 yamt simple_unlock(&kernel_lock);
1529 1.85 yamt splx(s);
1530 1.85 yamt }
1531 1.77 yamt
1532 1.77 yamt return hold_count;
1533 1.77 yamt }
1534 1.77 yamt
1535 1.77 yamt void
1536 1.77 yamt _kernel_lock_acquire_count(int hold_count)
1537 1.77 yamt {
1538 1.77 yamt
1539 1.85 yamt KASSERT(curcpu()->ci_data.cpu_biglock_count == 0);
1540 1.85 yamt
1541 1.85 yamt if (hold_count != 0) {
1542 1.85 yamt struct cpu_info *ci = curcpu();
1543 1.85 yamt int s;
1544 1.85 yamt
1545 1.85 yamt s = splbiglock();
1546 1.85 yamt while (!simple_lock_try(&kernel_lock)) {
1547 1.85 yamt splx(s);
1548 1.85 yamt SPINLOCK_SPIN_HOOK;
1549 1.85 yamt s = splbiglock();
1550 1.85 yamt }
1551 1.85 yamt ci->ci_data.cpu_biglock_count = hold_count;
1552 1.85 yamt splx(s);
1553 1.85 yamt }
1554 1.77 yamt }
1555 1.84 yamt #if defined(DEBUG)
1556 1.84 yamt void
1557 1.84 yamt _kernel_lock_assert_locked()
1558 1.84 yamt {
1559 1.95 erh simple_lock_assert_locked(&kernel_lock, "kernel_lock");
1560 1.84 yamt }
1561 1.84 yamt #endif
1562 1.94 erh
1563 1.99 ad int
1564 1.99 ad lock_owner_onproc(uintptr_t owner)
1565 1.99 ad {
1566 1.99 ad CPU_INFO_ITERATOR cii;
1567 1.99 ad struct cpu_info *ci;
1568 1.99 ad
1569 1.99 ad for (CPU_INFO_FOREACH(cii, ci))
1570 1.99 ad if (owner == (uintptr_t)ci || owner == (uintptr_t)ci->ci_curlwp)
1571 1.99 ad return (1);
1572 1.99 ad
1573 1.99 ad return (0);
1574 1.99 ad }
1575 1.99 ad
1576 1.99 ad #else /* MULTIPROCESSOR */
1577 1.99 ad
1578 1.99 ad int
1579 1.99 ad lock_owner_onproc(uintptr_t owner)
1580 1.99 ad {
1581 1.99 ad
1582 1.99 ad return 0;
1583 1.99 ad }
1584 1.99 ad
1585 1.62 thorpej #endif /* MULTIPROCESSOR */
1586