Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.49
      1 /*	$NetBSD: kern_lock.c,v 1.49 2000/11/20 20:04:49 thorpej 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 "opt_multiprocessor.h"
     83 #include "opt_lockdebug.h"
     84 #include "opt_ddb.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/proc.h>
     88 #include <sys/lock.h>
     89 #include <sys/systm.h>
     90 #include <machine/cpu.h>
     91 
     92 #if defined(LOCKDEBUG)
     93 #include <sys/syslog.h>
     94 /*
     95  * note that stdarg.h and the ansi style va_start macro is used for both
     96  * ansi and traditional c compiles.
     97  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     98  */
     99 #include <machine/stdarg.h>
    100 
    101 void	lock_printf(const char *fmt, ...)
    102     __attribute__((__format__(__printf__,1,2)));
    103 
    104 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    105 #endif
    106 
    107 /*
    108  * Locking primitives implementation.
    109  * Locks provide shared/exclusive sychronization.
    110  */
    111 
    112 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    113 #if defined(MULTIPROCESSOR) /* { */
    114 #define	COUNT_CPU(cpu_id, x)						\
    115 	curcpu()->ci_spin_locks += (x)
    116 #else
    117 u_long	spin_locks;
    118 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    119 #endif /* MULTIPROCESSOR */ /* } */
    120 
    121 #define	COUNT(lkp, p, cpu_id, x)					\
    122 do {									\
    123 	if ((lkp)->lk_flags & LK_SPIN)					\
    124 		COUNT_CPU((cpu_id), (x));				\
    125 	else								\
    126 		(p)->p_locks += (x);					\
    127 } while (/*CONSTCOND*/0)
    128 #else
    129 #define COUNT(lkp, p, cpu_id, x)
    130 #define COUNT_CPU(cpu_id, x)
    131 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    132 
    133 #ifndef SPINLOCK_INTERLOCK_RELEASE_HOOK		/* from <machine/lock.h> */
    134 #define	SPINLOCK_INTERLOCK_RELEASE_HOOK		/* nothing */
    135 #endif
    136 
    137 #define	INTERLOCK_ACQUIRE(lkp, flags, s)				\
    138 do {									\
    139 	if ((flags) & LK_SPIN)						\
    140 		s = splsched();						\
    141 	simple_lock(&(lkp)->lk_interlock);				\
    142 } while (0)
    143 
    144 #define	INTERLOCK_RELEASE(lkp, flags, s)				\
    145 do {									\
    146 	simple_unlock(&(lkp)->lk_interlock);				\
    147 	if ((flags) & LK_SPIN) {					\
    148 		splx(s);						\
    149 		SPINLOCK_INTERLOCK_RELEASE_HOOK;			\
    150 	}								\
    151 } while (0)
    152 
    153 /*
    154  * Acquire a resource.
    155  */
    156 #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
    157 	if ((extflags) & LK_SPIN) {					\
    158 		int interlocked;					\
    159 									\
    160 		if ((drain) == 0)					\
    161 			(lkp)->lk_waitcount++;				\
    162 		for (interlocked = 1;;) {				\
    163 			if (wanted) {					\
    164 				if (interlocked) {			\
    165 					INTERLOCK_RELEASE((lkp),	\
    166 					    LK_SPIN, s);		\
    167 					interlocked = 0;		\
    168 				}					\
    169 			} else if (interlocked) {			\
    170 				break;					\
    171 			} else {					\
    172 				INTERLOCK_ACQUIRE((lkp), LK_SPIN, s);	\
    173 				interlocked = 1;			\
    174 			}						\
    175 		}							\
    176 		if ((drain) == 0)					\
    177 			(lkp)->lk_waitcount--;				\
    178 		KASSERT((wanted) == 0);					\
    179 		error = 0;	/* sanity */				\
    180 	} else {							\
    181 		for (error = 0; wanted; ) {				\
    182 			if ((drain))					\
    183 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
    184 			else						\
    185 				(lkp)->lk_waitcount++;			\
    186 			/* XXX Cast away volatile. */			\
    187 			error = ltsleep((drain) ? &(lkp)->lk_flags :	\
    188 			    (void *)(lkp), (lkp)->lk_prio,		\
    189 			    (lkp)->lk_wmesg, (lkp)->lk_timo,		\
    190 			    &(lkp)->lk_interlock);			\
    191 			if ((drain) == 0)				\
    192 				(lkp)->lk_waitcount--;			\
    193 			if (error)					\
    194 				break;					\
    195 			if ((extflags) & LK_SLEEPFAIL) {		\
    196 				error = ENOLCK;				\
    197 				break;					\
    198 			}						\
    199 		}							\
    200 	}
    201 
    202 #define	SETHOLDER(lkp, pid, cpu_id)					\
    203 do {									\
    204 	if ((lkp)->lk_flags & LK_SPIN)					\
    205 		(lkp)->lk_cpu = cpu_id;					\
    206 	else								\
    207 		(lkp)->lk_lockholder = pid;				\
    208 } while (/*CONSTCOND*/0)
    209 
    210 #define	WEHOLDIT(lkp, pid, cpu_id)					\
    211 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    212 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    213 
    214 #define	WAKEUP_WAITER(lkp)						\
    215 do {									\
    216 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
    217 		/* XXX Cast away volatile. */				\
    218 		wakeup_one((void *)(lkp));				\
    219 	}								\
    220 } while (/*CONSTCOND*/0)
    221 
    222 #if defined(LOCKDEBUG) /* { */
    223 #if defined(MULTIPROCESSOR) /* { */
    224 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    225 
    226 #define	SPINLOCK_LIST_LOCK()						\
    227 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
    228 
    229 #define	SPINLOCK_LIST_UNLOCK()						\
    230 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
    231 #else
    232 #define	SPINLOCK_LIST_LOCK()	/* nothing */
    233 
    234 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    235 #endif /* MULTIPROCESSOR */ /* } */
    236 
    237 TAILQ_HEAD(, lock) spinlock_list =
    238     TAILQ_HEAD_INITIALIZER(spinlock_list);
    239 
    240 #define	HAVEIT(lkp)							\
    241 do {									\
    242 	if ((lkp)->lk_flags & LK_SPIN) {				\
    243 		int s = spllock();					\
    244 		SPINLOCK_LIST_LOCK();					\
    245 		/* XXX Cast away volatile. */				\
    246 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    247 		    lk_list);						\
    248 		SPINLOCK_LIST_UNLOCK();					\
    249 		splx(s);						\
    250 	}								\
    251 } while (/*CONSTCOND*/0)
    252 
    253 #define	DONTHAVEIT(lkp)							\
    254 do {									\
    255 	if ((lkp)->lk_flags & LK_SPIN) {				\
    256 		int s = spllock();					\
    257 		SPINLOCK_LIST_LOCK();					\
    258 		/* XXX Cast away volatile. */				\
    259 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    260 		    lk_list);						\
    261 		SPINLOCK_LIST_UNLOCK();					\
    262 		splx(s);						\
    263 	}								\
    264 } while (/*CONSTCOND*/0)
    265 #else
    266 #define	HAVEIT(lkp)		/* nothing */
    267 
    268 #define	DONTHAVEIT(lkp)		/* nothing */
    269 #endif /* LOCKDEBUG */ /* } */
    270 
    271 #if defined(LOCKDEBUG)
    272 /*
    273  * Lock debug printing routine; can be configured to print to console
    274  * or log to syslog.
    275  */
    276 void
    277 lock_printf(const char *fmt, ...)
    278 {
    279 	va_list ap;
    280 
    281 	va_start(ap, fmt);
    282 	if (lock_debug_syslog)
    283 		vlog(LOG_DEBUG, fmt, ap);
    284 	else
    285 		vprintf(fmt, ap);
    286 	va_end(ap);
    287 }
    288 #endif /* LOCKDEBUG */
    289 
    290 /*
    291  * Initialize a lock; required before use.
    292  */
    293 void
    294 lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
    295 {
    296 
    297 	memset(lkp, 0, sizeof(struct lock));
    298 	simple_lock_init(&lkp->lk_interlock);
    299 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    300 	if (flags & LK_SPIN)
    301 		lkp->lk_cpu = LK_NOCPU;
    302 	else {
    303 		lkp->lk_lockholder = LK_NOPROC;
    304 		lkp->lk_prio = prio;
    305 		lkp->lk_timo = timo;
    306 	}
    307 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    308 }
    309 
    310 /*
    311  * Determine the status of a lock.
    312  */
    313 int
    314 lockstatus(struct lock *lkp)
    315 {
    316 	int s, lock_type = 0;
    317 
    318 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    319 	if (lkp->lk_exclusivecount != 0)
    320 		lock_type = LK_EXCLUSIVE;
    321 	else if (lkp->lk_sharecount != 0)
    322 		lock_type = LK_SHARED;
    323 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    324 	return (lock_type);
    325 }
    326 
    327 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
    328 /*
    329  * Make sure no spin locks are held by a CPU that is about
    330  * to context switch.
    331  */
    332 void
    333 spinlock_switchcheck(void)
    334 {
    335 	u_long cnt;
    336 	int s;
    337 
    338 	s = spllock();
    339 #if defined(MULTIPROCESSOR)
    340 	cnt = curcpu()->ci_spin_locks;
    341 #else
    342 	cnt = spin_locks;
    343 #endif
    344 	splx(s);
    345 
    346 	if (cnt != 0)
    347 		panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
    348 		    (u_long) cpu_number(), cnt);
    349 }
    350 #endif /* LOCKDEBUG || DIAGNOSTIC */
    351 
    352 /*
    353  * Locks and IPLs (interrupt priority levels):
    354  *
    355  * Locks which may be taken from interrupt context must be handled
    356  * very carefully; you must spl to the highest IPL where the lock
    357  * is needed before acquiring the lock.
    358  *
    359  * It is also important to avoid deadlock, since certain (very high
    360  * priority) interrupts are often needed to keep the system as a whole
    361  * from deadlocking, and must not be blocked while you are spinning
    362  * waiting for a lower-priority lock.
    363  *
    364  * In addition, the lock-debugging hooks themselves need to use locks!
    365  *
    366  * A raw __cpu_simple_lock may be used from interrupts are long as it
    367  * is acquired and held at a single IPL.
    368  *
    369  * A simple_lock (which is a __cpu_simple_lock wrapped with some
    370  * debugging hooks) may be used at or below spllock(), which is
    371  * typically at or just below splhigh() (i.e. blocks everything
    372  * but certain machine-dependent extremely high priority interrupts).
    373  *
    374  * spinlockmgr spinlocks should be used at or below splsched().
    375  *
    376  * Some platforms may have interrupts of higher priority than splsched(),
    377  * including hard serial interrupts, inter-processor interrupts, and
    378  * kernel debugger traps.
    379  */
    380 
    381 /*
    382  * XXX XXX kludge around another kludge..
    383  *
    384  * vfs_shutdown() may be called from interrupt context, either as a result
    385  * of a panic, or from the debugger.   It proceeds to call
    386  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    387  *
    388  * We would like to make an attempt to sync the filesystems in this case, so
    389  * if this happens, we treat attempts to acquire locks specially.
    390  * All locks are acquired on behalf of proc0.
    391  *
    392  * If we've already paniced, we don't block waiting for locks, but
    393  * just barge right ahead since we're already going down in flames.
    394  */
    395 
    396 /*
    397  * Set, change, or release a lock.
    398  *
    399  * Shared requests increment the shared count. Exclusive requests set the
    400  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    401  * accepted shared locks and shared-to-exclusive upgrades to go away.
    402  */
    403 int
    404 lockmgr(__volatile struct lock *lkp, u_int flags,
    405     struct simplelock *interlkp)
    406 {
    407 	int error;
    408 	pid_t pid;
    409 	int extflags;
    410 	cpuid_t cpu_id;
    411 	struct proc *p = curproc;
    412 	int lock_shutdown_noblock = 0;
    413 	int s;
    414 
    415 	error = 0;
    416 
    417 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    418 	if (flags & LK_INTERLOCK)
    419 		simple_unlock(interlkp);
    420 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    421 
    422 #ifdef DIAGNOSTIC /* { */
    423 	/*
    424 	 * Don't allow spins on sleep locks and don't allow sleeps
    425 	 * on spin locks.
    426 	 */
    427 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    428 		panic("lockmgr: sleep/spin mismatch\n");
    429 #endif /* } */
    430 
    431 	if (extflags & LK_SPIN)
    432 		pid = LK_KERNPROC;
    433 	else {
    434 		if (p == NULL) {
    435 			if (!doing_shutdown) {
    436 #ifdef DIAGNOSTIC
    437 				panic("lockmgr: no context");
    438 #endif
    439 			} else {
    440 				p = &proc0;
    441 				if (panicstr && (!(flags & LK_NOWAIT))) {
    442 					flags |= LK_NOWAIT;
    443 					lock_shutdown_noblock = 1;
    444 				}
    445 			}
    446 		}
    447 		pid = p->p_pid;
    448 	}
    449 	cpu_id = cpu_number();
    450 
    451 	/*
    452 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    453 	 * exclusive lock is returned. The only valid operation thereafter
    454 	 * is a single release of that exclusive lock. This final release
    455 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    456 	 * further requests of any sort will result in a panic. The bits
    457 	 * selected for these two flags are chosen so that they will be set
    458 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    459 	 * The final release is permitted to give a new lease on life to
    460 	 * the lock by specifying LK_REENABLE.
    461 	 */
    462 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    463 #ifdef DIAGNOSTIC /* { */
    464 		if (lkp->lk_flags & LK_DRAINED)
    465 			panic("lockmgr: using decommissioned lock");
    466 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    467 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    468 			panic("lockmgr: non-release on draining lock: %d\n",
    469 			    flags & LK_TYPE_MASK);
    470 #endif /* DIAGNOSTIC */ /* } */
    471 		lkp->lk_flags &= ~LK_DRAINING;
    472 		if ((flags & LK_REENABLE) == 0)
    473 			lkp->lk_flags |= LK_DRAINED;
    474 	}
    475 
    476 	switch (flags & LK_TYPE_MASK) {
    477 
    478 	case LK_SHARED:
    479 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    480 			/*
    481 			 * If just polling, check to see if we will block.
    482 			 */
    483 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    484 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    485 				error = EBUSY;
    486 				break;
    487 			}
    488 			/*
    489 			 * Wait for exclusive locks and upgrades to clear.
    490 			 */
    491 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    492 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    493 			if (error)
    494 				break;
    495 			lkp->lk_sharecount++;
    496 			COUNT(lkp, p, cpu_id, 1);
    497 			break;
    498 		}
    499 		/*
    500 		 * We hold an exclusive lock, so downgrade it to shared.
    501 		 * An alternative would be to fail with EDEADLK.
    502 		 */
    503 		lkp->lk_sharecount++;
    504 		COUNT(lkp, p, cpu_id, 1);
    505 		/* fall into downgrade */
    506 
    507 	case LK_DOWNGRADE:
    508 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    509 		    lkp->lk_exclusivecount == 0)
    510 			panic("lockmgr: not holding exclusive lock");
    511 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    512 		lkp->lk_exclusivecount = 0;
    513 		lkp->lk_recurselevel = 0;
    514 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    515 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    516 		DONTHAVEIT(lkp);
    517 		WAKEUP_WAITER(lkp);
    518 		break;
    519 
    520 	case LK_EXCLUPGRADE:
    521 		/*
    522 		 * If another process is ahead of us to get an upgrade,
    523 		 * then we want to fail rather than have an intervening
    524 		 * exclusive access.
    525 		 */
    526 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    527 			lkp->lk_sharecount--;
    528 			COUNT(lkp, p, cpu_id, -1);
    529 			error = EBUSY;
    530 			break;
    531 		}
    532 		/* fall into normal upgrade */
    533 
    534 	case LK_UPGRADE:
    535 		/*
    536 		 * Upgrade a shared lock to an exclusive one. If another
    537 		 * shared lock has already requested an upgrade to an
    538 		 * exclusive lock, our shared lock is released and an
    539 		 * exclusive lock is requested (which will be granted
    540 		 * after the upgrade). If we return an error, the file
    541 		 * will always be unlocked.
    542 		 */
    543 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    544 			panic("lockmgr: upgrade exclusive lock");
    545 		lkp->lk_sharecount--;
    546 		COUNT(lkp, p, cpu_id, -1);
    547 		/*
    548 		 * If we are just polling, check to see if we will block.
    549 		 */
    550 		if ((extflags & LK_NOWAIT) &&
    551 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    552 		     lkp->lk_sharecount > 1)) {
    553 			error = EBUSY;
    554 			break;
    555 		}
    556 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    557 			/*
    558 			 * We are first shared lock to request an upgrade, so
    559 			 * request upgrade and wait for the shared count to
    560 			 * drop to zero, then take exclusive lock.
    561 			 */
    562 			lkp->lk_flags |= LK_WANT_UPGRADE;
    563 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    564 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    565 			if (error)
    566 				break;
    567 			lkp->lk_flags |= LK_HAVE_EXCL;
    568 			SETHOLDER(lkp, pid, cpu_id);
    569 			HAVEIT(lkp);
    570 			if (lkp->lk_exclusivecount != 0)
    571 				panic("lockmgr: non-zero exclusive count");
    572 			lkp->lk_exclusivecount = 1;
    573 			if (extflags & LK_SETRECURSE)
    574 				lkp->lk_recurselevel = 1;
    575 			COUNT(lkp, p, cpu_id, 1);
    576 			break;
    577 		}
    578 		/*
    579 		 * Someone else has requested upgrade. Release our shared
    580 		 * lock, awaken upgrade requestor if we are the last shared
    581 		 * lock, then request an exclusive lock.
    582 		 */
    583 		if (lkp->lk_sharecount == 0)
    584 			WAKEUP_WAITER(lkp);
    585 		/* fall into exclusive request */
    586 
    587 	case LK_EXCLUSIVE:
    588 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    589 			/*
    590 			 * Recursive lock.
    591 			 */
    592 			if ((extflags & LK_CANRECURSE) == 0 &&
    593 			     lkp->lk_recurselevel == 0) {
    594 				if (extflags & LK_RECURSEFAIL) {
    595 					error = EDEADLK;
    596 					break;
    597 				} else
    598 					panic("lockmgr: locking against myself");
    599 			}
    600 			lkp->lk_exclusivecount++;
    601 			if (extflags & LK_SETRECURSE &&
    602 			    lkp->lk_recurselevel == 0)
    603 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    604 			COUNT(lkp, p, cpu_id, 1);
    605 			break;
    606 		}
    607 		/*
    608 		 * If we are just polling, check to see if we will sleep.
    609 		 */
    610 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    611 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    612 		     lkp->lk_sharecount != 0)) {
    613 			error = EBUSY;
    614 			break;
    615 		}
    616 		/*
    617 		 * Try to acquire the want_exclusive flag.
    618 		 */
    619 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    620 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    621 		if (error)
    622 			break;
    623 		lkp->lk_flags |= LK_WANT_EXCL;
    624 		/*
    625 		 * Wait for shared locks and upgrades to finish.
    626 		 */
    627 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    628 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    629 		lkp->lk_flags &= ~LK_WANT_EXCL;
    630 		if (error)
    631 			break;
    632 		lkp->lk_flags |= LK_HAVE_EXCL;
    633 		SETHOLDER(lkp, pid, cpu_id);
    634 		HAVEIT(lkp);
    635 		if (lkp->lk_exclusivecount != 0)
    636 			panic("lockmgr: non-zero exclusive count");
    637 		lkp->lk_exclusivecount = 1;
    638 		if (extflags & LK_SETRECURSE)
    639 			lkp->lk_recurselevel = 1;
    640 		COUNT(lkp, p, cpu_id, 1);
    641 		break;
    642 
    643 	case LK_RELEASE:
    644 		if (lkp->lk_exclusivecount != 0) {
    645 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    646 				if (lkp->lk_flags & LK_SPIN) {
    647 					panic("lockmgr: processor %lu, not "
    648 					    "exclusive lock holder %lu "
    649 					    "unlocking", cpu_id, lkp->lk_cpu);
    650 				} else {
    651 					panic("lockmgr: pid %d, not "
    652 					    "exclusive lock holder %d "
    653 					    "unlocking", pid,
    654 					    lkp->lk_lockholder);
    655 				}
    656 			}
    657 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    658 				lkp->lk_recurselevel = 0;
    659 			lkp->lk_exclusivecount--;
    660 			COUNT(lkp, p, cpu_id, -1);
    661 			if (lkp->lk_exclusivecount == 0) {
    662 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    663 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    664 				DONTHAVEIT(lkp);
    665 			}
    666 		} else if (lkp->lk_sharecount != 0) {
    667 			lkp->lk_sharecount--;
    668 			COUNT(lkp, p, cpu_id, -1);
    669 		}
    670 #ifdef DIAGNOSTIC
    671 		else
    672 			panic("lockmgr: release of unlocked lock!");
    673 #endif
    674 		WAKEUP_WAITER(lkp);
    675 		break;
    676 
    677 	case LK_DRAIN:
    678 		/*
    679 		 * Check that we do not already hold the lock, as it can
    680 		 * never drain if we do. Unfortunately, we have no way to
    681 		 * check for holding a shared lock, but at least we can
    682 		 * check for an exclusive one.
    683 		 */
    684 		if (WEHOLDIT(lkp, pid, cpu_id))
    685 			panic("lockmgr: draining against myself");
    686 		/*
    687 		 * If we are just polling, check to see if we will sleep.
    688 		 */
    689 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    690 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    691 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    692 			error = EBUSY;
    693 			break;
    694 		}
    695 		ACQUIRE(lkp, error, extflags, 1,
    696 		    ((lkp->lk_flags &
    697 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    698 		     lkp->lk_sharecount != 0 ||
    699 		     lkp->lk_waitcount != 0));
    700 		if (error)
    701 			break;
    702 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    703 		SETHOLDER(lkp, pid, cpu_id);
    704 		HAVEIT(lkp);
    705 		lkp->lk_exclusivecount = 1;
    706 		/* XXX unlikely that we'd want this */
    707 		if (extflags & LK_SETRECURSE)
    708 			lkp->lk_recurselevel = 1;
    709 		COUNT(lkp, p, cpu_id, 1);
    710 		break;
    711 
    712 	default:
    713 		INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    714 		panic("lockmgr: unknown locktype request %d",
    715 		    flags & LK_TYPE_MASK);
    716 		/* NOTREACHED */
    717 	}
    718 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    719 	    ((lkp->lk_flags &
    720 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    721 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    722 		lkp->lk_flags &= ~LK_WAITDRAIN;
    723 		wakeup_one((void *)&lkp->lk_flags);
    724 	}
    725 	/*
    726 	 * Note that this panic will be a recursive panic, since
    727 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    728 	 */
    729 	if (error && lock_shutdown_noblock)
    730 		panic("lockmgr: deadlock (see previous panic)");
    731 
    732 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    733 	return (error);
    734 }
    735 
    736 /*
    737  * For a recursive spinlock held one or more times by the current CPU,
    738  * release all N locks, and return N.
    739  * Intended for use in mi_switch() shortly before context switching.
    740  */
    741 
    742 int
    743 spinlock_release_all(__volatile struct lock *lkp)
    744 {
    745 	int s, count;
    746 	cpuid_t cpu_id;
    747 
    748 	KASSERT(lkp->lk_flags & LK_SPIN);
    749 
    750 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    751 
    752 	cpu_id = cpu_number();
    753 	count = lkp->lk_exclusivecount;
    754 
    755 	if (count != 0) {
    756 #ifdef DIAGNOSTIC
    757 		if (WEHOLDIT(lkp, 0, cpu_id) == 0) {
    758 			panic("spinlock_release_all: processor %lu, not "
    759 			    "exclusive lock holder %lu "
    760 			    "unlocking", (long)cpu_id, lkp->lk_cpu);
    761 		}
    762 #endif
    763 		lkp->lk_recurselevel = 0;
    764 		lkp->lk_exclusivecount = 0;
    765 		COUNT_CPU(cpu_id, -count);
    766 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    767 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    768 		DONTHAVEIT(lkp);
    769 	}
    770 #ifdef DIAGNOSTIC
    771 	else if (lkp->lk_sharecount != 0)
    772 		panic("spinlock_release_all: release of shared lock!");
    773 	else
    774 		panic("spinlock_release_all: release of unlocked lock!");
    775 #endif
    776 	INTERLOCK_RELEASE(lkp, LK_SPIN, s);
    777 
    778 	return (count);
    779 }
    780 
    781 /*
    782  * For a recursive spinlock held one or more times by the current CPU,
    783  * release all N locks, and return N.
    784  * Intended for use in mi_switch() right after resuming execution.
    785  */
    786 
    787 void
    788 spinlock_acquire_count(__volatile struct lock *lkp, int count)
    789 {
    790 	int s, error;
    791 	cpuid_t cpu_id;
    792 
    793 	KASSERT(lkp->lk_flags & LK_SPIN);
    794 
    795 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    796 
    797 	cpu_id = cpu_number();
    798 
    799 #ifdef DIAGNOSTIC
    800 	if (WEHOLDIT(lkp, LK_NOPROC, cpu_id))
    801 		panic("spinlock_acquire_count: processor %lu already holds lock\n", (long)cpu_id);
    802 #endif
    803 	/*
    804 	 * Try to acquire the want_exclusive flag.
    805 	 */
    806 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_flags &
    807 	    (LK_HAVE_EXCL | LK_WANT_EXCL));
    808 	lkp->lk_flags |= LK_WANT_EXCL;
    809 	/*
    810 	 * Wait for shared locks and upgrades to finish.
    811 	 */
    812 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_sharecount != 0 ||
    813 	    (lkp->lk_flags & LK_WANT_UPGRADE));
    814 	lkp->lk_flags &= ~LK_WANT_EXCL;
    815 	lkp->lk_flags |= LK_HAVE_EXCL;
    816 	SETHOLDER(lkp, LK_NOPROC, cpu_id);
    817 	HAVEIT(lkp);
    818 	if (lkp->lk_exclusivecount != 0)
    819 		panic("lockmgr: non-zero exclusive count");
    820 	lkp->lk_exclusivecount = count;
    821 	lkp->lk_recurselevel = 1;
    822 	COUNT_CPU(cpu_id, count);
    823 
    824 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    825 }
    826 
    827 
    828 
    829 /*
    830  * Print out information about state of a lock. Used by VOP_PRINT
    831  * routines to display ststus about contained locks.
    832  */
    833 void
    834 lockmgr_printinfo(__volatile struct lock *lkp)
    835 {
    836 
    837 	if (lkp->lk_sharecount)
    838 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    839 		    lkp->lk_sharecount);
    840 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    841 		printf(" lock type %s: EXCL (count %d) by ",
    842 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    843 		if (lkp->lk_flags & LK_SPIN)
    844 			printf("processor %lu", lkp->lk_cpu);
    845 		else
    846 			printf("pid %d", lkp->lk_lockholder);
    847 	} else
    848 		printf(" not locked");
    849 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    850 		printf(" with %d pending", lkp->lk_waitcount);
    851 }
    852 
    853 #if defined(LOCKDEBUG) /* { */
    854 TAILQ_HEAD(, simplelock) simplelock_list =
    855     TAILQ_HEAD_INITIALIZER(simplelock_list);
    856 
    857 #if defined(MULTIPROCESSOR) /* { */
    858 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    859 
    860 #define	SLOCK_LIST_LOCK()						\
    861 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
    862 
    863 #define	SLOCK_LIST_UNLOCK()						\
    864 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
    865 
    866 #define	SLOCK_COUNT(x)							\
    867 	curcpu()->ci_simple_locks += (x)
    868 #else
    869 u_long simple_locks;
    870 
    871 #define	SLOCK_LIST_LOCK()	/* nothing */
    872 
    873 #define	SLOCK_LIST_UNLOCK()	/* nothing */
    874 
    875 #define	SLOCK_COUNT(x)		simple_locks += (x)
    876 #endif /* MULTIPROCESSOR */ /* } */
    877 
    878 #ifdef DDB /* { */
    879 #ifdef MULTIPROCESSOR
    880 int simple_lock_debugger = 1;	/* more serious on MP */
    881 #else
    882 int simple_lock_debugger = 0;
    883 #endif
    884 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    885 #else
    886 #define	SLOCK_DEBUGGER()	/* nothing */
    887 #endif /* } */
    888 
    889 #ifdef MULTIPROCESSOR
    890 #define SLOCK_MP()		lock_printf("on cpu %ld\n", 		\
    891 				    (u_long) cpu_number())
    892 #else
    893 #define SLOCK_MP()		/* nothing */
    894 #endif
    895 
    896 #define	SLOCK_WHERE(str, alp, id, l)					\
    897 do {									\
    898 	lock_printf(str);						\
    899 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
    900 	SLOCK_MP();							\
    901 	if ((alp)->lock_file != NULL)					\
    902 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
    903 		    (alp)->lock_line);					\
    904 	if ((alp)->unlock_file != NULL)					\
    905 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
    906 		    (alp)->unlock_line);				\
    907 	SLOCK_DEBUGGER();						\
    908 } while (/*CONSTCOND*/0)
    909 
    910 /*
    911  * Simple lock functions so that the debugger can see from whence
    912  * they are being called.
    913  */
    914 void
    915 simple_lock_init(struct simplelock *alp)
    916 {
    917 
    918 #if defined(MULTIPROCESSOR) /* { */
    919 	__cpu_simple_lock_init(&alp->lock_data);
    920 #else
    921 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    922 #endif /* } */
    923 	alp->lock_file = NULL;
    924 	alp->lock_line = 0;
    925 	alp->unlock_file = NULL;
    926 	alp->unlock_line = 0;
    927 	alp->lock_holder = LK_NOCPU;
    928 }
    929 
    930 void
    931 _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
    932 {
    933 	cpuid_t cpu_id = cpu_number();
    934 	int s;
    935 
    936 	s = spllock();
    937 
    938 	/*
    939 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    940 	 * don't take any action, and just fall into the normal spin case.
    941 	 */
    942 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    943 #if defined(MULTIPROCESSOR) /* { */
    944 		if (alp->lock_holder == cpu_id) {
    945 			SLOCK_WHERE("simple_lock: locking against myself\n",
    946 			    alp, id, l);
    947 			goto out;
    948 		}
    949 #else
    950 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    951 		goto out;
    952 #endif /* MULTIPROCESSOR */ /* } */
    953 	}
    954 
    955 #if defined(MULTIPROCESSOR) /* { */
    956 	/* Acquire the lock before modifying any fields. */
    957 	__cpu_simple_lock(&alp->lock_data);
    958 #else
    959 	alp->lock_data = __SIMPLELOCK_LOCKED;
    960 #endif /* } */
    961 
    962 	if (alp->lock_holder != LK_NOCPU) {
    963 		SLOCK_WHERE("simple_lock: uninitialized lock\n",
    964 		    alp, id, l);
    965 	}
    966 	alp->lock_file = id;
    967 	alp->lock_line = l;
    968 	alp->lock_holder = cpu_id;
    969 
    970 	SLOCK_LIST_LOCK();
    971 	/* XXX Cast away volatile */
    972 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    973 	SLOCK_LIST_UNLOCK();
    974 
    975 	SLOCK_COUNT(1);
    976 
    977  out:
    978 	splx(s);
    979 }
    980 
    981 int
    982 _simple_lock_held(__volatile struct simplelock *alp)
    983 {
    984 	cpuid_t cpu_id = cpu_number();
    985 	int s, locked = 0;
    986 
    987 	s = spllock();
    988 
    989 #if defined(MULTIPROCESSOR)
    990 	if (__cpu_simple_lock_try(&alp->lock_data) == 0)
    991 		locked = (alp->lock_holder == cpu_id);
    992 	else
    993 		__cpu_simple_unlock(&alp->lock_data);
    994 #else
    995 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    996 		locked = 1;
    997 		KASSERT(alp->lock_holder == cpu_id);
    998 	}
    999 #endif
   1000 
   1001 	splx(s);
   1002 
   1003 	return (locked);
   1004 }
   1005 
   1006 int
   1007 _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
   1008 {
   1009 	cpuid_t cpu_id = cpu_number();
   1010 	int s, rv = 0;
   1011 
   1012 	s = spllock();
   1013 
   1014 	/*
   1015 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1016 	 * don't take any action.
   1017 	 */
   1018 #if defined(MULTIPROCESSOR) /* { */
   1019 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
   1020 		if (alp->lock_holder == cpu_id)
   1021 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
   1022 			    alp, id, l);
   1023 		goto out;
   1024 	}
   1025 #else
   1026 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1027 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
   1028 		goto out;
   1029 	}
   1030 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1031 #endif /* MULTIPROCESSOR */ /* } */
   1032 
   1033 	/*
   1034 	 * At this point, we have acquired the lock.
   1035 	 */
   1036 
   1037 	rv = 1;
   1038 
   1039 	alp->lock_file = id;
   1040 	alp->lock_line = l;
   1041 	alp->lock_holder = cpu_id;
   1042 
   1043 	SLOCK_LIST_LOCK();
   1044 	/* XXX Cast away volatile. */
   1045 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
   1046 	SLOCK_LIST_UNLOCK();
   1047 
   1048 	SLOCK_COUNT(1);
   1049 
   1050  out:
   1051 	splx(s);
   1052 	return (rv);
   1053 }
   1054 
   1055 void
   1056 _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
   1057 {
   1058 	int s;
   1059 
   1060 	s = spllock();
   1061 
   1062 	/*
   1063 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
   1064 	 * the lock, and if we don't, we don't take any action.
   1065 	 */
   1066 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
   1067 		SLOCK_WHERE("simple_unlock: lock not held\n",
   1068 		    alp, id, l);
   1069 		goto out;
   1070 	}
   1071 
   1072 	SLOCK_LIST_LOCK();
   1073 	TAILQ_REMOVE(&simplelock_list, alp, list);
   1074 	SLOCK_LIST_UNLOCK();
   1075 
   1076 	SLOCK_COUNT(-1);
   1077 
   1078 	alp->list.tqe_next = NULL;	/* sanity */
   1079 	alp->list.tqe_prev = NULL;	/* sanity */
   1080 
   1081 	alp->unlock_file = id;
   1082 	alp->unlock_line = l;
   1083 
   1084 #if defined(MULTIPROCESSOR) /* { */
   1085 	alp->lock_holder = LK_NOCPU;
   1086 	/* Now that we've modified all fields, release the lock. */
   1087 	__cpu_simple_unlock(&alp->lock_data);
   1088 #else
   1089 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1090 	KASSERT(alp->lock_holder == cpu_number());
   1091 	alp->lock_holder = LK_NOCPU;
   1092 #endif /* } */
   1093 
   1094  out:
   1095 	splx(s);
   1096 }
   1097 
   1098 void
   1099 simple_lock_dump(void)
   1100 {
   1101 	struct simplelock *alp;
   1102 	int s;
   1103 
   1104 	s = spllock();
   1105 	SLOCK_LIST_LOCK();
   1106 	lock_printf("all simple locks:\n");
   1107 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
   1108 	     alp = TAILQ_NEXT(alp, list)) {
   1109 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
   1110 		    alp->lock_file, alp->lock_line);
   1111 	}
   1112 	SLOCK_LIST_UNLOCK();
   1113 	splx(s);
   1114 }
   1115 
   1116 void
   1117 simple_lock_freecheck(void *start, void *end)
   1118 {
   1119 	struct simplelock *alp;
   1120 	int s;
   1121 
   1122 	s = spllock();
   1123 	SLOCK_LIST_LOCK();
   1124 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
   1125 	     alp = TAILQ_NEXT(alp, list)) {
   1126 		if ((void *)alp >= start && (void *)alp < end) {
   1127 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
   1128 			    alp, alp->lock_holder, alp->lock_file,
   1129 			    alp->lock_line);
   1130 			SLOCK_DEBUGGER();
   1131 		}
   1132 	}
   1133 	SLOCK_LIST_UNLOCK();
   1134 	splx(s);
   1135 }
   1136 
   1137 void
   1138 simple_lock_switchcheck(void)
   1139 {
   1140 	struct simplelock *alp;
   1141 	cpuid_t cpu_id = cpu_number();
   1142 	int s;
   1143 
   1144 	/*
   1145 	 * We must be holding exactly one lock: the sched_lock.
   1146 	 */
   1147 
   1148 	SCHED_ASSERT_LOCKED();
   1149 
   1150 	s = spllock();
   1151 	SLOCK_LIST_LOCK();
   1152 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
   1153 	     alp = TAILQ_NEXT(alp, list)) {
   1154 		if (alp == &sched_lock)
   1155 			continue;
   1156 		if (alp->lock_holder == cpu_id) {
   1157 			lock_printf("switching with held simple_lock %p "
   1158 			    "CPU %lu %s:%d\n",
   1159 			    alp, alp->lock_holder, alp->lock_file,
   1160 			    alp->lock_line);
   1161 			SLOCK_DEBUGGER();
   1162 		}
   1163 	}
   1164 	SLOCK_LIST_UNLOCK();
   1165 	splx(s);
   1166 }
   1167 #endif /* LOCKDEBUG */ /* } */
   1168