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;
368 * Exclusive waiters sleeping with LK_SLEEPFAIL on
369 * and using interruptible sleeps/timeout may have
370 * left spourious lk_exslpfail counts on, so clean
373 lk->lk_exslpfail = 0;
374 queue = SQ_SHARED_QUEUE;
377 if (lockmgr_sunlock_try(lk, &orig_x)) {
378 sleepq_release(&lk->lock_object);
382 x |= LK_SHARERS_LOCK(1);
383 if (!atomic_fcmpset_rel_ptr(&lk->lk_lock, &x, v)) {
387 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
388 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
390 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
392 sleepq_release(&lk->lock_object);
396 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk, LOCKSTAT_READER);
397 return (wakeup_swapper);
401 assert_lockmgr(const struct lock_object *lock, int what)
404 panic("lockmgr locks do not support assertions");
408 lock_lockmgr(struct lock_object *lock, uintptr_t how)
411 panic("lockmgr locks do not support sleep interlocking");
415 unlock_lockmgr(struct lock_object *lock)
418 panic("lockmgr locks do not support sleep interlocking");
423 owner_lockmgr(const struct lock_object *lock, struct thread **owner)
426 panic("lockmgr locks do not support owner inquiring");
431 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
435 MPASS((flags & ~LK_INIT_MASK) == 0);
436 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
437 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
440 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
441 if (flags & LK_CANRECURSE)
442 iflags |= LO_RECURSABLE;
443 if ((flags & LK_NODUP) == 0)
445 if (flags & LK_NOPROFILE)
446 iflags |= LO_NOPROFILE;
447 if ((flags & LK_NOWITNESS) == 0)
448 iflags |= LO_WITNESS;
449 if (flags & LK_QUIET)
451 if (flags & LK_IS_VNODE)
452 iflags |= LO_IS_VNODE;
455 iflags |= flags & LK_NOSHARE;
457 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
458 lk->lk_lock = LK_UNLOCKED;
460 lk->lk_exslpfail = 0;
467 * XXX: Gross hacks to manipulate external lock flags after
468 * initialization. Used for certain vnode and buf locks.
471 lockallowshare(struct lock *lk)
474 lockmgr_assert(lk, KA_XLOCKED);
475 lk->lock_object.lo_flags &= ~LK_NOSHARE;
479 lockdisableshare(struct lock *lk)
482 lockmgr_assert(lk, KA_XLOCKED);
483 lk->lock_object.lo_flags |= LK_NOSHARE;
487 lockallowrecurse(struct lock *lk)
490 lockmgr_assert(lk, KA_XLOCKED);
491 lk->lock_object.lo_flags |= LO_RECURSABLE;
495 lockdisablerecurse(struct lock *lk)
498 lockmgr_assert(lk, KA_XLOCKED);
499 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
503 lockdestroy(struct lock *lk)
506 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
507 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
508 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
509 lock_destroy(&lk->lock_object);
512 static bool __always_inline
513 lockmgr_slock_try(struct lock *lk, uintptr_t *xp, int flags, bool fp)
517 * If no other thread has an exclusive lock, or
518 * no exclusive waiter is present, bump the count of
519 * sharers. Since we have to preserve the state of
520 * waiters, if we fail to acquire the shared lock
521 * loop back and retry.
523 while (LK_CAN_SHARE(*xp, flags, fp)) {
524 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, xp,
525 *xp + LK_ONE_SHARER)) {
532 static bool __always_inline
533 lockmgr_sunlock_try(struct lock *lk, uintptr_t *xp)
537 if (LK_SHARERS(*xp) > 1 || !(*xp & LK_ALL_WAITERS)) {
538 if (atomic_fcmpset_rel_ptr(&lk->lk_lock, xp,
539 *xp - LK_ONE_SHARER))
549 lockmgr_slock_adaptive(struct lock_delay_arg *lda, struct lock *lk, uintptr_t *xp,
552 struct thread *owner;
556 MPASS(x != LK_UNLOCKED);
557 owner = (struct thread *)LK_HOLDER(x);
559 MPASS(owner != curthread);
560 if (owner == (struct thread *)LK_KERNPROC)
562 if ((x & LK_SHARE) && LK_SHARERS(x) > 0)
566 if (!TD_IS_RUNNING(owner))
568 if ((x & LK_ALL_WAITERS) != 0)
571 x = lockmgr_read_value(lk);
572 if (LK_CAN_SHARE(x, flags, false)) {
576 owner = (struct thread *)LK_HOLDER(x);
580 static __noinline int
581 lockmgr_slock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
582 const char *file, int line, struct lockmgr_wait *lwa)
590 uint64_t sleep_time = 0;
592 #ifdef LOCK_PROFILING
593 uint64_t waittime = 0;
596 struct lock_delay_arg lda;
598 if (KERNEL_PANICKED())
601 tid = (uintptr_t)curthread;
603 if (LK_CAN_WITNESS(flags))
604 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
605 file, line, flags & LK_INTERLOCK ? ilk : NULL);
606 lock_delay_arg_init(&lda, &lockmgr_delay);
608 flags &= ~LK_ADAPTIVE;
609 x = lockmgr_read_value(lk);
611 * The lock may already be locked exclusive by curthread,
614 if (LK_HOLDER(x) == tid) {
616 "%s: %p already held in exclusive mode",
623 if (lockmgr_slock_try(lk, &x, flags, false))
626 if ((flags & (LK_ADAPTIVE | LK_INTERLOCK)) == LK_ADAPTIVE) {
627 if (lockmgr_slock_adaptive(&lda, lk, &x, flags))
632 PMC_SOFT_CALL( , , lock, failed);
634 lock_profile_obtain_lock_failed(&lk->lock_object,
635 &contested, &waittime);
638 * If the lock is expected to not sleep just give up
641 if (LK_TRYOP(flags)) {
642 LOCK_LOG2(lk, "%s: %p fails the try operation",
649 * Acquire the sleepqueue chain lock because we
650 * probabilly will need to manipulate waiters flags.
652 sleepq_lock(&lk->lock_object);
653 x = lockmgr_read_value(lk);
657 * if the lock can be acquired in shared mode, try
660 if (LK_CAN_SHARE(x, flags, false)) {
661 sleepq_release(&lk->lock_object);
666 * Try to set the LK_SHARED_WAITERS flag. If we fail,
667 * loop back and retry.
669 if ((x & LK_SHARED_WAITERS) == 0) {
670 if (!atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
671 x | LK_SHARED_WAITERS)) {
674 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
679 iwmesg = lk->lock_object.lo_name;
683 iwmesg = lwa->iwmesg;
689 * As far as we have been unable to acquire the
690 * shared lock and the shared waiters flag is set,
694 sleep_time -= lockstat_nsecs(&lk->lock_object);
696 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
699 sleep_time += lockstat_nsecs(&lk->lock_object);
701 flags &= ~LK_INTERLOCK;
704 "%s: interrupted sleep for %p with %d",
705 __func__, lk, error);
708 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
710 x = lockmgr_read_value(lk);
715 LOCKSTAT_RECORD4(lockmgr__block, lk, sleep_time,
716 LOCKSTAT_READER, (x & LK_SHARE) == 0,
717 (x & LK_SHARE) == 0 ? 0 : LK_SHARERS(x));
719 #ifdef LOCK_PROFILING
720 lockmgr_note_shared_acquire(lk, contested, waittime,
723 lockmgr_note_shared_acquire(lk, 0, 0, file, line,
729 lockmgr_exit(flags, ilk, 0);
734 lockmgr_xlock_adaptive(struct lock_delay_arg *lda, struct lock *lk, uintptr_t *xp)
736 struct thread *owner;
740 MPASS(x != LK_UNLOCKED);
741 owner = (struct thread *)LK_HOLDER(x);
743 MPASS(owner != curthread);
746 if ((x & LK_SHARE) && LK_SHARERS(x) > 0)
748 if (owner == (struct thread *)LK_KERNPROC)
750 if (!TD_IS_RUNNING(owner))
752 if ((x & LK_ALL_WAITERS) != 0)
755 x = lockmgr_read_value(lk);
756 if (x == LK_UNLOCKED) {
760 owner = (struct thread *)LK_HOLDER(x);
764 static __noinline int
765 lockmgr_xlock_hard(struct lock *lk, u_int flags, struct lock_object *ilk,
766 const char *file, int line, struct lockmgr_wait *lwa)
768 struct lock_class *class;
775 uint64_t sleep_time = 0;
777 #ifdef LOCK_PROFILING
778 uint64_t waittime = 0;
781 struct lock_delay_arg lda;
783 if (KERNEL_PANICKED())
786 tid = (uintptr_t)curthread;
788 if (LK_CAN_WITNESS(flags))
789 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
790 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
794 * If curthread already holds the lock and this one is
795 * allowed to recurse, simply recurse on it.
797 if (lockmgr_xlocked(lk)) {
798 if ((flags & LK_CANRECURSE) == 0 &&
799 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
801 * If the lock is expected to not panic just
802 * give up and return.
804 if (LK_TRYOP(flags)) {
806 "%s: %p fails the try operation",
811 if (flags & LK_INTERLOCK) {
812 class = LOCK_CLASS(ilk);
813 class->lc_unlock(ilk);
816 panic("%s: recursing on non recursive lockmgr %p "
817 "@ %s:%d\n", __func__, lk, file, line);
819 atomic_set_ptr(&lk->lk_lock, LK_WRITER_RECURSED);
821 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
822 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
823 lk->lk_recurse, file, line);
824 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
825 LK_TRYWIT(flags), file, line);
826 TD_LOCKS_INC(curthread);
831 lock_delay_arg_init(&lda, &lockmgr_delay);
833 flags &= ~LK_ADAPTIVE;
835 if (x == LK_UNLOCKED) {
836 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x, tid))
840 if ((flags & (LK_ADAPTIVE | LK_INTERLOCK)) == LK_ADAPTIVE) {
841 if (lockmgr_xlock_adaptive(&lda, lk, &x))
845 PMC_SOFT_CALL( , , lock, failed);
847 lock_profile_obtain_lock_failed(&lk->lock_object,
848 &contested, &waittime);
851 * If the lock is expected to not sleep just give up
854 if (LK_TRYOP(flags)) {
855 LOCK_LOG2(lk, "%s: %p fails the try operation",
862 * Acquire the sleepqueue chain lock because we
863 * probabilly will need to manipulate waiters flags.
865 sleepq_lock(&lk->lock_object);
866 x = lockmgr_read_value(lk);
870 * if the lock has been released while we spun on
871 * the sleepqueue chain lock just try again.
873 if (x == LK_UNLOCKED) {
874 sleepq_release(&lk->lock_object);
879 * The lock can be in the state where there is a
880 * pending queue of waiters, but still no owner.
881 * This happens when the lock is contested and an
882 * owner is going to claim the lock.
883 * If curthread is the one successfully acquiring it
884 * claim lock ownership and return, preserving waiters
887 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
888 if ((x & ~v) == LK_UNLOCKED) {
889 v &= ~LK_EXCLUSIVE_SPINNERS;
890 if (atomic_fcmpset_acq_ptr(&lk->lk_lock, &x,
892 sleepq_release(&lk->lock_object);
894 "%s: %p claimed by a new writer",
902 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
903 * fail, loop back and retry.
905 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
906 if (!atomic_fcmpset_ptr(&lk->lk_lock, &x,
907 x | LK_EXCLUSIVE_WAITERS)) {
910 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
915 iwmesg = lk->lock_object.lo_name;
919 iwmesg = lwa->iwmesg;
925 * As far as we have been unable to acquire the
926 * exclusive lock and the exclusive waiters flag
927 * is set, we will sleep.
930 sleep_time -= lockstat_nsecs(&lk->lock_object);
932 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
935 sleep_time += lockstat_nsecs(&lk->lock_object);
937 flags &= ~LK_INTERLOCK;
940 "%s: interrupted sleep for %p with %d",
941 __func__, lk, error);
944 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
946 x = lockmgr_read_value(lk);
951 LOCKSTAT_RECORD4(lockmgr__block, lk, sleep_time,
952 LOCKSTAT_WRITER, (x & LK_SHARE) == 0,
953 (x & LK_SHARE) == 0 ? 0 : LK_SHARERS(x));
955 #ifdef LOCK_PROFILING
956 lockmgr_note_exclusive_acquire(lk, contested, waittime,
959 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
965 lockmgr_exit(flags, ilk, 0);
969 static __noinline int
970 lockmgr_upgrade(struct lock *lk, u_int flags, struct lock_object *ilk,
971 const char *file, int line, struct lockmgr_wait *lwa)
973 uintptr_t tid, v, setv;
977 if (KERNEL_PANICKED())
980 tid = (uintptr_t)curthread;
982 _lockmgr_assert(lk, KA_SLOCKED, file, line);
984 op = flags & LK_TYPE_MASK;
985 v = lockmgr_read_value(lk);
987 if (LK_SHARERS_LOCK(v) > 1) {
988 if (op == LK_TRYUPGRADE) {
989 LOCK_LOG2(lk, "%s: %p failed the nowait upgrade",
994 if (lockmgr_sunlock_try(lk, &v)) {
995 lockmgr_note_shared_release(lk, file, line);
999 MPASS((v & ~LK_ALL_WAITERS) == LK_SHARERS_LOCK(1));
1002 setv |= (v & LK_ALL_WAITERS);
1005 * Try to switch from one shared lock to an exclusive one.
1006 * We need to preserve waiters flags during the operation.
1008 if (atomic_fcmpset_ptr(&lk->lk_lock, &v, setv)) {
1009 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
1011 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
1012 LK_TRYWIT(flags), file, line);
1013 LOCKSTAT_RECORD0(lockmgr__upgrade, lk);
1014 TD_SLOCKS_DEC(curthread);
1020 error = lockmgr_xlock_hard(lk, flags, ilk, file, line, lwa);
1021 flags &= ~LK_INTERLOCK;
1023 lockmgr_exit(flags, ilk, 0);
1028 lockmgr_lock_flags(struct lock *lk, u_int flags, struct lock_object *ilk,
1029 const char *file, int line)
1031 struct lock_class *class;
1036 if (KERNEL_PANICKED())
1039 op = flags & LK_TYPE_MASK;
1043 if (LK_CAN_WITNESS(flags))
1044 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
1045 file, line, flags & LK_INTERLOCK ? ilk : NULL);
1046 if (__predict_false(lk->lock_object.lo_flags & LK_NOSHARE))
1048 x = lockmgr_read_value(lk);
1049 if (lockmgr_slock_try(lk, &x, flags, true)) {
1050 lockmgr_note_shared_acquire(lk, 0, 0,
1054 return (lockmgr_slock_hard(lk, flags, ilk, file, line,
1059 if (LK_CAN_WITNESS(flags))
1060 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1061 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
1063 tid = (uintptr_t)curthread;
1064 if (lockmgr_read_value(lk) == LK_UNLOCKED &&
1065 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1066 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
1070 return (lockmgr_xlock_hard(lk, flags, ilk, file, line,
1076 return (lockmgr_upgrade(lk, flags, ilk, file, line, NULL));
1080 if (__predict_true(locked)) {
1081 if (__predict_false(flags & LK_INTERLOCK)) {
1082 class = LOCK_CLASS(ilk);
1083 class->lc_unlock(ilk);
1087 return (__lockmgr_args(lk, flags, ilk, LK_WMESG_DEFAULT,
1088 LK_PRIO_DEFAULT, LK_TIMO_DEFAULT, file, line));
1092 static __noinline int
1093 lockmgr_sunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk,
1094 const char *file, int line)
1097 int wakeup_swapper = 0;
1099 if (KERNEL_PANICKED())
1102 wakeup_swapper = wakeupshlk(lk, file, line);
1105 lockmgr_exit(flags, ilk, wakeup_swapper);
1109 static __noinline int
1110 lockmgr_xunlock_hard(struct lock *lk, uintptr_t x, u_int flags, struct lock_object *ilk,
1111 const char *file, int line)
1114 int wakeup_swapper = 0;
1118 if (KERNEL_PANICKED())
1121 tid = (uintptr_t)curthread;
1124 * As first option, treact the lock as if it has not
1126 * Fix-up the tid var if the lock has been disowned.
1128 if (LK_HOLDER(x) == LK_KERNPROC)
1132 * The lock is held in exclusive mode.
1133 * If the lock is recursed also, then unrecurse it.
1135 if (lockmgr_recursed_v(x)) {
1136 LOCK_LOG2(lk, "%s: %p unrecursing", __func__, lk);
1138 if (lk->lk_recurse == 0)
1139 atomic_clear_ptr(&lk->lk_lock, LK_WRITER_RECURSED);
1142 if (tid != LK_KERNPROC)
1143 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk,
1146 if (x == tid && atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED))
1149 sleepq_lock(&lk->lock_object);
1150 x = lockmgr_read_value(lk);
1154 * If the lock has exclusive waiters, give them
1155 * preference in order to avoid deadlock with
1156 * shared runners up.
1157 * If interruptible sleeps left the exclusive queue
1158 * empty avoid a starvation for the threads sleeping
1159 * on the shared queue by giving them precedence
1160 * and cleaning up the exclusive waiters bit anyway.
1161 * Please note that lk_exslpfail count may be lying
1162 * about the real number of waiters with the
1163 * LK_SLEEPFAIL flag on because they may be used in
1164 * conjunction with interruptible sleeps so
1165 * lk_exslpfail might be considered an 'upper limit'
1166 * bound, including the edge cases.
1168 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1169 realexslp = sleepq_sleepcnt(&lk->lock_object, SQ_EXCLUSIVE_QUEUE);
1170 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1171 if (lk->lk_exslpfail < realexslp) {
1172 lk->lk_exslpfail = 0;
1173 queue = SQ_EXCLUSIVE_QUEUE;
1174 v |= (x & LK_SHARED_WAITERS);
1176 lk->lk_exslpfail = 0;
1178 "%s: %p has only LK_SLEEPFAIL sleepers",
1181 "%s: %p waking up threads on the exclusive queue",
1183 wakeup_swapper = sleepq_broadcast(&lk->lock_object,
1184 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1185 queue = SQ_SHARED_QUEUE;
1189 * Exclusive waiters sleeping with LK_SLEEPFAIL
1190 * on and using interruptible sleeps/timeout
1191 * may have left spourious lk_exslpfail counts
1192 * on, so clean it up anyway.
1194 lk->lk_exslpfail = 0;
1195 queue = SQ_SHARED_QUEUE;
1198 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
1199 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1201 atomic_store_rel_ptr(&lk->lk_lock, v);
1202 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK, 0, queue);
1203 sleepq_release(&lk->lock_object);
1206 lockmgr_exit(flags, ilk, wakeup_swapper);
1211 * Lightweight entry points for common operations.
1213 * Functionality is similar to sx locks, in that none of the additional lockmgr
1214 * features are supported. To be clear, these are NOT supported:
1215 * 1. shared locking disablement
1216 * 2. returning with an error after sleep
1217 * 3. unlocking the interlock
1219 * If in doubt, use lockmgr_lock_flags.
1222 lockmgr_slock(struct lock *lk, u_int flags, const char *file, int line)
1226 MPASS((flags & LK_TYPE_MASK) == LK_SHARED);
1227 MPASS((flags & LK_INTERLOCK) == 0);
1228 MPASS((lk->lock_object.lo_flags & LK_NOSHARE) == 0);
1230 if (LK_CAN_WITNESS(flags))
1231 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
1233 x = lockmgr_read_value(lk);
1234 if (__predict_true(lockmgr_slock_try(lk, &x, flags, true))) {
1235 lockmgr_note_shared_acquire(lk, 0, 0, file, line, flags);
1239 return (lockmgr_slock_hard(lk, flags | LK_ADAPTIVE, NULL, file, line, NULL));
1243 lockmgr_xlock(struct lock *lk, u_int flags, const char *file, int line)
1247 MPASS((flags & LK_TYPE_MASK) == LK_EXCLUSIVE);
1248 MPASS((flags & LK_INTERLOCK) == 0);
1250 if (LK_CAN_WITNESS(flags))
1251 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1252 LOP_EXCLUSIVE, file, line, NULL);
1253 tid = (uintptr_t)curthread;
1254 if (atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1255 lockmgr_note_exclusive_acquire(lk, 0, 0, file, line,
1260 return (lockmgr_xlock_hard(lk, flags | LK_ADAPTIVE, NULL, file, line, NULL));
1264 lockmgr_unlock(struct lock *lk)
1273 _lockmgr_assert(lk, KA_LOCKED, file, line);
1274 x = lockmgr_read_value(lk);
1275 if (__predict_true(x & LK_SHARE) != 0) {
1276 lockmgr_note_shared_release(lk, file, line);
1277 if (lockmgr_sunlock_try(lk, &x)) {
1278 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk, LOCKSTAT_READER);
1280 return (lockmgr_sunlock_hard(lk, x, LK_RELEASE, NULL, file, line));
1283 tid = (uintptr_t)curthread;
1284 lockmgr_note_exclusive_release(lk, file, line);
1285 if (x == tid && atomic_cmpset_rel_ptr(&lk->lk_lock, tid, LK_UNLOCKED)) {
1286 LOCKSTAT_PROFILE_RELEASE_RWLOCK(lockmgr__release, lk,LOCKSTAT_WRITER);
1288 return (lockmgr_xunlock_hard(lk, x, LK_RELEASE, NULL, file, line));
1295 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
1296 const char *wmesg, int pri, int timo, const char *file, int line)
1299 struct lockmgr_wait lwa;
1300 struct lock_class *class;
1302 uintptr_t tid, v, x;
1303 u_int op, realexslp;
1304 int error, ipri, itimo, queue, wakeup_swapper;
1305 #ifdef LOCK_PROFILING
1306 uint64_t waittime = 0;
1310 if (KERNEL_PANICKED())
1314 tid = (uintptr_t)curthread;
1315 op = (flags & LK_TYPE_MASK);
1316 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
1317 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
1318 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
1320 lwa.iwmesg = iwmesg;
1324 MPASS((flags & ~LK_TOTAL_MASK) == 0);
1325 KASSERT((op & (op - 1)) == 0,
1326 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
1327 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
1328 (op != LK_DOWNGRADE && op != LK_RELEASE),
1329 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
1330 __func__, file, line));
1331 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
1332 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
1333 __func__, file, line));
1334 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
1335 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
1336 lk->lock_object.lo_name, file, line));
1338 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
1340 if (lk->lock_object.lo_flags & LK_NOSHARE) {
1348 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
1350 if (flags & LK_INTERLOCK)
1351 class->lc_unlock(ilk);
1359 return (lockmgr_slock_hard(lk, flags, ilk, file, line, &lwa));
1363 return (lockmgr_upgrade(lk, flags, ilk, file, line, &lwa));
1366 return (lockmgr_xlock_hard(lk, flags, ilk, file, line, &lwa));
1369 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1370 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
1373 * Panic if the lock is recursed.
1375 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
1376 if (flags & LK_INTERLOCK)
1377 class->lc_unlock(ilk);
1378 panic("%s: downgrade a recursed lockmgr %s @ %s:%d\n",
1379 __func__, iwmesg, file, line);
1381 TD_SLOCKS_INC(curthread);
1384 * In order to preserve waiters flags, just spin.
1387 x = lockmgr_read_value(lk);
1388 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1389 x &= LK_ALL_WAITERS;
1390 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1391 LK_SHARERS_LOCK(1) | x))
1395 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
1396 LOCKSTAT_RECORD0(lockmgr__downgrade, lk);
1399 _lockmgr_assert(lk, KA_LOCKED, file, line);
1400 x = lockmgr_read_value(lk);
1402 if (__predict_true(x & LK_SHARE) != 0) {
1403 lockmgr_note_shared_release(lk, file, line);
1404 return (lockmgr_sunlock_hard(lk, x, flags, ilk, file, line));
1406 lockmgr_note_exclusive_release(lk, file, line);
1407 return (lockmgr_xunlock_hard(lk, x, flags, ilk, file, line));
1411 if (LK_CAN_WITNESS(flags))
1412 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1413 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
1417 * Trying to drain a lock we already own will result in a
1420 if (lockmgr_xlocked(lk)) {
1421 if (flags & LK_INTERLOCK)
1422 class->lc_unlock(ilk);
1423 panic("%s: draining %s with the lock held @ %s:%d\n",
1424 __func__, iwmesg, file, line);
1428 if (lk->lk_lock == LK_UNLOCKED &&
1429 atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid))
1433 PMC_SOFT_CALL( , , lock, failed);
1435 lock_profile_obtain_lock_failed(&lk->lock_object,
1436 &contested, &waittime);
1439 * If the lock is expected to not sleep just give up
1442 if (LK_TRYOP(flags)) {
1443 LOCK_LOG2(lk, "%s: %p fails the try operation",
1450 * Acquire the sleepqueue chain lock because we
1451 * probabilly will need to manipulate waiters flags.
1453 sleepq_lock(&lk->lock_object);
1454 x = lockmgr_read_value(lk);
1457 * if the lock has been released while we spun on
1458 * the sleepqueue chain lock just try again.
1460 if (x == LK_UNLOCKED) {
1461 sleepq_release(&lk->lock_object);
1465 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1466 if ((x & ~v) == LK_UNLOCKED) {
1467 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1470 * If interruptible sleeps left the exclusive
1471 * queue empty avoid a starvation for the
1472 * threads sleeping on the shared queue by
1473 * giving them precedence and cleaning up the
1474 * exclusive waiters bit anyway.
1475 * Please note that lk_exslpfail count may be
1476 * lying about the real number of waiters with
1477 * the LK_SLEEPFAIL flag on because they may
1478 * be used in conjunction with interruptible
1479 * sleeps so lk_exslpfail might be considered
1480 * an 'upper limit' bound, including the edge
1483 if (v & LK_EXCLUSIVE_WAITERS) {
1484 queue = SQ_EXCLUSIVE_QUEUE;
1485 v &= ~LK_EXCLUSIVE_WAITERS;
1488 * Exclusive waiters sleeping with
1489 * LK_SLEEPFAIL on and using
1490 * interruptible sleeps/timeout may
1491 * have left spourious lk_exslpfail
1492 * counts on, so clean it up anyway.
1494 MPASS(v & LK_SHARED_WAITERS);
1495 lk->lk_exslpfail = 0;
1496 queue = SQ_SHARED_QUEUE;
1497 v &= ~LK_SHARED_WAITERS;
1499 if (queue == SQ_EXCLUSIVE_QUEUE) {
1501 sleepq_sleepcnt(&lk->lock_object,
1502 SQ_EXCLUSIVE_QUEUE);
1503 if (lk->lk_exslpfail >= realexslp) {
1504 lk->lk_exslpfail = 0;
1505 queue = SQ_SHARED_QUEUE;
1506 v &= ~LK_SHARED_WAITERS;
1507 if (realexslp != 0) {
1509 "%s: %p has only LK_SLEEPFAIL sleepers",
1512 "%s: %p waking up threads on the exclusive queue",
1518 SQ_EXCLUSIVE_QUEUE);
1521 lk->lk_exslpfail = 0;
1523 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1524 sleepq_release(&lk->lock_object);
1528 "%s: %p waking up all threads on the %s queue",
1529 __func__, lk, queue == SQ_SHARED_QUEUE ?
1530 "shared" : "exclusive");
1531 wakeup_swapper |= sleepq_broadcast(
1532 &lk->lock_object, SLEEPQ_LK, 0, queue);
1535 * If shared waiters have been woken up we need
1536 * to wait for one of them to acquire the lock
1537 * before to set the exclusive waiters in
1538 * order to avoid a deadlock.
1540 if (queue == SQ_SHARED_QUEUE) {
1541 for (v = lk->lk_lock;
1542 (v & LK_SHARE) && !LK_SHARERS(v);
1549 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1550 * fail, loop back and retry.
1552 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1553 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1554 x | LK_EXCLUSIVE_WAITERS)) {
1555 sleepq_release(&lk->lock_object);
1558 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1563 * As far as we have been unable to acquire the
1564 * exclusive lock and the exclusive waiters flag
1565 * is set, we will sleep.
1567 if (flags & LK_INTERLOCK) {
1568 class->lc_unlock(ilk);
1569 flags &= ~LK_INTERLOCK;
1572 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1573 SQ_EXCLUSIVE_QUEUE);
1574 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1576 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1581 lock_profile_obtain_lock_success(&lk->lock_object,
1582 contested, waittime, file, line);
1583 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1584 lk->lk_recurse, file, line);
1585 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1586 LK_TRYWIT(flags), file, line);
1587 TD_LOCKS_INC(curthread);
1592 if (flags & LK_INTERLOCK)
1593 class->lc_unlock(ilk);
1594 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1597 if (flags & LK_INTERLOCK)
1598 class->lc_unlock(ilk);
1606 _lockmgr_disown(struct lock *lk, const char *file, int line)
1610 if (SCHEDULER_STOPPED())
1613 tid = (uintptr_t)curthread;
1614 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1617 * Panic if the lock is recursed.
1619 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk))
1620 panic("%s: disown a recursed lockmgr @ %s:%d\n",
1621 __func__, file, line);
1624 * If the owner is already LK_KERNPROC just skip the whole operation.
1626 if (LK_HOLDER(lk->lk_lock) != tid)
1628 lock_profile_release_lock(&lk->lock_object);
1629 LOCKSTAT_RECORD1(lockmgr__disown, lk, LOCKSTAT_WRITER);
1630 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1631 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1632 TD_LOCKS_DEC(curthread);
1636 * In order to preserve waiters flags, just spin.
1639 x = lockmgr_read_value(lk);
1640 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1641 x &= LK_ALL_WAITERS;
1642 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1650 lockmgr_printinfo(const struct lock *lk)
1655 if (lk->lk_lock == LK_UNLOCKED)
1656 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1657 else if (lk->lk_lock & LK_SHARE)
1658 printf("lock type %s: SHARED (count %ju)\n",
1659 lk->lock_object.lo_name,
1660 (uintmax_t)LK_SHARERS(lk->lk_lock));
1662 td = lockmgr_xholder(lk);
1663 if (td == (struct thread *)LK_KERNPROC)
1664 printf("lock type %s: EXCL by KERNPROC\n",
1665 lk->lock_object.lo_name);
1667 printf("lock type %s: EXCL by thread %p "
1668 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name,
1669 td, td->td_proc->p_pid, td->td_proc->p_comm,
1674 if (x & LK_EXCLUSIVE_WAITERS)
1675 printf(" with exclusive waiters pending\n");
1676 if (x & LK_SHARED_WAITERS)
1677 printf(" with shared waiters pending\n");
1678 if (x & LK_EXCLUSIVE_SPINNERS)
1679 printf(" with exclusive spinners pending\n");
1685 lockstatus(const struct lock *lk)
1691 x = lockmgr_read_value(lk);
1694 if ((x & LK_SHARE) == 0) {
1695 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1699 } else if (x == LK_UNLOCKED)
1705 #ifdef INVARIANT_SUPPORT
1707 FEATURE(invariant_support,
1708 "Support for modules compiled with INVARIANTS option");
1711 #undef _lockmgr_assert
1715 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1719 if (KERNEL_PANICKED())
1723 case KA_SLOCKED | KA_NOTRECURSED:
1724 case KA_SLOCKED | KA_RECURSED:
1727 case KA_LOCKED | KA_NOTRECURSED:
1728 case KA_LOCKED | KA_RECURSED:
1732 * We cannot trust WITNESS if the lock is held in exclusive
1733 * mode and a call to lockmgr_disown() happened.
1734 * Workaround this skipping the check if the lock is held in
1735 * exclusive mode even for the KA_LOCKED case.
1737 if (slocked || (lk->lk_lock & LK_SHARE)) {
1738 witness_assert(&lk->lock_object, what, file, line);
1742 if (lk->lk_lock == LK_UNLOCKED ||
1743 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1744 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1745 panic("Lock %s not %slocked @ %s:%d\n",
1746 lk->lock_object.lo_name, slocked ? "share" : "",
1749 if ((lk->lk_lock & LK_SHARE) == 0) {
1750 if (lockmgr_recursed(lk)) {
1751 if (what & KA_NOTRECURSED)
1752 panic("Lock %s recursed @ %s:%d\n",
1753 lk->lock_object.lo_name, file,
1755 } else if (what & KA_RECURSED)
1756 panic("Lock %s not recursed @ %s:%d\n",
1757 lk->lock_object.lo_name, file, line);
1761 case KA_XLOCKED | KA_NOTRECURSED:
1762 case KA_XLOCKED | KA_RECURSED:
1763 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1764 panic("Lock %s not exclusively locked @ %s:%d\n",
1765 lk->lock_object.lo_name, file, line);
1766 if (lockmgr_recursed(lk)) {
1767 if (what & KA_NOTRECURSED)
1768 panic("Lock %s recursed @ %s:%d\n",
1769 lk->lock_object.lo_name, file, line);
1770 } else if (what & KA_RECURSED)
1771 panic("Lock %s not recursed @ %s:%d\n",
1772 lk->lock_object.lo_name, file, line);
1775 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1776 panic("Lock %s exclusively locked @ %s:%d\n",
1777 lk->lock_object.lo_name, file, line);
1780 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1788 lockmgr_chain(struct thread *td, struct thread **ownerp)
1790 const struct lock *lk;
1794 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1796 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1797 if (lk->lk_lock & LK_SHARE)
1798 db_printf("SHARED (count %ju)\n",
1799 (uintmax_t)LK_SHARERS(lk->lk_lock));
1801 db_printf("EXCL\n");
1802 *ownerp = lockmgr_xholder(lk);
1808 db_show_lockmgr(const struct lock_object *lock)
1811 const struct lock *lk;
1813 lk = (const struct lock *)lock;
1815 db_printf(" state: ");
1816 if (lk->lk_lock == LK_UNLOCKED)
1817 db_printf("UNLOCKED\n");
1818 else if (lk->lk_lock & LK_SHARE)
1819 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1821 td = lockmgr_xholder(lk);
1822 if (td == (struct thread *)LK_KERNPROC)
1823 db_printf("XLOCK: LK_KERNPROC\n");
1825 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1826 td->td_tid, td->td_proc->p_pid,
1827 td->td_proc->p_comm);
1828 if (lockmgr_recursed(lk))
1829 db_printf(" recursed: %d\n", lk->lk_recurse);
1831 db_printf(" waiters: ");
1832 switch (lk->lk_lock & LK_ALL_WAITERS) {
1833 case LK_SHARED_WAITERS:
1834 db_printf("shared\n");
1836 case LK_EXCLUSIVE_WAITERS:
1837 db_printf("exclusive\n");
1839 case LK_ALL_WAITERS:
1840 db_printf("shared and exclusive\n");
1843 db_printf("none\n");
1845 db_printf(" spinners: ");
1846 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1847 db_printf("exclusive\n");
1849 db_printf("none\n");