2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice(s), this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified other than the possible
13 * addition of one or more copyright notices.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice(s), this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 #include "opt_hwpmc_hooks.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)
75 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
76 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
79 #define STACK_PRINT(lk)
80 #define STACK_SAVE(lk)
81 #define STACK_ZERO(lk)
83 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
84 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
85 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
88 #define LOCK_LOG2(lk, string, arg1, arg2) \
89 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
90 CTR2(KTR_LOCK, (string), (arg1), (arg2))
91 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
92 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
93 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
95 #define GIANT_DECLARE \
97 WITNESS_SAVE_DECL(Giant)
98 #define GIANT_RESTORE() do { \
102 WITNESS_RESTORE(&Giant.lock_object, Giant); \
105 #define GIANT_SAVE() do { \
106 if (mtx_owned(&Giant)) { \
107 WITNESS_SAVE(&Giant.lock_object, Giant); \
108 while (mtx_owned(&Giant)) { \
110 mtx_unlock(&Giant); \
115 #define LK_CAN_SHARE(x, flags) \
116 (((x) & LK_SHARE) && \
117 (((x) & (LK_EXCLUSIVE_WAITERS | LK_EXCLUSIVE_SPINNERS)) == 0 || \
118 (curthread->td_lk_slocks != 0 && !(flags & LK_NODDLKTREAT)) || \
119 (curthread->td_pflags & TDP_DEADLKTREAT)))
120 #define LK_TRYOP(x) \
123 #define LK_CAN_WITNESS(x) \
124 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
125 #define LK_TRYWIT(x) \
126 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
128 #define LK_CAN_ADAPT(lk, f) \
129 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
130 ((f) & LK_SLEEPFAIL) == 0)
132 #define lockmgr_disowned(lk) \
133 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
135 #define lockmgr_xlocked(lk) \
136 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
138 static void assert_lockmgr(const struct lock_object *lock, int how);
140 static void db_show_lockmgr(const struct lock_object *lock);
142 static void lock_lockmgr(struct lock_object *lock, uintptr_t how);
144 static int owner_lockmgr(const struct lock_object *lock,
145 struct thread **owner);
147 static uintptr_t unlock_lockmgr(struct lock_object *lock);
149 struct lock_class lock_class_lockmgr = {
150 .lc_name = "lockmgr",
151 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
152 .lc_assert = assert_lockmgr,
154 .lc_ddb_show = db_show_lockmgr,
156 .lc_lock = lock_lockmgr,
157 .lc_unlock = unlock_lockmgr,
159 .lc_owner = owner_lockmgr,
163 struct lockmgr_wait {
169 static bool __always_inline lockmgr_slock_try(struct lock *lk, uintptr_t *xp,
171 static bool __always_inline lockmgr_sunlock_try(struct lock *lk, uintptr_t *xp);
174 lockmgr_exit(u_int flags, struct lock_object *ilk, int wakeup_swapper)
176 struct lock_class *class;
178 if (flags & LK_INTERLOCK) {
179 class = LOCK_CLASS(ilk);
180 class->lc_unlock(ilk);
183 if (__predict_false(wakeup_swapper))
188 lockmgr_note_shared_acquire(struct lock *lk, int contested,
189 uint64_t waittime, const char *file, int line, int flags)
192 lock_profile_obtain_lock_success(&lk->lock_object, contested, waittime,
194 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file, line);
195 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file, line);
196 TD_LOCKS_INC(curthread);
197 TD_SLOCKS_INC(curthread);
202 lockmgr_note_shared_release(struct lock *lk, const char *file, int line)
205 lock_profile_release_lock(&lk->lock_object);
206 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
207 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
208 TD_LOCKS_DEC(curthread);
209 TD_SLOCKS_DEC(curthread);
213 lockmgr_note_exclusive_acquire(struct lock *lk, int contested,
214 uint64_t waittime, const char *file, int line, int flags)
217 lock_profile_obtain_lock_success(&lk->lock_object, contested, waittime,
219 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
220 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE | LK_TRYWIT(flags), file,
222 TD_LOCKS_INC(curthread);
227 lockmgr_note_exclusive_release(struct lock *lk, const char *file, int line)
230 lock_profile_release_lock(&lk->lock_object);
231 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0, lk->lk_recurse, file,
233 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
234 TD_LOCKS_DEC(curthread);
237 static __inline struct thread *
238 lockmgr_xholder(const struct lock *lk)
243 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
247 * It assumes sleepq_lock held and returns with this one unheld.
248 * It also assumes the generic interlock is sane and previously checked.
249 * If LK_INTERLOCK is specified the interlock is not reacquired after the
253 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
254 const char *wmesg, int pri, int timo, int queue)
257 struct lock_class *class;
260 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
261 catch = pri & PCATCH;
265 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
266 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
268 if (flags & LK_INTERLOCK)
269 class->lc_unlock(ilk);
270 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
273 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
274 SLEEPQ_INTERRUPTIBLE : 0), queue);
275 if ((flags & LK_TIMELOCK) && timo)
276 sleepq_set_timeout(&lk->lock_object, timo);
279 * Decisional switch for real sleeping.
281 if ((flags & LK_TIMELOCK) && timo && catch)
282 error = sleepq_timedwait_sig(&lk->lock_object, pri);
283 else if ((flags & LK_TIMELOCK) && timo)
284 error = sleepq_timedwait(&lk->lock_object, pri);
286 error = sleepq_wait_sig(&lk->lock_object, pri);
288 sleepq_wait(&lk->lock_object, pri);
290 if ((flags & LK_SLEEPFAIL) && error == 0)
297 wakeupshlk(struct lock *lk, const char *file, int line)
299 uintptr_t v, x, orig_x;
301 int queue, wakeup_swapper;
306 if (lockmgr_sunlock_try(lk, &x))
310 * We should have a sharer with waiters, so enter the hard
311 * path in order to handle wakeups correctly.
313 sleepq_lock(&lk->lock_object);
314 orig_x = lk->lk_lock;
316 x = orig_x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
320 * If the lock has exclusive waiters, give them preference in
321 * order to avoid deadlock with shared runners up.
322 * If interruptible sleeps left the exclusive queue empty
323 * avoid a starvation for the threads sleeping on the shared
324 * queue by giving them precedence and cleaning up the
325 * exclusive waiters bit anyway.
326 * Please note that lk_exslpfail count may be lying about
327 * the real number of waiters with the LK_SLEEPFAIL flag on
328 * because they may be used in conjunction with interruptible
329 * sleeps so lk_exslpfail might be considered an 'upper limit'
330 * bound, including the edge cases.
332 realexslp = sleepq_sleepcnt(&lk->lock_object,
334 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
335 if (lk->lk_exslpfail < realexslp) {
336 lk->lk_exslpfail = 0;
337 queue = SQ_EXCLUSIVE_QUEUE;
338 v |= (x & LK_SHARED_WAITERS);
340 lk->lk_exslpfail = 0;
342 "%s: %p has only LK_SLEEPFAIL sleepers",
345 "%s: %p waking up threads on the exclusive queue",
348 sleepq_broadcast(&lk->lock_object,
349 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
350 queue = SQ_SHARED_QUEUE;
356 * Exclusive waiters sleeping with LK_SLEEPFAIL on
357 * and using interruptible sleeps/timeout may have
358 * left spourious lk_exslpfail counts on, so clean
361 lk->lk_exslpfail = 0;
362 queue = SQ_SHARED_QUEUE;
365 if (lockmgr_sunlock_try(lk, &orig_x)) {
366 sleepq_release(&lk->lock_object);
370 x |= LK_SHARERS_LOCK(1);
371 if (!atomic_fcmpset_rel_ptr(&lk->lk_lock, &x, v)) {
375 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
376 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
378 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
380 sleepq_release(&lk->lock_object);
384 lockmgr_note_shared_release(lk, file, line);
385 return (wakeup_swapper);
389 assert_lockmgr(const struct lock_object *lock, int what)
392 panic("lockmgr locks do not support assertions");
396 lock_lockmgr(struct lock_object *lock, uintptr_t how)
399 panic("lockmgr locks do not support sleep interlocking");
403 unlock_lockmgr(struct lock_object *lock)
406 panic("lockmgr locks do not support sleep interlocking");
411 owner_lockmgr(const struct lock_object *lock, struct thread **owner)
414 panic("lockmgr locks do not support owner inquiring");
419 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
423 MPASS((flags & ~LK_INIT_MASK) == 0);
424 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
425 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
428 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
429 if (flags & LK_CANRECURSE)
430 iflags |= LO_RECURSABLE;
431 if ((flags & LK_NODUP) == 0)
433 if (flags & LK_NOPROFILE)
434 iflags |= LO_NOPROFILE;
435 if ((flags & LK_NOWITNESS) == 0)
436 iflags |= LO_WITNESS;
437 if (flags & LK_QUIET)
439 if (flags & LK_IS_VNODE)
440 iflags |= LO_IS_VNODE;
441 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
443 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
444 lk->lk_lock = LK_UNLOCKED;
446 lk->lk_exslpfail = 0;
453 * XXX: Gross hacks to manipulate external lock flags after
454 * initialization. Used for certain vnode and buf locks.
457 lockallowshare(struct lock *lk)
460 lockmgr_assert(lk, KA_XLOCKED);
461 lk->lock_object.lo_flags &= ~LK_NOSHARE;
465 lockdisableshare(struct lock *lk)
468 lockmgr_assert(lk, KA_XLOCKED);
469 lk->lock_object.lo_flags |= LK_NOSHARE;
473 lockallowrecurse(struct lock *lk)
476 lockmgr_assert(lk, KA_XLOCKED);
477 lk->lock_object.lo_flags |= LO_RECURSABLE;
481 lockdisablerecurse(struct lock *lk)
484 lockmgr_assert(lk, KA_XLOCKED);
485 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
489 lockdestroy(struct lock *lk)
492 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
493 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
494 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
495 lock_destroy(&lk->lock_object);
498 static bool __always_inline
499 lockmgr_slock_try(struct lock *lk, uintptr_t *xp, int flags)
503 * If no other thread has an exclusive lock, or
504 * no exclusive waiter is present, bump the count of
505 * sharers. Since we have to preserve the state of
506 * waiters, if we fail to acquire the shared lock
507 * loop back and retry.
510 while (LK_CAN_SHARE(*xp, flags)) {
511 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, xp,
512 *xp + LK_ONE_SHARER)) {
519 static bool __always_inline
520 lockmgr_sunlock_try(struct lock *lk, uintptr_t *xp)
525 * If there is more than one shared lock held, just drop one
528 if (LK_SHARERS(*xp) > 1) {
529 if (atomic_fcmpset_rel_ptr(&lk->lk_lock, xp,
530 *xp - LK_ONE_SHARER))
536 * If there are not waiters on the exclusive queue, drop the
539 if ((*xp & LK_ALL_WAITERS) == 0) {
540 MPASS((*xp & ~LK_EXCLUSIVE_SPINNERS) ==
542 if (atomic_fcmpset_rel_ptr(&lk->lk_lock, xp,
552 static __noinline int
553 lockmgr_slock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
554 const char *file, int line, struct lockmgr_wait *lwa)
561 #ifdef LOCK_PROFILING
562 uint64_t waittime = 0;
566 if (__predict_false(panicstr != NULL))
569 tid = (uintptr_t)curthread;
571 if (LK_CAN_WITNESS(flags))
572 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
573 file, line, flags & LK_INTERLOCK ? ilk : NULL);
575 if (lockmgr_slock_try(lk, &x, flags))
578 PMC_SOFT_CALL( , , lock, failed);
580 lock_profile_obtain_lock_failed(&lk->lock_object,
581 &contested, &waittime);
584 * If the lock is already held by curthread in
585 * exclusive way avoid a deadlock.
587 if (LK_HOLDER(x) == tid) {
589 "%s: %p already held in exclusive mode",
596 * If the lock is expected to not sleep just give up
599 if (LK_TRYOP(flags)) {
600 LOCK_LOG2(lk, "%s: %p fails the try operation",
607 * Acquire the sleepqueue chain lock because we
608 * probabilly will need to manipulate waiters flags.
610 sleepq_lock(&lk->lock_object);
615 * if the lock can be acquired in shared mode, try
618 if (LK_CAN_SHARE(x, flags)) {
619 sleepq_release(&lk->lock_object);
624 * Try to set the LK_SHARED_WAITERS flag. If we fail,
625 * loop back and retry.
627 if ((x & LK_SHARED_WAITERS) == 0) {
628 if (!atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
629 x | LK_SHARED_WAITERS)) {
632 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
637 iwmesg = lk->lock_object.lo_name;
641 iwmesg = lwa->iwmesg;
647 * As far as we have been unable to acquire the
648 * shared lock and the shared waiters flag is set,
651 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
653 flags &= ~LK_INTERLOCK;
656 "%s: interrupted sleep for %p with %d",
657 __func__, lk, error);
660 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
664 #ifdef LOCK_PROFILING
665 lockmgr_note_shared_acquire(lk, contested, waittime,
668 lockmgr_note_shared_acquire(lk, 0, 0, file, line,
674 lockmgr_exit(flags, ilk, 0);
678 static __noinline int
679 lockmgr_xlock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
680 const char *file, int line, struct lockmgr_wait *lwa)
682 struct lock_class *class;
688 #ifdef LOCK_PROFILING
689 uint64_t waittime = 0;
693 if (__predict_false(panicstr != NULL))
696 tid = (uintptr_t)curthread;
698 if (LK_CAN_WITNESS(flags))
699 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
700 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
704 * If curthread already holds the lock and this one is
705 * allowed to recurse, simply recurse on it.
707 if (lockmgr_xlocked(lk)) {
708 if ((flags & LK_CANRECURSE) == 0 &&
709 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
711 * If the lock is expected to not panic just
712 * give up and return.
714 if (LK_TRYOP(flags)) {
716 "%s: %p fails the try operation",
721 if (flags & LK_INTERLOCK) {
722 class = LOCK_CLASS(ilk);
723 class->lc_unlock(ilk);
725 panic("%s: recursing on non recursive lockmgr %p "
726 "@ %s:%d\n", __func__, lk, file, line);
729 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
730 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
731 lk->lk_recurse, file, line);
732 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
733 LK_TRYWIT(flags), file, line);
734 TD_LOCKS_INC(curthread);
739 if (lk->lk_lock == LK_UNLOCKED &&
740 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid))
743 PMC_SOFT_CALL( , , lock, failed);
745 lock_profile_obtain_lock_failed(&lk->lock_object,
746 &contested, &waittime);
749 * If the lock is expected to not sleep just give up
752 if (LK_TRYOP(flags)) {
753 LOCK_LOG2(lk, "%s: %p fails the try operation",
760 * Acquire the sleepqueue chain lock because we
761 * probabilly will need to manipulate waiters flags.
763 sleepq_lock(&lk->lock_object);
768 * if the lock has been released while we spun on
769 * the sleepqueue chain lock just try again.
771 if (x == LK_UNLOCKED) {
772 sleepq_release(&lk->lock_object);
777 * The lock can be in the state where there is a
778 * pending queue of waiters, but still no owner.
779 * This happens when the lock is contested and an
780 * owner is going to claim the lock.
781 * If curthread is the one successfully acquiring it
782 * claim lock ownership and return, preserving waiters
785 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
786 if ((x & ~v) == LK_UNLOCKED) {
787 v &= ~LK_EXCLUSIVE_SPINNERS;
788 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
790 sleepq_release(&lk->lock_object);
792 "%s: %p claimed by a new writer",
800 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
801 * fail, loop back and retry.
803 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
804 if (!atomic_fcmpset_ptr(&lk->lk_lock, &x,
805 x | LK_EXCLUSIVE_WAITERS)) {
808 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
813 iwmesg = lk->lock_object.lo_name;
817 iwmesg = lwa->iwmesg;
823 * As far as we have been unable to acquire the
824 * exclusive lock and the exclusive waiters flag
825 * is set, we will sleep.
827 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
829 flags &= ~LK_INTERLOCK;
832 "%s: interrupted sleep for %p with %d",
833 __func__, lk, error);
836 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
840 #ifdef LOCK_PROFILING
841 lockmgr_note_exclusive_acquire(lk, contested, waittime,
844 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
850 lockmgr_exit(flags, ilk, 0);
854 static __noinline int
855 lockmgr_upgrade(struct lock *lk, u_int flags, struct lock_object *ilk,
856 const char *file, int line, struct lockmgr_wait *lwa)
860 int wakeup_swapper = 0;
863 if (__predict_false(panicstr != NULL))
866 tid = (uintptr_t)curthread;
868 _lockmgr_assert(lk, KA_SLOCKED, file, line);
870 x = v & LK_ALL_WAITERS;
871 v &= LK_EXCLUSIVE_SPINNERS;
874 * Try to switch from one shared lock to an exclusive one.
875 * We need to preserve waiters flags during the operation.
877 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
879 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
881 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
882 LK_TRYWIT(flags), file, line);
883 TD_SLOCKS_DEC(curthread);
887 op = flags & LK_TYPE_MASK;
890 * In LK_TRYUPGRADE mode, do not drop the lock,
891 * returning EBUSY instead.
893 if (op == LK_TRYUPGRADE) {
894 LOCK_LOG2(lk, "%s: %p failed the nowait upgrade",
901 * We have been unable to succeed in upgrading, so just
902 * give up the shared lock.
904 wakeup_swapper |= wakeupshlk(lk, file, line);
905 error = lockmgr_xlock_hard(lk, flags, ilk, file, line, lwa);
906 flags &= ~LK_INTERLOCK;
908 lockmgr_exit(flags, ilk, wakeup_swapper);
913 lockmgr_lock_fast_path(struct lock *lk, u_int flags, struct lock_object *ilk,
914 const char *file, int line)
916 struct lock_class *class;
921 if (__predict_false(panicstr != NULL))
924 op = flags & LK_TYPE_MASK;
928 if (LK_CAN_WITNESS(flags))
929 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
930 file, line, flags & LK_INTERLOCK ? ilk : NULL);
931 if (__predict_false(lk->lock_object.lo_flags & LK_NOSHARE))
933 if (lockmgr_slock_try(lk, &x, flags)) {
934 lockmgr_note_shared_acquire(lk, 0, 0,
938 return (lockmgr_slock_hard(lk, flags, ilk, file, line,
943 if (LK_CAN_WITNESS(flags))
944 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
945 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
947 tid = (uintptr_t)curthread;
948 if (lk->lk_lock == LK_UNLOCKED &&
949 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
950 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
954 return (lockmgr_xlock_hard(lk, flags, ilk, file, line,
960 return (lockmgr_upgrade(lk, flags, ilk, file, line, NULL));
964 if (__predict_true(locked)) {
965 if (__predict_false(flags & LK_INTERLOCK)) {
966 class = LOCK_CLASS(ilk);
967 class->lc_unlock(ilk);
971 return (__lockmgr_args(lk, flags, ilk, LK_WMESG_DEFAULT,
972 LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, file, line));
976 static __noinline int
977 lockmgr_sunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk,
978 const char *file, int line)
981 int wakeup_swapper = 0;
983 if (__predict_false(panicstr != NULL))
986 wakeup_swapper = wakeupshlk(lk, file, line);
989 lockmgr_exit(flags, ilk, wakeup_swapper);
993 static __noinline int
994 lockmgr_xunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk,
995 const char *file, int line)
998 int wakeup_swapper = 0;
1002 if (__predict_false(panicstr != NULL))
1005 tid = (uintptr_t)curthread;
1008 * As first option, treact the lock as if it has not
1010 * Fix-up the tid var if the lock has been disowned.
1012 if (LK_HOLDER(x) == LK_KERNPROC)
1015 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1016 TD_LOCKS_DEC(curthread);
1018 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
1021 * The lock is held in exclusive mode.
1022 * If the lock is recursed also, then unrecurse it.
1024 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
1025 LOCK_LOG2(lk, "%s: %p unrecursing", __func__, lk);
1029 if (tid != LK_KERNPROC)
1030 lock_profile_release_lock(&lk->lock_object);
1032 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED))
1035 sleepq_lock(&lk->lock_object);
1040 * If the lock has exclusive waiters, give them
1041 * preference in order to avoid deadlock with
1042 * shared runners up.
1043 * If interruptible sleeps left the exclusive queue
1044 * empty avoid a starvation for the threads sleeping
1045 * on the shared queue by giving them precedence
1046 * and cleaning up the exclusive waiters bit anyway.
1047 * Please note that lk_exslpfail count may be lying
1048 * about the real number of waiters with the
1049 * LK_SLEEPFAIL flag on because they may be used in
1050 * conjunction with interruptible sleeps so
1051 * lk_exslpfail might be considered an 'upper limit'
1052 * bound, including the edge cases.
1054 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1055 realexslp = sleepq_sleepcnt(&lk->lock_object, SQ_EXCLUSIVE_QUEUE);
1056 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1057 if (lk->lk_exslpfail < realexslp) {
1058 lk->lk_exslpfail = 0;
1059 queue = SQ_EXCLUSIVE_QUEUE;
1060 v |= (x & LK_SHARED_WAITERS);
1062 lk->lk_exslpfail = 0;
1064 "%s: %p has only LK_SLEEPFAIL sleepers",
1067 "%s: %p waking up threads on the exclusive queue",
1069 wakeup_swapper = sleepq_broadcast(&lk->lock_object,
1070 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1071 queue = SQ_SHARED_QUEUE;
1076 * Exclusive waiters sleeping with LK_SLEEPFAIL
1077 * on and using interruptible sleeps/timeout
1078 * may have left spourious lk_exslpfail counts
1079 * on, so clean it up anyway.
1081 lk->lk_exslpfail = 0;
1082 queue = SQ_SHARED_QUEUE;
1085 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
1086 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1088 atomic_store_rel_ptr(&lk->lk_lock, v);
1089 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK, 0, queue);
1090 sleepq_release(&lk->lock_object);
1093 lockmgr_exit(flags, ilk, wakeup_swapper);
1098 lockmgr_unlock_fast_path(struct lock *lk, u_int flags, struct lock_object *ilk)
1100 struct lock_class *class;
1106 if (__predict_false(panicstr != NULL))
1112 _lockmgr_assert(lk, KA_LOCKED, file, line);
1115 if (__predict_true(x & LK_SHARE) != 0) {
1116 if (lockmgr_sunlock_try(lk, &x)) {
1117 lockmgr_note_shared_release(lk, file, line);
1120 return (lockmgr_sunlock_hard(lk, x, flags, ilk, file, line));
1123 tid = (uintptr_t)curthread;
1124 if (!lockmgr_recursed(lk) &&
1125 atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED)) {
1126 lockmgr_note_exclusive_release(lk, file, line);
1129 return (lockmgr_xunlock_hard(lk, x, flags, ilk, file, line));
1132 if (__predict_false(flags & LK_INTERLOCK)) {
1133 class = LOCK_CLASS(ilk);
1134 class->lc_unlock(ilk);
1140 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
1141 const char *wmesg, int pri, int timo, const char *file, int line)
1144 struct lockmgr_wait lwa;
1145 struct lock_class *class;
1147 uintptr_t tid, v, x;
1148 u_int op, realexslp;
1149 int error, ipri, itimo, queue, wakeup_swapper;
1150 #ifdef LOCK_PROFILING
1151 uint64_t waittime = 0;
1155 if (panicstr != NULL)
1159 tid = (uintptr_t)curthread;
1160 op = (flags & LK_TYPE_MASK);
1161 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
1162 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
1163 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
1165 lwa.iwmesg = iwmesg;
1169 MPASS((flags & ~LK_TOTAL_MASK) == 0);
1170 KASSERT((op & (op - 1)) == 0,
1171 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
1172 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
1173 (op != LK_DOWNGRADE && op != LK_RELEASE),
1174 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
1175 __func__, file, line));
1176 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
1177 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
1178 __func__, file, line));
1179 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
1180 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
1181 lk->lock_object.lo_name, file, line));
1183 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
1185 if (lk->lock_object.lo_flags & LK_NOSHARE) {
1193 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
1195 if (flags & LK_INTERLOCK)
1196 class->lc_unlock(ilk);
1204 return (lockmgr_slock_hard(lk, flags, ilk, file, line, &lwa));
1208 return (lockmgr_upgrade(lk, flags, ilk, file, line, &lwa));
1211 return (lockmgr_xlock_hard(lk, flags, ilk, file, line, &lwa));
1214 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1215 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
1216 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
1219 * Panic if the lock is recursed.
1221 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
1222 if (flags & LK_INTERLOCK)
1223 class->lc_unlock(ilk);
1224 panic("%s: downgrade a recursed lockmgr %s @ %s:%d\n",
1225 __func__, iwmesg, file, line);
1227 TD_SLOCKS_INC(curthread);
1230 * In order to preserve waiters flags, just spin.
1234 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1235 x &= LK_ALL_WAITERS;
1236 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1237 LK_SHARERS_LOCK(1) | x))
1243 _lockmgr_assert(lk, KA_LOCKED, file, line);
1246 if (__predict_true(x & LK_SHARE) != 0) {
1247 return (lockmgr_sunlock_hard(lk, x, flags, ilk, file, line));
1249 return (lockmgr_xunlock_hard(lk, x, flags, ilk, file, line));
1253 if (LK_CAN_WITNESS(flags))
1254 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1255 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
1259 * Trying to drain a lock we already own will result in a
1262 if (lockmgr_xlocked(lk)) {
1263 if (flags & LK_INTERLOCK)
1264 class->lc_unlock(ilk);
1265 panic("%s: draining %s with the lock held @ %s:%d\n",
1266 __func__, iwmesg, file, line);
1270 if (lk->lk_lock == LK_UNLOCKED &&
1271 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid))
1275 PMC_SOFT_CALL( , , lock, failed);
1277 lock_profile_obtain_lock_failed(&lk->lock_object,
1278 &contested, &waittime);
1281 * If the lock is expected to not sleep just give up
1284 if (LK_TRYOP(flags)) {
1285 LOCK_LOG2(lk, "%s: %p fails the try operation",
1292 * Acquire the sleepqueue chain lock because we
1293 * probabilly will need to manipulate waiters flags.
1295 sleepq_lock(&lk->lock_object);
1299 * if the lock has been released while we spun on
1300 * the sleepqueue chain lock just try again.
1302 if (x == LK_UNLOCKED) {
1303 sleepq_release(&lk->lock_object);
1307 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1308 if ((x & ~v) == LK_UNLOCKED) {
1309 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1312 * If interruptible sleeps left the exclusive
1313 * queue empty avoid a starvation for the
1314 * threads sleeping on the shared queue by
1315 * giving them precedence and cleaning up the
1316 * exclusive waiters bit anyway.
1317 * Please note that lk_exslpfail count may be
1318 * lying about the real number of waiters with
1319 * the LK_SLEEPFAIL flag on because they may
1320 * be used in conjunction with interruptible
1321 * sleeps so lk_exslpfail might be considered
1322 * an 'upper limit' bound, including the edge
1325 if (v & LK_EXCLUSIVE_WAITERS) {
1326 queue = SQ_EXCLUSIVE_QUEUE;
1327 v &= ~LK_EXCLUSIVE_WAITERS;
1331 * Exclusive waiters sleeping with
1332 * LK_SLEEPFAIL on and using
1333 * interruptible sleeps/timeout may
1334 * have left spourious lk_exslpfail
1335 * counts on, so clean it up anyway.
1337 MPASS(v & LK_SHARED_WAITERS);
1338 lk->lk_exslpfail = 0;
1339 queue = SQ_SHARED_QUEUE;
1340 v &= ~LK_SHARED_WAITERS;
1342 if (queue == SQ_EXCLUSIVE_QUEUE) {
1344 sleepq_sleepcnt(&lk->lock_object,
1345 SQ_EXCLUSIVE_QUEUE);
1346 if (lk->lk_exslpfail >= realexslp) {
1347 lk->lk_exslpfail = 0;
1348 queue = SQ_SHARED_QUEUE;
1349 v &= ~LK_SHARED_WAITERS;
1350 if (realexslp != 0) {
1352 "%s: %p has only LK_SLEEPFAIL sleepers",
1355 "%s: %p waking up threads on the exclusive queue",
1361 SQ_EXCLUSIVE_QUEUE);
1364 lk->lk_exslpfail = 0;
1366 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1367 sleepq_release(&lk->lock_object);
1371 "%s: %p waking up all threads on the %s queue",
1372 __func__, lk, queue == SQ_SHARED_QUEUE ?
1373 "shared" : "exclusive");
1374 wakeup_swapper |= sleepq_broadcast(
1375 &lk->lock_object, SLEEPQ_LK, 0, queue);
1378 * If shared waiters have been woken up we need
1379 * to wait for one of them to acquire the lock
1380 * before to set the exclusive waiters in
1381 * order to avoid a deadlock.
1383 if (queue == SQ_SHARED_QUEUE) {
1384 for (v = lk->lk_lock;
1385 (v & LK_SHARE) && !LK_SHARERS(v);
1392 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1393 * fail, loop back and retry.
1395 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1396 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1397 x | LK_EXCLUSIVE_WAITERS)) {
1398 sleepq_release(&lk->lock_object);
1401 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1406 * As far as we have been unable to acquire the
1407 * exclusive lock and the exclusive waiters flag
1408 * is set, we will sleep.
1410 if (flags & LK_INTERLOCK) {
1411 class->lc_unlock(ilk);
1412 flags &= ~LK_INTERLOCK;
1415 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1416 SQ_EXCLUSIVE_QUEUE);
1417 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1419 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1424 lock_profile_obtain_lock_success(&lk->lock_object,
1425 contested, waittime, file, line);
1426 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1427 lk->lk_recurse, file, line);
1428 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1429 LK_TRYWIT(flags), file, line);
1430 TD_LOCKS_INC(curthread);
1435 if (flags & LK_INTERLOCK)
1436 class->lc_unlock(ilk);
1437 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1440 if (flags & LK_INTERLOCK)
1441 class->lc_unlock(ilk);
1449 _lockmgr_disown(struct lock *lk, const char *file, int line)
1453 if (SCHEDULER_STOPPED())
1456 tid = (uintptr_t)curthread;
1457 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1460 * Panic if the lock is recursed.
1462 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk))
1463 panic("%s: disown a recursed lockmgr @ %s:%d\n",
1464 __func__, file, line);
1467 * If the owner is already LK_KERNPROC just skip the whole operation.
1469 if (LK_HOLDER(lk->lk_lock) != tid)
1471 lock_profile_release_lock(&lk->lock_object);
1472 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1473 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1474 TD_LOCKS_DEC(curthread);
1478 * In order to preserve waiters flags, just spin.
1482 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1483 x &= LK_ALL_WAITERS;
1484 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1492 lockmgr_printinfo(const struct lock *lk)
1497 if (lk->lk_lock == LK_UNLOCKED)
1498 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1499 else if (lk->lk_lock & LK_SHARE)
1500 printf("lock type %s: SHARED (count %ju)\n",
1501 lk->lock_object.lo_name,
1502 (uintmax_t)LK_SHARERS(lk->lk_lock));
1504 td = lockmgr_xholder(lk);
1505 if (td == (struct thread *)LK_KERNPROC)
1506 printf("lock type %s: EXCL by KERNPROC\n",
1507 lk->lock_object.lo_name);
1509 printf("lock type %s: EXCL by thread %p "
1510 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name,
1511 td, td->td_proc->p_pid, td->td_proc->p_comm,
1516 if (x & LK_EXCLUSIVE_WAITERS)
1517 printf(" with exclusive waiters pending\n");
1518 if (x & LK_SHARED_WAITERS)
1519 printf(" with shared waiters pending\n");
1520 if (x & LK_EXCLUSIVE_SPINNERS)
1521 printf(" with exclusive spinners pending\n");
1527 lockstatus(const struct lock *lk)
1536 if ((x & LK_SHARE) == 0) {
1537 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1541 } else if (x == LK_UNLOCKED)
1547 #ifdef INVARIANT_SUPPORT
1549 FEATURE(invariant_support,
1550 "Support for modules compiled with INVARIANTS option");
1553 #undef _lockmgr_assert
1557 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1561 if (panicstr != NULL)
1565 case KA_SLOCKED | KA_NOTRECURSED:
1566 case KA_SLOCKED | KA_RECURSED:
1569 case KA_LOCKED | KA_NOTRECURSED:
1570 case KA_LOCKED | KA_RECURSED:
1574 * We cannot trust WITNESS if the lock is held in exclusive
1575 * mode and a call to lockmgr_disown() happened.
1576 * Workaround this skipping the check if the lock is held in
1577 * exclusive mode even for the KA_LOCKED case.
1579 if (slocked || (lk->lk_lock & LK_SHARE)) {
1580 witness_assert(&lk->lock_object, what, file, line);
1584 if (lk->lk_lock == LK_UNLOCKED ||
1585 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1586 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1587 panic("Lock %s not %slocked @ %s:%d\n",
1588 lk->lock_object.lo_name, slocked ? "share" : "",
1591 if ((lk->lk_lock & LK_SHARE) == 0) {
1592 if (lockmgr_recursed(lk)) {
1593 if (what & KA_NOTRECURSED)
1594 panic("Lock %s recursed @ %s:%d\n",
1595 lk->lock_object.lo_name, file,
1597 } else if (what & KA_RECURSED)
1598 panic("Lock %s not recursed @ %s:%d\n",
1599 lk->lock_object.lo_name, file, line);
1603 case KA_XLOCKED | KA_NOTRECURSED:
1604 case KA_XLOCKED | KA_RECURSED:
1605 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1606 panic("Lock %s not exclusively locked @ %s:%d\n",
1607 lk->lock_object.lo_name, file, line);
1608 if (lockmgr_recursed(lk)) {
1609 if (what & KA_NOTRECURSED)
1610 panic("Lock %s recursed @ %s:%d\n",
1611 lk->lock_object.lo_name, file, line);
1612 } else if (what & KA_RECURSED)
1613 panic("Lock %s not recursed @ %s:%d\n",
1614 lk->lock_object.lo_name, file, line);
1617 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1618 panic("Lock %s exclusively locked @ %s:%d\n",
1619 lk->lock_object.lo_name, file, line);
1622 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1630 lockmgr_chain(struct thread *td, struct thread **ownerp)
1636 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1638 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1639 if (lk->lk_lock & LK_SHARE)
1640 db_printf("SHARED (count %ju)\n",
1641 (uintmax_t)LK_SHARERS(lk->lk_lock));
1643 db_printf("EXCL\n");
1644 *ownerp = lockmgr_xholder(lk);
1650 db_show_lockmgr(const struct lock_object *lock)
1653 const struct lock *lk;
1655 lk = (const struct lock *)lock;
1657 db_printf(" state: ");
1658 if (lk->lk_lock == LK_UNLOCKED)
1659 db_printf("UNLOCKED\n");
1660 else if (lk->lk_lock & LK_SHARE)
1661 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1663 td = lockmgr_xholder(lk);
1664 if (td == (struct thread *)LK_KERNPROC)
1665 db_printf("XLOCK: LK_KERNPROC\n");
1667 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1668 td->td_tid, td->td_proc->p_pid,
1669 td->td_proc->p_comm);
1670 if (lockmgr_recursed(lk))
1671 db_printf(" recursed: %d\n", lk->lk_recurse);
1673 db_printf(" waiters: ");
1674 switch (lk->lk_lock & LK_ALL_WAITERS) {
1675 case LK_SHARED_WAITERS:
1676 db_printf("shared\n");
1678 case LK_EXCLUSIVE_WAITERS:
1679 db_printf("exclusive\n");
1681 case LK_ALL_WAITERS:
1682 db_printf("shared and exclusive\n");
1685 db_printf("none\n");
1687 db_printf(" spinners: ");
1688 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1689 db_printf("exclusive\n");
1691 db_printf("none\n");