kern_lock.c revision 44681
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 * $Id: kern_lock.c,v 1.23 1999/01/20 14:49:11 eivind Exp $
42 */
43
44#include "opt_lint.h"
45
46#include <sys/param.h>
47#include <sys/proc.h>
48#include <sys/lock.h>
49#include <sys/systm.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	int lock_wait;
108	lock_wait = LOCK_WAIT_TIME;
109	for (; lock_wait > 0; lock_wait--) {
110		int i;
111		if ((lkp->lk_flags & flags) == 0)
112			return 0;
113		simple_unlock(&lkp->lk_interlock);
114		for (i = LOCK_SAMPLE_WAIT; i > 0; i--) {
115			if ((lkp->lk_flags & flags) == 0) {
116				simple_lock(&lkp->lk_interlock);
117				if ((lkp->lk_flags & flags) == 0)
118					return 0;
119				break;
120			}
121		}
122	}
123	return 1;
124}
125
126static int
127acquire(struct lock *lkp, int extflags, int wanted) {
128	int s, error;
129
130	if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) {
131		return EBUSY;
132	}
133
134	if (((lkp->lk_flags | extflags) & LK_NOPAUSE) == 0) {
135		error = apause(lkp, wanted);
136		if (error == 0)
137			return 0;
138	}
139
140	s = splhigh();
141	while ((lkp->lk_flags & wanted) != 0) {
142		lkp->lk_flags |= LK_WAIT_NONZERO;
143		lkp->lk_waitcount++;
144		simple_unlock(&lkp->lk_interlock);
145		error = tsleep(lkp, lkp->lk_prio, lkp->lk_wmesg, lkp->lk_timo);
146		simple_lock(&lkp->lk_interlock);
147		if (lkp->lk_waitcount == 1) {
148			lkp->lk_flags &= ~LK_WAIT_NONZERO;
149			lkp->lk_waitcount = 0;
150		} else {
151			lkp->lk_waitcount--;
152		}
153		if (error) {
154			splx(s);
155			return error;
156		}
157		if (extflags & LK_SLEEPFAIL) {
158			splx(s);
159			return ENOLCK;
160		}
161	}
162	splx(s);
163	return 0;
164}
165
166/*
167 * Set, change, or release a lock.
168 *
169 * Shared requests increment the shared count. Exclusive requests set the
170 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
171 * accepted shared locks and shared-to-exclusive upgrades to go away.
172 */
173int
174#ifndef	DEBUG_LOCKS
175lockmgr(lkp, flags, interlkp, p)
176#else
177debuglockmgr(lkp, flags, interlkp, p, name, file, line)
178#endif
179	struct lock *lkp;
180	u_int flags;
181	struct simplelock *interlkp;
182	struct proc *p;
183#ifdef	DEBUG_LOCKS
184	const char *name;	/* Name of lock function */
185	const char *file;	/* Name of file call is from */
186	int line;		/* Line number in file */
187#endif
188{
189	int error;
190	pid_t pid;
191	int extflags;
192
193	error = 0;
194	if (p == NULL)
195		pid = LK_KERNPROC;
196	else
197		pid = p->p_pid;
198
199	simple_lock(&lkp->lk_interlock);
200	if (flags & LK_INTERLOCK)
201		simple_unlock(interlkp);
202
203	extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
204
205	switch (flags & LK_TYPE_MASK) {
206
207	case LK_SHARED:
208		/*
209		 * If we are not the exclusive lock holder, we have to block
210		 * while there is an exclusive lock holder or while an
211		 * exclusive lock request or upgrade request is in progress.
212		 *
213		 * However, if P_DEADLKTREAT is set, we override exclusive
214		 * lock requests or upgrade requests ( but not the exclusive
215		 * lock itself ).
216		 */
217		if (lkp->lk_lockholder != pid) {
218			if (p->p_flag & P_DEADLKTREAT) {
219				error = acquire(
220					    lkp,
221					    extflags,
222					    LK_HAVE_EXCL
223					);
224			} else {
225				error = acquire(
226					    lkp,
227					    extflags,
228					    LK_HAVE_EXCL | LK_WANT_EXCL |
229					     LK_WANT_UPGRADE
230					);
231			}
232			if (error)
233				break;
234			sharelock(lkp, 1);
235			COUNT(p, 1);
236			break;
237		}
238		/*
239		 * We hold an exclusive lock, so downgrade it to shared.
240		 * An alternative would be to fail with EDEADLK.
241		 */
242		sharelock(lkp, 1);
243		COUNT(p, 1);
244		/* fall into downgrade */
245
246	case LK_DOWNGRADE:
247#if !defined(MAX_PERF)
248		if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
249			panic("lockmgr: not holding exclusive lock");
250#endif
251		sharelock(lkp, lkp->lk_exclusivecount);
252		lkp->lk_exclusivecount = 0;
253		lkp->lk_flags &= ~LK_HAVE_EXCL;
254		lkp->lk_lockholder = LK_NOPROC;
255		if (lkp->lk_waitcount)
256			wakeup((void *)lkp);
257		break;
258
259	case LK_EXCLUPGRADE:
260		/*
261		 * If another process is ahead of us to get an upgrade,
262		 * then we want to fail rather than have an intervening
263		 * exclusive access.
264		 */
265		if (lkp->lk_flags & LK_WANT_UPGRADE) {
266			shareunlock(lkp, 1);
267			COUNT(p, -1);
268			error = EBUSY;
269			break;
270		}
271		/* fall into normal upgrade */
272
273	case LK_UPGRADE:
274		/*
275		 * Upgrade a shared lock to an exclusive one. If another
276		 * shared lock has already requested an upgrade to an
277		 * exclusive lock, our shared lock is released and an
278		 * exclusive lock is requested (which will be granted
279		 * after the upgrade). If we return an error, the file
280		 * will always be unlocked.
281		 */
282#if !defined(MAX_PERF)
283		if ((lkp->lk_lockholder == pid) || (lkp->lk_sharecount <= 0))
284			panic("lockmgr: upgrade exclusive lock");
285#endif
286		shareunlock(lkp, 1);
287		COUNT(p, -1);
288		/*
289		 * If we are just polling, check to see if we will block.
290		 */
291		if ((extflags & LK_NOWAIT) &&
292		    ((lkp->lk_flags & LK_WANT_UPGRADE) ||
293		     lkp->lk_sharecount > 1)) {
294			error = EBUSY;
295			break;
296		}
297		if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
298			/*
299			 * We are first shared lock to request an upgrade, so
300			 * request upgrade and wait for the shared count to
301			 * drop to zero, then take exclusive lock.
302			 */
303			lkp->lk_flags |= LK_WANT_UPGRADE;
304			error = acquire(lkp, extflags, LK_SHARE_NONZERO);
305			lkp->lk_flags &= ~LK_WANT_UPGRADE;
306
307			if (error)
308				break;
309			lkp->lk_flags |= LK_HAVE_EXCL;
310			lkp->lk_lockholder = pid;
311#if !defined(MAX_PERF)
312			if (lkp->lk_exclusivecount != 0)
313				panic("lockmgr: non-zero exclusive count");
314#endif
315			lkp->lk_exclusivecount = 1;
316#if defined(DEBUG_LOCKS)
317			lkp->lk_filename = file;
318			lkp->lk_lineno = line;
319			lkp->lk_lockername = name;
320#endif
321			COUNT(p, 1);
322			break;
323		}
324		/*
325		 * Someone else has requested upgrade. Release our shared
326		 * lock, awaken upgrade requestor if we are the last shared
327		 * lock, then request an exclusive lock.
328		 */
329		if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
330			LK_WAIT_NONZERO)
331			wakeup((void *)lkp);
332		/* fall into exclusive request */
333
334	case LK_EXCLUSIVE:
335		if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
336			/*
337			 *	Recursive lock.
338			 */
339#if !defined(MAX_PERF)
340			if ((extflags & LK_CANRECURSE) == 0)
341				panic("lockmgr: locking against myself");
342#endif
343			lkp->lk_exclusivecount++;
344			COUNT(p, 1);
345			break;
346		}
347		/*
348		 * If we are just polling, check to see if we will sleep.
349		 */
350		if ((extflags & LK_NOWAIT) &&
351		    (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
352			error = EBUSY;
353			break;
354		}
355		/*
356		 * Try to acquire the want_exclusive flag.
357		 */
358		error = acquire(lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
359		if (error)
360			break;
361		lkp->lk_flags |= LK_WANT_EXCL;
362		/*
363		 * Wait for shared locks and upgrades to finish.
364		 */
365		error = acquire(lkp, extflags, LK_WANT_UPGRADE | LK_SHARE_NONZERO);
366		lkp->lk_flags &= ~LK_WANT_EXCL;
367		if (error)
368			break;
369		lkp->lk_flags |= LK_HAVE_EXCL;
370		lkp->lk_lockholder = pid;
371#if !defined(MAX_PERF)
372		if (lkp->lk_exclusivecount != 0)
373			panic("lockmgr: non-zero exclusive count");
374#endif
375		lkp->lk_exclusivecount = 1;
376#if defined(DEBUG_LOCKS)
377			lkp->lk_filename = file;
378			lkp->lk_lineno = line;
379			lkp->lk_lockername = name;
380#endif
381		COUNT(p, 1);
382		break;
383
384	case LK_RELEASE:
385		if (lkp->lk_exclusivecount != 0) {
386#if !defined(MAX_PERF)
387			if (pid != lkp->lk_lockholder)
388				panic("lockmgr: pid %d, not %s %d unlocking",
389				    pid, "exclusive lock holder",
390				    lkp->lk_lockholder);
391#endif
392			COUNT(p, -1);
393			if (lkp->lk_exclusivecount == 1) {
394				lkp->lk_flags &= ~LK_HAVE_EXCL;
395				lkp->lk_lockholder = LK_NOPROC;
396				lkp->lk_exclusivecount = 0;
397			} else {
398				lkp->lk_exclusivecount--;
399			}
400		} else if (lkp->lk_flags & LK_SHARE_NONZERO) {
401			shareunlock(lkp, 1);
402			COUNT(p, -1);
403		}
404		if (lkp->lk_flags & LK_WAIT_NONZERO)
405			wakeup((void *)lkp);
406		break;
407
408	case LK_DRAIN:
409		/*
410		 * Check that we do not already hold the lock, as it can
411		 * never drain if we do. Unfortunately, we have no way to
412		 * check for holding a shared lock, but at least we can
413		 * check for an exclusive one.
414		 */
415#if !defined(MAX_PERF)
416		if (lkp->lk_lockholder == pid)
417			panic("lockmgr: draining against myself");
418#endif
419
420		error = acquiredrain(lkp, extflags);
421		if (error)
422			break;
423		lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
424		lkp->lk_lockholder = pid;
425		lkp->lk_exclusivecount = 1;
426#if defined(DEBUG_LOCKS)
427			lkp->lk_filename = file;
428			lkp->lk_lineno = line;
429			lkp->lk_lockername = name;
430#endif
431		COUNT(p, 1);
432		break;
433
434	default:
435#if !defined(MAX_PERF)
436		simple_unlock(&lkp->lk_interlock);
437		panic("lockmgr: unknown locktype request %d",
438		    flags & LK_TYPE_MASK);
439#endif
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	simple_unlock(&lkp->lk_interlock);
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		simple_unlock(&lkp->lk_interlock);
467		error = tsleep(&lkp->lk_flags, lkp->lk_prio,
468			lkp->lk_wmesg, lkp->lk_timo);
469		simple_lock(&lkp->lk_interlock);
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
491	simple_lock_init(&lkp->lk_interlock);
492	lkp->lk_flags = (flags & LK_EXTFLG_MASK);
493	lkp->lk_sharecount = 0;
494	lkp->lk_waitcount = 0;
495	lkp->lk_exclusivecount = 0;
496	lkp->lk_prio = prio;
497	lkp->lk_wmesg = wmesg;
498	lkp->lk_timo = timo;
499	lkp->lk_lockholder = LK_NOPROC;
500}
501
502/*
503 * Determine the status of a lock.
504 */
505int
506lockstatus(lkp)
507	struct lock *lkp;
508{
509	int lock_type = 0;
510
511	simple_lock(&lkp->lk_interlock);
512	if (lkp->lk_exclusivecount != 0)
513		lock_type = LK_EXCLUSIVE;
514	else if (lkp->lk_sharecount != 0)
515		lock_type = LK_SHARED;
516	simple_unlock(&lkp->lk_interlock);
517	return (lock_type);
518}
519
520/*
521 * Print out information about state of a lock. Used by VOP_PRINT
522 * routines to display status about contained locks.
523 */
524void
525lockmgr_printinfo(lkp)
526	struct lock *lkp;
527{
528
529	if (lkp->lk_sharecount)
530		printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
531		    lkp->lk_sharecount);
532	else if (lkp->lk_flags & LK_HAVE_EXCL)
533		printf(" lock type %s: EXCL (count %d) by pid %d",
534		    lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
535	if (lkp->lk_waitcount > 0)
536		printf(" with %d pending", lkp->lk_waitcount);
537}
538
539#if defined(SIMPLELOCK_DEBUG) && (NCPUS == 1 || defined(COMPILING_LINT))
540#include <sys/kernel.h>
541#include <sys/sysctl.h>
542
543static int lockpausetime = 0;
544SYSCTL_INT(_debug, OID_AUTO, lockpausetime, CTLFLAG_RW, &lockpausetime, 0, "");
545
546static int simplelockrecurse;
547
548/*
549 * Simple lock functions so that the debugger can see from whence
550 * they are being called.
551 */
552void
553simple_lock_init(alp)
554	struct simplelock *alp;
555{
556
557	alp->lock_data = 0;
558}
559
560void
561_simple_lock(alp, id, l)
562	struct simplelock *alp;
563	const char *id;
564	int l;
565{
566
567	if (simplelockrecurse)
568		return;
569	if (alp->lock_data == 1) {
570		if (lockpausetime == -1)
571			panic("%s:%d: simple_lock: lock held", id, l);
572		printf("%s:%d: simple_lock: lock held\n", id, l);
573		if (lockpausetime == 1) {
574			Debugger("simple_lock");
575			/*BACKTRACE(curproc); */
576		} else if (lockpausetime > 1) {
577			printf("%s:%d: simple_lock: lock held...", id, l);
578			tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
579			    lockpausetime * hz);
580			printf(" continuing\n");
581		}
582	}
583	alp->lock_data = 1;
584	if (curproc)
585		curproc->p_simple_locks++;
586}
587
588int
589_simple_lock_try(alp, id, l)
590	struct simplelock *alp;
591	const char *id;
592	int l;
593{
594
595	if (alp->lock_data)
596		return (0);
597	if (simplelockrecurse)
598		return (1);
599	alp->lock_data = 1;
600	if (curproc)
601		curproc->p_simple_locks++;
602	return (1);
603}
604
605void
606_simple_unlock(alp, id, l)
607	struct simplelock *alp;
608	const char *id;
609	int l;
610{
611
612	if (simplelockrecurse)
613		return;
614	if (alp->lock_data == 0) {
615		if (lockpausetime == -1)
616			panic("%s:%d: simple_unlock: lock not held", id, l);
617		printf("%s:%d: simple_unlock: lock not held\n", id, l);
618		if (lockpausetime == 1) {
619			Debugger("simple_unlock");
620			/* BACKTRACE(curproc); */
621		} else if (lockpausetime > 1) {
622			printf("%s:%d: simple_unlock: lock not held...", id, l);
623			tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
624			    lockpausetime * hz);
625			printf(" continuing\n");
626		}
627	}
628	alp->lock_data = 0;
629	if (curproc)
630		curproc->p_simple_locks--;
631}
632#elif defined(SIMPLELOCK_DEBUG)
633#error "SIMPLELOCK_DEBUG is not compatible with SMP!"
634#endif /* SIMPLELOCK_DEBUG && NCPUS == 1 */
635