kern_lock.c revision 1.110.2.1 1 1.110.2.1 ad /* $NetBSD: kern_lock.c,v 1.110.2.1 2007/03/13 17:50:52 ad Exp $ */
2 1.19 thorpej
3 1.19 thorpej /*-
4 1.105 ad * Copyright (c) 1999, 2000, 2006 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.105 ad * NASA Ames Research Center, and by Andrew Doran.
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.110.2.1 ad __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.110.2.1 2007/03/13 17:50:52 ad Exp $");
80 1.7 thorpej
81 1.21 thorpej #include "opt_multiprocessor.h"
82 1.18 chs #include "opt_ddb.h"
83 1.1 fvdl
84 1.105 ad #define __MUTEX_PRIVATE
85 1.105 ad
86 1.1 fvdl #include <sys/param.h>
87 1.1 fvdl #include <sys/proc.h>
88 1.1 fvdl #include <sys/lock.h>
89 1.2 fvdl #include <sys/systm.h>
90 1.105 ad #include <sys/lockdebug.h>
91 1.105 ad
92 1.1 fvdl #include <machine/cpu.h>
93 1.110 christos #include <machine/stdarg.h>
94 1.1 fvdl
95 1.98 ad #include <dev/lockstat.h>
96 1.98 ad
97 1.25 thorpej #if defined(LOCKDEBUG)
98 1.25 thorpej #include <sys/syslog.h>
99 1.25 thorpej /*
100 1.25 thorpej * note that stdarg.h and the ansi style va_start macro is used for both
101 1.25 thorpej * ansi and traditional c compiles.
102 1.25 thorpej * XXX: this requires that stdarg.h define: va_alist and va_dcl
103 1.25 thorpej */
104 1.25 thorpej #include <machine/stdarg.h>
105 1.25 thorpej
106 1.36 thorpej void lock_printf(const char *fmt, ...)
107 1.37 eeh __attribute__((__format__(__printf__,1,2)));
108 1.25 thorpej
109 1.105 ad static int acquire(volatile struct lock **, int *, int, int, int, uintptr_t);
110 1.73 yamt
111 1.57 sommerfe int lock_debug_syslog = 0; /* defaults to printf, but can be patched */
112 1.55 thorpej
113 1.55 thorpej #ifdef DDB
114 1.55 thorpej #include <ddb/ddbvar.h>
115 1.55 thorpej #include <machine/db_machdep.h>
116 1.55 thorpej #include <ddb/db_command.h>
117 1.55 thorpej #include <ddb/db_interface.h>
118 1.55 thorpej #endif
119 1.85 yamt #endif /* defined(LOCKDEBUG) */
120 1.85 yamt
121 1.85 yamt #if defined(MULTIPROCESSOR)
122 1.105 ad /*
123 1.105 ad * IPL_BIGLOCK: block IPLs which need to grab kernel_mutex.
124 1.105 ad * XXX IPL_VM or IPL_AUDIO should be enough.
125 1.105 ad */
126 1.105 ad #if !defined(__HAVE_SPLBIGLOCK)
127 1.105 ad #define splbiglock splclock
128 1.105 ad #endif
129 1.105 ad __cpu_simple_lock_t kernel_lock;
130 1.105 ad int kernel_lock_id;
131 1.25 thorpej #endif
132 1.25 thorpej
133 1.1 fvdl /*
134 1.1 fvdl * Locking primitives implementation.
135 1.56 wiz * Locks provide shared/exclusive synchronization.
136 1.1 fvdl */
137 1.1 fvdl
138 1.21 thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
139 1.110.2.1 ad #define COUNT(lkp, l, cpu_id, x) (l)->l_locks += (x)
140 1.1 fvdl #else
141 1.22 mellon #define COUNT(lkp, p, cpu_id, x)
142 1.21 thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
143 1.1 fvdl
144 1.63 chs #ifdef DDB /* { */
145 1.89 chs #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
146 1.63 chs int simple_lock_debugger = 1; /* more serious on MP */
147 1.63 chs #else
148 1.63 chs int simple_lock_debugger = 0;
149 1.63 chs #endif
150 1.93 erh #define SLOCK_DEBUGGER() if (simple_lock_debugger && db_onpanic) Debugger()
151 1.63 chs #define SLOCK_TRACE() \
152 1.63 chs db_stack_trace_print((db_expr_t)__builtin_frame_address(0), \
153 1.108 thorpej true, 65535, "", lock_printf);
154 1.63 chs #else
155 1.63 chs #define SLOCK_DEBUGGER() /* nothing */
156 1.63 chs #define SLOCK_TRACE() /* nothing */
157 1.63 chs #endif /* } */
158 1.63 chs
159 1.98 ad #define RETURN_ADDRESS ((uintptr_t)__builtin_return_address(0))
160 1.98 ad
161 1.1 fvdl /*
162 1.1 fvdl * Acquire a resource.
163 1.1 fvdl */
164 1.73 yamt static int
165 1.91 perry acquire(volatile struct lock **lkpp, int *s, int extflags,
166 1.102 yamt int drain, int wanted, uintptr_t ra)
167 1.73 yamt {
168 1.73 yamt int error;
169 1.91 perry volatile struct lock *lkp = *lkpp;
170 1.98 ad LOCKSTAT_TIMER(slptime);
171 1.105 ad LOCKSTAT_FLAG(lsflag);
172 1.73 yamt
173 1.73 yamt KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
174 1.73 yamt
175 1.110.2.1 ad LOCKSTAT_ENTER(lsflag);
176 1.73 yamt
177 1.110.2.1 ad for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
178 1.110.2.1 ad if (drain)
179 1.110.2.1 ad lkp->lk_flags |= LK_WAITDRAIN;
180 1.110.2.1 ad else {
181 1.73 yamt lkp->lk_waitcount++;
182 1.73 yamt lkp->lk_flags |= LK_WAIT_NONZERO;
183 1.73 yamt }
184 1.110.2.1 ad /* XXX Cast away volatile. */
185 1.110.2.1 ad LOCKSTAT_START_TIMER(lsflag, slptime);
186 1.110.2.1 ad error = mtsleep(drain ?
187 1.110.2.1 ad (volatile const void *)&lkp->lk_flags :
188 1.110.2.1 ad (volatile const void *)lkp, lkp->lk_prio,
189 1.110.2.1 ad lkp->lk_wmesg, lkp->lk_timo,
190 1.110.2.1 ad __UNVOLATILE(&lkp->lk_interlock));
191 1.110.2.1 ad LOCKSTAT_STOP_TIMER(lsflag, slptime);
192 1.110.2.1 ad LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
193 1.110.2.1 ad LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
194 1.73 yamt if (!drain) {
195 1.73 yamt lkp->lk_waitcount--;
196 1.73 yamt if (lkp->lk_waitcount == 0)
197 1.73 yamt lkp->lk_flags &= ~LK_WAIT_NONZERO;
198 1.73 yamt }
199 1.110.2.1 ad if (error)
200 1.110.2.1 ad break;
201 1.110.2.1 ad if (extflags & LK_SLEEPFAIL) {
202 1.110.2.1 ad error = ENOLCK;
203 1.110.2.1 ad break;
204 1.110.2.1 ad }
205 1.110.2.1 ad if (lkp->lk_newlock != NULL) {
206 1.110.2.1 ad mutex_enter(__UNVOLATILE
207 1.110.2.1 ad (&lkp->lk_newlock->lk_interlock));
208 1.110.2.1 ad mutex_exit(__UNVOLATILE
209 1.110.2.1 ad (&lkp->lk_interlock));
210 1.110.2.1 ad if (lkp->lk_waitcount == 0)
211 1.110.2.1 ad wakeup(&lkp->lk_newlock);
212 1.110.2.1 ad *lkpp = lkp = lkp->lk_newlock;
213 1.73 yamt }
214 1.1 fvdl }
215 1.1 fvdl
216 1.110.2.1 ad LOCKSTAT_EXIT(lsflag);
217 1.110.2.1 ad
218 1.73 yamt return error;
219 1.73 yamt }
220 1.73 yamt
221 1.69 thorpej #define SETHOLDER(lkp, pid, lid, cpu_id) \
222 1.19 thorpej do { \
223 1.110.2.1 ad (lkp)->lk_lockholder = pid; \
224 1.110.2.1 ad (lkp)->lk_locklwp = lid; \
225 1.30 thorpej } while (/*CONSTCOND*/0)
226 1.19 thorpej
227 1.69 thorpej #define WEHOLDIT(lkp, pid, lid, cpu_id) \
228 1.110.2.1 ad ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
229 1.19 thorpej
230 1.23 thorpej #define WAKEUP_WAITER(lkp) \
231 1.23 thorpej do { \
232 1.110.2.1 ad if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) { \
233 1.87 christos wakeup((lkp)); \
234 1.23 thorpej } \
235 1.30 thorpej } while (/*CONSTCOND*/0)
236 1.23 thorpej
237 1.25 thorpej #if defined(LOCKDEBUG)
238 1.25 thorpej /*
239 1.25 thorpej * Lock debug printing routine; can be configured to print to console
240 1.25 thorpej * or log to syslog.
241 1.25 thorpej */
242 1.25 thorpej void
243 1.25 thorpej lock_printf(const char *fmt, ...)
244 1.25 thorpej {
245 1.68 pk char b[150];
246 1.25 thorpej va_list ap;
247 1.25 thorpej
248 1.25 thorpej va_start(ap, fmt);
249 1.25 thorpej if (lock_debug_syslog)
250 1.25 thorpej vlog(LOG_DEBUG, fmt, ap);
251 1.68 pk else {
252 1.68 pk vsnprintf(b, sizeof(b), fmt, ap);
253 1.68 pk printf_nolog("%s", b);
254 1.68 pk }
255 1.25 thorpej va_end(ap);
256 1.25 thorpej }
257 1.25 thorpej #endif /* LOCKDEBUG */
258 1.25 thorpej
259 1.110 christos static void
260 1.110 christos lockpanic(volatile struct lock *lkp, const char *fmt, ...)
261 1.110 christos {
262 1.110 christos char s[150], b[150];
263 1.110 christos #ifdef LOCKDEBUG
264 1.110 christos static const char *locktype[] = {
265 1.110 christos "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
266 1.110 christos "downgrade", "release", "drain", "exclother", "*9*",
267 1.110 christos "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
268 1.110 christos };
269 1.110 christos #endif
270 1.110 christos
271 1.110 christos va_list ap;
272 1.110 christos va_start(ap, fmt);
273 1.110 christos vsnprintf(s, sizeof(s), fmt, ap);
274 1.110 christos va_end(ap);
275 1.110 christos bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
276 1.110 christos panic("%s ("
277 1.110 christos #ifdef LOCKDEBUG
278 1.110 christos "type %s "
279 1.110 christos #endif
280 1.110 christos "flags %s, sharecount %d, exclusivecount %d, "
281 1.110 christos "recurselevel %d, waitcount %d, wmesg %s"
282 1.110 christos #ifdef LOCKDEBUG
283 1.110 christos ", lock_file %s, unlock_file %s, lock_line %d, unlock_line %d"
284 1.110 christos #endif
285 1.110 christos ")\n",
286 1.110 christos s,
287 1.110 christos #ifdef LOCKDEBUG
288 1.110 christos locktype[lkp->lk_flags & LK_TYPE_MASK],
289 1.110 christos #endif
290 1.110 christos b, lkp->lk_sharecount, lkp->lk_exclusivecount,
291 1.110 christos lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg
292 1.110 christos #ifdef LOCKDEBUG
293 1.110 christos , lkp->lk_lock_file, lkp->lk_unlock_file, lkp->lk_lock_line,
294 1.110 christos lkp->lk_unlock_line
295 1.110 christos #endif
296 1.110 christos );
297 1.110 christos }
298 1.110 christos
299 1.1 fvdl /*
300 1.78 hannken * Transfer any waiting processes from one lock to another.
301 1.78 hannken */
302 1.78 hannken void
303 1.78 hannken transferlockers(struct lock *from, struct lock *to)
304 1.78 hannken {
305 1.78 hannken
306 1.78 hannken KASSERT(from != to);
307 1.78 hannken KASSERT((from->lk_flags & LK_WAITDRAIN) == 0);
308 1.78 hannken if (from->lk_waitcount == 0)
309 1.78 hannken return;
310 1.78 hannken from->lk_newlock = to;
311 1.78 hannken wakeup((void *)from);
312 1.78 hannken tsleep((void *)&from->lk_newlock, from->lk_prio, "lkxfer", 0);
313 1.78 hannken from->lk_newlock = NULL;
314 1.78 hannken from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
315 1.78 hannken KASSERT(from->lk_waitcount == 0);
316 1.78 hannken }
317 1.78 hannken
318 1.78 hannken
319 1.78 hannken /*
320 1.1 fvdl * Initialize a lock; required before use.
321 1.1 fvdl */
322 1.1 fvdl void
323 1.109 yamt lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
324 1.1 fvdl {
325 1.1 fvdl
326 1.8 perry memset(lkp, 0, sizeof(struct lock));
327 1.1 fvdl lkp->lk_flags = flags & LK_EXTFLG_MASK;
328 1.110.2.1 ad mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
329 1.110.2.1 ad lkp->lk_lockholder = LK_NOPROC;
330 1.110.2.1 ad lkp->lk_newlock = NULL;
331 1.110.2.1 ad lkp->lk_prio = prio;
332 1.110.2.1 ad lkp->lk_timo = timo;
333 1.110.2.1 ad lkp->lk_wmesg = wmesg;
334 1.50 thorpej #if defined(LOCKDEBUG)
335 1.50 thorpej lkp->lk_lock_file = NULL;
336 1.50 thorpej lkp->lk_unlock_file = NULL;
337 1.50 thorpej #endif
338 1.1 fvdl }
339 1.1 fvdl
340 1.1 fvdl /*
341 1.1 fvdl * Determine the status of a lock.
342 1.1 fvdl */
343 1.1 fvdl int
344 1.33 thorpej lockstatus(struct lock *lkp)
345 1.1 fvdl {
346 1.76 yamt int lock_type = 0;
347 1.76 yamt struct lwp *l = curlwp; /* XXX */
348 1.76 yamt pid_t pid;
349 1.76 yamt lwpid_t lid;
350 1.88 blymn cpuid_t cpu_num;
351 1.76 yamt
352 1.110.2.1 ad if (l == NULL) {
353 1.88 blymn cpu_num = cpu_number();
354 1.76 yamt pid = LK_KERNPROC;
355 1.76 yamt lid = 0;
356 1.76 yamt } else {
357 1.88 blymn cpu_num = LK_NOCPU;
358 1.76 yamt pid = l->l_proc->p_pid;
359 1.76 yamt lid = l->l_lid;
360 1.76 yamt }
361 1.1 fvdl
362 1.110.2.1 ad mutex_enter(&lkp->lk_interlock);
363 1.76 yamt if (lkp->lk_exclusivecount != 0) {
364 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num))
365 1.76 yamt lock_type = LK_EXCLUSIVE;
366 1.76 yamt else
367 1.76 yamt lock_type = LK_EXCLOTHER;
368 1.76 yamt } else if (lkp->lk_sharecount != 0)
369 1.1 fvdl lock_type = LK_SHARED;
370 1.103 chs else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
371 1.103 chs lock_type = LK_EXCLOTHER;
372 1.110.2.1 ad mutex_exit(__UNVOLATILE(&lkp->lk_interlock));
373 1.1 fvdl return (lock_type);
374 1.1 fvdl }
375 1.35 thorpej
376 1.1 fvdl /*
377 1.44 thorpej * Locks and IPLs (interrupt priority levels):
378 1.44 thorpej *
379 1.44 thorpej * Locks which may be taken from interrupt context must be handled
380 1.44 thorpej * very carefully; you must spl to the highest IPL where the lock
381 1.44 thorpej * is needed before acquiring the lock.
382 1.44 thorpej *
383 1.44 thorpej * In addition, the lock-debugging hooks themselves need to use locks!
384 1.44 thorpej *
385 1.44 thorpej * A raw __cpu_simple_lock may be used from interrupts are long as it
386 1.44 thorpej * is acquired and held at a single IPL.
387 1.44 thorpej *
388 1.44 thorpej * A simple_lock (which is a __cpu_simple_lock wrapped with some
389 1.44 thorpej * debugging hooks) may be used at or below spllock(), which is
390 1.44 thorpej * typically at or just below splhigh() (i.e. blocks everything
391 1.44 thorpej * but certain machine-dependent extremely high priority interrupts).
392 1.44 thorpej *
393 1.44 thorpej * Some platforms may have interrupts of higher priority than splsched(),
394 1.44 thorpej * including hard serial interrupts, inter-processor interrupts, and
395 1.44 thorpej * kernel debugger traps.
396 1.44 thorpej */
397 1.44 thorpej
398 1.44 thorpej /*
399 1.32 sommerfe * XXX XXX kludge around another kludge..
400 1.32 sommerfe *
401 1.32 sommerfe * vfs_shutdown() may be called from interrupt context, either as a result
402 1.32 sommerfe * of a panic, or from the debugger. It proceeds to call
403 1.32 sommerfe * sys_sync(&proc0, ...), pretending its running on behalf of proc0
404 1.32 sommerfe *
405 1.32 sommerfe * We would like to make an attempt to sync the filesystems in this case, so
406 1.32 sommerfe * if this happens, we treat attempts to acquire locks specially.
407 1.32 sommerfe * All locks are acquired on behalf of proc0.
408 1.32 sommerfe *
409 1.32 sommerfe * If we've already paniced, we don't block waiting for locks, but
410 1.32 sommerfe * just barge right ahead since we're already going down in flames.
411 1.32 sommerfe */
412 1.32 sommerfe
413 1.32 sommerfe /*
414 1.1 fvdl * Set, change, or release a lock.
415 1.1 fvdl *
416 1.1 fvdl * Shared requests increment the shared count. Exclusive requests set the
417 1.1 fvdl * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
418 1.1 fvdl * accepted shared locks and shared-to-exclusive upgrades to go away.
419 1.1 fvdl */
420 1.1 fvdl int
421 1.50 thorpej #if defined(LOCKDEBUG)
422 1.91 perry _lockmgr(volatile struct lock *lkp, u_int flags,
423 1.110.2.1 ad kmutex_t *interlkp, const char *file, int line)
424 1.50 thorpej #else
425 1.91 perry lockmgr(volatile struct lock *lkp, u_int flags,
426 1.110.2.1 ad kmutex_t *interlkp)
427 1.50 thorpej #endif
428 1.1 fvdl {
429 1.1 fvdl int error;
430 1.1 fvdl pid_t pid;
431 1.69 thorpej lwpid_t lid;
432 1.1 fvdl int extflags;
433 1.88 blymn cpuid_t cpu_num;
434 1.69 thorpej struct lwp *l = curlwp;
435 1.32 sommerfe int lock_shutdown_noblock = 0;
436 1.110.2.1 ad kmutex_t *mutex;
437 1.67 scw int s = 0;
438 1.1 fvdl
439 1.1 fvdl error = 0;
440 1.110.2.1 ad mutex = __UNVOLATILE(&lkp->lk_interlock);
441 1.19 thorpej
442 1.80 yamt /* LK_RETRY is for vn_lock, not for lockmgr. */
443 1.79 yamt KASSERT((flags & LK_RETRY) == 0);
444 1.79 yamt
445 1.110.2.1 ad mutex_enter(mutex);
446 1.1 fvdl if (flags & LK_INTERLOCK)
447 1.110.2.1 ad mutex_exit(__UNVOLATILE(interlkp));
448 1.1 fvdl extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
449 1.19 thorpej
450 1.110.2.1 ad if (l == NULL) {
451 1.110.2.1 ad if (!doing_shutdown) {
452 1.110.2.1 ad panic("lockmgr: no context");
453 1.110.2.1 ad } else {
454 1.110.2.1 ad l = &lwp0;
455 1.110.2.1 ad if (panicstr && (!(flags & LK_NOWAIT))) {
456 1.110.2.1 ad flags |= LK_NOWAIT;
457 1.110.2.1 ad lock_shutdown_noblock = 1;
458 1.32 sommerfe }
459 1.32 sommerfe }
460 1.19 thorpej }
461 1.110.2.1 ad lid = l->l_lid;
462 1.110.2.1 ad pid = l->l_proc->p_pid;
463 1.88 blymn cpu_num = cpu_number();
464 1.19 thorpej
465 1.1 fvdl /*
466 1.1 fvdl * Once a lock has drained, the LK_DRAINING flag is set and an
467 1.1 fvdl * exclusive lock is returned. The only valid operation thereafter
468 1.1 fvdl * is a single release of that exclusive lock. This final release
469 1.1 fvdl * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
470 1.1 fvdl * further requests of any sort will result in a panic. The bits
471 1.1 fvdl * selected for these two flags are chosen so that they will be set
472 1.1 fvdl * in memory that is freed (freed memory is filled with 0xdeadbeef).
473 1.1 fvdl * The final release is permitted to give a new lease on life to
474 1.1 fvdl * the lock by specifying LK_REENABLE.
475 1.1 fvdl */
476 1.1 fvdl if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
477 1.28 thorpej #ifdef DIAGNOSTIC /* { */
478 1.1 fvdl if (lkp->lk_flags & LK_DRAINED)
479 1.110 christos lockpanic(lkp, "lockmgr: using decommissioned lock");
480 1.1 fvdl if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
481 1.88 blymn WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
482 1.110 christos lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
483 1.1 fvdl flags & LK_TYPE_MASK);
484 1.28 thorpej #endif /* DIAGNOSTIC */ /* } */
485 1.1 fvdl lkp->lk_flags &= ~LK_DRAINING;
486 1.1 fvdl if ((flags & LK_REENABLE) == 0)
487 1.1 fvdl lkp->lk_flags |= LK_DRAINED;
488 1.1 fvdl }
489 1.1 fvdl
490 1.1 fvdl switch (flags & LK_TYPE_MASK) {
491 1.1 fvdl
492 1.1 fvdl case LK_SHARED:
493 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
494 1.1 fvdl /*
495 1.1 fvdl * If just polling, check to see if we will block.
496 1.1 fvdl */
497 1.1 fvdl if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
498 1.1 fvdl (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
499 1.1 fvdl error = EBUSY;
500 1.1 fvdl break;
501 1.1 fvdl }
502 1.1 fvdl /*
503 1.1 fvdl * Wait for exclusive locks and upgrades to clear.
504 1.1 fvdl */
505 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
506 1.98 ad LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
507 1.98 ad RETURN_ADDRESS);
508 1.1 fvdl if (error)
509 1.1 fvdl break;
510 1.1 fvdl lkp->lk_sharecount++;
511 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
512 1.88 blymn COUNT(lkp, l, cpu_num, 1);
513 1.1 fvdl break;
514 1.1 fvdl }
515 1.1 fvdl /*
516 1.1 fvdl * We hold an exclusive lock, so downgrade it to shared.
517 1.1 fvdl * An alternative would be to fail with EDEADLK.
518 1.1 fvdl */
519 1.1 fvdl lkp->lk_sharecount++;
520 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
521 1.88 blymn COUNT(lkp, l, cpu_num, 1);
522 1.1 fvdl /* fall into downgrade */
523 1.1 fvdl
524 1.1 fvdl case LK_DOWNGRADE:
525 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
526 1.19 thorpej lkp->lk_exclusivecount == 0)
527 1.110 christos lockpanic(lkp, "lockmgr: not holding exclusive lock");
528 1.1 fvdl lkp->lk_sharecount += lkp->lk_exclusivecount;
529 1.73 yamt lkp->lk_flags |= LK_SHARE_NONZERO;
530 1.1 fvdl lkp->lk_exclusivecount = 0;
531 1.15 fvdl lkp->lk_recurselevel = 0;
532 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
533 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
534 1.50 thorpej #if defined(LOCKDEBUG)
535 1.50 thorpej lkp->lk_unlock_file = file;
536 1.50 thorpej lkp->lk_unlock_line = line;
537 1.50 thorpej #endif
538 1.23 thorpej WAKEUP_WAITER(lkp);
539 1.1 fvdl break;
540 1.1 fvdl
541 1.1 fvdl case LK_EXCLUPGRADE:
542 1.1 fvdl /*
543 1.1 fvdl * If another process is ahead of us to get an upgrade,
544 1.1 fvdl * then we want to fail rather than have an intervening
545 1.1 fvdl * exclusive access.
546 1.1 fvdl */
547 1.1 fvdl if (lkp->lk_flags & LK_WANT_UPGRADE) {
548 1.1 fvdl lkp->lk_sharecount--;
549 1.73 yamt if (lkp->lk_sharecount == 0)
550 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
551 1.88 blymn COUNT(lkp, l, cpu_num, -1);
552 1.1 fvdl error = EBUSY;
553 1.1 fvdl break;
554 1.1 fvdl }
555 1.1 fvdl /* fall into normal upgrade */
556 1.1 fvdl
557 1.1 fvdl case LK_UPGRADE:
558 1.1 fvdl /*
559 1.1 fvdl * Upgrade a shared lock to an exclusive one. If another
560 1.1 fvdl * shared lock has already requested an upgrade to an
561 1.1 fvdl * exclusive lock, our shared lock is released and an
562 1.1 fvdl * exclusive lock is requested (which will be granted
563 1.1 fvdl * after the upgrade). If we return an error, the file
564 1.1 fvdl * will always be unlocked.
565 1.1 fvdl */
566 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
567 1.110 christos lockpanic(lkp, "lockmgr: upgrade exclusive lock");
568 1.1 fvdl lkp->lk_sharecount--;
569 1.73 yamt if (lkp->lk_sharecount == 0)
570 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
571 1.88 blymn COUNT(lkp, l, cpu_num, -1);
572 1.1 fvdl /*
573 1.1 fvdl * If we are just polling, check to see if we will block.
574 1.1 fvdl */
575 1.1 fvdl if ((extflags & LK_NOWAIT) &&
576 1.1 fvdl ((lkp->lk_flags & LK_WANT_UPGRADE) ||
577 1.1 fvdl lkp->lk_sharecount > 1)) {
578 1.1 fvdl error = EBUSY;
579 1.1 fvdl break;
580 1.1 fvdl }
581 1.1 fvdl if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
582 1.1 fvdl /*
583 1.1 fvdl * We are first shared lock to request an upgrade, so
584 1.1 fvdl * request upgrade and wait for the shared count to
585 1.1 fvdl * drop to zero, then take exclusive lock.
586 1.1 fvdl */
587 1.1 fvdl lkp->lk_flags |= LK_WANT_UPGRADE;
588 1.98 ad error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
589 1.98 ad RETURN_ADDRESS);
590 1.1 fvdl lkp->lk_flags &= ~LK_WANT_UPGRADE;
591 1.83 yamt if (error) {
592 1.83 yamt WAKEUP_WAITER(lkp);
593 1.1 fvdl break;
594 1.83 yamt }
595 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
596 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
597 1.50 thorpej #if defined(LOCKDEBUG)
598 1.50 thorpej lkp->lk_lock_file = file;
599 1.50 thorpej lkp->lk_lock_line = line;
600 1.50 thorpej #endif
601 1.1 fvdl if (lkp->lk_exclusivecount != 0)
602 1.110 christos lockpanic(lkp, "lockmgr: non-zero exclusive count");
603 1.1 fvdl lkp->lk_exclusivecount = 1;
604 1.15 fvdl if (extflags & LK_SETRECURSE)
605 1.15 fvdl lkp->lk_recurselevel = 1;
606 1.88 blymn COUNT(lkp, l, cpu_num, 1);
607 1.1 fvdl break;
608 1.1 fvdl }
609 1.1 fvdl /*
610 1.1 fvdl * Someone else has requested upgrade. Release our shared
611 1.1 fvdl * lock, awaken upgrade requestor if we are the last shared
612 1.1 fvdl * lock, then request an exclusive lock.
613 1.1 fvdl */
614 1.23 thorpej if (lkp->lk_sharecount == 0)
615 1.23 thorpej WAKEUP_WAITER(lkp);
616 1.1 fvdl /* fall into exclusive request */
617 1.1 fvdl
618 1.1 fvdl case LK_EXCLUSIVE:
619 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
620 1.1 fvdl /*
621 1.19 thorpej * Recursive lock.
622 1.1 fvdl */
623 1.15 fvdl if ((extflags & LK_CANRECURSE) == 0 &&
624 1.16 sommerfe lkp->lk_recurselevel == 0) {
625 1.16 sommerfe if (extflags & LK_RECURSEFAIL) {
626 1.16 sommerfe error = EDEADLK;
627 1.16 sommerfe break;
628 1.16 sommerfe } else
629 1.110 christos lockpanic(lkp, "lockmgr: locking against myself");
630 1.16 sommerfe }
631 1.1 fvdl lkp->lk_exclusivecount++;
632 1.15 fvdl if (extflags & LK_SETRECURSE &&
633 1.15 fvdl lkp->lk_recurselevel == 0)
634 1.15 fvdl lkp->lk_recurselevel = lkp->lk_exclusivecount;
635 1.88 blymn COUNT(lkp, l, cpu_num, 1);
636 1.1 fvdl break;
637 1.1 fvdl }
638 1.1 fvdl /*
639 1.1 fvdl * If we are just polling, check to see if we will sleep.
640 1.1 fvdl */
641 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
642 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
643 1.73 yamt LK_SHARE_NONZERO))) {
644 1.1 fvdl error = EBUSY;
645 1.1 fvdl break;
646 1.1 fvdl }
647 1.1 fvdl /*
648 1.1 fvdl * Try to acquire the want_exclusive flag.
649 1.1 fvdl */
650 1.82 yamt error = acquire(&lkp, &s, extflags, 0,
651 1.98 ad LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
652 1.1 fvdl if (error)
653 1.1 fvdl break;
654 1.1 fvdl lkp->lk_flags |= LK_WANT_EXCL;
655 1.1 fvdl /*
656 1.1 fvdl * Wait for shared locks and upgrades to finish.
657 1.1 fvdl */
658 1.78 hannken error = acquire(&lkp, &s, extflags, 0,
659 1.98 ad LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
660 1.98 ad RETURN_ADDRESS);
661 1.1 fvdl lkp->lk_flags &= ~LK_WANT_EXCL;
662 1.83 yamt if (error) {
663 1.83 yamt WAKEUP_WAITER(lkp);
664 1.1 fvdl break;
665 1.83 yamt }
666 1.1 fvdl lkp->lk_flags |= LK_HAVE_EXCL;
667 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
668 1.50 thorpej #if defined(LOCKDEBUG)
669 1.50 thorpej lkp->lk_lock_file = file;
670 1.50 thorpej lkp->lk_lock_line = line;
671 1.50 thorpej #endif
672 1.1 fvdl if (lkp->lk_exclusivecount != 0)
673 1.110 christos lockpanic(lkp, "lockmgr: non-zero exclusive count");
674 1.1 fvdl lkp->lk_exclusivecount = 1;
675 1.15 fvdl if (extflags & LK_SETRECURSE)
676 1.15 fvdl lkp->lk_recurselevel = 1;
677 1.88 blymn COUNT(lkp, l, cpu_num, 1);
678 1.1 fvdl break;
679 1.1 fvdl
680 1.1 fvdl case LK_RELEASE:
681 1.1 fvdl if (lkp->lk_exclusivecount != 0) {
682 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
683 1.110.2.1 ad lockpanic(lkp, "lockmgr: pid %d, not "
684 1.110.2.1 ad "exclusive lock holder %d "
685 1.110.2.1 ad "unlocking", pid,
686 1.110.2.1 ad lkp->lk_lockholder);
687 1.19 thorpej }
688 1.15 fvdl if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
689 1.15 fvdl lkp->lk_recurselevel = 0;
690 1.1 fvdl lkp->lk_exclusivecount--;
691 1.88 blymn COUNT(lkp, l, cpu_num, -1);
692 1.1 fvdl if (lkp->lk_exclusivecount == 0) {
693 1.1 fvdl lkp->lk_flags &= ~LK_HAVE_EXCL;
694 1.69 thorpej SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
695 1.50 thorpej #if defined(LOCKDEBUG)
696 1.50 thorpej lkp->lk_unlock_file = file;
697 1.50 thorpej lkp->lk_unlock_line = line;
698 1.50 thorpej #endif
699 1.1 fvdl }
700 1.1 fvdl } else if (lkp->lk_sharecount != 0) {
701 1.1 fvdl lkp->lk_sharecount--;
702 1.73 yamt if (lkp->lk_sharecount == 0)
703 1.73 yamt lkp->lk_flags &= ~LK_SHARE_NONZERO;
704 1.88 blymn COUNT(lkp, l, cpu_num, -1);
705 1.1 fvdl }
706 1.39 thorpej #ifdef DIAGNOSTIC
707 1.39 thorpej else
708 1.110 christos lockpanic(lkp, "lockmgr: release of unlocked lock!");
709 1.39 thorpej #endif
710 1.23 thorpej WAKEUP_WAITER(lkp);
711 1.1 fvdl break;
712 1.1 fvdl
713 1.1 fvdl case LK_DRAIN:
714 1.1 fvdl /*
715 1.86 perry * Check that we do not already hold the lock, as it can
716 1.1 fvdl * never drain if we do. Unfortunately, we have no way to
717 1.1 fvdl * check for holding a shared lock, but at least we can
718 1.1 fvdl * check for an exclusive one.
719 1.1 fvdl */
720 1.88 blymn if (WEHOLDIT(lkp, pid, lid, cpu_num))
721 1.110 christos lockpanic(lkp, "lockmgr: draining against myself");
722 1.1 fvdl /*
723 1.1 fvdl * If we are just polling, check to see if we will sleep.
724 1.1 fvdl */
725 1.73 yamt if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
726 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
727 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
728 1.1 fvdl error = EBUSY;
729 1.1 fvdl break;
730 1.1 fvdl }
731 1.78 hannken error = acquire(&lkp, &s, extflags, 1,
732 1.73 yamt LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
733 1.98 ad LK_SHARE_NONZERO | LK_WAIT_NONZERO,
734 1.98 ad RETURN_ADDRESS);
735 1.23 thorpej if (error)
736 1.23 thorpej break;
737 1.1 fvdl lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
738 1.88 blymn SETHOLDER(lkp, pid, lid, cpu_num);
739 1.50 thorpej #if defined(LOCKDEBUG)
740 1.50 thorpej lkp->lk_lock_file = file;
741 1.50 thorpej lkp->lk_lock_line = line;
742 1.50 thorpej #endif
743 1.1 fvdl lkp->lk_exclusivecount = 1;
744 1.15 fvdl /* XXX unlikely that we'd want this */
745 1.15 fvdl if (extflags & LK_SETRECURSE)
746 1.15 fvdl lkp->lk_recurselevel = 1;
747 1.88 blymn COUNT(lkp, l, cpu_num, 1);
748 1.1 fvdl break;
749 1.1 fvdl
750 1.1 fvdl default:
751 1.110.2.1 ad mutex_exit(mutex);
752 1.110 christos lockpanic(lkp, "lockmgr: unknown locktype request %d",
753 1.1 fvdl flags & LK_TYPE_MASK);
754 1.1 fvdl /* NOTREACHED */
755 1.1 fvdl }
756 1.110.2.1 ad if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
757 1.23 thorpej ((lkp->lk_flags &
758 1.73 yamt (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
759 1.73 yamt LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
760 1.1 fvdl lkp->lk_flags &= ~LK_WAITDRAIN;
761 1.87 christos wakeup(&lkp->lk_flags);
762 1.1 fvdl }
763 1.32 sommerfe /*
764 1.32 sommerfe * Note that this panic will be a recursive panic, since
765 1.32 sommerfe * we only set lock_shutdown_noblock above if panicstr != NULL.
766 1.32 sommerfe */
767 1.32 sommerfe if (error && lock_shutdown_noblock)
768 1.110 christos lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
769 1.86 perry
770 1.110.2.1 ad mutex_exit(mutex);
771 1.1 fvdl return (error);
772 1.1 fvdl }
773 1.1 fvdl
774 1.1 fvdl /*
775 1.1 fvdl * Print out information about state of a lock. Used by VOP_PRINT
776 1.1 fvdl * routines to display ststus about contained locks.
777 1.1 fvdl */
778 1.2 fvdl void
779 1.91 perry lockmgr_printinfo(volatile struct lock *lkp)
780 1.1 fvdl {
781 1.1 fvdl
782 1.1 fvdl if (lkp->lk_sharecount)
783 1.1 fvdl printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
784 1.1 fvdl lkp->lk_sharecount);
785 1.19 thorpej else if (lkp->lk_flags & LK_HAVE_EXCL) {
786 1.19 thorpej printf(" lock type %s: EXCL (count %d) by ",
787 1.19 thorpej lkp->lk_wmesg, lkp->lk_exclusivecount);
788 1.110.2.1 ad printf("pid %d.%d", lkp->lk_lockholder,
789 1.110.2.1 ad lkp->lk_locklwp);
790 1.19 thorpej } else
791 1.19 thorpej printf(" not locked");
792 1.110.2.1 ad if (lkp->lk_waitcount > 0)
793 1.1 fvdl printf(" with %d pending", lkp->lk_waitcount);
794 1.1 fvdl }
795 1.1 fvdl
796 1.21 thorpej #if defined(LOCKDEBUG) /* { */
797 1.91 perry _TAILQ_HEAD(, struct simplelock, volatile) simplelock_list =
798 1.21 thorpej TAILQ_HEAD_INITIALIZER(simplelock_list);
799 1.21 thorpej
800 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
801 1.21 thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
802 1.21 thorpej
803 1.21 thorpej #define SLOCK_LIST_LOCK() \
804 1.29 sommerfe __cpu_simple_lock(&simplelock_list_slock.lock_data)
805 1.21 thorpej
806 1.21 thorpej #define SLOCK_LIST_UNLOCK() \
807 1.29 sommerfe __cpu_simple_unlock(&simplelock_list_slock.lock_data)
808 1.21 thorpej
809 1.21 thorpej #define SLOCK_COUNT(x) \
810 1.47 sommerfe curcpu()->ci_simple_locks += (x)
811 1.21 thorpej #else
812 1.21 thorpej u_long simple_locks;
813 1.21 thorpej
814 1.21 thorpej #define SLOCK_LIST_LOCK() /* nothing */
815 1.21 thorpej
816 1.21 thorpej #define SLOCK_LIST_UNLOCK() /* nothing */
817 1.21 thorpej
818 1.21 thorpej #define SLOCK_COUNT(x) simple_locks += (x)
819 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
820 1.21 thorpej
821 1.26 sommerfe #ifdef MULTIPROCESSOR
822 1.75 wiz #define SLOCK_MP() lock_printf("on CPU %ld\n", \
823 1.46 thorpej (u_long) cpu_number())
824 1.26 sommerfe #else
825 1.26 sommerfe #define SLOCK_MP() /* nothing */
826 1.26 sommerfe #endif
827 1.26 sommerfe
828 1.21 thorpej #define SLOCK_WHERE(str, alp, id, l) \
829 1.21 thorpej do { \
830 1.58 chs lock_printf("\n"); \
831 1.25 thorpej lock_printf(str); \
832 1.33 thorpej lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
833 1.26 sommerfe SLOCK_MP(); \
834 1.21 thorpej if ((alp)->lock_file != NULL) \
835 1.25 thorpej lock_printf("last locked: %s:%d\n", (alp)->lock_file, \
836 1.21 thorpej (alp)->lock_line); \
837 1.21 thorpej if ((alp)->unlock_file != NULL) \
838 1.25 thorpej lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
839 1.21 thorpej (alp)->unlock_line); \
840 1.58 chs SLOCK_TRACE() \
841 1.21 thorpej SLOCK_DEBUGGER(); \
842 1.30 thorpej } while (/*CONSTCOND*/0)
843 1.12 chs
844 1.1 fvdl /*
845 1.1 fvdl * Simple lock functions so that the debugger can see from whence
846 1.1 fvdl * they are being called.
847 1.1 fvdl */
848 1.1 fvdl void
849 1.91 perry simple_lock_init(volatile struct simplelock *alp)
850 1.1 fvdl {
851 1.21 thorpej
852 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
853 1.27 thorpej __cpu_simple_lock_init(&alp->lock_data);
854 1.21 thorpej #else
855 1.27 thorpej alp->lock_data = __SIMPLELOCK_UNLOCKED;
856 1.21 thorpej #endif /* } */
857 1.5 chs alp->lock_file = NULL;
858 1.5 chs alp->lock_line = 0;
859 1.5 chs alp->unlock_file = NULL;
860 1.5 chs alp->unlock_line = 0;
861 1.41 thorpej alp->lock_holder = LK_NOCPU;
862 1.1 fvdl }
863 1.1 fvdl
864 1.1 fvdl void
865 1.91 perry _simple_lock(volatile struct simplelock *alp, const char *id, int l)
866 1.1 fvdl {
867 1.88 blymn cpuid_t cpu_num = cpu_number();
868 1.12 chs int s;
869 1.12 chs
870 1.44 thorpej s = spllock();
871 1.21 thorpej
872 1.21 thorpej /*
873 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
874 1.21 thorpej * don't take any action, and just fall into the normal spin case.
875 1.21 thorpej */
876 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
877 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
878 1.88 blymn if (alp->lock_holder == cpu_num) {
879 1.21 thorpej SLOCK_WHERE("simple_lock: locking against myself\n",
880 1.21 thorpej alp, id, l);
881 1.21 thorpej goto out;
882 1.1 fvdl }
883 1.21 thorpej #else
884 1.21 thorpej SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
885 1.21 thorpej goto out;
886 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
887 1.1 fvdl }
888 1.21 thorpej
889 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
890 1.21 thorpej /* Acquire the lock before modifying any fields. */
891 1.70 pk splx(s);
892 1.27 thorpej __cpu_simple_lock(&alp->lock_data);
893 1.70 pk s = spllock();
894 1.21 thorpej #else
895 1.27 thorpej alp->lock_data = __SIMPLELOCK_LOCKED;
896 1.21 thorpej #endif /* } */
897 1.21 thorpej
898 1.45 sommerfe if (alp->lock_holder != LK_NOCPU) {
899 1.45 sommerfe SLOCK_WHERE("simple_lock: uninitialized lock\n",
900 1.45 sommerfe alp, id, l);
901 1.45 sommerfe }
902 1.5 chs alp->lock_file = id;
903 1.5 chs alp->lock_line = l;
904 1.88 blymn alp->lock_holder = cpu_num;
905 1.21 thorpej
906 1.21 thorpej SLOCK_LIST_LOCK();
907 1.87 christos TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
908 1.21 thorpej SLOCK_LIST_UNLOCK();
909 1.21 thorpej
910 1.21 thorpej SLOCK_COUNT(1);
911 1.21 thorpej
912 1.21 thorpej out:
913 1.18 chs splx(s);
914 1.38 thorpej }
915 1.38 thorpej
916 1.38 thorpej int
917 1.91 perry _simple_lock_held(volatile struct simplelock *alp)
918 1.38 thorpej {
919 1.54 enami #if defined(MULTIPROCESSOR) || defined(DIAGNOSTIC)
920 1.88 blymn cpuid_t cpu_num = cpu_number();
921 1.54 enami #endif
922 1.38 thorpej int s, locked = 0;
923 1.38 thorpej
924 1.44 thorpej s = spllock();
925 1.42 thorpej
926 1.42 thorpej #if defined(MULTIPROCESSOR)
927 1.38 thorpej if (__cpu_simple_lock_try(&alp->lock_data) == 0)
928 1.88 blymn locked = (alp->lock_holder == cpu_num);
929 1.38 thorpej else
930 1.38 thorpej __cpu_simple_unlock(&alp->lock_data);
931 1.38 thorpej #else
932 1.42 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
933 1.42 thorpej locked = 1;
934 1.88 blymn KASSERT(alp->lock_holder == cpu_num);
935 1.42 thorpej }
936 1.42 thorpej #endif
937 1.38 thorpej
938 1.38 thorpej splx(s);
939 1.42 thorpej
940 1.38 thorpej return (locked);
941 1.1 fvdl }
942 1.1 fvdl
943 1.1 fvdl int
944 1.91 perry _simple_lock_try(volatile struct simplelock *alp, const char *id, int l)
945 1.1 fvdl {
946 1.88 blymn cpuid_t cpu_num = cpu_number();
947 1.21 thorpej int s, rv = 0;
948 1.1 fvdl
949 1.44 thorpej s = spllock();
950 1.21 thorpej
951 1.21 thorpej /*
952 1.21 thorpej * MULTIPROCESSOR case: This is `safe' since if it's not us, we
953 1.21 thorpej * don't take any action.
954 1.21 thorpej */
955 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
956 1.27 thorpej if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
957 1.88 blymn if (alp->lock_holder == cpu_num)
958 1.21 thorpej SLOCK_WHERE("simple_lock_try: locking against myself\n",
959 1.26 sommerfe alp, id, l);
960 1.21 thorpej goto out;
961 1.21 thorpej }
962 1.21 thorpej #else
963 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_LOCKED) {
964 1.21 thorpej SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
965 1.21 thorpej goto out;
966 1.18 chs }
967 1.27 thorpej alp->lock_data = __SIMPLELOCK_LOCKED;
968 1.21 thorpej #endif /* MULTIPROCESSOR */ /* } */
969 1.21 thorpej
970 1.21 thorpej /*
971 1.21 thorpej * At this point, we have acquired the lock.
972 1.21 thorpej */
973 1.21 thorpej
974 1.21 thorpej rv = 1;
975 1.18 chs
976 1.5 chs alp->lock_file = id;
977 1.5 chs alp->lock_line = l;
978 1.88 blymn alp->lock_holder = cpu_num;
979 1.21 thorpej
980 1.21 thorpej SLOCK_LIST_LOCK();
981 1.87 christos TAILQ_INSERT_TAIL(&simplelock_list, alp, list);
982 1.21 thorpej SLOCK_LIST_UNLOCK();
983 1.21 thorpej
984 1.21 thorpej SLOCK_COUNT(1);
985 1.21 thorpej
986 1.21 thorpej out:
987 1.12 chs splx(s);
988 1.21 thorpej return (rv);
989 1.1 fvdl }
990 1.1 fvdl
991 1.1 fvdl void
992 1.91 perry _simple_unlock(volatile struct simplelock *alp, const char *id, int l)
993 1.1 fvdl {
994 1.12 chs int s;
995 1.1 fvdl
996 1.44 thorpej s = spllock();
997 1.21 thorpej
998 1.21 thorpej /*
999 1.21 thorpej * MULTIPROCESSOR case: This is `safe' because we think we hold
1000 1.21 thorpej * the lock, and if we don't, we don't take any action.
1001 1.21 thorpej */
1002 1.27 thorpej if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
1003 1.21 thorpej SLOCK_WHERE("simple_unlock: lock not held\n",
1004 1.21 thorpej alp, id, l);
1005 1.21 thorpej goto out;
1006 1.21 thorpej }
1007 1.21 thorpej
1008 1.21 thorpej SLOCK_LIST_LOCK();
1009 1.21 thorpej TAILQ_REMOVE(&simplelock_list, alp, list);
1010 1.21 thorpej SLOCK_LIST_UNLOCK();
1011 1.21 thorpej
1012 1.21 thorpej SLOCK_COUNT(-1);
1013 1.21 thorpej
1014 1.21 thorpej alp->list.tqe_next = NULL; /* sanity */
1015 1.21 thorpej alp->list.tqe_prev = NULL; /* sanity */
1016 1.21 thorpej
1017 1.5 chs alp->unlock_file = id;
1018 1.5 chs alp->unlock_line = l;
1019 1.21 thorpej
1020 1.21 thorpej #if defined(MULTIPROCESSOR) /* { */
1021 1.26 sommerfe alp->lock_holder = LK_NOCPU;
1022 1.21 thorpej /* Now that we've modified all fields, release the lock. */
1023 1.27 thorpej __cpu_simple_unlock(&alp->lock_data);
1024 1.21 thorpej #else
1025 1.27 thorpej alp->lock_data = __SIMPLELOCK_UNLOCKED;
1026 1.41 thorpej KASSERT(alp->lock_holder == cpu_number());
1027 1.41 thorpej alp->lock_holder = LK_NOCPU;
1028 1.21 thorpej #endif /* } */
1029 1.21 thorpej
1030 1.21 thorpej out:
1031 1.18 chs splx(s);
1032 1.12 chs }
1033 1.12 chs
1034 1.12 chs void
1035 1.33 thorpej simple_lock_dump(void)
1036 1.12 chs {
1037 1.91 perry volatile struct simplelock *alp;
1038 1.12 chs int s;
1039 1.12 chs
1040 1.44 thorpej s = spllock();
1041 1.21 thorpej SLOCK_LIST_LOCK();
1042 1.25 thorpej lock_printf("all simple locks:\n");
1043 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1044 1.25 thorpej lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
1045 1.21 thorpej alp->lock_file, alp->lock_line);
1046 1.12 chs }
1047 1.21 thorpej SLOCK_LIST_UNLOCK();
1048 1.12 chs splx(s);
1049 1.12 chs }
1050 1.12 chs
1051 1.12 chs void
1052 1.33 thorpej simple_lock_freecheck(void *start, void *end)
1053 1.12 chs {
1054 1.91 perry volatile struct simplelock *alp;
1055 1.12 chs int s;
1056 1.12 chs
1057 1.44 thorpej s = spllock();
1058 1.21 thorpej SLOCK_LIST_LOCK();
1059 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1060 1.91 perry if ((volatile void *)alp >= start &&
1061 1.91 perry (volatile void *)alp < end) {
1062 1.25 thorpej lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
1063 1.34 thorpej alp, alp->lock_holder, alp->lock_file,
1064 1.34 thorpej alp->lock_line);
1065 1.34 thorpej SLOCK_DEBUGGER();
1066 1.34 thorpej }
1067 1.34 thorpej }
1068 1.34 thorpej SLOCK_LIST_UNLOCK();
1069 1.34 thorpej splx(s);
1070 1.34 thorpej }
1071 1.34 thorpej
1072 1.55 thorpej /*
1073 1.55 thorpej * We must be holding exactly one lock: the sched_lock.
1074 1.55 thorpej */
1075 1.55 thorpej
1076 1.34 thorpej void
1077 1.34 thorpej simple_lock_switchcheck(void)
1078 1.34 thorpej {
1079 1.55 thorpej
1080 1.105 ad simple_lock_only_held(NULL, "switching");
1081 1.55 thorpej }
1082 1.55 thorpej
1083 1.93 erh /*
1084 1.93 erh * Drop into the debugger if lp isn't the only lock held.
1085 1.93 erh * lp may be NULL.
1086 1.93 erh */
1087 1.55 thorpej void
1088 1.55 thorpej simple_lock_only_held(volatile struct simplelock *lp, const char *where)
1089 1.55 thorpej {
1090 1.91 perry volatile struct simplelock *alp;
1091 1.88 blymn cpuid_t cpu_num = cpu_number();
1092 1.34 thorpej int s;
1093 1.34 thorpej
1094 1.55 thorpej if (lp) {
1095 1.55 thorpej LOCK_ASSERT(simple_lock_held(lp));
1096 1.55 thorpej }
1097 1.44 thorpej s = spllock();
1098 1.34 thorpej SLOCK_LIST_LOCK();
1099 1.58 chs TAILQ_FOREACH(alp, &simplelock_list, list) {
1100 1.55 thorpej if (alp == lp)
1101 1.42 thorpej continue;
1102 1.88 blymn if (alp->lock_holder == cpu_num)
1103 1.55 thorpej break;
1104 1.12 chs }
1105 1.21 thorpej SLOCK_LIST_UNLOCK();
1106 1.12 chs splx(s);
1107 1.55 thorpej
1108 1.55 thorpej if (alp != NULL) {
1109 1.58 chs lock_printf("\n%s with held simple_lock %p "
1110 1.55 thorpej "CPU %lu %s:%d\n",
1111 1.55 thorpej where, alp, alp->lock_holder, alp->lock_file,
1112 1.55 thorpej alp->lock_line);
1113 1.58 chs SLOCK_TRACE();
1114 1.55 thorpej SLOCK_DEBUGGER();
1115 1.55 thorpej }
1116 1.1 fvdl }
1117 1.94 erh
1118 1.94 erh /*
1119 1.94 erh * Set to 1 by simple_lock_assert_*().
1120 1.94 erh * Can be cleared from ddb to avoid a panic.
1121 1.94 erh */
1122 1.94 erh int slock_assert_will_panic;
1123 1.94 erh
1124 1.94 erh /*
1125 1.94 erh * If the lock isn't held, print a traceback, optionally drop into the
1126 1.94 erh * debugger, then panic.
1127 1.94 erh * The panic can be avoided by clearing slock_assert_with_panic from the
1128 1.94 erh * debugger.
1129 1.94 erh */
1130 1.94 erh void
1131 1.94 erh _simple_lock_assert_locked(volatile struct simplelock *alp,
1132 1.94 erh const char *lockname, const char *id, int l)
1133 1.94 erh {
1134 1.94 erh if (simple_lock_held(alp) == 0) {
1135 1.94 erh slock_assert_will_panic = 1;
1136 1.94 erh lock_printf("%s lock not held\n", lockname);
1137 1.94 erh SLOCK_WHERE("lock not held", alp, id, l);
1138 1.94 erh if (slock_assert_will_panic)
1139 1.94 erh panic("%s: not locked", lockname);
1140 1.94 erh }
1141 1.94 erh }
1142 1.94 erh
1143 1.94 erh void
1144 1.94 erh _simple_lock_assert_unlocked(volatile struct simplelock *alp,
1145 1.94 erh const char *lockname, const char *id, int l)
1146 1.94 erh {
1147 1.94 erh if (simple_lock_held(alp)) {
1148 1.94 erh slock_assert_will_panic = 1;
1149 1.94 erh lock_printf("%s lock held\n", lockname);
1150 1.94 erh SLOCK_WHERE("lock held", alp, id, l);
1151 1.94 erh if (slock_assert_will_panic)
1152 1.94 erh panic("%s: locked", lockname);
1153 1.94 erh }
1154 1.94 erh }
1155 1.94 erh
1156 1.96 yamt void
1157 1.96 yamt assert_sleepable(struct simplelock *interlock, const char *msg)
1158 1.96 yamt {
1159 1.96 yamt
1160 1.97 yamt if (curlwp == NULL) {
1161 1.97 yamt panic("assert_sleepable: NULL curlwp");
1162 1.97 yamt }
1163 1.96 yamt simple_lock_only_held(interlock, msg);
1164 1.96 yamt }
1165 1.96 yamt
1166 1.21 thorpej #endif /* LOCKDEBUG */ /* } */
1167 1.62 thorpej
1168 1.62 thorpej #if defined(MULTIPROCESSOR)
1169 1.105 ad
1170 1.62 thorpej /*
1171 1.62 thorpej * Functions for manipulating the kernel_lock. We put them here
1172 1.62 thorpej * so that they show up in profiles.
1173 1.62 thorpej */
1174 1.62 thorpej
1175 1.105 ad #define _KERNEL_LOCK_ABORT(msg) \
1176 1.105 ad LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops, \
1177 1.105 ad __FUNCTION__, msg)
1178 1.105 ad
1179 1.105 ad #ifdef LOCKDEBUG
1180 1.105 ad #define _KERNEL_LOCK_ASSERT(cond) \
1181 1.105 ad do { \
1182 1.105 ad if (!(cond)) \
1183 1.105 ad _KERNEL_LOCK_ABORT("assertion failed: " #cond); \
1184 1.105 ad } while (/* CONSTCOND */ 0)
1185 1.105 ad #else
1186 1.105 ad #define _KERNEL_LOCK_ASSERT(cond) /* nothing */
1187 1.105 ad #endif
1188 1.105 ad
1189 1.105 ad void _kernel_lock_dump(volatile void *);
1190 1.105 ad
1191 1.105 ad lockops_t _kernel_lock_ops = {
1192 1.105 ad "Kernel lock",
1193 1.105 ad 0,
1194 1.105 ad _kernel_lock_dump
1195 1.105 ad };
1196 1.105 ad
1197 1.85 yamt /*
1198 1.105 ad * Initialize the kernel lock.
1199 1.85 yamt */
1200 1.62 thorpej void
1201 1.62 thorpej _kernel_lock_init(void)
1202 1.62 thorpej {
1203 1.62 thorpej
1204 1.105 ad __cpu_simple_lock_init(&kernel_lock);
1205 1.105 ad kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
1206 1.62 thorpej }
1207 1.62 thorpej
1208 1.62 thorpej /*
1209 1.105 ad * Print debugging information about the kernel lock.
1210 1.62 thorpej */
1211 1.62 thorpej void
1212 1.105 ad _kernel_lock_dump(volatile void *junk)
1213 1.62 thorpej {
1214 1.85 yamt struct cpu_info *ci = curcpu();
1215 1.62 thorpej
1216 1.105 ad (void)junk;
1217 1.85 yamt
1218 1.105 ad printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
1219 1.105 ad ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
1220 1.62 thorpej }
1221 1.62 thorpej
1222 1.105 ad /*
1223 1.105 ad * Acquire 'nlocks' holds on the kernel lock. If 'l' is non-null, the
1224 1.105 ad * acquisition is from process context.
1225 1.105 ad */
1226 1.62 thorpej void
1227 1.105 ad _kernel_lock(int nlocks, struct lwp *l)
1228 1.62 thorpej {
1229 1.85 yamt struct cpu_info *ci = curcpu();
1230 1.105 ad LOCKSTAT_TIMER(spintime);
1231 1.105 ad LOCKSTAT_FLAG(lsflag);
1232 1.105 ad struct lwp *owant;
1233 1.105 ad #ifdef LOCKDEBUG
1234 1.105 ad u_int spins;
1235 1.105 ad #endif
1236 1.85 yamt int s;
1237 1.85 yamt
1238 1.105 ad (void)l;
1239 1.105 ad
1240 1.105 ad if (nlocks == 0)
1241 1.105 ad return;
1242 1.105 ad _KERNEL_LOCK_ASSERT(nlocks > 0);
1243 1.62 thorpej
1244 1.85 yamt s = splbiglock();
1245 1.105 ad
1246 1.105 ad if (ci->ci_biglock_count != 0) {
1247 1.105 ad _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
1248 1.105 ad ci->ci_biglock_count += nlocks;
1249 1.105 ad splx(s);
1250 1.105 ad return;
1251 1.105 ad }
1252 1.105 ad
1253 1.107 ad LOCKDEBUG_WANTLOCK(kernel_lock_id,
1254 1.107 ad (uintptr_t)__builtin_return_address(0), 0);
1255 1.107 ad
1256 1.105 ad if (__cpu_simple_lock_try(&kernel_lock)) {
1257 1.105 ad ci->ci_biglock_count = nlocks;
1258 1.105 ad LOCKDEBUG_LOCKED(kernel_lock_id,
1259 1.105 ad (uintptr_t)__builtin_return_address(0), 0);
1260 1.105 ad splx(s);
1261 1.105 ad return;
1262 1.105 ad }
1263 1.105 ad
1264 1.105 ad LOCKSTAT_ENTER(lsflag);
1265 1.105 ad LOCKSTAT_START_TIMER(lsflag, spintime);
1266 1.105 ad
1267 1.105 ad /*
1268 1.105 ad * Before setting ci_biglock_wanted we must post a store
1269 1.105 ad * fence (see kern_mutex.c). This is accomplished by the
1270 1.105 ad * __cpu_simple_lock_try() above.
1271 1.105 ad */
1272 1.105 ad owant = ci->ci_biglock_wanted;
1273 1.105 ad ci->ci_biglock_wanted = curlwp; /* XXXAD */
1274 1.105 ad
1275 1.105 ad #ifdef LOCKDEBUG
1276 1.105 ad spins = 0;
1277 1.105 ad #endif
1278 1.105 ad
1279 1.105 ad do {
1280 1.105 ad while (kernel_lock == __SIMPLELOCK_LOCKED) {
1281 1.105 ad #ifdef LOCKDEBUG
1282 1.105 ad if (SPINLOCK_SPINOUT(spins))
1283 1.105 ad _KERNEL_LOCK_ABORT("spinout");
1284 1.105 ad #endif
1285 1.105 ad splx(s);
1286 1.105 ad SPINLOCK_SPIN_HOOK;
1287 1.105 ad (void)splbiglock();
1288 1.105 ad }
1289 1.105 ad } while (!__cpu_simple_lock_try(&kernel_lock));
1290 1.105 ad
1291 1.105 ad ci->ci_biglock_wanted = owant;
1292 1.105 ad ci->ci_biglock_count += nlocks;
1293 1.107 ad LOCKSTAT_STOP_TIMER(lsflag, spintime);
1294 1.107 ad LOCKDEBUG_LOCKED(kernel_lock_id,
1295 1.107 ad (uintptr_t)__builtin_return_address(0), 0);
1296 1.85 yamt splx(s);
1297 1.105 ad
1298 1.105 ad /*
1299 1.105 ad * Again, another store fence is required (see kern_mutex.c).
1300 1.105 ad */
1301 1.105 ad mb_write();
1302 1.107 ad if (owant == NULL) {
1303 1.107 ad LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
1304 1.107 ad 1, spintime);
1305 1.107 ad }
1306 1.105 ad LOCKSTAT_EXIT(lsflag);
1307 1.62 thorpej }
1308 1.62 thorpej
1309 1.62 thorpej /*
1310 1.105 ad * Release 'nlocks' holds on the kernel lock. If 'nlocks' is zero, release
1311 1.105 ad * all holds. If 'l' is non-null, the release is from process context.
1312 1.62 thorpej */
1313 1.62 thorpej void
1314 1.105 ad _kernel_unlock(int nlocks, struct lwp *l, int *countp)
1315 1.62 thorpej {
1316 1.105 ad struct cpu_info *ci = curcpu();
1317 1.105 ad u_int olocks;
1318 1.105 ad int s;
1319 1.62 thorpej
1320 1.105 ad (void)l;
1321 1.62 thorpej
1322 1.105 ad _KERNEL_LOCK_ASSERT(nlocks < 2);
1323 1.62 thorpej
1324 1.105 ad olocks = ci->ci_biglock_count;
1325 1.77 yamt
1326 1.105 ad if (olocks == 0) {
1327 1.105 ad _KERNEL_LOCK_ASSERT(nlocks <= 0);
1328 1.105 ad if (countp != NULL)
1329 1.105 ad *countp = 0;
1330 1.105 ad return;
1331 1.105 ad }
1332 1.77 yamt
1333 1.105 ad _KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
1334 1.85 yamt
1335 1.105 ad if (nlocks == 0)
1336 1.105 ad nlocks = olocks;
1337 1.105 ad else if (nlocks == -1) {
1338 1.105 ad nlocks = 1;
1339 1.105 ad _KERNEL_LOCK_ASSERT(olocks == 1);
1340 1.105 ad }
1341 1.85 yamt
1342 1.105 ad s = splbiglock();
1343 1.105 ad if ((ci->ci_biglock_count -= nlocks) == 0) {
1344 1.105 ad LOCKDEBUG_UNLOCKED(kernel_lock_id,
1345 1.105 ad (uintptr_t)__builtin_return_address(0), 0);
1346 1.105 ad __cpu_simple_unlock(&kernel_lock);
1347 1.85 yamt }
1348 1.105 ad splx(s);
1349 1.77 yamt
1350 1.105 ad if (countp != NULL)
1351 1.105 ad *countp = olocks;
1352 1.77 yamt }
1353 1.77 yamt
1354 1.84 yamt #if defined(DEBUG)
1355 1.105 ad /*
1356 1.105 ad * Assert that the kernel lock is held.
1357 1.105 ad */
1358 1.84 yamt void
1359 1.105 ad _kernel_lock_assert_locked(void)
1360 1.84 yamt {
1361 1.100 yamt
1362 1.105 ad if (kernel_lock != __SIMPLELOCK_LOCKED ||
1363 1.105 ad curcpu()->ci_biglock_count == 0)
1364 1.105 ad _KERNEL_LOCK_ABORT("not locked");
1365 1.84 yamt }
1366 1.100 yamt
1367 1.100 yamt void
1368 1.100 yamt _kernel_lock_assert_unlocked()
1369 1.100 yamt {
1370 1.100 yamt
1371 1.105 ad if (curcpu()->ci_biglock_count != 0)
1372 1.105 ad _KERNEL_LOCK_ABORT("locked");
1373 1.100 yamt }
1374 1.84 yamt #endif
1375 1.94 erh
1376 1.105 ad #endif /* MULTIPROCESSOR || LOCKDEBUG */
1377