Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.110.2.9
      1 /*	$NetBSD: kern_lock.c,v 1.110.2.9 2007/07/29 11:33:05 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.9 2007/07/29 11:33:05 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 void
    327 lockdestroy(struct lock *lkp)
    328 {
    329 
    330 	mutex_destroy(&lkp->lk_interlock);
    331 }
    332 
    333 /*
    334  * Determine the status of a lock.
    335  */
    336 int
    337 lockstatus(struct lock *lkp)
    338 {
    339 	int lock_type = 0;
    340 	struct lwp *l = curlwp; /* XXX */
    341 	pid_t pid;
    342 	lwpid_t lid;
    343 	cpuid_t cpu_num;
    344 
    345 	if (l == NULL) {
    346 		cpu_num = cpu_number();
    347 		pid = LK_KERNPROC;
    348 		lid = 0;
    349 	} else {
    350 		cpu_num = LK_NOCPU;
    351 		pid = l->l_proc->p_pid;
    352 		lid = l->l_lid;
    353 	}
    354 
    355 	mutex_enter(&lkp->lk_interlock);
    356 	if (lkp->lk_exclusivecount != 0) {
    357 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    358 			lock_type = LK_EXCLUSIVE;
    359 		else
    360 			lock_type = LK_EXCLOTHER;
    361 	} else if (lkp->lk_sharecount != 0)
    362 		lock_type = LK_SHARED;
    363 	else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
    364 		lock_type = LK_EXCLOTHER;
    365 	mutex_exit(__UNVOLATILE(&lkp->lk_interlock));
    366 	return (lock_type);
    367 }
    368 
    369 /*
    370  * XXX XXX kludge around another kludge..
    371  *
    372  * vfs_shutdown() may be called from interrupt context, either as a result
    373  * of a panic, or from the debugger.   It proceeds to call
    374  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    375  *
    376  * We would like to make an attempt to sync the filesystems in this case, so
    377  * if this happens, we treat attempts to acquire locks specially.
    378  * All locks are acquired on behalf of proc0.
    379  *
    380  * If we've already paniced, we don't block waiting for locks, but
    381  * just barge right ahead since we're already going down in flames.
    382  */
    383 
    384 /*
    385  * Set, change, or release a lock.
    386  *
    387  * Shared requests increment the shared count. Exclusive requests set the
    388  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    389  * accepted shared locks and shared-to-exclusive upgrades to go away.
    390  */
    391 int
    392 #if defined(LOCKDEBUG)
    393 _lockmgr(volatile struct lock *lkp, u_int flags,
    394     kmutex_t *interlkp, const char *file, int line)
    395 #else
    396 lockmgr(volatile struct lock *lkp, u_int flags,
    397     kmutex_t *interlkp)
    398 #endif
    399 {
    400 	int error;
    401 	pid_t pid;
    402 	lwpid_t lid;
    403 	int extflags;
    404 	cpuid_t cpu_num;
    405 	struct lwp *l = curlwp;
    406 	int lock_shutdown_noblock = 0;
    407 	kmutex_t *mutex;
    408 	int s = 0;
    409 
    410 	error = 0;
    411 	mutex = __UNVOLATILE(&lkp->lk_interlock);
    412 
    413 	/* LK_RETRY is for vn_lock, not for lockmgr. */
    414 	KASSERT((flags & LK_RETRY) == 0);
    415 	KASSERT((l->l_flag & LW_INTR) == 0 || panicstr != NULL);
    416 
    417 	mutex_enter(mutex);
    418 	if (flags & LK_INTERLOCK)
    419 		mutex_exit(__UNVOLATILE(interlkp));
    420 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    421 
    422 	if (l == NULL) {
    423 		if (!doing_shutdown) {
    424 			panic("lockmgr: no context");
    425 		} else {
    426 			l = &lwp0;
    427 			if (panicstr && (!(flags & LK_NOWAIT))) {
    428 				flags |= LK_NOWAIT;
    429 				lock_shutdown_noblock = 1;
    430 			}
    431 		}
    432 	}
    433 	lid = l->l_lid;
    434 	pid = l->l_proc->p_pid;
    435 	cpu_num = cpu_number();
    436 
    437 	/*
    438 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    439 	 * exclusive lock is returned. The only valid operation thereafter
    440 	 * is a single release of that exclusive lock. This final release
    441 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    442 	 * further requests of any sort will result in a panic. The bits
    443 	 * selected for these two flags are chosen so that they will be set
    444 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    445 	 * The final release is permitted to give a new lease on life to
    446 	 * the lock by specifying LK_REENABLE.
    447 	 */
    448 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    449 #ifdef DIAGNOSTIC /* { */
    450 		if (lkp->lk_flags & LK_DRAINED)
    451 			lockpanic(lkp, "lockmgr: using decommissioned lock");
    452 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    453 		    WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
    454 			lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
    455 			    flags & LK_TYPE_MASK);
    456 #endif /* DIAGNOSTIC */ /* } */
    457 		lkp->lk_flags &= ~LK_DRAINING;
    458 		if ((flags & LK_REENABLE) == 0)
    459 			lkp->lk_flags |= LK_DRAINED;
    460 	}
    461 
    462 	switch (flags & LK_TYPE_MASK) {
    463 
    464 	case LK_SHARED:
    465 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    466 			/*
    467 			 * If just polling, check to see if we will block.
    468 			 */
    469 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    470 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    471 				error = EBUSY;
    472 				break;
    473 			}
    474 			/*
    475 			 * Wait for exclusive locks and upgrades to clear.
    476 			 */
    477 			error = acquire(&lkp, &s, extflags, 0,
    478 			    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
    479 			    RETURN_ADDRESS);
    480 			if (error)
    481 				break;
    482 			lkp->lk_sharecount++;
    483 			lkp->lk_flags |= LK_SHARE_NONZERO;
    484 			COUNT(lkp, l, cpu_num, 1);
    485 			break;
    486 		}
    487 		/*
    488 		 * We hold an exclusive lock, so downgrade it to shared.
    489 		 * An alternative would be to fail with EDEADLK.
    490 		 */
    491 		lkp->lk_sharecount++;
    492 		lkp->lk_flags |= LK_SHARE_NONZERO;
    493 		COUNT(lkp, l, cpu_num, 1);
    494 		/* fall into downgrade */
    495 
    496 	case LK_DOWNGRADE:
    497 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
    498 		    lkp->lk_exclusivecount == 0)
    499 			lockpanic(lkp, "lockmgr: not holding exclusive lock");
    500 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    501 		lkp->lk_flags |= LK_SHARE_NONZERO;
    502 		lkp->lk_exclusivecount = 0;
    503 		lkp->lk_recurselevel = 0;
    504 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    505 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    506 #if defined(LOCKDEBUG)
    507 		lkp->lk_unlock_file = file;
    508 		lkp->lk_unlock_line = line;
    509 #endif
    510 		WAKEUP_WAITER(lkp);
    511 		break;
    512 
    513 	case LK_EXCLUPGRADE:
    514 		/*
    515 		 * If another process is ahead of us to get an upgrade,
    516 		 * then we want to fail rather than have an intervening
    517 		 * exclusive access.
    518 		 */
    519 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    520 			lkp->lk_sharecount--;
    521 			if (lkp->lk_sharecount == 0)
    522 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    523 			COUNT(lkp, l, cpu_num, -1);
    524 			error = EBUSY;
    525 			break;
    526 		}
    527 		/* fall into normal upgrade */
    528 
    529 	case LK_UPGRADE:
    530 		/*
    531 		 * Upgrade a shared lock to an exclusive one. If another
    532 		 * shared lock has already requested an upgrade to an
    533 		 * exclusive lock, our shared lock is released and an
    534 		 * exclusive lock is requested (which will be granted
    535 		 * after the upgrade). If we return an error, the file
    536 		 * will always be unlocked.
    537 		 */
    538 		if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
    539 			lockpanic(lkp, "lockmgr: upgrade exclusive lock");
    540 		lkp->lk_sharecount--;
    541 		if (lkp->lk_sharecount == 0)
    542 			lkp->lk_flags &= ~LK_SHARE_NONZERO;
    543 		COUNT(lkp, l, cpu_num, -1);
    544 		/*
    545 		 * If we are just polling, check to see if we will block.
    546 		 */
    547 		if ((extflags & LK_NOWAIT) &&
    548 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    549 		     lkp->lk_sharecount > 1)) {
    550 			error = EBUSY;
    551 			break;
    552 		}
    553 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    554 			/*
    555 			 * We are first shared lock to request an upgrade, so
    556 			 * request upgrade and wait for the shared count to
    557 			 * drop to zero, then take exclusive lock.
    558 			 */
    559 			lkp->lk_flags |= LK_WANT_UPGRADE;
    560 			error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
    561 			    RETURN_ADDRESS);
    562 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    563 			if (error) {
    564 				WAKEUP_WAITER(lkp);
    565 				break;
    566 			}
    567 			lkp->lk_flags |= LK_HAVE_EXCL;
    568 			SETHOLDER(lkp, pid, lid, cpu_num);
    569 #if defined(LOCKDEBUG)
    570 			lkp->lk_lock_file = file;
    571 			lkp->lk_lock_line = line;
    572 #endif
    573 			if (lkp->lk_exclusivecount != 0)
    574 				lockpanic(lkp, "lockmgr: non-zero exclusive count");
    575 			lkp->lk_exclusivecount = 1;
    576 			if (extflags & LK_SETRECURSE)
    577 				lkp->lk_recurselevel = 1;
    578 			COUNT(lkp, l, cpu_num, 1);
    579 			break;
    580 		}
    581 		/*
    582 		 * Someone else has requested upgrade. Release our shared
    583 		 * lock, awaken upgrade requestor if we are the last shared
    584 		 * lock, then request an exclusive lock.
    585 		 */
    586 		if (lkp->lk_sharecount == 0)
    587 			WAKEUP_WAITER(lkp);
    588 		/* fall into exclusive request */
    589 
    590 	case LK_EXCLUSIVE:
    591 		if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
    592 			/*
    593 			 * Recursive lock.
    594 			 */
    595 			if ((extflags & LK_CANRECURSE) == 0 &&
    596 			     lkp->lk_recurselevel == 0) {
    597 				if (extflags & LK_RECURSEFAIL) {
    598 					error = EDEADLK;
    599 					break;
    600 				} else
    601 					lockpanic(lkp, "lockmgr: locking against myself");
    602 			}
    603 			lkp->lk_exclusivecount++;
    604 			if (extflags & LK_SETRECURSE &&
    605 			    lkp->lk_recurselevel == 0)
    606 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    607 			COUNT(lkp, l, cpu_num, 1);
    608 			break;
    609 		}
    610 		/*
    611 		 * If we are just polling, check to see if we will sleep.
    612 		 */
    613 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    614 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    615 		     LK_SHARE_NONZERO))) {
    616 			error = EBUSY;
    617 			break;
    618 		}
    619 		/*
    620 		 * Try to acquire the want_exclusive flag.
    621 		 */
    622 		error = acquire(&lkp, &s, extflags, 0,
    623 		    LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
    624 		if (error)
    625 			break;
    626 		lkp->lk_flags |= LK_WANT_EXCL;
    627 		/*
    628 		 * Wait for shared locks and upgrades to finish.
    629 		 */
    630 		error = acquire(&lkp, &s, extflags, 0,
    631 		    LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
    632 		    RETURN_ADDRESS);
    633 		lkp->lk_flags &= ~LK_WANT_EXCL;
    634 		if (error) {
    635 			WAKEUP_WAITER(lkp);
    636 			break;
    637 		}
    638 		lkp->lk_flags |= LK_HAVE_EXCL;
    639 		SETHOLDER(lkp, pid, lid, cpu_num);
    640 #if defined(LOCKDEBUG)
    641 		lkp->lk_lock_file = file;
    642 		lkp->lk_lock_line = line;
    643 #endif
    644 		if (lkp->lk_exclusivecount != 0)
    645 			lockpanic(lkp, "lockmgr: non-zero exclusive count");
    646 		lkp->lk_exclusivecount = 1;
    647 		if (extflags & LK_SETRECURSE)
    648 			lkp->lk_recurselevel = 1;
    649 		COUNT(lkp, l, cpu_num, 1);
    650 		break;
    651 
    652 	case LK_RELEASE:
    653 		if (lkp->lk_exclusivecount != 0) {
    654 			if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    655 				lockpanic(lkp, "lockmgr: pid %d.%d, not "
    656 				    "exclusive lock holder %d.%d "
    657 				    "unlocking", pid, lid,
    658 				    lkp->lk_lockholder,
    659 				    lkp->lk_locklwp);
    660 			}
    661 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    662 				lkp->lk_recurselevel = 0;
    663 			lkp->lk_exclusivecount--;
    664 			COUNT(lkp, l, cpu_num, -1);
    665 			if (lkp->lk_exclusivecount == 0) {
    666 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    667 				SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    668 #if defined(LOCKDEBUG)
    669 				lkp->lk_unlock_file = file;
    670 				lkp->lk_unlock_line = line;
    671 #endif
    672 			}
    673 		} else if (lkp->lk_sharecount != 0) {
    674 			lkp->lk_sharecount--;
    675 			if (lkp->lk_sharecount == 0)
    676 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    677 			COUNT(lkp, l, cpu_num, -1);
    678 		}
    679 #ifdef DIAGNOSTIC
    680 		else
    681 			lockpanic(lkp, "lockmgr: release of unlocked lock!");
    682 #endif
    683 		WAKEUP_WAITER(lkp);
    684 		break;
    685 
    686 	case LK_DRAIN:
    687 		/*
    688 		 * Check that we do not already hold the lock, as it can
    689 		 * never drain if we do. Unfortunately, we have no way to
    690 		 * check for holding a shared lock, but at least we can
    691 		 * check for an exclusive one.
    692 		 */
    693 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    694 			lockpanic(lkp, "lockmgr: draining against myself");
    695 		/*
    696 		 * If we are just polling, check to see if we will sleep.
    697 		 */
    698 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    699 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    700 		     LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
    701 			error = EBUSY;
    702 			break;
    703 		}
    704 		error = acquire(&lkp, &s, extflags, 1,
    705 		    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    706 		    LK_SHARE_NONZERO | LK_WAIT_NONZERO,
    707 		    RETURN_ADDRESS);
    708 		if (error)
    709 			break;
    710 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    711 		SETHOLDER(lkp, pid, lid, cpu_num);
    712 #if defined(LOCKDEBUG)
    713 		lkp->lk_lock_file = file;
    714 		lkp->lk_lock_line = line;
    715 #endif
    716 		lkp->lk_exclusivecount = 1;
    717 		/* XXX unlikely that we'd want this */
    718 		if (extflags & LK_SETRECURSE)
    719 			lkp->lk_recurselevel = 1;
    720 		COUNT(lkp, l, cpu_num, 1);
    721 		break;
    722 
    723 	default:
    724 		mutex_exit(mutex);
    725 		lockpanic(lkp, "lockmgr: unknown locktype request %d",
    726 		    flags & LK_TYPE_MASK);
    727 		/* NOTREACHED */
    728 	}
    729 	if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
    730 	    ((lkp->lk_flags &
    731 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    732 	      LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
    733 		lkp->lk_flags &= ~LK_WAITDRAIN;
    734 		wakeup(&lkp->lk_flags);
    735 	}
    736 	/*
    737 	 * Note that this panic will be a recursive panic, since
    738 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    739 	 */
    740 	if (error && lock_shutdown_noblock)
    741 		lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
    742 
    743 	mutex_exit(mutex);
    744 	return (error);
    745 }
    746 
    747 /*
    748  * Print out information about state of a lock. Used by VOP_PRINT
    749  * routines to display ststus about contained locks.
    750  */
    751 void
    752 lockmgr_printinfo(volatile struct lock *lkp)
    753 {
    754 
    755 	if (lkp->lk_sharecount)
    756 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    757 		    lkp->lk_sharecount);
    758 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    759 		printf(" lock type %s: EXCL (count %d) by ",
    760 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    761 		printf("pid %d.%d", lkp->lk_lockholder,
    762 		    lkp->lk_locklwp);
    763 	} else
    764 		printf(" not locked");
    765 	if (lkp->lk_waitcount > 0)
    766 		printf(" with %d pending", lkp->lk_waitcount);
    767 }
    768 
    769 #if defined(LOCKDEBUG)
    770 void
    771 assert_sleepable(struct simplelock *interlock, const char *msg)
    772 {
    773 
    774 	LOCKDEBUG_BARRIER(&kernel_lock, 1);
    775 	if (CURCPU_IDLE_P()) {
    776 		panic("assert_sleepable: idle");
    777 	}
    778 }
    779 #endif
    780 
    781 #if defined(MULTIPROCESSOR)
    782 
    783 /*
    784  * Functions for manipulating the kernel_lock.  We put them here
    785  * so that they show up in profiles.
    786  */
    787 
    788 #define	_KERNEL_LOCK_ABORT(msg)						\
    789     LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops,	\
    790         __FUNCTION__, msg)
    791 
    792 #ifdef LOCKDEBUG
    793 #define	_KERNEL_LOCK_ASSERT(cond)					\
    794 do {									\
    795 	if (!(cond))							\
    796 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
    797 } while (/* CONSTCOND */ 0)
    798 #else
    799 #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
    800 #endif
    801 
    802 void	_kernel_lock_dump(volatile void *);
    803 
    804 lockops_t _kernel_lock_ops = {
    805 	"Kernel lock",
    806 	0,
    807 	_kernel_lock_dump
    808 };
    809 
    810 /*
    811  * Initialize the kernel lock.
    812  */
    813 void
    814 _kernel_lock_init(void)
    815 {
    816 
    817 	__cpu_simple_lock_init(&kernel_lock);
    818 	kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops);
    819 }
    820 
    821 /*
    822  * Print debugging information about the kernel lock.
    823  */
    824 void
    825 _kernel_lock_dump(volatile void *junk)
    826 {
    827 	struct cpu_info *ci = curcpu();
    828 
    829 	(void)junk;
    830 
    831 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
    832 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
    833 }
    834 
    835 /*
    836  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
    837  * acquisition is from process context.
    838  */
    839 void
    840 _kernel_lock(int nlocks, struct lwp *l)
    841 {
    842 	struct cpu_info *ci = curcpu();
    843 	LOCKSTAT_TIMER(spintime);
    844 	LOCKSTAT_FLAG(lsflag);
    845 	struct lwp *owant;
    846 #ifdef LOCKDEBUG
    847 	u_int spins;
    848 #endif
    849 	int s;
    850 
    851 	if (nlocks == 0)
    852 		return;
    853 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    854 
    855 	l = curlwp;
    856 	s = splbiglock();
    857 
    858 	if (ci->ci_biglock_count != 0) {
    859 		_KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
    860 		ci->ci_biglock_count += nlocks;
    861 		l->l_blcnt += nlocks;
    862 		splx(s);
    863 		return;
    864 	}
    865 
    866 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    867 	LOCKDEBUG_WANTLOCK(kernel_lock_id,
    868 	    (uintptr_t)__builtin_return_address(0), 0);
    869 
    870 	if (__cpu_simple_lock_try(&kernel_lock)) {
    871 		ci->ci_biglock_count = nlocks;
    872 		l->l_blcnt = nlocks;
    873 		LOCKDEBUG_LOCKED(kernel_lock_id,
    874 		    (uintptr_t)__builtin_return_address(0), 0);
    875 		splx(s);
    876 		return;
    877 	}
    878 
    879 	LOCKSTAT_ENTER(lsflag);
    880 	LOCKSTAT_START_TIMER(lsflag, spintime);
    881 
    882 	/*
    883 	 * Before setting ci_biglock_wanted we must post a store
    884 	 * fence (see kern_mutex.c).  This is accomplished by the
    885 	 * __cpu_simple_lock_try() above.
    886 	 */
    887 	owant = ci->ci_biglock_wanted;
    888 	ci->ci_biglock_wanted = curlwp;	/* XXXAD */
    889 
    890 #ifdef LOCKDEBUG
    891 	spins = 0;
    892 #endif
    893 
    894 	do {
    895 		while (kernel_lock == __SIMPLELOCK_LOCKED) {
    896 #ifdef LOCKDEBUG
    897 			if (SPINLOCK_SPINOUT(spins))
    898 				_KERNEL_LOCK_ABORT("spinout");
    899 #endif
    900 			splx(s);
    901 			SPINLOCK_SPIN_HOOK;
    902 			(void)splbiglock();
    903 		}
    904 	} while (!__cpu_simple_lock_try(&kernel_lock));
    905 
    906 	ci->ci_biglock_wanted = owant;
    907 	ci->ci_biglock_count = nlocks;
    908 	l->l_blcnt = nlocks;
    909 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    910 	LOCKDEBUG_LOCKED(kernel_lock_id,
    911 	    (uintptr_t)__builtin_return_address(0), 0);
    912 	splx(s);
    913 
    914 	/*
    915 	 * Again, another store fence is required (see kern_mutex.c).
    916 	 */
    917 	mb_write();
    918 	if (owant == NULL) {
    919 		LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
    920 		    1, spintime);
    921 	}
    922 	LOCKSTAT_EXIT(lsflag);
    923 }
    924 
    925 /*
    926  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    927  * all holds.  If 'l' is non-null, the release is from process context.
    928  */
    929 void
    930 _kernel_unlock(int nlocks, struct lwp *l, int *countp)
    931 {
    932 	struct cpu_info *ci = curcpu();
    933 	u_int olocks;
    934 	int s;
    935 
    936 	l = curlwp;
    937 
    938 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    939 
    940 	olocks = l->l_blcnt;
    941 
    942 	if (olocks == 0) {
    943 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    944 		if (countp != NULL)
    945 			*countp = 0;
    946 		return;
    947 	}
    948 
    949 	_KERNEL_LOCK_ASSERT(kernel_lock == __SIMPLELOCK_LOCKED);
    950 
    951 	if (nlocks == 0)
    952 		nlocks = olocks;
    953 	else if (nlocks == -1) {
    954 		nlocks = 1;
    955 		_KERNEL_LOCK_ASSERT(olocks == 1);
    956 	}
    957 
    958 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    959 
    960 	s = splbiglock();
    961 	l->l_blcnt -= nlocks;
    962 	if ((ci->ci_biglock_count -= nlocks) == 0) {
    963 		LOCKDEBUG_UNLOCKED(kernel_lock_id,
    964 		    (uintptr_t)__builtin_return_address(0), 0);
    965 		__cpu_simple_unlock(&kernel_lock);
    966 	}
    967 	splx(s);
    968 
    969 	if (countp != NULL)
    970 		*countp = olocks;
    971 }
    972 
    973 #if defined(DEBUG)
    974 /*
    975  * Assert that the kernel lock is held.
    976  */
    977 void
    978 _kernel_lock_assert_locked(void)
    979 {
    980 
    981 	if (kernel_lock != __SIMPLELOCK_LOCKED ||
    982 	    curcpu()->ci_biglock_count == 0)
    983 		_KERNEL_LOCK_ABORT("not locked");
    984 }
    985 
    986 void
    987 _kernel_lock_assert_unlocked()
    988 {
    989 
    990 	if (curcpu()->ci_biglock_count != 0)
    991 		_KERNEL_LOCK_ABORT("locked");
    992 }
    993 #endif
    994 
    995 #endif	/* MULTIPROCESSOR || LOCKDEBUG */
    996