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