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_mutex.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
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  * Machine independent bits of mutex implementation.
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD$");
   38 
   39 #include "opt_adaptive_mutexes.h"
   40 #include "opt_ddb.h"
   41 #include "opt_mprof.h"
   42 #include "opt_mutex_wake_all.h"
   43 #include "opt_sched.h"
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/bus.h>
   48 #include <sys/kdb.h>
   49 #include <sys/kernel.h>
   50 #include <sys/ktr.h>
   51 #include <sys/lock.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mutex.h>
   54 #include <sys/proc.h>
   55 #include <sys/resourcevar.h>
   56 #include <sys/sched.h>
   57 #include <sys/sbuf.h>
   58 #include <sys/sysctl.h>
   59 #include <sys/turnstile.h>
   60 #include <sys/vmmeter.h>
   61 
   62 #include <machine/atomic.h>
   63 #include <machine/bus.h>
   64 #include <machine/clock.h>
   65 #include <machine/cpu.h>
   66 
   67 #include <ddb/ddb.h>
   68 
   69 #include <vm/vm.h>
   70 #include <vm/vm_extern.h>
   71 
   72 /* 
   73  * Force MUTEX_WAKE_ALL for now.
   74  * single thread wakeup needs fixes to avoid race conditions with 
   75  * priority inheritance.
   76  */
   77 #ifndef MUTEX_WAKE_ALL
   78 #define MUTEX_WAKE_ALL
   79 #endif
   80 
   81 /*
   82  * Internal utility macros.
   83  */
   84 #define mtx_unowned(m)  ((m)->mtx_lock == MTX_UNOWNED)
   85 
   86 #define mtx_owner(m)    (mtx_unowned((m)) ? NULL \
   87         : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
   88 
   89 /*
   90  * Lock classes for sleep and spin mutexes.
   91  */
   92 struct lock_class lock_class_mtx_sleep = {
   93         "sleep mutex",
   94         LC_SLEEPLOCK | LC_RECURSABLE
   95 };
   96 struct lock_class lock_class_mtx_spin = {
   97         "spin mutex",
   98         LC_SPINLOCK | LC_RECURSABLE
   99 };
  100 
  101 /*
  102  * System-wide mutexes
  103  */
  104 struct mtx sched_lock;
  105 struct mtx Giant;
  106 
  107 #ifdef MUTEX_PROFILING
  108 SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
  109 SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
  110 static int mutex_prof_enable = 0;
  111 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
  112     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
  113 
  114 struct mutex_prof {
  115         const char      *name;
  116         const char      *file;
  117         int             line;
  118         uintmax_t       cnt_max;
  119         uintmax_t       cnt_tot;
  120         uintmax_t       cnt_cur;
  121         uintmax_t       cnt_contest_holding;
  122         uintmax_t       cnt_contest_locking;
  123         struct mutex_prof *next;
  124 };
  125 
  126 /*
  127  * mprof_buf is a static pool of profiling records to avoid possible
  128  * reentrance of the memory allocation functions.
  129  *
  130  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
  131  */
  132 #ifdef MPROF_BUFFERS
  133 #define NUM_MPROF_BUFFERS       MPROF_BUFFERS
  134 #else
  135 #define NUM_MPROF_BUFFERS       1000
  136 #endif
  137 static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
  138 static int first_free_mprof_buf;
  139 #ifndef MPROF_HASH_SIZE
  140 #define MPROF_HASH_SIZE         1009
  141 #endif
  142 #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE
  143 #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE
  144 #endif
  145 static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
  146 /* SWAG: sbuf size = avg stat. line size * number of locks */
  147 #define MPROF_SBUF_SIZE         256 * 400
  148 
  149 static int mutex_prof_acquisitions;
  150 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
  151     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
  152 static int mutex_prof_records;
  153 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
  154     &mutex_prof_records, 0, "Number of profiling records");
  155 static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
  156 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
  157     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
  158 static int mutex_prof_rejected;
  159 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
  160     &mutex_prof_rejected, 0, "Number of rejected profiling records");
  161 static int mutex_prof_hashsize = MPROF_HASH_SIZE;
  162 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
  163     &mutex_prof_hashsize, 0, "Hash size");
  164 static int mutex_prof_collisions = 0;
  165 SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
  166     &mutex_prof_collisions, 0, "Number of hash collisions");
  167 
  168 /*
  169  * mprof_mtx protects the profiling buffers and the hash.
  170  */
  171 static struct mtx mprof_mtx;
  172 MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
  173 
  174 static u_int64_t
  175 nanoseconds(void)
  176 {
  177         struct timespec tv;
  178 
  179         nanotime(&tv);
  180         return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
  181 }
  182 
  183 static int
  184 dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
  185 {
  186         struct sbuf *sb;
  187         int error, i;
  188         static int multiplier = 1;
  189 
  190         if (first_free_mprof_buf == 0)
  191                 return (SYSCTL_OUT(req, "No locking recorded",
  192                     sizeof("No locking recorded")));
  193 
  194 retry_sbufops:
  195         sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN);
  196         sbuf_printf(sb, "%6s %12s %11s %5s %12s %12s %s\n",
  197             "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name");
  198         /*
  199          * XXX this spinlock seems to be by far the largest perpetrator
  200          * of spinlock latency (1.6 msec on an Athlon1600 was recorded
  201          * even before I pessimized it further by moving the average
  202          * computation here).
  203          */
  204         mtx_lock_spin(&mprof_mtx);
  205         for (i = 0; i < first_free_mprof_buf; ++i) {
  206                 sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n",
  207                     mprof_buf[i].cnt_max / 1000,
  208                     mprof_buf[i].cnt_tot / 1000,
  209                     mprof_buf[i].cnt_cur,
  210                     mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 :
  211                         mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000),
  212                     mprof_buf[i].cnt_contest_holding,
  213                     mprof_buf[i].cnt_contest_locking,
  214                     mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
  215                 if (sbuf_overflowed(sb)) {
  216                         mtx_unlock_spin(&mprof_mtx);
  217                         sbuf_delete(sb);
  218                         multiplier++;
  219                         goto retry_sbufops;
  220                 }
  221         }
  222         mtx_unlock_spin(&mprof_mtx);
  223         sbuf_finish(sb);
  224         error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
  225         sbuf_delete(sb);
  226         return (error);
  227 }
  228 SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
  229     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
  230 
  231 static int
  232 reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
  233 {
  234         int error, v;
  235 
  236         if (first_free_mprof_buf == 0)
  237                 return (0);
  238 
  239         v = 0;
  240         error = sysctl_handle_int(oidp, &v, 0, req);
  241         if (error)
  242                 return (error);
  243         if (req->newptr == NULL)
  244                 return (error);
  245         if (v == 0)
  246                 return (0);
  247 
  248         mtx_lock_spin(&mprof_mtx);
  249         bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf);
  250         bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE);
  251         first_free_mprof_buf = 0;
  252         mtx_unlock_spin(&mprof_mtx);
  253         return (0);
  254 }
  255 SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
  256     NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics");
  257 #endif
  258 
  259 /*
  260  * Function versions of the inlined __mtx_* macros.  These are used by
  261  * modules and can also be called from assembly language if needed.
  262  */
  263 void
  264 _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
  265 {
  266 
  267         MPASS(curthread != NULL);
  268         KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
  269             ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
  270             file, line));
  271         WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
  272             file, line);
  273         _get_sleep_lock(m, curthread, opts, file, line);
  274         LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
  275             line);
  276         WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
  277 #ifdef MUTEX_PROFILING
  278         /* don't reset the timer when/if recursing */
  279         if (m->mtx_acqtime == 0) {
  280                 m->mtx_filename = file;
  281                 m->mtx_lineno = line;
  282                 m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
  283                 ++mutex_prof_acquisitions;
  284         }
  285 #endif
  286 }
  287 
  288 void
  289 _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
  290 {
  291 
  292         MPASS(curthread != NULL);
  293         KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
  294             ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
  295             file, line));
  296         WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
  297         LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
  298             line);
  299         mtx_assert(m, MA_OWNED);
  300 #ifdef MUTEX_PROFILING
  301         if (m->mtx_acqtime != 0) {
  302                 static const char *unknown = "(unknown)";
  303                 struct mutex_prof *mpp;
  304                 u_int64_t acqtime, now;
  305                 const char *p, *q;
  306                 volatile u_int hash;
  307 
  308                 now = nanoseconds();
  309                 acqtime = m->mtx_acqtime;
  310                 m->mtx_acqtime = 0;
  311                 if (now <= acqtime)
  312                         goto out;
  313                 for (p = m->mtx_filename;
  314                     p != NULL && strncmp(p, "../", 3) == 0; p += 3)
  315                         /* nothing */ ;
  316                 if (p == NULL || *p == '\0')
  317                         p = unknown;
  318                 for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
  319                         hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
  320                 mtx_lock_spin(&mprof_mtx);
  321                 for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
  322                         if (mpp->line == m->mtx_lineno &&
  323                             strcmp(mpp->file, p) == 0)
  324                                 break;
  325                 if (mpp == NULL) {
  326                         /* Just exit if we cannot get a trace buffer */
  327                         if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
  328                                 ++mutex_prof_rejected;
  329                                 goto unlock;
  330                         }
  331                         mpp = &mprof_buf[first_free_mprof_buf++];
  332                         mpp->name = mtx_name(m);
  333                         mpp->file = p;
  334                         mpp->line = m->mtx_lineno;
  335                         mpp->next = mprof_hash[hash];
  336                         if (mprof_hash[hash] != NULL)
  337                                 ++mutex_prof_collisions;
  338                         mprof_hash[hash] = mpp;
  339                         ++mutex_prof_records;
  340                 }
  341                 /*
  342                  * Record if the mutex has been held longer now than ever
  343                  * before.
  344                  */
  345                 if (now - acqtime > mpp->cnt_max)
  346                         mpp->cnt_max = now - acqtime;
  347                 mpp->cnt_tot += now - acqtime;
  348                 mpp->cnt_cur++;
  349                 /*
  350                  * There's a small race, really we should cmpxchg
  351                  * 0 with the current value, but that would bill
  352                  * the contention to the wrong lock instance if
  353                  * it followed this also.
  354                  */
  355                 mpp->cnt_contest_holding += m->mtx_contest_holding;
  356                 m->mtx_contest_holding = 0;
  357                 mpp->cnt_contest_locking += m->mtx_contest_locking;
  358                 m->mtx_contest_locking = 0;
  359 unlock:
  360                 mtx_unlock_spin(&mprof_mtx);
  361         }
  362 out:
  363 #endif
  364         _rel_sleep_lock(m, curthread, opts, file, line);
  365 }
  366 
  367 void
  368 _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
  369 {
  370 
  371         MPASS(curthread != NULL);
  372         KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
  373             ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
  374             m->mtx_object.lo_name, file, line));
  375         WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
  376             file, line);
  377         _get_spin_lock(m, curthread, opts, file, line);
  378         LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
  379             line);
  380         WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
  381 }
  382 
  383 void
  384 _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
  385 {
  386 
  387         MPASS(curthread != NULL);
  388         KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
  389             ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
  390             m->mtx_object.lo_name, file, line));
  391         WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
  392         LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
  393             line);
  394         mtx_assert(m, MA_OWNED);
  395         _rel_spin_lock(m);
  396 }
  397 
  398 /*
  399  * The important part of mtx_trylock{,_flags}()
  400  * Tries to acquire lock `m.'  If this function is called on a mutex that
  401  * is already owned, it will recursively acquire the lock.
  402  */
  403 int
  404 _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
  405 {
  406         int rval;
  407 
  408         MPASS(curthread != NULL);
  409 
  410         if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
  411                 m->mtx_recurse++;
  412                 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
  413                 rval = 1;
  414         } else
  415                 rval = _obtain_lock(m, curthread);
  416 
  417         LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
  418         if (rval)
  419                 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
  420                     file, line);
  421 
  422         return (rval);
  423 }
  424 
  425 /*
  426  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
  427  *
  428  * We call this if the lock is either contested (i.e. we need to go to
  429  * sleep waiting for it), or if we need to recurse on it.
  430  */
  431 void
  432 _mtx_lock_sleep(struct mtx *m, struct thread *td, int opts, const char *file,
  433     int line)
  434 {
  435         struct turnstile *ts;
  436 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
  437         struct thread *owner;
  438 #endif
  439         uintptr_t v;
  440 #ifdef KTR
  441         int cont_logged = 0;
  442 #endif
  443 #ifdef MUTEX_PROFILING
  444         int contested;
  445 #endif
  446 
  447         if (mtx_owned(m)) {
  448                 KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
  449             ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
  450                     m->mtx_object.lo_name, file, line));
  451                 m->mtx_recurse++;
  452                 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
  453                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  454                         CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
  455                 return;
  456         }
  457 
  458         if (LOCK_LOG_TEST(&m->mtx_object, opts))
  459                 CTR4(KTR_LOCK,
  460                     "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
  461                     m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
  462 
  463 #ifdef MUTEX_PROFILING
  464         contested = 0;
  465 #endif
  466         while (!_obtain_lock(m, td)) {
  467 #ifdef MUTEX_PROFILING
  468                 contested = 1;
  469                 atomic_add_int(&m->mtx_contest_holding, 1);
  470 #endif
  471                 ts = turnstile_lookup(&m->mtx_object);
  472                 v = m->mtx_lock;
  473 
  474                 /*
  475                  * Check if the lock has been released while spinning for
  476                  * the turnstile chain lock.
  477                  */
  478                 if (v == MTX_UNOWNED) {
  479                         turnstile_release(&m->mtx_object);
  480                         cpu_spinwait();
  481                         continue;
  482                 }
  483 
  484 #ifdef MUTEX_WAKE_ALL
  485                 MPASS(v != MTX_CONTESTED);
  486 #else
  487                 /*
  488                  * The mutex was marked contested on release. This means that
  489                  * there are other threads blocked on it.  Grab ownership of
  490                  * it and propagate its priority to the current thread if
  491                  * necessary.
  492                  */
  493                 if (v == MTX_CONTESTED) {
  494                         MPASS(ts != NULL);
  495                         m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
  496                         turnstile_claim(ts);
  497                         break;
  498                 }
  499 #endif
  500 
  501                 /*
  502                  * If the mutex isn't already contested and a failure occurs
  503                  * setting the contested bit, the mutex was either released
  504                  * or the state of the MTX_RECURSED bit changed.
  505                  */
  506                 if ((v & MTX_CONTESTED) == 0 &&
  507                     !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
  508                         (void *)(v | MTX_CONTESTED))) {
  509                         turnstile_release(&m->mtx_object);
  510                         cpu_spinwait();
  511                         continue;
  512                 }
  513 
  514 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
  515                 /*
  516                  * If the current owner of the lock is executing on another
  517                  * CPU, spin instead of blocking.
  518                  */
  519                 owner = (struct thread *)(v & MTX_FLAGMASK);
  520 #ifdef ADAPTIVE_GIANT
  521                 if (TD_IS_RUNNING(owner)) {
  522 #else
  523                 if (m != &Giant && TD_IS_RUNNING(owner)) {
  524 #endif
  525                         turnstile_release(&m->mtx_object);
  526                         while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
  527                                 cpu_spinwait();
  528                         }
  529                         continue;
  530                 }
  531 #endif  /* SMP && !NO_ADAPTIVE_MUTEXES */
  532 
  533                 /*
  534                  * We definitely must sleep for this lock.
  535                  */
  536                 mtx_assert(m, MA_NOTOWNED);
  537 
  538 #ifdef KTR
  539                 if (!cont_logged) {
  540                         CTR6(KTR_CONTENTION,
  541                             "contention: %p at %s:%d wants %s, taken by %s:%d",
  542                             td, file, line, m->mtx_object.lo_name,
  543                             WITNESS_FILE(&m->mtx_object),
  544                             WITNESS_LINE(&m->mtx_object));
  545                         cont_logged = 1;
  546                 }
  547 #endif
  548 
  549                 /*
  550                  * Block on the turnstile.
  551                  */
  552                 turnstile_wait(ts, &m->mtx_object, mtx_owner(m));
  553         }
  554 
  555 #ifdef KTR
  556         if (cont_logged) {
  557                 CTR4(KTR_CONTENTION,
  558                     "contention end: %s acquired by %p at %s:%d",
  559                     m->mtx_object.lo_name, td, file, line);
  560         }
  561 #endif
  562 #ifdef MUTEX_PROFILING
  563         if (contested)
  564                 m->mtx_contest_locking++;
  565         m->mtx_contest_holding = 0;
  566 #endif
  567         return;
  568 }
  569 
  570 #ifdef SMP
  571 /*
  572  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
  573  *
  574  * This is only called if we need to actually spin for the lock. Recursion
  575  * is handled inline.
  576  */
  577 void
  578 _mtx_lock_spin(struct mtx *m, struct thread *td, int opts, const char *file,
  579     int line)
  580 {
  581         int i = 0;
  582 
  583         if (LOCK_LOG_TEST(&m->mtx_object, opts))
  584                 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
  585 
  586         for (;;) {
  587                 if (_obtain_lock(m, td))
  588                         break;
  589 
  590                 /* Give interrupts a chance while we spin. */
  591                 critical_exit();
  592                 while (m->mtx_lock != MTX_UNOWNED) {
  593                         if (i++ < 10000000) {
  594                                 cpu_spinwait();
  595                                 continue;
  596                         }
  597                         if (i < 60000000)
  598                                 DELAY(1);
  599                         else if (!kdb_active) {
  600                                 printf("spin lock %s held by %p for > 5 seconds\n",
  601                                     m->mtx_object.lo_name, (void *)m->mtx_lock);
  602 #ifdef WITNESS
  603                                 witness_display_spinlock(&m->mtx_object,
  604                                     mtx_owner(m));
  605 #endif
  606                                 panic("spin lock held too long");
  607                         }
  608                         cpu_spinwait();
  609                 }
  610                 critical_enter();
  611         }
  612 
  613         if (LOCK_LOG_TEST(&m->mtx_object, opts))
  614                 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
  615 
  616         return;
  617 }
  618 #endif /* SMP */
  619 
  620 /*
  621  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
  622  *
  623  * We are only called here if the lock is recursed or contested (i.e. we
  624  * need to wake up a blocked thread).
  625  */
  626 void
  627 _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
  628 {
  629         struct turnstile *ts;
  630 #ifndef PREEMPTION
  631         struct thread *td, *td1;
  632 #endif
  633 
  634         if (mtx_recursed(m)) {
  635                 if (--(m->mtx_recurse) == 0)
  636                         atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
  637                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  638                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
  639                 return;
  640         }
  641 
  642         ts = turnstile_lookup(&m->mtx_object);
  643         if (LOCK_LOG_TEST(&m->mtx_object, opts))
  644                 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
  645 
  646 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
  647         if (ts == NULL) {
  648                 _release_lock_quick(m);
  649                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  650                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
  651                 turnstile_release(&m->mtx_object);
  652                 return;
  653         }
  654 #else
  655         MPASS(ts != NULL);
  656 #endif
  657 #ifndef PREEMPTION
  658         /* XXX */
  659         td1 = turnstile_head(ts);
  660 #endif
  661 #ifdef MUTEX_WAKE_ALL
  662         turnstile_broadcast(ts);
  663         _release_lock_quick(m);
  664 #else
  665         if (turnstile_signal(ts)) {
  666                 _release_lock_quick(m);
  667                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  668                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
  669         } else {
  670                 m->mtx_lock = MTX_CONTESTED;
  671                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  672                         CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
  673                             m);
  674         }
  675 #endif
  676         turnstile_unpend(ts);
  677 
  678 #ifndef PREEMPTION
  679         /*
  680          * XXX: This is just a hack until preemption is done.  However,
  681          * once preemption is done we need to either wrap the
  682          * turnstile_signal() and release of the actual lock in an
  683          * extra critical section or change the preemption code to
  684          * always just set a flag and never do instant-preempts.
  685          */
  686         td = curthread;
  687         if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
  688                 return;
  689         mtx_lock_spin(&sched_lock);
  690         if (!TD_IS_RUNNING(td1)) {
  691 #ifdef notyet
  692                 if (td->td_ithd != NULL) {
  693                         struct ithd *it = td->td_ithd;
  694 
  695                         if (it->it_interrupted) {
  696                                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  697                                         CTR2(KTR_LOCK,
  698                                     "_mtx_unlock_sleep: %p interrupted %p",
  699                                             it, it->it_interrupted);
  700                                 intr_thd_fixup(it);
  701                         }
  702                 }
  703 #endif
  704                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  705                         CTR2(KTR_LOCK,
  706                             "_mtx_unlock_sleep: %p switching out lock=%p", m,
  707                             (void *)m->mtx_lock);
  708 
  709                 mi_switch(SW_INVOL, NULL);
  710                 if (LOCK_LOG_TEST(&m->mtx_object, opts))
  711                         CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
  712                             m, (void *)m->mtx_lock);
  713         }
  714         mtx_unlock_spin(&sched_lock);
  715 #endif
  716 
  717         return;
  718 }
  719 
  720 /*
  721  * All the unlocking of MTX_SPIN locks is done inline.
  722  * See the _rel_spin_lock() macro for the details.
  723  */
  724 
  725 /*
  726  * The backing function for the INVARIANTS-enabled mtx_assert()
  727  */
  728 #ifdef INVARIANT_SUPPORT
  729 void
  730 _mtx_assert(struct mtx *m, int what, const char *file, int line)
  731 {
  732 
  733         if (panicstr != NULL)
  734                 return;
  735         switch (what) {
  736         case MA_OWNED:
  737         case MA_OWNED | MA_RECURSED:
  738         case MA_OWNED | MA_NOTRECURSED:
  739                 if (!mtx_owned(m))
  740                         panic("mutex %s not owned at %s:%d",
  741                             m->mtx_object.lo_name, file, line);
  742                 if (mtx_recursed(m)) {
  743                         if ((what & MA_NOTRECURSED) != 0)
  744                                 panic("mutex %s recursed at %s:%d",
  745                                     m->mtx_object.lo_name, file, line);
  746                 } else if ((what & MA_RECURSED) != 0) {
  747                         panic("mutex %s unrecursed at %s:%d",
  748                             m->mtx_object.lo_name, file, line);
  749                 }
  750                 break;
  751         case MA_NOTOWNED:
  752                 if (mtx_owned(m))
  753                         panic("mutex %s owned at %s:%d",
  754                             m->mtx_object.lo_name, file, line);
  755                 break;
  756         default:
  757                 panic("unknown mtx_assert at %s:%d", file, line);
  758         }
  759 }
  760 #endif
  761 
  762 /*
  763  * The MUTEX_DEBUG-enabled mtx_validate()
  764  *
  765  * Most of these checks have been moved off into the LO_INITIALIZED flag
  766  * maintained by the witness code.
  767  */
  768 #ifdef MUTEX_DEBUG
  769 
  770 void    mtx_validate(struct mtx *);
  771 
  772 void
  773 mtx_validate(struct mtx *m)
  774 {
  775 
  776 /*
  777  * XXX: When kernacc() does not require Giant we can reenable this check
  778  */
  779 #ifdef notyet
  780 /*
  781  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
  782  * we can re-enable the kernacc() checks.
  783  */
  784 #ifndef __alpha__
  785         /*
  786          * Can't call kernacc() from early init386(), especially when
  787          * initializing Giant mutex, because some stuff in kernacc()
  788          * requires Giant itself.
  789          */
  790         if (!cold)
  791                 if (!kernacc((caddr_t)m, sizeof(m),
  792                     VM_PROT_READ | VM_PROT_WRITE))
  793                         panic("Can't read and write to mutex %p", m);
  794 #endif
  795 #endif
  796 }
  797 #endif
  798 
  799 /*
  800  * General init routine used by the MTX_SYSINIT() macro.
  801  */
  802 void
  803 mtx_sysinit(void *arg)
  804 {
  805         struct mtx_args *margs = arg;
  806 
  807         mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
  808 }
  809 
  810 /*
  811  * Mutex initialization routine; initialize lock `m' of type contained in
  812  * `opts' with options contained in `opts' and name `name.'  The optional
  813  * lock type `type' is used as a general lock category name for use with
  814  * witness.
  815  */
  816 void
  817 mtx_init(struct mtx *m, const char *name, const char *type, int opts)
  818 {
  819         struct lock_object *lock;
  820 
  821         MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
  822             MTX_NOWITNESS | MTX_DUPOK)) == 0);
  823 
  824 #ifdef MUTEX_DEBUG
  825         /* Diagnostic and error correction */
  826         mtx_validate(m);
  827 #endif
  828 
  829         lock = &m->mtx_object;
  830         KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
  831             ("mutex \"%s\" %p already initialized", name, m));
  832         bzero(m, sizeof(*m));
  833         if (opts & MTX_SPIN)
  834                 lock->lo_class = &lock_class_mtx_spin;
  835         else
  836                 lock->lo_class = &lock_class_mtx_sleep;
  837         lock->lo_name = name;
  838         lock->lo_type = type != NULL ? type : name;
  839         if (opts & MTX_QUIET)
  840                 lock->lo_flags = LO_QUIET;
  841         if (opts & MTX_RECURSE)
  842                 lock->lo_flags |= LO_RECURSABLE;
  843         if ((opts & MTX_NOWITNESS) == 0)
  844                 lock->lo_flags |= LO_WITNESS;
  845         if (opts & MTX_DUPOK)
  846                 lock->lo_flags |= LO_DUPOK;
  847 
  848         m->mtx_lock = MTX_UNOWNED;
  849 
  850         LOCK_LOG_INIT(lock, opts);
  851 
  852         WITNESS_INIT(lock);
  853 }
  854 
  855 /*
  856  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
  857  * passed in as a flag here because if the corresponding mtx_init() was
  858  * called with MTX_QUIET set, then it will already be set in the mutex's
  859  * flags.
  860  */
  861 void
  862 mtx_destroy(struct mtx *m)
  863 {
  864 
  865         LOCK_LOG_DESTROY(&m->mtx_object, 0);
  866 
  867         if (!mtx_owned(m))
  868                 MPASS(mtx_unowned(m));
  869         else {
  870                 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
  871 
  872                 /* Tell witness this isn't locked to make it happy. */
  873                 WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
  874                     __LINE__);
  875         }
  876 
  877         WITNESS_DESTROY(&m->mtx_object);
  878 }
  879 
  880 /*
  881  * Intialize the mutex code and system mutexes.  This is called from the MD
  882  * startup code prior to mi_startup().  The per-CPU data space needs to be
  883  * setup before this is called.
  884  */
  885 void
  886 mutex_init(void)
  887 {
  888 
  889         /* Setup thread0 so that mutexes work. */
  890         LIST_INIT(&thread0.td_contested);
  891 
  892         /* Setup turnstiles so that sleep mutexes work. */
  893         init_turnstiles();
  894 
  895         /*
  896          * Initialize mutexes.
  897          */
  898         mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
  899         mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
  900         mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
  901         mtx_lock(&Giant);
  902 }

Cache object: 451c779267d41718491300e211c7728e


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