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: releng/6.1/sys/kern/subr_turnstile.c 158179 2006-04-30 16:44:43Z cvs2svn $");
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 * If the thread is asleep, then we are probably about
184 * to deadlock. To make debugging this easier, just
185 * panic and tell the user which thread misbehaved so
186 * they can hopefully get a stack trace from the truly
187 * misbehaving thread.
188 */
189 if (TD_IS_SLEEPING(td)) {
190 printf(
191 "Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
192 td->td_tid, td->td_proc->p_pid);
193 #ifdef DDB
194 db_trace_thread(td, -1);
195 #endif
196 panic("sleeping thread");
197 }
198
199 /*
200 * If this thread already has higher priority than the
201 * thread that is being blocked, we are finished.
202 */
203 if (td->td_priority <= pri)
204 return;
205
206 /*
207 * Bump this thread's priority.
208 */
209 sched_lend_prio(td, pri);
210
211 /*
212 * If lock holder is actually running or on the run queue
213 * then we are done.
214 */
215 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
216 MPASS(td->td_blocked == NULL);
217 return;
218 }
219
220 #ifndef SMP
221 /*
222 * For UP, we check to see if td is curthread (this shouldn't
223 * ever happen however as it would mean we are in a deadlock.)
224 */
225 KASSERT(td != curthread, ("Deadlock detected"));
226 #endif
227
228 /*
229 * If we aren't blocked on a lock, we should be.
230 */
231 KASSERT(TD_ON_LOCK(td), (
232 "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
233 td->td_tid, td->td_proc->p_comm, td->td_state,
234 ts->ts_lockobj->lo_name));
235
236 /*
237 * Pick up the lock that td is blocked on.
238 */
239 ts = td->td_blocked;
240 MPASS(ts != NULL);
241 tc = TC_LOOKUP(ts->ts_lockobj);
242 mtx_lock_spin(&tc->tc_lock);
243
244 /* Resort td on the list if needed. */
245 if (!turnstile_adjust_thread(ts, td)) {
246 mtx_unlock_spin(&tc->tc_lock);
247 return;
248 }
249 mtx_unlock_spin(&tc->tc_lock);
250 }
251 }
252
253 /*
254 * Adjust the thread's position on a turnstile after its priority has been
255 * changed.
256 */
257 static int
258 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
259 {
260 struct turnstile_chain *tc;
261 struct thread *td1, *td2;
262
263 mtx_assert(&sched_lock, MA_OWNED);
264 MPASS(TD_ON_LOCK(td));
265
266 /*
267 * This thread may not be blocked on this turnstile anymore
268 * but instead might already be woken up on another CPU
269 * that is waiting on sched_lock in turnstile_unpend() to
270 * finish waking this thread up. We can detect this case
271 * by checking to see if this thread has been given a
272 * turnstile by either turnstile_signal() or
273 * turnstile_broadcast(). In this case, treat the thread as
274 * if it was already running.
275 */
276 if (td->td_turnstile != NULL)
277 return (0);
278
279 /*
280 * Check if the thread needs to be moved on the blocked chain.
281 * It needs to be moved if either its priority is lower than
282 * the previous thread or higher than the next thread.
283 */
284 tc = TC_LOOKUP(ts->ts_lockobj);
285 mtx_assert(&tc->tc_lock, MA_OWNED);
286 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
287 td2 = TAILQ_NEXT(td, td_lockq);
288 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
289 (td2 != NULL && td->td_priority > td2->td_priority)) {
290
291 /*
292 * Remove thread from blocked chain and determine where
293 * it should be moved to.
294 */
295 mtx_lock_spin(&td_contested_lock);
296 TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
297 TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
298 MPASS(td1->td_proc->p_magic == P_MAGIC);
299 if (td1->td_priority > td->td_priority)
300 break;
301 }
302
303 if (td1 == NULL)
304 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
305 else
306 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
307 mtx_unlock_spin(&td_contested_lock);
308 if (td1 == NULL)
309 CTR3(KTR_LOCK,
310 "turnstile_adjust_thread: td %d put at tail on [%p] %s",
311 td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
312 else
313 CTR4(KTR_LOCK,
314 "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
315 td->td_tid, td1->td_tid, ts->ts_lockobj,
316 ts->ts_lockobj->lo_name);
317 }
318 return (1);
319 }
320
321 /*
322 * Early initialization of turnstiles. This is not done via a SYSINIT()
323 * since this needs to be initialized very early when mutexes are first
324 * initialized.
325 */
326 void
327 init_turnstiles(void)
328 {
329 int i;
330
331 for (i = 0; i < TC_TABLESIZE; i++) {
332 LIST_INIT(&turnstile_chains[i].tc_turnstiles);
333 mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
334 NULL, MTX_SPIN);
335 }
336 mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
337 thread0.td_turnstile = NULL;
338 }
339
340 #ifdef TURNSTILE_PROFILING
341 static void
342 init_turnstile_profiling(void *arg)
343 {
344 struct sysctl_oid *chain_oid;
345 char chain_name[10];
346 int i;
347
348 for (i = 0; i < TC_TABLESIZE; i++) {
349 snprintf(chain_name, sizeof(chain_name), "%d", i);
350 chain_oid = SYSCTL_ADD_NODE(NULL,
351 SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
352 chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
353 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
354 "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
355 NULL);
356 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
357 "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
358 0, NULL);
359 }
360 }
361 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
362 init_turnstile_profiling, NULL);
363 #endif
364
365 static void
366 init_turnstile0(void *dummy)
367 {
368
369 thread0.td_turnstile = turnstile_alloc();
370 }
371 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
372
373 /*
374 * Update a thread on the turnstile list after it's priority has been changed.
375 * The old priority is passed in as an argument.
376 */
377 void
378 turnstile_adjust(struct thread *td, u_char oldpri)
379 {
380 struct turnstile_chain *tc;
381 struct turnstile *ts;
382
383 mtx_assert(&sched_lock, MA_OWNED);
384 MPASS(TD_ON_LOCK(td));
385
386 /*
387 * Pick up the lock that td is blocked on.
388 */
389 ts = td->td_blocked;
390 MPASS(ts != NULL);
391 tc = TC_LOOKUP(ts->ts_lockobj);
392 mtx_lock_spin(&tc->tc_lock);
393
394 /* Resort the turnstile on the list. */
395 if (!turnstile_adjust_thread(ts, td)) {
396 mtx_unlock_spin(&tc->tc_lock);
397 return;
398 }
399
400 /*
401 * If our priority was lowered and we are at the head of the
402 * turnstile, then propagate our new priority up the chain.
403 * Note that we currently don't try to revoke lent priorities
404 * when our priority goes up.
405 */
406 if (td == TAILQ_FIRST(&ts->ts_blocked) && td->td_priority < oldpri) {
407 mtx_unlock_spin(&tc->tc_lock);
408 propagate_priority(td);
409 } else
410 mtx_unlock_spin(&tc->tc_lock);
411 }
412
413 /*
414 * Set the owner of the lock this turnstile is attached to.
415 */
416 static void
417 turnstile_setowner(struct turnstile *ts, struct thread *owner)
418 {
419
420 mtx_assert(&td_contested_lock, MA_OWNED);
421 MPASS(owner->td_proc->p_magic == P_MAGIC);
422 MPASS(ts->ts_owner == NULL);
423 ts->ts_owner = owner;
424 LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
425 }
426
427 /*
428 * Malloc a turnstile for a new thread, initialize it and return it.
429 */
430 struct turnstile *
431 turnstile_alloc(void)
432 {
433 struct turnstile *ts;
434
435 ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
436 TAILQ_INIT(&ts->ts_blocked);
437 TAILQ_INIT(&ts->ts_pending);
438 LIST_INIT(&ts->ts_free);
439 return (ts);
440 }
441
442 /*
443 * Free a turnstile when a thread is destroyed.
444 */
445 void
446 turnstile_free(struct turnstile *ts)
447 {
448
449 MPASS(ts != NULL);
450 MPASS(TAILQ_EMPTY(&ts->ts_blocked));
451 MPASS(TAILQ_EMPTY(&ts->ts_pending));
452 free(ts, M_TURNSTILE);
453 }
454
455 /*
456 * Lock the turnstile chain associated with the specified lock.
457 */
458 void
459 turnstile_lock(struct lock_object *lock)
460 {
461 struct turnstile_chain *tc;
462
463 tc = TC_LOOKUP(lock);
464 mtx_lock_spin(&tc->tc_lock);
465 }
466
467 /*
468 * Look up the turnstile for a lock in the hash table locking the associated
469 * turnstile chain along the way. If no turnstile is found in the hash
470 * table, NULL is returned.
471 */
472 struct turnstile *
473 turnstile_lookup(struct lock_object *lock)
474 {
475 struct turnstile_chain *tc;
476 struct turnstile *ts;
477
478 tc = TC_LOOKUP(lock);
479 mtx_assert(&tc->tc_lock, MA_OWNED);
480 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
481 if (ts->ts_lockobj == lock)
482 return (ts);
483 return (NULL);
484 }
485
486 /*
487 * Unlock the turnstile chain associated with a given lock.
488 */
489 void
490 turnstile_release(struct lock_object *lock)
491 {
492 struct turnstile_chain *tc;
493
494 tc = TC_LOOKUP(lock);
495 mtx_unlock_spin(&tc->tc_lock);
496 }
497
498 /*
499 * Take ownership of a turnstile and adjust the priority of the new
500 * owner appropriately.
501 */
502 void
503 turnstile_claim(struct lock_object *lock)
504 {
505 struct turnstile_chain *tc;
506 struct turnstile *ts;
507 struct thread *td, *owner;
508
509 tc = TC_LOOKUP(lock);
510 mtx_assert(&tc->tc_lock, MA_OWNED);
511 ts = turnstile_lookup(lock);
512 MPASS(ts != NULL);
513
514 owner = curthread;
515 mtx_lock_spin(&td_contested_lock);
516 turnstile_setowner(ts, owner);
517 mtx_unlock_spin(&td_contested_lock);
518
519 td = TAILQ_FIRST(&ts->ts_blocked);
520 MPASS(td != NULL);
521 MPASS(td->td_proc->p_magic == P_MAGIC);
522 mtx_unlock_spin(&tc->tc_lock);
523
524 /*
525 * Update the priority of the new owner if needed.
526 */
527 mtx_lock_spin(&sched_lock);
528 if (td->td_priority < owner->td_priority)
529 sched_lend_prio(owner, td->td_priority);
530 mtx_unlock_spin(&sched_lock);
531 }
532
533 /*
534 * Block the current thread on the turnstile assicated with 'lock'. This
535 * function will context switch and not return until this thread has been
536 * woken back up. This function must be called with the appropriate
537 * turnstile chain locked and will return with it unlocked.
538 */
539 void
540 turnstile_wait(struct lock_object *lock, struct thread *owner)
541 {
542 struct turnstile_chain *tc;
543 struct turnstile *ts;
544 struct thread *td, *td1;
545
546 td = curthread;
547 tc = TC_LOOKUP(lock);
548 mtx_assert(&tc->tc_lock, MA_OWNED);
549 MPASS(td->td_turnstile != NULL);
550 MPASS(owner != NULL);
551 MPASS(owner->td_proc->p_magic == P_MAGIC);
552
553 /* Look up the turnstile associated with the lock 'lock'. */
554 ts = turnstile_lookup(lock);
555
556 /*
557 * If the lock does not already have a turnstile, use this thread's
558 * turnstile. Otherwise insert the current thread into the
559 * turnstile already in use by this lock.
560 */
561 if (ts == NULL) {
562 #ifdef TURNSTILE_PROFILING
563 tc->tc_depth++;
564 if (tc->tc_depth > tc->tc_max_depth) {
565 tc->tc_max_depth = tc->tc_depth;
566 if (tc->tc_max_depth > turnstile_max_depth)
567 turnstile_max_depth = tc->tc_max_depth;
568 }
569 #endif
570 ts = td->td_turnstile;
571 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
572 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
573 ("thread's turnstile has pending threads"));
574 KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
575 ("thread's turnstile has a non-empty queue"));
576 KASSERT(LIST_EMPTY(&ts->ts_free),
577 ("thread's turnstile has a non-empty free list"));
578 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
579 ts->ts_lockobj = lock;
580 mtx_lock_spin(&td_contested_lock);
581 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
582 turnstile_setowner(ts, owner);
583 mtx_unlock_spin(&td_contested_lock);
584 } else {
585 TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
586 if (td1->td_priority > td->td_priority)
587 break;
588 mtx_lock_spin(&td_contested_lock);
589 if (td1 != NULL)
590 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
591 else
592 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
593 mtx_unlock_spin(&td_contested_lock);
594 MPASS(td->td_turnstile != NULL);
595 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
596 MPASS(owner == ts->ts_owner);
597 }
598 td->td_turnstile = NULL;
599 mtx_unlock_spin(&tc->tc_lock);
600
601 mtx_lock_spin(&sched_lock);
602 /*
603 * Handle race condition where a thread on another CPU that owns
604 * lock 'lock' could have woken us in between us dropping the
605 * turnstile chain lock and acquiring the sched_lock.
606 */
607 if (td->td_flags & TDF_TSNOBLOCK) {
608 td->td_flags &= ~TDF_TSNOBLOCK;
609 mtx_unlock_spin(&sched_lock);
610 return;
611 }
612
613 #ifdef notyet
614 /*
615 * If we're borrowing an interrupted thread's VM context, we
616 * must clean up before going to sleep.
617 */
618 if (td->td_ithd != NULL) {
619 struct ithd *it = td->td_ithd;
620
621 if (it->it_interrupted) {
622 if (LOCK_LOG_TEST(lock, 0))
623 CTR3(KTR_LOCK, "%s: %p interrupted %p",
624 __func__, it, it->it_interrupted);
625 intr_thd_fixup(it);
626 }
627 }
628 #endif
629
630 /* Save who we are blocked on and switch. */
631 td->td_blocked = ts;
632 td->td_lockname = lock->lo_name;
633 TD_SET_LOCK(td);
634 propagate_priority(td);
635
636 if (LOCK_LOG_TEST(lock, 0))
637 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
638 td->td_tid, lock, lock->lo_name);
639
640 mi_switch(SW_VOL, NULL);
641
642 if (LOCK_LOG_TEST(lock, 0))
643 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
644 __func__, td->td_tid, lock, lock->lo_name);
645
646 mtx_unlock_spin(&sched_lock);
647 }
648
649 /*
650 * Pick the highest priority thread on this turnstile and put it on the
651 * pending list. This must be called with the turnstile chain locked.
652 */
653 int
654 turnstile_signal(struct turnstile *ts)
655 {
656 struct turnstile_chain *tc;
657 struct thread *td;
658 int empty;
659
660 MPASS(ts != NULL);
661 MPASS(curthread->td_proc->p_magic == P_MAGIC);
662 MPASS(ts->ts_owner == curthread);
663 tc = TC_LOOKUP(ts->ts_lockobj);
664 mtx_assert(&tc->tc_lock, MA_OWNED);
665
666 /*
667 * Pick the highest priority thread blocked on this lock and
668 * move it to the pending list.
669 */
670 td = TAILQ_FIRST(&ts->ts_blocked);
671 MPASS(td->td_proc->p_magic == P_MAGIC);
672 mtx_lock_spin(&td_contested_lock);
673 TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
674 mtx_unlock_spin(&td_contested_lock);
675 TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
676
677 /*
678 * If the turnstile is now empty, remove it from its chain and
679 * give it to the about-to-be-woken thread. Otherwise take a
680 * turnstile from the free list and give it to the thread.
681 */
682 empty = TAILQ_EMPTY(&ts->ts_blocked);
683 if (empty) {
684 MPASS(LIST_EMPTY(&ts->ts_free));
685 #ifdef TURNSTILE_PROFILING
686 tc->tc_depth--;
687 #endif
688 } else
689 ts = LIST_FIRST(&ts->ts_free);
690 MPASS(ts != NULL);
691 LIST_REMOVE(ts, ts_hash);
692 td->td_turnstile = ts;
693
694 return (empty);
695 }
696
697 /*
698 * Put all blocked threads on the pending list. This must be called with
699 * the turnstile chain locked.
700 */
701 void
702 turnstile_broadcast(struct turnstile *ts)
703 {
704 struct turnstile_chain *tc;
705 struct turnstile *ts1;
706 struct thread *td;
707
708 MPASS(ts != NULL);
709 MPASS(curthread->td_proc->p_magic == P_MAGIC);
710 MPASS(ts->ts_owner == curthread);
711 tc = TC_LOOKUP(ts->ts_lockobj);
712 mtx_assert(&tc->tc_lock, MA_OWNED);
713
714 /*
715 * Transfer the blocked list to the pending list.
716 */
717 mtx_lock_spin(&td_contested_lock);
718 TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
719 mtx_unlock_spin(&td_contested_lock);
720
721 /*
722 * Give a turnstile to each thread. The last thread gets
723 * this turnstile.
724 */
725 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
726 if (LIST_EMPTY(&ts->ts_free)) {
727 MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
728 ts1 = ts;
729 #ifdef TURNSTILE_PROFILING
730 tc->tc_depth--;
731 #endif
732 } else
733 ts1 = LIST_FIRST(&ts->ts_free);
734 MPASS(ts1 != NULL);
735 LIST_REMOVE(ts1, ts_hash);
736 td->td_turnstile = ts1;
737 }
738 }
739
740 /*
741 * Wakeup all threads on the pending list and adjust the priority of the
742 * current thread appropriately. This must be called with the turnstile
743 * chain locked.
744 */
745 void
746 turnstile_unpend(struct turnstile *ts)
747 {
748 TAILQ_HEAD( ,thread) pending_threads;
749 struct turnstile_chain *tc;
750 struct thread *td;
751 u_char cp, pri;
752
753 MPASS(ts != NULL);
754 MPASS(ts->ts_owner == curthread);
755 tc = TC_LOOKUP(ts->ts_lockobj);
756 mtx_assert(&tc->tc_lock, MA_OWNED);
757 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
758
759 /*
760 * Move the list of pending threads out of the turnstile and
761 * into a local variable.
762 */
763 TAILQ_INIT(&pending_threads);
764 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
765 #ifdef INVARIANTS
766 if (TAILQ_EMPTY(&ts->ts_blocked))
767 ts->ts_lockobj = NULL;
768 #endif
769
770 /*
771 * Remove the turnstile from this thread's list of contested locks
772 * since this thread doesn't own it anymore. New threads will
773 * not be blocking on the turnstile until it is claimed by a new
774 * owner.
775 */
776 mtx_lock_spin(&td_contested_lock);
777 ts->ts_owner = NULL;
778 LIST_REMOVE(ts, ts_link);
779 mtx_unlock_spin(&td_contested_lock);
780 critical_enter();
781 mtx_unlock_spin(&tc->tc_lock);
782
783 /*
784 * Adjust the priority of curthread based on other contested
785 * locks it owns. Don't lower the priority below the base
786 * priority however.
787 */
788 td = curthread;
789 pri = PRI_MAX;
790 mtx_lock_spin(&sched_lock);
791 mtx_lock_spin(&td_contested_lock);
792 LIST_FOREACH(ts, &td->td_contested, ts_link) {
793 cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
794 if (cp < pri)
795 pri = cp;
796 }
797 mtx_unlock_spin(&td_contested_lock);
798 sched_unlend_prio(td, pri);
799
800 /*
801 * Wake up all the pending threads. If a thread is not blocked
802 * on a lock, then it is currently executing on another CPU in
803 * turnstile_wait() or sitting on a run queue waiting to resume
804 * in turnstile_wait(). Set a flag to force it to try to acquire
805 * the lock again instead of blocking.
806 */
807 while (!TAILQ_EMPTY(&pending_threads)) {
808 td = TAILQ_FIRST(&pending_threads);
809 TAILQ_REMOVE(&pending_threads, td, td_lockq);
810 MPASS(td->td_proc->p_magic == P_MAGIC);
811 if (TD_ON_LOCK(td)) {
812 td->td_blocked = NULL;
813 td->td_lockname = NULL;
814 TD_CLR_LOCK(td);
815 MPASS(TD_CAN_RUN(td));
816 setrunqueue(td, SRQ_BORING);
817 } else {
818 td->td_flags |= TDF_TSNOBLOCK;
819 MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
820 }
821 }
822 critical_exit();
823 mtx_unlock_spin(&sched_lock);
824 }
825
826 /*
827 * Return the first thread in a turnstile.
828 */
829 struct thread *
830 turnstile_head(struct turnstile *ts)
831 {
832 #ifdef INVARIANTS
833 struct turnstile_chain *tc;
834
835 MPASS(ts != NULL);
836 tc = TC_LOOKUP(ts->ts_lockobj);
837 mtx_assert(&tc->tc_lock, MA_OWNED);
838 #endif
839 return (TAILQ_FIRST(&ts->ts_blocked));
840 }
841
842 /*
843 * Returns true if a turnstile is empty.
844 */
845 int
846 turnstile_empty(struct turnstile *ts)
847 {
848 #ifdef INVARIANTS
849 struct turnstile_chain *tc;
850
851 MPASS(ts != NULL);
852 tc = TC_LOOKUP(ts->ts_lockobj);
853 mtx_assert(&tc->tc_lock, MA_OWNED);
854 #endif
855 return (TAILQ_EMPTY(&ts->ts_blocked));
856 }
Cache object: 90cc078283ad9eb11a0e3a11c344cacd
|