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