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"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #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, uintptr_t how);
146 static int owner_lockmgr(const struct lock_object *lock,
147 struct thread **owner);
149 static uintptr_t 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 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
241 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
248 * If there is more than one shared lock held, just drop one
251 if (LK_SHARERS(x) > 1) {
252 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
259 * If there are not waiters on the exclusive queue, drop the
262 if ((x & LK_ALL_WAITERS) == 0) {
263 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
265 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
271 * We should have a sharer with waiters, so enter the hard
272 * path in order to handle wakeups correctly.
274 sleepq_lock(&lk->lock_object);
275 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
279 * If the lock has exclusive waiters, give them preference in
280 * order to avoid deadlock with shared runners up.
281 * If interruptible sleeps left the exclusive queue empty
282 * avoid a starvation for the threads sleeping on the shared
283 * queue by giving them precedence and cleaning up the
284 * exclusive waiters bit anyway.
285 * Please note that lk_exslpfail count may be lying about
286 * the real number of waiters with the LK_SLEEPFAIL flag on
287 * because they may be used in conjuction with interruptible
288 * sleeps so lk_exslpfail might be considered an 'upper limit'
289 * bound, including the edge cases.
291 realexslp = sleepq_sleepcnt(&lk->lock_object,
293 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
294 if (lk->lk_exslpfail < realexslp) {
295 lk->lk_exslpfail = 0;
296 queue = SQ_EXCLUSIVE_QUEUE;
297 v |= (x & LK_SHARED_WAITERS);
299 lk->lk_exslpfail = 0;
301 "%s: %p has only LK_SLEEPFAIL sleepers",
304 "%s: %p waking up threads on the exclusive queue",
307 sleepq_broadcast(&lk->lock_object,
308 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
309 queue = SQ_SHARED_QUEUE;
315 * Exclusive waiters sleeping with LK_SLEEPFAIL on
316 * and using interruptible sleeps/timeout may have
317 * left spourious lk_exslpfail counts on, so clean
320 lk->lk_exslpfail = 0;
321 queue = SQ_SHARED_QUEUE;
324 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
326 sleepq_release(&lk->lock_object);
329 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
330 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
332 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
334 sleepq_release(&lk->lock_object);
338 lock_profile_release_lock(&lk->lock_object);
339 TD_LOCKS_DEC(curthread);
340 TD_SLOCKS_DEC(curthread);
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, uintptr_t 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 if (flags & LK_IS_VNODE)
396 iflags |= LO_IS_VNODE;
397 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
399 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
400 lk->lk_lock = LK_UNLOCKED;
402 lk->lk_exslpfail = 0;
409 * XXX: Gross hacks to manipulate external lock flags after
410 * initialization. Used for certain vnode and buf locks.
413 lockallowshare(struct lock *lk)
416 lockmgr_assert(lk, KA_XLOCKED);
417 lk->lock_object.lo_flags &= ~LK_NOSHARE;
421 lockallowrecurse(struct lock *lk)
424 lockmgr_assert(lk, KA_XLOCKED);
425 lk->lock_object.lo_flags |= LO_RECURSABLE;
429 lockdisablerecurse(struct lock *lk)
432 lockmgr_assert(lk, KA_XLOCKED);
433 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
437 lockdestroy(struct lock *lk)
440 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
441 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
442 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
443 lock_destroy(&lk->lock_object);
447 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
448 const char *wmesg, int pri, int timo, const char *file, int line)
451 struct lock_class *class;
455 int error, ipri, itimo, queue, wakeup_swapper;
456 #ifdef LOCK_PROFILING
457 uint64_t waittime = 0;
460 #ifdef ADAPTIVE_LOCKMGRS
461 volatile struct thread *owner;
462 u_int i, spintries = 0;
466 tid = (uintptr_t)curthread;
467 op = (flags & LK_TYPE_MASK);
468 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
469 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
470 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
472 MPASS((flags & ~LK_TOTAL_MASK) == 0);
473 KASSERT((op & (op - 1)) == 0,
474 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
475 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
476 (op != LK_DOWNGRADE && op != LK_RELEASE),
477 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
478 __func__, file, line));
479 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
480 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
481 __func__, file, line));
482 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
483 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread,
484 lk->lock_object.lo_name, file, line));
486 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
487 if (panicstr != NULL) {
488 if (flags & LK_INTERLOCK)
489 class->lc_unlock(ilk);
493 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,
514 file, line, flags & LK_INTERLOCK ? ilk : NULL);
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);
698 _lockmgr_assert(lk, KA_SLOCKED, file, line);
700 x = v & LK_ALL_WAITERS;
701 v &= LK_EXCLUSIVE_SPINNERS;
704 * Try to switch from one shared lock to an exclusive one.
705 * We need to preserve waiters flags during the operation.
707 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
709 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
711 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
712 LK_TRYWIT(flags), file, line);
713 TD_SLOCKS_DEC(curthread);
718 * In LK_TRYUPGRADE mode, do not drop the lock,
719 * returning EBUSY instead.
721 if (op == LK_TRYUPGRADE) {
722 LOCK_LOG2(lk, "%s: %p failed the nowait upgrade",
729 * We have been unable to succeed in upgrading, so just
730 * give up the shared lock.
732 wakeup_swapper |= wakeupshlk(lk, file, line);
736 if (LK_CAN_WITNESS(flags))
737 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
738 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
742 * If curthread already holds the lock and this one is
743 * allowed to recurse, simply recurse on it.
745 if (lockmgr_xlocked(lk)) {
746 if ((flags & LK_CANRECURSE) == 0 &&
747 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
750 * If the lock is expected to not panic just
751 * give up and return.
753 if (LK_TRYOP(flags)) {
755 "%s: %p fails the try operation",
760 if (flags & LK_INTERLOCK)
761 class->lc_unlock(ilk);
762 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
763 __func__, iwmesg, file, line);
766 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
767 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
768 lk->lk_recurse, file, line);
769 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
770 LK_TRYWIT(flags), file, line);
771 TD_LOCKS_INC(curthread);
775 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
778 PMC_SOFT_CALL( , , lock, failed);
780 lock_profile_obtain_lock_failed(&lk->lock_object,
781 &contested, &waittime);
784 * If the lock is expected to not sleep just give up
787 if (LK_TRYOP(flags)) {
788 LOCK_LOG2(lk, "%s: %p fails the try operation",
794 #ifdef ADAPTIVE_LOCKMGRS
796 * If the owner is running on another CPU, spin until
797 * the owner stops running or the state of the lock
801 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
802 LK_HOLDER(x) != LK_KERNPROC) {
803 owner = (struct thread *)LK_HOLDER(x);
804 if (LOCK_LOG_TEST(&lk->lock_object, 0))
806 "%s: spinning on %p held by %p",
807 __func__, lk, owner);
810 * If we are holding also an interlock drop it
811 * in order to avoid a deadlock if the lockmgr
812 * owner is adaptively spinning on the
815 if (flags & LK_INTERLOCK) {
816 class->lc_unlock(ilk);
817 flags &= ~LK_INTERLOCK;
820 while (LK_HOLDER(lk->lk_lock) ==
821 (uintptr_t)owner && TD_IS_RUNNING(owner))
825 } else if (LK_CAN_ADAPT(lk, flags) &&
826 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
827 spintries < alk_retries) {
828 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
829 !atomic_cmpset_ptr(&lk->lk_lock, x,
830 x | LK_EXCLUSIVE_SPINNERS))
832 if (flags & LK_INTERLOCK) {
833 class->lc_unlock(ilk);
834 flags &= ~LK_INTERLOCK;
838 for (i = 0; i < alk_loops; i++) {
839 if (LOCK_LOG_TEST(&lk->lock_object, 0))
841 "%s: shared spinning on %p with %u and %u",
842 __func__, lk, spintries, i);
844 LK_EXCLUSIVE_SPINNERS) == 0)
855 * Acquire the sleepqueue chain lock because we
856 * probabilly will need to manipulate waiters flags.
858 sleepq_lock(&lk->lock_object);
862 * if the lock has been released while we spun on
863 * the sleepqueue chain lock just try again.
865 if (x == LK_UNLOCKED) {
866 sleepq_release(&lk->lock_object);
870 #ifdef ADAPTIVE_LOCKMGRS
872 * The current lock owner might have started executing
873 * on another CPU (or the lock could have changed
874 * owner) while we were waiting on the turnstile
875 * chain lock. If so, drop the turnstile lock and try
878 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
879 LK_HOLDER(x) != LK_KERNPROC) {
880 owner = (struct thread *)LK_HOLDER(x);
881 if (TD_IS_RUNNING(owner)) {
882 sleepq_release(&lk->lock_object);
889 * The lock can be in the state where there is a
890 * pending queue of waiters, but still no owner.
891 * This happens when the lock is contested and an
892 * owner is going to claim the lock.
893 * If curthread is the one successfully acquiring it
894 * claim lock ownership and return, preserving waiters
897 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
898 if ((x & ~v) == LK_UNLOCKED) {
899 v &= ~LK_EXCLUSIVE_SPINNERS;
900 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
902 sleepq_release(&lk->lock_object);
904 "%s: %p claimed by a new writer",
908 sleepq_release(&lk->lock_object);
913 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
914 * fail, loop back and retry.
916 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
917 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
918 x | LK_EXCLUSIVE_WAITERS)) {
919 sleepq_release(&lk->lock_object);
922 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
927 * As far as we have been unable to acquire the
928 * exclusive lock and the exclusive waiters flag
929 * is set, we will sleep.
931 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
933 flags &= ~LK_INTERLOCK;
936 "%s: interrupted sleep for %p with %d",
937 __func__, lk, error);
940 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
944 lock_profile_obtain_lock_success(&lk->lock_object,
945 contested, waittime, file, line);
946 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
947 lk->lk_recurse, file, line);
948 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
949 LK_TRYWIT(flags), file, line);
950 TD_LOCKS_INC(curthread);
955 _lockmgr_assert(lk, KA_XLOCKED, file, line);
956 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
957 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
960 * Panic if the lock is recursed.
962 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
963 if (flags & LK_INTERLOCK)
964 class->lc_unlock(ilk);
965 panic("%s: downgrade a recursed lockmgr %s @ %s:%d\n",
966 __func__, iwmesg, file, line);
968 TD_SLOCKS_INC(curthread);
971 * In order to preserve waiters flags, just spin.
975 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
977 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
978 LK_SHARERS_LOCK(1) | x))
984 _lockmgr_assert(lk, KA_LOCKED, file, line);
987 if ((x & LK_SHARE) == 0) {
990 * As first option, treact the lock as if it has not
992 * Fix-up the tid var if the lock has been disowned.
994 if (LK_HOLDER(x) == LK_KERNPROC)
997 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
999 TD_LOCKS_DEC(curthread);
1001 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
1002 lk->lk_recurse, file, line);
1005 * The lock is held in exclusive mode.
1006 * If the lock is recursed also, then unrecurse it.
1008 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
1009 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
1014 if (tid != LK_KERNPROC)
1015 lock_profile_release_lock(&lk->lock_object);
1017 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
1021 sleepq_lock(&lk->lock_object);
1026 * If the lock has exclusive waiters, give them
1027 * preference in order to avoid deadlock with
1028 * shared runners up.
1029 * If interruptible sleeps left the exclusive queue
1030 * empty avoid a starvation for the threads sleeping
1031 * on the shared queue by giving them precedence
1032 * and cleaning up the exclusive waiters bit anyway.
1033 * Please note that lk_exslpfail count may be lying
1034 * about the real number of waiters with the
1035 * LK_SLEEPFAIL flag on because they may be used in
1036 * conjuction with interruptible sleeps so
1037 * lk_exslpfail might be considered an 'upper limit'
1038 * bound, including the edge cases.
1040 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1041 realexslp = sleepq_sleepcnt(&lk->lock_object,
1042 SQ_EXCLUSIVE_QUEUE);
1043 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1044 if (lk->lk_exslpfail < realexslp) {
1045 lk->lk_exslpfail = 0;
1046 queue = SQ_EXCLUSIVE_QUEUE;
1047 v |= (x & LK_SHARED_WAITERS);
1049 lk->lk_exslpfail = 0;
1051 "%s: %p has only LK_SLEEPFAIL sleepers",
1054 "%s: %p waking up threads on the exclusive queue",
1057 sleepq_broadcast(&lk->lock_object,
1058 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1059 queue = SQ_SHARED_QUEUE;
1064 * Exclusive waiters sleeping with LK_SLEEPFAIL
1065 * on and using interruptible sleeps/timeout
1066 * may have left spourious lk_exslpfail counts
1067 * on, so clean it up anyway.
1069 lk->lk_exslpfail = 0;
1070 queue = SQ_SHARED_QUEUE;
1074 "%s: %p waking up threads on the %s queue",
1075 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1077 atomic_store_rel_ptr(&lk->lk_lock, v);
1078 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1079 SLEEPQ_LK, 0, queue);
1080 sleepq_release(&lk->lock_object);
1083 wakeup_swapper = wakeupshlk(lk, file, line);
1086 if (LK_CAN_WITNESS(flags))
1087 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1088 LOP_EXCLUSIVE, file, line, flags & LK_INTERLOCK ?
1092 * Trying to drain a lock we already own will result in a
1095 if (lockmgr_xlocked(lk)) {
1096 if (flags & LK_INTERLOCK)
1097 class->lc_unlock(ilk);
1098 panic("%s: draining %s with the lock held @ %s:%d\n",
1099 __func__, iwmesg, file, line);
1102 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1104 PMC_SOFT_CALL( , , lock, failed);
1106 lock_profile_obtain_lock_failed(&lk->lock_object,
1107 &contested, &waittime);
1110 * If the lock is expected to not sleep just give up
1113 if (LK_TRYOP(flags)) {
1114 LOCK_LOG2(lk, "%s: %p fails the try operation",
1121 * Acquire the sleepqueue chain lock because we
1122 * probabilly will need to manipulate waiters flags.
1124 sleepq_lock(&lk->lock_object);
1128 * if the lock has been released while we spun on
1129 * the sleepqueue chain lock just try again.
1131 if (x == LK_UNLOCKED) {
1132 sleepq_release(&lk->lock_object);
1136 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1137 if ((x & ~v) == LK_UNLOCKED) {
1138 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1141 * If interruptible sleeps left the exclusive
1142 * queue empty avoid a starvation for the
1143 * threads sleeping on the shared queue by
1144 * giving them precedence and cleaning up the
1145 * exclusive waiters bit anyway.
1146 * Please note that lk_exslpfail count may be
1147 * lying about the real number of waiters with
1148 * the LK_SLEEPFAIL flag on because they may
1149 * be used in conjuction with interruptible
1150 * sleeps so lk_exslpfail might be considered
1151 * an 'upper limit' bound, including the edge
1154 if (v & LK_EXCLUSIVE_WAITERS) {
1155 queue = SQ_EXCLUSIVE_QUEUE;
1156 v &= ~LK_EXCLUSIVE_WAITERS;
1160 * Exclusive waiters sleeping with
1161 * LK_SLEEPFAIL on and using
1162 * interruptible sleeps/timeout may
1163 * have left spourious lk_exslpfail
1164 * counts on, so clean it up anyway.
1166 MPASS(v & LK_SHARED_WAITERS);
1167 lk->lk_exslpfail = 0;
1168 queue = SQ_SHARED_QUEUE;
1169 v &= ~LK_SHARED_WAITERS;
1171 if (queue == SQ_EXCLUSIVE_QUEUE) {
1173 sleepq_sleepcnt(&lk->lock_object,
1174 SQ_EXCLUSIVE_QUEUE);
1175 if (lk->lk_exslpfail >= realexslp) {
1176 lk->lk_exslpfail = 0;
1177 queue = SQ_SHARED_QUEUE;
1178 v &= ~LK_SHARED_WAITERS;
1179 if (realexslp != 0) {
1181 "%s: %p has only LK_SLEEPFAIL sleepers",
1184 "%s: %p waking up threads on the exclusive queue",
1190 SQ_EXCLUSIVE_QUEUE);
1193 lk->lk_exslpfail = 0;
1195 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1196 sleepq_release(&lk->lock_object);
1200 "%s: %p waking up all threads on the %s queue",
1201 __func__, lk, queue == SQ_SHARED_QUEUE ?
1202 "shared" : "exclusive");
1203 wakeup_swapper |= sleepq_broadcast(
1204 &lk->lock_object, SLEEPQ_LK, 0, queue);
1207 * If shared waiters have been woken up we need
1208 * to wait for one of them to acquire the lock
1209 * before to set the exclusive waiters in
1210 * order to avoid a deadlock.
1212 if (queue == SQ_SHARED_QUEUE) {
1213 for (v = lk->lk_lock;
1214 (v & LK_SHARE) && !LK_SHARERS(v);
1221 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1222 * fail, loop back and retry.
1224 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1225 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1226 x | LK_EXCLUSIVE_WAITERS)) {
1227 sleepq_release(&lk->lock_object);
1230 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1235 * As far as we have been unable to acquire the
1236 * exclusive lock and the exclusive waiters flag
1237 * is set, we will sleep.
1239 if (flags & LK_INTERLOCK) {
1240 class->lc_unlock(ilk);
1241 flags &= ~LK_INTERLOCK;
1244 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1245 SQ_EXCLUSIVE_QUEUE);
1246 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1248 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1253 lock_profile_obtain_lock_success(&lk->lock_object,
1254 contested, waittime, file, line);
1255 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1256 lk->lk_recurse, file, line);
1257 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1258 LK_TRYWIT(flags), file, line);
1259 TD_LOCKS_INC(curthread);
1264 if (flags & LK_INTERLOCK)
1265 class->lc_unlock(ilk);
1266 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1269 if (flags & LK_INTERLOCK)
1270 class->lc_unlock(ilk);
1278 _lockmgr_disown(struct lock *lk, const char *file, int line)
1282 if (SCHEDULER_STOPPED())
1285 tid = (uintptr_t)curthread;
1286 _lockmgr_assert(lk, KA_XLOCKED, file, line);
1289 * Panic if the lock is recursed.
1291 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk))
1292 panic("%s: disown a recursed lockmgr @ %s:%d\n",
1293 __func__, file, line);
1296 * If the owner is already LK_KERNPROC just skip the whole operation.
1298 if (LK_HOLDER(lk->lk_lock) != tid)
1300 lock_profile_release_lock(&lk->lock_object);
1301 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1302 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1303 TD_LOCKS_DEC(curthread);
1307 * In order to preserve waiters flags, just spin.
1311 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1312 x &= LK_ALL_WAITERS;
1313 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1321 lockmgr_printinfo(const struct lock *lk)
1326 if (lk->lk_lock == LK_UNLOCKED)
1327 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1328 else if (lk->lk_lock & LK_SHARE)
1329 printf("lock type %s: SHARED (count %ju)\n",
1330 lk->lock_object.lo_name,
1331 (uintmax_t)LK_SHARERS(lk->lk_lock));
1333 td = lockmgr_xholder(lk);
1334 printf("lock type %s: EXCL by thread %p "
1335 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name, td,
1336 td->td_proc->p_pid, td->td_proc->p_comm, td->td_tid);
1340 if (x & LK_EXCLUSIVE_WAITERS)
1341 printf(" with exclusive waiters pending\n");
1342 if (x & LK_SHARED_WAITERS)
1343 printf(" with shared waiters pending\n");
1344 if (x & LK_EXCLUSIVE_SPINNERS)
1345 printf(" with exclusive spinners pending\n");
1351 lockstatus(const struct lock *lk)
1360 if ((x & LK_SHARE) == 0) {
1361 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1365 } else if (x == LK_UNLOCKED)
1371 #ifdef INVARIANT_SUPPORT
1373 FEATURE(invariant_support,
1374 "Support for modules compiled with INVARIANTS option");
1377 #undef _lockmgr_assert
1381 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line)
1385 if (panicstr != NULL)
1389 case KA_SLOCKED | KA_NOTRECURSED:
1390 case KA_SLOCKED | KA_RECURSED:
1393 case KA_LOCKED | KA_NOTRECURSED:
1394 case KA_LOCKED | KA_RECURSED:
1398 * We cannot trust WITNESS if the lock is held in exclusive
1399 * mode and a call to lockmgr_disown() happened.
1400 * Workaround this skipping the check if the lock is held in
1401 * exclusive mode even for the KA_LOCKED case.
1403 if (slocked || (lk->lk_lock & LK_SHARE)) {
1404 witness_assert(&lk->lock_object, what, file, line);
1408 if (lk->lk_lock == LK_UNLOCKED ||
1409 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1410 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1411 panic("Lock %s not %slocked @ %s:%d\n",
1412 lk->lock_object.lo_name, slocked ? "share" : "",
1415 if ((lk->lk_lock & LK_SHARE) == 0) {
1416 if (lockmgr_recursed(lk)) {
1417 if (what & KA_NOTRECURSED)
1418 panic("Lock %s recursed @ %s:%d\n",
1419 lk->lock_object.lo_name, file,
1421 } else if (what & KA_RECURSED)
1422 panic("Lock %s not recursed @ %s:%d\n",
1423 lk->lock_object.lo_name, file, line);
1427 case KA_XLOCKED | KA_NOTRECURSED:
1428 case KA_XLOCKED | KA_RECURSED:
1429 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1430 panic("Lock %s not exclusively locked @ %s:%d\n",
1431 lk->lock_object.lo_name, file, line);
1432 if (lockmgr_recursed(lk)) {
1433 if (what & KA_NOTRECURSED)
1434 panic("Lock %s recursed @ %s:%d\n",
1435 lk->lock_object.lo_name, file, line);
1436 } else if (what & KA_RECURSED)
1437 panic("Lock %s not recursed @ %s:%d\n",
1438 lk->lock_object.lo_name, file, line);
1441 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1442 panic("Lock %s exclusively locked @ %s:%d\n",
1443 lk->lock_object.lo_name, file, line);
1446 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1454 lockmgr_chain(struct thread *td, struct thread **ownerp)
1460 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1462 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1463 if (lk->lk_lock & LK_SHARE)
1464 db_printf("SHARED (count %ju)\n",
1465 (uintmax_t)LK_SHARERS(lk->lk_lock));
1467 db_printf("EXCL\n");
1468 *ownerp = lockmgr_xholder(lk);
1474 db_show_lockmgr(const struct lock_object *lock)
1477 const struct lock *lk;
1479 lk = (const struct lock *)lock;
1481 db_printf(" state: ");
1482 if (lk->lk_lock == LK_UNLOCKED)
1483 db_printf("UNLOCKED\n");
1484 else if (lk->lk_lock & LK_SHARE)
1485 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1487 td = lockmgr_xholder(lk);
1488 if (td == (struct thread *)LK_KERNPROC)
1489 db_printf("XLOCK: LK_KERNPROC\n");
1491 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1492 td->td_tid, td->td_proc->p_pid,
1493 td->td_proc->p_comm);
1494 if (lockmgr_recursed(lk))
1495 db_printf(" recursed: %d\n", lk->lk_recurse);
1497 db_printf(" waiters: ");
1498 switch (lk->lk_lock & LK_ALL_WAITERS) {
1499 case LK_SHARED_WAITERS:
1500 db_printf("shared\n");
1502 case LK_EXCLUSIVE_WAITERS:
1503 db_printf("exclusive\n");
1505 case LK_ALL_WAITERS:
1506 db_printf("shared and exclusive\n");
1509 db_printf("none\n");
1511 db_printf(" spinners: ");
1512 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1513 db_printf("exclusive\n");
1515 db_printf("none\n");