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