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_kdtrace.h"
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
39 #include <sys/lock_profile.h>
40 #include <sys/lockmgr.h>
41 #include <sys/mutex.h>
43 #include <sys/sleepqueue.h>
45 #include <sys/stack.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
50 #include <machine/cpu.h>
56 CTASSERT(((LK_ADAPTIVE | LK_EXSLPFAIL | LK_NOSHARE) & LO_CLASSFLAGS) ==
57 (LK_ADAPTIVE | LK_EXSLPFAIL | LK_NOSHARE));
58 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
59 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
61 #define SQ_EXCLUSIVE_QUEUE 0
62 #define SQ_SHARED_QUEUE 1
64 #ifdef ADAPTIVE_LOCKMGRS
65 #define ALK_RETRIES 10
66 #define ALK_LOOPS 10000
70 #define _lockmgr_assert(lk, what, file, line)
71 #define TD_LOCKS_INC(td)
72 #define TD_LOCKS_DEC(td)
74 #define TD_LOCKS_INC(td) ((td)->td_locks++)
75 #define TD_LOCKS_DEC(td) ((td)->td_locks--)
77 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++)
78 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--)
81 #define STACK_PRINT(lk)
82 #define STACK_SAVE(lk)
83 #define STACK_ZERO(lk)
85 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
86 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack)
87 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack)
90 #define LOCK_LOG2(lk, string, arg1, arg2) \
91 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
92 CTR2(KTR_LOCK, (string), (arg1), (arg2))
93 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \
94 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \
95 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
97 #define GIANT_DECLARE \
99 WITNESS_SAVE_DECL(Giant)
100 #define GIANT_RESTORE() do { \
104 WITNESS_RESTORE(&Giant.lock_object, Giant); \
107 #define GIANT_SAVE() do { \
108 if (mtx_owned(&Giant)) { \
109 WITNESS_SAVE(&Giant.lock_object, Giant); \
110 while (mtx_owned(&Giant)) { \
112 mtx_unlock(&Giant); \
117 #define LK_CAN_SHARE(x) \
118 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \
119 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \
120 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
121 #define LK_TRYOP(x) \
124 #define LK_CAN_WITNESS(x) \
125 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
126 #define LK_TRYWIT(x) \
127 (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
129 #define LK_CAN_ADAPT(lk, f) \
130 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \
131 ((f) & LK_SLEEPFAIL) == 0)
133 #define lockmgr_disowned(lk) \
134 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
136 #define lockmgr_xlocked(lk) \
137 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
139 static void assert_lockmgr(struct lock_object *lock, int how);
141 static void db_show_lockmgr(struct lock_object *lock);
143 static void lock_lockmgr(struct lock_object *lock, int how);
145 static int owner_lockmgr(struct lock_object *lock, struct thread **owner);
147 static int unlock_lockmgr(struct lock_object *lock);
149 struct lock_class lock_class_lockmgr = {
150 .lc_name = "lockmgr",
151 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
152 .lc_assert = assert_lockmgr,
154 .lc_ddb_show = db_show_lockmgr,
156 .lc_lock = lock_lockmgr,
157 .lc_unlock = unlock_lockmgr,
159 .lc_owner = owner_lockmgr,
163 static __inline struct thread *
164 lockmgr_xholder(struct lock *lk)
169 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
173 * It assumes sleepq_lock held and returns with this one unheld.
174 * It also assumes the generic interlock is sane and previously checked.
175 * If LK_INTERLOCK is specified the interlock is not reacquired after the
179 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
180 const char *wmesg, int pri, int timo, int queue)
183 struct lock_class *class;
186 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
187 catch = pri & PCATCH;
191 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
192 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
194 if (flags & LK_INTERLOCK)
195 class->lc_unlock(ilk);
198 * LK_EXSLPFAIL is not invariant during the lock pattern but it is
199 * always protected by the sleepqueue spinlock, thus it is safe to
200 * handle within the lo_flags.
202 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
203 lk->lock_object.lo_flags |= LK_EXSLPFAIL;
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 the LK_EXSLPFAIL flag may be lying about
283 * the real presence of waiters with the LK_SLEEPFAIL flag on
284 * because they may be used in conjuction with interruptible
287 realexslp = sleepq_sleepcnt(&lk->lock_object,
289 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
290 if ((lk->lock_object.lo_flags & LK_EXSLPFAIL) == 0) {
291 lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
292 queue = SQ_EXCLUSIVE_QUEUE;
293 v |= (x & LK_SHARED_WAITERS);
295 lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
297 "%s: %p has only LK_SLEEPFAIL sleepers",
300 "%s: %p waking up threads on the exclusive queue",
303 sleepq_broadcast(&lk->lock_object,
304 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
305 queue = SQ_SHARED_QUEUE;
311 * Exclusive waiters sleeping with LK_SLEEPFAIL on
312 * and using interruptible sleeps/timeout may have
313 * left spourious LK_EXSLPFAIL flag on, so clean
316 lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
317 queue = SQ_SHARED_QUEUE;
320 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
322 sleepq_release(&lk->lock_object);
325 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
326 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
328 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
330 sleepq_release(&lk->lock_object);
334 lock_profile_release_lock(&lk->lock_object);
335 return (wakeup_swapper);
339 assert_lockmgr(struct lock_object *lock, int what)
342 panic("lockmgr locks do not support assertions");
346 lock_lockmgr(struct lock_object *lock, int how)
349 panic("lockmgr locks do not support sleep interlocking");
353 unlock_lockmgr(struct lock_object *lock)
356 panic("lockmgr locks do not support sleep interlocking");
361 owner_lockmgr(struct lock_object *lock, struct thread **owner)
364 panic("lockmgr locks do not support owner inquiring");
369 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
373 MPASS((flags & ~LK_INIT_MASK) == 0);
374 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
375 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
378 iflags = LO_SLEEPABLE | LO_UPGRADABLE;
379 if (flags & LK_CANRECURSE)
380 iflags |= LO_RECURSABLE;
381 if ((flags & LK_NODUP) == 0)
383 if (flags & LK_NOPROFILE)
384 iflags |= LO_NOPROFILE;
385 if ((flags & LK_NOWITNESS) == 0)
386 iflags |= LO_WITNESS;
387 if (flags & LK_QUIET)
389 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
391 lk->lk_lock = LK_UNLOCKED;
395 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
400 lockdestroy(struct lock *lk)
403 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
404 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
405 KASSERT((lk->lock_object.lo_flags & LK_EXSLPFAIL) == 0,
406 ("lockmgr still exclusive waiters"));
407 lock_destroy(&lk->lock_object);
411 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
412 const char *wmesg, int pri, int timo, const char *file, int line)
415 struct lock_class *class;
419 int error, ipri, itimo, queue, wakeup_swapper;
420 #ifdef LOCK_PROFILING
421 uint64_t waittime = 0;
424 #ifdef ADAPTIVE_LOCKMGRS
425 volatile struct thread *owner;
426 u_int i, spintries = 0;
430 tid = (uintptr_t)curthread;
431 op = (flags & LK_TYPE_MASK);
432 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
433 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
434 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
436 MPASS((flags & ~LK_TOTAL_MASK) == 0);
437 KASSERT((op & (op - 1)) == 0,
438 ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
439 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
440 (op != LK_DOWNGRADE && op != LK_RELEASE),
441 ("%s: Invalid flags in regard of the operation desired @ %s:%d",
442 __func__, file, line));
443 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
444 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
445 __func__, file, line));
447 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
448 if (panicstr != NULL) {
449 if (flags & LK_INTERLOCK)
450 class->lc_unlock(ilk);
454 if (op == LK_SHARED && (lk->lock_object.lo_flags & LK_NOSHARE))
460 if (LK_CAN_WITNESS(flags))
461 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
467 * If no other thread has an exclusive lock, or
468 * no exclusive waiter is present, bump the count of
469 * sharers. Since we have to preserve the state of
470 * waiters, if we fail to acquire the shared lock
471 * loop back and retry.
473 if (LK_CAN_SHARE(x)) {
474 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
479 lock_profile_obtain_lock_failed(&lk->lock_object,
480 &contested, &waittime);
483 * If the lock is already held by curthread in
484 * exclusive way avoid a deadlock.
486 if (LK_HOLDER(x) == tid) {
488 "%s: %p already held in exclusive mode",
495 * If the lock is expected to not sleep just give up
498 if (LK_TRYOP(flags)) {
499 LOCK_LOG2(lk, "%s: %p fails the try operation",
505 #ifdef ADAPTIVE_LOCKMGRS
507 * If the owner is running on another CPU, spin until
508 * the owner stops running or the state of the lock
509 * changes. We need a double-state handle here
510 * because for a failed acquisition the lock can be
511 * either held in exclusive mode or shared mode
512 * (for the writer starvation avoidance technique).
514 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
515 LK_HOLDER(x) != LK_KERNPROC) {
516 owner = (struct thread *)LK_HOLDER(x);
517 if (LOCK_LOG_TEST(&lk->lock_object, 0))
519 "%s: spinning on %p held by %p",
520 __func__, lk, owner);
523 * If we are holding also an interlock drop it
524 * in order to avoid a deadlock if the lockmgr
525 * owner is adaptively spinning on the
528 if (flags & LK_INTERLOCK) {
529 class->lc_unlock(ilk);
530 flags &= ~LK_INTERLOCK;
533 while (LK_HOLDER(lk->lk_lock) ==
534 (uintptr_t)owner && TD_IS_RUNNING(owner))
538 } else if (LK_CAN_ADAPT(lk, flags) &&
539 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
540 spintries < ALK_RETRIES) {
541 if (flags & LK_INTERLOCK) {
542 class->lc_unlock(ilk);
543 flags &= ~LK_INTERLOCK;
547 for (i = 0; i < ALK_LOOPS; i++) {
548 if (LOCK_LOG_TEST(&lk->lock_object, 0))
550 "%s: shared spinning on %p with %u and %u",
551 __func__, lk, spintries, i);
553 if ((x & LK_SHARE) == 0 ||
554 LK_CAN_SHARE(x) != 0)
565 * Acquire the sleepqueue chain lock because we
566 * probabilly will need to manipulate waiters flags.
568 sleepq_lock(&lk->lock_object);
572 * if the lock can be acquired in shared mode, try
575 if (LK_CAN_SHARE(x)) {
576 sleepq_release(&lk->lock_object);
580 #ifdef ADAPTIVE_LOCKMGRS
582 * The current lock owner might have started executing
583 * on another CPU (or the lock could have changed
584 * owner) while we were waiting on the turnstile
585 * chain lock. If so, drop the turnstile lock and try
588 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
589 LK_HOLDER(x) != LK_KERNPROC) {
590 owner = (struct thread *)LK_HOLDER(x);
591 if (TD_IS_RUNNING(owner)) {
592 sleepq_release(&lk->lock_object);
599 * Try to set the LK_SHARED_WAITERS flag. If we fail,
600 * loop back and retry.
602 if ((x & LK_SHARED_WAITERS) == 0) {
603 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
604 x | LK_SHARED_WAITERS)) {
605 sleepq_release(&lk->lock_object);
608 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
613 * As far as we have been unable to acquire the
614 * shared lock and the shared waiters flag is set,
617 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
619 flags &= ~LK_INTERLOCK;
622 "%s: interrupted sleep for %p with %d",
623 __func__, lk, error);
626 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
630 lock_profile_obtain_lock_success(&lk->lock_object,
631 contested, waittime, file, line);
632 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
634 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
636 TD_LOCKS_INC(curthread);
637 TD_SLOCKS_INC(curthread);
642 _lockmgr_assert(lk, KA_SLOCKED, file, line);
644 x = v & LK_ALL_WAITERS;
645 v &= LK_EXCLUSIVE_SPINNERS;
648 * Try to switch from one shared lock to an exclusive one.
649 * We need to preserve waiters flags during the operation.
651 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
653 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
655 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
656 LK_TRYWIT(flags), file, line);
657 TD_SLOCKS_DEC(curthread);
662 * We have been unable to succeed in upgrading, so just
663 * give up the shared lock.
665 wakeup_swapper |= wakeupshlk(lk, file, line);
669 if (LK_CAN_WITNESS(flags))
670 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
671 LOP_EXCLUSIVE, file, line, ilk);
674 * If curthread already holds the lock and this one is
675 * allowed to recurse, simply recurse on it.
677 if (lockmgr_xlocked(lk)) {
678 if ((flags & LK_CANRECURSE) == 0 &&
679 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
682 * If the lock is expected to not panic just
683 * give up and return.
685 if (LK_TRYOP(flags)) {
687 "%s: %p fails the try operation",
692 if (flags & LK_INTERLOCK)
693 class->lc_unlock(ilk);
694 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
695 __func__, iwmesg, file, line);
698 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
699 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
700 lk->lk_recurse, file, line);
701 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
702 LK_TRYWIT(flags), file, line);
703 TD_LOCKS_INC(curthread);
707 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
709 lock_profile_obtain_lock_failed(&lk->lock_object,
710 &contested, &waittime);
713 * If the lock is expected to not sleep just give up
716 if (LK_TRYOP(flags)) {
717 LOCK_LOG2(lk, "%s: %p fails the try operation",
723 #ifdef ADAPTIVE_LOCKMGRS
725 * If the owner is running on another CPU, spin until
726 * the owner stops running or the state of the lock
730 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
731 LK_HOLDER(x) != LK_KERNPROC) {
732 owner = (struct thread *)LK_HOLDER(x);
733 if (LOCK_LOG_TEST(&lk->lock_object, 0))
735 "%s: spinning on %p held by %p",
736 __func__, lk, owner);
739 * If we are holding also an interlock drop it
740 * in order to avoid a deadlock if the lockmgr
741 * owner is adaptively spinning on the
744 if (flags & LK_INTERLOCK) {
745 class->lc_unlock(ilk);
746 flags &= ~LK_INTERLOCK;
749 while (LK_HOLDER(lk->lk_lock) ==
750 (uintptr_t)owner && TD_IS_RUNNING(owner))
754 } else if (LK_CAN_ADAPT(lk, flags) &&
755 (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
756 spintries < ALK_RETRIES) {
757 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
758 !atomic_cmpset_ptr(&lk->lk_lock, x,
759 x | LK_EXCLUSIVE_SPINNERS))
761 if (flags & LK_INTERLOCK) {
762 class->lc_unlock(ilk);
763 flags &= ~LK_INTERLOCK;
767 for (i = 0; i < ALK_LOOPS; i++) {
768 if (LOCK_LOG_TEST(&lk->lock_object, 0))
770 "%s: shared spinning on %p with %u and %u",
771 __func__, lk, spintries, i);
773 LK_EXCLUSIVE_SPINNERS) == 0)
784 * Acquire the sleepqueue chain lock because we
785 * probabilly will need to manipulate waiters flags.
787 sleepq_lock(&lk->lock_object);
791 * if the lock has been released while we spun on
792 * the sleepqueue chain lock just try again.
794 if (x == LK_UNLOCKED) {
795 sleepq_release(&lk->lock_object);
799 #ifdef ADAPTIVE_LOCKMGRS
801 * The current lock owner might have started executing
802 * on another CPU (or the lock could have changed
803 * owner) while we were waiting on the turnstile
804 * chain lock. If so, drop the turnstile lock and try
807 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
808 LK_HOLDER(x) != LK_KERNPROC) {
809 owner = (struct thread *)LK_HOLDER(x);
810 if (TD_IS_RUNNING(owner)) {
811 sleepq_release(&lk->lock_object);
818 * The lock can be in the state where there is a
819 * pending queue of waiters, but still no owner.
820 * This happens when the lock is contested and an
821 * owner is going to claim the lock.
822 * If curthread is the one successfully acquiring it
823 * claim lock ownership and return, preserving waiters
826 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
827 if ((x & ~v) == LK_UNLOCKED) {
828 v &= ~LK_EXCLUSIVE_SPINNERS;
829 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
831 sleepq_release(&lk->lock_object);
833 "%s: %p claimed by a new writer",
837 sleepq_release(&lk->lock_object);
842 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
843 * fail, loop back and retry.
845 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
846 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
847 x | LK_EXCLUSIVE_WAITERS)) {
848 sleepq_release(&lk->lock_object);
851 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
856 * As far as we have been unable to acquire the
857 * exclusive lock and the exclusive waiters flag
858 * is set, we will sleep.
860 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
862 flags &= ~LK_INTERLOCK;
865 "%s: interrupted sleep for %p with %d",
866 __func__, lk, error);
869 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
873 lock_profile_obtain_lock_success(&lk->lock_object,
874 contested, waittime, file, line);
875 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
876 lk->lk_recurse, file, line);
877 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
878 LK_TRYWIT(flags), file, line);
879 TD_LOCKS_INC(curthread);
884 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
885 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
886 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
887 TD_SLOCKS_INC(curthread);
890 * In order to preserve waiters flags, just spin.
894 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
896 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
897 LK_SHARERS_LOCK(1) | x))
903 _lockmgr_assert(lk, KA_LOCKED, file, line);
906 if ((x & LK_SHARE) == 0) {
909 * As first option, treact the lock as if it has not
911 * Fix-up the tid var if the lock has been disowned.
913 if (LK_HOLDER(x) == LK_KERNPROC)
916 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
918 TD_LOCKS_DEC(curthread);
920 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
921 lk->lk_recurse, file, line);
924 * The lock is held in exclusive mode.
925 * If the lock is recursed also, then unrecurse it.
927 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
928 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
933 if (tid != LK_KERNPROC)
934 lock_profile_release_lock(&lk->lock_object);
936 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
940 sleepq_lock(&lk->lock_object);
945 * If the lock has exclusive waiters, give them
946 * preference in order to avoid deadlock with
948 * If interruptible sleeps left the exclusive queue
949 * empty avoid a starvation for the threads sleeping
950 * on the shared queue by giving them precedence
951 * and cleaning up the exclusive waiters bit anyway.
952 * Please note that the LK_EXSLPFAIL flag may be lying
953 * about the real presence of waiters with the
954 * LK_SLEEPFAIL flag on because they may be used in
955 * conjuction with interruptible sleeps.
957 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
958 realexslp = sleepq_sleepcnt(&lk->lock_object,
960 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
961 if ((lk->lock_object.lo_flags &
962 LK_EXSLPFAIL) == 0) {
963 lk->lock_object.lo_flags &=
965 queue = SQ_EXCLUSIVE_QUEUE;
966 v |= (x & LK_SHARED_WAITERS);
968 lk->lock_object.lo_flags &=
971 "%s: %p has only LK_SLEEPFAIL sleepers",
974 "%s: %p waking up threads on the exclusive queue",
977 sleepq_broadcast(&lk->lock_object,
978 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
979 queue = SQ_SHARED_QUEUE;
984 * Exclusive waiters sleeping with LK_SLEEPFAIL
985 * on and using interruptible sleeps/timeout
986 * may have left spourious LK_EXSLPFAIL flag
987 * on, so clean it up anyway.
989 lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
990 queue = SQ_SHARED_QUEUE;
994 "%s: %p waking up threads on the %s queue",
995 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
997 atomic_store_rel_ptr(&lk->lk_lock, v);
998 wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
999 SLEEPQ_LK, 0, queue);
1000 sleepq_release(&lk->lock_object);
1003 wakeup_swapper = wakeupshlk(lk, file, line);
1006 if (LK_CAN_WITNESS(flags))
1007 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1008 LOP_EXCLUSIVE, file, line, ilk);
1011 * Trying to drain a lock we already own will result in a
1014 if (lockmgr_xlocked(lk)) {
1015 if (flags & LK_INTERLOCK)
1016 class->lc_unlock(ilk);
1017 panic("%s: draining %s with the lock held @ %s:%d\n",
1018 __func__, iwmesg, file, line);
1021 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1022 lock_profile_obtain_lock_failed(&lk->lock_object,
1023 &contested, &waittime);
1026 * If the lock is expected to not sleep just give up
1029 if (LK_TRYOP(flags)) {
1030 LOCK_LOG2(lk, "%s: %p fails the try operation",
1037 * Acquire the sleepqueue chain lock because we
1038 * probabilly will need to manipulate waiters flags.
1040 sleepq_lock(&lk->lock_object);
1044 * if the lock has been released while we spun on
1045 * the sleepqueue chain lock just try again.
1047 if (x == LK_UNLOCKED) {
1048 sleepq_release(&lk->lock_object);
1052 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1053 if ((x & ~v) == LK_UNLOCKED) {
1054 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1057 * If interruptible sleeps left the exclusive
1058 * queue empty avoid a starvation for the
1059 * threads sleeping on the shared queue by
1060 * giving them precedence and cleaning up the
1061 * exclusive waiters bit anyway.
1062 * Please note that the LK_EXSLPFAIL flag may
1063 * be lying about the real presence of waiters
1064 * with the LK_SLEEPFAIL flag on because they
1065 * may be used in conjuction with interruptible
1068 if (v & LK_EXCLUSIVE_WAITERS) {
1069 queue = SQ_EXCLUSIVE_QUEUE;
1070 v &= ~LK_EXCLUSIVE_WAITERS;
1074 * Exclusive waiters sleeping with
1075 * LK_SLEEPFAIL on and using
1076 * interruptible sleeps/timeout may
1077 * have left spourious LK_EXSLPFAIL
1078 * flag on, so clean it up anyway.
1080 MPASS(v & LK_SHARED_WAITERS);
1081 lk->lock_object.lo_flags &=
1083 queue = SQ_SHARED_QUEUE;
1084 v &= ~LK_SHARED_WAITERS;
1086 if (queue == SQ_EXCLUSIVE_QUEUE) {
1088 sleepq_sleepcnt(&lk->lock_object,
1089 SQ_EXCLUSIVE_QUEUE);
1090 if ((lk->lock_object.lo_flags &
1091 LK_EXSLPFAIL) == 0) {
1092 lk->lock_object.lo_flags &=
1094 queue = SQ_SHARED_QUEUE;
1095 v &= ~LK_SHARED_WAITERS;
1096 if (realexslp != 0) {
1098 "%s: %p has only LK_SLEEPFAIL sleepers",
1101 "%s: %p waking up threads on the exclusive queue",
1107 SQ_EXCLUSIVE_QUEUE);
1110 lk->lock_object.lo_flags &=
1113 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1114 sleepq_release(&lk->lock_object);
1118 "%s: %p waking up all threads on the %s queue",
1119 __func__, lk, queue == SQ_SHARED_QUEUE ?
1120 "shared" : "exclusive");
1121 wakeup_swapper |= sleepq_broadcast(
1122 &lk->lock_object, SLEEPQ_LK, 0, queue);
1125 * If shared waiters have been woken up we need
1126 * to wait for one of them to acquire the lock
1127 * before to set the exclusive waiters in
1128 * order to avoid a deadlock.
1130 if (queue == SQ_SHARED_QUEUE) {
1131 for (v = lk->lk_lock;
1132 (v & LK_SHARE) && !LK_SHARERS(v);
1139 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we
1140 * fail, loop back and retry.
1142 if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1143 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1144 x | LK_EXCLUSIVE_WAITERS)) {
1145 sleepq_release(&lk->lock_object);
1148 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1153 * As far as we have been unable to acquire the
1154 * exclusive lock and the exclusive waiters flag
1155 * is set, we will sleep.
1157 if (flags & LK_INTERLOCK) {
1158 class->lc_unlock(ilk);
1159 flags &= ~LK_INTERLOCK;
1162 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1163 SQ_EXCLUSIVE_QUEUE);
1164 sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1166 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1171 lock_profile_obtain_lock_success(&lk->lock_object,
1172 contested, waittime, file, line);
1173 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1174 lk->lk_recurse, file, line);
1175 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1176 LK_TRYWIT(flags), file, line);
1177 TD_LOCKS_INC(curthread);
1182 if (flags & LK_INTERLOCK)
1183 class->lc_unlock(ilk);
1184 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1187 if (flags & LK_INTERLOCK)
1188 class->lc_unlock(ilk);
1196 _lockmgr_disown(struct lock *lk, const char *file, int line)
1200 tid = (uintptr_t)curthread;
1201 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1204 * If the owner is already LK_KERNPROC just skip the whole operation.
1206 if (LK_HOLDER(lk->lk_lock) != tid)
1208 lock_profile_release_lock(&lk->lock_object);
1209 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1210 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1211 TD_LOCKS_DEC(curthread);
1215 * In order to preserve waiters flags, just spin.
1219 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1220 x &= LK_ALL_WAITERS;
1221 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1229 lockmgr_printinfo(struct lock *lk)
1234 if (lk->lk_lock == LK_UNLOCKED)
1235 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1236 else if (lk->lk_lock & LK_SHARE)
1237 printf("lock type %s: SHARED (count %ju)\n",
1238 lk->lock_object.lo_name,
1239 (uintmax_t)LK_SHARERS(lk->lk_lock));
1241 td = lockmgr_xholder(lk);
1242 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1243 lk->lock_object.lo_name, td, td->td_proc->p_pid);
1247 if (x & LK_EXCLUSIVE_WAITERS)
1248 printf(" with exclusive waiters pending\n");
1249 if (x & LK_SHARED_WAITERS)
1250 printf(" with shared waiters pending\n");
1251 if (x & LK_EXCLUSIVE_SPINNERS)
1252 printf(" with exclusive spinners pending\n");
1258 lockstatus(struct lock *lk)
1267 if ((x & LK_SHARE) == 0) {
1268 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1272 } else if (x == LK_UNLOCKED)
1278 #ifdef INVARIANT_SUPPORT
1280 #undef _lockmgr_assert
1284 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1288 if (panicstr != NULL)
1292 case KA_SLOCKED | KA_NOTRECURSED:
1293 case KA_SLOCKED | KA_RECURSED:
1296 case KA_LOCKED | KA_NOTRECURSED:
1297 case KA_LOCKED | KA_RECURSED:
1301 * We cannot trust WITNESS if the lock is held in exclusive
1302 * mode and a call to lockmgr_disown() happened.
1303 * Workaround this skipping the check if the lock is held in
1304 * exclusive mode even for the KA_LOCKED case.
1306 if (slocked || (lk->lk_lock & LK_SHARE)) {
1307 witness_assert(&lk->lock_object, what, file, line);
1311 if (lk->lk_lock == LK_UNLOCKED ||
1312 ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1313 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1314 panic("Lock %s not %slocked @ %s:%d\n",
1315 lk->lock_object.lo_name, slocked ? "share" : "",
1318 if ((lk->lk_lock & LK_SHARE) == 0) {
1319 if (lockmgr_recursed(lk)) {
1320 if (what & KA_NOTRECURSED)
1321 panic("Lock %s recursed @ %s:%d\n",
1322 lk->lock_object.lo_name, file,
1324 } else if (what & KA_RECURSED)
1325 panic("Lock %s not recursed @ %s:%d\n",
1326 lk->lock_object.lo_name, file, line);
1330 case KA_XLOCKED | KA_NOTRECURSED:
1331 case KA_XLOCKED | KA_RECURSED:
1332 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1333 panic("Lock %s not exclusively locked @ %s:%d\n",
1334 lk->lock_object.lo_name, file, line);
1335 if (lockmgr_recursed(lk)) {
1336 if (what & KA_NOTRECURSED)
1337 panic("Lock %s recursed @ %s:%d\n",
1338 lk->lock_object.lo_name, file, line);
1339 } else if (what & KA_RECURSED)
1340 panic("Lock %s not recursed @ %s:%d\n",
1341 lk->lock_object.lo_name, file, line);
1344 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1345 panic("Lock %s exclusively locked @ %s:%d\n",
1346 lk->lock_object.lo_name, file, line);
1349 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1357 lockmgr_chain(struct thread *td, struct thread **ownerp)
1363 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1365 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1366 if (lk->lk_lock & LK_SHARE)
1367 db_printf("SHARED (count %ju)\n",
1368 (uintmax_t)LK_SHARERS(lk->lk_lock));
1370 db_printf("EXCL\n");
1371 *ownerp = lockmgr_xholder(lk);
1377 db_show_lockmgr(struct lock_object *lock)
1382 lk = (struct lock *)lock;
1384 db_printf(" state: ");
1385 if (lk->lk_lock == LK_UNLOCKED)
1386 db_printf("UNLOCKED\n");
1387 else if (lk->lk_lock & LK_SHARE)
1388 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1390 td = lockmgr_xholder(lk);
1391 if (td == (struct thread *)LK_KERNPROC)
1392 db_printf("XLOCK: LK_KERNPROC\n");
1394 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1395 td->td_tid, td->td_proc->p_pid,
1396 td->td_proc->p_comm);
1397 if (lockmgr_recursed(lk))
1398 db_printf(" recursed: %d\n", lk->lk_recurse);
1400 db_printf(" waiters: ");
1401 switch (lk->lk_lock & LK_ALL_WAITERS) {
1402 case LK_SHARED_WAITERS:
1403 db_printf("shared\n");
1405 case LK_EXCLUSIVE_WAITERS:
1406 db_printf("exclusive\n");
1408 case LK_ALL_WAITERS:
1409 db_printf("shared and exclusive\n");
1412 db_printf("none\n");
1414 db_printf(" spinners: ");
1415 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1416 db_printf("exclusive\n");
1418 db_printf("none\n");