]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/kern/kern_lock.c
MFC r223364:
[FreeBSD/stable/8.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_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)));
60
61 #define SQ_EXCLUSIVE_QUEUE      0
62 #define SQ_SHARED_QUEUE         1
63
64 #ifdef ADAPTIVE_LOCKMGRS
65 #define ALK_RETRIES             10
66 #define ALK_LOOPS               10000
67 #endif
68
69 #ifndef INVARIANTS
70 #define _lockmgr_assert(lk, what, file, line)
71 #define TD_LOCKS_INC(td)
72 #define TD_LOCKS_DEC(td)
73 #else
74 #define TD_LOCKS_INC(td)        ((td)->td_locks++)
75 #define TD_LOCKS_DEC(td)        ((td)->td_locks--)
76 #endif
77 #define TD_SLOCKS_INC(td)       ((td)->td_lk_slocks++)
78 #define TD_SLOCKS_DEC(td)       ((td)->td_lk_slocks--)
79
80 #ifndef DEBUG_LOCKS
81 #define STACK_PRINT(lk)
82 #define STACK_SAVE(lk)
83 #define STACK_ZERO(lk)
84 #else
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)
88 #endif
89
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))
96
97 #define GIANT_DECLARE                                                   \
98         int _i = 0;                                                     \
99         WITNESS_SAVE_DECL(Giant)
100 #define GIANT_RESTORE() do {                                            \
101         if (_i > 0) {                                                   \
102                 while (_i--)                                            \
103                         mtx_lock(&Giant);                               \
104                 WITNESS_RESTORE(&Giant.lock_object, Giant);             \
105         }                                                               \
106 } while (0)
107 #define GIANT_SAVE() do {                                               \
108         if (mtx_owned(&Giant)) {                                        \
109                 WITNESS_SAVE(&Giant.lock_object, Giant);                \
110                 while (mtx_owned(&Giant)) {                             \
111                         _i++;                                           \
112                         mtx_unlock(&Giant);                             \
113                 }                                                       \
114         }                                                               \
115 } while (0)
116
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)                                                     \
122         ((x) & LK_NOWAIT)
123
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)
128
129 #define LK_CAN_ADAPT(lk, f)                                             \
130         (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 &&             \
131         ((f) & LK_SLEEPFAIL) == 0)
132
133 #define lockmgr_disowned(lk)                                            \
134         (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC)
135
136 #define lockmgr_xlocked(lk)                                             \
137         (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread)
138
139 static void      assert_lockmgr(struct lock_object *lock, int how);
140 #ifdef DDB
141 static void      db_show_lockmgr(struct lock_object *lock);
142 #endif
143 static void      lock_lockmgr(struct lock_object *lock, int how);
144 #ifdef KDTRACE_HOOKS
145 static int       owner_lockmgr(struct lock_object *lock, struct thread **owner);
146 #endif
147 static int       unlock_lockmgr(struct lock_object *lock);
148
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,
153 #ifdef DDB
154         .lc_ddb_show = db_show_lockmgr,
155 #endif
156         .lc_lock = lock_lockmgr,
157         .lc_unlock = unlock_lockmgr,
158 #ifdef KDTRACE_HOOKS
159         .lc_owner = owner_lockmgr,
160 #endif
161 };
162
163 static __inline struct thread *
164 lockmgr_xholder(struct lock *lk)
165 {
166         uintptr_t x;
167
168         x = lk->lk_lock;
169         return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x));
170 }
171
172 /*
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
176  * sleep.
177  */
178 static __inline int
179 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk,
180     const char *wmesg, int pri, int timo, int queue)
181 {
182         GIANT_DECLARE;
183         struct lock_class *class;
184         int catch, error;
185
186         class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
187         catch = pri & PCATCH;
188         pri &= PRIMASK;
189         error = 0;
190
191         LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk,
192             (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared");
193
194         if (flags & LK_INTERLOCK)
195                 class->lc_unlock(ilk);
196
197         /*
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.
201          */
202         if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0)
203                 lk->lock_object.lo_flags |= LK_EXSLPFAIL;
204         GIANT_SAVE();
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);
209
210         /*
211          * Decisional switch for real sleeping.
212          */
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);
217         else if (catch)
218                 error = sleepq_wait_sig(&lk->lock_object, pri);
219         else
220                 sleepq_wait(&lk->lock_object, pri);
221         GIANT_RESTORE();
222         if ((flags & LK_SLEEPFAIL) && error == 0)
223                 error = ENOLCK;
224
225         return (error);
226 }
227
228 static __inline int
229 wakeupshlk(struct lock *lk, const char *file, int line)
230 {
231         uintptr_t v, x;
232         u_int realexslp;
233         int queue, wakeup_swapper;
234
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);
239
240         wakeup_swapper = 0;
241         for (;;) {
242                 x = lk->lk_lock;
243
244                 /*
245                  * If there is more than one shared lock held, just drop one
246                  * and return.
247                  */
248                 if (LK_SHARERS(x) > 1) {
249                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, x,
250                             x - LK_ONE_SHARER))
251                                 break;
252                         continue;
253                 }
254
255                 /*
256                  * If there are not waiters on the exclusive queue, drop the
257                  * lock quickly.
258                  */
259                 if ((x & LK_ALL_WAITERS) == 0) {
260                         MPASS((x & ~LK_EXCLUSIVE_SPINNERS) ==
261                             LK_SHARERS_LOCK(1));
262                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED))
263                                 break;
264                         continue;
265                 }
266
267                 /*
268                  * We should have a sharer with waiters, so enter the hard
269                  * path in order to handle wakeups correctly.
270                  */
271                 sleepq_lock(&lk->lock_object);
272                 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
273                 v = LK_UNLOCKED;
274
275                 /*
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
285                  * sleeps.
286                  */
287                 realexslp = sleepq_sleepcnt(&lk->lock_object,
288                     SQ_EXCLUSIVE_QUEUE);
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);
294                         } else {
295                                 lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
296                                 LOCK_LOG2(lk,
297                                     "%s: %p has only LK_SLEEPFAIL sleepers",
298                                     __func__, lk);
299                                 LOCK_LOG2(lk,
300                             "%s: %p waking up threads on the exclusive queue",
301                                     __func__, lk);
302                                 wakeup_swapper =
303                                     sleepq_broadcast(&lk->lock_object,
304                                     SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
305                                 queue = SQ_SHARED_QUEUE;
306                         }
307                                 
308                 } else {
309
310                         /*
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
314                          * it up anyway.
315                          */
316                         lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
317                         queue = SQ_SHARED_QUEUE;
318                 }
319
320                 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x,
321                     v)) {
322                         sleepq_release(&lk->lock_object);
323                         continue;
324                 }
325                 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue",
326                     __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
327                     "exclusive");
328                 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK,
329                     0, queue);
330                 sleepq_release(&lk->lock_object);
331                 break;
332         }
333
334         lock_profile_release_lock(&lk->lock_object);
335         return (wakeup_swapper);
336 }
337
338 static void
339 assert_lockmgr(struct lock_object *lock, int what)
340 {
341
342         panic("lockmgr locks do not support assertions");
343 }
344
345 static void
346 lock_lockmgr(struct lock_object *lock, int how)
347 {
348
349         panic("lockmgr locks do not support sleep interlocking");
350 }
351
352 static int
353 unlock_lockmgr(struct lock_object *lock)
354 {
355
356         panic("lockmgr locks do not support sleep interlocking");
357 }
358
359 #ifdef KDTRACE_HOOKS
360 static int
361 owner_lockmgr(struct lock_object *lock, struct thread **owner)
362 {
363
364         panic("lockmgr locks do not support owner inquiring");
365 }
366 #endif
367
368 void
369 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags)
370 {
371         int iflags;
372
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,
376             &lk->lk_lock));
377
378         iflags = LO_SLEEPABLE | LO_UPGRADABLE;
379         if (flags & LK_CANRECURSE)
380                 iflags |= LO_RECURSABLE;
381         if ((flags & LK_NODUP) == 0)
382                 iflags |= LO_DUPOK;
383         if (flags & LK_NOPROFILE)
384                 iflags |= LO_NOPROFILE;
385         if ((flags & LK_NOWITNESS) == 0)
386                 iflags |= LO_WITNESS;
387         if (flags & LK_QUIET)
388                 iflags |= LO_QUIET;
389         iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE);
390
391         lk->lk_lock = LK_UNLOCKED;
392         lk->lk_recurse = 0;
393         lk->lk_timo = timo;
394         lk->lk_pri = pri;
395         lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags);
396         STACK_ZERO(lk);
397 }
398
399 void
400 lockdestroy(struct lock *lk)
401 {
402
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);
408 }
409
410 int
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)
413 {
414         GIANT_DECLARE;
415         struct lock_class *class;
416         const char *iwmesg;
417         uintptr_t tid, v, x;
418         u_int op, realexslp;
419         int error, ipri, itimo, queue, wakeup_swapper;
420 #ifdef LOCK_PROFILING
421         uint64_t waittime = 0;
422         int contested = 0;
423 #endif
424 #ifdef ADAPTIVE_LOCKMGRS
425         volatile struct thread *owner;
426         u_int i, spintries = 0;
427 #endif
428
429         error = 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;
435
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));
446
447         class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL;
448         if (panicstr != NULL) {
449                 if (flags & LK_INTERLOCK)
450                         class->lc_unlock(ilk);
451                 return (0);
452         }
453
454         if (op == LK_SHARED && (lk->lock_object.lo_flags & LK_NOSHARE))
455                 op = LK_EXCLUSIVE;
456
457         wakeup_swapper = 0;
458         switch (op) {
459         case LK_SHARED:
460                 if (LK_CAN_WITNESS(flags))
461                         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER,
462                             file, line, ilk);
463                 for (;;) {
464                         x = lk->lk_lock;
465
466                         /*
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.
472                          */
473                         if (LK_CAN_SHARE(x)) {
474                                 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x,
475                                     x + LK_ONE_SHARER))
476                                         break;
477                                 continue;
478                         }
479                         lock_profile_obtain_lock_failed(&lk->lock_object,
480                             &contested, &waittime);
481
482                         /*
483                          * If the lock is already held by curthread in
484                          * exclusive way avoid a deadlock.
485                          */
486                         if (LK_HOLDER(x) == tid) {
487                                 LOCK_LOG2(lk,
488                                     "%s: %p already held in exclusive mode",
489                                     __func__, lk);
490                                 error = EDEADLK;
491                                 break;
492                         }
493
494                         /*
495                          * If the lock is expected to not sleep just give up
496                          * and return.
497                          */
498                         if (LK_TRYOP(flags)) {
499                                 LOCK_LOG2(lk, "%s: %p fails the try operation",
500                                     __func__, lk);
501                                 error = EBUSY;
502                                 break;
503                         }
504
505 #ifdef ADAPTIVE_LOCKMGRS
506                         /*
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).
513                          */
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))
518                                         CTR3(KTR_LOCK,
519                                             "%s: spinning on %p held by %p",
520                                             __func__, lk, owner);
521
522                                 /*
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
526                                  * interlock itself.
527                                  */
528                                 if (flags & LK_INTERLOCK) {
529                                         class->lc_unlock(ilk);
530                                         flags &= ~LK_INTERLOCK;
531                                 }
532                                 GIANT_SAVE();
533                                 while (LK_HOLDER(lk->lk_lock) ==
534                                     (uintptr_t)owner && TD_IS_RUNNING(owner))
535                                         cpu_spinwait();
536                                 GIANT_RESTORE();
537                                 continue;
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;
544                                 }
545                                 GIANT_SAVE();
546                                 spintries++;
547                                 for (i = 0; i < ALK_LOOPS; i++) {
548                                         if (LOCK_LOG_TEST(&lk->lock_object, 0))
549                                                 CTR4(KTR_LOCK,
550                                     "%s: shared spinning on %p with %u and %u",
551                                                     __func__, lk, spintries, i);
552                                         x = lk->lk_lock;
553                                         if ((x & LK_SHARE) == 0 ||
554                                             LK_CAN_SHARE(x) != 0)
555                                                 break;
556                                         cpu_spinwait();
557                                 }
558                                 GIANT_RESTORE();
559                                 if (i != ALK_LOOPS)
560                                         continue;
561                         }
562 #endif
563
564                         /*
565                          * Acquire the sleepqueue chain lock because we
566                          * probabilly will need to manipulate waiters flags.
567                          */
568                         sleepq_lock(&lk->lock_object);
569                         x = lk->lk_lock;
570
571                         /*
572                          * if the lock can be acquired in shared mode, try
573                          * again.
574                          */
575                         if (LK_CAN_SHARE(x)) {
576                                 sleepq_release(&lk->lock_object);
577                                 continue;
578                         }
579
580 #ifdef ADAPTIVE_LOCKMGRS
581                         /*
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
586                          * again.
587                          */
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);
593                                         continue;
594                                 }
595                         }
596 #endif
597
598                         /*
599                          * Try to set the LK_SHARED_WAITERS flag.  If we fail,
600                          * loop back and retry.
601                          */
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);
606                                         continue;
607                                 }
608                                 LOCK_LOG2(lk, "%s: %p set shared waiters flag",
609                                     __func__, lk);
610                         }
611
612                         /*
613                          * As far as we have been unable to acquire the
614                          * shared lock and the shared waiters flag is set,
615                          * we will sleep.
616                          */
617                         error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
618                             SQ_SHARED_QUEUE);
619                         flags &= ~LK_INTERLOCK;
620                         if (error) {
621                                 LOCK_LOG3(lk,
622                                     "%s: interrupted sleep for %p with %d",
623                                     __func__, lk, error);
624                                 break;
625                         }
626                         LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
627                             __func__, lk);
628                 }
629                 if (error == 0) {
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,
633                             line);
634                         WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file,
635                             line);
636                         TD_LOCKS_INC(curthread);
637                         TD_SLOCKS_INC(curthread);
638                         STACK_SAVE(lk);
639                 }
640                 break;
641         case LK_UPGRADE:
642                 _lockmgr_assert(lk, KA_SLOCKED, file, line);
643                 v = lk->lk_lock;
644                 x = v & LK_ALL_WAITERS;
645                 v &= LK_EXCLUSIVE_SPINNERS;
646
647                 /*
648                  * Try to switch from one shared lock to an exclusive one.
649                  * We need to preserve waiters flags during the operation.
650                  */
651                 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v,
652                     tid | x)) {
653                         LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file,
654                             line);
655                         WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE |
656                             LK_TRYWIT(flags), file, line);
657                         TD_SLOCKS_DEC(curthread);
658                         break;
659                 }
660
661                 /*
662                  * We have been unable to succeed in upgrading, so just
663                  * give up the shared lock.
664                  */
665                 wakeup_swapper |= wakeupshlk(lk, file, line);
666
667                 /* FALLTHROUGH */
668         case LK_EXCLUSIVE:
669                 if (LK_CAN_WITNESS(flags))
670                         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
671                             LOP_EXCLUSIVE, file, line, ilk);
672
673                 /*
674                  * If curthread already holds the lock and this one is
675                  * allowed to recurse, simply recurse on it.
676                  */
677                 if (lockmgr_xlocked(lk)) {
678                         if ((flags & LK_CANRECURSE) == 0 &&
679                             (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) {
680
681                                 /*
682                                  * If the lock is expected to not panic just
683                                  * give up and return.
684                                  */
685                                 if (LK_TRYOP(flags)) {
686                                         LOCK_LOG2(lk,
687                                             "%s: %p fails the try operation",
688                                             __func__, lk);
689                                         error = EBUSY;
690                                         break;
691                                 }
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);
696                         }
697                         lk->lk_recurse++;
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);
704                         break;
705                 }
706
707                 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED,
708                     tid)) {
709                         lock_profile_obtain_lock_failed(&lk->lock_object,
710                             &contested, &waittime);
711
712                         /*
713                          * If the lock is expected to not sleep just give up
714                          * and return.
715                          */
716                         if (LK_TRYOP(flags)) {
717                                 LOCK_LOG2(lk, "%s: %p fails the try operation",
718                                     __func__, lk);
719                                 error = EBUSY;
720                                 break;
721                         }
722
723 #ifdef ADAPTIVE_LOCKMGRS
724                         /*
725                          * If the owner is running on another CPU, spin until
726                          * the owner stops running or the state of the lock
727                          * changes.
728                          */
729                         x = lk->lk_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))
734                                         CTR3(KTR_LOCK,
735                                             "%s: spinning on %p held by %p",
736                                             __func__, lk, owner);
737
738                                 /*
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
742                                  * interlock itself.
743                                  */
744                                 if (flags & LK_INTERLOCK) {
745                                         class->lc_unlock(ilk);
746                                         flags &= ~LK_INTERLOCK;
747                                 }
748                                 GIANT_SAVE();
749                                 while (LK_HOLDER(lk->lk_lock) ==
750                                     (uintptr_t)owner && TD_IS_RUNNING(owner))
751                                         cpu_spinwait();
752                                 GIANT_RESTORE();
753                                 continue;
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))
760                                         continue;
761                                 if (flags & LK_INTERLOCK) {
762                                         class->lc_unlock(ilk);
763                                         flags &= ~LK_INTERLOCK;
764                                 }
765                                 GIANT_SAVE();
766                                 spintries++;
767                                 for (i = 0; i < ALK_LOOPS; i++) {
768                                         if (LOCK_LOG_TEST(&lk->lock_object, 0))
769                                                 CTR4(KTR_LOCK,
770                                     "%s: shared spinning on %p with %u and %u",
771                                                     __func__, lk, spintries, i);
772                                         if ((lk->lk_lock &
773                                             LK_EXCLUSIVE_SPINNERS) == 0)
774                                                 break;
775                                         cpu_spinwait();
776                                 }
777                                 GIANT_RESTORE();
778                                 if (i != ALK_LOOPS)
779                                         continue;
780                         }
781 #endif
782
783                         /*
784                          * Acquire the sleepqueue chain lock because we
785                          * probabilly will need to manipulate waiters flags.
786                          */
787                         sleepq_lock(&lk->lock_object);
788                         x = lk->lk_lock;
789
790                         /*
791                          * if the lock has been released while we spun on
792                          * the sleepqueue chain lock just try again.
793                          */
794                         if (x == LK_UNLOCKED) {
795                                 sleepq_release(&lk->lock_object);
796                                 continue;
797                         }
798
799 #ifdef ADAPTIVE_LOCKMGRS
800                         /*
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
805                          * again.
806                          */
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);
812                                         continue;
813                                 }
814                         }
815 #endif
816
817                         /*
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
824                          * flags.
825                          */
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,
830                                     tid | v)) {
831                                         sleepq_release(&lk->lock_object);
832                                         LOCK_LOG2(lk,
833                                             "%s: %p claimed by a new writer",
834                                             __func__, lk);
835                                         break;
836                                 }
837                                 sleepq_release(&lk->lock_object);
838                                 continue;
839                         }
840
841                         /*
842                          * Try to set the LK_EXCLUSIVE_WAITERS flag.  If we
843                          * fail, loop back and retry.
844                          */
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);
849                                         continue;
850                                 }
851                                 LOCK_LOG2(lk, "%s: %p set excl waiters flag",
852                                     __func__, lk);
853                         }
854
855                         /*
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.
859                          */
860                         error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo,
861                             SQ_EXCLUSIVE_QUEUE);
862                         flags &= ~LK_INTERLOCK;
863                         if (error) {
864                                 LOCK_LOG3(lk,
865                                     "%s: interrupted sleep for %p with %d",
866                                     __func__, lk, error);
867                                 break;
868                         }
869                         LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
870                             __func__, lk);
871                 }
872                 if (error == 0) {
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);
880                         STACK_SAVE(lk);
881                 }
882                 break;
883         case LK_DOWNGRADE:
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);
888
889                 /*
890                  * In order to preserve waiters flags, just spin.
891                  */
892                 for (;;) {
893                         x = lk->lk_lock;
894                         MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
895                         x &= LK_ALL_WAITERS;
896                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
897                             LK_SHARERS_LOCK(1) | x))
898                                 break;
899                         cpu_spinwait();
900                 }
901                 break;
902         case LK_RELEASE:
903                 _lockmgr_assert(lk, KA_LOCKED, file, line);
904                 x = lk->lk_lock;
905
906                 if ((x & LK_SHARE) == 0) {
907
908                         /*
909                          * As first option, treact the lock as if it has not
910                          * any waiter.
911                          * Fix-up the tid var if the lock has been disowned.
912                          */
913                         if (LK_HOLDER(x) == LK_KERNPROC)
914                                 tid = LK_KERNPROC;
915                         else {
916                                 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE,
917                                     file, line);
918                                 TD_LOCKS_DEC(curthread);
919                         }
920                         LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0,
921                             lk->lk_recurse, file, line);
922
923                         /*
924                          * The lock is held in exclusive mode.
925                          * If the lock is recursed also, then unrecurse it.
926                          */
927                         if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) {
928                                 LOCK_LOG2(lk, "%s: %p unrecursing", __func__,
929                                     lk);
930                                 lk->lk_recurse--;
931                                 break;
932                         }
933                         if (tid != LK_KERNPROC)
934                                 lock_profile_release_lock(&lk->lock_object);
935
936                         if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid,
937                             LK_UNLOCKED))
938                                 break;
939
940                         sleepq_lock(&lk->lock_object);
941                         x = lk->lk_lock;
942                         v = LK_UNLOCKED;
943
944                         /*
945                          * If the lock has exclusive waiters, give them
946                          * preference in order to avoid deadlock with
947                          * shared runners up.
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.
956                          */
957                         MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
958                         realexslp = sleepq_sleepcnt(&lk->lock_object,
959                             SQ_EXCLUSIVE_QUEUE);
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 &=
964                                             ~LK_EXSLPFAIL;
965                                         queue = SQ_EXCLUSIVE_QUEUE;
966                                         v |= (x & LK_SHARED_WAITERS);
967                                 } else {
968                                         lk->lock_object.lo_flags &=
969                                             ~LK_EXSLPFAIL;
970                                         LOCK_LOG2(lk,
971                                         "%s: %p has only LK_SLEEPFAIL sleepers",
972                                             __func__, lk);
973                                         LOCK_LOG2(lk,
974                         "%s: %p waking up threads on the exclusive queue",
975                                             __func__, lk);
976                                         wakeup_swapper =
977                                             sleepq_broadcast(&lk->lock_object,
978                                             SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE);
979                                         queue = SQ_SHARED_QUEUE;
980                                 }
981                         } else {
982
983                                 /*
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.
988                                  */
989                                 lk->lock_object.lo_flags &= ~LK_EXSLPFAIL;
990                                 queue = SQ_SHARED_QUEUE;
991                         }
992
993                         LOCK_LOG3(lk,
994                             "%s: %p waking up threads on the %s queue",
995                             __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" :
996                             "exclusive");
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);
1001                         break;
1002                 } else
1003                         wakeup_swapper = wakeupshlk(lk, file, line);
1004                 break;
1005         case LK_DRAIN:
1006                 if (LK_CAN_WITNESS(flags))
1007                         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER |
1008                             LOP_EXCLUSIVE, file, line, ilk);
1009
1010                 /*
1011                  * Trying to drain a lock we already own will result in a
1012                  * deadlock.
1013                  */
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);
1019                 }
1020
1021                 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) {
1022                         lock_profile_obtain_lock_failed(&lk->lock_object,
1023                             &contested, &waittime);
1024
1025                         /*
1026                          * If the lock is expected to not sleep just give up
1027                          * and return.
1028                          */
1029                         if (LK_TRYOP(flags)) {
1030                                 LOCK_LOG2(lk, "%s: %p fails the try operation",
1031                                     __func__, lk);
1032                                 error = EBUSY;
1033                                 break;
1034                         }
1035
1036                         /*
1037                          * Acquire the sleepqueue chain lock because we
1038                          * probabilly will need to manipulate waiters flags.
1039                          */
1040                         sleepq_lock(&lk->lock_object);
1041                         x = lk->lk_lock;
1042
1043                         /*
1044                          * if the lock has been released while we spun on
1045                          * the sleepqueue chain lock just try again.
1046                          */
1047                         if (x == LK_UNLOCKED) {
1048                                 sleepq_release(&lk->lock_object);
1049                                 continue;
1050                         }
1051
1052                         v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS);
1053                         if ((x & ~v) == LK_UNLOCKED) {
1054                                 v = (x & ~LK_EXCLUSIVE_SPINNERS);
1055
1056                                 /*
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
1066                                  * sleeps.
1067                                  */
1068                                 if (v & LK_EXCLUSIVE_WAITERS) {
1069                                         queue = SQ_EXCLUSIVE_QUEUE;
1070                                         v &= ~LK_EXCLUSIVE_WAITERS;
1071                                 } else {
1072
1073                                         /*
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.
1079                                          */
1080                                         MPASS(v & LK_SHARED_WAITERS);
1081                                         lk->lock_object.lo_flags &=
1082                                             ~LK_EXSLPFAIL;
1083                                         queue = SQ_SHARED_QUEUE;
1084                                         v &= ~LK_SHARED_WAITERS;
1085                                 }
1086                                 if (queue == SQ_EXCLUSIVE_QUEUE) {
1087                                         realexslp =
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 &=
1093                                                     ~LK_EXSLPFAIL;
1094                                                 queue = SQ_SHARED_QUEUE;
1095                                                 v &= ~LK_SHARED_WAITERS;
1096                                                 if (realexslp != 0) {
1097                                                         LOCK_LOG2(lk,
1098                                         "%s: %p has only LK_SLEEPFAIL sleepers",
1099                                                             __func__, lk);
1100                                                         LOCK_LOG2(lk,
1101                         "%s: %p waking up threads on the exclusive queue",
1102                                                             __func__, lk);
1103                                                         wakeup_swapper =
1104                                                             sleepq_broadcast(
1105                                                             &lk->lock_object,
1106                                                             SLEEPQ_LK, 0,
1107                                                             SQ_EXCLUSIVE_QUEUE);
1108                                                 }
1109                                         } else
1110                                                 lk->lock_object.lo_flags &=
1111                                                     ~LK_EXSLPFAIL;
1112                                 }
1113                                 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) {
1114                                         sleepq_release(&lk->lock_object);
1115                                         continue;
1116                                 }
1117                                 LOCK_LOG3(lk,
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);
1123
1124                                 /*
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.
1129                                  */
1130                                 if (queue == SQ_SHARED_QUEUE) {
1131                                         for (v = lk->lk_lock;
1132                                             (v & LK_SHARE) && !LK_SHARERS(v);
1133                                             v = lk->lk_lock)
1134                                                 cpu_spinwait();
1135                                 }
1136                         }
1137
1138                         /*
1139                          * Try to set the LK_EXCLUSIVE_WAITERS flag.  If we
1140                          * fail, loop back and retry.
1141                          */
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);
1146                                         continue;
1147                                 }
1148                                 LOCK_LOG2(lk, "%s: %p set drain waiters flag",
1149                                     __func__, lk);
1150                         }
1151
1152                         /*
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.
1156                          */
1157                         if (flags & LK_INTERLOCK) {
1158                                 class->lc_unlock(ilk);
1159                                 flags &= ~LK_INTERLOCK;
1160                         }
1161                         GIANT_SAVE();
1162                         sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK,
1163                             SQ_EXCLUSIVE_QUEUE);
1164                         sleepq_wait(&lk->lock_object, ipri & PRIMASK);
1165                         GIANT_RESTORE();
1166                         LOCK_LOG2(lk, "%s: %p resuming from the sleep queue",
1167                             __func__, lk);
1168                 }
1169
1170                 if (error == 0) {
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);
1178                         STACK_SAVE(lk);
1179                 }
1180                 break;
1181         default:
1182                 if (flags & LK_INTERLOCK)
1183                         class->lc_unlock(ilk);
1184                 panic("%s: unknown lockmgr request 0x%x\n", __func__, op);
1185         }
1186
1187         if (flags & LK_INTERLOCK)
1188                 class->lc_unlock(ilk);
1189         if (wakeup_swapper)
1190                 kick_proc0();
1191
1192         return (error);
1193 }
1194
1195 void
1196 _lockmgr_disown(struct lock *lk, const char *file, int line)
1197 {
1198         uintptr_t tid, x;
1199
1200         tid = (uintptr_t)curthread;
1201         _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line);
1202
1203         /*
1204          * If the owner is already LK_KERNPROC just skip the whole operation.
1205          */
1206         if (LK_HOLDER(lk->lk_lock) != tid)
1207                 return;
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);
1212         STACK_SAVE(lk);
1213
1214         /*
1215          * In order to preserve waiters flags, just spin.
1216          */
1217         for (;;) {
1218                 x = lk->lk_lock;
1219                 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0);
1220                 x &= LK_ALL_WAITERS;
1221                 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x,
1222                     LK_KERNPROC | x))
1223                         return;
1224                 cpu_spinwait();
1225         }
1226 }
1227
1228 void
1229 lockmgr_printinfo(struct lock *lk)
1230 {
1231         struct thread *td;
1232         uintptr_t x;
1233
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));
1240         else {
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);
1244         }
1245
1246         x = lk->lk_lock;
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");
1253
1254         STACK_PRINT(lk);
1255 }
1256
1257 int
1258 lockstatus(struct lock *lk)
1259 {
1260         uintptr_t v, x;
1261         int ret;
1262
1263         ret = LK_SHARED;
1264         x = lk->lk_lock;
1265         v = LK_HOLDER(x);
1266
1267         if ((x & LK_SHARE) == 0) {
1268                 if (v == (uintptr_t)curthread || v == LK_KERNPROC)
1269                         ret = LK_EXCLUSIVE;
1270                 else
1271                         ret = LK_EXCLOTHER;
1272         } else if (x == LK_UNLOCKED)
1273                 ret = 0;
1274
1275         return (ret);
1276 }
1277
1278 #ifdef INVARIANT_SUPPORT
1279 #ifndef INVARIANTS
1280 #undef  _lockmgr_assert
1281 #endif
1282
1283 void
1284 _lockmgr_assert(struct lock *lk, int what, const char *file, int line)
1285 {
1286         int slocked = 0;
1287
1288         if (panicstr != NULL)
1289                 return;
1290         switch (what) {
1291         case KA_SLOCKED:
1292         case KA_SLOCKED | KA_NOTRECURSED:
1293         case KA_SLOCKED | KA_RECURSED:
1294                 slocked = 1;
1295         case KA_LOCKED:
1296         case KA_LOCKED | KA_NOTRECURSED:
1297         case KA_LOCKED | KA_RECURSED:
1298 #ifdef WITNESS
1299
1300                 /*
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.
1305                  */
1306                 if (slocked || (lk->lk_lock & LK_SHARE)) {
1307                         witness_assert(&lk->lock_object, what, file, line);
1308                         break;
1309                 }
1310 #endif
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" : "",
1316                             file, line);
1317
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,
1323                                             line);
1324                         } else if (what & KA_RECURSED)
1325                                 panic("Lock %s not recursed @ %s:%d\n",
1326                                     lk->lock_object.lo_name, file, line);
1327                 }
1328                 break;
1329         case KA_XLOCKED:
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);
1342                 break;
1343         case KA_UNLOCKED:
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);
1347                 break;
1348         default:
1349                 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file,
1350                     line);
1351         }
1352 }
1353 #endif
1354
1355 #ifdef DDB
1356 int
1357 lockmgr_chain(struct thread *td, struct thread **ownerp)
1358 {
1359         struct lock *lk;
1360
1361         lk = td->td_wchan;
1362
1363         if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr)
1364                 return (0);
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));
1369         else
1370                 db_printf("EXCL\n");
1371         *ownerp = lockmgr_xholder(lk);
1372
1373         return (1);
1374 }
1375
1376 static void
1377 db_show_lockmgr(struct lock_object *lock)
1378 {
1379         struct thread *td;
1380         struct lock *lk;
1381
1382         lk = (struct lock *)lock;
1383
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));
1389         else {
1390                 td = lockmgr_xholder(lk);
1391                 if (td == (struct thread *)LK_KERNPROC)
1392                         db_printf("XLOCK: LK_KERNPROC\n");
1393                 else
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);
1399         }
1400         db_printf(" waiters: ");
1401         switch (lk->lk_lock & LK_ALL_WAITERS) {
1402         case LK_SHARED_WAITERS:
1403                 db_printf("shared\n");
1404                 break;
1405         case LK_EXCLUSIVE_WAITERS:
1406                 db_printf("exclusive\n");
1407                 break;
1408         case LK_ALL_WAITERS:
1409                 db_printf("shared and exclusive\n");
1410                 break;
1411         default:
1412                 db_printf("none\n");
1413         }
1414         db_printf(" spinners: ");
1415         if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS)
1416                 db_printf("exclusive\n");
1417         else
1418                 db_printf("none\n");
1419 }
1420 #endif