3 * The Regents of the University of California. All rights reserved.
6 * John S. Dyson. All rights reserved.
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
41 * $FreeBSD: src/sys/kern/kern_lock.c,v 1.31.2.3 2001/12/25 01:44:44 dillon Exp $
42 * $DragonFly: src/sys/kern/kern_lock.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
47 #include <sys/param.h>
50 #include <sys/systm.h>
53 * Locking primitives implementation.
54 * Locks provide shared/exclusive sychronization.
57 #ifdef SIMPLELOCK_DEBUG
58 #define COUNT(p, x) if (p) (p)->p_locks += (x)
63 #define LOCK_WAIT_TIME 100
64 #define LOCK_SAMPLE_WAIT 7
66 #if defined(DIAGNOSTIC)
69 #define LOCK_INLINE __inline
72 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
73 LK_SHARE_NONZERO | LK_WAIT_NONZERO)
75 static int acquire(struct lock *lkp, int extflags, int wanted);
76 static int apause(struct lock *lkp, int flags);
77 static int acquiredrain(struct lock *lkp, int extflags) ;
79 static LOCK_INLINE void
80 sharelock(struct lock *lkp, int incr) {
81 lkp->lk_flags |= LK_SHARE_NONZERO;
82 lkp->lk_sharecount += incr;
85 static LOCK_INLINE void
86 shareunlock(struct lock *lkp, int decr) {
88 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
90 if (lkp->lk_sharecount == decr) {
91 lkp->lk_flags &= ~LK_SHARE_NONZERO;
92 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
95 lkp->lk_sharecount = 0;
97 lkp->lk_sharecount -= decr;
102 * This is the waitloop optimization, and note for this to work
103 * simple_lock and simple_unlock should be subroutines to avoid
104 * optimization troubles.
107 apause(struct lock *lkp, int flags)
113 if ((lkp->lk_flags & flags) == 0)
116 for (lock_wait = LOCK_WAIT_TIME; lock_wait > 0; lock_wait--) {
117 simple_unlock(&lkp->lk_interlock);
118 for (i = LOCK_SAMPLE_WAIT; i > 0; i--)
119 if ((lkp->lk_flags & flags) == 0)
121 simple_lock(&lkp->lk_interlock);
122 if ((lkp->lk_flags & flags) == 0)
130 acquire(struct lock *lkp, int extflags, int wanted) {
133 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) {
137 if (((lkp->lk_flags | extflags) & LK_NOPAUSE) == 0) {
138 error = apause(lkp, wanted);
144 while ((lkp->lk_flags & wanted) != 0) {
145 lkp->lk_flags |= LK_WAIT_NONZERO;
147 simple_unlock(&lkp->lk_interlock);
148 error = tsleep(lkp, lkp->lk_prio, lkp->lk_wmesg,
149 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
150 simple_lock(&lkp->lk_interlock);
151 if (lkp->lk_waitcount == 1) {
152 lkp->lk_flags &= ~LK_WAIT_NONZERO;
153 lkp->lk_waitcount = 0;
161 if (extflags & LK_SLEEPFAIL) {
171 * Set, change, or release a lock.
173 * Shared requests increment the shared count. Exclusive requests set the
174 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
175 * accepted shared locks and shared-to-exclusive upgrades to go away.
179 lockmgr(lkp, flags, interlkp, p)
181 debuglockmgr(lkp, flags, interlkp, p, name, file, line)
185 struct simplelock *interlkp;
188 const char *name; /* Name of lock function */
189 const char *file; /* Name of file call is from */
190 int line; /* Line number in file */
203 simple_lock(&lkp->lk_interlock);
204 if (flags & LK_INTERLOCK)
205 simple_unlock(interlkp);
207 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
209 switch (flags & LK_TYPE_MASK) {
213 * If we are not the exclusive lock holder, we have to block
214 * while there is an exclusive lock holder or while an
215 * exclusive lock request or upgrade request is in progress.
217 * However, if P_DEADLKTREAT is set, we override exclusive
218 * lock requests or upgrade requests ( but not the exclusive
221 if (lkp->lk_lockholder != pid) {
222 if (p && (p->p_flag & P_DEADLKTREAT)) {
232 LK_HAVE_EXCL | LK_WANT_EXCL |
243 * We hold an exclusive lock, so downgrade it to shared.
244 * An alternative would be to fail with EDEADLK.
248 /* fall into downgrade */
251 if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0)
252 panic("lockmgr: not holding exclusive lock");
253 sharelock(lkp, lkp->lk_exclusivecount);
254 lkp->lk_exclusivecount = 0;
255 lkp->lk_flags &= ~LK_HAVE_EXCL;
256 lkp->lk_lockholder = LK_NOPROC;
257 if (lkp->lk_waitcount)
263 * If another process is ahead of us to get an upgrade,
264 * then we want to fail rather than have an intervening
267 if (lkp->lk_flags & LK_WANT_UPGRADE) {
273 /* fall into normal upgrade */
277 * Upgrade a shared lock to an exclusive one. If another
278 * shared lock has already requested an upgrade to an
279 * exclusive lock, our shared lock is released and an
280 * exclusive lock is requested (which will be granted
281 * after the upgrade). If we return an error, the file
282 * will always be unlocked.
284 if ((lkp->lk_lockholder == pid) || (lkp->lk_sharecount <= 0))
285 panic("lockmgr: upgrade exclusive lock");
289 * If we are just polling, check to see if we will block.
291 if ((extflags & LK_NOWAIT) &&
292 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
293 lkp->lk_sharecount > 1)) {
297 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
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.
303 lkp->lk_flags |= LK_WANT_UPGRADE;
304 error = acquire(lkp, extflags, LK_SHARE_NONZERO);
305 lkp->lk_flags &= ~LK_WANT_UPGRADE;
309 lkp->lk_flags |= LK_HAVE_EXCL;
310 lkp->lk_lockholder = pid;
311 if (lkp->lk_exclusivecount != 0)
312 panic("lockmgr: non-zero exclusive count");
313 lkp->lk_exclusivecount = 1;
314 #if defined(DEBUG_LOCKS)
315 lkp->lk_filename = file;
316 lkp->lk_lineno = line;
317 lkp->lk_lockername = name;
323 * Someone else has requested upgrade. Release our shared
324 * lock, awaken upgrade requestor if we are the last shared
325 * lock, then request an exclusive lock.
327 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
330 /* fall into exclusive request */
333 if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) {
337 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
338 panic("lockmgr: locking against myself");
339 if ((extflags & LK_CANRECURSE) != 0) {
340 lkp->lk_exclusivecount++;
346 * If we are just polling, check to see if we will sleep.
348 if ((extflags & LK_NOWAIT) &&
349 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
354 * Try to acquire the want_exclusive flag.
356 error = acquire(lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
359 lkp->lk_flags |= LK_WANT_EXCL;
361 * Wait for shared locks and upgrades to finish.
363 error = acquire(lkp, extflags, LK_WANT_UPGRADE | LK_SHARE_NONZERO);
364 lkp->lk_flags &= ~LK_WANT_EXCL;
367 lkp->lk_flags |= LK_HAVE_EXCL;
368 lkp->lk_lockholder = pid;
369 if (lkp->lk_exclusivecount != 0)
370 panic("lockmgr: non-zero exclusive count");
371 lkp->lk_exclusivecount = 1;
372 #if defined(DEBUG_LOCKS)
373 lkp->lk_filename = file;
374 lkp->lk_lineno = line;
375 lkp->lk_lockername = name;
381 if (lkp->lk_exclusivecount != 0) {
382 if (lkp->lk_lockholder != pid &&
383 lkp->lk_lockholder != LK_KERNPROC) {
384 panic("lockmgr: pid %d, not %s %d unlocking",
385 pid, "exclusive lock holder",
388 if (lkp->lk_lockholder != LK_KERNPROC) {
391 if (lkp->lk_exclusivecount == 1) {
392 lkp->lk_flags &= ~LK_HAVE_EXCL;
393 lkp->lk_lockholder = LK_NOPROC;
394 lkp->lk_exclusivecount = 0;
396 lkp->lk_exclusivecount--;
398 } else if (lkp->lk_flags & LK_SHARE_NONZERO) {
402 if (lkp->lk_flags & LK_WAIT_NONZERO)
408 * Check that we do not already hold the lock, as it can
409 * never drain if we do. Unfortunately, we have no way to
410 * check for holding a shared lock, but at least we can
411 * check for an exclusive one.
413 if (lkp->lk_lockholder == pid)
414 panic("lockmgr: draining against myself");
416 error = acquiredrain(lkp, extflags);
419 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
420 lkp->lk_lockholder = pid;
421 lkp->lk_exclusivecount = 1;
422 #if defined(DEBUG_LOCKS)
423 lkp->lk_filename = file;
424 lkp->lk_lineno = line;
425 lkp->lk_lockername = name;
431 simple_unlock(&lkp->lk_interlock);
432 panic("lockmgr: unknown locktype request %d",
433 flags & LK_TYPE_MASK);
436 if ((lkp->lk_flags & LK_WAITDRAIN) &&
437 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
438 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
439 lkp->lk_flags &= ~LK_WAITDRAIN;
440 wakeup((void *)&lkp->lk_flags);
442 simple_unlock(&lkp->lk_interlock);
447 acquiredrain(struct lock *lkp, int extflags) {
450 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
454 error = apause(lkp, LK_ALL);
458 while (lkp->lk_flags & LK_ALL) {
459 lkp->lk_flags |= LK_WAITDRAIN;
460 simple_unlock(&lkp->lk_interlock);
461 error = tsleep(&lkp->lk_flags, lkp->lk_prio,
463 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
464 simple_lock(&lkp->lk_interlock);
467 if (extflags & LK_SLEEPFAIL) {
475 * Initialize a lock; required before use.
478 lockinit(lkp, prio, wmesg, timo, flags)
486 simple_lock_init(&lkp->lk_interlock);
487 lkp->lk_flags = (flags & LK_EXTFLG_MASK);
488 lkp->lk_sharecount = 0;
489 lkp->lk_waitcount = 0;
490 lkp->lk_exclusivecount = 0;
492 lkp->lk_wmesg = wmesg;
494 lkp->lk_lockholder = LK_NOPROC;
498 * Determine the status of a lock.
507 simple_lock(&lkp->lk_interlock);
508 if (lkp->lk_exclusivecount != 0) {
509 if (p == NULL || lkp->lk_lockholder == p->p_pid)
510 lock_type = LK_EXCLUSIVE;
512 lock_type = LK_EXCLOTHER;
513 } else if (lkp->lk_sharecount != 0)
514 lock_type = LK_SHARED;
515 simple_unlock(&lkp->lk_interlock);
520 * Determine the number of holders of a lock.
528 simple_lock(&lkp->lk_interlock);
529 count = lkp->lk_exclusivecount + lkp->lk_sharecount;
530 simple_unlock(&lkp->lk_interlock);
535 * Print out information about state of a lock. Used by VOP_PRINT
536 * routines to display status about contained locks.
539 lockmgr_printinfo(lkp)
543 if (lkp->lk_sharecount)
544 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
546 else if (lkp->lk_flags & LK_HAVE_EXCL)
547 printf(" lock type %s: EXCL (count %d) by pid %d",
548 lkp->lk_wmesg, lkp->lk_exclusivecount, lkp->lk_lockholder);
549 if (lkp->lk_waitcount > 0)
550 printf(" with %d pending", lkp->lk_waitcount);
553 #if defined(SIMPLELOCK_DEBUG) && (MAXCPU == 1 || defined(COMPILING_LINT))
554 #include <sys/kernel.h>
555 #include <sys/sysctl.h>
557 static int lockpausetime = 0;
558 SYSCTL_INT(_debug, OID_AUTO, lockpausetime, CTLFLAG_RW, &lockpausetime, 0, "");
560 static int simplelockrecurse;
563 * Simple lock functions so that the debugger can see from whence
564 * they are being called.
567 simple_lock_init(alp)
568 struct simplelock *alp;
575 _simple_lock(alp, id, l)
576 struct simplelock *alp;
581 if (simplelockrecurse)
583 if (alp->lock_data == 1) {
584 if (lockpausetime == -1)
585 panic("%s:%d: simple_lock: lock held", id, l);
586 printf("%s:%d: simple_lock: lock held\n", id, l);
587 if (lockpausetime == 1) {
588 Debugger("simple_lock");
589 /*BACKTRACE(curproc); */
590 } else if (lockpausetime > 1) {
591 printf("%s:%d: simple_lock: lock held...", id, l);
592 tsleep(&lockpausetime, PCATCH | PPAUSE, "slock",
594 printf(" continuing\n");
599 curproc->p_simple_locks++;
603 _simple_lock_try(alp, id, l)
604 struct simplelock *alp;
611 if (simplelockrecurse)
615 curproc->p_simple_locks++;
620 _simple_unlock(alp, id, l)
621 struct simplelock *alp;
626 if (simplelockrecurse)
628 if (alp->lock_data == 0) {
629 if (lockpausetime == -1)
630 panic("%s:%d: simple_unlock: lock not held", id, l);
631 printf("%s:%d: simple_unlock: lock not held\n", id, l);
632 if (lockpausetime == 1) {
633 Debugger("simple_unlock");
634 /* BACKTRACE(curproc); */
635 } else if (lockpausetime > 1) {
636 printf("%s:%d: simple_unlock: lock not held...", id, l);
637 tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock",
639 printf(" continuing\n");
644 curproc->p_simple_locks--;
646 #elif defined(SIMPLELOCK_DEBUG)
647 #error "SIMPLELOCK_DEBUG is not compatible with SMP!"
648 #endif /* SIMPLELOCK_DEBUG && MAXCPU == 1 */