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,
492 if (flags & LK_INTERLOCK)
493 class->lc_unlock(ilk);
501 if (LK_CAN_WITNESS(flags))
502 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
508 * If no other thread has an exclusive lock, or
509 * no exclusive waiter is present, bump the count of
510 * sharers. Since we have to preserve the state of
511 * waiters, if we fail to acquire the shared lock
512 * loop back and retry.
514 if (LK_CAN_SHARE(x)) {
515 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
521 PMC_SOFT_CALL( , , lock, failed);
523 lock_profile_obtain_lock_failed(&lk->lock_object,
524 &contested, &waittime);
527 * If the lock is already held by curthread in
528 * exclusive way avoid a deadlock.
530 if (LK_HOLDER(x) == tid) {
532 "%s: %p already held in exclusive mode",
539 * If the lock is expected to not sleep just give up
542 if (LK_TRYOP(flags)) {
543 LOCK_LOG2(lk, "%s: %p fails the try operation",
549 #ifdef ADAPTIVE_LOCKMGRS
551 * If the owner is running on another CPU, spin until
552 * the owner stops running or the state of the lock
553 * changes. We need a double-state handle here
554 * because for a failed acquisition the lock can be
555 * either held in exclusive mode or shared mode
556 * (for the writer starvation avoidance technique).
558 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
559 LK_HOLDER(x) != LK_KERNPROC) {
560 owner = (struct thread *)LK_HOLDER(x);
561 if (LOCK_LOG_TEST(&lk->lock_object, 0))
563 "%s: spinning on %p held by %p",
564 __func__, lk, owner);
567 * If we are holding also an interlock drop it
568 * in order to avoid a deadlock if the lockmgr
569 * owner is adaptively spinning on the
572 if (flags & LK_INTERLOCK) {
573 class->lc_unlock(ilk);
574 flags &= ~LK_INTERLOCK;
577 while (LK_HOLDER(lk->lk_lock) ==
578 (uintptr_t)owner && TD_IS_RUNNING(owner))
582 } else if (LK_CAN_ADAPT(lk, flags) &&
583 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
584 spintries < ALK_RETRIES) {
585 if (flags & LK_INTERLOCK) {
586 class->lc_unlock(ilk);
587 flags &= ~LK_INTERLOCK;
591 for (i = 0; i < ALK_LOOPS; i++) {
592 if (LOCK_LOG_TEST(&lk->lock_object, 0))
594 "%s: shared spinning on %p with %u and %u",
595 __func__, lk, spintries, i);
597 if ((x & LK_SHARE) == 0 ||
598 LK_CAN_SHARE(x) != 0)
609 * Acquire the sleepqueue chain lock because we
610 * probabilly will need to manipulate waiters flags.
612 sleepq_lock(&lk->lock_object);
616 * if the lock can be acquired in shared mode, try
619 if (LK_CAN_SHARE(x)) {
620 sleepq_release(&lk->lock_object);
624 #ifdef ADAPTIVE_LOCKMGRS
626 * The current lock owner might have started executing
627 * on another CPU (or the lock could have changed
628 * owner) while we were waiting on the turnstile
629 * chain lock. If so, drop the turnstile lock and try
632 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
633 LK_HOLDER(x) != LK_KERNPROC) {
634 owner = (struct thread *)LK_HOLDER(x);
635 if (TD_IS_RUNNING(owner)) {
636 sleepq_release(&lk->lock_object);
643 * Try to set the LK_SHARED_WAITERS flag. If we fail,
644 * loop back and retry.
646 if ((x & LK_SHARED_WAITERS) == 0) {
647 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
648 x | LK_SHARED_WAITERS)) {
649 sleepq_release(&lk->lock_object);
652 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
657 * As far as we have been unable to acquire the
658 * shared lock and the shared waiters flag is set,
661 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
663 flags &= ~LK_INTERLOCK;
666 "%s: interrupted sleep for %p with %d",
667 __func__, lk, error);
670 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
674 lock_profile_obtain_lock_success(&lk->lock_object,
675 contested, waittime, file, line);
676 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
678 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
680 TD_LOCKS_INC(curthread);
681 TD_SLOCKS_INC(curthread);
686 _lockmgr_assert(lk, KA_SLOCKED, file, line);
688 x = v & LK_ALL_WAITERS;
689 v &= LK_EXCLUSIVE_SPINNERS;
692 * Try to switch from one shared lock to an exclusive one.
693 * We need to preserve waiters flags during the operation.
695 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
697 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
699 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
700 LK_TRYWIT(flags), file, line);
701 TD_SLOCKS_DEC(curthread);
706 * We have been unable to succeed in upgrading, so just
707 * give up the shared lock.
709 wakeup_swapper |= wakeupshlk(lk, file, line);
713 if (LK_CAN_WITNESS(flags))
714 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
715 LOP_EXCLUSIVE, file, line, ilk);
718 * If curthread already holds the lock and this one is
719 * allowed to recurse, simply recurse on it.
721 if (lockmgr_xlocked(lk)) {
722 if ((flags & LK_CANRECURSE) == 0 &&
723 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
726 * If the lock is expected to not panic just
727 * give up and return.
729 if (LK_TRYOP(flags)) {
731 "%s: %p fails the try operation",
736 if (flags & LK_INTERLOCK)
737 class->lc_unlock(ilk);
738 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
739 __func__, iwmesg, file, line);
742 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
743 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
744 lk->lk_recurse, file, line);
745 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
746 LK_TRYWIT(flags), file, line);
747 TD_LOCKS_INC(curthread);
751 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
754 PMC_SOFT_CALL( , , lock, failed);
756 lock_profile_obtain_lock_failed(&lk->lock_object,
757 &contested, &waittime);
760 * If the lock is expected to not sleep just give up
763 if (LK_TRYOP(flags)) {
764 LOCK_LOG2(lk, "%s: %p fails the try operation",
770 #ifdef ADAPTIVE_LOCKMGRS
772 * If the owner is running on another CPU, spin until
773 * the owner stops running or the state of the lock
777 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
778 LK_HOLDER(x) != LK_KERNPROC) {
779 owner = (struct thread *)LK_HOLDER(x);
780 if (LOCK_LOG_TEST(&lk->lock_object, 0))
782 "%s: spinning on %p held by %p",
783 __func__, lk, owner);
786 * If we are holding also an interlock drop it
787 * in order to avoid a deadlock if the lockmgr
788 * owner is adaptively spinning on the
791 if (flags & LK_INTERLOCK) {
792 class->lc_unlock(ilk);
793 flags &= ~LK_INTERLOCK;
796 while (LK_HOLDER(lk->lk_lock) ==
797 (uintptr_t)owner && TD_IS_RUNNING(owner))
801 } else if (LK_CAN_ADAPT(lk, flags) &&
802 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
803 spintries < ALK_RETRIES) {
804 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
805 !atomic_cmpset_ptr(&lk->lk_lock, x,
806 x | LK_EXCLUSIVE_SPINNERS))
808 if (flags & LK_INTERLOCK) {
809 class->lc_unlock(ilk);
810 flags &= ~LK_INTERLOCK;
814 for (i = 0; i < ALK_LOOPS; i++) {
815 if (LOCK_LOG_TEST(&lk->lock_object, 0))
817 "%s: shared spinning on %p with %u and %u",
818 __func__, lk, spintries, i);
820 LK_EXCLUSIVE_SPINNERS) == 0)
831 * Acquire the sleepqueue chain lock because we
832 * probabilly will need to manipulate waiters flags.
834 sleepq_lock(&lk->lock_object);
838 * if the lock has been released while we spun on
839 * the sleepqueue chain lock just try again.
841 if (x == LK_UNLOCKED) {
842 sleepq_release(&lk->lock_object);
846 #ifdef ADAPTIVE_LOCKMGRS
848 * The current lock owner might have started executing
849 * on another CPU (or the lock could have changed
850 * owner) while we were waiting on the turnstile
851 * chain lock. If so, drop the turnstile lock and try
854 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
855 LK_HOLDER(x) != LK_KERNPROC) {
856 owner = (struct thread *)LK_HOLDER(x);
857 if (TD_IS_RUNNING(owner)) {
858 sleepq_release(&lk->lock_object);
865 * The lock can be in the state where there is a
866 * pending queue of waiters, but still no owner.
867 * This happens when the lock is contested and an
868 * owner is going to claim the lock.
869 * If curthread is the one successfully acquiring it
870 * claim lock ownership and return, preserving waiters
873 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
874 if ((x & ~v) == LK_UNLOCKED) {
875 v &= ~LK_EXCLUSIVE_SPINNERS;
876 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
878 sleepq_release(&lk->lock_object);
880 "%s: %p claimed by a new writer",
884 sleepq_release(&lk->lock_object);
889 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
890 * fail, loop back and retry.
892 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
893 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
894 x | LK_EXCLUSIVE_WAITERS)) {
895 sleepq_release(&lk->lock_object);
898 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
903 * As far as we have been unable to acquire the
904 * exclusive lock and the exclusive waiters flag
905 * is set, we will sleep.
907 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
909 flags &= ~LK_INTERLOCK;
912 "%s: interrupted sleep for %p with %d",
913 __func__, lk, error);
916 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
920 lock_profile_obtain_lock_success(&lk->lock_object,
921 contested, waittime, file, line);
922 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
923 lk->lk_recurse, file, line);
924 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
925 LK_TRYWIT(flags), file, line);
926 TD_LOCKS_INC(curthread);
931 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
932 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
933 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
934 TD_SLOCKS_INC(curthread);
937 * In order to preserve waiters flags, just spin.
941 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
943 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
944 LK_SHARERS_LOCK(1) | x))
950 _lockmgr_assert(lk, KA_LOCKED, file, line);
953 if ((x & LK_SHARE) == 0) {
956 * As first option, treact the lock as if it has not
958 * Fix-up the tid var if the lock has been disowned.
960 if (LK_HOLDER(x) == LK_KERNPROC)
963 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
965 TD_LOCKS_DEC(curthread);
967 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
968 lk->lk_recurse, file, line);
971 * The lock is held in exclusive mode.
972 * If the lock is recursed also, then unrecurse it.
974 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
975 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
980 if (tid != LK_KERNPROC)
981 lock_profile_release_lock(&lk->lock_object);
983 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
987 sleepq_lock(&lk->lock_object);
992 * If the lock has exclusive waiters, give them
993 * preference in order to avoid deadlock with
995 * If interruptible sleeps left the exclusive queue
996 * empty avoid a starvation for the threads sleeping
997 * on the shared queue by giving them precedence
998 * and cleaning up the exclusive waiters bit anyway.
999 * Please note that lk_exslpfail count may be lying
1000 * about the real number of waiters with the
1001 * LK_SLEEPFAIL flag on because they may be used in
1002 * conjuction with interruptible sleeps so
1003 * lk_exslpfail might be considered an 'upper limit'
1004 * bound, including the edge cases.
1006 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1007 realexslp = sleepq_sleepcnt(&lk->lock_object,
1008 SQ_EXCLUSIVE_QUEUE);
1009 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
1010 if (lk->lk_exslpfail < realexslp) {
1011 lk->lk_exslpfail = 0;
1012 queue = SQ_EXCLUSIVE_QUEUE;
1013 v |= (x & LK_SHARED_WAITERS);
1015 lk->lk_exslpfail = 0;
1017 "%s: %p has only LK_SLEEPFAIL sleepers",
1020 "%s: %p waking up threads on the exclusive queue",
1023 sleepq_broadcast(&lk->lock_object,
1024 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1025 queue = SQ_SHARED_QUEUE;
1030 * Exclusive waiters sleeping with LK_SLEEPFAIL
1031 * on and using interruptible sleeps/timeout
1032 * may have left spourious lk_exslpfail counts
1033 * on, so clean it up anyway.
1035 lk->lk_exslpfail = 0;
1036 queue = SQ_SHARED_QUEUE;
1040 "%s: %p waking up threads on the %s queue",
1041 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1043 atomic_store_rel_ptr(&lk->lk_lock, v);
1044 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1045 SLEEPQ_LK, 0, queue);
1046 sleepq_release(&lk->lock_object);
1049 wakeup_swapper = wakeupshlk(lk, file, line);
1052 if (LK_CAN_WITNESS(flags))
1053 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1054 LOP_EXCLUSIVE, file, line, ilk);
1057 * Trying to drain a lock we already own will result in a
1060 if (lockmgr_xlocked(lk)) {
1061 if (flags & LK_INTERLOCK)
1062 class->lc_unlock(ilk);
1063 panic("%s: draining %s with the lock held @ %s:%d\n",
1064 __func__, iwmesg, file, line);
1067 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1069 PMC_SOFT_CALL( , , lock, failed);
1071 lock_profile_obtain_lock_failed(&lk->lock_object,
1072 &contested, &waittime);
1075 * If the lock is expected to not sleep just give up
1078 if (LK_TRYOP(flags)) {
1079 LOCK_LOG2(lk, "%s: %p fails the try operation",
1086 * Acquire the sleepqueue chain lock because we
1087 * probabilly will need to manipulate waiters flags.
1089 sleepq_lock(&lk->lock_object);
1093 * if the lock has been released while we spun on
1094 * the sleepqueue chain lock just try again.
1096 if (x == LK_UNLOCKED) {
1097 sleepq_release(&lk->lock_object);
1101 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1102 if ((x & ~v) == LK_UNLOCKED) {
1103 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1106 * If interruptible sleeps left the exclusive
1107 * queue empty avoid a starvation for the
1108 * threads sleeping on the shared queue by
1109 * giving them precedence and cleaning up the
1110 * exclusive waiters bit anyway.
1111 * Please note that lk_exslpfail count may be
1112 * lying about the real number of waiters with
1113 * the LK_SLEEPFAIL flag on because they may
1114 * be used in conjuction with interruptible
1115 * sleeps so lk_exslpfail might be considered
1116 * an 'upper limit' bound, including the edge
1119 if (v & LK_EXCLUSIVE_WAITERS) {
1120 queue = SQ_EXCLUSIVE_QUEUE;
1121 v &= ~LK_EXCLUSIVE_WAITERS;
1125 * Exclusive waiters sleeping with
1126 * LK_SLEEPFAIL on and using
1127 * interruptible sleeps/timeout may
1128 * have left spourious lk_exslpfail
1129 * counts on, so clean it up anyway.
1131 MPASS(v & LK_SHARED_WAITERS);
1132 lk->lk_exslpfail = 0;
1133 queue = SQ_SHARED_QUEUE;
1134 v &= ~LK_SHARED_WAITERS;
1136 if (queue == SQ_EXCLUSIVE_QUEUE) {
1138 sleepq_sleepcnt(&lk->lock_object,
1139 SQ_EXCLUSIVE_QUEUE);
1140 if (lk->lk_exslpfail >= realexslp) {
1141 lk->lk_exslpfail = 0;
1142 queue = SQ_SHARED_QUEUE;
1143 v &= ~LK_SHARED_WAITERS;
1144 if (realexslp != 0) {
1146 "%s: %p has only LK_SLEEPFAIL sleepers",
1149 "%s: %p waking up threads on the exclusive queue",
1155 SQ_EXCLUSIVE_QUEUE);
1158 lk->lk_exslpfail = 0;
1160 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1161 sleepq_release(&lk->lock_object);
1165 "%s: %p waking up all threads on the %s queue",
1166 __func__, lk, queue == SQ_SHARED_QUEUE ?
1167 "shared" : "exclusive");
1168 wakeup_swapper |= sleepq_broadcast(
1169 &lk->lock_object, SLEEPQ_LK, 0, queue);
1172 * If shared waiters have been woken up we need
1173 * to wait for one of them to acquire the lock
1174 * before to set the exclusive waiters in
1175 * order to avoid a deadlock.
1177 if (queue == SQ_SHARED_QUEUE) {
1178 for (v = lk->lk_lock;
1179 (v & LK_SHARE) && !LK_SHARERS(v);
1186 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1187 * fail, loop back and retry.
1189 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1190 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1191 x | LK_EXCLUSIVE_WAITERS)) {
1192 sleepq_release(&lk->lock_object);
1195 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1200 * As far as we have been unable to acquire the
1201 * exclusive lock and the exclusive waiters flag
1202 * is set, we will sleep.
1204 if (flags & LK_INTERLOCK) {
1205 class->lc_unlock(ilk);
1206 flags &= ~LK_INTERLOCK;
1209 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1210 SQ_EXCLUSIVE_QUEUE);
1211 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1213 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1218 lock_profile_obtain_lock_success(&lk->lock_object,
1219 contested, waittime, file, line);
1220 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1221 lk->lk_recurse, file, line);
1222 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1223 LK_TRYWIT(flags), file, line);
1224 TD_LOCKS_INC(curthread);
1229 if (flags & LK_INTERLOCK)
1230 class->lc_unlock(ilk);
1231 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1234 if (flags & LK_INTERLOCK)
1235 class->lc_unlock(ilk);
1243 _lockmgr_disown(struct lock *lk, const char *file, int line)
1247 if (SCHEDULER_STOPPED())
1250 tid = (uintptr_t)curthread;
1251 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1254 * If the owner is already LK_KERNPROC just skip the whole operation.
1256 if (LK_HOLDER(lk->lk_lock) != tid)
1258 lock_profile_release_lock(&lk->lock_object);
1259 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1260 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1261 TD_LOCKS_DEC(curthread);
1265 * In order to preserve waiters flags, just spin.
1269 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1270 x &= LK_ALL_WAITERS;
1271 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1279 lockmgr_printinfo(struct lock *lk)
1284 if (lk->lk_lock == LK_UNLOCKED)
1285 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1286 else if (lk->lk_lock & LK_SHARE)
1287 printf("lock type %s: SHARED (count %ju)\n",
1288 lk->lock_object.lo_name,
1289 (uintmax_t)LK_SHARERS(lk->lk_lock));
1291 td = lockmgr_xholder(lk);
1292 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1293 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1297 if (x & LK_EXCLUSIVE_WAITERS)
1298 printf(" with exclusive waiters pending\n");
1299 if (x & LK_SHARED_WAITERS)
1300 printf(" with shared waiters pending\n");
1301 if (x & LK_EXCLUSIVE_SPINNERS)
1302 printf(" with exclusive spinners pending\n");
1308 lockstatus(struct lock *lk)
1317 if ((x & LK_SHARE) == 0) {
1318 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1322 } else if (x == LK_UNLOCKED)
1328 #ifdef INVARIANT_SUPPORT
1330 FEATURE(invariant_support,
1331 "Support for modules compiled with INVARIANTS option");
1334 #undef _lockmgr_assert
1338 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1342 if (panicstr != NULL)
1346 case KA_SLOCKED | KA_NOTRECURSED:
1347 case KA_SLOCKED | KA_RECURSED:
1350 case KA_LOCKED | KA_NOTRECURSED:
1351 case KA_LOCKED | KA_RECURSED:
1355 * We cannot trust WITNESS if the lock is held in exclusive
1356 * mode and a call to lockmgr_disown() happened.
1357 * Workaround this skipping the check if the lock is held in
1358 * exclusive mode even for the KA_LOCKED case.
1360 if (slocked || (lk->lk_lock & LK_SHARE)) {
1361 witness_assert(&lk->lock_object, what, file, line);
1365 if (lk->lk_lock == LK_UNLOCKED ||
1366 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1367 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1368 panic("Lock %s not %slocked @ %s:%d\n",
1369 lk->lock_object.lo_name, slocked ? "share" : "",
1372 if ((lk->lk_lock & LK_SHARE) == 0) {
1373 if (lockmgr_recursed(lk)) {
1374 if (what & KA_NOTRECURSED)
1375 panic("Lock %s recursed @ %s:%d\n",
1376 lk->lock_object.lo_name, file,
1378 } else if (what & KA_RECURSED)
1379 panic("Lock %s not recursed @ %s:%d\n",
1380 lk->lock_object.lo_name, file, line);
1384 case KA_XLOCKED | KA_NOTRECURSED:
1385 case KA_XLOCKED | KA_RECURSED:
1386 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1387 panic("Lock %s not exclusively locked @ %s:%d\n",
1388 lk->lock_object.lo_name, file, line);
1389 if (lockmgr_recursed(lk)) {
1390 if (what & KA_NOTRECURSED)
1391 panic("Lock %s recursed @ %s:%d\n",
1392 lk->lock_object.lo_name, file, line);
1393 } else if (what & KA_RECURSED)
1394 panic("Lock %s not recursed @ %s:%d\n",
1395 lk->lock_object.lo_name, file, line);
1398 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1399 panic("Lock %s exclusively locked @ %s:%d\n",
1400 lk->lock_object.lo_name, file, line);
1403 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1411 lockmgr_chain(struct thread *td, struct thread **ownerp)
1417 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1419 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1420 if (lk->lk_lock & LK_SHARE)
1421 db_printf("SHARED (count %ju)\n",
1422 (uintmax_t)LK_SHARERS(lk->lk_lock));
1424 db_printf("EXCL\n");
1425 *ownerp = lockmgr_xholder(lk);
1431 db_show_lockmgr(struct lock_object *lock)
1436 lk = (struct lock *)lock;
1438 db_printf(" state: ");
1439 if (lk->lk_lock == LK_UNLOCKED)
1440 db_printf("UNLOCKED\n");
1441 else if (lk->lk_lock & LK_SHARE)
1442 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1444 td = lockmgr_xholder(lk);
1445 if (td == (struct thread *)LK_KERNPROC)
1446 db_printf("XLOCK: LK_KERNPROC\n");
1448 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1449 td->td_tid, td->td_proc->p_pid,
1450 td->td_proc->p_comm);
1451 if (lockmgr_recursed(lk))
1452 db_printf(" recursed: %d\n", lk->lk_recurse);
1454 db_printf(" waiters: ");
1455 switch (lk->lk_lock & LK_ALL_WAITERS) {
1456 case LK_SHARED_WAITERS:
1457 db_printf("shared\n");
1459 case LK_EXCLUSIVE_WAITERS:
1460 db_printf("exclusive\n");
1462 case LK_ALL_WAITERS:
1463 db_printf("shared and exclusive\n");
1466 db_printf("none\n");
1468 db_printf(" spinners: ");
1469 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1470 db_printf("exclusive\n");
1472 db_printf("none\n");