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