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