kern_lock.c revision 176249
1/*-
2 * Copyright (c) 1995
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Copyright (C) 1997
6 *	John S. Dyson.  All rights reserved.
7 *
8 * This code contains ideas from software contributed to Berkeley by
9 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
10 * System project at Carnegie-Mellon University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 *    must display the following acknowledgement:
22 *	This product includes software developed by the University of
23 *	California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 *    may be used to endorse or promote products derived from this software
26 *    without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 *	@(#)kern_lock.c	8.18 (Berkeley) 5/21/95
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/kern/kern_lock.c 176249 2008-02-13 20:44:19Z attilio $");
45
46#include "opt_ddb.h"
47#include "opt_global.h"
48
49#include <sys/param.h>
50#include <sys/kdb.h>
51#include <sys/kernel.h>
52#include <sys/ktr.h>
53#include <sys/lock.h>
54#include <sys/lockmgr.h>
55#include <sys/mutex.h>
56#include <sys/proc.h>
57#include <sys/systm.h>
58#include <sys/lock_profile.h>
59#ifdef DEBUG_LOCKS
60#include <sys/stack.h>
61#endif
62
63#define	LOCKMGR_TRYOP(x)	((x) & LK_NOWAIT)
64#define	LOCKMGR_TRYW(x)		(LOCKMGR_TRYOP((x)) ? LOP_TRYLOCK : 0)
65#define	LOCKMGR_UNHELD(x)	(((x) & (LK_HAVE_EXCL | LK_SHARE_NONZERO)) == 0)
66#define	LOCKMGR_NOTOWNER(td)	((td) != curthread && (td) != LK_KERNPROC)
67
68static void	assert_lockmgr(struct lock_object *lock, int what);
69#ifdef DDB
70#include <ddb/ddb.h>
71static void	db_show_lockmgr(struct lock_object *lock);
72#endif
73static void	lock_lockmgr(struct lock_object *lock, int how);
74static int	unlock_lockmgr(struct lock_object *lock);
75
76struct lock_class lock_class_lockmgr = {
77	.lc_name = "lockmgr",
78	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
79	.lc_assert = assert_lockmgr,
80#ifdef DDB
81	.lc_ddb_show = db_show_lockmgr,
82#endif
83	.lc_lock = lock_lockmgr,
84	.lc_unlock = unlock_lockmgr,
85};
86
87#ifndef INVARIANTS
88#define	_lockmgr_assert(lkp, what, file, line)
89#endif
90
91/*
92 * Locking primitives implementation.
93 * Locks provide shared/exclusive sychronization.
94 */
95
96void
97assert_lockmgr(struct lock_object *lock, int what)
98{
99
100	panic("lockmgr locks do not support assertions");
101}
102
103void
104lock_lockmgr(struct lock_object *lock, int how)
105{
106
107	panic("lockmgr locks do not support sleep interlocking");
108}
109
110int
111unlock_lockmgr(struct lock_object *lock)
112{
113
114	panic("lockmgr locks do not support sleep interlocking");
115}
116
117#define	COUNT(td, x)	((td)->td_locks += (x))
118#define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
119	LK_SHARE_NONZERO | LK_WAIT_NONZERO)
120
121static int acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime);
122static int acquiredrain(struct lock *lkp, int extflags) ;
123
124static __inline void
125sharelock(struct thread *td, struct lock *lkp, int incr) {
126	lkp->lk_flags |= LK_SHARE_NONZERO;
127	lkp->lk_sharecount += incr;
128	COUNT(td, incr);
129}
130
131static __inline void
132shareunlock(struct thread *td, struct lock *lkp, int decr) {
133
134	KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
135
136	COUNT(td, -decr);
137	if (lkp->lk_sharecount == decr) {
138		lkp->lk_flags &= ~LK_SHARE_NONZERO;
139		if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
140			wakeup(lkp);
141		}
142		lkp->lk_sharecount = 0;
143	} else {
144		lkp->lk_sharecount -= decr;
145	}
146}
147
148static int
149acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime)
150{
151	struct lock *lkp = *lkpp;
152	int error;
153	CTR3(KTR_LOCK,
154	    "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x",
155	    lkp, extflags, wanted);
156
157	if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted))
158		return EBUSY;
159	error = 0;
160	if ((lkp->lk_flags & wanted) != 0)
161		lock_profile_obtain_lock_failed(&lkp->lk_object, contested, waittime);
162
163	while ((lkp->lk_flags & wanted) != 0) {
164		CTR2(KTR_LOCK,
165		    "acquire(): lkp == %p, lk_flags == 0x%x sleeping",
166		    lkp, lkp->lk_flags);
167		lkp->lk_flags |= LK_WAIT_NONZERO;
168		lkp->lk_waitcount++;
169		error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
170		    lkp->lk_wmesg,
171		    ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
172		lkp->lk_waitcount--;
173		if (lkp->lk_waitcount == 0)
174			lkp->lk_flags &= ~LK_WAIT_NONZERO;
175		if (error)
176			break;
177		if (extflags & LK_SLEEPFAIL) {
178			error = ENOLCK;
179			break;
180		}
181		if (lkp->lk_newlock != NULL) {
182			mtx_lock(lkp->lk_newlock->lk_interlock);
183			mtx_unlock(lkp->lk_interlock);
184			if (lkp->lk_waitcount == 0)
185				wakeup((void *)(&lkp->lk_newlock));
186			*lkpp = lkp = lkp->lk_newlock;
187		}
188	}
189	mtx_assert(lkp->lk_interlock, MA_OWNED);
190	return (error);
191}
192
193/*
194 * Set, change, or release a lock.
195 *
196 * Shared requests increment the shared count. Exclusive requests set the
197 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
198 * accepted shared locks and shared-to-exclusive upgrades to go away.
199 */
200int
201_lockmgr(struct lock *lkp, u_int flags, struct mtx *interlkp, char *file,
202    int line)
203
204{
205	struct thread *td;
206	int error;
207	int extflags, lockflags;
208	int contested = 0;
209	uint64_t waitstart = 0;
210
211	error = 0;
212	td = curthread;
213
214#ifdef INVARIANTS
215	if (lkp->lk_flags & LK_DESTROYED) {
216		if (flags & LK_INTERLOCK)
217			mtx_unlock(interlkp);
218		if (panicstr != NULL)
219			return (0);
220		panic("%s: %p lockmgr is destroyed", __func__, lkp);
221	}
222#endif
223	if ((flags & LK_INTERNAL) == 0)
224		mtx_lock(lkp->lk_interlock);
225	CTR6(KTR_LOCK,
226	    "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), owner == %p, exclusivecount == %d, flags == 0x%x, "
227	    "td == %p", lkp, lkp->lk_wmesg, lkp->lk_lockholder,
228	    lkp->lk_exclusivecount, flags, td);
229#ifdef DEBUG_LOCKS
230	{
231		struct stack stack; /* XXX */
232		stack_save(&stack);
233		CTRSTACK(KTR_LOCK, &stack, 0, 1);
234	}
235#endif
236
237	if (flags & LK_INTERLOCK) {
238		mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
239		mtx_unlock(interlkp);
240	}
241
242	if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
243		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
244		    &lkp->lk_interlock->lock_object,
245		    "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
246
247	if (panicstr != NULL) {
248		mtx_unlock(lkp->lk_interlock);
249		return (0);
250	}
251	if ((lkp->lk_flags & LK_NOSHARE) &&
252	    (flags & LK_TYPE_MASK) == LK_SHARED) {
253		flags &= ~LK_TYPE_MASK;
254		flags |= LK_EXCLUSIVE;
255	}
256	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
257
258	switch (flags & LK_TYPE_MASK) {
259
260	case LK_SHARED:
261		if (!LOCKMGR_TRYOP(extflags))
262			WITNESS_CHECKORDER(&lkp->lk_object, LOP_NEWORDER, file,
263			    line);
264		/*
265		 * If we are not the exclusive lock holder, we have to block
266		 * while there is an exclusive lock holder or while an
267		 * exclusive lock request or upgrade request is in progress.
268		 *
269		 * However, if TDP_DEADLKTREAT is set, we override exclusive
270		 * lock requests or upgrade requests ( but not the exclusive
271		 * lock itself ).
272		 */
273		if (lkp->lk_lockholder != td) {
274			lockflags = LK_HAVE_EXCL;
275			if (!(td->td_pflags & TDP_DEADLKTREAT))
276				lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
277			error = acquire(&lkp, extflags, lockflags, &contested, &waitstart);
278			if (error)
279				break;
280			sharelock(td, lkp, 1);
281			if (lkp->lk_sharecount == 1)
282				lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
283			WITNESS_LOCK(&lkp->lk_object, LOCKMGR_TRYW(extflags),
284			    file, line);
285
286#if defined(DEBUG_LOCKS)
287			stack_save(&lkp->lk_stack);
288#endif
289			break;
290		}
291		/*
292		 * We hold an exclusive lock, so downgrade it to shared.
293		 * An alternative would be to fail with EDEADLK.
294		 */
295		/* FALLTHROUGH downgrade */
296
297	case LK_DOWNGRADE:
298		_lockmgr_assert(lkp, KA_XLOCKED, file, line);
299		sharelock(td, lkp, lkp->lk_exclusivecount);
300		WITNESS_DOWNGRADE(&lkp->lk_object, 0, file, line);
301		COUNT(td, -lkp->lk_exclusivecount);
302		lkp->lk_exclusivecount = 0;
303		lkp->lk_flags &= ~LK_HAVE_EXCL;
304		lkp->lk_lockholder = LK_NOPROC;
305		if (lkp->lk_waitcount)
306			wakeup((void *)lkp);
307		break;
308
309	case LK_UPGRADE:
310		/*
311		 * Upgrade a shared lock to an exclusive one. If another
312		 * shared lock has already requested an upgrade to an
313		 * exclusive lock, our shared lock is released and an
314		 * exclusive lock is requested (which will be granted
315		 * after the upgrade). If we return an error, the file
316		 * will always be unlocked.
317		 */
318		_lockmgr_assert(lkp, KA_SLOCKED, file, line);
319		shareunlock(td, lkp, 1);
320		if (lkp->lk_sharecount == 0)
321			lock_profile_release_lock(&lkp->lk_object);
322		/*
323		 * If we are just polling, check to see if we will block.
324		 */
325		if ((extflags & LK_NOWAIT) &&
326		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
327		     lkp->lk_sharecount > 1)) {
328			error = EBUSY;
329			WITNESS_UNLOCK(&lkp->lk_object, 0, file, line);
330			break;
331		}
332		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
333			/*
334			 * We are first shared lock to request an upgrade, so
335			 * request upgrade and wait for the shared count to
336			 * drop to zero, then take exclusive lock.
337			 */
338			lkp->lk_flags |= LK_WANT_UPGRADE;
339			error = acquire(&lkp, extflags, LK_SHARE_NONZERO, &contested, &waitstart);
340			lkp->lk_flags &= ~LK_WANT_UPGRADE;
341
342			if (error) {
343			         if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO))
344			                   wakeup((void *)lkp);
345				WITNESS_UNLOCK(&lkp->lk_object, 0, file, line);
346			         break;
347			}
348			if (lkp->lk_exclusivecount != 0)
349				panic("lockmgr: non-zero exclusive count");
350			lkp->lk_flags |= LK_HAVE_EXCL;
351			lkp->lk_lockholder = td;
352			lkp->lk_exclusivecount = 1;
353			WITNESS_UPGRADE(&lkp->lk_object, LOP_EXCLUSIVE |
354			    LOP_TRYLOCK, file, line);
355			COUNT(td, 1);
356			lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
357#if defined(DEBUG_LOCKS)
358			stack_save(&lkp->lk_stack);
359#endif
360			break;
361		}
362		/*
363		 * Someone else has requested upgrade. Release our shared
364		 * lock, awaken upgrade requestor if we are the last shared
365		 * lock, then request an exclusive lock.
366		 */
367		WITNESS_UNLOCK(&lkp->lk_object, 0, file, line);
368		if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
369			LK_WAIT_NONZERO)
370			wakeup((void *)lkp);
371		/* FALLTHROUGH exclusive request */
372
373	case LK_EXCLUSIVE:
374		if (!LOCKMGR_TRYOP(extflags))
375			WITNESS_CHECKORDER(&lkp->lk_object, LOP_NEWORDER |
376			    LOP_EXCLUSIVE, file, line);
377		if (lkp->lk_lockholder == td) {
378			/*
379			 *	Recursive lock.
380			 */
381			if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
382				panic("lockmgr: locking against myself");
383			if ((extflags & LK_CANRECURSE) != 0) {
384				lkp->lk_exclusivecount++;
385				WITNESS_LOCK(&lkp->lk_object, LOP_EXCLUSIVE |
386				    LOCKMGR_TRYW(extflags), file, line);
387				COUNT(td, 1);
388				break;
389			}
390		}
391		/*
392		 * If we are just polling, check to see if we will sleep.
393		 */
394		if ((extflags & LK_NOWAIT) &&
395		    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
396			error = EBUSY;
397			break;
398		}
399		/*
400		 * Try to acquire the want_exclusive flag.
401		 */
402		error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL), &contested, &waitstart);
403		if (error)
404			break;
405		lkp->lk_flags |= LK_WANT_EXCL;
406		/*
407		 * Wait for shared locks and upgrades to finish.
408		 */
409		error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO, &contested, &waitstart);
410		lkp->lk_flags &= ~LK_WANT_EXCL;
411		if (error) {
412			if (lkp->lk_flags & LK_WAIT_NONZERO)
413			         wakeup((void *)lkp);
414			break;
415		}
416		lkp->lk_flags |= LK_HAVE_EXCL;
417		lkp->lk_lockholder = td;
418		if (lkp->lk_exclusivecount != 0)
419			panic("lockmgr: non-zero exclusive count");
420		lkp->lk_exclusivecount = 1;
421		WITNESS_LOCK(&lkp->lk_object, LOP_EXCLUSIVE |
422		    LOCKMGR_TRYW(extflags), file, line);
423		COUNT(td, 1);
424		lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
425#if defined(DEBUG_LOCKS)
426		stack_save(&lkp->lk_stack);
427#endif
428		break;
429
430	case LK_RELEASE:
431		_lockmgr_assert(lkp, KA_LOCKED, file, line);
432		if (lkp->lk_exclusivecount != 0) {
433			if (lkp->lk_lockholder != LK_KERNPROC) {
434				WITNESS_UNLOCK(&lkp->lk_object, LOP_EXCLUSIVE,
435				    file, line);
436				COUNT(td, -1);
437			}
438			if (lkp->lk_exclusivecount-- == 1) {
439				lkp->lk_flags &= ~LK_HAVE_EXCL;
440				lkp->lk_lockholder = LK_NOPROC;
441				lock_profile_release_lock(&lkp->lk_object);
442			}
443		} else if (lkp->lk_flags & LK_SHARE_NONZERO) {
444			WITNESS_UNLOCK(&lkp->lk_object, 0, file, line);
445			shareunlock(td, lkp, 1);
446		}
447
448		if (lkp->lk_flags & LK_WAIT_NONZERO)
449			wakeup((void *)lkp);
450		break;
451
452	case LK_DRAIN:
453		/*
454		 * Check that we do not already hold the lock, as it can
455		 * never drain if we do. Unfortunately, we have no way to
456		 * check for holding a shared lock, but at least we can
457		 * check for an exclusive one.
458		 */
459		if (!LOCKMGR_TRYOP(extflags))
460			WITNESS_CHECKORDER(&lkp->lk_object, LOP_NEWORDER |
461			    LOP_EXCLUSIVE, file, line);
462		if (lkp->lk_lockholder == td)
463			panic("lockmgr: draining against myself");
464
465		error = acquiredrain(lkp, extflags);
466		if (error)
467			break;
468		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
469		lkp->lk_lockholder = td;
470		lkp->lk_exclusivecount = 1;
471		WITNESS_LOCK(&lkp->lk_object, LOP_EXCLUSIVE |
472		    LOCKMGR_TRYW(extflags), file, line);
473		COUNT(td, 1);
474#if defined(DEBUG_LOCKS)
475		stack_save(&lkp->lk_stack);
476#endif
477		break;
478
479	default:
480		mtx_unlock(lkp->lk_interlock);
481		panic("lockmgr: unknown locktype request %d",
482		    flags & LK_TYPE_MASK);
483		/* NOTREACHED */
484	}
485	if ((lkp->lk_flags & LK_WAITDRAIN) &&
486	    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
487		LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
488		lkp->lk_flags &= ~LK_WAITDRAIN;
489		wakeup((void *)&lkp->lk_flags);
490	}
491	mtx_unlock(lkp->lk_interlock);
492	return (error);
493}
494
495static int
496acquiredrain(struct lock *lkp, int extflags) {
497	int error;
498
499	if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
500		return EBUSY;
501	}
502	while (lkp->lk_flags & LK_ALL) {
503		lkp->lk_flags |= LK_WAITDRAIN;
504		error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
505			lkp->lk_wmesg,
506			((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
507		if (error)
508			return error;
509		if (extflags & LK_SLEEPFAIL) {
510			return ENOLCK;
511		}
512	}
513	return 0;
514}
515
516/*
517 * Initialize a lock; required before use.
518 */
519void
520lockinit(lkp, prio, wmesg, timo, flags)
521	struct lock *lkp;
522	int prio;
523	const char *wmesg;
524	int timo;
525	int flags;
526{
527	int iflags;
528
529	CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
530	    "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
531
532	lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
533	lkp->lk_flags = (flags & LK_EXTFLG_MASK) & ~(LK_NOWITNESS | LK_NODUP);
534	lkp->lk_sharecount = 0;
535	lkp->lk_waitcount = 0;
536	lkp->lk_exclusivecount = 0;
537	lkp->lk_prio = prio;
538	lkp->lk_timo = timo;
539	lkp->lk_lockholder = LK_NOPROC;
540	lkp->lk_newlock = NULL;
541	iflags = LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE;
542	if (!(flags & LK_NODUP))
543		iflags |= LO_DUPOK;
544	if (!(flags & LK_NOWITNESS))
545		iflags |= LO_WITNESS;
546#ifdef DEBUG_LOCKS
547	stack_zero(&lkp->lk_stack);
548#endif
549	lock_init(&lkp->lk_object, &lock_class_lockmgr, wmesg, NULL, iflags);
550}
551
552/*
553 * Destroy a lock.
554 */
555void
556lockdestroy(lkp)
557	struct lock *lkp;
558{
559
560	CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
561	    lkp, lkp->lk_wmesg);
562	KASSERT((lkp->lk_flags & (LK_HAVE_EXCL | LK_SHARE_NONZERO)) == 0,
563	    ("lockmgr still held"));
564	KASSERT(lkp->lk_exclusivecount == 0, ("lockmgr still recursed"));
565	lkp->lk_flags = LK_DESTROYED;
566	lock_destroy(&lkp->lk_object);
567}
568
569/*
570 * Disown the lockmgr.
571 */
572void
573_lockmgr_disown(struct lock *lkp, const char *file, int line)
574{
575	struct thread *td;
576
577	td = curthread;
578	KASSERT(panicstr != NULL || (lkp->lk_flags & LK_DESTROYED) == 0,
579	    ("%s: %p lockmgr is destroyed", __func__, lkp));
580	_lockmgr_assert(lkp, KA_XLOCKED | KA_NOTRECURSED, file, line);
581
582	/*
583	 * Drop the lock reference and switch the owner.  This will result
584	 * in an atomic operation like td_lock is only accessed by curthread
585	 * and lk_lockholder only needs one write.  Note also that the lock
586	 * owner can be alredy KERNPROC, so in that case just skip the
587	 * decrement.
588	 */
589	if (lkp->lk_lockholder == td) {
590		WITNESS_UNLOCK(&lkp->lk_object, LOP_EXCLUSIVE, file, line);
591		td->td_locks--;
592	}
593	lkp->lk_lockholder = LK_KERNPROC;
594}
595
596/*
597 * Determine the status of a lock.
598 */
599int
600lockstatus(lkp, td)
601	struct lock *lkp;
602	struct thread *td;
603{
604	int lock_type = 0;
605	int interlocked;
606
607	KASSERT(td == curthread,
608	    ("%s: thread passed argument (%p) is not valid", __func__, td));
609	KASSERT((lkp->lk_flags & LK_DESTROYED) == 0,
610	    ("%s: %p lockmgr is destroyed", __func__, lkp));
611
612	if (!kdb_active) {
613		interlocked = 1;
614		mtx_lock(lkp->lk_interlock);
615	} else
616		interlocked = 0;
617	if (lkp->lk_exclusivecount != 0) {
618		if (lkp->lk_lockholder == td)
619			lock_type = LK_EXCLUSIVE;
620		else
621			lock_type = LK_EXCLOTHER;
622	} else if (lkp->lk_sharecount != 0)
623		lock_type = LK_SHARED;
624	if (interlocked)
625		mtx_unlock(lkp->lk_interlock);
626	return (lock_type);
627}
628
629/*
630 * Determine the number of waiters on a lock.
631 */
632int
633lockwaiters(lkp)
634	struct lock *lkp;
635{
636	int count;
637
638	KASSERT((lkp->lk_flags & LK_DESTROYED) == 0,
639	    ("%s: %p lockmgr is destroyed", __func__, lkp));
640	mtx_lock(lkp->lk_interlock);
641	count = lkp->lk_waitcount;
642	mtx_unlock(lkp->lk_interlock);
643	return (count);
644}
645
646/*
647 * Print out information about state of a lock. Used by VOP_PRINT
648 * routines to display status about contained locks.
649 */
650void
651lockmgr_printinfo(lkp)
652	struct lock *lkp;
653{
654
655	if (lkp->lk_sharecount)
656		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
657		    lkp->lk_sharecount);
658	else if (lkp->lk_flags & LK_HAVE_EXCL)
659		printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)",
660		    lkp->lk_wmesg, lkp->lk_exclusivecount,
661		    lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid);
662	if (lkp->lk_waitcount > 0)
663		printf(" with %d pending", lkp->lk_waitcount);
664#ifdef DEBUG_LOCKS
665	stack_print_ddb(&lkp->lk_stack);
666#endif
667}
668
669#ifdef INVARIANT_SUPPORT
670#ifndef INVARIANTS
671#undef _lockmgr_assert
672#endif
673
674void
675_lockmgr_assert(struct lock *lkp, int what, const char *file, int line)
676{
677	struct thread *td;
678	u_int x;
679	int slocked = 0;
680
681	x = lkp->lk_flags;
682	td = lkp->lk_lockholder;
683	if (panicstr != NULL)
684		return;
685	switch (what) {
686	case KA_SLOCKED:
687	case KA_SLOCKED | KA_NOTRECURSED:
688	case KA_SLOCKED | KA_RECURSED:
689		slocked = 1;
690	case KA_LOCKED:
691	case KA_LOCKED | KA_NOTRECURSED:
692	case KA_LOCKED | KA_RECURSED:
693#ifdef WITNESS
694		/*
695		 * We cannot trust WITNESS if the lock is held in
696		 * exclusive mode and a call to lockmgr_disown() happened.
697		 * Workaround this skipping the check if the lock is
698		 * held in exclusive mode even for the KA_LOCKED case.
699		 */
700		if (slocked || (x & LK_HAVE_EXCL) == 0) {
701			witness_assert(&lkp->lk_object, what, file, line);
702			break;
703		}
704#endif
705		if (LOCKMGR_UNHELD(x) || ((x & LK_SHARE_NONZERO) == 0 &&
706		    (slocked || LOCKMGR_NOTOWNER(td))))
707			panic("Lock %s not %slocked @ %s:%d\n",
708			    lkp->lk_object.lo_name, slocked ? "share " : "",
709			    file, line);
710		if ((x & LK_SHARE_NONZERO) == 0) {
711			if (lockmgr_recursed(lkp)) {
712				if (what & KA_NOTRECURSED)
713					panic("Lock %s recursed @ %s:%d\n",
714					    lkp->lk_object.lo_name, file, line);
715			} else if (what & KA_RECURSED)
716				panic("Lock %s not recursed @ %s:%d\n",
717				    lkp->lk_object.lo_name, file, line);
718		}
719		break;
720	case KA_XLOCKED:
721	case KA_XLOCKED | KA_NOTRECURSED:
722	case KA_XLOCKED | KA_RECURSED:
723		if ((x & LK_HAVE_EXCL) == 0 || LOCKMGR_NOTOWNER(td))
724			panic("Lock %s not exclusively locked @ %s:%d\n",
725			    lkp->lk_object.lo_name, file, line);
726		if (lockmgr_recursed(lkp)) {
727			if (what & KA_NOTRECURSED)
728				panic("Lock %s recursed @ %s:%d\n",
729				    lkp->lk_object.lo_name, file, line);
730		} else if (what & KA_RECURSED)
731			panic("Lock %s not recursed @ %s:%d\n",
732			    lkp->lk_object.lo_name, file, line);
733		break;
734	case KA_UNLOCKED:
735		if (td == curthread || td == LK_KERNPROC)
736			panic("Lock %s exclusively locked @ %s:%d\n",
737			    lkp->lk_object.lo_name, file, line);
738		break;
739	case KA_HELD:
740	case KA_UNHELD:
741		if (LOCKMGR_UNHELD(x)) {
742			if (what & KA_HELD)
743				panic("Lock %s not locked by anyone @ %s:%d\n",
744				    lkp->lk_object.lo_name, file, line);
745		} else if (what & KA_UNHELD)
746			panic("Lock %s locked by someone @ %s:%d\n",
747			    lkp->lk_object.lo_name, file, line);
748		break;
749	default:
750		panic("Unknown lockmgr lock assertion: 0x%x @ %s:%d", what,
751		    file, line);
752	}
753}
754#endif	/* INVARIANT_SUPPORT */
755
756#ifdef DDB
757/*
758 * Check to see if a thread that is blocked on a sleep queue is actually
759 * blocked on a 'struct lock'.  If so, output some details and return true.
760 * If the lock has an exclusive owner, return that in *ownerp.
761 */
762int
763lockmgr_chain(struct thread *td, struct thread **ownerp)
764{
765	struct lock *lkp;
766
767	lkp = td->td_wchan;
768
769	/* Simple test to see if wchan points to a lockmgr lock. */
770	if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr &&
771	    lkp->lk_wmesg == td->td_wmesg)
772		goto ok;
773
774	/*
775	 * If this thread is doing a DRAIN, then it would be asleep on
776	 * &lkp->lk_flags rather than lkp.
777	 */
778	lkp = (struct lock *)((char *)td->td_wchan -
779	    offsetof(struct lock, lk_flags));
780	if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr &&
781	    lkp->lk_wmesg == td->td_wmesg && (lkp->lk_flags & LK_WAITDRAIN))
782		goto ok;
783
784	/* Doen't seem to be a lockmgr lock. */
785	return (0);
786
787ok:
788	/* Ok, we think we have a lockmgr lock, so output some details. */
789	db_printf("blocked on lk \"%s\" ", lkp->lk_wmesg);
790	if (lkp->lk_sharecount) {
791		db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
792		*ownerp = NULL;
793	} else {
794		db_printf("EXCL (count %d)\n", lkp->lk_exclusivecount);
795		*ownerp = lkp->lk_lockholder;
796	}
797	return (1);
798}
799
800void
801db_show_lockmgr(struct lock_object *lock)
802{
803	struct thread *td;
804	struct lock *lkp;
805
806	lkp = (struct lock *)lock;
807
808	db_printf(" lock type: %s\n", lkp->lk_wmesg);
809	db_printf(" state: ");
810	if (lkp->lk_sharecount)
811		db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
812	else if (lkp->lk_flags & LK_HAVE_EXCL) {
813		td = lkp->lk_lockholder;
814		db_printf("EXCL (count %d) %p ", lkp->lk_exclusivecount, td);
815		db_printf("(tid %d, pid %d, \"%s\")\n", td->td_tid,
816		    td->td_proc->p_pid, td->td_name);
817	} else
818		db_printf("UNLOCKED\n");
819	if (lkp->lk_waitcount > 0)
820		db_printf(" waiters: %d\n", lkp->lk_waitcount);
821}
822#endif
823