Lines Matching +full:lock +full:- +full:- +full:- +full:-

1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
38 * Implementation of the `witness' lock verifier. Originally implemented for
39 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
45 * Pronunciation: 'wit-n&s
59 * life -- Pilot>
64 * Special rules concerning Giant and lock orders:
69 * 2) Giant must be released when blocking on a sleepable lock.
73 * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule
79 * a sleepable lock because it is a non-sleepable lock and non-sleepable
80 * locks may always be acquired while holding a sleepable lock. The second
81 * case, Giant before a sleepable lock, follows from rule 2) above. Suppose
82 * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1
85 * execute. Thus, acquiring Giant both before and after a sleepable lock
86 * will not result in a lock order reversal.
100 #include <sys/lock.h>
128 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
129 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */
130 #define LI_NORELEASE 0x00020000 /* Lock not allowed to be released. */
131 #define LI_SLEEPABLE 0x00040000 /* Lock may be held while sleeping. */
161 #define WITNESS_UNRELATED 0x00 /* No lock order relation. */
170 #define WITNESS_REVERSAL 0x10 /* A lock order reversal has been
174 #define WITNESS_LOCK_ORDER_KNOWN 0x80 /* This lock order is known. */
188 * Lock instances. A lock instance is the data associated with a lock while
189 * it is held by witness. For example, a lock instance will hold the
190 * recursion count of a lock. Lock instances are held in lists. Spin locks
191 * are held in a per-cpu list while sleep locks are held in per-thread list.
203 * lock may be held by more than one thread if it is a shared lock. Locks
207 * when we traverse the list we read children[count-1] as the first entry
217 * The main witness structure. One of these per named lock type in the system
252 * Key type for the lock order data hash table.
266 * The witness lock order data hash table. Keys are witness index tuples
267 * (struct witness_lock_order_key), elements are lock order data objects
292 * Returns 0 if one of the locks is a spin lock and the other is not.
299 return ((w1->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) == in witness_lock_type_equal()
300 (w2->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))); in witness_lock_type_equal()
308 return (a->from == b->from && a->to == b->to); in witness_lock_order_key_equal()
319 const struct lock_object *lock);
362 static void witness_setflag(struct lock_object *lock, int flag, int set);
370 * If set to 0, lock order checking is disabled. If set to -1,
372 * lock order checking for all locks. At runtime, lock order checking
380 "witness is watching lock operations");
386 * - a lock hierarchy violation occurs
387 * - locks are held when going to sleep.
401 * - a lock hierarchy violation occurs
402 * - locks are held when going to sleep.
462 /* lock list */
478 /* The lock order data hash */
503 { "sigio lock", &lock_class_mtx_sleep },
506 { "pmc-sleep", &lock_class_mtx_sleep },
508 { "process lock", &lock_class_mtx_sleep },
511 { "time lock", &lock_class_mtx_sleep },
516 { "umtx lock", &lock_class_mtx_sleep },
578 { "bpf global lock", &lock_class_sx },
579 { "bpf cdev lock", &lock_class_mtx_sleep },
591 { "802.11 com lock", &lock_class_mtx_sleep},
643 { "dn->dn_mtx", &lock_class_sx },
644 { "dr->dt.di.dr_mtx", &lock_class_sx },
645 { "db->db_mtx", &lock_class_sx },
670 { "syscons video lock", &lock_class_mtx_spin },
674 { "turnstile lock", &lock_class_mtx_spin },
675 { "sched lock", &lock_class_mtx_spin },
686 { "sched lock", &lock_class_mtx_spin },
688 { "pmc-per-proc", &lock_class_mtx_spin },
702 { "mprof lock", &lock_class_mtx_spin },
703 { "zombie lock", &lock_class_mtx_spin },
707 { "NDIS thread lock", &lock_class_mtx_spin },
715 { "pmc-leaf", &lock_class_mtx_spin },
717 { "blocked lock", &lock_class_mtx_spin },
724 * order problems with blessed lock pairs. Please do not add an entry to the
729 * See the comment in ufs_dirhash.c. Basically, a vnode lock serializes
730 * both lock orders, so a deadlock cannot happen as a result of this
754 * This global is set to 1 once the static lock orders have been enrolled
788 * The WITNESS-enabled diagnostic code. Note that the witness code does
789 * assume that the early boot is single-threaded at least until after this
795 struct lock_object *lock; in witness_startup() local
822 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | in witness_startup()
824 for (i = witness_count - 1; i >= 0; i--) { in witness_startup()
830 KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, in witness_startup()
835 w_free_cnt--; in witness_startup()
847 for (order = order_lists; order->w_name != NULL; order++) { in witness_startup()
848 w = enroll(order->w_name, order->w_class); in witness_startup()
851 w->w_file = "order list"; in witness_startup()
852 for (order++; order->w_name != NULL; order++) { in witness_startup()
853 w1 = enroll(order->w_name, order->w_class); in witness_startup()
856 w1->w_file = "order list"; in witness_startup()
865 lock = pending_locks[i].wh_lock; in witness_startup()
866 KASSERT(lock->lo_flags & LO_WITNESS, in witness_startup()
867 ("%s: lock %s is on pending list but not LO_WITNESS", in witness_startup()
868 __func__, lock->lo_name)); in witness_startup()
869 lock->lo_witness = enroll(pending_locks[i].wh_type, in witness_startup()
870 LOCK_CLASS(lock)); in witness_startup()
880 witness_init(struct lock_object *lock, const char *type) in witness_init() argument
885 class = LOCK_CLASS(lock); in witness_init()
886 if ((lock->lo_flags & LO_RECURSABLE) != 0 && in witness_init()
887 (class->lc_flags & LC_RECURSABLE) == 0) in witness_init()
888 kassert_panic("%s: lock (%s) %s can not be recursable", in witness_init()
889 __func__, class->lc_name, lock->lo_name); in witness_init()
890 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && in witness_init()
891 (class->lc_flags & LC_SLEEPABLE) == 0) in witness_init()
892 kassert_panic("%s: lock (%s) %s can not be sleepable", in witness_init()
893 __func__, class->lc_name, lock->lo_name); in witness_init()
894 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && in witness_init()
895 (class->lc_flags & LC_UPGRADABLE) == 0) in witness_init()
896 kassert_panic("%s: lock (%s) %s can not be upgradable", in witness_init()
897 __func__, class->lc_name, lock->lo_name); in witness_init()
900 * If we shouldn't watch this lock, then just clear lo_witness. in witness_init()
902 * enroll this lock, so defer it to witness_initialize() by adding in witness_init()
904 * the lock now. in witness_init()
907 (lock->lo_flags & LO_WITNESS) == 0) in witness_init()
908 lock->lo_witness = NULL; in witness_init()
910 pending_locks[pending_cnt].wh_lock = lock; in witness_init()
917 lock->lo_witness = enroll(type, class); in witness_init()
921 witness_destroy(struct lock_object *lock) in witness_destroy() argument
926 class = LOCK_CLASS(lock); in witness_destroy()
929 panic("lock (%s) %s destroyed while witness_cold", in witness_destroy()
930 class->lc_name, lock->lo_name); in witness_destroy()
932 /* XXX: need to verify that no one holds the lock */ in witness_destroy()
933 if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) in witness_destroy()
935 w = lock->lo_witness; in witness_destroy()
938 MPASS(w->w_refcount > 0); in witness_destroy()
939 w->w_refcount--; in witness_destroy()
941 if (w->w_refcount == 0) in witness_destroy()
956 w->w_ddb_level = -1; in witness_ddb_compute_levels()
963 if (w->w_num_ancestors > 0) in witness_ddb_compute_levels()
974 if (w->w_ddb_level >= l) in witness_ddb_level_descendants()
977 w->w_ddb_level = l; in witness_ddb_level_descendants()
981 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) in witness_ddb_level_descendants()
995 w->w_name, w->w_class->lc_name, in witness_ddb_display_descendants()
996 w->w_ddb_level, w->w_refcount); in witness_ddb_display_descendants()
997 if (w->w_displayed) { in witness_ddb_display_descendants()
998 prnt(" -- (already displayed)\n"); in witness_ddb_display_descendants()
1001 w->w_displayed = 1; in witness_ddb_display_descendants()
1002 if (w->w_file != NULL && w->w_line != 0) in witness_ddb_display_descendants()
1003 prnt(" -- last acquired @ %s:%d\n", fixup_filename(w->w_file), in witness_ddb_display_descendants()
1004 w->w_line); in witness_ddb_display_descendants()
1006 prnt(" -- never acquired\n"); in witness_ddb_display_descendants()
1008 WITNESS_INDEX_ASSERT(w->w_index); in witness_ddb_display_descendants()
1012 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) in witness_ddb_display_descendants()
1025 if (w->w_file == NULL || w->w_ddb_level > 0) in witness_ddb_display_list()
1028 /* This lock has no anscestors - display its descendants. */ in witness_ddb_display_list()
1045 w->w_displayed = 0; in witness_ddb_display()
1069 if (w->w_file != NULL || w->w_refcount == 0) in witness_ddb_display()
1071 prnt("%s (type: %s, depth: %d)\n", w->w_name, in witness_ddb_display()
1072 w->w_class->lc_name, w->w_ddb_level); in witness_ddb_display()
1083 if (witness_watch == -1 || KERNEL_PANICKED()) in witness_defineorder()
1087 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || in witness_defineorder()
1088 lock2->lo_witness == NULL) in witness_defineorder()
1095 * If we already have either an explicit or implied lock order that in witness_defineorder()
1099 isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { in witness_defineorder()
1106 lock2->lo_witness->w_name, lock1->lo_witness->w_name); in witness_defineorder()
1107 itismychild(lock1->lo_witness, lock2->lo_witness); in witness_defineorder()
1113 witness_checkorder(struct lock_object *lock, int flags, const char *file, in witness_checkorder() argument
1123 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || in witness_checkorder()
1127 w = lock->lo_witness; in witness_checkorder()
1128 class = LOCK_CLASS(lock); in witness_checkorder()
1131 if (class->lc_flags & LC_SLEEPLOCK) { in witness_checkorder()
1134 * implicitly enforces a lock order of all sleep locks before in witness_checkorder()
1137 if (td->td_critnest != 0 && !kdb_active) in witness_checkorder()
1138 kassert_panic("acquiring blockable sleep lock with " in witness_checkorder()
1140 class->lc_name, lock->lo_name, in witness_checkorder()
1144 * If this is the first lock acquired then just return as in witness_checkorder()
1147 lock_list = td->td_sleeplocks; in witness_checkorder()
1148 if (lock_list == NULL || lock_list->ll_count == 0) in witness_checkorder()
1152 * If this is the first lock, just return as no order in witness_checkorder()
1161 if (lock_list == NULL || lock_list->ll_count == 0) { in witness_checkorder()
1169 * Check to see if we are recursing on a lock we already own. If in witness_checkorder()
1170 * so, make sure that we don't mismatch exclusive and shared lock in witness_checkorder()
1173 lock1 = find_instance(lock_list, lock); in witness_checkorder()
1175 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && in witness_checkorder()
1177 witness_output("shared lock of (%s) %s @ %s:%d\n", in witness_checkorder()
1178 class->lc_name, lock->lo_name, in witness_checkorder()
1181 fixup_filename(lock1->li_file), lock1->li_line); in witness_checkorder()
1182 kassert_panic("excl->share"); in witness_checkorder()
1184 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && in witness_checkorder()
1186 witness_output("exclusive lock of (%s) %s @ %s:%d\n", in witness_checkorder()
1187 class->lc_name, lock->lo_name, in witness_checkorder()
1190 fixup_filename(lock1->li_file), lock1->li_line); in witness_checkorder()
1191 kassert_panic("share->excl"); in witness_checkorder()
1202 iclass->lc_name, interlock->lo_name, in witness_checkorder()
1204 else if ((lock1->li_flags & LI_RECURSEMASK) != 0) in witness_checkorder()
1206 iclass->lc_name, interlock->lo_name, in witness_checkorder()
1211 * Find the previously acquired lock, but ignore interlocks. in witness_checkorder()
1213 plock = &lock_list->ll_children[lock_list->ll_count - 1]; in witness_checkorder()
1214 if (interlock != NULL && plock->li_lock == interlock) { in witness_checkorder()
1215 if (lock_list->ll_count > 1) in witness_checkorder()
1217 &lock_list->ll_children[lock_list->ll_count - 2]; in witness_checkorder()
1219 lle = lock_list->ll_next; in witness_checkorder()
1222 * The interlock is the only lock we hold, so in witness_checkorder()
1227 plock = &lle->ll_children[lle->ll_count - 1]; in witness_checkorder()
1232 * Try to perform most checks without a lock. If this succeeds we in witness_checkorder()
1233 * can skip acquiring the lock and return success. Otherwise we redo in witness_checkorder()
1234 * the check with the lock held to handle races with concurrent updates. in witness_checkorder()
1236 w1 = plock->li_lock->lo_witness; in witness_checkorder()
1249 * have to check for this on the last lock we just acquired. Any in witness_checkorder()
1250 * other cases will be caught as lock order violations. in witness_checkorder()
1253 i = w->w_index; in witness_checkorder()
1254 if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && in witness_checkorder()
1257 w->w_reversed = 1; in witness_checkorder()
1260 "acquiring duplicate lock of same type: \"%s\"\n", in witness_checkorder()
1261 w->w_name); in witness_checkorder()
1262 witness_output(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, in witness_checkorder()
1263 fixup_filename(plock->li_file), plock->li_line); in witness_checkorder()
1264 witness_output(" 2nd %s @ %s:%d\n", lock->lo_name, in witness_checkorder()
1274 * If we know that the lock we are acquiring comes after in witness_checkorder()
1275 * the lock we most recently acquired in the lock order tree, in witness_checkorder()
1281 for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) { in witness_checkorder()
1282 for (i = lle->ll_count - 1; i >= 0; i--, j++) { in witness_checkorder()
1287 lock1 = &lle->ll_children[i]; in witness_checkorder()
1292 if (interlock == lock1->li_lock) in witness_checkorder()
1296 * If this lock doesn't undergo witness checking, in witness_checkorder()
1299 w1 = lock1->li_lock->lo_witness; in witness_checkorder()
1301 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, in witness_checkorder()
1302 ("lock missing witness structure")); in witness_checkorder()
1308 * lock, then skip it. in witness_checkorder()
1310 if ((lock1->li_flags & LI_SLEEPABLE) != 0 && in witness_checkorder()
1311 lock == &Giant.lock_object) in witness_checkorder()
1315 * If we are locking a sleepable lock and this lock in witness_checkorder()
1318 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && in witness_checkorder()
1320 lock1->li_lock == &Giant.lock_object) in witness_checkorder()
1324 * If we are locking a sleepable lock and this lock in witness_checkorder()
1325 * isn't sleepable, we want to treat it as a lock in witness_checkorder()
1326 * order violation to enfore a general lock order of in witness_checkorder()
1327 * sleepable locks before non-sleepable locks. in witness_checkorder()
1329 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && in witness_checkorder()
1331 (lock1->li_flags & LI_SLEEPABLE) == 0) in witness_checkorder()
1335 * If we are locking Giant and this is a non-sleepable in witness_checkorder()
1336 * lock, then treat it as a reversal. in witness_checkorder()
1338 if ((lock1->li_flags & LI_SLEEPABLE) == 0 && in witness_checkorder()
1339 lock == &Giant.lock_object) in witness_checkorder()
1343 * Check the lock order hierarchy for a reveresal. in witness_checkorder()
1350 * We have a lock order violation, check to see if it in witness_checkorder()
1355 if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) in witness_checkorder()
1359 w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; in witness_checkorder()
1360 w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; in witness_checkorder()
1361 w->w_reversed = w1->w_reversed = 1; in witness_checkorder()
1365 * If the lock order is blessed, bail before logging in witness_checkorder()
1366 * anything. We don't look for other lock order in witness_checkorder()
1379 stack_copy(&data->wlod_stack, in witness_checkorder()
1393 if ((lock->lo_flags & LO_IS_VNODE) != 0 && in witness_checkorder()
1394 (lock1->li_lock->lo_flags & LO_IS_VNODE) != 0) in witness_checkorder()
1401 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && in witness_checkorder()
1403 (lock1->li_flags & LI_SLEEPABLE) == 0) in witness_checkorder()
1405 "lock order reversal: (sleepable after non-sleepable)\n"); in witness_checkorder()
1406 else if ((lock1->li_flags & LI_SLEEPABLE) == 0 in witness_checkorder()
1407 && lock == &Giant.lock_object) in witness_checkorder()
1409 "lock order reversal: (Giant after non-sleepable)\n"); in witness_checkorder()
1411 witness_output("lock order reversal:\n"); in witness_checkorder()
1414 * Try to locate an earlier lock with in witness_checkorder()
1418 lock2 = &lle->ll_children[i]; in witness_checkorder()
1419 MPASS(lock2->li_lock != NULL); in witness_checkorder()
1420 if (lock2->li_lock->lo_witness == w) in witness_checkorder()
1422 if (i == 0 && lle->ll_next != NULL) { in witness_checkorder()
1423 lle = lle->ll_next; in witness_checkorder()
1424 i = lle->ll_count - 1; in witness_checkorder()
1427 i--; in witness_checkorder()
1431 lock1->li_lock, lock1->li_lock->lo_name, in witness_checkorder()
1432 w1->w_name, w1->w_class->lc_name, in witness_checkorder()
1433 fixup_filename(lock1->li_file), in witness_checkorder()
1434 lock1->li_line); in witness_checkorder()
1436 lock, lock->lo_name, w->w_name, in witness_checkorder()
1437 w->w_class->lc_name, fixup_filename(file), in witness_checkorder()
1440 struct witness *w2 = lock2->li_lock->lo_witness; in witness_checkorder()
1443 lock2->li_lock, lock2->li_lock->lo_name, in witness_checkorder()
1444 w2->w_name, w2->w_class->lc_name, in witness_checkorder()
1445 fixup_filename(lock2->li_file), in witness_checkorder()
1446 lock2->li_line); in witness_checkorder()
1448 lock1->li_lock, lock1->li_lock->lo_name, in witness_checkorder()
1449 w1->w_name, w1->w_class->lc_name, in witness_checkorder()
1450 fixup_filename(lock1->li_file), in witness_checkorder()
1451 lock1->li_line); in witness_checkorder()
1452 witness_output(" 3rd %p %s (%s, %s) @ %s:%d\n", lock, in witness_checkorder()
1453 lock->lo_name, w->w_name, in witness_checkorder()
1454 w->w_class->lc_name, fixup_filename(file), in witness_checkorder()
1467 "lock order %s -> %s established at:\n", in witness_checkorder()
1468 w->w_name, w1->w_name); in witness_checkorder()
1474 "lock order %s -> %s attempted at:\n", in witness_checkorder()
1475 w1->w_name, w->w_name); in witness_checkorder()
1489 * If requested, build a new lock order. However, don't build a new in witness_checkorder()
1490 * relationship between a sleepable lock and Giant if it is in the in witness_checkorder()
1491 * wrong direction. The correct lock order is that sleepable locks in witness_checkorder()
1495 !(plock->li_lock == &Giant.lock_object && in witness_checkorder()
1496 (lock->lo_flags & LO_SLEEPABLE) != 0 && in witness_checkorder()
1499 w->w_name, plock->li_lock->lo_witness->w_name); in witness_checkorder()
1500 itismychild(plock->li_lock->lo_witness, w); in witness_checkorder()
1507 witness_lock(struct lock_object *lock, int flags, const char *file, int line) in witness_lock() argument
1514 if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || in witness_lock()
1517 w = lock->lo_witness; in witness_lock()
1520 /* Determine lock list for this lock. */ in witness_lock()
1521 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) in witness_lock()
1522 lock_list = &td->td_sleeplocks; in witness_lock()
1526 /* Check to see if we are recursing on a lock we already own. */ in witness_lock()
1527 instance = find_instance(*lock_list, lock); in witness_lock()
1529 instance->li_flags++; in witness_lock()
1531 td->td_proc->p_pid, lock->lo_name, in witness_lock()
1532 instance->li_flags & LI_RECURSEMASK); in witness_lock()
1533 instance->li_file = file; in witness_lock()
1534 instance->li_line = line; in witness_lock()
1538 /* Update per-witness last file and line acquire. */ in witness_lock()
1539 w->w_file = file; in witness_lock()
1540 w->w_line = line; in witness_lock()
1542 /* Find the next open lock instance in the list and fill it. */ in witness_lock()
1544 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { in witness_lock()
1548 lle->ll_next = *lock_list; in witness_lock()
1550 td->td_proc->p_pid, lle); in witness_lock()
1553 instance = &lle->ll_children[lle->ll_count++]; in witness_lock()
1554 instance->li_lock = lock; in witness_lock()
1555 instance->li_line = line; in witness_lock()
1556 instance->li_file = file; in witness_lock()
1557 instance->li_flags = 0; in witness_lock()
1559 instance->li_flags |= LI_EXCLUSIVE; in witness_lock()
1560 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && (flags & LOP_NOSLEEP) == 0) in witness_lock()
1561 instance->li_flags |= LI_SLEEPABLE; in witness_lock()
1563 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); in witness_lock()
1567 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) in witness_upgrade() argument
1573 if (lock->lo_witness == NULL || witness_watch == -1 || KERNEL_PANICKED()) in witness_upgrade()
1575 class = LOCK_CLASS(lock); in witness_upgrade()
1577 if ((lock->lo_flags & LO_UPGRADABLE) == 0) in witness_upgrade()
1579 "upgrade of non-upgradable lock (%s) %s @ %s:%d", in witness_upgrade()
1580 class->lc_name, lock->lo_name, in witness_upgrade()
1582 if ((class->lc_flags & LC_SLEEPLOCK) == 0) in witness_upgrade()
1584 "upgrade of non-sleep lock (%s) %s @ %s:%d", in witness_upgrade()
1585 class->lc_name, lock->lo_name, in witness_upgrade()
1588 instance = find_instance(curthread->td_sleeplocks, lock); in witness_upgrade()
1590 kassert_panic("upgrade of unlocked lock (%s) %s @ %s:%d", in witness_upgrade()
1591 class->lc_name, lock->lo_name, in witness_upgrade()
1596 if ((instance->li_flags & LI_EXCLUSIVE) != 0) in witness_upgrade()
1598 "upgrade of exclusive lock (%s) %s @ %s:%d", in witness_upgrade()
1599 class->lc_name, lock->lo_name, in witness_upgrade()
1601 if ((instance->li_flags & LI_RECURSEMASK) != 0) in witness_upgrade()
1603 "upgrade of recursed lock (%s) %s r=%d @ %s:%d", in witness_upgrade()
1604 class->lc_name, lock->lo_name, in witness_upgrade()
1605 instance->li_flags & LI_RECURSEMASK, in witness_upgrade()
1608 instance->li_flags |= LI_EXCLUSIVE; in witness_upgrade()
1612 witness_downgrade(struct lock_object *lock, int flags, const char *file, in witness_downgrade() argument
1619 if (lock->lo_witness == NULL || witness_watch == -1 || KERNEL_PANICKED()) in witness_downgrade()
1621 class = LOCK_CLASS(lock); in witness_downgrade()
1623 if ((lock->lo_flags & LO_UPGRADABLE) == 0) in witness_downgrade()
1625 "downgrade of non-upgradable lock (%s) %s @ %s:%d", in witness_downgrade()
1626 class->lc_name, lock->lo_name, in witness_downgrade()
1628 if ((class->lc_flags & LC_SLEEPLOCK) == 0) in witness_downgrade()
1630 "downgrade of non-sleep lock (%s) %s @ %s:%d", in witness_downgrade()
1631 class->lc_name, lock->lo_name, in witness_downgrade()
1634 instance = find_instance(curthread->td_sleeplocks, lock); in witness_downgrade()
1636 kassert_panic("downgrade of unlocked lock (%s) %s @ %s:%d", in witness_downgrade()
1637 class->lc_name, lock->lo_name, in witness_downgrade()
1642 if ((instance->li_flags & LI_EXCLUSIVE) == 0) in witness_downgrade()
1644 "downgrade of shared lock (%s) %s @ %s:%d", in witness_downgrade()
1645 class->lc_name, lock->lo_name, in witness_downgrade()
1647 if ((instance->li_flags & LI_RECURSEMASK) != 0) in witness_downgrade()
1649 "downgrade of recursed lock (%s) %s r=%d @ %s:%d", in witness_downgrade()
1650 class->lc_name, lock->lo_name, in witness_downgrade()
1651 instance->li_flags & LI_RECURSEMASK, in witness_downgrade()
1654 instance->li_flags &= ~LI_EXCLUSIVE; in witness_downgrade()
1658 witness_unlock(struct lock_object *lock, int flags, const char *file, int line) in witness_unlock() argument
1667 if (witness_cold || lock->lo_witness == NULL || KERNEL_PANICKED()) in witness_unlock()
1670 class = LOCK_CLASS(lock); in witness_unlock()
1672 /* Find lock instance associated with this lock. */ in witness_unlock()
1673 if (class->lc_flags & LC_SLEEPLOCK) in witness_unlock()
1674 lock_list = &td->td_sleeplocks; in witness_unlock()
1678 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) in witness_unlock()
1679 for (i = 0; i < (*lock_list)->ll_count; i++) { in witness_unlock()
1680 instance = &(*lock_list)->ll_children[i]; in witness_unlock()
1681 if (instance->li_lock == lock) in witness_unlock()
1692 kassert_panic("lock (%s) %s not locked @ %s:%d", class->lc_name, in witness_unlock()
1693 lock->lo_name, fixup_filename(file), line); in witness_unlock()
1701 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && in witness_unlock()
1704 class->lc_name, lock->lo_name, fixup_filename(file), line); in witness_unlock()
1706 fixup_filename(instance->li_file), instance->li_line); in witness_unlock()
1707 kassert_panic("excl->ushare"); in witness_unlock()
1709 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && in witness_unlock()
1712 class->lc_name, lock->lo_name, fixup_filename(file), line); in witness_unlock()
1714 fixup_filename(instance->li_file), in witness_unlock()
1715 instance->li_line); in witness_unlock()
1716 kassert_panic("share->uexcl"); in witness_unlock()
1719 if ((instance->li_flags & LI_RECURSEMASK) > 0) { in witness_unlock()
1721 td->td_proc->p_pid, instance->li_lock->lo_name, in witness_unlock()
1722 instance->li_flags); in witness_unlock()
1723 instance->li_flags--; in witness_unlock()
1726 /* The lock is now being dropped, check for NORELEASE flag */ in witness_unlock()
1727 if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) { in witness_unlock()
1729 class->lc_name, lock->lo_name, fixup_filename(file), line); in witness_unlock()
1730 kassert_panic("lock marked norelease"); in witness_unlock()
1736 td->td_proc->p_pid, instance->li_lock->lo_name, in witness_unlock()
1737 (*lock_list)->ll_count - 1); in witness_unlock()
1738 for (j = i; j < (*lock_list)->ll_count - 1; j++) in witness_unlock()
1739 (*lock_list)->ll_children[j] = in witness_unlock()
1740 (*lock_list)->ll_children[j + 1]; in witness_unlock()
1741 (*lock_list)->ll_count--; in witness_unlock()
1753 if ((*lock_list)->ll_count == 0) { in witness_unlock()
1755 if (lle->ll_next == NULL) in witness_unlock()
1759 *lock_list = lle->ll_next; in witness_unlock()
1761 td->td_proc->p_pid, lle); in witness_unlock()
1772 lle = td->td_sleeplocks; in witness_thread_exit()
1775 if (lle->ll_count != 0) { in witness_thread_exit()
1776 for (n = 0; lle != NULL; lle = lle->ll_next) in witness_thread_exit()
1777 for (i = lle->ll_count - 1; i >= 0; i--) { in witness_thread_exit()
1782 witness_list_lock(&lle->ll_children[i], in witness_thread_exit()
1793 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1795 * non-exempt locks are held, then a supplied message is printed to the
1800 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) in witness_warn() argument
1812 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) in witness_warn()
1813 for (i = lle->ll_count - 1; i >= 0; i--) { in witness_warn()
1814 lock1 = &lle->ll_children[i]; in witness_warn()
1815 if (lock1->li_lock == lock) in witness_warn()
1818 lock1->li_lock == &Giant.lock_object) in witness_warn()
1821 (lock1->li_flags & LI_SLEEPABLE) != 0) in witness_warn()
1829 "non-sleepable " : ""); in witness_warn()
1842 if (lock_list != NULL && lock_list->ll_count != 0) { in witness_warn()
1851 lock1 = &lock_list->ll_children[lock_list->ll_count - 1]; in witness_warn()
1852 if (lock_list->ll_count == 1 && lock_list->ll_next == NULL && in witness_warn()
1853 lock1->li_lock == lock && n == 0) in witness_warn()
1860 (flags & WARN_SLEEPOK) != 0 ? "non-sleepable " : ""); in witness_warn()
1872 witness_file(struct lock_object *lock) in witness_file() argument
1876 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) in witness_file()
1878 w = lock->lo_witness; in witness_file()
1879 return (w->w_file); in witness_file()
1883 witness_line(struct lock_object *lock) in witness_line() argument
1887 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) in witness_line()
1889 w = lock->lo_witness; in witness_line()
1890 return (w->w_line); in witness_line()
1900 if (witness_watch == -1 || KERNEL_PANICKED()) in enroll()
1902 if ((lock_class->lc_flags & LC_SPINLOCK)) { in enroll()
1905 } else if ((lock_class->lc_flags & LC_SLEEPLOCK) == 0) { in enroll()
1906 kassert_panic("lock class %s is not sleep or spin", in enroll()
1907 lock_class->lc_name); in enroll()
1918 strcpy(w->w_name, description); in enroll()
1919 w->w_class = lock_class; in enroll()
1920 w->w_refcount = 1; in enroll()
1922 if (lock_class->lc_flags & LC_SPINLOCK) { in enroll()
1925 } else if (lock_class->lc_flags & LC_SLEEPLOCK) { in enroll()
1936 w->w_refcount++; in enroll()
1937 if (w->w_refcount == 1) in enroll()
1938 w->w_class = lock_class; in enroll()
1940 if (lock_class != w->w_class) in enroll()
1942 "lock (%s) %s does not match earlier (%s) lock", in enroll()
1943 description, lock_class->lc_name, in enroll()
1944 w->w_class->lc_name); in enroll()
1952 MPASS(w->w_refcount == 0); in depart()
1953 if (w->w_class->lc_flags & LC_SLEEPLOCK) { in depart()
1954 w_sleep_cnt--; in depart()
1956 w_spin_cnt--; in depart()
1961 w->w_file = NULL; in depart()
1962 w->w_line = 0; in depart()
1985 pi = parent->w_index; in adopt()
1986 ci = child->w_index; in adopt()
1998 parent->w_num_descendants++; in adopt()
1999 child->w_num_ancestors++; in adopt()
2035 * this as a lock order reversal earlier. in adopt()
2044 witness_watch = -1; in adopt()
2053 witness_watch = -1; in adopt()
2077 "the same lock type", __func__, parent->w_name, in itismychild()
2078 parent->w_class->lc_name, child->w_name, in itismychild()
2079 child->w_class->lc_name); in itismychild()
2096 i1 = w1->w_index; in _isitmyx()
2097 i2 = w2->w_index; in _isitmyx()
2112 fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, in _isitmyx()
2116 witness_watch = -1; in _isitmyx()
2150 if (strcmp(w1->w_name, b->b_lock1) == 0) { in blessed()
2151 if (strcmp(w2->w_name, b->b_lock2) == 0) in blessed()
2155 if (strcmp(w1->w_name, b->b_lock2) == 0) in blessed()
2156 if (strcmp(w2->w_name, b->b_lock1) == 0) in blessed()
2171 if (witness_watch == -1) { in witness_get()
2176 witness_watch = -1; in witness_get()
2183 w_free_cnt--; in witness_get()
2184 index = w->w_index; in witness_get()
2188 w->w_index = index; in witness_get()
2207 if (witness_watch == -1) in witness_lock_list_get()
2212 witness_watch = -1; in witness_lock_list_get()
2217 w_lock_list_free = lle->ll_next; in witness_lock_list_get()
2228 lle->ll_next = w_lock_list_free; in witness_lock_list_free()
2234 find_instance(struct lock_list_entry *list, const struct lock_object *lock) in find_instance() argument
2240 for (lle = list; lle != NULL; lle = lle->ll_next) in find_instance()
2241 for (i = lle->ll_count - 1; i >= 0; i--) { in find_instance()
2242 instance = &lle->ll_children[i]; in find_instance()
2243 if (instance->li_lock == lock) in find_instance()
2253 struct lock_object *lock; in witness_list_lock() local
2255 lock = instance->li_lock; in witness_list_lock()
2256 prnt("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? in witness_list_lock()
2257 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); in witness_list_lock()
2258 if (lock->lo_witness->w_name != lock->lo_name) in witness_list_lock()
2259 prnt(" (%s)", lock->lo_witness->w_name); in witness_list_lock()
2261 instance->li_flags & LI_RECURSEMASK, lock, in witness_list_lock()
2262 fixup_filename(instance->li_file), instance->li_line); in witness_list_lock()
2301 if (td->td_sleeplocks == NULL) in witness_thread_has_locks()
2303 return (td->td_sleeplocks->ll_count != 0); in witness_thread_has_locks()
2327 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) in witness_list_locks()
2328 for (i = lle->ll_count - 1; i >= 0; i--) { in witness_list_locks()
2329 witness_list_lock(&lle->ll_children[i], prnt); in witness_list_locks()
2337 * out acquiring a spin lock, and we assume that the other CPU is stuck
2338 * with this lock held. So, we go groveling around in the other CPU's
2339 * per-cpu data to try to find the lock instance for this spin lock to
2343 witness_display_spinlock(struct lock_object *lock, struct thread *owner, in witness_display_spinlock() argument
2349 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) in witness_display_spinlock()
2351 pc = pcpu_find(owner->td_oncpu); in witness_display_spinlock()
2352 instance = find_instance(pc->pc_spinlocks, lock); in witness_display_spinlock()
2358 witness_save(struct lock_object *lock, const char **filep, int *linep) in witness_save() argument
2376 if (lock->lo_witness == NULL || witness_watch == -1 || KERNEL_PANICKED()) in witness_save()
2378 class = LOCK_CLASS(lock); in witness_save()
2379 if (class->lc_flags & LC_SLEEPLOCK) in witness_save()
2380 lock_list = curthread->td_sleeplocks; in witness_save()
2386 instance = find_instance(lock_list, lock); in witness_save()
2388 kassert_panic("%s: lock (%s) %s not locked", __func__, in witness_save()
2389 class->lc_name, lock->lo_name); in witness_save()
2392 *filep = instance->li_file; in witness_save()
2393 *linep = instance->li_line; in witness_save()
2397 witness_restore(struct lock_object *lock, const char *file, int line) in witness_restore() argument
2411 if (lock->lo_witness == NULL || witness_watch == -1 || KERNEL_PANICKED()) in witness_restore()
2413 class = LOCK_CLASS(lock); in witness_restore()
2414 if (class->lc_flags & LC_SLEEPLOCK) in witness_restore()
2415 lock_list = curthread->td_sleeplocks; in witness_restore()
2421 instance = find_instance(lock_list, lock); in witness_restore()
2423 kassert_panic("%s: lock (%s) %s not locked", __func__, in witness_restore()
2424 class->lc_name, lock->lo_name); in witness_restore()
2425 lock->lo_witness->w_file = file; in witness_restore()
2426 lock->lo_witness->w_line = line; in witness_restore()
2429 instance->li_file = file; in witness_restore()
2430 instance->li_line = line; in witness_restore()
2434 witness_find_instance(const struct lock_object *lock, in witness_find_instance() argument
2440 if (lock->lo_witness == NULL || witness_watch < 1 || KERNEL_PANICKED()) in witness_find_instance()
2442 class = LOCK_CLASS(lock); in witness_find_instance()
2443 if ((class->lc_flags & LC_SLEEPLOCK) != 0) { in witness_find_instance()
2444 *instance = find_instance(curthread->td_sleeplocks, lock); in witness_find_instance()
2446 } else if ((class->lc_flags & LC_SPINLOCK) != 0) { in witness_find_instance()
2447 *instance = find_instance(PCPU_GET(spinlocks), lock); in witness_find_instance()
2450 kassert_panic("Lock (%s) %s is not sleep or spin!", in witness_find_instance()
2451 class->lc_name, lock->lo_name); in witness_find_instance()
2460 witness_assert(const struct lock_object *lock, int flags, const char *file, in witness_assert() argument
2467 if (!witness_find_instance(lock, &instance)) in witness_assert()
2469 class = LOCK_CLASS(lock); in witness_assert()
2473 kassert_panic("Lock (%s) %s locked @ %s:%d.", in witness_assert()
2474 class->lc_name, lock->lo_name, in witness_assert()
2487 kassert_panic("Lock (%s) %s not locked @ %s:%d.", in witness_assert()
2488 class->lc_name, lock->lo_name, in witness_assert()
2493 (instance->li_flags & LI_EXCLUSIVE) == 0) in witness_assert()
2495 "Lock (%s) %s not exclusively locked @ %s:%d.", in witness_assert()
2496 class->lc_name, lock->lo_name, in witness_assert()
2499 (instance->li_flags & LI_EXCLUSIVE) != 0) in witness_assert()
2501 "Lock (%s) %s exclusively locked @ %s:%d.", in witness_assert()
2502 class->lc_name, lock->lo_name, in witness_assert()
2505 (instance->li_flags & LI_RECURSEMASK) == 0) in witness_assert()
2506 kassert_panic("Lock (%s) %s not recursed @ %s:%d.", in witness_assert()
2507 class->lc_name, lock->lo_name, in witness_assert()
2510 (instance->li_flags & LI_RECURSEMASK) != 0) in witness_assert()
2511 kassert_panic("Lock (%s) %s recursed @ %s:%d.", in witness_assert()
2512 class->lc_name, lock->lo_name, in witness_assert()
2516 kassert_panic("Invalid lock assertion at %s:%d.", in witness_assert()
2523 * Checks the ownership of the lock by curthread, consulting the witness list.
2526 * -1 if not owned
2530 witness_is_owned(const struct lock_object *lock) in witness_is_owned() argument
2535 if (!witness_find_instance(lock, &instance)) in witness_is_owned()
2537 return (instance == NULL ? -1 : 1); in witness_is_owned()
2544 witness_setflag(struct lock_object *lock, int flag, int set) in witness_setflag() argument
2550 if (lock->lo_witness == NULL || witness_watch == -1 || KERNEL_PANICKED()) in witness_setflag()
2552 class = LOCK_CLASS(lock); in witness_setflag()
2553 if (class->lc_flags & LC_SLEEPLOCK) in witness_setflag()
2554 lock_list = curthread->td_sleeplocks; in witness_setflag()
2560 instance = find_instance(lock_list, lock); in witness_setflag()
2562 kassert_panic("%s: lock (%s) %s not locked", __func__, in witness_setflag()
2563 class->lc_name, lock->lo_name); in witness_setflag()
2568 instance->li_flags |= flag; in witness_setflag()
2570 instance->li_flags &= ~flag; in witness_setflag()
2574 witness_norelease(struct lock_object *lock) in witness_norelease() argument
2577 witness_setflag(lock, LI_NORELEASE, 1); in witness_norelease()
2581 witness_releaseok(struct lock_object *lock) in witness_releaseok() argument
2584 witness_setflag(lock, LI_NORELEASE, 0); in witness_releaseok()
2598 witness_list_locks(&td->td_sleeplocks, db_printf); in witness_ddb_list()
2604 * the per-cpu data for a given cpu then we could use in witness_ddb_list()
2605 * td->td_oncpu to get the list of spinlocks for this thread in witness_ddb_list()
2609 * lock or stopped the other CPU to make sure it wasn't changing the in witness_ddb_list()
2644 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, in DB_SHOW_ALL_COMMAND()
2645 p->p_comm, td, td->td_tid); in DB_SHOW_ALL_COMMAND()
2680 stack_zero(&tmp_data1->wlod_stack); in sbuf_print_witness_badstacks()
2681 stack_zero(&tmp_data2->wlod_stack); in sbuf_print_witness_badstacks()
2701 if (w1->w_reversed == 0) { in sbuf_print_witness_badstacks()
2706 /* Copy w1 locally so we can release the spin lock. */ in sbuf_print_witness_badstacks()
2710 if (tmp_w1->w_reversed == 0) in sbuf_print_witness_badstacks()
2732 * spin lock. in sbuf_print_witness_badstacks()
2737 stack_zero(&tmp_data1->wlod_stack); in sbuf_print_witness_badstacks()
2738 stack_copy(&data1->wlod_stack, in sbuf_print_witness_badstacks()
2739 &tmp_data1->wlod_stack); in sbuf_print_witness_badstacks()
2742 stack_zero(&tmp_data2->wlod_stack); in sbuf_print_witness_badstacks()
2743 stack_copy(&data2->wlod_stack, in sbuf_print_witness_badstacks()
2744 &tmp_data2->wlod_stack); in sbuf_print_witness_badstacks()
2753 tmp_w1->w_name, tmp_w1->w_class->lc_name, in sbuf_print_witness_badstacks()
2754 tmp_w2->w_name, tmp_w2->w_class->lc_name); in sbuf_print_witness_badstacks()
2757 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", in sbuf_print_witness_badstacks()
2758 tmp_w1->w_name, tmp_w1->w_class->lc_name, in sbuf_print_witness_badstacks()
2759 tmp_w2->w_name, tmp_w2->w_class->lc_name); in sbuf_print_witness_badstacks()
2760 stack_sbuf_print(sb, &tmp_data1->wlod_stack); in sbuf_print_witness_badstacks()
2765 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", in sbuf_print_witness_badstacks()
2766 tmp_w2->w_name, tmp_w2->w_class->lc_name, in sbuf_print_witness_badstacks()
2767 tmp_w1->w_name, tmp_w1->w_class->lc_name); in sbuf_print_witness_badstacks()
2768 stack_sbuf_print(sb, &tmp_data2->wlod_stack); in sbuf_print_witness_badstacks()
2813 sbuf_print_witness_badstacks(sb, &req->oldidx); in sysctl_debug_witness_badstacks()
2866 if (error != 0 || req->newptr == NULL) in sysctl_debug_witness_channel()
2911 w->w_displayed = 0; in sysctl_debug_witness_fullgraph()
2932 if (error != 0 || req->newptr == NULL) in sysctl_debug_witness_watch()
2934 if (value > 1 || value < -1 || in sysctl_debug_witness_watch()
2935 (witness_watch == -1 && value != witness_watch)) in sysctl_debug_witness_watch()
2946 if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) in witness_add_fullgraph()
2948 w->w_displayed = 1; in witness_add_fullgraph()
2950 WITNESS_INDEX_ASSERT(w->w_index); in witness_add_fullgraph()
2952 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { in witness_add_fullgraph()
2953 sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, in witness_add_fullgraph()
2963 * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit
3001 /* Initialize the lock order data hash. */ in witness_init_hash_tables()
3026 if (strcmp(w->w_name, key) == 0) in witness_hash_get()
3028 w = w->w_hash_next; in witness_hash_get()
3041 MPASS(w->w_name != NULL); in witness_hash_put()
3044 KASSERT(witness_hash_get(w->w_name) == NULL, in witness_hash_put()
3046 KASSERT(w->w_hash_next == NULL, in witness_hash_put()
3047 ("%s: w->w_hash_next != NULL", __func__)); in witness_hash_put()
3049 hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; in witness_hash_put()
3050 w->w_hash_next = w_hash.wh_array[hash]; in witness_hash_put()
3063 key.from = parent->w_index; in witness_lock_order_get()
3064 key.to = child->w_index; in witness_lock_order_get()
3067 if ((w_rmatrix[parent->w_index][child->w_index] in witness_lock_order_get()
3075 if (witness_lock_order_key_equal(&data->wlod_key, &key)) in witness_lock_order_get()
3077 data = data->wlod_next; in witness_lock_order_get()
3094 w_rmatrix[parent->w_index][child->w_index] in witness_lock_order_check()
3110 key.from = parent->w_index; in witness_lock_order_add()
3111 key.to = child->w_index; in witness_lock_order_add()
3114 if (w_rmatrix[parent->w_index][child->w_index] in witness_lock_order_add()
3120 w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; in witness_lock_order_add()
3124 w_lofree = data->wlod_next; in witness_lock_order_add()
3125 data->wlod_next = w_lohash.wloh_array[hash]; in witness_lock_order_add()
3126 data->wlod_key = key; in witness_lock_order_add()
3129 stack_save(&data->wlod_stack); in witness_lock_order_add()