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