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