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