Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.128
      1 /*	$NetBSD: kern_lock.c,v 1.128 2007/11/30 23:05:43 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000, 2006, 2007 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, and by Andrew Doran.
     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. Neither the name of the University nor the names of its contributors
     60  *    may be used to endorse or promote products derived from this software
     61  *    without specific prior written permission.
     62  *
     63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     73  * SUCH DAMAGE.
     74  *
     75  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     76  */
     77 
     78 #include <sys/cdefs.h>
     79 __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.128 2007/11/30 23:05:43 ad Exp $");
     80 
     81 #include "opt_multiprocessor.h"
     82 
     83 #include <sys/param.h>
     84 #include <sys/proc.h>
     85 #include <sys/lock.h>
     86 #include <sys/systm.h>
     87 #include <sys/kernel.h>
     88 #include <sys/lockdebug.h>
     89 #include <sys/cpu.h>
     90 #include <sys/syslog.h>
     91 #include <sys/atomic.h>
     92 
     93 #include <machine/stdarg.h>
     94 
     95 #include <dev/lockstat.h>
     96 
     97 /*
     98  * note that stdarg.h and the ansi style va_start macro is used for both
     99  * ansi and traditional c compiles.
    100  * XXX: this requires that stdarg.h define: va_alist and va_dcl
    101  */
    102 void	lock_printf(const char *fmt, ...)
    103     __attribute__((__format__(__printf__,1,2)));
    104 
    105 static int acquire(struct lock **, int *, int, int, int, uintptr_t);
    106 
    107 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    108 bool	kernel_lock_dodebug;
    109 __cpu_simple_lock_t kernel_lock;
    110 
    111 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    112 #define	COUNT(lkp, l, cpu_id, x)	(l)->l_locks += (x)
    113 #else
    114 #define COUNT(lkp, p, cpu_id, x)
    115 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    116 
    117 #define	RETURN_ADDRESS		((uintptr_t)__builtin_return_address(0))
    118 
    119 /*
    120  * Acquire a resource.
    121  */
    122 static int
    123 acquire(struct lock **lkpp, int *s, int extflags,
    124 	int drain, int wanted, uintptr_t ra)
    125 {
    126 	int error;
    127 	struct lock *lkp = *lkpp;
    128 	LOCKSTAT_TIMER(slptime);
    129 	LOCKSTAT_FLAG(lsflag);
    130 
    131 	KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
    132 
    133 	LOCKSTAT_ENTER(lsflag);
    134 
    135 	for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
    136 		if (drain)
    137 			lkp->lk_flags |= LK_WAITDRAIN;
    138 		else {
    139 			lkp->lk_waitcount++;
    140 			lkp->lk_flags |= LK_WAIT_NONZERO;
    141 		}
    142 		LOCKSTAT_START_TIMER(lsflag, slptime);
    143 		error = ltsleep(drain ? (void *)&lkp->lk_flags : (void *)lkp,
    144 		    lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo,
    145 		    &lkp->lk_interlock);
    146 		LOCKSTAT_STOP_TIMER(lsflag, slptime);
    147 		LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
    148 		    LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
    149 		if (!drain) {
    150 			lkp->lk_waitcount--;
    151 			if (lkp->lk_waitcount == 0)
    152 				lkp->lk_flags &= ~LK_WAIT_NONZERO;
    153 		}
    154 		if (error)
    155 			break;
    156 		if (extflags & LK_SLEEPFAIL) {
    157 			error = ENOLCK;
    158 			break;
    159 		}
    160 	}
    161 
    162 	LOCKSTAT_EXIT(lsflag);
    163 
    164 	return error;
    165 }
    166 
    167 #define	SETHOLDER(lkp, pid, lid, cpu_id)				\
    168 do {									\
    169 	(lkp)->lk_lockholder = pid;					\
    170 	(lkp)->lk_locklwp = lid;					\
    171 } while (/*CONSTCOND*/0)
    172 
    173 #define	WEHOLDIT(lkp, pid, lid, cpu_id)					\
    174 	 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
    175 
    176 #define	WAKEUP_WAITER(lkp)						\
    177 do {									\
    178 	if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) {			\
    179 		wakeup((lkp));						\
    180 	}								\
    181 } while (/*CONSTCOND*/0)
    182 
    183 #if defined(LOCKDEBUG)
    184 /*
    185  * Lock debug printing routine; can be configured to print to console
    186  * or log to syslog.
    187  */
    188 void
    189 lock_printf(const char *fmt, ...)
    190 {
    191 	char b[150];
    192 	va_list ap;
    193 
    194 	va_start(ap, fmt);
    195 	if (lock_debug_syslog)
    196 		vlog(LOG_DEBUG, fmt, ap);
    197 	else {
    198 		vsnprintf(b, sizeof(b), fmt, ap);
    199 		printf_nolog("%s", b);
    200 	}
    201 	va_end(ap);
    202 }
    203 #endif /* LOCKDEBUG */
    204 
    205 static void
    206 lockpanic(struct lock *lkp, const char *fmt, ...)
    207 {
    208 	char s[150], b[150];
    209 	static const char *locktype[] = {
    210 	    "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
    211 	    "downgrade", "release", "drain", "exclother", "*9*",
    212 	    "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
    213 	};
    214 	va_list ap;
    215 	va_start(ap, fmt);
    216 	vsnprintf(s, sizeof(s), fmt, ap);
    217 	va_end(ap);
    218 	bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
    219 	panic("%s ("
    220 	    "type %s flags %s, sharecount %d, exclusivecount %d, "
    221 	    "recurselevel %d, waitcount %d, wmesg %s"
    222 	    ", lock_addr %p, unlock_addr %p"
    223 	    ")\n",
    224 	    s, locktype[lkp->lk_flags & LK_TYPE_MASK],
    225 	    b, lkp->lk_sharecount, lkp->lk_exclusivecount,
    226 	    lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg,
    227 	    (void *)lkp->lk_lock_addr, (void *)lkp->lk_unlock_addr
    228 	);
    229 }
    230 
    231 /*
    232  * Initialize a lock; required before use.
    233  */
    234 void
    235 lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
    236 {
    237 
    238 	memset(lkp, 0, sizeof(struct lock));
    239 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    240 	simple_lock_init(&lkp->lk_interlock);
    241 	lkp->lk_lockholder = LK_NOPROC;
    242 	lkp->lk_prio = prio;
    243 	lkp->lk_timo = timo;
    244 	lkp->lk_wmesg = wmesg;
    245 	lkp->lk_lock_addr = 0;
    246 	lkp->lk_unlock_addr = 0;
    247 }
    248 
    249 void
    250 lockdestroy(struct lock *lkp)
    251 {
    252 
    253 	/* nothing yet */
    254 }
    255 
    256 /*
    257  * Determine the status of a lock.
    258  */
    259 int
    260 lockstatus(struct lock *lkp)
    261 {
    262 	int lock_type = 0;
    263 	struct lwp *l = curlwp; /* XXX */
    264 	pid_t pid;
    265 	lwpid_t lid;
    266 	cpuid_t cpu_num;
    267 
    268 	if (l == NULL) {
    269 		cpu_num = cpu_number();
    270 		pid = LK_KERNPROC;
    271 		lid = 0;
    272 	} else {
    273 		cpu_num = LK_NOCPU;
    274 		pid = l->l_proc->p_pid;
    275 		lid = l->l_lid;
    276 	}
    277 
    278 	simple_lock(&lkp->lk_interlock);
    279 	if (lkp->lk_exclusivecount != 0) {
    280 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    281 			lock_type = LK_EXCLUSIVE;
    282 		else
    283 			lock_type = LK_EXCLOTHER;
    284 	} else if (lkp->lk_sharecount != 0)
    285 		lock_type = LK_SHARED;
    286 	else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
    287 		lock_type = LK_EXCLOTHER;
    288 	simple_unlock(&lkp->lk_interlock);
    289 	return (lock_type);
    290 }
    291 
    292 /*
    293  * XXX XXX kludge around another kludge..
    294  *
    295  * vfs_shutdown() may be called from interrupt context, either as a result
    296  * of a panic, or from the debugger.   It proceeds to call
    297  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    298  *
    299  * We would like to make an attempt to sync the filesystems in this case, so
    300  * if this happens, we treat attempts to acquire locks specially.
    301  * All locks are acquired on behalf of proc0.
    302  *
    303  * If we've already paniced, we don't block waiting for locks, but
    304  * just barge right ahead since we're already going down in flames.
    305  */
    306 
    307 /*
    308  * Set, change, or release a lock.
    309  *
    310  * Shared requests increment the shared count. Exclusive requests set the
    311  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    312  * accepted shared locks and shared-to-exclusive upgrades to go away.
    313  */
    314 int
    315 lockmgr(struct lock *lkp, u_int flags, struct simplelock *interlkp)
    316 {
    317 	int error;
    318 	pid_t pid;
    319 	lwpid_t lid;
    320 	int extflags;
    321 	cpuid_t cpu_num;
    322 	struct lwp *l = curlwp;
    323 	int lock_shutdown_noblock = 0;
    324 	int s = 0;
    325 
    326 	error = 0;
    327 
    328 	/* LK_RETRY is for vn_lock, not for lockmgr. */
    329 	KASSERT((flags & LK_RETRY) == 0);
    330 	KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
    331 
    332 	simple_lock(&lkp->lk_interlock);
    333 	if (flags & LK_INTERLOCK)
    334 		simple_unlock(interlkp);
    335 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    336 
    337 	if (l == NULL) {
    338 		if (!doing_shutdown) {
    339 			panic("lockmgr: no context");
    340 		} else {
    341 			l = &lwp0;
    342 			if (panicstr && (!(flags & LK_NOWAIT))) {
    343 				flags |= LK_NOWAIT;
    344 				lock_shutdown_noblock = 1;
    345 			}
    346 		}
    347 	}
    348 	lid = l->l_lid;
    349 	pid = l->l_proc->p_pid;
    350 	cpu_num = cpu_number();
    351 
    352 	/*
    353 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    354 	 * exclusive lock is returned. The only valid operation thereafter
    355 	 * is a single release of that exclusive lock. This final release
    356 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    357 	 * further requests of any sort will result in a panic. The bits
    358 	 * selected for these two flags are chosen so that they will be set
    359 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    360 	 * The final release is permitted to give a new lease on life to
    361 	 * the lock by specifying LK_REENABLE.
    362 	 */
    363 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    364 #ifdef DIAGNOSTIC /* { */
    365 		if (lkp->lk_flags & LK_DRAINED)
    366 			lockpanic(lkp, "lockmgr: using decommissioned lock");
    367 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    368 		    WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
    369 			lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
    370 			    flags & LK_TYPE_MASK);
    371 #endif /* DIAGNOSTIC */ /* } */
    372 		lkp->lk_flags &= ~LK_DRAINING;
    373 		if ((flags & LK_REENABLE) == 0)
    374 			lkp->lk_flags |= LK_DRAINED;
    375 	}
    376 
    377 	switch (flags & LK_TYPE_MASK) {
    378 
    379 	case LK_SHARED:
    380 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    381 			/*
    382 			 * If just polling, check to see if we will block.
    383 			 */
    384 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    385 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    386 				error = EBUSY;
    387 				break;
    388 			}
    389 			/*
    390 			 * Wait for exclusive locks and upgrades to clear.
    391 			 */
    392 			error = acquire(&lkp, &s, extflags, 0,
    393 			    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
    394 			    RETURN_ADDRESS);
    395 			if (error)
    396 				break;
    397 			lkp->lk_sharecount++;
    398 			lkp->lk_flags |= LK_SHARE_NONZERO;
    399 			COUNT(lkp, l, cpu_num, 1);
    400 			break;
    401 		}
    402 		/*
    403 		 * We hold an exclusive lock, so downgrade it to shared.
    404 		 * An alternative would be to fail with EDEADLK.
    405 		 */
    406 		lkp->lk_sharecount++;
    407 		lkp->lk_flags |= LK_SHARE_NONZERO;
    408 		COUNT(lkp, l, cpu_num, 1);
    409 		/* fall into downgrade */
    410 
    411 	case LK_DOWNGRADE:
    412 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
    413 		    lkp->lk_exclusivecount == 0)
    414 			lockpanic(lkp, "lockmgr: not holding exclusive lock");
    415 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    416 		lkp->lk_flags |= LK_SHARE_NONZERO;
    417 		lkp->lk_exclusivecount = 0;
    418 		lkp->lk_recurselevel = 0;
    419 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    420 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    421 #if defined(LOCKDEBUG)
    422 		lkp->lk_unlock_addr = RETURN_ADDRESS;
    423 #endif
    424 		WAKEUP_WAITER(lkp);
    425 		break;
    426 
    427 	case LK_EXCLUPGRADE:
    428 		/*
    429 		 * If another process is ahead of us to get an upgrade,
    430 		 * then we want to fail rather than have an intervening
    431 		 * exclusive access.
    432 		 */
    433 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    434 			lkp->lk_sharecount--;
    435 			if (lkp->lk_sharecount == 0)
    436 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    437 			COUNT(lkp, l, cpu_num, -1);
    438 			error = EBUSY;
    439 			break;
    440 		}
    441 		/* fall into normal upgrade */
    442 
    443 	case LK_UPGRADE:
    444 		/*
    445 		 * Upgrade a shared lock to an exclusive one. If another
    446 		 * shared lock has already requested an upgrade to an
    447 		 * exclusive lock, our shared lock is released and an
    448 		 * exclusive lock is requested (which will be granted
    449 		 * after the upgrade). If we return an error, the file
    450 		 * will always be unlocked.
    451 		 */
    452 		if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
    453 			lockpanic(lkp, "lockmgr: upgrade exclusive lock");
    454 		lkp->lk_sharecount--;
    455 		if (lkp->lk_sharecount == 0)
    456 			lkp->lk_flags &= ~LK_SHARE_NONZERO;
    457 		COUNT(lkp, l, cpu_num, -1);
    458 		/*
    459 		 * If we are just polling, check to see if we will block.
    460 		 */
    461 		if ((extflags & LK_NOWAIT) &&
    462 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    463 		     lkp->lk_sharecount > 1)) {
    464 			error = EBUSY;
    465 			break;
    466 		}
    467 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    468 			/*
    469 			 * We are first shared lock to request an upgrade, so
    470 			 * request upgrade and wait for the shared count to
    471 			 * drop to zero, then take exclusive lock.
    472 			 */
    473 			lkp->lk_flags |= LK_WANT_UPGRADE;
    474 			error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
    475 			    RETURN_ADDRESS);
    476 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    477 			if (error) {
    478 				WAKEUP_WAITER(lkp);
    479 				break;
    480 			}
    481 			lkp->lk_flags |= LK_HAVE_EXCL;
    482 			SETHOLDER(lkp, pid, lid, cpu_num);
    483 #if defined(LOCKDEBUG)
    484 			lkp->lk_lock_addr = RETURN_ADDRESS;
    485 #endif
    486 			if (lkp->lk_exclusivecount != 0)
    487 				lockpanic(lkp, "lockmgr: non-zero exclusive count");
    488 			lkp->lk_exclusivecount = 1;
    489 			if (extflags & LK_SETRECURSE)
    490 				lkp->lk_recurselevel = 1;
    491 			COUNT(lkp, l, cpu_num, 1);
    492 			break;
    493 		}
    494 		/*
    495 		 * Someone else has requested upgrade. Release our shared
    496 		 * lock, awaken upgrade requestor if we are the last shared
    497 		 * lock, then request an exclusive lock.
    498 		 */
    499 		if (lkp->lk_sharecount == 0)
    500 			WAKEUP_WAITER(lkp);
    501 		/* fall into exclusive request */
    502 
    503 	case LK_EXCLUSIVE:
    504 		if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
    505 			/*
    506 			 * Recursive lock.
    507 			 */
    508 			if ((extflags & LK_CANRECURSE) == 0 &&
    509 			     lkp->lk_recurselevel == 0) {
    510 				if (extflags & LK_RECURSEFAIL) {
    511 					error = EDEADLK;
    512 					break;
    513 				} else
    514 					lockpanic(lkp, "lockmgr: locking against myself");
    515 			}
    516 			lkp->lk_exclusivecount++;
    517 			if (extflags & LK_SETRECURSE &&
    518 			    lkp->lk_recurselevel == 0)
    519 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    520 			COUNT(lkp, l, cpu_num, 1);
    521 			break;
    522 		}
    523 		/*
    524 		 * If we are just polling, check to see if we will sleep.
    525 		 */
    526 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    527 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    528 		     LK_SHARE_NONZERO))) {
    529 			error = EBUSY;
    530 			break;
    531 		}
    532 		/*
    533 		 * Try to acquire the want_exclusive flag.
    534 		 */
    535 		error = acquire(&lkp, &s, extflags, 0,
    536 		    LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
    537 		if (error)
    538 			break;
    539 		lkp->lk_flags |= LK_WANT_EXCL;
    540 		/*
    541 		 * Wait for shared locks and upgrades to finish.
    542 		 */
    543 		error = acquire(&lkp, &s, extflags, 0,
    544 		    LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
    545 		    RETURN_ADDRESS);
    546 		lkp->lk_flags &= ~LK_WANT_EXCL;
    547 		if (error) {
    548 			WAKEUP_WAITER(lkp);
    549 			break;
    550 		}
    551 		lkp->lk_flags |= LK_HAVE_EXCL;
    552 		SETHOLDER(lkp, pid, lid, cpu_num);
    553 #if defined(LOCKDEBUG)
    554 		lkp->lk_lock_addr = RETURN_ADDRESS;
    555 #endif
    556 		if (lkp->lk_exclusivecount != 0)
    557 			lockpanic(lkp, "lockmgr: non-zero exclusive count");
    558 		lkp->lk_exclusivecount = 1;
    559 		if (extflags & LK_SETRECURSE)
    560 			lkp->lk_recurselevel = 1;
    561 		COUNT(lkp, l, cpu_num, 1);
    562 		break;
    563 
    564 	case LK_RELEASE:
    565 		if (lkp->lk_exclusivecount != 0) {
    566 			if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    567 				lockpanic(lkp, "lockmgr: pid %d.%d, not "
    568 				    "exclusive lock holder %d.%d "
    569 				    "unlocking", pid, lid,
    570 				    lkp->lk_lockholder,
    571 				    lkp->lk_locklwp);
    572 			}
    573 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    574 				lkp->lk_recurselevel = 0;
    575 			lkp->lk_exclusivecount--;
    576 			COUNT(lkp, l, cpu_num, -1);
    577 			if (lkp->lk_exclusivecount == 0) {
    578 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    579 				SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    580 #if defined(LOCKDEBUG)
    581 				lkp->lk_unlock_addr = RETURN_ADDRESS;
    582 #endif
    583 			}
    584 		} else if (lkp->lk_sharecount != 0) {
    585 			lkp->lk_sharecount--;
    586 			if (lkp->lk_sharecount == 0)
    587 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    588 			COUNT(lkp, l, cpu_num, -1);
    589 		}
    590 #ifdef DIAGNOSTIC
    591 		else
    592 			lockpanic(lkp, "lockmgr: release of unlocked lock!");
    593 #endif
    594 		WAKEUP_WAITER(lkp);
    595 		break;
    596 
    597 	case LK_DRAIN:
    598 		/*
    599 		 * Check that we do not already hold the lock, as it can
    600 		 * never drain if we do. Unfortunately, we have no way to
    601 		 * check for holding a shared lock, but at least we can
    602 		 * check for an exclusive one.
    603 		 */
    604 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    605 			lockpanic(lkp, "lockmgr: draining against myself");
    606 		/*
    607 		 * If we are just polling, check to see if we will sleep.
    608 		 */
    609 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    610 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    611 		     LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
    612 			error = EBUSY;
    613 			break;
    614 		}
    615 		error = acquire(&lkp, &s, extflags, 1,
    616 		    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    617 		    LK_SHARE_NONZERO | LK_WAIT_NONZERO,
    618 		    RETURN_ADDRESS);
    619 		if (error)
    620 			break;
    621 		lkp->lk_flags |= LK_HAVE_EXCL;
    622 		if ((extflags & LK_RESURRECT) == 0)
    623 			lkp->lk_flags |= LK_DRAINING;
    624 		SETHOLDER(lkp, pid, lid, cpu_num);
    625 #if defined(LOCKDEBUG)
    626 		lkp->lk_lock_addr = RETURN_ADDRESS;
    627 #endif
    628 		lkp->lk_exclusivecount = 1;
    629 		/* XXX unlikely that we'd want this */
    630 		if (extflags & LK_SETRECURSE)
    631 			lkp->lk_recurselevel = 1;
    632 		COUNT(lkp, l, cpu_num, 1);
    633 		break;
    634 
    635 	default:
    636 		simple_unlock(&lkp->lk_interlock);
    637 		lockpanic(lkp, "lockmgr: unknown locktype request %d",
    638 		    flags & LK_TYPE_MASK);
    639 		/* NOTREACHED */
    640 	}
    641 	if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
    642 	    ((lkp->lk_flags &
    643 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    644 	      LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
    645 		lkp->lk_flags &= ~LK_WAITDRAIN;
    646 		wakeup(&lkp->lk_flags);
    647 	}
    648 	/*
    649 	 * Note that this panic will be a recursive panic, since
    650 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    651 	 */
    652 	if (error && lock_shutdown_noblock)
    653 		lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
    654 
    655 	simple_unlock(&lkp->lk_interlock);
    656 	return (error);
    657 }
    658 
    659 /*
    660  * Print out information about state of a lock. Used by VOP_PRINT
    661  * routines to display ststus about contained locks.
    662  */
    663 void
    664 lockmgr_printinfo(struct lock *lkp)
    665 {
    666 
    667 	if (lkp->lk_sharecount)
    668 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    669 		    lkp->lk_sharecount);
    670 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    671 		printf(" lock type %s: EXCL (count %d) by ",
    672 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    673 		printf("pid %d.%d", lkp->lk_lockholder,
    674 		    lkp->lk_locklwp);
    675 	} else
    676 		printf(" not locked");
    677 	if (lkp->lk_waitcount > 0)
    678 		printf(" with %d pending", lkp->lk_waitcount);
    679 }
    680 
    681 #if defined(LOCKDEBUG)
    682 void
    683 assert_sleepable(struct simplelock *interlock, const char *msg)
    684 {
    685 
    686 	if (panicstr != NULL)
    687 		return;
    688 	LOCKDEBUG_BARRIER(&kernel_lock, 1);
    689 	if (CURCPU_IDLE_P() && !cold) {
    690 		panic("assert_sleepable: idle");
    691 	}
    692 }
    693 #endif
    694 
    695 /*
    696  * rump doesn't need the kernel lock so force it out.  We cannot
    697  * currently easily include it for compilation because of
    698  * a) SPINLOCK_* b) membar_producer().  They are defined in different
    699  * places / way for each arch, so just simply do not bother to
    700  * fight a lot for no gain (i.e. pain but still no gain).
    701  */
    702 #ifndef _RUMPKERNEL
    703 /*
    704  * Functions for manipulating the kernel_lock.  We put them here
    705  * so that they show up in profiles.
    706  */
    707 
    708 #define	_KERNEL_LOCK_ABORT(msg)						\
    709     LOCKDEBUG_ABORT(&kernel_lock, &_kernel_lock_ops, __func__, msg)
    710 
    711 #ifdef LOCKDEBUG
    712 #define	_KERNEL_LOCK_ASSERT(cond)					\
    713 do {									\
    714 	if (!(cond))							\
    715 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
    716 } while (/* CONSTCOND */ 0)
    717 #else
    718 #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
    719 #endif
    720 
    721 void	_kernel_lock_dump(volatile void *);
    722 
    723 lockops_t _kernel_lock_ops = {
    724 	"Kernel lock",
    725 	0,
    726 	_kernel_lock_dump
    727 };
    728 
    729 /*
    730  * Initialize the kernel lock.
    731  */
    732 void
    733 kernel_lock_init(void)
    734 {
    735 
    736 	__cpu_simple_lock_init(&kernel_lock);
    737 	kernel_lock_dodebug = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops,
    738 	    RETURN_ADDRESS);
    739 }
    740 
    741 /*
    742  * Print debugging information about the kernel lock.
    743  */
    744 void
    745 _kernel_lock_dump(volatile void *junk)
    746 {
    747 	struct cpu_info *ci = curcpu();
    748 
    749 	(void)junk;
    750 
    751 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
    752 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
    753 }
    754 
    755 /*
    756  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
    757  * acquisition is from process context.
    758  */
    759 void
    760 _kernel_lock(int nlocks, struct lwp *l)
    761 {
    762 	struct cpu_info *ci = curcpu();
    763 	LOCKSTAT_TIMER(spintime);
    764 	LOCKSTAT_FLAG(lsflag);
    765 	struct lwp *owant;
    766 #ifdef LOCKDEBUG
    767 	u_int spins;
    768 #endif
    769 	int s;
    770 
    771 	if (nlocks == 0)
    772 		return;
    773 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    774 
    775 	l = curlwp;
    776 
    777 	if (ci->ci_biglock_count != 0) {
    778 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(&kernel_lock));
    779 		ci->ci_biglock_count += nlocks;
    780 		l->l_blcnt += nlocks;
    781 		return;
    782 	}
    783 
    784 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    785 	LOCKDEBUG_WANTLOCK(kernel_lock_dodebug, &kernel_lock, RETURN_ADDRESS,
    786 	    0);
    787 
    788 	s = splvm();
    789 	if (__cpu_simple_lock_try(&kernel_lock)) {
    790 		ci->ci_biglock_count = nlocks;
    791 		l->l_blcnt = nlocks;
    792 		LOCKDEBUG_LOCKED(kernel_lock_dodebug, &kernel_lock,
    793 		    RETURN_ADDRESS, 0);
    794 		splx(s);
    795 		return;
    796 	}
    797 
    798 	LOCKSTAT_ENTER(lsflag);
    799 	LOCKSTAT_START_TIMER(lsflag, spintime);
    800 
    801 	/*
    802 	 * Before setting ci_biglock_wanted we must post a store
    803 	 * fence (see kern_mutex.c).  This is accomplished by the
    804 	 * __cpu_simple_lock_try() above.
    805 	 */
    806 	owant = ci->ci_biglock_wanted;
    807 	ci->ci_biglock_wanted = curlwp;	/* XXXAD */
    808 
    809 #ifdef LOCKDEBUG
    810 	spins = 0;
    811 #endif
    812 
    813 	do {
    814 		splx(s);
    815 		while (__SIMPLELOCK_LOCKED_P(&kernel_lock)) {
    816 #ifdef LOCKDEBUG
    817 			if (SPINLOCK_SPINOUT(spins))
    818 				_KERNEL_LOCK_ABORT("spinout");
    819 #endif
    820 			SPINLOCK_BACKOFF_HOOK;
    821 			SPINLOCK_SPIN_HOOK;
    822 		}
    823 		(void)splvm();
    824 	} while (!__cpu_simple_lock_try(&kernel_lock));
    825 
    826 	ci->ci_biglock_wanted = owant;
    827 	ci->ci_biglock_count = nlocks;
    828 	l->l_blcnt = nlocks;
    829 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    830 	LOCKDEBUG_LOCKED(kernel_lock_dodebug, &kernel_lock, RETURN_ADDRESS, 0);
    831 	splx(s);
    832 
    833 	/*
    834 	 * Again, another store fence is required (see kern_mutex.c).
    835 	 */
    836 	membar_producer();
    837 	if (owant == NULL) {
    838 		LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
    839 		    1, spintime);
    840 	}
    841 	LOCKSTAT_EXIT(lsflag);
    842 }
    843 
    844 /*
    845  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    846  * all holds.  If 'l' is non-null, the release is from process context.
    847  */
    848 void
    849 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
    850 {
    851 	struct cpu_info *ci = curcpu();
    852 	u_int olocks;
    853 	int s;
    854 
    855 	l = curlwp;
    856 
    857 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    858 
    859 	olocks = l->l_blcnt;
    860 
    861 	if (olocks == 0) {
    862 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    863 		if (countp != NULL)
    864 			*countp = 0;
    865 		return;
    866 	}
    867 
    868 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(&kernel_lock));
    869 
    870 	if (nlocks == 0)
    871 		nlocks = olocks;
    872 	else if (nlocks == -1) {
    873 		nlocks = 1;
    874 		_KERNEL_LOCK_ASSERT(olocks == 1);
    875 	}
    876 
    877 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    878 
    879 	l->l_blcnt -= nlocks;
    880 	if (ci->ci_biglock_count == nlocks) {
    881 		s = splvm();
    882 		LOCKDEBUG_UNLOCKED(kernel_lock_dodebug, &kernel_lock,
    883 		    RETURN_ADDRESS, 0);
    884 		ci->ci_biglock_count = 0;
    885 		__cpu_simple_unlock(&kernel_lock);
    886 		splx(s);
    887 	} else
    888 		ci->ci_biglock_count -= nlocks;
    889 
    890 	if (countp != NULL)
    891 		*countp = olocks;
    892 }
    893 #endif /* !_RUMPKERNEL */
    894