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_sx.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) 2007 Attilio Rao <attilio@freebsd.org>
    3  * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice(s), this list of conditions and the following disclaimer as
   11  *    the first lines of this file unmodified other than the possible
   12  *    addition of one or more copyright notices.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice(s), this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
   18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   20  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
   21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
   27  * DAMAGE.
   28  */
   29 
   30 /*
   31  * Shared/exclusive locks.  This implementation attempts to ensure
   32  * deterministic lock granting behavior, so that slocks and xlocks are
   33  * interleaved.
   34  *
   35  * Priority propagation will not generally raise the priority of lock holders,
   36  * so should not be relied upon in combination with sx locks.
   37  */
   38 
   39 #include "opt_adaptive_sx.h"
   40 #include "opt_ddb.h"
   41 
   42 #include <sys/cdefs.h>
   43 __FBSDID("$FreeBSD: releng/7.4/sys/kern/kern_sx.c 197984 2009-10-12 15:56:27Z attilio $");
   44 
   45 #include <sys/param.h>
   46 #include <sys/ktr.h>
   47 #include <sys/lock.h>
   48 #include <sys/mutex.h>
   49 #include <sys/proc.h>
   50 #include <sys/sleepqueue.h>
   51 #include <sys/sx.h>
   52 #include <sys/systm.h>
   53 
   54 #ifdef ADAPTIVE_SX
   55 #include <machine/cpu.h>
   56 #endif
   57 
   58 #ifdef DDB
   59 #include <ddb/ddb.h>
   60 #endif
   61 
   62 #if !defined(SMP) && defined(ADAPTIVE_SX)
   63 #error "You must have SMP to enable the ADAPTIVE_SX option"
   64 #endif
   65 
   66 CTASSERT(((SX_ADAPTIVESPIN | SX_RECURSE) & LO_CLASSFLAGS) ==
   67     (SX_ADAPTIVESPIN | SX_RECURSE));
   68 
   69 /* Handy macros for sleep queues. */
   70 #define SQ_EXCLUSIVE_QUEUE      0
   71 #define SQ_SHARED_QUEUE         1
   72 
   73 /*
   74  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
   75  * drop Giant anytime we have to sleep or if we adaptively spin.
   76  */
   77 #define GIANT_DECLARE                                                   \
   78         int _giantcnt = 0;                                              \
   79         WITNESS_SAVE_DECL(Giant)                                        \
   80 
   81 #define GIANT_SAVE() do {                                               \
   82         if (mtx_owned(&Giant)) {                                        \
   83                 WITNESS_SAVE(&Giant.lock_object, Giant);                \
   84                 while (mtx_owned(&Giant)) {                             \
   85                         _giantcnt++;                                    \
   86                         mtx_unlock(&Giant);                             \
   87                 }                                                       \
   88         }                                                               \
   89 } while (0)
   90 
   91 #define GIANT_RESTORE() do {                                            \
   92         if (_giantcnt > 0) {                                            \
   93                 mtx_assert(&Giant, MA_NOTOWNED);                        \
   94                 while (_giantcnt--)                                     \
   95                         mtx_lock(&Giant);                               \
   96                 WITNESS_RESTORE(&Giant.lock_object, Giant);             \
   97         }                                                               \
   98 } while (0)
   99 
  100 /*
  101  * Returns true if an exclusive lock is recursed.  It assumes
  102  * curthread currently has an exclusive lock.
  103  */
  104 #define sx_recursed(sx)         ((sx)->sx_recurse != 0)
  105 
  106 #ifdef DDB
  107 static void     db_show_sx(struct lock_object *lock);
  108 #endif
  109 static void     lock_sx(struct lock_object *lock, int how);
  110 static int      unlock_sx(struct lock_object *lock);
  111 
  112 struct lock_class lock_class_sx = {
  113         .lc_name = "sx",
  114         .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
  115 #ifdef DDB
  116         .lc_ddb_show = db_show_sx,
  117 #endif
  118         .lc_lock = lock_sx,
  119         .lc_unlock = unlock_sx,
  120 };
  121 
  122 #ifndef INVARIANTS
  123 #define _sx_assert(sx, what, file, line)
  124 #endif
  125 
  126 void
  127 lock_sx(struct lock_object *lock, int how)
  128 {
  129         struct sx *sx;
  130 
  131         sx = (struct sx *)lock;
  132         if (how)
  133                 sx_xlock(sx);
  134         else
  135                 sx_slock(sx);
  136 }
  137 
  138 int
  139 unlock_sx(struct lock_object *lock)
  140 {
  141         struct sx *sx;
  142 
  143         sx = (struct sx *)lock;
  144         sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
  145         if (sx_xlocked(sx)) {
  146                 sx_xunlock(sx);
  147                 return (1);
  148         } else {
  149                 sx_sunlock(sx);
  150                 return (0);
  151         }
  152 }
  153 
  154 void
  155 sx_sysinit(void *arg)
  156 {
  157         struct sx_args *sargs = arg;
  158 
  159         sx_init(sargs->sa_sx, sargs->sa_desc);
  160 }
  161 
  162 void
  163 sx_init_flags(struct sx *sx, const char *description, int opts)
  164 {
  165         int flags;
  166 
  167         MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
  168             SX_NOPROFILE | SX_ADAPTIVESPIN)) == 0);
  169         ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
  170             ("%s: sx_lock not aligned for %s:%p", __func__, description,
  171             &sx->sx_lock));
  172 
  173         flags = LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE;
  174         if (opts & SX_DUPOK)
  175                 flags |= LO_DUPOK;
  176         if (opts & SX_NOPROFILE)
  177                 flags |= LO_NOPROFILE;
  178         if (!(opts & SX_NOWITNESS))
  179                 flags |= LO_WITNESS;
  180         if (opts & SX_QUIET)
  181                 flags |= LO_QUIET;
  182 
  183         flags |= opts & (SX_ADAPTIVESPIN | SX_RECURSE);
  184         sx->sx_lock = SX_LOCK_UNLOCKED;
  185         sx->sx_recurse = 0;
  186         lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
  187 }
  188 
  189 void
  190 sx_destroy(struct sx *sx)
  191 {
  192 
  193         KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
  194         KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
  195         sx->sx_lock = SX_LOCK_DESTROYED;
  196         lock_destroy(&sx->lock_object);
  197 }
  198 
  199 int
  200 _sx_slock(struct sx *sx, int opts, const char *file, int line)
  201 {
  202         int error = 0;
  203 
  204         MPASS(curthread != NULL);
  205         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  206             ("sx_slock() of destroyed sx @ %s:%d", file, line));
  207         WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line);
  208         error = __sx_slock(sx, opts, file, line);
  209         if (!error) {
  210                 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
  211                 WITNESS_LOCK(&sx->lock_object, 0, file, line);
  212                 curthread->td_locks++;
  213         }
  214 
  215         return (error);
  216 }
  217 
  218 int
  219 _sx_try_slock(struct sx *sx, const char *file, int line)
  220 {
  221         uintptr_t x;
  222 
  223         for (;;) {
  224                 x = sx->sx_lock;
  225                 KASSERT(x != SX_LOCK_DESTROYED,
  226                     ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
  227                 if (!(x & SX_LOCK_SHARED))
  228                         break;
  229                 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
  230                         LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
  231                         WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
  232                         curthread->td_locks++;
  233                         return (1);
  234                 }
  235         }
  236 
  237         LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
  238         return (0);
  239 }
  240 
  241 int
  242 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
  243 {
  244         int error = 0;
  245 
  246         MPASS(curthread != NULL);
  247         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  248             ("sx_xlock() of destroyed sx @ %s:%d", file, line));
  249         WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
  250             line);
  251         error = __sx_xlock(sx, curthread, opts, file, line);
  252         if (!error) {
  253                 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
  254                     file, line);
  255                 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
  256                 curthread->td_locks++;
  257         }
  258 
  259         return (error);
  260 }
  261 
  262 int
  263 _sx_try_xlock(struct sx *sx, const char *file, int line)
  264 {
  265         int rval;
  266 
  267         MPASS(curthread != NULL);
  268         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  269             ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
  270 
  271         if (sx_xlocked(sx) && (sx->lock_object.lo_flags & SX_RECURSE) != 0) {
  272                 sx->sx_recurse++;
  273                 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
  274                 rval = 1;
  275         } else
  276                 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
  277                     (uintptr_t)curthread);
  278         LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
  279         if (rval) {
  280                 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
  281                     file, line);
  282                 curthread->td_locks++;
  283         }
  284 
  285         return (rval);
  286 }
  287 
  288 void
  289 _sx_sunlock(struct sx *sx, const char *file, int line)
  290 {
  291 
  292         MPASS(curthread != NULL);
  293         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  294             ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
  295         _sx_assert(sx, SA_SLOCKED, file, line);
  296         curthread->td_locks--;
  297         WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
  298         LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
  299 #ifdef LOCK_PROFILING_SHARED
  300         if (SX_SHARERS(sx->sx_lock) == 1)
  301                 lock_profile_release_lock(&sx->lock_object);
  302 #endif
  303         __sx_sunlock(sx, file, line);
  304 }
  305 
  306 void
  307 _sx_xunlock(struct sx *sx, const char *file, int line)
  308 {
  309 
  310         MPASS(curthread != NULL);
  311         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  312             ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
  313         _sx_assert(sx, SA_XLOCKED, file, line);
  314         curthread->td_locks--;
  315         WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
  316         LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
  317             line);
  318         if (!sx_recursed(sx))
  319                 lock_profile_release_lock(&sx->lock_object);
  320         __sx_xunlock(sx, curthread, file, line);
  321 }
  322 
  323 /*
  324  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
  325  * This will only succeed if this thread holds a single shared lock.
  326  * Return 1 if if the upgrade succeed, 0 otherwise.
  327  */
  328 int
  329 _sx_try_upgrade(struct sx *sx, const char *file, int line)
  330 {
  331         uintptr_t x;
  332         int success;
  333 
  334         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  335             ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
  336         _sx_assert(sx, SA_SLOCKED, file, line);
  337 
  338         /*
  339          * Try to switch from one shared lock to an exclusive lock.  We need
  340          * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
  341          * we will wake up the exclusive waiters when we drop the lock.
  342          */
  343         x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
  344         success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
  345             (uintptr_t)curthread | x);
  346         LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
  347         if (success)
  348                 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
  349                     file, line);
  350         return (success);
  351 }
  352 
  353 /*
  354  * Downgrade an unrecursed exclusive lock into a single shared lock.
  355  */
  356 void
  357 _sx_downgrade(struct sx *sx, const char *file, int line)
  358 {
  359         uintptr_t x;
  360         int wakeup_swapper;
  361 
  362         KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
  363             ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
  364         _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
  365 #ifndef INVARIANTS
  366         if (sx_recursed(sx))
  367                 panic("downgrade of a recursed lock");
  368 #endif
  369 
  370         WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
  371 
  372         /*
  373          * Try to switch from an exclusive lock with no shared waiters
  374          * to one sharer with no shared waiters.  If there are
  375          * exclusive waiters, we don't need to lock the sleep queue so
  376          * long as we preserve the flag.  We do one quick try and if
  377          * that fails we grab the sleepq lock to keep the flags from
  378          * changing and do it the slow way.
  379          *
  380          * We have to lock the sleep queue if there are shared waiters
  381          * so we can wake them up.
  382          */
  383         x = sx->sx_lock;
  384         if (!(x & SX_LOCK_SHARED_WAITERS) &&
  385             atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
  386             (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
  387                 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
  388                 return;
  389         }
  390 
  391         /*
  392          * Lock the sleep queue so we can read the waiters bits
  393          * without any races and wakeup any shared waiters.
  394          */
  395         sleepq_lock(&sx->lock_object);
  396 
  397         /*
  398          * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
  399          * shared lock.  If there are any shared waiters, wake them up.
  400          */
  401         wakeup_swapper = 0;
  402         x = sx->sx_lock;
  403         atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
  404             (x & SX_LOCK_EXCLUSIVE_WAITERS));
  405         if (x & SX_LOCK_SHARED_WAITERS)
  406                 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
  407                     -1, SQ_SHARED_QUEUE);
  408         else
  409                 sleepq_release(&sx->lock_object);
  410 
  411         LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
  412 
  413         if (wakeup_swapper)
  414                 kick_proc0();
  415 }
  416 
  417 /*
  418  * This function represents the so-called 'hard case' for sx_xlock
  419  * operation.  All 'easy case' failures are redirected to this.  Note
  420  * that ideally this would be a static function, but it needs to be
  421  * accessible from at least sx.h.
  422  */
  423 int
  424 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
  425     int line)
  426 {
  427         GIANT_DECLARE;
  428 #ifdef ADAPTIVE_SX
  429         volatile struct thread *owner;
  430 #endif
  431         uint64_t waittime = 0;
  432         uintptr_t x;
  433         int contested = 0, error = 0;
  434 
  435         /* If we already hold an exclusive lock, then recurse. */
  436         if (sx_xlocked(sx)) {
  437                 KASSERT((sx->lock_object.lo_flags & SX_RECURSE) != 0,
  438             ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
  439                     sx->lock_object.lo_name, file, line));
  440                 sx->sx_recurse++;
  441                 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
  442                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  443                         CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
  444                 return (0);
  445         }
  446 
  447         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  448                 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
  449                     sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
  450 
  451         while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
  452 #ifdef ADAPTIVE_SX
  453                 /*
  454                  * If the lock is write locked and the owner is
  455                  * running on another CPU, spin until the owner stops
  456                  * running or the state of the lock changes.
  457                  */
  458                 x = sx->sx_lock;
  459                 if (!(x & SX_LOCK_SHARED) &&
  460                     (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
  461                         x = SX_OWNER(x);
  462                         owner = (struct thread *)x;
  463                         if (TD_IS_RUNNING(owner)) {
  464                                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  465                                         CTR3(KTR_LOCK,
  466                                             "%s: spinning on %p held by %p",
  467                                             __func__, sx, owner);
  468                                 GIANT_SAVE();
  469                                 lock_profile_obtain_lock_failed(
  470                                     &sx->lock_object, &contested, &waittime);
  471                                 while (SX_OWNER(sx->sx_lock) == x &&
  472                                     TD_IS_RUNNING(owner))
  473                                         cpu_spinwait();
  474                                 continue;
  475                         }
  476                 }
  477 #endif
  478 
  479                 sleepq_lock(&sx->lock_object);
  480                 x = sx->sx_lock;
  481 
  482                 /*
  483                  * If the lock was released while spinning on the
  484                  * sleep queue chain lock, try again.
  485                  */
  486                 if (x == SX_LOCK_UNLOCKED) {
  487                         sleepq_release(&sx->lock_object);
  488                         continue;
  489                 }
  490 
  491 #ifdef ADAPTIVE_SX
  492                 /*
  493                  * The current lock owner might have started executing
  494                  * on another CPU (or the lock could have changed
  495                  * owners) while we were waiting on the sleep queue
  496                  * chain lock.  If so, drop the sleep queue lock and try
  497                  * again.
  498                  */
  499                 if (!(x & SX_LOCK_SHARED) &&
  500                     (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
  501                         owner = (struct thread *)SX_OWNER(x);
  502                         if (TD_IS_RUNNING(owner)) {
  503                                 sleepq_release(&sx->lock_object);
  504                                 continue;
  505                         }
  506                 }
  507 #endif
  508 
  509                 /*
  510                  * If an exclusive lock was released with both shared
  511                  * and exclusive waiters and a shared waiter hasn't
  512                  * woken up and acquired the lock yet, sx_lock will be
  513                  * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
  514                  * If we see that value, try to acquire it once.  Note
  515                  * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
  516                  * as there are other exclusive waiters still.  If we
  517                  * fail, restart the loop.
  518                  */
  519                 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
  520                         if (atomic_cmpset_acq_ptr(&sx->sx_lock,
  521                             SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
  522                             tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
  523                                 sleepq_release(&sx->lock_object);
  524                                 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
  525                                     __func__, sx);
  526                                 break;
  527                         }
  528                         sleepq_release(&sx->lock_object);
  529                         continue;
  530                 }
  531 
  532                 /*
  533                  * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
  534                  * than loop back and retry.
  535                  */
  536                 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
  537                         if (!atomic_cmpset_ptr(&sx->sx_lock, x,
  538                             x | SX_LOCK_EXCLUSIVE_WAITERS)) {
  539                                 sleepq_release(&sx->lock_object);
  540                                 continue;
  541                         }
  542                         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  543                                 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
  544                                     __func__, sx);
  545                 }
  546 
  547                 /*
  548                  * Since we have been unable to acquire the exclusive
  549                  * lock and the exclusive waiters flag is set, we have
  550                  * to sleep.
  551                  */
  552                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  553                         CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
  554                             __func__, sx);
  555 
  556                 GIANT_SAVE();
  557                 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
  558                     &waittime);
  559                 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
  560                     SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
  561                     SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
  562                 if (!(opts & SX_INTERRUPTIBLE))
  563                         sleepq_wait(&sx->lock_object);
  564                 else
  565                         error = sleepq_wait_sig(&sx->lock_object);
  566 
  567                 if (error) {
  568                         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  569                                 CTR2(KTR_LOCK,
  570                         "%s: interruptible sleep by %p suspended by signal",
  571                                     __func__, sx);
  572                         break;
  573                 }
  574                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  575                         CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
  576                             __func__, sx);
  577         }
  578 
  579         GIANT_RESTORE();
  580         if (!error)
  581                 lock_profile_obtain_lock_success(&sx->lock_object, contested,
  582                     waittime, file, line);
  583         return (error);
  584 }
  585 
  586 /*
  587  * This function represents the so-called 'hard case' for sx_xunlock
  588  * operation.  All 'easy case' failures are redirected to this.  Note
  589  * that ideally this would be a static function, but it needs to be
  590  * accessible from at least sx.h.
  591  */
  592 void
  593 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
  594 {
  595         uintptr_t x;
  596         int queue, wakeup_swapper;
  597 
  598         MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
  599 
  600         /* If the lock is recursed, then unrecurse one level. */
  601         if (sx_xlocked(sx) && sx_recursed(sx)) {
  602                 if ((--sx->sx_recurse) == 0)
  603                         atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
  604                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  605                         CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
  606                 return;
  607         }
  608         MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
  609             SX_LOCK_EXCLUSIVE_WAITERS));
  610         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  611                 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
  612 
  613         sleepq_lock(&sx->lock_object);
  614         x = SX_LOCK_UNLOCKED;
  615 
  616         /*
  617          * The wake up algorithm here is quite simple and probably not
  618          * ideal.  It gives precedence to shared waiters if they are
  619          * present.  For this condition, we have to preserve the
  620          * state of the exclusive waiters flag.
  621          */
  622         if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) {
  623                 queue = SQ_SHARED_QUEUE;
  624                 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
  625         } else
  626                 queue = SQ_EXCLUSIVE_QUEUE;
  627 
  628         /* Wake up all the waiters for the specific queue. */
  629         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  630                 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
  631                     __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
  632                     "exclusive");
  633         atomic_store_rel_ptr(&sx->sx_lock, x);
  634         wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1,
  635             queue);
  636         if (wakeup_swapper)
  637                 kick_proc0();
  638 }
  639 
  640 /*
  641  * This function represents the so-called 'hard case' for sx_slock
  642  * operation.  All 'easy case' failures are redirected to this.  Note
  643  * that ideally this would be a static function, but it needs to be
  644  * accessible from at least sx.h.
  645  */
  646 int
  647 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
  648 {
  649         GIANT_DECLARE;
  650 #ifdef ADAPTIVE_SX
  651         volatile struct thread *owner;
  652 #endif
  653 #ifdef LOCK_PROFILING_SHARED
  654         uint64_t waittime = 0;
  655         int contested = 0;
  656 #endif
  657         uintptr_t x;
  658         int error = 0;
  659 
  660         /*
  661          * As with rwlocks, we don't make any attempt to try to block
  662          * shared locks once there is an exclusive waiter.
  663          */
  664         for (;;) {
  665                 x = sx->sx_lock;
  666 
  667                 /*
  668                  * If no other thread has an exclusive lock then try to bump up
  669                  * the count of sharers.  Since we have to preserve the state
  670                  * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
  671                  * shared lock loop back and retry.
  672                  */
  673                 if (x & SX_LOCK_SHARED) {
  674                         MPASS(!(x & SX_LOCK_SHARED_WAITERS));
  675                         if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
  676                             x + SX_ONE_SHARER)) {
  677 #ifdef LOCK_PROFILING_SHARED
  678                                 if (SX_SHARERS(x) == 0)
  679                                         lock_profile_obtain_lock_success(
  680                                             &sx->lock_object, contested,
  681                                             waittime, file, line);
  682 #endif
  683                                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  684                                         CTR4(KTR_LOCK,
  685                                             "%s: %p succeed %p -> %p", __func__,
  686                                             sx, (void *)x,
  687                                             (void *)(x + SX_ONE_SHARER));
  688                                 break;
  689                         }
  690                         continue;
  691                 }
  692 
  693 #ifdef ADAPTIVE_SX
  694                 /*
  695                  * If the owner is running on another CPU, spin until
  696                  * the owner stops running or the state of the lock
  697                  * changes.
  698                  */
  699                 else if (sx->lock_object.lo_flags & SX_ADAPTIVESPIN) {
  700                         x = SX_OWNER(x);
  701                         owner = (struct thread *)x;
  702                         if (TD_IS_RUNNING(owner)) {
  703                                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  704                                         CTR3(KTR_LOCK,
  705                                             "%s: spinning on %p held by %p",
  706                                             __func__, sx, owner);
  707                                 GIANT_SAVE();
  708 #ifdef LOCK_PROFILING_SHARED
  709                                 lock_profile_obtain_lock_failed(
  710                                     &sx->lock_object, &contested, &waittime);
  711 #endif
  712                                 while (SX_OWNER(sx->sx_lock) == x &&
  713                                     TD_IS_RUNNING(owner))
  714                                         cpu_spinwait();
  715                                 continue;
  716                         }
  717                 }
  718 #endif
  719 
  720                 /*
  721                  * Some other thread already has an exclusive lock, so
  722                  * start the process of blocking.
  723                  */
  724                 sleepq_lock(&sx->lock_object);
  725                 x = sx->sx_lock;
  726 
  727                 /*
  728                  * The lock could have been released while we spun.
  729                  * In this case loop back and retry.
  730                  */
  731                 if (x & SX_LOCK_SHARED) {
  732                         sleepq_release(&sx->lock_object);
  733                         continue;
  734                 }
  735 
  736 #ifdef ADAPTIVE_SX
  737                 /*
  738                  * If the owner is running on another CPU, spin until
  739                  * the owner stops running or the state of the lock
  740                  * changes.
  741                  */
  742                 if (!(x & SX_LOCK_SHARED) &&
  743                     (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) {
  744                         owner = (struct thread *)SX_OWNER(x);
  745                         if (TD_IS_RUNNING(owner)) {
  746                                 sleepq_release(&sx->lock_object);
  747                                 continue;
  748                         }
  749                 }
  750 #endif
  751 
  752                 /*
  753                  * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
  754                  * fail to set it drop the sleep queue lock and loop
  755                  * back.
  756                  */
  757                 if (!(x & SX_LOCK_SHARED_WAITERS)) {
  758                         if (!atomic_cmpset_ptr(&sx->sx_lock, x,
  759                             x | SX_LOCK_SHARED_WAITERS)) {
  760                                 sleepq_release(&sx->lock_object);
  761                                 continue;
  762                         }
  763                         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  764                                 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
  765                                     __func__, sx);
  766                 }
  767 
  768                 /*
  769                  * Since we have been unable to acquire the shared lock,
  770                  * we have to sleep.
  771                  */
  772                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  773                         CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
  774                             __func__, sx);
  775 
  776                 GIANT_SAVE();
  777 #ifdef LOCK_PROFILING_SHARED
  778                 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
  779                     &waittime);
  780 #endif
  781                 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
  782                     SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
  783                     SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
  784                 if (!(opts & SX_INTERRUPTIBLE))
  785                         sleepq_wait(&sx->lock_object);
  786                 else
  787                         error = sleepq_wait_sig(&sx->lock_object);
  788 
  789                 if (error) {
  790                         if (LOCK_LOG_TEST(&sx->lock_object, 0))
  791                                 CTR2(KTR_LOCK,
  792                         "%s: interruptible sleep by %p suspended by signal",
  793                                     __func__, sx);
  794                         break;
  795                 }
  796                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  797                         CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
  798                             __func__, sx);
  799         }
  800 
  801         GIANT_RESTORE();
  802         return (error);
  803 }
  804 
  805 /*
  806  * This function represents the so-called 'hard case' for sx_sunlock
  807  * operation.  All 'easy case' failures are redirected to this.  Note
  808  * that ideally this would be a static function, but it needs to be
  809  * accessible from at least sx.h.
  810  */
  811 void
  812 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
  813 {
  814         uintptr_t x;
  815         int wakeup_swapper;
  816 
  817         for (;;) {
  818                 x = sx->sx_lock;
  819 
  820                 /*
  821                  * We should never have sharers while at least one thread
  822                  * holds a shared lock.
  823                  */
  824                 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
  825                     ("%s: waiting sharers", __func__));
  826 
  827                 /*
  828                  * See if there is more than one shared lock held.  If
  829                  * so, just drop one and return.
  830                  */
  831                 if (SX_SHARERS(x) > 1) {
  832                         if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
  833                             x - SX_ONE_SHARER)) {
  834                                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  835                                         CTR4(KTR_LOCK,
  836                                             "%s: %p succeeded %p -> %p",
  837                                             __func__, sx, (void *)x,
  838                                             (void *)(x - SX_ONE_SHARER));
  839                                 break;
  840                         }
  841                         continue;
  842                 }
  843 
  844                 /*
  845                  * If there aren't any waiters for an exclusive lock,
  846                  * then try to drop it quickly.
  847                  */
  848                 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
  849                         MPASS(x == SX_SHARERS_LOCK(1));
  850                         if (atomic_cmpset_rel_ptr(&sx->sx_lock,
  851                             SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
  852                                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  853                                         CTR2(KTR_LOCK, "%s: %p last succeeded",
  854                                             __func__, sx);
  855                                 break;
  856                         }
  857                         continue;
  858                 }
  859 
  860                 /*
  861                  * At this point, there should just be one sharer with
  862                  * exclusive waiters.
  863                  */
  864                 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
  865 
  866                 sleepq_lock(&sx->lock_object);
  867 
  868                 /*
  869                  * Wake up semantic here is quite simple:
  870                  * Just wake up all the exclusive waiters.
  871                  * Note that the state of the lock could have changed,
  872                  * so if it fails loop back and retry.
  873                  */
  874                 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
  875                     SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
  876                     SX_LOCK_UNLOCKED)) {
  877                         sleepq_release(&sx->lock_object);
  878                         continue;
  879                 }
  880                 if (LOCK_LOG_TEST(&sx->lock_object, 0))
  881                         CTR2(KTR_LOCK, "%s: %p waking up all thread on"
  882                             "exclusive queue", __func__, sx);
  883                 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
  884                     -1, SQ_EXCLUSIVE_QUEUE);
  885                 if (wakeup_swapper)
  886                         kick_proc0();
  887                 break;
  888         }
  889 }
  890 
  891 #ifdef INVARIANT_SUPPORT
  892 #ifndef INVARIANTS
  893 #undef  _sx_assert
  894 #endif
  895 
  896 /*
  897  * In the non-WITNESS case, sx_assert() can only detect that at least
  898  * *some* thread owns an slock, but it cannot guarantee that *this*
  899  * thread owns an slock.
  900  */
  901 void
  902 _sx_assert(struct sx *sx, int what, const char *file, int line)
  903 {
  904 #ifndef WITNESS
  905         int slocked = 0;
  906 #endif
  907 
  908         if (panicstr != NULL)
  909                 return;
  910         switch (what) {
  911         case SA_SLOCKED:
  912         case SA_SLOCKED | SA_NOTRECURSED:
  913         case SA_SLOCKED | SA_RECURSED:
  914 #ifndef WITNESS
  915                 slocked = 1;
  916                 /* FALLTHROUGH */
  917 #endif
  918         case SA_LOCKED:
  919         case SA_LOCKED | SA_NOTRECURSED:
  920         case SA_LOCKED | SA_RECURSED:
  921 #ifdef WITNESS
  922                 witness_assert(&sx->lock_object, what, file, line);
  923 #else
  924                 /*
  925                  * If some other thread has an exclusive lock or we
  926                  * have one and are asserting a shared lock, fail.
  927                  * Also, if no one has a lock at all, fail.
  928                  */
  929                 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
  930                     (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
  931                     sx_xholder(sx) != curthread)))
  932                         panic("Lock %s not %slocked @ %s:%d\n",
  933                             sx->lock_object.lo_name, slocked ? "share " : "",
  934                             file, line);
  935 
  936                 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
  937                         if (sx_recursed(sx)) {
  938                                 if (what & SA_NOTRECURSED)
  939                                         panic("Lock %s recursed @ %s:%d\n",
  940                                             sx->lock_object.lo_name, file,
  941                                             line);
  942                         } else if (what & SA_RECURSED)
  943                                 panic("Lock %s not recursed @ %s:%d\n",
  944                                     sx->lock_object.lo_name, file, line);
  945                 }
  946 #endif
  947                 break;
  948         case SA_XLOCKED:
  949         case SA_XLOCKED | SA_NOTRECURSED:
  950         case SA_XLOCKED | SA_RECURSED:
  951                 if (sx_xholder(sx) != curthread)
  952                         panic("Lock %s not exclusively locked @ %s:%d\n",
  953                             sx->lock_object.lo_name, file, line);
  954                 if (sx_recursed(sx)) {
  955                         if (what & SA_NOTRECURSED)
  956                                 panic("Lock %s recursed @ %s:%d\n",
  957                                     sx->lock_object.lo_name, file, line);
  958                 } else if (what & SA_RECURSED)
  959                         panic("Lock %s not recursed @ %s:%d\n",
  960                             sx->lock_object.lo_name, file, line);
  961                 break;
  962         case SA_UNLOCKED:
  963 #ifdef WITNESS
  964                 witness_assert(&sx->lock_object, what, file, line);
  965 #else
  966                 /*
  967                  * If we hold an exclusve lock fail.  We can't
  968                  * reliably check to see if we hold a shared lock or
  969                  * not.
  970                  */
  971                 if (sx_xholder(sx) == curthread)
  972                         panic("Lock %s exclusively locked @ %s:%d\n",
  973                             sx->lock_object.lo_name, file, line);
  974 #endif
  975                 break;
  976         default:
  977                 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
  978                     line);
  979         }
  980 }
  981 #endif  /* INVARIANT_SUPPORT */
  982 
  983 #ifdef DDB
  984 static void
  985 db_show_sx(struct lock_object *lock)
  986 {
  987         struct thread *td;
  988         struct sx *sx;
  989 
  990         sx = (struct sx *)lock;
  991 
  992         db_printf(" state: ");
  993         if (sx->sx_lock == SX_LOCK_UNLOCKED)
  994                 db_printf("UNLOCKED\n");
  995         else if (sx->sx_lock == SX_LOCK_DESTROYED) {
  996                 db_printf("DESTROYED\n");
  997                 return;
  998         } else if (sx->sx_lock & SX_LOCK_SHARED)
  999                 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
 1000         else {
 1001                 td = sx_xholder(sx);
 1002                 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
 1003                     td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
 1004                 if (sx_recursed(sx))
 1005                         db_printf(" recursed: %d\n", sx->sx_recurse);
 1006         }
 1007 
 1008         db_printf(" waiters: ");
 1009         switch(sx->sx_lock &
 1010             (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
 1011         case SX_LOCK_SHARED_WAITERS:
 1012                 db_printf("shared\n");
 1013                 break;
 1014         case SX_LOCK_EXCLUSIVE_WAITERS:
 1015                 db_printf("exclusive\n");
 1016                 break;
 1017         case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
 1018                 db_printf("exclusive and shared\n");
 1019                 break;
 1020         default:
 1021                 db_printf("none\n");
 1022         }
 1023 }
 1024 
 1025 /*
 1026  * Check to see if a thread that is blocked on a sleep queue is actually
 1027  * blocked on an sx lock.  If so, output some details and return true.
 1028  * If the lock has an exclusive owner, return that in *ownerp.
 1029  */
 1030 int
 1031 sx_chain(struct thread *td, struct thread **ownerp)
 1032 {
 1033         struct sx *sx;
 1034 
 1035         /*
 1036          * Check to see if this thread is blocked on an sx lock.
 1037          * First, we check the lock class.  If that is ok, then we
 1038          * compare the lock name against the wait message.
 1039          */
 1040         sx = td->td_wchan;
 1041         if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
 1042             sx->lock_object.lo_name != td->td_wmesg)
 1043                 return (0);
 1044 
 1045         /* We think we have an sx lock, so output some details. */
 1046         db_printf("blocked on sx \"%s\" ", td->td_wmesg);
 1047         *ownerp = sx_xholder(sx);
 1048         if (sx->sx_lock & SX_LOCK_SHARED)
 1049                 db_printf("SLOCK (count %ju)\n",
 1050                     (uintmax_t)SX_SHARERS(sx->sx_lock));
 1051         else
 1052                 db_printf("XLOCK\n");
 1053         return (1);
 1054 }
 1055 #endif

Cache object: 713180a2dab66e36b4b7c52e6c4bc9d8


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