]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_lock.c
Merge ACPICA 20110112. Switch to BSD/GPLv2 dual license[1].
[FreeBSD/FreeBSD.git] / sys / kern / kern_lock.c
1 /*-
2  * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
15  *
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
26  * DAMAGE.
27  */
28
29 #include "opt_adaptive_lockmgrs.h"
30 #include "opt_ddb.h"
31 #include "opt_kdtrace.h"
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/ktr.h>
38 #include <sys/lock.h>
39 #include <sys/lock_profile.h>
40 #include <sys/lockmgr.h>
41 #include <sys/mutex.h>
42 #include <sys/proc.h>
43 #include <sys/sleepqueue.h>
44 #ifdef DEBUG_LOCKS
45 #include <sys/stack.h>
46 #endif
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49
50 #include <machine/cpu.h>
51
52 #ifdef DDB
53 #include <ddb/ddb.h>
54 #endif
55
56 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) ==
57     (LK_ADAPTIVE | LK_NOSHARE));
58 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED &
59     ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS)));
60
61 #define SQ_EXCLUSIVE_QUEUE      0
62 #define SQ_SHARED_QUEUE         1
63
64 #ifndef INVARIANTS
65 #define _lockmgr_assert(lk, what, file, line)
66 #define TD_LOCKS_INC(td)
67 #define TD_LOCKS_DEC(td)
68 #else
69 #define TD_LOCKS_INC(td)        ((td)->td_locks++)
70 #define TD_LOCKS_DEC(td)        ((td)->td_locks--)
71 #endif
72 #define TD_SLOCKS_INC(td)       ((td)->td_lk_slocks++)
73 #define TD_SLOCKS_DEC(td)       ((td)->td_lk_slocks--)
74
75 #ifndef DEBUG_LOCKS
76 #define STACK_PRINT(lk)
77 #define STACK_SAVE(lk)
78 #define STACK_ZERO(lk)
79 #else
80 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack)
81 #define STACK_SAVE(lk)  stack_save(&(lk)->lk_stack)
82 #define STACK_ZERO(lk)  stack_zero(&(lk)->lk_stack)
83 #endif
84
85 #define LOCK_LOG2(lk, string, arg1, arg2)                               \
86         if (LOCK_LOG_TEST(&(lk)->lock_object, 0))                       \
87                 CTR2(KTR_LOCK, (string), (arg1), (arg2))
88 #define LOCK_LOG3(lk, string, arg1, arg2, arg3)                         \
89         if (LOCK_LOG_TEST(&(lk)->lock_object, 0))                       \
90                 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3))
91
92 #define GIANT_DECLARE                                                   \
93         int _i = 0;                                                     \
94         WITNESS_SAVE_DECL(Giant)
95 #define GIANT_RESTORE() do {                                            \
96         if (_i > 0) {                                                   \
97                 while (_i--)                                            \
98                         mtx_lock(&Giant);                               \
99                 WITNESS_RESTORE(&Giant.lock_object, Giant);             \
100         }                                                               \
101 } while (0)
102 #define GIANT_SAVE() do {                                               \
103         if (mtx_owned(&Giant)) {                                        \
104                 WITNESS_SAVE(&Giant.lock_object, Giant);                \
105                 while (mtx_owned(&Giant)) {                             \
106                         _i++;                                           \
107                         mtx_unlock(&Giant);                             \
108                 }                                                       \
109         }                                                               \
110 } while (0)
111
112 #define LK_CAN_SHARE(x)                                                 \
113         (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 ||      \
114         ((x) & LK_EXCLUSIVE_SPINNERS) == 0 ||                           \
115         curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT)))
116 #define LK_TRYOP(x)                                                     \
117         ((x) & LK_NOWAIT)
118
119 #define LK_CAN_WITNESS(x)                                               \
120         (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x))
121 #define LK_TRYWIT(x)                                                    \
122         (LK_TRYOP(x) ? LOP_TRYLOCK : 0)
123
124 #define LK_CAN_ADAPT(lk, f)                                             \
125         (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 &&             \
126         ((f) & LK_SLEEPFAIL) == 0)
127
128 #define lockmgr_disowned(lk)                                            \
129         (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
130
131 #define lockmgr_xlocked(lk)                                             \
132         (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
133
134 static void      assert_lockmgr(struct lock_object *lock, int how);
135 #ifdef DDB
136 static void      db_show_lockmgr(struct lock_object *lock);
137 #endif
138 static void      lock_lockmgr(struct lock_object *lock, int how);
139 #ifdef KDTRACE_HOOKS
140 static int       owner_lockmgr(struct lock_object *lock, struct thread **owner);
141 #endif
142 static int       unlock_lockmgr(struct lock_object *lock);
143
144 struct lock_class lock_class_lockmgr = {
145         .lc_name = "lockmgr",
146         .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE,
147         .lc_assert = assert_lockmgr,
148 #ifdef DDB
149         .lc_ddb_show = db_show_lockmgr,
150 #endif
151         .lc_lock = lock_lockmgr,
152         .lc_unlock = unlock_lockmgr,
153 #ifdef KDTRACE_HOOKS
154         .lc_owner = owner_lockmgr,
155 #endif
156 };
157
158 #ifdef ADAPTIVE_LOCKMGRS
159 static u_int alk_retries = 10;
160 static u_int alk_loops = 10000;
161 SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL, "lockmgr debugging");
162 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, "");
163 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, "");
164 #endif
165
166 static __inline struct thread *
167 lockmgr_xholder(struct lock *lk)
168 {
169         uintptr_t x;
170
171         x = lk->lk_lock;
172         return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
173 }
174
175 /*
176  * It assumes sleepq_lock held and returns with this one unheld.
177  * It also assumes the generic interlock is sane and previously checked.
178  * If LK_INTERLOCK is specified the interlock is not reacquired after the
179  * sleep.
180  */
181 static __inline int
182 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
183     const char *wmesg, int pri, int timo, int queue)
184 {
185         GIANT_DECLARE;
186         struct lock_class *class;
187         int catch, error;
188
189         class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
190         catch = pri & PCATCH;
191         pri &= PRIMASK;
192         error = 0;
193
194         LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
195             (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
196
197         if (flags & LK_INTERLOCK)
198                 class->lc_unlock(ilk);
199         if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
200                 lk->lk_exslpfail++;
201         GIANT_SAVE();
202         sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ?
203             SLEEPQ_INTERRUPTIBLE : 0), queue);
204         if ((flags & LK_TIMELOCK) && timo)
205                 sleepq_set_timeout(&lk->lock_object, timo);
206
207         /*
208          * Decisional switch for real sleeping.
209          */
210         if ((flags & LK_TIMELOCK) && timo && catch)
211                 error = sleepq_timedwait_sig(&lk->lock_object, pri);
212         else if ((flags & LK_TIMELOCK) && timo)
213                 error = sleepq_timedwait(&lk->lock_object, pri);
214         else if (catch)
215                 error = sleepq_wait_sig(&lk->lock_object, pri);
216         else
217                 sleepq_wait(&lk->lock_object, pri);
218         GIANT_RESTORE();
219         if ((flags & LK_SLEEPFAIL) && error == 0)
220                 error = ENOLCK;
221
222         return (error);
223 }
224
225 static __inline int
226 wakeupshlk(struct lock *lk, const char *file, int line)
227 {
228         uintptr_t v, x;
229         u_int realexslp;
230         int queue, wakeup_swapper;
231
232         TD_LOCKS_DEC(curthread);
233         TD_SLOCKS_DEC(curthread);
234         WITNESS_UNLOCK(&lk->lock_object, 0, file, line);
235         LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line);
236
237         wakeup_swapper = 0;
238         for (;;) {
239                 x = lk->lk_lock;
240
241                 /*
242                  * If there is more than one shared lock held, just drop one
243                  * and return.
244                  */
245                 if (LK_SHARERS(x) > 1) {
246                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
247                             x - LK_ONE_SHARER))
248                                 break;
249                         continue;
250                 }
251
252                 /*
253                  * If there are not waiters on the exclusive queue, drop the
254                  * lock quickly.
255                  */
256                 if ((x & LK_ALL_WAITERS) == 0) {
257                         MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
258                             LK_SHARERS_LOCK(1));
259                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
260                                 break;
261                         continue;
262                 }
263
264                 /*
265                  * We should have a sharer with waiters, so enter the hard
266                  * path in order to handle wakeups correctly.
267                  */
268                 sleepq_lock(&lk->lock_object);
269                 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
270                 v = LK_UNLOCKED;
271
272                 /*
273                  * If the lock has exclusive waiters, give them preference in
274                  * order to avoid deadlock with shared runners up.
275                  * If interruptible sleeps left the exclusive queue empty
276                  * avoid a starvation for the threads sleeping on the shared
277                  * queue by giving them precedence and cleaning up the
278                  * exclusive waiters bit anyway.
279                  * Please note that lk_exslpfail count may be lying about
280                  * the real number of waiters with the LK_SLEEPFAIL flag on
281                  * because they may be used in conjuction with interruptible
282                  * sleeps so lk_exslpfail might be considered an 'upper limit'
283                  * bound, including the edge cases.
284                  */
285                 realexslp = sleepq_sleepcnt(&lk->lock_object,
286                     SQ_EXCLUSIVE_QUEUE);
287                 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
288                         if (lk->lk_exslpfail < realexslp) {
289                                 lk->lk_exslpfail = 0;
290                                 queue = SQ_EXCLUSIVE_QUEUE;
291                                 v |= (x & LK_SHARED_WAITERS);
292                         } else {
293                                 lk->lk_exslpfail = 0;
294                                 LOCK_LOG2(lk,
295                                     "%s: %p has only LK_SLEEPFAIL sleepers",
296                                     __func__, lk);
297                                 LOCK_LOG2(lk,
298                             "%s: %p waking up threads on the exclusive queue",
299                                     __func__, lk);
300                                 wakeup_swapper =
301                                     sleepq_broadcast(&lk->lock_object,
302                                     SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
303                                 queue = SQ_SHARED_QUEUE;
304                         }
305                                 
306                 } else {
307
308                         /*
309                          * Exclusive waiters sleeping with LK_SLEEPFAIL on
310                          * and using interruptible sleeps/timeout may have
311                          * left spourious lk_exslpfail counts on, so clean
312                          * it up anyway.
313                          */
314                         lk->lk_exslpfail = 0;
315                         queue = SQ_SHARED_QUEUE;
316                 }
317
318                 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
319                     v)) {
320                         sleepq_release(&lk->lock_object);
321                         continue;
322                 }
323                 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
324                     __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
325                     "exclusive");
326                 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
327                     0, queue);
328                 sleepq_release(&lk->lock_object);
329                 break;
330         }
331
332         lock_profile_release_lock(&lk->lock_object);
333         return (wakeup_swapper);
334 }
335
336 static void
337 assert_lockmgr(struct lock_object *lock, int what)
338 {
339
340         panic("lockmgr locks do not support assertions");
341 }
342
343 static void
344 lock_lockmgr(struct lock_object *lock, int how)
345 {
346
347         panic("lockmgr locks do not support sleep interlocking");
348 }
349
350 static int
351 unlock_lockmgr(struct lock_object *lock)
352 {
353
354         panic("lockmgr locks do not support sleep interlocking");
355 }
356
357 #ifdef KDTRACE_HOOKS
358 static int
359 owner_lockmgr(struct lock_object *lock, struct thread **owner)
360 {
361
362         panic("lockmgr locks do not support owner inquiring");
363 }
364 #endif
365
366 void
367 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
368 {
369         int iflags;
370
371         MPASS((flags & ~LK_INIT_MASK) == 0);
372         ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock,
373             ("%s: lockmgr not aligned for %s: %p", __func__, wmesg,
374             &lk->lk_lock));
375
376         iflags = LO_SLEEPABLE | LO_UPGRADABLE;
377         if (flags & LK_CANRECURSE)
378                 iflags |= LO_RECURSABLE;
379         if ((flags & LK_NODUP) == 0)
380                 iflags |= LO_DUPOK;
381         if (flags & LK_NOPROFILE)
382                 iflags |= LO_NOPROFILE;
383         if ((flags & LK_NOWITNESS) == 0)
384                 iflags |= LO_WITNESS;
385         if (flags & LK_QUIET)
386                 iflags |= LO_QUIET;
387         iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
388
389         lk->lk_lock = LK_UNLOCKED;
390         lk->lk_recurse = 0;
391         lk->lk_exslpfail = 0;
392         lk->lk_timo = timo;
393         lk->lk_pri = pri;
394         lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
395         STACK_ZERO(lk);
396 }
397
398 /*
399  * XXX: Gross hacks to manipulate external lock flags after
400  * initialization.  Used for certain vnode and buf locks.
401  */
402 void
403 lockallowshare(struct lock *lk)
404 {
405
406         lockmgr_assert(lk, KA_XLOCKED);
407         lk->lock_object.lo_flags &= ~LK_NOSHARE;
408 }
409
410 void
411 lockallowrecurse(struct lock *lk)
412 {
413
414         lockmgr_assert(lk, KA_XLOCKED);
415         lk->lock_object.lo_flags |= LO_RECURSABLE;
416 }
417
418 void
419 lockdisablerecurse(struct lock *lk)
420 {
421
422         lockmgr_assert(lk, KA_XLOCKED);
423         lk->lock_object.lo_flags &= ~LO_RECURSABLE;
424 }
425
426 void
427 lockdestroy(struct lock *lk)
428 {
429
430         KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held"));
431         KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed"));
432         KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters"));
433         lock_destroy(&lk->lock_object);
434 }
435
436 int
437 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk,
438     const char *wmesg, int pri, int timo, const char *file, int line)
439 {
440         GIANT_DECLARE;
441         struct lock_class *class;
442         const char *iwmesg;
443         uintptr_t tid, v, x;
444         u_int op, realexslp;
445         int error, ipri, itimo, queue, wakeup_swapper;
446 #ifdef LOCK_PROFILING
447         uint64_t waittime = 0;
448         int contested = 0;
449 #endif
450 #ifdef ADAPTIVE_LOCKMGRS
451         volatile struct thread *owner;
452         u_int i, spintries = 0;
453 #endif
454
455         error = 0;
456         tid = (uintptr_t)curthread;
457         op = (flags & LK_TYPE_MASK);
458         iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg;
459         ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri;
460         itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo;
461
462         MPASS((flags & ~LK_TOTAL_MASK) == 0);
463         KASSERT((op & (op - 1)) == 0,
464             ("%s: Invalid requested operation @ %s:%d", __func__, file, line));
465         KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 ||
466             (op != LK_DOWNGRADE && op != LK_RELEASE),
467             ("%s: Invalid flags in regard of the operation desired @ %s:%d",
468             __func__, file, line));
469         KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL,
470             ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d",
471             __func__, file, line));
472
473         class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
474         if (panicstr != NULL) {
475                 if (flags & LK_INTERLOCK)
476                         class->lc_unlock(ilk);
477                 return (0);
478         }
479
480         if (op == LK_SHARED && (lk->lock_object.lo_flags & LK_NOSHARE))
481                 op = LK_EXCLUSIVE;
482
483         wakeup_swapper = 0;
484         switch (op) {
485         case LK_SHARED:
486                 if (LK_CAN_WITNESS(flags))
487                         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
488                             file, line, ilk);
489                 for (;;) {
490                         x = lk->lk_lock;
491
492                         /*
493                          * If no other thread has an exclusive lock, or
494                          * no exclusive waiter is present, bump the count of
495                          * sharers.  Since we have to preserve the state of
496                          * waiters, if we fail to acquire the shared lock
497                          * loop back and retry.
498                          */
499                         if (LK_CAN_SHARE(x)) {
500                                 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
501                                     x + LK_ONE_SHARER))
502                                         break;
503                                 continue;
504                         }
505                         lock_profile_obtain_lock_failed(&lk->lock_object,
506                             &contested, &waittime);
507
508                         /*
509                          * If the lock is already held by curthread in
510                          * exclusive way avoid a deadlock.
511                          */
512                         if (LK_HOLDER(x) == tid) {
513                                 LOCK_LOG2(lk,
514                                     "%s: %p already held in exclusive mode",
515                                     __func__, lk);
516                                 error = EDEADLK;
517                                 break;
518                         }
519
520                         /*
521                          * If the lock is expected to not sleep just give up
522                          * and return.
523                          */
524                         if (LK_TRYOP(flags)) {
525                                 LOCK_LOG2(lk, "%s: %p fails the try operation",
526                                     __func__, lk);
527                                 error = EBUSY;
528                                 break;
529                         }
530
531 #ifdef ADAPTIVE_LOCKMGRS
532                         /*
533                          * If the owner is running on another CPU, spin until
534                          * the owner stops running or the state of the lock
535                          * changes.  We need a double-state handle here
536                          * because for a failed acquisition the lock can be
537                          * either held in exclusive mode or shared mode
538                          * (for the writer starvation avoidance technique).
539                          */
540                         if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
541                             LK_HOLDER(x) != LK_KERNPROC) {
542                                 owner = (struct thread *)LK_HOLDER(x);
543                                 if (LOCK_LOG_TEST(&lk->lock_object, 0))
544                                         CTR3(KTR_LOCK,
545                                             "%s: spinning on %p held by %p",
546                                             __func__, lk, owner);
547
548                                 /*
549                                  * If we are holding also an interlock drop it
550                                  * in order to avoid a deadlock if the lockmgr
551                                  * owner is adaptively spinning on the
552                                  * interlock itself.
553                                  */
554                                 if (flags & LK_INTERLOCK) {
555                                         class->lc_unlock(ilk);
556                                         flags &= ~LK_INTERLOCK;
557                                 }
558                                 GIANT_SAVE();
559                                 while (LK_HOLDER(lk->lk_lock) ==
560                                     (uintptr_t)owner && TD_IS_RUNNING(owner))
561                                         cpu_spinwait();
562                                 GIANT_RESTORE();
563                                 continue;
564                         } else if (LK_CAN_ADAPT(lk, flags) &&
565                             (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
566                             spintries < alk_retries) {
567                                 if (flags & LK_INTERLOCK) {
568                                         class->lc_unlock(ilk);
569                                         flags &= ~LK_INTERLOCK;
570                                 }
571                                 GIANT_SAVE();
572                                 spintries++;
573                                 for (i = 0; i < alk_loops; i++) {
574                                         if (LOCK_LOG_TEST(&lk->lock_object, 0))
575                                                 CTR4(KTR_LOCK,
576                                     "%s: shared spinning on %p with %u and %u",
577                                                     __func__, lk, spintries, i);
578                                         x = lk->lk_lock;
579                                         if ((x & LK_SHARE) == 0 ||
580                                             LK_CAN_SHARE(x) != 0)
581                                                 break;
582                                         cpu_spinwait();
583                                 }
584                                 GIANT_RESTORE();
585                                 if (i != alk_loops)
586                                         continue;
587                         }
588 #endif
589
590                         /*
591                          * Acquire the sleepqueue chain lock because we
592                          * probabilly will need to manipulate waiters flags.
593                          */
594                         sleepq_lock(&lk->lock_object);
595                         x = lk->lk_lock;
596
597                         /*
598                          * if the lock can be acquired in shared mode, try
599                          * again.
600                          */
601                         if (LK_CAN_SHARE(x)) {
602                                 sleepq_release(&lk->lock_object);
603                                 continue;
604                         }
605
606 #ifdef ADAPTIVE_LOCKMGRS
607                         /*
608                          * The current lock owner might have started executing
609                          * on another CPU (or the lock could have changed
610                          * owner) while we were waiting on the turnstile
611                          * chain lock.  If so, drop the turnstile lock and try
612                          * again.
613                          */
614                         if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
615                             LK_HOLDER(x) != LK_KERNPROC) {
616                                 owner = (struct thread *)LK_HOLDER(x);
617                                 if (TD_IS_RUNNING(owner)) {
618                                         sleepq_release(&lk->lock_object);
619                                         continue;
620                                 }
621                         }
622 #endif
623
624                         /*
625                          * Try to set the LK_SHARED_WAITERS flag.  If we fail,
626                          * loop back and retry.
627                          */
628                         if ((x & LK_SHARED_WAITERS) == 0) {
629                                 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x,
630                                     x | LK_SHARED_WAITERS)) {
631                                         sleepq_release(&lk->lock_object);
632                                         continue;
633                                 }
634                                 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
635                                     __func__, lk);
636                         }
637
638                         /*
639                          * As far as we have been unable to acquire the
640                          * shared lock and the shared waiters flag is set,
641                          * we will sleep.
642                          */
643                         error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
644                             SQ_SHARED_QUEUE);
645                         flags &= ~LK_INTERLOCK;
646                         if (error) {
647                                 LOCK_LOG3(lk,
648                                     "%s: interrupted sleep for %p with %d",
649                                     __func__, lk, error);
650                                 break;
651                         }
652                         LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
653                             __func__, lk);
654                 }
655                 if (error == 0) {
656                         lock_profile_obtain_lock_success(&lk->lock_object,
657                             contested, waittime, file, line);
658                         LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file,
659                             line);
660                         WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
661                             line);
662                         TD_LOCKS_INC(curthread);
663                         TD_SLOCKS_INC(curthread);
664                         STACK_SAVE(lk);
665                 }
666                 break;
667         case LK_UPGRADE:
668                 _lockmgr_assert(lk, KA_SLOCKED, file, line);
669                 v = lk->lk_lock;
670                 x = v & LK_ALL_WAITERS;
671                 v &= LK_EXCLUSIVE_SPINNERS;
672
673                 /*
674                  * Try to switch from one shared lock to an exclusive one.
675                  * We need to preserve waiters flags during the operation.
676                  */
677                 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
678                     tid | x)) {
679                         LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
680                             line);
681                         WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
682                             LK_TRYWIT(flags), file, line);
683                         TD_SLOCKS_DEC(curthread);
684                         break;
685                 }
686
687                 /*
688                  * We have been unable to succeed in upgrading, so just
689                  * give up the shared lock.
690                  */
691                 wakeup_swapper |= wakeupshlk(lk, file, line);
692
693                 /* FALLTHROUGH */
694         case LK_EXCLUSIVE:
695                 if (LK_CAN_WITNESS(flags))
696                         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
697                             LOP_EXCLUSIVE, file, line, ilk);
698
699                 /*
700                  * If curthread already holds the lock and this one is
701                  * allowed to recurse, simply recurse on it.
702                  */
703                 if (lockmgr_xlocked(lk)) {
704                         if ((flags & LK_CANRECURSE) == 0 &&
705                             (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
706
707                                 /*
708                                  * If the lock is expected to not panic just
709                                  * give up and return.
710                                  */
711                                 if (LK_TRYOP(flags)) {
712                                         LOCK_LOG2(lk,
713                                             "%s: %p fails the try operation",
714                                             __func__, lk);
715                                         error = EBUSY;
716                                         break;
717                                 }
718                                 if (flags & LK_INTERLOCK)
719                                         class->lc_unlock(ilk);
720                 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n",
721                                     __func__, iwmesg, file, line);
722                         }
723                         lk->lk_recurse++;
724                         LOCK_LOG2(lk, "%s: %p recursing", __func__, lk);
725                         LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
726                             lk->lk_recurse, file, line);
727                         WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
728                             LK_TRYWIT(flags), file, line);
729                         TD_LOCKS_INC(curthread);
730                         break;
731                 }
732
733                 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
734                     tid)) {
735                         lock_profile_obtain_lock_failed(&lk->lock_object,
736                             &contested, &waittime);
737
738                         /*
739                          * If the lock is expected to not sleep just give up
740                          * and return.
741                          */
742                         if (LK_TRYOP(flags)) {
743                                 LOCK_LOG2(lk, "%s: %p fails the try operation",
744                                     __func__, lk);
745                                 error = EBUSY;
746                                 break;
747                         }
748
749 #ifdef ADAPTIVE_LOCKMGRS
750                         /*
751                          * If the owner is running on another CPU, spin until
752                          * the owner stops running or the state of the lock
753                          * changes.
754                          */
755                         x = lk->lk_lock;
756                         if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
757                             LK_HOLDER(x) != LK_KERNPROC) {
758                                 owner = (struct thread *)LK_HOLDER(x);
759                                 if (LOCK_LOG_TEST(&lk->lock_object, 0))
760                                         CTR3(KTR_LOCK,
761                                             "%s: spinning on %p held by %p",
762                                             __func__, lk, owner);
763
764                                 /*
765                                  * If we are holding also an interlock drop it
766                                  * in order to avoid a deadlock if the lockmgr
767                                  * owner is adaptively spinning on the
768                                  * interlock itself.
769                                  */
770                                 if (flags & LK_INTERLOCK) {
771                                         class->lc_unlock(ilk);
772                                         flags &= ~LK_INTERLOCK;
773                                 }
774                                 GIANT_SAVE();
775                                 while (LK_HOLDER(lk->lk_lock) ==
776                                     (uintptr_t)owner && TD_IS_RUNNING(owner))
777                                         cpu_spinwait();
778                                 GIANT_RESTORE();
779                                 continue;
780                         } else if (LK_CAN_ADAPT(lk, flags) &&
781                             (x & LK_SHARE) != 0 && LK_SHARERS(x) &&
782                             spintries < alk_retries) {
783                                 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 &&
784                                     !atomic_cmpset_ptr(&lk->lk_lock, x,
785                                     x | LK_EXCLUSIVE_SPINNERS))
786                                         continue;
787                                 if (flags & LK_INTERLOCK) {
788                                         class->lc_unlock(ilk);
789                                         flags &= ~LK_INTERLOCK;
790                                 }
791                                 GIANT_SAVE();
792                                 spintries++;
793                                 for (i = 0; i < alk_loops; i++) {
794                                         if (LOCK_LOG_TEST(&lk->lock_object, 0))
795                                                 CTR4(KTR_LOCK,
796                                     "%s: shared spinning on %p with %u and %u",
797                                                     __func__, lk, spintries, i);
798                                         if ((lk->lk_lock &
799                                             LK_EXCLUSIVE_SPINNERS) == 0)
800                                                 break;
801                                         cpu_spinwait();
802                                 }
803                                 GIANT_RESTORE();
804                                 if (i != alk_loops)
805                                         continue;
806                         }
807 #endif
808
809                         /*
810                          * Acquire the sleepqueue chain lock because we
811                          * probabilly will need to manipulate waiters flags.
812                          */
813                         sleepq_lock(&lk->lock_object);
814                         x = lk->lk_lock;
815
816                         /*
817                          * if the lock has been released while we spun on
818                          * the sleepqueue chain lock just try again.
819                          */
820                         if (x == LK_UNLOCKED) {
821                                 sleepq_release(&lk->lock_object);
822                                 continue;
823                         }
824
825 #ifdef ADAPTIVE_LOCKMGRS
826                         /*
827                          * The current lock owner might have started executing
828                          * on another CPU (or the lock could have changed
829                          * owner) while we were waiting on the turnstile
830                          * chain lock.  If so, drop the turnstile lock and try
831                          * again.
832                          */
833                         if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 &&
834                             LK_HOLDER(x) != LK_KERNPROC) {
835                                 owner = (struct thread *)LK_HOLDER(x);
836                                 if (TD_IS_RUNNING(owner)) {
837                                         sleepq_release(&lk->lock_object);
838                                         continue;
839                                 }
840                         }
841 #endif
842
843                         /*
844                          * The lock can be in the state where there is a
845                          * pending queue of waiters, but still no owner.
846                          * This happens when the lock is contested and an
847                          * owner is going to claim the lock.
848                          * If curthread is the one successfully acquiring it
849                          * claim lock ownership and return, preserving waiters
850                          * flags.
851                          */
852                         v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
853                         if ((x & ~v) == LK_UNLOCKED) {
854                                 v &= ~LK_EXCLUSIVE_SPINNERS;
855                                 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
856                                     tid | v)) {
857                                         sleepq_release(&lk->lock_object);
858                                         LOCK_LOG2(lk,
859                                             "%s: %p claimed by a new writer",
860                                             __func__, lk);
861                                         break;
862                                 }
863                                 sleepq_release(&lk->lock_object);
864                                 continue;
865                         }
866
867                         /*
868                          * Try to set the LK_EXCLUSIVE_WAITERS flag.  If we
869                          * fail, loop back and retry.
870                          */
871                         if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
872                                 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
873                                     x | LK_EXCLUSIVE_WAITERS)) {
874                                         sleepq_release(&lk->lock_object);
875                                         continue;
876                                 }
877                                 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
878                                     __func__, lk);
879                         }
880
881                         /*
882                          * As far as we have been unable to acquire the
883                          * exclusive lock and the exclusive waiters flag
884                          * is set, we will sleep.
885                          */
886                         error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
887                             SQ_EXCLUSIVE_QUEUE);
888                         flags &= ~LK_INTERLOCK;
889                         if (error) {
890                                 LOCK_LOG3(lk,
891                                     "%s: interrupted sleep for %p with %d",
892                                     __func__, lk, error);
893                                 break;
894                         }
895                         LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
896                             __func__, lk);
897                 }
898                 if (error == 0) {
899                         lock_profile_obtain_lock_success(&lk->lock_object,
900                             contested, waittime, file, line);
901                         LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0,
902                             lk->lk_recurse, file, line);
903                         WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
904                             LK_TRYWIT(flags), file, line);
905                         TD_LOCKS_INC(curthread);
906                         STACK_SAVE(lk);
907                 }
908                 break;
909         case LK_DOWNGRADE:
910                 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
911                 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line);
912                 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line);
913                 TD_SLOCKS_INC(curthread);
914
915                 /*
916                  * In order to preserve waiters flags, just spin.
917                  */
918                 for (;;) {
919                         x = lk->lk_lock;
920                         MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
921                         x &= LK_ALL_WAITERS;
922                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
923                             LK_SHARERS_LOCK(1) | x))
924                                 break;
925                         cpu_spinwait();
926                 }
927                 break;
928         case LK_RELEASE:
929                 _lockmgr_assert(lk, KA_LOCKED, file, line);
930                 x = lk->lk_lock;
931
932                 if ((x & LK_SHARE) == 0) {
933
934                         /*
935                          * As first option, treact the lock as if it has not
936                          * any waiter.
937                          * Fix-up the tid var if the lock has been disowned.
938                          */
939                         if (LK_HOLDER(x) == LK_KERNPROC)
940                                 tid = LK_KERNPROC;
941                         else {
942                                 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
943                                     file, line);
944                                 TD_LOCKS_DEC(curthread);
945                         }
946                         LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
947                             lk->lk_recurse, file, line);
948
949                         /*
950                          * The lock is held in exclusive mode.
951                          * If the lock is recursed also, then unrecurse it.
952                          */
953                         if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
954                                 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
955                                     lk);
956                                 lk->lk_recurse--;
957                                 break;
958                         }
959                         if (tid != LK_KERNPROC)
960                                 lock_profile_release_lock(&lk->lock_object);
961
962                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
963                             LK_UNLOCKED))
964                                 break;
965
966                         sleepq_lock(&lk->lock_object);
967                         x = lk->lk_lock;
968                         v = LK_UNLOCKED;
969
970                         /*
971                          * If the lock has exclusive waiters, give them
972                          * preference in order to avoid deadlock with
973                          * shared runners up.
974                          * If interruptible sleeps left the exclusive queue
975                          * empty avoid a starvation for the threads sleeping
976                          * on the shared queue by giving them precedence
977                          * and cleaning up the exclusive waiters bit anyway.
978                          * Please note that lk_exslpfail count may be lying
979                          * about the real number of waiters with the
980                          * LK_SLEEPFAIL flag on because they may be used in
981                          * conjuction with interruptible sleeps so
982                          * lk_exslpfail might be considered an 'upper limit'
983                          * bound, including the edge cases.
984                          */
985                         MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
986                         realexslp = sleepq_sleepcnt(&lk->lock_object,
987                             SQ_EXCLUSIVE_QUEUE);
988                         if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) {
989                                 if (lk->lk_exslpfail < realexslp) {
990                                         lk->lk_exslpfail = 0;
991                                         queue = SQ_EXCLUSIVE_QUEUE;
992                                         v |= (x & LK_SHARED_WAITERS);
993                                 } else {
994                                         lk->lk_exslpfail = 0;
995                                         LOCK_LOG2(lk,
996                                         "%s: %p has only LK_SLEEPFAIL sleepers",
997                                             __func__, lk);
998                                         LOCK_LOG2(lk,
999                         "%s: %p waking up threads on the exclusive queue",
1000                                             __func__, lk);
1001                                         wakeup_swapper =
1002                                             sleepq_broadcast(&lk->lock_object,
1003                                             SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
1004                                         queue = SQ_SHARED_QUEUE;
1005                                 }
1006                         } else {
1007
1008                                 /*
1009                                  * Exclusive waiters sleeping with LK_SLEEPFAIL
1010                                  * on and using interruptible sleeps/timeout
1011                                  * may have left spourious lk_exslpfail counts
1012                                  * on, so clean it up anyway. 
1013                                  */
1014                                 lk->lk_exslpfail = 0;
1015                                 queue = SQ_SHARED_QUEUE;
1016                         }
1017
1018                         LOCK_LOG3(lk,
1019                             "%s: %p waking up threads on the %s queue",
1020                             __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
1021                             "exclusive");
1022                         atomic_store_rel_ptr(&lk->lk_lock, v);
1023                         wakeup_swapper |= sleepq_broadcast(&lk->lock_object,
1024                             SLEEPQ_LK, 0, queue);
1025                         sleepq_release(&lk->lock_object);
1026                         break;
1027                 } else
1028                         wakeup_swapper = wakeupshlk(lk, file, line);
1029                 break;
1030         case LK_DRAIN:
1031                 if (LK_CAN_WITNESS(flags))
1032                         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1033                             LOP_EXCLUSIVE, file, line, ilk);
1034
1035                 /*
1036                  * Trying to drain a lock we already own will result in a
1037                  * deadlock.
1038                  */
1039                 if (lockmgr_xlocked(lk)) {
1040                         if (flags & LK_INTERLOCK)
1041                                 class->lc_unlock(ilk);
1042                         panic("%s: draining %s with the lock held @ %s:%d\n",
1043                             __func__, iwmesg, file, line);
1044                 }
1045
1046                 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1047                         lock_profile_obtain_lock_failed(&lk->lock_object,
1048                             &contested, &waittime);
1049
1050                         /*
1051                          * If the lock is expected to not sleep just give up
1052                          * and return.
1053                          */
1054                         if (LK_TRYOP(flags)) {
1055                                 LOCK_LOG2(lk, "%s: %p fails the try operation",
1056                                     __func__, lk);
1057                                 error = EBUSY;
1058                                 break;
1059                         }
1060
1061                         /*
1062                          * Acquire the sleepqueue chain lock because we
1063                          * probabilly will need to manipulate waiters flags.
1064                          */
1065                         sleepq_lock(&lk->lock_object);
1066                         x = lk->lk_lock;
1067
1068                         /*
1069                          * if the lock has been released while we spun on
1070                          * the sleepqueue chain lock just try again.
1071                          */
1072                         if (x == LK_UNLOCKED) {
1073                                 sleepq_release(&lk->lock_object);
1074                                 continue;
1075                         }
1076
1077                         v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1078                         if ((x & ~v) == LK_UNLOCKED) {
1079                                 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1080
1081                                 /*
1082                                  * If interruptible sleeps left the exclusive
1083                                  * queue empty avoid a starvation for the
1084                                  * threads sleeping on the shared queue by
1085                                  * giving them precedence and cleaning up the
1086                                  * exclusive waiters bit anyway.
1087                                  * Please note that lk_exslpfail count may be
1088                                  * lying about the real number of waiters with
1089                                  * the LK_SLEEPFAIL flag on because they may
1090                                  * be used in conjuction with interruptible
1091                                  * sleeps so lk_exslpfail might be considered
1092                                  * an 'upper limit' bound, including the edge
1093                                  * cases.
1094                                  */
1095                                 if (v & LK_EXCLUSIVE_WAITERS) {
1096                                         queue = SQ_EXCLUSIVE_QUEUE;
1097                                         v &= ~LK_EXCLUSIVE_WAITERS;
1098                                 } else {
1099
1100                                         /*
1101                                          * Exclusive waiters sleeping with
1102                                          * LK_SLEEPFAIL on and using
1103                                          * interruptible sleeps/timeout may
1104                                          * have left spourious lk_exslpfail
1105                                          * counts on, so clean it up anyway.
1106                                          */
1107                                         MPASS(v & LK_SHARED_WAITERS);
1108                                         lk->lk_exslpfail = 0;
1109                                         queue = SQ_SHARED_QUEUE;
1110                                         v &= ~LK_SHARED_WAITERS;
1111                                 }
1112                                 if (queue == SQ_EXCLUSIVE_QUEUE) {
1113                                         realexslp =
1114                                             sleepq_sleepcnt(&lk->lock_object,
1115                                             SQ_EXCLUSIVE_QUEUE);
1116                                         if (lk->lk_exslpfail >= realexslp) {
1117                                                 lk->lk_exslpfail = 0;
1118                                                 queue = SQ_SHARED_QUEUE;
1119                                                 v &= ~LK_SHARED_WAITERS;
1120                                                 if (realexslp != 0) {
1121                                                         LOCK_LOG2(lk,
1122                                         "%s: %p has only LK_SLEEPFAIL sleepers",
1123                                                             __func__, lk);
1124                                                         LOCK_LOG2(lk,
1125                         "%s: %p waking up threads on the exclusive queue",
1126                                                             __func__, lk);
1127                                                         wakeup_swapper =
1128                                                             sleepq_broadcast(
1129                                                             &lk->lock_object,
1130                                                             SLEEPQ_LK, 0,
1131                                                             SQ_EXCLUSIVE_QUEUE);
1132                                                 }
1133                                         } else
1134                                                 lk->lk_exslpfail = 0;
1135                                 }
1136                                 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1137                                         sleepq_release(&lk->lock_object);
1138                                         continue;
1139                                 }
1140                                 LOCK_LOG3(lk,
1141                                 "%s: %p waking up all threads on the %s queue",
1142                                     __func__, lk, queue == SQ_SHARED_QUEUE ?
1143                                     "shared" : "exclusive");
1144                                 wakeup_swapper |= sleepq_broadcast(
1145                                     &lk->lock_object, SLEEPQ_LK, 0, queue);
1146
1147                                 /*
1148                                  * If shared waiters have been woken up we need
1149                                  * to wait for one of them to acquire the lock
1150                                  * before to set the exclusive waiters in
1151                                  * order to avoid a deadlock.
1152                                  */
1153                                 if (queue == SQ_SHARED_QUEUE) {
1154                                         for (v = lk->lk_lock;
1155                                             (v & LK_SHARE) && !LK_SHARERS(v);
1156                                             v = lk->lk_lock)
1157                                                 cpu_spinwait();
1158                                 }
1159                         }
1160
1161                         /*
1162                          * Try to set the LK_EXCLUSIVE_WAITERS flag.  If we
1163                          * fail, loop back and retry.
1164                          */
1165                         if ((x & LK_EXCLUSIVE_WAITERS) == 0) {
1166                                 if (!atomic_cmpset_ptr(&lk->lk_lock, x,
1167                                     x | LK_EXCLUSIVE_WAITERS)) {
1168                                         sleepq_release(&lk->lock_object);
1169                                         continue;
1170                                 }
1171                                 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1172                                     __func__, lk);
1173                         }
1174
1175                         /*
1176                          * As far as we have been unable to acquire the
1177                          * exclusive lock and the exclusive waiters flag
1178                          * is set, we will sleep.
1179                          */
1180                         if (flags & LK_INTERLOCK) {
1181                                 class->lc_unlock(ilk);
1182                                 flags &= ~LK_INTERLOCK;
1183                         }
1184                         GIANT_SAVE();
1185                         sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1186                             SQ_EXCLUSIVE_QUEUE);
1187                         sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1188                         GIANT_RESTORE();
1189                         LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1190                             __func__, lk);
1191                 }
1192
1193                 if (error == 0) {
1194                         lock_profile_obtain_lock_success(&lk->lock_object,
1195                             contested, waittime, file, line);
1196                         LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0,
1197                             lk->lk_recurse, file, line);
1198                         WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE |
1199                             LK_TRYWIT(flags), file, line);
1200                         TD_LOCKS_INC(curthread);
1201                         STACK_SAVE(lk);
1202                 }
1203                 break;
1204         default:
1205                 if (flags & LK_INTERLOCK)
1206                         class->lc_unlock(ilk);
1207                 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1208         }
1209
1210         if (flags & LK_INTERLOCK)
1211                 class->lc_unlock(ilk);
1212         if (wakeup_swapper)
1213                 kick_proc0();
1214
1215         return (error);
1216 }
1217
1218 void
1219 _lockmgr_disown(struct lock *lk, const char *file, int line)
1220 {
1221         uintptr_t tid, x;
1222
1223         tid = (uintptr_t)curthread;
1224         _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1225
1226         /*
1227          * If the owner is already LK_KERNPROC just skip the whole operation.
1228          */
1229         if (LK_HOLDER(lk->lk_lock) != tid)
1230                 return;
1231         lock_profile_release_lock(&lk->lock_object);
1232         LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line);
1233         WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line);
1234         TD_LOCKS_DEC(curthread);
1235         STACK_SAVE(lk);
1236
1237         /*
1238          * In order to preserve waiters flags, just spin.
1239          */
1240         for (;;) {
1241                 x = lk->lk_lock;
1242                 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1243                 x &= LK_ALL_WAITERS;
1244                 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1245                     LK_KERNPROC | x))
1246                         return;
1247                 cpu_spinwait();
1248         }
1249 }
1250
1251 void
1252 lockmgr_printinfo(struct lock *lk)
1253 {
1254         struct thread *td;
1255         uintptr_t x;
1256
1257         if (lk->lk_lock == LK_UNLOCKED)
1258                 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name);
1259         else if (lk->lk_lock & LK_SHARE)
1260                 printf("lock type %s: SHARED (count %ju)\n",
1261                     lk->lock_object.lo_name,
1262                     (uintmax_t)LK_SHARERS(lk->lk_lock));
1263         else {
1264                 td = lockmgr_xholder(lk);
1265                 printf("lock type %s: EXCL by thread %p (pid %d)\n",
1266                     lk->lock_object.lo_name, td, td->td_proc->p_pid);
1267         }
1268
1269         x = lk->lk_lock;
1270         if (x & LK_EXCLUSIVE_WAITERS)
1271                 printf(" with exclusive waiters pending\n");
1272         if (x & LK_SHARED_WAITERS)
1273                 printf(" with shared waiters pending\n");
1274         if (x & LK_EXCLUSIVE_SPINNERS)
1275                 printf(" with exclusive spinners pending\n");
1276
1277         STACK_PRINT(lk);
1278 }
1279
1280 int
1281 lockstatus(struct lock *lk)
1282 {
1283         uintptr_t v, x;
1284         int ret;
1285
1286         ret = LK_SHARED;
1287         x = lk->lk_lock;
1288         v = LK_HOLDER(x);
1289
1290         if ((x & LK_SHARE) == 0) {
1291                 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1292                         ret = LK_EXCLUSIVE;
1293                 else
1294                         ret = LK_EXCLOTHER;
1295         } else if (x == LK_UNLOCKED)
1296                 ret = 0;
1297
1298         return (ret);
1299 }
1300
1301 #ifdef INVARIANT_SUPPORT
1302 #ifndef INVARIANTS
1303 #undef  _lockmgr_assert
1304 #endif
1305
1306 void
1307 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1308 {
1309         int slocked = 0;
1310
1311         if (panicstr != NULL)
1312                 return;
1313         switch (what) {
1314         case KA_SLOCKED:
1315         case KA_SLOCKED | KA_NOTRECURSED:
1316         case KA_SLOCKED | KA_RECURSED:
1317                 slocked = 1;
1318         case KA_LOCKED:
1319         case KA_LOCKED | KA_NOTRECURSED:
1320         case KA_LOCKED | KA_RECURSED:
1321 #ifdef WITNESS
1322
1323                 /*
1324                  * We cannot trust WITNESS if the lock is held in exclusive
1325                  * mode and a call to lockmgr_disown() happened.
1326                  * Workaround this skipping the check if the lock is held in
1327                  * exclusive mode even for the KA_LOCKED case.
1328                  */
1329                 if (slocked || (lk->lk_lock & LK_SHARE)) {
1330                         witness_assert(&lk->lock_object, what, file, line);
1331                         break;
1332                 }
1333 #endif
1334                 if (lk->lk_lock == LK_UNLOCKED ||
1335                     ((lk->lk_lock & LK_SHARE) == 0 && (slocked ||
1336                     (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)))))
1337                         panic("Lock %s not %slocked @ %s:%d\n",
1338                             lk->lock_object.lo_name, slocked ? "share" : "",
1339                             file, line);
1340
1341                 if ((lk->lk_lock & LK_SHARE) == 0) {
1342                         if (lockmgr_recursed(lk)) {
1343                                 if (what & KA_NOTRECURSED)
1344                                         panic("Lock %s recursed @ %s:%d\n",
1345                                             lk->lock_object.lo_name, file,
1346                                             line);
1347                         } else if (what & KA_RECURSED)
1348                                 panic("Lock %s not recursed @ %s:%d\n",
1349                                     lk->lock_object.lo_name, file, line);
1350                 }
1351                 break;
1352         case KA_XLOCKED:
1353         case KA_XLOCKED | KA_NOTRECURSED:
1354         case KA_XLOCKED | KA_RECURSED:
1355                 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))
1356                         panic("Lock %s not exclusively locked @ %s:%d\n",
1357                             lk->lock_object.lo_name, file, line);
1358                 if (lockmgr_recursed(lk)) {
1359                         if (what & KA_NOTRECURSED)
1360                                 panic("Lock %s recursed @ %s:%d\n",
1361                                     lk->lock_object.lo_name, file, line);
1362                 } else if (what & KA_RECURSED)
1363                         panic("Lock %s not recursed @ %s:%d\n",
1364                             lk->lock_object.lo_name, file, line);
1365                 break;
1366         case KA_UNLOCKED:
1367                 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk))
1368                         panic("Lock %s exclusively locked @ %s:%d\n",
1369                             lk->lock_object.lo_name, file, line);
1370                 break;
1371         default:
1372                 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1373                     line);
1374         }
1375 }
1376 #endif
1377
1378 #ifdef DDB
1379 int
1380 lockmgr_chain(struct thread *td, struct thread **ownerp)
1381 {
1382         struct lock *lk;
1383
1384         lk = td->td_wchan;
1385
1386         if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1387                 return (0);
1388         db_printf("blocked on lockmgr %s", lk->lock_object.lo_name);
1389         if (lk->lk_lock & LK_SHARE)
1390                 db_printf("SHARED (count %ju)\n",
1391                     (uintmax_t)LK_SHARERS(lk->lk_lock));
1392         else
1393                 db_printf("EXCL\n");
1394         *ownerp = lockmgr_xholder(lk);
1395
1396         return (1);
1397 }
1398
1399 static void
1400 db_show_lockmgr(struct lock_object *lock)
1401 {
1402         struct thread *td;
1403         struct lock *lk;
1404
1405         lk = (struct lock *)lock;
1406
1407         db_printf(" state: ");
1408         if (lk->lk_lock == LK_UNLOCKED)
1409                 db_printf("UNLOCKED\n");
1410         else if (lk->lk_lock & LK_SHARE)
1411                 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock));
1412         else {
1413                 td = lockmgr_xholder(lk);
1414                 if (td == (struct thread *)LK_KERNPROC)
1415                         db_printf("XLOCK: LK_KERNPROC\n");
1416                 else
1417                         db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1418                             td->td_tid, td->td_proc->p_pid,
1419                             td->td_proc->p_comm);
1420                 if (lockmgr_recursed(lk))
1421                         db_printf(" recursed: %d\n", lk->lk_recurse);
1422         }
1423         db_printf(" waiters: ");
1424         switch (lk->lk_lock & LK_ALL_WAITERS) {
1425         case LK_SHARED_WAITERS:
1426                 db_printf("shared\n");
1427                 break;
1428         case LK_EXCLUSIVE_WAITERS:
1429                 db_printf("exclusive\n");
1430                 break;
1431         case LK_ALL_WAITERS:
1432                 db_printf("shared and exclusive\n");
1433                 break;
1434         default:
1435                 db_printf("none\n");
1436         }
1437         db_printf(" spinners: ");
1438         if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1439                 db_printf("exclusive\n");
1440         else
1441                 db_printf("none\n");
1442 }
1443 #endif