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