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