kern_lock.c revision 174137
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 174137 2007-12-01 22:04:16Z rwatson $");
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
63static void	assert_lockmgr(struct lock_object *lock, int what);
64#ifdef DDB
65#include <ddb/ddb.h>
66static void	db_show_lockmgr(struct lock_object *lock);
67#endif
68static void	lock_lockmgr(struct lock_object *lock, int how);
69static int	unlock_lockmgr(struct lock_object *lock);
70
71struct lock_class lock_class_lockmgr = {
72	.lc_name = "lockmgr",
73	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
74	.lc_assert = assert_lockmgr,
75#ifdef DDB
76	.lc_ddb_show = db_show_lockmgr,
77#endif
78	.lc_lock = lock_lockmgr,
79	.lc_unlock = unlock_lockmgr,
80};
81
82/*
83 * Locking primitives implementation.
84 * Locks provide shared/exclusive sychronization.
85 */
86
87void
88assert_lockmgr(struct lock_object *lock, int what)
89{
90
91	panic("lockmgr locks do not support assertions");
92}
93
94void
95lock_lockmgr(struct lock_object *lock, int how)
96{
97
98	panic("lockmgr locks do not support sleep interlocking");
99}
100
101int
102unlock_lockmgr(struct lock_object *lock)
103{
104
105	panic("lockmgr locks do not support sleep interlocking");
106}
107
108#define	COUNT(td, x)	if ((td)) (td)->td_locks += (x)
109#define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
110	LK_SHARE_NONZERO | LK_WAIT_NONZERO)
111
112static int acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime);
113static int acquiredrain(struct lock *lkp, int extflags) ;
114
115static __inline void
116sharelock(struct thread *td, struct lock *lkp, int incr) {
117	lkp->lk_flags |= LK_SHARE_NONZERO;
118	lkp->lk_sharecount += incr;
119	COUNT(td, incr);
120}
121
122static __inline void
123shareunlock(struct thread *td, struct lock *lkp, int decr) {
124
125	KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
126
127	COUNT(td, -decr);
128	if (lkp->lk_sharecount == decr) {
129		lkp->lk_flags &= ~LK_SHARE_NONZERO;
130		if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
131			wakeup(lkp);
132		}
133		lkp->lk_sharecount = 0;
134	} else {
135		lkp->lk_sharecount -= decr;
136	}
137}
138
139static int
140acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime)
141{
142	struct lock *lkp = *lkpp;
143	int error;
144	CTR3(KTR_LOCK,
145	    "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x",
146	    lkp, extflags, wanted);
147
148	if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted))
149		return EBUSY;
150	error = 0;
151	if ((lkp->lk_flags & wanted) != 0)
152		lock_profile_obtain_lock_failed(&lkp->lk_object, contested, waittime);
153
154	while ((lkp->lk_flags & wanted) != 0) {
155		CTR2(KTR_LOCK,
156		    "acquire(): lkp == %p, lk_flags == 0x%x sleeping",
157		    lkp, lkp->lk_flags);
158		lkp->lk_flags |= LK_WAIT_NONZERO;
159		lkp->lk_waitcount++;
160		error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
161		    lkp->lk_wmesg,
162		    ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
163		lkp->lk_waitcount--;
164		if (lkp->lk_waitcount == 0)
165			lkp->lk_flags &= ~LK_WAIT_NONZERO;
166		if (error)
167			break;
168		if (extflags & LK_SLEEPFAIL) {
169			error = ENOLCK;
170			break;
171		}
172		if (lkp->lk_newlock != NULL) {
173			mtx_lock(lkp->lk_newlock->lk_interlock);
174			mtx_unlock(lkp->lk_interlock);
175			if (lkp->lk_waitcount == 0)
176				wakeup((void *)(&lkp->lk_newlock));
177			*lkpp = lkp = lkp->lk_newlock;
178		}
179	}
180	mtx_assert(lkp->lk_interlock, MA_OWNED);
181	return (error);
182}
183
184/*
185 * Set, change, or release a lock.
186 *
187 * Shared requests increment the shared count. Exclusive requests set the
188 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
189 * accepted shared locks and shared-to-exclusive upgrades to go away.
190 */
191int
192_lockmgr(struct lock *lkp, u_int flags, struct mtx *interlkp,
193	 struct thread *td, char *file, int line)
194
195{
196	int error;
197	struct thread *thr;
198	int extflags, lockflags;
199	int contested = 0;
200	uint64_t waitstart = 0;
201
202	error = 0;
203	if (td == NULL)
204		thr = LK_KERNPROC;
205	else
206		thr = td;
207
208	if ((flags & LK_INTERNAL) == 0)
209		mtx_lock(lkp->lk_interlock);
210	CTR6(KTR_LOCK,
211	    "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), owner == %p, exclusivecount == %d, flags == 0x%x, "
212	    "td == %p", lkp, lkp->lk_wmesg, lkp->lk_lockholder,
213	    lkp->lk_exclusivecount, flags, td);
214#ifdef DEBUG_LOCKS
215	{
216		struct stack stack; /* XXX */
217		stack_save(&stack);
218		CTRSTACK(KTR_LOCK, &stack, 0, 1);
219	}
220#endif
221
222	if (flags & LK_INTERLOCK) {
223		mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
224		mtx_unlock(interlkp);
225	}
226
227	if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
228		WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
229		    &lkp->lk_interlock->lock_object,
230		    "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
231
232	if (panicstr != NULL) {
233		mtx_unlock(lkp->lk_interlock);
234		return (0);
235	}
236	if ((lkp->lk_flags & LK_NOSHARE) &&
237	    (flags & LK_TYPE_MASK) == LK_SHARED) {
238		flags &= ~LK_TYPE_MASK;
239		flags |= LK_EXCLUSIVE;
240	}
241	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
242
243	switch (flags & LK_TYPE_MASK) {
244
245	case LK_SHARED:
246		/*
247		 * If we are not the exclusive lock holder, we have to block
248		 * while there is an exclusive lock holder or while an
249		 * exclusive lock request or upgrade request is in progress.
250		 *
251		 * However, if TDP_DEADLKTREAT is set, we override exclusive
252		 * lock requests or upgrade requests ( but not the exclusive
253		 * lock itself ).
254		 */
255		if (lkp->lk_lockholder != thr) {
256			lockflags = LK_HAVE_EXCL;
257			if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT))
258				lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
259			error = acquire(&lkp, extflags, lockflags, &contested, &waitstart);
260			if (error)
261				break;
262			sharelock(td, lkp, 1);
263			if (lkp->lk_sharecount == 1)
264				lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
265
266#if defined(DEBUG_LOCKS)
267			stack_save(&lkp->lk_stack);
268#endif
269			break;
270		}
271		/*
272		 * We hold an exclusive lock, so downgrade it to shared.
273		 * An alternative would be to fail with EDEADLK.
274		 */
275		sharelock(td, lkp, 1);
276		if (lkp->lk_sharecount == 1)
277			lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
278		/* FALLTHROUGH downgrade */
279
280	case LK_DOWNGRADE:
281		KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0,
282			("lockmgr: not holding exclusive lock "
283			"(owner thread (%p) != thread (%p), exlcnt (%d) != 0",
284			lkp->lk_lockholder, thr, lkp->lk_exclusivecount));
285		sharelock(td, lkp, lkp->lk_exclusivecount);
286		COUNT(td, -lkp->lk_exclusivecount);
287		lkp->lk_exclusivecount = 0;
288		lkp->lk_flags &= ~LK_HAVE_EXCL;
289		lkp->lk_lockholder = LK_NOPROC;
290		if (lkp->lk_waitcount)
291			wakeup((void *)lkp);
292		break;
293
294	case LK_EXCLUPGRADE:
295		/*
296		 * If another process is ahead of us to get an upgrade,
297		 * then we want to fail rather than have an intervening
298		 * exclusive access.
299		 */
300		if (lkp->lk_flags & LK_WANT_UPGRADE) {
301			shareunlock(td, lkp, 1);
302			error = EBUSY;
303			break;
304		}
305		/* FALLTHROUGH normal upgrade */
306
307	case LK_UPGRADE:
308		/*
309		 * Upgrade a shared lock to an exclusive one. If another
310		 * shared lock has already requested an upgrade to an
311		 * exclusive lock, our shared lock is released and an
312		 * exclusive lock is requested (which will be granted
313		 * after the upgrade). If we return an error, the file
314		 * will always be unlocked.
315		 */
316		if (lkp->lk_lockholder == thr)
317			panic("lockmgr: upgrade exclusive lock");
318		if (lkp->lk_sharecount <= 0)
319			panic("lockmgr: upgrade without shared");
320		shareunlock(td, lkp, 1);
321		if (lkp->lk_sharecount == 0)
322			lock_profile_release_lock(&lkp->lk_object);
323		/*
324		 * If we are just polling, check to see if we will block.
325		 */
326		if ((extflags & LK_NOWAIT) &&
327		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
328		     lkp->lk_sharecount > 1)) {
329			error = EBUSY;
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			         break;
346			}
347			if (lkp->lk_exclusivecount != 0)
348				panic("lockmgr: non-zero exclusive count");
349			lkp->lk_flags |= LK_HAVE_EXCL;
350			lkp->lk_lockholder = thr;
351			lkp->lk_exclusivecount = 1;
352			COUNT(td, 1);
353			lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
354#if defined(DEBUG_LOCKS)
355			stack_save(&lkp->lk_stack);
356#endif
357			break;
358		}
359		/*
360		 * Someone else has requested upgrade. Release our shared
361		 * lock, awaken upgrade requestor if we are the last shared
362		 * lock, then request an exclusive lock.
363		 */
364		if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
365			LK_WAIT_NONZERO)
366			wakeup((void *)lkp);
367		/* FALLTHROUGH exclusive request */
368
369	case LK_EXCLUSIVE:
370		if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) {
371			/*
372			 *	Recursive lock.
373			 */
374			if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
375				panic("lockmgr: locking against myself");
376			if ((extflags & LK_CANRECURSE) != 0) {
377				lkp->lk_exclusivecount++;
378				COUNT(td, 1);
379				break;
380			}
381		}
382		/*
383		 * If we are just polling, check to see if we will sleep.
384		 */
385		if ((extflags & LK_NOWAIT) &&
386		    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
387			error = EBUSY;
388			break;
389		}
390		/*
391		 * Try to acquire the want_exclusive flag.
392		 */
393		error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL), &contested, &waitstart);
394		if (error)
395			break;
396		lkp->lk_flags |= LK_WANT_EXCL;
397		/*
398		 * Wait for shared locks and upgrades to finish.
399		 */
400		error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO, &contested, &waitstart);
401		lkp->lk_flags &= ~LK_WANT_EXCL;
402		if (error) {
403			if (lkp->lk_flags & LK_WAIT_NONZERO)
404			         wakeup((void *)lkp);
405			break;
406		}
407		lkp->lk_flags |= LK_HAVE_EXCL;
408		lkp->lk_lockholder = thr;
409		if (lkp->lk_exclusivecount != 0)
410			panic("lockmgr: non-zero exclusive count");
411		lkp->lk_exclusivecount = 1;
412		COUNT(td, 1);
413		lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
414#if defined(DEBUG_LOCKS)
415		stack_save(&lkp->lk_stack);
416#endif
417		break;
418
419	case LK_RELEASE:
420		if (lkp->lk_exclusivecount != 0) {
421			if (lkp->lk_lockholder != thr &&
422			    lkp->lk_lockholder != LK_KERNPROC) {
423				panic("lockmgr: thread %p, not %s %p unlocking",
424				    thr, "exclusive lock holder",
425				    lkp->lk_lockholder);
426			}
427			if (lkp->lk_lockholder != LK_KERNPROC)
428				COUNT(td, -1);
429			if (lkp->lk_exclusivecount == 1) {
430				lkp->lk_flags &= ~LK_HAVE_EXCL;
431				lkp->lk_lockholder = LK_NOPROC;
432				lkp->lk_exclusivecount = 0;
433				lock_profile_release_lock(&lkp->lk_object);
434			} else {
435				lkp->lk_exclusivecount--;
436			}
437		} else if (lkp->lk_flags & LK_SHARE_NONZERO)
438			shareunlock(td, lkp, 1);
439		else  {
440			printf("lockmgr: thread %p unlocking unheld lock\n",
441			    thr);
442			kdb_backtrace();
443		}
444
445		if (lkp->lk_flags & LK_WAIT_NONZERO)
446			wakeup((void *)lkp);
447		break;
448
449	case LK_DRAIN:
450		/*
451		 * Check that we do not already hold the lock, as it can
452		 * never drain if we do. Unfortunately, we have no way to
453		 * check for holding a shared lock, but at least we can
454		 * check for an exclusive one.
455		 */
456		if (lkp->lk_lockholder == thr)
457			panic("lockmgr: draining against myself");
458
459		error = acquiredrain(lkp, extflags);
460		if (error)
461			break;
462		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
463		lkp->lk_lockholder = thr;
464		lkp->lk_exclusivecount = 1;
465		COUNT(td, 1);
466#if defined(DEBUG_LOCKS)
467		stack_save(&lkp->lk_stack);
468#endif
469		break;
470
471	default:
472		mtx_unlock(lkp->lk_interlock);
473		panic("lockmgr: unknown locktype request %d",
474		    flags & LK_TYPE_MASK);
475		/* NOTREACHED */
476	}
477	if ((lkp->lk_flags & LK_WAITDRAIN) &&
478	    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
479		LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
480		lkp->lk_flags &= ~LK_WAITDRAIN;
481		wakeup((void *)&lkp->lk_flags);
482	}
483	mtx_unlock(lkp->lk_interlock);
484	return (error);
485}
486
487static int
488acquiredrain(struct lock *lkp, int extflags) {
489	int error;
490
491	if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
492		return EBUSY;
493	}
494	while (lkp->lk_flags & LK_ALL) {
495		lkp->lk_flags |= LK_WAITDRAIN;
496		error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
497			lkp->lk_wmesg,
498			((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
499		if (error)
500			return error;
501		if (extflags & LK_SLEEPFAIL) {
502			return ENOLCK;
503		}
504	}
505	return 0;
506}
507
508/*
509 * Initialize a lock; required before use.
510 */
511void
512lockinit(lkp, prio, wmesg, timo, flags)
513	struct lock *lkp;
514	int prio;
515	const char *wmesg;
516	int timo;
517	int flags;
518{
519	CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
520	    "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
521
522	lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
523	lkp->lk_flags = (flags & LK_EXTFLG_MASK);
524	lkp->lk_sharecount = 0;
525	lkp->lk_waitcount = 0;
526	lkp->lk_exclusivecount = 0;
527	lkp->lk_prio = prio;
528	lkp->lk_timo = timo;
529	lkp->lk_lockholder = LK_NOPROC;
530	lkp->lk_newlock = NULL;
531#ifdef DEBUG_LOCKS
532	stack_zero(&lkp->lk_stack);
533#endif
534	lock_init(&lkp->lk_object, &lock_class_lockmgr, wmesg, NULL,
535	    LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE);
536}
537
538/*
539 * Destroy a lock.
540 */
541void
542lockdestroy(lkp)
543	struct lock *lkp;
544{
545
546	CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
547	    lkp, lkp->lk_wmesg);
548	lock_destroy(&lkp->lk_object);
549}
550
551/*
552 * Determine the status of a lock.
553 */
554int
555lockstatus(lkp, td)
556	struct lock *lkp;
557	struct thread *td;
558{
559	int lock_type = 0;
560	int interlocked;
561
562	if (!kdb_active) {
563		interlocked = 1;
564		mtx_lock(lkp->lk_interlock);
565	} else
566		interlocked = 0;
567	if (lkp->lk_exclusivecount != 0) {
568		if (td == NULL || lkp->lk_lockholder == td)
569			lock_type = LK_EXCLUSIVE;
570		else
571			lock_type = LK_EXCLOTHER;
572	} else if (lkp->lk_sharecount != 0)
573		lock_type = LK_SHARED;
574	if (interlocked)
575		mtx_unlock(lkp->lk_interlock);
576	return (lock_type);
577}
578
579/*
580 * Determine the number of holders of a lock.
581 */
582int
583lockcount(lkp)
584	struct lock *lkp;
585{
586	int count;
587
588	mtx_lock(lkp->lk_interlock);
589	count = lkp->lk_exclusivecount + lkp->lk_sharecount;
590	mtx_unlock(lkp->lk_interlock);
591	return (count);
592}
593
594/*
595 * Determine the number of waiters on a lock.
596 */
597int
598lockwaiters(lkp)
599	struct lock *lkp;
600{
601	int count;
602
603	mtx_lock(lkp->lk_interlock);
604	count = lkp->lk_waitcount;
605	mtx_unlock(lkp->lk_interlock);
606	return (count);
607}
608
609/*
610 * Print out information about state of a lock. Used by VOP_PRINT
611 * routines to display status about contained locks.
612 */
613void
614lockmgr_printinfo(lkp)
615	struct lock *lkp;
616{
617
618	if (lkp->lk_sharecount)
619		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
620		    lkp->lk_sharecount);
621	else if (lkp->lk_flags & LK_HAVE_EXCL)
622		printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)",
623		    lkp->lk_wmesg, lkp->lk_exclusivecount,
624		    lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid);
625	if (lkp->lk_waitcount > 0)
626		printf(" with %d pending", lkp->lk_waitcount);
627#ifdef DEBUG_LOCKS
628	stack_print_ddb(&lkp->lk_stack);
629#endif
630}
631
632#ifdef DDB
633/*
634 * Check to see if a thread that is blocked on a sleep queue is actually
635 * blocked on a 'struct lock'.  If so, output some details and return true.
636 * If the lock has an exclusive owner, return that in *ownerp.
637 */
638int
639lockmgr_chain(struct thread *td, struct thread **ownerp)
640{
641	struct lock *lkp;
642
643	lkp = td->td_wchan;
644
645	/* Simple test to see if wchan points to a lockmgr lock. */
646	if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr &&
647	    lkp->lk_wmesg == td->td_wmesg)
648		goto ok;
649
650	/*
651	 * If this thread is doing a DRAIN, then it would be asleep on
652	 * &lkp->lk_flags rather than lkp.
653	 */
654	lkp = (struct lock *)((char *)td->td_wchan -
655	    offsetof(struct lock, lk_flags));
656	if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr &&
657	    lkp->lk_wmesg == td->td_wmesg && (lkp->lk_flags & LK_WAITDRAIN))
658		goto ok;
659
660	/* Doen't seem to be a lockmgr lock. */
661	return (0);
662
663ok:
664	/* Ok, we think we have a lockmgr lock, so output some details. */
665	db_printf("blocked on lk \"%s\" ", lkp->lk_wmesg);
666	if (lkp->lk_sharecount) {
667		db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
668		*ownerp = NULL;
669	} else {
670		db_printf("EXCL (count %d)\n", lkp->lk_exclusivecount);
671		*ownerp = lkp->lk_lockholder;
672	}
673	return (1);
674}
675
676void
677db_show_lockmgr(struct lock_object *lock)
678{
679	struct thread *td;
680	struct lock *lkp;
681
682	lkp = (struct lock *)lock;
683
684	db_printf(" lock type: %s\n", lkp->lk_wmesg);
685	db_printf(" state: ");
686	if (lkp->lk_sharecount)
687		db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
688	else if (lkp->lk_flags & LK_HAVE_EXCL) {
689		td = lkp->lk_lockholder;
690		db_printf("EXCL (count %d) %p ", lkp->lk_exclusivecount, td);
691		db_printf("(tid %d, pid %d, \"%s\")\n", td->td_tid,
692		    td->td_proc->p_pid, td->td_name);
693	} else
694		db_printf("UNLOCKED\n");
695	if (lkp->lk_waitcount > 0)
696		db_printf(" waiters: %d\n", lkp->lk_waitcount);
697}
698#endif
699