Home | History | Annotate | Line # | Download | only in kern
kern_lock.c revision 1.25.2.2
      1  1.25.2.2    bouyer /*	$NetBSD: kern_lock.c,v 1.25.2.2 2000/11/22 16:05:20 bouyer Exp $	*/
      2      1.19   thorpej 
      3      1.19   thorpej /*-
      4  1.25.2.1    bouyer  * Copyright (c) 1999, 2000 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.19   thorpej  * NASA Ames Research Center.
     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.1      fvdl /*
     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.1      fvdl  * 3. All advertising materials mentioning features or use of this software
     60       1.1      fvdl  *    must display the following acknowledgement:
     61       1.1      fvdl  *	This product includes software developed by the University of
     62       1.1      fvdl  *	California, Berkeley and its contributors.
     63       1.1      fvdl  * 4. Neither the name of the University nor the names of its contributors
     64       1.1      fvdl  *    may be used to endorse or promote products derived from this software
     65       1.1      fvdl  *    without specific prior written permission.
     66       1.1      fvdl  *
     67       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     68       1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     69       1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     70       1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     71       1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     72       1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     73       1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     74       1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     75       1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     76       1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     77       1.1      fvdl  * SUCH DAMAGE.
     78       1.1      fvdl  *
     79       1.1      fvdl  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
     80       1.1      fvdl  */
     81       1.7   thorpej 
     82      1.21   thorpej #include "opt_multiprocessor.h"
     83       1.7   thorpej #include "opt_lockdebug.h"
     84      1.18       chs #include "opt_ddb.h"
     85       1.1      fvdl 
     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.1      fvdl #include <machine/cpu.h>
     91       1.1      fvdl 
     92      1.25   thorpej #if defined(LOCKDEBUG)
     93      1.25   thorpej #include <sys/syslog.h>
     94      1.25   thorpej /*
     95      1.25   thorpej  * note that stdarg.h and the ansi style va_start macro is used for both
     96      1.25   thorpej  * ansi and traditional c compiles.
     97      1.25   thorpej  * XXX: this requires that stdarg.h define: va_alist and va_dcl
     98      1.25   thorpej  */
     99      1.25   thorpej #include <machine/stdarg.h>
    100      1.25   thorpej 
    101  1.25.2.1    bouyer void	lock_printf(const char *fmt, ...)
    102  1.25.2.1    bouyer     __attribute__((__format__(__printf__,1,2)));
    103      1.25   thorpej 
    104      1.25   thorpej int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
    105      1.25   thorpej #endif
    106      1.25   thorpej 
    107       1.1      fvdl /*
    108       1.1      fvdl  * Locking primitives implementation.
    109       1.1      fvdl  * Locks provide shared/exclusive sychronization.
    110       1.1      fvdl  */
    111       1.1      fvdl 
    112      1.21   thorpej #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
    113      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    114      1.21   thorpej #define	COUNT_CPU(cpu_id, x)						\
    115  1.25.2.1    bouyer 	curcpu()->ci_spin_locks += (x)
    116      1.21   thorpej #else
    117      1.21   thorpej u_long	spin_locks;
    118      1.21   thorpej #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
    119      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    120      1.21   thorpej 
    121      1.21   thorpej #define	COUNT(lkp, p, cpu_id, x)					\
    122      1.21   thorpej do {									\
    123      1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN)					\
    124      1.21   thorpej 		COUNT_CPU((cpu_id), (x));				\
    125      1.21   thorpej 	else								\
    126      1.21   thorpej 		(p)->p_locks += (x);					\
    127  1.25.2.1    bouyer } while (/*CONSTCOND*/0)
    128       1.1      fvdl #else
    129      1.22    mellon #define COUNT(lkp, p, cpu_id, x)
    130  1.25.2.1    bouyer #define COUNT_CPU(cpu_id, x)
    131      1.21   thorpej #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
    132       1.1      fvdl 
    133  1.25.2.2    bouyer #ifndef SPINLOCK_INTERLOCK_RELEASE_HOOK		/* from <machine/lock.h> */
    134  1.25.2.2    bouyer #define	SPINLOCK_INTERLOCK_RELEASE_HOOK		/* nothing */
    135  1.25.2.2    bouyer #endif
    136  1.25.2.2    bouyer 
    137  1.25.2.1    bouyer #define	INTERLOCK_ACQUIRE(lkp, flags, s)				\
    138  1.25.2.1    bouyer do {									\
    139  1.25.2.1    bouyer 	if ((flags) & LK_SPIN)						\
    140  1.25.2.1    bouyer 		s = splsched();						\
    141  1.25.2.1    bouyer 	simple_lock(&(lkp)->lk_interlock);				\
    142  1.25.2.1    bouyer } while (0)
    143  1.25.2.1    bouyer 
    144  1.25.2.1    bouyer #define	INTERLOCK_RELEASE(lkp, flags, s)				\
    145  1.25.2.1    bouyer do {									\
    146  1.25.2.1    bouyer 	simple_unlock(&(lkp)->lk_interlock);				\
    147  1.25.2.2    bouyer 	if ((flags) & LK_SPIN) {					\
    148  1.25.2.1    bouyer 		splx(s);						\
    149  1.25.2.2    bouyer 		SPINLOCK_INTERLOCK_RELEASE_HOOK;			\
    150  1.25.2.2    bouyer 	}								\
    151  1.25.2.1    bouyer } while (0)
    152  1.25.2.1    bouyer 
    153       1.1      fvdl /*
    154       1.1      fvdl  * Acquire a resource.
    155       1.1      fvdl  */
    156      1.23   thorpej #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
    157      1.19   thorpej 	if ((extflags) & LK_SPIN) {					\
    158      1.19   thorpej 		int interlocked;					\
    159      1.19   thorpej 									\
    160      1.23   thorpej 		if ((drain) == 0)					\
    161      1.23   thorpej 			(lkp)->lk_waitcount++;				\
    162      1.19   thorpej 		for (interlocked = 1;;) {				\
    163      1.19   thorpej 			if (wanted) {					\
    164      1.19   thorpej 				if (interlocked) {			\
    165  1.25.2.1    bouyer 					INTERLOCK_RELEASE((lkp),	\
    166  1.25.2.1    bouyer 					    LK_SPIN, s);		\
    167      1.19   thorpej 					interlocked = 0;		\
    168      1.19   thorpej 				}					\
    169      1.19   thorpej 			} else if (interlocked) {			\
    170      1.19   thorpej 				break;					\
    171      1.19   thorpej 			} else {					\
    172  1.25.2.1    bouyer 				INTERLOCK_ACQUIRE((lkp), LK_SPIN, s);	\
    173      1.19   thorpej 				interlocked = 1;			\
    174      1.19   thorpej 			}						\
    175      1.19   thorpej 		}							\
    176      1.23   thorpej 		if ((drain) == 0)					\
    177      1.23   thorpej 			(lkp)->lk_waitcount--;				\
    178      1.19   thorpej 		KASSERT((wanted) == 0);					\
    179      1.19   thorpej 		error = 0;	/* sanity */				\
    180      1.19   thorpej 	} else {							\
    181      1.19   thorpej 		for (error = 0; wanted; ) {				\
    182      1.23   thorpej 			if ((drain))					\
    183      1.23   thorpej 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
    184      1.23   thorpej 			else						\
    185      1.23   thorpej 				(lkp)->lk_waitcount++;			\
    186      1.23   thorpej 			/* XXX Cast away volatile. */			\
    187  1.25.2.1    bouyer 			error = ltsleep((drain) ? &(lkp)->lk_flags :	\
    188      1.23   thorpej 			    (void *)(lkp), (lkp)->lk_prio,		\
    189  1.25.2.1    bouyer 			    (lkp)->lk_wmesg, (lkp)->lk_timo,		\
    190  1.25.2.1    bouyer 			    &(lkp)->lk_interlock);			\
    191      1.23   thorpej 			if ((drain) == 0)				\
    192      1.23   thorpej 				(lkp)->lk_waitcount--;			\
    193      1.19   thorpej 			if (error)					\
    194      1.19   thorpej 				break;					\
    195      1.19   thorpej 			if ((extflags) & LK_SLEEPFAIL) {		\
    196      1.19   thorpej 				error = ENOLCK;				\
    197      1.19   thorpej 				break;					\
    198      1.19   thorpej 			}						\
    199       1.1      fvdl 		}							\
    200       1.1      fvdl 	}
    201       1.1      fvdl 
    202      1.19   thorpej #define	SETHOLDER(lkp, pid, cpu_id)					\
    203      1.19   thorpej do {									\
    204      1.19   thorpej 	if ((lkp)->lk_flags & LK_SPIN)					\
    205      1.19   thorpej 		(lkp)->lk_cpu = cpu_id;					\
    206      1.19   thorpej 	else								\
    207      1.19   thorpej 		(lkp)->lk_lockholder = pid;				\
    208  1.25.2.1    bouyer } while (/*CONSTCOND*/0)
    209      1.19   thorpej 
    210      1.19   thorpej #define	WEHOLDIT(lkp, pid, cpu_id)					\
    211      1.19   thorpej 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
    212      1.19   thorpej 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
    213      1.19   thorpej 
    214      1.23   thorpej #define	WAKEUP_WAITER(lkp)						\
    215      1.23   thorpej do {									\
    216      1.23   thorpej 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
    217      1.23   thorpej 		/* XXX Cast away volatile. */				\
    218      1.23   thorpej 		wakeup_one((void *)(lkp));				\
    219      1.23   thorpej 	}								\
    220  1.25.2.1    bouyer } while (/*CONSTCOND*/0)
    221      1.23   thorpej 
    222      1.21   thorpej #if defined(LOCKDEBUG) /* { */
    223      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    224      1.21   thorpej struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
    225      1.21   thorpej 
    226  1.25.2.1    bouyer #define	SPINLOCK_LIST_LOCK()						\
    227  1.25.2.1    bouyer 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
    228      1.21   thorpej 
    229  1.25.2.1    bouyer #define	SPINLOCK_LIST_UNLOCK()						\
    230  1.25.2.1    bouyer 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
    231      1.21   thorpej #else
    232      1.21   thorpej #define	SPINLOCK_LIST_LOCK()	/* nothing */
    233      1.21   thorpej 
    234      1.21   thorpej #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
    235      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    236      1.21   thorpej 
    237      1.21   thorpej TAILQ_HEAD(, lock) spinlock_list =
    238      1.21   thorpej     TAILQ_HEAD_INITIALIZER(spinlock_list);
    239      1.21   thorpej 
    240      1.21   thorpej #define	HAVEIT(lkp)							\
    241      1.21   thorpej do {									\
    242      1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN) {				\
    243  1.25.2.1    bouyer 		int s = spllock();					\
    244      1.21   thorpej 		SPINLOCK_LIST_LOCK();					\
    245      1.21   thorpej 		/* XXX Cast away volatile. */				\
    246      1.21   thorpej 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
    247      1.21   thorpej 		    lk_list);						\
    248      1.21   thorpej 		SPINLOCK_LIST_UNLOCK();					\
    249      1.21   thorpej 		splx(s);						\
    250      1.21   thorpej 	}								\
    251  1.25.2.1    bouyer } while (/*CONSTCOND*/0)
    252      1.21   thorpej 
    253      1.21   thorpej #define	DONTHAVEIT(lkp)							\
    254      1.21   thorpej do {									\
    255      1.21   thorpej 	if ((lkp)->lk_flags & LK_SPIN) {				\
    256  1.25.2.1    bouyer 		int s = spllock();					\
    257      1.21   thorpej 		SPINLOCK_LIST_LOCK();					\
    258      1.21   thorpej 		/* XXX Cast away volatile. */				\
    259      1.21   thorpej 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
    260      1.21   thorpej 		    lk_list);						\
    261      1.21   thorpej 		SPINLOCK_LIST_UNLOCK();					\
    262      1.21   thorpej 		splx(s);						\
    263      1.21   thorpej 	}								\
    264  1.25.2.1    bouyer } while (/*CONSTCOND*/0)
    265      1.21   thorpej #else
    266      1.21   thorpej #define	HAVEIT(lkp)		/* nothing */
    267      1.21   thorpej 
    268      1.21   thorpej #define	DONTHAVEIT(lkp)		/* nothing */
    269      1.21   thorpej #endif /* LOCKDEBUG */ /* } */
    270      1.21   thorpej 
    271      1.25   thorpej #if defined(LOCKDEBUG)
    272      1.25   thorpej /*
    273      1.25   thorpej  * Lock debug printing routine; can be configured to print to console
    274      1.25   thorpej  * or log to syslog.
    275      1.25   thorpej  */
    276      1.25   thorpej void
    277      1.25   thorpej lock_printf(const char *fmt, ...)
    278      1.25   thorpej {
    279      1.25   thorpej 	va_list ap;
    280      1.25   thorpej 
    281      1.25   thorpej 	va_start(ap, fmt);
    282      1.25   thorpej 	if (lock_debug_syslog)
    283      1.25   thorpej 		vlog(LOG_DEBUG, fmt, ap);
    284      1.25   thorpej 	else
    285      1.25   thorpej 		vprintf(fmt, ap);
    286      1.25   thorpej 	va_end(ap);
    287      1.25   thorpej }
    288      1.25   thorpej #endif /* LOCKDEBUG */
    289      1.25   thorpej 
    290       1.1      fvdl /*
    291       1.1      fvdl  * Initialize a lock; required before use.
    292       1.1      fvdl  */
    293       1.1      fvdl void
    294  1.25.2.1    bouyer lockinit(struct lock *lkp, int prio, const char *wmesg, int timo, int flags)
    295       1.1      fvdl {
    296       1.1      fvdl 
    297       1.8     perry 	memset(lkp, 0, sizeof(struct lock));
    298       1.1      fvdl 	simple_lock_init(&lkp->lk_interlock);
    299       1.1      fvdl 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
    300      1.19   thorpej 	if (flags & LK_SPIN)
    301      1.19   thorpej 		lkp->lk_cpu = LK_NOCPU;
    302      1.19   thorpej 	else {
    303      1.19   thorpej 		lkp->lk_lockholder = LK_NOPROC;
    304      1.19   thorpej 		lkp->lk_prio = prio;
    305      1.19   thorpej 		lkp->lk_timo = timo;
    306      1.19   thorpej 	}
    307      1.19   thorpej 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
    308       1.1      fvdl }
    309       1.1      fvdl 
    310       1.1      fvdl /*
    311       1.1      fvdl  * Determine the status of a lock.
    312       1.1      fvdl  */
    313       1.1      fvdl int
    314  1.25.2.1    bouyer lockstatus(struct lock *lkp)
    315       1.1      fvdl {
    316  1.25.2.1    bouyer 	int s, lock_type = 0;
    317       1.1      fvdl 
    318  1.25.2.1    bouyer 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    319       1.1      fvdl 	if (lkp->lk_exclusivecount != 0)
    320       1.1      fvdl 		lock_type = LK_EXCLUSIVE;
    321       1.1      fvdl 	else if (lkp->lk_sharecount != 0)
    322       1.1      fvdl 		lock_type = LK_SHARED;
    323  1.25.2.1    bouyer 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    324       1.1      fvdl 	return (lock_type);
    325       1.1      fvdl }
    326       1.1      fvdl 
    327  1.25.2.1    bouyer #if defined(LOCKDEBUG) || defined(DIAGNOSTIC)
    328  1.25.2.1    bouyer /*
    329  1.25.2.1    bouyer  * Make sure no spin locks are held by a CPU that is about
    330  1.25.2.1    bouyer  * to context switch.
    331  1.25.2.1    bouyer  */
    332  1.25.2.1    bouyer void
    333  1.25.2.1    bouyer spinlock_switchcheck(void)
    334  1.25.2.1    bouyer {
    335  1.25.2.1    bouyer 	u_long cnt;
    336  1.25.2.1    bouyer 	int s;
    337  1.25.2.1    bouyer 
    338  1.25.2.1    bouyer 	s = spllock();
    339  1.25.2.1    bouyer #if defined(MULTIPROCESSOR)
    340  1.25.2.1    bouyer 	cnt = curcpu()->ci_spin_locks;
    341  1.25.2.1    bouyer #else
    342  1.25.2.1    bouyer 	cnt = spin_locks;
    343  1.25.2.1    bouyer #endif
    344  1.25.2.1    bouyer 	splx(s);
    345  1.25.2.1    bouyer 
    346  1.25.2.1    bouyer 	if (cnt != 0)
    347  1.25.2.1    bouyer 		panic("spinlock_switchcheck: CPU %lu has %lu spin locks",
    348  1.25.2.1    bouyer 		    (u_long) cpu_number(), cnt);
    349  1.25.2.1    bouyer }
    350  1.25.2.1    bouyer #endif /* LOCKDEBUG || DIAGNOSTIC */
    351  1.25.2.1    bouyer 
    352  1.25.2.1    bouyer /*
    353  1.25.2.1    bouyer  * Locks and IPLs (interrupt priority levels):
    354  1.25.2.1    bouyer  *
    355  1.25.2.1    bouyer  * Locks which may be taken from interrupt context must be handled
    356  1.25.2.1    bouyer  * very carefully; you must spl to the highest IPL where the lock
    357  1.25.2.1    bouyer  * is needed before acquiring the lock.
    358  1.25.2.1    bouyer  *
    359  1.25.2.1    bouyer  * It is also important to avoid deadlock, since certain (very high
    360  1.25.2.1    bouyer  * priority) interrupts are often needed to keep the system as a whole
    361  1.25.2.1    bouyer  * from deadlocking, and must not be blocked while you are spinning
    362  1.25.2.1    bouyer  * waiting for a lower-priority lock.
    363  1.25.2.1    bouyer  *
    364  1.25.2.1    bouyer  * In addition, the lock-debugging hooks themselves need to use locks!
    365  1.25.2.1    bouyer  *
    366  1.25.2.1    bouyer  * A raw __cpu_simple_lock may be used from interrupts are long as it
    367  1.25.2.1    bouyer  * is acquired and held at a single IPL.
    368  1.25.2.1    bouyer  *
    369  1.25.2.1    bouyer  * A simple_lock (which is a __cpu_simple_lock wrapped with some
    370  1.25.2.1    bouyer  * debugging hooks) may be used at or below spllock(), which is
    371  1.25.2.1    bouyer  * typically at or just below splhigh() (i.e. blocks everything
    372  1.25.2.1    bouyer  * but certain machine-dependent extremely high priority interrupts).
    373  1.25.2.1    bouyer  *
    374  1.25.2.1    bouyer  * spinlockmgr spinlocks should be used at or below splsched().
    375  1.25.2.1    bouyer  *
    376  1.25.2.1    bouyer  * Some platforms may have interrupts of higher priority than splsched(),
    377  1.25.2.1    bouyer  * including hard serial interrupts, inter-processor interrupts, and
    378  1.25.2.1    bouyer  * kernel debugger traps.
    379  1.25.2.1    bouyer  */
    380  1.25.2.1    bouyer 
    381  1.25.2.1    bouyer /*
    382  1.25.2.1    bouyer  * XXX XXX kludge around another kludge..
    383  1.25.2.1    bouyer  *
    384  1.25.2.1    bouyer  * vfs_shutdown() may be called from interrupt context, either as a result
    385  1.25.2.1    bouyer  * of a panic, or from the debugger.   It proceeds to call
    386  1.25.2.1    bouyer  * sys_sync(&proc0, ...), pretending its running on behalf of proc0
    387  1.25.2.1    bouyer  *
    388  1.25.2.1    bouyer  * We would like to make an attempt to sync the filesystems in this case, so
    389  1.25.2.1    bouyer  * if this happens, we treat attempts to acquire locks specially.
    390  1.25.2.1    bouyer  * All locks are acquired on behalf of proc0.
    391  1.25.2.1    bouyer  *
    392  1.25.2.1    bouyer  * If we've already paniced, we don't block waiting for locks, but
    393  1.25.2.1    bouyer  * just barge right ahead since we're already going down in flames.
    394  1.25.2.1    bouyer  */
    395  1.25.2.1    bouyer 
    396       1.1      fvdl /*
    397       1.1      fvdl  * Set, change, or release a lock.
    398       1.1      fvdl  *
    399       1.1      fvdl  * Shared requests increment the shared count. Exclusive requests set the
    400       1.1      fvdl  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
    401       1.1      fvdl  * accepted shared locks and shared-to-exclusive upgrades to go away.
    402       1.1      fvdl  */
    403       1.1      fvdl int
    404  1.25.2.1    bouyer lockmgr(__volatile struct lock *lkp, u_int flags,
    405  1.25.2.1    bouyer     struct simplelock *interlkp)
    406       1.1      fvdl {
    407       1.1      fvdl 	int error;
    408       1.1      fvdl 	pid_t pid;
    409       1.1      fvdl 	int extflags;
    410      1.24   thorpej 	cpuid_t cpu_id;
    411       1.6      fvdl 	struct proc *p = curproc;
    412  1.25.2.1    bouyer 	int lock_shutdown_noblock = 0;
    413  1.25.2.1    bouyer 	int s;
    414       1.1      fvdl 
    415       1.1      fvdl 	error = 0;
    416      1.19   thorpej 
    417  1.25.2.1    bouyer 	INTERLOCK_ACQUIRE(lkp, lkp->lk_flags, s);
    418       1.1      fvdl 	if (flags & LK_INTERLOCK)
    419       1.1      fvdl 		simple_unlock(interlkp);
    420       1.1      fvdl 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
    421      1.19   thorpej 
    422      1.21   thorpej #ifdef DIAGNOSTIC /* { */
    423      1.19   thorpej 	/*
    424      1.19   thorpej 	 * Don't allow spins on sleep locks and don't allow sleeps
    425      1.19   thorpej 	 * on spin locks.
    426      1.19   thorpej 	 */
    427      1.19   thorpej 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
    428      1.19   thorpej 		panic("lockmgr: sleep/spin mismatch\n");
    429      1.21   thorpej #endif /* } */
    430      1.19   thorpej 
    431      1.19   thorpej 	if (extflags & LK_SPIN)
    432      1.19   thorpej 		pid = LK_KERNPROC;
    433      1.19   thorpej 	else {
    434  1.25.2.1    bouyer 		if (p == NULL) {
    435  1.25.2.1    bouyer 			if (!doing_shutdown) {
    436  1.25.2.1    bouyer #ifdef DIAGNOSTIC
    437  1.25.2.1    bouyer 				panic("lockmgr: no context");
    438  1.25.2.1    bouyer #endif
    439  1.25.2.1    bouyer 			} else {
    440  1.25.2.1    bouyer 				p = &proc0;
    441  1.25.2.1    bouyer 				if (panicstr && (!(flags & LK_NOWAIT))) {
    442  1.25.2.1    bouyer 					flags |= LK_NOWAIT;
    443  1.25.2.1    bouyer 					lock_shutdown_noblock = 1;
    444  1.25.2.1    bouyer 				}
    445  1.25.2.1    bouyer 			}
    446  1.25.2.1    bouyer 		}
    447      1.19   thorpej 		pid = p->p_pid;
    448      1.19   thorpej 	}
    449      1.24   thorpej 	cpu_id = cpu_number();
    450      1.19   thorpej 
    451       1.1      fvdl 	/*
    452       1.1      fvdl 	 * Once a lock has drained, the LK_DRAINING flag is set and an
    453       1.1      fvdl 	 * exclusive lock is returned. The only valid operation thereafter
    454       1.1      fvdl 	 * is a single release of that exclusive lock. This final release
    455       1.1      fvdl 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
    456       1.1      fvdl 	 * further requests of any sort will result in a panic. The bits
    457       1.1      fvdl 	 * selected for these two flags are chosen so that they will be set
    458       1.1      fvdl 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
    459       1.1      fvdl 	 * The final release is permitted to give a new lease on life to
    460       1.1      fvdl 	 * the lock by specifying LK_REENABLE.
    461       1.1      fvdl 	 */
    462       1.1      fvdl 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
    463  1.25.2.1    bouyer #ifdef DIAGNOSTIC /* { */
    464       1.1      fvdl 		if (lkp->lk_flags & LK_DRAINED)
    465       1.1      fvdl 			panic("lockmgr: using decommissioned lock");
    466       1.1      fvdl 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
    467      1.19   thorpej 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
    468       1.1      fvdl 			panic("lockmgr: non-release on draining lock: %d\n",
    469       1.1      fvdl 			    flags & LK_TYPE_MASK);
    470  1.25.2.1    bouyer #endif /* DIAGNOSTIC */ /* } */
    471       1.1      fvdl 		lkp->lk_flags &= ~LK_DRAINING;
    472       1.1      fvdl 		if ((flags & LK_REENABLE) == 0)
    473       1.1      fvdl 			lkp->lk_flags |= LK_DRAINED;
    474       1.1      fvdl 	}
    475       1.1      fvdl 
    476       1.1      fvdl 	switch (flags & LK_TYPE_MASK) {
    477       1.1      fvdl 
    478       1.1      fvdl 	case LK_SHARED:
    479      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    480       1.1      fvdl 			/*
    481       1.1      fvdl 			 * If just polling, check to see if we will block.
    482       1.1      fvdl 			 */
    483       1.1      fvdl 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
    484       1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
    485       1.1      fvdl 				error = EBUSY;
    486       1.1      fvdl 				break;
    487       1.1      fvdl 			}
    488       1.1      fvdl 			/*
    489       1.1      fvdl 			 * Wait for exclusive locks and upgrades to clear.
    490       1.1      fvdl 			 */
    491      1.23   thorpej 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    492       1.1      fvdl 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
    493       1.1      fvdl 			if (error)
    494       1.1      fvdl 				break;
    495       1.1      fvdl 			lkp->lk_sharecount++;
    496      1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    497       1.1      fvdl 			break;
    498       1.1      fvdl 		}
    499       1.1      fvdl 		/*
    500       1.1      fvdl 		 * We hold an exclusive lock, so downgrade it to shared.
    501       1.1      fvdl 		 * An alternative would be to fail with EDEADLK.
    502       1.1      fvdl 		 */
    503       1.1      fvdl 		lkp->lk_sharecount++;
    504      1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    505       1.1      fvdl 		/* fall into downgrade */
    506       1.1      fvdl 
    507       1.1      fvdl 	case LK_DOWNGRADE:
    508      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
    509      1.19   thorpej 		    lkp->lk_exclusivecount == 0)
    510       1.1      fvdl 			panic("lockmgr: not holding exclusive lock");
    511       1.1      fvdl 		lkp->lk_sharecount += lkp->lk_exclusivecount;
    512       1.1      fvdl 		lkp->lk_exclusivecount = 0;
    513      1.15      fvdl 		lkp->lk_recurselevel = 0;
    514       1.1      fvdl 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    515      1.19   thorpej 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    516      1.21   thorpej 		DONTHAVEIT(lkp);
    517      1.23   thorpej 		WAKEUP_WAITER(lkp);
    518       1.1      fvdl 		break;
    519       1.1      fvdl 
    520       1.1      fvdl 	case LK_EXCLUPGRADE:
    521       1.1      fvdl 		/*
    522       1.1      fvdl 		 * If another process is ahead of us to get an upgrade,
    523       1.1      fvdl 		 * then we want to fail rather than have an intervening
    524       1.1      fvdl 		 * exclusive access.
    525       1.1      fvdl 		 */
    526       1.1      fvdl 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
    527       1.1      fvdl 			lkp->lk_sharecount--;
    528      1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    529       1.1      fvdl 			error = EBUSY;
    530       1.1      fvdl 			break;
    531       1.1      fvdl 		}
    532       1.1      fvdl 		/* fall into normal upgrade */
    533       1.1      fvdl 
    534       1.1      fvdl 	case LK_UPGRADE:
    535       1.1      fvdl 		/*
    536       1.1      fvdl 		 * Upgrade a shared lock to an exclusive one. If another
    537       1.1      fvdl 		 * shared lock has already requested an upgrade to an
    538       1.1      fvdl 		 * exclusive lock, our shared lock is released and an
    539       1.1      fvdl 		 * exclusive lock is requested (which will be granted
    540       1.1      fvdl 		 * after the upgrade). If we return an error, the file
    541       1.1      fvdl 		 * will always be unlocked.
    542       1.1      fvdl 		 */
    543      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
    544       1.1      fvdl 			panic("lockmgr: upgrade exclusive lock");
    545       1.1      fvdl 		lkp->lk_sharecount--;
    546      1.21   thorpej 		COUNT(lkp, p, cpu_id, -1);
    547       1.1      fvdl 		/*
    548       1.1      fvdl 		 * If we are just polling, check to see if we will block.
    549       1.1      fvdl 		 */
    550       1.1      fvdl 		if ((extflags & LK_NOWAIT) &&
    551       1.1      fvdl 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
    552       1.1      fvdl 		     lkp->lk_sharecount > 1)) {
    553       1.1      fvdl 			error = EBUSY;
    554       1.1      fvdl 			break;
    555       1.1      fvdl 		}
    556       1.1      fvdl 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
    557       1.1      fvdl 			/*
    558       1.1      fvdl 			 * We are first shared lock to request an upgrade, so
    559       1.1      fvdl 			 * request upgrade and wait for the shared count to
    560       1.1      fvdl 			 * drop to zero, then take exclusive lock.
    561       1.1      fvdl 			 */
    562       1.1      fvdl 			lkp->lk_flags |= LK_WANT_UPGRADE;
    563      1.23   thorpej 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
    564       1.1      fvdl 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
    565       1.1      fvdl 			if (error)
    566       1.1      fvdl 				break;
    567       1.1      fvdl 			lkp->lk_flags |= LK_HAVE_EXCL;
    568      1.19   thorpej 			SETHOLDER(lkp, pid, cpu_id);
    569      1.21   thorpej 			HAVEIT(lkp);
    570       1.1      fvdl 			if (lkp->lk_exclusivecount != 0)
    571       1.1      fvdl 				panic("lockmgr: non-zero exclusive count");
    572       1.1      fvdl 			lkp->lk_exclusivecount = 1;
    573      1.15      fvdl 			if (extflags & LK_SETRECURSE)
    574      1.15      fvdl 				lkp->lk_recurselevel = 1;
    575      1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    576       1.1      fvdl 			break;
    577       1.1      fvdl 		}
    578       1.1      fvdl 		/*
    579       1.1      fvdl 		 * Someone else has requested upgrade. Release our shared
    580       1.1      fvdl 		 * lock, awaken upgrade requestor if we are the last shared
    581       1.1      fvdl 		 * lock, then request an exclusive lock.
    582       1.1      fvdl 		 */
    583      1.23   thorpej 		if (lkp->lk_sharecount == 0)
    584      1.23   thorpej 			WAKEUP_WAITER(lkp);
    585       1.1      fvdl 		/* fall into exclusive request */
    586       1.1      fvdl 
    587       1.1      fvdl 	case LK_EXCLUSIVE:
    588      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id)) {
    589       1.1      fvdl 			/*
    590      1.19   thorpej 			 * Recursive lock.
    591       1.1      fvdl 			 */
    592      1.15      fvdl 			if ((extflags & LK_CANRECURSE) == 0 &&
    593      1.16  sommerfe 			     lkp->lk_recurselevel == 0) {
    594      1.16  sommerfe 				if (extflags & LK_RECURSEFAIL) {
    595      1.16  sommerfe 					error = EDEADLK;
    596      1.16  sommerfe 					break;
    597      1.16  sommerfe 				} else
    598      1.16  sommerfe 					panic("lockmgr: locking against myself");
    599      1.16  sommerfe 			}
    600       1.1      fvdl 			lkp->lk_exclusivecount++;
    601      1.15      fvdl 			if (extflags & LK_SETRECURSE &&
    602      1.15      fvdl 			    lkp->lk_recurselevel == 0)
    603      1.15      fvdl 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
    604      1.21   thorpej 			COUNT(lkp, p, cpu_id, 1);
    605       1.1      fvdl 			break;
    606       1.1      fvdl 		}
    607       1.1      fvdl 		/*
    608       1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    609       1.1      fvdl 		 */
    610       1.1      fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    611       1.1      fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    612       1.1      fvdl 		     lkp->lk_sharecount != 0)) {
    613       1.1      fvdl 			error = EBUSY;
    614       1.1      fvdl 			break;
    615       1.1      fvdl 		}
    616       1.1      fvdl 		/*
    617       1.1      fvdl 		 * Try to acquire the want_exclusive flag.
    618       1.1      fvdl 		 */
    619      1.23   thorpej 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
    620       1.1      fvdl 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
    621       1.1      fvdl 		if (error)
    622       1.1      fvdl 			break;
    623       1.1      fvdl 		lkp->lk_flags |= LK_WANT_EXCL;
    624       1.1      fvdl 		/*
    625       1.1      fvdl 		 * Wait for shared locks and upgrades to finish.
    626       1.1      fvdl 		 */
    627      1.23   thorpej 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
    628       1.1      fvdl 		       (lkp->lk_flags & LK_WANT_UPGRADE));
    629       1.1      fvdl 		lkp->lk_flags &= ~LK_WANT_EXCL;
    630       1.1      fvdl 		if (error)
    631       1.1      fvdl 			break;
    632       1.1      fvdl 		lkp->lk_flags |= LK_HAVE_EXCL;
    633      1.19   thorpej 		SETHOLDER(lkp, pid, cpu_id);
    634      1.21   thorpej 		HAVEIT(lkp);
    635       1.1      fvdl 		if (lkp->lk_exclusivecount != 0)
    636       1.1      fvdl 			panic("lockmgr: non-zero exclusive count");
    637       1.1      fvdl 		lkp->lk_exclusivecount = 1;
    638      1.15      fvdl 		if (extflags & LK_SETRECURSE)
    639      1.15      fvdl 			lkp->lk_recurselevel = 1;
    640      1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    641       1.1      fvdl 		break;
    642       1.1      fvdl 
    643       1.1      fvdl 	case LK_RELEASE:
    644       1.1      fvdl 		if (lkp->lk_exclusivecount != 0) {
    645      1.19   thorpej 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
    646      1.19   thorpej 				if (lkp->lk_flags & LK_SPIN) {
    647      1.19   thorpej 					panic("lockmgr: processor %lu, not "
    648      1.19   thorpej 					    "exclusive lock holder %lu "
    649      1.19   thorpej 					    "unlocking", cpu_id, lkp->lk_cpu);
    650      1.19   thorpej 				} else {
    651      1.19   thorpej 					panic("lockmgr: pid %d, not "
    652      1.19   thorpej 					    "exclusive lock holder %d "
    653      1.19   thorpej 					    "unlocking", pid,
    654      1.19   thorpej 					    lkp->lk_lockholder);
    655      1.19   thorpej 				}
    656      1.19   thorpej 			}
    657      1.15      fvdl 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
    658      1.15      fvdl 				lkp->lk_recurselevel = 0;
    659       1.1      fvdl 			lkp->lk_exclusivecount--;
    660      1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    661       1.1      fvdl 			if (lkp->lk_exclusivecount == 0) {
    662       1.1      fvdl 				lkp->lk_flags &= ~LK_HAVE_EXCL;
    663      1.19   thorpej 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    664      1.21   thorpej 				DONTHAVEIT(lkp);
    665       1.1      fvdl 			}
    666       1.1      fvdl 		} else if (lkp->lk_sharecount != 0) {
    667       1.1      fvdl 			lkp->lk_sharecount--;
    668      1.21   thorpej 			COUNT(lkp, p, cpu_id, -1);
    669       1.1      fvdl 		}
    670  1.25.2.1    bouyer #ifdef DIAGNOSTIC
    671  1.25.2.1    bouyer 		else
    672  1.25.2.1    bouyer 			panic("lockmgr: release of unlocked lock!");
    673  1.25.2.1    bouyer #endif
    674      1.23   thorpej 		WAKEUP_WAITER(lkp);
    675       1.1      fvdl 		break;
    676       1.1      fvdl 
    677       1.1      fvdl 	case LK_DRAIN:
    678       1.1      fvdl 		/*
    679       1.1      fvdl 		 * Check that we do not already hold the lock, as it can
    680       1.1      fvdl 		 * never drain if we do. Unfortunately, we have no way to
    681       1.1      fvdl 		 * check for holding a shared lock, but at least we can
    682       1.1      fvdl 		 * check for an exclusive one.
    683       1.1      fvdl 		 */
    684      1.19   thorpej 		if (WEHOLDIT(lkp, pid, cpu_id))
    685       1.1      fvdl 			panic("lockmgr: draining against myself");
    686       1.1      fvdl 		/*
    687       1.1      fvdl 		 * If we are just polling, check to see if we will sleep.
    688       1.1      fvdl 		 */
    689       1.1      fvdl 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
    690       1.1      fvdl 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    691       1.1      fvdl 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
    692       1.1      fvdl 			error = EBUSY;
    693       1.1      fvdl 			break;
    694       1.1      fvdl 		}
    695      1.23   thorpej 		ACQUIRE(lkp, error, extflags, 1,
    696      1.23   thorpej 		    ((lkp->lk_flags &
    697      1.23   thorpej 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
    698      1.23   thorpej 		     lkp->lk_sharecount != 0 ||
    699      1.23   thorpej 		     lkp->lk_waitcount != 0));
    700      1.23   thorpej 		if (error)
    701      1.23   thorpej 			break;
    702       1.1      fvdl 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
    703      1.19   thorpej 		SETHOLDER(lkp, pid, cpu_id);
    704      1.21   thorpej 		HAVEIT(lkp);
    705       1.1      fvdl 		lkp->lk_exclusivecount = 1;
    706      1.15      fvdl 		/* XXX unlikely that we'd want this */
    707      1.15      fvdl 		if (extflags & LK_SETRECURSE)
    708      1.15      fvdl 			lkp->lk_recurselevel = 1;
    709      1.21   thorpej 		COUNT(lkp, p, cpu_id, 1);
    710       1.1      fvdl 		break;
    711       1.1      fvdl 
    712       1.1      fvdl 	default:
    713  1.25.2.1    bouyer 		INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    714       1.1      fvdl 		panic("lockmgr: unknown locktype request %d",
    715       1.1      fvdl 		    flags & LK_TYPE_MASK);
    716       1.1      fvdl 		/* NOTREACHED */
    717       1.1      fvdl 	}
    718      1.23   thorpej 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
    719      1.23   thorpej 	    ((lkp->lk_flags &
    720      1.23   thorpej 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
    721       1.1      fvdl 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
    722       1.1      fvdl 		lkp->lk_flags &= ~LK_WAITDRAIN;
    723      1.20   thorpej 		wakeup_one((void *)&lkp->lk_flags);
    724       1.1      fvdl 	}
    725  1.25.2.1    bouyer 	/*
    726  1.25.2.1    bouyer 	 * Note that this panic will be a recursive panic, since
    727  1.25.2.1    bouyer 	 * we only set lock_shutdown_noblock above if panicstr != NULL.
    728  1.25.2.1    bouyer 	 */
    729  1.25.2.1    bouyer 	if (error && lock_shutdown_noblock)
    730  1.25.2.1    bouyer 		panic("lockmgr: deadlock (see previous panic)");
    731  1.25.2.1    bouyer 
    732  1.25.2.1    bouyer 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    733       1.1      fvdl 	return (error);
    734       1.1      fvdl }
    735       1.1      fvdl 
    736       1.1      fvdl /*
    737  1.25.2.1    bouyer  * For a recursive spinlock held one or more times by the current CPU,
    738  1.25.2.1    bouyer  * release all N locks, and return N.
    739  1.25.2.1    bouyer  * Intended for use in mi_switch() shortly before context switching.
    740  1.25.2.1    bouyer  */
    741  1.25.2.1    bouyer 
    742  1.25.2.1    bouyer int
    743  1.25.2.1    bouyer spinlock_release_all(__volatile struct lock *lkp)
    744  1.25.2.1    bouyer {
    745  1.25.2.1    bouyer 	int s, count;
    746  1.25.2.1    bouyer 	cpuid_t cpu_id;
    747  1.25.2.1    bouyer 
    748  1.25.2.1    bouyer 	KASSERT(lkp->lk_flags & LK_SPIN);
    749  1.25.2.1    bouyer 
    750  1.25.2.1    bouyer 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    751  1.25.2.1    bouyer 
    752  1.25.2.1    bouyer 	cpu_id = cpu_number();
    753  1.25.2.1    bouyer 	count = lkp->lk_exclusivecount;
    754  1.25.2.1    bouyer 
    755  1.25.2.1    bouyer 	if (count != 0) {
    756  1.25.2.1    bouyer #ifdef DIAGNOSTIC
    757  1.25.2.1    bouyer 		if (WEHOLDIT(lkp, 0, cpu_id) == 0) {
    758  1.25.2.1    bouyer 			panic("spinlock_release_all: processor %lu, not "
    759  1.25.2.1    bouyer 			    "exclusive lock holder %lu "
    760  1.25.2.1    bouyer 			    "unlocking", (long)cpu_id, lkp->lk_cpu);
    761  1.25.2.1    bouyer 		}
    762  1.25.2.1    bouyer #endif
    763  1.25.2.1    bouyer 		lkp->lk_recurselevel = 0;
    764  1.25.2.1    bouyer 		lkp->lk_exclusivecount = 0;
    765  1.25.2.1    bouyer 		COUNT_CPU(cpu_id, -count);
    766  1.25.2.1    bouyer 		lkp->lk_flags &= ~LK_HAVE_EXCL;
    767  1.25.2.1    bouyer 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
    768  1.25.2.1    bouyer 		DONTHAVEIT(lkp);
    769  1.25.2.1    bouyer 	}
    770  1.25.2.1    bouyer #ifdef DIAGNOSTIC
    771  1.25.2.1    bouyer 	else if (lkp->lk_sharecount != 0)
    772  1.25.2.1    bouyer 		panic("spinlock_release_all: release of shared lock!");
    773  1.25.2.1    bouyer 	else
    774  1.25.2.1    bouyer 		panic("spinlock_release_all: release of unlocked lock!");
    775  1.25.2.1    bouyer #endif
    776  1.25.2.1    bouyer 	INTERLOCK_RELEASE(lkp, LK_SPIN, s);
    777  1.25.2.1    bouyer 
    778  1.25.2.1    bouyer 	return (count);
    779  1.25.2.1    bouyer }
    780  1.25.2.1    bouyer 
    781  1.25.2.1    bouyer /*
    782  1.25.2.1    bouyer  * For a recursive spinlock held one or more times by the current CPU,
    783  1.25.2.1    bouyer  * release all N locks, and return N.
    784  1.25.2.1    bouyer  * Intended for use in mi_switch() right after resuming execution.
    785  1.25.2.1    bouyer  */
    786  1.25.2.1    bouyer 
    787  1.25.2.1    bouyer void
    788  1.25.2.1    bouyer spinlock_acquire_count(__volatile struct lock *lkp, int count)
    789  1.25.2.1    bouyer {
    790  1.25.2.1    bouyer 	int s, error;
    791  1.25.2.1    bouyer 	cpuid_t cpu_id;
    792  1.25.2.1    bouyer 
    793  1.25.2.1    bouyer 	KASSERT(lkp->lk_flags & LK_SPIN);
    794  1.25.2.1    bouyer 
    795  1.25.2.1    bouyer 	INTERLOCK_ACQUIRE(lkp, LK_SPIN, s);
    796  1.25.2.1    bouyer 
    797  1.25.2.1    bouyer 	cpu_id = cpu_number();
    798  1.25.2.1    bouyer 
    799  1.25.2.1    bouyer #ifdef DIAGNOSTIC
    800  1.25.2.1    bouyer 	if (WEHOLDIT(lkp, LK_NOPROC, cpu_id))
    801  1.25.2.1    bouyer 		panic("spinlock_acquire_count: processor %lu already holds lock\n", (long)cpu_id);
    802  1.25.2.1    bouyer #endif
    803  1.25.2.1    bouyer 	/*
    804  1.25.2.1    bouyer 	 * Try to acquire the want_exclusive flag.
    805  1.25.2.1    bouyer 	 */
    806  1.25.2.1    bouyer 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_flags &
    807  1.25.2.1    bouyer 	    (LK_HAVE_EXCL | LK_WANT_EXCL));
    808  1.25.2.1    bouyer 	lkp->lk_flags |= LK_WANT_EXCL;
    809  1.25.2.1    bouyer 	/*
    810  1.25.2.1    bouyer 	 * Wait for shared locks and upgrades to finish.
    811  1.25.2.1    bouyer 	 */
    812  1.25.2.1    bouyer 	ACQUIRE(lkp, error, LK_SPIN, 0, lkp->lk_sharecount != 0 ||
    813  1.25.2.1    bouyer 	    (lkp->lk_flags & LK_WANT_UPGRADE));
    814  1.25.2.1    bouyer 	lkp->lk_flags &= ~LK_WANT_EXCL;
    815  1.25.2.1    bouyer 	lkp->lk_flags |= LK_HAVE_EXCL;
    816  1.25.2.1    bouyer 	SETHOLDER(lkp, LK_NOPROC, cpu_id);
    817  1.25.2.1    bouyer 	HAVEIT(lkp);
    818  1.25.2.1    bouyer 	if (lkp->lk_exclusivecount != 0)
    819  1.25.2.1    bouyer 		panic("lockmgr: non-zero exclusive count");
    820  1.25.2.1    bouyer 	lkp->lk_exclusivecount = count;
    821  1.25.2.1    bouyer 	lkp->lk_recurselevel = 1;
    822  1.25.2.1    bouyer 	COUNT_CPU(cpu_id, count);
    823  1.25.2.1    bouyer 
    824  1.25.2.1    bouyer 	INTERLOCK_RELEASE(lkp, lkp->lk_flags, s);
    825  1.25.2.1    bouyer }
    826  1.25.2.1    bouyer 
    827  1.25.2.1    bouyer 
    828  1.25.2.1    bouyer 
    829  1.25.2.1    bouyer /*
    830       1.1      fvdl  * Print out information about state of a lock. Used by VOP_PRINT
    831       1.1      fvdl  * routines to display ststus about contained locks.
    832       1.1      fvdl  */
    833       1.2      fvdl void
    834  1.25.2.1    bouyer lockmgr_printinfo(__volatile struct lock *lkp)
    835       1.1      fvdl {
    836       1.1      fvdl 
    837       1.1      fvdl 	if (lkp->lk_sharecount)
    838       1.1      fvdl 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
    839       1.1      fvdl 		    lkp->lk_sharecount);
    840      1.19   thorpej 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
    841      1.19   thorpej 		printf(" lock type %s: EXCL (count %d) by ",
    842      1.19   thorpej 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
    843      1.19   thorpej 		if (lkp->lk_flags & LK_SPIN)
    844      1.19   thorpej 			printf("processor %lu", lkp->lk_cpu);
    845      1.19   thorpej 		else
    846      1.19   thorpej 			printf("pid %d", lkp->lk_lockholder);
    847      1.19   thorpej 	} else
    848      1.19   thorpej 		printf(" not locked");
    849      1.19   thorpej 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
    850       1.1      fvdl 		printf(" with %d pending", lkp->lk_waitcount);
    851       1.1      fvdl }
    852       1.1      fvdl 
    853      1.21   thorpej #if defined(LOCKDEBUG) /* { */
    854      1.21   thorpej TAILQ_HEAD(, simplelock) simplelock_list =
    855      1.21   thorpej     TAILQ_HEAD_INITIALIZER(simplelock_list);
    856      1.21   thorpej 
    857      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    858      1.21   thorpej struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
    859      1.21   thorpej 
    860      1.21   thorpej #define	SLOCK_LIST_LOCK()						\
    861  1.25.2.1    bouyer 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
    862      1.21   thorpej 
    863      1.21   thorpej #define	SLOCK_LIST_UNLOCK()						\
    864  1.25.2.1    bouyer 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
    865      1.21   thorpej 
    866      1.21   thorpej #define	SLOCK_COUNT(x)							\
    867  1.25.2.1    bouyer 	curcpu()->ci_simple_locks += (x)
    868      1.21   thorpej #else
    869      1.21   thorpej u_long simple_locks;
    870      1.21   thorpej 
    871      1.21   thorpej #define	SLOCK_LIST_LOCK()	/* nothing */
    872      1.21   thorpej 
    873      1.21   thorpej #define	SLOCK_LIST_UNLOCK()	/* nothing */
    874      1.21   thorpej 
    875      1.21   thorpej #define	SLOCK_COUNT(x)		simple_locks += (x)
    876      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    877      1.21   thorpej 
    878      1.21   thorpej #ifdef DDB /* { */
    879  1.25.2.1    bouyer #ifdef MULTIPROCESSOR
    880  1.25.2.1    bouyer int simple_lock_debugger = 1;	/* more serious on MP */
    881  1.25.2.1    bouyer #else
    882      1.18       chs int simple_lock_debugger = 0;
    883  1.25.2.1    bouyer #endif
    884      1.21   thorpej #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
    885      1.21   thorpej #else
    886      1.21   thorpej #define	SLOCK_DEBUGGER()	/* nothing */
    887      1.21   thorpej #endif /* } */
    888      1.21   thorpej 
    889  1.25.2.1    bouyer #ifdef MULTIPROCESSOR
    890  1.25.2.1    bouyer #define SLOCK_MP()		lock_printf("on cpu %ld\n", 		\
    891  1.25.2.1    bouyer 				    (u_long) cpu_number())
    892  1.25.2.1    bouyer #else
    893  1.25.2.1    bouyer #define SLOCK_MP()		/* nothing */
    894  1.25.2.1    bouyer #endif
    895  1.25.2.1    bouyer 
    896      1.21   thorpej #define	SLOCK_WHERE(str, alp, id, l)					\
    897      1.21   thorpej do {									\
    898      1.25   thorpej 	lock_printf(str);						\
    899  1.25.2.1    bouyer 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \
    900  1.25.2.1    bouyer 	SLOCK_MP();							\
    901      1.21   thorpej 	if ((alp)->lock_file != NULL)					\
    902      1.25   thorpej 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
    903      1.21   thorpej 		    (alp)->lock_line);					\
    904      1.21   thorpej 	if ((alp)->unlock_file != NULL)					\
    905      1.25   thorpej 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
    906      1.21   thorpej 		    (alp)->unlock_line);				\
    907      1.21   thorpej 	SLOCK_DEBUGGER();						\
    908  1.25.2.1    bouyer } while (/*CONSTCOND*/0)
    909      1.12       chs 
    910       1.1      fvdl /*
    911       1.1      fvdl  * Simple lock functions so that the debugger can see from whence
    912       1.1      fvdl  * they are being called.
    913       1.1      fvdl  */
    914       1.1      fvdl void
    915  1.25.2.1    bouyer simple_lock_init(struct simplelock *alp)
    916       1.1      fvdl {
    917      1.21   thorpej 
    918      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    919  1.25.2.1    bouyer 	__cpu_simple_lock_init(&alp->lock_data);
    920      1.21   thorpej #else
    921  1.25.2.1    bouyer 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
    922      1.21   thorpej #endif /* } */
    923       1.5       chs 	alp->lock_file = NULL;
    924       1.5       chs 	alp->lock_line = 0;
    925       1.5       chs 	alp->unlock_file = NULL;
    926       1.5       chs 	alp->unlock_line = 0;
    927  1.25.2.1    bouyer 	alp->lock_holder = LK_NOCPU;
    928       1.1      fvdl }
    929       1.1      fvdl 
    930       1.1      fvdl void
    931  1.25.2.1    bouyer _simple_lock(__volatile struct simplelock *alp, const char *id, int l)
    932       1.1      fvdl {
    933      1.24   thorpej 	cpuid_t cpu_id = cpu_number();
    934      1.12       chs 	int s;
    935      1.12       chs 
    936  1.25.2.1    bouyer 	s = spllock();
    937      1.21   thorpej 
    938      1.21   thorpej 	/*
    939      1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
    940      1.21   thorpej 	 * don't take any action, and just fall into the normal spin case.
    941      1.21   thorpej 	 */
    942  1.25.2.1    bouyer 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    943      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    944      1.21   thorpej 		if (alp->lock_holder == cpu_id) {
    945      1.21   thorpej 			SLOCK_WHERE("simple_lock: locking against myself\n",
    946      1.21   thorpej 			    alp, id, l);
    947      1.21   thorpej 			goto out;
    948       1.1      fvdl 		}
    949      1.21   thorpej #else
    950      1.21   thorpej 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
    951      1.21   thorpej 		goto out;
    952      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
    953       1.1      fvdl 	}
    954      1.21   thorpej 
    955      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
    956      1.21   thorpej 	/* Acquire the lock before modifying any fields. */
    957  1.25.2.1    bouyer 	__cpu_simple_lock(&alp->lock_data);
    958      1.21   thorpej #else
    959  1.25.2.1    bouyer 	alp->lock_data = __SIMPLELOCK_LOCKED;
    960      1.21   thorpej #endif /* } */
    961      1.21   thorpej 
    962  1.25.2.1    bouyer 	if (alp->lock_holder != LK_NOCPU) {
    963  1.25.2.1    bouyer 		SLOCK_WHERE("simple_lock: uninitialized lock\n",
    964  1.25.2.1    bouyer 		    alp, id, l);
    965  1.25.2.1    bouyer 	}
    966       1.5       chs 	alp->lock_file = id;
    967       1.5       chs 	alp->lock_line = l;
    968      1.21   thorpej 	alp->lock_holder = cpu_id;
    969      1.21   thorpej 
    970      1.21   thorpej 	SLOCK_LIST_LOCK();
    971      1.21   thorpej 	/* XXX Cast away volatile */
    972      1.21   thorpej 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
    973      1.21   thorpej 	SLOCK_LIST_UNLOCK();
    974      1.21   thorpej 
    975      1.21   thorpej 	SLOCK_COUNT(1);
    976      1.21   thorpej 
    977      1.21   thorpej  out:
    978      1.18       chs 	splx(s);
    979       1.1      fvdl }
    980       1.1      fvdl 
    981       1.1      fvdl int
    982  1.25.2.1    bouyer _simple_lock_held(__volatile struct simplelock *alp)
    983  1.25.2.1    bouyer {
    984  1.25.2.1    bouyer 	cpuid_t cpu_id = cpu_number();
    985  1.25.2.1    bouyer 	int s, locked = 0;
    986  1.25.2.1    bouyer 
    987  1.25.2.1    bouyer 	s = spllock();
    988  1.25.2.1    bouyer 
    989  1.25.2.1    bouyer #if defined(MULTIPROCESSOR)
    990  1.25.2.1    bouyer 	if (__cpu_simple_lock_try(&alp->lock_data) == 0)
    991  1.25.2.1    bouyer 		locked = (alp->lock_holder == cpu_id);
    992  1.25.2.1    bouyer 	else
    993  1.25.2.1    bouyer 		__cpu_simple_unlock(&alp->lock_data);
    994  1.25.2.1    bouyer #else
    995  1.25.2.1    bouyer 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
    996  1.25.2.1    bouyer 		locked = 1;
    997  1.25.2.1    bouyer 		KASSERT(alp->lock_holder == cpu_id);
    998  1.25.2.1    bouyer 	}
    999  1.25.2.1    bouyer #endif
   1000  1.25.2.1    bouyer 
   1001  1.25.2.1    bouyer 	splx(s);
   1002  1.25.2.1    bouyer 
   1003  1.25.2.1    bouyer 	return (locked);
   1004  1.25.2.1    bouyer }
   1005  1.25.2.1    bouyer 
   1006  1.25.2.1    bouyer int
   1007  1.25.2.1    bouyer _simple_lock_try(__volatile struct simplelock *alp, const char *id, int l)
   1008       1.1      fvdl {
   1009      1.24   thorpej 	cpuid_t cpu_id = cpu_number();
   1010      1.21   thorpej 	int s, rv = 0;
   1011       1.1      fvdl 
   1012  1.25.2.1    bouyer 	s = spllock();
   1013      1.21   thorpej 
   1014      1.21   thorpej 	/*
   1015      1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
   1016      1.21   thorpej 	 * don't take any action.
   1017      1.21   thorpej 	 */
   1018      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
   1019  1.25.2.1    bouyer 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
   1020      1.21   thorpej 		if (alp->lock_holder == cpu_id)
   1021      1.21   thorpej 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
   1022  1.25.2.1    bouyer 			    alp, id, l);
   1023      1.21   thorpej 		goto out;
   1024      1.21   thorpej 	}
   1025      1.21   thorpej #else
   1026  1.25.2.1    bouyer 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
   1027      1.21   thorpej 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
   1028      1.21   thorpej 		goto out;
   1029      1.18       chs 	}
   1030  1.25.2.1    bouyer 	alp->lock_data = __SIMPLELOCK_LOCKED;
   1031      1.21   thorpej #endif /* MULTIPROCESSOR */ /* } */
   1032      1.21   thorpej 
   1033      1.21   thorpej 	/*
   1034      1.21   thorpej 	 * At this point, we have acquired the lock.
   1035      1.21   thorpej 	 */
   1036      1.21   thorpej 
   1037      1.21   thorpej 	rv = 1;
   1038      1.18       chs 
   1039       1.5       chs 	alp->lock_file = id;
   1040       1.5       chs 	alp->lock_line = l;
   1041      1.21   thorpej 	alp->lock_holder = cpu_id;
   1042      1.21   thorpej 
   1043      1.21   thorpej 	SLOCK_LIST_LOCK();
   1044      1.21   thorpej 	/* XXX Cast away volatile. */
   1045      1.21   thorpej 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
   1046      1.21   thorpej 	SLOCK_LIST_UNLOCK();
   1047      1.21   thorpej 
   1048      1.21   thorpej 	SLOCK_COUNT(1);
   1049      1.21   thorpej 
   1050      1.21   thorpej  out:
   1051      1.12       chs 	splx(s);
   1052      1.21   thorpej 	return (rv);
   1053       1.1      fvdl }
   1054       1.1      fvdl 
   1055       1.1      fvdl void
   1056  1.25.2.1    bouyer _simple_unlock(__volatile struct simplelock *alp, const char *id, int l)
   1057       1.1      fvdl {
   1058      1.12       chs 	int s;
   1059       1.1      fvdl 
   1060  1.25.2.1    bouyer 	s = spllock();
   1061      1.21   thorpej 
   1062      1.21   thorpej 	/*
   1063      1.21   thorpej 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
   1064      1.21   thorpej 	 * the lock, and if we don't, we don't take any action.
   1065      1.21   thorpej 	 */
   1066  1.25.2.1    bouyer 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
   1067      1.21   thorpej 		SLOCK_WHERE("simple_unlock: lock not held\n",
   1068      1.21   thorpej 		    alp, id, l);
   1069      1.21   thorpej 		goto out;
   1070      1.21   thorpej 	}
   1071      1.21   thorpej 
   1072      1.21   thorpej 	SLOCK_LIST_LOCK();
   1073      1.21   thorpej 	TAILQ_REMOVE(&simplelock_list, alp, list);
   1074      1.21   thorpej 	SLOCK_LIST_UNLOCK();
   1075      1.21   thorpej 
   1076      1.21   thorpej 	SLOCK_COUNT(-1);
   1077      1.21   thorpej 
   1078      1.21   thorpej 	alp->list.tqe_next = NULL;	/* sanity */
   1079      1.21   thorpej 	alp->list.tqe_prev = NULL;	/* sanity */
   1080      1.21   thorpej 
   1081       1.5       chs 	alp->unlock_file = id;
   1082       1.5       chs 	alp->unlock_line = l;
   1083      1.21   thorpej 
   1084      1.21   thorpej #if defined(MULTIPROCESSOR) /* { */
   1085  1.25.2.1    bouyer 	alp->lock_holder = LK_NOCPU;
   1086      1.21   thorpej 	/* Now that we've modified all fields, release the lock. */
   1087  1.25.2.1    bouyer 	__cpu_simple_unlock(&alp->lock_data);
   1088      1.21   thorpej #else
   1089  1.25.2.1    bouyer 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
   1090  1.25.2.1    bouyer 	KASSERT(alp->lock_holder == cpu_number());
   1091  1.25.2.1    bouyer 	alp->lock_holder = LK_NOCPU;
   1092      1.21   thorpej #endif /* } */
   1093      1.21   thorpej 
   1094      1.21   thorpej  out:
   1095      1.18       chs 	splx(s);
   1096      1.12       chs }
   1097      1.12       chs 
   1098      1.12       chs void
   1099  1.25.2.1    bouyer simple_lock_dump(void)
   1100      1.12       chs {
   1101      1.12       chs 	struct simplelock *alp;
   1102      1.12       chs 	int s;
   1103      1.12       chs 
   1104  1.25.2.1    bouyer 	s = spllock();
   1105      1.21   thorpej 	SLOCK_LIST_LOCK();
   1106      1.25   thorpej 	lock_printf("all simple locks:\n");
   1107      1.21   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
   1108      1.21   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
   1109      1.25   thorpej 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
   1110      1.21   thorpej 		    alp->lock_file, alp->lock_line);
   1111      1.12       chs 	}
   1112      1.21   thorpej 	SLOCK_LIST_UNLOCK();
   1113      1.12       chs 	splx(s);
   1114      1.12       chs }
   1115      1.12       chs 
   1116      1.12       chs void
   1117  1.25.2.1    bouyer simple_lock_freecheck(void *start, void *end)
   1118      1.12       chs {
   1119      1.12       chs 	struct simplelock *alp;
   1120      1.12       chs 	int s;
   1121      1.12       chs 
   1122  1.25.2.1    bouyer 	s = spllock();
   1123      1.21   thorpej 	SLOCK_LIST_LOCK();
   1124      1.21   thorpej 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
   1125      1.21   thorpej 	     alp = TAILQ_NEXT(alp, list)) {
   1126      1.12       chs 		if ((void *)alp >= start && (void *)alp < end) {
   1127      1.25   thorpej 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
   1128  1.25.2.1    bouyer 			    alp, alp->lock_holder, alp->lock_file,
   1129  1.25.2.1    bouyer 			    alp->lock_line);
   1130  1.25.2.1    bouyer 			SLOCK_DEBUGGER();
   1131  1.25.2.1    bouyer 		}
   1132  1.25.2.1    bouyer 	}
   1133  1.25.2.1    bouyer 	SLOCK_LIST_UNLOCK();
   1134  1.25.2.1    bouyer 	splx(s);
   1135  1.25.2.1    bouyer }
   1136  1.25.2.1    bouyer 
   1137  1.25.2.1    bouyer void
   1138  1.25.2.1    bouyer simple_lock_switchcheck(void)
   1139  1.25.2.1    bouyer {
   1140  1.25.2.1    bouyer 	struct simplelock *alp;
   1141  1.25.2.1    bouyer 	cpuid_t cpu_id = cpu_number();
   1142  1.25.2.1    bouyer 	int s;
   1143  1.25.2.1    bouyer 
   1144  1.25.2.1    bouyer 	/*
   1145  1.25.2.1    bouyer 	 * We must be holding exactly one lock: the sched_lock.
   1146  1.25.2.1    bouyer 	 */
   1147  1.25.2.1    bouyer 
   1148  1.25.2.1    bouyer 	SCHED_ASSERT_LOCKED();
   1149  1.25.2.1    bouyer 
   1150  1.25.2.1    bouyer 	s = spllock();
   1151  1.25.2.1    bouyer 	SLOCK_LIST_LOCK();
   1152  1.25.2.1    bouyer 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
   1153  1.25.2.1    bouyer 	     alp = TAILQ_NEXT(alp, list)) {
   1154  1.25.2.1    bouyer 		if (alp == &sched_lock)
   1155  1.25.2.1    bouyer 			continue;
   1156  1.25.2.1    bouyer 		if (alp->lock_holder == cpu_id) {
   1157  1.25.2.1    bouyer 			lock_printf("switching with held simple_lock %p "
   1158  1.25.2.1    bouyer 			    "CPU %lu %s:%d\n",
   1159      1.21   thorpej 			    alp, alp->lock_holder, alp->lock_file,
   1160      1.21   thorpej 			    alp->lock_line);
   1161      1.21   thorpej 			SLOCK_DEBUGGER();
   1162      1.12       chs 		}
   1163      1.12       chs 	}
   1164      1.21   thorpej 	SLOCK_LIST_UNLOCK();
   1165      1.12       chs 	splx(s);
   1166       1.1      fvdl }
   1167      1.21   thorpej #endif /* LOCKDEBUG */ /* } */
   1168