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
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include "opt_global.h"
49 #include <sys/param.h>
51 #include <sys/kernel.h>
54 #include <sys/lockmgr.h>
55 #include <sys/mutex.h>
57 #include <sys/systm.h>
58 #include <sys/lock_profile.h>
60 #include <sys/stack.h>
65 static void db_show_lockmgr(struct lock_object *lock);
67 static void lock_lockmgr(struct lock_object *lock, int how);
68 static int unlock_lockmgr(struct lock_object *lock);
70 struct lock_class lock_class_lockmgr = {
72 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
74 .lc_ddb_show = db_show_lockmgr,
76 .lc_lock = lock_lockmgr,
77 .lc_unlock = unlock_lockmgr,
81 * Locking primitives implementation.
82 * Locks provide shared/exclusive sychronization.
86 lock_lockmgr(struct lock_object *lock, int how)
89 panic("lockmgr locks do not support sleep interlocking");
93 unlock_lockmgr(struct lock_object *lock)
96 panic("lockmgr locks do not support sleep interlocking");
99 #define COUNT(td, x) if ((td)) (td)->td_locks += (x)
100 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
101 LK_SHARE_NONZERO | LK_WAIT_NONZERO)
103 static int acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime);
104 static int acquiredrain(struct lock *lkp, int extflags) ;
107 sharelock(struct thread *td, struct lock *lkp, int incr) {
108 lkp->lk_flags |= LK_SHARE_NONZERO;
109 lkp->lk_sharecount += incr;
114 shareunlock(struct thread *td, struct lock *lkp, int decr) {
116 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
119 if (lkp->lk_sharecount == decr) {
120 lkp->lk_flags &= ~LK_SHARE_NONZERO;
121 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
124 lkp->lk_sharecount = 0;
126 lkp->lk_sharecount -= decr;
131 acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime)
133 struct lock *lkp = *lkpp;
136 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x",
137 lkp, extflags, wanted);
139 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted))
142 if ((lkp->lk_flags & wanted) != 0)
143 lock_profile_obtain_lock_failed(&lkp->lk_object, contested, waittime);
145 while ((lkp->lk_flags & wanted) != 0) {
147 "acquire(): lkp == %p, lk_flags == 0x%x sleeping",
149 lkp->lk_flags |= LK_WAIT_NONZERO;
151 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
153 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
155 if (lkp->lk_waitcount == 0)
156 lkp->lk_flags &= ~LK_WAIT_NONZERO;
159 if (extflags & LK_SLEEPFAIL) {
163 if (lkp->lk_newlock != NULL) {
164 mtx_lock(lkp->lk_newlock->lk_interlock);
165 mtx_unlock(lkp->lk_interlock);
166 if (lkp->lk_waitcount == 0)
167 wakeup((void *)(&lkp->lk_newlock));
168 *lkpp = lkp = lkp->lk_newlock;
171 mtx_assert(lkp->lk_interlock, MA_OWNED);
176 * Set, change, or release a lock.
178 * Shared requests increment the shared count. Exclusive requests set the
179 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
180 * accepted shared locks and shared-to-exclusive upgrades to go away.
183 _lockmgr(struct lock *lkp, u_int flags, struct mtx *interlkp,
184 struct thread *td, char *file, int line)
189 int extflags, lockflags;
191 uint64_t waitstart = 0;
199 if ((flags & LK_INTERNAL) == 0)
200 mtx_lock(lkp->lk_interlock);
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);
207 struct stack stack; /* XXX */
209 CTRSTACK(KTR_LOCK, &stack, 0, 1);
213 if (flags & LK_INTERLOCK) {
214 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
215 mtx_unlock(interlkp);
218 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
219 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
220 &lkp->lk_interlock->lock_object,
221 "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
223 if (panicstr != NULL) {
224 mtx_unlock(lkp->lk_interlock);
227 if ((lkp->lk_flags & LK_NOSHARE) &&
228 (flags & LK_TYPE_MASK) == LK_SHARED) {
229 flags &= ~LK_TYPE_MASK;
230 flags |= LK_EXCLUSIVE;
232 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
234 switch (flags & LK_TYPE_MASK) {
238 * If we are not the exclusive lock holder, we have to block
239 * while there is an exclusive lock holder or while an
240 * exclusive lock request or upgrade request is in progress.
242 * However, if TDP_DEADLKTREAT is set, we override exclusive
243 * lock requests or upgrade requests ( but not the exclusive
246 if (lkp->lk_lockholder != thr) {
247 lockflags = LK_HAVE_EXCL;
248 if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT))
249 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
250 error = acquire(&lkp, extflags, lockflags, &contested, &waitstart);
253 sharelock(td, lkp, 1);
254 if (lkp->lk_sharecount == 1)
255 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
257 #if defined(DEBUG_LOCKS)
258 stack_save(&lkp->lk_stack);
263 * We hold an exclusive lock, so downgrade it to shared.
264 * An alternative would be to fail with EDEADLK.
266 sharelock(td, lkp, 1);
267 if (lkp->lk_sharecount == 1)
268 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
269 /* FALLTHROUGH downgrade */
272 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0,
273 ("lockmgr: not holding exclusive lock "
274 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0",
275 lkp->lk_lockholder, thr, lkp->lk_exclusivecount));
276 sharelock(td, lkp, lkp->lk_exclusivecount);
277 COUNT(td, -lkp->lk_exclusivecount);
278 lkp->lk_exclusivecount = 0;
279 lkp->lk_flags &= ~LK_HAVE_EXCL;
280 lkp->lk_lockholder = LK_NOPROC;
281 if (lkp->lk_waitcount)
287 * If another process is ahead of us to get an upgrade,
288 * then we want to fail rather than have an intervening
291 if (lkp->lk_flags & LK_WANT_UPGRADE) {
292 shareunlock(td, lkp, 1);
296 /* FALLTHROUGH normal upgrade */
300 * Upgrade a shared lock to an exclusive one. If another
301 * shared lock has already requested an upgrade to an
302 * exclusive lock, our shared lock is released and an
303 * exclusive lock is requested (which will be granted
304 * after the upgrade). If we return an error, the file
305 * will always be unlocked.
307 if (lkp->lk_lockholder == thr)
308 panic("lockmgr: upgrade exclusive lock");
309 if (lkp->lk_sharecount <= 0)
310 panic("lockmgr: upgrade without shared");
311 shareunlock(td, lkp, 1);
312 if (lkp->lk_sharecount == 0)
313 lock_profile_release_lock(&lkp->lk_object);
315 * If we are just polling, check to see if we will block.
317 if ((extflags & LK_NOWAIT) &&
318 ((lkp->lk_flags & LK_WANT_UPGRADE) ||
319 lkp->lk_sharecount > 1)) {
323 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
325 * We are first shared lock to request an upgrade, so
326 * request upgrade and wait for the shared count to
327 * drop to zero, then take exclusive lock.
329 lkp->lk_flags |= LK_WANT_UPGRADE;
330 error = acquire(&lkp, extflags, LK_SHARE_NONZERO, &contested, &waitstart);
331 lkp->lk_flags &= ~LK_WANT_UPGRADE;
334 if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO))
338 if (lkp->lk_exclusivecount != 0)
339 panic("lockmgr: non-zero exclusive count");
340 lkp->lk_flags |= LK_HAVE_EXCL;
341 lkp->lk_lockholder = thr;
342 lkp->lk_exclusivecount = 1;
344 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
345 #if defined(DEBUG_LOCKS)
346 stack_save(&lkp->lk_stack);
351 * Someone else has requested upgrade. Release our shared
352 * lock, awaken upgrade requestor if we are the last shared
353 * lock, then request an exclusive lock.
355 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
358 /* FALLTHROUGH exclusive request */
361 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) {
365 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
366 panic("lockmgr: locking against myself");
367 if ((extflags & LK_CANRECURSE) != 0) {
368 lkp->lk_exclusivecount++;
374 * If we are just polling, check to see if we will sleep.
376 if ((extflags & LK_NOWAIT) &&
377 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
382 * Try to acquire the want_exclusive flag.
384 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL), &contested, &waitstart);
387 lkp->lk_flags |= LK_WANT_EXCL;
389 * Wait for shared locks and upgrades to finish.
391 error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO, &contested, &waitstart);
392 lkp->lk_flags &= ~LK_WANT_EXCL;
394 if (lkp->lk_flags & LK_WAIT_NONZERO)
398 lkp->lk_flags |= LK_HAVE_EXCL;
399 lkp->lk_lockholder = thr;
400 if (lkp->lk_exclusivecount != 0)
401 panic("lockmgr: non-zero exclusive count");
402 lkp->lk_exclusivecount = 1;
404 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line);
405 #if defined(DEBUG_LOCKS)
406 stack_save(&lkp->lk_stack);
411 if (lkp->lk_exclusivecount != 0) {
412 if (lkp->lk_lockholder != thr &&
413 lkp->lk_lockholder != LK_KERNPROC) {
414 panic("lockmgr: thread %p, not %s %p unlocking",
415 thr, "exclusive lock holder",
418 if (lkp->lk_lockholder != LK_KERNPROC)
420 if (lkp->lk_exclusivecount == 1) {
421 lkp->lk_flags &= ~LK_HAVE_EXCL;
422 lkp->lk_lockholder = LK_NOPROC;
423 lkp->lk_exclusivecount = 0;
424 lock_profile_release_lock(&lkp->lk_object);
426 lkp->lk_exclusivecount--;
428 } else if (lkp->lk_flags & LK_SHARE_NONZERO)
429 shareunlock(td, lkp, 1);
431 printf("lockmgr: thread %p unlocking unheld lock\n",
436 if (lkp->lk_flags & LK_WAIT_NONZERO)
442 * Check that we do not already hold the lock, as it can
443 * never drain if we do. Unfortunately, we have no way to
444 * check for holding a shared lock, but at least we can
445 * check for an exclusive one.
447 if (lkp->lk_lockholder == thr)
448 panic("lockmgr: draining against myself");
450 error = acquiredrain(lkp, extflags);
453 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
454 lkp->lk_lockholder = thr;
455 lkp->lk_exclusivecount = 1;
457 #if defined(DEBUG_LOCKS)
458 stack_save(&lkp->lk_stack);
463 mtx_unlock(lkp->lk_interlock);
464 panic("lockmgr: unknown locktype request %d",
465 flags & LK_TYPE_MASK);
468 if ((lkp->lk_flags & LK_WAITDRAIN) &&
469 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
470 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
471 lkp->lk_flags &= ~LK_WAITDRAIN;
472 wakeup((void *)&lkp->lk_flags);
474 mtx_unlock(lkp->lk_interlock);
479 acquiredrain(struct lock *lkp, int extflags) {
482 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
485 while (lkp->lk_flags & LK_ALL) {
486 lkp->lk_flags |= LK_WAITDRAIN;
487 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
489 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
492 if (extflags & LK_SLEEPFAIL) {
500 * Transfer any waiting processes from one lock to another.
503 transferlockers(from, to)
508 KASSERT(from != to, ("lock transfer to self"));
509 KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock"));
511 mtx_lock(from->lk_interlock);
512 if (from->lk_waitcount == 0) {
513 mtx_unlock(from->lk_interlock);
516 from->lk_newlock = to;
517 wakeup((void *)from);
518 msleep(&from->lk_newlock, from->lk_interlock, from->lk_prio,
520 from->lk_newlock = NULL;
521 from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
522 KASSERT(from->lk_waitcount == 0, ("active lock"));
523 mtx_unlock(from->lk_interlock);
528 * Initialize a lock; required before use.
531 lockinit(lkp, prio, wmesg, timo, flags)
538 CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
539 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
541 lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
542 lkp->lk_flags = (flags & LK_EXTFLG_MASK);
543 lkp->lk_sharecount = 0;
544 lkp->lk_waitcount = 0;
545 lkp->lk_exclusivecount = 0;
548 lkp->lk_lockholder = LK_NOPROC;
549 lkp->lk_newlock = NULL;
551 stack_zero(&lkp->lk_stack);
553 lock_init(&lkp->lk_object, &lock_class_lockmgr, wmesg, NULL,
554 LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE);
565 CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
567 lock_destroy(&lkp->lk_object);
571 * Determine the status of a lock.
583 mtx_lock(lkp->lk_interlock);
586 if (lkp->lk_exclusivecount != 0) {
587 if (td == NULL || lkp->lk_lockholder == td)
588 lock_type = LK_EXCLUSIVE;
590 lock_type = LK_EXCLOTHER;
591 } else if (lkp->lk_sharecount != 0)
592 lock_type = LK_SHARED;
594 mtx_unlock(lkp->lk_interlock);
599 * Determine the number of holders of a lock.
607 mtx_lock(lkp->lk_interlock);
608 count = lkp->lk_exclusivecount + lkp->lk_sharecount;
609 mtx_unlock(lkp->lk_interlock);
614 * Determine the number of waiters on a lock.
622 mtx_lock(lkp->lk_interlock);
623 count = lkp->lk_waitcount;
624 mtx_unlock(lkp->lk_interlock);
629 * Print out information about state of a lock. Used by VOP_PRINT
630 * routines to display status about contained locks.
633 lockmgr_printinfo(lkp)
637 if (lkp->lk_sharecount)
638 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
640 else if (lkp->lk_flags & LK_HAVE_EXCL)
641 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)",
642 lkp->lk_wmesg, lkp->lk_exclusivecount,
643 lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid);
644 if (lkp->lk_waitcount > 0)
645 printf(" with %d pending", lkp->lk_waitcount);
647 stack_print_ddb(&lkp->lk_stack);
653 * Check to see if a thread that is blocked on a sleep queue is actually
654 * blocked on a 'struct lock'. If so, output some details and return true.
655 * If the lock has an exclusive owner, return that in *ownerp.
658 lockmgr_chain(struct thread *td, struct thread **ownerp)
664 /* Simple test to see if wchan points to a lockmgr lock. */
665 if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr &&
666 lkp->lk_wmesg == td->td_wmesg)
670 * If this thread is doing a DRAIN, then it would be asleep on
671 * &lkp->lk_flags rather than lkp.
673 lkp = (struct lock *)((char *)td->td_wchan -
674 offsetof(struct lock, lk_flags));
675 if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr &&
676 lkp->lk_wmesg == td->td_wmesg && (lkp->lk_flags & LK_WAITDRAIN))
679 /* Doen't seem to be a lockmgr lock. */
683 /* Ok, we think we have a lockmgr lock, so output some details. */
684 db_printf("blocked on lk \"%s\" ", lkp->lk_wmesg);
685 if (lkp->lk_sharecount) {
686 db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
689 db_printf("EXCL (count %d)\n", lkp->lk_exclusivecount);
690 *ownerp = lkp->lk_lockholder;
696 db_show_lockmgr(struct lock_object *lock)
701 lkp = (struct lock *)lock;
703 db_printf(" lock type: %s\n", lkp->lk_wmesg);
704 db_printf(" state: ");
705 if (lkp->lk_sharecount)
706 db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
707 else if (lkp->lk_flags & LK_HAVE_EXCL) {
708 td = lkp->lk_lockholder;
709 db_printf("EXCL (count %d) %p ", lkp->lk_exclusivecount, td);
710 db_printf("(tid %d, pid %d, \"%s\")\n", td->td_tid,
711 td->td_proc->p_pid, td->td_proc->p_comm);
713 db_printf("UNLOCKED\n");
714 if (lkp->lk_waitcount > 0)
715 db_printf(" waiters: %d\n", lkp->lk_waitcount);