Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.35
      1 /*	$NetBSD: kern_lock.c,v 1.35 2000/08/07 22:10:53 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center.
     10  *
     11  * This code is derived from software contributed to The NetBSD Foundation
     12  * by Ross Harvey.
     13  *
     14  * Redistribution and use in source and binary forms, with or without
     15  * modification, are permitted provided that the following conditions
     16  * are met:
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  * 3. All advertising materials mentioning features or use of this software
     23  *    must display the following acknowledgement:
     24  *	This product includes software developed by the NetBSD
     25  *	Foundation, Inc. and its contributors.
     26  * 4. Neither the name of The NetBSD Foundation nor the names of its
     27  *    contributors may be used to endorse or promote products derived
     28  *    from this software without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     40  * POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 
     43 /*
     44  * Copyright (c) 1995
     45  *	The Regents of the University of California.  All rights reserved.
     46  *
     47  * This code contains ideas from software contributed to Berkeley by
     48  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
     49  * System project at Carnegie-Mellon University.
     50  *
     51  * Redistribution and use in source and binary forms, with or without
     52  * modification, are permitted provided that the following conditions
     53  * are met:
     54  * 1. Redistributions of source code must retain the above copyright
     55  *    notice, this list of conditions and the following disclaimer.
     56  * 2. Redistributions in binary form must reproduce the above copyright
     57  *    notice, this list of conditions and the following disclaimer in the
     58  *    documentation and/or other materials provided with the distribution.
     59  * 3. All advertising materials mentioning features or use of this software
     60  *    must display the following acknowledgement:
     61  *	This product includes software developed by the University of
     62  *	California, Berkeley and its contributors.
     63  * 4. Neither the name of the University nor the names of its contributors
     64  *    may be used to endorse or promote products derived from this software
     65  *    without specific prior written permission.
     66  *
     67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77  * SUCH DAMAGE.
     78  *
     79  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     80  */
     81 
     82 #include "opt_multiprocessor.h"
     83 #include "opt_lockdebug.h"
     84 #include "opt_ddb.h"
     85 
     86 #include <sys/param.h>
     87 #include <sys/proc.h>
     88 #include <sys/lock.h>
     89 #include <sys/systm.h>
     90 #include <machine/cpu.h>
     91 
     92 #if defined(__HAVE_ATOMIC_OPERATIONS)
     93 #include <machine/atomic.h>
     94 #endif
     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 
    107 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    108 #endif
    109 
    110 /*
    111  * Locking primitives implementation.
    112  * Locks provide shared/exclusive sychronization.
    113  */
    114 
    115 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    116 #if defined(MULTIPROCESSOR) /* { */
    117 #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
    118 #define	COUNT_CPU(cpu_id, x)						\
    119 	atomic_add_ulong(&curcpu()->ci_spin_locks, (x))
    120 #else
    121 #define	COUNT_CPU(cpu_id, x)	/* not safe */
    122 #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
    123 #else
    124 u_long	spin_locks;
    125 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    126 #endif /* MULTIPROCESSOR */ /* } */
    127 
    128 #define	COUNT(lkp, p, cpu_id, x)					\
    129 do {									\
    130 	if ((lkp)->lk_flags & LK_SPIN)					\
    131 		COUNT_CPU((cpu_id), (x));				\
    132 	else								\
    133 		(p)->p_locks += (x);					\
    134 } while (/*CONSTCOND*/0)
    135 #else
    136 #define COUNT(lkp, p, cpu_id, x)
    137 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    138 
    139 /*
    140  * Acquire a resource.
    141  */
    142 #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
    143 	if ((extflags) & LK_SPIN) {					\
    144 		int interlocked;					\
    145 									\
    146 		if ((drain) == 0)					\
    147 			(lkp)->lk_waitcount++;				\
    148 		for (interlocked = 1;;) {				\
    149 			if (wanted) {					\
    150 				if (interlocked) {			\
    151 					simple_unlock(&(lkp)->lk_interlock); \
    152 					interlocked = 0;		\
    153 				}					\
    154 			} else if (interlocked) {			\
    155 				break;					\
    156 			} else {					\
    157 				simple_lock(&(lkp)->lk_interlock);	\
    158 				interlocked = 1;			\
    159 			}						\
    160 		}							\
    161 		if ((drain) == 0)					\
    162 			(lkp)->lk_waitcount--;				\
    163 		KASSERT((wanted) == 0);					\
    164 		error = 0;	/* sanity */				\
    165 	} else {							\
    166 		for (error = 0; wanted; ) {				\
    167 			if ((drain))					\
    168 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
    169 			else						\
    170 				(lkp)->lk_waitcount++;			\
    171 			/* XXX Cast away volatile. */			\
    172 			error = ltsleep((drain) ? &(lkp)->lk_flags :	\
    173 			    (void *)(lkp), (lkp)->lk_prio,		\
    174 			    (lkp)->lk_wmesg, (lkp)->lk_timo,		\
    175 			    &(lkp)->lk_interlock);			\
    176 			if ((drain) == 0)				\
    177 				(lkp)->lk_waitcount--;			\
    178 			if (error)					\
    179 				break;					\
    180 			if ((extflags) & LK_SLEEPFAIL) {		\
    181 				error = ENOLCK;				\
    182 				break;					\
    183 			}						\
    184 		}							\
    185 	}
    186 
    187 #define	SETHOLDER(lkp, pid, cpu_id)					\
    188 do {									\
    189 	if ((lkp)->lk_flags & LK_SPIN)					\
    190 		(lkp)->lk_cpu = cpu_id;					\
    191 	else								\
    192 		(lkp)->lk_lockholder = pid;				\
    193 } while (/*CONSTCOND*/0)
    194 
    195 #define	WEHOLDIT(lkp, pid, cpu_id)					\
    196 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    197 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    198 
    199 #define	WAKEUP_WAITER(lkp)						\
    200 do {									\
    201 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
    202 		/* XXX Cast away volatile. */				\
    203 		wakeup_one((void *)(lkp));				\
    204 	}								\
    205 } while (/*CONSTCOND*/0)
    206 
    207 #if defined(LOCKDEBUG) /* { */
    208 #if defined(MULTIPROCESSOR) /* { */
    209 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    210 
    211 #define	SPINLOCK_LIST_LOCK()						\
    212 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
    213 
    214 #define	SPINLOCK_LIST_UNLOCK()						\
    215 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
    216 #else
    217 #define	SPINLOCK_LIST_LOCK()	/* nothing */
    218 
    219 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    220 #endif /* MULTIPROCESSOR */ /* } */
    221 
    222 TAILQ_HEAD(, lock) spinlock_list =
    223     TAILQ_HEAD_INITIALIZER(spinlock_list);
    224 
    225 #define	HAVEIT(lkp)							\
    226 do {									\
    227 	if ((lkp)->lk_flags & LK_SPIN) {				\
    228 		int s = splhigh();					\
    229 		SPINLOCK_LIST_LOCK();					\
    230 		/* XXX Cast away volatile. */				\
    231 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    232 		    lk_list);						\
    233 		SPINLOCK_LIST_UNLOCK();					\
    234 		splx(s);						\
    235 	}								\
    236 } while (/*CONSTCOND*/0)
    237 
    238 #define	DONTHAVEIT(lkp)							\
    239 do {									\
    240 	if ((lkp)->lk_flags & LK_SPIN) {				\
    241 		int s = splhigh();					\
    242 		SPINLOCK_LIST_LOCK();					\
    243 		/* XXX Cast away volatile. */				\
    244 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    245 		    lk_list);						\
    246 		SPINLOCK_LIST_UNLOCK();					\
    247 		splx(s);						\
    248 	}								\
    249 } while (/*CONSTCOND*/0)
    250 #else
    251 #define	HAVEIT(lkp)		/* nothing */
    252 
    253 #define	DONTHAVEIT(lkp)		/* nothing */
    254 #endif /* LOCKDEBUG */ /* } */
    255 
    256 #if defined(LOCKDEBUG)
    257 /*
    258  * Lock debug printing routine; can be configured to print to console
    259  * or log to syslog.
    260  */
    261 void
    262 lock_printf(const char *fmt, ...)
    263 {
    264 	va_list ap;
    265 
    266 	va_start(ap, fmt);
    267 	if (lock_debug_syslog)
    268 		vlog(LOG_DEBUG, fmt, ap);
    269 	else
    270 		vprintf(fmt, ap);
    271 	va_end(ap);
    272 }
    273 #endif /* LOCKDEBUG */
    274 
    275 /*
    276  * Initialize a lock; required before use.
    277  */
    278 void
    279 lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
    280 {
    281 
    282 	memset(lkp, 0, sizeof(struct lock));
    283 	simple_lock_init(&lkp->lk_interlock);
    284 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    285 	if (flags & LK_SPIN)
    286 		lkp->lk_cpu = LK_NOCPU;
    287 	else {
    288 		lkp->lk_lockholder = LK_NOPROC;
    289 		lkp->lk_prio = prio;
    290 		lkp->lk_timo = timo;
    291 	}
    292 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    293 }
    294 
    295 /*
    296  * Determine the status of a lock.
    297  */
    298 int
    299 lockstatus(struct lock *lkp)
    300 {
    301 	int lock_type = 0;
    302 
    303 	simple_lock(&lkp->lk_interlock);
    304 	if (lkp->lk_exclusivecount != 0)
    305 		lock_type = LK_EXCLUSIVE;
    306 	else if (lkp->lk_sharecount != 0)
    307 		lock_type = LK_SHARED;
    308 	simple_unlock(&lkp->lk_interlock);
    309 	return (lock_type);
    310 }
    311 
    312 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
    313 /*
    314  * Make sure no spin locks are held by a CPU that is about
    315  * to context switch.
    316  */
    317 void
    318 spinlock_switchcheck(void)
    319 {
    320 	u_long cnt;
    321 	int s;
    322 
    323 	s = splhigh();
    324 #if defined(MULTIPROCESSOR)
    325 	cnt = curcpu()->ci_spin_locks;
    326 #else
    327 	cnt = spin_locks;
    328 #endif
    329 	splx(s);
    330 
    331 	if (cnt != 0)
    332 		panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
    333 		    (u_long) cpu_number(), cnt);
    334 }
    335 #endif /* LOCKDEBUG || DIAGNOSTIC */
    336 
    337 /*
    338  * XXX XXX kludge around another kludge..
    339  *
    340  * vfs_shutdown() may be called from interrupt context, either as a result
    341  * of a panic, or from the debugger.   It proceeds to call
    342  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    343  *
    344  * We would like to make an attempt to sync the filesystems in this case, so
    345  * if this happens, we treat attempts to acquire locks specially.
    346  * All locks are acquired on behalf of proc0.
    347  *
    348  * If we've already paniced, we don't block waiting for locks, but
    349  * just barge right ahead since we're already going down in flames.
    350  */
    351 
    352 /*
    353  * Set, change, or release a lock.
    354  *
    355  * Shared requests increment the shared count. Exclusive requests set the
    356  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    357  * accepted shared locks and shared-to-exclusive upgrades to go away.
    358  */
    359 int
    360 lockmgr(__volatile struct lock *lkp, u_int flags,
    361     struct simplelock *interlkp)
    362 {
    363 	int error;
    364 	pid_t pid;
    365 	int extflags;
    366 	cpuid_t cpu_id;
    367 	struct proc *p = curproc;
    368 	int lock_shutdown_noblock = 0;
    369 
    370 	error = 0;
    371 
    372 	simple_lock(&lkp->lk_interlock);
    373 	if (flags & LK_INTERLOCK)
    374 		simple_unlock(interlkp);
    375 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    376 
    377 #ifdef DIAGNOSTIC /* { */
    378 	/*
    379 	 * Don't allow spins on sleep locks and don't allow sleeps
    380 	 * on spin locks.
    381 	 */
    382 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    383 		panic("lockmgr: sleep/spin mismatch\n");
    384 #endif /* } */
    385 
    386 	if (extflags & LK_SPIN)
    387 		pid = LK_KERNPROC;
    388 	else {
    389 		if (p == NULL) {
    390 			if (!doing_shutdown) {
    391 #ifdef DIAGNOSTIC
    392 				panic("lockmgr: no context");
    393 #endif
    394 			} else {
    395 				p = &proc0;
    396 				if (panicstr && (!(flags & LK_NOWAIT))) {
    397 					flags |= LK_NOWAIT;
    398 					lock_shutdown_noblock = 1;
    399 				}
    400 			}
    401 		}
    402 		pid = p->p_pid;
    403 	}
    404 	cpu_id = cpu_number();
    405 
    406 	/*
    407 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    408 	 * exclusive lock is returned. The only valid operation thereafter
    409 	 * is a single release of that exclusive lock. This final release
    410 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    411 	 * further requests of any sort will result in a panic. The bits
    412 	 * selected for these two flags are chosen so that they will be set
    413 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    414 	 * The final release is permitted to give a new lease on life to
    415 	 * the lock by specifying LK_REENABLE.
    416 	 */
    417 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    418 #ifdef DIAGNOSTIC /* { */
    419 		if (lkp->lk_flags & LK_DRAINED)
    420 			panic("lockmgr: using decommissioned lock");
    421 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    422 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    423 			panic("lockmgr: non-release on draining lock: %d\n",
    424 			    flags & LK_TYPE_MASK);
    425 #endif /* DIAGNOSTIC */ /* } */
    426 		lkp->lk_flags &= ~LK_DRAINING;
    427 		if ((flags & LK_REENABLE) == 0)
    428 			lkp->lk_flags |= LK_DRAINED;
    429 	}
    430 
    431 	switch (flags & LK_TYPE_MASK) {
    432 
    433 	case LK_SHARED:
    434 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    435 			/*
    436 			 * If just polling, check to see if we will block.
    437 			 */
    438 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    439 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    440 				error = EBUSY;
    441 				break;
    442 			}
    443 			/*
    444 			 * Wait for exclusive locks and upgrades to clear.
    445 			 */
    446 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    447 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    448 			if (error)
    449 				break;
    450 			lkp->lk_sharecount++;
    451 			COUNT(lkp, p, cpu_id, 1);
    452 			break;
    453 		}
    454 		/*
    455 		 * We hold an exclusive lock, so downgrade it to shared.
    456 		 * An alternative would be to fail with EDEADLK.
    457 		 */
    458 		lkp->lk_sharecount++;
    459 		COUNT(lkp, p, cpu_id, 1);
    460 		/* fall into downgrade */
    461 
    462 	case LK_DOWNGRADE:
    463 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    464 		    lkp->lk_exclusivecount == 0)
    465 			panic("lockmgr: not holding exclusive lock");
    466 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    467 		lkp->lk_exclusivecount = 0;
    468 		lkp->lk_recurselevel = 0;
    469 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    470 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    471 		DONTHAVEIT(lkp);
    472 		WAKEUP_WAITER(lkp);
    473 		break;
    474 
    475 	case LK_EXCLUPGRADE:
    476 		/*
    477 		 * If another process is ahead of us to get an upgrade,
    478 		 * then we want to fail rather than have an intervening
    479 		 * exclusive access.
    480 		 */
    481 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    482 			lkp->lk_sharecount--;
    483 			COUNT(lkp, p, cpu_id, -1);
    484 			error = EBUSY;
    485 			break;
    486 		}
    487 		/* fall into normal upgrade */
    488 
    489 	case LK_UPGRADE:
    490 		/*
    491 		 * Upgrade a shared lock to an exclusive one. If another
    492 		 * shared lock has already requested an upgrade to an
    493 		 * exclusive lock, our shared lock is released and an
    494 		 * exclusive lock is requested (which will be granted
    495 		 * after the upgrade). If we return an error, the file
    496 		 * will always be unlocked.
    497 		 */
    498 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    499 			panic("lockmgr: upgrade exclusive lock");
    500 		lkp->lk_sharecount--;
    501 		COUNT(lkp, p, cpu_id, -1);
    502 		/*
    503 		 * If we are just polling, check to see if we will block.
    504 		 */
    505 		if ((extflags & LK_NOWAIT) &&
    506 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    507 		     lkp->lk_sharecount > 1)) {
    508 			error = EBUSY;
    509 			break;
    510 		}
    511 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    512 			/*
    513 			 * We are first shared lock to request an upgrade, so
    514 			 * request upgrade and wait for the shared count to
    515 			 * drop to zero, then take exclusive lock.
    516 			 */
    517 			lkp->lk_flags |= LK_WANT_UPGRADE;
    518 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    519 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    520 			if (error)
    521 				break;
    522 			lkp->lk_flags |= LK_HAVE_EXCL;
    523 			SETHOLDER(lkp, pid, cpu_id);
    524 			HAVEIT(lkp);
    525 			if (lkp->lk_exclusivecount != 0)
    526 				panic("lockmgr: non-zero exclusive count");
    527 			lkp->lk_exclusivecount = 1;
    528 			if (extflags & LK_SETRECURSE)
    529 				lkp->lk_recurselevel = 1;
    530 			COUNT(lkp, p, cpu_id, 1);
    531 			break;
    532 		}
    533 		/*
    534 		 * Someone else has requested upgrade. Release our shared
    535 		 * lock, awaken upgrade requestor if we are the last shared
    536 		 * lock, then request an exclusive lock.
    537 		 */
    538 		if (lkp->lk_sharecount == 0)
    539 			WAKEUP_WAITER(lkp);
    540 		/* fall into exclusive request */
    541 
    542 	case LK_EXCLUSIVE:
    543 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    544 			/*
    545 			 * Recursive lock.
    546 			 */
    547 			if ((extflags & LK_CANRECURSE) == 0 &&
    548 			     lkp->lk_recurselevel == 0) {
    549 				if (extflags & LK_RECURSEFAIL) {
    550 					error = EDEADLK;
    551 					break;
    552 				} else
    553 					panic("lockmgr: locking against myself");
    554 			}
    555 			lkp->lk_exclusivecount++;
    556 			if (extflags & LK_SETRECURSE &&
    557 			    lkp->lk_recurselevel == 0)
    558 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    559 			COUNT(lkp, p, cpu_id, 1);
    560 			break;
    561 		}
    562 		/*
    563 		 * If we are just polling, check to see if we will sleep.
    564 		 */
    565 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    566 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    567 		     lkp->lk_sharecount != 0)) {
    568 			error = EBUSY;
    569 			break;
    570 		}
    571 		/*
    572 		 * Try to acquire the want_exclusive flag.
    573 		 */
    574 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    575 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    576 		if (error)
    577 			break;
    578 		lkp->lk_flags |= LK_WANT_EXCL;
    579 		/*
    580 		 * Wait for shared locks and upgrades to finish.
    581 		 */
    582 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    583 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    584 		lkp->lk_flags &= ~LK_WANT_EXCL;
    585 		if (error)
    586 			break;
    587 		lkp->lk_flags |= LK_HAVE_EXCL;
    588 		SETHOLDER(lkp, pid, cpu_id);
    589 		HAVEIT(lkp);
    590 		if (lkp->lk_exclusivecount != 0)
    591 			panic("lockmgr: non-zero exclusive count");
    592 		lkp->lk_exclusivecount = 1;
    593 		if (extflags & LK_SETRECURSE)
    594 			lkp->lk_recurselevel = 1;
    595 		COUNT(lkp, p, cpu_id, 1);
    596 		break;
    597 
    598 	case LK_RELEASE:
    599 		if (lkp->lk_exclusivecount != 0) {
    600 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    601 				if (lkp->lk_flags & LK_SPIN) {
    602 					panic("lockmgr: processor %lu, not "
    603 					    "exclusive lock holder %lu "
    604 					    "unlocking", cpu_id, lkp->lk_cpu);
    605 				} else {
    606 					panic("lockmgr: pid %d, not "
    607 					    "exclusive lock holder %d "
    608 					    "unlocking", pid,
    609 					    lkp->lk_lockholder);
    610 				}
    611 			}
    612 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    613 				lkp->lk_recurselevel = 0;
    614 			lkp->lk_exclusivecount--;
    615 			COUNT(lkp, p, cpu_id, -1);
    616 			if (lkp->lk_exclusivecount == 0) {
    617 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    618 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    619 				DONTHAVEIT(lkp);
    620 			}
    621 		} else if (lkp->lk_sharecount != 0) {
    622 			lkp->lk_sharecount--;
    623 			COUNT(lkp, p, cpu_id, -1);
    624 		}
    625 		WAKEUP_WAITER(lkp);
    626 		break;
    627 
    628 	case LK_DRAIN:
    629 		/*
    630 		 * Check that we do not already hold the lock, as it can
    631 		 * never drain if we do. Unfortunately, we have no way to
    632 		 * check for holding a shared lock, but at least we can
    633 		 * check for an exclusive one.
    634 		 */
    635 		if (WEHOLDIT(lkp, pid, cpu_id))
    636 			panic("lockmgr: draining against myself");
    637 		/*
    638 		 * If we are just polling, check to see if we will sleep.
    639 		 */
    640 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    641 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    642 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    643 			error = EBUSY;
    644 			break;
    645 		}
    646 		ACQUIRE(lkp, error, extflags, 1,
    647 		    ((lkp->lk_flags &
    648 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    649 		     lkp->lk_sharecount != 0 ||
    650 		     lkp->lk_waitcount != 0));
    651 		if (error)
    652 			break;
    653 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    654 		SETHOLDER(lkp, pid, cpu_id);
    655 		HAVEIT(lkp);
    656 		lkp->lk_exclusivecount = 1;
    657 		/* XXX unlikely that we'd want this */
    658 		if (extflags & LK_SETRECURSE)
    659 			lkp->lk_recurselevel = 1;
    660 		COUNT(lkp, p, cpu_id, 1);
    661 		break;
    662 
    663 	default:
    664 		simple_unlock(&lkp->lk_interlock);
    665 		panic("lockmgr: unknown locktype request %d",
    666 		    flags & LK_TYPE_MASK);
    667 		/* NOTREACHED */
    668 	}
    669 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    670 	    ((lkp->lk_flags &
    671 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    672 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    673 		lkp->lk_flags &= ~LK_WAITDRAIN;
    674 		wakeup_one((void *)&lkp->lk_flags);
    675 	}
    676 	/*
    677 	 * Note that this panic will be a recursive panic, since
    678 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    679 	 */
    680 	if (error && lock_shutdown_noblock)
    681 		panic("lockmgr: deadlock (see previous panic)");
    682 
    683 	simple_unlock(&lkp->lk_interlock);
    684 	return (error);
    685 }
    686 
    687 /*
    688  * Print out information about state of a lock. Used by VOP_PRINT
    689  * routines to display ststus about contained locks.
    690  */
    691 void
    692 lockmgr_printinfo(__volatile struct lock *lkp)
    693 {
    694 
    695 	if (lkp->lk_sharecount)
    696 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    697 		    lkp->lk_sharecount);
    698 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    699 		printf(" lock type %s: EXCL (count %d) by ",
    700 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    701 		if (lkp->lk_flags & LK_SPIN)
    702 			printf("processor %lu", lkp->lk_cpu);
    703 		else
    704 			printf("pid %d", lkp->lk_lockholder);
    705 	} else
    706 		printf(" not locked");
    707 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    708 		printf(" with %d pending", lkp->lk_waitcount);
    709 }
    710 
    711 #if defined(LOCKDEBUG) /* { */
    712 TAILQ_HEAD(, simplelock) simplelock_list =
    713     TAILQ_HEAD_INITIALIZER(simplelock_list);
    714 
    715 #if defined(MULTIPROCESSOR) /* { */
    716 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    717 
    718 #define	SLOCK_LIST_LOCK()						\
    719 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
    720 
    721 #define	SLOCK_LIST_UNLOCK()						\
    722 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
    723 
    724 #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
    725 #define	SLOCK_COUNT(x)							\
    726 	atomic_add_ulong(&curcpu()->ci_simple_locks, (x))
    727 #else
    728 #define	SLOCK_COUNT(x)		/* not safe */
    729 #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
    730 #else
    731 u_long simple_locks;
    732 
    733 #define	SLOCK_LIST_LOCK()	/* nothing */
    734 
    735 #define	SLOCK_LIST_UNLOCK()	/* nothing */
    736 
    737 #define	SLOCK_COUNT(x)		simple_locks += (x)
    738 #endif /* MULTIPROCESSOR */ /* } */
    739 
    740 #ifdef DDB /* { */
    741 int simple_lock_debugger = 0;
    742 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    743 #else
    744 #define	SLOCK_DEBUGGER()	/* nothing */
    745 #endif /* } */
    746 
    747 #ifdef MULTIPROCESSOR
    748 #define SLOCK_MP()		lock_printf("on cpu %d\n", cpu_number())
    749 #else
    750 #define SLOCK_MP()		/* nothing */
    751 #endif
    752 
    753 #define	SLOCK_WHERE(str, alp, id, l)					\
    754 do {									\
    755 	lock_printf(str);						\
    756 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
    757 	SLOCK_MP();							\
    758 	if ((alp)->lock_file != NULL)					\
    759 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
    760 		    (alp)->lock_line);					\
    761 	if ((alp)->unlock_file != NULL)					\
    762 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
    763 		    (alp)->unlock_line);				\
    764 	SLOCK_DEBUGGER();						\
    765 } while (/*CONSTCOND*/0)
    766 
    767 /*
    768  * Simple lock functions so that the debugger can see from whence
    769  * they are being called.
    770  */
    771 void
    772 simple_lock_init(struct simplelock *alp)
    773 {
    774 
    775 #if defined(MULTIPROCESSOR) /* { */
    776 	__cpu_simple_lock_init(&alp->lock_data);
    777 #else
    778 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    779 #endif /* } */
    780 	alp->lock_file = NULL;
    781 	alp->lock_line = 0;
    782 	alp->unlock_file = NULL;
    783 	alp->unlock_line = 0;
    784 	alp->lock_holder = 0;
    785 }
    786 
    787 void
    788 _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
    789 {
    790 	cpuid_t cpu_id = cpu_number();
    791 	int s;
    792 
    793 	s = splhigh();
    794 
    795 	/*
    796 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    797 	 * don't take any action, and just fall into the normal spin case.
    798 	 */
    799 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    800 #if defined(MULTIPROCESSOR) /* { */
    801 		if (alp->lock_holder == cpu_id) {
    802 			SLOCK_WHERE("simple_lock: locking against myself\n",
    803 			    alp, id, l);
    804 			goto out;
    805 		}
    806 #else
    807 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    808 		goto out;
    809 #endif /* MULTIPROCESSOR */ /* } */
    810 	}
    811 
    812 #if defined(MULTIPROCESSOR) /* { */
    813 	/* Acquire the lock before modifying any fields. */
    814 	__cpu_simple_lock(&alp->lock_data);
    815 #else
    816 	alp->lock_data = __SIMPLELOCK_LOCKED;
    817 #endif /* } */
    818 
    819 	alp->lock_file = id;
    820 	alp->lock_line = l;
    821 	alp->lock_holder = cpu_id;
    822 
    823 	SLOCK_LIST_LOCK();
    824 	/* XXX Cast away volatile */
    825 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    826 	SLOCK_LIST_UNLOCK();
    827 
    828 	SLOCK_COUNT(1);
    829 
    830  out:
    831 	splx(s);
    832 }
    833 
    834 int
    835 _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
    836 {
    837 	cpuid_t cpu_id = cpu_number();
    838 	int s, rv = 0;
    839 
    840 	s = splhigh();
    841 
    842 	/*
    843 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    844 	 * don't take any action.
    845 	 */
    846 #if defined(MULTIPROCESSOR) /* { */
    847 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
    848 		if (alp->lock_holder == cpu_id)
    849 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
    850 			    alp, id, l);
    851 		goto out;
    852 	}
    853 #else
    854 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    855 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
    856 		goto out;
    857 	}
    858 	alp->lock_data = __SIMPLELOCK_LOCKED;
    859 #endif /* MULTIPROCESSOR */ /* } */
    860 
    861 	/*
    862 	 * At this point, we have acquired the lock.
    863 	 */
    864 
    865 	rv = 1;
    866 
    867 	alp->lock_file = id;
    868 	alp->lock_line = l;
    869 	alp->lock_holder = cpu_id;
    870 
    871 	SLOCK_LIST_LOCK();
    872 	/* XXX Cast away volatile. */
    873 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    874 	SLOCK_LIST_UNLOCK();
    875 
    876 	SLOCK_COUNT(1);
    877 
    878  out:
    879 	splx(s);
    880 	return (rv);
    881 }
    882 
    883 void
    884 _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
    885 {
    886 	int s;
    887 
    888 	s = splhigh();
    889 
    890 	/*
    891 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
    892 	 * the lock, and if we don't, we don't take any action.
    893 	 */
    894 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
    895 		SLOCK_WHERE("simple_unlock: lock not held\n",
    896 		    alp, id, l);
    897 		goto out;
    898 	}
    899 
    900 	SLOCK_LIST_LOCK();
    901 	TAILQ_REMOVE(&simplelock_list, alp, list);
    902 	SLOCK_LIST_UNLOCK();
    903 
    904 	SLOCK_COUNT(-1);
    905 
    906 	alp->list.tqe_next = NULL;	/* sanity */
    907 	alp->list.tqe_prev = NULL;	/* sanity */
    908 
    909 	alp->unlock_file = id;
    910 	alp->unlock_line = l;
    911 
    912 #if defined(MULTIPROCESSOR) /* { */
    913 	alp->lock_holder = LK_NOCPU;
    914 	/* Now that we've modified all fields, release the lock. */
    915 	__cpu_simple_unlock(&alp->lock_data);
    916 #else
    917 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    918 #endif /* } */
    919 
    920  out:
    921 	splx(s);
    922 }
    923 
    924 void
    925 simple_lock_dump(void)
    926 {
    927 	struct simplelock *alp;
    928 	int s;
    929 
    930 	s = splhigh();
    931 	SLOCK_LIST_LOCK();
    932 	lock_printf("all simple locks:\n");
    933 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    934 	     alp = TAILQ_NEXT(alp, list)) {
    935 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
    936 		    alp->lock_file, alp->lock_line);
    937 	}
    938 	SLOCK_LIST_UNLOCK();
    939 	splx(s);
    940 }
    941 
    942 void
    943 simple_lock_freecheck(void *start, void *end)
    944 {
    945 	struct simplelock *alp;
    946 	int s;
    947 
    948 	s = splhigh();
    949 	SLOCK_LIST_LOCK();
    950 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    951 	     alp = TAILQ_NEXT(alp, list)) {
    952 		if ((void *)alp >= start && (void *)alp < end) {
    953 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
    954 			    alp, alp->lock_holder, alp->lock_file,
    955 			    alp->lock_line);
    956 			SLOCK_DEBUGGER();
    957 		}
    958 	}
    959 	SLOCK_LIST_UNLOCK();
    960 	splx(s);
    961 }
    962 
    963 void
    964 simple_lock_switchcheck(void)
    965 {
    966 	struct simplelock *alp;
    967 	cpuid_t cpu_id = cpu_number();
    968 	int s;
    969 
    970 	s = splhigh();
    971 	SLOCK_LIST_LOCK();
    972 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    973 	     alp = TAILQ_NEXT(alp, list)) {
    974 		if (alp->lock_holder == cpu_id) {
    975 			lock_printf("switching with held simple_lock %p "
    976 			    "CPU %lu %s:%s\n",
    977 			    alp, alp->lock_holder, alp->lock_file,
    978 			    alp->lock_line);
    979 			SLOCK_DEBUGGER();
    980 		}
    981 	}
    982 	SLOCK_LIST_UNLOCK();
    983 	splx(s);
    984 }
    985 #endif /* LOCKDEBUG */ /* } */
    986