xref: /netbsd-src/sys/kern/kern_lock.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: kern_lock.c,v 1.30 2000/05/23 05:17:11 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Ross Harvey.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. All advertising materials mentioning features or use of this software
23  *    must display the following acknowledgement:
24  *	This product includes software developed by the NetBSD
25  *	Foundation, Inc. and its contributors.
26  * 4. Neither the name of The NetBSD Foundation nor the names of its
27  *    contributors may be used to endorse or promote products derived
28  *    from this software without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42 
43 /*
44  * Copyright (c) 1995
45  *	The Regents of the University of California.  All rights reserved.
46  *
47  * This code contains ideas from software contributed to Berkeley by
48  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
49  * System project at Carnegie-Mellon University.
50  *
51  * Redistribution and use in source and binary forms, with or without
52  * modification, are permitted provided that the following conditions
53  * are met:
54  * 1. Redistributions of source code must retain the above copyright
55  *    notice, this list of conditions and the following disclaimer.
56  * 2. Redistributions in binary form must reproduce the above copyright
57  *    notice, this list of conditions and the following disclaimer in the
58  *    documentation and/or other materials provided with the distribution.
59  * 3. All advertising materials mentioning features or use of this software
60  *    must display the following acknowledgement:
61  *	This product includes software developed by the University of
62  *	California, Berkeley and its contributors.
63  * 4. Neither the name of the University nor the names of its contributors
64  *    may be used to endorse or promote products derived from this software
65  *    without specific prior written permission.
66  *
67  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77  * SUCH DAMAGE.
78  *
79  *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
80  */
81 
82 #include "opt_multiprocessor.h"
83 #include "opt_lockdebug.h"
84 #include "opt_ddb.h"
85 
86 #include <sys/param.h>
87 #include <sys/proc.h>
88 #include <sys/lock.h>
89 #include <sys/systm.h>
90 #include <machine/cpu.h>
91 
92 #if defined(__HAVE_ATOMIC_OPERATIONS)
93 #include <machine/atomic.h>
94 #endif
95 
96 #if defined(LOCKDEBUG)
97 #include <sys/syslog.h>
98 /*
99  * note that stdarg.h and the ansi style va_start macro is used for both
100  * ansi and traditional c compiles.
101  * XXX: this requires that stdarg.h define: va_alist and va_dcl
102  */
103 #include <machine/stdarg.h>
104 
105 void	lock_printf __P((const char *fmt, ...));
106 
107 int	lock_debug_syslog = 0;	/* defaults to printf, but can be patched */
108 #endif
109 
110 /*
111  * Locking primitives implementation.
112  * Locks provide shared/exclusive sychronization.
113  */
114 
115 #if defined(LOCKDEBUG) || defined(DIAGNOSTIC) /* { */
116 #if defined(MULTIPROCESSOR) /* { */
117 #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
118 #define	COUNT_CPU(cpu_id, x)						\
119 	atomic_add_ulong(&curcpu()->ci_spin_locks, (x))
120 #else
121 #define	COUNT_CPU(cpu_id, x)	/* not safe */
122 #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
123 #else
124 u_long	spin_locks;
125 #define	COUNT_CPU(cpu_id, x)	spin_locks += (x)
126 #endif /* MULTIPROCESSOR */ /* } */
127 
128 #define	COUNT(lkp, p, cpu_id, x)					\
129 do {									\
130 	if ((lkp)->lk_flags & LK_SPIN)					\
131 		COUNT_CPU((cpu_id), (x));				\
132 	else								\
133 		(p)->p_locks += (x);					\
134 } while (/*CONSTCOND*/0)
135 #else
136 #define COUNT(lkp, p, cpu_id, x)
137 #endif /* LOCKDEBUG || DIAGNOSTIC */ /* } */
138 
139 /*
140  * Acquire a resource.
141  */
142 #define ACQUIRE(lkp, error, extflags, drain, wanted)			\
143 	if ((extflags) & LK_SPIN) {					\
144 		int interlocked;					\
145 									\
146 		if ((drain) == 0)					\
147 			(lkp)->lk_waitcount++;				\
148 		for (interlocked = 1;;) {				\
149 			if (wanted) {					\
150 				if (interlocked) {			\
151 					simple_unlock(&(lkp)->lk_interlock); \
152 					interlocked = 0;		\
153 				}					\
154 			} else if (interlocked) {			\
155 				break;					\
156 			} else {					\
157 				simple_lock(&(lkp)->lk_interlock);	\
158 				interlocked = 1;			\
159 			}						\
160 		}							\
161 		if ((drain) == 0)					\
162 			(lkp)->lk_waitcount--;				\
163 		KASSERT((wanted) == 0);					\
164 		error = 0;	/* sanity */				\
165 	} else {							\
166 		for (error = 0; wanted; ) {				\
167 			if ((drain))					\
168 				(lkp)->lk_flags |= LK_WAITDRAIN;	\
169 			else						\
170 				(lkp)->lk_waitcount++;			\
171 			simple_unlock(&(lkp)->lk_interlock);		\
172 			/* XXX Cast away volatile. */			\
173 			error = tsleep((drain) ? &(lkp)->lk_flags :	\
174 			    (void *)(lkp), (lkp)->lk_prio,		\
175 			    (lkp)->lk_wmesg, (lkp)->lk_timo);		\
176 			simple_lock(&(lkp)->lk_interlock);		\
177 			if ((drain) == 0)				\
178 				(lkp)->lk_waitcount--;			\
179 			if (error)					\
180 				break;					\
181 			if ((extflags) & LK_SLEEPFAIL) {		\
182 				error = ENOLCK;				\
183 				break;					\
184 			}						\
185 		}							\
186 	}
187 
188 #define	SETHOLDER(lkp, pid, cpu_id)					\
189 do {									\
190 	if ((lkp)->lk_flags & LK_SPIN)					\
191 		(lkp)->lk_cpu = cpu_id;					\
192 	else								\
193 		(lkp)->lk_lockholder = pid;				\
194 } while (/*CONSTCOND*/0)
195 
196 #define	WEHOLDIT(lkp, pid, cpu_id)					\
197 	(((lkp)->lk_flags & LK_SPIN) != 0 ?				\
198 	 ((lkp)->lk_cpu == (cpu_id)) : ((lkp)->lk_lockholder == (pid)))
199 
200 #define	WAKEUP_WAITER(lkp)						\
201 do {									\
202 	if (((lkp)->lk_flags & LK_SPIN) == 0 && (lkp)->lk_waitcount) {	\
203 		/* XXX Cast away volatile. */				\
204 		wakeup_one((void *)(lkp));				\
205 	}								\
206 } while (/*CONSTCOND*/0)
207 
208 #if defined(LOCKDEBUG) /* { */
209 #if defined(MULTIPROCESSOR) /* { */
210 struct simplelock spinlock_list_slock = SIMPLELOCK_INITIALIZER;
211 
212 #define	SPINLOCK_LIST_LOCK()						\
213 	__cpu_simple_lock(&spinlock_list_slock.lock_data)
214 
215 #define	SPINLOCK_LIST_UNLOCK()						\
216 	__cpu_simple_unlock(&spinlock_list_slock.lock_data)
217 #else
218 #define	SPINLOCK_LIST_LOCK()	/* nothing */
219 
220 #define	SPINLOCK_LIST_UNLOCK()	/* nothing */
221 #endif /* MULTIPROCESSOR */ /* } */
222 
223 TAILQ_HEAD(, lock) spinlock_list =
224     TAILQ_HEAD_INITIALIZER(spinlock_list);
225 
226 #define	HAVEIT(lkp)							\
227 do {									\
228 	if ((lkp)->lk_flags & LK_SPIN) {				\
229 		int s = splhigh();					\
230 		SPINLOCK_LIST_LOCK();					\
231 		/* XXX Cast away volatile. */				\
232 		TAILQ_INSERT_TAIL(&spinlock_list, (struct lock *)(lkp),	\
233 		    lk_list);						\
234 		SPINLOCK_LIST_UNLOCK();					\
235 		splx(s);						\
236 	}								\
237 } while (/*CONSTCOND*/0)
238 
239 #define	DONTHAVEIT(lkp)							\
240 do {									\
241 	if ((lkp)->lk_flags & LK_SPIN) {				\
242 		int s = splhigh();					\
243 		SPINLOCK_LIST_LOCK();					\
244 		/* XXX Cast away volatile. */				\
245 		TAILQ_REMOVE(&spinlock_list, (struct lock *)(lkp),	\
246 		    lk_list);						\
247 		SPINLOCK_LIST_UNLOCK();					\
248 		splx(s);						\
249 	}								\
250 } while (/*CONSTCOND*/0)
251 #else
252 #define	HAVEIT(lkp)		/* nothing */
253 
254 #define	DONTHAVEIT(lkp)		/* nothing */
255 #endif /* LOCKDEBUG */ /* } */
256 
257 #if defined(LOCKDEBUG)
258 /*
259  * Lock debug printing routine; can be configured to print to console
260  * or log to syslog.
261  */
262 void
263 #ifdef __STDC__
264 lock_printf(const char *fmt, ...)
265 #else
266 lock_printf(fmt, va_alist)
267 	char *fmt;
268 	va_dcl
269 #endif
270 {
271 	va_list ap;
272 
273 	va_start(ap, fmt);
274 	if (lock_debug_syslog)
275 		vlog(LOG_DEBUG, fmt, ap);
276 	else
277 		vprintf(fmt, ap);
278 	va_end(ap);
279 }
280 #endif /* LOCKDEBUG */
281 
282 /*
283  * Initialize a lock; required before use.
284  */
285 void
286 lockinit(lkp, prio, wmesg, timo, flags)
287 	struct lock *lkp;
288 	int prio;
289 	const char *wmesg;
290 	int timo;
291 	int flags;
292 {
293 
294 	memset(lkp, 0, sizeof(struct lock));
295 	simple_lock_init(&lkp->lk_interlock);
296 	lkp->lk_flags = flags & LK_EXTFLG_MASK;
297 	if (flags & LK_SPIN)
298 		lkp->lk_cpu = LK_NOCPU;
299 	else {
300 		lkp->lk_lockholder = LK_NOPROC;
301 		lkp->lk_prio = prio;
302 		lkp->lk_timo = timo;
303 	}
304 	lkp->lk_wmesg = wmesg;	/* just a name for spin locks */
305 }
306 
307 /*
308  * Determine the status of a lock.
309  */
310 int
311 lockstatus(lkp)
312 	struct lock *lkp;
313 {
314 	int lock_type = 0;
315 
316 	simple_lock(&lkp->lk_interlock);
317 	if (lkp->lk_exclusivecount != 0)
318 		lock_type = LK_EXCLUSIVE;
319 	else if (lkp->lk_sharecount != 0)
320 		lock_type = LK_SHARED;
321 	simple_unlock(&lkp->lk_interlock);
322 	return (lock_type);
323 }
324 
325 /*
326  * Set, change, or release a lock.
327  *
328  * Shared requests increment the shared count. Exclusive requests set the
329  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
330  * accepted shared locks and shared-to-exclusive upgrades to go away.
331  */
332 int
333 lockmgr(lkp, flags, interlkp)
334 	__volatile struct lock *lkp;
335 	u_int flags;
336 	struct simplelock *interlkp;
337 {
338 	int error;
339 	pid_t pid;
340 	int extflags;
341 	cpuid_t cpu_id;
342 	struct proc *p = curproc;
343 
344 	error = 0;
345 
346 	simple_lock(&lkp->lk_interlock);
347 	if (flags & LK_INTERLOCK)
348 		simple_unlock(interlkp);
349 	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
350 
351 #ifdef DIAGNOSTIC /* { */
352 	/*
353 	 * Don't allow spins on sleep locks and don't allow sleeps
354 	 * on spin locks.
355 	 */
356 	if ((flags ^ lkp->lk_flags) & LK_SPIN)
357 		panic("lockmgr: sleep/spin mismatch\n");
358 #endif /* } */
359 
360 	if (extflags & LK_SPIN)
361 		pid = LK_KERNPROC;
362 	else {
363 #ifdef DIAGNOSTIC /* { */
364 		if (p == NULL)
365 			panic("lockmgr: no context");
366 #endif /* } */
367 		pid = p->p_pid;
368 	}
369 	cpu_id = cpu_number();
370 
371 	/*
372 	 * Once a lock has drained, the LK_DRAINING flag is set and an
373 	 * exclusive lock is returned. The only valid operation thereafter
374 	 * is a single release of that exclusive lock. This final release
375 	 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any
376 	 * further requests of any sort will result in a panic. The bits
377 	 * selected for these two flags are chosen so that they will be set
378 	 * in memory that is freed (freed memory is filled with 0xdeadbeef).
379 	 * The final release is permitted to give a new lease on life to
380 	 * the lock by specifying LK_REENABLE.
381 	 */
382 	if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) {
383 #ifdef DIAGNOSTIC /* { */
384 		if (lkp->lk_flags & LK_DRAINED)
385 			panic("lockmgr: using decommissioned lock");
386 		if ((flags & LK_TYPE_MASK) != LK_RELEASE ||
387 		    WEHOLDIT(lkp, pid, cpu_id) == 0)
388 			panic("lockmgr: non-release on draining lock: %d\n",
389 			    flags & LK_TYPE_MASK);
390 #endif /* DIAGNOSTIC */ /* } */
391 		lkp->lk_flags &= ~LK_DRAINING;
392 		if ((flags & LK_REENABLE) == 0)
393 			lkp->lk_flags |= LK_DRAINED;
394 	}
395 
396 	switch (flags & LK_TYPE_MASK) {
397 
398 	case LK_SHARED:
399 		if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
400 			/*
401 			 * If just polling, check to see if we will block.
402 			 */
403 			if ((extflags & LK_NOWAIT) && (lkp->lk_flags &
404 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) {
405 				error = EBUSY;
406 				break;
407 			}
408 			/*
409 			 * Wait for exclusive locks and upgrades to clear.
410 			 */
411 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
412 			    (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE));
413 			if (error)
414 				break;
415 			lkp->lk_sharecount++;
416 			COUNT(lkp, p, cpu_id, 1);
417 			break;
418 		}
419 		/*
420 		 * We hold an exclusive lock, so downgrade it to shared.
421 		 * An alternative would be to fail with EDEADLK.
422 		 */
423 		lkp->lk_sharecount++;
424 		COUNT(lkp, p, cpu_id, 1);
425 		/* fall into downgrade */
426 
427 	case LK_DOWNGRADE:
428 		if (WEHOLDIT(lkp, pid, cpu_id) == 0 ||
429 		    lkp->lk_exclusivecount == 0)
430 			panic("lockmgr: not holding exclusive lock");
431 		lkp->lk_sharecount += lkp->lk_exclusivecount;
432 		lkp->lk_exclusivecount = 0;
433 		lkp->lk_recurselevel = 0;
434 		lkp->lk_flags &= ~LK_HAVE_EXCL;
435 		SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
436 		DONTHAVEIT(lkp);
437 		WAKEUP_WAITER(lkp);
438 		break;
439 
440 	case LK_EXCLUPGRADE:
441 		/*
442 		 * If another process is ahead of us to get an upgrade,
443 		 * then we want to fail rather than have an intervening
444 		 * exclusive access.
445 		 */
446 		if (lkp->lk_flags & LK_WANT_UPGRADE) {
447 			lkp->lk_sharecount--;
448 			COUNT(lkp, p, cpu_id, -1);
449 			error = EBUSY;
450 			break;
451 		}
452 		/* fall into normal upgrade */
453 
454 	case LK_UPGRADE:
455 		/*
456 		 * Upgrade a shared lock to an exclusive one. If another
457 		 * shared lock has already requested an upgrade to an
458 		 * exclusive lock, our shared lock is released and an
459 		 * exclusive lock is requested (which will be granted
460 		 * after the upgrade). If we return an error, the file
461 		 * will always be unlocked.
462 		 */
463 		if (WEHOLDIT(lkp, pid, cpu_id) || lkp->lk_sharecount <= 0)
464 			panic("lockmgr: upgrade exclusive lock");
465 		lkp->lk_sharecount--;
466 		COUNT(lkp, p, cpu_id, -1);
467 		/*
468 		 * If we are just polling, check to see if we will block.
469 		 */
470 		if ((extflags & LK_NOWAIT) &&
471 		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
472 		     lkp->lk_sharecount > 1)) {
473 			error = EBUSY;
474 			break;
475 		}
476 		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
477 			/*
478 			 * We are first shared lock to request an upgrade, so
479 			 * request upgrade and wait for the shared count to
480 			 * drop to zero, then take exclusive lock.
481 			 */
482 			lkp->lk_flags |= LK_WANT_UPGRADE;
483 			ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount);
484 			lkp->lk_flags &= ~LK_WANT_UPGRADE;
485 			if (error)
486 				break;
487 			lkp->lk_flags |= LK_HAVE_EXCL;
488 			SETHOLDER(lkp, pid, cpu_id);
489 			HAVEIT(lkp);
490 			if (lkp->lk_exclusivecount != 0)
491 				panic("lockmgr: non-zero exclusive count");
492 			lkp->lk_exclusivecount = 1;
493 			if (extflags & LK_SETRECURSE)
494 				lkp->lk_recurselevel = 1;
495 			COUNT(lkp, p, cpu_id, 1);
496 			break;
497 		}
498 		/*
499 		 * Someone else has requested upgrade. Release our shared
500 		 * lock, awaken upgrade requestor if we are the last shared
501 		 * lock, then request an exclusive lock.
502 		 */
503 		if (lkp->lk_sharecount == 0)
504 			WAKEUP_WAITER(lkp);
505 		/* fall into exclusive request */
506 
507 	case LK_EXCLUSIVE:
508 		if (WEHOLDIT(lkp, pid, cpu_id)) {
509 			/*
510 			 * Recursive lock.
511 			 */
512 			if ((extflags & LK_CANRECURSE) == 0 &&
513 			     lkp->lk_recurselevel == 0) {
514 				if (extflags & LK_RECURSEFAIL) {
515 					error = EDEADLK;
516 					break;
517 				} else
518 					panic("lockmgr: locking against myself");
519 			}
520 			lkp->lk_exclusivecount++;
521 			if (extflags & LK_SETRECURSE &&
522 			    lkp->lk_recurselevel == 0)
523 				lkp->lk_recurselevel = lkp->lk_exclusivecount;
524 			COUNT(lkp, p, cpu_id, 1);
525 			break;
526 		}
527 		/*
528 		 * If we are just polling, check to see if we will sleep.
529 		 */
530 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
531 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
532 		     lkp->lk_sharecount != 0)) {
533 			error = EBUSY;
534 			break;
535 		}
536 		/*
537 		 * Try to acquire the want_exclusive flag.
538 		 */
539 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_flags &
540 		    (LK_HAVE_EXCL | LK_WANT_EXCL));
541 		if (error)
542 			break;
543 		lkp->lk_flags |= LK_WANT_EXCL;
544 		/*
545 		 * Wait for shared locks and upgrades to finish.
546 		 */
547 		ACQUIRE(lkp, error, extflags, 0, lkp->lk_sharecount != 0 ||
548 		       (lkp->lk_flags & LK_WANT_UPGRADE));
549 		lkp->lk_flags &= ~LK_WANT_EXCL;
550 		if (error)
551 			break;
552 		lkp->lk_flags |= LK_HAVE_EXCL;
553 		SETHOLDER(lkp, pid, cpu_id);
554 		HAVEIT(lkp);
555 		if (lkp->lk_exclusivecount != 0)
556 			panic("lockmgr: non-zero exclusive count");
557 		lkp->lk_exclusivecount = 1;
558 		if (extflags & LK_SETRECURSE)
559 			lkp->lk_recurselevel = 1;
560 		COUNT(lkp, p, cpu_id, 1);
561 		break;
562 
563 	case LK_RELEASE:
564 		if (lkp->lk_exclusivecount != 0) {
565 			if (WEHOLDIT(lkp, pid, cpu_id) == 0) {
566 				if (lkp->lk_flags & LK_SPIN) {
567 					panic("lockmgr: processor %lu, not "
568 					    "exclusive lock holder %lu "
569 					    "unlocking", cpu_id, lkp->lk_cpu);
570 				} else {
571 					panic("lockmgr: pid %d, not "
572 					    "exclusive lock holder %d "
573 					    "unlocking", pid,
574 					    lkp->lk_lockholder);
575 				}
576 			}
577 			if (lkp->lk_exclusivecount == lkp->lk_recurselevel)
578 				lkp->lk_recurselevel = 0;
579 			lkp->lk_exclusivecount--;
580 			COUNT(lkp, p, cpu_id, -1);
581 			if (lkp->lk_exclusivecount == 0) {
582 				lkp->lk_flags &= ~LK_HAVE_EXCL;
583 				SETHOLDER(lkp, LK_NOPROC, LK_NOCPU);
584 				DONTHAVEIT(lkp);
585 			}
586 		} else if (lkp->lk_sharecount != 0) {
587 			lkp->lk_sharecount--;
588 			COUNT(lkp, p, cpu_id, -1);
589 		}
590 		WAKEUP_WAITER(lkp);
591 		break;
592 
593 	case LK_DRAIN:
594 		/*
595 		 * Check that we do not already hold the lock, as it can
596 		 * never drain if we do. Unfortunately, we have no way to
597 		 * check for holding a shared lock, but at least we can
598 		 * check for an exclusive one.
599 		 */
600 		if (WEHOLDIT(lkp, pid, cpu_id))
601 			panic("lockmgr: draining against myself");
602 		/*
603 		 * If we are just polling, check to see if we will sleep.
604 		 */
605 		if ((extflags & LK_NOWAIT) && ((lkp->lk_flags &
606 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
607 		     lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) {
608 			error = EBUSY;
609 			break;
610 		}
611 		ACQUIRE(lkp, error, extflags, 1,
612 		    ((lkp->lk_flags &
613 		     (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) ||
614 		     lkp->lk_sharecount != 0 ||
615 		     lkp->lk_waitcount != 0));
616 		if (error)
617 			break;
618 		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
619 		SETHOLDER(lkp, pid, cpu_id);
620 		HAVEIT(lkp);
621 		lkp->lk_exclusivecount = 1;
622 		/* XXX unlikely that we'd want this */
623 		if (extflags & LK_SETRECURSE)
624 			lkp->lk_recurselevel = 1;
625 		COUNT(lkp, p, cpu_id, 1);
626 		break;
627 
628 	default:
629 		simple_unlock(&lkp->lk_interlock);
630 		panic("lockmgr: unknown locktype request %d",
631 		    flags & LK_TYPE_MASK);
632 		/* NOTREACHED */
633 	}
634 	if ((lkp->lk_flags & (LK_WAITDRAIN|LK_SPIN)) == LK_WAITDRAIN &&
635 	    ((lkp->lk_flags &
636 	      (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 &&
637 	     lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) {
638 		lkp->lk_flags &= ~LK_WAITDRAIN;
639 		wakeup_one((void *)&lkp->lk_flags);
640 	}
641 	simple_unlock(&lkp->lk_interlock);
642 	return (error);
643 }
644 
645 /*
646  * Print out information about state of a lock. Used by VOP_PRINT
647  * routines to display ststus about contained locks.
648  */
649 void
650 lockmgr_printinfo(lkp)
651 	__volatile struct lock *lkp;
652 {
653 
654 	if (lkp->lk_sharecount)
655 		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
656 		    lkp->lk_sharecount);
657 	else if (lkp->lk_flags & LK_HAVE_EXCL) {
658 		printf(" lock type %s: EXCL (count %d) by ",
659 		    lkp->lk_wmesg, lkp->lk_exclusivecount);
660 		if (lkp->lk_flags & LK_SPIN)
661 			printf("processor %lu", lkp->lk_cpu);
662 		else
663 			printf("pid %d", lkp->lk_lockholder);
664 	} else
665 		printf(" not locked");
666 	if ((lkp->lk_flags & LK_SPIN) == 0 && lkp->lk_waitcount > 0)
667 		printf(" with %d pending", lkp->lk_waitcount);
668 }
669 
670 #if defined(LOCKDEBUG) /* { */
671 TAILQ_HEAD(, simplelock) simplelock_list =
672     TAILQ_HEAD_INITIALIZER(simplelock_list);
673 
674 #if defined(MULTIPROCESSOR) /* { */
675 struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER;
676 
677 #define	SLOCK_LIST_LOCK()						\
678 	__cpu_simple_lock(&simplelock_list_slock.lock_data)
679 
680 #define	SLOCK_LIST_UNLOCK()						\
681 	__cpu_simple_unlock(&simplelock_list_slock.lock_data)
682 
683 #if defined(__HAVE_ATOMIC_OPERATIONS) /* { */
684 #define	SLOCK_COUNT(x)							\
685 	atomic_add_ulong(&curcpu()->ci_simple_locks, (x))
686 #else
687 #define	SLOCK_COUNT(x)		/* not safe */
688 #endif /* __HAVE_ATOMIC_OPERATIONS */ /* } */
689 #else
690 u_long simple_locks;
691 
692 #define	SLOCK_LIST_LOCK()	/* nothing */
693 
694 #define	SLOCK_LIST_UNLOCK()	/* nothing */
695 
696 #define	SLOCK_COUNT(x)		simple_locks += (x)
697 #endif /* MULTIPROCESSOR */ /* } */
698 
699 #ifdef DDB /* { */
700 int simple_lock_debugger = 0;
701 #define	SLOCK_DEBUGGER()	if (simple_lock_debugger) Debugger()
702 #else
703 #define	SLOCK_DEBUGGER()	/* nothing */
704 #endif /* } */
705 
706 #ifdef MULTIPROCESSOR
707 #define SLOCK_MP()		lock_printf("on cpu %d\n", cpu_number())
708 #else
709 #define SLOCK_MP()		/* nothing */
710 #endif
711 
712 #define	SLOCK_WHERE(str, alp, id, l)					\
713 do {									\
714 	lock_printf(str);						\
715 	lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l));		\
716 	SLOCK_MP();							\
717 	if ((alp)->lock_file != NULL)					\
718 		lock_printf("last locked: %s:%d\n", (alp)->lock_file,	\
719 		    (alp)->lock_line);					\
720 	if ((alp)->unlock_file != NULL)					\
721 		lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \
722 		    (alp)->unlock_line);				\
723 	SLOCK_DEBUGGER();						\
724 } while (/*CONSTCOND*/0)
725 
726 /*
727  * Simple lock functions so that the debugger can see from whence
728  * they are being called.
729  */
730 void
731 simple_lock_init(alp)
732 	struct simplelock *alp;
733 {
734 
735 #if defined(MULTIPROCESSOR) /* { */
736 	__cpu_simple_lock_init(&alp->lock_data);
737 #else
738 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
739 #endif /* } */
740 	alp->lock_file = NULL;
741 	alp->lock_line = 0;
742 	alp->unlock_file = NULL;
743 	alp->unlock_line = 0;
744 	alp->lock_holder = 0;
745 }
746 
747 void
748 _simple_lock(alp, id, l)
749 	__volatile struct simplelock *alp;
750 	const char *id;
751 	int l;
752 {
753 	cpuid_t cpu_id = cpu_number();
754 	int s;
755 
756 	s = splhigh();
757 
758 	/*
759 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
760 	 * don't take any action, and just fall into the normal spin case.
761 	 */
762 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
763 #if defined(MULTIPROCESSOR) /* { */
764 		if (alp->lock_holder == cpu_id) {
765 			SLOCK_WHERE("simple_lock: locking against myself\n",
766 			    alp, id, l);
767 			goto out;
768 		}
769 #else
770 		SLOCK_WHERE("simple_lock: lock held\n", alp, id, l);
771 		goto out;
772 #endif /* MULTIPROCESSOR */ /* } */
773 	}
774 
775 #if defined(MULTIPROCESSOR) /* { */
776 	/* Acquire the lock before modifying any fields. */
777 	__cpu_simple_lock(&alp->lock_data);
778 #else
779 	alp->lock_data = __SIMPLELOCK_LOCKED;
780 #endif /* } */
781 
782 	alp->lock_file = id;
783 	alp->lock_line = l;
784 	alp->lock_holder = cpu_id;
785 
786 	SLOCK_LIST_LOCK();
787 	/* XXX Cast away volatile */
788 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
789 	SLOCK_LIST_UNLOCK();
790 
791 	SLOCK_COUNT(1);
792 
793  out:
794 	splx(s);
795 }
796 
797 int
798 _simple_lock_try(alp, id, l)
799 	__volatile struct simplelock *alp;
800 	const char *id;
801 	int l;
802 {
803 	cpuid_t cpu_id = cpu_number();
804 	int s, rv = 0;
805 
806 	s = splhigh();
807 
808 	/*
809 	 * MULTIPROCESSOR case: This is `safe' since if it's not us, we
810 	 * don't take any action.
811 	 */
812 #if defined(MULTIPROCESSOR) /* { */
813 	if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) {
814 		if (alp->lock_holder == cpu_id)
815 			SLOCK_WHERE("simple_lock_try: locking against myself\n",
816 			    alp, id, l);
817 		goto out;
818 	}
819 #else
820 	if (alp->lock_data == __SIMPLELOCK_LOCKED) {
821 		SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l);
822 		goto out;
823 	}
824 	alp->lock_data = __SIMPLELOCK_LOCKED;
825 #endif /* MULTIPROCESSOR */ /* } */
826 
827 	/*
828 	 * At this point, we have acquired the lock.
829 	 */
830 
831 	rv = 1;
832 
833 	alp->lock_file = id;
834 	alp->lock_line = l;
835 	alp->lock_holder = cpu_id;
836 
837 	SLOCK_LIST_LOCK();
838 	/* XXX Cast away volatile. */
839 	TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list);
840 	SLOCK_LIST_UNLOCK();
841 
842 	SLOCK_COUNT(1);
843 
844  out:
845 	splx(s);
846 	return (rv);
847 }
848 
849 void
850 _simple_unlock(alp, id, l)
851 	__volatile struct simplelock *alp;
852 	const char *id;
853 	int l;
854 {
855 	int s;
856 
857 	s = splhigh();
858 
859 	/*
860 	 * MULTIPROCESSOR case: This is `safe' because we think we hold
861 	 * the lock, and if we don't, we don't take any action.
862 	 */
863 	if (alp->lock_data == __SIMPLELOCK_UNLOCKED) {
864 		SLOCK_WHERE("simple_unlock: lock not held\n",
865 		    alp, id, l);
866 		goto out;
867 	}
868 
869 	SLOCK_LIST_LOCK();
870 	TAILQ_REMOVE(&simplelock_list, alp, list);
871 	SLOCK_LIST_UNLOCK();
872 
873 	SLOCK_COUNT(-1);
874 
875 	alp->list.tqe_next = NULL;	/* sanity */
876 	alp->list.tqe_prev = NULL;	/* sanity */
877 
878 	alp->unlock_file = id;
879 	alp->unlock_line = l;
880 
881 #if defined(MULTIPROCESSOR) /* { */
882 	alp->lock_holder = LK_NOCPU;
883 	/* Now that we've modified all fields, release the lock. */
884 	__cpu_simple_unlock(&alp->lock_data);
885 #else
886 	alp->lock_data = __SIMPLELOCK_UNLOCKED;
887 #endif /* } */
888 
889  out:
890 	splx(s);
891 }
892 
893 void
894 simple_lock_dump()
895 {
896 	struct simplelock *alp;
897 	int s;
898 
899 	s = splhigh();
900 	SLOCK_LIST_LOCK();
901 	lock_printf("all simple locks:\n");
902 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
903 	     alp = TAILQ_NEXT(alp, list)) {
904 		lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder,
905 		    alp->lock_file, alp->lock_line);
906 	}
907 	SLOCK_LIST_UNLOCK();
908 	splx(s);
909 }
910 
911 void
912 simple_lock_freecheck(start, end)
913 	void *start, *end;
914 {
915 	struct simplelock *alp;
916 	int s;
917 
918 	s = splhigh();
919 	SLOCK_LIST_LOCK();
920 	for (alp = TAILQ_FIRST(&simplelock_list); alp != NULL;
921 	     alp = TAILQ_NEXT(alp, list)) {
922 		if ((void *)alp >= start && (void *)alp < end) {
923 			lock_printf("freeing simple_lock %p CPU %lu %s:%d\n",
924 			    alp, alp->lock_holder, alp->lock_file,
925 			    alp->lock_line);
926 			SLOCK_DEBUGGER();
927 		}
928 	}
929 	SLOCK_LIST_UNLOCK();
930 	splx(s);
931 }
932 #endif /* LOCKDEBUG */ /* } */
933