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/kern_lock.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) 1995
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Copyright (C) 1997
    6  *      John S. Dyson.  All rights reserved.
    7  *
    8  * This code contains ideas from software contributed to Berkeley by
    9  * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating
   10  * System project at Carnegie-Mellon University.
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *      This product includes software developed by the University of
   23  *      California, Berkeley and its contributors.
   24  * 4. Neither the name of the University nor the names of its contributors
   25  *    may be used to endorse or promote products derived from this software
   26  *    without specific prior written permission.
   27  *
   28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   38  * SUCH DAMAGE.
   39  *
   40  *      @(#)kern_lock.c 8.18 (Berkeley) 5/21/95
   41  */
   42 
   43 #include <sys/cdefs.h>
   44 __FBSDID("$FreeBSD$");
   45 
   46 #include "opt_ddb.h"
   47 
   48 #include <sys/param.h>
   49 #include <sys/kdb.h>
   50 #include <sys/kernel.h>
   51 #include <sys/ktr.h>
   52 #include <sys/lock.h>
   53 #include <sys/lockmgr.h>
   54 #include <sys/mutex.h>
   55 #include <sys/proc.h>
   56 #include <sys/systm.h>
   57 #ifdef DEBUG_LOCKS
   58 #include <sys/stack.h>
   59 #endif
   60 
   61 #ifdef DDB
   62 #include <ddb/ddb.h>
   63 #endif
   64 
   65 /*
   66  * Locking primitives implementation.
   67  * Locks provide shared/exclusive sychronization.
   68  */
   69 
   70 #define COUNT(td, x)    if ((td)) (td)->td_locks += (x)
   71 #define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \
   72         LK_SHARE_NONZERO | LK_WAIT_NONZERO)
   73 
   74 static int acquire(struct lock **lkpp, int extflags, int wanted);
   75 static int acquiredrain(struct lock *lkp, int extflags) ;
   76 
   77 static __inline void
   78 sharelock(struct thread *td, struct lock *lkp, int incr) {
   79         lkp->lk_flags |= LK_SHARE_NONZERO;
   80         lkp->lk_sharecount += incr;
   81         COUNT(td, incr);
   82 }
   83 
   84 static __inline void
   85 shareunlock(struct thread *td, struct lock *lkp, int decr) {
   86 
   87         KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr"));
   88 
   89         COUNT(td, -decr);
   90         if (lkp->lk_sharecount == decr) {
   91                 lkp->lk_flags &= ~LK_SHARE_NONZERO;
   92                 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) {
   93                         wakeup(lkp);
   94                 }
   95                 lkp->lk_sharecount = 0;
   96         } else {
   97                 lkp->lk_sharecount -= decr;
   98         }
   99 }
  100 
  101 static int
  102 acquire(struct lock **lkpp, int extflags, int wanted)
  103 {
  104         struct lock *lkp = *lkpp;
  105         int error;
  106         CTR3(KTR_LOCK,
  107             "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x",
  108             lkp, extflags, wanted);
  109 
  110         if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted))
  111                 return EBUSY;
  112         error = 0;
  113         while ((lkp->lk_flags & wanted) != 0) {
  114                 CTR2(KTR_LOCK,
  115                     "acquire(): lkp == %p, lk_flags == 0x%x sleeping",
  116                     lkp, lkp->lk_flags);
  117                 lkp->lk_flags |= LK_WAIT_NONZERO;
  118                 lkp->lk_waitcount++;
  119                 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio,
  120                     lkp->lk_wmesg, 
  121                     ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
  122                 lkp->lk_waitcount--;
  123                 if (lkp->lk_waitcount == 0)
  124                         lkp->lk_flags &= ~LK_WAIT_NONZERO;
  125                 if (error)
  126                         break;
  127                 if (extflags & LK_SLEEPFAIL) {
  128                         error = ENOLCK;
  129                         break;
  130                 }
  131                 if (lkp->lk_newlock != NULL) {
  132                         mtx_lock(lkp->lk_newlock->lk_interlock);
  133                         mtx_unlock(lkp->lk_interlock);
  134                         if (lkp->lk_waitcount == 0)
  135                                 wakeup((void *)(&lkp->lk_newlock));
  136                         *lkpp = lkp = lkp->lk_newlock;
  137                 }
  138         }
  139         mtx_assert(lkp->lk_interlock, MA_OWNED);
  140         return (error);
  141 }
  142 
  143 /*
  144  * Set, change, or release a lock.
  145  *
  146  * Shared requests increment the shared count. Exclusive requests set the
  147  * LK_WANT_EXCL flag (preventing further shared locks), and wait for already
  148  * accepted shared locks and shared-to-exclusive upgrades to go away.
  149  */
  150 int
  151 lockmgr(lkp, flags, interlkp, td)
  152         struct lock *lkp;
  153         u_int flags;
  154         struct mtx *interlkp;
  155         struct thread *td;
  156 {
  157         int error;
  158         struct thread *thr;
  159         int extflags, lockflags;
  160 
  161         error = 0;
  162         if (td == NULL)
  163                 thr = LK_KERNPROC;
  164         else
  165                 thr = td;
  166 
  167         if ((flags & LK_INTERNAL) == 0)
  168                 mtx_lock(lkp->lk_interlock);
  169         CTR6(KTR_LOCK,
  170             "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), owner == %p, exclusivecount == %d, flags == 0x%x, "
  171             "td == %p", lkp, lkp->lk_wmesg, lkp->lk_lockholder,
  172             lkp->lk_exclusivecount, flags, td);
  173 #ifdef DEBUG_LOCKS
  174         {
  175                 struct stack stack; /* XXX */
  176                 stack_save(&stack);
  177                 CTRSTACK(KTR_LOCK, &stack, 0, 1);
  178         }
  179 #endif
  180 
  181         if (flags & LK_INTERLOCK) {
  182                 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED);
  183                 mtx_unlock(interlkp);
  184         }
  185 
  186         if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0)
  187                 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK,
  188                     &lkp->lk_interlock->mtx_object,
  189                     "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg);
  190 
  191         if (panicstr != NULL) {
  192                 mtx_unlock(lkp->lk_interlock);
  193                 return (0);
  194         }
  195         if ((lkp->lk_flags & LK_NOSHARE) &&
  196             (flags & LK_TYPE_MASK) == LK_SHARED) {
  197                 flags &= ~LK_TYPE_MASK;
  198                 flags |= LK_EXCLUSIVE;
  199         }
  200         extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK;
  201 
  202         switch (flags & LK_TYPE_MASK) {
  203 
  204         case LK_SHARED:
  205                 /*
  206                  * If we are not the exclusive lock holder, we have to block
  207                  * while there is an exclusive lock holder or while an
  208                  * exclusive lock request or upgrade request is in progress.
  209                  *
  210                  * However, if TDP_DEADLKTREAT is set, we override exclusive
  211                  * lock requests or upgrade requests ( but not the exclusive
  212                  * lock itself ).
  213                  */
  214                 if (lkp->lk_lockholder != thr) {
  215                         lockflags = LK_HAVE_EXCL;
  216                         if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT))
  217                                 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE;
  218                         error = acquire(&lkp, extflags, lockflags);
  219                         if (error)
  220                                 break;
  221                         sharelock(td, lkp, 1);
  222 #if defined(DEBUG_LOCKS)
  223                         stack_save(&lkp->lk_stack);
  224 #endif
  225                         break;
  226                 }
  227                 /*
  228                  * We hold an exclusive lock, so downgrade it to shared.
  229                  * An alternative would be to fail with EDEADLK.
  230                  */
  231                 sharelock(td, lkp, 1);
  232                 /* FALLTHROUGH downgrade */
  233 
  234         case LK_DOWNGRADE:
  235                 KASSERT(lkp->lk_lockholder == thr && lkp->lk_exclusivecount != 0,
  236                         ("lockmgr: not holding exclusive lock "
  237                         "(owner thread (%p) != thread (%p), exlcnt (%d) != 0",
  238                         lkp->lk_lockholder, thr, lkp->lk_exclusivecount));
  239                 sharelock(td, lkp, lkp->lk_exclusivecount);
  240                 COUNT(td, -lkp->lk_exclusivecount);
  241                 lkp->lk_exclusivecount = 0;
  242                 lkp->lk_flags &= ~LK_HAVE_EXCL;
  243                 lkp->lk_lockholder = LK_NOPROC;
  244                 if (lkp->lk_waitcount)
  245                         wakeup((void *)lkp);
  246                 break;
  247 
  248         case LK_EXCLUPGRADE:
  249                 /*
  250                  * If another process is ahead of us to get an upgrade,
  251                  * then we want to fail rather than have an intervening
  252                  * exclusive access.
  253                  */
  254                 if (lkp->lk_flags & LK_WANT_UPGRADE) {
  255                         shareunlock(td, lkp, 1);
  256                         error = EBUSY;
  257                         break;
  258                 }
  259                 /* FALLTHROUGH normal upgrade */
  260 
  261         case LK_UPGRADE:
  262                 /*
  263                  * Upgrade a shared lock to an exclusive one. If another
  264                  * shared lock has already requested an upgrade to an
  265                  * exclusive lock, our shared lock is released and an
  266                  * exclusive lock is requested (which will be granted
  267                  * after the upgrade). If we return an error, the file
  268                  * will always be unlocked.
  269                  */
  270                 if (lkp->lk_lockholder == thr)
  271                         panic("lockmgr: upgrade exclusive lock");
  272                 if (lkp->lk_sharecount <= 0)
  273                         panic("lockmgr: upgrade without shared");
  274                 shareunlock(td, lkp, 1);
  275                 /*
  276                  * If we are just polling, check to see if we will block.
  277                  */
  278                 if ((extflags & LK_NOWAIT) &&
  279                     ((lkp->lk_flags & LK_WANT_UPGRADE) ||
  280                      lkp->lk_sharecount > 1)) {
  281                         error = EBUSY;
  282                         break;
  283                 }
  284                 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) {
  285                         /*
  286                          * We are first shared lock to request an upgrade, so
  287                          * request upgrade and wait for the shared count to
  288                          * drop to zero, then take exclusive lock.
  289                          */
  290                         lkp->lk_flags |= LK_WANT_UPGRADE;
  291                         error = acquire(&lkp, extflags, LK_SHARE_NONZERO);
  292                         lkp->lk_flags &= ~LK_WANT_UPGRADE;
  293 
  294                         if (error) {
  295                                  if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO))
  296                                            wakeup((void *)lkp);
  297                                  break;
  298                         }
  299                         if (lkp->lk_exclusivecount != 0)
  300                                 panic("lockmgr: non-zero exclusive count");
  301                         lkp->lk_flags |= LK_HAVE_EXCL;
  302                         lkp->lk_lockholder = thr;
  303                         lkp->lk_exclusivecount = 1;
  304                         COUNT(td, 1);
  305 #if defined(DEBUG_LOCKS)
  306                         stack_save(&lkp->lk_stack);
  307 #endif
  308                         break;
  309                 }
  310                 /*
  311                  * Someone else has requested upgrade. Release our shared
  312                  * lock, awaken upgrade requestor if we are the last shared
  313                  * lock, then request an exclusive lock.
  314                  */
  315                 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) ==
  316                         LK_WAIT_NONZERO)
  317                         wakeup((void *)lkp);
  318                 /* FALLTHROUGH exclusive request */
  319 
  320         case LK_EXCLUSIVE:
  321                 if (lkp->lk_lockholder == thr && thr != LK_KERNPROC) {
  322                         /*
  323                          *      Recursive lock.
  324                          */
  325                         if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0)
  326                                 panic("lockmgr: locking against myself");
  327                         if ((extflags & LK_CANRECURSE) != 0) {
  328                                 lkp->lk_exclusivecount++;
  329                                 COUNT(td, 1);
  330                                 break;
  331                         }
  332                 }
  333                 /*
  334                  * If we are just polling, check to see if we will sleep.
  335                  */
  336                 if ((extflags & LK_NOWAIT) &&
  337                     (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) {
  338                         error = EBUSY;
  339                         break;
  340                 }
  341                 /*
  342                  * Try to acquire the want_exclusive flag.
  343                  */
  344                 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL));
  345                 if (error)
  346                         break;
  347                 lkp->lk_flags |= LK_WANT_EXCL;
  348                 /*
  349                  * Wait for shared locks and upgrades to finish.
  350                  */
  351                 error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO);
  352                 lkp->lk_flags &= ~LK_WANT_EXCL;
  353                 if (error) {
  354                         if (lkp->lk_flags & LK_WAIT_NONZERO)            
  355                                  wakeup((void *)lkp);
  356                         break;
  357                 }       
  358                 lkp->lk_flags |= LK_HAVE_EXCL;
  359                 lkp->lk_lockholder = thr;
  360                 if (lkp->lk_exclusivecount != 0)
  361                         panic("lockmgr: non-zero exclusive count");
  362                 lkp->lk_exclusivecount = 1;
  363                 COUNT(td, 1);
  364 #if defined(DEBUG_LOCKS)
  365                 stack_save(&lkp->lk_stack);
  366 #endif
  367                 break;
  368 
  369         case LK_RELEASE:
  370                 if (lkp->lk_exclusivecount != 0) {
  371                         if (lkp->lk_lockholder != thr &&
  372                             lkp->lk_lockholder != LK_KERNPROC) {
  373                                 panic("lockmgr: thread %p, not %s %p unlocking",
  374                                     thr, "exclusive lock holder",
  375                                     lkp->lk_lockholder);
  376                         }
  377                         if (lkp->lk_lockholder != LK_KERNPROC)
  378                                 COUNT(td, -1);
  379                         if (lkp->lk_exclusivecount == 1) {
  380                                 lkp->lk_flags &= ~LK_HAVE_EXCL;
  381                                 lkp->lk_lockholder = LK_NOPROC;
  382                                 lkp->lk_exclusivecount = 0;
  383                         } else {
  384                                 lkp->lk_exclusivecount--;
  385                         }
  386                 } else if (lkp->lk_flags & LK_SHARE_NONZERO)
  387                         shareunlock(td, lkp, 1);
  388                 if (lkp->lk_flags & LK_WAIT_NONZERO)
  389                         wakeup((void *)lkp);
  390                 break;
  391 
  392         case LK_DRAIN:
  393                 /*
  394                  * Check that we do not already hold the lock, as it can 
  395                  * never drain if we do. Unfortunately, we have no way to
  396                  * check for holding a shared lock, but at least we can
  397                  * check for an exclusive one.
  398                  */
  399                 if (lkp->lk_lockholder == thr)
  400                         panic("lockmgr: draining against myself");
  401 
  402                 error = acquiredrain(lkp, extflags);
  403                 if (error)
  404                         break;
  405                 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL;
  406                 lkp->lk_lockholder = thr;
  407                 lkp->lk_exclusivecount = 1;
  408                 COUNT(td, 1);
  409 #if defined(DEBUG_LOCKS)
  410                 stack_save(&lkp->lk_stack);
  411 #endif
  412                 break;
  413 
  414         default:
  415                 mtx_unlock(lkp->lk_interlock);
  416                 panic("lockmgr: unknown locktype request %d",
  417                     flags & LK_TYPE_MASK);
  418                 /* NOTREACHED */
  419         }
  420         if ((lkp->lk_flags & LK_WAITDRAIN) &&
  421             (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE |
  422                 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) {
  423                 lkp->lk_flags &= ~LK_WAITDRAIN;
  424                 wakeup((void *)&lkp->lk_flags);
  425         }
  426         mtx_unlock(lkp->lk_interlock);
  427         return (error);
  428 }
  429 
  430 static int
  431 acquiredrain(struct lock *lkp, int extflags) {
  432         int error;
  433 
  434         if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) {
  435                 return EBUSY;
  436         }
  437         while (lkp->lk_flags & LK_ALL) {
  438                 lkp->lk_flags |= LK_WAITDRAIN;
  439                 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio,
  440                         lkp->lk_wmesg, 
  441                         ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0));
  442                 if (error)
  443                         return error;
  444                 if (extflags & LK_SLEEPFAIL) {
  445                         return ENOLCK;
  446                 }
  447         }
  448         return 0;
  449 }
  450 
  451 /*
  452  * Transfer any waiting processes from one lock to another.
  453  */
  454 void
  455 transferlockers(from, to)
  456         struct lock *from;
  457         struct lock *to;
  458 {
  459 
  460         KASSERT(from != to, ("lock transfer to self"));
  461         KASSERT((from->lk_flags&LK_WAITDRAIN) == 0, ("transfer draining lock"));
  462 
  463         mtx_lock(from->lk_interlock);
  464         if (from->lk_waitcount == 0) {
  465                 mtx_unlock(from->lk_interlock);
  466                 return;
  467         }
  468         from->lk_newlock = to;
  469         wakeup((void *)from);
  470         msleep(&from->lk_newlock, from->lk_interlock, from->lk_prio,
  471             "lkxfer", 0);
  472         from->lk_newlock = NULL;
  473         from->lk_flags &= ~(LK_WANT_EXCL | LK_WANT_UPGRADE);
  474         KASSERT(from->lk_waitcount == 0, ("active lock"));
  475         mtx_unlock(from->lk_interlock);
  476 }
  477 
  478 
  479 /*
  480  * Initialize a lock; required before use.
  481  */
  482 void
  483 lockinit(lkp, prio, wmesg, timo, flags)
  484         struct lock *lkp;
  485         int prio;
  486         const char *wmesg;
  487         int timo;
  488         int flags;
  489 {
  490         CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", "
  491             "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags);
  492 
  493         lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder);
  494         lkp->lk_flags = (flags & LK_EXTFLG_MASK);
  495         lkp->lk_sharecount = 0;
  496         lkp->lk_waitcount = 0;
  497         lkp->lk_exclusivecount = 0;
  498         lkp->lk_prio = prio;
  499         lkp->lk_wmesg = wmesg;
  500         lkp->lk_timo = timo;
  501         lkp->lk_lockholder = LK_NOPROC;
  502         lkp->lk_newlock = NULL;
  503 #ifdef DEBUG_LOCKS
  504         stack_zero(&lkp->lk_stack);
  505 #endif
  506 }
  507 
  508 /*
  509  * Destroy a lock.
  510  */
  511 void
  512 lockdestroy(lkp)
  513         struct lock *lkp;
  514 {
  515         CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")",
  516             lkp, lkp->lk_wmesg);
  517 }
  518 
  519 /*
  520  * Determine the status of a lock.
  521  */
  522 int
  523 lockstatus(lkp, td)
  524         struct lock *lkp;
  525         struct thread *td;
  526 {
  527         int lock_type = 0;
  528         int interlocked;
  529 
  530         if (!kdb_active) {
  531                 interlocked = 1;
  532                 mtx_lock(lkp->lk_interlock);
  533         } else
  534                 interlocked = 0;
  535         if (lkp->lk_exclusivecount != 0) {
  536                 if (td == NULL || lkp->lk_lockholder == td)
  537                         lock_type = LK_EXCLUSIVE;
  538                 else
  539                         lock_type = LK_EXCLOTHER;
  540         } else if (lkp->lk_sharecount != 0)
  541                 lock_type = LK_SHARED;
  542         if (interlocked)
  543                 mtx_unlock(lkp->lk_interlock);
  544         return (lock_type);
  545 }
  546 
  547 /*
  548  * Determine the number of holders of a lock.
  549  */
  550 int
  551 lockcount(lkp)
  552         struct lock *lkp;
  553 {
  554         int count;
  555 
  556         mtx_lock(lkp->lk_interlock);
  557         count = lkp->lk_exclusivecount + lkp->lk_sharecount;
  558         mtx_unlock(lkp->lk_interlock);
  559         return (count);
  560 }
  561 
  562 /*
  563  * Determine the number of waiters on a lock.
  564  */
  565 int
  566 lockwaiters(lkp)
  567         struct lock *lkp;
  568 {
  569         int count;
  570 
  571         mtx_lock(lkp->lk_interlock);
  572         count = lkp->lk_waitcount;
  573         mtx_unlock(lkp->lk_interlock);
  574         return (count);
  575 }
  576 
  577 /*
  578  * Print out information about state of a lock. Used by VOP_PRINT
  579  * routines to display status about contained locks.
  580  */
  581 void
  582 lockmgr_printinfo(lkp)
  583         struct lock *lkp;
  584 {
  585 
  586         if (lkp->lk_sharecount)
  587                 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg,
  588                     lkp->lk_sharecount);
  589         else if (lkp->lk_flags & LK_HAVE_EXCL)
  590                 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)",
  591                     lkp->lk_wmesg, lkp->lk_exclusivecount,
  592                     lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid);
  593         if (lkp->lk_waitcount > 0)
  594                 printf(" with %d pending", lkp->lk_waitcount);
  595 #ifdef DEBUG_LOCKS
  596         stack_print(&lkp->lk_stack);
  597 #endif
  598 }
  599 
  600 #ifdef DDB
  601 /*
  602  * Check to see if a thread that is blocked on a sleep queue is actually
  603  * blocked on a 'struct lock'.  If so, output some details and return true.
  604  * If the lock has an exclusive owner, return that in *ownerp.
  605  */
  606 int
  607 lockmgr_chain(struct thread *td, struct thread **ownerp)
  608 {
  609         struct lock *lkp;
  610 
  611         lkp = td->td_wchan;
  612 
  613         /* Simple test to see if wchan points to a lockmgr lock. */
  614         if (lkp->lk_wmesg == td->td_wmesg)
  615                 goto ok;
  616 
  617         /*
  618          * If this thread is doing a DRAIN, then it would be asleep on
  619          * &lkp->lk_flags rather than lkp.
  620          */
  621         lkp = (struct lock *)((char *)td->td_wchan -
  622             offsetof(struct lock, lk_flags));
  623         if (lkp->lk_wmesg == td->td_wmesg && (lkp->lk_flags & LK_WAITDRAIN))
  624                 goto ok;
  625 
  626         /* Doen't seem to be a lockmgr lock. */
  627         return (0);
  628 
  629 ok:
  630         /* Ok, we think we have a lockmgr lock, so output some details. */
  631         db_printf("blocked on lk \"%s\" ", lkp->lk_wmesg);
  632         if (lkp->lk_sharecount) {
  633                 db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
  634                 *ownerp = NULL;
  635         } else {
  636                 db_printf("EXCL (count %d)\n", lkp->lk_exclusivecount);
  637                 *ownerp = lkp->lk_lockholder;
  638         }
  639         return (1);
  640 }
  641 
  642 DB_SHOW_COMMAND(lockmgr, db_show_lockmgr)
  643 {
  644         struct thread *td;
  645         struct lock *lkp;
  646 
  647         if (!have_addr)
  648                 return;
  649         lkp = (struct lock *)addr;
  650 
  651         db_printf("lock type: %s\n", lkp->lk_wmesg);
  652         db_printf("state: ");
  653         if (lkp->lk_sharecount)
  654                 db_printf("SHARED (count %d)\n", lkp->lk_sharecount);
  655         else if (lkp->lk_flags & LK_HAVE_EXCL) {
  656                 td = lkp->lk_lockholder;
  657                 db_printf("EXCL (count %d) %p ", lkp->lk_exclusivecount, td);
  658                 db_printf("(tid %d, pid %d, \"%s\")\n", td->td_tid,
  659                     td->td_proc->p_pid, td->td_proc->p_comm);
  660         } else
  661                 db_printf("UNLOCKED\n");
  662         if (lkp->lk_waitcount > 0)
  663                 db_printf("waiters: %d\n", lkp->lk_waitcount);
  664 }
  665 #endif

Cache object: 01feeb6798f4d0a468bc3d319f786d56


[ 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.