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
70 #ifdef ADAPTIVE_LOCKMGRS
71 #define ALK_RETRIES 10
72 #define ALK_LOOPS 10000
76 #define _lockmgr_assert(lk, what, file, line)
77 #define TD_LOCKS_INC(td)
78 #define TD_LOCKS_DEC(td)
80 #define TD_LOCKS_INC(td) ((td)->td_locks++)
81 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
83 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
84 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
87 #define STACK_PRINT(lk)
88 #define STACK_SAVE(lk)
89 #define STACK_ZERO(lk)
91 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
92 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
93 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
96 #define LOCK_LOG2(lk, string, arg1, arg2) \
97 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
98 CTR2(KTR_LOCK, (string), (arg1), (arg2))
99 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
100 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
101 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
103 #define GIANT_DECLARE \
105 WITNESS_SAVE_DECL(Giant)
106 #define GIANT_RESTORE() do { \
110 WITNESS_RESTORE(&Giant.lock_object, Giant); \
113 #define GIANT_SAVE() do { \
114 if (mtx_owned(&Giant)) { \
115 WITNESS_SAVE(&Giant.lock_object, Giant); \
116 while (mtx_owned(&Giant)) { \
118 mtx_unlock(&Giant); \
123 #define LK_CAN_SHARE(x) \
124 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
125 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
126 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
127 #define LK_TRYOP(x) \
130 #define LK_CAN_WITNESS(x) \
131 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
132 #define LK_TRYWIT(x) \
133 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
135 #define LK_CAN_ADAPT(lk, f) \
136 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
137 ((f) & LK_SLEEPFAIL) == 0)
139 #define lockmgr_disowned(lk) \
140 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
142 #define lockmgr_xlocked(lk) \
143 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
145 static void assert_lockmgr(struct lock_object *lock, int how);
147 static void db_show_lockmgr(struct lock_object *lock);
149 static void lock_lockmgr(struct lock_object *lock, int how);
151 static int owner_lockmgr(struct lock_object *lock, struct thread **owner);
153 static int unlock_lockmgr(struct lock_object *lock);
155 struct lock_class lock_class_lockmgr = {
156 .lc_name = "lockmgr",
157 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
158 .lc_assert = assert_lockmgr,
160 .lc_ddb_show = db_show_lockmgr,
162 .lc_lock = lock_lockmgr,
163 .lc_unlock = unlock_lockmgr,
165 .lc_owner = owner_lockmgr,
169 static __inline struct thread *
170 lockmgr_xholder(struct lock *lk)
175 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
179 * It assumes sleepq_lock held and returns with this one unheld.
180 * It also assumes the generic interlock is sane and previously checked.
181 * If LK_INTERLOCK is specified the interlock is not reacquired after the
185 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
186 const char *wmesg, int pri, int timo, int queue)
189 struct lock_class *class;
192 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
193 catch = pri & PCATCH;
197 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
198 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
200 if (flags & LK_INTERLOCK)
201 class->lc_unlock(ilk);
202 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
205 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
206 SLEEPQ_INTERRUPTIBLE : 0), queue);
207 if ((flags & LK_TIMELOCK) && timo)
208 sleepq_set_timeout(&lk->lock_object, timo);
211 * Decisional switch for real sleeping.
213 if ((flags & LK_TIMELOCK) && timo && catch)
214 error = sleepq_timedwait_sig(&lk->lock_object, pri);
215 else if ((flags & LK_TIMELOCK) && timo)
216 error = sleepq_timedwait(&lk->lock_object, pri);
218 error = sleepq_wait_sig(&lk->lock_object, pri);
220 sleepq_wait(&lk->lock_object, pri);
222 if ((flags & LK_SLEEPFAIL) && error == 0)
229 wakeupshlk(struct lock *lk, const char *file, int line)
233 int queue, wakeup_swapper;
235 TD_LOCKS_DEC(curthread);
236 TD_SLOCKS_DEC(curthread);
237 WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
238 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
245 * If there is more than one shared lock held, just drop one
248 if (LK_SHARERS(x) > 1) {
249 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
256 * If there are not waiters on the exclusive queue, drop the
259 if ((x & LK_ALL_WAITERS) == 0) {
260 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
262 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
268 * We should have a sharer with waiters, so enter the hard
269 * path in order to handle wakeups correctly.
271 sleepq_lock(&lk->lock_object);
272 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
276 * If the lock has exclusive waiters, give them preference in
277 * order to avoid deadlock with shared runners up.
278 * If interruptible sleeps left the exclusive queue empty
279 * avoid a starvation for the threads sleeping on the shared
280 * queue by giving them precedence and cleaning up the
281 * exclusive waiters bit anyway.
282 * Please note that lk_exslpfail count may be lying about
283 * the real number of waiters with the LK_SLEEPFAIL flag on
284 * because they may be used in conjuction with interruptible
285 * sleeps so lk_exslpfail might be considered an 'upper limit'
286 * bound, including the edge cases.
288 realexslp = sleepq_sleepcnt(&lk->lock_object,
290 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
291 if (lk->lk_exslpfail < realexslp) {
292 lk->lk_exslpfail = 0;
293 queue = SQ_EXCLUSIVE_QUEUE;
294 v |= (x & LK_SHARED_WAITERS);
296 lk->lk_exslpfail = 0;
298 "%s: %p has only LK_SLEEPFAIL sleepers",
301 "%s: %p waking up threads on the exclusive queue",
304 sleepq_broadcast(&lk->lock_object,
305 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
306 queue = SQ_SHARED_QUEUE;
312 * Exclusive waiters sleeping with LK_SLEEPFAIL on
313 * and using interruptible sleeps/timeout may have
314 * left spourious lk_exslpfail counts on, so clean
317 lk->lk_exslpfail = 0;
318 queue = SQ_SHARED_QUEUE;
321 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
323 sleepq_release(&lk->lock_object);
326 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
327 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
329 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
331 sleepq_release(&lk->lock_object);
335 lock_profile_release_lock(&lk->lock_object);
336 return (wakeup_swapper);
340 assert_lockmgr(struct lock_object *lock, int what)
343 panic("lockmgr locks do not support assertions");
347 lock_lockmgr(struct lock_object *lock, int how)
350 panic("lockmgr locks do not support sleep interlocking");
354 unlock_lockmgr(struct lock_object *lock)
357 panic("lockmgr locks do not support sleep interlocking");
362 owner_lockmgr(struct lock_object *lock, struct thread **owner)
365 panic("lockmgr locks do not support owner inquiring");
370 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
374 MPASS((flags & ~LK_INIT_MASK) == 0);
375 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
376 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
379 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
380 if (flags & LK_CANRECURSE)
381 iflags |= LO_RECURSABLE;
382 if ((flags & LK_NODUP) == 0)
384 if (flags & LK_NOPROFILE)
385 iflags |= LO_NOPROFILE;
386 if ((flags & LK_NOWITNESS) == 0)
387 iflags |= LO_WITNESS;
388 if (flags & LK_QUIET)
390 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
392 lk->lk_lock = LK_UNLOCKED;
394 lk->lk_exslpfail = 0;
397 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
402 * XXX: Gross hacks to manipulate external lock flags after
403 * initialization. Used for certain vnode and buf locks.
406 lockallowshare(struct lock *lk)
409 lockmgr_assert(lk, KA_XLOCKED);
410 lk->lock_object.lo_flags &= ~LK_NOSHARE;
414 lockallowrecurse(struct lock *lk)
417 lockmgr_assert(lk, KA_XLOCKED);
418 lk->lock_object.lo_flags |= LO_RECURSABLE;
422 lockdisablerecurse(struct lock *lk)
425 lockmgr_assert(lk, KA_XLOCKED);
426 lk->lock_object.lo_flags &= ~LO_RECURSABLE;
430 lockdestroy(struct lock *lk)
433 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
434 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
435 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
436 lock_destroy(&lk->lock_object);
440 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
441 const char *wmesg, int pri, int timo, const char *file, int line)
444 struct lock_class *class;
448 int error, ipri, itimo, queue, wakeup_swapper;
449 #ifdef LOCK_PROFILING
450 uint64_t waittime = 0;
453 #ifdef ADAPTIVE_LOCKMGRS
454 volatile struct thread *owner;
455 u_int i, spintries = 0;
459 tid = (uintptr_t)curthread;
460 op = (flags & LK_TYPE_MASK);
461 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
462 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
463 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
465 MPASS((flags & ~LK_TOTAL_MASK) == 0);
466 KASSERT((op & (op - 1)) == 0,
467 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
468 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
469 (op != LK_DOWNGRADE && op != LK_RELEASE),
470 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
471 __func__, file, line));
472 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
473 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
474 __func__, file, line));
476 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
477 if (panicstr != NULL) {
478 if (flags & LK_INTERLOCK)
479 class->lc_unlock(ilk);
483 if (lk->lock_object.lo_flags & LK_NOSHARE) {
490 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED,
499 if (LK_CAN_WITNESS(flags))
500 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
506 * If no other thread has an exclusive lock, or
507 * no exclusive waiter is present, bump the count of
508 * sharers. Since we have to preserve the state of
509 * waiters, if we fail to acquire the shared lock
510 * loop back and retry.
512 if (LK_CAN_SHARE(x)) {
513 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
519 PMC_SOFT_CALL( , , lock, failed);
521 lock_profile_obtain_lock_failed(&lk->lock_object,
522 &contested, &waittime);
525 * If the lock is already held by curthread in
526 * exclusive way avoid a deadlock.
528 if (LK_HOLDER(x) == tid) {
530 "%s: %p already held in exclusive mode",
537 * If the lock is expected to not sleep just give up
540 if (LK_TRYOP(flags)) {
541 LOCK_LOG2(lk, "%s: %p fails the try operation",
547 #ifdef ADAPTIVE_LOCKMGRS
549 * If the owner is running on another CPU, spin until
550 * the owner stops running or the state of the lock
551 * changes. We need a double-state handle here
552 * because for a failed acquisition the lock can be
553 * either held in exclusive mode or shared mode
554 * (for the writer starvation avoidance technique).
556 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
557 LK_HOLDER(x) != LK_KERNPROC) {
558 owner = (struct thread *)LK_HOLDER(x);
559 if (LOCK_LOG_TEST(&lk->lock_object, 0))
561 "%s: spinning on %p held by %p",
562 __func__, lk, owner);
565 * If we are holding also an interlock drop it
566 * in order to avoid a deadlock if the lockmgr
567 * owner is adaptively spinning on the
570 if (flags & LK_INTERLOCK) {
571 class->lc_unlock(ilk);
572 flags &= ~LK_INTERLOCK;
575 while (LK_HOLDER(lk->lk_lock) ==
576 (uintptr_t)owner && TD_IS_RUNNING(owner))
580 } else if (LK_CAN_ADAPT(lk, flags) &&
581 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
582 spintries < ALK_RETRIES) {
583 if (flags & LK_INTERLOCK) {
584 class->lc_unlock(ilk);
585 flags &= ~LK_INTERLOCK;
589 for (i = 0; i < ALK_LOOPS; i++) {
590 if (LOCK_LOG_TEST(&lk->lock_object, 0))
592 "%s: shared spinning on %p with %u and %u",
593 __func__, lk, spintries, i);
595 if ((x & LK_SHARE) == 0 ||
596 LK_CAN_SHARE(x) != 0)
607 * Acquire the sleepqueue chain lock because we
608 * probabilly will need to manipulate waiters flags.
610 sleepq_lock(&lk->lock_object);
614 * if the lock can be acquired in shared mode, try
617 if (LK_CAN_SHARE(x)) {
618 sleepq_release(&lk->lock_object);
622 #ifdef ADAPTIVE_LOCKMGRS
624 * The current lock owner might have started executing
625 * on another CPU (or the lock could have changed
626 * owner) while we were waiting on the turnstile
627 * chain lock. If so, drop the turnstile lock and try
630 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
631 LK_HOLDER(x) != LK_KERNPROC) {
632 owner = (struct thread *)LK_HOLDER(x);
633 if (TD_IS_RUNNING(owner)) {
634 sleepq_release(&lk->lock_object);
641 * Try to set the LK_SHARED_WAITERS flag. If we fail,
642 * loop back and retry.
644 if ((x & LK_SHARED_WAITERS) == 0) {
645 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
646 x | LK_SHARED_WAITERS)) {
647 sleepq_release(&lk->lock_object);
650 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
655 * As far as we have been unable to acquire the
656 * shared lock and the shared waiters flag is set,
659 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
661 flags &= ~LK_INTERLOCK;
664 "%s: interrupted sleep for %p with %d",
665 __func__, lk, error);
668 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
672 lock_profile_obtain_lock_success(&lk->lock_object,
673 contested, waittime, file, line);
674 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
676 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
678 TD_LOCKS_INC(curthread);
679 TD_SLOCKS_INC(curthread);
684 _lockmgr_assert(lk, KA_SLOCKED, file, line);
686 x = v & LK_ALL_WAITERS;
687 v &= LK_EXCLUSIVE_SPINNERS;
690 * Try to switch from one shared lock to an exclusive one.
691 * We need to preserve waiters flags during the operation.
693 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
695 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
697 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
698 LK_TRYWIT(flags), file, line);
699 TD_SLOCKS_DEC(curthread);
704 * We have been unable to succeed in upgrading, so just
705 * give up the shared lock.
707 wakeup_swapper |= wakeupshlk(lk, file, line);
711 if (LK_CAN_WITNESS(flags))
712 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
713 LOP_EXCLUSIVE, file, line, ilk);
716 * If curthread already holds the lock and this one is
717 * allowed to recurse, simply recurse on it.
719 if (lockmgr_xlocked(lk)) {
720 if ((flags & LK_CANRECURSE) == 0 &&
721 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
724 * If the lock is expected to not panic just
725 * give up and return.
727 if (LK_TRYOP(flags)) {
729 "%s: %p fails the try operation",
734 if (flags & LK_INTERLOCK)
735 class->lc_unlock(ilk);
736 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
737 __func__, iwmesg, file, line);
740 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
741 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
742 lk->lk_recurse, file, line);
743 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
744 LK_TRYWIT(flags), file, line);
745 TD_LOCKS_INC(curthread);
749 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
752 PMC_SOFT_CALL( , , lock, failed);
754 lock_profile_obtain_lock_failed(&lk->lock_object,
755 &contested, &waittime);
758 * If the lock is expected to not sleep just give up
761 if (LK_TRYOP(flags)) {
762 LOCK_LOG2(lk, "%s: %p fails the try operation",
768 #ifdef ADAPTIVE_LOCKMGRS
770 * If the owner is running on another CPU, spin until
771 * the owner stops running or the state of the lock
775 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
776 LK_HOLDER(x) != LK_KERNPROC) {
777 owner = (struct thread *)LK_HOLDER(x);
778 if (LOCK_LOG_TEST(&lk->lock_object, 0))
780 "%s: spinning on %p held by %p",
781 __func__, lk, owner);
784 * If we are holding also an interlock drop it
785 * in order to avoid a deadlock if the lockmgr
786 * owner is adaptively spinning on the
789 if (flags & LK_INTERLOCK) {
790 class->lc_unlock(ilk);
791 flags &= ~LK_INTERLOCK;
794 while (LK_HOLDER(lk->lk_lock) ==
795 (uintptr_t)owner && TD_IS_RUNNING(owner))
799 } else if (LK_CAN_ADAPT(lk, flags) &&
800 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
801 spintries < ALK_RETRIES) {
802 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
803 !atomic_cmpset_ptr(&lk->lk_lock, x,
804 x | LK_EXCLUSIVE_SPINNERS))
806 if (flags & LK_INTERLOCK) {
807 class->lc_unlock(ilk);
808 flags &= ~LK_INTERLOCK;
812 for (i = 0; i < ALK_LOOPS; i++) {
813 if (LOCK_LOG_TEST(&lk->lock_object, 0))
815 "%s: shared spinning on %p with %u and %u",
816 __func__, lk, spintries, i);
818 LK_EXCLUSIVE_SPINNERS) == 0)
829 * Acquire the sleepqueue chain lock because we
830 * probabilly will need to manipulate waiters flags.
832 sleepq_lock(&lk->lock_object);
836 * if the lock has been released while we spun on
837 * the sleepqueue chain lock just try again.
839 if (x == LK_UNLOCKED) {
840 sleepq_release(&lk->lock_object);
844 #ifdef ADAPTIVE_LOCKMGRS
846 * The current lock owner might have started executing
847 * on another CPU (or the lock could have changed
848 * owner) while we were waiting on the turnstile
849 * chain lock. If so, drop the turnstile lock and try
852 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
853 LK_HOLDER(x) != LK_KERNPROC) {
854 owner = (struct thread *)LK_HOLDER(x);
855 if (TD_IS_RUNNING(owner)) {
856 sleepq_release(&lk->lock_object);
863 * The lock can be in the state where there is a
864 * pending queue of waiters, but still no owner.
865 * This happens when the lock is contested and an
866 * owner is going to claim the lock.
867 * If curthread is the one successfully acquiring it
868 * claim lock ownership and return, preserving waiters
871 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
872 if ((x & ~v) == LK_UNLOCKED) {
873 v &= ~LK_EXCLUSIVE_SPINNERS;
874 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
876 sleepq_release(&lk->lock_object);
878 "%s: %p claimed by a new writer",
882 sleepq_release(&lk->lock_object);
887 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
888 * fail, loop back and retry.
890 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
891 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
892 x | LK_EXCLUSIVE_WAITERS)) {
893 sleepq_release(&lk->lock_object);
896 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
901 * As far as we have been unable to acquire the
902 * exclusive lock and the exclusive waiters flag
903 * is set, we will sleep.
905 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
907 flags &= ~LK_INTERLOCK;
910 "%s: interrupted sleep for %p with %d",
911 __func__, lk, error);
914 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
918 lock_profile_obtain_lock_success(&lk->lock_object,
919 contested, waittime, file, line);
920 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
921 lk->lk_recurse, file, line);
922 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
923 LK_TRYWIT(flags), file, line);
924 TD_LOCKS_INC(curthread);
929 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
930 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
931 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
932 TD_SLOCKS_INC(curthread);
935 * In order to preserve waiters flags, just spin.
939 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
941 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
942 LK_SHARERS_LOCK(1) | x))
948 _lockmgr_assert(lk, KA_LOCKED, file, line);
951 if ((x & LK_SHARE) == 0) {
954 * As first option, treact the lock as if it has not
956 * Fix-up the tid var if the lock has been disowned.
958 if (LK_HOLDER(x) == LK_KERNPROC)
961 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
963 TD_LOCKS_DEC(curthread);
965 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
966 lk->lk_recurse, file, line);
969 * The lock is held in exclusive mode.
970 * If the lock is recursed also, then unrecurse it.
972 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
973 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
978 if (tid != LK_KERNPROC)
979 lock_profile_release_lock(&lk->lock_object);
981 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
985 sleepq_lock(&lk->lock_object);
990 * If the lock has exclusive waiters, give them
991 * preference in order to avoid deadlock with
993 * If interruptible sleeps left the exclusive queue
994 * empty avoid a starvation for the threads sleeping
995 * on the shared queue by giving them precedence
996 * and cleaning up the exclusive waiters bit anyway.
997 * Please note that lk_exslpfail count may be lying
998 * about the real number of waiters with the
999 * LK_SLEEPFAIL flag on because they may be used in
1000 * conjuction with interruptible sleeps so
1001 * lk_exslpfail might be considered an 'upper limit'
1002 * bound, including the edge cases.
1004 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1005 realexslp = sleepq_sleepcnt(&lk->lock_object,
1006 SQ_EXCLUSIVE_QUEUE);
1007 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1008 if (lk->lk_exslpfail < realexslp) {
1009 lk->lk_exslpfail = 0;
1010 queue = SQ_EXCLUSIVE_QUEUE;
1011 v |= (x & LK_SHARED_WAITERS);
1013 lk->lk_exslpfail = 0;
1015 "%s: %p has only LK_SLEEPFAIL sleepers",
1018 "%s: %p waking up threads on the exclusive queue",
1021 sleepq_broadcast(&lk->lock_object,
1022 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1023 queue = SQ_SHARED_QUEUE;
1028 * Exclusive waiters sleeping with LK_SLEEPFAIL
1029 * on and using interruptible sleeps/timeout
1030 * may have left spourious lk_exslpfail counts
1031 * on, so clean it up anyway.
1033 lk->lk_exslpfail = 0;
1034 queue = SQ_SHARED_QUEUE;
1038 "%s: %p waking up threads on the %s queue",
1039 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1041 atomic_store_rel_ptr(&lk->lk_lock, v);
1042 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1043 SLEEPQ_LK, 0, queue);
1044 sleepq_release(&lk->lock_object);
1047 wakeup_swapper = wakeupshlk(lk, file, line);
1050 if (LK_CAN_WITNESS(flags))
1051 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1052 LOP_EXCLUSIVE, file, line, ilk);
1055 * Trying to drain a lock we already own will result in a
1058 if (lockmgr_xlocked(lk)) {
1059 if (flags & LK_INTERLOCK)
1060 class->lc_unlock(ilk);
1061 panic("%s: draining %s with the lock held @ %s:%d\n",
1062 __func__, iwmesg, file, line);
1065 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1067 PMC_SOFT_CALL( , , lock, failed);
1069 lock_profile_obtain_lock_failed(&lk->lock_object,
1070 &contested, &waittime);
1073 * If the lock is expected to not sleep just give up
1076 if (LK_TRYOP(flags)) {
1077 LOCK_LOG2(lk, "%s: %p fails the try operation",
1084 * Acquire the sleepqueue chain lock because we
1085 * probabilly will need to manipulate waiters flags.
1087 sleepq_lock(&lk->lock_object);
1091 * if the lock has been released while we spun on
1092 * the sleepqueue chain lock just try again.
1094 if (x == LK_UNLOCKED) {
1095 sleepq_release(&lk->lock_object);
1099 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1100 if ((x & ~v) == LK_UNLOCKED) {
1101 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1104 * If interruptible sleeps left the exclusive
1105 * queue empty avoid a starvation for the
1106 * threads sleeping on the shared queue by
1107 * giving them precedence and cleaning up the
1108 * exclusive waiters bit anyway.
1109 * Please note that lk_exslpfail count may be
1110 * lying about the real number of waiters with
1111 * the LK_SLEEPFAIL flag on because they may
1112 * be used in conjuction with interruptible
1113 * sleeps so lk_exslpfail might be considered
1114 * an 'upper limit' bound, including the edge
1117 if (v & LK_EXCLUSIVE_WAITERS) {
1118 queue = SQ_EXCLUSIVE_QUEUE;
1119 v &= ~LK_EXCLUSIVE_WAITERS;
1123 * Exclusive waiters sleeping with
1124 * LK_SLEEPFAIL on and using
1125 * interruptible sleeps/timeout may
1126 * have left spourious lk_exslpfail
1127 * counts on, so clean it up anyway.
1129 MPASS(v & LK_SHARED_WAITERS);
1130 lk->lk_exslpfail = 0;
1131 queue = SQ_SHARED_QUEUE;
1132 v &= ~LK_SHARED_WAITERS;
1134 if (queue == SQ_EXCLUSIVE_QUEUE) {
1136 sleepq_sleepcnt(&lk->lock_object,
1137 SQ_EXCLUSIVE_QUEUE);
1138 if (lk->lk_exslpfail >= realexslp) {
1139 lk->lk_exslpfail = 0;
1140 queue = SQ_SHARED_QUEUE;
1141 v &= ~LK_SHARED_WAITERS;
1142 if (realexslp != 0) {
1144 "%s: %p has only LK_SLEEPFAIL sleepers",
1147 "%s: %p waking up threads on the exclusive queue",
1153 SQ_EXCLUSIVE_QUEUE);
1156 lk->lk_exslpfail = 0;
1158 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1159 sleepq_release(&lk->lock_object);
1163 "%s: %p waking up all threads on the %s queue",
1164 __func__, lk, queue == SQ_SHARED_QUEUE ?
1165 "shared" : "exclusive");
1166 wakeup_swapper |= sleepq_broadcast(
1167 &lk->lock_object, SLEEPQ_LK, 0, queue);
1170 * If shared waiters have been woken up we need
1171 * to wait for one of them to acquire the lock
1172 * before to set the exclusive waiters in
1173 * order to avoid a deadlock.
1175 if (queue == SQ_SHARED_QUEUE) {
1176 for (v = lk->lk_lock;
1177 (v & LK_SHARE) && !LK_SHARERS(v);
1184 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1185 * fail, loop back and retry.
1187 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1188 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1189 x | LK_EXCLUSIVE_WAITERS)) {
1190 sleepq_release(&lk->lock_object);
1193 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1198 * As far as we have been unable to acquire the
1199 * exclusive lock and the exclusive waiters flag
1200 * is set, we will sleep.
1202 if (flags & LK_INTERLOCK) {
1203 class->lc_unlock(ilk);
1204 flags &= ~LK_INTERLOCK;
1207 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1208 SQ_EXCLUSIVE_QUEUE);
1209 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1211 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1216 lock_profile_obtain_lock_success(&lk->lock_object,
1217 contested, waittime, file, line);
1218 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1219 lk->lk_recurse, file, line);
1220 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1221 LK_TRYWIT(flags), file, line);
1222 TD_LOCKS_INC(curthread);
1227 if (flags & LK_INTERLOCK)
1228 class->lc_unlock(ilk);
1229 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1232 if (flags & LK_INTERLOCK)
1233 class->lc_unlock(ilk);
1241 _lockmgr_disown(struct lock *lk, const char *file, int line)
1245 if (SCHEDULER_STOPPED())
1248 tid = (uintptr_t)curthread;
1249 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1252 * If the owner is already LK_KERNPROC just skip the whole operation.
1254 if (LK_HOLDER(lk->lk_lock) != tid)
1256 lock_profile_release_lock(&lk->lock_object);
1257 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1258 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1259 TD_LOCKS_DEC(curthread);
1263 * In order to preserve waiters flags, just spin.
1267 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1268 x &= LK_ALL_WAITERS;
1269 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1277 lockmgr_printinfo(struct lock *lk)
1282 if (lk->lk_lock == LK_UNLOCKED)
1283 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1284 else if (lk->lk_lock & LK_SHARE)
1285 printf("lock type %s: SHARED (count %ju)\n",
1286 lk->lock_object.lo_name,
1287 (uintmax_t)LK_SHARERS(lk->lk_lock));
1289 td = lockmgr_xholder(lk);
1290 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1291 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1295 if (x & LK_EXCLUSIVE_WAITERS)
1296 printf(" with exclusive waiters pending\n");
1297 if (x & LK_SHARED_WAITERS)
1298 printf(" with shared waiters pending\n");
1299 if (x & LK_EXCLUSIVE_SPINNERS)
1300 printf(" with exclusive spinners pending\n");
1306 lockstatus(struct lock *lk)
1315 if ((x & LK_SHARE) == 0) {
1316 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1320 } else if (x == LK_UNLOCKED)
1326 #ifdef INVARIANT_SUPPORT
1328 FEATURE(invariant_support,
1329 "Support for modules compiled with INVARIANTS option");
1332 #undef _lockmgr_assert
1336 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1340 if (panicstr != NULL)
1344 case KA_SLOCKED | KA_NOTRECURSED:
1345 case KA_SLOCKED | KA_RECURSED:
1348 case KA_LOCKED | KA_NOTRECURSED:
1349 case KA_LOCKED | KA_RECURSED:
1353 * We cannot trust WITNESS if the lock is held in exclusive
1354 * mode and a call to lockmgr_disown() happened.
1355 * Workaround this skipping the check if the lock is held in
1356 * exclusive mode even for the KA_LOCKED case.
1358 if (slocked || (lk->lk_lock & LK_SHARE)) {
1359 witness_assert(&lk->lock_object, what, file, line);
1363 if (lk->lk_lock == LK_UNLOCKED ||
1364 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1365 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1366 panic("Lock %s not %slocked @ %s:%d\n",
1367 lk->lock_object.lo_name, slocked ? "share" : "",
1370 if ((lk->lk_lock & LK_SHARE) == 0) {
1371 if (lockmgr_recursed(lk)) {
1372 if (what & KA_NOTRECURSED)
1373 panic("Lock %s recursed @ %s:%d\n",
1374 lk->lock_object.lo_name, file,
1376 } else if (what & KA_RECURSED)
1377 panic("Lock %s not recursed @ %s:%d\n",
1378 lk->lock_object.lo_name, file, line);
1382 case KA_XLOCKED | KA_NOTRECURSED:
1383 case KA_XLOCKED | KA_RECURSED:
1384 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1385 panic("Lock %s not exclusively locked @ %s:%d\n",
1386 lk->lock_object.lo_name, file, line);
1387 if (lockmgr_recursed(lk)) {
1388 if (what & KA_NOTRECURSED)
1389 panic("Lock %s recursed @ %s:%d\n",
1390 lk->lock_object.lo_name, file, line);
1391 } else if (what & KA_RECURSED)
1392 panic("Lock %s not recursed @ %s:%d\n",
1393 lk->lock_object.lo_name, file, line);
1396 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1397 panic("Lock %s exclusively locked @ %s:%d\n",
1398 lk->lock_object.lo_name, file, line);
1401 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1409 lockmgr_chain(struct thread *td, struct thread **ownerp)
1415 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1417 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1418 if (lk->lk_lock & LK_SHARE)
1419 db_printf("SHARED (count %ju)\n",
1420 (uintmax_t)LK_SHARERS(lk->lk_lock));
1422 db_printf("EXCL\n");
1423 *ownerp = lockmgr_xholder(lk);
1429 db_show_lockmgr(struct lock_object *lock)
1434 lk = (struct lock *)lock;
1436 db_printf(" state: ");
1437 if (lk->lk_lock == LK_UNLOCKED)
1438 db_printf("UNLOCKED\n");
1439 else if (lk->lk_lock & LK_SHARE)
1440 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1442 td = lockmgr_xholder(lk);
1443 if (td == (struct thread *)LK_KERNPROC)
1444 db_printf("XLOCK: LK_KERNPROC\n");
1446 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1447 td->td_tid, td->td_proc->p_pid,
1448 td->td_proc->p_comm);
1449 if (lockmgr_recursed(lk))
1450 db_printf(" recursed: %d\n", lk->lk_recurse);
1452 db_printf(" waiters: ");
1453 switch (lk->lk_lock & LK_ALL_WAITERS) {
1454 case LK_SHARED_WAITERS:
1455 db_printf("shared\n");
1457 case LK_EXCLUSIVE_WAITERS:
1458 db_printf("exclusive\n");
1460 case LK_ALL_WAITERS:
1461 db_printf("shared and exclusive\n");
1464 db_printf("none\n");
1466 db_printf(" spinners: ");
1467 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1468 db_printf("exclusive\n");
1470 db_printf("none\n");