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_rwlock.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) 2006 John Baldwin <jhb@FreeBSD.org>
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 
   27 /*
   28  * Machine independent bits of reader/writer lock implementation.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD: releng/11.1/sys/kern/kern_rwlock.c 320241 2017-06-22 18:40:34Z markj $");
   33 
   34 #include "opt_ddb.h"
   35 #include "opt_hwpmc_hooks.h"
   36 #include "opt_no_adaptive_rwlocks.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/kdb.h>
   40 #include <sys/ktr.h>
   41 #include <sys/kernel.h>
   42 #include <sys/lock.h>
   43 #include <sys/mutex.h>
   44 #include <sys/proc.h>
   45 #include <sys/rwlock.h>
   46 #include <sys/sched.h>
   47 #include <sys/smp.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/systm.h>
   50 #include <sys/turnstile.h>
   51 
   52 #include <machine/cpu.h>
   53 
   54 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
   55 #define ADAPTIVE_RWLOCKS
   56 #endif
   57 
   58 #ifdef HWPMC_HOOKS
   59 #include <sys/pmckern.h>
   60 PMC_SOFT_DECLARE( , , lock, failed);
   61 #endif
   62 
   63 /*
   64  * Return the rwlock address when the lock cookie address is provided.
   65  * This functionality assumes that struct rwlock* have a member named rw_lock.
   66  */
   67 #define rwlock2rw(c)    (__containerof(c, struct rwlock, rw_lock))
   68 
   69 #ifdef DDB
   70 #include <ddb/ddb.h>
   71 
   72 static void     db_show_rwlock(const struct lock_object *lock);
   73 #endif
   74 static void     assert_rw(const struct lock_object *lock, int what);
   75 static void     lock_rw(struct lock_object *lock, uintptr_t how);
   76 #ifdef KDTRACE_HOOKS
   77 static int      owner_rw(const struct lock_object *lock, struct thread **owner);
   78 #endif
   79 static uintptr_t unlock_rw(struct lock_object *lock);
   80 
   81 struct lock_class lock_class_rw = {
   82         .lc_name = "rw",
   83         .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
   84         .lc_assert = assert_rw,
   85 #ifdef DDB
   86         .lc_ddb_show = db_show_rwlock,
   87 #endif
   88         .lc_lock = lock_rw,
   89         .lc_unlock = unlock_rw,
   90 #ifdef KDTRACE_HOOKS
   91         .lc_owner = owner_rw,
   92 #endif
   93 };
   94 
   95 #ifdef ADAPTIVE_RWLOCKS
   96 static int rowner_retries = 10;
   97 static int rowner_loops = 10000;
   98 static SYSCTL_NODE(_debug, OID_AUTO, rwlock, CTLFLAG_RD, NULL,
   99     "rwlock debugging");
  100 SYSCTL_INT(_debug_rwlock, OID_AUTO, retry, CTLFLAG_RW, &rowner_retries, 0, "");
  101 SYSCTL_INT(_debug_rwlock, OID_AUTO, loops, CTLFLAG_RW, &rowner_loops, 0, "");
  102 
  103 static struct lock_delay_config __read_mostly rw_delay;
  104 
  105 SYSCTL_INT(_debug_rwlock, OID_AUTO, delay_base, CTLFLAG_RW, &rw_delay.base,
  106     0, "");
  107 SYSCTL_INT(_debug_rwlock, OID_AUTO, delay_max, CTLFLAG_RW, &rw_delay.max,
  108     0, "");
  109 
  110 LOCK_DELAY_SYSINIT_DEFAULT(rw_delay);
  111 #endif
  112 
  113 /*
  114  * Return a pointer to the owning thread if the lock is write-locked or
  115  * NULL if the lock is unlocked or read-locked.
  116  */
  117 
  118 #define lv_rw_wowner(v)                                                 \
  119         ((v) & RW_LOCK_READ ? NULL :                                    \
  120          (struct thread *)RW_OWNER((v)))
  121 
  122 #define rw_wowner(rw)   lv_rw_wowner(RW_READ_VALUE(rw))
  123 
  124 /*
  125  * Returns if a write owner is recursed.  Write ownership is not assured
  126  * here and should be previously checked.
  127  */
  128 #define rw_recursed(rw)         ((rw)->rw_recurse != 0)
  129 
  130 /*
  131  * Return true if curthread helds the lock.
  132  */
  133 #define rw_wlocked(rw)          (rw_wowner((rw)) == curthread)
  134 
  135 /*
  136  * Return a pointer to the owning thread for this lock who should receive
  137  * any priority lent by threads that block on this lock.  Currently this
  138  * is identical to rw_wowner().
  139  */
  140 #define rw_owner(rw)            rw_wowner(rw)
  141 
  142 #ifndef INVARIANTS
  143 #define __rw_assert(c, what, file, line)
  144 #endif
  145 
  146 void
  147 assert_rw(const struct lock_object *lock, int what)
  148 {
  149 
  150         rw_assert((const struct rwlock *)lock, what);
  151 }
  152 
  153 void
  154 lock_rw(struct lock_object *lock, uintptr_t how)
  155 {
  156         struct rwlock *rw;
  157 
  158         rw = (struct rwlock *)lock;
  159         if (how)
  160                 rw_rlock(rw);
  161         else
  162                 rw_wlock(rw);
  163 }
  164 
  165 uintptr_t
  166 unlock_rw(struct lock_object *lock)
  167 {
  168         struct rwlock *rw;
  169 
  170         rw = (struct rwlock *)lock;
  171         rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
  172         if (rw->rw_lock & RW_LOCK_READ) {
  173                 rw_runlock(rw);
  174                 return (1);
  175         } else {
  176                 rw_wunlock(rw);
  177                 return (0);
  178         }
  179 }
  180 
  181 #ifdef KDTRACE_HOOKS
  182 int
  183 owner_rw(const struct lock_object *lock, struct thread **owner)
  184 {
  185         const struct rwlock *rw = (const struct rwlock *)lock;
  186         uintptr_t x = rw->rw_lock;
  187 
  188         *owner = rw_wowner(rw);
  189         return ((x & RW_LOCK_READ) != 0 ?  (RW_READERS(x) != 0) :
  190             (*owner != NULL));
  191 }
  192 #endif
  193 
  194 void
  195 _rw_init_flags(volatile uintptr_t *c, const char *name, int opts)
  196 {
  197         struct rwlock *rw;
  198         int flags;
  199 
  200         rw = rwlock2rw(c);
  201 
  202         MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
  203             RW_RECURSE | RW_NEW)) == 0);
  204         ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
  205             ("%s: rw_lock not aligned for %s: %p", __func__, name,
  206             &rw->rw_lock));
  207 
  208         flags = LO_UPGRADABLE;
  209         if (opts & RW_DUPOK)
  210                 flags |= LO_DUPOK;
  211         if (opts & RW_NOPROFILE)
  212                 flags |= LO_NOPROFILE;
  213         if (!(opts & RW_NOWITNESS))
  214                 flags |= LO_WITNESS;
  215         if (opts & RW_RECURSE)
  216                 flags |= LO_RECURSABLE;
  217         if (opts & RW_QUIET)
  218                 flags |= LO_QUIET;
  219         if (opts & RW_NEW)
  220                 flags |= LO_NEW;
  221 
  222         lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
  223         rw->rw_lock = RW_UNLOCKED;
  224         rw->rw_recurse = 0;
  225 }
  226 
  227 void
  228 _rw_destroy(volatile uintptr_t *c)
  229 {
  230         struct rwlock *rw;
  231 
  232         rw = rwlock2rw(c);
  233 
  234         KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw));
  235         KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw));
  236         rw->rw_lock = RW_DESTROYED;
  237         lock_destroy(&rw->lock_object);
  238 }
  239 
  240 void
  241 rw_sysinit(void *arg)
  242 {
  243         struct rw_args *args = arg;
  244 
  245         rw_init((struct rwlock *)args->ra_rw, args->ra_desc);
  246 }
  247 
  248 void
  249 rw_sysinit_flags(void *arg)
  250 {
  251         struct rw_args_flags *args = arg;
  252 
  253         rw_init_flags((struct rwlock *)args->ra_rw, args->ra_desc,
  254             args->ra_flags);
  255 }
  256 
  257 int
  258 _rw_wowned(const volatile uintptr_t *c)
  259 {
  260 
  261         return (rw_wowner(rwlock2rw(c)) == curthread);
  262 }
  263 
  264 void
  265 _rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line)
  266 {
  267         struct rwlock *rw;
  268         uintptr_t tid, v;
  269 
  270         rw = rwlock2rw(c);
  271 
  272         KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
  273             !TD_IS_IDLETHREAD(curthread),
  274             ("rw_wlock() by idle thread %p on rwlock %s @ %s:%d",
  275             curthread, rw->lock_object.lo_name, file, line));
  276         KASSERT(rw->rw_lock != RW_DESTROYED,
  277             ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
  278         WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
  279             line, NULL);
  280         tid = (uintptr_t)curthread;
  281         v = RW_UNLOCKED;
  282         if (!_rw_write_lock_fetch(rw, &v, tid))
  283                 _rw_wlock_hard(rw, v, tid, file, line);
  284         else
  285                 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw,
  286                     0, 0, file, line, LOCKSTAT_WRITER);
  287 
  288         LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
  289         WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
  290         TD_LOCKS_INC(curthread);
  291 }
  292 
  293 int
  294 __rw_try_wlock(volatile uintptr_t *c, const char *file, int line)
  295 {
  296         struct rwlock *rw;
  297         struct thread *td;
  298         uintptr_t tid, v;
  299         int rval;
  300         bool recursed;
  301 
  302         td = curthread;
  303         tid = (uintptr_t)td;
  304         if (SCHEDULER_STOPPED_TD(td))
  305                 return (1);
  306 
  307         rw = rwlock2rw(c);
  308 
  309         KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
  310             ("rw_try_wlock() by idle thread %p on rwlock %s @ %s:%d",
  311             curthread, rw->lock_object.lo_name, file, line));
  312         KASSERT(rw->rw_lock != RW_DESTROYED,
  313             ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
  314 
  315         rval = 1;
  316         recursed = false;
  317         v = RW_UNLOCKED;
  318         for (;;) {
  319                 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, &v, tid))
  320                         break;
  321                 if (v == RW_UNLOCKED)
  322                         continue;
  323                 if (v == tid && (rw->lock_object.lo_flags & LO_RECURSABLE)) {
  324                         rw->rw_recurse++;
  325                         atomic_set_ptr(&rw->rw_lock, RW_LOCK_WRITER_RECURSED);
  326                         break;
  327                 }
  328                 rval = 0;
  329                 break;
  330         }
  331 
  332         LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
  333         if (rval) {
  334                 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
  335                     file, line);
  336                 if (!recursed)
  337                         LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire,
  338                             rw, 0, 0, file, line, LOCKSTAT_WRITER);
  339                 TD_LOCKS_INC(curthread);
  340         }
  341         return (rval);
  342 }
  343 
  344 void
  345 _rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line)
  346 {
  347         struct rwlock *rw;
  348 
  349         rw = rwlock2rw(c);
  350 
  351         KASSERT(rw->rw_lock != RW_DESTROYED,
  352             ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
  353         __rw_assert(c, RA_WLOCKED, file, line);
  354         WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
  355         LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
  356             line);
  357 
  358 #ifdef LOCK_PROFILING
  359         _rw_wunlock_hard(rw, (uintptr_t)curthread, file, line);
  360 #else
  361         __rw_wunlock(rw, curthread, file, line);
  362 #endif
  363 
  364         TD_LOCKS_DEC(curthread);
  365 }
  366 
  367 /*
  368  * Determines whether a new reader can acquire a lock.  Succeeds if the
  369  * reader already owns a read lock and the lock is locked for read to
  370  * prevent deadlock from reader recursion.  Also succeeds if the lock
  371  * is unlocked and has no writer waiters or spinners.  Failing otherwise
  372  * prioritizes writers before readers.
  373  */
  374 #define RW_CAN_READ(td, _rw)                                            \
  375     (((td)->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) &   \
  376     (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) ==  \
  377     RW_LOCK_READ)
  378 
  379 static bool __always_inline
  380 __rw_rlock_try(struct rwlock *rw, struct thread *td, uintptr_t *vp,
  381     const char *file, int line)
  382 {
  383 
  384         /*
  385          * Handle the easy case.  If no other thread has a write
  386          * lock, then try to bump up the count of read locks.  Note
  387          * that we have to preserve the current state of the
  388          * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
  389          * read lock, then rw_lock must have changed, so restart
  390          * the loop.  Note that this handles the case of a
  391          * completely unlocked rwlock since such a lock is encoded
  392          * as a read lock with no waiters.
  393          */
  394         while (RW_CAN_READ(td, *vp)) {
  395                 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, vp,
  396                         *vp + RW_ONE_READER)) {
  397                         if (LOCK_LOG_TEST(&rw->lock_object, 0))
  398                                 CTR4(KTR_LOCK,
  399                                     "%s: %p succeed %p -> %p", __func__,
  400                                     rw, (void *)*vp,
  401                                     (void *)(*vp + RW_ONE_READER));
  402                         td->td_rw_rlocks++;
  403                         return (true);
  404                 }
  405         }
  406         return (false);
  407 }
  408 
  409 static void __noinline
  410 __rw_rlock_hard(volatile uintptr_t *c, struct thread *td, uintptr_t v,
  411     const char *file, int line)
  412 {
  413         struct rwlock *rw;
  414         struct turnstile *ts;
  415 #ifdef ADAPTIVE_RWLOCKS
  416         volatile struct thread *owner;
  417         int spintries = 0;
  418         int i;
  419 #endif
  420 #ifdef LOCK_PROFILING
  421         uint64_t waittime = 0;
  422         int contested = 0;
  423 #endif
  424 #if defined(ADAPTIVE_RWLOCKS) || defined(KDTRACE_HOOKS)
  425         struct lock_delay_arg lda;
  426 #endif
  427 #ifdef KDTRACE_HOOKS
  428         uintptr_t state;
  429         u_int sleep_cnt = 0;
  430         int64_t sleep_time = 0;
  431         int64_t all_time = 0;
  432 #endif
  433 
  434         if (SCHEDULER_STOPPED())
  435                 return;
  436 
  437 #if defined(ADAPTIVE_RWLOCKS)
  438         lock_delay_arg_init(&lda, &rw_delay);
  439 #elif defined(KDTRACE_HOOKS)
  440         lock_delay_arg_init(&lda, NULL);
  441 #endif
  442         rw = rwlock2rw(c);
  443 
  444 #ifdef KDTRACE_HOOKS
  445         all_time -= lockstat_nsecs(&rw->lock_object);
  446 #endif
  447 #ifdef KDTRACE_HOOKS
  448         state = v;
  449 #endif
  450         for (;;) {
  451                 if (__rw_rlock_try(rw, td, &v, file, line))
  452                         break;
  453 #ifdef KDTRACE_HOOKS
  454                 lda.spin_cnt++;
  455 #endif
  456 #ifdef HWPMC_HOOKS
  457                 PMC_SOFT_CALL( , , lock, failed);
  458 #endif
  459                 lock_profile_obtain_lock_failed(&rw->lock_object,
  460                     &contested, &waittime);
  461 
  462 #ifdef ADAPTIVE_RWLOCKS
  463                 /*
  464                  * If the owner is running on another CPU, spin until
  465                  * the owner stops running or the state of the lock
  466                  * changes.
  467                  */
  468                 if ((v & RW_LOCK_READ) == 0) {
  469                         owner = (struct thread *)RW_OWNER(v);
  470                         if (TD_IS_RUNNING(owner)) {
  471                                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  472                                         CTR3(KTR_LOCK,
  473                                             "%s: spinning on %p held by %p",
  474                                             __func__, rw, owner);
  475                                 KTR_STATE1(KTR_SCHED, "thread",
  476                                     sched_tdname(curthread), "spinning",
  477                                     "lockname:\"%s\"", rw->lock_object.lo_name);
  478                                 do {
  479                                         lock_delay(&lda);
  480                                         v = RW_READ_VALUE(rw);
  481                                         owner = lv_rw_wowner(v);
  482                                 } while (owner != NULL && TD_IS_RUNNING(owner));
  483                                 KTR_STATE0(KTR_SCHED, "thread",
  484                                     sched_tdname(curthread), "running");
  485                                 continue;
  486                         }
  487                 } else if (spintries < rowner_retries) {
  488                         spintries++;
  489                         KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
  490                             "spinning", "lockname:\"%s\"",
  491                             rw->lock_object.lo_name);
  492                         for (i = 0; i < rowner_loops; i++) {
  493                                 v = RW_READ_VALUE(rw);
  494                                 if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(td, v))
  495                                         break;
  496                                 cpu_spinwait();
  497                         }
  498                         v = RW_READ_VALUE(rw);
  499 #ifdef KDTRACE_HOOKS
  500                         lda.spin_cnt += rowner_loops - i;
  501 #endif
  502                         KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
  503                             "running");
  504                         if (i != rowner_loops)
  505                                 continue;
  506                 }
  507 #endif
  508 
  509                 /*
  510                  * Okay, now it's the hard case.  Some other thread already
  511                  * has a write lock or there are write waiters present,
  512                  * acquire the turnstile lock so we can begin the process
  513                  * of blocking.
  514                  */
  515                 ts = turnstile_trywait(&rw->lock_object);
  516 
  517                 /*
  518                  * The lock might have been released while we spun, so
  519                  * recheck its state and restart the loop if needed.
  520                  */
  521                 v = RW_READ_VALUE(rw);
  522                 if (RW_CAN_READ(td, v)) {
  523                         turnstile_cancel(ts);
  524                         continue;
  525                 }
  526 
  527 #ifdef ADAPTIVE_RWLOCKS
  528                 /*
  529                  * The current lock owner might have started executing
  530                  * on another CPU (or the lock could have changed
  531                  * owners) while we were waiting on the turnstile
  532                  * chain lock.  If so, drop the turnstile lock and try
  533                  * again.
  534                  */
  535                 if ((v & RW_LOCK_READ) == 0) {
  536                         owner = (struct thread *)RW_OWNER(v);
  537                         if (TD_IS_RUNNING(owner)) {
  538                                 turnstile_cancel(ts);
  539                                 continue;
  540                         }
  541                 }
  542 #endif
  543 
  544                 /*
  545                  * The lock is held in write mode or it already has waiters.
  546                  */
  547                 MPASS(!RW_CAN_READ(td, v));
  548 
  549                 /*
  550                  * If the RW_LOCK_READ_WAITERS flag is already set, then
  551                  * we can go ahead and block.  If it is not set then try
  552                  * to set it.  If we fail to set it drop the turnstile
  553                  * lock and restart the loop.
  554                  */
  555                 if (!(v & RW_LOCK_READ_WAITERS)) {
  556                         if (!atomic_cmpset_ptr(&rw->rw_lock, v,
  557                             v | RW_LOCK_READ_WAITERS)) {
  558                                 turnstile_cancel(ts);
  559                                 v = RW_READ_VALUE(rw);
  560                                 continue;
  561                         }
  562                         if (LOCK_LOG_TEST(&rw->lock_object, 0))
  563                                 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
  564                                     __func__, rw);
  565                 }
  566 
  567                 /*
  568                  * We were unable to acquire the lock and the read waiters
  569                  * flag is set, so we must block on the turnstile.
  570                  */
  571                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  572                         CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
  573                             rw);
  574 #ifdef KDTRACE_HOOKS
  575                 sleep_time -= lockstat_nsecs(&rw->lock_object);
  576 #endif
  577                 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
  578 #ifdef KDTRACE_HOOKS
  579                 sleep_time += lockstat_nsecs(&rw->lock_object);
  580                 sleep_cnt++;
  581 #endif
  582                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  583                         CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
  584                             __func__, rw);
  585                 v = RW_READ_VALUE(rw);
  586         }
  587 #ifdef KDTRACE_HOOKS
  588         all_time += lockstat_nsecs(&rw->lock_object);
  589         if (sleep_time)
  590                 LOCKSTAT_RECORD4(rw__block, rw, sleep_time,
  591                     LOCKSTAT_READER, (state & RW_LOCK_READ) == 0,
  592                     (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
  593 
  594         /* Record only the loops spinning and not sleeping. */
  595         if (lda.spin_cnt > sleep_cnt)
  596                 LOCKSTAT_RECORD4(rw__spin, rw, all_time - sleep_time,
  597                     LOCKSTAT_READER, (state & RW_LOCK_READ) == 0,
  598                     (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
  599 #endif
  600         /*
  601          * TODO: acquire "owner of record" here.  Here be turnstile dragons
  602          * however.  turnstiles don't like owners changing between calls to
  603          * turnstile_wait() currently.
  604          */
  605         LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw, contested,
  606             waittime, file, line, LOCKSTAT_READER);
  607 }
  608 
  609 void
  610 __rw_rlock(volatile uintptr_t *c, const char *file, int line)
  611 {
  612         struct rwlock *rw;
  613         struct thread *td;
  614         uintptr_t v;
  615 
  616         td = curthread;
  617         rw = rwlock2rw(c);
  618 
  619         KASSERT(kdb_active != 0 || SCHEDULER_STOPPED_TD(td) ||
  620             !TD_IS_IDLETHREAD(td),
  621             ("rw_rlock() by idle thread %p on rwlock %s @ %s:%d",
  622             td, rw->lock_object.lo_name, file, line));
  623         KASSERT(rw->rw_lock != RW_DESTROYED,
  624             ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
  625         KASSERT(rw_wowner(rw) != td,
  626             ("rw_rlock: wlock already held for %s @ %s:%d",
  627             rw->lock_object.lo_name, file, line));
  628         WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
  629 
  630         v = RW_READ_VALUE(rw);
  631         if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(rw__acquire) ||
  632             !__rw_rlock_try(rw, td, &v, file, line)))
  633                 __rw_rlock_hard(c, td, v, file, line);
  634 
  635         LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
  636         WITNESS_LOCK(&rw->lock_object, 0, file, line);
  637         TD_LOCKS_INC(curthread);
  638 }
  639 
  640 int
  641 __rw_try_rlock(volatile uintptr_t *c, const char *file, int line)
  642 {
  643         struct rwlock *rw;
  644         uintptr_t x;
  645 
  646         if (SCHEDULER_STOPPED())
  647                 return (1);
  648 
  649         rw = rwlock2rw(c);
  650 
  651         KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
  652             ("rw_try_rlock() by idle thread %p on rwlock %s @ %s:%d",
  653             curthread, rw->lock_object.lo_name, file, line));
  654 
  655         x = rw->rw_lock;
  656         for (;;) {
  657                 KASSERT(rw->rw_lock != RW_DESTROYED,
  658                     ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
  659                 if (!(x & RW_LOCK_READ))
  660                         break;
  661                 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, &x, x + RW_ONE_READER)) {
  662                         LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
  663                             line);
  664                         WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
  665                         LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire,
  666                             rw, 0, 0, file, line, LOCKSTAT_READER);
  667                         TD_LOCKS_INC(curthread);
  668                         curthread->td_rw_rlocks++;
  669                         return (1);
  670                 }
  671         }
  672 
  673         LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
  674         return (0);
  675 }
  676 
  677 static bool __always_inline
  678 __rw_runlock_try(struct rwlock *rw, struct thread *td, uintptr_t *vp)
  679 {
  680 
  681         for (;;) {
  682                 /*
  683                  * See if there is more than one read lock held.  If so,
  684                  * just drop one and return.
  685                  */
  686                 if (RW_READERS(*vp) > 1) {
  687                         if (atomic_fcmpset_rel_ptr(&rw->rw_lock, vp,
  688                             *vp - RW_ONE_READER)) {
  689                                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  690                                         CTR4(KTR_LOCK,
  691                                             "%s: %p succeeded %p -> %p",
  692                                             __func__, rw, (void *)*vp,
  693                                             (void *)(*vp - RW_ONE_READER));
  694                                 td->td_rw_rlocks--;
  695                                 return (true);
  696                         }
  697                         continue;
  698                 }
  699                 /*
  700                  * If there aren't any waiters for a write lock, then try
  701                  * to drop it quickly.
  702                  */
  703                 if (!(*vp & RW_LOCK_WAITERS)) {
  704                         MPASS((*vp & ~RW_LOCK_WRITE_SPINNER) ==
  705                             RW_READERS_LOCK(1));
  706                         if (atomic_fcmpset_rel_ptr(&rw->rw_lock, vp,
  707                             RW_UNLOCKED)) {
  708                                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  709                                         CTR2(KTR_LOCK, "%s: %p last succeeded",
  710                                             __func__, rw);
  711                                 td->td_rw_rlocks--;
  712                                 return (true);
  713                         }
  714                         continue;
  715                 }
  716                 break;
  717         }
  718         return (false);
  719 }
  720 
  721 static void __noinline
  722 __rw_runlock_hard(volatile uintptr_t *c, struct thread *td, uintptr_t v,
  723     const char *file, int line)
  724 {
  725         struct rwlock *rw;
  726         struct turnstile *ts;
  727         uintptr_t x, queue;
  728 
  729         if (SCHEDULER_STOPPED())
  730                 return;
  731 
  732         rw = rwlock2rw(c);
  733 
  734         for (;;) {
  735                 if (__rw_runlock_try(rw, td, &v))
  736                         break;
  737 
  738                 /*
  739                  * Ok, we know we have waiters and we think we are the
  740                  * last reader, so grab the turnstile lock.
  741                  */
  742                 turnstile_chain_lock(&rw->lock_object);
  743                 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
  744                 MPASS(v & RW_LOCK_WAITERS);
  745 
  746                 /*
  747                  * Try to drop our lock leaving the lock in a unlocked
  748                  * state.
  749                  *
  750                  * If you wanted to do explicit lock handoff you'd have to
  751                  * do it here.  You'd also want to use turnstile_signal()
  752                  * and you'd have to handle the race where a higher
  753                  * priority thread blocks on the write lock before the
  754                  * thread you wakeup actually runs and have the new thread
  755                  * "steal" the lock.  For now it's a lot simpler to just
  756                  * wakeup all of the waiters.
  757                  *
  758                  * As above, if we fail, then another thread might have
  759                  * acquired a read lock, so drop the turnstile lock and
  760                  * restart.
  761                  */
  762                 x = RW_UNLOCKED;
  763                 if (v & RW_LOCK_WRITE_WAITERS) {
  764                         queue = TS_EXCLUSIVE_QUEUE;
  765                         x |= (v & RW_LOCK_READ_WAITERS);
  766                 } else
  767                         queue = TS_SHARED_QUEUE;
  768                 if (!atomic_cmpset_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
  769                     x)) {
  770                         turnstile_chain_unlock(&rw->lock_object);
  771                         v = RW_READ_VALUE(rw);
  772                         continue;
  773                 }
  774                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  775                         CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
  776                             __func__, rw);
  777 
  778                 /*
  779                  * Ok.  The lock is released and all that's left is to
  780                  * wake up the waiters.  Note that the lock might not be
  781                  * free anymore, but in that case the writers will just
  782                  * block again if they run before the new lock holder(s)
  783                  * release the lock.
  784                  */
  785                 ts = turnstile_lookup(&rw->lock_object);
  786                 MPASS(ts != NULL);
  787                 turnstile_broadcast(ts, queue);
  788                 turnstile_unpend(ts, TS_SHARED_LOCK);
  789                 turnstile_chain_unlock(&rw->lock_object);
  790                 td->td_rw_rlocks--;
  791                 break;
  792         }
  793         LOCKSTAT_PROFILE_RELEASE_RWLOCK(rw__release, rw, LOCKSTAT_READER);
  794 }
  795 
  796 void
  797 _rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line)
  798 {
  799         struct rwlock *rw;
  800         struct thread *td;
  801         uintptr_t v;
  802 
  803         rw = rwlock2rw(c);
  804 
  805         KASSERT(rw->rw_lock != RW_DESTROYED,
  806             ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
  807         __rw_assert(c, RA_RLOCKED, file, line);
  808         WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
  809         LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
  810 
  811         td = curthread;
  812         v = RW_READ_VALUE(rw);
  813 
  814         if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(rw__release) ||
  815             !__rw_runlock_try(rw, td, &v)))
  816                 __rw_runlock_hard(c, td, v, file, line);
  817 
  818         TD_LOCKS_DEC(curthread);
  819 }
  820 
  821 /*
  822  * This function is called when we are unable to obtain a write lock on the
  823  * first try.  This means that at least one other thread holds either a
  824  * read or write lock.
  825  */
  826 void
  827 __rw_wlock_hard(volatile uintptr_t *c, uintptr_t v, uintptr_t tid,
  828     const char *file, int line)
  829 {
  830         struct rwlock *rw;
  831         struct turnstile *ts;
  832 #ifdef ADAPTIVE_RWLOCKS
  833         volatile struct thread *owner;
  834         int spintries = 0;
  835         int i;
  836 #endif
  837         uintptr_t x;
  838 #ifdef LOCK_PROFILING
  839         uint64_t waittime = 0;
  840         int contested = 0;
  841 #endif
  842 #if defined(ADAPTIVE_RWLOCKS) || defined(KDTRACE_HOOKS)
  843         struct lock_delay_arg lda;
  844 #endif
  845 #ifdef KDTRACE_HOOKS
  846         uintptr_t state;
  847         u_int sleep_cnt = 0;
  848         int64_t sleep_time = 0;
  849         int64_t all_time = 0;
  850 #endif
  851 
  852         if (SCHEDULER_STOPPED())
  853                 return;
  854 
  855 #if defined(ADAPTIVE_RWLOCKS)
  856         lock_delay_arg_init(&lda, &rw_delay);
  857 #elif defined(KDTRACE_HOOKS)
  858         lock_delay_arg_init(&lda, NULL);
  859 #endif
  860         rw = rwlock2rw(c);
  861         if (__predict_false(v == RW_UNLOCKED))
  862                 v = RW_READ_VALUE(rw);
  863 
  864         if (__predict_false(lv_rw_wowner(v) == (struct thread *)tid)) {
  865                 KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE,
  866                     ("%s: recursing but non-recursive rw %s @ %s:%d\n",
  867                     __func__, rw->lock_object.lo_name, file, line));
  868                 rw->rw_recurse++;
  869                 atomic_set_ptr(&rw->rw_lock, RW_LOCK_WRITER_RECURSED);
  870                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
  871                         CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
  872                 return;
  873         }
  874 
  875         if (LOCK_LOG_TEST(&rw->lock_object, 0))
  876                 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
  877                     rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
  878 
  879 #ifdef KDTRACE_HOOKS
  880         all_time -= lockstat_nsecs(&rw->lock_object);
  881         state = v;
  882 #endif
  883         for (;;) {
  884                 if (v == RW_UNLOCKED) {
  885                         if (_rw_write_lock_fetch(rw, &v, tid))
  886                                 break;
  887                         continue;
  888                 }
  889 #ifdef KDTRACE_HOOKS
  890                 lda.spin_cnt++;
  891 #endif
  892 #ifdef HWPMC_HOOKS
  893                 PMC_SOFT_CALL( , , lock, failed);
  894 #endif
  895                 lock_profile_obtain_lock_failed(&rw->lock_object,
  896                     &contested, &waittime);
  897 #ifdef ADAPTIVE_RWLOCKS
  898                 /*
  899                  * If the lock is write locked and the owner is
  900                  * running on another CPU, spin until the owner stops
  901                  * running or the state of the lock changes.
  902                  */
  903                 owner = lv_rw_wowner(v);
  904                 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
  905                         if (LOCK_LOG_TEST(&rw->lock_object, 0))
  906                                 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
  907                                     __func__, rw, owner);
  908                         KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
  909                             "spinning", "lockname:\"%s\"",
  910                             rw->lock_object.lo_name);
  911                         do {
  912                                 lock_delay(&lda);
  913                                 v = RW_READ_VALUE(rw);
  914                                 owner = lv_rw_wowner(v);
  915                         } while (owner != NULL && TD_IS_RUNNING(owner));
  916                         KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
  917                             "running");
  918                         continue;
  919                 }
  920                 if ((v & RW_LOCK_READ) && RW_READERS(v) &&
  921                     spintries < rowner_retries) {
  922                         if (!(v & RW_LOCK_WRITE_SPINNER)) {
  923                                 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
  924                                     v | RW_LOCK_WRITE_SPINNER)) {
  925                                         v = RW_READ_VALUE(rw);
  926                                         continue;
  927                                 }
  928                         }
  929                         spintries++;
  930                         KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
  931                             "spinning", "lockname:\"%s\"",
  932                             rw->lock_object.lo_name);
  933                         for (i = 0; i < rowner_loops; i++) {
  934                                 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
  935                                         break;
  936                                 cpu_spinwait();
  937                         }
  938                         KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
  939                             "running");
  940                         v = RW_READ_VALUE(rw);
  941 #ifdef KDTRACE_HOOKS
  942                         lda.spin_cnt += rowner_loops - i;
  943 #endif
  944                         if (i != rowner_loops)
  945                                 continue;
  946                 }
  947 #endif
  948                 ts = turnstile_trywait(&rw->lock_object);
  949                 v = RW_READ_VALUE(rw);
  950 
  951 #ifdef ADAPTIVE_RWLOCKS
  952                 /*
  953                  * The current lock owner might have started executing
  954                  * on another CPU (or the lock could have changed
  955                  * owners) while we were waiting on the turnstile
  956                  * chain lock.  If so, drop the turnstile lock and try
  957                  * again.
  958                  */
  959                 if (!(v & RW_LOCK_READ)) {
  960                         owner = (struct thread *)RW_OWNER(v);
  961                         if (TD_IS_RUNNING(owner)) {
  962                                 turnstile_cancel(ts);
  963                                 continue;
  964                         }
  965                 }
  966 #endif
  967                 /*
  968                  * Check for the waiters flags about this rwlock.
  969                  * If the lock was released, without maintain any pending
  970                  * waiters queue, simply try to acquire it.
  971                  * If a pending waiters queue is present, claim the lock
  972                  * ownership and maintain the pending queue.
  973                  */
  974                 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
  975                 if ((v & ~x) == RW_UNLOCKED) {
  976                         x &= ~RW_LOCK_WRITE_SPINNER;
  977                         if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
  978                                 if (x)
  979                                         turnstile_claim(ts);
  980                                 else
  981                                         turnstile_cancel(ts);
  982                                 break;
  983                         }
  984                         turnstile_cancel(ts);
  985                         v = RW_READ_VALUE(rw);
  986                         continue;
  987                 }
  988                 /*
  989                  * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
  990                  * set it.  If we fail to set it, then loop back and try
  991                  * again.
  992                  */
  993                 if (!(v & RW_LOCK_WRITE_WAITERS)) {
  994                         if (!atomic_cmpset_ptr(&rw->rw_lock, v,
  995                             v | RW_LOCK_WRITE_WAITERS)) {
  996                                 turnstile_cancel(ts);
  997                                 v = RW_READ_VALUE(rw);
  998                                 continue;
  999                         }
 1000                         if (LOCK_LOG_TEST(&rw->lock_object, 0))
 1001                                 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
 1002                                     __func__, rw);
 1003                 }
 1004                 /*
 1005                  * We were unable to acquire the lock and the write waiters
 1006                  * flag is set, so we must block on the turnstile.
 1007                  */
 1008                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
 1009                         CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
 1010                             rw);
 1011 #ifdef KDTRACE_HOOKS
 1012                 sleep_time -= lockstat_nsecs(&rw->lock_object);
 1013 #endif
 1014                 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
 1015 #ifdef KDTRACE_HOOKS
 1016                 sleep_time += lockstat_nsecs(&rw->lock_object);
 1017                 sleep_cnt++;
 1018 #endif
 1019                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
 1020                         CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
 1021                             __func__, rw);
 1022 #ifdef ADAPTIVE_RWLOCKS
 1023                 spintries = 0;
 1024 #endif
 1025                 v = RW_READ_VALUE(rw);
 1026         }
 1027 #ifdef KDTRACE_HOOKS
 1028         all_time += lockstat_nsecs(&rw->lock_object);
 1029         if (sleep_time)
 1030                 LOCKSTAT_RECORD4(rw__block, rw, sleep_time,
 1031                     LOCKSTAT_WRITER, (state & RW_LOCK_READ) == 0,
 1032                     (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
 1033 
 1034         /* Record only the loops spinning and not sleeping. */
 1035         if (lda.spin_cnt > sleep_cnt)
 1036                 LOCKSTAT_RECORD4(rw__spin, rw, all_time - sleep_time,
 1037                     LOCKSTAT_WRITER, (state & RW_LOCK_READ) == 0,
 1038                     (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
 1039 #endif
 1040         LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw, contested,
 1041             waittime, file, line, LOCKSTAT_WRITER);
 1042 }
 1043 
 1044 /*
 1045  * This function is called if lockstat is active or the first try at releasing
 1046  * a write lock failed.  The latter means that the lock is recursed or one of
 1047  * the 2 waiter bits must be set indicating that at least one thread is waiting
 1048  * on this lock.
 1049  */
 1050 void
 1051 __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file,
 1052     int line)
 1053 {
 1054         struct rwlock *rw;
 1055         struct turnstile *ts;
 1056         uintptr_t v;
 1057         int queue;
 1058 
 1059         if (SCHEDULER_STOPPED())
 1060                 return;
 1061 
 1062         rw = rwlock2rw(c);
 1063         v = RW_READ_VALUE(rw);
 1064         if (v & RW_LOCK_WRITER_RECURSED) {
 1065                 if (--(rw->rw_recurse) == 0)
 1066                         atomic_clear_ptr(&rw->rw_lock, RW_LOCK_WRITER_RECURSED);
 1067                 if (LOCK_LOG_TEST(&rw->lock_object, 0))
 1068                         CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
 1069                 return;
 1070         }
 1071 
 1072         LOCKSTAT_PROFILE_RELEASE_RWLOCK(rw__release, rw, LOCKSTAT_WRITER);
 1073         if (v == tid && _rw_write_unlock(rw, tid))
 1074                 return;
 1075 
 1076         KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
 1077             ("%s: neither of the waiter flags are set", __func__));
 1078 
 1079         if (LOCK_LOG_TEST(&rw->lock_object, 0))
 1080                 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
 1081 
 1082         turnstile_chain_lock(&rw->lock_object);
 1083         ts = turnstile_lookup(&rw->lock_object);
 1084         MPASS(ts != NULL);
 1085 
 1086         /*
 1087          * Use the same algo as sx locks for now.  Prefer waking up shared
 1088          * waiters if we have any over writers.  This is probably not ideal.
 1089          *
 1090          * 'v' is the value we are going to write back to rw_lock.  If we
 1091          * have waiters on both queues, we need to preserve the state of
 1092          * the waiter flag for the queue we don't wake up.  For now this is
 1093          * hardcoded for the algorithm mentioned above.
 1094          *
 1095          * In the case of both readers and writers waiting we wakeup the
 1096          * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
 1097          * new writer comes in before a reader it will claim the lock up
 1098          * above.  There is probably a potential priority inversion in
 1099          * there that could be worked around either by waking both queues
 1100          * of waiters or doing some complicated lock handoff gymnastics.
 1101          */
 1102         v = RW_UNLOCKED;
 1103         if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
 1104                 queue = TS_EXCLUSIVE_QUEUE;
 1105                 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
 1106         } else
 1107                 queue = TS_SHARED_QUEUE;
 1108 
 1109         /* Wake up all waiters for the specific queue. */
 1110         if (LOCK_LOG_TEST(&rw->lock_object, 0))
 1111                 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
 1112                     queue == TS_SHARED_QUEUE ? "read" : "write");
 1113         turnstile_broadcast(ts, queue);
 1114         atomic_store_rel_ptr(&rw->rw_lock, v);
 1115         turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
 1116         turnstile_chain_unlock(&rw->lock_object);
 1117 }
 1118 
 1119 /*
 1120  * Attempt to do a non-blocking upgrade from a read lock to a write
 1121  * lock.  This will only succeed if this thread holds a single read
 1122  * lock.  Returns true if the upgrade succeeded and false otherwise.
 1123  */
 1124 int
 1125 __rw_try_upgrade(volatile uintptr_t *c, const char *file, int line)
 1126 {
 1127         struct rwlock *rw;
 1128         uintptr_t v, x, tid;
 1129         struct turnstile *ts;
 1130         int success;
 1131 
 1132         if (SCHEDULER_STOPPED())
 1133                 return (1);
 1134 
 1135         rw = rwlock2rw(c);
 1136 
 1137         KASSERT(rw->rw_lock != RW_DESTROYED,
 1138             ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
 1139         __rw_assert(c, RA_RLOCKED, file, line);
 1140 
 1141         /*
 1142          * Attempt to switch from one reader to a writer.  If there
 1143          * are any write waiters, then we will have to lock the
 1144          * turnstile first to prevent races with another writer
 1145          * calling turnstile_wait() before we have claimed this
 1146          * turnstile.  So, do the simple case of no waiters first.
 1147          */
 1148         tid = (uintptr_t)curthread;
 1149         success = 0;
 1150         for (;;) {
 1151                 v = rw->rw_lock;
 1152                 if (RW_READERS(v) > 1)
 1153                         break;
 1154                 if (!(v & RW_LOCK_WAITERS)) {
 1155                         success = atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid);
 1156                         if (!success)
 1157                                 continue;
 1158                         break;
 1159                 }
 1160 
 1161                 /*
 1162                  * Ok, we think we have waiters, so lock the turnstile.
 1163                  */
 1164                 ts = turnstile_trywait(&rw->lock_object);
 1165                 v = rw->rw_lock;
 1166                 if (RW_READERS(v) > 1) {
 1167                         turnstile_cancel(ts);
 1168                         break;
 1169                 }
 1170                 /*
 1171                  * Try to switch from one reader to a writer again.  This time
 1172                  * we honor the current state of the waiters flags.
 1173                  * If we obtain the lock with the flags set, then claim
 1174                  * ownership of the turnstile.
 1175                  */
 1176                 x = rw->rw_lock & RW_LOCK_WAITERS;
 1177                 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
 1178                 if (success) {
 1179                         if (x)
 1180                                 turnstile_claim(ts);
 1181                         else
 1182                                 turnstile_cancel(ts);
 1183                         break;
 1184                 }
 1185                 turnstile_cancel(ts);
 1186         }
 1187         LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
 1188         if (success) {
 1189                 curthread->td_rw_rlocks--;
 1190                 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
 1191                     file, line);
 1192                 LOCKSTAT_RECORD0(rw__upgrade, rw);
 1193         }
 1194         return (success);
 1195 }
 1196 
 1197 /*
 1198  * Downgrade a write lock into a single read lock.
 1199  */
 1200 void
 1201 __rw_downgrade(volatile uintptr_t *c, const char *file, int line)
 1202 {
 1203         struct rwlock *rw;
 1204         struct turnstile *ts;
 1205         uintptr_t tid, v;
 1206         int rwait, wwait;
 1207 
 1208         if (SCHEDULER_STOPPED())
 1209                 return;
 1210 
 1211         rw = rwlock2rw(c);
 1212 
 1213         KASSERT(rw->rw_lock != RW_DESTROYED,
 1214             ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
 1215         __rw_assert(c, RA_WLOCKED | RA_NOTRECURSED, file, line);
 1216 #ifndef INVARIANTS
 1217         if (rw_recursed(rw))
 1218                 panic("downgrade of a recursed lock");
 1219 #endif
 1220 
 1221         WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
 1222 
 1223         /*
 1224          * Convert from a writer to a single reader.  First we handle
 1225          * the easy case with no waiters.  If there are any waiters, we
 1226          * lock the turnstile and "disown" the lock.
 1227          */
 1228         tid = (uintptr_t)curthread;
 1229         if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
 1230                 goto out;
 1231 
 1232         /*
 1233          * Ok, we think we have waiters, so lock the turnstile so we can
 1234          * read the waiter flags without any races.
 1235          */
 1236         turnstile_chain_lock(&rw->lock_object);
 1237         v = rw->rw_lock & RW_LOCK_WAITERS;
 1238         rwait = v & RW_LOCK_READ_WAITERS;
 1239         wwait = v & RW_LOCK_WRITE_WAITERS;
 1240         MPASS(rwait | wwait);
 1241 
 1242         /*
 1243          * Downgrade from a write lock while preserving waiters flag
 1244          * and give up ownership of the turnstile.
 1245          */
 1246         ts = turnstile_lookup(&rw->lock_object);
 1247         MPASS(ts != NULL);
 1248         if (!wwait)
 1249                 v &= ~RW_LOCK_READ_WAITERS;
 1250         atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
 1251         /*
 1252          * Wake other readers if there are no writers pending.  Otherwise they
 1253          * won't be able to acquire the lock anyway.
 1254          */
 1255         if (rwait && !wwait) {
 1256                 turnstile_broadcast(ts, TS_SHARED_QUEUE);
 1257                 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
 1258         } else
 1259                 turnstile_disown(ts);
 1260         turnstile_chain_unlock(&rw->lock_object);
 1261 out:
 1262         curthread->td_rw_rlocks++;
 1263         LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
 1264         LOCKSTAT_RECORD0(rw__downgrade, rw);
 1265 }
 1266 
 1267 #ifdef INVARIANT_SUPPORT
 1268 #ifndef INVARIANTS
 1269 #undef __rw_assert
 1270 #endif
 1271 
 1272 /*
 1273  * In the non-WITNESS case, rw_assert() can only detect that at least
 1274  * *some* thread owns an rlock, but it cannot guarantee that *this*
 1275  * thread owns an rlock.
 1276  */
 1277 void
 1278 __rw_assert(const volatile uintptr_t *c, int what, const char *file, int line)
 1279 {
 1280         const struct rwlock *rw;
 1281 
 1282         if (panicstr != NULL)
 1283                 return;
 1284 
 1285         rw = rwlock2rw(c);
 1286 
 1287         switch (what) {
 1288         case RA_LOCKED:
 1289         case RA_LOCKED | RA_RECURSED:
 1290         case RA_LOCKED | RA_NOTRECURSED:
 1291         case RA_RLOCKED:
 1292         case RA_RLOCKED | RA_RECURSED:
 1293         case RA_RLOCKED | RA_NOTRECURSED:
 1294 #ifdef WITNESS
 1295                 witness_assert(&rw->lock_object, what, file, line);
 1296 #else
 1297                 /*
 1298                  * If some other thread has a write lock or we have one
 1299                  * and are asserting a read lock, fail.  Also, if no one
 1300                  * has a lock at all, fail.
 1301                  */
 1302                 if (rw->rw_lock == RW_UNLOCKED ||
 1303                     (!(rw->rw_lock & RW_LOCK_READ) && (what & RA_RLOCKED ||
 1304                     rw_wowner(rw) != curthread)))
 1305                         panic("Lock %s not %slocked @ %s:%d\n",
 1306                             rw->lock_object.lo_name, (what & RA_RLOCKED) ?
 1307                             "read " : "", file, line);
 1308 
 1309                 if (!(rw->rw_lock & RW_LOCK_READ) && !(what & RA_RLOCKED)) {
 1310                         if (rw_recursed(rw)) {
 1311                                 if (what & RA_NOTRECURSED)
 1312                                         panic("Lock %s recursed @ %s:%d\n",
 1313                                             rw->lock_object.lo_name, file,
 1314                                             line);
 1315                         } else if (what & RA_RECURSED)
 1316                                 panic("Lock %s not recursed @ %s:%d\n",
 1317                                     rw->lock_object.lo_name, file, line);
 1318                 }
 1319 #endif
 1320                 break;
 1321         case RA_WLOCKED:
 1322         case RA_WLOCKED | RA_RECURSED:
 1323         case RA_WLOCKED | RA_NOTRECURSED:
 1324                 if (rw_wowner(rw) != curthread)
 1325                         panic("Lock %s not exclusively locked @ %s:%d\n",
 1326                             rw->lock_object.lo_name, file, line);
 1327                 if (rw_recursed(rw)) {
 1328                         if (what & RA_NOTRECURSED)
 1329                                 panic("Lock %s recursed @ %s:%d\n",
 1330                                     rw->lock_object.lo_name, file, line);
 1331                 } else if (what & RA_RECURSED)
 1332                         panic("Lock %s not recursed @ %s:%d\n",
 1333                             rw->lock_object.lo_name, file, line);
 1334                 break;
 1335         case RA_UNLOCKED:
 1336 #ifdef WITNESS
 1337                 witness_assert(&rw->lock_object, what, file, line);
 1338 #else
 1339                 /*
 1340                  * If we hold a write lock fail.  We can't reliably check
 1341                  * to see if we hold a read lock or not.
 1342                  */
 1343                 if (rw_wowner(rw) == curthread)
 1344                         panic("Lock %s exclusively locked @ %s:%d\n",
 1345                             rw->lock_object.lo_name, file, line);
 1346 #endif
 1347                 break;
 1348         default:
 1349                 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
 1350                     line);
 1351         }
 1352 }
 1353 #endif /* INVARIANT_SUPPORT */
 1354 
 1355 #ifdef DDB
 1356 void
 1357 db_show_rwlock(const struct lock_object *lock)
 1358 {
 1359         const struct rwlock *rw;
 1360         struct thread *td;
 1361 
 1362         rw = (const struct rwlock *)lock;
 1363 
 1364         db_printf(" state: ");
 1365         if (rw->rw_lock == RW_UNLOCKED)
 1366                 db_printf("UNLOCKED\n");
 1367         else if (rw->rw_lock == RW_DESTROYED) {
 1368                 db_printf("DESTROYED\n");
 1369                 return;
 1370         } else if (rw->rw_lock & RW_LOCK_READ)
 1371                 db_printf("RLOCK: %ju locks\n",
 1372                     (uintmax_t)(RW_READERS(rw->rw_lock)));
 1373         else {
 1374                 td = rw_wowner(rw);
 1375                 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
 1376                     td->td_tid, td->td_proc->p_pid, td->td_name);
 1377                 if (rw_recursed(rw))
 1378                         db_printf(" recursed: %u\n", rw->rw_recurse);
 1379         }
 1380         db_printf(" waiters: ");
 1381         switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
 1382         case RW_LOCK_READ_WAITERS:
 1383                 db_printf("readers\n");
 1384                 break;
 1385         case RW_LOCK_WRITE_WAITERS:
 1386                 db_printf("writers\n");
 1387                 break;
 1388         case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
 1389                 db_printf("readers and writers\n");
 1390                 break;
 1391         default:
 1392                 db_printf("none\n");
 1393                 break;
 1394         }
 1395 }
 1396 
 1397 #endif

Cache object: 29311ef31354668f717db2c5f7c881c4


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