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