xref: /freebsd/sys/kern/subr_turnstile.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
1 /*-
2  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. Berkeley Software Design Inc's name may not be used to endorse or
13  *    promote products derived from this software without specific prior
14  *    written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER 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
26  * SUCH DAMAGE.
27  *
28  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
29  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
30  */
31 
32 /*
33  * Implementation of turnstiles used to hold queue of threads blocked on
34  * non-sleepable locks.  Sleepable locks use condition variables to
35  * implement their queues.  Turnstiles differ from a sleep queue in that
36  * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
37  * when one thread is enqueued onto a turnstile, it can lend its priority
38  * to the owning thread.
39  *
40  * We wish to avoid bloating locks with an embedded turnstile and we do not
41  * want to use back-pointers in the locks for the same reason.  Thus, we
42  * use a similar approach to that of Solaris 7 as described in Solaris
43  * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
44  * in a hash table based on the address of the lock.  Each entry in the
45  * hash table is a linked-lists of turnstiles and is called a turnstile
46  * chain.  Each chain contains a spin mutex that protects all of the
47  * turnstiles in the chain.
48  *
49  * Each time a thread is created, a turnstile is malloc'd and attached to
50  * that thread.  When a thread blocks on a lock, if it is the first thread
51  * to block, it lends its turnstile to the lock.  If the lock already has
52  * a turnstile, then it gives its turnstile to the lock's turnstile's free
53  * list.  When a thread is woken up, it takes a turnstile from the free list
54  * if there are any other waiters.  If it is the only thread blocked on the
55  * lock, then it reclaims the turnstile associated with the lock and removes
56  * it from the hash table.
57  */
58 
59 #include "opt_turnstile_profiling.h"
60 
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/ktr.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mutex.h>
71 #include <sys/proc.h>
72 #include <sys/queue.h>
73 #include <sys/sched.h>
74 #include <sys/sysctl.h>
75 #include <sys/turnstile.h>
76 
77 /*
78  * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
79  * number chosen because the sleep queue's use the same value for the
80  * shift.  Basically, we ignore the lower 8 bits of the address.
81  * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
82  */
83 #define	TC_TABLESIZE	128			/* Must be power of 2. */
84 #define	TC_MASK		(TC_TABLESIZE - 1)
85 #define	TC_SHIFT	8
86 #define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
87 #define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
88 
89 /*
90  * There are three different lists of turnstiles as follows.  The list
91  * connected by ts_link entries is a per-thread list of all the turnstiles
92  * attached to locks that we own.  This is used to fixup our priority when
93  * a lock is released.  The other two lists use the ts_hash entries.  The
94  * first of these two is the turnstile chain list that a turnstile is on
95  * when it is attached to a lock.  The second list to use ts_hash is the
96  * free list hung off of a turnstile that is attached to a lock.
97  *
98  * Each turnstile contains two lists of threads.  The ts_blocked list is
99  * a linked list of threads blocked on the turnstile's lock.  The
100  * ts_pending list is a linked list of threads previously awakened by
101  * turnstile_signal() or turnstile_wait() that are waiting to be put on
102  * the run queue.
103  *
104  * Locking key:
105  *  c - turnstile chain lock
106  *  q - td_contested lock
107  */
108 struct turnstile {
109 	TAILQ_HEAD(, thread) ts_blocked;	/* (c + q) Blocked threads. */
110 	TAILQ_HEAD(, thread) ts_pending;	/* (c) Pending threads. */
111 	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
112 	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
113 	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
114 	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
115 	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
116 };
117 
118 struct turnstile_chain {
119 	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
120 	struct mtx tc_lock;			/* Spin lock for this chain. */
121 #ifdef TURNSTILE_PROFILING
122 	u_int	tc_depth;			/* Length of tc_queues. */
123 	u_int	tc_max_depth;			/* Max length of tc_queues. */
124 #endif
125 };
126 
127 #ifdef TURNSTILE_PROFILING
128 u_int turnstile_max_depth;
129 SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
130 SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
131     "turnstile chain stats");
132 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
133     &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
134 #endif
135 static struct mtx td_contested_lock;
136 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
137 
138 static MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
139 
140 /*
141  * Prototypes for non-exported routines.
142  */
143 static void	init_turnstile0(void *dummy);
144 #ifdef TURNSTILE_PROFILING
145 static void	init_turnstile_profiling(void *arg);
146 #endif
147 static void	propagate_priority(struct thread *td);
148 static int	turnstile_adjust_thread(struct turnstile *ts,
149 		    struct thread *td);
150 static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
151 
152 /*
153  * Walks the chain of turnstiles and their owners to propagate the priority
154  * of the thread being blocked to all the threads holding locks that have to
155  * release their locks before this thread can run again.
156  */
157 static void
158 propagate_priority(struct thread *td)
159 {
160 	struct turnstile_chain *tc;
161 	struct turnstile *ts;
162 	int pri;
163 
164 	mtx_assert(&sched_lock, MA_OWNED);
165 	pri = td->td_priority;
166 	ts = td->td_blocked;
167 	for (;;) {
168 		td = ts->ts_owner;
169 
170 		if (td == NULL) {
171 			/*
172 			 * This really isn't quite right. Really
173 			 * ought to bump priority of thread that
174 			 * next acquires the lock.
175 			 */
176 			return;
177 		}
178 
179 		MPASS(td->td_proc != NULL);
180 		MPASS(td->td_proc->p_magic == P_MAGIC);
181 
182 		/*
183 		 * XXX: The owner of a turnstile can be stale if it is the
184 		 * first thread to grab a slock of a sx lock.  In that case
185 		 * it is possible for us to be at SSLEEP or some other
186 		 * weird state.  We should probably just return if the state
187 		 * isn't SRUN or SLOCK.
188 		 */
189 		KASSERT(!TD_IS_SLEEPING(td),
190 		    ("sleeping thread (tid %d) owns a non-sleepable lock",
191 		    td->td_tid));
192 
193 		/*
194 		 * If this thread already has higher priority than the
195 		 * thread that is being blocked, we are finished.
196 		 */
197 		if (td->td_priority <= pri)
198 			return;
199 
200 		/*
201 		 * Bump this thread's priority.
202 		 */
203 		sched_lend_prio(td, pri);
204 
205 		/*
206 		 * If lock holder is actually running or on the run queue
207 		 * then we are done.
208 		 */
209 		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
210 			MPASS(td->td_blocked == NULL);
211 			return;
212 		}
213 
214 #ifndef SMP
215 		/*
216 		 * For UP, we check to see if td is curthread (this shouldn't
217 		 * ever happen however as it would mean we are in a deadlock.)
218 		 */
219 		KASSERT(td != curthread, ("Deadlock detected"));
220 #endif
221 
222 		/*
223 		 * If we aren't blocked on a lock, we should be.
224 		 */
225 		KASSERT(TD_ON_LOCK(td), (
226 		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
227 		    td->td_tid, td->td_proc->p_comm, td->td_state,
228 		    ts->ts_lockobj->lo_name));
229 
230 		/*
231 		 * Pick up the lock that td is blocked on.
232 		 */
233 		ts = td->td_blocked;
234 		MPASS(ts != NULL);
235 		tc = TC_LOOKUP(ts->ts_lockobj);
236 		mtx_lock_spin(&tc->tc_lock);
237 
238 		/* Resort td on the list if needed. */
239 		if (!turnstile_adjust_thread(ts, td)) {
240 			mtx_unlock_spin(&tc->tc_lock);
241 			return;
242 		}
243 		mtx_unlock_spin(&tc->tc_lock);
244 	}
245 }
246 
247 /*
248  * Adjust the thread's position on a turnstile after its priority has been
249  * changed.
250  */
251 static int
252 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
253 {
254 	struct turnstile_chain *tc;
255 	struct thread *td1, *td2;
256 
257 	mtx_assert(&sched_lock, MA_OWNED);
258 	MPASS(TD_ON_LOCK(td));
259 
260 	/*
261 	 * This thread may not be blocked on this turnstile anymore
262 	 * but instead might already be woken up on another CPU
263 	 * that is waiting on sched_lock in turnstile_unpend() to
264 	 * finish waking this thread up.  We can detect this case
265 	 * by checking to see if this thread has been given a
266 	 * turnstile by either turnstile_signal() or
267 	 * turnstile_broadcast().  In this case, treat the thread as
268 	 * if it was already running.
269 	 */
270 	if (td->td_turnstile != NULL)
271 		return (0);
272 
273 	/*
274 	 * Check if the thread needs to be moved on the blocked chain.
275 	 * It needs to be moved if either its priority is lower than
276 	 * the previous thread or higher than the next thread.
277 	 */
278 	tc = TC_LOOKUP(ts->ts_lockobj);
279 	mtx_assert(&tc->tc_lock, MA_OWNED);
280 	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
281 	td2 = TAILQ_NEXT(td, td_lockq);
282 	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
283 	    (td2 != NULL && td->td_priority > td2->td_priority)) {
284 
285 		/*
286 		 * Remove thread from blocked chain and determine where
287 		 * it should be moved to.
288 		 */
289 		mtx_lock_spin(&td_contested_lock);
290 		TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
291 		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
292 			MPASS(td1->td_proc->p_magic == P_MAGIC);
293 			if (td1->td_priority > td->td_priority)
294 				break;
295 		}
296 
297 		if (td1 == NULL)
298 			TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
299 		else
300 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
301 		mtx_unlock_spin(&td_contested_lock);
302 		if (td1 == NULL)
303 			CTR3(KTR_LOCK,
304 		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
305 			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
306 		else
307 			CTR4(KTR_LOCK,
308 		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
309 			    td->td_tid, td1->td_tid, ts->ts_lockobj,
310 			    ts->ts_lockobj->lo_name);
311 	}
312 	return (1);
313 }
314 
315 /*
316  * Early initialization of turnstiles.  This is not done via a SYSINIT()
317  * since this needs to be initialized very early when mutexes are first
318  * initialized.
319  */
320 void
321 init_turnstiles(void)
322 {
323 	int i;
324 
325 	for (i = 0; i < TC_TABLESIZE; i++) {
326 		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
327 		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
328 		    NULL, MTX_SPIN);
329 	}
330 	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
331 	thread0.td_turnstile = NULL;
332 }
333 
334 #ifdef TURNSTILE_PROFILING
335 static void
336 init_turnstile_profiling(void *arg)
337 {
338 	struct sysctl_oid *chain_oid;
339 	char chain_name[10];
340 	int i;
341 
342 	for (i = 0; i < TC_TABLESIZE; i++) {
343 		snprintf(chain_name, sizeof(chain_name), "%d", i);
344 		chain_oid = SYSCTL_ADD_NODE(NULL,
345 		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
346 		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
347 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
348 		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
349 		    NULL);
350 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
351 		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
352 		    0, NULL);
353 	}
354 }
355 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
356     init_turnstile_profiling, NULL);
357 #endif
358 
359 static void
360 init_turnstile0(void *dummy)
361 {
362 
363 	thread0.td_turnstile = turnstile_alloc();
364 }
365 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
366 
367 /*
368  * Update a thread on the turnstile list after it's priority has been changed.
369  * The old priority is passed in as an argument.
370  */
371 void
372 turnstile_adjust(struct thread *td, u_char oldpri)
373 {
374 	struct turnstile_chain *tc;
375 	struct turnstile *ts;
376 
377 	mtx_assert(&sched_lock, MA_OWNED);
378 	MPASS(TD_ON_LOCK(td));
379 
380 	/*
381 	 * Pick up the lock that td is blocked on.
382 	 */
383 	ts = td->td_blocked;
384 	MPASS(ts != NULL);
385 	tc = TC_LOOKUP(ts->ts_lockobj);
386 	mtx_lock_spin(&tc->tc_lock);
387 
388 	/* Resort the turnstile on the list. */
389 	if (!turnstile_adjust_thread(ts, td)) {
390 		mtx_unlock_spin(&tc->tc_lock);
391 		return;
392 	}
393 
394 	/*
395 	 * If our priority was lowered and we are at the head of the
396 	 * turnstile, then propagate our new priority up the chain.
397 	 * Note that we currently don't try to revoke lent priorities
398 	 * when our priority goes up.
399 	 */
400 	if (td == TAILQ_FIRST(&ts->ts_blocked) && td->td_priority < oldpri) {
401 		mtx_unlock_spin(&tc->tc_lock);
402 		propagate_priority(td);
403 	} else
404 		mtx_unlock_spin(&tc->tc_lock);
405 }
406 
407 /*
408  * Set the owner of the lock this turnstile is attached to.
409  */
410 static void
411 turnstile_setowner(struct turnstile *ts, struct thread *owner)
412 {
413 
414 	mtx_assert(&td_contested_lock, MA_OWNED);
415 	MPASS(owner->td_proc->p_magic == P_MAGIC);
416 	MPASS(ts->ts_owner == NULL);
417 	ts->ts_owner = owner;
418 	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
419 }
420 
421 /*
422  * Malloc a turnstile for a new thread, initialize it and return it.
423  */
424 struct turnstile *
425 turnstile_alloc(void)
426 {
427 	struct turnstile *ts;
428 
429 	ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
430 	TAILQ_INIT(&ts->ts_blocked);
431 	TAILQ_INIT(&ts->ts_pending);
432 	LIST_INIT(&ts->ts_free);
433 	return (ts);
434 }
435 
436 /*
437  * Free a turnstile when a thread is destroyed.
438  */
439 void
440 turnstile_free(struct turnstile *ts)
441 {
442 
443 	MPASS(ts != NULL);
444 	MPASS(TAILQ_EMPTY(&ts->ts_blocked));
445 	MPASS(TAILQ_EMPTY(&ts->ts_pending));
446 	free(ts, M_TURNSTILE);
447 }
448 
449 /*
450  * Lock the turnstile chain associated with the specified lock.
451  */
452 void
453 turnstile_lock(struct lock_object *lock)
454 {
455 	struct turnstile_chain *tc;
456 
457 	tc = TC_LOOKUP(lock);
458 	mtx_lock_spin(&tc->tc_lock);
459 }
460 
461 /*
462  * Look up the turnstile for a lock in the hash table locking the associated
463  * turnstile chain along the way.  If no turnstile is found in the hash
464  * table, NULL is returned.
465  */
466 struct turnstile *
467 turnstile_lookup(struct lock_object *lock)
468 {
469 	struct turnstile_chain *tc;
470 	struct turnstile *ts;
471 
472 	tc = TC_LOOKUP(lock);
473 	mtx_assert(&tc->tc_lock, MA_OWNED);
474 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
475 		if (ts->ts_lockobj == lock)
476 			return (ts);
477 	return (NULL);
478 }
479 
480 /*
481  * Unlock the turnstile chain associated with a given lock.
482  */
483 void
484 turnstile_release(struct lock_object *lock)
485 {
486 	struct turnstile_chain *tc;
487 
488 	tc = TC_LOOKUP(lock);
489 	mtx_unlock_spin(&tc->tc_lock);
490 }
491 
492 /*
493  * Take ownership of a turnstile and adjust the priority of the new
494  * owner appropriately.
495  */
496 void
497 turnstile_claim(struct lock_object *lock)
498 {
499 	struct turnstile_chain *tc;
500 	struct turnstile *ts;
501 	struct thread *td, *owner;
502 
503 	tc = TC_LOOKUP(lock);
504 	mtx_assert(&tc->tc_lock, MA_OWNED);
505 	ts = turnstile_lookup(lock);
506 	MPASS(ts != NULL);
507 
508 	owner = curthread;
509 	mtx_lock_spin(&td_contested_lock);
510 	turnstile_setowner(ts, owner);
511 	mtx_unlock_spin(&td_contested_lock);
512 
513 	td = TAILQ_FIRST(&ts->ts_blocked);
514 	MPASS(td != NULL);
515 	MPASS(td->td_proc->p_magic == P_MAGIC);
516 	mtx_unlock_spin(&tc->tc_lock);
517 
518 	/*
519 	 * Update the priority of the new owner if needed.
520 	 */
521 	mtx_lock_spin(&sched_lock);
522 	if (td->td_priority < owner->td_priority)
523 		sched_lend_prio(owner, td->td_priority);
524 	mtx_unlock_spin(&sched_lock);
525 }
526 
527 /*
528  * Block the current thread on the turnstile assicated with 'lock'.  This
529  * function will context switch and not return until this thread has been
530  * woken back up.  This function must be called with the appropriate
531  * turnstile chain locked and will return with it unlocked.
532  */
533 void
534 turnstile_wait(struct lock_object *lock, struct thread *owner)
535 {
536 	struct turnstile_chain *tc;
537 	struct turnstile *ts;
538 	struct thread *td, *td1;
539 
540 	td = curthread;
541 	tc = TC_LOOKUP(lock);
542 	mtx_assert(&tc->tc_lock, MA_OWNED);
543 	MPASS(td->td_turnstile != NULL);
544 	MPASS(owner != NULL);
545 	MPASS(owner->td_proc->p_magic == P_MAGIC);
546 
547 	/* Look up the turnstile associated with the lock 'lock'. */
548 	ts = turnstile_lookup(lock);
549 
550 	/*
551 	 * If the lock does not already have a turnstile, use this thread's
552 	 * turnstile.  Otherwise insert the current thread into the
553 	 * turnstile already in use by this lock.
554 	 */
555 	if (ts == NULL) {
556 #ifdef TURNSTILE_PROFILING
557 		tc->tc_depth++;
558 		if (tc->tc_depth > tc->tc_max_depth) {
559 			tc->tc_max_depth = tc->tc_depth;
560 			if (tc->tc_max_depth > turnstile_max_depth)
561 				turnstile_max_depth = tc->tc_max_depth;
562 		}
563 #endif
564 		ts = td->td_turnstile;
565 		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
566 		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
567 		    ("thread's turnstile has pending threads"));
568 		KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
569 		    ("thread's turnstile has a non-empty queue"));
570 		KASSERT(LIST_EMPTY(&ts->ts_free),
571 		    ("thread's turnstile has a non-empty free list"));
572 		KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
573 		ts->ts_lockobj = lock;
574 		mtx_lock_spin(&td_contested_lock);
575 		TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
576 		turnstile_setowner(ts, owner);
577 		mtx_unlock_spin(&td_contested_lock);
578 	} else {
579 		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
580 			if (td1->td_priority > td->td_priority)
581 				break;
582 		mtx_lock_spin(&td_contested_lock);
583 		if (td1 != NULL)
584 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
585 		else
586 			TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
587 		mtx_unlock_spin(&td_contested_lock);
588 		MPASS(td->td_turnstile != NULL);
589 		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
590 		MPASS(owner == ts->ts_owner);
591 	}
592 	td->td_turnstile = NULL;
593 	mtx_unlock_spin(&tc->tc_lock);
594 
595 	mtx_lock_spin(&sched_lock);
596 	/*
597 	 * Handle race condition where a thread on another CPU that owns
598 	 * lock 'lock' could have woken us in between us dropping the
599 	 * turnstile chain lock and acquiring the sched_lock.
600 	 */
601 	if (td->td_flags & TDF_TSNOBLOCK) {
602 		td->td_flags &= ~TDF_TSNOBLOCK;
603 		mtx_unlock_spin(&sched_lock);
604 		return;
605 	}
606 
607 #ifdef notyet
608 	/*
609 	 * If we're borrowing an interrupted thread's VM context, we
610 	 * must clean up before going to sleep.
611 	 */
612 	if (td->td_ithd != NULL) {
613 		struct ithd *it = td->td_ithd;
614 
615 		if (it->it_interrupted) {
616 			if (LOCK_LOG_TEST(lock, 0))
617 				CTR3(KTR_LOCK, "%s: %p interrupted %p",
618 				    __func__, it, it->it_interrupted);
619 			intr_thd_fixup(it);
620 		}
621 	}
622 #endif
623 
624 	/* Save who we are blocked on and switch. */
625 	td->td_blocked = ts;
626 	td->td_lockname = lock->lo_name;
627 	TD_SET_LOCK(td);
628 	propagate_priority(td);
629 
630 	if (LOCK_LOG_TEST(lock, 0))
631 		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
632 		    td->td_tid, lock, lock->lo_name);
633 
634 	mi_switch(SW_VOL, NULL);
635 
636 	if (LOCK_LOG_TEST(lock, 0))
637 		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
638 		    __func__, td->td_tid, lock, lock->lo_name);
639 
640 	mtx_unlock_spin(&sched_lock);
641 }
642 
643 /*
644  * Pick the highest priority thread on this turnstile and put it on the
645  * pending list.  This must be called with the turnstile chain locked.
646  */
647 int
648 turnstile_signal(struct turnstile *ts)
649 {
650 	struct turnstile_chain *tc;
651 	struct thread *td;
652 	int empty;
653 
654 	MPASS(ts != NULL);
655 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
656 	MPASS(ts->ts_owner == curthread);
657 	tc = TC_LOOKUP(ts->ts_lockobj);
658 	mtx_assert(&tc->tc_lock, MA_OWNED);
659 
660 	/*
661 	 * Pick the highest priority thread blocked on this lock and
662 	 * move it to the pending list.
663 	 */
664 	td = TAILQ_FIRST(&ts->ts_blocked);
665 	MPASS(td->td_proc->p_magic == P_MAGIC);
666 	mtx_lock_spin(&td_contested_lock);
667 	TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
668 	mtx_unlock_spin(&td_contested_lock);
669 	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
670 
671 	/*
672 	 * If the turnstile is now empty, remove it from its chain and
673 	 * give it to the about-to-be-woken thread.  Otherwise take a
674 	 * turnstile from the free list and give it to the thread.
675 	 */
676 	empty = TAILQ_EMPTY(&ts->ts_blocked);
677 	if (empty) {
678 		MPASS(LIST_EMPTY(&ts->ts_free));
679 #ifdef TURNSTILE_PROFILING
680 		tc->tc_depth--;
681 #endif
682 	} else
683 		ts = LIST_FIRST(&ts->ts_free);
684 	MPASS(ts != NULL);
685 	LIST_REMOVE(ts, ts_hash);
686 	td->td_turnstile = ts;
687 
688 	return (empty);
689 }
690 
691 /*
692  * Put all blocked threads on the pending list.  This must be called with
693  * the turnstile chain locked.
694  */
695 void
696 turnstile_broadcast(struct turnstile *ts)
697 {
698 	struct turnstile_chain *tc;
699 	struct turnstile *ts1;
700 	struct thread *td;
701 
702 	MPASS(ts != NULL);
703 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
704 	MPASS(ts->ts_owner == curthread);
705 	tc = TC_LOOKUP(ts->ts_lockobj);
706 	mtx_assert(&tc->tc_lock, MA_OWNED);
707 
708 	/*
709 	 * Transfer the blocked list to the pending list.
710 	 */
711 	mtx_lock_spin(&td_contested_lock);
712 	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
713 	mtx_unlock_spin(&td_contested_lock);
714 
715 	/*
716 	 * Give a turnstile to each thread.  The last thread gets
717 	 * this turnstile.
718 	 */
719 	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
720 		if (LIST_EMPTY(&ts->ts_free)) {
721 			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
722 			ts1 = ts;
723 #ifdef TURNSTILE_PROFILING
724 			tc->tc_depth--;
725 #endif
726 		} else
727 			ts1 = LIST_FIRST(&ts->ts_free);
728 		MPASS(ts1 != NULL);
729 		LIST_REMOVE(ts1, ts_hash);
730 		td->td_turnstile = ts1;
731 	}
732 }
733 
734 /*
735  * Wakeup all threads on the pending list and adjust the priority of the
736  * current thread appropriately.  This must be called with the turnstile
737  * chain locked.
738  */
739 void
740 turnstile_unpend(struct turnstile *ts)
741 {
742 	TAILQ_HEAD( ,thread) pending_threads;
743 	struct turnstile_chain *tc;
744 	struct thread *td;
745 	u_char cp, pri;
746 
747 	MPASS(ts != NULL);
748 	MPASS(ts->ts_owner == curthread);
749 	tc = TC_LOOKUP(ts->ts_lockobj);
750 	mtx_assert(&tc->tc_lock, MA_OWNED);
751 	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
752 
753 	/*
754 	 * Move the list of pending threads out of the turnstile and
755 	 * into a local variable.
756 	 */
757 	TAILQ_INIT(&pending_threads);
758 	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
759 #ifdef INVARIANTS
760 	if (TAILQ_EMPTY(&ts->ts_blocked))
761 		ts->ts_lockobj = NULL;
762 #endif
763 
764 	/*
765 	 * Remove the turnstile from this thread's list of contested locks
766 	 * since this thread doesn't own it anymore.  New threads will
767 	 * not be blocking on the turnstile until it is claimed by a new
768 	 * owner.
769 	 */
770 	mtx_lock_spin(&td_contested_lock);
771 	ts->ts_owner = NULL;
772 	LIST_REMOVE(ts, ts_link);
773 	mtx_unlock_spin(&td_contested_lock);
774 	critical_enter();
775 	mtx_unlock_spin(&tc->tc_lock);
776 
777 	/*
778 	 * Adjust the priority of curthread based on other contested
779 	 * locks it owns.  Don't lower the priority below the base
780 	 * priority however.
781 	 */
782 	td = curthread;
783 	pri = PRI_MAX;
784 	mtx_lock_spin(&sched_lock);
785 	mtx_lock_spin(&td_contested_lock);
786 	LIST_FOREACH(ts, &td->td_contested, ts_link) {
787 		cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
788 		if (cp < pri)
789 			pri = cp;
790 	}
791 	mtx_unlock_spin(&td_contested_lock);
792 	sched_unlend_prio(td, pri);
793 
794 	/*
795 	 * Wake up all the pending threads.  If a thread is not blocked
796 	 * on a lock, then it is currently executing on another CPU in
797 	 * turnstile_wait() or sitting on a run queue waiting to resume
798 	 * in turnstile_wait().  Set a flag to force it to try to acquire
799 	 * the lock again instead of blocking.
800 	 */
801 	while (!TAILQ_EMPTY(&pending_threads)) {
802 		td = TAILQ_FIRST(&pending_threads);
803 		TAILQ_REMOVE(&pending_threads, td, td_lockq);
804 		MPASS(td->td_proc->p_magic == P_MAGIC);
805 		if (TD_ON_LOCK(td)) {
806 			td->td_blocked = NULL;
807 			td->td_lockname = NULL;
808 			TD_CLR_LOCK(td);
809 			MPASS(TD_CAN_RUN(td));
810 			setrunqueue(td, SRQ_BORING);
811 		} else {
812 			td->td_flags |= TDF_TSNOBLOCK;
813 			MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
814 		}
815 	}
816 	critical_exit();
817 	mtx_unlock_spin(&sched_lock);
818 }
819 
820 /*
821  * Return the first thread in a turnstile.
822  */
823 struct thread *
824 turnstile_head(struct turnstile *ts)
825 {
826 #ifdef INVARIANTS
827 	struct turnstile_chain *tc;
828 
829 	MPASS(ts != NULL);
830 	tc = TC_LOOKUP(ts->ts_lockobj);
831 	mtx_assert(&tc->tc_lock, MA_OWNED);
832 #endif
833 	return (TAILQ_FIRST(&ts->ts_blocked));
834 }
835 
836 /*
837  * Returns true if a turnstile is empty.
838  */
839 int
840 turnstile_empty(struct turnstile *ts)
841 {
842 #ifdef INVARIANTS
843 	struct turnstile_chain *tc;
844 
845 	MPASS(ts != NULL);
846 	tc = TC_LOOKUP(ts->ts_lockobj);
847 	mtx_assert(&tc->tc_lock, MA_OWNED);
848 #endif
849 	return (TAILQ_EMPTY(&ts->ts_blocked));
850 }
851