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>
39 #include <sys/lock_profile.h>
40 #include <sys/lockmgr.h>
41 #include <sys/mutex.h>
43 #include <sys/sleepqueue.h>
45 #include <sys/stack.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
50 #include <machine/cpu.h>
56 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
57 (LK_ADAPTIVE | LK_NOSHARE));
58 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
59 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
61 #define SQ_EXCLUSIVE_QUEUE 0
62 #define SQ_SHARED_QUEUE 1
65 #define _lockmgr_assert(lk, what, file, line)
66 #define TD_LOCKS_INC(td)
67 #define TD_LOCKS_DEC(td)
69 #define TD_LOCKS_INC(td) ((td)->td_locks++)
70 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
72 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
73 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
76 #define STACK_PRINT(lk)
77 #define STACK_SAVE(lk)
78 #define STACK_ZERO(lk)
80 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
81 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
82 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
85 #define LOCK_LOG2(lk, string, arg1, arg2) \
86 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
87 CTR2(KTR_LOCK, (string), (arg1), (arg2))
88 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
89 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
90 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
92 #define GIANT_DECLARE \
94 WITNESS_SAVE_DECL(Giant)
95 #define GIANT_RESTORE() do { \
99 WITNESS_RESTORE(&Giant.lock_object, Giant); \
102 #define GIANT_SAVE() do { \
103 if (mtx_owned(&Giant)) { \
104 WITNESS_SAVE(&Giant.lock_object, Giant); \
105 while (mtx_owned(&Giant)) { \
107 mtx_unlock(&Giant); \
112 #define LK_CAN_SHARE(x) \
113 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
114 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
115 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
116 #define LK_TRYOP(x) \
119 #define LK_CAN_WITNESS(x) \
120 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
121 #define LK_TRYWIT(x) \
122 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
124 #define LK_CAN_ADAPT(lk, f) \
125 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
126 ((f) & LK_SLEEPFAIL) == 0)
128 #define lockmgr_disowned(lk) \
129 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
131 #define lockmgr_xlocked(lk) \
132 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
134 static void assert_lockmgr(struct lock_object *lock, int how);
136 static void db_show_lockmgr(struct lock_object *lock);
138 static void lock_lockmgr(struct lock_object *lock, int how);
140 static int owner_lockmgr(struct lock_object *lock, struct thread **owner);
142 static int unlock_lockmgr(struct lock_object *lock);
144 struct lock_class lock_class_lockmgr = {
145 .lc_name = "lockmgr",
146 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
147 .lc_assert = assert_lockmgr,
149 .lc_ddb_show = db_show_lockmgr,
151 .lc_lock = lock_lockmgr,
152 .lc_unlock = unlock_lockmgr,
154 .lc_owner = owner_lockmgr,
158 #ifdef ADAPTIVE_LOCKMGRS
159 static u_int alk_retries = 10;
160 static u_int alk_loops = 10000;
161 SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL, "lockmgr debugging");
162 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, "");
163 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, "");
166 static __inline struct thread *
167 lockmgr_xholder(struct lock *lk)
172 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
176 * It assumes sleepq_lock held and returns with this one unheld.
177 * It also assumes the generic interlock is sane and previously checked.
178 * If LK_INTERLOCK is specified the interlock is not reacquired after the
182 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
183 const char *wmesg, int pri, int timo, int queue)
186 struct lock_class *class;
189 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
190 catch = pri & PCATCH;
194 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
195 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
197 if (flags & LK_INTERLOCK)
198 class->lc_unlock(ilk);
199 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
202 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
203 SLEEPQ_INTERRUPTIBLE : 0), queue);
204 if ((flags & LK_TIMELOCK) && timo)
205 sleepq_set_timeout(&lk->lock_object, timo);
208 * Decisional switch for real sleeping.
210 if ((flags & LK_TIMELOCK) && timo && catch)
211 error = sleepq_timedwait_sig(&lk->lock_object, pri);
212 else if ((flags & LK_TIMELOCK) && timo)
213 error = sleepq_timedwait(&lk->lock_object, pri);
215 error = sleepq_wait_sig(&lk->lock_object, pri);
217 sleepq_wait(&lk->lock_object, pri);
219 if ((flags & LK_SLEEPFAIL) && error == 0)
226 wakeupshlk(struct lock *lk, const char *file, int line)
230 int queue, wakeup_swapper;
232 TD_LOCKS_DEC(curthread);
233 TD_SLOCKS_DEC(curthread);
234 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
235 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
242 * If there is more than one shared lock held, just drop one
245 if (LK_SHARERS(x) > 1) {
246 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
253 * If there are not waiters on the exclusive queue, drop the
256 if ((x & LK_ALL_WAITERS) == 0) {
257 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
259 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
265 * We should have a sharer with waiters, so enter the hard
266 * path in order to handle wakeups correctly.
268 sleepq_lock(&lk->lock_object);
269 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
273 * If the lock has exclusive waiters, give them preference in
274 * order to avoid deadlock with shared runners up.
275 * If interruptible sleeps left the exclusive queue empty
276 * avoid a starvation for the threads sleeping on the shared
277 * queue by giving them precedence and cleaning up the
278 * exclusive waiters bit anyway.
279 * Please note that lk_exslpfail count may be lying about
280 * the real number of waiters with the LK_SLEEPFAIL flag on
281 * because they may be used in conjuction with interruptible
282 * sleeps so lk_exslpfail might be considered an 'upper limit'
283 * bound, including the edge cases.
285 realexslp = sleepq_sleepcnt(&lk->lock_object,
287 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
288 if (lk->lk_exslpfail < realexslp) {
289 lk->lk_exslpfail = 0;
290 queue = SQ_EXCLUSIVE_QUEUE;
291 v |= (x & LK_SHARED_WAITERS);
293 lk->lk_exslpfail = 0;
295 "%s: %p has only LK_SLEEPFAIL sleepers",
298 "%s: %p waking up threads on the exclusive queue",
301 sleepq_broadcast(&lk->lock_object,
302 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
303 queue = SQ_SHARED_QUEUE;
309 * Exclusive waiters sleeping with LK_SLEEPFAIL on
310 * and using interruptible sleeps/timeout may have
311 * left spourious lk_exslpfail counts on, so clean
314 lk->lk_exslpfail = 0;
315 queue = SQ_SHARED_QUEUE;
318 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
320 sleepq_release(&lk->lock_object);
323 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
324 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
326 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
328 sleepq_release(&lk->lock_object);
332 lock_profile_release_lock(&lk->lock_object);
333 return (wakeup_swapper);
337 assert_lockmgr(struct lock_object *lock, int what)
340 panic("lockmgr locks do not support assertions");
344 lock_lockmgr(struct lock_object *lock, int how)
347 panic("lockmgr locks do not support sleep interlocking");
351 unlock_lockmgr(struct lock_object *lock)
354 panic("lockmgr locks do not support sleep interlocking");
359 owner_lockmgr(struct lock_object *lock, struct thread **owner)
362 panic("lockmgr locks do not support owner inquiring");
367 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
371 MPASS((flags & ~LK_INIT_MASK) == 0);
372 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
373 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
376 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
377 if (flags & LK_CANRECURSE)
378 iflags |= LO_RECURSABLE;
379 if ((flags & LK_NODUP) == 0)
381 if (flags & LK_NOPROFILE)
382 iflags |= LO_NOPROFILE;
383 if ((flags & LK_NOWITNESS) == 0)
384 iflags |= LO_WITNESS;
385 if (flags & LK_QUIET)
387 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
389 lk->lk_lock = LK_UNLOCKED;
391 lk->lk_exslpfail = 0;
394 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
399 * XXX: Gross hacks to manipulate external lock flags after
400 * initialization. Used for certain vnode and buf locks.
403 lockallowshare(struct lock *lk)
406 lockmgr_assert(lk, KA_XLOCKED);
407 lk->lock_object.lo_flags &= ~LK_NOSHARE;
411 lockallowrecurse(struct lock *lk)
414 lockmgr_assert(lk, KA_XLOCKED);
415 lk->lock_object.lo_flags |= LO_RECURSABLE;
419 lockdisablerecurse(struct lock *lk)
422 lockmgr_assert(lk, KA_XLOCKED);
423 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
427 lockdestroy(struct lock *lk)
430 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
431 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
432 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
433 lock_destroy(&lk->lock_object);
437 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
438 const char *wmesg, int pri, int timo, const char *file, int line)
441 struct lock_class *class;
445 int error, ipri, itimo, queue, wakeup_swapper;
446 #ifdef LOCK_PROFILING
447 uint64_t waittime = 0;
450 #ifdef ADAPTIVE_LOCKMGRS
451 volatile struct thread *owner;
452 u_int i, spintries = 0;
456 tid = (uintptr_t)curthread;
457 op = (flags & LK_TYPE_MASK);
458 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
459 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
460 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
462 MPASS((flags & ~LK_TOTAL_MASK) == 0);
463 KASSERT((op & (op - 1)) == 0,
464 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
465 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
466 (op != LK_DOWNGRADE && op != LK_RELEASE),
467 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
468 __func__, file, line));
469 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
470 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
471 __func__, file, line));
473 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
474 if (panicstr != NULL) {
475 if (flags & LK_INTERLOCK)
476 class->lc_unlock(ilk);
480 if (op == LK_SHARED && (lk->lock_object.lo_flags & LK_NOSHARE))
486 if (LK_CAN_WITNESS(flags))
487 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
493 * If no other thread has an exclusive lock, or
494 * no exclusive waiter is present, bump the count of
495 * sharers. Since we have to preserve the state of
496 * waiters, if we fail to acquire the shared lock
497 * loop back and retry.
499 if (LK_CAN_SHARE(x)) {
500 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
505 lock_profile_obtain_lock_failed(&lk->lock_object,
506 &contested, &waittime);
509 * If the lock is already held by curthread in
510 * exclusive way avoid a deadlock.
512 if (LK_HOLDER(x) == tid) {
514 "%s: %p already held in exclusive mode",
521 * If the lock is expected to not sleep just give up
524 if (LK_TRYOP(flags)) {
525 LOCK_LOG2(lk, "%s: %p fails the try operation",
531 #ifdef ADAPTIVE_LOCKMGRS
533 * If the owner is running on another CPU, spin until
534 * the owner stops running or the state of the lock
535 * changes. We need a double-state handle here
536 * because for a failed acquisition the lock can be
537 * either held in exclusive mode or shared mode
538 * (for the writer starvation avoidance technique).
540 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
541 LK_HOLDER(x) != LK_KERNPROC) {
542 owner = (struct thread *)LK_HOLDER(x);
543 if (LOCK_LOG_TEST(&lk->lock_object, 0))
545 "%s: spinning on %p held by %p",
546 __func__, lk, owner);
549 * If we are holding also an interlock drop it
550 * in order to avoid a deadlock if the lockmgr
551 * owner is adaptively spinning on the
554 if (flags & LK_INTERLOCK) {
555 class->lc_unlock(ilk);
556 flags &= ~LK_INTERLOCK;
559 while (LK_HOLDER(lk->lk_lock) ==
560 (uintptr_t)owner && TD_IS_RUNNING(owner))
564 } else if (LK_CAN_ADAPT(lk, flags) &&
565 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
566 spintries < alk_retries) {
567 if (flags & LK_INTERLOCK) {
568 class->lc_unlock(ilk);
569 flags &= ~LK_INTERLOCK;
573 for (i = 0; i < alk_loops; i++) {
574 if (LOCK_LOG_TEST(&lk->lock_object, 0))
576 "%s: shared spinning on %p with %u and %u",
577 __func__, lk, spintries, i);
579 if ((x & LK_SHARE) == 0 ||
580 LK_CAN_SHARE(x) != 0)
591 * Acquire the sleepqueue chain lock because we
592 * probabilly will need to manipulate waiters flags.
594 sleepq_lock(&lk->lock_object);
598 * if the lock can be acquired in shared mode, try
601 if (LK_CAN_SHARE(x)) {
602 sleepq_release(&lk->lock_object);
606 #ifdef ADAPTIVE_LOCKMGRS
608 * The current lock owner might have started executing
609 * on another CPU (or the lock could have changed
610 * owner) while we were waiting on the turnstile
611 * chain lock. If so, drop the turnstile lock and try
614 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
615 LK_HOLDER(x) != LK_KERNPROC) {
616 owner = (struct thread *)LK_HOLDER(x);
617 if (TD_IS_RUNNING(owner)) {
618 sleepq_release(&lk->lock_object);
625 * Try to set the LK_SHARED_WAITERS flag. If we fail,
626 * loop back and retry.
628 if ((x & LK_SHARED_WAITERS) == 0) {
629 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
630 x | LK_SHARED_WAITERS)) {
631 sleepq_release(&lk->lock_object);
634 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
639 * As far as we have been unable to acquire the
640 * shared lock and the shared waiters flag is set,
643 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
645 flags &= ~LK_INTERLOCK;
648 "%s: interrupted sleep for %p with %d",
649 __func__, lk, error);
652 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
656 lock_profile_obtain_lock_success(&lk->lock_object,
657 contested, waittime, file, line);
658 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
660 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
662 TD_LOCKS_INC(curthread);
663 TD_SLOCKS_INC(curthread);
668 _lockmgr_assert(lk, KA_SLOCKED, file, line);
670 x = v & LK_ALL_WAITERS;
671 v &= LK_EXCLUSIVE_SPINNERS;
674 * Try to switch from one shared lock to an exclusive one.
675 * We need to preserve waiters flags during the operation.
677 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
679 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
681 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
682 LK_TRYWIT(flags), file, line);
683 TD_SLOCKS_DEC(curthread);
688 * We have been unable to succeed in upgrading, so just
689 * give up the shared lock.
691 wakeup_swapper |= wakeupshlk(lk, file, line);
695 if (LK_CAN_WITNESS(flags))
696 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
697 LOP_EXCLUSIVE, file, line, ilk);
700 * If curthread already holds the lock and this one is
701 * allowed to recurse, simply recurse on it.
703 if (lockmgr_xlocked(lk)) {
704 if ((flags & LK_CANRECURSE) == 0 &&
705 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
708 * If the lock is expected to not panic just
709 * give up and return.
711 if (LK_TRYOP(flags)) {
713 "%s: %p fails the try operation",
718 if (flags & LK_INTERLOCK)
719 class->lc_unlock(ilk);
720 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
721 __func__, iwmesg, file, line);
724 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
725 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
726 lk->lk_recurse, file, line);
727 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
728 LK_TRYWIT(flags), file, line);
729 TD_LOCKS_INC(curthread);
733 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
735 lock_profile_obtain_lock_failed(&lk->lock_object,
736 &contested, &waittime);
739 * If the lock is expected to not sleep just give up
742 if (LK_TRYOP(flags)) {
743 LOCK_LOG2(lk, "%s: %p fails the try operation",
749 #ifdef ADAPTIVE_LOCKMGRS
751 * If the owner is running on another CPU, spin until
752 * the owner stops running or the state of the lock
756 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
757 LK_HOLDER(x) != LK_KERNPROC) {
758 owner = (struct thread *)LK_HOLDER(x);
759 if (LOCK_LOG_TEST(&lk->lock_object, 0))
761 "%s: spinning on %p held by %p",
762 __func__, lk, owner);
765 * If we are holding also an interlock drop it
766 * in order to avoid a deadlock if the lockmgr
767 * owner is adaptively spinning on the
770 if (flags & LK_INTERLOCK) {
771 class->lc_unlock(ilk);
772 flags &= ~LK_INTERLOCK;
775 while (LK_HOLDER(lk->lk_lock) ==
776 (uintptr_t)owner && TD_IS_RUNNING(owner))
780 } else if (LK_CAN_ADAPT(lk, flags) &&
781 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
782 spintries < alk_retries) {
783 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
784 !atomic_cmpset_ptr(&lk->lk_lock, x,
785 x | LK_EXCLUSIVE_SPINNERS))
787 if (flags & LK_INTERLOCK) {
788 class->lc_unlock(ilk);
789 flags &= ~LK_INTERLOCK;
793 for (i = 0; i < alk_loops; i++) {
794 if (LOCK_LOG_TEST(&lk->lock_object, 0))
796 "%s: shared spinning on %p with %u and %u",
797 __func__, lk, spintries, i);
799 LK_EXCLUSIVE_SPINNERS) == 0)
810 * Acquire the sleepqueue chain lock because we
811 * probabilly will need to manipulate waiters flags.
813 sleepq_lock(&lk->lock_object);
817 * if the lock has been released while we spun on
818 * the sleepqueue chain lock just try again.
820 if (x == LK_UNLOCKED) {
821 sleepq_release(&lk->lock_object);
825 #ifdef ADAPTIVE_LOCKMGRS
827 * The current lock owner might have started executing
828 * on another CPU (or the lock could have changed
829 * owner) while we were waiting on the turnstile
830 * chain lock. If so, drop the turnstile lock and try
833 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
834 LK_HOLDER(x) != LK_KERNPROC) {
835 owner = (struct thread *)LK_HOLDER(x);
836 if (TD_IS_RUNNING(owner)) {
837 sleepq_release(&lk->lock_object);
844 * The lock can be in the state where there is a
845 * pending queue of waiters, but still no owner.
846 * This happens when the lock is contested and an
847 * owner is going to claim the lock.
848 * If curthread is the one successfully acquiring it
849 * claim lock ownership and return, preserving waiters
852 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
853 if ((x & ~v) == LK_UNLOCKED) {
854 v &= ~LK_EXCLUSIVE_SPINNERS;
855 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
857 sleepq_release(&lk->lock_object);
859 "%s: %p claimed by a new writer",
863 sleepq_release(&lk->lock_object);
868 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
869 * fail, loop back and retry.
871 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
872 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
873 x | LK_EXCLUSIVE_WAITERS)) {
874 sleepq_release(&lk->lock_object);
877 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
882 * As far as we have been unable to acquire the
883 * exclusive lock and the exclusive waiters flag
884 * is set, we will sleep.
886 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
888 flags &= ~LK_INTERLOCK;
891 "%s: interrupted sleep for %p with %d",
892 __func__, lk, error);
895 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
899 lock_profile_obtain_lock_success(&lk->lock_object,
900 contested, waittime, file, line);
901 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
902 lk->lk_recurse, file, line);
903 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
904 LK_TRYWIT(flags), file, line);
905 TD_LOCKS_INC(curthread);
910 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
911 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
912 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
913 TD_SLOCKS_INC(curthread);
916 * In order to preserve waiters flags, just spin.
920 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
922 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
923 LK_SHARERS_LOCK(1) | x))
929 _lockmgr_assert(lk, KA_LOCKED, file, line);
932 if ((x & LK_SHARE) == 0) {
935 * As first option, treact the lock as if it has not
937 * Fix-up the tid var if the lock has been disowned.
939 if (LK_HOLDER(x) == LK_KERNPROC)
942 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
944 TD_LOCKS_DEC(curthread);
946 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
947 lk->lk_recurse, file, line);
950 * The lock is held in exclusive mode.
951 * If the lock is recursed also, then unrecurse it.
953 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
954 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
959 if (tid != LK_KERNPROC)
960 lock_profile_release_lock(&lk->lock_object);
962 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
966 sleepq_lock(&lk->lock_object);
971 * If the lock has exclusive waiters, give them
972 * preference in order to avoid deadlock with
974 * If interruptible sleeps left the exclusive queue
975 * empty avoid a starvation for the threads sleeping
976 * on the shared queue by giving them precedence
977 * and cleaning up the exclusive waiters bit anyway.
978 * Please note that lk_exslpfail count may be lying
979 * about the real number of waiters with the
980 * LK_SLEEPFAIL flag on because they may be used in
981 * conjuction with interruptible sleeps so
982 * lk_exslpfail might be considered an 'upper limit'
983 * bound, including the edge cases.
985 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
986 realexslp = sleepq_sleepcnt(&lk->lock_object,
988 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
989 if (lk->lk_exslpfail < realexslp) {
990 lk->lk_exslpfail = 0;
991 queue = SQ_EXCLUSIVE_QUEUE;
992 v |= (x & LK_SHARED_WAITERS);
994 lk->lk_exslpfail = 0;
996 "%s: %p has only LK_SLEEPFAIL sleepers",
999 "%s: %p waking up threads on the exclusive queue",
1002 sleepq_broadcast(&lk->lock_object,
1003 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1004 queue = SQ_SHARED_QUEUE;
1009 * Exclusive waiters sleeping with LK_SLEEPFAIL
1010 * on and using interruptible sleeps/timeout
1011 * may have left spourious lk_exslpfail counts
1012 * on, so clean it up anyway.
1014 lk->lk_exslpfail = 0;
1015 queue = SQ_SHARED_QUEUE;
1019 "%s: %p waking up threads on the %s queue",
1020 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1022 atomic_store_rel_ptr(&lk->lk_lock, v);
1023 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1024 SLEEPQ_LK, 0, queue);
1025 sleepq_release(&lk->lock_object);
1028 wakeup_swapper = wakeupshlk(lk, file, line);
1031 if (LK_CAN_WITNESS(flags))
1032 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1033 LOP_EXCLUSIVE, file, line, ilk);
1036 * Trying to drain a lock we already own will result in a
1039 if (lockmgr_xlocked(lk)) {
1040 if (flags & LK_INTERLOCK)
1041 class->lc_unlock(ilk);
1042 panic("%s: draining %s with the lock held @ %s:%d\n",
1043 __func__, iwmesg, file, line);
1046 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1047 lock_profile_obtain_lock_failed(&lk->lock_object,
1048 &contested, &waittime);
1051 * If the lock is expected to not sleep just give up
1054 if (LK_TRYOP(flags)) {
1055 LOCK_LOG2(lk, "%s: %p fails the try operation",
1062 * Acquire the sleepqueue chain lock because we
1063 * probabilly will need to manipulate waiters flags.
1065 sleepq_lock(&lk->lock_object);
1069 * if the lock has been released while we spun on
1070 * the sleepqueue chain lock just try again.
1072 if (x == LK_UNLOCKED) {
1073 sleepq_release(&lk->lock_object);
1077 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1078 if ((x & ~v) == LK_UNLOCKED) {
1079 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1082 * If interruptible sleeps left the exclusive
1083 * queue empty avoid a starvation for the
1084 * threads sleeping on the shared queue by
1085 * giving them precedence and cleaning up the
1086 * exclusive waiters bit anyway.
1087 * Please note that lk_exslpfail count may be
1088 * lying about the real number of waiters with
1089 * the LK_SLEEPFAIL flag on because they may
1090 * be used in conjuction with interruptible
1091 * sleeps so lk_exslpfail might be considered
1092 * an 'upper limit' bound, including the edge
1095 if (v & LK_EXCLUSIVE_WAITERS) {
1096 queue = SQ_EXCLUSIVE_QUEUE;
1097 v &= ~LK_EXCLUSIVE_WAITERS;
1101 * Exclusive waiters sleeping with
1102 * LK_SLEEPFAIL on and using
1103 * interruptible sleeps/timeout may
1104 * have left spourious lk_exslpfail
1105 * counts on, so clean it up anyway.
1107 MPASS(v & LK_SHARED_WAITERS);
1108 lk->lk_exslpfail = 0;
1109 queue = SQ_SHARED_QUEUE;
1110 v &= ~LK_SHARED_WAITERS;
1112 if (queue == SQ_EXCLUSIVE_QUEUE) {
1114 sleepq_sleepcnt(&lk->lock_object,
1115 SQ_EXCLUSIVE_QUEUE);
1116 if (lk->lk_exslpfail >= realexslp) {
1117 lk->lk_exslpfail = 0;
1118 queue = SQ_SHARED_QUEUE;
1119 v &= ~LK_SHARED_WAITERS;
1120 if (realexslp != 0) {
1122 "%s: %p has only LK_SLEEPFAIL sleepers",
1125 "%s: %p waking up threads on the exclusive queue",
1131 SQ_EXCLUSIVE_QUEUE);
1134 lk->lk_exslpfail = 0;
1136 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1137 sleepq_release(&lk->lock_object);
1141 "%s: %p waking up all threads on the %s queue",
1142 __func__, lk, queue == SQ_SHARED_QUEUE ?
1143 "shared" : "exclusive");
1144 wakeup_swapper |= sleepq_broadcast(
1145 &lk->lock_object, SLEEPQ_LK, 0, queue);
1148 * If shared waiters have been woken up we need
1149 * to wait for one of them to acquire the lock
1150 * before to set the exclusive waiters in
1151 * order to avoid a deadlock.
1153 if (queue == SQ_SHARED_QUEUE) {
1154 for (v = lk->lk_lock;
1155 (v & LK_SHARE) && !LK_SHARERS(v);
1162 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1163 * fail, loop back and retry.
1165 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1166 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1167 x | LK_EXCLUSIVE_WAITERS)) {
1168 sleepq_release(&lk->lock_object);
1171 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1176 * As far as we have been unable to acquire the
1177 * exclusive lock and the exclusive waiters flag
1178 * is set, we will sleep.
1180 if (flags & LK_INTERLOCK) {
1181 class->lc_unlock(ilk);
1182 flags &= ~LK_INTERLOCK;
1185 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1186 SQ_EXCLUSIVE_QUEUE);
1187 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1189 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1194 lock_profile_obtain_lock_success(&lk->lock_object,
1195 contested, waittime, file, line);
1196 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1197 lk->lk_recurse, file, line);
1198 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1199 LK_TRYWIT(flags), file, line);
1200 TD_LOCKS_INC(curthread);
1205 if (flags & LK_INTERLOCK)
1206 class->lc_unlock(ilk);
1207 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1210 if (flags & LK_INTERLOCK)
1211 class->lc_unlock(ilk);
1219 _lockmgr_disown(struct lock *lk, const char *file, int line)
1223 tid = (uintptr_t)curthread;
1224 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1227 * If the owner is already LK_KERNPROC just skip the whole operation.
1229 if (LK_HOLDER(lk->lk_lock) != tid)
1231 lock_profile_release_lock(&lk->lock_object);
1232 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1233 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1234 TD_LOCKS_DEC(curthread);
1238 * In order to preserve waiters flags, just spin.
1242 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1243 x &= LK_ALL_WAITERS;
1244 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1252 lockmgr_printinfo(struct lock *lk)
1257 if (lk->lk_lock == LK_UNLOCKED)
1258 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1259 else if (lk->lk_lock & LK_SHARE)
1260 printf("lock type %s: SHARED (count %ju)\n",
1261 lk->lock_object.lo_name,
1262 (uintmax_t)LK_SHARERS(lk->lk_lock));
1264 td = lockmgr_xholder(lk);
1265 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1266 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1270 if (x & LK_EXCLUSIVE_WAITERS)
1271 printf(" with exclusive waiters pending\n");
1272 if (x & LK_SHARED_WAITERS)
1273 printf(" with shared waiters pending\n");
1274 if (x & LK_EXCLUSIVE_SPINNERS)
1275 printf(" with exclusive spinners pending\n");
1281 lockstatus(struct lock *lk)
1290 if ((x & LK_SHARE) == 0) {
1291 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1295 } else if (x == LK_UNLOCKED)
1301 #ifdef INVARIANT_SUPPORT
1303 FEATURE(invariant_support,
1304 "Support for modules compiled with INVARIANTS option");
1307 #undef _lockmgr_assert
1311 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1315 if (panicstr != NULL)
1319 case KA_SLOCKED | KA_NOTRECURSED:
1320 case KA_SLOCKED | KA_RECURSED:
1323 case KA_LOCKED | KA_NOTRECURSED:
1324 case KA_LOCKED | KA_RECURSED:
1328 * We cannot trust WITNESS if the lock is held in exclusive
1329 * mode and a call to lockmgr_disown() happened.
1330 * Workaround this skipping the check if the lock is held in
1331 * exclusive mode even for the KA_LOCKED case.
1333 if (slocked || (lk->lk_lock & LK_SHARE)) {
1334 witness_assert(&lk->lock_object, what, file, line);
1338 if (lk->lk_lock == LK_UNLOCKED ||
1339 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1340 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1341 panic("Lock %s not %slocked @ %s:%d\n",
1342 lk->lock_object.lo_name, slocked ? "share" : "",
1345 if ((lk->lk_lock & LK_SHARE) == 0) {
1346 if (lockmgr_recursed(lk)) {
1347 if (what & KA_NOTRECURSED)
1348 panic("Lock %s recursed @ %s:%d\n",
1349 lk->lock_object.lo_name, file,
1351 } else if (what & KA_RECURSED)
1352 panic("Lock %s not recursed @ %s:%d\n",
1353 lk->lock_object.lo_name, file, line);
1357 case KA_XLOCKED | KA_NOTRECURSED:
1358 case KA_XLOCKED | KA_RECURSED:
1359 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1360 panic("Lock %s not exclusively locked @ %s:%d\n",
1361 lk->lock_object.lo_name, file, line);
1362 if (lockmgr_recursed(lk)) {
1363 if (what & KA_NOTRECURSED)
1364 panic("Lock %s recursed @ %s:%d\n",
1365 lk->lock_object.lo_name, file, line);
1366 } else if (what & KA_RECURSED)
1367 panic("Lock %s not recursed @ %s:%d\n",
1368 lk->lock_object.lo_name, file, line);
1371 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1372 panic("Lock %s exclusively locked @ %s:%d\n",
1373 lk->lock_object.lo_name, file, line);
1376 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1384 lockmgr_chain(struct thread *td, struct thread **ownerp)
1390 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1392 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1393 if (lk->lk_lock & LK_SHARE)
1394 db_printf("SHARED (count %ju)\n",
1395 (uintmax_t)LK_SHARERS(lk->lk_lock));
1397 db_printf("EXCL\n");
1398 *ownerp = lockmgr_xholder(lk);
1404 db_show_lockmgr(struct lock_object *lock)
1409 lk = (struct lock *)lock;
1411 db_printf(" state: ");
1412 if (lk->lk_lock == LK_UNLOCKED)
1413 db_printf("UNLOCKED\n");
1414 else if (lk->lk_lock & LK_SHARE)
1415 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1417 td = lockmgr_xholder(lk);
1418 if (td == (struct thread *)LK_KERNPROC)
1419 db_printf("XLOCK: LK_KERNPROC\n");
1421 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1422 td->td_tid, td->td_proc->p_pid,
1423 td->td_proc->p_comm);
1424 if (lockmgr_recursed(lk))
1425 db_printf(" recursed: %d\n", lk->lk_recurse);
1427 db_printf(" waiters: ");
1428 switch (lk->lk_lock & LK_ALL_WAITERS) {
1429 case LK_SHARED_WAITERS:
1430 db_printf("shared\n");
1432 case LK_EXCLUSIVE_WAITERS:
1433 db_printf("exclusive\n");
1435 case LK_ALL_WAITERS:
1436 db_printf("shared and exclusive\n");
1439 db_printf("none\n");
1441 db_printf(" spinners: ");
1442 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1443 db_printf("exclusive\n");
1445 db_printf("none\n");