29 #include "opt_adaptive_lockmgrs.h"
31 #include "opt_hwpmc_hooks.h"
32 #include "opt_kdtrace.h"
34 #include <sys/cdefs.h>
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
71 #ifdef ADAPTIVE_LOCKMGRS
72 #define ALK_RETRIES 10
73 #define ALK_LOOPS 10000
77 #define _lockmgr_assert(lk, what, file, line)
78 #define TD_LOCKS_INC(td)
79 #define TD_LOCKS_DEC(td)
81 #define TD_LOCKS_INC(td) ((td)->td_locks++)
82 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
84 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
85 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
88 #define STACK_PRINT(lk)
89 #define STACK_SAVE(lk)
90 #define STACK_ZERO(lk)
92 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
93 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
94 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
97 #define LOCK_LOG2(lk, string, arg1, arg2) \
98 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
99 CTR2(KTR_LOCK, (string), (arg1), (arg2))
100 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
101 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
102 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
104 #define GIANT_DECLARE \
106 WITNESS_SAVE_DECL(Giant)
107 #define GIANT_RESTORE() do { \
111 WITNESS_RESTORE(&Giant.lock_object, Giant); \
114 #define GIANT_SAVE() do { \
115 if (mtx_owned(&Giant)) { \
116 WITNESS_SAVE(&Giant.lock_object, Giant); \
117 while (mtx_owned(&Giant)) { \
119 mtx_unlock(&Giant); \
124 #define LK_CAN_SHARE(x) \
125 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
126 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
127 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
128 #define LK_TRYOP(x) \
131 #define LK_CAN_WITNESS(x) \
132 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
133 #define LK_TRYWIT(x) \
134 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
136 #define LK_CAN_ADAPT(lk, f) \
137 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
138 ((f) & LK_SLEEPFAIL) == 0)
140 #define lockmgr_disowned(lk) \
141 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
143 #define lockmgr_xlocked(lk) \
144 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
148 static void db_show_lockmgr(
struct lock_object *lock);
150 static void lock_lockmgr(
struct lock_object *lock,
int how);
152 static int owner_lockmgr(
struct lock_object *lock,
struct thread **owner);
157 .lc_name =
"lockmgr",
158 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
161 .lc_ddb_show = db_show_lockmgr,
166 .lc_owner = owner_lockmgr,
170 static __inline
struct thread *
176 return ((x & LK_SHARE) ? NULL : (
struct thread *)LK_HOLDER(x));
186 sleeplk(
struct lock *lk, u_int flags,
struct lock_object *ilk,
187 const char *wmesg,
int pri,
int timo,
int queue)
190 struct lock_class *
class;
193 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
194 catch = pri & PCATCH;
198 LOCK_LOG3(lk,
"%s: %p blocking on the %s sleepqueue", __func__, lk,
201 if (flags & LK_INTERLOCK)
202 class->lc_unlock(ilk);
206 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (
catch ?
207 SLEEPQ_INTERRUPTIBLE : 0), queue);
208 if ((flags & LK_TIMELOCK) && timo)
214 if ((flags & LK_TIMELOCK) && timo &&
catch)
216 else if ((flags & LK_TIMELOCK) && timo)
223 if ((flags & LK_SLEEPFAIL) && error == 0)
234 int queue, wakeup_swapper;
238 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
239 LOCK_LOG_LOCK(
"SUNLOCK", &lk->lock_object, 0, 0, file, line);
249 if (LK_SHARERS(x) > 1) {
250 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
260 if ((x & LK_ALL_WAITERS) == 0) {
261 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
263 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
273 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
291 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
292 if (lk->lk_exslpfail < realexslp) {
293 lk->lk_exslpfail = 0;
295 v |= (x & LK_SHARED_WAITERS);
297 lk->lk_exslpfail = 0;
299 "%s: %p has only LK_SLEEPFAIL sleepers",
302 "%s: %p waking up threads on the exclusive queue",
318 lk->lk_exslpfail = 0;
322 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
327 LOCK_LOG3(lk,
"%s: %p waking up threads on the %s queue",
336 lock_profile_release_lock(&lk->lock_object);
337 return (wakeup_swapper);
344 panic(
"lockmgr locks do not support assertions");
351 panic(
"lockmgr locks do not support sleep interlocking");
358 panic(
"lockmgr locks do not support sleep interlocking");
363 owner_lockmgr(
struct lock_object *lock,
struct thread **owner)
366 panic(
"lockmgr locks do not support owner inquiring");
371 lockinit(
struct lock *lk,
int pri,
const char *wmesg,
int timo,
int flags)
375 MPASS((flags & ~LK_INIT_MASK) == 0);
376 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
377 (
"%s: lockmgr not aligned for %s: %p", __func__, wmesg,
380 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
381 if (flags & LK_CANRECURSE)
382 iflags |= LO_RECURSABLE;
383 if ((flags & LK_NODUP) == 0)
385 if (flags & LK_NOPROFILE)
386 iflags |= LO_NOPROFILE;
387 if ((flags & LK_NOWITNESS) == 0)
388 iflags |= LO_WITNESS;
389 if (flags & LK_QUIET)
391 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
393 lk->lk_lock = LK_UNLOCKED;
395 lk->lk_exslpfail = 0;
398 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
410 lockmgr_assert(lk, KA_XLOCKED);
411 lk->lock_object.lo_flags &= ~LK_NOSHARE;
418 lockmgr_assert(lk, KA_XLOCKED);
419 lk->lock_object.lo_flags |= LO_RECURSABLE;
426 lockmgr_assert(lk, KA_XLOCKED);
427 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
434 KASSERT(lk->lk_lock == LK_UNLOCKED, (
"lockmgr still held"));
435 KASSERT(lk->lk_recurse == 0, (
"lockmgr still recursed"));
436 KASSERT(lk->lk_exslpfail == 0, (
"lockmgr still exclusive waiters"));
442 const char *wmesg,
int pri,
int timo,
const char *file,
int line)
445 struct lock_class *
class;
449 int error, ipri, itimo, queue, wakeup_swapper;
450 #ifdef LOCK_PROFILING
454 #ifdef ADAPTIVE_LOCKMGRS
455 volatile struct thread *owner;
456 u_int i, spintries = 0;
460 tid = (uintptr_t)curthread;
461 op = (flags & LK_TYPE_MASK);
462 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
463 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
464 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
466 MPASS((flags & ~LK_TOTAL_MASK) == 0);
467 KASSERT((op & (op - 1)) == 0,
468 (
"%s: Invalid requested operation @ %s:%d", __func__, file, line));
469 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
470 (op != LK_DOWNGRADE && op != LK_RELEASE),
471 (
"%s: Invalid flags in regard of the operation desired @ %s:%d",
472 __func__, file, line));
473 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
474 (
"%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
475 __func__, file, line));
476 KASSERT(
kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
477 (
"%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
478 lk->lock_object.lo_name, file, line));
480 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
482 if (flags & LK_INTERLOCK)
483 class->lc_unlock(ilk);
487 if (lk->lock_object.lo_flags & LK_NOSHARE) {
497 if (flags & LK_INTERLOCK)
498 class->lc_unlock(ilk);
507 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
520 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
526 PMC_SOFT_CALL( , , lock, failed);
528 lock_profile_obtain_lock_failed(&lk->lock_object,
529 &contested, &waittime);
535 if (LK_HOLDER(x) == tid) {
537 "%s: %p already held in exclusive mode",
548 LOCK_LOG2(lk,
"%s: %p fails the try operation",
554 #ifdef ADAPTIVE_LOCKMGRS
564 LK_HOLDER(x) != LK_KERNPROC) {
565 owner = (
struct thread *)LK_HOLDER(x);
566 if (LOCK_LOG_TEST(&lk->lock_object, 0))
568 "%s: spinning on %p held by %p",
569 __func__, lk, owner);
577 if (flags & LK_INTERLOCK) {
578 class->lc_unlock(ilk);
579 flags &= ~LK_INTERLOCK;
582 while (LK_HOLDER(lk->lk_lock) ==
583 (uintptr_t)owner && TD_IS_RUNNING(owner))
588 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
589 spintries < ALK_RETRIES) {
590 if (flags & LK_INTERLOCK) {
591 class->lc_unlock(ilk);
592 flags &= ~LK_INTERLOCK;
596 for (i = 0; i < ALK_LOOPS; i++) {
597 if (LOCK_LOG_TEST(&lk->lock_object, 0))
599 "%s: shared spinning on %p with %u and %u",
600 __func__, lk, spintries, i);
602 if ((x & LK_SHARE) == 0 ||
629 #ifdef ADAPTIVE_LOCKMGRS
638 LK_HOLDER(x) != LK_KERNPROC) {
639 owner = (
struct thread *)LK_HOLDER(x);
640 if (TD_IS_RUNNING(owner)) {
651 if ((x & LK_SHARED_WAITERS) == 0) {
652 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
653 x | LK_SHARED_WAITERS)) {
657 LOCK_LOG2(lk,
"%s: %p set shared waiters flag",
666 error =
sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
668 flags &= ~LK_INTERLOCK;
671 "%s: interrupted sleep for %p with %d",
672 __func__, lk, error);
675 LOCK_LOG2(lk,
"%s: %p resuming from the sleep queue",
679 lock_profile_obtain_lock_success(&lk->lock_object,
680 contested, waittime, file, line);
681 LOCK_LOG_LOCK(
"SLOCK", &lk->lock_object, 0, 0, file,
683 WITNESS_LOCK(&lk->lock_object,
LK_TRYWIT(flags), file,
694 x = v & LK_ALL_WAITERS;
695 v &= LK_EXCLUSIVE_SPINNERS;
701 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
703 LOCK_LOG_LOCK(
"XUPGRADE", &lk->lock_object, 0, 0, file,
705 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
715 if (op == LK_TRYUPGRADE) {
716 LOCK_LOG2(lk,
"%s: %p failed the nowait upgrade",
731 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
732 LOP_EXCLUSIVE, file, line, ilk);
739 if ((flags & LK_CANRECURSE) == 0 &&
740 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
748 "%s: %p fails the try operation",
753 if (flags & LK_INTERLOCK)
754 class->lc_unlock(ilk);
755 panic(
"%s: recursing on non recursive lockmgr %s @ %s:%d\n",
756 __func__, iwmesg, file, line);
759 LOCK_LOG2(lk,
"%s: %p recursing", __func__, lk);
760 LOCK_LOG_LOCK(
"XLOCK", &lk->lock_object, 0,
761 lk->lk_recurse, file, line);
762 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
768 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
771 PMC_SOFT_CALL( , , lock, failed);
773 lock_profile_obtain_lock_failed(&lk->lock_object,
774 &contested, &waittime);
781 LOCK_LOG2(lk,
"%s: %p fails the try operation",
787 #ifdef ADAPTIVE_LOCKMGRS
795 LK_HOLDER(x) != LK_KERNPROC) {
796 owner = (
struct thread *)LK_HOLDER(x);
797 if (LOCK_LOG_TEST(&lk->lock_object, 0))
799 "%s: spinning on %p held by %p",
800 __func__, lk, owner);
808 if (flags & LK_INTERLOCK) {
809 class->lc_unlock(ilk);
810 flags &= ~LK_INTERLOCK;
813 while (LK_HOLDER(lk->lk_lock) ==
814 (uintptr_t)owner && TD_IS_RUNNING(owner))
819 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
820 spintries < ALK_RETRIES) {
821 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
822 !atomic_cmpset_ptr(&lk->lk_lock, x,
823 x | LK_EXCLUSIVE_SPINNERS))
825 if (flags & LK_INTERLOCK) {
826 class->lc_unlock(ilk);
827 flags &= ~LK_INTERLOCK;
831 for (i = 0; i < ALK_LOOPS; i++) {
832 if (LOCK_LOG_TEST(&lk->lock_object, 0))
834 "%s: shared spinning on %p with %u and %u",
835 __func__, lk, spintries, i);
837 LK_EXCLUSIVE_SPINNERS) == 0)
858 if (x == LK_UNLOCKED) {
863 #ifdef ADAPTIVE_LOCKMGRS
872 LK_HOLDER(x) != LK_KERNPROC) {
873 owner = (
struct thread *)LK_HOLDER(x);
874 if (TD_IS_RUNNING(owner)) {
890 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
891 if ((x & ~v) == LK_UNLOCKED) {
892 v &= ~LK_EXCLUSIVE_SPINNERS;
893 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
897 "%s: %p claimed by a new writer",
909 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
910 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
911 x | LK_EXCLUSIVE_WAITERS)) {
915 LOCK_LOG2(lk,
"%s: %p set excl waiters flag",
924 error =
sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
926 flags &= ~LK_INTERLOCK;
929 "%s: interrupted sleep for %p with %d",
930 __func__, lk, error);
933 LOCK_LOG2(lk,
"%s: %p resuming from the sleep queue",
937 lock_profile_obtain_lock_success(&lk->lock_object,
938 contested, waittime, file, line);
939 LOCK_LOG_LOCK(
"XLOCK", &lk->lock_object, 0,
940 lk->lk_recurse, file, line);
941 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
949 LOCK_LOG_LOCK(
"XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
950 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
958 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
960 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
961 LK_SHARERS_LOCK(1) | x))
970 if ((x & LK_SHARE) == 0) {
977 if (LK_HOLDER(x) == LK_KERNPROC)
980 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
984 LOCK_LOG_LOCK(
"XUNLOCK", &lk->lock_object, 0,
985 lk->lk_recurse, file, line);
992 LOCK_LOG2(lk,
"%s: %p unrecursing", __func__,
997 if (tid != LK_KERNPROC)
998 lock_profile_release_lock(&lk->lock_object);
1000 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
1023 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1026 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1027 if (lk->lk_exslpfail < realexslp) {
1028 lk->lk_exslpfail = 0;
1030 v |= (x & LK_SHARED_WAITERS);
1032 lk->lk_exslpfail = 0;
1034 "%s: %p has only LK_SLEEPFAIL sleepers",
1037 "%s: %p waking up threads on the exclusive queue",
1052 lk->lk_exslpfail = 0;
1057 "%s: %p waking up threads on the %s queue",
1060 atomic_store_rel_ptr(&lk->lk_lock, v);
1062 SLEEPQ_LK, 0, queue);
1070 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1071 LOP_EXCLUSIVE, file, line, ilk);
1078 if (flags & LK_INTERLOCK)
1079 class->lc_unlock(ilk);
1080 panic(
"%s: draining %s with the lock held @ %s:%d\n",
1081 __func__, iwmesg, file, line);
1084 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1086 PMC_SOFT_CALL( , , lock, failed);
1088 lock_profile_obtain_lock_failed(&lk->lock_object,
1089 &contested, &waittime);
1096 LOCK_LOG2(lk,
"%s: %p fails the try operation",
1113 if (x == LK_UNLOCKED) {
1118 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1119 if ((x & ~v) == LK_UNLOCKED) {
1120 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1136 if (v & LK_EXCLUSIVE_WAITERS) {
1138 v &= ~LK_EXCLUSIVE_WAITERS;
1148 MPASS(v & LK_SHARED_WAITERS);
1149 lk->lk_exslpfail = 0;
1151 v &= ~LK_SHARED_WAITERS;
1157 if (lk->lk_exslpfail >= realexslp) {
1158 lk->lk_exslpfail = 0;
1160 v &= ~LK_SHARED_WAITERS;
1161 if (realexslp != 0) {
1163 "%s: %p has only LK_SLEEPFAIL sleepers",
1166 "%s: %p waking up threads on the exclusive queue",
1175 lk->lk_exslpfail = 0;
1177 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1182 "%s: %p waking up all threads on the %s queue",
1184 "shared" :
"exclusive");
1186 &lk->lock_object, SLEEPQ_LK, 0, queue);
1195 for (v = lk->lk_lock;
1196 (v & LK_SHARE) && !LK_SHARERS(v);
1206 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1207 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1208 x | LK_EXCLUSIVE_WAITERS)) {
1212 LOCK_LOG2(lk,
"%s: %p set drain waiters flag",
1221 if (flags & LK_INTERLOCK) {
1222 class->lc_unlock(ilk);
1223 flags &= ~LK_INTERLOCK;
1226 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1230 LOCK_LOG2(lk,
"%s: %p resuming from the sleep queue",
1235 lock_profile_obtain_lock_success(&lk->lock_object,
1236 contested, waittime, file, line);
1237 LOCK_LOG_LOCK(
"DRAIN", &lk->lock_object, 0,
1238 lk->lk_recurse, file, line);
1239 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1246 if (flags & LK_INTERLOCK)
1247 class->lc_unlock(ilk);
1248 panic(
"%s: unknown lockmgr request 0x%x\n", __func__, op);
1251 if (flags & LK_INTERLOCK)
1252 class->lc_unlock(ilk);
1264 if (SCHEDULER_STOPPED())
1267 tid = (uintptr_t)curthread;
1273 if (LK_HOLDER(lk->lk_lock) != tid)
1275 lock_profile_release_lock(&lk->lock_object);
1276 LOCK_LOG_LOCK(
"XDISOWN", &lk->lock_object, 0, 0, file, line);
1277 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1286 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1287 x &= LK_ALL_WAITERS;
1288 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1301 if (lk->lk_lock == LK_UNLOCKED)
1302 printf(
"lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1303 else if (lk->lk_lock & LK_SHARE)
1304 printf(
"lock type %s: SHARED (count %ju)\n",
1305 lk->lock_object.lo_name,
1306 (uintmax_t)LK_SHARERS(lk->lk_lock));
1309 printf(
"lock type %s: EXCL by thread %p (pid %d)\n",
1310 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1314 if (x & LK_EXCLUSIVE_WAITERS)
1315 printf(
" with exclusive waiters pending\n");
1316 if (x & LK_SHARED_WAITERS)
1317 printf(
" with shared waiters pending\n");
1318 if (x & LK_EXCLUSIVE_SPINNERS)
1319 printf(
" with exclusive spinners pending\n");
1334 if ((x & LK_SHARE) == 0) {
1335 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1339 }
else if (x == LK_UNLOCKED)
1345 #ifdef INVARIANT_SUPPORT
1348 "Support for modules compiled with INVARIANTS option");
1351 #undef _lockmgr_assert
1355 _lockmgr_assert(
struct lock *lk,
int what,
const char *file,
int line)
1363 case KA_SLOCKED | KA_NOTRECURSED:
1364 case KA_SLOCKED | KA_RECURSED:
1367 case KA_LOCKED | KA_NOTRECURSED:
1368 case KA_LOCKED | KA_RECURSED:
1377 if (slocked || (lk->lk_lock & LK_SHARE)) {
1382 if (lk->lk_lock == LK_UNLOCKED ||
1383 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1385 panic(
"Lock %s not %slocked @ %s:%d\n",
1386 lk->lock_object.lo_name, slocked ?
"share" :
"",
1389 if ((lk->lk_lock & LK_SHARE) == 0) {
1390 if (lockmgr_recursed(lk)) {
1391 if (what & KA_NOTRECURSED)
1392 panic(
"Lock %s recursed @ %s:%d\n",
1393 lk->lock_object.lo_name, file,
1395 }
else if (what & KA_RECURSED)
1396 panic(
"Lock %s not recursed @ %s:%d\n",
1397 lk->lock_object.lo_name, file, line);
1401 case KA_XLOCKED | KA_NOTRECURSED:
1402 case KA_XLOCKED | KA_RECURSED:
1404 panic(
"Lock %s not exclusively locked @ %s:%d\n",
1405 lk->lock_object.lo_name, file, line);
1406 if (lockmgr_recursed(lk)) {
1407 if (what & KA_NOTRECURSED)
1408 panic(
"Lock %s recursed @ %s:%d\n",
1409 lk->lock_object.lo_name, file, line);
1410 }
else if (what & KA_RECURSED)
1411 panic(
"Lock %s not recursed @ %s:%d\n",
1412 lk->lock_object.lo_name, file, line);
1416 panic(
"Lock %s exclusively locked @ %s:%d\n",
1417 lk->lock_object.lo_name, file, line);
1420 panic(
"Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1428 lockmgr_chain(
struct thread *td,
struct thread **ownerp)
1434 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1436 db_printf(
"blocked on lockmgr %s", lk->lock_object.lo_name);
1437 if (lk->lk_lock & LK_SHARE)
1438 db_printf(
"SHARED (count %ju)\n",
1439 (uintmax_t)LK_SHARERS(lk->lk_lock));
1441 db_printf(
"EXCL\n");
1448 db_show_lockmgr(
struct lock_object *lock)
1453 lk = (
struct lock *)lock;
1455 db_printf(
" state: ");
1456 if (lk->lk_lock == LK_UNLOCKED)
1457 db_printf(
"UNLOCKED\n");
1458 else if (lk->lk_lock & LK_SHARE)
1459 db_printf(
"SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1462 if (td == (
struct thread *)LK_KERNPROC)
1463 db_printf(
"XLOCK: LK_KERNPROC\n");
1465 db_printf(
"XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1466 td->td_tid, td->td_proc->p_pid,
1467 td->td_proc->p_comm);
1468 if (lockmgr_recursed(lk))
1469 db_printf(
" recursed: %d\n", lk->lk_recurse);
1471 db_printf(
" waiters: ");
1472 switch (lk->lk_lock & LK_ALL_WAITERS) {
1473 case LK_SHARED_WAITERS:
1474 db_printf(
"shared\n");
1476 case LK_EXCLUSIVE_WAITERS:
1477 db_printf(
"exclusive\n");
1479 case LK_ALL_WAITERS:
1480 db_printf(
"shared and exclusive\n");
1483 db_printf(
"none\n");
1485 db_printf(
" spinners: ");
1486 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1487 db_printf(
"exclusive\n");
1489 db_printf(
"none\n");
void lockdestroy(struct lock *lk)
int lockstatus(struct lock *lk)
#define LOCK_LOG3(lk, string, arg1, arg2, arg3)
void sleepq_release(void *wchan)
void sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, int flags, int queue)
#define _lockmgr_assert(lk, what, file, line)
#define TD_SLOCKS_INC(td)
void witness_assert(struct lock_object *lock, int flags, const char *file, int line)
#define SQ_EXCLUSIVE_QUEUE
static int unlock_lockmgr(struct lock_object *lock)
int sleepq_timedwait_sig(void *wchan, int pri)
void lockallowrecurse(struct lock *lk)
void panic(const char *fmt,...)
void lockallowshare(struct lock *lk)
static __inline int sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk, const char *wmesg, int pri, int timo, int queue)
#define lockmgr_xlocked(lk)
#define lockmgr_disowned(lk)
void sleepq_set_timeout(void *wchan, int timo)
int __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk, const char *wmesg, int pri, int timo, const char *file, int line)
int sleepq_wait_sig(void *wchan, int pri)
void sleepq_lock(void *wchan)
#define LK_CAN_WITNESS(x)
#define LOCK_LOG2(lk, string, arg1, arg2)
void _lockmgr_disown(struct lock *lk, const char *file, int line)
void lockdisablerecurse(struct lock *lk)
void sleepq_wait(void *wchan, int pri)
void lock_init(struct lock_object *lock, struct lock_class *class, const char *name, const char *type, int flags)
static __inline struct thread * lockmgr_xholder(struct lock *lk)
#define TD_SLOCKS_DEC(td)
int sleepq_broadcast(void *wchan, int flags, int pri, int queue)
#define LK_CAN_ADAPT(lk, f)
CTASSERT(((LK_ADAPTIVE|LK_NOSHARE)&LO_CLASSFLAGS)==(LK_ADAPTIVE|LK_NOSHARE))
int printf(const char *fmt,...)
static __inline int wakeupshlk(struct lock *lk, const char *file, int line)
struct lock_class lock_class_lockmgr
static void lock_lockmgr(struct lock_object *lock, int how)
void lockmgr_printinfo(struct lock *lk)
void lock_destroy(struct lock_object *lock)
FEATURE(kdtrace_hooks,"Kernel DTrace hooks which are required to load DTrace kernel modules")
u_int sleepq_sleepcnt(void *wchan, int queue)
static void assert_lockmgr(struct lock_object *lock, int how)
int sleepq_timedwait(void *wchan, int pri)
void lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)