2 * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice(s), this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified other than the possible
11 * addition of one or more copyright notices.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice(s), this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 #include "opt_adaptive_lockmgrs.h"
31 #include "opt_kdtrace.h"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/linker_set.h>
40 #include <sys/lock_profile.h>
41 #include <sys/lockmgr.h>
42 #include <sys/mutex.h>
44 #include <sys/sleepqueue.h>
46 #include <sys/stack.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #include <machine/cpu.h>
57 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
58 (LK_ADAPTIVE | LK_NOSHARE));
59 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
60 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
62 #define SQ_EXCLUSIVE_QUEUE 0
63 #define SQ_SHARED_QUEUE 1
66 #define _lockmgr_assert(lk, what, file, line)
67 #define TD_LOCKS_INC(td)
68 #define TD_LOCKS_DEC(td)
70 #define TD_LOCKS_INC(td) ((td)->td_locks++)
71 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
73 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
74 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
77 #define STACK_PRINT(lk)
78 #define STACK_SAVE(lk)
79 #define STACK_ZERO(lk)
81 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
82 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
83 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
86 #define LOCK_LOG2(lk, string, arg1, arg2) \
87 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
88 CTR2(KTR_LOCK, (string), (arg1), (arg2))
89 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
90 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
91 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
93 #define GIANT_DECLARE \
95 WITNESS_SAVE_DECL(Giant)
96 #define GIANT_RESTORE() do { \
100 WITNESS_RESTORE(&Giant.lock_object, Giant); \
103 #define GIANT_SAVE() do { \
104 if (mtx_owned(&Giant)) { \
105 WITNESS_SAVE(&Giant.lock_object, Giant); \
106 while (mtx_owned(&Giant)) { \
108 mtx_unlock(&Giant); \
113 #define LK_CAN_SHARE(x) \
114 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
115 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
116 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
117 #define LK_TRYOP(x) \
120 #define LK_CAN_WITNESS(x) \
121 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
122 #define LK_TRYWIT(x) \
123 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
125 #define LK_CAN_ADAPT(lk, f) \
126 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
127 ((f) & LK_SLEEPFAIL) == 0)
129 #define lockmgr_disowned(lk) \
130 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
132 #define lockmgr_xlocked(lk) \
133 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
135 static void assert_lockmgr(struct lock_object *lock, int how);
137 static void db_show_lockmgr(struct lock_object *lock);
139 static void lock_lockmgr(struct lock_object *lock, int how);
141 static int owner_lockmgr(struct lock_object *lock, struct thread **owner);
143 static int unlock_lockmgr(struct lock_object *lock);
145 struct lock_class lock_class_lockmgr = {
146 .lc_name = "lockmgr",
147 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
148 .lc_assert = assert_lockmgr,
150 .lc_ddb_show = db_show_lockmgr,
152 .lc_lock = lock_lockmgr,
153 .lc_unlock = unlock_lockmgr,
155 .lc_owner = owner_lockmgr,
159 #ifdef ADAPTIVE_LOCKMGRS
160 static u_int alk_retries = 10;
161 static u_int alk_loops = 10000;
162 SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL, "lockmgr debugging");
163 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, "");
164 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, "");
167 static __inline struct thread *
168 lockmgr_xholder(struct lock *lk)
173 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
177 * It assumes sleepq_lock held and returns with this one unheld.
178 * It also assumes the generic interlock is sane and previously checked.
179 * If LK_INTERLOCK is specified the interlock is not reacquired after the
183 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
184 const char *wmesg, int pri, int timo, int queue)
187 struct lock_class *class;
190 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
191 catch = pri & PCATCH;
195 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
196 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
198 if (flags & LK_INTERLOCK)
199 class->lc_unlock(ilk);
200 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
203 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
204 SLEEPQ_INTERRUPTIBLE : 0), queue);
205 if ((flags & LK_TIMELOCK) && timo)
206 sleepq_set_timeout(&lk->lock_object, timo);
209 * Decisional switch for real sleeping.
211 if ((flags & LK_TIMELOCK) && timo && catch)
212 error = sleepq_timedwait_sig(&lk->lock_object, pri);
213 else if ((flags & LK_TIMELOCK) && timo)
214 error = sleepq_timedwait(&lk->lock_object, pri);
216 error = sleepq_wait_sig(&lk->lock_object, pri);
218 sleepq_wait(&lk->lock_object, pri);
220 if ((flags & LK_SLEEPFAIL) && error == 0)
227 wakeupshlk(struct lock *lk, const char *file, int line)
231 int queue, wakeup_swapper;
233 TD_LOCKS_DEC(curthread);
234 TD_SLOCKS_DEC(curthread);
235 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
236 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
243 * If there is more than one shared lock held, just drop one
246 if (LK_SHARERS(x) > 1) {
247 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
254 * If there are not waiters on the exclusive queue, drop the
257 if ((x & LK_ALL_WAITERS) == 0) {
258 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
260 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
266 * We should have a sharer with waiters, so enter the hard
267 * path in order to handle wakeups correctly.
269 sleepq_lock(&lk->lock_object);
270 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
274 * If the lock has exclusive waiters, give them preference in
275 * order to avoid deadlock with shared runners up.
276 * If interruptible sleeps left the exclusive queue empty
277 * avoid a starvation for the threads sleeping on the shared
278 * queue by giving them precedence and cleaning up the
279 * exclusive waiters bit anyway.
281 realexslp = sleepq_sleepcnt(&lk->lock_object,
283 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
284 if (lk->lk_exslpfail < realexslp) {
285 lk->lk_exslpfail = 0;
286 queue = SQ_EXCLUSIVE_QUEUE;
287 v |= (x & LK_SHARED_WAITERS);
289 lk->lk_exslpfail = 0;
291 "%s: %p has only LK_SLEEPFAIL sleepers",
294 "%s: %p waking up threads on the exclusive queue",
297 sleepq_broadcast(&lk->lock_object,
298 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
299 queue = SQ_SHARED_QUEUE;
303 MPASS(lk->lk_exslpfail == 0);
304 queue = SQ_SHARED_QUEUE;
307 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
309 sleepq_release(&lk->lock_object);
312 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
313 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
315 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
317 sleepq_release(&lk->lock_object);
321 lock_profile_release_lock(&lk->lock_object);
322 return (wakeup_swapper);
326 assert_lockmgr(struct lock_object *lock, int what)
329 panic("lockmgr locks do not support assertions");
333 lock_lockmgr(struct lock_object *lock, int how)
336 panic("lockmgr locks do not support sleep interlocking");
340 unlock_lockmgr(struct lock_object *lock)
343 panic("lockmgr locks do not support sleep interlocking");
348 owner_lockmgr(struct lock_object *lock, struct thread **owner)
351 panic("lockmgr locks do not support owner inquiring");
356 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
360 MPASS((flags & ~LK_INIT_MASK) == 0);
361 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
362 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
365 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
366 if (flags & LK_CANRECURSE)
367 iflags |= LO_RECURSABLE;
368 if ((flags & LK_NODUP) == 0)
370 if (flags & LK_NOPROFILE)
371 iflags |= LO_NOPROFILE;
372 if ((flags & LK_NOWITNESS) == 0)
373 iflags |= LO_WITNESS;
374 if (flags & LK_QUIET)
376 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
378 lk->lk_lock = LK_UNLOCKED;
380 lk->lk_exslpfail = 0;
383 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
388 lockdestroy(struct lock *lk)
391 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
392 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
393 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
394 lock_destroy(&lk->lock_object);
398 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
399 const char *wmesg, int pri, int timo, const char *file, int line)
402 struct lock_class *class;
406 int error, ipri, itimo, queue, wakeup_swapper;
407 #ifdef LOCK_PROFILING
408 uint64_t waittime = 0;
411 #ifdef ADAPTIVE_LOCKMGRS
412 volatile struct thread *owner;
413 u_int i, spintries = 0;
417 tid = (uintptr_t)curthread;
418 op = (flags & LK_TYPE_MASK);
419 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
420 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
421 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
423 MPASS((flags & ~LK_TOTAL_MASK) == 0);
424 KASSERT((op & (op - 1)) == 0,
425 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
426 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
427 (op != LK_DOWNGRADE && op != LK_RELEASE),
428 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
429 __func__, file, line));
430 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
431 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
432 __func__, file, line));
434 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
435 if (panicstr != NULL) {
436 if (flags & LK_INTERLOCK)
437 class->lc_unlock(ilk);
441 if (op == LK_SHARED && (lk->lock_object.lo_flags & LK_NOSHARE))
447 if (LK_CAN_WITNESS(flags))
448 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
454 * If no other thread has an exclusive lock, or
455 * no exclusive waiter is present, bump the count of
456 * sharers. Since we have to preserve the state of
457 * waiters, if we fail to acquire the shared lock
458 * loop back and retry.
460 if (LK_CAN_SHARE(x)) {
461 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
466 lock_profile_obtain_lock_failed(&lk->lock_object,
467 &contested, &waittime);
470 * If the lock is already held by curthread in
471 * exclusive way avoid a deadlock.
473 if (LK_HOLDER(x) == tid) {
475 "%s: %p already held in exclusive mode",
482 * If the lock is expected to not sleep just give up
485 if (LK_TRYOP(flags)) {
486 LOCK_LOG2(lk, "%s: %p fails the try operation",
492 #ifdef ADAPTIVE_LOCKMGRS
494 * If the owner is running on another CPU, spin until
495 * the owner stops running or the state of the lock
496 * changes. We need a double-state handle here
497 * because for a failed acquisition the lock can be
498 * either held in exclusive mode or shared mode
499 * (for the writer starvation avoidance technique).
501 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
502 LK_HOLDER(x) != LK_KERNPROC) {
503 owner = (struct thread *)LK_HOLDER(x);
504 if (LOCK_LOG_TEST(&lk->lock_object, 0))
506 "%s: spinning on %p held by %p",
507 __func__, lk, owner);
510 * If we are holding also an interlock drop it
511 * in order to avoid a deadlock if the lockmgr
512 * owner is adaptively spinning on the
515 if (flags & LK_INTERLOCK) {
516 class->lc_unlock(ilk);
517 flags &= ~LK_INTERLOCK;
520 while (LK_HOLDER(lk->lk_lock) ==
521 (uintptr_t)owner && TD_IS_RUNNING(owner))
525 } else if (LK_CAN_ADAPT(lk, flags) &&
526 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
527 spintries < alk_retries) {
528 if (flags & LK_INTERLOCK) {
529 class->lc_unlock(ilk);
530 flags &= ~LK_INTERLOCK;
534 for (i = 0; i < alk_loops; i++) {
535 if (LOCK_LOG_TEST(&lk->lock_object, 0))
537 "%s: shared spinning on %p with %u and %u",
538 __func__, lk, spintries, i);
540 if ((x & LK_SHARE) == 0 ||
541 LK_CAN_SHARE(x) != 0)
552 * Acquire the sleepqueue chain lock because we
553 * probabilly will need to manipulate waiters flags.
555 sleepq_lock(&lk->lock_object);
559 * if the lock can be acquired in shared mode, try
562 if (LK_CAN_SHARE(x)) {
563 sleepq_release(&lk->lock_object);
567 #ifdef ADAPTIVE_LOCKMGRS
569 * The current lock owner might have started executing
570 * on another CPU (or the lock could have changed
571 * owner) while we were waiting on the turnstile
572 * chain lock. If so, drop the turnstile lock and try
575 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
576 LK_HOLDER(x) != LK_KERNPROC) {
577 owner = (struct thread *)LK_HOLDER(x);
578 if (TD_IS_RUNNING(owner)) {
579 sleepq_release(&lk->lock_object);
586 * Try to set the LK_SHARED_WAITERS flag. If we fail,
587 * loop back and retry.
589 if ((x & LK_SHARED_WAITERS) == 0) {
590 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
591 x | LK_SHARED_WAITERS)) {
592 sleepq_release(&lk->lock_object);
595 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
600 * As far as we have been unable to acquire the
601 * shared lock and the shared waiters flag is set,
604 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
606 flags &= ~LK_INTERLOCK;
609 "%s: interrupted sleep for %p with %d",
610 __func__, lk, error);
613 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
617 lock_profile_obtain_lock_success(&lk->lock_object,
618 contested, waittime, file, line);
619 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
621 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
623 TD_LOCKS_INC(curthread);
624 TD_SLOCKS_INC(curthread);
629 _lockmgr_assert(lk, KA_SLOCKED, file, line);
631 x = v & LK_ALL_WAITERS;
632 v &= LK_EXCLUSIVE_SPINNERS;
635 * Try to switch from one shared lock to an exclusive one.
636 * We need to preserve waiters flags during the operation.
638 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
640 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
642 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
643 LK_TRYWIT(flags), file, line);
644 TD_SLOCKS_DEC(curthread);
649 * We have been unable to succeed in upgrading, so just
650 * give up the shared lock.
652 wakeup_swapper |= wakeupshlk(lk, file, line);
656 if (LK_CAN_WITNESS(flags))
657 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
658 LOP_EXCLUSIVE, file, line, ilk);
661 * If curthread already holds the lock and this one is
662 * allowed to recurse, simply recurse on it.
664 if (lockmgr_xlocked(lk)) {
665 if ((flags & LK_CANRECURSE) == 0 &&
666 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
669 * If the lock is expected to not panic just
670 * give up and return.
672 if (LK_TRYOP(flags)) {
674 "%s: %p fails the try operation",
679 if (flags & LK_INTERLOCK)
680 class->lc_unlock(ilk);
681 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
682 __func__, iwmesg, file, line);
685 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
686 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
687 lk->lk_recurse, file, line);
688 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
689 LK_TRYWIT(flags), file, line);
690 TD_LOCKS_INC(curthread);
694 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
696 lock_profile_obtain_lock_failed(&lk->lock_object,
697 &contested, &waittime);
700 * If the lock is expected to not sleep just give up
703 if (LK_TRYOP(flags)) {
704 LOCK_LOG2(lk, "%s: %p fails the try operation",
710 #ifdef ADAPTIVE_LOCKMGRS
712 * If the owner is running on another CPU, spin until
713 * the owner stops running or the state of the lock
717 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
718 LK_HOLDER(x) != LK_KERNPROC) {
719 owner = (struct thread *)LK_HOLDER(x);
720 if (LOCK_LOG_TEST(&lk->lock_object, 0))
722 "%s: spinning on %p held by %p",
723 __func__, lk, owner);
726 * If we are holding also an interlock drop it
727 * in order to avoid a deadlock if the lockmgr
728 * owner is adaptively spinning on the
731 if (flags & LK_INTERLOCK) {
732 class->lc_unlock(ilk);
733 flags &= ~LK_INTERLOCK;
736 while (LK_HOLDER(lk->lk_lock) ==
737 (uintptr_t)owner && TD_IS_RUNNING(owner))
741 } else if (LK_CAN_ADAPT(lk, flags) &&
742 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
743 spintries < alk_retries) {
744 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
745 !atomic_cmpset_ptr(&lk->lk_lock, x,
746 x | LK_EXCLUSIVE_SPINNERS))
748 if (flags & LK_INTERLOCK) {
749 class->lc_unlock(ilk);
750 flags &= ~LK_INTERLOCK;
754 for (i = 0; i < alk_loops; i++) {
755 if (LOCK_LOG_TEST(&lk->lock_object, 0))
757 "%s: shared spinning on %p with %u and %u",
758 __func__, lk, spintries, i);
760 LK_EXCLUSIVE_SPINNERS) == 0)
771 * Acquire the sleepqueue chain lock because we
772 * probabilly will need to manipulate waiters flags.
774 sleepq_lock(&lk->lock_object);
778 * if the lock has been released while we spun on
779 * the sleepqueue chain lock just try again.
781 if (x == LK_UNLOCKED) {
782 sleepq_release(&lk->lock_object);
786 #ifdef ADAPTIVE_LOCKMGRS
788 * The current lock owner might have started executing
789 * on another CPU (or the lock could have changed
790 * owner) while we were waiting on the turnstile
791 * chain lock. If so, drop the turnstile lock and try
794 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
795 LK_HOLDER(x) != LK_KERNPROC) {
796 owner = (struct thread *)LK_HOLDER(x);
797 if (TD_IS_RUNNING(owner)) {
798 sleepq_release(&lk->lock_object);
805 * The lock can be in the state where there is a
806 * pending queue of waiters, but still no owner.
807 * This happens when the lock is contested and an
808 * owner is going to claim the lock.
809 * If curthread is the one successfully acquiring it
810 * claim lock ownership and return, preserving waiters
813 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
814 if ((x & ~v) == LK_UNLOCKED) {
815 v &= ~LK_EXCLUSIVE_SPINNERS;
816 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
818 sleepq_release(&lk->lock_object);
820 "%s: %p claimed by a new writer",
824 sleepq_release(&lk->lock_object);
829 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
830 * fail, loop back and retry.
832 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
833 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
834 x | LK_EXCLUSIVE_WAITERS)) {
835 sleepq_release(&lk->lock_object);
838 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
843 * As far as we have been unable to acquire the
844 * exclusive lock and the exclusive waiters flag
845 * is set, we will sleep.
847 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
849 flags &= ~LK_INTERLOCK;
852 "%s: interrupted sleep for %p with %d",
853 __func__, lk, error);
856 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
860 lock_profile_obtain_lock_success(&lk->lock_object,
861 contested, waittime, file, line);
862 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
863 lk->lk_recurse, file, line);
864 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
865 LK_TRYWIT(flags), file, line);
866 TD_LOCKS_INC(curthread);
871 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
872 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
873 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
874 TD_SLOCKS_INC(curthread);
877 * In order to preserve waiters flags, just spin.
881 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
883 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
884 LK_SHARERS_LOCK(1) | x))
890 _lockmgr_assert(lk, KA_LOCKED, file, line);
893 if ((x & LK_SHARE) == 0) {
896 * As first option, treact the lock as if it has not
898 * Fix-up the tid var if the lock has been disowned.
900 if (LK_HOLDER(x) == LK_KERNPROC)
903 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
905 TD_LOCKS_DEC(curthread);
907 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
908 lk->lk_recurse, file, line);
911 * The lock is held in exclusive mode.
912 * If the lock is recursed also, then unrecurse it.
914 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
915 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
920 if (tid != LK_KERNPROC)
921 lock_profile_release_lock(&lk->lock_object);
923 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
927 sleepq_lock(&lk->lock_object);
932 * If the lock has exclusive waiters, give them
933 * preference in order to avoid deadlock with
935 * If interruptible sleeps left the exclusive queue
936 * empty avoid a starvation for the threads sleeping
937 * on the shared queue by giving them precedence
938 * and cleaning up the exclusive waiters bit anyway.
940 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
941 realexslp = sleepq_sleepcnt(&lk->lock_object,
943 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
944 if (lk->lk_exslpfail < realexslp) {
945 lk->lk_exslpfail = 0;
946 queue = SQ_EXCLUSIVE_QUEUE;
947 v |= (x & LK_SHARED_WAITERS);
949 lk->lk_exslpfail = 0;
951 "%s: %p has only LK_SLEEPFAIL sleepers",
954 "%s: %p waking up threads on the exclusive queue",
957 sleepq_broadcast(&lk->lock_object,
958 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
959 queue = SQ_SHARED_QUEUE;
962 MPASS(lk->lk_exslpfail == 0);
963 queue = SQ_SHARED_QUEUE;
967 "%s: %p waking up threads on the %s queue",
968 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
970 atomic_store_rel_ptr(&lk->lk_lock, v);
971 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
972 SLEEPQ_LK, 0, queue);
973 sleepq_release(&lk->lock_object);
976 wakeup_swapper = wakeupshlk(lk, file, line);
979 if (LK_CAN_WITNESS(flags))
980 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
981 LOP_EXCLUSIVE, file, line, ilk);
984 * Trying to drain a lock we already own will result in a
987 if (lockmgr_xlocked(lk)) {
988 if (flags & LK_INTERLOCK)
989 class->lc_unlock(ilk);
990 panic("%s: draining %s with the lock held @ %s:%d\n",
991 __func__, iwmesg, file, line);
994 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
995 lock_profile_obtain_lock_failed(&lk->lock_object,
996 &contested, &waittime);
999 * If the lock is expected to not sleep just give up
1002 if (LK_TRYOP(flags)) {
1003 LOCK_LOG2(lk, "%s: %p fails the try operation",
1010 * Acquire the sleepqueue chain lock because we
1011 * probabilly will need to manipulate waiters flags.
1013 sleepq_lock(&lk->lock_object);
1017 * if the lock has been released while we spun on
1018 * the sleepqueue chain lock just try again.
1020 if (x == LK_UNLOCKED) {
1021 sleepq_release(&lk->lock_object);
1025 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1026 if ((x & ~v) == LK_UNLOCKED) {
1027 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1030 * If interruptible sleeps left the exclusive
1031 * queue empty avoid a starvation for the
1032 * threads sleeping on the shared queue by
1033 * giving them precedence and cleaning up the
1034 * exclusive waiters bit anyway.
1036 if (v & LK_EXCLUSIVE_WAITERS) {
1037 queue = SQ_EXCLUSIVE_QUEUE;
1038 v &= ~LK_EXCLUSIVE_WAITERS;
1040 MPASS(v & LK_SHARED_WAITERS);
1041 MPASS(lk->lk_exslpfail == 0);
1042 queue = SQ_SHARED_QUEUE;
1043 v &= ~LK_SHARED_WAITERS;
1045 if (queue == SQ_EXCLUSIVE_QUEUE) {
1047 sleepq_sleepcnt(&lk->lock_object,
1048 SQ_EXCLUSIVE_QUEUE);
1049 if (lk->lk_exslpfail >= realexslp) {
1050 lk->lk_exslpfail = 0;
1051 queue = SQ_SHARED_QUEUE;
1052 v &= ~LK_SHARED_WAITERS;
1053 if (realexslp != 0) {
1055 "%s: %p has only LK_SLEEPFAIL sleepers",
1058 "%s: %p waking up threads on the exclusive queue",
1064 SQ_EXCLUSIVE_QUEUE);
1067 lk->lk_exslpfail = 0;
1069 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1070 sleepq_release(&lk->lock_object);
1074 "%s: %p waking up all threads on the %s queue",
1075 __func__, lk, queue == SQ_SHARED_QUEUE ?
1076 "shared" : "exclusive");
1077 wakeup_swapper |= sleepq_broadcast(
1078 &lk->lock_object, SLEEPQ_LK, 0, queue);
1081 * If shared waiters have been woken up we need
1082 * to wait for one of them to acquire the lock
1083 * before to set the exclusive waiters in
1084 * order to avoid a deadlock.
1086 if (queue == SQ_SHARED_QUEUE) {
1087 for (v = lk->lk_lock;
1088 (v & LK_SHARE) && !LK_SHARERS(v);
1095 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1096 * fail, loop back and retry.
1098 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1099 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1100 x | LK_EXCLUSIVE_WAITERS)) {
1101 sleepq_release(&lk->lock_object);
1104 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1109 * As far as we have been unable to acquire the
1110 * exclusive lock and the exclusive waiters flag
1111 * is set, we will sleep.
1113 if (flags & LK_INTERLOCK) {
1114 class->lc_unlock(ilk);
1115 flags &= ~LK_INTERLOCK;
1118 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1119 SQ_EXCLUSIVE_QUEUE);
1120 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1122 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1127 lock_profile_obtain_lock_success(&lk->lock_object,
1128 contested, waittime, file, line);
1129 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1130 lk->lk_recurse, file, line);
1131 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1132 LK_TRYWIT(flags), file, line);
1133 TD_LOCKS_INC(curthread);
1138 if (flags & LK_INTERLOCK)
1139 class->lc_unlock(ilk);
1140 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1143 if (flags & LK_INTERLOCK)
1144 class->lc_unlock(ilk);
1152 _lockmgr_disown(struct lock *lk, const char *file, int line)
1156 tid = (uintptr_t)curthread;
1157 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1160 * If the owner is already LK_KERNPROC just skip the whole operation.
1162 if (LK_HOLDER(lk->lk_lock) != tid)
1164 lock_profile_release_lock(&lk->lock_object);
1165 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1166 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1167 TD_LOCKS_DEC(curthread);
1171 * In order to preserve waiters flags, just spin.
1175 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1176 x &= LK_ALL_WAITERS;
1177 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1185 lockmgr_printinfo(struct lock *lk)
1190 if (lk->lk_lock == LK_UNLOCKED)
1191 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1192 else if (lk->lk_lock & LK_SHARE)
1193 printf("lock type %s: SHARED (count %ju)\n",
1194 lk->lock_object.lo_name,
1195 (uintmax_t)LK_SHARERS(lk->lk_lock));
1197 td = lockmgr_xholder(lk);
1198 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1199 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1203 if (x & LK_EXCLUSIVE_WAITERS)
1204 printf(" with exclusive waiters pending\n");
1205 if (x & LK_SHARED_WAITERS)
1206 printf(" with shared waiters pending\n");
1207 if (x & LK_EXCLUSIVE_SPINNERS)
1208 printf(" with exclusive spinners pending\n");
1214 lockstatus(struct lock *lk)
1223 if ((x & LK_SHARE) == 0) {
1224 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1228 } else if (x == LK_UNLOCKED)
1234 #ifdef INVARIANT_SUPPORT
1236 #undef _lockmgr_assert
1240 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1244 if (panicstr != NULL)
1248 case KA_SLOCKED | KA_NOTRECURSED:
1249 case KA_SLOCKED | KA_RECURSED:
1252 case KA_LOCKED | KA_NOTRECURSED:
1253 case KA_LOCKED | KA_RECURSED:
1257 * We cannot trust WITNESS if the lock is held in exclusive
1258 * mode and a call to lockmgr_disown() happened.
1259 * Workaround this skipping the check if the lock is held in
1260 * exclusive mode even for the KA_LOCKED case.
1262 if (slocked || (lk->lk_lock & LK_SHARE)) {
1263 witness_assert(&lk->lock_object, what, file, line);
1267 if (lk->lk_lock == LK_UNLOCKED ||
1268 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1269 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1270 panic("Lock %s not %slocked @ %s:%d\n",
1271 lk->lock_object.lo_name, slocked ? "share" : "",
1274 if ((lk->lk_lock & LK_SHARE) == 0) {
1275 if (lockmgr_recursed(lk)) {
1276 if (what & KA_NOTRECURSED)
1277 panic("Lock %s recursed @ %s:%d\n",
1278 lk->lock_object.lo_name, file,
1280 } else if (what & KA_RECURSED)
1281 panic("Lock %s not recursed @ %s:%d\n",
1282 lk->lock_object.lo_name, file, line);
1286 case KA_XLOCKED | KA_NOTRECURSED:
1287 case KA_XLOCKED | KA_RECURSED:
1288 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1289 panic("Lock %s not exclusively locked @ %s:%d\n",
1290 lk->lock_object.lo_name, file, line);
1291 if (lockmgr_recursed(lk)) {
1292 if (what & KA_NOTRECURSED)
1293 panic("Lock %s recursed @ %s:%d\n",
1294 lk->lock_object.lo_name, file, line);
1295 } else if (what & KA_RECURSED)
1296 panic("Lock %s not recursed @ %s:%d\n",
1297 lk->lock_object.lo_name, file, line);
1300 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1301 panic("Lock %s exclusively locked @ %s:%d\n",
1302 lk->lock_object.lo_name, file, line);
1305 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1313 lockmgr_chain(struct thread *td, struct thread **ownerp)
1319 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1321 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1322 if (lk->lk_lock & LK_SHARE)
1323 db_printf("SHARED (count %ju)\n",
1324 (uintmax_t)LK_SHARERS(lk->lk_lock));
1326 db_printf("EXCL\n");
1327 *ownerp = lockmgr_xholder(lk);
1333 db_show_lockmgr(struct lock_object *lock)
1338 lk = (struct lock *)lock;
1340 db_printf(" state: ");
1341 if (lk->lk_lock == LK_UNLOCKED)
1342 db_printf("UNLOCKED\n");
1343 else if (lk->lk_lock & LK_SHARE)
1344 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1346 td = lockmgr_xholder(lk);
1347 if (td == (struct thread *)LK_KERNPROC)
1348 db_printf("XLOCK: LK_KERNPROC\n");
1350 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1351 td->td_tid, td->td_proc->p_pid,
1352 td->td_proc->p_comm);
1353 if (lockmgr_recursed(lk))
1354 db_printf(" recursed: %d\n", lk->lk_recurse);
1356 db_printf(" waiters: ");
1357 switch (lk->lk_lock & LK_ALL_WAITERS) {
1358 case LK_SHARED_WAITERS:
1359 db_printf("shared\n");
1361 case LK_EXCLUSIVE_WAITERS:
1362 db_printf("exclusive\n");
1364 case LK_ALL_WAITERS:
1365 db_printf("shared and exclusive\n");
1368 db_printf("none\n");
1370 db_printf(" spinners: ");
1371 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1372 db_printf("exclusive\n");
1374 db_printf("none\n");