Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.16.2.1.2.1
      1 /*	$NetBSD: kern_lock.c,v 1.16.2.1.2.1 1999/06/07 04:25:30 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code contains ideas from software contributed to Berkeley by
      8  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
      9  * System project at Carnegie-Mellon University.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  *
     39  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     40  */
     41 
     42 #include "opt_lockdebug.h"
     43 #include "opt_ddb.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/proc.h>
     47 #include <sys/lock.h>
     48 #include <sys/systm.h>
     49 #include <machine/cpu.h>
     50 
     51 /*
     52  * Locking primitives implementation.
     53  * Locks provide shared/exclusive sychronization.
     54  */
     55 
     56 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
     57 #define COUNT(p, x) if (p) (p)->p_locks += (x)
     58 #else
     59 #define COUNT(p, x)
     60 #endif
     61 
     62 #if 0 /*#was defined(MULTIPROCESSOR)*/
     63 /*-
     64 
     65 This macro is Bad Style and it doesn't work either... [pk, 10-14-1998]
     66 
     67 -*
     68  * For multiprocessor system, try spin lock first.
     69  *
     70  * This should be inline expanded below, but we cannot have #if
     71  * inside a multiline define.
     72  */
     73 
     74 int lock_wait_time = 100;
     75 #define PAUSE(lkp, wanted)						\
     76 		if (lock_wait_time > 0) {				\
     77 			int i;						\
     78 									\
     79 			simple_unlock(&lkp->lk_interlock);		\
     80 			for (i = lock_wait_time; i > 0; i--)		\
     81 				if (!(wanted))				\
     82 					break;				\
     83 			simple_lock(&lkp->lk_interlock);		\
     84 		}							\
     85 		if (!(wanted))						\
     86 			break;
     87 
     88 #else /* ! MULTIPROCESSOR */
     89 
     90 /*
     91  * It is an error to spin on a uniprocessor as nothing will ever cause
     92  * the simple lock to clear while we are executing.
     93  */
     94 #define PAUSE(lkp, wanted)
     95 
     96 #endif /* MULTIPROCESSOR */
     97 
     98 /*
     99  * Acquire a resource.
    100  */
    101 #define ACQUIRE(lkp, error, extflags, wanted)				\
    102 	PAUSE(lkp, wanted);						\
    103 	for (error = 0; wanted; ) {					\
    104 		(lkp)->lk_waitcount++;					\
    105 		simple_unlock(&(lkp)->lk_interlock);			\
    106 		error = tsleep((void *)lkp, (lkp)->lk_prio,		\
    107 		    (lkp)->lk_wmesg, (lkp)->lk_timo);			\
    108 		simple_lock(&(lkp)->lk_interlock);			\
    109 		(lkp)->lk_waitcount--;					\
    110 		if (error)						\
    111 			break;						\
    112 		if ((extflags) & LK_SLEEPFAIL) {			\
    113 			error = ENOLCK;					\
    114 			break;						\
    115 		}							\
    116 	}
    117 
    118 /*
    119  * Initialize a lock; required before use.
    120  */
    121 void
    122 lockinit(lkp, prio, wmesg, timo, flags)
    123 	struct lock *lkp;
    124 	int prio;
    125 	const char *wmesg;
    126 	int timo;
    127 	int flags;
    128 {
    129 
    130 	memset(lkp, 0, sizeof(struct lock));
    131 	simple_lock_init(&lkp->lk_interlock);
    132 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    133 	lkp->lk_prio = prio;
    134 	lkp->lk_timo = timo;
    135 	lkp->lk_wmesg = wmesg;
    136 	lkp->lk_lockholder = LK_NOPROC;
    137 }
    138 
    139 /*
    140  * Determine the status of a lock.
    141  */
    142 int
    143 lockstatus(lkp)
    144 	struct lock *lkp;
    145 {
    146 	int lock_type = 0;
    147 
    148 	simple_lock(&lkp->lk_interlock);
    149 	if (lkp->lk_exclusivecount != 0)
    150 		lock_type = LK_EXCLUSIVE;
    151 	else if (lkp->lk_sharecount != 0)
    152 		lock_type = LK_SHARED;
    153 	simple_unlock(&lkp->lk_interlock);
    154 	return (lock_type);
    155 }
    156 
    157 /*
    158  * Set, change, or release a lock.
    159  *
    160  * Shared requests increment the shared count. Exclusive requests set the
    161  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    162  * accepted shared locks and shared-to-exclusive upgrades to go away.
    163  */
    164 int
    165 lockmgr(lkp, flags, interlkp)
    166 	__volatile struct lock *lkp;
    167 	u_int flags;
    168 	struct simplelock *interlkp;
    169 {
    170 	int error;
    171 	pid_t pid;
    172 	int extflags;
    173 	struct proc *p = curproc;
    174 
    175 	error = 0;
    176 	if (p)
    177 		pid = p->p_pid;
    178 	else
    179 		pid = LK_KERNPROC;
    180 	simple_lock(&lkp->lk_interlock);
    181 	if (flags & LK_INTERLOCK)
    182 		simple_unlock(interlkp);
    183 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    184 #ifdef DIAGNOSTIC
    185 	/*
    186 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    187 	 * exclusive lock is returned. The only valid operation thereafter
    188 	 * is a single release of that exclusive lock. This final release
    189 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    190 	 * further requests of any sort will result in a panic. The bits
    191 	 * selected for these two flags are chosen so that they will be set
    192 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    193 	 * The final release is permitted to give a new lease on life to
    194 	 * the lock by specifying LK_REENABLE.
    195 	 */
    196 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    197 		if (lkp->lk_flags & LK_DRAINED)
    198 			panic("lockmgr: using decommissioned lock");
    199 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    200 		    lkp->lk_lockholder != pid)
    201 			panic("lockmgr: non-release on draining lock: %d\n",
    202 			    flags & LK_TYPE_MASK);
    203 		lkp->lk_flags &= ~LK_DRAINING;
    204 		if ((flags & LK_REENABLE) == 0)
    205 			lkp->lk_flags |= LK_DRAINED;
    206 	}
    207 #endif DIAGNOSTIC
    208 
    209 	switch (flags & LK_TYPE_MASK) {
    210 
    211 	case LK_SHARED:
    212 		if (lkp->lk_lockholder != pid) {
    213 			/*
    214 			 * If just polling, check to see if we will block.
    215 			 */
    216 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    217 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    218 				error = EBUSY;
    219 				break;
    220 			}
    221 			/*
    222 			 * Wait for exclusive locks and upgrades to clear.
    223 			 */
    224 			ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    225 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    226 			if (error)
    227 				break;
    228 			lkp->lk_sharecount++;
    229 			COUNT(p, 1);
    230 			break;
    231 		}
    232 		/*
    233 		 * We hold an exclusive lock, so downgrade it to shared.
    234 		 * An alternative would be to fail with EDEADLK.
    235 		 */
    236 		lkp->lk_sharecount++;
    237 		COUNT(p, 1);
    238 		/* fall into downgrade */
    239 
    240 	case LK_DOWNGRADE:
    241 		if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
    242 			panic("lockmgr: not holding exclusive lock");
    243 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    244 		lkp->lk_exclusivecount = 0;
    245 		lkp->lk_recurselevel = 0;
    246 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    247 		lkp->lk_lockholder = LK_NOPROC;
    248 		if (lkp->lk_waitcount)
    249 			wakeup((void *)lkp);
    250 		break;
    251 
    252 	case LK_EXCLUPGRADE:
    253 		/*
    254 		 * If another process is ahead of us to get an upgrade,
    255 		 * then we want to fail rather than have an intervening
    256 		 * exclusive access.
    257 		 */
    258 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    259 			lkp->lk_sharecount--;
    260 			COUNT(p, -1);
    261 			error = EBUSY;
    262 			break;
    263 		}
    264 		/* fall into normal upgrade */
    265 
    266 	case LK_UPGRADE:
    267 		/*
    268 		 * Upgrade a shared lock to an exclusive one. If another
    269 		 * shared lock has already requested an upgrade to an
    270 		 * exclusive lock, our shared lock is released and an
    271 		 * exclusive lock is requested (which will be granted
    272 		 * after the upgrade). If we return an error, the file
    273 		 * will always be unlocked.
    274 		 */
    275 		if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0)
    276 			panic("lockmgr: upgrade exclusive lock");
    277 		lkp->lk_sharecount--;
    278 		COUNT(p, -1);
    279 		/*
    280 		 * If we are just polling, check to see if we will block.
    281 		 */
    282 		if ((extflags & LK_NOWAIT) &&
    283 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    284 		     lkp->lk_sharecount > 1)) {
    285 			error = EBUSY;
    286 			break;
    287 		}
    288 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    289 			/*
    290 			 * We are first shared lock to request an upgrade, so
    291 			 * request upgrade and wait for the shared count to
    292 			 * drop to zero, then take exclusive lock.
    293 			 */
    294 			lkp->lk_flags |= LK_WANT_UPGRADE;
    295 			ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
    296 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    297 			if (error)
    298 				break;
    299 			lkp->lk_flags |= LK_HAVE_EXCL;
    300 			lkp->lk_lockholder = pid;
    301 			if (lkp->lk_exclusivecount != 0)
    302 				panic("lockmgr: non-zero exclusive count");
    303 			lkp->lk_exclusivecount = 1;
    304 			if (extflags & LK_SETRECURSE)
    305 				lkp->lk_recurselevel = 1;
    306 			COUNT(p, 1);
    307 			break;
    308 		}
    309 		/*
    310 		 * Someone else has requested upgrade. Release our shared
    311 		 * lock, awaken upgrade requestor if we are the last shared
    312 		 * lock, then request an exclusive lock.
    313 		 */
    314 		if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
    315 			wakeup((void *)lkp);
    316 		/* fall into exclusive request */
    317 
    318 	case LK_EXCLUSIVE:
    319 		if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
    320 			/*
    321 			 *	Recursive lock.
    322 			 */
    323 			if ((extflags & LK_CANRECURSE) == 0 &&
    324 			     lkp->lk_recurselevel == 0) {
    325 				if (extflags & LK_RECURSEFAIL) {
    326 					error = EDEADLK;
    327 					break;
    328 				} else
    329 					panic("lockmgr: locking against myself");
    330 			}
    331 			lkp->lk_exclusivecount++;
    332 			if (extflags & LK_SETRECURSE &&
    333 			    lkp->lk_recurselevel == 0)
    334 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    335 			COUNT(p, 1);
    336 			break;
    337 		}
    338 		/*
    339 		 * If we are just polling, check to see if we will sleep.
    340 		 */
    341 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    342 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    343 		     lkp->lk_sharecount != 0)) {
    344 			error = EBUSY;
    345 			break;
    346 		}
    347 		/*
    348 		 * Try to acquire the want_exclusive flag.
    349 		 */
    350 		ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    351 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    352 		if (error)
    353 			break;
    354 		lkp->lk_flags |= LK_WANT_EXCL;
    355 		/*
    356 		 * Wait for shared locks and upgrades to finish.
    357 		 */
    358 		ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
    359 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    360 		lkp->lk_flags &= ~LK_WANT_EXCL;
    361 		if (error)
    362 			break;
    363 		lkp->lk_flags |= LK_HAVE_EXCL;
    364 		lkp->lk_lockholder = pid;
    365 		if (lkp->lk_exclusivecount != 0)
    366 			panic("lockmgr: non-zero exclusive count");
    367 		lkp->lk_exclusivecount = 1;
    368 		if (extflags & LK_SETRECURSE)
    369 			lkp->lk_recurselevel = 1;
    370 		COUNT(p, 1);
    371 		break;
    372 
    373 	case LK_RELEASE:
    374 		if (lkp->lk_exclusivecount != 0) {
    375 			if (pid != lkp->lk_lockholder)
    376 				panic("lockmgr: pid %d, not exclusive lock "
    377 				    "holder %d unlocking", pid,
    378 				    lkp->lk_lockholder);
    379 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    380 				lkp->lk_recurselevel = 0;
    381 			lkp->lk_exclusivecount--;
    382 			COUNT(p, -1);
    383 			if (lkp->lk_exclusivecount == 0) {
    384 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    385 				lkp->lk_lockholder = LK_NOPROC;
    386 			}
    387 		} else if (lkp->lk_sharecount != 0) {
    388 			lkp->lk_sharecount--;
    389 			COUNT(p, -1);
    390 		}
    391 		if (lkp->lk_waitcount)
    392 			wakeup((void *)lkp);
    393 		break;
    394 
    395 	case LK_DRAIN:
    396 		/*
    397 		 * Check that we do not already hold the lock, as it can
    398 		 * never drain if we do. Unfortunately, we have no way to
    399 		 * check for holding a shared lock, but at least we can
    400 		 * check for an exclusive one.
    401 		 */
    402 		if (lkp->lk_lockholder == pid)
    403 			panic("lockmgr: draining against myself");
    404 		/*
    405 		 * If we are just polling, check to see if we will sleep.
    406 		 */
    407 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    408 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    409 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    410 			error = EBUSY;
    411 			break;
    412 		}
    413 		PAUSE(lkp, ((lkp->lk_flags &
    414 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    415 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0));
    416 		for (error = 0; ((lkp->lk_flags &
    417 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    418 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
    419 			lkp->lk_flags |= LK_WAITDRAIN;
    420 			simple_unlock(&lkp->lk_interlock);
    421 			if ((error = tsleep((void *)&lkp->lk_flags,
    422 			    lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
    423 				return (error);
    424 			if ((extflags) & LK_SLEEPFAIL)
    425 				return (ENOLCK);
    426 			simple_lock(&lkp->lk_interlock);
    427 		}
    428 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    429 		lkp->lk_lockholder = pid;
    430 		lkp->lk_exclusivecount = 1;
    431 		/* XXX unlikely that we'd want this */
    432 		if (extflags & LK_SETRECURSE)
    433 			lkp->lk_recurselevel = 1;
    434 		COUNT(p, 1);
    435 		break;
    436 
    437 	default:
    438 		simple_unlock(&lkp->lk_interlock);
    439 		panic("lockmgr: unknown locktype request %d",
    440 		    flags & LK_TYPE_MASK);
    441 		/* NOTREACHED */
    442 	}
    443 	if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
    444 	     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    445 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    446 		lkp->lk_flags &= ~LK_WAITDRAIN;
    447 		wakeup((void *)&lkp->lk_flags);
    448 	}
    449 	simple_unlock(&lkp->lk_interlock);
    450 	return (error);
    451 }
    452 
    453 /*
    454  * Print out information about state of a lock. Used by VOP_PRINT
    455  * routines to display ststus about contained locks.
    456  */
    457 void
    458 lockmgr_printinfo(lkp)
    459 	struct lock *lkp;
    460 {
    461 
    462 	if (lkp->lk_sharecount)
    463 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    464 		    lkp->lk_sharecount);
    465 	else if (lkp->lk_flags & LK_HAVE_EXCL)
    466 		printf(" lock type %s: EXCL (count %d) by pid %d",
    467 		    lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
    468 	if (lkp->lk_waitcount > 0)
    469 		printf(" with %d pending", lkp->lk_waitcount);
    470 }
    471 
    472 #if defined(LOCKDEBUG) && !defined(MULTIPROCESSOR)
    473 #include <sys/kernel.h>
    474 #include <vm/vm.h>
    475 #include <sys/sysctl.h>
    476 int lockpausetime = 0;
    477 struct ctldebug debug2 = { "lockpausetime", &lockpausetime };
    478 int simplelockrecurse;
    479 LIST_HEAD(slocklist, simplelock) slockdebuglist;
    480 int simple_lock_debugger = 0;
    481 
    482 /*
    483  * Simple lock functions so that the debugger can see from whence
    484  * they are being called.
    485  */
    486 void
    487 simple_lock_init(alp)
    488 	struct simplelock *alp;
    489 {
    490 	alp->lock_data = SLOCK_UNLOCKED;
    491 	alp->lock_file = NULL;
    492 	alp->lock_line = 0;
    493 	alp->unlock_file = NULL;
    494 	alp->unlock_line = 0;
    495 	alp->lock_holder = 0;
    496 }
    497 
    498 void
    499 _simple_lock(alp, id, l)
    500 	__volatile struct simplelock *alp;
    501 	const char *id;
    502 	int l;
    503 {
    504 	int s;
    505 
    506 	if (simplelockrecurse)
    507 		return;
    508 	if (alp->lock_data != SLOCK_UNLOCKED) {
    509 		printf("simple_lock: lock held\n");
    510 		printf("currently at: %s:%d\n", id, l);
    511 		printf("last locked: %s:%d\n",
    512 		       alp->lock_file, alp->lock_line);
    513 		printf("last unlocked: %s:%d\n",
    514 		       alp->unlock_file, alp->unlock_line);
    515 		if (lockpausetime == -1)
    516 			panic("simple_lock: lock held");
    517 		if (lockpausetime == 1) {
    518 #ifdef BACKTRACE
    519 			BACKTRACE(curproc);
    520 #endif
    521 		}
    522 		if (simple_lock_debugger) {
    523 			Debugger();
    524 		}
    525 		return;
    526 	}
    527 
    528 	s = splhigh();
    529 	LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
    530 	splx(s);
    531 
    532 	alp->lock_data = SLOCK_LOCKED;
    533 	alp->lock_file = id;
    534 	alp->lock_line = l;
    535 	if (curproc)
    536 		curproc->p_simple_locks++;
    537 }
    538 
    539 int
    540 _simple_lock_try(alp, id, l)
    541 	__volatile struct simplelock *alp;
    542 	const char *id;
    543 	int l;
    544 {
    545 	int s;
    546 
    547 	if (alp->lock_data != SLOCK_UNLOCKED) {
    548 		printf("simple_lock_try: lock held\n");
    549 		printf("currently at: %s:%d\n", id, l);
    550 		printf("last locked: %s:%d\n",
    551 		       alp->lock_file, alp->lock_line);
    552 		printf("last unlocked: %s:%d\n",
    553 		       alp->unlock_file, alp->unlock_line);
    554 		if (lockpausetime == -1)
    555 			panic("simple_lock_try: lock held");
    556 		if (lockpausetime == 1) {
    557 #ifdef BACKTRACE
    558 			BACKTRACE(curproc);
    559 #endif
    560 		}
    561 		if (simple_lock_debugger) {
    562 			Debugger();
    563 		}
    564 		return (0);
    565 	}
    566 	if (simplelockrecurse)
    567 		return (1);
    568 	alp->lock_data = SLOCK_LOCKED;
    569 	alp->lock_file = id;
    570 	alp->lock_line = l;
    571 
    572 	s = splhigh();
    573 	LIST_INSERT_HEAD(&slockdebuglist, (struct simplelock *)alp, list);
    574 	splx(s);
    575 
    576 	if (curproc)
    577 		curproc->p_simple_locks++;
    578 	return (1);
    579 }
    580 
    581 void
    582 _simple_unlock(alp, id, l)
    583 	__volatile struct simplelock *alp;
    584 	const char *id;
    585 	int l;
    586 {
    587 	int s;
    588 
    589 	if (simplelockrecurse)
    590 		return;
    591 	if (alp->lock_data == SLOCK_UNLOCKED) {
    592 		printf("simple_unlock: lock not held\n");
    593 		printf("currently at: %s:%d\n", id, l);
    594 		printf("last locked: %s:%d\n",
    595 		       alp->lock_file, alp->lock_line);
    596 		printf("last unlocked: %s:%d\n",
    597 		       alp->unlock_file, alp->unlock_line);
    598 		if (lockpausetime == -1)
    599 			panic("simple_unlock: lock not held");
    600 		if (lockpausetime == 1) {
    601 #ifdef BACKTRACE
    602 			BACKTRACE(curproc);
    603 #endif
    604 		}
    605 		if (simple_lock_debugger) {
    606 			Debugger();
    607 		}
    608 		return;
    609 	}
    610 
    611 	s = splhigh();
    612 	LIST_REMOVE(alp, list);
    613 	alp->list.le_next = NULL;
    614 	alp->list.le_prev = NULL;
    615 	splx(s);
    616 
    617 	alp->lock_data = SLOCK_UNLOCKED;
    618 	alp->unlock_file = id;
    619 	alp->unlock_line = l;
    620 	if (curproc)
    621 		curproc->p_simple_locks--;
    622 }
    623 
    624 void
    625 _simple_lock_assert(alp, value, id, l)
    626 	__volatile struct simplelock *alp;
    627 	int value;
    628 	const char *id;
    629 	int l;
    630 {
    631 	if (alp->lock_data != value) {
    632 		panic("lock %p: value %d != expected %d at %s:%d",
    633 		      alp, alp->lock_data, value, id, l);
    634 	}
    635 }
    636 
    637 
    638 void
    639 simple_lock_dump()
    640 {
    641 	struct simplelock *alp;
    642 	int s;
    643 
    644 	s = splhigh();
    645 	printf("all simple locks:\n");
    646 	for (alp = LIST_FIRST(&slockdebuglist);
    647 	     alp != NULL;
    648 	     alp = LIST_NEXT(alp, list)) {
    649 		printf("%p  %s:%d\n", alp, alp->lock_file, alp->lock_line);
    650 	}
    651 	splx(s);
    652 }
    653 
    654 void
    655 simple_lock_freecheck(start, end)
    656 void *start, *end;
    657 {
    658 	struct simplelock *alp;
    659 	int s;
    660 
    661 	s = splhigh();
    662 	for (alp = LIST_FIRST(&slockdebuglist);
    663 	     alp != NULL;
    664 	     alp = LIST_NEXT(alp, list)) {
    665 		if ((void *)alp >= start && (void *)alp < end) {
    666 			printf("freeing simple_lock %p %s:%d\n",
    667 			       alp, alp->lock_file, alp->lock_line);
    668 #ifdef DDB
    669 			Debugger();
    670 #endif
    671 		}
    672 	}
    673 	splx(s);
    674 }
    675 #endif /* LOCKDEBUG && ! MULTIPROCESSOR */
    676