2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice(s), this list of conditions and the following disclaimer as
12 * the first lines of this file unmodified other than the possible
13 * addition of one or more copyright notices.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice(s), this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32 #include "opt_hwpmc_hooks.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
41 #include <sys/lock_profile.h>
42 #include <sys/lockmgr.h>
43 #include <sys/lockstat.h>
44 #include <sys/mutex.h>
46 #include <sys/sleepqueue.h>
48 #include <sys/stack.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
53 #include <machine/cpu.h>
60 #include <sys/pmckern.h>
61 PMC_SOFT_DECLARE( , , lock, failed);
64 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
65 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
67 #define SQ_EXCLUSIVE_QUEUE 0
68 #define SQ_SHARED_QUEUE 1
71 #define _lockmgr_assert(lk, what, file, line)
74 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
75 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
78 #define STACK_PRINT(lk)
79 #define STACK_SAVE(lk)
80 #define STACK_ZERO(lk)
82 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
83 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
84 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
87 #define LOCK_LOG2(lk, string, arg1, arg2) \
88 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
89 CTR2(KTR_LOCK, (string), (arg1), (arg2))
90 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
91 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
92 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
94 #define GIANT_DECLARE \
96 WITNESS_SAVE_DECL(Giant)
97 #define GIANT_RESTORE() do { \
98 if (__predict_false(_i > 0)) { \
101 WITNESS_RESTORE(&Giant.lock_object, Giant); \
104 #define GIANT_SAVE() do { \
105 if (__predict_false(mtx_owned(&Giant))) { \
106 WITNESS_SAVE(&Giant.lock_object, Giant); \
107 while (mtx_owned(&Giant)) { \
109 mtx_unlock(&Giant); \
114 static bool __always_inline
115 LK_CAN_SHARE(uintptr_t x, int flags, bool fp)
118 if ((x & (LK_SHARE | LK_EXCLUSIVE_WAITERS | LK_EXCLUSIVE_SPINNERS)) ==
121 if (fp || (!(x & LK_SHARE)))
123 if ((curthread->td_lk_slocks != 0 && !(flags & LK_NODDLKTREAT)) ||
124 (curthread->td_pflags & TDP_DEADLKTREAT))
129 #define LK_TRYOP(x) \
132 #define LK_CAN_WITNESS(x) \
133 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
134 #define LK_TRYWIT(x) \
135 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
137 #define lockmgr_disowned(lk) \
138 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
140 #define lockmgr_xlocked_v(v) \
141 (((v) & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
143 #define lockmgr_xlocked(lk) lockmgr_xlocked_v(lockmgr_read_value(lk))
145 static void assert_lockmgr(const struct lock_object *lock, int how);
147 static void db_show_lockmgr(const struct lock_object *lock);
149 static void lock_lockmgr(struct lock_object *lock, uintptr_t how);
151 static int owner_lockmgr(const struct lock_object *lock,
152 struct thread **owner);
154 static uintptr_t unlock_lockmgr(struct lock_object *lock);
156 struct lock_class lock_class_lockmgr = {
157 .lc_name = "lockmgr",
158 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
159 .lc_assert = assert_lockmgr,
161 .lc_ddb_show = db_show_lockmgr,
163 .lc_lock = lock_lockmgr,
164 .lc_unlock = unlock_lockmgr,
166 .lc_owner = owner_lockmgr,
170 static __read_mostly bool lk_adaptive = true;
171 static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL, "lockmgr debugging");
172 SYSCTL_BOOL(_debug_lockmgr, OID_AUTO, adaptive_spinning, CTLFLAG_RW, &lk_adaptive,
174 #define lockmgr_delay locks_delay
176 struct lockmgr_wait {
182 static bool __always_inline lockmgr_slock_try(struct lock *lk, uintptr_t *xp,
184 static bool __always_inline lockmgr_sunlock_try(struct lock *lk, uintptr_t *xp);
187 lockmgr_exit(u_int flags, struct lock_object *ilk, int wakeup_swapper)
189 struct lock_class *class;
191 if (flags & LK_INTERLOCK) {
192 class = LOCK_CLASS(ilk);
193 class->lc_unlock(ilk);
196 if (__predict_false(wakeup_swapper))
201 lockmgr_note_shared_acquire(struct lock *lk, int contested,
202 uint64_t waittime, const char *file, int line, int flags)
205 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(lockmgr__acquire, lk, contested,
206 waittime, file, line, LOCKSTAT_READER);
207 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file, line);
208 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file, line);
209 TD_LOCKS_INC(curthread);
210 TD_SLOCKS_INC(curthread);
215 lockmgr_note_shared_release(struct lock *lk, const char *file, int line)
218 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
219 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
220 TD_LOCKS_DEC(curthread);
221 TD_SLOCKS_DEC(curthread);
225 lockmgr_note_exclusive_acquire(struct lock *lk, int contested,
226 uint64_t waittime, const char *file, int line, int flags)
229 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(lockmgr__acquire, lk, contested,
230 waittime, file, line, LOCKSTAT_WRITER);
231 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0, lk->lk_recurse, file, line);
232 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE | LK_TRYWIT(flags), file,
234 TD_LOCKS_INC(curthread);
239 lockmgr_note_exclusive_release(struct lock *lk, const char *file, int line)
242 if (LK_HOLDER(lockmgr_read_value(lk)) != LK_KERNPROC) {
243 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
244 TD_LOCKS_DEC(curthread);
246 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0, lk->lk_recurse, file,
250 static __inline struct thread *
251 lockmgr_xholder(const struct lock *lk)
255 x = lockmgr_read_value(lk);
256 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
260 * It assumes sleepq_lock held and returns with this one unheld.
261 * It also assumes the generic interlock is sane and previously checked.
262 * If LK_INTERLOCK is specified the interlock is not reacquired after the
266 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
267 const char *wmesg, int pri, int timo, int queue)
270 struct lock_class *class;
273 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
274 catch = pri & PCATCH;
278 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
279 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
281 if (flags & LK_INTERLOCK)
282 class->lc_unlock(ilk);
283 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
286 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
287 SLEEPQ_INTERRUPTIBLE : 0), queue);
288 if ((flags & LK_TIMELOCK) && timo)
289 sleepq_set_timeout(&lk->lock_object, timo);
292 * Decisional switch for real sleeping.
294 if ((flags & LK_TIMELOCK) && timo && catch)
295 error = sleepq_timedwait_sig(&lk->lock_object, pri);
296 else if ((flags & LK_TIMELOCK) && timo)
297 error = sleepq_timedwait(&lk->lock_object, pri);
299 error = sleepq_wait_sig(&lk->lock_object, pri);
301 sleepq_wait(&lk->lock_object, pri);
303 if ((flags & LK_SLEEPFAIL) && error == 0)
310 wakeupshlk(struct lock *lk, const char *file, int line)
312 uintptr_t v, x, orig_x;
314 int queue, wakeup_swapper;
318 x = lockmgr_read_value(lk);
319 if (lockmgr_sunlock_try(lk, &x))
323 * We should have a sharer with waiters, so enter the hard
324 * path in order to handle wakeups correctly.
326 sleepq_lock(&lk->lock_object);
327 orig_x = lockmgr_read_value(lk);
329 x = orig_x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
333 * If the lock has exclusive waiters, give them preference in
334 * order to avoid deadlock with shared runners up.
335 * If interruptible sleeps left the exclusive queue empty
336 * avoid a starvation for the threads sleeping on the shared
337 * queue by giving them precedence and cleaning up the
338 * exclusive waiters bit anyway.
339 * Please note that lk_exslpfail count may be lying about
340 * the real number of waiters with the LK_SLEEPFAIL flag on
341 * because they may be used in conjunction with interruptible
342 * sleeps so lk_exslpfail might be considered an 'upper limit'
343 * bound, including the edge cases.
345 realexslp = sleepq_sleepcnt(&lk->lock_object,
347 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
348 if (lk->lk_exslpfail < realexslp) {
349 lk->lk_exslpfail = 0;
350 queue = SQ_EXCLUSIVE_QUEUE;
351 v |= (x & LK_SHARED_WAITERS);
353 lk->lk_exslpfail = 0;
355 "%s: %p has only LK_SLEEPFAIL sleepers",
358 "%s: %p waking up threads on the exclusive queue",
361 sleepq_broadcast(&lk->lock_object,
362 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
363 queue = SQ_SHARED_QUEUE;
369 * Exclusive waiters sleeping with LK_SLEEPFAIL on
370 * and using interruptible sleeps/timeout may have
371 * left spourious lk_exslpfail counts on, so clean
374 lk->lk_exslpfail = 0;
375 queue = SQ_SHARED_QUEUE;
378 if (lockmgr_sunlock_try(lk, &orig_x)) {
379 sleepq_release(&lk->lock_object);
383 x |= LK_SHARERS_LOCK(1);
384 if (!atomic_fcmpset_rel_ptr(&lk->lk_lock, &x, v)) {
388 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
389 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
391 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
393 sleepq_release(&lk->lock_object);
397 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk, LOCKSTAT_READER);
398 return (wakeup_swapper);
402 assert_lockmgr(const struct lock_object *lock, int what)
405 panic("lockmgr locks do not support assertions");
409 lock_lockmgr(struct lock_object *lock, uintptr_t how)
412 panic("lockmgr locks do not support sleep interlocking");
416 unlock_lockmgr(struct lock_object *lock)
419 panic("lockmgr locks do not support sleep interlocking");
424 owner_lockmgr(const struct lock_object *lock, struct thread **owner)
427 panic("lockmgr locks do not support owner inquiring");
432 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
436 MPASS((flags & ~LK_INIT_MASK) == 0);
437 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
438 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
441 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
442 if (flags & LK_CANRECURSE)
443 iflags |= LO_RECURSABLE;
444 if ((flags & LK_NODUP) == 0)
446 if (flags & LK_NOPROFILE)
447 iflags |= LO_NOPROFILE;
448 if ((flags & LK_NOWITNESS) == 0)
449 iflags |= LO_WITNESS;
450 if (flags & LK_QUIET)
452 if (flags & LK_IS_VNODE)
453 iflags |= LO_IS_VNODE;
456 iflags |= flags & LK_NOSHARE;
458 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
459 lk->lk_lock = LK_UNLOCKED;
461 lk->lk_exslpfail = 0;
468 * XXX: Gross hacks to manipulate external lock flags after
469 * initialization. Used for certain vnode and buf locks.
472 lockallowshare(struct lock *lk)
475 lockmgr_assert(lk, KA_XLOCKED);
476 lk->lock_object.lo_flags &= ~LK_NOSHARE;
480 lockdisableshare(struct lock *lk)
483 lockmgr_assert(lk, KA_XLOCKED);
484 lk->lock_object.lo_flags |= LK_NOSHARE;
488 lockallowrecurse(struct lock *lk)
491 lockmgr_assert(lk, KA_XLOCKED);
492 lk->lock_object.lo_flags |= LO_RECURSABLE;
496 lockdisablerecurse(struct lock *lk)
499 lockmgr_assert(lk, KA_XLOCKED);
500 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
504 lockdestroy(struct lock *lk)
507 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
508 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
509 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
510 lock_destroy(&lk->lock_object);
513 static bool __always_inline
514 lockmgr_slock_try(struct lock *lk, uintptr_t *xp, int flags, bool fp)
518 * If no other thread has an exclusive lock, or
519 * no exclusive waiter is present, bump the count of
520 * sharers. Since we have to preserve the state of
521 * waiters, if we fail to acquire the shared lock
522 * loop back and retry.
524 while (LK_CAN_SHARE(*xp, flags, fp)) {
525 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, xp,
526 *xp + LK_ONE_SHARER)) {
533 static bool __always_inline
534 lockmgr_sunlock_try(struct lock *lk, uintptr_t *xp)
538 if (LK_SHARERS(*xp) > 1 || !(*xp & LK_ALL_WAITERS)) {
539 if (atomic_fcmpset_rel_ptr(&lk->lk_lock, xp,
540 *xp - LK_ONE_SHARER))
550 lockmgr_slock_adaptive(struct lock_delay_arg *lda, struct lock *lk, uintptr_t *xp,
553 struct thread *owner;
557 MPASS(x != LK_UNLOCKED);
558 owner = (struct thread *)LK_HOLDER(x);
560 MPASS(owner != curthread);
561 if (owner == (struct thread *)LK_KERNPROC)
563 if ((x & LK_SHARE) && LK_SHARERS(x) > 0)
567 if (!TD_IS_RUNNING(owner))
569 if ((x & LK_ALL_WAITERS) != 0)
572 x = lockmgr_read_value(lk);
573 if (LK_CAN_SHARE(x, flags, false)) {
577 owner = (struct thread *)LK_HOLDER(x);
581 static __noinline int
582 lockmgr_slock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
583 const char *file, int line, struct lockmgr_wait *lwa)
591 uint64_t sleep_time = 0;
593 #ifdef LOCK_PROFILING
594 uint64_t waittime = 0;
597 struct lock_delay_arg lda;
599 if (KERNEL_PANICKED())
602 tid = (uintptr_t)curthread;
604 if (LK_CAN_WITNESS(flags))
605 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
606 file, line, flags & LK_INTERLOCK ? ilk : NULL);
607 lock_delay_arg_init(&lda, &lockmgr_delay);
609 flags &= ~LK_ADAPTIVE;
610 x = lockmgr_read_value(lk);
612 * The lock may already be locked exclusive by curthread,
615 if (LK_HOLDER(x) == tid) {
617 "%s: %p already held in exclusive mode",
624 if (lockmgr_slock_try(lk, &x, flags, false))
627 if ((flags & (LK_ADAPTIVE | LK_INTERLOCK)) == LK_ADAPTIVE) {
628 if (lockmgr_slock_adaptive(&lda, lk, &x, flags))
633 PMC_SOFT_CALL( , , lock, failed);
635 lock_profile_obtain_lock_failed(&lk->lock_object,
636 &contested, &waittime);
639 * If the lock is expected to not sleep just give up
642 if (LK_TRYOP(flags)) {
643 LOCK_LOG2(lk, "%s: %p fails the try operation",
650 * Acquire the sleepqueue chain lock because we
651 * probabilly will need to manipulate waiters flags.
653 sleepq_lock(&lk->lock_object);
654 x = lockmgr_read_value(lk);
658 * if the lock can be acquired in shared mode, try
661 if (LK_CAN_SHARE(x, flags, false)) {
662 sleepq_release(&lk->lock_object);
667 * Try to set the LK_SHARED_WAITERS flag. If we fail,
668 * loop back and retry.
670 if ((x & LK_SHARED_WAITERS) == 0) {
671 if (!atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
672 x | LK_SHARED_WAITERS)) {
675 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
680 iwmesg = lk->lock_object.lo_name;
684 iwmesg = lwa->iwmesg;
690 * As far as we have been unable to acquire the
691 * shared lock and the shared waiters flag is set,
695 sleep_time -= lockstat_nsecs(&lk->lock_object);
697 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
700 sleep_time += lockstat_nsecs(&lk->lock_object);
702 flags &= ~LK_INTERLOCK;
705 "%s: interrupted sleep for %p with %d",
706 __func__, lk, error);
709 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
711 x = lockmgr_read_value(lk);
716 LOCKSTAT_RECORD4(lockmgr__block, lk, sleep_time,
717 LOCKSTAT_READER, (x & LK_SHARE) == 0,
718 (x & LK_SHARE) == 0 ? 0 : LK_SHARERS(x));
720 #ifdef LOCK_PROFILING
721 lockmgr_note_shared_acquire(lk, contested, waittime,
724 lockmgr_note_shared_acquire(lk, 0, 0, file, line,
730 lockmgr_exit(flags, ilk, 0);
735 lockmgr_xlock_adaptive(struct lock_delay_arg *lda, struct lock *lk, uintptr_t *xp)
737 struct thread *owner;
741 MPASS(x != LK_UNLOCKED);
742 owner = (struct thread *)LK_HOLDER(x);
744 MPASS(owner != curthread);
747 if ((x & LK_SHARE) && LK_SHARERS(x) > 0)
749 if (owner == (struct thread *)LK_KERNPROC)
751 if (!TD_IS_RUNNING(owner))
753 if ((x & LK_ALL_WAITERS) != 0)
756 x = lockmgr_read_value(lk);
757 if (x == LK_UNLOCKED) {
761 owner = (struct thread *)LK_HOLDER(x);
765 static __noinline int
766 lockmgr_xlock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
767 const char *file, int line, struct lockmgr_wait *lwa)
769 struct lock_class *class;
776 uint64_t sleep_time = 0;
778 #ifdef LOCK_PROFILING
779 uint64_t waittime = 0;
782 struct lock_delay_arg lda;
784 if (KERNEL_PANICKED())
787 tid = (uintptr_t)curthread;
789 if (LK_CAN_WITNESS(flags))
790 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
791 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
795 * If curthread already holds the lock and this one is
796 * allowed to recurse, simply recurse on it.
798 if (lockmgr_xlocked(lk)) {
799 if ((flags & LK_CANRECURSE) == 0 &&
800 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
802 * If the lock is expected to not panic just
803 * give up and return.
805 if (LK_TRYOP(flags)) {
807 "%s: %p fails the try operation",
812 if (flags & LK_INTERLOCK) {
813 class = LOCK_CLASS(ilk);
814 class->lc_unlock(ilk);
817 panic("%s: recursing on non recursive lockmgr %p "
818 "@ %s:%d\n", __func__, lk, file, line);
820 atomic_set_ptr(&lk->lk_lock, LK_WRITER_RECURSED);
822 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
823 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
824 lk->lk_recurse, file, line);
825 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
826 LK_TRYWIT(flags), file, line);
827 TD_LOCKS_INC(curthread);
832 lock_delay_arg_init(&lda, &lockmgr_delay);
834 flags &= ~LK_ADAPTIVE;
836 if (x == LK_UNLOCKED) {
837 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x, tid))
841 if ((flags & (LK_ADAPTIVE | LK_INTERLOCK)) == LK_ADAPTIVE) {
842 if (lockmgr_xlock_adaptive(&lda, lk, &x))
846 PMC_SOFT_CALL( , , lock, failed);
848 lock_profile_obtain_lock_failed(&lk->lock_object,
849 &contested, &waittime);
852 * If the lock is expected to not sleep just give up
855 if (LK_TRYOP(flags)) {
856 LOCK_LOG2(lk, "%s: %p fails the try operation",
863 * Acquire the sleepqueue chain lock because we
864 * probabilly will need to manipulate waiters flags.
866 sleepq_lock(&lk->lock_object);
867 x = lockmgr_read_value(lk);
871 * if the lock has been released while we spun on
872 * the sleepqueue chain lock just try again.
874 if (x == LK_UNLOCKED) {
875 sleepq_release(&lk->lock_object);
880 * The lock can be in the state where there is a
881 * pending queue of waiters, but still no owner.
882 * This happens when the lock is contested and an
883 * owner is going to claim the lock.
884 * If curthread is the one successfully acquiring it
885 * claim lock ownership and return, preserving waiters
888 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
889 if ((x & ~v) == LK_UNLOCKED) {
890 v &= ~LK_EXCLUSIVE_SPINNERS;
891 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
893 sleepq_release(&lk->lock_object);
895 "%s: %p claimed by a new writer",
903 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
904 * fail, loop back and retry.
906 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
907 if (!atomic_fcmpset_ptr(&lk->lk_lock, &x,
908 x | LK_EXCLUSIVE_WAITERS)) {
911 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
916 iwmesg = lk->lock_object.lo_name;
920 iwmesg = lwa->iwmesg;
926 * As far as we have been unable to acquire the
927 * exclusive lock and the exclusive waiters flag
928 * is set, we will sleep.
931 sleep_time -= lockstat_nsecs(&lk->lock_object);
933 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
936 sleep_time += lockstat_nsecs(&lk->lock_object);
938 flags &= ~LK_INTERLOCK;
941 "%s: interrupted sleep for %p with %d",
942 __func__, lk, error);
945 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
947 x = lockmgr_read_value(lk);
952 LOCKSTAT_RECORD4(lockmgr__block, lk, sleep_time,
953 LOCKSTAT_WRITER, (x & LK_SHARE) == 0,
954 (x & LK_SHARE) == 0 ? 0 : LK_SHARERS(x));
956 #ifdef LOCK_PROFILING
957 lockmgr_note_exclusive_acquire(lk, contested, waittime,
960 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
966 lockmgr_exit(flags, ilk, 0);
970 static __noinline int
971 lockmgr_upgrade(struct lock *lk, u_int flags, struct lock_object *ilk,
972 const char *file, int line, struct lockmgr_wait *lwa)
974 uintptr_t tid, v, setv;
978 if (KERNEL_PANICKED())
981 tid = (uintptr_t)curthread;
983 _lockmgr_assert(lk, KA_SLOCKED, file, line);
985 op = flags & LK_TYPE_MASK;
986 v = lockmgr_read_value(lk);
988 if (LK_SHARERS_LOCK(v) > 1) {
989 if (op == LK_TRYUPGRADE) {
990 LOCK_LOG2(lk, "%s: %p failed the nowait upgrade",
995 if (lockmgr_sunlock_try(lk, &v)) {
996 lockmgr_note_shared_release(lk, file, line);
1000 MPASS((v & ~LK_ALL_WAITERS) == LK_SHARERS_LOCK(1));
1003 setv |= (v & LK_ALL_WAITERS);
1006 * Try to switch from one shared lock to an exclusive one.
1007 * We need to preserve waiters flags during the operation.
1009 if (atomic_fcmpset_ptr(&lk->lk_lock, &v, setv)) {
1010 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
1012 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
1013 LK_TRYWIT(flags), file, line);
1014 LOCKSTAT_RECORD0(lockmgr__upgrade, lk);
1015 TD_SLOCKS_DEC(curthread);
1021 error = lockmgr_xlock_hard(lk, flags, ilk, file, line, lwa);
1022 flags &= ~LK_INTERLOCK;
1024 lockmgr_exit(flags, ilk, 0);
1029 lockmgr_lock_flags(struct lock *lk, u_int flags, struct lock_object *ilk,
1030 const char *file, int line)
1032 struct lock_class *class;
1037 if (KERNEL_PANICKED())
1040 op = flags & LK_TYPE_MASK;
1044 if (LK_CAN_WITNESS(flags))
1045 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
1046 file, line, flags & LK_INTERLOCK ? ilk : NULL);
1047 if (__predict_false(lk->lock_object.lo_flags & LK_NOSHARE))
1049 x = lockmgr_read_value(lk);
1050 if (lockmgr_slock_try(lk, &x, flags, true)) {
1051 lockmgr_note_shared_acquire(lk, 0, 0,
1055 return (lockmgr_slock_hard(lk, flags, ilk, file, line,
1060 if (LK_CAN_WITNESS(flags))
1061 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1062 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
1064 tid = (uintptr_t)curthread;
1065 if (lockmgr_read_value(lk) == LK_UNLOCKED &&
1066 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1067 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
1071 return (lockmgr_xlock_hard(lk, flags, ilk, file, line,
1077 return (lockmgr_upgrade(lk, flags, ilk, file, line, NULL));
1081 if (__predict_true(locked)) {
1082 if (__predict_false(flags & LK_INTERLOCK)) {
1083 class = LOCK_CLASS(ilk);
1084 class->lc_unlock(ilk);
1088 return (__lockmgr_args(lk, flags, ilk, LK_WMESG_DEFAULT,
1089 LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, file, line));
1093 static __noinline int
1094 lockmgr_sunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk,
1095 const char *file, int line)
1098 int wakeup_swapper = 0;
1100 if (KERNEL_PANICKED())
1103 wakeup_swapper = wakeupshlk(lk, file, line);
1106 lockmgr_exit(flags, ilk, wakeup_swapper);
1110 static __noinline int
1111 lockmgr_xunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk,
1112 const char *file, int line)
1115 int wakeup_swapper = 0;
1119 if (KERNEL_PANICKED())
1122 tid = (uintptr_t)curthread;
1125 * As first option, treact the lock as if it has not
1127 * Fix-up the tid var if the lock has been disowned.
1129 if (LK_HOLDER(x) == LK_KERNPROC)
1133 * The lock is held in exclusive mode.
1134 * If the lock is recursed also, then unrecurse it.
1136 if (lockmgr_recursed_v(x)) {
1137 LOCK_LOG2(lk, "%s: %p unrecursing", __func__, lk);
1139 if (lk->lk_recurse == 0)
1140 atomic_clear_ptr(&lk->lk_lock, LK_WRITER_RECURSED);
1143 if (tid != LK_KERNPROC)
1144 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk,
1147 if (x == tid && atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED))
1150 sleepq_lock(&lk->lock_object);
1151 x = lockmgr_read_value(lk);
1155 * If the lock has exclusive waiters, give them
1156 * preference in order to avoid deadlock with
1157 * shared runners up.
1158 * If interruptible sleeps left the exclusive queue
1159 * empty avoid a starvation for the threads sleeping
1160 * on the shared queue by giving them precedence
1161 * and cleaning up the exclusive waiters bit anyway.
1162 * Please note that lk_exslpfail count may be lying
1163 * about the real number of waiters with the
1164 * LK_SLEEPFAIL flag on because they may be used in
1165 * conjunction with interruptible sleeps so
1166 * lk_exslpfail might be considered an 'upper limit'
1167 * bound, including the edge cases.
1169 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1170 realexslp = sleepq_sleepcnt(&lk->lock_object, SQ_EXCLUSIVE_QUEUE);
1171 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1172 if (lk->lk_exslpfail < realexslp) {
1173 lk->lk_exslpfail = 0;
1174 queue = SQ_EXCLUSIVE_QUEUE;
1175 v |= (x & LK_SHARED_WAITERS);
1177 lk->lk_exslpfail = 0;
1179 "%s: %p has only LK_SLEEPFAIL sleepers",
1182 "%s: %p waking up threads on the exclusive queue",
1184 wakeup_swapper = sleepq_broadcast(&lk->lock_object,
1185 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1186 queue = SQ_SHARED_QUEUE;
1191 * Exclusive waiters sleeping with LK_SLEEPFAIL
1192 * on and using interruptible sleeps/timeout
1193 * may have left spourious lk_exslpfail counts
1194 * on, so clean it up anyway.
1196 lk->lk_exslpfail = 0;
1197 queue = SQ_SHARED_QUEUE;
1200 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
1201 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1203 atomic_store_rel_ptr(&lk->lk_lock, v);
1204 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK, 0, queue);
1205 sleepq_release(&lk->lock_object);
1208 lockmgr_exit(flags, ilk, wakeup_swapper);
1213 * Lightweight entry points for common operations.
1215 * Functionality is similar to sx locks, in that none of the additional lockmgr
1216 * features are supported. To be clear, these are NOT supported:
1217 * 1. shared locking disablement
1218 * 2. returning with an error after sleep
1219 * 3. unlocking the interlock
1221 * If in doubt, use lockmgr_lock_flags.
1224 lockmgr_slock(struct lock *lk, u_int flags, const char *file, int line)
1228 MPASS((flags & LK_TYPE_MASK) == LK_SHARED);
1229 MPASS((flags & LK_INTERLOCK) == 0);
1230 MPASS((lk->lock_object.lo_flags & LK_NOSHARE) == 0);
1232 if (LK_CAN_WITNESS(flags))
1233 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
1235 x = lockmgr_read_value(lk);
1236 if (__predict_true(lockmgr_slock_try(lk, &x, flags, true))) {
1237 lockmgr_note_shared_acquire(lk, 0, 0, file, line, flags);
1241 return (lockmgr_slock_hard(lk, flags | LK_ADAPTIVE, NULL, file, line, NULL));
1245 lockmgr_xlock(struct lock *lk, u_int flags, const char *file, int line)
1249 MPASS((flags & LK_TYPE_MASK) == LK_EXCLUSIVE);
1250 MPASS((flags & LK_INTERLOCK) == 0);
1252 if (LK_CAN_WITNESS(flags))
1253 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1254 LOP_EXCLUSIVE, file, line, NULL);
1255 tid = (uintptr_t)curthread;
1256 if (atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1257 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
1262 return (lockmgr_xlock_hard(lk, flags | LK_ADAPTIVE, NULL, file, line, NULL));
1266 lockmgr_unlock(struct lock *lk)
1275 _lockmgr_assert(lk, KA_LOCKED, file, line);
1276 x = lockmgr_read_value(lk);
1277 if (__predict_true(x & LK_SHARE) != 0) {
1278 lockmgr_note_shared_release(lk, file, line);
1279 if (lockmgr_sunlock_try(lk, &x)) {
1280 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk, LOCKSTAT_READER);
1282 return (lockmgr_sunlock_hard(lk, x, LK_RELEASE, NULL, file, line));
1285 tid = (uintptr_t)curthread;
1286 lockmgr_note_exclusive_release(lk, file, line);
1287 if (x == tid && atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED)) {
1288 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk,LOCKSTAT_WRITER);
1290 return (lockmgr_xunlock_hard(lk, x, LK_RELEASE, NULL, file, line));
1297 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
1298 const char *wmesg, int pri, int timo, const char *file, int line)
1301 struct lockmgr_wait lwa;
1302 struct lock_class *class;
1304 uintptr_t tid, v, x;
1305 u_int op, realexslp;
1306 int error, ipri, itimo, queue, wakeup_swapper;
1307 #ifdef LOCK_PROFILING
1308 uint64_t waittime = 0;
1312 if (KERNEL_PANICKED())
1316 tid = (uintptr_t)curthread;
1317 op = (flags & LK_TYPE_MASK);
1318 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
1319 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
1320 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
1322 lwa.iwmesg = iwmesg;
1326 MPASS((flags & ~LK_TOTAL_MASK) == 0);
1327 KASSERT((op & (op - 1)) == 0,
1328 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
1329 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
1330 (op != LK_DOWNGRADE && op != LK_RELEASE),
1331 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
1332 __func__, file, line));
1333 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
1334 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
1335 __func__, file, line));
1336 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
1337 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
1338 lk->lock_object.lo_name, file, line));
1340 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
1342 if (lk->lock_object.lo_flags & LK_NOSHARE) {
1350 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
1352 if (flags & LK_INTERLOCK)
1353 class->lc_unlock(ilk);
1361 return (lockmgr_slock_hard(lk, flags, ilk, file, line, &lwa));
1365 return (lockmgr_upgrade(lk, flags, ilk, file, line, &lwa));
1368 return (lockmgr_xlock_hard(lk, flags, ilk, file, line, &lwa));
1371 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1372 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
1375 * Panic if the lock is recursed.
1377 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
1378 if (flags & LK_INTERLOCK)
1379 class->lc_unlock(ilk);
1380 panic("%s: downgrade a recursed lockmgr %s @ %s:%d\n",
1381 __func__, iwmesg, file, line);
1383 TD_SLOCKS_INC(curthread);
1386 * In order to preserve waiters flags, just spin.
1389 x = lockmgr_read_value(lk);
1390 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1391 x &= LK_ALL_WAITERS;
1392 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1393 LK_SHARERS_LOCK(1) | x))
1397 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
1398 LOCKSTAT_RECORD0(lockmgr__downgrade, lk);
1401 _lockmgr_assert(lk, KA_LOCKED, file, line);
1402 x = lockmgr_read_value(lk);
1404 if (__predict_true(x & LK_SHARE) != 0) {
1405 lockmgr_note_shared_release(lk, file, line);
1406 return (lockmgr_sunlock_hard(lk, x, flags, ilk, file, line));
1408 lockmgr_note_exclusive_release(lk, file, line);
1409 return (lockmgr_xunlock_hard(lk, x, flags, ilk, file, line));
1413 if (LK_CAN_WITNESS(flags))
1414 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1415 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
1419 * Trying to drain a lock we already own will result in a
1422 if (lockmgr_xlocked(lk)) {
1423 if (flags & LK_INTERLOCK)
1424 class->lc_unlock(ilk);
1425 panic("%s: draining %s with the lock held @ %s:%d\n",
1426 __func__, iwmesg, file, line);
1430 if (lk->lk_lock == LK_UNLOCKED &&
1431 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid))
1435 PMC_SOFT_CALL( , , lock, failed);
1437 lock_profile_obtain_lock_failed(&lk->lock_object,
1438 &contested, &waittime);
1441 * If the lock is expected to not sleep just give up
1444 if (LK_TRYOP(flags)) {
1445 LOCK_LOG2(lk, "%s: %p fails the try operation",
1452 * Acquire the sleepqueue chain lock because we
1453 * probabilly will need to manipulate waiters flags.
1455 sleepq_lock(&lk->lock_object);
1456 x = lockmgr_read_value(lk);
1459 * if the lock has been released while we spun on
1460 * the sleepqueue chain lock just try again.
1462 if (x == LK_UNLOCKED) {
1463 sleepq_release(&lk->lock_object);
1467 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1468 if ((x & ~v) == LK_UNLOCKED) {
1469 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1472 * If interruptible sleeps left the exclusive
1473 * queue empty avoid a starvation for the
1474 * threads sleeping on the shared queue by
1475 * giving them precedence and cleaning up the
1476 * exclusive waiters bit anyway.
1477 * Please note that lk_exslpfail count may be
1478 * lying about the real number of waiters with
1479 * the LK_SLEEPFAIL flag on because they may
1480 * be used in conjunction with interruptible
1481 * sleeps so lk_exslpfail might be considered
1482 * an 'upper limit' bound, including the edge
1485 if (v & LK_EXCLUSIVE_WAITERS) {
1486 queue = SQ_EXCLUSIVE_QUEUE;
1487 v &= ~LK_EXCLUSIVE_WAITERS;
1491 * Exclusive waiters sleeping with
1492 * LK_SLEEPFAIL on and using
1493 * interruptible sleeps/timeout may
1494 * have left spourious lk_exslpfail
1495 * counts on, so clean it up anyway.
1497 MPASS(v & LK_SHARED_WAITERS);
1498 lk->lk_exslpfail = 0;
1499 queue = SQ_SHARED_QUEUE;
1500 v &= ~LK_SHARED_WAITERS;
1502 if (queue == SQ_EXCLUSIVE_QUEUE) {
1504 sleepq_sleepcnt(&lk->lock_object,
1505 SQ_EXCLUSIVE_QUEUE);
1506 if (lk->lk_exslpfail >= realexslp) {
1507 lk->lk_exslpfail = 0;
1508 queue = SQ_SHARED_QUEUE;
1509 v &= ~LK_SHARED_WAITERS;
1510 if (realexslp != 0) {
1512 "%s: %p has only LK_SLEEPFAIL sleepers",
1515 "%s: %p waking up threads on the exclusive queue",
1521 SQ_EXCLUSIVE_QUEUE);
1524 lk->lk_exslpfail = 0;
1526 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1527 sleepq_release(&lk->lock_object);
1531 "%s: %p waking up all threads on the %s queue",
1532 __func__, lk, queue == SQ_SHARED_QUEUE ?
1533 "shared" : "exclusive");
1534 wakeup_swapper |= sleepq_broadcast(
1535 &lk->lock_object, SLEEPQ_LK, 0, queue);
1538 * If shared waiters have been woken up we need
1539 * to wait for one of them to acquire the lock
1540 * before to set the exclusive waiters in
1541 * order to avoid a deadlock.
1543 if (queue == SQ_SHARED_QUEUE) {
1544 for (v = lk->lk_lock;
1545 (v & LK_SHARE) && !LK_SHARERS(v);
1552 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1553 * fail, loop back and retry.
1555 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1556 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1557 x | LK_EXCLUSIVE_WAITERS)) {
1558 sleepq_release(&lk->lock_object);
1561 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1566 * As far as we have been unable to acquire the
1567 * exclusive lock and the exclusive waiters flag
1568 * is set, we will sleep.
1570 if (flags & LK_INTERLOCK) {
1571 class->lc_unlock(ilk);
1572 flags &= ~LK_INTERLOCK;
1575 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1576 SQ_EXCLUSIVE_QUEUE);
1577 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1579 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1584 lock_profile_obtain_lock_success(&lk->lock_object,
1585 contested, waittime, file, line);
1586 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1587 lk->lk_recurse, file, line);
1588 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1589 LK_TRYWIT(flags), file, line);
1590 TD_LOCKS_INC(curthread);
1595 if (flags & LK_INTERLOCK)
1596 class->lc_unlock(ilk);
1597 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1600 if (flags & LK_INTERLOCK)
1601 class->lc_unlock(ilk);
1609 _lockmgr_disown(struct lock *lk, const char *file, int line)
1613 if (SCHEDULER_STOPPED())
1616 tid = (uintptr_t)curthread;
1617 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1620 * Panic if the lock is recursed.
1622 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk))
1623 panic("%s: disown a recursed lockmgr @ %s:%d\n",
1624 __func__, file, line);
1627 * If the owner is already LK_KERNPROC just skip the whole operation.
1629 if (LK_HOLDER(lk->lk_lock) != tid)
1631 lock_profile_release_lock(&lk->lock_object);
1632 LOCKSTAT_RECORD1(lockmgr__disown, lk, LOCKSTAT_WRITER);
1633 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1634 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1635 TD_LOCKS_DEC(curthread);
1639 * In order to preserve waiters flags, just spin.
1642 x = lockmgr_read_value(lk);
1643 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1644 x &= LK_ALL_WAITERS;
1645 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1653 lockmgr_printinfo(const struct lock *lk)
1658 if (lk->lk_lock == LK_UNLOCKED)
1659 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1660 else if (lk->lk_lock & LK_SHARE)
1661 printf("lock type %s: SHARED (count %ju)\n",
1662 lk->lock_object.lo_name,
1663 (uintmax_t)LK_SHARERS(lk->lk_lock));
1665 td = lockmgr_xholder(lk);
1666 if (td == (struct thread *)LK_KERNPROC)
1667 printf("lock type %s: EXCL by KERNPROC\n",
1668 lk->lock_object.lo_name);
1670 printf("lock type %s: EXCL by thread %p "
1671 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name,
1672 td, td->td_proc->p_pid, td->td_proc->p_comm,
1677 if (x & LK_EXCLUSIVE_WAITERS)
1678 printf(" with exclusive waiters pending\n");
1679 if (x & LK_SHARED_WAITERS)
1680 printf(" with shared waiters pending\n");
1681 if (x & LK_EXCLUSIVE_SPINNERS)
1682 printf(" with exclusive spinners pending\n");
1688 lockstatus(const struct lock *lk)
1694 x = lockmgr_read_value(lk);
1697 if ((x & LK_SHARE) == 0) {
1698 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1702 } else if (x == LK_UNLOCKED)
1708 #ifdef INVARIANT_SUPPORT
1710 FEATURE(invariant_support,
1711 "Support for modules compiled with INVARIANTS option");
1714 #undef _lockmgr_assert
1718 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1722 if (KERNEL_PANICKED())
1726 case KA_SLOCKED | KA_NOTRECURSED:
1727 case KA_SLOCKED | KA_RECURSED:
1730 case KA_LOCKED | KA_NOTRECURSED:
1731 case KA_LOCKED | KA_RECURSED:
1735 * We cannot trust WITNESS if the lock is held in exclusive
1736 * mode and a call to lockmgr_disown() happened.
1737 * Workaround this skipping the check if the lock is held in
1738 * exclusive mode even for the KA_LOCKED case.
1740 if (slocked || (lk->lk_lock & LK_SHARE)) {
1741 witness_assert(&lk->lock_object, what, file, line);
1745 if (lk->lk_lock == LK_UNLOCKED ||
1746 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1747 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1748 panic("Lock %s not %slocked @ %s:%d\n",
1749 lk->lock_object.lo_name, slocked ? "share" : "",
1752 if ((lk->lk_lock & LK_SHARE) == 0) {
1753 if (lockmgr_recursed(lk)) {
1754 if (what & KA_NOTRECURSED)
1755 panic("Lock %s recursed @ %s:%d\n",
1756 lk->lock_object.lo_name, file,
1758 } else if (what & KA_RECURSED)
1759 panic("Lock %s not recursed @ %s:%d\n",
1760 lk->lock_object.lo_name, file, line);
1764 case KA_XLOCKED | KA_NOTRECURSED:
1765 case KA_XLOCKED | KA_RECURSED:
1766 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1767 panic("Lock %s not exclusively locked @ %s:%d\n",
1768 lk->lock_object.lo_name, file, line);
1769 if (lockmgr_recursed(lk)) {
1770 if (what & KA_NOTRECURSED)
1771 panic("Lock %s recursed @ %s:%d\n",
1772 lk->lock_object.lo_name, file, line);
1773 } else if (what & KA_RECURSED)
1774 panic("Lock %s not recursed @ %s:%d\n",
1775 lk->lock_object.lo_name, file, line);
1778 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1779 panic("Lock %s exclusively locked @ %s:%d\n",
1780 lk->lock_object.lo_name, file, line);
1783 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1791 lockmgr_chain(struct thread *td, struct thread **ownerp)
1793 const struct lock *lk;
1797 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1799 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1800 if (lk->lk_lock & LK_SHARE)
1801 db_printf("SHARED (count %ju)\n",
1802 (uintmax_t)LK_SHARERS(lk->lk_lock));
1804 db_printf("EXCL\n");
1805 *ownerp = lockmgr_xholder(lk);
1811 db_show_lockmgr(const struct lock_object *lock)
1814 const struct lock *lk;
1816 lk = (const struct lock *)lock;
1818 db_printf(" state: ");
1819 if (lk->lk_lock == LK_UNLOCKED)
1820 db_printf("UNLOCKED\n");
1821 else if (lk->lk_lock & LK_SHARE)
1822 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1824 td = lockmgr_xholder(lk);
1825 if (td == (struct thread *)LK_KERNPROC)
1826 db_printf("XLOCK: LK_KERNPROC\n");
1828 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1829 td->td_tid, td->td_proc->p_pid,
1830 td->td_proc->p_comm);
1831 if (lockmgr_recursed(lk))
1832 db_printf(" recursed: %d\n", lk->lk_recurse);
1834 db_printf(" waiters: ");
1835 switch (lk->lk_lock & LK_ALL_WAITERS) {
1836 case LK_SHARED_WAITERS:
1837 db_printf("shared\n");
1839 case LK_EXCLUSIVE_WAITERS:
1840 db_printf("exclusive\n");
1842 case LK_ALL_WAITERS:
1843 db_printf("shared and exclusive\n");
1846 db_printf("none\n");
1848 db_printf(" spinners: ");
1849 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1850 db_printf("exclusive\n");
1852 db_printf("none\n");