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_hwpmc_hooks.h"
32 #include "opt_kdtrace.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
41 #include <sys/lock_profile.h>
42 #include <sys/lockmgr.h>
43 #include <sys/mutex.h>
45 #include <sys/sleepqueue.h>
47 #include <sys/stack.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
52 #include <machine/cpu.h>
59 #include <sys/pmckern.h>
60 PMC_SOFT_DECLARE( , , lock, failed);
63 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
64 (LK_ADAPTIVE | LK_NOSHARE));
65 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
66 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
68 #define SQ_EXCLUSIVE_QUEUE 0
69 #define SQ_SHARED_QUEUE 1
72 #define _lockmgr_assert(lk, what, file, line)
73 #define TD_LOCKS_INC(td)
74 #define TD_LOCKS_DEC(td)
76 #define TD_LOCKS_INC(td) ((td)->td_locks++)
77 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
79 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
80 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
83 #define STACK_PRINT(lk)
84 #define STACK_SAVE(lk)
85 #define STACK_ZERO(lk)
87 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
88 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
89 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
92 #define LOCK_LOG2(lk, string, arg1, arg2) \
93 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
94 CTR2(KTR_LOCK, (string), (arg1), (arg2))
95 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
96 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
97 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
99 #define GIANT_DECLARE \
101 WITNESS_SAVE_DECL(Giant)
102 #define GIANT_RESTORE() do { \
106 WITNESS_RESTORE(&Giant.lock_object, Giant); \
109 #define GIANT_SAVE() do { \
110 if (mtx_owned(&Giant)) { \
111 WITNESS_SAVE(&Giant.lock_object, Giant); \
112 while (mtx_owned(&Giant)) { \
114 mtx_unlock(&Giant); \
119 #define LK_CAN_SHARE(x) \
120 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
121 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
122 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
123 #define LK_TRYOP(x) \
126 #define LK_CAN_WITNESS(x) \
127 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
128 #define LK_TRYWIT(x) \
129 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
131 #define LK_CAN_ADAPT(lk, f) \
132 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
133 ((f) & LK_SLEEPFAIL) == 0)
135 #define lockmgr_disowned(lk) \
136 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
138 #define lockmgr_xlocked(lk) \
139 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
141 static void assert_lockmgr(const struct lock_object *lock, int how);
143 static void db_show_lockmgr(const struct lock_object *lock);
145 static void lock_lockmgr(struct lock_object *lock, int how);
147 static int owner_lockmgr(const struct lock_object *lock,
148 struct thread **owner);
150 static int unlock_lockmgr(struct lock_object *lock);
152 struct lock_class lock_class_lockmgr = {
153 .lc_name = "lockmgr",
154 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
155 .lc_assert = assert_lockmgr,
157 .lc_ddb_show = db_show_lockmgr,
159 .lc_lock = lock_lockmgr,
160 .lc_unlock = unlock_lockmgr,
162 .lc_owner = owner_lockmgr,
166 #ifdef ADAPTIVE_LOCKMGRS
167 static u_int alk_retries = 10;
168 static u_int alk_loops = 10000;
169 static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL,
170 "lockmgr debugging");
171 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, "");
172 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, "");
175 static __inline struct thread *
176 lockmgr_xholder(const struct lock *lk)
181 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
185 * It assumes sleepq_lock held and returns with this one unheld.
186 * It also assumes the generic interlock is sane and previously checked.
187 * If LK_INTERLOCK is specified the interlock is not reacquired after the
191 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
192 const char *wmesg, int pri, int timo, int queue)
195 struct lock_class *class;
198 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
199 catch = pri & PCATCH;
203 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
204 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
206 if (flags & LK_INTERLOCK)
207 class->lc_unlock(ilk);
208 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
211 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
212 SLEEPQ_INTERRUPTIBLE : 0), queue);
213 if ((flags & LK_TIMELOCK) && timo)
214 sleepq_set_timeout(&lk->lock_object, timo);
217 * Decisional switch for real sleeping.
219 if ((flags & LK_TIMELOCK) && timo && catch)
220 error = sleepq_timedwait_sig(&lk->lock_object, pri);
221 else if ((flags & LK_TIMELOCK) && timo)
222 error = sleepq_timedwait(&lk->lock_object, pri);
224 error = sleepq_wait_sig(&lk->lock_object, pri);
226 sleepq_wait(&lk->lock_object, pri);
228 if ((flags & LK_SLEEPFAIL) && error == 0)
235 wakeupshlk(struct lock *lk, const char *file, int line)
239 int queue, wakeup_swapper;
241 TD_LOCKS_DEC(curthread);
242 TD_SLOCKS_DEC(curthread);
243 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
244 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
251 * If there is more than one shared lock held, just drop one
254 if (LK_SHARERS(x) > 1) {
255 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
262 * If there are not waiters on the exclusive queue, drop the
265 if ((x & LK_ALL_WAITERS) == 0) {
266 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
268 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
274 * We should have a sharer with waiters, so enter the hard
275 * path in order to handle wakeups correctly.
277 sleepq_lock(&lk->lock_object);
278 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
282 * If the lock has exclusive waiters, give them preference in
283 * order to avoid deadlock with shared runners up.
284 * If interruptible sleeps left the exclusive queue empty
285 * avoid a starvation for the threads sleeping on the shared
286 * queue by giving them precedence and cleaning up the
287 * exclusive waiters bit anyway.
288 * Please note that lk_exslpfail count may be lying about
289 * the real number of waiters with the LK_SLEEPFAIL flag on
290 * because they may be used in conjuction with interruptible
291 * sleeps so lk_exslpfail might be considered an 'upper limit'
292 * bound, including the edge cases.
294 realexslp = sleepq_sleepcnt(&lk->lock_object,
296 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
297 if (lk->lk_exslpfail < realexslp) {
298 lk->lk_exslpfail = 0;
299 queue = SQ_EXCLUSIVE_QUEUE;
300 v |= (x & LK_SHARED_WAITERS);
302 lk->lk_exslpfail = 0;
304 "%s: %p has only LK_SLEEPFAIL sleepers",
307 "%s: %p waking up threads on the exclusive queue",
310 sleepq_broadcast(&lk->lock_object,
311 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
312 queue = SQ_SHARED_QUEUE;
318 * Exclusive waiters sleeping with LK_SLEEPFAIL on
319 * and using interruptible sleeps/timeout may have
320 * left spourious lk_exslpfail counts on, so clean
323 lk->lk_exslpfail = 0;
324 queue = SQ_SHARED_QUEUE;
327 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
329 sleepq_release(&lk->lock_object);
332 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
333 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
335 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
337 sleepq_release(&lk->lock_object);
341 lock_profile_release_lock(&lk->lock_object);
342 return (wakeup_swapper);
346 assert_lockmgr(const struct lock_object *lock, int what)
349 panic("lockmgr locks do not support assertions");
353 lock_lockmgr(struct lock_object *lock, int how)
356 panic("lockmgr locks do not support sleep interlocking");
360 unlock_lockmgr(struct lock_object *lock)
363 panic("lockmgr locks do not support sleep interlocking");
368 owner_lockmgr(const struct lock_object *lock, struct thread **owner)
371 panic("lockmgr locks do not support owner inquiring");
376 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
380 MPASS((flags & ~LK_INIT_MASK) == 0);
381 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
382 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
385 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
386 if (flags & LK_CANRECURSE)
387 iflags |= LO_RECURSABLE;
388 if ((flags & LK_NODUP) == 0)
390 if (flags & LK_NOPROFILE)
391 iflags |= LO_NOPROFILE;
392 if ((flags & LK_NOWITNESS) == 0)
393 iflags |= LO_WITNESS;
394 if (flags & LK_QUIET)
396 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
398 lk->lk_lock = LK_UNLOCKED;
400 lk->lk_exslpfail = 0;
403 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
408 * XXX: Gross hacks to manipulate external lock flags after
409 * initialization. Used for certain vnode and buf locks.
412 lockallowshare(struct lock *lk)
415 lockmgr_assert(lk, KA_XLOCKED);
416 lk->lock_object.lo_flags &= ~LK_NOSHARE;
420 lockallowrecurse(struct lock *lk)
423 lockmgr_assert(lk, KA_XLOCKED);
424 lk->lock_object.lo_flags |= LO_RECURSABLE;
428 lockdisablerecurse(struct lock *lk)
431 lockmgr_assert(lk, KA_XLOCKED);
432 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
436 lockdestroy(struct lock *lk)
439 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
440 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
441 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
442 lock_destroy(&lk->lock_object);
446 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
447 const char *wmesg, int pri, int timo, const char *file, int line)
450 struct lock_class *class;
454 int error, ipri, itimo, queue, wakeup_swapper;
455 #ifdef LOCK_PROFILING
456 uint64_t waittime = 0;
459 #ifdef ADAPTIVE_LOCKMGRS
460 volatile struct thread *owner;
461 u_int i, spintries = 0;
465 tid = (uintptr_t)curthread;
466 op = (flags & LK_TYPE_MASK);
467 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
468 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
469 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
471 MPASS((flags & ~LK_TOTAL_MASK) == 0);
472 KASSERT((op & (op - 1)) == 0,
473 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
474 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
475 (op != LK_DOWNGRADE && op != LK_RELEASE),
476 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
477 __func__, file, line));
478 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
479 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
480 __func__, file, line));
481 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
482 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
483 lk->lock_object.lo_name, file, line));
485 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
486 if (panicstr != NULL) {
487 if (flags & LK_INTERLOCK)
488 class->lc_unlock(ilk);
492 if (lk->lock_object.lo_flags & LK_NOSHARE) {
499 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
501 if (flags & LK_INTERLOCK)
502 class->lc_unlock(ilk);
510 if (LK_CAN_WITNESS(flags))
511 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
517 * If no other thread has an exclusive lock, or
518 * no exclusive waiter is present, bump the count of
519 * sharers. Since we have to preserve the state of
520 * waiters, if we fail to acquire the shared lock
521 * loop back and retry.
523 if (LK_CAN_SHARE(x)) {
524 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
530 PMC_SOFT_CALL( , , lock, failed);
532 lock_profile_obtain_lock_failed(&lk->lock_object,
533 &contested, &waittime);
536 * If the lock is already held by curthread in
537 * exclusive way avoid a deadlock.
539 if (LK_HOLDER(x) == tid) {
541 "%s: %p already held in exclusive mode",
548 * If the lock is expected to not sleep just give up
551 if (LK_TRYOP(flags)) {
552 LOCK_LOG2(lk, "%s: %p fails the try operation",
558 #ifdef ADAPTIVE_LOCKMGRS
560 * If the owner is running on another CPU, spin until
561 * the owner stops running or the state of the lock
562 * changes. We need a double-state handle here
563 * because for a failed acquisition the lock can be
564 * either held in exclusive mode or shared mode
565 * (for the writer starvation avoidance technique).
567 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
568 LK_HOLDER(x) != LK_KERNPROC) {
569 owner = (struct thread *)LK_HOLDER(x);
570 if (LOCK_LOG_TEST(&lk->lock_object, 0))
572 "%s: spinning on %p held by %p",
573 __func__, lk, owner);
576 * If we are holding also an interlock drop it
577 * in order to avoid a deadlock if the lockmgr
578 * owner is adaptively spinning on the
581 if (flags & LK_INTERLOCK) {
582 class->lc_unlock(ilk);
583 flags &= ~LK_INTERLOCK;
586 while (LK_HOLDER(lk->lk_lock) ==
587 (uintptr_t)owner && TD_IS_RUNNING(owner))
591 } else if (LK_CAN_ADAPT(lk, flags) &&
592 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
593 spintries < alk_retries) {
594 if (flags & LK_INTERLOCK) {
595 class->lc_unlock(ilk);
596 flags &= ~LK_INTERLOCK;
600 for (i = 0; i < alk_loops; i++) {
601 if (LOCK_LOG_TEST(&lk->lock_object, 0))
603 "%s: shared spinning on %p with %u and %u",
604 __func__, lk, spintries, i);
606 if ((x & LK_SHARE) == 0 ||
607 LK_CAN_SHARE(x) != 0)
618 * Acquire the sleepqueue chain lock because we
619 * probabilly will need to manipulate waiters flags.
621 sleepq_lock(&lk->lock_object);
625 * if the lock can be acquired in shared mode, try
628 if (LK_CAN_SHARE(x)) {
629 sleepq_release(&lk->lock_object);
633 #ifdef ADAPTIVE_LOCKMGRS
635 * The current lock owner might have started executing
636 * on another CPU (or the lock could have changed
637 * owner) while we were waiting on the turnstile
638 * chain lock. If so, drop the turnstile lock and try
641 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
642 LK_HOLDER(x) != LK_KERNPROC) {
643 owner = (struct thread *)LK_HOLDER(x);
644 if (TD_IS_RUNNING(owner)) {
645 sleepq_release(&lk->lock_object);
652 * Try to set the LK_SHARED_WAITERS flag. If we fail,
653 * loop back and retry.
655 if ((x & LK_SHARED_WAITERS) == 0) {
656 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
657 x | LK_SHARED_WAITERS)) {
658 sleepq_release(&lk->lock_object);
661 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
666 * As far as we have been unable to acquire the
667 * shared lock and the shared waiters flag is set,
670 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
672 flags &= ~LK_INTERLOCK;
675 "%s: interrupted sleep for %p with %d",
676 __func__, lk, error);
679 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
683 lock_profile_obtain_lock_success(&lk->lock_object,
684 contested, waittime, file, line);
685 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
687 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
689 TD_LOCKS_INC(curthread);
690 TD_SLOCKS_INC(curthread);
695 _lockmgr_assert(lk, KA_SLOCKED, file, line);
697 x = v & LK_ALL_WAITERS;
698 v &= LK_EXCLUSIVE_SPINNERS;
701 * Try to switch from one shared lock to an exclusive one.
702 * We need to preserve waiters flags during the operation.
704 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
706 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
708 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
709 LK_TRYWIT(flags), file, line);
710 TD_SLOCKS_DEC(curthread);
715 * We have been unable to succeed in upgrading, so just
716 * give up the shared lock.
718 wakeup_swapper |= wakeupshlk(lk, file, line);
722 if (LK_CAN_WITNESS(flags))
723 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
724 LOP_EXCLUSIVE, file, line, ilk);
727 * If curthread already holds the lock and this one is
728 * allowed to recurse, simply recurse on it.
730 if (lockmgr_xlocked(lk)) {
731 if ((flags & LK_CANRECURSE) == 0 &&
732 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
735 * If the lock is expected to not panic just
736 * give up and return.
738 if (LK_TRYOP(flags)) {
740 "%s: %p fails the try operation",
745 if (flags & LK_INTERLOCK)
746 class->lc_unlock(ilk);
747 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
748 __func__, iwmesg, file, line);
751 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
752 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
753 lk->lk_recurse, file, line);
754 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
755 LK_TRYWIT(flags), file, line);
756 TD_LOCKS_INC(curthread);
760 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
763 PMC_SOFT_CALL( , , lock, failed);
765 lock_profile_obtain_lock_failed(&lk->lock_object,
766 &contested, &waittime);
769 * If the lock is expected to not sleep just give up
772 if (LK_TRYOP(flags)) {
773 LOCK_LOG2(lk, "%s: %p fails the try operation",
779 #ifdef ADAPTIVE_LOCKMGRS
781 * If the owner is running on another CPU, spin until
782 * the owner stops running or the state of the lock
786 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
787 LK_HOLDER(x) != LK_KERNPROC) {
788 owner = (struct thread *)LK_HOLDER(x);
789 if (LOCK_LOG_TEST(&lk->lock_object, 0))
791 "%s: spinning on %p held by %p",
792 __func__, lk, owner);
795 * If we are holding also an interlock drop it
796 * in order to avoid a deadlock if the lockmgr
797 * owner is adaptively spinning on the
800 if (flags & LK_INTERLOCK) {
801 class->lc_unlock(ilk);
802 flags &= ~LK_INTERLOCK;
805 while (LK_HOLDER(lk->lk_lock) ==
806 (uintptr_t)owner && TD_IS_RUNNING(owner))
810 } else if (LK_CAN_ADAPT(lk, flags) &&
811 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
812 spintries < alk_retries) {
813 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
814 !atomic_cmpset_ptr(&lk->lk_lock, x,
815 x | LK_EXCLUSIVE_SPINNERS))
817 if (flags & LK_INTERLOCK) {
818 class->lc_unlock(ilk);
819 flags &= ~LK_INTERLOCK;
823 for (i = 0; i < alk_loops; i++) {
824 if (LOCK_LOG_TEST(&lk->lock_object, 0))
826 "%s: shared spinning on %p with %u and %u",
827 __func__, lk, spintries, i);
829 LK_EXCLUSIVE_SPINNERS) == 0)
840 * Acquire the sleepqueue chain lock because we
841 * probabilly will need to manipulate waiters flags.
843 sleepq_lock(&lk->lock_object);
847 * if the lock has been released while we spun on
848 * the sleepqueue chain lock just try again.
850 if (x == LK_UNLOCKED) {
851 sleepq_release(&lk->lock_object);
855 #ifdef ADAPTIVE_LOCKMGRS
857 * The current lock owner might have started executing
858 * on another CPU (or the lock could have changed
859 * owner) while we were waiting on the turnstile
860 * chain lock. If so, drop the turnstile lock and try
863 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
864 LK_HOLDER(x) != LK_KERNPROC) {
865 owner = (struct thread *)LK_HOLDER(x);
866 if (TD_IS_RUNNING(owner)) {
867 sleepq_release(&lk->lock_object);
874 * The lock can be in the state where there is a
875 * pending queue of waiters, but still no owner.
876 * This happens when the lock is contested and an
877 * owner is going to claim the lock.
878 * If curthread is the one successfully acquiring it
879 * claim lock ownership and return, preserving waiters
882 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
883 if ((x & ~v) == LK_UNLOCKED) {
884 v &= ~LK_EXCLUSIVE_SPINNERS;
885 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
887 sleepq_release(&lk->lock_object);
889 "%s: %p claimed by a new writer",
893 sleepq_release(&lk->lock_object);
898 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
899 * fail, loop back and retry.
901 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
902 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
903 x | LK_EXCLUSIVE_WAITERS)) {
904 sleepq_release(&lk->lock_object);
907 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
912 * As far as we have been unable to acquire the
913 * exclusive lock and the exclusive waiters flag
914 * is set, we will sleep.
916 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
918 flags &= ~LK_INTERLOCK;
921 "%s: interrupted sleep for %p with %d",
922 __func__, lk, error);
925 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
929 lock_profile_obtain_lock_success(&lk->lock_object,
930 contested, waittime, file, line);
931 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
932 lk->lk_recurse, file, line);
933 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
934 LK_TRYWIT(flags), file, line);
935 TD_LOCKS_INC(curthread);
940 _lockmgr_assert(lk, KA_XLOCKED, file, line);
941 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
942 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
945 * Panic if the lock is recursed.
947 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
948 if (flags & LK_INTERLOCK)
949 class->lc_unlock(ilk);
950 panic("%s: downgrade a recursed lockmgr %s @ %s:%d\n",
951 __func__, iwmesg, file, line);
953 TD_SLOCKS_INC(curthread);
956 * In order to preserve waiters flags, just spin.
960 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
962 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
963 LK_SHARERS_LOCK(1) | x))
969 _lockmgr_assert(lk, KA_LOCKED, file, line);
972 if ((x & LK_SHARE) == 0) {
975 * As first option, treact the lock as if it has not
977 * Fix-up the tid var if the lock has been disowned.
979 if (LK_HOLDER(x) == LK_KERNPROC)
982 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
984 TD_LOCKS_DEC(curthread);
986 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
987 lk->lk_recurse, file, line);
990 * The lock is held in exclusive mode.
991 * If the lock is recursed also, then unrecurse it.
993 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
994 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
999 if (tid != LK_KERNPROC)
1000 lock_profile_release_lock(&lk->lock_object);
1002 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
1006 sleepq_lock(&lk->lock_object);
1011 * If the lock has exclusive waiters, give them
1012 * preference in order to avoid deadlock with
1013 * shared runners up.
1014 * If interruptible sleeps left the exclusive queue
1015 * empty avoid a starvation for the threads sleeping
1016 * on the shared queue by giving them precedence
1017 * and cleaning up the exclusive waiters bit anyway.
1018 * Please note that lk_exslpfail count may be lying
1019 * about the real number of waiters with the
1020 * LK_SLEEPFAIL flag on because they may be used in
1021 * conjuction with interruptible sleeps so
1022 * lk_exslpfail might be considered an 'upper limit'
1023 * bound, including the edge cases.
1025 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1026 realexslp = sleepq_sleepcnt(&lk->lock_object,
1027 SQ_EXCLUSIVE_QUEUE);
1028 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1029 if (lk->lk_exslpfail < realexslp) {
1030 lk->lk_exslpfail = 0;
1031 queue = SQ_EXCLUSIVE_QUEUE;
1032 v |= (x & LK_SHARED_WAITERS);
1034 lk->lk_exslpfail = 0;
1036 "%s: %p has only LK_SLEEPFAIL sleepers",
1039 "%s: %p waking up threads on the exclusive queue",
1042 sleepq_broadcast(&lk->lock_object,
1043 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1044 queue = SQ_SHARED_QUEUE;
1049 * Exclusive waiters sleeping with LK_SLEEPFAIL
1050 * on and using interruptible sleeps/timeout
1051 * may have left spourious lk_exslpfail counts
1052 * on, so clean it up anyway.
1054 lk->lk_exslpfail = 0;
1055 queue = SQ_SHARED_QUEUE;
1059 "%s: %p waking up threads on the %s queue",
1060 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1062 atomic_store_rel_ptr(&lk->lk_lock, v);
1063 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1064 SLEEPQ_LK, 0, queue);
1065 sleepq_release(&lk->lock_object);
1068 wakeup_swapper = wakeupshlk(lk, file, line);
1071 if (LK_CAN_WITNESS(flags))
1072 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1073 LOP_EXCLUSIVE, file, line, ilk);
1076 * Trying to drain a lock we already own will result in a
1079 if (lockmgr_xlocked(lk)) {
1080 if (flags & LK_INTERLOCK)
1081 class->lc_unlock(ilk);
1082 panic("%s: draining %s with the lock held @ %s:%d\n",
1083 __func__, iwmesg, file, line);
1086 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1088 PMC_SOFT_CALL( , , lock, failed);
1090 lock_profile_obtain_lock_failed(&lk->lock_object,
1091 &contested, &waittime);
1094 * If the lock is expected to not sleep just give up
1097 if (LK_TRYOP(flags)) {
1098 LOCK_LOG2(lk, "%s: %p fails the try operation",
1105 * Acquire the sleepqueue chain lock because we
1106 * probabilly will need to manipulate waiters flags.
1108 sleepq_lock(&lk->lock_object);
1112 * if the lock has been released while we spun on
1113 * the sleepqueue chain lock just try again.
1115 if (x == LK_UNLOCKED) {
1116 sleepq_release(&lk->lock_object);
1120 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1121 if ((x & ~v) == LK_UNLOCKED) {
1122 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1125 * If interruptible sleeps left the exclusive
1126 * queue empty avoid a starvation for the
1127 * threads sleeping on the shared queue by
1128 * giving them precedence and cleaning up the
1129 * exclusive waiters bit anyway.
1130 * Please note that lk_exslpfail count may be
1131 * lying about the real number of waiters with
1132 * the LK_SLEEPFAIL flag on because they may
1133 * be used in conjuction with interruptible
1134 * sleeps so lk_exslpfail might be considered
1135 * an 'upper limit' bound, including the edge
1138 if (v & LK_EXCLUSIVE_WAITERS) {
1139 queue = SQ_EXCLUSIVE_QUEUE;
1140 v &= ~LK_EXCLUSIVE_WAITERS;
1144 * Exclusive waiters sleeping with
1145 * LK_SLEEPFAIL on and using
1146 * interruptible sleeps/timeout may
1147 * have left spourious lk_exslpfail
1148 * counts on, so clean it up anyway.
1150 MPASS(v & LK_SHARED_WAITERS);
1151 lk->lk_exslpfail = 0;
1152 queue = SQ_SHARED_QUEUE;
1153 v &= ~LK_SHARED_WAITERS;
1155 if (queue == SQ_EXCLUSIVE_QUEUE) {
1157 sleepq_sleepcnt(&lk->lock_object,
1158 SQ_EXCLUSIVE_QUEUE);
1159 if (lk->lk_exslpfail >= realexslp) {
1160 lk->lk_exslpfail = 0;
1161 queue = SQ_SHARED_QUEUE;
1162 v &= ~LK_SHARED_WAITERS;
1163 if (realexslp != 0) {
1165 "%s: %p has only LK_SLEEPFAIL sleepers",
1168 "%s: %p waking up threads on the exclusive queue",
1174 SQ_EXCLUSIVE_QUEUE);
1177 lk->lk_exslpfail = 0;
1179 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1180 sleepq_release(&lk->lock_object);
1184 "%s: %p waking up all threads on the %s queue",
1185 __func__, lk, queue == SQ_SHARED_QUEUE ?
1186 "shared" : "exclusive");
1187 wakeup_swapper |= sleepq_broadcast(
1188 &lk->lock_object, SLEEPQ_LK, 0, queue);
1191 * If shared waiters have been woken up we need
1192 * to wait for one of them to acquire the lock
1193 * before to set the exclusive waiters in
1194 * order to avoid a deadlock.
1196 if (queue == SQ_SHARED_QUEUE) {
1197 for (v = lk->lk_lock;
1198 (v & LK_SHARE) && !LK_SHARERS(v);
1205 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1206 * fail, loop back and retry.
1208 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1209 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1210 x | LK_EXCLUSIVE_WAITERS)) {
1211 sleepq_release(&lk->lock_object);
1214 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1219 * As far as we have been unable to acquire the
1220 * exclusive lock and the exclusive waiters flag
1221 * is set, we will sleep.
1223 if (flags & LK_INTERLOCK) {
1224 class->lc_unlock(ilk);
1225 flags &= ~LK_INTERLOCK;
1228 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1229 SQ_EXCLUSIVE_QUEUE);
1230 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1232 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1237 lock_profile_obtain_lock_success(&lk->lock_object,
1238 contested, waittime, file, line);
1239 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1240 lk->lk_recurse, file, line);
1241 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1242 LK_TRYWIT(flags), file, line);
1243 TD_LOCKS_INC(curthread);
1248 if (flags & LK_INTERLOCK)
1249 class->lc_unlock(ilk);
1250 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1253 if (flags & LK_INTERLOCK)
1254 class->lc_unlock(ilk);
1262 _lockmgr_disown(struct lock *lk, const char *file, int line)
1266 if (SCHEDULER_STOPPED())
1269 tid = (uintptr_t)curthread;
1270 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1273 * Panic if the lock is recursed.
1275 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk))
1276 panic("%s: disown a recursed lockmgr @ %s:%d\n",
1277 __func__, file, line);
1280 * If the owner is already LK_KERNPROC just skip the whole operation.
1282 if (LK_HOLDER(lk->lk_lock) != tid)
1284 lock_profile_release_lock(&lk->lock_object);
1285 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1286 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1287 TD_LOCKS_DEC(curthread);
1291 * In order to preserve waiters flags, just spin.
1295 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1296 x &= LK_ALL_WAITERS;
1297 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1305 lockmgr_printinfo(const struct lock *lk)
1310 if (lk->lk_lock == LK_UNLOCKED)
1311 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1312 else if (lk->lk_lock & LK_SHARE)
1313 printf("lock type %s: SHARED (count %ju)\n",
1314 lk->lock_object.lo_name,
1315 (uintmax_t)LK_SHARERS(lk->lk_lock));
1317 td = lockmgr_xholder(lk);
1318 printf("lock type %s: EXCL by thread %p "
1319 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name, td,
1320 td->td_proc->p_pid, td->td_proc->p_comm, td->td_tid);
1324 if (x & LK_EXCLUSIVE_WAITERS)
1325 printf(" with exclusive waiters pending\n");
1326 if (x & LK_SHARED_WAITERS)
1327 printf(" with shared waiters pending\n");
1328 if (x & LK_EXCLUSIVE_SPINNERS)
1329 printf(" with exclusive spinners pending\n");
1335 lockstatus(const struct lock *lk)
1344 if ((x & LK_SHARE) == 0) {
1345 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1349 } else if (x == LK_UNLOCKED)
1355 #ifdef INVARIANT_SUPPORT
1357 FEATURE(invariant_support,
1358 "Support for modules compiled with INVARIANTS option");
1361 #undef _lockmgr_assert
1365 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1369 if (panicstr != NULL)
1373 case KA_SLOCKED | KA_NOTRECURSED:
1374 case KA_SLOCKED | KA_RECURSED:
1377 case KA_LOCKED | KA_NOTRECURSED:
1378 case KA_LOCKED | KA_RECURSED:
1382 * We cannot trust WITNESS if the lock is held in exclusive
1383 * mode and a call to lockmgr_disown() happened.
1384 * Workaround this skipping the check if the lock is held in
1385 * exclusive mode even for the KA_LOCKED case.
1387 if (slocked || (lk->lk_lock & LK_SHARE)) {
1388 witness_assert(&lk->lock_object, what, file, line);
1392 if (lk->lk_lock == LK_UNLOCKED ||
1393 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1394 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1395 panic("Lock %s not %slocked @ %s:%d\n",
1396 lk->lock_object.lo_name, slocked ? "share" : "",
1399 if ((lk->lk_lock & LK_SHARE) == 0) {
1400 if (lockmgr_recursed(lk)) {
1401 if (what & KA_NOTRECURSED)
1402 panic("Lock %s recursed @ %s:%d\n",
1403 lk->lock_object.lo_name, file,
1405 } else if (what & KA_RECURSED)
1406 panic("Lock %s not recursed @ %s:%d\n",
1407 lk->lock_object.lo_name, file, line);
1411 case KA_XLOCKED | KA_NOTRECURSED:
1412 case KA_XLOCKED | KA_RECURSED:
1413 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1414 panic("Lock %s not exclusively locked @ %s:%d\n",
1415 lk->lock_object.lo_name, file, line);
1416 if (lockmgr_recursed(lk)) {
1417 if (what & KA_NOTRECURSED)
1418 panic("Lock %s recursed @ %s:%d\n",
1419 lk->lock_object.lo_name, file, line);
1420 } else if (what & KA_RECURSED)
1421 panic("Lock %s not recursed @ %s:%d\n",
1422 lk->lock_object.lo_name, file, line);
1425 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1426 panic("Lock %s exclusively locked @ %s:%d\n",
1427 lk->lock_object.lo_name, file, line);
1430 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1438 lockmgr_chain(struct thread *td, struct thread **ownerp)
1444 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1446 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1447 if (lk->lk_lock & LK_SHARE)
1448 db_printf("SHARED (count %ju)\n",
1449 (uintmax_t)LK_SHARERS(lk->lk_lock));
1451 db_printf("EXCL\n");
1452 *ownerp = lockmgr_xholder(lk);
1458 db_show_lockmgr(const struct lock_object *lock)
1461 const struct lock *lk;
1463 lk = (const struct lock *)lock;
1465 db_printf(" state: ");
1466 if (lk->lk_lock == LK_UNLOCKED)
1467 db_printf("UNLOCKED\n");
1468 else if (lk->lk_lock & LK_SHARE)
1469 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1471 td = lockmgr_xholder(lk);
1472 if (td == (struct thread *)LK_KERNPROC)
1473 db_printf("XLOCK: LK_KERNPROC\n");
1475 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1476 td->td_tid, td->td_proc->p_pid,
1477 td->td_proc->p_comm);
1478 if (lockmgr_recursed(lk))
1479 db_printf(" recursed: %d\n", lk->lk_recurse);
1481 db_printf(" waiters: ");
1482 switch (lk->lk_lock & LK_ALL_WAITERS) {
1483 case LK_SHARED_WAITERS:
1484 db_printf("shared\n");
1486 case LK_EXCLUSIVE_WAITERS:
1487 db_printf("exclusive\n");
1489 case LK_ALL_WAITERS:
1490 db_printf("shared and exclusive\n");
1493 db_printf("none\n");
1495 db_printf(" spinners: ");
1496 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1497 db_printf("exclusive\n");
1499 db_printf("none\n");