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>
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>
58 #include <sys/pmckern.h>
59 PMC_SOFT_DECLARE( , , lock, failed);
62 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
63 (LK_ADAPTIVE | LK_NOSHARE));
64 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
65 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
67 #define SQ_EXCLUSIVE_QUEUE 0
68 #define SQ_SHARED_QUEUE 1
71 #define _lockmgr_assert(lk, what, file, line)
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(const struct lock_object *lock, int how);
142 static void db_show_lockmgr(const struct lock_object *lock);
144 static void lock_lockmgr(struct lock_object *lock, int how);
146 static int owner_lockmgr(const struct lock_object *lock,
147 struct thread **owner);
149 static int unlock_lockmgr(struct lock_object *lock);
151 struct lock_class lock_class_lockmgr = {
152 .lc_name = "lockmgr",
153 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
154 .lc_assert = assert_lockmgr,
156 .lc_ddb_show = db_show_lockmgr,
158 .lc_lock = lock_lockmgr,
159 .lc_unlock = unlock_lockmgr,
161 .lc_owner = owner_lockmgr,
165 #ifdef ADAPTIVE_LOCKMGRS
166 static u_int alk_retries = 10;
167 static u_int alk_loops = 10000;
168 static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL,
169 "lockmgr debugging");
170 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, "");
171 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, "");
174 static __inline struct thread *
175 lockmgr_xholder(const struct lock *lk)
180 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
184 * It assumes sleepq_lock held and returns with this one unheld.
185 * It also assumes the generic interlock is sane and previously checked.
186 * If LK_INTERLOCK is specified the interlock is not reacquired after the
190 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
191 const char *wmesg, int pri, int timo, int queue)
194 struct lock_class *class;
197 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
198 catch = pri & PCATCH;
202 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
203 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
205 if (flags & LK_INTERLOCK)
206 class->lc_unlock(ilk);
207 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
210 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
211 SLEEPQ_INTERRUPTIBLE : 0), queue);
212 if ((flags & LK_TIMELOCK) && timo)
213 sleepq_set_timeout(&lk->lock_object, timo);
216 * Decisional switch for real sleeping.
218 if ((flags & LK_TIMELOCK) && timo && catch)
219 error = sleepq_timedwait_sig(&lk->lock_object, pri);
220 else if ((flags & LK_TIMELOCK) && timo)
221 error = sleepq_timedwait(&lk->lock_object, pri);
223 error = sleepq_wait_sig(&lk->lock_object, pri);
225 sleepq_wait(&lk->lock_object, pri);
227 if ((flags & LK_SLEEPFAIL) && error == 0)
234 wakeupshlk(struct lock *lk, const char *file, int line)
238 int queue, wakeup_swapper;
240 TD_LOCKS_DEC(curthread);
241 TD_SLOCKS_DEC(curthread);
242 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
243 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
250 * If there is more than one shared lock held, just drop one
253 if (LK_SHARERS(x) > 1) {
254 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
261 * If there are not waiters on the exclusive queue, drop the
264 if ((x & LK_ALL_WAITERS) == 0) {
265 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
267 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
273 * We should have a sharer with waiters, so enter the hard
274 * path in order to handle wakeups correctly.
276 sleepq_lock(&lk->lock_object);
277 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
281 * If the lock has exclusive waiters, give them preference in
282 * order to avoid deadlock with shared runners up.
283 * If interruptible sleeps left the exclusive queue empty
284 * avoid a starvation for the threads sleeping on the shared
285 * queue by giving them precedence and cleaning up the
286 * exclusive waiters bit anyway.
287 * Please note that lk_exslpfail count may be lying about
288 * the real number of waiters with the LK_SLEEPFAIL flag on
289 * because they may be used in conjuction with interruptible
290 * sleeps so lk_exslpfail might be considered an 'upper limit'
291 * bound, including the edge cases.
293 realexslp = sleepq_sleepcnt(&lk->lock_object,
295 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
296 if (lk->lk_exslpfail < realexslp) {
297 lk->lk_exslpfail = 0;
298 queue = SQ_EXCLUSIVE_QUEUE;
299 v |= (x & LK_SHARED_WAITERS);
301 lk->lk_exslpfail = 0;
303 "%s: %p has only LK_SLEEPFAIL sleepers",
306 "%s: %p waking up threads on the exclusive queue",
309 sleepq_broadcast(&lk->lock_object,
310 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
311 queue = SQ_SHARED_QUEUE;
317 * Exclusive waiters sleeping with LK_SLEEPFAIL on
318 * and using interruptible sleeps/timeout may have
319 * left spourious lk_exslpfail counts on, so clean
322 lk->lk_exslpfail = 0;
323 queue = SQ_SHARED_QUEUE;
326 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
328 sleepq_release(&lk->lock_object);
331 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
332 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
334 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
336 sleepq_release(&lk->lock_object);
340 lock_profile_release_lock(&lk->lock_object);
341 return (wakeup_swapper);
345 assert_lockmgr(const struct lock_object *lock, int what)
348 panic("lockmgr locks do not support assertions");
352 lock_lockmgr(struct lock_object *lock, int how)
355 panic("lockmgr locks do not support sleep interlocking");
359 unlock_lockmgr(struct lock_object *lock)
362 panic("lockmgr locks do not support sleep interlocking");
367 owner_lockmgr(const struct lock_object *lock, struct thread **owner)
370 panic("lockmgr locks do not support owner inquiring");
375 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
379 MPASS((flags & ~LK_INIT_MASK) == 0);
380 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
381 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
384 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
385 if (flags & LK_CANRECURSE)
386 iflags |= LO_RECURSABLE;
387 if ((flags & LK_NODUP) == 0)
389 if (flags & LK_NOPROFILE)
390 iflags |= LO_NOPROFILE;
391 if ((flags & LK_NOWITNESS) == 0)
392 iflags |= LO_WITNESS;
393 if (flags & LK_QUIET)
395 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
397 lk->lk_lock = LK_UNLOCKED;
399 lk->lk_exslpfail = 0;
402 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
407 * XXX: Gross hacks to manipulate external lock flags after
408 * initialization. Used for certain vnode and buf locks.
411 lockallowshare(struct lock *lk)
414 lockmgr_assert(lk, KA_XLOCKED);
415 lk->lock_object.lo_flags &= ~LK_NOSHARE;
419 lockallowrecurse(struct lock *lk)
422 lockmgr_assert(lk, KA_XLOCKED);
423 lk->lock_object.lo_flags |= LO_RECURSABLE;
427 lockdisablerecurse(struct lock *lk)
430 lockmgr_assert(lk, KA_XLOCKED);
431 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
435 lockdestroy(struct lock *lk)
438 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
439 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
440 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
441 lock_destroy(&lk->lock_object);
445 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
446 const char *wmesg, int pri, int timo, const char *file, int line)
449 struct lock_class *class;
453 int error, ipri, itimo, queue, wakeup_swapper;
454 #ifdef LOCK_PROFILING
455 uint64_t waittime = 0;
458 #ifdef ADAPTIVE_LOCKMGRS
459 volatile struct thread *owner;
460 u_int i, spintries = 0;
464 tid = (uintptr_t)curthread;
465 op = (flags & LK_TYPE_MASK);
466 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
467 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
468 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
470 MPASS((flags & ~LK_TOTAL_MASK) == 0);
471 KASSERT((op & (op - 1)) == 0,
472 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
473 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
474 (op != LK_DOWNGRADE && op != LK_RELEASE),
475 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
476 __func__, file, line));
477 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
478 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
479 __func__, file, line));
480 KASSERT(!TD_IS_IDLETHREAD(curthread),
481 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
482 lk->lock_object.lo_name, file, line));
484 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
485 if (panicstr != NULL) {
486 if (flags & LK_INTERLOCK)
487 class->lc_unlock(ilk);
491 if (lk->lock_object.lo_flags & LK_NOSHARE) {
498 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
507 if (LK_CAN_WITNESS(flags))
508 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
514 * If no other thread has an exclusive lock, or
515 * no exclusive waiter is present, bump the count of
516 * sharers. Since we have to preserve the state of
517 * waiters, if we fail to acquire the shared lock
518 * loop back and retry.
520 if (LK_CAN_SHARE(x)) {
521 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
527 PMC_SOFT_CALL( , , lock, failed);
529 lock_profile_obtain_lock_failed(&lk->lock_object,
530 &contested, &waittime);
533 * If the lock is already held by curthread in
534 * exclusive way avoid a deadlock.
536 if (LK_HOLDER(x) == tid) {
538 "%s: %p already held in exclusive mode",
545 * If the lock is expected to not sleep just give up
548 if (LK_TRYOP(flags)) {
549 LOCK_LOG2(lk, "%s: %p fails the try operation",
555 #ifdef ADAPTIVE_LOCKMGRS
557 * If the owner is running on another CPU, spin until
558 * the owner stops running or the state of the lock
559 * changes. We need a double-state handle here
560 * because for a failed acquisition the lock can be
561 * either held in exclusive mode or shared mode
562 * (for the writer starvation avoidance technique).
564 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
565 LK_HOLDER(x) != LK_KERNPROC) {
566 owner = (struct thread *)LK_HOLDER(x);
567 if (LOCK_LOG_TEST(&lk->lock_object, 0))
569 "%s: spinning on %p held by %p",
570 __func__, lk, owner);
573 * If we are holding also an interlock drop it
574 * in order to avoid a deadlock if the lockmgr
575 * owner is adaptively spinning on the
578 if (flags & LK_INTERLOCK) {
579 class->lc_unlock(ilk);
580 flags &= ~LK_INTERLOCK;
583 while (LK_HOLDER(lk->lk_lock) ==
584 (uintptr_t)owner && TD_IS_RUNNING(owner))
588 } else if (LK_CAN_ADAPT(lk, flags) &&
589 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
590 spintries < alk_retries) {
591 if (flags & LK_INTERLOCK) {
592 class->lc_unlock(ilk);
593 flags &= ~LK_INTERLOCK;
597 for (i = 0; i < alk_loops; i++) {
598 if (LOCK_LOG_TEST(&lk->lock_object, 0))
600 "%s: shared spinning on %p with %u and %u",
601 __func__, lk, spintries, i);
603 if ((x & LK_SHARE) == 0 ||
604 LK_CAN_SHARE(x) != 0)
615 * Acquire the sleepqueue chain lock because we
616 * probabilly will need to manipulate waiters flags.
618 sleepq_lock(&lk->lock_object);
622 * if the lock can be acquired in shared mode, try
625 if (LK_CAN_SHARE(x)) {
626 sleepq_release(&lk->lock_object);
630 #ifdef ADAPTIVE_LOCKMGRS
632 * The current lock owner might have started executing
633 * on another CPU (or the lock could have changed
634 * owner) while we were waiting on the turnstile
635 * chain lock. If so, drop the turnstile lock and try
638 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
639 LK_HOLDER(x) != LK_KERNPROC) {
640 owner = (struct thread *)LK_HOLDER(x);
641 if (TD_IS_RUNNING(owner)) {
642 sleepq_release(&lk->lock_object);
649 * Try to set the LK_SHARED_WAITERS flag. If we fail,
650 * loop back and retry.
652 if ((x & LK_SHARED_WAITERS) == 0) {
653 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
654 x | LK_SHARED_WAITERS)) {
655 sleepq_release(&lk->lock_object);
658 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
663 * As far as we have been unable to acquire the
664 * shared lock and the shared waiters flag is set,
667 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
669 flags &= ~LK_INTERLOCK;
672 "%s: interrupted sleep for %p with %d",
673 __func__, lk, error);
676 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
680 lock_profile_obtain_lock_success(&lk->lock_object,
681 contested, waittime, file, line);
682 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
684 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
686 TD_LOCKS_INC(curthread);
687 TD_SLOCKS_INC(curthread);
692 _lockmgr_assert(lk, KA_SLOCKED, file, line);
694 x = v & LK_ALL_WAITERS;
695 v &= LK_EXCLUSIVE_SPINNERS;
698 * Try to switch from one shared lock to an exclusive one.
699 * We need to preserve waiters flags during the operation.
701 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
703 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
705 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
706 LK_TRYWIT(flags), file, line);
707 TD_SLOCKS_DEC(curthread);
712 * We have been unable to succeed in upgrading, so just
713 * give up the shared lock.
715 wakeup_swapper |= wakeupshlk(lk, file, line);
719 if (LK_CAN_WITNESS(flags))
720 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
721 LOP_EXCLUSIVE, file, line, ilk);
724 * If curthread already holds the lock and this one is
725 * allowed to recurse, simply recurse on it.
727 if (lockmgr_xlocked(lk)) {
728 if ((flags & LK_CANRECURSE) == 0 &&
729 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
732 * If the lock is expected to not panic just
733 * give up and return.
735 if (LK_TRYOP(flags)) {
737 "%s: %p fails the try operation",
742 if (flags & LK_INTERLOCK)
743 class->lc_unlock(ilk);
744 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
745 __func__, iwmesg, file, line);
748 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
749 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
750 lk->lk_recurse, file, line);
751 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
752 LK_TRYWIT(flags), file, line);
753 TD_LOCKS_INC(curthread);
757 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
760 PMC_SOFT_CALL( , , lock, failed);
762 lock_profile_obtain_lock_failed(&lk->lock_object,
763 &contested, &waittime);
766 * If the lock is expected to not sleep just give up
769 if (LK_TRYOP(flags)) {
770 LOCK_LOG2(lk, "%s: %p fails the try operation",
776 #ifdef ADAPTIVE_LOCKMGRS
778 * If the owner is running on another CPU, spin until
779 * the owner stops running or the state of the lock
783 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
784 LK_HOLDER(x) != LK_KERNPROC) {
785 owner = (struct thread *)LK_HOLDER(x);
786 if (LOCK_LOG_TEST(&lk->lock_object, 0))
788 "%s: spinning on %p held by %p",
789 __func__, lk, owner);
792 * If we are holding also an interlock drop it
793 * in order to avoid a deadlock if the lockmgr
794 * owner is adaptively spinning on the
797 if (flags & LK_INTERLOCK) {
798 class->lc_unlock(ilk);
799 flags &= ~LK_INTERLOCK;
802 while (LK_HOLDER(lk->lk_lock) ==
803 (uintptr_t)owner && TD_IS_RUNNING(owner))
807 } else if (LK_CAN_ADAPT(lk, flags) &&
808 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
809 spintries < alk_retries) {
810 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
811 !atomic_cmpset_ptr(&lk->lk_lock, x,
812 x | LK_EXCLUSIVE_SPINNERS))
814 if (flags & LK_INTERLOCK) {
815 class->lc_unlock(ilk);
816 flags &= ~LK_INTERLOCK;
820 for (i = 0; i < alk_loops; i++) {
821 if (LOCK_LOG_TEST(&lk->lock_object, 0))
823 "%s: shared spinning on %p with %u and %u",
824 __func__, lk, spintries, i);
826 LK_EXCLUSIVE_SPINNERS) == 0)
837 * Acquire the sleepqueue chain lock because we
838 * probabilly will need to manipulate waiters flags.
840 sleepq_lock(&lk->lock_object);
844 * if the lock has been released while we spun on
845 * the sleepqueue chain lock just try again.
847 if (x == LK_UNLOCKED) {
848 sleepq_release(&lk->lock_object);
852 #ifdef ADAPTIVE_LOCKMGRS
854 * The current lock owner might have started executing
855 * on another CPU (or the lock could have changed
856 * owner) while we were waiting on the turnstile
857 * chain lock. If so, drop the turnstile lock and try
860 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
861 LK_HOLDER(x) != LK_KERNPROC) {
862 owner = (struct thread *)LK_HOLDER(x);
863 if (TD_IS_RUNNING(owner)) {
864 sleepq_release(&lk->lock_object);
871 * The lock can be in the state where there is a
872 * pending queue of waiters, but still no owner.
873 * This happens when the lock is contested and an
874 * owner is going to claim the lock.
875 * If curthread is the one successfully acquiring it
876 * claim lock ownership and return, preserving waiters
879 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
880 if ((x & ~v) == LK_UNLOCKED) {
881 v &= ~LK_EXCLUSIVE_SPINNERS;
882 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
884 sleepq_release(&lk->lock_object);
886 "%s: %p claimed by a new writer",
890 sleepq_release(&lk->lock_object);
895 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
896 * fail, loop back and retry.
898 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
899 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
900 x | LK_EXCLUSIVE_WAITERS)) {
901 sleepq_release(&lk->lock_object);
904 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
909 * As far as we have been unable to acquire the
910 * exclusive lock and the exclusive waiters flag
911 * is set, we will sleep.
913 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
915 flags &= ~LK_INTERLOCK;
918 "%s: interrupted sleep for %p with %d",
919 __func__, lk, error);
922 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
926 lock_profile_obtain_lock_success(&lk->lock_object,
927 contested, waittime, file, line);
928 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
929 lk->lk_recurse, file, line);
930 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
931 LK_TRYWIT(flags), file, line);
932 TD_LOCKS_INC(curthread);
937 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
938 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
939 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
940 TD_SLOCKS_INC(curthread);
943 * In order to preserve waiters flags, just spin.
947 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
949 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
950 LK_SHARERS_LOCK(1) | x))
956 _lockmgr_assert(lk, KA_LOCKED, file, line);
959 if ((x & LK_SHARE) == 0) {
962 * As first option, treact the lock as if it has not
964 * Fix-up the tid var if the lock has been disowned.
966 if (LK_HOLDER(x) == LK_KERNPROC)
969 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
971 TD_LOCKS_DEC(curthread);
973 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
974 lk->lk_recurse, file, line);
977 * The lock is held in exclusive mode.
978 * If the lock is recursed also, then unrecurse it.
980 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
981 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
986 if (tid != LK_KERNPROC)
987 lock_profile_release_lock(&lk->lock_object);
989 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
993 sleepq_lock(&lk->lock_object);
998 * If the lock has exclusive waiters, give them
999 * preference in order to avoid deadlock with
1000 * shared runners up.
1001 * If interruptible sleeps left the exclusive queue
1002 * empty avoid a starvation for the threads sleeping
1003 * on the shared queue by giving them precedence
1004 * and cleaning up the exclusive waiters bit anyway.
1005 * Please note that lk_exslpfail count may be lying
1006 * about the real number of waiters with the
1007 * LK_SLEEPFAIL flag on because they may be used in
1008 * conjuction with interruptible sleeps so
1009 * lk_exslpfail might be considered an 'upper limit'
1010 * bound, including the edge cases.
1012 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1013 realexslp = sleepq_sleepcnt(&lk->lock_object,
1014 SQ_EXCLUSIVE_QUEUE);
1015 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1016 if (lk->lk_exslpfail < realexslp) {
1017 lk->lk_exslpfail = 0;
1018 queue = SQ_EXCLUSIVE_QUEUE;
1019 v |= (x & LK_SHARED_WAITERS);
1021 lk->lk_exslpfail = 0;
1023 "%s: %p has only LK_SLEEPFAIL sleepers",
1026 "%s: %p waking up threads on the exclusive queue",
1029 sleepq_broadcast(&lk->lock_object,
1030 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1031 queue = SQ_SHARED_QUEUE;
1036 * Exclusive waiters sleeping with LK_SLEEPFAIL
1037 * on and using interruptible sleeps/timeout
1038 * may have left spourious lk_exslpfail counts
1039 * on, so clean it up anyway.
1041 lk->lk_exslpfail = 0;
1042 queue = SQ_SHARED_QUEUE;
1046 "%s: %p waking up threads on the %s queue",
1047 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1049 atomic_store_rel_ptr(&lk->lk_lock, v);
1050 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1051 SLEEPQ_LK, 0, queue);
1052 sleepq_release(&lk->lock_object);
1055 wakeup_swapper = wakeupshlk(lk, file, line);
1058 if (LK_CAN_WITNESS(flags))
1059 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1060 LOP_EXCLUSIVE, file, line, ilk);
1063 * Trying to drain a lock we already own will result in a
1066 if (lockmgr_xlocked(lk)) {
1067 if (flags & LK_INTERLOCK)
1068 class->lc_unlock(ilk);
1069 panic("%s: draining %s with the lock held @ %s:%d\n",
1070 __func__, iwmesg, file, line);
1073 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1075 PMC_SOFT_CALL( , , lock, failed);
1077 lock_profile_obtain_lock_failed(&lk->lock_object,
1078 &contested, &waittime);
1081 * If the lock is expected to not sleep just give up
1084 if (LK_TRYOP(flags)) {
1085 LOCK_LOG2(lk, "%s: %p fails the try operation",
1092 * Acquire the sleepqueue chain lock because we
1093 * probabilly will need to manipulate waiters flags.
1095 sleepq_lock(&lk->lock_object);
1099 * if the lock has been released while we spun on
1100 * the sleepqueue chain lock just try again.
1102 if (x == LK_UNLOCKED) {
1103 sleepq_release(&lk->lock_object);
1107 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1108 if ((x & ~v) == LK_UNLOCKED) {
1109 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1112 * If interruptible sleeps left the exclusive
1113 * queue empty avoid a starvation for the
1114 * threads sleeping on the shared queue by
1115 * giving them precedence and cleaning up the
1116 * exclusive waiters bit anyway.
1117 * Please note that lk_exslpfail count may be
1118 * lying about the real number of waiters with
1119 * the LK_SLEEPFAIL flag on because they may
1120 * be used in conjuction with interruptible
1121 * sleeps so lk_exslpfail might be considered
1122 * an 'upper limit' bound, including the edge
1125 if (v & LK_EXCLUSIVE_WAITERS) {
1126 queue = SQ_EXCLUSIVE_QUEUE;
1127 v &= ~LK_EXCLUSIVE_WAITERS;
1131 * Exclusive waiters sleeping with
1132 * LK_SLEEPFAIL on and using
1133 * interruptible sleeps/timeout may
1134 * have left spourious lk_exslpfail
1135 * counts on, so clean it up anyway.
1137 MPASS(v & LK_SHARED_WAITERS);
1138 lk->lk_exslpfail = 0;
1139 queue = SQ_SHARED_QUEUE;
1140 v &= ~LK_SHARED_WAITERS;
1142 if (queue == SQ_EXCLUSIVE_QUEUE) {
1144 sleepq_sleepcnt(&lk->lock_object,
1145 SQ_EXCLUSIVE_QUEUE);
1146 if (lk->lk_exslpfail >= realexslp) {
1147 lk->lk_exslpfail = 0;
1148 queue = SQ_SHARED_QUEUE;
1149 v &= ~LK_SHARED_WAITERS;
1150 if (realexslp != 0) {
1152 "%s: %p has only LK_SLEEPFAIL sleepers",
1155 "%s: %p waking up threads on the exclusive queue",
1161 SQ_EXCLUSIVE_QUEUE);
1164 lk->lk_exslpfail = 0;
1166 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1167 sleepq_release(&lk->lock_object);
1171 "%s: %p waking up all threads on the %s queue",
1172 __func__, lk, queue == SQ_SHARED_QUEUE ?
1173 "shared" : "exclusive");
1174 wakeup_swapper |= sleepq_broadcast(
1175 &lk->lock_object, SLEEPQ_LK, 0, queue);
1178 * If shared waiters have been woken up we need
1179 * to wait for one of them to acquire the lock
1180 * before to set the exclusive waiters in
1181 * order to avoid a deadlock.
1183 if (queue == SQ_SHARED_QUEUE) {
1184 for (v = lk->lk_lock;
1185 (v & LK_SHARE) && !LK_SHARERS(v);
1192 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1193 * fail, loop back and retry.
1195 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1196 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1197 x | LK_EXCLUSIVE_WAITERS)) {
1198 sleepq_release(&lk->lock_object);
1201 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1206 * As far as we have been unable to acquire the
1207 * exclusive lock and the exclusive waiters flag
1208 * is set, we will sleep.
1210 if (flags & LK_INTERLOCK) {
1211 class->lc_unlock(ilk);
1212 flags &= ~LK_INTERLOCK;
1215 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1216 SQ_EXCLUSIVE_QUEUE);
1217 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1219 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1224 lock_profile_obtain_lock_success(&lk->lock_object,
1225 contested, waittime, file, line);
1226 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1227 lk->lk_recurse, file, line);
1228 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1229 LK_TRYWIT(flags), file, line);
1230 TD_LOCKS_INC(curthread);
1235 if (flags & LK_INTERLOCK)
1236 class->lc_unlock(ilk);
1237 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1240 if (flags & LK_INTERLOCK)
1241 class->lc_unlock(ilk);
1249 _lockmgr_disown(struct lock *lk, const char *file, int line)
1253 if (SCHEDULER_STOPPED())
1256 tid = (uintptr_t)curthread;
1257 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1260 * If the owner is already LK_KERNPROC just skip the whole operation.
1262 if (LK_HOLDER(lk->lk_lock) != tid)
1264 lock_profile_release_lock(&lk->lock_object);
1265 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1266 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1267 TD_LOCKS_DEC(curthread);
1271 * In order to preserve waiters flags, just spin.
1275 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1276 x &= LK_ALL_WAITERS;
1277 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1285 lockmgr_printinfo(const struct lock *lk)
1290 if (lk->lk_lock == LK_UNLOCKED)
1291 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1292 else if (lk->lk_lock & LK_SHARE)
1293 printf("lock type %s: SHARED (count %ju)\n",
1294 lk->lock_object.lo_name,
1295 (uintmax_t)LK_SHARERS(lk->lk_lock));
1297 td = lockmgr_xholder(lk);
1298 printf("lock type %s: EXCL by thread %p "
1299 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name, td,
1300 td->td_proc->p_pid, td->td_proc->p_comm, td->td_tid);
1304 if (x & LK_EXCLUSIVE_WAITERS)
1305 printf(" with exclusive waiters pending\n");
1306 if (x & LK_SHARED_WAITERS)
1307 printf(" with shared waiters pending\n");
1308 if (x & LK_EXCLUSIVE_SPINNERS)
1309 printf(" with exclusive spinners pending\n");
1315 lockstatus(const struct lock *lk)
1324 if ((x & LK_SHARE) == 0) {
1325 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1329 } else if (x == LK_UNLOCKED)
1335 #ifdef INVARIANT_SUPPORT
1337 FEATURE(invariant_support,
1338 "Support for modules compiled with INVARIANTS option");
1341 #undef _lockmgr_assert
1345 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1349 if (panicstr != NULL)
1353 case KA_SLOCKED | KA_NOTRECURSED:
1354 case KA_SLOCKED | KA_RECURSED:
1357 case KA_LOCKED | KA_NOTRECURSED:
1358 case KA_LOCKED | KA_RECURSED:
1362 * We cannot trust WITNESS if the lock is held in exclusive
1363 * mode and a call to lockmgr_disown() happened.
1364 * Workaround this skipping the check if the lock is held in
1365 * exclusive mode even for the KA_LOCKED case.
1367 if (slocked || (lk->lk_lock & LK_SHARE)) {
1368 witness_assert(&lk->lock_object, what, file, line);
1372 if (lk->lk_lock == LK_UNLOCKED ||
1373 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1374 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1375 panic("Lock %s not %slocked @ %s:%d\n",
1376 lk->lock_object.lo_name, slocked ? "share" : "",
1379 if ((lk->lk_lock & LK_SHARE) == 0) {
1380 if (lockmgr_recursed(lk)) {
1381 if (what & KA_NOTRECURSED)
1382 panic("Lock %s recursed @ %s:%d\n",
1383 lk->lock_object.lo_name, file,
1385 } else if (what & KA_RECURSED)
1386 panic("Lock %s not recursed @ %s:%d\n",
1387 lk->lock_object.lo_name, file, line);
1391 case KA_XLOCKED | KA_NOTRECURSED:
1392 case KA_XLOCKED | KA_RECURSED:
1393 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1394 panic("Lock %s not exclusively locked @ %s:%d\n",
1395 lk->lock_object.lo_name, file, line);
1396 if (lockmgr_recursed(lk)) {
1397 if (what & KA_NOTRECURSED)
1398 panic("Lock %s recursed @ %s:%d\n",
1399 lk->lock_object.lo_name, file, line);
1400 } else if (what & KA_RECURSED)
1401 panic("Lock %s not recursed @ %s:%d\n",
1402 lk->lock_object.lo_name, file, line);
1405 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1406 panic("Lock %s exclusively locked @ %s:%d\n",
1407 lk->lock_object.lo_name, file, line);
1410 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1418 lockmgr_chain(struct thread *td, struct thread **ownerp)
1424 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1426 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1427 if (lk->lk_lock & LK_SHARE)
1428 db_printf("SHARED (count %ju)\n",
1429 (uintmax_t)LK_SHARERS(lk->lk_lock));
1431 db_printf("EXCL\n");
1432 *ownerp = lockmgr_xholder(lk);
1438 db_show_lockmgr(const struct lock_object *lock)
1441 const struct lock *lk;
1443 lk = (const struct lock *)lock;
1445 db_printf(" state: ");
1446 if (lk->lk_lock == LK_UNLOCKED)
1447 db_printf("UNLOCKED\n");
1448 else if (lk->lk_lock & LK_SHARE)
1449 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1451 td = lockmgr_xholder(lk);
1452 if (td == (struct thread *)LK_KERNPROC)
1453 db_printf("XLOCK: LK_KERNPROC\n");
1455 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1456 td->td_tid, td->td_proc->p_pid,
1457 td->td_proc->p_comm);
1458 if (lockmgr_recursed(lk))
1459 db_printf(" recursed: %d\n", lk->lk_recurse);
1461 db_printf(" waiters: ");
1462 switch (lk->lk_lock & LK_ALL_WAITERS) {
1463 case LK_SHARED_WAITERS:
1464 db_printf("shared\n");
1466 case LK_EXCLUSIVE_WAITERS:
1467 db_printf("exclusive\n");
1469 case LK_ALL_WAITERS:
1470 db_printf("shared and exclusive\n");
1473 db_printf("none\n");
1475 db_printf(" spinners: ");
1476 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1477 db_printf("exclusive\n");
1479 db_printf("none\n");