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