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