Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.110.2.17
      1  1.110.2.17        ad /*	$NetBSD: kern_lock.c,v 1.110.2.17 2007/10/18 15:47:33 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.17        ad __KERNEL_RCSID(0, "$NetBSD: kern_lock.c,v 1.110.2.17 2007/10/18 15:47:33 ad Exp $");
     80         1.7   thorpej 
     81        1.21   thorpej #include "opt_multiprocessor.h"
     82       1.105        ad 
     83         1.1      fvdl #include <sys/param.h>
     84         1.1      fvdl #include <sys/proc.h>
     85         1.1      fvdl #include <sys/lock.h>
     86         1.2      fvdl #include <sys/systm.h>
     87       1.105        ad #include <sys/lockdebug.h>
     88  1.110.2.15        ad #include <sys/cpu.h>
     89  1.110.2.15        ad #include <sys/syslog.h>
     90       1.105        ad 
     91       1.110  christos #include <machine/stdarg.h>
     92         1.1      fvdl 
     93        1.98        ad #include <dev/lockstat.h>
     94        1.98        ad 
     95        1.25   thorpej /*
     96        1.25   thorpej  * note that stdarg.h and the ansi style va_start macro is used for both
     97        1.25   thorpej  * ansi and traditional c compiles.
     98        1.25   thorpej  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     99        1.25   thorpej  */
    100        1.36   thorpej void	lock_printf(const char *fmt, ...)
    101        1.37       eeh     __attribute__((__format__(__printf__,1,2)));
    102        1.25   thorpej 
    103  1.110.2.16        ad static int acquire(struct lock **, int *, int, int, int, uintptr_t);
    104        1.73      yamt 
    105        1.57  sommerfe int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    106  1.110.2.15        ad int	kernel_lock_id;
    107   1.110.2.5        ad __cpu_simple_lock_t kernel_lock;
    108   1.110.2.5        ad 
    109        1.21   thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    110   1.110.2.1        ad #define	COUNT(lkp, l, cpu_id, x)	(l)->l_locks += (x)
    111         1.1      fvdl #else
    112        1.22    mellon #define COUNT(lkp, p, cpu_id, x)
    113        1.21   thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    114         1.1      fvdl 
    115        1.98        ad #define	RETURN_ADDRESS		((uintptr_t)__builtin_return_address(0))
    116        1.98        ad 
    117         1.1      fvdl /*
    118         1.1      fvdl  * Acquire a resource.
    119         1.1      fvdl  */
    120        1.73      yamt static int
    121  1.110.2.16        ad acquire(struct lock **lkpp, int *s, int extflags,
    122  1.110.2.15        ad 	int drain, int wanted, uintptr_t ra)
    123        1.73      yamt {
    124        1.73      yamt 	int error;
    125  1.110.2.16        ad 	struct lock *lkp = *lkpp;
    126        1.98        ad 	LOCKSTAT_TIMER(slptime);
    127       1.105        ad 	LOCKSTAT_FLAG(lsflag);
    128        1.73      yamt 
    129        1.73      yamt 	KASSERT(drain || (wanted & LK_WAIT_NONZERO) == 0);
    130        1.73      yamt 
    131   1.110.2.1        ad 	LOCKSTAT_ENTER(lsflag);
    132        1.73      yamt 
    133   1.110.2.1        ad 	for (error = 0; (lkp->lk_flags & wanted) != 0; ) {
    134   1.110.2.1        ad 		if (drain)
    135   1.110.2.1        ad 			lkp->lk_flags |= LK_WAITDRAIN;
    136   1.110.2.1        ad 		else {
    137        1.73      yamt 			lkp->lk_waitcount++;
    138        1.73      yamt 			lkp->lk_flags |= LK_WAIT_NONZERO;
    139        1.73      yamt 		}
    140   1.110.2.1        ad 		LOCKSTAT_START_TIMER(lsflag, slptime);
    141  1.110.2.16        ad 		error = mtsleep(drain ? (void *)&lkp->lk_flags : (void *)lkp,
    142  1.110.2.16        ad 		    lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo,
    143   1.110.2.1        ad 		    __UNVOLATILE(&lkp->lk_interlock));
    144   1.110.2.1        ad 		LOCKSTAT_STOP_TIMER(lsflag, slptime);
    145   1.110.2.1        ad 		LOCKSTAT_EVENT_RA(lsflag, (void *)(uintptr_t)lkp,
    146   1.110.2.1        ad 		    LB_LOCKMGR | LB_SLEEP1, 1, slptime, ra);
    147        1.73      yamt 		if (!drain) {
    148        1.73      yamt 			lkp->lk_waitcount--;
    149        1.73      yamt 			if (lkp->lk_waitcount == 0)
    150        1.73      yamt 				lkp->lk_flags &= ~LK_WAIT_NONZERO;
    151        1.73      yamt 		}
    152   1.110.2.1        ad 		if (error)
    153   1.110.2.1        ad 			break;
    154   1.110.2.1        ad 		if (extflags & LK_SLEEPFAIL) {
    155   1.110.2.1        ad 			error = ENOLCK;
    156   1.110.2.1        ad 			break;
    157   1.110.2.1        ad 		}
    158         1.1      fvdl 	}
    159         1.1      fvdl 
    160   1.110.2.1        ad 	LOCKSTAT_EXIT(lsflag);
    161   1.110.2.1        ad 
    162        1.73      yamt 	return error;
    163        1.73      yamt }
    164        1.73      yamt 
    165        1.69   thorpej #define	SETHOLDER(lkp, pid, lid, cpu_id)				\
    166        1.19   thorpej do {									\
    167   1.110.2.1        ad 	(lkp)->lk_lockholder = pid;					\
    168   1.110.2.1        ad 	(lkp)->lk_locklwp = lid;					\
    169        1.30   thorpej } while (/*CONSTCOND*/0)
    170        1.19   thorpej 
    171        1.69   thorpej #define	WEHOLDIT(lkp, pid, lid, cpu_id)					\
    172   1.110.2.1        ad 	 ((lkp)->lk_lockholder == (pid) && (lkp)->lk_locklwp == (lid))
    173        1.19   thorpej 
    174        1.23   thorpej #define	WAKEUP_WAITER(lkp)						\
    175        1.23   thorpej do {									\
    176   1.110.2.1        ad 	if (((lkp)->lk_flags & LK_WAIT_NONZERO) != 0) {			\
    177        1.87  christos 		wakeup((lkp));						\
    178        1.23   thorpej 	}								\
    179        1.30   thorpej } while (/*CONSTCOND*/0)
    180        1.23   thorpej 
    181        1.25   thorpej #if defined(LOCKDEBUG)
    182        1.25   thorpej /*
    183        1.25   thorpej  * Lock debug printing routine; can be configured to print to console
    184        1.25   thorpej  * or log to syslog.
    185        1.25   thorpej  */
    186        1.25   thorpej void
    187        1.25   thorpej lock_printf(const char *fmt, ...)
    188        1.25   thorpej {
    189        1.68        pk 	char b[150];
    190        1.25   thorpej 	va_list ap;
    191        1.25   thorpej 
    192        1.25   thorpej 	va_start(ap, fmt);
    193        1.25   thorpej 	if (lock_debug_syslog)
    194        1.25   thorpej 		vlog(LOG_DEBUG, fmt, ap);
    195        1.68        pk 	else {
    196        1.68        pk 		vsnprintf(b, sizeof(b), fmt, ap);
    197        1.68        pk 		printf_nolog("%s", b);
    198        1.68        pk 	}
    199        1.25   thorpej 	va_end(ap);
    200        1.25   thorpej }
    201        1.25   thorpej #endif /* LOCKDEBUG */
    202        1.25   thorpej 
    203       1.110  christos static void
    204  1.110.2.16        ad lockpanic(struct lock *lkp, const char *fmt, ...)
    205       1.110  christos {
    206       1.110  christos 	char s[150], b[150];
    207       1.110  christos 	static const char *locktype[] = {
    208       1.110  christos 	    "*0*", "shared", "exclusive", "upgrade", "exclupgrade",
    209       1.110  christos 	    "downgrade", "release", "drain", "exclother", "*9*",
    210       1.110  christos 	    "*10*", "*11*", "*12*", "*13*", "*14*", "*15*"
    211       1.110  christos 	};
    212       1.110  christos 	va_list ap;
    213       1.110  christos 	va_start(ap, fmt);
    214       1.110  christos 	vsnprintf(s, sizeof(s), fmt, ap);
    215       1.110  christos 	va_end(ap);
    216       1.110  christos 	bitmask_snprintf(lkp->lk_flags, __LK_FLAG_BITS, b, sizeof(b));
    217       1.110  christos 	panic("%s ("
    218  1.110.2.14        ad 	    "type %s flags %s, sharecount %d, exclusivecount %d, "
    219       1.110  christos 	    "recurselevel %d, waitcount %d, wmesg %s"
    220  1.110.2.13        ad 	    ", lock_addr %p, unlock_addr %p"
    221       1.110  christos 	    ")\n",
    222  1.110.2.14        ad 	    s, locktype[lkp->lk_flags & LK_TYPE_MASK],
    223       1.110  christos 	    b, lkp->lk_sharecount, lkp->lk_exclusivecount,
    224  1.110.2.14        ad 	    lkp->lk_recurselevel, lkp->lk_waitcount, lkp->lk_wmesg,
    225  1.110.2.14        ad 	    (void *)lkp->lk_lock_addr, (void *)lkp->lk_unlock_addr
    226       1.110  christos 	);
    227       1.110  christos }
    228       1.110  christos 
    229         1.1      fvdl /*
    230         1.1      fvdl  * Initialize a lock; required before use.
    231         1.1      fvdl  */
    232         1.1      fvdl void
    233       1.109      yamt lockinit(struct lock *lkp, pri_t prio, const char *wmesg, int timo, int flags)
    234         1.1      fvdl {
    235         1.1      fvdl 
    236         1.8     perry 	memset(lkp, 0, sizeof(struct lock));
    237         1.1      fvdl 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    238   1.110.2.1        ad 	mutex_init(&lkp->lk_interlock, MUTEX_DEFAULT, IPL_NONE);
    239   1.110.2.1        ad 	lkp->lk_lockholder = LK_NOPROC;
    240   1.110.2.1        ad 	lkp->lk_prio = prio;
    241   1.110.2.1        ad 	lkp->lk_timo = timo;
    242   1.110.2.1        ad 	lkp->lk_wmesg = wmesg;
    243  1.110.2.13        ad 	lkp->lk_lock_addr = 0;
    244  1.110.2.13        ad 	lkp->lk_unlock_addr = 0;
    245         1.1      fvdl }
    246         1.1      fvdl 
    247   1.110.2.9        ad void
    248   1.110.2.9        ad lockdestroy(struct lock *lkp)
    249   1.110.2.9        ad {
    250   1.110.2.9        ad 
    251   1.110.2.9        ad 	mutex_destroy(&lkp->lk_interlock);
    252   1.110.2.9        ad }
    253   1.110.2.9        ad 
    254         1.1      fvdl /*
    255         1.1      fvdl  * Determine the status of a lock.
    256         1.1      fvdl  */
    257         1.1      fvdl int
    258        1.33   thorpej lockstatus(struct lock *lkp)
    259         1.1      fvdl {
    260        1.76      yamt 	int lock_type = 0;
    261        1.76      yamt 	struct lwp *l = curlwp; /* XXX */
    262        1.76      yamt 	pid_t pid;
    263        1.76      yamt 	lwpid_t lid;
    264        1.88     blymn 	cpuid_t cpu_num;
    265        1.76      yamt 
    266   1.110.2.1        ad 	if (l == NULL) {
    267        1.88     blymn 		cpu_num = cpu_number();
    268        1.76      yamt 		pid = LK_KERNPROC;
    269        1.76      yamt 		lid = 0;
    270        1.76      yamt 	} else {
    271        1.88     blymn 		cpu_num = LK_NOCPU;
    272        1.76      yamt 		pid = l->l_proc->p_pid;
    273        1.76      yamt 		lid = l->l_lid;
    274        1.76      yamt 	}
    275         1.1      fvdl 
    276   1.110.2.1        ad 	mutex_enter(&lkp->lk_interlock);
    277        1.76      yamt 	if (lkp->lk_exclusivecount != 0) {
    278        1.88     blymn 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    279        1.76      yamt 			lock_type = LK_EXCLUSIVE;
    280        1.76      yamt 		else
    281        1.76      yamt 			lock_type = LK_EXCLOTHER;
    282        1.76      yamt 	} else if (lkp->lk_sharecount != 0)
    283         1.1      fvdl 		lock_type = LK_SHARED;
    284       1.103       chs 	else if (lkp->lk_flags & (LK_WANT_EXCL | LK_WANT_UPGRADE))
    285       1.103       chs 		lock_type = LK_EXCLOTHER;
    286  1.110.2.16        ad 	mutex_exit(&lkp->lk_interlock);
    287         1.1      fvdl 	return (lock_type);
    288         1.1      fvdl }
    289        1.35   thorpej 
    290         1.1      fvdl /*
    291        1.32  sommerfe  * XXX XXX kludge around another kludge..
    292        1.32  sommerfe  *
    293        1.32  sommerfe  * vfs_shutdown() may be called from interrupt context, either as a result
    294        1.32  sommerfe  * of a panic, or from the debugger.   It proceeds to call
    295        1.32  sommerfe  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    296        1.32  sommerfe  *
    297        1.32  sommerfe  * We would like to make an attempt to sync the filesystems in this case, so
    298        1.32  sommerfe  * if this happens, we treat attempts to acquire locks specially.
    299        1.32  sommerfe  * All locks are acquired on behalf of proc0.
    300        1.32  sommerfe  *
    301        1.32  sommerfe  * If we've already paniced, we don't block waiting for locks, but
    302        1.32  sommerfe  * just barge right ahead since we're already going down in flames.
    303        1.32  sommerfe  */
    304        1.32  sommerfe 
    305        1.32  sommerfe /*
    306         1.1      fvdl  * Set, change, or release a lock.
    307         1.1      fvdl  *
    308         1.1      fvdl  * Shared requests increment the shared count. Exclusive requests set the
    309         1.1      fvdl  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    310         1.1      fvdl  * accepted shared locks and shared-to-exclusive upgrades to go away.
    311         1.1      fvdl  */
    312         1.1      fvdl int
    313  1.110.2.16        ad lockmgr(struct lock *lkp, u_int flags, kmutex_t *interlkp)
    314         1.1      fvdl {
    315         1.1      fvdl 	int error;
    316         1.1      fvdl 	pid_t pid;
    317        1.69   thorpej 	lwpid_t lid;
    318         1.1      fvdl 	int extflags;
    319        1.88     blymn 	cpuid_t cpu_num;
    320        1.69   thorpej 	struct lwp *l = curlwp;
    321        1.32  sommerfe 	int lock_shutdown_noblock = 0;
    322        1.67       scw 	int s = 0;
    323         1.1      fvdl 
    324         1.1      fvdl 	error = 0;
    325        1.19   thorpej 
    326        1.80      yamt 	/* LK_RETRY is for vn_lock, not for lockmgr. */
    327        1.79      yamt 	KASSERT((flags & LK_RETRY) == 0);
    328  1.110.2.17        ad 	KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
    329        1.79      yamt 
    330  1.110.2.16        ad 	mutex_enter(&lkp->lk_interlock);
    331         1.1      fvdl 	if (flags & LK_INTERLOCK)
    332  1.110.2.16        ad 		mutex_exit(interlkp);
    333         1.1      fvdl 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    334        1.19   thorpej 
    335   1.110.2.1        ad 	if (l == NULL) {
    336   1.110.2.1        ad 		if (!doing_shutdown) {
    337   1.110.2.1        ad 			panic("lockmgr: no context");
    338   1.110.2.1        ad 		} else {
    339   1.110.2.1        ad 			l = &lwp0;
    340   1.110.2.1        ad 			if (panicstr && (!(flags & LK_NOWAIT))) {
    341   1.110.2.1        ad 				flags |= LK_NOWAIT;
    342   1.110.2.1        ad 				lock_shutdown_noblock = 1;
    343        1.32  sommerfe 			}
    344        1.32  sommerfe 		}
    345        1.19   thorpej 	}
    346   1.110.2.1        ad 	lid = l->l_lid;
    347   1.110.2.1        ad 	pid = l->l_proc->p_pid;
    348        1.88     blymn 	cpu_num = cpu_number();
    349        1.19   thorpej 
    350         1.1      fvdl 	/*
    351         1.1      fvdl 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    352         1.1      fvdl 	 * exclusive lock is returned. The only valid operation thereafter
    353         1.1      fvdl 	 * is a single release of that exclusive lock. This final release
    354         1.1      fvdl 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    355         1.1      fvdl 	 * further requests of any sort will result in a panic. The bits
    356         1.1      fvdl 	 * selected for these two flags are chosen so that they will be set
    357         1.1      fvdl 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    358         1.1      fvdl 	 * The final release is permitted to give a new lease on life to
    359         1.1      fvdl 	 * the lock by specifying LK_REENABLE.
    360         1.1      fvdl 	 */
    361         1.1      fvdl 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    362        1.28   thorpej #ifdef DIAGNOSTIC /* { */
    363         1.1      fvdl 		if (lkp->lk_flags & LK_DRAINED)
    364       1.110  christos 			lockpanic(lkp, "lockmgr: using decommissioned lock");
    365         1.1      fvdl 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    366        1.88     blymn 		    WEHOLDIT(lkp, pid, lid, cpu_num) == 0)
    367       1.110  christos 			lockpanic(lkp, "lockmgr: non-release on draining lock: %d",
    368         1.1      fvdl 			    flags & LK_TYPE_MASK);
    369        1.28   thorpej #endif /* DIAGNOSTIC */ /* } */
    370         1.1      fvdl 		lkp->lk_flags &= ~LK_DRAINING;
    371         1.1      fvdl 		if ((flags & LK_REENABLE) == 0)
    372         1.1      fvdl 			lkp->lk_flags |= LK_DRAINED;
    373         1.1      fvdl 	}
    374         1.1      fvdl 
    375         1.1      fvdl 	switch (flags & LK_TYPE_MASK) {
    376         1.1      fvdl 
    377         1.1      fvdl 	case LK_SHARED:
    378        1.88     blymn 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    379         1.1      fvdl 			/*
    380         1.1      fvdl 			 * If just polling, check to see if we will block.
    381         1.1      fvdl 			 */
    382         1.1      fvdl 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    383         1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    384         1.1      fvdl 				error = EBUSY;
    385         1.1      fvdl 				break;
    386         1.1      fvdl 			}
    387         1.1      fvdl 			/*
    388         1.1      fvdl 			 * Wait for exclusive locks and upgrades to clear.
    389         1.1      fvdl 			 */
    390        1.78   hannken 			error = acquire(&lkp, &s, extflags, 0,
    391        1.98        ad 			    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE,
    392        1.98        ad 			    RETURN_ADDRESS);
    393         1.1      fvdl 			if (error)
    394         1.1      fvdl 				break;
    395         1.1      fvdl 			lkp->lk_sharecount++;
    396        1.73      yamt 			lkp->lk_flags |= LK_SHARE_NONZERO;
    397        1.88     blymn 			COUNT(lkp, l, cpu_num, 1);
    398         1.1      fvdl 			break;
    399         1.1      fvdl 		}
    400         1.1      fvdl 		/*
    401         1.1      fvdl 		 * We hold an exclusive lock, so downgrade it to shared.
    402         1.1      fvdl 		 * An alternative would be to fail with EDEADLK.
    403         1.1      fvdl 		 */
    404         1.1      fvdl 		lkp->lk_sharecount++;
    405        1.73      yamt 		lkp->lk_flags |= LK_SHARE_NONZERO;
    406        1.88     blymn 		COUNT(lkp, l, cpu_num, 1);
    407         1.1      fvdl 		/* fall into downgrade */
    408         1.1      fvdl 
    409         1.1      fvdl 	case LK_DOWNGRADE:
    410        1.88     blymn 		if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0 ||
    411        1.19   thorpej 		    lkp->lk_exclusivecount == 0)
    412       1.110  christos 			lockpanic(lkp, "lockmgr: not holding exclusive lock");
    413         1.1      fvdl 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    414        1.73      yamt 		lkp->lk_flags |= LK_SHARE_NONZERO;
    415         1.1      fvdl 		lkp->lk_exclusivecount = 0;
    416        1.15      fvdl 		lkp->lk_recurselevel = 0;
    417         1.1      fvdl 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    418        1.69   thorpej 		SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    419        1.50   thorpej #if defined(LOCKDEBUG)
    420  1.110.2.13        ad 		lkp->lk_unlock_addr = RETURN_ADDRESS;
    421        1.50   thorpej #endif
    422        1.23   thorpej 		WAKEUP_WAITER(lkp);
    423         1.1      fvdl 		break;
    424         1.1      fvdl 
    425         1.1      fvdl 	case LK_EXCLUPGRADE:
    426         1.1      fvdl 		/*
    427         1.1      fvdl 		 * If another process is ahead of us to get an upgrade,
    428         1.1      fvdl 		 * then we want to fail rather than have an intervening
    429         1.1      fvdl 		 * exclusive access.
    430         1.1      fvdl 		 */
    431         1.1      fvdl 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    432         1.1      fvdl 			lkp->lk_sharecount--;
    433        1.73      yamt 			if (lkp->lk_sharecount == 0)
    434        1.73      yamt 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    435        1.88     blymn 			COUNT(lkp, l, cpu_num, -1);
    436         1.1      fvdl 			error = EBUSY;
    437         1.1      fvdl 			break;
    438         1.1      fvdl 		}
    439         1.1      fvdl 		/* fall into normal upgrade */
    440         1.1      fvdl 
    441         1.1      fvdl 	case LK_UPGRADE:
    442         1.1      fvdl 		/*
    443         1.1      fvdl 		 * Upgrade a shared lock to an exclusive one. If another
    444         1.1      fvdl 		 * shared lock has already requested an upgrade to an
    445         1.1      fvdl 		 * exclusive lock, our shared lock is released and an
    446         1.1      fvdl 		 * exclusive lock is requested (which will be granted
    447         1.1      fvdl 		 * after the upgrade). If we return an error, the file
    448         1.1      fvdl 		 * will always be unlocked.
    449         1.1      fvdl 		 */
    450        1.88     blymn 		if (WEHOLDIT(lkp, pid, lid, cpu_num) || lkp->lk_sharecount <= 0)
    451       1.110  christos 			lockpanic(lkp, "lockmgr: upgrade exclusive lock");
    452         1.1      fvdl 		lkp->lk_sharecount--;
    453        1.73      yamt 		if (lkp->lk_sharecount == 0)
    454        1.73      yamt 			lkp->lk_flags &= ~LK_SHARE_NONZERO;
    455        1.88     blymn 		COUNT(lkp, l, cpu_num, -1);
    456         1.1      fvdl 		/*
    457         1.1      fvdl 		 * If we are just polling, check to see if we will block.
    458         1.1      fvdl 		 */
    459         1.1      fvdl 		if ((extflags & LK_NOWAIT) &&
    460         1.1      fvdl 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    461         1.1      fvdl 		     lkp->lk_sharecount > 1)) {
    462         1.1      fvdl 			error = EBUSY;
    463         1.1      fvdl 			break;
    464         1.1      fvdl 		}
    465         1.1      fvdl 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    466         1.1      fvdl 			/*
    467         1.1      fvdl 			 * We are first shared lock to request an upgrade, so
    468         1.1      fvdl 			 * request upgrade and wait for the shared count to
    469         1.1      fvdl 			 * drop to zero, then take exclusive lock.
    470         1.1      fvdl 			 */
    471         1.1      fvdl 			lkp->lk_flags |= LK_WANT_UPGRADE;
    472        1.98        ad 			error = acquire(&lkp, &s, extflags, 0, LK_SHARE_NONZERO,
    473        1.98        ad 			    RETURN_ADDRESS);
    474         1.1      fvdl 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    475        1.83      yamt 			if (error) {
    476        1.83      yamt 				WAKEUP_WAITER(lkp);
    477         1.1      fvdl 				break;
    478        1.83      yamt 			}
    479         1.1      fvdl 			lkp->lk_flags |= LK_HAVE_EXCL;
    480        1.88     blymn 			SETHOLDER(lkp, pid, lid, cpu_num);
    481        1.50   thorpej #if defined(LOCKDEBUG)
    482  1.110.2.13        ad 			lkp->lk_lock_addr = RETURN_ADDRESS;
    483        1.50   thorpej #endif
    484         1.1      fvdl 			if (lkp->lk_exclusivecount != 0)
    485       1.110  christos 				lockpanic(lkp, "lockmgr: non-zero exclusive count");
    486         1.1      fvdl 			lkp->lk_exclusivecount = 1;
    487        1.15      fvdl 			if (extflags & LK_SETRECURSE)
    488        1.15      fvdl 				lkp->lk_recurselevel = 1;
    489        1.88     blymn 			COUNT(lkp, l, cpu_num, 1);
    490         1.1      fvdl 			break;
    491         1.1      fvdl 		}
    492         1.1      fvdl 		/*
    493         1.1      fvdl 		 * Someone else has requested upgrade. Release our shared
    494         1.1      fvdl 		 * lock, awaken upgrade requestor if we are the last shared
    495         1.1      fvdl 		 * lock, then request an exclusive lock.
    496         1.1      fvdl 		 */
    497        1.23   thorpej 		if (lkp->lk_sharecount == 0)
    498        1.23   thorpej 			WAKEUP_WAITER(lkp);
    499         1.1      fvdl 		/* fall into exclusive request */
    500         1.1      fvdl 
    501         1.1      fvdl 	case LK_EXCLUSIVE:
    502        1.88     blymn 		if (WEHOLDIT(lkp, pid, lid, cpu_num)) {
    503         1.1      fvdl 			/*
    504        1.19   thorpej 			 * Recursive lock.
    505         1.1      fvdl 			 */
    506        1.15      fvdl 			if ((extflags & LK_CANRECURSE) == 0 &&
    507        1.16  sommerfe 			     lkp->lk_recurselevel == 0) {
    508        1.16  sommerfe 				if (extflags & LK_RECURSEFAIL) {
    509        1.16  sommerfe 					error = EDEADLK;
    510        1.16  sommerfe 					break;
    511        1.16  sommerfe 				} else
    512       1.110  christos 					lockpanic(lkp, "lockmgr: locking against myself");
    513        1.16  sommerfe 			}
    514         1.1      fvdl 			lkp->lk_exclusivecount++;
    515        1.15      fvdl 			if (extflags & LK_SETRECURSE &&
    516        1.15      fvdl 			    lkp->lk_recurselevel == 0)
    517        1.15      fvdl 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    518        1.88     blymn 			COUNT(lkp, l, cpu_num, 1);
    519         1.1      fvdl 			break;
    520         1.1      fvdl 		}
    521         1.1      fvdl 		/*
    522         1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    523         1.1      fvdl 		 */
    524        1.73      yamt 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    525        1.73      yamt 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    526        1.73      yamt 		     LK_SHARE_NONZERO))) {
    527         1.1      fvdl 			error = EBUSY;
    528         1.1      fvdl 			break;
    529         1.1      fvdl 		}
    530         1.1      fvdl 		/*
    531         1.1      fvdl 		 * Try to acquire the want_exclusive flag.
    532         1.1      fvdl 		 */
    533        1.82      yamt 		error = acquire(&lkp, &s, extflags, 0,
    534        1.98        ad 		    LK_HAVE_EXCL | LK_WANT_EXCL, RETURN_ADDRESS);
    535         1.1      fvdl 		if (error)
    536         1.1      fvdl 			break;
    537         1.1      fvdl 		lkp->lk_flags |= LK_WANT_EXCL;
    538         1.1      fvdl 		/*
    539         1.1      fvdl 		 * Wait for shared locks and upgrades to finish.
    540         1.1      fvdl 		 */
    541        1.78   hannken 		error = acquire(&lkp, &s, extflags, 0,
    542        1.98        ad 		    LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO,
    543        1.98        ad 		    RETURN_ADDRESS);
    544         1.1      fvdl 		lkp->lk_flags &= ~LK_WANT_EXCL;
    545        1.83      yamt 		if (error) {
    546        1.83      yamt 			WAKEUP_WAITER(lkp);
    547         1.1      fvdl 			break;
    548        1.83      yamt 		}
    549         1.1      fvdl 		lkp->lk_flags |= LK_HAVE_EXCL;
    550        1.88     blymn 		SETHOLDER(lkp, pid, lid, cpu_num);
    551        1.50   thorpej #if defined(LOCKDEBUG)
    552  1.110.2.13        ad 		lkp->lk_lock_addr = RETURN_ADDRESS;
    553        1.50   thorpej #endif
    554         1.1      fvdl 		if (lkp->lk_exclusivecount != 0)
    555       1.110  christos 			lockpanic(lkp, "lockmgr: non-zero exclusive count");
    556         1.1      fvdl 		lkp->lk_exclusivecount = 1;
    557        1.15      fvdl 		if (extflags & LK_SETRECURSE)
    558        1.15      fvdl 			lkp->lk_recurselevel = 1;
    559        1.88     blymn 		COUNT(lkp, l, cpu_num, 1);
    560         1.1      fvdl 		break;
    561         1.1      fvdl 
    562         1.1      fvdl 	case LK_RELEASE:
    563         1.1      fvdl 		if (lkp->lk_exclusivecount != 0) {
    564        1.88     blymn 			if (WEHOLDIT(lkp, pid, lid, cpu_num) == 0) {
    565   1.110.2.6        ad 				lockpanic(lkp, "lockmgr: pid %d.%d, not "
    566   1.110.2.6        ad 				    "exclusive lock holder %d.%d "
    567   1.110.2.6        ad 				    "unlocking", pid, lid,
    568   1.110.2.6        ad 				    lkp->lk_lockholder,
    569   1.110.2.6        ad 				    lkp->lk_locklwp);
    570        1.19   thorpej 			}
    571        1.15      fvdl 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    572        1.15      fvdl 				lkp->lk_recurselevel = 0;
    573         1.1      fvdl 			lkp->lk_exclusivecount--;
    574        1.88     blymn 			COUNT(lkp, l, cpu_num, -1);
    575         1.1      fvdl 			if (lkp->lk_exclusivecount == 0) {
    576         1.1      fvdl 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    577        1.69   thorpej 				SETHOLDER(lkp, LK_NOPROC, 0, LK_NOCPU);
    578        1.50   thorpej #if defined(LOCKDEBUG)
    579  1.110.2.13        ad 				lkp->lk_unlock_addr = RETURN_ADDRESS;
    580        1.50   thorpej #endif
    581         1.1      fvdl 			}
    582         1.1      fvdl 		} else if (lkp->lk_sharecount != 0) {
    583         1.1      fvdl 			lkp->lk_sharecount--;
    584        1.73      yamt 			if (lkp->lk_sharecount == 0)
    585        1.73      yamt 				lkp->lk_flags &= ~LK_SHARE_NONZERO;
    586        1.88     blymn 			COUNT(lkp, l, cpu_num, -1);
    587         1.1      fvdl 		}
    588        1.39   thorpej #ifdef DIAGNOSTIC
    589        1.39   thorpej 		else
    590       1.110  christos 			lockpanic(lkp, "lockmgr: release of unlocked lock!");
    591        1.39   thorpej #endif
    592        1.23   thorpej 		WAKEUP_WAITER(lkp);
    593         1.1      fvdl 		break;
    594         1.1      fvdl 
    595         1.1      fvdl 	case LK_DRAIN:
    596         1.1      fvdl 		/*
    597        1.86     perry 		 * Check that we do not already hold the lock, as it can
    598         1.1      fvdl 		 * never drain if we do. Unfortunately, we have no way to
    599         1.1      fvdl 		 * check for holding a shared lock, but at least we can
    600         1.1      fvdl 		 * check for an exclusive one.
    601         1.1      fvdl 		 */
    602        1.88     blymn 		if (WEHOLDIT(lkp, pid, lid, cpu_num))
    603       1.110  christos 			lockpanic(lkp, "lockmgr: draining against myself");
    604         1.1      fvdl 		/*
    605         1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    606         1.1      fvdl 		 */
    607        1.73      yamt 		if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    608        1.73      yamt 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    609        1.73      yamt 		     LK_SHARE_NONZERO | LK_WAIT_NONZERO))) {
    610         1.1      fvdl 			error = EBUSY;
    611         1.1      fvdl 			break;
    612         1.1      fvdl 		}
    613        1.78   hannken 		error = acquire(&lkp, &s, extflags, 1,
    614        1.73      yamt 		    LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    615        1.98        ad 		    LK_SHARE_NONZERO | LK_WAIT_NONZERO,
    616        1.98        ad 		    RETURN_ADDRESS);
    617        1.23   thorpej 		if (error)
    618        1.23   thorpej 			break;
    619  1.110.2.10        ad 		lkp->lk_flags |= LK_HAVE_EXCL;
    620  1.110.2.10        ad 		if ((extflags & LK_RESURRECT) == 0)
    621  1.110.2.10        ad 			lkp->lk_flags |= LK_DRAINING;
    622        1.88     blymn 		SETHOLDER(lkp, pid, lid, cpu_num);
    623        1.50   thorpej #if defined(LOCKDEBUG)
    624  1.110.2.13        ad 		lkp->lk_lock_addr = RETURN_ADDRESS;
    625        1.50   thorpej #endif
    626         1.1      fvdl 		lkp->lk_exclusivecount = 1;
    627        1.15      fvdl 		/* XXX unlikely that we'd want this */
    628        1.15      fvdl 		if (extflags & LK_SETRECURSE)
    629        1.15      fvdl 			lkp->lk_recurselevel = 1;
    630        1.88     blymn 		COUNT(lkp, l, cpu_num, 1);
    631         1.1      fvdl 		break;
    632         1.1      fvdl 
    633         1.1      fvdl 	default:
    634  1.110.2.16        ad 		mutex_exit(&lkp->lk_interlock);
    635       1.110  christos 		lockpanic(lkp, "lockmgr: unknown locktype request %d",
    636         1.1      fvdl 		    flags & LK_TYPE_MASK);
    637         1.1      fvdl 		/* NOTREACHED */
    638         1.1      fvdl 	}
    639   1.110.2.1        ad 	if ((lkp->lk_flags & LK_WAITDRAIN) != 0 &&
    640        1.23   thorpej 	    ((lkp->lk_flags &
    641        1.73      yamt 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
    642        1.73      yamt 	      LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0)) {
    643         1.1      fvdl 		lkp->lk_flags &= ~LK_WAITDRAIN;
    644        1.87  christos 		wakeup(&lkp->lk_flags);
    645         1.1      fvdl 	}
    646        1.32  sommerfe 	/*
    647        1.32  sommerfe 	 * Note that this panic will be a recursive panic, since
    648        1.32  sommerfe 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    649        1.32  sommerfe 	 */
    650        1.32  sommerfe 	if (error && lock_shutdown_noblock)
    651       1.110  christos 		lockpanic(lkp, "lockmgr: deadlock (see previous panic)");
    652        1.86     perry 
    653  1.110.2.16        ad 	mutex_exit(&lkp->lk_interlock);
    654         1.1      fvdl 	return (error);
    655         1.1      fvdl }
    656         1.1      fvdl 
    657         1.1      fvdl /*
    658         1.1      fvdl  * Print out information about state of a lock. Used by VOP_PRINT
    659         1.1      fvdl  * routines to display ststus about contained locks.
    660         1.1      fvdl  */
    661         1.2      fvdl void
    662  1.110.2.16        ad lockmgr_printinfo(struct lock *lkp)
    663         1.1      fvdl {
    664         1.1      fvdl 
    665         1.1      fvdl 	if (lkp->lk_sharecount)
    666         1.1      fvdl 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    667         1.1      fvdl 		    lkp->lk_sharecount);
    668        1.19   thorpej 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    669        1.19   thorpej 		printf(" lock type %s: EXCL (count %d) by ",
    670        1.19   thorpej 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    671   1.110.2.1        ad 		printf("pid %d.%d", lkp->lk_lockholder,
    672   1.110.2.1        ad 		    lkp->lk_locklwp);
    673        1.19   thorpej 	} else
    674        1.19   thorpej 		printf(" not locked");
    675   1.110.2.1        ad 	if (lkp->lk_waitcount > 0)
    676         1.1      fvdl 		printf(" with %d pending", lkp->lk_waitcount);
    677         1.1      fvdl }
    678         1.1      fvdl 
    679   1.110.2.3        ad #if defined(LOCKDEBUG)
    680        1.96      yamt void
    681        1.96      yamt assert_sleepable(struct simplelock *interlock, const char *msg)
    682        1.96      yamt {
    683        1.96      yamt 
    684  1.110.2.10        ad 	if (panicstr != NULL)
    685  1.110.2.10        ad 		return;
    686   1.110.2.4        ad 	LOCKDEBUG_BARRIER(&kernel_lock, 1);
    687   1.110.2.6        ad 	if (CURCPU_IDLE_P()) {
    688   1.110.2.6        ad 		panic("assert_sleepable: idle");
    689        1.97      yamt 	}
    690        1.96      yamt }
    691   1.110.2.3        ad #endif
    692        1.96      yamt 
    693        1.62   thorpej /*
    694        1.62   thorpej  * Functions for manipulating the kernel_lock.  We put them here
    695        1.62   thorpej  * so that they show up in profiles.
    696        1.62   thorpej  */
    697        1.62   thorpej 
    698       1.105        ad #define	_KERNEL_LOCK_ABORT(msg)						\
    699       1.105        ad     LOCKDEBUG_ABORT(kernel_lock_id, &kernel_lock, &_kernel_lock_ops,	\
    700  1.110.2.12        ad         __func__, msg)
    701       1.105        ad 
    702       1.105        ad #ifdef LOCKDEBUG
    703       1.105        ad #define	_KERNEL_LOCK_ASSERT(cond)					\
    704       1.105        ad do {									\
    705       1.105        ad 	if (!(cond))							\
    706       1.105        ad 		_KERNEL_LOCK_ABORT("assertion failed: " #cond);		\
    707       1.105        ad } while (/* CONSTCOND */ 0)
    708       1.105        ad #else
    709       1.105        ad #define	_KERNEL_LOCK_ASSERT(cond)	/* nothing */
    710       1.105        ad #endif
    711       1.105        ad 
    712       1.105        ad void	_kernel_lock_dump(volatile void *);
    713       1.105        ad 
    714       1.105        ad lockops_t _kernel_lock_ops = {
    715       1.105        ad 	"Kernel lock",
    716       1.105        ad 	0,
    717       1.105        ad 	_kernel_lock_dump
    718       1.105        ad };
    719       1.105        ad 
    720        1.85      yamt /*
    721       1.105        ad  * Initialize the kernel lock.
    722        1.85      yamt  */
    723        1.62   thorpej void
    724  1.110.2.15        ad kernel_lock_init(void)
    725        1.62   thorpej {
    726        1.62   thorpej 
    727       1.105        ad 	__cpu_simple_lock_init(&kernel_lock);
    728  1.110.2.10        ad 	kernel_lock_id = LOCKDEBUG_ALLOC(&kernel_lock, &_kernel_lock_ops,
    729  1.110.2.13        ad 	    RETURN_ADDRESS);
    730        1.62   thorpej }
    731        1.62   thorpej 
    732        1.62   thorpej /*
    733       1.105        ad  * Print debugging information about the kernel lock.
    734        1.62   thorpej  */
    735        1.62   thorpej void
    736       1.105        ad _kernel_lock_dump(volatile void *junk)
    737        1.62   thorpej {
    738        1.85      yamt 	struct cpu_info *ci = curcpu();
    739        1.62   thorpej 
    740       1.105        ad 	(void)junk;
    741        1.85      yamt 
    742       1.105        ad 	printf_nolog("curcpu holds : %18d wanted by: %#018lx\n",
    743       1.105        ad 	    ci->ci_biglock_count, (long)ci->ci_biglock_wanted);
    744        1.62   thorpej }
    745        1.62   thorpej 
    746       1.105        ad /*
    747       1.105        ad  * Acquire 'nlocks' holds on the kernel lock.  If 'l' is non-null, the
    748       1.105        ad  * acquisition is from process context.
    749       1.105        ad  */
    750        1.62   thorpej void
    751       1.105        ad _kernel_lock(int nlocks, struct lwp *l)
    752        1.62   thorpej {
    753        1.85      yamt 	struct cpu_info *ci = curcpu();
    754       1.105        ad 	LOCKSTAT_TIMER(spintime);
    755       1.105        ad 	LOCKSTAT_FLAG(lsflag);
    756       1.105        ad 	struct lwp *owant;
    757       1.105        ad #ifdef LOCKDEBUG
    758       1.105        ad 	u_int spins;
    759       1.105        ad #endif
    760        1.85      yamt 	int s;
    761        1.85      yamt 
    762       1.105        ad 	if (nlocks == 0)
    763       1.105        ad 		return;
    764       1.105        ad 	_KERNEL_LOCK_ASSERT(nlocks > 0);
    765        1.62   thorpej 
    766   1.110.2.7        ad 	l = curlwp;
    767       1.105        ad 
    768       1.105        ad 	if (ci->ci_biglock_count != 0) {
    769  1.110.2.12        ad 		_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(&kernel_lock));
    770       1.105        ad 		ci->ci_biglock_count += nlocks;
    771   1.110.2.7        ad 		l->l_blcnt += nlocks;
    772       1.105        ad 		return;
    773       1.105        ad 	}
    774       1.105        ad 
    775   1.110.2.7        ad 	_KERNEL_LOCK_ASSERT(l->l_blcnt == 0);
    776  1.110.2.13        ad 	LOCKDEBUG_WANTLOCK(kernel_lock_id, RETURN_ADDRESS, 0);
    777       1.107        ad 
    778  1.110.2.15        ad 	s = splvm();
    779       1.105        ad 	if (__cpu_simple_lock_try(&kernel_lock)) {
    780       1.105        ad 		ci->ci_biglock_count = nlocks;
    781   1.110.2.7        ad 		l->l_blcnt = nlocks;
    782  1.110.2.13        ad 		LOCKDEBUG_LOCKED(kernel_lock_id, RETURN_ADDRESS, 0);
    783       1.105        ad 		splx(s);
    784       1.105        ad 		return;
    785       1.105        ad 	}
    786       1.105        ad 
    787       1.105        ad 	LOCKSTAT_ENTER(lsflag);
    788       1.105        ad 	LOCKSTAT_START_TIMER(lsflag, spintime);
    789       1.105        ad 
    790       1.105        ad 	/*
    791       1.105        ad 	 * Before setting ci_biglock_wanted we must post a store
    792       1.105        ad 	 * fence (see kern_mutex.c).  This is accomplished by the
    793       1.105        ad 	 * __cpu_simple_lock_try() above.
    794       1.105        ad 	 */
    795       1.105        ad 	owant = ci->ci_biglock_wanted;
    796       1.105        ad 	ci->ci_biglock_wanted = curlwp;	/* XXXAD */
    797       1.105        ad 
    798       1.105        ad #ifdef LOCKDEBUG
    799       1.105        ad 	spins = 0;
    800       1.105        ad #endif
    801       1.105        ad 
    802       1.105        ad 	do {
    803  1.110.2.11        ad 		splx(s);
    804       1.105        ad 		while (kernel_lock == __SIMPLELOCK_LOCKED) {
    805       1.105        ad #ifdef LOCKDEBUG
    806       1.105        ad 			if (SPINLOCK_SPINOUT(spins))
    807       1.105        ad 				_KERNEL_LOCK_ABORT("spinout");
    808       1.105        ad #endif
    809  1.110.2.11        ad 			SPINLOCK_BACKOFF_HOOK;
    810       1.105        ad 			SPINLOCK_SPIN_HOOK;
    811       1.105        ad 		}
    812  1.110.2.15        ad 		(void)splvm();
    813       1.105        ad 	} while (!__cpu_simple_lock_try(&kernel_lock));
    814       1.105        ad 
    815       1.105        ad 	ci->ci_biglock_wanted = owant;
    816   1.110.2.7        ad 	ci->ci_biglock_count = nlocks;
    817   1.110.2.7        ad 	l->l_blcnt = nlocks;
    818       1.107        ad 	LOCKSTAT_STOP_TIMER(lsflag, spintime);
    819  1.110.2.13        ad 	LOCKDEBUG_LOCKED(kernel_lock_id, RETURN_ADDRESS, 0);
    820        1.85      yamt 	splx(s);
    821       1.105        ad 
    822       1.105        ad 	/*
    823       1.105        ad 	 * Again, another store fence is required (see kern_mutex.c).
    824       1.105        ad 	 */
    825       1.105        ad 	mb_write();
    826       1.107        ad 	if (owant == NULL) {
    827       1.107        ad 		LOCKSTAT_EVENT(lsflag, &kernel_lock, LB_KERNEL_LOCK | LB_SPIN,
    828       1.107        ad 		    1, spintime);
    829       1.107        ad 	}
    830       1.105        ad 	LOCKSTAT_EXIT(lsflag);
    831        1.62   thorpej }
    832        1.62   thorpej 
    833        1.62   thorpej /*
    834       1.105        ad  * Release 'nlocks' holds on the kernel lock.  If 'nlocks' is zero, release
    835       1.105        ad  * all holds.  If 'l' is non-null, the release is from process context.
    836        1.62   thorpej  */
    837        1.62   thorpej void
    838       1.105        ad _kernel_unlock(int nlocks, struct lwp *l, int *countp)
    839        1.62   thorpej {
    840       1.105        ad 	struct cpu_info *ci = curcpu();
    841       1.105        ad 	u_int olocks;
    842       1.105        ad 	int s;
    843        1.62   thorpej 
    844   1.110.2.7        ad 	l = curlwp;
    845        1.62   thorpej 
    846       1.105        ad 	_KERNEL_LOCK_ASSERT(nlocks < 2);
    847        1.62   thorpej 
    848   1.110.2.7        ad 	olocks = l->l_blcnt;
    849        1.77      yamt 
    850       1.105        ad 	if (olocks == 0) {
    851       1.105        ad 		_KERNEL_LOCK_ASSERT(nlocks <= 0);
    852       1.105        ad 		if (countp != NULL)
    853       1.105        ad 			*countp = 0;
    854       1.105        ad 		return;
    855       1.105        ad 	}
    856        1.77      yamt 
    857  1.110.2.12        ad 	_KERNEL_LOCK_ASSERT(__SIMPLELOCK_LOCKED_P(&kernel_lock));
    858        1.85      yamt 
    859       1.105        ad 	if (nlocks == 0)
    860       1.105        ad 		nlocks = olocks;
    861       1.105        ad 	else if (nlocks == -1) {
    862       1.105        ad 		nlocks = 1;
    863       1.105        ad 		_KERNEL_LOCK_ASSERT(olocks == 1);
    864       1.105        ad 	}
    865        1.85      yamt 
    866   1.110.2.7        ad 	_KERNEL_LOCK_ASSERT(ci->ci_biglock_count >= l->l_blcnt);
    867   1.110.2.7        ad 
    868   1.110.2.7        ad 	l->l_blcnt -= nlocks;
    869  1.110.2.11        ad 	if (ci->ci_biglock_count == nlocks) {
    870  1.110.2.15        ad 		s = splvm();
    871  1.110.2.13        ad 		LOCKDEBUG_UNLOCKED(kernel_lock_id, RETURN_ADDRESS, 0);
    872  1.110.2.11        ad 		ci->ci_biglock_count = 0;
    873       1.105        ad 		__cpu_simple_unlock(&kernel_lock);
    874  1.110.2.11        ad 		splx(s);
    875  1.110.2.11        ad 	} else
    876  1.110.2.11        ad 		ci->ci_biglock_count -= nlocks;
    877        1.77      yamt 
    878       1.105        ad 	if (countp != NULL)
    879       1.105        ad 		*countp = olocks;
    880        1.77      yamt }
    881        1.77      yamt 
    882        1.84      yamt #if defined(DEBUG)
    883       1.105        ad /*
    884       1.105        ad  * Assert that the kernel lock is held.
    885       1.105        ad  */
    886        1.84      yamt void
    887       1.105        ad _kernel_lock_assert_locked(void)
    888        1.84      yamt {
    889       1.100      yamt 
    890  1.110.2.12        ad 	if (!__SIMPLELOCK_LOCKED_P(&kernel_lock) ||
    891       1.105        ad 	    curcpu()->ci_biglock_count == 0)
    892       1.105        ad 		_KERNEL_LOCK_ABORT("not locked");
    893        1.84      yamt }
    894       1.100      yamt 
    895       1.100      yamt void
    896       1.100      yamt _kernel_lock_assert_unlocked()
    897       1.100      yamt {
    898       1.100      yamt 
    899       1.105        ad 	if (curcpu()->ci_biglock_count != 0)
    900       1.105        ad 		_KERNEL_LOCK_ABORT("locked");
    901       1.100      yamt }
    902        1.84      yamt #endif
    903