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