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_rmlock.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 Stephan Uphoff <ups@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  * 3. Neither the name of the author nor the names of any co-contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   24  * HOWEVER 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
   27  * SUCH DAMAGE.
   28  */
   29 
   30 /*
   31  * Machine independent bits of reader/writer lock implementation.
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD: releng/10.3/sys/kern/kern_rmlock.c 288958 2015-10-06 22:28:28Z jhb $");
   36 
   37 #include "opt_ddb.h"
   38 #include "opt_kdtrace.h"
   39 
   40 #include <sys/param.h>
   41 #include <sys/systm.h>
   42 
   43 #include <sys/kernel.h>
   44 #include <sys/kdb.h>
   45 #include <sys/ktr.h>
   46 #include <sys/lock.h>
   47 #include <sys/mutex.h>
   48 #include <sys/proc.h>
   49 #include <sys/rmlock.h>
   50 #include <sys/sched.h>
   51 #include <sys/smp.h>
   52 #include <sys/turnstile.h>
   53 #include <sys/lock_profile.h>
   54 #include <machine/cpu.h>
   55 
   56 #ifdef DDB
   57 #include <ddb/ddb.h>
   58 #endif
   59 
   60 /*
   61  * A cookie to mark destroyed rmlocks.  This is stored in the head of
   62  * rm_activeReaders.
   63  */
   64 #define RM_DESTROYED    ((void *)0xdead)
   65 
   66 #define rm_destroyed(rm)                                                \
   67         (LIST_FIRST(&(rm)->rm_activeReaders) == RM_DESTROYED)
   68 
   69 #define RMPF_ONQUEUE    1
   70 #define RMPF_SIGNAL     2
   71 
   72 #ifndef INVARIANTS
   73 #define _rm_assert(c, what, file, line)
   74 #endif
   75 
   76 static void     assert_rm(const struct lock_object *lock, int what);
   77 #ifdef DDB
   78 static void     db_show_rm(const struct lock_object *lock);
   79 #endif
   80 static void     lock_rm(struct lock_object *lock, uintptr_t how);
   81 #ifdef KDTRACE_HOOKS
   82 static int      owner_rm(const struct lock_object *lock, struct thread **owner);
   83 #endif
   84 static uintptr_t unlock_rm(struct lock_object *lock);
   85 
   86 struct lock_class lock_class_rm = {
   87         .lc_name = "rm",
   88         .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
   89         .lc_assert = assert_rm,
   90 #ifdef DDB
   91         .lc_ddb_show = db_show_rm,
   92 #endif
   93         .lc_lock = lock_rm,
   94         .lc_unlock = unlock_rm,
   95 #ifdef KDTRACE_HOOKS
   96         .lc_owner = owner_rm,
   97 #endif
   98 };
   99 
  100 struct lock_class lock_class_rm_sleepable = {
  101         .lc_name = "sleepable rm",
  102         .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE,
  103         .lc_assert = assert_rm,
  104 #ifdef DDB
  105         .lc_ddb_show = db_show_rm,
  106 #endif
  107         .lc_lock = lock_rm,
  108         .lc_unlock = unlock_rm,
  109 #ifdef KDTRACE_HOOKS
  110         .lc_owner = owner_rm,
  111 #endif
  112 };
  113 
  114 static void
  115 assert_rm(const struct lock_object *lock, int what)
  116 {
  117 
  118         rm_assert((const struct rmlock *)lock, what);
  119 }
  120 
  121 static void
  122 lock_rm(struct lock_object *lock, uintptr_t how)
  123 {
  124         struct rmlock *rm;
  125         struct rm_priotracker *tracker;
  126 
  127         rm = (struct rmlock *)lock;
  128         if (how == 0)
  129                 rm_wlock(rm);
  130         else {
  131                 tracker = (struct rm_priotracker *)how;
  132                 rm_rlock(rm, tracker);
  133         }
  134 }
  135 
  136 static uintptr_t
  137 unlock_rm(struct lock_object *lock)
  138 {
  139         struct thread *td;
  140         struct pcpu *pc;
  141         struct rmlock *rm;
  142         struct rm_queue *queue;
  143         struct rm_priotracker *tracker;
  144         uintptr_t how;
  145 
  146         rm = (struct rmlock *)lock;
  147         tracker = NULL;
  148         how = 0;
  149         rm_assert(rm, RA_LOCKED | RA_NOTRECURSED);
  150         if (rm_wowned(rm))
  151                 rm_wunlock(rm);
  152         else {
  153                 /*
  154                  * Find the right rm_priotracker structure for curthread.
  155                  * The guarantee about its uniqueness is given by the fact
  156                  * we already asserted the lock wasn't recursively acquired.
  157                  */
  158                 critical_enter();
  159                 td = curthread;
  160                 pc = pcpu_find(curcpu);
  161                 for (queue = pc->pc_rm_queue.rmq_next;
  162                     queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
  163                         tracker = (struct rm_priotracker *)queue;
  164                                 if ((tracker->rmp_rmlock == rm) &&
  165                                     (tracker->rmp_thread == td)) {
  166                                         how = (uintptr_t)tracker;
  167                                         break;
  168                                 }
  169                 }
  170                 KASSERT(tracker != NULL,
  171                     ("rm_priotracker is non-NULL when lock held in read mode"));
  172                 critical_exit();
  173                 rm_runlock(rm, tracker);
  174         }
  175         return (how);
  176 }
  177 
  178 #ifdef KDTRACE_HOOKS
  179 static int
  180 owner_rm(const struct lock_object *lock, struct thread **owner)
  181 {
  182         const struct rmlock *rm;
  183         struct lock_class *lc;
  184 
  185         rm = (const struct rmlock *)lock;
  186         lc = LOCK_CLASS(&rm->rm_wlock_object);
  187         return (lc->lc_owner(&rm->rm_wlock_object, owner));
  188 }
  189 #endif
  190 
  191 static struct mtx rm_spinlock;
  192 
  193 MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
  194 
  195 /*
  196  * Add or remove tracker from per-cpu list.
  197  *
  198  * The per-cpu list can be traversed at any time in forward direction from an
  199  * interrupt on the *local* cpu.
  200  */
  201 static void inline
  202 rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
  203 {
  204         struct rm_queue *next;
  205 
  206         /* Initialize all tracker pointers */
  207         tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
  208         next = pc->pc_rm_queue.rmq_next;
  209         tracker->rmp_cpuQueue.rmq_next = next;
  210 
  211         /* rmq_prev is not used during froward traversal. */
  212         next->rmq_prev = &tracker->rmp_cpuQueue;
  213 
  214         /* Update pointer to first element. */
  215         pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue;
  216 }
  217 
  218 /*
  219  * Return a count of the number of trackers the thread 'td' already
  220  * has on this CPU for the lock 'rm'.
  221  */
  222 static int
  223 rm_trackers_present(const struct pcpu *pc, const struct rmlock *rm,
  224     const struct thread *td)
  225 {
  226         struct rm_queue *queue;
  227         struct rm_priotracker *tracker;
  228         int count;
  229 
  230         count = 0;
  231         for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
  232             queue = queue->rmq_next) {
  233                 tracker = (struct rm_priotracker *)queue;
  234                 if ((tracker->rmp_rmlock == rm) && (tracker->rmp_thread == td))
  235                         count++;
  236         }
  237         return (count);
  238 }
  239 
  240 static void inline
  241 rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
  242 {
  243         struct rm_queue *next, *prev;
  244 
  245         next = tracker->rmp_cpuQueue.rmq_next;
  246         prev = tracker->rmp_cpuQueue.rmq_prev;
  247 
  248         /* Not used during forward traversal. */
  249         next->rmq_prev = prev;
  250 
  251         /* Remove from list. */
  252         prev->rmq_next = next;
  253 }
  254 
  255 static void
  256 rm_cleanIPI(void *arg)
  257 {
  258         struct pcpu *pc;
  259         struct rmlock *rm = arg;
  260         struct rm_priotracker *tracker;
  261         struct rm_queue *queue;
  262         pc = pcpu_find(curcpu);
  263 
  264         for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
  265             queue = queue->rmq_next) {
  266                 tracker = (struct rm_priotracker *)queue;
  267                 if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
  268                         tracker->rmp_flags = RMPF_ONQUEUE;
  269                         mtx_lock_spin(&rm_spinlock);
  270                         LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
  271                             rmp_qentry);
  272                         mtx_unlock_spin(&rm_spinlock);
  273                 }
  274         }
  275 }
  276 
  277 void
  278 rm_init_flags(struct rmlock *rm, const char *name, int opts)
  279 {
  280         struct lock_class *lc;
  281         int liflags;
  282 
  283         liflags = 0;
  284         if (!(opts & RM_NOWITNESS))
  285                 liflags |= LO_WITNESS;
  286         if (opts & RM_RECURSE)
  287                 liflags |= LO_RECURSABLE;
  288         rm->rm_writecpus = all_cpus;
  289         LIST_INIT(&rm->rm_activeReaders);
  290         if (opts & RM_SLEEPABLE) {
  291                 liflags |= LO_SLEEPABLE;
  292                 lc = &lock_class_rm_sleepable;
  293                 sx_init_flags(&rm->rm_lock_sx, "rmlock_sx", SX_NOWITNESS);
  294         } else {
  295                 lc = &lock_class_rm;
  296                 mtx_init(&rm->rm_lock_mtx, name, "rmlock_mtx", MTX_NOWITNESS);
  297         }
  298         lock_init(&rm->lock_object, lc, name, NULL, liflags);
  299 }
  300 
  301 void
  302 rm_init(struct rmlock *rm, const char *name)
  303 {
  304 
  305         rm_init_flags(rm, name, 0);
  306 }
  307 
  308 void
  309 rm_destroy(struct rmlock *rm)
  310 {
  311 
  312         rm_assert(rm, RA_UNLOCKED);
  313         LIST_FIRST(&rm->rm_activeReaders) = RM_DESTROYED;
  314         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  315                 sx_destroy(&rm->rm_lock_sx);
  316         else
  317                 mtx_destroy(&rm->rm_lock_mtx);
  318         lock_destroy(&rm->lock_object);
  319 }
  320 
  321 int
  322 rm_wowned(const struct rmlock *rm)
  323 {
  324 
  325         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  326                 return (sx_xlocked(&rm->rm_lock_sx));
  327         else
  328                 return (mtx_owned(&rm->rm_lock_mtx));
  329 }
  330 
  331 void
  332 rm_sysinit(void *arg)
  333 {
  334         struct rm_args *args = arg;
  335 
  336         rm_init(args->ra_rm, args->ra_desc);
  337 }
  338 
  339 void
  340 rm_sysinit_flags(void *arg)
  341 {
  342         struct rm_args_flags *args = arg;
  343 
  344         rm_init_flags(args->ra_rm, args->ra_desc, args->ra_opts);
  345 }
  346 
  347 static int
  348 _rm_rlock_hard(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
  349 {
  350         struct pcpu *pc;
  351 
  352         critical_enter();
  353         pc = pcpu_find(curcpu);
  354 
  355         /* Check if we just need to do a proper critical_exit. */
  356         if (!CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)) {
  357                 critical_exit();
  358                 return (1);
  359         }
  360 
  361         /* Remove our tracker from the per-cpu list. */
  362         rm_tracker_remove(pc, tracker);
  363 
  364         /* Check to see if the IPI granted us the lock after all. */
  365         if (tracker->rmp_flags) {
  366                 /* Just add back tracker - we hold the lock. */
  367                 rm_tracker_add(pc, tracker);
  368                 critical_exit();
  369                 return (1);
  370         }
  371 
  372         /*
  373          * We allow readers to aquire a lock even if a writer is blocked if
  374          * the lock is recursive and the reader already holds the lock.
  375          */
  376         if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) {
  377                 /*
  378                  * Just grant the lock if this thread already has a tracker
  379                  * for this lock on the per-cpu queue.
  380                  */
  381                 if (rm_trackers_present(pc, rm, curthread) != 0) {
  382                         mtx_lock_spin(&rm_spinlock);
  383                         LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
  384                             rmp_qentry);
  385                         tracker->rmp_flags = RMPF_ONQUEUE;
  386                         mtx_unlock_spin(&rm_spinlock);
  387                         rm_tracker_add(pc, tracker);
  388                         critical_exit();
  389                         return (1);
  390                 }
  391         }
  392 
  393         sched_unpin();
  394         critical_exit();
  395 
  396         if (trylock) {
  397                 if (rm->lock_object.lo_flags & LO_SLEEPABLE) {
  398                         if (!sx_try_xlock(&rm->rm_lock_sx))
  399                                 return (0);
  400                 } else {
  401                         if (!mtx_trylock(&rm->rm_lock_mtx))
  402                                 return (0);
  403                 }
  404         } else {
  405                 if (rm->lock_object.lo_flags & LO_SLEEPABLE) {
  406                         THREAD_SLEEPING_OK();
  407                         sx_xlock(&rm->rm_lock_sx);
  408                         THREAD_NO_SLEEPING();
  409                 } else
  410                         mtx_lock(&rm->rm_lock_mtx);
  411         }
  412 
  413         critical_enter();
  414         pc = pcpu_find(curcpu);
  415         CPU_CLR(pc->pc_cpuid, &rm->rm_writecpus);
  416         rm_tracker_add(pc, tracker);
  417         sched_pin();
  418         critical_exit();
  419 
  420         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  421                 sx_xunlock(&rm->rm_lock_sx);
  422         else
  423                 mtx_unlock(&rm->rm_lock_mtx);
  424 
  425         return (1);
  426 }
  427 
  428 int
  429 _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
  430 {
  431         struct thread *td = curthread;
  432         struct pcpu *pc;
  433 
  434         if (SCHEDULER_STOPPED())
  435                 return (1);
  436 
  437         tracker->rmp_flags  = 0;
  438         tracker->rmp_thread = td;
  439         tracker->rmp_rmlock = rm;
  440 
  441         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  442                 THREAD_NO_SLEEPING();
  443 
  444         td->td_critnest++;      /* critical_enter(); */
  445 
  446         __compiler_membar();
  447 
  448         pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
  449 
  450         rm_tracker_add(pc, tracker);
  451 
  452         sched_pin();
  453 
  454         __compiler_membar();
  455 
  456         td->td_critnest--;
  457 
  458         /*
  459          * Fast path to combine two common conditions into a single
  460          * conditional jump.
  461          */
  462         if (0 == (td->td_owepreempt |
  463             CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)))
  464                 return (1);
  465 
  466         /* We do not have a read token and need to acquire one. */
  467         return _rm_rlock_hard(rm, tracker, trylock);
  468 }
  469 
  470 static void
  471 _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker)
  472 {
  473 
  474         if (td->td_owepreempt) {
  475                 td->td_critnest++;
  476                 critical_exit();
  477         }
  478 
  479         if (!tracker->rmp_flags)
  480                 return;
  481 
  482         mtx_lock_spin(&rm_spinlock);
  483         LIST_REMOVE(tracker, rmp_qentry);
  484 
  485         if (tracker->rmp_flags & RMPF_SIGNAL) {
  486                 struct rmlock *rm;
  487                 struct turnstile *ts;
  488 
  489                 rm = tracker->rmp_rmlock;
  490 
  491                 turnstile_chain_lock(&rm->lock_object);
  492                 mtx_unlock_spin(&rm_spinlock);
  493 
  494                 ts = turnstile_lookup(&rm->lock_object);
  495 
  496                 turnstile_signal(ts, TS_EXCLUSIVE_QUEUE);
  497                 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
  498                 turnstile_chain_unlock(&rm->lock_object);
  499         } else
  500                 mtx_unlock_spin(&rm_spinlock);
  501 }
  502 
  503 void
  504 _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker)
  505 {
  506         struct pcpu *pc;
  507         struct thread *td = tracker->rmp_thread;
  508 
  509         if (SCHEDULER_STOPPED())
  510                 return;
  511 
  512         td->td_critnest++;      /* critical_enter(); */
  513         pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
  514         rm_tracker_remove(pc, tracker);
  515         td->td_critnest--;
  516         sched_unpin();
  517 
  518         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  519                 THREAD_SLEEPING_OK();
  520 
  521         if (0 == (td->td_owepreempt | tracker->rmp_flags))
  522                 return;
  523 
  524         _rm_unlock_hard(td, tracker);
  525 }
  526 
  527 void
  528 _rm_wlock(struct rmlock *rm)
  529 {
  530         struct rm_priotracker *prio;
  531         struct turnstile *ts;
  532         cpuset_t readcpus;
  533 
  534         if (SCHEDULER_STOPPED())
  535                 return;
  536 
  537         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  538                 sx_xlock(&rm->rm_lock_sx);
  539         else
  540                 mtx_lock(&rm->rm_lock_mtx);
  541 
  542         if (CPU_CMP(&rm->rm_writecpus, &all_cpus)) {
  543                 /* Get all read tokens back */
  544                 readcpus = all_cpus;
  545                 CPU_NAND(&readcpus, &rm->rm_writecpus);
  546                 rm->rm_writecpus = all_cpus;
  547 
  548                 /*
  549                  * Assumes rm->rm_writecpus update is visible on other CPUs
  550                  * before rm_cleanIPI is called.
  551                  */
  552 #ifdef SMP
  553                 smp_rendezvous_cpus(readcpus,
  554                     smp_no_rendevous_barrier,
  555                     rm_cleanIPI,
  556                     smp_no_rendevous_barrier,
  557                     rm);
  558 
  559 #else
  560                 rm_cleanIPI(rm);
  561 #endif
  562 
  563                 mtx_lock_spin(&rm_spinlock);
  564                 while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) {
  565                         ts = turnstile_trywait(&rm->lock_object);
  566                         prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL;
  567                         mtx_unlock_spin(&rm_spinlock);
  568                         turnstile_wait(ts, prio->rmp_thread,
  569                             TS_EXCLUSIVE_QUEUE);
  570                         mtx_lock_spin(&rm_spinlock);
  571                 }
  572                 mtx_unlock_spin(&rm_spinlock);
  573         }
  574 }
  575 
  576 void
  577 _rm_wunlock(struct rmlock *rm)
  578 {
  579 
  580         if (rm->lock_object.lo_flags & LO_SLEEPABLE)
  581                 sx_xunlock(&rm->rm_lock_sx);
  582         else
  583                 mtx_unlock(&rm->rm_lock_mtx);
  584 }
  585 
  586 #if LOCK_DEBUG > 0
  587 
  588 void
  589 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
  590 {
  591 
  592         if (SCHEDULER_STOPPED())
  593                 return;
  594 
  595         KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
  596             ("rm_wlock() by idle thread %p on rmlock %s @ %s:%d",
  597             curthread, rm->lock_object.lo_name, file, line));
  598         KASSERT(!rm_destroyed(rm),
  599             ("rm_wlock() of destroyed rmlock @ %s:%d", file, line));
  600         _rm_assert(rm, RA_UNLOCKED, file, line);
  601 
  602         WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE,
  603             file, line, NULL);
  604 
  605         _rm_wlock(rm);
  606 
  607         LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line);
  608 
  609         WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
  610 
  611         curthread->td_locks++;
  612 
  613 }
  614 
  615 void
  616 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
  617 {
  618 
  619         if (SCHEDULER_STOPPED())
  620                 return;
  621 
  622         KASSERT(!rm_destroyed(rm),
  623             ("rm_wunlock() of destroyed rmlock @ %s:%d", file, line));
  624         _rm_assert(rm, RA_WLOCKED, file, line);
  625         WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
  626         LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line);
  627         _rm_wunlock(rm);
  628         curthread->td_locks--;
  629 }
  630 
  631 int
  632 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
  633     int trylock, const char *file, int line)
  634 {
  635 
  636         if (SCHEDULER_STOPPED())
  637                 return (1);
  638 
  639 #ifdef INVARIANTS
  640         if (!(rm->lock_object.lo_flags & LO_RECURSABLE) && !trylock) {
  641                 critical_enter();
  642                 KASSERT(rm_trackers_present(pcpu_find(curcpu), rm,
  643                     curthread) == 0,
  644                     ("rm_rlock: recursed on non-recursive rmlock %s @ %s:%d\n",
  645                     rm->lock_object.lo_name, file, line));
  646                 critical_exit();
  647         }
  648 #endif
  649         KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
  650             ("rm_rlock() by idle thread %p on rmlock %s @ %s:%d",
  651             curthread, rm->lock_object.lo_name, file, line));
  652         KASSERT(!rm_destroyed(rm),
  653             ("rm_rlock() of destroyed rmlock @ %s:%d", file, line));
  654         if (!trylock) {
  655                 KASSERT(!rm_wowned(rm),
  656                     ("rm_rlock: wlock already held for %s @ %s:%d",
  657                     rm->lock_object.lo_name, file, line));
  658                 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER, file, line,
  659                     NULL);
  660         }
  661 
  662         if (_rm_rlock(rm, tracker, trylock)) {
  663                 if (trylock)
  664                         LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 1, file,
  665                             line);
  666                 else
  667                         LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file,
  668                             line);
  669                 WITNESS_LOCK(&rm->lock_object, 0, file, line);
  670 
  671                 curthread->td_locks++;
  672 
  673                 return (1);
  674         } else if (trylock)
  675                 LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 0, file, line);
  676 
  677         return (0);
  678 }
  679 
  680 void
  681 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
  682     const char *file, int line)
  683 {
  684 
  685         if (SCHEDULER_STOPPED())
  686                 return;
  687 
  688         KASSERT(!rm_destroyed(rm),
  689             ("rm_runlock() of destroyed rmlock @ %s:%d", file, line));
  690         _rm_assert(rm, RA_RLOCKED, file, line);
  691         WITNESS_UNLOCK(&rm->lock_object, 0, file, line);
  692         LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line);
  693         _rm_runlock(rm, tracker);
  694         curthread->td_locks--;
  695 }
  696 
  697 #else
  698 
  699 /*
  700  * Just strip out file and line arguments if no lock debugging is enabled in
  701  * the kernel - we are called from a kernel module.
  702  */
  703 void
  704 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
  705 {
  706 
  707         _rm_wlock(rm);
  708 }
  709 
  710 void
  711 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
  712 {
  713 
  714         _rm_wunlock(rm);
  715 }
  716 
  717 int
  718 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
  719     int trylock, const char *file, int line)
  720 {
  721 
  722         return _rm_rlock(rm, tracker, trylock);
  723 }
  724 
  725 void
  726 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
  727     const char *file, int line)
  728 {
  729 
  730         _rm_runlock(rm, tracker);
  731 }
  732 
  733 #endif
  734 
  735 #ifdef INVARIANT_SUPPORT
  736 #ifndef INVARIANTS
  737 #undef _rm_assert
  738 #endif
  739 
  740 /*
  741  * Note that this does not need to use witness_assert() for read lock
  742  * assertions since an exact count of read locks held by this thread
  743  * is computable.
  744  */
  745 void
  746 _rm_assert(const struct rmlock *rm, int what, const char *file, int line)
  747 {
  748         int count;
  749 
  750         if (panicstr != NULL)
  751                 return;
  752         switch (what) {
  753         case RA_LOCKED:
  754         case RA_LOCKED | RA_RECURSED:
  755         case RA_LOCKED | RA_NOTRECURSED:
  756         case RA_RLOCKED:
  757         case RA_RLOCKED | RA_RECURSED:
  758         case RA_RLOCKED | RA_NOTRECURSED:
  759                 /*
  760                  * Handle the write-locked case.  Unlike other
  761                  * primitives, writers can never recurse.
  762                  */
  763                 if (rm_wowned(rm)) {
  764                         if (what & RA_RLOCKED)
  765                                 panic("Lock %s exclusively locked @ %s:%d\n",
  766                                     rm->lock_object.lo_name, file, line);
  767                         if (what & RA_RECURSED)
  768                                 panic("Lock %s not recursed @ %s:%d\n",
  769                                     rm->lock_object.lo_name, file, line);
  770                         break;
  771                 }
  772 
  773                 critical_enter();
  774                 count = rm_trackers_present(pcpu_find(curcpu), rm, curthread);
  775                 critical_exit();
  776 
  777                 if (count == 0)
  778                         panic("Lock %s not %slocked @ %s:%d\n",
  779                             rm->lock_object.lo_name, (what & RA_RLOCKED) ?
  780                             "read " : "", file, line);
  781                 if (count > 1) {
  782                         if (what & RA_NOTRECURSED)
  783                                 panic("Lock %s recursed @ %s:%d\n",
  784                                     rm->lock_object.lo_name, file, line);
  785                 } else if (what & RA_RECURSED)
  786                         panic("Lock %s not recursed @ %s:%d\n",
  787                             rm->lock_object.lo_name, file, line);
  788                 break;
  789         case RA_WLOCKED:
  790                 if (!rm_wowned(rm))
  791                         panic("Lock %s not exclusively locked @ %s:%d\n",
  792                             rm->lock_object.lo_name, file, line);
  793                 break;
  794         case RA_UNLOCKED:
  795                 if (rm_wowned(rm))
  796                         panic("Lock %s exclusively locked @ %s:%d\n",
  797                             rm->lock_object.lo_name, file, line);
  798 
  799                 critical_enter();
  800                 count = rm_trackers_present(pcpu_find(curcpu), rm, curthread);
  801                 critical_exit();
  802 
  803                 if (count != 0)
  804                         panic("Lock %s read locked @ %s:%d\n",
  805                             rm->lock_object.lo_name, file, line);
  806                 break;
  807         default:
  808                 panic("Unknown rm lock assertion: %d @ %s:%d", what, file,
  809                     line);
  810         }
  811 }
  812 #endif /* INVARIANT_SUPPORT */
  813 
  814 #ifdef DDB
  815 static void
  816 print_tracker(struct rm_priotracker *tr)
  817 {
  818         struct thread *td;
  819 
  820         td = tr->rmp_thread;
  821         db_printf("   thread %p (tid %d, pid %d, \"%s\") {", td, td->td_tid,
  822             td->td_proc->p_pid, td->td_name);
  823         if (tr->rmp_flags & RMPF_ONQUEUE) {
  824                 db_printf("ONQUEUE");
  825                 if (tr->rmp_flags & RMPF_SIGNAL)
  826                         db_printf(",SIGNAL");
  827         } else
  828                 db_printf("");
  829         db_printf("}\n");
  830 }
  831 
  832 static void
  833 db_show_rm(const struct lock_object *lock)
  834 {
  835         struct rm_priotracker *tr;
  836         struct rm_queue *queue;
  837         const struct rmlock *rm;
  838         struct lock_class *lc;
  839         struct pcpu *pc;
  840 
  841         rm = (const struct rmlock *)lock;
  842         db_printf(" writecpus: ");
  843         ddb_display_cpuset(__DEQUALIFY(const cpuset_t *, &rm->rm_writecpus));
  844         db_printf("\n");
  845         db_printf(" per-CPU readers:\n");
  846         STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)
  847                 for (queue = pc->pc_rm_queue.rmq_next;
  848                     queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
  849                         tr = (struct rm_priotracker *)queue;
  850                         if (tr->rmp_rmlock == rm)
  851                                 print_tracker(tr);
  852                 }
  853         db_printf(" active readers:\n");
  854         LIST_FOREACH(tr, &rm->rm_activeReaders, rmp_qentry)
  855                 print_tracker(tr);
  856         lc = LOCK_CLASS(&rm->rm_wlock_object);
  857         db_printf("Backing write-lock (%s):\n", lc->lc_name);
  858         lc->lc_ddb_show(&rm->rm_wlock_object);
  859 }
  860 #endif

Cache object: 69a5a3670578916aa599dbbeb3f98120


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