Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.27
      1 /*	$NetBSD: kern_lock.c,v 1.27 2000/04/29 03:31:46 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 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 __P((const char *fmt, ...));
    102 
    103 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    104 #endif
    105 
    106 /*
    107  * Locking primitives implementation.
    108  * Locks provide shared/exclusive sychronization.
    109  */
    110 
    111 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    112 #if defined(MULTIPROCESSOR) /* { */
    113 #define	COUNT_CPU(cpu_id, x)						\
    114 	/* atomic_add_ulong(&curcpu().ci_spin_locks, (x)) */
    115 #else
    116 u_long	spin_locks;
    117 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    118 #endif /* MULTIPROCESSOR */ /* } */
    119 
    120 #define	COUNT(lkp, p, cpu_id, x)					\
    121 do {									\
    122 	if ((lkp)->lk_flags & LK_SPIN)					\
    123 		COUNT_CPU((cpu_id), (x));				\
    124 	else								\
    125 		(p)->p_locks += (x);					\
    126 } while (0)
    127 #else
    128 #define COUNT(lkp, p, cpu_id, x)
    129 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    130 
    131 /*
    132  * Acquire a resource.
    133  */
    134 #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
    135 	if ((extflags) & LK_SPIN) {					\
    136 		int interlocked;					\
    137 									\
    138 		if ((drain) == 0)					\
    139 			(lkp)->lk_waitcount++;				\
    140 		for (interlocked = 1;;) {				\
    141 			if (wanted) {					\
    142 				if (interlocked) {			\
    143 					simple_unlock(&(lkp)->lk_interlock); \
    144 					interlocked = 0;		\
    145 				}					\
    146 			} else if (interlocked) {			\
    147 				break;					\
    148 			} else {					\
    149 				simple_lock(&(lkp)->lk_interlock);	\
    150 				interlocked = 1;			\
    151 			}						\
    152 		}							\
    153 		if ((drain) == 0)					\
    154 			(lkp)->lk_waitcount--;				\
    155 		KASSERT((wanted) == 0);					\
    156 		error = 0;	/* sanity */				\
    157 	} else {							\
    158 		for (error = 0; wanted; ) {				\
    159 			if ((drain))					\
    160 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
    161 			else						\
    162 				(lkp)->lk_waitcount++;			\
    163 			simple_unlock(&(lkp)->lk_interlock);		\
    164 			/* XXX Cast away volatile. */			\
    165 			error = tsleep((drain) ? &(lkp)->lk_flags :	\
    166 			    (void *)(lkp), (lkp)->lk_prio,		\
    167 			    (lkp)->lk_wmesg, (lkp)->lk_timo);		\
    168 			simple_lock(&(lkp)->lk_interlock);		\
    169 			if ((drain) == 0)				\
    170 				(lkp)->lk_waitcount--;			\
    171 			if (error)					\
    172 				break;					\
    173 			if ((extflags) & LK_SLEEPFAIL) {		\
    174 				error = ENOLCK;				\
    175 				break;					\
    176 			}						\
    177 		}							\
    178 	}
    179 
    180 #define	SETHOLDER(lkp, pid, cpu_id)					\
    181 do {									\
    182 	if ((lkp)->lk_flags & LK_SPIN)					\
    183 		(lkp)->lk_cpu = cpu_id;					\
    184 	else								\
    185 		(lkp)->lk_lockholder = pid;				\
    186 } while (0)
    187 
    188 #define	WEHOLDIT(lkp, pid, cpu_id)					\
    189 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    190 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    191 
    192 #define	WAKEUP_WAITER(lkp)						\
    193 do {									\
    194 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
    195 		/* XXX Cast away volatile. */				\
    196 		wakeup_one((void *)(lkp));				\
    197 	}								\
    198 } while (0)
    199 
    200 #if defined(LOCKDEBUG) /* { */
    201 #if defined(MULTIPROCESSOR) /* { */
    202 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    203 
    204 #define	SPINLOCK_LIST_LOCK()						\
    205 	__cpu_simple_lock(&spinlock_list_slock->lock_data)
    206 
    207 #define	SPINLOCK_LIST_UNLOCK()						\
    208 	__cpu_simple_unlock(&spinlock_list_slock->lock_data)
    209 #else
    210 #define	SPINLOCK_LIST_LOCK()	/* nothing */
    211 
    212 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    213 #endif /* MULTIPROCESSOR */ /* } */
    214 
    215 TAILQ_HEAD(, lock) spinlock_list =
    216     TAILQ_HEAD_INITIALIZER(spinlock_list);
    217 
    218 #define	HAVEIT(lkp)							\
    219 do {									\
    220 	if ((lkp)->lk_flags & LK_SPIN) {				\
    221 		int s = splhigh();					\
    222 		SPINLOCK_LIST_LOCK();					\
    223 		/* XXX Cast away volatile. */				\
    224 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    225 		    lk_list);						\
    226 		SPINLOCK_LIST_UNLOCK();					\
    227 		splx(s);						\
    228 	}								\
    229 } while (0)
    230 
    231 #define	DONTHAVEIT(lkp)							\
    232 do {									\
    233 	if ((lkp)->lk_flags & LK_SPIN) {				\
    234 		int s = splhigh();					\
    235 		SPINLOCK_LIST_LOCK();					\
    236 		/* XXX Cast away volatile. */				\
    237 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    238 		    lk_list);						\
    239 		SPINLOCK_LIST_UNLOCK();					\
    240 		splx(s);						\
    241 	}								\
    242 } while (0)
    243 #else
    244 #define	HAVEIT(lkp)		/* nothing */
    245 
    246 #define	DONTHAVEIT(lkp)		/* nothing */
    247 #endif /* LOCKDEBUG */ /* } */
    248 
    249 #if defined(LOCKDEBUG)
    250 /*
    251  * Lock debug printing routine; can be configured to print to console
    252  * or log to syslog.
    253  */
    254 void
    255 #ifdef __STDC__
    256 lock_printf(const char *fmt, ...)
    257 #else
    258 lock_printf(fmt, va_alist)
    259 	char *fmt;
    260 	va_dcl
    261 #endif
    262 {
    263 	va_list ap;
    264 
    265 	va_start(ap, fmt);
    266 	if (lock_debug_syslog)
    267 		vlog(LOG_DEBUG, fmt, ap);
    268 	else
    269 		vprintf(fmt, ap);
    270 	va_end(ap);
    271 }
    272 #endif /* LOCKDEBUG */
    273 
    274 /*
    275  * Initialize a lock; required before use.
    276  */
    277 void
    278 lockinit(lkp, prio, wmesg, timo, flags)
    279 	struct lock *lkp;
    280 	int prio;
    281 	const char *wmesg;
    282 	int timo;
    283 	int flags;
    284 {
    285 
    286 	memset(lkp, 0, sizeof(struct lock));
    287 	simple_lock_init(&lkp->lk_interlock);
    288 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    289 	if (flags & LK_SPIN)
    290 		lkp->lk_cpu = LK_NOCPU;
    291 	else {
    292 		lkp->lk_lockholder = LK_NOPROC;
    293 		lkp->lk_prio = prio;
    294 		lkp->lk_timo = timo;
    295 	}
    296 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    297 }
    298 
    299 /*
    300  * Determine the status of a lock.
    301  */
    302 int
    303 lockstatus(lkp)
    304 	struct lock *lkp;
    305 {
    306 	int lock_type = 0;
    307 
    308 	simple_lock(&lkp->lk_interlock);
    309 	if (lkp->lk_exclusivecount != 0)
    310 		lock_type = LK_EXCLUSIVE;
    311 	else if (lkp->lk_sharecount != 0)
    312 		lock_type = LK_SHARED;
    313 	simple_unlock(&lkp->lk_interlock);
    314 	return (lock_type);
    315 }
    316 
    317 /*
    318  * Set, change, or release a lock.
    319  *
    320  * Shared requests increment the shared count. Exclusive requests set the
    321  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    322  * accepted shared locks and shared-to-exclusive upgrades to go away.
    323  */
    324 int
    325 lockmgr(lkp, flags, interlkp)
    326 	__volatile struct lock *lkp;
    327 	u_int flags;
    328 	struct simplelock *interlkp;
    329 {
    330 	int error;
    331 	pid_t pid;
    332 	int extflags;
    333 	cpuid_t cpu_id;
    334 	struct proc *p = curproc;
    335 
    336 	error = 0;
    337 
    338 	simple_lock(&lkp->lk_interlock);
    339 	if (flags & LK_INTERLOCK)
    340 		simple_unlock(interlkp);
    341 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    342 
    343 #ifdef DIAGNOSTIC /* { */
    344 	/*
    345 	 * Don't allow spins on sleep locks and don't allow sleeps
    346 	 * on spin locks.
    347 	 */
    348 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    349 		panic("lockmgr: sleep/spin mismatch\n");
    350 #endif /* } */
    351 
    352 	if (extflags & LK_SPIN)
    353 		pid = LK_KERNPROC;
    354 	else {
    355 #ifdef DIAGNOSTIC /* { */
    356 		if (p == NULL)
    357 			panic("lockmgr: no context");
    358 #endif /* } */
    359 		pid = p->p_pid;
    360 	}
    361 	cpu_id = cpu_number();
    362 
    363 #ifdef DIAGNOSTIC /* { */
    364 	/*
    365 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    366 	 * exclusive lock is returned. The only valid operation thereafter
    367 	 * is a single release of that exclusive lock. This final release
    368 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    369 	 * further requests of any sort will result in a panic. The bits
    370 	 * selected for these two flags are chosen so that they will be set
    371 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    372 	 * The final release is permitted to give a new lease on life to
    373 	 * the lock by specifying LK_REENABLE.
    374 	 */
    375 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    376 		if (lkp->lk_flags & LK_DRAINED)
    377 			panic("lockmgr: using decommissioned lock");
    378 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    379 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    380 			panic("lockmgr: non-release on draining lock: %d\n",
    381 			    flags & LK_TYPE_MASK);
    382 		lkp->lk_flags &= ~LK_DRAINING;
    383 		if ((flags & LK_REENABLE) == 0)
    384 			lkp->lk_flags |= LK_DRAINED;
    385 	}
    386 #endif /* DIAGNOSTIC */ /* } */
    387 
    388 	switch (flags & LK_TYPE_MASK) {
    389 
    390 	case LK_SHARED:
    391 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    392 			/*
    393 			 * If just polling, check to see if we will block.
    394 			 */
    395 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    396 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    397 				error = EBUSY;
    398 				break;
    399 			}
    400 			/*
    401 			 * Wait for exclusive locks and upgrades to clear.
    402 			 */
    403 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    404 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    405 			if (error)
    406 				break;
    407 			lkp->lk_sharecount++;
    408 			COUNT(lkp, p, cpu_id, 1);
    409 			break;
    410 		}
    411 		/*
    412 		 * We hold an exclusive lock, so downgrade it to shared.
    413 		 * An alternative would be to fail with EDEADLK.
    414 		 */
    415 		lkp->lk_sharecount++;
    416 		COUNT(lkp, p, cpu_id, 1);
    417 		/* fall into downgrade */
    418 
    419 	case LK_DOWNGRADE:
    420 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    421 		    lkp->lk_exclusivecount == 0)
    422 			panic("lockmgr: not holding exclusive lock");
    423 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    424 		lkp->lk_exclusivecount = 0;
    425 		lkp->lk_recurselevel = 0;
    426 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    427 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    428 		DONTHAVEIT(lkp);
    429 		WAKEUP_WAITER(lkp);
    430 		break;
    431 
    432 	case LK_EXCLUPGRADE:
    433 		/*
    434 		 * If another process is ahead of us to get an upgrade,
    435 		 * then we want to fail rather than have an intervening
    436 		 * exclusive access.
    437 		 */
    438 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    439 			lkp->lk_sharecount--;
    440 			COUNT(lkp, p, cpu_id, -1);
    441 			error = EBUSY;
    442 			break;
    443 		}
    444 		/* fall into normal upgrade */
    445 
    446 	case LK_UPGRADE:
    447 		/*
    448 		 * Upgrade a shared lock to an exclusive one. If another
    449 		 * shared lock has already requested an upgrade to an
    450 		 * exclusive lock, our shared lock is released and an
    451 		 * exclusive lock is requested (which will be granted
    452 		 * after the upgrade). If we return an error, the file
    453 		 * will always be unlocked.
    454 		 */
    455 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    456 			panic("lockmgr: upgrade exclusive lock");
    457 		lkp->lk_sharecount--;
    458 		COUNT(lkp, p, cpu_id, -1);
    459 		/*
    460 		 * If we are just polling, check to see if we will block.
    461 		 */
    462 		if ((extflags & LK_NOWAIT) &&
    463 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    464 		     lkp->lk_sharecount > 1)) {
    465 			error = EBUSY;
    466 			break;
    467 		}
    468 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    469 			/*
    470 			 * We are first shared lock to request an upgrade, so
    471 			 * request upgrade and wait for the shared count to
    472 			 * drop to zero, then take exclusive lock.
    473 			 */
    474 			lkp->lk_flags |= LK_WANT_UPGRADE;
    475 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    476 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    477 			if (error)
    478 				break;
    479 			lkp->lk_flags |= LK_HAVE_EXCL;
    480 			SETHOLDER(lkp, pid, cpu_id);
    481 			HAVEIT(lkp);
    482 			if (lkp->lk_exclusivecount != 0)
    483 				panic("lockmgr: non-zero exclusive count");
    484 			lkp->lk_exclusivecount = 1;
    485 			if (extflags & LK_SETRECURSE)
    486 				lkp->lk_recurselevel = 1;
    487 			COUNT(lkp, p, cpu_id, 1);
    488 			break;
    489 		}
    490 		/*
    491 		 * Someone else has requested upgrade. Release our shared
    492 		 * lock, awaken upgrade requestor if we are the last shared
    493 		 * lock, then request an exclusive lock.
    494 		 */
    495 		if (lkp->lk_sharecount == 0)
    496 			WAKEUP_WAITER(lkp);
    497 		/* fall into exclusive request */
    498 
    499 	case LK_EXCLUSIVE:
    500 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    501 			/*
    502 			 * Recursive lock.
    503 			 */
    504 			if ((extflags & LK_CANRECURSE) == 0 &&
    505 			     lkp->lk_recurselevel == 0) {
    506 				if (extflags & LK_RECURSEFAIL) {
    507 					error = EDEADLK;
    508 					break;
    509 				} else
    510 					panic("lockmgr: locking against myself");
    511 			}
    512 			lkp->lk_exclusivecount++;
    513 			if (extflags & LK_SETRECURSE &&
    514 			    lkp->lk_recurselevel == 0)
    515 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    516 			COUNT(lkp, p, cpu_id, 1);
    517 			break;
    518 		}
    519 		/*
    520 		 * If we are just polling, check to see if we will sleep.
    521 		 */
    522 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    523 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    524 		     lkp->lk_sharecount != 0)) {
    525 			error = EBUSY;
    526 			break;
    527 		}
    528 		/*
    529 		 * Try to acquire the want_exclusive flag.
    530 		 */
    531 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    532 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    533 		if (error)
    534 			break;
    535 		lkp->lk_flags |= LK_WANT_EXCL;
    536 		/*
    537 		 * Wait for shared locks and upgrades to finish.
    538 		 */
    539 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    540 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    541 		lkp->lk_flags &= ~LK_WANT_EXCL;
    542 		if (error)
    543 			break;
    544 		lkp->lk_flags |= LK_HAVE_EXCL;
    545 		SETHOLDER(lkp, pid, cpu_id);
    546 		HAVEIT(lkp);
    547 		if (lkp->lk_exclusivecount != 0)
    548 			panic("lockmgr: non-zero exclusive count");
    549 		lkp->lk_exclusivecount = 1;
    550 		if (extflags & LK_SETRECURSE)
    551 			lkp->lk_recurselevel = 1;
    552 		COUNT(lkp, p, cpu_id, 1);
    553 		break;
    554 
    555 	case LK_RELEASE:
    556 		if (lkp->lk_exclusivecount != 0) {
    557 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    558 				if (lkp->lk_flags & LK_SPIN) {
    559 					panic("lockmgr: processor %lu, not "
    560 					    "exclusive lock holder %lu "
    561 					    "unlocking", cpu_id, lkp->lk_cpu);
    562 				} else {
    563 					panic("lockmgr: pid %d, not "
    564 					    "exclusive lock holder %d "
    565 					    "unlocking", pid,
    566 					    lkp->lk_lockholder);
    567 				}
    568 			}
    569 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    570 				lkp->lk_recurselevel = 0;
    571 			lkp->lk_exclusivecount--;
    572 			COUNT(lkp, p, cpu_id, -1);
    573 			if (lkp->lk_exclusivecount == 0) {
    574 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    575 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    576 				DONTHAVEIT(lkp);
    577 			}
    578 		} else if (lkp->lk_sharecount != 0) {
    579 			lkp->lk_sharecount--;
    580 			COUNT(lkp, p, cpu_id, -1);
    581 		}
    582 		WAKEUP_WAITER(lkp);
    583 		break;
    584 
    585 	case LK_DRAIN:
    586 		/*
    587 		 * Check that we do not already hold the lock, as it can
    588 		 * never drain if we do. Unfortunately, we have no way to
    589 		 * check for holding a shared lock, but at least we can
    590 		 * check for an exclusive one.
    591 		 */
    592 		if (WEHOLDIT(lkp, pid, cpu_id))
    593 			panic("lockmgr: draining against myself");
    594 		/*
    595 		 * If we are just polling, check to see if we will sleep.
    596 		 */
    597 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    598 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    599 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    600 			error = EBUSY;
    601 			break;
    602 		}
    603 		ACQUIRE(lkp, error, extflags, 1,
    604 		    ((lkp->lk_flags &
    605 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    606 		     lkp->lk_sharecount != 0 ||
    607 		     lkp->lk_waitcount != 0));
    608 		if (error)
    609 			break;
    610 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    611 		SETHOLDER(lkp, pid, cpu_id);
    612 		HAVEIT(lkp);
    613 		lkp->lk_exclusivecount = 1;
    614 		/* XXX unlikely that we'd want this */
    615 		if (extflags & LK_SETRECURSE)
    616 			lkp->lk_recurselevel = 1;
    617 		COUNT(lkp, p, cpu_id, 1);
    618 		break;
    619 
    620 	default:
    621 		simple_unlock(&lkp->lk_interlock);
    622 		panic("lockmgr: unknown locktype request %d",
    623 		    flags & LK_TYPE_MASK);
    624 		/* NOTREACHED */
    625 	}
    626 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    627 	    ((lkp->lk_flags &
    628 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    629 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    630 		lkp->lk_flags &= ~LK_WAITDRAIN;
    631 		wakeup_one((void *)&lkp->lk_flags);
    632 	}
    633 	simple_unlock(&lkp->lk_interlock);
    634 	return (error);
    635 }
    636 
    637 /*
    638  * Print out information about state of a lock. Used by VOP_PRINT
    639  * routines to display ststus about contained locks.
    640  */
    641 void
    642 lockmgr_printinfo(lkp)
    643 	__volatile struct lock *lkp;
    644 {
    645 
    646 	if (lkp->lk_sharecount)
    647 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    648 		    lkp->lk_sharecount);
    649 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    650 		printf(" lock type %s: EXCL (count %d) by ",
    651 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    652 		if (lkp->lk_flags & LK_SPIN)
    653 			printf("processor %lu", lkp->lk_cpu);
    654 		else
    655 			printf("pid %d", lkp->lk_lockholder);
    656 	} else
    657 		printf(" not locked");
    658 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    659 		printf(" with %d pending", lkp->lk_waitcount);
    660 }
    661 
    662 #if defined(LOCKDEBUG) /* { */
    663 TAILQ_HEAD(, simplelock) simplelock_list =
    664     TAILQ_HEAD_INITIALIZER(simplelock_list);
    665 
    666 #if defined(MULTIPROCESSOR) /* { */
    667 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    668 
    669 #define	SLOCK_LIST_LOCK()						\
    670 	__cpu_simple_lock(&simplelock_list_slock->lock_data)
    671 
    672 #define	SLOCK_LIST_UNLOCK()						\
    673 	__cpu_simple_unlock(&simplelock_list_slock->lock_data)
    674 
    675 #define	SLOCK_COUNT(x)							\
    676 	/* atomic_add_ulong(&curcpu()->ci_simple_locks, (x)) */
    677 #else
    678 u_long simple_locks;
    679 
    680 #define	SLOCK_LIST_LOCK()	/* nothing */
    681 
    682 #define	SLOCK_LIST_UNLOCK()	/* nothing */
    683 
    684 #define	SLOCK_COUNT(x)		simple_locks += (x)
    685 #endif /* MULTIPROCESSOR */ /* } */
    686 
    687 #ifdef DDB /* { */
    688 int simple_lock_debugger = 0;
    689 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    690 #else
    691 #define	SLOCK_DEBUGGER()	/* nothing */
    692 #endif /* } */
    693 
    694 #ifdef MULTIPROCESSOR
    695 #define SLOCK_MP()		lock_printf("on cpu %d\n", cpu_number())
    696 #else
    697 #define SLOCK_MP()		/* nothing */
    698 #endif
    699 
    700 #define	SLOCK_WHERE(str, alp, id, l)					\
    701 do {									\
    702 	lock_printf(str);						\
    703 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l));		\
    704 	SLOCK_MP();							\
    705 	if ((alp)->lock_file != NULL)					\
    706 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
    707 		    (alp)->lock_line);					\
    708 	if ((alp)->unlock_file != NULL)					\
    709 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
    710 		    (alp)->unlock_line);				\
    711 	SLOCK_DEBUGGER();						\
    712 } while (0)
    713 
    714 /*
    715  * Simple lock functions so that the debugger can see from whence
    716  * they are being called.
    717  */
    718 void
    719 simple_lock_init(alp)
    720 	struct simplelock *alp;
    721 {
    722 
    723 #if defined(MULTIPROCESSOR) /* { */
    724 	__cpu_simple_lock_init(&alp->lock_data);
    725 #else
    726 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    727 #endif /* } */
    728 	alp->lock_file = NULL;
    729 	alp->lock_line = 0;
    730 	alp->unlock_file = NULL;
    731 	alp->unlock_line = 0;
    732 	alp->lock_holder = 0;
    733 }
    734 
    735 void
    736 _simple_lock(alp, id, l)
    737 	__volatile struct simplelock *alp;
    738 	const char *id;
    739 	int l;
    740 {
    741 	cpuid_t cpu_id = cpu_number();
    742 	int s;
    743 
    744 	s = splhigh();
    745 
    746 	/*
    747 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    748 	 * don't take any action, and just fall into the normal spin case.
    749 	 */
    750 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    751 #if defined(MULTIPROCESSOR) /* { */
    752 		if (alp->lock_holder == cpu_id) {
    753 			SLOCK_WHERE("simple_lock: locking against myself\n",
    754 			    alp, id, l);
    755 			goto out;
    756 		}
    757 #else
    758 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    759 		goto out;
    760 #endif /* MULTIPROCESSOR */ /* } */
    761 	}
    762 
    763 #if defined(MULTIPROCESSOR) /* { */
    764 	/* Acquire the lock before modifying any fields. */
    765 	__cpu_simple_lock(&alp->lock_data);
    766 #else
    767 	alp->lock_data = __SIMPLELOCK_LOCKED;
    768 #endif /* } */
    769 
    770 	alp->lock_file = id;
    771 	alp->lock_line = l;
    772 	alp->lock_holder = cpu_id;
    773 
    774 	SLOCK_LIST_LOCK();
    775 	/* XXX Cast away volatile */
    776 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    777 	SLOCK_LIST_UNLOCK();
    778 
    779 	SLOCK_COUNT(1);
    780 
    781  out:
    782 	splx(s);
    783 }
    784 
    785 int
    786 _simple_lock_try(alp, id, l)
    787 	__volatile struct simplelock *alp;
    788 	const char *id;
    789 	int l;
    790 {
    791 	cpuid_t cpu_id = cpu_number();
    792 	int s, rv = 0;
    793 
    794 	s = splhigh();
    795 
    796 	/*
    797 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    798 	 * don't take any action.
    799 	 */
    800 #if defined(MULTIPROCESSOR) /* { */
    801 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
    802 		if (alp->lock_holder == cpu_id)
    803 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
    804 			    alp, id, l);
    805 		goto out;
    806 	}
    807 #else
    808 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    809 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
    810 		goto out;
    811 	}
    812 	alp->lock_data = __SIMPLELOCK_LOCKED;
    813 #endif /* MULTIPROCESSOR */ /* } */
    814 
    815 	/*
    816 	 * At this point, we have acquired the lock.
    817 	 */
    818 
    819 	rv = 1;
    820 
    821 	alp->lock_file = id;
    822 	alp->lock_line = l;
    823 	alp->lock_holder = cpu_id;
    824 
    825 	SLOCK_LIST_LOCK();
    826 	/* XXX Cast away volatile. */
    827 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    828 	SLOCK_LIST_UNLOCK();
    829 
    830 	SLOCK_COUNT(1);
    831 
    832  out:
    833 	splx(s);
    834 	return (rv);
    835 }
    836 
    837 void
    838 _simple_unlock(alp, id, l)
    839 	__volatile struct simplelock *alp;
    840 	const char *id;
    841 	int l;
    842 {
    843 	int s;
    844 
    845 	s = splhigh();
    846 
    847 	/*
    848 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
    849 	 * the lock, and if we don't, we don't take any action.
    850 	 */
    851 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
    852 		SLOCK_WHERE("simple_unlock: lock not held\n",
    853 		    alp, id, l);
    854 		goto out;
    855 	}
    856 
    857 	SLOCK_LIST_LOCK();
    858 	TAILQ_REMOVE(&simplelock_list, alp, list);
    859 	SLOCK_LIST_UNLOCK();
    860 
    861 	SLOCK_COUNT(-1);
    862 
    863 	alp->list.tqe_next = NULL;	/* sanity */
    864 	alp->list.tqe_prev = NULL;	/* sanity */
    865 
    866 	alp->unlock_file = id;
    867 	alp->unlock_line = l;
    868 
    869 #if defined(MULTIPROCESSOR) /* { */
    870 	alp->lock_holder = LK_NOCPU;
    871 	/* Now that we've modified all fields, release the lock. */
    872 	__cpu_simple_unlock(&alp->lock_data);
    873 #else
    874 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    875 #endif /* } */
    876 
    877  out:
    878 	splx(s);
    879 }
    880 
    881 void
    882 simple_lock_dump()
    883 {
    884 	struct simplelock *alp;
    885 	int s;
    886 
    887 	s = splhigh();
    888 	SLOCK_LIST_LOCK();
    889 	lock_printf("all simple locks:\n");
    890 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    891 	     alp = TAILQ_NEXT(alp, list)) {
    892 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
    893 		    alp->lock_file, alp->lock_line);
    894 	}
    895 	SLOCK_LIST_UNLOCK();
    896 	splx(s);
    897 }
    898 
    899 void
    900 simple_lock_freecheck(start, end)
    901 	void *start, *end;
    902 {
    903 	struct simplelock *alp;
    904 	int s;
    905 
    906 	s = splhigh();
    907 	SLOCK_LIST_LOCK();
    908 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
    909 	     alp = TAILQ_NEXT(alp, list)) {
    910 		if ((void *)alp >= start && (void *)alp < end) {
    911 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
    912 			    alp, alp->lock_holder, alp->lock_file,
    913 			    alp->lock_line);
    914 			SLOCK_DEBUGGER();
    915 		}
    916 	}
    917 	SLOCK_LIST_UNLOCK();
    918 	splx(s);
    919 }
    920 #endif /* LOCKDEBUG */ /* } */
    921