2 * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice(s), this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified other than the possible
11 * addition of one or more copyright notices.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice(s), this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 #include "opt_adaptive_lockmgrs.h"
31 #include "opt_kdtrace.h"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/linker_set.h>
40 #include <sys/lock_profile.h>
41 #include <sys/lockmgr.h>
42 #include <sys/mutex.h>
44 #include <sys/sleepqueue.h>
46 #include <sys/stack.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
51 #include <machine/cpu.h>
57 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
58 (LK_ADAPTIVE | LK_NOSHARE));
59 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
60 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
62 #define SQ_EXCLUSIVE_QUEUE 0
63 #define SQ_SHARED_QUEUE 1
65 #ifdef ADAPTIVE_LOCKMGRS
66 #define ALK_RETRIES 10
67 #define ALK_LOOPS 10000
71 #define _lockmgr_assert(lk, what, file, line)
72 #define TD_LOCKS_INC(td)
73 #define TD_LOCKS_DEC(td)
75 #define TD_LOCKS_INC(td) ((td)->td_locks++)
76 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
78 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
79 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
82 #define STACK_PRINT(lk)
83 #define STACK_SAVE(lk)
84 #define STACK_ZERO(lk)
86 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
87 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
88 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
91 #define LOCK_LOG2(lk, string, arg1, arg2) \
92 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
93 CTR2(KTR_LOCK, (string), (arg1), (arg2))
94 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
95 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
96 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
98 #define GIANT_DECLARE \
100 WITNESS_SAVE_DECL(Giant)
101 #define GIANT_RESTORE() do { \
105 WITNESS_RESTORE(&Giant.lock_object, Giant); \
108 #define GIANT_SAVE() do { \
109 if (mtx_owned(&Giant)) { \
110 WITNESS_SAVE(&Giant.lock_object, Giant); \
111 while (mtx_owned(&Giant)) { \
113 mtx_unlock(&Giant); \
118 #define LK_CAN_SHARE(x) \
119 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
120 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
121 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
122 #define LK_TRYOP(x) \
125 #define LK_CAN_WITNESS(x) \
126 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
127 #define LK_TRYWIT(x) \
128 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
130 #define LK_CAN_ADAPT(lk, f) \
131 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
132 ((f) & LK_SLEEPFAIL) == 0)
134 #define lockmgr_disowned(lk) \
135 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
137 #define lockmgr_xlocked(lk) \
138 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
140 static void assert_lockmgr(struct lock_object *lock, int how);
142 static void db_show_lockmgr(struct lock_object *lock);
144 static void lock_lockmgr(struct lock_object *lock, int how);
146 static int owner_lockmgr(struct lock_object *lock, struct thread **owner);
148 static int unlock_lockmgr(struct lock_object *lock);
150 struct lock_class lock_class_lockmgr = {
151 .lc_name = "lockmgr",
152 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
153 .lc_assert = assert_lockmgr,
155 .lc_ddb_show = db_show_lockmgr,
157 .lc_lock = lock_lockmgr,
158 .lc_unlock = unlock_lockmgr,
160 .lc_owner = owner_lockmgr,
164 static __inline struct thread *
165 lockmgr_xholder(struct lock *lk)
170 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
174 * It assumes sleepq_lock held and returns with this one unheld.
175 * It also assumes the generic interlock is sane and previously checked.
176 * If LK_INTERLOCK is specified the interlock is not reacquired after the
180 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
181 const char *wmesg, int pri, int timo, int queue)
184 struct lock_class *class;
187 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
188 catch = pri & PCATCH;
192 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
193 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
195 if (flags & LK_INTERLOCK)
196 class->lc_unlock(ilk);
198 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
199 SLEEPQ_INTERRUPTIBLE : 0), queue);
200 if ((flags & LK_TIMELOCK) && timo)
201 sleepq_set_timeout(&lk->lock_object, timo);
204 * Decisional switch for real sleeping.
206 if ((flags & LK_TIMELOCK) && timo && catch)
207 error = sleepq_timedwait_sig(&lk->lock_object, pri);
208 else if ((flags & LK_TIMELOCK) && timo)
209 error = sleepq_timedwait(&lk->lock_object, pri);
211 error = sleepq_wait_sig(&lk->lock_object, pri);
213 sleepq_wait(&lk->lock_object, pri);
215 if ((flags & LK_SLEEPFAIL) && error == 0)
222 wakeupshlk(struct lock *lk, const char *file, int line)
225 int queue, wakeup_swapper;
227 TD_LOCKS_DEC(curthread);
228 TD_SLOCKS_DEC(curthread);
229 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
230 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
237 * If there is more than one shared lock held, just drop one
240 if (LK_SHARERS(x) > 1) {
241 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
248 * If there are not waiters on the exclusive queue, drop the
251 if ((x & LK_ALL_WAITERS) == 0) {
252 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
254 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
260 * We should have a sharer with waiters, so enter the hard
261 * path in order to handle wakeups correctly.
263 sleepq_lock(&lk->lock_object);
264 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
268 * If the lock has exclusive waiters, give them preference in
269 * order to avoid deadlock with shared runners up.
271 if (x & LK_EXCLUSIVE_WAITERS) {
272 queue = SQ_EXCLUSIVE_QUEUE;
273 v |= (x & LK_SHARED_WAITERS);
275 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
277 queue = SQ_SHARED_QUEUE;
280 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
282 sleepq_release(&lk->lock_object);
285 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
286 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
288 wakeup_swapper = sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
290 sleepq_release(&lk->lock_object);
294 lock_profile_release_lock(&lk->lock_object);
295 return (wakeup_swapper);
299 assert_lockmgr(struct lock_object *lock, int what)
302 panic("lockmgr locks do not support assertions");
306 lock_lockmgr(struct lock_object *lock, int how)
309 panic("lockmgr locks do not support sleep interlocking");
313 unlock_lockmgr(struct lock_object *lock)
316 panic("lockmgr locks do not support sleep interlocking");
321 owner_lockmgr(struct lock_object *lock, struct thread **owner)
324 panic("lockmgr locks do not support owner inquiring");
329 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
333 MPASS((flags & ~LK_INIT_MASK) == 0);
334 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
335 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
338 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
339 if (flags & LK_CANRECURSE)
340 iflags |= LO_RECURSABLE;
341 if ((flags & LK_NODUP) == 0)
343 if (flags & LK_NOPROFILE)
344 iflags |= LO_NOPROFILE;
345 if ((flags & LK_NOWITNESS) == 0)
346 iflags |= LO_WITNESS;
347 if (flags & LK_QUIET)
349 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
351 lk->lk_lock = LK_UNLOCKED;
355 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
360 lockdestroy(struct lock *lk)
363 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
364 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
365 lock_destroy(&lk->lock_object);
369 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
370 const char *wmesg, int pri, int timo, const char *file, int line)
373 struct lock_class *class;
377 int error, ipri, itimo, queue, wakeup_swapper;
378 #ifdef LOCK_PROFILING
379 uint64_t waittime = 0;
382 #ifdef ADAPTIVE_LOCKMGRS
383 volatile struct thread *owner;
384 u_int i, spintries = 0;
388 tid = (uintptr_t)curthread;
389 op = (flags & LK_TYPE_MASK);
390 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
391 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
392 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
394 MPASS((flags & ~LK_TOTAL_MASK) == 0);
395 KASSERT((op & (op - 1)) == 0,
396 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
397 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
398 (op != LK_DOWNGRADE && op != LK_RELEASE),
399 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
400 __func__, file, line));
401 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
402 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
403 __func__, file, line));
405 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
406 if (panicstr != NULL) {
407 if (flags & LK_INTERLOCK)
408 class->lc_unlock(ilk);
412 if (op == LK_SHARED && (lk->lock_object.lo_flags & LK_NOSHARE))
418 if (LK_CAN_WITNESS(flags))
419 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
425 * If no other thread has an exclusive lock, or
426 * no exclusive waiter is present, bump the count of
427 * sharers. Since we have to preserve the state of
428 * waiters, if we fail to acquire the shared lock
429 * loop back and retry.
431 if (LK_CAN_SHARE(x)) {
432 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
437 lock_profile_obtain_lock_failed(&lk->lock_object,
438 &contested, &waittime);
441 * If the lock is already held by curthread in
442 * exclusive way avoid a deadlock.
444 if (LK_HOLDER(x) == tid) {
446 "%s: %p already held in exclusive mode",
453 * If the lock is expected to not sleep just give up
456 if (LK_TRYOP(flags)) {
457 LOCK_LOG2(lk, "%s: %p fails the try operation",
463 #ifdef ADAPTIVE_LOCKMGRS
465 * If the owner is running on another CPU, spin until
466 * the owner stops running or the state of the lock
467 * changes. We need a double-state handle here
468 * because for a failed acquisition the lock can be
469 * either held in exclusive mode or shared mode
470 * (for the writer starvation avoidance technique).
472 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
473 LK_HOLDER(x) != LK_KERNPROC) {
474 owner = (struct thread *)LK_HOLDER(x);
475 if (LOCK_LOG_TEST(&lk->lock_object, 0))
477 "%s: spinning on %p held by %p",
478 __func__, lk, owner);
481 * If we are holding also an interlock drop it
482 * in order to avoid a deadlock if the lockmgr
483 * owner is adaptively spinning on the
486 if (flags & LK_INTERLOCK) {
487 class->lc_unlock(ilk);
488 flags &= ~LK_INTERLOCK;
491 while (LK_HOLDER(lk->lk_lock) ==
492 (uintptr_t)owner && TD_IS_RUNNING(owner))
496 } else if (LK_CAN_ADAPT(lk, flags) &&
497 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
498 spintries < ALK_RETRIES) {
499 if (flags & LK_INTERLOCK) {
500 class->lc_unlock(ilk);
501 flags &= ~LK_INTERLOCK;
505 for (i = 0; i < ALK_LOOPS; i++) {
506 if (LOCK_LOG_TEST(&lk->lock_object, 0))
508 "%s: shared spinning on %p with %u and %u",
509 __func__, lk, spintries, i);
511 if ((x & LK_SHARE) == 0 ||
512 LK_CAN_SHARE(x) != 0)
523 * Acquire the sleepqueue chain lock because we
524 * probabilly will need to manipulate waiters flags.
526 sleepq_lock(&lk->lock_object);
530 * if the lock can be acquired in shared mode, try
533 if (LK_CAN_SHARE(x)) {
534 sleepq_release(&lk->lock_object);
538 #ifdef ADAPTIVE_LOCKMGRS
540 * The current lock owner might have started executing
541 * on another CPU (or the lock could have changed
542 * owner) while we were waiting on the turnstile
543 * chain lock. If so, drop the turnstile lock and try
546 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
547 LK_HOLDER(x) != LK_KERNPROC) {
548 owner = (struct thread *)LK_HOLDER(x);
549 if (TD_IS_RUNNING(owner)) {
550 sleepq_release(&lk->lock_object);
557 * Try to set the LK_SHARED_WAITERS flag. If we fail,
558 * loop back and retry.
560 if ((x & LK_SHARED_WAITERS) == 0) {
561 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
562 x | LK_SHARED_WAITERS)) {
563 sleepq_release(&lk->lock_object);
566 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
571 * As far as we have been unable to acquire the
572 * shared lock and the shared waiters flag is set,
575 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
577 flags &= ~LK_INTERLOCK;
580 "%s: interrupted sleep for %p with %d",
581 __func__, lk, error);
584 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
588 lock_profile_obtain_lock_success(&lk->lock_object,
589 contested, waittime, file, line);
590 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
592 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
594 TD_LOCKS_INC(curthread);
595 TD_SLOCKS_INC(curthread);
600 _lockmgr_assert(lk, KA_SLOCKED, file, line);
602 x = v & LK_ALL_WAITERS;
603 v &= LK_EXCLUSIVE_SPINNERS;
606 * Try to switch from one shared lock to an exclusive one.
607 * We need to preserve waiters flags during the operation.
609 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
611 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
613 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
614 LK_TRYWIT(flags), file, line);
615 TD_SLOCKS_DEC(curthread);
620 * We have been unable to succeed in upgrading, so just
621 * give up the shared lock.
623 wakeup_swapper |= wakeupshlk(lk, file, line);
627 if (LK_CAN_WITNESS(flags))
628 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
629 LOP_EXCLUSIVE, file, line, ilk);
632 * If curthread already holds the lock and this one is
633 * allowed to recurse, simply recurse on it.
635 if (lockmgr_xlocked(lk)) {
636 if ((flags & LK_CANRECURSE) == 0 &&
637 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
640 * If the lock is expected to not panic just
641 * give up and return.
643 if (LK_TRYOP(flags)) {
645 "%s: %p fails the try operation",
650 if (flags & LK_INTERLOCK)
651 class->lc_unlock(ilk);
652 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
653 __func__, iwmesg, file, line);
656 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
657 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
658 lk->lk_recurse, file, line);
659 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
660 LK_TRYWIT(flags), file, line);
661 TD_LOCKS_INC(curthread);
665 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
667 lock_profile_obtain_lock_failed(&lk->lock_object,
668 &contested, &waittime);
671 * If the lock is expected to not sleep just give up
674 if (LK_TRYOP(flags)) {
675 LOCK_LOG2(lk, "%s: %p fails the try operation",
681 #ifdef ADAPTIVE_LOCKMGRS
683 * If the owner is running on another CPU, spin until
684 * the owner stops running or the state of the lock
688 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
689 LK_HOLDER(x) != LK_KERNPROC) {
690 owner = (struct thread *)LK_HOLDER(x);
691 if (LOCK_LOG_TEST(&lk->lock_object, 0))
693 "%s: spinning on %p held by %p",
694 __func__, lk, owner);
697 * If we are holding also an interlock drop it
698 * in order to avoid a deadlock if the lockmgr
699 * owner is adaptively spinning on the
702 if (flags & LK_INTERLOCK) {
703 class->lc_unlock(ilk);
704 flags &= ~LK_INTERLOCK;
707 while (LK_HOLDER(lk->lk_lock) ==
708 (uintptr_t)owner && TD_IS_RUNNING(owner))
712 } else if (LK_CAN_ADAPT(lk, flags) &&
713 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
714 spintries < ALK_RETRIES) {
715 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
716 !atomic_cmpset_ptr(&lk->lk_lock, x,
717 x | LK_EXCLUSIVE_SPINNERS))
719 if (flags & LK_INTERLOCK) {
720 class->lc_unlock(ilk);
721 flags &= ~LK_INTERLOCK;
725 for (i = 0; i < ALK_LOOPS; i++) {
726 if (LOCK_LOG_TEST(&lk->lock_object, 0))
728 "%s: shared spinning on %p with %u and %u",
729 __func__, lk, spintries, i);
731 LK_EXCLUSIVE_SPINNERS) == 0)
742 * Acquire the sleepqueue chain lock because we
743 * probabilly will need to manipulate waiters flags.
745 sleepq_lock(&lk->lock_object);
749 * if the lock has been released while we spun on
750 * the sleepqueue chain lock just try again.
752 if (x == LK_UNLOCKED) {
753 sleepq_release(&lk->lock_object);
757 #ifdef ADAPTIVE_LOCKMGRS
759 * The current lock owner might have started executing
760 * on another CPU (or the lock could have changed
761 * owner) while we were waiting on the turnstile
762 * chain lock. If so, drop the turnstile lock and try
765 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
766 LK_HOLDER(x) != LK_KERNPROC) {
767 owner = (struct thread *)LK_HOLDER(x);
768 if (TD_IS_RUNNING(owner)) {
769 sleepq_release(&lk->lock_object);
776 * The lock can be in the state where there is a
777 * pending queue of waiters, but still no owner.
778 * This happens when the lock is contested and an
779 * owner is going to claim the lock.
780 * If curthread is the one successfully acquiring it
781 * claim lock ownership and return, preserving waiters
784 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
785 if ((x & ~v) == LK_UNLOCKED) {
786 v &= ~LK_EXCLUSIVE_SPINNERS;
787 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
789 sleepq_release(&lk->lock_object);
791 "%s: %p claimed by a new writer",
795 sleepq_release(&lk->lock_object);
800 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
801 * fail, loop back and retry.
803 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
804 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
805 x | LK_EXCLUSIVE_WAITERS)) {
806 sleepq_release(&lk->lock_object);
809 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
814 * As far as we have been unable to acquire the
815 * exclusive lock and the exclusive waiters flag
816 * is set, we will sleep.
818 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
820 flags &= ~LK_INTERLOCK;
823 "%s: interrupted sleep for %p with %d",
824 __func__, lk, error);
827 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
831 lock_profile_obtain_lock_success(&lk->lock_object,
832 contested, waittime, file, line);
833 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
834 lk->lk_recurse, file, line);
835 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
836 LK_TRYWIT(flags), file, line);
837 TD_LOCKS_INC(curthread);
842 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
843 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
844 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
845 TD_SLOCKS_INC(curthread);
848 * In order to preserve waiters flags, just spin.
852 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
854 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
855 LK_SHARERS_LOCK(1) | x))
861 _lockmgr_assert(lk, KA_LOCKED, file, line);
864 if ((x & LK_SHARE) == 0) {
867 * As first option, treact the lock as if it has not
869 * Fix-up the tid var if the lock has been disowned.
871 if (LK_HOLDER(x) == LK_KERNPROC)
874 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
876 TD_LOCKS_DEC(curthread);
878 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
879 lk->lk_recurse, file, line);
882 * The lock is held in exclusive mode.
883 * If the lock is recursed also, then unrecurse it.
885 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
886 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
891 if (tid != LK_KERNPROC)
892 lock_profile_release_lock(&lk->lock_object);
894 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
898 sleepq_lock(&lk->lock_object);
903 * If the lock has exclusive waiters, give them
904 * preference in order to avoid deadlock with
907 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
908 if (x & LK_EXCLUSIVE_WAITERS) {
909 queue = SQ_EXCLUSIVE_QUEUE;
910 v |= (x & LK_SHARED_WAITERS);
912 MPASS((x & LK_ALL_WAITERS) ==
914 queue = SQ_SHARED_QUEUE;
918 "%s: %p waking up threads on the %s queue",
919 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
921 atomic_store_rel_ptr(&lk->lk_lock, v);
922 wakeup_swapper = sleepq_broadcast(&lk->lock_object,
923 SLEEPQ_LK, 0, queue);
924 sleepq_release(&lk->lock_object);
927 wakeup_swapper = wakeupshlk(lk, file, line);
930 if (LK_CAN_WITNESS(flags))
931 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
932 LOP_EXCLUSIVE, file, line, ilk);
935 * Trying to drain a lock we already own will result in a
938 if (lockmgr_xlocked(lk)) {
939 if (flags & LK_INTERLOCK)
940 class->lc_unlock(ilk);
941 panic("%s: draining %s with the lock held @ %s:%d\n",
942 __func__, iwmesg, file, line);
945 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
946 lock_profile_obtain_lock_failed(&lk->lock_object,
947 &contested, &waittime);
950 * If the lock is expected to not sleep just give up
953 if (LK_TRYOP(flags)) {
954 LOCK_LOG2(lk, "%s: %p fails the try operation",
961 * Acquire the sleepqueue chain lock because we
962 * probabilly will need to manipulate waiters flags.
964 sleepq_lock(&lk->lock_object);
968 * if the lock has been released while we spun on
969 * the sleepqueue chain lock just try again.
971 if (x == LK_UNLOCKED) {
972 sleepq_release(&lk->lock_object);
976 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
977 if ((x & ~v) == LK_UNLOCKED) {
978 v = (x & ~LK_EXCLUSIVE_SPINNERS);
979 if (v & LK_EXCLUSIVE_WAITERS) {
980 queue = SQ_EXCLUSIVE_QUEUE;
981 v &= ~LK_EXCLUSIVE_WAITERS;
983 MPASS(v & LK_SHARED_WAITERS);
984 queue = SQ_SHARED_QUEUE;
985 v &= ~LK_SHARED_WAITERS;
987 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
988 sleepq_release(&lk->lock_object);
992 "%s: %p waking up all threads on the %s queue",
993 __func__, lk, queue == SQ_SHARED_QUEUE ?
994 "shared" : "exclusive");
995 wakeup_swapper |= sleepq_broadcast(
996 &lk->lock_object, SLEEPQ_LK, 0, queue);
999 * If shared waiters have been woken up we need
1000 * to wait for one of them to acquire the lock
1001 * before to set the exclusive waiters in
1002 * order to avoid a deadlock.
1004 if (queue == SQ_SHARED_QUEUE) {
1005 for (v = lk->lk_lock;
1006 (v & LK_SHARE) && !LK_SHARERS(v);
1013 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1014 * fail, loop back and retry.
1016 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1017 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1018 x | LK_EXCLUSIVE_WAITERS)) {
1019 sleepq_release(&lk->lock_object);
1022 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1027 * As far as we have been unable to acquire the
1028 * exclusive lock and the exclusive waiters flag
1029 * is set, we will sleep.
1031 if (flags & LK_INTERLOCK) {
1032 class->lc_unlock(ilk);
1033 flags &= ~LK_INTERLOCK;
1036 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1037 SQ_EXCLUSIVE_QUEUE);
1038 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1040 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1045 lock_profile_obtain_lock_success(&lk->lock_object,
1046 contested, waittime, file, line);
1047 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1048 lk->lk_recurse, file, line);
1049 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1050 LK_TRYWIT(flags), file, line);
1051 TD_LOCKS_INC(curthread);
1056 if (flags & LK_INTERLOCK)
1057 class->lc_unlock(ilk);
1058 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1061 if (flags & LK_INTERLOCK)
1062 class->lc_unlock(ilk);
1070 _lockmgr_disown(struct lock *lk, const char *file, int line)
1074 tid = (uintptr_t)curthread;
1075 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1078 * If the owner is already LK_KERNPROC just skip the whole operation.
1080 if (LK_HOLDER(lk->lk_lock) != tid)
1082 lock_profile_release_lock(&lk->lock_object);
1083 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1084 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1085 TD_LOCKS_DEC(curthread);
1088 * In order to preserve waiters flags, just spin.
1092 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1093 x &= LK_ALL_WAITERS;
1094 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1102 lockmgr_printinfo(struct lock *lk)
1107 if (lk->lk_lock == LK_UNLOCKED)
1108 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1109 else if (lk->lk_lock & LK_SHARE)
1110 printf("lock type %s: SHARED (count %ju)\n",
1111 lk->lock_object.lo_name,
1112 (uintmax_t)LK_SHARERS(lk->lk_lock));
1114 td = lockmgr_xholder(lk);
1115 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1116 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1120 if (x & LK_EXCLUSIVE_WAITERS)
1121 printf(" with exclusive waiters pending\n");
1122 if (x & LK_SHARED_WAITERS)
1123 printf(" with shared waiters pending\n");
1124 if (x & LK_EXCLUSIVE_SPINNERS)
1125 printf(" with exclusive spinners pending\n");
1131 lockstatus(struct lock *lk)
1140 if ((x & LK_SHARE) == 0) {
1141 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1145 } else if (x == LK_UNLOCKED)
1151 #ifdef INVARIANT_SUPPORT
1153 #undef _lockmgr_assert
1157 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1161 if (panicstr != NULL)
1165 case KA_SLOCKED | KA_NOTRECURSED:
1166 case KA_SLOCKED | KA_RECURSED:
1169 case KA_LOCKED | KA_NOTRECURSED:
1170 case KA_LOCKED | KA_RECURSED:
1174 * We cannot trust WITNESS if the lock is held in exclusive
1175 * mode and a call to lockmgr_disown() happened.
1176 * Workaround this skipping the check if the lock is held in
1177 * exclusive mode even for the KA_LOCKED case.
1179 if (slocked || (lk->lk_lock & LK_SHARE)) {
1180 witness_assert(&lk->lock_object, what, file, line);
1184 if (lk->lk_lock == LK_UNLOCKED ||
1185 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1186 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1187 panic("Lock %s not %slocked @ %s:%d\n",
1188 lk->lock_object.lo_name, slocked ? "share" : "",
1191 if ((lk->lk_lock & LK_SHARE) == 0) {
1192 if (lockmgr_recursed(lk)) {
1193 if (what & KA_NOTRECURSED)
1194 panic("Lock %s recursed @ %s:%d\n",
1195 lk->lock_object.lo_name, file,
1197 } else if (what & KA_RECURSED)
1198 panic("Lock %s not recursed @ %s:%d\n",
1199 lk->lock_object.lo_name, file, line);
1203 case KA_XLOCKED | KA_NOTRECURSED:
1204 case KA_XLOCKED | KA_RECURSED:
1205 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1206 panic("Lock %s not exclusively locked @ %s:%d\n",
1207 lk->lock_object.lo_name, file, line);
1208 if (lockmgr_recursed(lk)) {
1209 if (what & KA_NOTRECURSED)
1210 panic("Lock %s recursed @ %s:%d\n",
1211 lk->lock_object.lo_name, file, line);
1212 } else if (what & KA_RECURSED)
1213 panic("Lock %s not recursed @ %s:%d\n",
1214 lk->lock_object.lo_name, file, line);
1217 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1218 panic("Lock %s exclusively locked @ %s:%d\n",
1219 lk->lock_object.lo_name, file, line);
1222 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1230 lockmgr_chain(struct thread *td, struct thread **ownerp)
1236 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1238 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1239 if (lk->lk_lock & LK_SHARE)
1240 db_printf("SHARED (count %ju)\n",
1241 (uintmax_t)LK_SHARERS(lk->lk_lock));
1243 db_printf("EXCL\n");
1244 *ownerp = lockmgr_xholder(lk);
1250 db_show_lockmgr(struct lock_object *lock)
1255 lk = (struct lock *)lock;
1257 db_printf(" state: ");
1258 if (lk->lk_lock == LK_UNLOCKED)
1259 db_printf("UNLOCKED\n");
1260 else if (lk->lk_lock & LK_SHARE)
1261 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1263 td = lockmgr_xholder(lk);
1264 if (td == (struct thread *)LK_KERNPROC)
1265 db_printf("XLOCK: LK_KERNPROC\n");
1267 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1268 td->td_tid, td->td_proc->p_pid,
1269 td->td_proc->p_comm);
1270 if (lockmgr_recursed(lk))
1271 db_printf(" recursed: %d\n", lk->lk_recurse);
1273 db_printf(" waiters: ");
1274 switch (lk->lk_lock & LK_ALL_WAITERS) {
1275 case LK_SHARED_WAITERS:
1276 db_printf("shared\n");
1278 case LK_EXCLUSIVE_WAITERS:
1279 db_printf("exclusive\n");
1281 case LK_ALL_WAITERS:
1282 db_printf("shared and exclusive\n");
1285 db_printf("none\n");
1287 db_printf(" spinners: ");
1288 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1289 db_printf("exclusive\n");
1291 db_printf("none\n");