Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.7
      1 /*	$NetBSD: kern_lock.c,v 1.7 1998/05/20 01:32:29 thorpej 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 
     44 #include <sys/param.h>
     45 #include <sys/proc.h>
     46 #include <sys/lock.h>
     47 #include <sys/systm.h>
     48 #include <machine/cpu.h>
     49 
     50 /*
     51  * Locking primitives implementation.
     52  * Locks provide shared/exclusive sychronization.
     53  */
     54 
     55 #ifdef LOCKDEBUG
     56 #define COUNT(p, x) if (p) (p)->p_locks += (x)
     57 #else
     58 #define COUNT(p, x)
     59 #endif
     60 
     61 #if NCPUS > 1
     62 
     63 /*
     64  * For multiprocessor system, try spin lock first.
     65  *
     66  * This should be inline expanded below, but we cannot have #if
     67  * inside a multiline define.
     68  */
     69 int lock_wait_time = 100;
     70 #define PAUSE(lkp, wanted)						\
     71 		if (lock_wait_time > 0) {				\
     72 			int i;						\
     73 									\
     74 			simple_unlock(&lkp->lk_interlock);		\
     75 			for (i = lock_wait_time; i > 0; i--)		\
     76 				if (!(wanted))				\
     77 					break;				\
     78 			simple_lock(&lkp->lk_interlock);		\
     79 		}							\
     80 		if (!(wanted))						\
     81 			break;
     82 
     83 #else /* NCPUS == 1 */
     84 
     85 /*
     86  * It is an error to spin on a uniprocessor as nothing will ever cause
     87  * the simple lock to clear while we are executing.
     88  */
     89 #define PAUSE(lkp, wanted)
     90 
     91 #endif /* NCPUS == 1 */
     92 
     93 /*
     94  * Acquire a resource.
     95  */
     96 #define ACQUIRE(lkp, error, extflags, wanted)				\
     97 	PAUSE(lkp, wanted);						\
     98 	for (error = 0; wanted; ) {					\
     99 		(lkp)->lk_waitcount++;					\
    100 		simple_unlock(&(lkp)->lk_interlock);			\
    101 		error = tsleep((void *)lkp, (lkp)->lk_prio,		\
    102 		    (lkp)->lk_wmesg, (lkp)->lk_timo);			\
    103 		simple_lock(&(lkp)->lk_interlock);			\
    104 		(lkp)->lk_waitcount--;					\
    105 		if (error)						\
    106 			break;						\
    107 		if ((extflags) & LK_SLEEPFAIL) {			\
    108 			error = ENOLCK;					\
    109 			break;						\
    110 		}							\
    111 	}
    112 
    113 /*
    114  * Initialize a lock; required before use.
    115  */
    116 void
    117 lockinit(lkp, prio, wmesg, timo, flags)
    118 	struct lock *lkp;
    119 	int prio;
    120 	const char *wmesg;
    121 	int timo;
    122 	int flags;
    123 {
    124 
    125 	bzero(lkp, sizeof(struct lock));
    126 	simple_lock_init(&lkp->lk_interlock);
    127 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    128 	lkp->lk_prio = prio;
    129 	lkp->lk_timo = timo;
    130 	lkp->lk_wmesg = wmesg;
    131 	lkp->lk_lockholder = LK_NOPROC;
    132 }
    133 
    134 /*
    135  * Determine the status of a lock.
    136  */
    137 int
    138 lockstatus(lkp)
    139 	struct lock *lkp;
    140 {
    141 	int lock_type = 0;
    142 
    143 	simple_lock(&lkp->lk_interlock);
    144 	if (lkp->lk_exclusivecount != 0)
    145 		lock_type = LK_EXCLUSIVE;
    146 	else if (lkp->lk_sharecount != 0)
    147 		lock_type = LK_SHARED;
    148 	simple_unlock(&lkp->lk_interlock);
    149 	return (lock_type);
    150 }
    151 
    152 /*
    153  * Set, change, or release a lock.
    154  *
    155  * Shared requests increment the shared count. Exclusive requests set the
    156  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    157  * accepted shared locks and shared-to-exclusive upgrades to go away.
    158  */
    159 int
    160 lockmgr(lkp, flags, interlkp)
    161 	__volatile struct lock *lkp;
    162 	u_int flags;
    163 	struct simplelock *interlkp;
    164 {
    165 	int error;
    166 	pid_t pid;
    167 	int extflags;
    168 	struct proc *p = curproc;
    169 
    170 	error = 0;
    171 	if (p)
    172 		pid = p->p_pid;
    173 	else
    174 		pid = LK_KERNPROC;
    175 	simple_lock(&lkp->lk_interlock);
    176 	if (flags & LK_INTERLOCK)
    177 		simple_unlock(interlkp);
    178 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    179 #ifdef DIAGNOSTIC
    180 	/*
    181 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    182 	 * exclusive lock is returned. The only valid operation thereafter
    183 	 * is a single release of that exclusive lock. This final release
    184 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    185 	 * further requests of any sort will result in a panic. The bits
    186 	 * selected for these two flags are chosen so that they will be set
    187 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    188 	 * The final release is permitted to give a new lease on life to
    189 	 * the lock by specifying LK_REENABLE.
    190 	 */
    191 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    192 		if (lkp->lk_flags & LK_DRAINED)
    193 			panic("lockmgr: using decommissioned lock");
    194 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    195 		    lkp->lk_lockholder != pid)
    196 			panic("lockmgr: non-release on draining lock: %d\n",
    197 			    flags & LK_TYPE_MASK);
    198 		lkp->lk_flags &= ~LK_DRAINING;
    199 		if ((flags & LK_REENABLE) == 0)
    200 			lkp->lk_flags |= LK_DRAINED;
    201 	}
    202 #endif DIAGNOSTIC
    203 
    204 	switch (flags & LK_TYPE_MASK) {
    205 
    206 	case LK_SHARED:
    207 		if (lkp->lk_lockholder != pid) {
    208 			/*
    209 			 * If just polling, check to see if we will block.
    210 			 */
    211 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    212 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    213 				error = EBUSY;
    214 				break;
    215 			}
    216 			/*
    217 			 * Wait for exclusive locks and upgrades to clear.
    218 			 */
    219 			ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    220 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    221 			if (error)
    222 				break;
    223 			lkp->lk_sharecount++;
    224 			COUNT(p, 1);
    225 			break;
    226 		}
    227 		/*
    228 		 * We hold an exclusive lock, so downgrade it to shared.
    229 		 * An alternative would be to fail with EDEADLK.
    230 		 */
    231 		lkp->lk_sharecount++;
    232 		COUNT(p, 1);
    233 		/* fall into downgrade */
    234 
    235 	case LK_DOWNGRADE:
    236 		if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
    237 			panic("lockmgr: not holding exclusive lock");
    238 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    239 		lkp->lk_exclusivecount = 0;
    240 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    241 		lkp->lk_lockholder = LK_NOPROC;
    242 		if (lkp->lk_waitcount)
    243 			wakeup((void *)lkp);
    244 		break;
    245 
    246 	case LK_EXCLUPGRADE:
    247 		/*
    248 		 * If another process is ahead of us to get an upgrade,
    249 		 * then we want to fail rather than have an intervening
    250 		 * exclusive access.
    251 		 */
    252 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    253 			lkp->lk_sharecount--;
    254 			COUNT(p, -1);
    255 			error = EBUSY;
    256 			break;
    257 		}
    258 		/* fall into normal upgrade */
    259 
    260 	case LK_UPGRADE:
    261 		/*
    262 		 * Upgrade a shared lock to an exclusive one. If another
    263 		 * shared lock has already requested an upgrade to an
    264 		 * exclusive lock, our shared lock is released and an
    265 		 * exclusive lock is requested (which will be granted
    266 		 * after the upgrade). If we return an error, the file
    267 		 * will always be unlocked.
    268 		 */
    269 		if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0)
    270 			panic("lockmgr: upgrade exclusive lock");
    271 		lkp->lk_sharecount--;
    272 		COUNT(p, -1);
    273 		/*
    274 		 * If we are just polling, check to see if we will block.
    275 		 */
    276 		if ((extflags & LK_NOWAIT) &&
    277 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    278 		     lkp->lk_sharecount > 1)) {
    279 			error = EBUSY;
    280 			break;
    281 		}
    282 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    283 			/*
    284 			 * We are first shared lock to request an upgrade, so
    285 			 * request upgrade and wait for the shared count to
    286 			 * drop to zero, then take exclusive lock.
    287 			 */
    288 			lkp->lk_flags |= LK_WANT_UPGRADE;
    289 			ACQUIRE(lkp, error, extflags, lkp->lk_sharecount);
    290 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    291 			if (error)
    292 				break;
    293 			lkp->lk_flags |= LK_HAVE_EXCL;
    294 			lkp->lk_lockholder = pid;
    295 			if (lkp->lk_exclusivecount != 0)
    296 				panic("lockmgr: non-zero exclusive count");
    297 			lkp->lk_exclusivecount = 1;
    298 			COUNT(p, 1);
    299 			break;
    300 		}
    301 		/*
    302 		 * Someone else has requested upgrade. Release our shared
    303 		 * lock, awaken upgrade requestor if we are the last shared
    304 		 * lock, then request an exclusive lock.
    305 		 */
    306 		if (lkp->lk_sharecount == 0 && lkp->lk_waitcount)
    307 			wakeup((void *)lkp);
    308 		/* fall into exclusive request */
    309 
    310 	case LK_EXCLUSIVE:
    311 		if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
    312 			/*
    313 			 *	Recursive lock.
    314 			 */
    315 			if ((extflags & LK_CANRECURSE) == 0)
    316 				panic("lockmgr: locking against myself");
    317 			lkp->lk_exclusivecount++;
    318 			COUNT(p, 1);
    319 			break;
    320 		}
    321 		/*
    322 		 * If we are just polling, check to see if we will sleep.
    323 		 */
    324 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    325 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    326 		     lkp->lk_sharecount != 0)) {
    327 			error = EBUSY;
    328 			break;
    329 		}
    330 		/*
    331 		 * Try to acquire the want_exclusive flag.
    332 		 */
    333 		ACQUIRE(lkp, error, extflags, lkp->lk_flags &
    334 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    335 		if (error)
    336 			break;
    337 		lkp->lk_flags |= LK_WANT_EXCL;
    338 		/*
    339 		 * Wait for shared locks and upgrades to finish.
    340 		 */
    341 		ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 ||
    342 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    343 		lkp->lk_flags &= ~LK_WANT_EXCL;
    344 		if (error)
    345 			break;
    346 		lkp->lk_flags |= LK_HAVE_EXCL;
    347 		lkp->lk_lockholder = pid;
    348 		if (lkp->lk_exclusivecount != 0)
    349 			panic("lockmgr: non-zero exclusive count");
    350 		lkp->lk_exclusivecount = 1;
    351 		COUNT(p, 1);
    352 		break;
    353 
    354 	case LK_RELEASE:
    355 		if (lkp->lk_exclusivecount != 0) {
    356 			if (pid != lkp->lk_lockholder)
    357 				panic("lockmgr: pid %d, not %s %d unlocking",
    358 				    pid, "exclusive lock holder",
    359 				    lkp->lk_lockholder);
    360 			lkp->lk_exclusivecount--;
    361 			COUNT(p, -1);
    362 			if (lkp->lk_exclusivecount == 0) {
    363 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    364 				lkp->lk_lockholder = LK_NOPROC;
    365 			}
    366 		} else if (lkp->lk_sharecount != 0) {
    367 			lkp->lk_sharecount--;
    368 			COUNT(p, -1);
    369 		}
    370 		if (lkp->lk_waitcount)
    371 			wakeup((void *)lkp);
    372 		break;
    373 
    374 	case LK_DRAIN:
    375 		/*
    376 		 * Check that we do not already hold the lock, as it can
    377 		 * never drain if we do. Unfortunately, we have no way to
    378 		 * check for holding a shared lock, but at least we can
    379 		 * check for an exclusive one.
    380 		 */
    381 		if (lkp->lk_lockholder == pid)
    382 			panic("lockmgr: draining against myself");
    383 		/*
    384 		 * If we are just polling, check to see if we will sleep.
    385 		 */
    386 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    387 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    388 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    389 			error = EBUSY;
    390 			break;
    391 		}
    392 		PAUSE(lkp, ((lkp->lk_flags &
    393 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    394 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0));
    395 		for (error = 0; ((lkp->lk_flags &
    396 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    397 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) {
    398 			lkp->lk_flags |= LK_WAITDRAIN;
    399 			simple_unlock(&lkp->lk_interlock);
    400 			if ((error = tsleep((void *)&lkp->lk_flags,
    401 			    lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo)))
    402 				return (error);
    403 			if ((extflags) & LK_SLEEPFAIL)
    404 				return (ENOLCK);
    405 			simple_lock(&lkp->lk_interlock);
    406 		}
    407 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    408 		lkp->lk_lockholder = pid;
    409 		lkp->lk_exclusivecount = 1;
    410 		COUNT(p, 1);
    411 		break;
    412 
    413 	default:
    414 		simple_unlock(&lkp->lk_interlock);
    415 		panic("lockmgr: unknown locktype request %d",
    416 		    flags & LK_TYPE_MASK);
    417 		/* NOTREACHED */
    418 	}
    419 	if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags &
    420 	     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    421 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    422 		lkp->lk_flags &= ~LK_WAITDRAIN;
    423 		wakeup((void *)&lkp->lk_flags);
    424 	}
    425 	simple_unlock(&lkp->lk_interlock);
    426 	return (error);
    427 }
    428 
    429 /*
    430  * Print out information about state of a lock. Used by VOP_PRINT
    431  * routines to display ststus about contained locks.
    432  */
    433 void
    434 lockmgr_printinfo(lkp)
    435 	struct lock *lkp;
    436 {
    437 
    438 	if (lkp->lk_sharecount)
    439 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    440 		    lkp->lk_sharecount);
    441 	else if (lkp->lk_flags & LK_HAVE_EXCL)
    442 		printf(" lock type %s: EXCL (count %d) by pid %d",
    443 		    lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
    444 	if (lkp->lk_waitcount > 0)
    445 		printf(" with %d pending", lkp->lk_waitcount);
    446 }
    447 
    448 #if defined(LOCKDEBUG) && NCPUS == 1
    449 #include <sys/kernel.h>
    450 #include <vm/vm.h>
    451 #include <sys/sysctl.h>
    452 int lockpausetime = 0;
    453 struct ctldebug debug2 = { "lockpausetime", &lockpausetime };
    454 int simplelockrecurse;
    455 /*
    456  * Simple lock functions so that the debugger can see from whence
    457  * they are being called.
    458  */
    459 void
    460 simple_lock_init(alp)
    461 	struct simplelock *alp;
    462 {
    463 	alp->lock_data = 0;
    464 	alp->lock_file = NULL;
    465 	alp->lock_line = 0;
    466 	alp->unlock_file = NULL;
    467 	alp->unlock_line = 0;
    468 }
    469 
    470 void
    471 _simple_lock(alp, id, l)
    472 	__volatile struct simplelock *alp;
    473 	const char *id;
    474 	int l;
    475 {
    476 	if (simplelockrecurse)
    477 		return;
    478 	if (alp->lock_data == 1) {
    479 		printf("simple_lock: lock held\n");
    480 		printf("currently at: %s:%d\n", id, l);
    481 		printf("last locked: %s:%d\n",
    482 		       alp->lock_file, alp->lock_line);
    483 		printf("last unlocked: %s:%d\n",
    484 		       alp->unlock_file, alp->unlock_line);
    485 		if (lockpausetime == -1)
    486 			panic("simple_lock: lock held");
    487 		if (lockpausetime == 1) {
    488 #ifdef BACKTRACE
    489 			BACKTRACE(curproc);
    490 #endif
    491 		} else if (lockpausetime > 1) {
    492 			printf("simple_lock: lock held, pausing...");
    493 			tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
    494 			    lockpausetime * hz);
    495 			printf(" continuing\n");
    496 		}
    497 	}
    498 	alp->lock_data = 1;
    499 	alp->lock_file = id;
    500 	alp->lock_line = l;
    501 	if (curproc)
    502 		curproc->p_simple_locks++;
    503 }
    504 
    505 int
    506 _simple_lock_try(alp, id, l)
    507 	__volatile struct simplelock *alp;
    508 	const char *id;
    509 	int l;
    510 {
    511 
    512 	if (alp->lock_data)
    513 		return (0);
    514 	if (simplelockrecurse)
    515 		return (1);
    516 	alp->lock_data = 1;
    517 	alp->lock_file = id;
    518 	alp->lock_line = l;
    519 	if (curproc)
    520 		curproc->p_simple_locks++;
    521 	return (1);
    522 }
    523 
    524 void
    525 _simple_unlock(alp, id, l)
    526 	__volatile struct simplelock *alp;
    527 	const char *id;
    528 	int l;
    529 {
    530 
    531 	if (simplelockrecurse)
    532 		return;
    533 	if (alp->lock_data == 0) {
    534 		printf("simple_unlock: lock not held\n");
    535 		printf("currently at: %s:%d\n", id, l);
    536 		printf("last locked: %s:%d\n",
    537 		       alp->lock_file, alp->lock_line);
    538 		printf("last unlocked: %s:%d\n",
    539 		       alp->unlock_file, alp->unlock_line);
    540 		if (lockpausetime == -1)
    541 			panic("simple_unlock: lock not held");
    542 		if (lockpausetime == 1) {
    543 #ifdef BACKTRACE
    544 			BACKTRACE(curproc);
    545 #endif
    546 		} else if (lockpausetime > 1) {
    547 			printf("simple_unlock: lock not held, pausing...");
    548 			tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
    549 			    lockpausetime * hz);
    550 			printf(" continuing\n");
    551 		}
    552 	}
    553 	alp->lock_data = 0;
    554 	alp->unlock_file = id;
    555 	alp->unlock_line = l;
    556 	if (curproc)
    557 		curproc->p_simple_locks--;
    558 }
    559 #endif /* LOCKDEBUG && NCPUS == 1 */
    560