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_hwpmc_hooks.h"
32 #include "opt_kdtrace.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/mutex.h>
45 #include <sys/sleepqueue.h>
47 #include <sys/stack.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
52 #include <machine/cpu.h>
59 #include <sys/pmckern.h>
60 PMC_SOFT_DECLARE( , , lock, failed);
63 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
64 (LK_ADAPTIVE | LK_NOSHARE));
65 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
66 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
68 #define SQ_EXCLUSIVE_QUEUE 0
69 #define SQ_SHARED_QUEUE 1
72 #define _lockmgr_assert(lk, what, file, line)
73 #define TD_LOCKS_INC(td)
74 #define TD_LOCKS_DEC(td)
76 #define TD_LOCKS_INC(td) ((td)->td_locks++)
77 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
79 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
80 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
83 #define STACK_PRINT(lk)
84 #define STACK_SAVE(lk)
85 #define STACK_ZERO(lk)
87 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
88 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
89 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
92 #define LOCK_LOG2(lk, string, arg1, arg2) \
93 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
94 CTR2(KTR_LOCK, (string), (arg1), (arg2))
95 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
96 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
97 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
99 #define GIANT_DECLARE \
101 WITNESS_SAVE_DECL(Giant)
102 #define GIANT_RESTORE() do { \
106 WITNESS_RESTORE(&Giant.lock_object, Giant); \
109 #define GIANT_SAVE() do { \
110 if (mtx_owned(&Giant)) { \
111 WITNESS_SAVE(&Giant.lock_object, Giant); \
112 while (mtx_owned(&Giant)) { \
114 mtx_unlock(&Giant); \
119 #define LK_CAN_SHARE(x) \
120 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
121 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
122 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
123 #define LK_TRYOP(x) \
126 #define LK_CAN_WITNESS(x) \
127 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
128 #define LK_TRYWIT(x) \
129 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
131 #define LK_CAN_ADAPT(lk, f) \
132 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
133 ((f) & LK_SLEEPFAIL) == 0)
135 #define lockmgr_disowned(lk) \
136 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
138 #define lockmgr_xlocked(lk) \
139 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
141 static void assert_lockmgr(const struct lock_object *lock, int how);
143 static void db_show_lockmgr(const struct lock_object *lock);
145 static void lock_lockmgr(struct lock_object *lock, int how);
147 static int owner_lockmgr(const struct lock_object *lock,
148 struct thread **owner);
150 static int unlock_lockmgr(struct lock_object *lock);
152 struct lock_class lock_class_lockmgr = {
153 .lc_name = "lockmgr",
154 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
155 .lc_assert = assert_lockmgr,
157 .lc_ddb_show = db_show_lockmgr,
159 .lc_lock = lock_lockmgr,
160 .lc_unlock = unlock_lockmgr,
162 .lc_owner = owner_lockmgr,
166 #ifdef ADAPTIVE_LOCKMGRS
167 static u_int alk_retries = 10;
168 static u_int alk_loops = 10000;
169 static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL,
170 "lockmgr debugging");
171 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, "");
172 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, "");
175 static __inline struct thread *
176 lockmgr_xholder(const struct lock *lk)
181 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
185 * It assumes sleepq_lock held and returns with this one unheld.
186 * It also assumes the generic interlock is sane and previously checked.
187 * If LK_INTERLOCK is specified the interlock is not reacquired after the
191 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
192 const char *wmesg, int pri, int timo, int queue)
195 struct lock_class *class;
198 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
199 catch = pri & PCATCH;
203 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
204 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
206 if (flags & LK_INTERLOCK)
207 class->lc_unlock(ilk);
208 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
211 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
212 SLEEPQ_INTERRUPTIBLE : 0), queue);
213 if ((flags & LK_TIMELOCK) && timo)
214 sleepq_set_timeout(&lk->lock_object, timo);
217 * Decisional switch for real sleeping.
219 if ((flags & LK_TIMELOCK) && timo && catch)
220 error = sleepq_timedwait_sig(&lk->lock_object, pri);
221 else if ((flags & LK_TIMELOCK) && timo)
222 error = sleepq_timedwait(&lk->lock_object, pri);
224 error = sleepq_wait_sig(&lk->lock_object, pri);
226 sleepq_wait(&lk->lock_object, pri);
228 if ((flags & LK_SLEEPFAIL) && error == 0)
235 wakeupshlk(struct lock *lk, const char *file, int line)
239 int queue, wakeup_swapper;
241 TD_LOCKS_DEC(curthread);
242 TD_SLOCKS_DEC(curthread);
243 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
244 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
251 * If there is more than one shared lock held, just drop one
254 if (LK_SHARERS(x) > 1) {
255 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
262 * If there are not waiters on the exclusive queue, drop the
265 if ((x & LK_ALL_WAITERS) == 0) {
266 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
268 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
274 * We should have a sharer with waiters, so enter the hard
275 * path in order to handle wakeups correctly.
277 sleepq_lock(&lk->lock_object);
278 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
282 * If the lock has exclusive waiters, give them preference in
283 * order to avoid deadlock with shared runners up.
284 * If interruptible sleeps left the exclusive queue empty
285 * avoid a starvation for the threads sleeping on the shared
286 * queue by giving them precedence and cleaning up the
287 * exclusive waiters bit anyway.
288 * Please note that lk_exslpfail count may be lying about
289 * the real number of waiters with the LK_SLEEPFAIL flag on
290 * because they may be used in conjuction with interruptible
291 * sleeps so lk_exslpfail might be considered an 'upper limit'
292 * bound, including the edge cases.
294 realexslp = sleepq_sleepcnt(&lk->lock_object,
296 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
297 if (lk->lk_exslpfail < realexslp) {
298 lk->lk_exslpfail = 0;
299 queue = SQ_EXCLUSIVE_QUEUE;
300 v |= (x & LK_SHARED_WAITERS);
302 lk->lk_exslpfail = 0;
304 "%s: %p has only LK_SLEEPFAIL sleepers",
307 "%s: %p waking up threads on the exclusive queue",
310 sleepq_broadcast(&lk->lock_object,
311 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
312 queue = SQ_SHARED_QUEUE;
318 * Exclusive waiters sleeping with LK_SLEEPFAIL on
319 * and using interruptible sleeps/timeout may have
320 * left spourious lk_exslpfail counts on, so clean
323 lk->lk_exslpfail = 0;
324 queue = SQ_SHARED_QUEUE;
327 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
329 sleepq_release(&lk->lock_object);
332 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
333 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
335 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
337 sleepq_release(&lk->lock_object);
341 lock_profile_release_lock(&lk->lock_object);
342 return (wakeup_swapper);
346 assert_lockmgr(const struct lock_object *lock, int what)
349 panic("lockmgr locks do not support assertions");
353 lock_lockmgr(struct lock_object *lock, int how)
356 panic("lockmgr locks do not support sleep interlocking");
360 unlock_lockmgr(struct lock_object *lock)
363 panic("lockmgr locks do not support sleep interlocking");
368 owner_lockmgr(const struct lock_object *lock, struct thread **owner)
371 panic("lockmgr locks do not support owner inquiring");
376 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
380 MPASS((flags & ~LK_INIT_MASK) == 0);
381 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
382 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
385 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
386 if (flags & LK_CANRECURSE)
387 iflags |= LO_RECURSABLE;
388 if ((flags & LK_NODUP) == 0)
390 if (flags & LK_NOPROFILE)
391 iflags |= LO_NOPROFILE;
392 if ((flags & LK_NOWITNESS) == 0)
393 iflags |= LO_WITNESS;
394 if (flags & LK_QUIET)
396 if (flags & LK_IS_VNODE)
397 iflags |= LO_IS_VNODE;
398 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
400 lk->lk_lock = LK_UNLOCKED;
402 lk->lk_exslpfail = 0;
405 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
410 * XXX: Gross hacks to manipulate external lock flags after
411 * initialization. Used for certain vnode and buf locks.
414 lockallowshare(struct lock *lk)
417 lockmgr_assert(lk, KA_XLOCKED);
418 lk->lock_object.lo_flags &= ~LK_NOSHARE;
422 lockallowrecurse(struct lock *lk)
425 lockmgr_assert(lk, KA_XLOCKED);
426 lk->lock_object.lo_flags |= LO_RECURSABLE;
430 lockdisablerecurse(struct lock *lk)
433 lockmgr_assert(lk, KA_XLOCKED);
434 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
438 lockdestroy(struct lock *lk)
441 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
442 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
443 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
444 lock_destroy(&lk->lock_object);
448 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
449 const char *wmesg, int pri, int timo, const char *file, int line)
452 struct lock_class *class;
456 int error, ipri, itimo, queue, wakeup_swapper;
457 #ifdef LOCK_PROFILING
458 uint64_t waittime = 0;
461 #ifdef ADAPTIVE_LOCKMGRS
462 volatile struct thread *owner;
463 u_int i, spintries = 0;
467 tid = (uintptr_t)curthread;
468 op = (flags & LK_TYPE_MASK);
469 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
470 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
471 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
473 MPASS((flags & ~LK_TOTAL_MASK) == 0);
474 KASSERT((op & (op - 1)) == 0,
475 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
476 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
477 (op != LK_DOWNGRADE && op != LK_RELEASE),
478 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
479 __func__, file, line));
480 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
481 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
482 __func__, file, line));
483 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
484 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
485 lk->lock_object.lo_name, file, line));
487 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
488 if (panicstr != NULL) {
489 if (flags & LK_INTERLOCK)
490 class->lc_unlock(ilk);
494 if (lk->lock_object.lo_flags & LK_NOSHARE) {
501 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
503 if (flags & LK_INTERLOCK)
504 class->lc_unlock(ilk);
512 if (LK_CAN_WITNESS(flags))
513 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
519 * If no other thread has an exclusive lock, or
520 * no exclusive waiter is present, bump the count of
521 * sharers. Since we have to preserve the state of
522 * waiters, if we fail to acquire the shared lock
523 * loop back and retry.
525 if (LK_CAN_SHARE(x)) {
526 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
532 PMC_SOFT_CALL( , , lock, failed);
534 lock_profile_obtain_lock_failed(&lk->lock_object,
535 &contested, &waittime);
538 * If the lock is already held by curthread in
539 * exclusive way avoid a deadlock.
541 if (LK_HOLDER(x) == tid) {
543 "%s: %p already held in exclusive mode",
550 * If the lock is expected to not sleep just give up
553 if (LK_TRYOP(flags)) {
554 LOCK_LOG2(lk, "%s: %p fails the try operation",
560 #ifdef ADAPTIVE_LOCKMGRS
562 * If the owner is running on another CPU, spin until
563 * the owner stops running or the state of the lock
564 * changes. We need a double-state handle here
565 * because for a failed acquisition the lock can be
566 * either held in exclusive mode or shared mode
567 * (for the writer starvation avoidance technique).
569 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
570 LK_HOLDER(x) != LK_KERNPROC) {
571 owner = (struct thread *)LK_HOLDER(x);
572 if (LOCK_LOG_TEST(&lk->lock_object, 0))
574 "%s: spinning on %p held by %p",
575 __func__, lk, owner);
578 * If we are holding also an interlock drop it
579 * in order to avoid a deadlock if the lockmgr
580 * owner is adaptively spinning on the
583 if (flags & LK_INTERLOCK) {
584 class->lc_unlock(ilk);
585 flags &= ~LK_INTERLOCK;
588 while (LK_HOLDER(lk->lk_lock) ==
589 (uintptr_t)owner && TD_IS_RUNNING(owner))
593 } else if (LK_CAN_ADAPT(lk, flags) &&
594 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
595 spintries < alk_retries) {
596 if (flags & LK_INTERLOCK) {
597 class->lc_unlock(ilk);
598 flags &= ~LK_INTERLOCK;
602 for (i = 0; i < alk_loops; i++) {
603 if (LOCK_LOG_TEST(&lk->lock_object, 0))
605 "%s: shared spinning on %p with %u and %u",
606 __func__, lk, spintries, i);
608 if ((x & LK_SHARE) == 0 ||
609 LK_CAN_SHARE(x) != 0)
620 * Acquire the sleepqueue chain lock because we
621 * probabilly will need to manipulate waiters flags.
623 sleepq_lock(&lk->lock_object);
627 * if the lock can be acquired in shared mode, try
630 if (LK_CAN_SHARE(x)) {
631 sleepq_release(&lk->lock_object);
635 #ifdef ADAPTIVE_LOCKMGRS
637 * The current lock owner might have started executing
638 * on another CPU (or the lock could have changed
639 * owner) while we were waiting on the turnstile
640 * chain lock. If so, drop the turnstile lock and try
643 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
644 LK_HOLDER(x) != LK_KERNPROC) {
645 owner = (struct thread *)LK_HOLDER(x);
646 if (TD_IS_RUNNING(owner)) {
647 sleepq_release(&lk->lock_object);
654 * Try to set the LK_SHARED_WAITERS flag. If we fail,
655 * loop back and retry.
657 if ((x & LK_SHARED_WAITERS) == 0) {
658 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
659 x | LK_SHARED_WAITERS)) {
660 sleepq_release(&lk->lock_object);
663 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
668 * As far as we have been unable to acquire the
669 * shared lock and the shared waiters flag is set,
672 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
674 flags &= ~LK_INTERLOCK;
677 "%s: interrupted sleep for %p with %d",
678 __func__, lk, error);
681 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
685 lock_profile_obtain_lock_success(&lk->lock_object,
686 contested, waittime, file, line);
687 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
689 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
691 TD_LOCKS_INC(curthread);
692 TD_SLOCKS_INC(curthread);
697 _lockmgr_assert(lk, KA_SLOCKED, file, line);
699 x = v & LK_ALL_WAITERS;
700 v &= LK_EXCLUSIVE_SPINNERS;
703 * Try to switch from one shared lock to an exclusive one.
704 * We need to preserve waiters flags during the operation.
706 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
708 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
710 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
711 LK_TRYWIT(flags), file, line);
712 TD_SLOCKS_DEC(curthread);
717 * We have been unable to succeed in upgrading, so just
718 * give up the shared lock.
720 wakeup_swapper |= wakeupshlk(lk, file, line);
724 if (LK_CAN_WITNESS(flags))
725 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
726 LOP_EXCLUSIVE, file, line, ilk);
729 * If curthread already holds the lock and this one is
730 * allowed to recurse, simply recurse on it.
732 if (lockmgr_xlocked(lk)) {
733 if ((flags & LK_CANRECURSE) == 0 &&
734 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
737 * If the lock is expected to not panic just
738 * give up and return.
740 if (LK_TRYOP(flags)) {
742 "%s: %p fails the try operation",
747 if (flags & LK_INTERLOCK)
748 class->lc_unlock(ilk);
749 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
750 __func__, iwmesg, file, line);
753 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
754 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
755 lk->lk_recurse, file, line);
756 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
757 LK_TRYWIT(flags), file, line);
758 TD_LOCKS_INC(curthread);
762 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
765 PMC_SOFT_CALL( , , lock, failed);
767 lock_profile_obtain_lock_failed(&lk->lock_object,
768 &contested, &waittime);
771 * If the lock is expected to not sleep just give up
774 if (LK_TRYOP(flags)) {
775 LOCK_LOG2(lk, "%s: %p fails the try operation",
781 #ifdef ADAPTIVE_LOCKMGRS
783 * If the owner is running on another CPU, spin until
784 * the owner stops running or the state of the lock
788 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
789 LK_HOLDER(x) != LK_KERNPROC) {
790 owner = (struct thread *)LK_HOLDER(x);
791 if (LOCK_LOG_TEST(&lk->lock_object, 0))
793 "%s: spinning on %p held by %p",
794 __func__, lk, owner);
797 * If we are holding also an interlock drop it
798 * in order to avoid a deadlock if the lockmgr
799 * owner is adaptively spinning on the
802 if (flags & LK_INTERLOCK) {
803 class->lc_unlock(ilk);
804 flags &= ~LK_INTERLOCK;
807 while (LK_HOLDER(lk->lk_lock) ==
808 (uintptr_t)owner && TD_IS_RUNNING(owner))
812 } else if (LK_CAN_ADAPT(lk, flags) &&
813 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
814 spintries < alk_retries) {
815 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
816 !atomic_cmpset_ptr(&lk->lk_lock, x,
817 x | LK_EXCLUSIVE_SPINNERS))
819 if (flags & LK_INTERLOCK) {
820 class->lc_unlock(ilk);
821 flags &= ~LK_INTERLOCK;
825 for (i = 0; i < alk_loops; i++) {
826 if (LOCK_LOG_TEST(&lk->lock_object, 0))
828 "%s: shared spinning on %p with %u and %u",
829 __func__, lk, spintries, i);
831 LK_EXCLUSIVE_SPINNERS) == 0)
842 * Acquire the sleepqueue chain lock because we
843 * probabilly will need to manipulate waiters flags.
845 sleepq_lock(&lk->lock_object);
849 * if the lock has been released while we spun on
850 * the sleepqueue chain lock just try again.
852 if (x == LK_UNLOCKED) {
853 sleepq_release(&lk->lock_object);
857 #ifdef ADAPTIVE_LOCKMGRS
859 * The current lock owner might have started executing
860 * on another CPU (or the lock could have changed
861 * owner) while we were waiting on the turnstile
862 * chain lock. If so, drop the turnstile lock and try
865 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
866 LK_HOLDER(x) != LK_KERNPROC) {
867 owner = (struct thread *)LK_HOLDER(x);
868 if (TD_IS_RUNNING(owner)) {
869 sleepq_release(&lk->lock_object);
876 * The lock can be in the state where there is a
877 * pending queue of waiters, but still no owner.
878 * This happens when the lock is contested and an
879 * owner is going to claim the lock.
880 * If curthread is the one successfully acquiring it
881 * claim lock ownership and return, preserving waiters
884 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
885 if ((x & ~v) == LK_UNLOCKED) {
886 v &= ~LK_EXCLUSIVE_SPINNERS;
887 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
889 sleepq_release(&lk->lock_object);
891 "%s: %p claimed by a new writer",
895 sleepq_release(&lk->lock_object);
900 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
901 * fail, loop back and retry.
903 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
904 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
905 x | LK_EXCLUSIVE_WAITERS)) {
906 sleepq_release(&lk->lock_object);
909 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
914 * As far as we have been unable to acquire the
915 * exclusive lock and the exclusive waiters flag
916 * is set, we will sleep.
918 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
920 flags &= ~LK_INTERLOCK;
923 "%s: interrupted sleep for %p with %d",
924 __func__, lk, error);
927 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
931 lock_profile_obtain_lock_success(&lk->lock_object,
932 contested, waittime, file, line);
933 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
934 lk->lk_recurse, file, line);
935 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
936 LK_TRYWIT(flags), file, line);
937 TD_LOCKS_INC(curthread);
942 _lockmgr_assert(lk, KA_XLOCKED, file, line);
943 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
944 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
947 * Panic if the lock is recursed.
949 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
950 if (flags & LK_INTERLOCK)
951 class->lc_unlock(ilk);
952 panic("%s: downgrade a recursed lockmgr %s @ %s:%d\n",
953 __func__, iwmesg, file, line);
955 TD_SLOCKS_INC(curthread);
958 * In order to preserve waiters flags, just spin.
962 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
964 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
965 LK_SHARERS_LOCK(1) | x))
971 _lockmgr_assert(lk, KA_LOCKED, file, line);
974 if ((x & LK_SHARE) == 0) {
977 * As first option, treact the lock as if it has not
979 * Fix-up the tid var if the lock has been disowned.
981 if (LK_HOLDER(x) == LK_KERNPROC)
984 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
986 TD_LOCKS_DEC(curthread);
988 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
989 lk->lk_recurse, file, line);
992 * The lock is held in exclusive mode.
993 * If the lock is recursed also, then unrecurse it.
995 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
996 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
1001 if (tid != LK_KERNPROC)
1002 lock_profile_release_lock(&lk->lock_object);
1004 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
1008 sleepq_lock(&lk->lock_object);
1013 * If the lock has exclusive waiters, give them
1014 * preference in order to avoid deadlock with
1015 * shared runners up.
1016 * If interruptible sleeps left the exclusive queue
1017 * empty avoid a starvation for the threads sleeping
1018 * on the shared queue by giving them precedence
1019 * and cleaning up the exclusive waiters bit anyway.
1020 * Please note that lk_exslpfail count may be lying
1021 * about the real number of waiters with the
1022 * LK_SLEEPFAIL flag on because they may be used in
1023 * conjuction with interruptible sleeps so
1024 * lk_exslpfail might be considered an 'upper limit'
1025 * bound, including the edge cases.
1027 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1028 realexslp = sleepq_sleepcnt(&lk->lock_object,
1029 SQ_EXCLUSIVE_QUEUE);
1030 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1031 if (lk->lk_exslpfail < realexslp) {
1032 lk->lk_exslpfail = 0;
1033 queue = SQ_EXCLUSIVE_QUEUE;
1034 v |= (x & LK_SHARED_WAITERS);
1036 lk->lk_exslpfail = 0;
1038 "%s: %p has only LK_SLEEPFAIL sleepers",
1041 "%s: %p waking up threads on the exclusive queue",
1044 sleepq_broadcast(&lk->lock_object,
1045 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1046 queue = SQ_SHARED_QUEUE;
1051 * Exclusive waiters sleeping with LK_SLEEPFAIL
1052 * on and using interruptible sleeps/timeout
1053 * may have left spourious lk_exslpfail counts
1054 * on, so clean it up anyway.
1056 lk->lk_exslpfail = 0;
1057 queue = SQ_SHARED_QUEUE;
1061 "%s: %p waking up threads on the %s queue",
1062 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1064 atomic_store_rel_ptr(&lk->lk_lock, v);
1065 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1066 SLEEPQ_LK, 0, queue);
1067 sleepq_release(&lk->lock_object);
1070 wakeup_swapper = wakeupshlk(lk, file, line);
1073 if (LK_CAN_WITNESS(flags))
1074 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1075 LOP_EXCLUSIVE, file, line, ilk);
1078 * Trying to drain a lock we already own will result in a
1081 if (lockmgr_xlocked(lk)) {
1082 if (flags & LK_INTERLOCK)
1083 class->lc_unlock(ilk);
1084 panic("%s: draining %s with the lock held @ %s:%d\n",
1085 __func__, iwmesg, file, line);
1088 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1090 PMC_SOFT_CALL( , , lock, failed);
1092 lock_profile_obtain_lock_failed(&lk->lock_object,
1093 &contested, &waittime);
1096 * If the lock is expected to not sleep just give up
1099 if (LK_TRYOP(flags)) {
1100 LOCK_LOG2(lk, "%s: %p fails the try operation",
1107 * Acquire the sleepqueue chain lock because we
1108 * probabilly will need to manipulate waiters flags.
1110 sleepq_lock(&lk->lock_object);
1114 * if the lock has been released while we spun on
1115 * the sleepqueue chain lock just try again.
1117 if (x == LK_UNLOCKED) {
1118 sleepq_release(&lk->lock_object);
1122 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1123 if ((x & ~v) == LK_UNLOCKED) {
1124 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1127 * If interruptible sleeps left the exclusive
1128 * queue empty avoid a starvation for the
1129 * threads sleeping on the shared queue by
1130 * giving them precedence and cleaning up the
1131 * exclusive waiters bit anyway.
1132 * Please note that lk_exslpfail count may be
1133 * lying about the real number of waiters with
1134 * the LK_SLEEPFAIL flag on because they may
1135 * be used in conjuction with interruptible
1136 * sleeps so lk_exslpfail might be considered
1137 * an 'upper limit' bound, including the edge
1140 if (v & LK_EXCLUSIVE_WAITERS) {
1141 queue = SQ_EXCLUSIVE_QUEUE;
1142 v &= ~LK_EXCLUSIVE_WAITERS;
1146 * Exclusive waiters sleeping with
1147 * LK_SLEEPFAIL on and using
1148 * interruptible sleeps/timeout may
1149 * have left spourious lk_exslpfail
1150 * counts on, so clean it up anyway.
1152 MPASS(v & LK_SHARED_WAITERS);
1153 lk->lk_exslpfail = 0;
1154 queue = SQ_SHARED_QUEUE;
1155 v &= ~LK_SHARED_WAITERS;
1157 if (queue == SQ_EXCLUSIVE_QUEUE) {
1159 sleepq_sleepcnt(&lk->lock_object,
1160 SQ_EXCLUSIVE_QUEUE);
1161 if (lk->lk_exslpfail >= realexslp) {
1162 lk->lk_exslpfail = 0;
1163 queue = SQ_SHARED_QUEUE;
1164 v &= ~LK_SHARED_WAITERS;
1165 if (realexslp != 0) {
1167 "%s: %p has only LK_SLEEPFAIL sleepers",
1170 "%s: %p waking up threads on the exclusive queue",
1176 SQ_EXCLUSIVE_QUEUE);
1179 lk->lk_exslpfail = 0;
1181 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1182 sleepq_release(&lk->lock_object);
1186 "%s: %p waking up all threads on the %s queue",
1187 __func__, lk, queue == SQ_SHARED_QUEUE ?
1188 "shared" : "exclusive");
1189 wakeup_swapper |= sleepq_broadcast(
1190 &lk->lock_object, SLEEPQ_LK, 0, queue);
1193 * If shared waiters have been woken up we need
1194 * to wait for one of them to acquire the lock
1195 * before to set the exclusive waiters in
1196 * order to avoid a deadlock.
1198 if (queue == SQ_SHARED_QUEUE) {
1199 for (v = lk->lk_lock;
1200 (v & LK_SHARE) && !LK_SHARERS(v);
1207 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1208 * fail, loop back and retry.
1210 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1211 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1212 x | LK_EXCLUSIVE_WAITERS)) {
1213 sleepq_release(&lk->lock_object);
1216 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1221 * As far as we have been unable to acquire the
1222 * exclusive lock and the exclusive waiters flag
1223 * is set, we will sleep.
1225 if (flags & LK_INTERLOCK) {
1226 class->lc_unlock(ilk);
1227 flags &= ~LK_INTERLOCK;
1230 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1231 SQ_EXCLUSIVE_QUEUE);
1232 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1234 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1239 lock_profile_obtain_lock_success(&lk->lock_object,
1240 contested, waittime, file, line);
1241 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1242 lk->lk_recurse, file, line);
1243 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1244 LK_TRYWIT(flags), file, line);
1245 TD_LOCKS_INC(curthread);
1250 if (flags & LK_INTERLOCK)
1251 class->lc_unlock(ilk);
1252 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1255 if (flags & LK_INTERLOCK)
1256 class->lc_unlock(ilk);
1264 _lockmgr_disown(struct lock *lk, const char *file, int line)
1268 if (SCHEDULER_STOPPED())
1271 tid = (uintptr_t)curthread;
1272 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1275 * Panic if the lock is recursed.
1277 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk))
1278 panic("%s: disown a recursed lockmgr @ %s:%d\n",
1279 __func__, file, line);
1282 * If the owner is already LK_KERNPROC just skip the whole operation.
1284 if (LK_HOLDER(lk->lk_lock) != tid)
1286 lock_profile_release_lock(&lk->lock_object);
1287 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1288 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1289 TD_LOCKS_DEC(curthread);
1293 * In order to preserve waiters flags, just spin.
1297 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1298 x &= LK_ALL_WAITERS;
1299 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1307 lockmgr_printinfo(const struct lock *lk)
1312 if (lk->lk_lock == LK_UNLOCKED)
1313 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1314 else if (lk->lk_lock & LK_SHARE)
1315 printf("lock type %s: SHARED (count %ju)\n",
1316 lk->lock_object.lo_name,
1317 (uintmax_t)LK_SHARERS(lk->lk_lock));
1319 td = lockmgr_xholder(lk);
1320 printf("lock type %s: EXCL by thread %p "
1321 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name, td,
1322 td->td_proc->p_pid, td->td_proc->p_comm, td->td_tid);
1326 if (x & LK_EXCLUSIVE_WAITERS)
1327 printf(" with exclusive waiters pending\n");
1328 if (x & LK_SHARED_WAITERS)
1329 printf(" with shared waiters pending\n");
1330 if (x & LK_EXCLUSIVE_SPINNERS)
1331 printf(" with exclusive spinners pending\n");
1337 lockstatus(const struct lock *lk)
1346 if ((x & LK_SHARE) == 0) {
1347 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1351 } else if (x == LK_UNLOCKED)
1357 #ifdef INVARIANT_SUPPORT
1359 FEATURE(invariant_support,
1360 "Support for modules compiled with INVARIANTS option");
1363 #undef _lockmgr_assert
1367 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1371 if (panicstr != NULL)
1375 case KA_SLOCKED | KA_NOTRECURSED:
1376 case KA_SLOCKED | KA_RECURSED:
1379 case KA_LOCKED | KA_NOTRECURSED:
1380 case KA_LOCKED | KA_RECURSED:
1384 * We cannot trust WITNESS if the lock is held in exclusive
1385 * mode and a call to lockmgr_disown() happened.
1386 * Workaround this skipping the check if the lock is held in
1387 * exclusive mode even for the KA_LOCKED case.
1389 if (slocked || (lk->lk_lock & LK_SHARE)) {
1390 witness_assert(&lk->lock_object, what, file, line);
1394 if (lk->lk_lock == LK_UNLOCKED ||
1395 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1396 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1397 panic("Lock %s not %slocked @ %s:%d\n",
1398 lk->lock_object.lo_name, slocked ? "share" : "",
1401 if ((lk->lk_lock & LK_SHARE) == 0) {
1402 if (lockmgr_recursed(lk)) {
1403 if (what & KA_NOTRECURSED)
1404 panic("Lock %s recursed @ %s:%d\n",
1405 lk->lock_object.lo_name, file,
1407 } else if (what & KA_RECURSED)
1408 panic("Lock %s not recursed @ %s:%d\n",
1409 lk->lock_object.lo_name, file, line);
1413 case KA_XLOCKED | KA_NOTRECURSED:
1414 case KA_XLOCKED | KA_RECURSED:
1415 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1416 panic("Lock %s not exclusively locked @ %s:%d\n",
1417 lk->lock_object.lo_name, file, line);
1418 if (lockmgr_recursed(lk)) {
1419 if (what & KA_NOTRECURSED)
1420 panic("Lock %s recursed @ %s:%d\n",
1421 lk->lock_object.lo_name, file, line);
1422 } else if (what & KA_RECURSED)
1423 panic("Lock %s not recursed @ %s:%d\n",
1424 lk->lock_object.lo_name, file, line);
1427 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1428 panic("Lock %s exclusively locked @ %s:%d\n",
1429 lk->lock_object.lo_name, file, line);
1432 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1440 lockmgr_chain(struct thread *td, struct thread **ownerp)
1446 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1448 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1449 if (lk->lk_lock & LK_SHARE)
1450 db_printf("SHARED (count %ju)\n",
1451 (uintmax_t)LK_SHARERS(lk->lk_lock));
1453 db_printf("EXCL\n");
1454 *ownerp = lockmgr_xholder(lk);
1460 db_show_lockmgr(const struct lock_object *lock)
1463 const struct lock *lk;
1465 lk = (const struct lock *)lock;
1467 db_printf(" state: ");
1468 if (lk->lk_lock == LK_UNLOCKED)
1469 db_printf("UNLOCKED\n");
1470 else if (lk->lk_lock & LK_SHARE)
1471 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1473 td = lockmgr_xholder(lk);
1474 if (td == (struct thread *)LK_KERNPROC)
1475 db_printf("XLOCK: LK_KERNPROC\n");
1477 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1478 td->td_tid, td->td_proc->p_pid,
1479 td->td_proc->p_comm);
1480 if (lockmgr_recursed(lk))
1481 db_printf(" recursed: %d\n", lk->lk_recurse);
1483 db_printf(" waiters: ");
1484 switch (lk->lk_lock & LK_ALL_WAITERS) {
1485 case LK_SHARED_WAITERS:
1486 db_printf("shared\n");
1488 case LK_EXCLUSIVE_WAITERS:
1489 db_printf("exclusive\n");
1491 case LK_ALL_WAITERS:
1492 db_printf("shared and exclusive\n");
1495 db_printf("none\n");
1497 db_printf(" spinners: ");
1498 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1499 db_printf("exclusive\n");
1501 db_printf("none\n");