32#include "opt_hwpmc_hooks.h"
40#include <sys/limits.h>
42#include <sys/lock_profile.h>
43#include <sys/lockmgr.h>
44#include <sys/lockstat.h>
47#include <sys/sleepqueue.h>
51#include <sys/sysctl.h>
54#include <machine/cpu.h>
61#include <sys/pmckern.h>
62PMC_SOFT_DECLARE( , , lock, failed);
69 "prio flags wont fit in u_short pri in struct lock");
72 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
74#define SQ_EXCLUSIVE_QUEUE 0
75#define SQ_SHARED_QUEUE 1
78#define _lockmgr_assert(lk, what, file, line)
81#define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
82#define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
85#define STACK_PRINT(lk)
89#define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
90#define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
91#define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
94#define LOCK_LOG2(lk, string, arg1, arg2) \
95 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
96 CTR2(KTR_LOCK, (string), (arg1), (arg2))
97#define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
98 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
99 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
101#define GIANT_DECLARE \
103 WITNESS_SAVE_DECL(Giant)
104#define GIANT_RESTORE() do { \
105 if (__predict_false(_i > 0)) { \
108 WITNESS_RESTORE(&Giant.lock_object, Giant); \
111#define GIANT_SAVE() do { \
112 if (__predict_false(mtx_owned(&Giant))) { \
113 WITNESS_SAVE(&Giant.lock_object, Giant); \
114 while (mtx_owned(&Giant)) { \
116 mtx_unlock(&Giant); \
121static bool __always_inline
125 if ((x & (LK_SHARE | LK_EXCLUSIVE_WAITERS | LK_EXCLUSIVE_SPINNERS)) ==
128 if (fp || (!(x & LK_SHARE)))
130 if ((curthread->td_lk_slocks != 0 && !(
flags & LK_NODDLKTREAT)) ||
131 (curthread->td_pflags & TDP_DEADLKTREAT))
139#define LK_CAN_WITNESS(x) \
140 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
141#define LK_TRYWIT(x) \
142 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
144#define lockmgr_disowned(lk) \
145 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
147#define lockmgr_xlocked_v(v) \
148 (((v) & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
150#define lockmgr_xlocked(lk) lockmgr_xlocked_v(lockmgr_read_value(lk))
152static void assert_lockmgr(
const struct lock_object *lock,
int how);
154static void db_show_lockmgr(
const struct lock_object *lock);
156static void lock_lockmgr(
struct lock_object *lock, uintptr_t how);
158static int owner_lockmgr(
const struct lock_object *lock,
159 struct thread **owner);
164 .lc_name =
"lockmgr",
165 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
168 .lc_ddb_show = db_show_lockmgr,
173 .lc_owner = owner_lockmgr,
178static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL,
"lockmgr debugging");
181#define lockmgr_delay locks_delay
196 struct lock_class *
class;
198 if (
flags & LK_INTERLOCK) {
199 class = LOCK_CLASS(ilk);
200 class->lc_unlock(ilk);
203 if (__predict_false(wakeup_swapper))
209 uint64_t waittime,
const char *file,
int line,
int flags)
212 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(lockmgr__acquire, lk, contested,
213 waittime, file, line, LOCKSTAT_READER);
214 LOCK_LOG_LOCK(
"SLOCK", &lk->lock_object, 0, 0, file, line);
216 TD_LOCKS_INC(curthread);
225 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
226 LOCK_LOG_LOCK(
"SUNLOCK", &lk->lock_object, 0, 0, file, line);
227 TD_LOCKS_DEC(curthread);
233 uint64_t waittime,
const char *file,
int line,
int flags)
236 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(lockmgr__acquire, lk, contested,
237 waittime, file, line, LOCKSTAT_WRITER);
238 LOCK_LOG_LOCK(
"XLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
239 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
LK_TRYWIT(
flags), file,
241 TD_LOCKS_INC(curthread);
249 if (LK_HOLDER(lockmgr_read_value(lk)) != LK_KERNPROC) {
250 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
251 TD_LOCKS_DEC(curthread);
253 LOCK_LOG_LOCK(
"XUNLOCK", &lk->lock_object, 0, lk->lk_recurse, file,
257static __inline
struct thread *
262 x = lockmgr_read_value(lk);
263 return ((x & LK_SHARE) ? NULL : (
struct thread *)LK_HOLDER(x));
274 const char *wmesg,
int pri,
int timo,
int queue)
277 struct lock_class *
class;
280 class = (
flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
281 catch = pri & PCATCH;
285 LOCK_LOG3(lk,
"%s: %p blocking on the %s sleepqueue", __func__, lk,
288 if (
flags & LK_INTERLOCK)
289 class->lc_unlock(ilk);
291 if (lk->lk_exslpfail < USHRT_MAX)
295 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (
catch ?
296 SLEEPQ_INTERRUPTIBLE : 0), queue);
297 if ((
flags & LK_TIMELOCK) && timo)
298 sleepq_set_timeout(&lk->lock_object, timo);
303 if ((
flags & LK_TIMELOCK) && timo &&
catch)
305 else if ((
flags & LK_TIMELOCK) && timo)
312 if ((
flags & LK_SLEEPFAIL) && error == 0)
321 uintptr_t v, x, orig_x;
323 int queue, wakeup_swapper;
327 x = lockmgr_read_value(lk);
336 orig_x = lockmgr_read_value(lk);
338 x = orig_x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
356 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
357 if (lk->lk_exslpfail != USHRT_MAX && lk->lk_exslpfail < realexslp) {
358 lk->lk_exslpfail = 0;
360 v |= (x & LK_SHARED_WAITERS);
362 lk->lk_exslpfail = 0;
364 "%s: %p has only LK_SLEEPFAIL sleepers",
367 "%s: %p waking up threads on the exclusive queue",
381 lk->lk_exslpfail = 0;
390 x |= LK_SHARERS_LOCK(1);
391 if (!atomic_fcmpset_rel_ptr(&lk->lk_lock, &x, v)) {
395 LOCK_LOG3(lk,
"%s: %p waking up threads on the %s queue",
404 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk, LOCKSTAT_READER);
405 return (wakeup_swapper);
412 panic(
"lockmgr locks do not support assertions");
419 panic(
"lockmgr locks do not support sleep interlocking");
426 panic(
"lockmgr locks do not support sleep interlocking");
431owner_lockmgr(
const struct lock_object *lock,
struct thread **owner)
434 panic(
"lockmgr locks do not support owner inquiring");
443 MPASS((
flags & ~LK_INIT_MASK) == 0);
444 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
445 (
"%s: lockmgr not aligned for %s: %p", __func__, wmesg,
448 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
449 if (
flags & LK_CANRECURSE)
450 iflags |= LO_RECURSABLE;
451 if ((
flags & LK_NODUP) == 0)
453 if (
flags & LK_NOPROFILE)
454 iflags |= LO_NOPROFILE;
455 if ((
flags & LK_NOWITNESS) == 0)
456 iflags |= LO_WITNESS;
457 if (
flags & LK_QUIET)
459 if (
flags & LK_IS_VNODE)
460 iflags |= LO_IS_VNODE;
463 iflags |=
flags & LK_NOSHARE;
466 lk->lk_lock = LK_UNLOCKED;
468 lk->lk_exslpfail = 0;
482 lockmgr_assert(lk, KA_XLOCKED);
483 lk->lock_object.lo_flags &= ~LK_NOSHARE;
490 lockmgr_assert(lk, KA_XLOCKED);
491 lk->lock_object.lo_flags |= LK_NOSHARE;
498 lockmgr_assert(lk, KA_XLOCKED);
499 lk->lock_object.lo_flags |= LO_RECURSABLE;
506 lockmgr_assert(lk, KA_XLOCKED);
507 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
514 KASSERT(lk->lk_lock == LK_UNLOCKED, (
"lockmgr still held"));
515 KASSERT(lk->lk_recurse == 0, (
"lockmgr still recursed"));
516 KASSERT(lk->lk_exslpfail == 0, (
"lockmgr still exclusive waiters"));
520static bool __always_inline
532 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, xp,
533 *xp + LK_ONE_SHARER)) {
540static bool __always_inline
545 if (LK_SHARERS(*xp) > 1 || !(*xp & LK_ALL_WAITERS)) {
546 if (atomic_fcmpset_rel_ptr(&lk->lk_lock, xp,
547 *xp - LK_ONE_SHARER))
560 struct thread *owner;
564 MPASS(x != LK_UNLOCKED);
565 owner = (
struct thread *)LK_HOLDER(x);
567 MPASS(owner != curthread);
568 if (owner == (
struct thread *)LK_KERNPROC)
570 if ((x & LK_SHARE) && LK_SHARERS(x) > 0)
574 if (!TD_IS_RUNNING(owner))
576 if ((x & LK_ALL_WAITERS) != 0)
579 x = lockmgr_read_value(lk);
584 owner = (
struct thread *)LK_HOLDER(x);
598 uint64_t sleep_time = 0;
601 uint64_t waittime = 0;
604 struct lock_delay_arg lda;
606 if (KERNEL_PANICKED())
609 tid = (uintptr_t)curthread;
612 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
613 file, line,
flags & LK_INTERLOCK ? ilk : NULL);
614 x = lockmgr_read_value(lk);
617 flags &= ~LK_ADAPTIVE;
622 if (LK_HOLDER(x) == tid) {
624 "%s: %p already held in exclusive mode",
634 lock_profile_obtain_lock_failed(&lk->lock_object,
false,
635 &contested, &waittime);
637 if ((
flags & (LK_ADAPTIVE | LK_INTERLOCK)) == LK_ADAPTIVE) {
643 PMC_SOFT_CALL( , , lock, failed);
651 LOCK_LOG2(lk,
"%s: %p fails the try operation",
662 x = lockmgr_read_value(lk);
678 if ((x & LK_SHARED_WAITERS) == 0) {
679 if (!atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
680 x | LK_SHARED_WAITERS)) {
683 LOCK_LOG2(lk,
"%s: %p set shared waiters flag",
688 iwmesg = lk->lock_object.lo_name;
710 flags &= ~LK_INTERLOCK;
713 "%s: interrupted sleep for %p with %d",
714 __func__, lk, error);
717 LOCK_LOG2(lk,
"%s: %p resuming from the sleep queue",
719 x = lockmgr_read_value(lk);
724 LOCKSTAT_RECORD4(lockmgr__block, lk, sleep_time,
725 LOCKSTAT_READER, (x & LK_SHARE) == 0,
726 (x & LK_SHARE) == 0 ? 0 : LK_SHARERS(x));
745 struct thread *owner;
749 MPASS(x != LK_UNLOCKED);
750 owner = (
struct thread *)LK_HOLDER(x);
752 MPASS(owner != curthread);
755 if ((x & LK_SHARE) && LK_SHARERS(x) > 0)
757 if (owner == (
struct thread *)LK_KERNPROC)
759 if (!TD_IS_RUNNING(owner))
761 if ((x & LK_ALL_WAITERS) != 0)
764 x = lockmgr_read_value(lk);
765 if (x == LK_UNLOCKED) {
769 owner = (
struct thread *)LK_HOLDER(x);
777 struct lock_class *
class;
784 uint64_t sleep_time = 0;
787 uint64_t waittime = 0;
790 struct lock_delay_arg lda;
792 if (KERNEL_PANICKED())
795 tid = (uintptr_t)curthread;
798 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
799 LOP_EXCLUSIVE, file, line,
flags & LK_INTERLOCK ?
807 if ((
flags & LK_CANRECURSE) == 0 &&
808 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
815 "%s: %p fails the try operation",
820 if (
flags & LK_INTERLOCK) {
821 class = LOCK_CLASS(ilk);
822 class->lc_unlock(ilk);
825 panic(
"%s: recursing on non recursive lockmgr %p "
826 "@ %s:%d\n", __func__, lk, file, line);
828 atomic_set_ptr(&lk->lk_lock, LK_WRITER_RECURSED);
830 LOCK_LOG2(lk,
"%s: %p recursing", __func__, lk);
831 LOCK_LOG_LOCK(
"XLOCK", &lk->lock_object, 0,
832 lk->lk_recurse, file, line);
833 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
835 TD_LOCKS_INC(curthread);
842 flags &= ~LK_ADAPTIVE;
844 if (x == LK_UNLOCKED) {
845 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x, tid))
850 lock_profile_obtain_lock_failed(&lk->lock_object,
false,
851 &contested, &waittime);
853 if ((
flags & (LK_ADAPTIVE | LK_INTERLOCK)) == LK_ADAPTIVE) {
858 PMC_SOFT_CALL( , , lock, failed);
866 LOCK_LOG2(lk,
"%s: %p fails the try operation",
877 x = lockmgr_read_value(lk);
884 if (x == LK_UNLOCKED) {
898 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
899 if ((x & ~v) == LK_UNLOCKED) {
900 v &= ~LK_EXCLUSIVE_SPINNERS;
901 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
905 "%s: %p claimed by a new writer",
916 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
917 if (!atomic_fcmpset_ptr(&lk->lk_lock, &x,
918 x | LK_EXCLUSIVE_WAITERS)) {
921 LOCK_LOG2(lk,
"%s: %p set excl waiters flag",
926 iwmesg = lk->lock_object.lo_name;
948 flags &= ~LK_INTERLOCK;
951 "%s: interrupted sleep for %p with %d",
952 __func__, lk, error);
955 LOCK_LOG2(lk,
"%s: %p resuming from the sleep queue",
957 x = lockmgr_read_value(lk);
962 LOCKSTAT_RECORD4(lockmgr__block, lk, sleep_time,
963 LOCKSTAT_WRITER, (x & LK_SHARE) == 0,
964 (x & LK_SHARE) == 0 ? 0 : LK_SHARERS(x));
984 uintptr_t tid, v, setv;
988 if (KERNEL_PANICKED())
991 tid = (uintptr_t)curthread;
995 op =
flags & LK_TYPE_MASK;
996 v = lockmgr_read_value(lk);
998 if (LK_SHARERS(v) > 1) {
999 if (op == LK_TRYUPGRADE) {
1000 LOCK_LOG2(lk,
"%s: %p failed the nowait upgrade",
1005 if (atomic_fcmpset_rel_ptr(&lk->lk_lock, &v,
1006 v - LK_ONE_SHARER)) {
1012 MPASS((v & ~LK_ALL_WAITERS) == LK_SHARERS_LOCK(1));
1015 setv |= (v & LK_ALL_WAITERS);
1021 if (atomic_fcmpset_ptr(&lk->lk_lock, &v, setv)) {
1022 LOCK_LOG_LOCK(
"XUPGRADE", &lk->lock_object, 0, 0, file,
1024 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
1026 LOCKSTAT_RECORD0(lockmgr__upgrade, lk);
1034 flags &= ~LK_INTERLOCK;
1042 const char *file,
int line)
1044 struct lock_class *
class;
1049 if (KERNEL_PANICKED())
1052 op =
flags & LK_TYPE_MASK;
1057 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
1058 file, line,
flags & LK_INTERLOCK ? ilk : NULL);
1059 if (__predict_false(lk->lock_object.lo_flags & LK_NOSHARE))
1061 x = lockmgr_read_value(lk);
1073 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1074 LOP_EXCLUSIVE, file, line,
flags & LK_INTERLOCK ?
1076 tid = (uintptr_t)curthread;
1077 if (lockmgr_read_value(lk) == LK_UNLOCKED &&
1078 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1093 if (__predict_true(locked)) {
1094 if (__predict_false(
flags & LK_INTERLOCK)) {
1095 class = LOCK_CLASS(ilk);
1096 class->lc_unlock(ilk);
1101 LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, file, line));
1105static __noinline
int
1107 const char *file,
int line)
1110 int wakeup_swapper = 0;
1112 if (KERNEL_PANICKED())
1122static __noinline
int
1124 const char *file,
int line)
1127 int wakeup_swapper = 0;
1131 if (KERNEL_PANICKED())
1134 tid = (uintptr_t)curthread;
1141 if (LK_HOLDER(x) == LK_KERNPROC)
1148 if (lockmgr_recursed_v(x)) {
1149 LOCK_LOG2(lk,
"%s: %p unrecursing", __func__, lk);
1151 if (lk->lk_recurse == 0)
1152 atomic_clear_ptr(&lk->lk_lock, LK_WRITER_RECURSED);
1155 if (tid != LK_KERNPROC)
1156 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk,
1159 if (x == tid && atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED))
1163 x = lockmgr_read_value(lk);
1181 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1183 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1184 if (lk->lk_exslpfail != USHRT_MAX && lk->lk_exslpfail < realexslp) {
1185 lk->lk_exslpfail = 0;
1187 v |= (x & LK_SHARED_WAITERS);
1189 lk->lk_exslpfail = 0;
1191 "%s: %p has only LK_SLEEPFAIL sleepers",
1194 "%s: %p waking up threads on the exclusive queue",
1207 lk->lk_exslpfail = 0;
1211 LOCK_LOG3(lk,
"%s: %p waking up threads on the %s queue",
1214 atomic_store_rel_ptr(&lk->lk_lock, v);
1239 MPASS((
flags & LK_TYPE_MASK) == LK_SHARED);
1240 MPASS((
flags & LK_INTERLOCK) == 0);
1241 MPASS((lk->lock_object.lo_flags & LK_NOSHARE) == 0);
1244 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
1246 x = lockmgr_read_value(lk);
1260 MPASS((
flags & LK_TYPE_MASK) == LK_EXCLUSIVE);
1261 MPASS((
flags & LK_INTERLOCK) == 0);
1264 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1265 LOP_EXCLUSIVE, file, line, NULL);
1266 tid = (uintptr_t)curthread;
1267 if (atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1287 x = lockmgr_read_value(lk);
1288 if (__predict_true(x & LK_SHARE) != 0) {
1291 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk, LOCKSTAT_READER);
1296 tid = (uintptr_t)curthread;
1298 if (x == tid && atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED)) {
1299 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk,LOCKSTAT_WRITER);
1309 const char *wmesg,
int pri,
int timo,
const char *file,
int line)
1313 struct lock_class *
class;
1315 uintptr_t tid, v, x;
1316 u_int op, realexslp;
1317 int error, ipri, itimo, queue, wakeup_swapper;
1318#ifdef LOCK_PROFILING
1319 uint64_t waittime = 0;
1323 if (KERNEL_PANICKED())
1327 tid = (uintptr_t)curthread;
1328 op = (
flags & LK_TYPE_MASK);
1329 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
1330 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
1331 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
1337 MPASS((
flags & ~LK_TOTAL_MASK) == 0);
1338 KASSERT((op & (op - 1)) == 0,
1339 (
"%s: Invalid requested operation @ %s:%d", __func__, file, line));
1340 KASSERT((
flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
1341 (op != LK_DOWNGRADE && op != LK_RELEASE),
1342 (
"%s: Invalid flags in regard of the operation desired @ %s:%d",
1343 __func__, file, line));
1344 KASSERT((
flags & LK_INTERLOCK) == 0 || ilk != NULL,
1345 (
"%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
1346 __func__, file, line));
1347 KASSERT(
kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
1348 (
"%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
1349 lk->lock_object.lo_name, file, line));
1351 class = (
flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
1353 if (lk->lock_object.lo_flags & LK_NOSHARE) {
1363 if (
flags & LK_INTERLOCK)
1364 class->lc_unlock(ilk);
1383 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
1389 if (
flags & LK_INTERLOCK)
1390 class->lc_unlock(ilk);
1391 panic(
"%s: downgrade a recursed lockmgr %s @ %s:%d\n",
1392 __func__, iwmesg, file, line);
1400 x = lockmgr_read_value(lk);
1401 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1402 x &= LK_ALL_WAITERS;
1403 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1404 LK_SHARERS_LOCK(1) | x))
1408 LOCK_LOG_LOCK(
"XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
1409 LOCKSTAT_RECORD0(lockmgr__downgrade, lk);
1413 x = lockmgr_read_value(lk);
1415 if (__predict_true(x & LK_SHARE) != 0) {
1425 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1426 LOP_EXCLUSIVE, file, line,
flags & LK_INTERLOCK ?
1434 if (
flags & LK_INTERLOCK)
1435 class->lc_unlock(ilk);
1436 panic(
"%s: draining %s with the lock held @ %s:%d\n",
1437 __func__, iwmesg, file, line);
1441 if (lk->lk_lock == LK_UNLOCKED &&
1442 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid))
1446 PMC_SOFT_CALL( , , lock, failed);
1448 lock_profile_obtain_lock_failed(&lk->lock_object,
false,
1449 &contested, &waittime);
1456 LOCK_LOG2(lk,
"%s: %p fails the try operation",
1467 x = lockmgr_read_value(lk);
1473 if (x == LK_UNLOCKED) {
1478 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1479 if ((x & ~v) == LK_UNLOCKED) {
1480 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1496 if (v & LK_EXCLUSIVE_WAITERS) {
1498 v &= ~LK_EXCLUSIVE_WAITERS;
1507 MPASS(v & LK_SHARED_WAITERS);
1508 lk->lk_exslpfail = 0;
1510 v &= ~LK_SHARED_WAITERS;
1516 if (lk->lk_exslpfail >= realexslp) {
1517 lk->lk_exslpfail = 0;
1519 v &= ~LK_SHARED_WAITERS;
1520 if (realexslp != 0) {
1522 "%s: %p has only LK_SLEEPFAIL sleepers",
1525 "%s: %p waking up threads on the exclusive queue",
1534 lk->lk_exslpfail = 0;
1536 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1541 "%s: %p waking up all threads on the %s queue",
1543 "shared" :
"exclusive");
1545 &lk->lock_object, SLEEPQ_LK, 0, queue);
1554 for (v = lk->lk_lock;
1555 (v & LK_SHARE) && !LK_SHARERS(v);
1565 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1566 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1567 x | LK_EXCLUSIVE_WAITERS)) {
1571 LOCK_LOG2(lk,
"%s: %p set drain waiters flag",
1580 if (
flags & LK_INTERLOCK) {
1581 class->lc_unlock(ilk);
1582 flags &= ~LK_INTERLOCK;
1585 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1589 LOCK_LOG2(lk,
"%s: %p resuming from the sleep queue",
1594 lock_profile_obtain_lock_success(&lk->lock_object,
1595 false, contested, waittime, file, line);
1596 LOCK_LOG_LOCK(
"DRAIN", &lk->lock_object, 0,
1597 lk->lk_recurse, file, line);
1598 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1600 TD_LOCKS_INC(curthread);
1605 if (
flags & LK_INTERLOCK)
1606 class->lc_unlock(ilk);
1607 panic(
"%s: unknown lockmgr request 0x%x\n", __func__, op);
1610 if (
flags & LK_INTERLOCK)
1611 class->lc_unlock(ilk);
1623 if (SCHEDULER_STOPPED())
1626 tid = (uintptr_t)curthread;
1633 panic(
"%s: disown a recursed lockmgr @ %s:%d\n",
1634 __func__, file, line);
1639 if (LK_HOLDER(lk->lk_lock) != tid)
1641 lock_profile_release_lock(&lk->lock_object,
false);
1642 LOCKSTAT_RECORD1(lockmgr__disown, lk, LOCKSTAT_WRITER);
1643 LOCK_LOG_LOCK(
"XDISOWN", &lk->lock_object, 0, 0, file, line);
1644 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1645 TD_LOCKS_DEC(curthread);
1652 x = lockmgr_read_value(lk);
1653 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1654 x &= LK_ALL_WAITERS;
1655 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1668 if (lk->lk_lock == LK_UNLOCKED)
1669 printf(
"lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1670 else if (lk->lk_lock & LK_SHARE)
1671 printf(
"lock type %s: SHARED (count %ju)\n",
1672 lk->lock_object.lo_name,
1673 (uintmax_t)LK_SHARERS(lk->lk_lock));
1676 if (td == (
struct thread *)LK_KERNPROC)
1677 printf(
"lock type %s: EXCL by KERNPROC\n",
1678 lk->lock_object.lo_name);
1680 printf(
"lock type %s: EXCL by thread %p "
1681 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name,
1682 td, td->td_proc->p_pid, td->td_proc->p_comm,
1687 if (x & LK_EXCLUSIVE_WAITERS)
1688 printf(
" with exclusive waiters pending\n");
1689 if (x & LK_SHARED_WAITERS)
1690 printf(
" with shared waiters pending\n");
1691 if (x & LK_EXCLUSIVE_SPINNERS)
1692 printf(
" with exclusive spinners pending\n");
1704 x = lockmgr_read_value(lk);
1707 if ((x & LK_SHARE) == 0) {
1708 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1712 }
else if (x == LK_UNLOCKED)
1718#ifdef INVARIANT_SUPPORT
1721 "Support for modules compiled with INVARIANTS option");
1724#undef _lockmgr_assert
1728_lockmgr_assert(
const struct lock *lk,
int what,
const char *file,
int line)
1732 if (KERNEL_PANICKED())
1736 case KA_SLOCKED | KA_NOTRECURSED:
1737 case KA_SLOCKED | KA_RECURSED:
1740 case KA_LOCKED | KA_NOTRECURSED:
1741 case KA_LOCKED | KA_RECURSED:
1750 if (slocked || (lk->lk_lock & LK_SHARE)) {
1755 if (lk->lk_lock == LK_UNLOCKED ||
1756 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1758 panic(
"Lock %s not %slocked @ %s:%d\n",
1759 lk->lock_object.lo_name, slocked ?
"share" :
"",
1762 if ((lk->lk_lock & LK_SHARE) == 0) {
1763 if (lockmgr_recursed(lk)) {
1764 if (what & KA_NOTRECURSED)
1765 panic(
"Lock %s recursed @ %s:%d\n",
1766 lk->lock_object.lo_name, file,
1768 }
else if (what & KA_RECURSED)
1769 panic(
"Lock %s not recursed @ %s:%d\n",
1770 lk->lock_object.lo_name, file, line);
1774 case KA_XLOCKED | KA_NOTRECURSED:
1775 case KA_XLOCKED | KA_RECURSED:
1777 panic(
"Lock %s not exclusively locked @ %s:%d\n",
1778 lk->lock_object.lo_name, file, line);
1779 if (lockmgr_recursed(lk)) {
1780 if (what & KA_NOTRECURSED)
1781 panic(
"Lock %s recursed @ %s:%d\n",
1782 lk->lock_object.lo_name, file, line);
1783 }
else if (what & KA_RECURSED)
1784 panic(
"Lock %s not recursed @ %s:%d\n",
1785 lk->lock_object.lo_name, file, line);
1789 panic(
"Lock %s exclusively locked @ %s:%d\n",
1790 lk->lock_object.lo_name, file, line);
1793 panic(
"Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1801lockmgr_chain(
struct thread *td,
struct thread **ownerp)
1803 const struct lock *lk;
1809 db_printf(
"blocked on lockmgr %s", lk->lock_object.lo_name);
1810 if (lk->lk_lock & LK_SHARE)
1811 db_printf(
"SHARED (count %ju)\n",
1812 (uintmax_t)LK_SHARERS(lk->lk_lock));
1814 db_printf(
"EXCL\n");
1821db_show_lockmgr(
const struct lock_object *lock)
1824 const struct lock *lk;
1826 lk = (
const struct lock *)lock;
1828 db_printf(
" state: ");
1829 if (lk->lk_lock == LK_UNLOCKED)
1830 db_printf(
"UNLOCKED\n");
1831 else if (lk->lk_lock & LK_SHARE)
1832 db_printf(
"SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1835 if (td == (
struct thread *)LK_KERNPROC)
1836 db_printf(
"XLOCK: LK_KERNPROC\n");
1838 db_printf(
"XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1839 td->td_tid, td->td_proc->p_pid,
1840 td->td_proc->p_comm);
1841 if (lockmgr_recursed(lk))
1842 db_printf(
" recursed: %d\n", lk->lk_recurse);
1844 db_printf(
" waiters: ");
1845 switch (lk->lk_lock & LK_ALL_WAITERS) {
1846 case LK_SHARED_WAITERS:
1847 db_printf(
"shared\n");
1849 case LK_EXCLUSIVE_WAITERS:
1850 db_printf(
"exclusive\n");
1852 case LK_ALL_WAITERS:
1853 db_printf(
"shared and exclusive\n");
1856 db_printf(
"none\n");
1858 db_printf(
" spinners: ");
1859 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1860 db_printf(
"exclusive\n");
1862 db_printf(
"none\n");
FEATURE(kdtrace_hooks, "Kernel DTrace hooks which are required to load DTrace kernel modules")
SYSCTL_BOOL(_debug_lockmgr, OID_AUTO, adaptive_spinning, CTLFLAG_RW, &lk_adaptive, 0, "")
void lockdestroy(struct lock *lk)
#define lockmgr_disowned(lk)
int lockstatus(const struct lock *lk)
void lockmgr_printinfo(const struct lock *lk)
static void lockmgr_note_shared_acquire(struct lock *lk, int contested, uint64_t waittime, const char *file, int line, int flags)
static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL, "lockmgr debugging")
static bool __always_inline LK_CAN_SHARE(uintptr_t x, int flags, bool fp)
void lockallowshare(struct lock *lk)
#define SQ_EXCLUSIVE_QUEUE
void lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
static void assert_lockmgr(const struct lock_object *lock, int how)
void lockallowrecurse(struct lock *lk)
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)
static __inline int wakeupshlk(struct lock *lk, const char *file, int line)
#define LK_CAN_WITNESS(x)
static bool lockmgr_slock_adaptive(struct lock_delay_arg *lda, struct lock *lk, uintptr_t *xp, int flags)
static void lockmgr_exit(u_int flags, struct lock_object *ilk, int wakeup_swapper)
void lockdisablerecurse(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)
static void lockmgr_note_shared_release(struct lock *lk, const char *file, int line)
#define lockmgr_xlocked(lk)
static bool lockmgr_xlock_adaptive(struct lock_delay_arg *lda, struct lock *lk, uintptr_t *xp)
static bool __always_inline lockmgr_slock_try(struct lock *lk, uintptr_t *xp, int flags, bool fp)
static __noinline int lockmgr_upgrade(struct lock *lk, u_int flags, struct lock_object *ilk, const char *file, int line, struct lockmgr_wait *lwa)
int lockmgr_xlock(struct lock *lk, u_int flags, const char *file, int line)
#define TD_SLOCKS_DEC(td)
static __noinline int lockmgr_sunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk, const char *file, int line)
int lockmgr_lock_flags(struct lock *lk, u_int flags, struct lock_object *ilk, const char *file, int line)
struct lock_class lock_class_lockmgr
_Static_assert((PRILASTFLAG *2) - 1<=USHRT_MAX, "prio flags wont fit in u_short pri in struct lock")
static bool __always_inline lockmgr_sunlock_try(struct lock *lk, uintptr_t *xp)
static __noinline int lockmgr_xlock_hard(struct lock *lk, u_int flags, struct lock_object *ilk, const char *file, int line, struct lockmgr_wait *lwa)
#define LOCK_LOG2(lk, string, arg1, arg2)
int lockmgr_slock(struct lock *lk, u_int flags, const char *file, int line)
static __read_mostly bool lk_adaptive
static __inline struct thread * lockmgr_xholder(const struct lock *lk)
int lockmgr_unlock(struct lock *lk)
static __noinline int lockmgr_slock_hard(struct lock *lk, u_int flags, struct lock_object *ilk, const char *file, int line, struct lockmgr_wait *lwa)
#define LOCK_LOG3(lk, string, arg1, arg2, arg3)
static void lock_lockmgr(struct lock_object *lock, uintptr_t how)
static void lockmgr_note_exclusive_acquire(struct lock *lk, int contested, uint64_t waittime, const char *file, int line, int flags)
void _lockmgr_disown(struct lock *lk, const char *file, int line)
#define TD_SLOCKS_INC(td)
#define _lockmgr_assert(lk, what, file, line)
static __noinline int lockmgr_xunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk, const char *file, int line)
static void lockmgr_note_exclusive_release(struct lock *lk, const char *file, int line)
void lockdisableshare(struct lock *lk)
CTASSERT(LK_UNLOCKED==(LK_UNLOCKED &~(LK_ALL_WAITERS|LK_EXCLUSIVE_SPINNERS)))
static uintptr_t unlock_lockmgr(struct lock_object *lock)
uint64_t lockstat_nsecs(struct lock_object *lo)
void panic(const char *fmt,...)
static bool kasan_enabled __read_mostly
u_char __read_frequently kdb_active
void lock_destroy(struct lock_object *lock)
void lock_init(struct lock_object *lock, struct lock_class *class, const char *name, const char *type, int flags)
void lock_delay(struct lock_delay_arg *la)
int printf(const char *fmt,...)
void sleepq_release(const void *wchan)
int sleepq_wait_sig(const void *wchan, int pri)
int sleepq_broadcast(const void *wchan, int flags, int pri, int queue)
int sleepq_timedwait_sig(const void *wchan, int pri)
int sleepq_timedwait(const void *wchan, int pri)
u_int sleepq_sleepcnt(const void *wchan, int queue)
void sleepq_add(const void *wchan, struct lock_object *lock, const char *wmesg, int flags, int queue)
void sleepq_wait(const void *wchan, int pri)
void sleepq_lock(const void *wchan)
void witness_assert(const struct lock_object *lock, int flags, const char *file, int line)