The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_turnstile.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    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 allocated from a UMA zone
   50  * and attached to that thread.  When a thread blocks on a lock, if it is the
   51  * first thread to block, it lends its turnstile to the lock.  If the lock
   52  * already has a turnstile, then it gives its turnstile to the lock's
   53  * turnstile's free list.  When a thread is woken up, it takes a turnstile from
   54  * the free list if there are any other waiters.  If it is the only thread
   55  * blocked on the lock, then it reclaims the turnstile associated with the lock
   56  * and removes it from the hash table.
   57  */
   58 
   59 #include <sys/cdefs.h>
   60 __FBSDID("$FreeBSD$");
   61 
   62 #include "opt_ddb.h"
   63 #include "opt_turnstile_profiling.h"
   64 #include "opt_sched.h"
   65 
   66 #include <sys/param.h>
   67 #include <sys/systm.h>
   68 #include <sys/kernel.h>
   69 #include <sys/ktr.h>
   70 #include <sys/lock.h>
   71 #include <sys/mutex.h>
   72 #include <sys/proc.h>
   73 #include <sys/queue.h>
   74 #include <sys/sched.h>
   75 #include <sys/sysctl.h>
   76 #include <sys/turnstile.h>
   77 
   78 #include <vm/uma.h>
   79 
   80 #ifdef DDB
   81 #include <sys/kdb.h>
   82 #include <ddb/ddb.h>
   83 #include <sys/lockmgr.h>
   84 #include <sys/sx.h>
   85 #endif
   86 
   87 /*
   88  * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
   89  * number chosen because the sleep queue's use the same value for the
   90  * shift.  Basically, we ignore the lower 8 bits of the address.
   91  * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
   92  */
   93 #define TC_TABLESIZE    128                     /* Must be power of 2. */
   94 #define TC_MASK         (TC_TABLESIZE - 1)
   95 #define TC_SHIFT        8
   96 #define TC_HASH(lock)   (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
   97 #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)]
   98 
   99 /*
  100  * There are three different lists of turnstiles as follows.  The list
  101  * connected by ts_link entries is a per-thread list of all the turnstiles
  102  * attached to locks that we own.  This is used to fixup our priority when
  103  * a lock is released.  The other two lists use the ts_hash entries.  The
  104  * first of these two is the turnstile chain list that a turnstile is on
  105  * when it is attached to a lock.  The second list to use ts_hash is the
  106  * free list hung off of a turnstile that is attached to a lock.
  107  *
  108  * Each turnstile contains three lists of threads.  The two ts_blocked lists
  109  * are linked list of threads blocked on the turnstile's lock.  One list is
  110  * for exclusive waiters, and the other is for shared waiters.  The
  111  * ts_pending list is a linked list of threads previously awakened by
  112  * turnstile_signal() or turnstile_wait() that are waiting to be put on
  113  * the run queue.
  114  *
  115  * Locking key:
  116  *  c - turnstile chain lock
  117  *  q - td_contested lock
  118  */
  119 struct turnstile {
  120         struct mtx ts_lock;                     /* Spin lock for self. */
  121         struct threadqueue ts_blocked[2];       /* (c + q) Blocked threads. */
  122         struct threadqueue ts_pending;          /* (c) Pending threads. */
  123         LIST_ENTRY(turnstile) ts_hash;          /* (c) Chain and free list. */
  124         LIST_ENTRY(turnstile) ts_link;          /* (q) Contested locks. */
  125         LIST_HEAD(, turnstile) ts_free;         /* (c) Free turnstiles. */
  126         struct lock_object *ts_lockobj;         /* (c) Lock we reference. */
  127         struct thread *ts_owner;                /* (c + q) Who owns the lock. */
  128 };
  129 
  130 struct turnstile_chain {
  131         LIST_HEAD(, turnstile) tc_turnstiles;   /* List of turnstiles. */
  132         struct mtx tc_lock;                     /* Spin lock for this chain. */
  133 #ifdef TURNSTILE_PROFILING
  134         u_int   tc_depth;                       /* Length of tc_queues. */
  135         u_int   tc_max_depth;                   /* Max length of tc_queues. */
  136 #endif
  137 };
  138 
  139 #ifdef TURNSTILE_PROFILING
  140 u_int turnstile_max_depth;
  141 SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
  142 SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
  143     "turnstile chain stats");
  144 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
  145     &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
  146 #endif
  147 static struct mtx td_contested_lock;
  148 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
  149 static uma_zone_t turnstile_zone;
  150 
  151 /*
  152  * Prototypes for non-exported routines.
  153  */
  154 static void     init_turnstile0(void *dummy);
  155 #ifdef TURNSTILE_PROFILING
  156 static void     init_turnstile_profiling(void *arg);
  157 #endif
  158 static void     propagate_priority(struct thread *td);
  159 static int      turnstile_adjust_thread(struct turnstile *ts,
  160                     struct thread *td);
  161 static struct thread *turnstile_first_waiter(struct turnstile *ts);
  162 static void     turnstile_setowner(struct turnstile *ts, struct thread *owner);
  163 #ifdef INVARIANTS
  164 static void     turnstile_dtor(void *mem, int size, void *arg);
  165 #endif
  166 static int      turnstile_init(void *mem, int size, int flags);
  167 static void     turnstile_fini(void *mem, int size);
  168 
  169 /*
  170  * Walks the chain of turnstiles and their owners to propagate the priority
  171  * of the thread being blocked to all the threads holding locks that have to
  172  * release their locks before this thread can run again.
  173  */
  174 static void
  175 propagate_priority(struct thread *td)
  176 {
  177         struct turnstile *ts;
  178         int pri;
  179 
  180         THREAD_LOCK_ASSERT(td, MA_OWNED);
  181         pri = td->td_priority;
  182         ts = td->td_blocked;
  183         MPASS(td->td_lock == &ts->ts_lock);
  184         /*
  185          * Grab a recursive lock on this turnstile chain so it stays locked
  186          * for the whole operation.  The caller expects us to return with
  187          * the original lock held.  We only ever lock down the chain so
  188          * the lock order is constant.
  189          */
  190         mtx_lock_spin(&ts->ts_lock);
  191         for (;;) {
  192                 td = ts->ts_owner;
  193 
  194                 if (td == NULL) {
  195                         /*
  196                          * This might be a read lock with no owner.  There's
  197                          * not much we can do, so just bail.
  198                          */
  199                         mtx_unlock_spin(&ts->ts_lock);
  200                         return;
  201                 }
  202 
  203                 thread_lock_flags(td, MTX_DUPOK);
  204                 mtx_unlock_spin(&ts->ts_lock);
  205                 MPASS(td->td_proc != NULL);
  206                 MPASS(td->td_proc->p_magic == P_MAGIC);
  207 
  208                 /*
  209                  * If the thread is asleep, then we are probably about
  210                  * to deadlock.  To make debugging this easier, just
  211                  * panic and tell the user which thread misbehaved so
  212                  * they can hopefully get a stack trace from the truly
  213                  * misbehaving thread.
  214                  */
  215                 if (TD_IS_SLEEPING(td)) {
  216                         printf(
  217                 "Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
  218                             td->td_tid, td->td_proc->p_pid);
  219 #ifdef DDB
  220                         db_trace_thread(td, -1);
  221 #endif
  222                         panic("sleeping thread");
  223                 }
  224 
  225                 /*
  226                  * If this thread already has higher priority than the
  227                  * thread that is being blocked, we are finished.
  228                  */
  229                 if (td->td_priority <= pri) {
  230                         thread_unlock(td);
  231                         return;
  232                 }
  233 
  234                 /*
  235                  * Bump this thread's priority.
  236                  */
  237                 sched_lend_prio(td, pri);
  238 
  239                 /*
  240                  * If lock holder is actually running or on the run queue
  241                  * then we are done.
  242                  */
  243                 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
  244                         MPASS(td->td_blocked == NULL);
  245                         thread_unlock(td);
  246                         return;
  247                 }
  248 
  249 #ifndef SMP
  250                 /*
  251                  * For UP, we check to see if td is curthread (this shouldn't
  252                  * ever happen however as it would mean we are in a deadlock.)
  253                  */
  254                 KASSERT(td != curthread, ("Deadlock detected"));
  255 #endif
  256 
  257                 /*
  258                  * If we aren't blocked on a lock, we should be.
  259                  */
  260                 KASSERT(TD_ON_LOCK(td), (
  261                     "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
  262                     td->td_tid, td->td_proc->p_comm, td->td_state,
  263                     ts->ts_lockobj->lo_name));
  264 
  265                 /*
  266                  * Pick up the lock that td is blocked on.
  267                  */
  268                 ts = td->td_blocked;
  269                 MPASS(ts != NULL);
  270                 MPASS(td->td_lock == &ts->ts_lock);
  271                 /* Resort td on the list if needed. */
  272                 if (!turnstile_adjust_thread(ts, td)) {
  273                         mtx_unlock_spin(&ts->ts_lock);
  274                         return;
  275                 }
  276                 /* The thread lock is released as ts lock above. */
  277         }
  278 }
  279 
  280 /*
  281  * Adjust the thread's position on a turnstile after its priority has been
  282  * changed.
  283  */
  284 static int
  285 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
  286 {
  287         struct thread *td1, *td2;
  288         int queue;
  289 
  290         THREAD_LOCK_ASSERT(td, MA_OWNED);
  291         MPASS(TD_ON_LOCK(td));
  292 
  293         /*
  294          * This thread may not be blocked on this turnstile anymore
  295          * but instead might already be woken up on another CPU
  296          * that is waiting on the thread lock in turnstile_unpend() to
  297          * finish waking this thread up.  We can detect this case
  298          * by checking to see if this thread has been given a
  299          * turnstile by either turnstile_signal() or
  300          * turnstile_broadcast().  In this case, treat the thread as
  301          * if it was already running.
  302          */
  303         if (td->td_turnstile != NULL)
  304                 return (0);
  305 
  306         /*
  307          * Check if the thread needs to be moved on the blocked chain.
  308          * It needs to be moved if either its priority is lower than
  309          * the previous thread or higher than the next thread.
  310          */
  311         MPASS(td->td_lock == &ts->ts_lock);
  312         td1 = TAILQ_PREV(td, threadqueue, td_lockq);
  313         td2 = TAILQ_NEXT(td, td_lockq);
  314         if ((td1 != NULL && td->td_priority < td1->td_priority) ||
  315             (td2 != NULL && td->td_priority > td2->td_priority)) {
  316 
  317                 /*
  318                  * Remove thread from blocked chain and determine where
  319                  * it should be moved to.
  320                  */
  321                 queue = td->td_tsqueue;
  322                 MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
  323                 mtx_lock_spin(&td_contested_lock);
  324                 TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
  325                 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
  326                         MPASS(td1->td_proc->p_magic == P_MAGIC);
  327                         if (td1->td_priority > td->td_priority)
  328                                 break;
  329                 }
  330 
  331                 if (td1 == NULL)
  332                         TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
  333                 else
  334                         TAILQ_INSERT_BEFORE(td1, td, td_lockq);
  335                 mtx_unlock_spin(&td_contested_lock);
  336                 if (td1 == NULL)
  337                         CTR3(KTR_LOCK,
  338                     "turnstile_adjust_thread: td %d put at tail on [%p] %s",
  339                             td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
  340                 else
  341                         CTR4(KTR_LOCK,
  342                     "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
  343                             td->td_tid, td1->td_tid, ts->ts_lockobj,
  344                             ts->ts_lockobj->lo_name);
  345         }
  346         return (1);
  347 }
  348 
  349 /*
  350  * Early initialization of turnstiles.  This is not done via a SYSINIT()
  351  * since this needs to be initialized very early when mutexes are first
  352  * initialized.
  353  */
  354 void
  355 init_turnstiles(void)
  356 {
  357         int i;
  358 
  359         for (i = 0; i < TC_TABLESIZE; i++) {
  360                 LIST_INIT(&turnstile_chains[i].tc_turnstiles);
  361                 mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
  362                     NULL, MTX_SPIN);
  363         }
  364         mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
  365         LIST_INIT(&thread0.td_contested);
  366         thread0.td_turnstile = NULL;
  367 }
  368 
  369 #ifdef TURNSTILE_PROFILING
  370 static void
  371 init_turnstile_profiling(void *arg)
  372 {
  373         struct sysctl_oid *chain_oid;
  374         char chain_name[10];
  375         int i;
  376 
  377         for (i = 0; i < TC_TABLESIZE; i++) {
  378                 snprintf(chain_name, sizeof(chain_name), "%d", i);
  379                 chain_oid = SYSCTL_ADD_NODE(NULL, 
  380                     SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
  381                     chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
  382                 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
  383                     "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
  384                     NULL);
  385                 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
  386                     "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
  387                     0, NULL);
  388         }
  389 }
  390 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
  391     init_turnstile_profiling, NULL);
  392 #endif
  393 
  394 static void
  395 init_turnstile0(void *dummy)
  396 {
  397 
  398         turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
  399             NULL,
  400 #ifdef INVARIANTS
  401             turnstile_dtor,
  402 #else
  403             NULL,
  404 #endif
  405             turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
  406         thread0.td_turnstile = turnstile_alloc();
  407 }
  408 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
  409 
  410 /*
  411  * Update a thread on the turnstile list after it's priority has been changed.
  412  * The old priority is passed in as an argument.
  413  */
  414 void
  415 turnstile_adjust(struct thread *td, u_char oldpri)
  416 {
  417         struct turnstile *ts;
  418 
  419         MPASS(TD_ON_LOCK(td));
  420 
  421         /*
  422          * Pick up the lock that td is blocked on.
  423          */
  424         ts = td->td_blocked;
  425         MPASS(ts != NULL);
  426         MPASS(td->td_lock == &ts->ts_lock);
  427         mtx_assert(&ts->ts_lock, MA_OWNED);
  428 
  429         /* Resort the turnstile on the list. */
  430         if (!turnstile_adjust_thread(ts, td))
  431                 return;
  432         /*
  433          * If our priority was lowered and we are at the head of the
  434          * turnstile, then propagate our new priority up the chain.
  435          * Note that we currently don't try to revoke lent priorities
  436          * when our priority goes up.
  437          */
  438         MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
  439             td->td_tsqueue == TS_SHARED_QUEUE);
  440         if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
  441             td->td_priority < oldpri) {
  442                 propagate_priority(td);
  443         }
  444 }
  445 
  446 /*
  447  * Set the owner of the lock this turnstile is attached to.
  448  */
  449 static void
  450 turnstile_setowner(struct turnstile *ts, struct thread *owner)
  451 {
  452 
  453         mtx_assert(&td_contested_lock, MA_OWNED);
  454         MPASS(ts->ts_owner == NULL);
  455 
  456         /* A shared lock might not have an owner. */
  457         if (owner == NULL)
  458                 return;
  459 
  460         MPASS(owner->td_proc->p_magic == P_MAGIC);
  461         ts->ts_owner = owner;
  462         LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
  463 }
  464 
  465 #ifdef INVARIANTS
  466 /*
  467  * UMA zone item deallocator.
  468  */
  469 static void
  470 turnstile_dtor(void *mem, int size, void *arg)
  471 {
  472         struct turnstile *ts;
  473 
  474         ts = mem;
  475         MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
  476         MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
  477         MPASS(TAILQ_EMPTY(&ts->ts_pending));
  478 }
  479 #endif
  480 
  481 /*
  482  * UMA zone item initializer.
  483  */
  484 static int
  485 turnstile_init(void *mem, int size, int flags)
  486 {
  487         struct turnstile *ts;
  488 
  489         bzero(mem, size);
  490         ts = mem;
  491         TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
  492         TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
  493         TAILQ_INIT(&ts->ts_pending);
  494         LIST_INIT(&ts->ts_free);
  495         mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE);
  496         return (0);
  497 }
  498 
  499 static void
  500 turnstile_fini(void *mem, int size)
  501 {
  502         struct turnstile *ts;
  503 
  504         ts = mem;
  505         mtx_destroy(&ts->ts_lock);
  506 }
  507 
  508 /*
  509  * Get a turnstile for a new thread.
  510  */
  511 struct turnstile *
  512 turnstile_alloc(void)
  513 {
  514 
  515         return (uma_zalloc(turnstile_zone, M_WAITOK));
  516 }
  517 
  518 /*
  519  * Free a turnstile when a thread is destroyed.
  520  */
  521 void
  522 turnstile_free(struct turnstile *ts)
  523 {
  524 
  525         uma_zfree(turnstile_zone, ts);
  526 }
  527 
  528 /*
  529  * Lock the turnstile chain associated with the specified lock.
  530  */
  531 void
  532 turnstile_chain_lock(struct lock_object *lock)
  533 {
  534         struct turnstile_chain *tc;
  535 
  536         tc = TC_LOOKUP(lock);
  537         mtx_lock_spin(&tc->tc_lock);
  538 }
  539 
  540 struct turnstile *
  541 turnstile_trywait(struct lock_object *lock)
  542 {
  543         struct turnstile_chain *tc;
  544         struct turnstile *ts;
  545 
  546         tc = TC_LOOKUP(lock);
  547         mtx_lock_spin(&tc->tc_lock);
  548         LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
  549                 if (ts->ts_lockobj == lock) {
  550                         mtx_lock_spin(&ts->ts_lock);
  551                         return (ts);
  552                 }
  553 
  554         ts = curthread->td_turnstile;
  555         MPASS(ts != NULL);
  556         mtx_lock_spin(&ts->ts_lock);
  557         KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
  558         ts->ts_lockobj = lock;
  559 
  560         return (ts);
  561 }
  562 
  563 void
  564 turnstile_cancel(struct turnstile *ts)
  565 {
  566         struct turnstile_chain *tc;
  567         struct lock_object *lock;
  568 
  569         mtx_assert(&ts->ts_lock, MA_OWNED);
  570 
  571         mtx_unlock_spin(&ts->ts_lock);
  572         lock = ts->ts_lockobj;
  573         if (ts == curthread->td_turnstile)
  574                 ts->ts_lockobj = NULL;
  575         tc = TC_LOOKUP(lock);
  576         mtx_unlock_spin(&tc->tc_lock);
  577 }
  578 
  579 /*
  580  * Look up the turnstile for a lock in the hash table locking the associated
  581  * turnstile chain along the way.  If no turnstile is found in the hash
  582  * table, NULL is returned.
  583  */
  584 struct turnstile *
  585 turnstile_lookup(struct lock_object *lock)
  586 {
  587         struct turnstile_chain *tc;
  588         struct turnstile *ts;
  589 
  590         tc = TC_LOOKUP(lock);
  591         mtx_assert(&tc->tc_lock, MA_OWNED);
  592         LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
  593                 if (ts->ts_lockobj == lock) {
  594                         mtx_lock_spin(&ts->ts_lock);
  595                         return (ts);
  596                 }
  597         return (NULL);
  598 }
  599 
  600 /*
  601  * Unlock the turnstile chain associated with a given lock.
  602  */
  603 void
  604 turnstile_chain_unlock(struct lock_object *lock)
  605 {
  606         struct turnstile_chain *tc;
  607 
  608         tc = TC_LOOKUP(lock);
  609         mtx_unlock_spin(&tc->tc_lock);
  610 }
  611 
  612 /*
  613  * Return a pointer to the thread waiting on this turnstile with the
  614  * most important priority or NULL if the turnstile has no waiters.
  615  */
  616 static struct thread *
  617 turnstile_first_waiter(struct turnstile *ts)
  618 {
  619         struct thread *std, *xtd;
  620 
  621         std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
  622         xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
  623         if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
  624                 return (std);
  625         return (xtd);
  626 }
  627 
  628 /*
  629  * Take ownership of a turnstile and adjust the priority of the new
  630  * owner appropriately.
  631  */
  632 void
  633 turnstile_claim(struct turnstile *ts)
  634 {
  635         struct thread *td, *owner;
  636         struct turnstile_chain *tc;
  637 
  638         mtx_assert(&ts->ts_lock, MA_OWNED);
  639         MPASS(ts != curthread->td_turnstile);
  640 
  641         owner = curthread;
  642         mtx_lock_spin(&td_contested_lock);
  643         turnstile_setowner(ts, owner);
  644         mtx_unlock_spin(&td_contested_lock);
  645 
  646         td = turnstile_first_waiter(ts);
  647         MPASS(td != NULL);
  648         MPASS(td->td_proc->p_magic == P_MAGIC);
  649         MPASS(td->td_lock == &ts->ts_lock);
  650 
  651         /*
  652          * Update the priority of the new owner if needed.
  653          */
  654         thread_lock(owner);
  655         if (td->td_priority < owner->td_priority)
  656                 sched_lend_prio(owner, td->td_priority);
  657         thread_unlock(owner);
  658         tc = TC_LOOKUP(ts->ts_lockobj);
  659         mtx_unlock_spin(&ts->ts_lock);
  660         mtx_unlock_spin(&tc->tc_lock);
  661 }
  662 
  663 /*
  664  * Block the current thread on the turnstile assicated with 'lock'.  This
  665  * function will context switch and not return until this thread has been
  666  * woken back up.  This function must be called with the appropriate
  667  * turnstile chain locked and will return with it unlocked.
  668  */
  669 void
  670 turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
  671 {
  672         struct turnstile_chain *tc;
  673         struct thread *td, *td1;
  674         struct lock_object *lock;
  675 
  676         td = curthread;
  677         mtx_assert(&ts->ts_lock, MA_OWNED);
  678         if (queue == TS_SHARED_QUEUE)
  679                 MPASS(owner != NULL);
  680         if (owner)
  681                 MPASS(owner->td_proc->p_magic == P_MAGIC);
  682         MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
  683 
  684         /*
  685          * If the lock does not already have a turnstile, use this thread's
  686          * turnstile.  Otherwise insert the current thread into the
  687          * turnstile already in use by this lock.
  688          */
  689         tc = TC_LOOKUP(ts->ts_lockobj);
  690         if (ts == td->td_turnstile) {
  691         mtx_assert(&tc->tc_lock, MA_OWNED);
  692 #ifdef TURNSTILE_PROFILING
  693                 tc->tc_depth++;
  694                 if (tc->tc_depth > tc->tc_max_depth) {
  695                         tc->tc_max_depth = tc->tc_depth;
  696                         if (tc->tc_max_depth > turnstile_max_depth)
  697                                 turnstile_max_depth = tc->tc_max_depth;
  698                 }
  699 #endif
  700                 tc = TC_LOOKUP(ts->ts_lockobj);
  701                 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
  702                 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
  703                     ("thread's turnstile has pending threads"));
  704                 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
  705                     ("thread's turnstile has exclusive waiters"));
  706                 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
  707                     ("thread's turnstile has shared waiters"));
  708                 KASSERT(LIST_EMPTY(&ts->ts_free),
  709                     ("thread's turnstile has a non-empty free list"));
  710                 MPASS(ts->ts_lockobj != NULL);
  711                 mtx_lock_spin(&td_contested_lock);
  712                 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
  713                 turnstile_setowner(ts, owner);
  714                 mtx_unlock_spin(&td_contested_lock);
  715         } else {
  716                 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
  717                         if (td1->td_priority > td->td_priority)
  718                                 break;
  719                 mtx_lock_spin(&td_contested_lock);
  720                 if (td1 != NULL)
  721                         TAILQ_INSERT_BEFORE(td1, td, td_lockq);
  722                 else
  723                         TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
  724                 MPASS(owner == ts->ts_owner);
  725                 mtx_unlock_spin(&td_contested_lock);
  726                 MPASS(td->td_turnstile != NULL);
  727                 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
  728         }
  729         thread_lock(td);
  730         thread_lock_set(td, &ts->ts_lock);
  731         td->td_turnstile = NULL;
  732 
  733         /* Save who we are blocked on and switch. */
  734         lock = ts->ts_lockobj;
  735         td->td_tsqueue = queue;
  736         td->td_blocked = ts;
  737         td->td_lockname = lock->lo_name;
  738         TD_SET_LOCK(td);
  739         mtx_unlock_spin(&tc->tc_lock);
  740         propagate_priority(td);
  741 
  742         if (LOCK_LOG_TEST(lock, 0))
  743                 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
  744                     td->td_tid, lock, lock->lo_name);
  745 
  746         MPASS(td->td_lock == &ts->ts_lock);
  747         SCHED_STAT_INC(switch_turnstile);
  748         mi_switch(SW_VOL, NULL);
  749 
  750         if (LOCK_LOG_TEST(lock, 0))
  751                 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
  752                     __func__, td->td_tid, lock, lock->lo_name);
  753         thread_unlock(td);
  754 }
  755 
  756 /*
  757  * Pick the highest priority thread on this turnstile and put it on the
  758  * pending list.  This must be called with the turnstile chain locked.
  759  */
  760 int
  761 turnstile_signal(struct turnstile *ts, int queue)
  762 {
  763         struct turnstile_chain *tc;
  764         struct thread *td;
  765         int empty;
  766 
  767         MPASS(ts != NULL);
  768         mtx_assert(&ts->ts_lock, MA_OWNED);
  769         MPASS(curthread->td_proc->p_magic == P_MAGIC);
  770         MPASS(ts->ts_owner == curthread ||
  771             (queue == TS_EXCLUSIVE_QUEUE && ts->ts_owner == NULL));
  772         MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
  773 
  774         /*
  775          * Pick the highest priority thread blocked on this lock and
  776          * move it to the pending list.
  777          */
  778         td = TAILQ_FIRST(&ts->ts_blocked[queue]);
  779         MPASS(td->td_proc->p_magic == P_MAGIC);
  780         mtx_lock_spin(&td_contested_lock);
  781         TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
  782         mtx_unlock_spin(&td_contested_lock);
  783         TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
  784 
  785         /*
  786          * If the turnstile is now empty, remove it from its chain and
  787          * give it to the about-to-be-woken thread.  Otherwise take a
  788          * turnstile from the free list and give it to the thread.
  789          */
  790         empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
  791             TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
  792         if (empty) {
  793                 tc = TC_LOOKUP(ts->ts_lockobj);
  794                 mtx_assert(&tc->tc_lock, MA_OWNED);
  795                 MPASS(LIST_EMPTY(&ts->ts_free));
  796 #ifdef TURNSTILE_PROFILING
  797                 tc->tc_depth--;
  798 #endif
  799         } else
  800                 ts = LIST_FIRST(&ts->ts_free);
  801         MPASS(ts != NULL);
  802         LIST_REMOVE(ts, ts_hash);
  803         td->td_turnstile = ts;
  804 
  805         return (empty);
  806 }
  807         
  808 /*
  809  * Put all blocked threads on the pending list.  This must be called with
  810  * the turnstile chain locked.
  811  */
  812 void
  813 turnstile_broadcast(struct turnstile *ts, int queue)
  814 {
  815         struct turnstile_chain *tc;
  816         struct turnstile *ts1;
  817         struct thread *td;
  818 
  819         MPASS(ts != NULL);
  820         mtx_assert(&ts->ts_lock, MA_OWNED);
  821         MPASS(curthread->td_proc->p_magic == P_MAGIC);
  822         MPASS(ts->ts_owner == curthread ||
  823             (queue == TS_EXCLUSIVE_QUEUE && ts->ts_owner == NULL));
  824         /*
  825          * We must have the chain locked so that we can remove the empty
  826          * turnstile from the hash queue.
  827          */
  828         tc = TC_LOOKUP(ts->ts_lockobj);
  829         mtx_assert(&tc->tc_lock, MA_OWNED);
  830         MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
  831 
  832         /*
  833          * Transfer the blocked list to the pending list.
  834          */
  835         mtx_lock_spin(&td_contested_lock);
  836         TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
  837         mtx_unlock_spin(&td_contested_lock);
  838 
  839         /*
  840          * Give a turnstile to each thread.  The last thread gets
  841          * this turnstile if the turnstile is empty.
  842          */
  843         TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
  844                 if (LIST_EMPTY(&ts->ts_free)) {
  845                         MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
  846                         ts1 = ts;
  847 #ifdef TURNSTILE_PROFILING
  848                         tc->tc_depth--;
  849 #endif
  850                 } else
  851                         ts1 = LIST_FIRST(&ts->ts_free);
  852                 MPASS(ts1 != NULL);
  853                 LIST_REMOVE(ts1, ts_hash);
  854                 td->td_turnstile = ts1;
  855         }
  856 }
  857 
  858 /*
  859  * Wakeup all threads on the pending list and adjust the priority of the
  860  * current thread appropriately.  This must be called with the turnstile
  861  * chain locked.
  862  */
  863 void
  864 turnstile_unpend(struct turnstile *ts, int owner_type)
  865 {
  866         TAILQ_HEAD( ,thread) pending_threads;
  867         struct turnstile *nts;
  868         struct thread *td;
  869         u_char cp, pri;
  870 
  871         MPASS(ts != NULL);
  872         mtx_assert(&ts->ts_lock, MA_OWNED);
  873         MPASS(ts->ts_owner == curthread ||
  874             (owner_type == TS_SHARED_LOCK && ts->ts_owner == NULL));
  875         MPASS(!TAILQ_EMPTY(&ts->ts_pending));
  876 
  877         /*
  878          * Move the list of pending threads out of the turnstile and
  879          * into a local variable.
  880          */
  881         TAILQ_INIT(&pending_threads);
  882         TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
  883 #ifdef INVARIANTS
  884         if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
  885             TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
  886                 ts->ts_lockobj = NULL;
  887 #endif
  888         /*
  889          * Adjust the priority of curthread based on other contested
  890          * locks it owns.  Don't lower the priority below the base
  891          * priority however.
  892          */
  893         td = curthread;
  894         pri = PRI_MAX;
  895         thread_lock(td);
  896         mtx_lock_spin(&td_contested_lock);
  897         /*
  898          * Remove the turnstile from this thread's list of contested locks
  899          * since this thread doesn't own it anymore.  New threads will
  900          * not be blocking on the turnstile until it is claimed by a new
  901          * owner.  There might not be a current owner if this is a shared
  902          * lock.
  903          */
  904         if (ts->ts_owner != NULL) {
  905                 ts->ts_owner = NULL;
  906                 LIST_REMOVE(ts, ts_link);
  907         }
  908         LIST_FOREACH(nts, &td->td_contested, ts_link) {
  909                 cp = turnstile_first_waiter(nts)->td_priority;
  910                 if (cp < pri)
  911                         pri = cp;
  912         }
  913         mtx_unlock_spin(&td_contested_lock);
  914         sched_unlend_prio(td, pri);
  915         thread_unlock(td);
  916         /*
  917          * Wake up all the pending threads.  If a thread is not blocked
  918          * on a lock, then it is currently executing on another CPU in
  919          * turnstile_wait() or sitting on a run queue waiting to resume
  920          * in turnstile_wait().  Set a flag to force it to try to acquire
  921          * the lock again instead of blocking.
  922          */
  923         while (!TAILQ_EMPTY(&pending_threads)) {
  924                 td = TAILQ_FIRST(&pending_threads);
  925                 TAILQ_REMOVE(&pending_threads, td, td_lockq);
  926                 thread_lock(td);
  927                 MPASS(td->td_lock == &ts->ts_lock);
  928                 MPASS(td->td_proc->p_magic == P_MAGIC);
  929                 MPASS(TD_ON_LOCK(td));
  930                 TD_CLR_LOCK(td);
  931                 MPASS(TD_CAN_RUN(td));
  932                 td->td_blocked = NULL;
  933                 td->td_lockname = NULL;
  934 #ifdef INVARIANTS
  935                 td->td_tsqueue = 0xff;
  936 #endif
  937                 sched_add(td, SRQ_BORING);
  938                 thread_unlock(td);
  939         }
  940         mtx_unlock_spin(&ts->ts_lock);
  941 }
  942 
  943 /*
  944  * Give up ownership of a turnstile.  This must be called with the
  945  * turnstile chain locked.
  946  */
  947 void
  948 turnstile_disown(struct turnstile *ts)
  949 {
  950         struct thread *td;
  951         u_char cp, pri;
  952 
  953         MPASS(ts != NULL);
  954         mtx_assert(&ts->ts_lock, MA_OWNED);
  955         MPASS(ts->ts_owner == curthread);
  956         MPASS(TAILQ_EMPTY(&ts->ts_pending));
  957         MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
  958             !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
  959 
  960         /*
  961          * Remove the turnstile from this thread's list of contested locks
  962          * since this thread doesn't own it anymore.  New threads will
  963          * not be blocking on the turnstile until it is claimed by a new
  964          * owner.
  965          */
  966         mtx_lock_spin(&td_contested_lock);
  967         ts->ts_owner = NULL;
  968         LIST_REMOVE(ts, ts_link);
  969         mtx_unlock_spin(&td_contested_lock);
  970 
  971         /*
  972          * Adjust the priority of curthread based on other contested
  973          * locks it owns.  Don't lower the priority below the base
  974          * priority however.
  975          */
  976         td = curthread;
  977         pri = PRI_MAX;
  978         thread_lock(td);
  979         mtx_unlock_spin(&ts->ts_lock);
  980         mtx_lock_spin(&td_contested_lock);
  981         LIST_FOREACH(ts, &td->td_contested, ts_link) {
  982                 cp = turnstile_first_waiter(ts)->td_priority;
  983                 if (cp < pri)
  984                         pri = cp;
  985         }
  986         mtx_unlock_spin(&td_contested_lock);
  987         sched_unlend_prio(td, pri);
  988         thread_unlock(td);
  989 }
  990 
  991 /*
  992  * Return the first thread in a turnstile.
  993  */
  994 struct thread *
  995 turnstile_head(struct turnstile *ts, int queue)
  996 {
  997 #ifdef INVARIANTS
  998 
  999         MPASS(ts != NULL);
 1000         MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
 1001         mtx_assert(&ts->ts_lock, MA_OWNED);
 1002 #endif
 1003         return (TAILQ_FIRST(&ts->ts_blocked[queue]));
 1004 }
 1005 
 1006 /*
 1007  * Returns true if a sub-queue of a turnstile is empty.
 1008  */
 1009 int
 1010 turnstile_empty(struct turnstile *ts, int queue)
 1011 {
 1012 #ifdef INVARIANTS
 1013 
 1014         MPASS(ts != NULL);
 1015         MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
 1016         mtx_assert(&ts->ts_lock, MA_OWNED);
 1017 #endif
 1018         return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
 1019 }
 1020 
 1021 #ifdef DDB
 1022 static void
 1023 print_thread(struct thread *td, const char *prefix)
 1024 {
 1025 
 1026         db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
 1027             td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
 1028             td->td_proc->p_comm);
 1029 }
 1030 
 1031 static void
 1032 print_queue(struct threadqueue *queue, const char *header, const char *prefix)
 1033 {
 1034         struct thread *td;
 1035 
 1036         db_printf("%s:\n", header);
 1037         if (TAILQ_EMPTY(queue)) {
 1038                 db_printf("%sempty\n", prefix);
 1039                 return;
 1040         }
 1041         TAILQ_FOREACH(td, queue, td_lockq) {
 1042                 print_thread(td, prefix);
 1043         }
 1044 }
 1045 
 1046 DB_SHOW_COMMAND(turnstile, db_show_turnstile)
 1047 {
 1048         struct turnstile_chain *tc;
 1049         struct turnstile *ts;
 1050         struct lock_object *lock;
 1051         int i;
 1052 
 1053         if (!have_addr)
 1054                 return;
 1055 
 1056         /*
 1057          * First, see if there is an active turnstile for the lock indicated
 1058          * by the address.
 1059          */
 1060         lock = (struct lock_object *)addr;
 1061         tc = TC_LOOKUP(lock);
 1062         LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
 1063                 if (ts->ts_lockobj == lock)
 1064                         goto found;
 1065 
 1066         /*
 1067          * Second, see if there is an active turnstile at the address
 1068          * indicated.
 1069          */
 1070         for (i = 0; i < TC_TABLESIZE; i++)
 1071                 LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
 1072                         if (ts == (struct turnstile *)addr)
 1073                                 goto found;
 1074                 }
 1075 
 1076         db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
 1077         return;
 1078 found:
 1079         lock = ts->ts_lockobj;
 1080         db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
 1081             lock->lo_name);
 1082         if (ts->ts_owner)
 1083                 print_thread(ts->ts_owner, "Lock Owner: ");
 1084         else
 1085                 db_printf("Lock Owner: none\n");
 1086         print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
 1087         print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
 1088             "\t");
 1089         print_queue(&ts->ts_pending, "Pending Threads", "\t");
 1090         
 1091 }
 1092 
 1093 /*
 1094  * Show all the threads a particular thread is waiting on based on
 1095  * non-sleepable and non-spin locks.
 1096  */
 1097 static void
 1098 print_lockchain(struct thread *td, const char *prefix)
 1099 {
 1100         struct lock_object *lock;
 1101         struct lock_class *class;
 1102         struct turnstile *ts;
 1103 
 1104         /*
 1105          * Follow the chain.  We keep walking as long as the thread is
 1106          * blocked on a turnstile that has an owner.
 1107          */
 1108         while (!db_pager_quit) {
 1109                 db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
 1110                     td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
 1111                     td->td_proc->p_comm);
 1112                 switch (td->td_state) {
 1113                 case TDS_INACTIVE:
 1114                         db_printf("is inactive\n");
 1115                         return;
 1116                 case TDS_CAN_RUN:
 1117                         db_printf("can run\n");
 1118                         return;
 1119                 case TDS_RUNQ:
 1120                         db_printf("is on a run queue\n");
 1121                         return;
 1122                 case TDS_RUNNING:
 1123                         db_printf("running on CPU %d\n", td->td_oncpu);
 1124                         return;
 1125                 case TDS_INHIBITED:
 1126                         if (TD_ON_LOCK(td)) {
 1127                                 ts = td->td_blocked;
 1128                                 lock = ts->ts_lockobj;
 1129                                 class = LOCK_CLASS(lock);
 1130                                 db_printf("blocked on lock %p (%s) \"%s\"\n",
 1131                                     lock, class->lc_name, lock->lo_name);
 1132                                 if (ts->ts_owner == NULL)
 1133                                         return;
 1134                                 td = ts->ts_owner;
 1135                                 break;
 1136                         }
 1137                         db_printf("inhibited\n");
 1138                         return;
 1139                 default:
 1140                         db_printf("??? (%#x)\n", td->td_state);
 1141                         return;
 1142                 }
 1143         }
 1144 }
 1145 
 1146 DB_SHOW_COMMAND(lockchain, db_show_lockchain)
 1147 {
 1148         struct thread *td;
 1149 
 1150         /* Figure out which thread to start with. */
 1151         if (have_addr)
 1152                 td = db_lookup_thread(addr, TRUE);
 1153         else
 1154                 td = kdb_thread;
 1155 
 1156         print_lockchain(td, "");
 1157 }
 1158 
 1159 DB_SHOW_COMMAND(allchains, db_show_allchains)
 1160 {
 1161         struct thread *td;
 1162         struct proc *p;
 1163         int i;
 1164 
 1165         i = 1;
 1166         FOREACH_PROC_IN_SYSTEM(p) {
 1167                 FOREACH_THREAD_IN_PROC(p, td) {
 1168                         if (TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) {
 1169                                 db_printf("chain %d:\n", i++);
 1170                                 print_lockchain(td, " ");
 1171                         }
 1172                         if (db_pager_quit)
 1173                                 return;
 1174                 }
 1175         }
 1176 }
 1177 
 1178 /*
 1179  * Show all the threads a particular thread is waiting on based on
 1180  * sleepable locks.
 1181  */
 1182 static void
 1183 print_sleepchain(struct thread *td, const char *prefix)
 1184 {
 1185         struct thread *owner;
 1186 
 1187         /*
 1188          * Follow the chain.  We keep walking as long as the thread is
 1189          * blocked on a sleep lock that has an owner.
 1190          */
 1191         while (!db_pager_quit) {
 1192                 db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
 1193                     td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
 1194                     td->td_proc->p_comm);
 1195                 switch (td->td_state) {
 1196                 case TDS_INACTIVE:
 1197                         db_printf("is inactive\n");
 1198                         return;
 1199                 case TDS_CAN_RUN:
 1200                         db_printf("can run\n");
 1201                         return;
 1202                 case TDS_RUNQ:
 1203                         db_printf("is on a run queue\n");
 1204                         return;
 1205                 case TDS_RUNNING:
 1206                         db_printf("running on CPU %d\n", td->td_oncpu);
 1207                         return;
 1208                 case TDS_INHIBITED:
 1209                         if (TD_ON_SLEEPQ(td)) {
 1210                                 if (lockmgr_chain(td, &owner) ||
 1211                                     sx_chain(td, &owner)) {
 1212                                         if (owner == NULL)
 1213                                                 return;
 1214                                         td = owner;
 1215                                         break;
 1216                                 }
 1217                                 db_printf("sleeping on %p \"%s\"\n",
 1218                                     td->td_wchan, td->td_wmesg);
 1219                                 return;
 1220                         }
 1221                         db_printf("inhibited\n");
 1222                         return;
 1223                 default:
 1224                         db_printf("??? (%#x)\n", td->td_state);
 1225                         return;
 1226                 }
 1227         }
 1228 }
 1229 
 1230 DB_SHOW_COMMAND(sleepchain, db_show_sleepchain)
 1231 {
 1232         struct thread *td;
 1233 
 1234         /* Figure out which thread to start with. */
 1235         if (have_addr)
 1236                 td = db_lookup_thread(addr, TRUE);
 1237         else
 1238                 td = kdb_thread;
 1239 
 1240         print_sleepchain(td, "");
 1241 }
 1242 
 1243 static void     print_waiters(struct turnstile *ts, int indent);
 1244         
 1245 static void
 1246 print_waiter(struct thread *td, int indent)
 1247 {
 1248         struct turnstile *ts;
 1249         int i;
 1250 
 1251         if (db_pager_quit)
 1252                 return;
 1253         for (i = 0; i < indent; i++)
 1254                 db_printf(" ");
 1255         print_thread(td, "thread ");
 1256         LIST_FOREACH(ts, &td->td_contested, ts_link)
 1257                 print_waiters(ts, indent + 1);
 1258 }
 1259 
 1260 static void
 1261 print_waiters(struct turnstile *ts, int indent)
 1262 {
 1263         struct lock_object *lock;
 1264         struct lock_class *class;
 1265         struct thread *td;
 1266         int i;
 1267 
 1268         if (db_pager_quit)
 1269                 return;
 1270         lock = ts->ts_lockobj;
 1271         class = LOCK_CLASS(lock);
 1272         for (i = 0; i < indent; i++)
 1273                 db_printf(" ");
 1274         db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
 1275         TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
 1276                 print_waiter(td, indent + 1);
 1277         TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
 1278                 print_waiter(td, indent + 1);
 1279         TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
 1280                 print_waiter(td, indent + 1);
 1281 }
 1282 
 1283 DB_SHOW_COMMAND(locktree, db_show_locktree)
 1284 {
 1285         struct lock_object *lock;
 1286         struct lock_class *class;
 1287         struct turnstile_chain *tc;
 1288         struct turnstile *ts;
 1289 
 1290         if (!have_addr)
 1291                 return;
 1292         lock = (struct lock_object *)addr;
 1293         tc = TC_LOOKUP(lock);
 1294         LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
 1295                 if (ts->ts_lockobj == lock)
 1296                         break;
 1297         if (ts == NULL) {
 1298                 class = LOCK_CLASS(lock);
 1299                 db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
 1300                     lock->lo_name);
 1301         } else
 1302                 print_waiters(ts, 0);
 1303 }
 1304 #endif

Cache object: c56ca2691e8ea2d478d0345489565ff5


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.