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