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/subr_smp.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) 2001, 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  * 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  * This module holds the global variables and machine independent functions
   32  * used for the kernel SMP support.
   33  */
   34 
   35 #include <sys/cdefs.h>
   36 __FBSDID("$FreeBSD$");
   37 
   38 #include <sys/param.h>
   39 #include <sys/systm.h>
   40 #include <sys/kernel.h>
   41 #include <sys/ktr.h>
   42 #include <sys/proc.h>
   43 #include <sys/bus.h>
   44 #include <sys/lock.h>
   45 #include <sys/mutex.h>
   46 #include <sys/pcpu.h>
   47 #include <sys/smp.h>
   48 #include <sys/sysctl.h>
   49 
   50 #include <machine/cpu.h>
   51 #include <machine/smp.h>
   52 
   53 #include "opt_sched.h"
   54 
   55 #ifdef SMP
   56 volatile cpumask_t stopped_cpus;
   57 volatile cpumask_t started_cpus;
   58 cpumask_t idle_cpus_mask;
   59 cpumask_t hlt_cpus_mask;
   60 cpumask_t logical_cpus_mask;
   61 
   62 void (*cpustop_restartfunc)(void);
   63 #endif
   64 /* This is used in modules that need to work in both SMP and UP. */
   65 cpumask_t all_cpus;
   66 
   67 int mp_ncpus;
   68 /* export this for libkvm consumers. */
   69 int mp_maxcpus = MAXCPU;
   70 
   71 struct cpu_top *smp_topology;
   72 volatile int smp_started;
   73 u_int mp_maxid;
   74 
   75 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
   76 
   77 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0,
   78     "Max CPU ID.");
   79 
   80 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
   81     "Max number of CPUs that the system was compiled for.");
   82 
   83 int smp_active = 0;     /* are the APs allowed to run? */
   84 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
   85     "Number of Auxillary Processors (APs) that were successfully started");
   86 
   87 int smp_disabled = 0;   /* has smp been disabled? */
   88 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
   89     "SMP has been disabled from the loader");
   90 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
   91 
   92 int smp_cpus = 1;       /* how many cpu's running */
   93 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
   94     "Number of CPUs online");
   95 
   96 #ifdef SMP
   97 /* Enable forwarding of a signal to a process running on a different CPU */
   98 static int forward_signal_enabled = 1;
   99 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
  100            &forward_signal_enabled, 0,
  101            "Forwarding of a signal to a process on a different CPU");
  102 
  103 /* Enable forwarding of roundrobin to all other cpus */
  104 static int forward_roundrobin_enabled = 1;
  105 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
  106            &forward_roundrobin_enabled, 0,
  107            "Forwarding of roundrobin to all other CPUs");
  108 
  109 /* Variables needed for SMP rendezvous. */
  110 static volatile int smp_rv_ncpus;
  111 static void (*volatile smp_rv_setup_func)(void *arg);
  112 static void (*volatile smp_rv_action_func)(void *arg);
  113 static void (*volatile smp_rv_teardown_func)(void *arg);
  114 static void * volatile smp_rv_func_arg;
  115 static volatile int smp_rv_waiters[4];
  116 
  117 /* 
  118  * Shared mutex to restrict busywaits between smp_rendezvous() and
  119  * smp(_targeted)_tlb_shootdown().  A deadlock occurs if both of these
  120  * functions trigger at once and cause multiple CPUs to busywait with
  121  * interrupts disabled. 
  122  */
  123 struct mtx smp_ipi_mtx;
  124 
  125 /*
  126  * Let the MD SMP code initialize mp_maxid very early if it can.
  127  */
  128 static void
  129 mp_setmaxid(void *dummy)
  130 {
  131         cpu_mp_setmaxid();
  132 }
  133 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
  134 
  135 /*
  136  * Call the MD SMP initialization code.
  137  */
  138 static void
  139 mp_start(void *dummy)
  140 {
  141 
  142         /* Probe for MP hardware. */
  143         if (smp_disabled != 0 || cpu_mp_probe() == 0) {
  144                 mp_ncpus = 1;
  145                 all_cpus = PCPU_GET(cpumask);
  146                 return;
  147         }
  148 
  149         mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
  150         cpu_mp_start();
  151         printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
  152             mp_ncpus);
  153         cpu_mp_announce();
  154 }
  155 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
  156 
  157 void
  158 forward_signal(struct thread *td)
  159 {
  160         int id;
  161 
  162         /*
  163          * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
  164          * this thread, so all we need to do is poke it if it is currently
  165          * executing so that it executes ast().
  166          */
  167         THREAD_LOCK_ASSERT(td, MA_OWNED);
  168         KASSERT(TD_IS_RUNNING(td),
  169             ("forward_signal: thread is not TDS_RUNNING"));
  170 
  171         CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
  172 
  173         if (!smp_started || cold || panicstr)
  174                 return;
  175         if (!forward_signal_enabled)
  176                 return;
  177 
  178         /* No need to IPI ourself. */
  179         if (td == curthread)
  180                 return;
  181 
  182         id = td->td_oncpu;
  183         if (id == NOCPU)
  184                 return;
  185         ipi_selected(1 << id, IPI_AST);
  186 }
  187 
  188 void
  189 forward_roundrobin(void)
  190 {
  191         struct pcpu *pc;
  192         struct thread *td;
  193         cpumask_t id, map, me;
  194 
  195         CTR0(KTR_SMP, "forward_roundrobin()");
  196 
  197         if (!smp_started || cold || panicstr)
  198                 return;
  199         if (!forward_roundrobin_enabled)
  200                 return;
  201         map = 0;
  202         me = PCPU_GET(cpumask);
  203         SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
  204                 td = pc->pc_curthread;
  205                 id = pc->pc_cpumask;
  206                 if (id != me && (id & stopped_cpus) == 0 &&
  207                     !TD_IS_IDLETHREAD(td)) {
  208                         td->td_flags |= TDF_NEEDRESCHED;
  209                         map |= id;
  210                 }
  211         }
  212         ipi_selected(map, IPI_AST);
  213 }
  214 
  215 /*
  216  * When called the executing CPU will send an IPI to all other CPUs
  217  *  requesting that they halt execution.
  218  *
  219  * Usually (but not necessarily) called with 'other_cpus' as its arg.
  220  *
  221  *  - Signals all CPUs in map to stop.
  222  *  - Waits for each to stop.
  223  *
  224  * Returns:
  225  *  -1: error
  226  *   0: NA
  227  *   1: ok
  228  *
  229  * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
  230  *            from executing at same time.
  231  */
  232 int
  233 stop_cpus(cpumask_t map)
  234 {
  235         int i;
  236 
  237         if (!smp_started)
  238                 return 0;
  239 
  240         CTR1(KTR_SMP, "stop_cpus(%x)", map);
  241 
  242         /* send the stop IPI to all CPUs in map */
  243         ipi_selected(map, IPI_STOP);
  244 
  245         i = 0;
  246         while ((stopped_cpus & map) != map) {
  247                 /* spin */
  248                 cpu_spinwait();
  249                 i++;
  250 #ifdef DIAGNOSTIC
  251                 if (i == 100000) {
  252                         printf("timeout stopping cpus\n");
  253                         break;
  254                 }
  255 #endif
  256         }
  257 
  258         return 1;
  259 }
  260 
  261 /*
  262  * Called by a CPU to restart stopped CPUs. 
  263  *
  264  * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
  265  *
  266  *  - Signals all CPUs in map to restart.
  267  *  - Waits for each to restart.
  268  *
  269  * Returns:
  270  *  -1: error
  271  *   0: NA
  272  *   1: ok
  273  */
  274 int
  275 restart_cpus(cpumask_t map)
  276 {
  277 
  278         if (!smp_started)
  279                 return 0;
  280 
  281         CTR1(KTR_SMP, "restart_cpus(%x)", map);
  282 
  283         /* signal other cpus to restart */
  284         atomic_store_rel_int(&started_cpus, map);
  285 
  286         /* wait for each to clear its bit */
  287         while ((stopped_cpus & map) != 0)
  288                 cpu_spinwait();
  289 
  290         return 1;
  291 }
  292 
  293 /*
  294  * All-CPU rendezvous.  CPUs are signalled, all execute the setup function 
  295  * (if specified), rendezvous, execute the action function (if specified),
  296  * rendezvous again, execute the teardown function (if specified), and then
  297  * resume.
  298  *
  299  * Note that the supplied external functions _must_ be reentrant and aware
  300  * that they are running in parallel and in an unknown lock context.
  301  */
  302 void
  303 smp_rendezvous_action(void)
  304 {
  305         struct thread *td;
  306         void *local_func_arg;
  307         void (*local_setup_func)(void*);
  308         void (*local_action_func)(void*);
  309         void (*local_teardown_func)(void*);
  310 #ifdef INVARIANTS
  311         int owepreempt;
  312 #endif
  313 
  314         /* Ensure we have up-to-date values. */
  315         atomic_add_acq_int(&smp_rv_waiters[0], 1);
  316         while (smp_rv_waiters[0] < smp_rv_ncpus)
  317                 cpu_spinwait();
  318 
  319         /* Fetch rendezvous parameters after acquire barrier. */
  320         local_func_arg = smp_rv_func_arg;
  321         local_setup_func = smp_rv_setup_func;
  322         local_action_func = smp_rv_action_func;
  323         local_teardown_func = smp_rv_teardown_func;
  324 
  325         /*
  326          * Use a nested critical section to prevent any preemptions
  327          * from occurring during a rendezvous action routine.
  328          * Specifically, if a rendezvous handler is invoked via an IPI
  329          * and the interrupted thread was in the critical_exit()
  330          * function after setting td_critnest to 0 but before
  331          * performing a deferred preemption, this routine can be
  332          * invoked with td_critnest set to 0 and td_owepreempt true.
  333          * In that case, a critical_exit() during the rendezvous
  334          * action would trigger a preemption which is not permitted in
  335          * a rendezvous action.  To fix this, wrap all of the
  336          * rendezvous action handlers in a critical section.  We
  337          * cannot use a regular critical section however as having
  338          * critical_exit() preempt from this routine would also be
  339          * problematic (the preemption must not occur before the IPI
  340          * has been acknowleged via an EOI).  Instead, we
  341          * intentionally ignore td_owepreempt when leaving the
  342          * critical setion.  This should be harmless because we do not
  343          * permit rendezvous action routines to schedule threads, and
  344          * thus td_owepreempt should never transition from 0 to 1
  345          * during this routine.
  346          */
  347         td = curthread;
  348         td->td_critnest++;
  349 #ifdef INVARIANTS
  350         owepreempt = td->td_owepreempt;
  351 #endif
  352         
  353         /*
  354          * If requested, run a setup function before the main action
  355          * function.  Ensure all CPUs have completed the setup
  356          * function before moving on to the action function.
  357          */
  358         if (local_setup_func != smp_no_rendevous_barrier) {
  359                 if (smp_rv_setup_func != NULL)
  360                         smp_rv_setup_func(smp_rv_func_arg);
  361                 atomic_add_int(&smp_rv_waiters[1], 1);
  362                 while (smp_rv_waiters[1] < smp_rv_ncpus)
  363                         cpu_spinwait();
  364         }
  365 
  366         if (local_action_func != NULL)
  367                 local_action_func(local_func_arg);
  368 
  369         if (local_teardown_func != smp_no_rendevous_barrier) {
  370                 /*
  371                  * Signal that the main action has been completed.  If a
  372                  * full exit rendezvous is requested, then all CPUs will
  373                  * wait here until all CPUs have finished the main action.
  374                  */
  375                 atomic_add_int(&smp_rv_waiters[2], 1);
  376                 while (smp_rv_waiters[2] < smp_rv_ncpus)
  377                         cpu_spinwait();
  378 
  379                 if (local_teardown_func != NULL)
  380                         local_teardown_func(local_func_arg);
  381         }
  382 
  383         /*
  384          * Signal that the rendezvous is fully completed by this CPU.
  385          * This means that no member of smp_rv_* pseudo-structure will be
  386          * accessed by this target CPU after this point; in particular,
  387          * memory pointed by smp_rv_func_arg.
  388          */
  389         atomic_add_int(&smp_rv_waiters[3], 1);
  390 
  391         td->td_critnest--;
  392         KASSERT(owepreempt == td->td_owepreempt,
  393             ("rendezvous action changed td_owepreempt"));
  394 }
  395 
  396 void
  397 smp_rendezvous_cpus(cpumask_t map,
  398         void (* setup_func)(void *), 
  399         void (* action_func)(void *),
  400         void (* teardown_func)(void *),
  401         void *arg)
  402 {
  403         int i, ncpus = 0;
  404 
  405         if (!smp_started) {
  406                 if (setup_func != NULL)
  407                         setup_func(arg);
  408                 if (action_func != NULL)
  409                         action_func(arg);
  410                 if (teardown_func != NULL)
  411                         teardown_func(arg);
  412                 return;
  413         }
  414 
  415         for (i = 0; i <= mp_maxid; i++)
  416                 if (((1 << i) & map) != 0 && !CPU_ABSENT(i))
  417                         ncpus++;
  418         if (ncpus == 0)
  419                 panic("ncpus is 0 with map=0x%x", map);
  420 
  421         mtx_lock_spin(&smp_ipi_mtx);
  422 
  423         /* Pass rendezvous parameters via global variables. */
  424         smp_rv_ncpus = ncpus;
  425         smp_rv_setup_func = setup_func;
  426         smp_rv_action_func = action_func;
  427         smp_rv_teardown_func = teardown_func;
  428         smp_rv_func_arg = arg;
  429         smp_rv_waiters[1] = 0;
  430         smp_rv_waiters[2] = 0;
  431         smp_rv_waiters[3] = 0;
  432         atomic_store_rel_int(&smp_rv_waiters[0], 0);
  433 
  434         /*
  435          * Signal other processors, which will enter the IPI with
  436          * interrupts off.
  437          */
  438         ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS);
  439 
  440         /* Check if the current CPU is in the map */
  441         if ((map & (1 << curcpu)) != 0)
  442                 smp_rendezvous_action();
  443 
  444         /*
  445          * Ensure that the master CPU waits for all the other
  446          * CPUs to finish the rendezvous, so that smp_rv_*
  447          * pseudo-structure and the arg are guaranteed to not
  448          * be in use.
  449          */
  450         while (atomic_load_acq_int(&smp_rv_waiters[3]) < ncpus)
  451                 cpu_spinwait();
  452 
  453         mtx_unlock_spin(&smp_ipi_mtx);
  454 }
  455 
  456 void
  457 smp_rendezvous(void (* setup_func)(void *), 
  458                void (* action_func)(void *),
  459                void (* teardown_func)(void *),
  460                void *arg)
  461 {
  462         smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
  463 }
  464 #else /* !SMP */
  465 
  466 /*
  467  * Provide dummy SMP support for UP kernels.  Modules that need to use SMP
  468  * APIs will still work using this dummy support.
  469  */
  470 static void
  471 mp_setvariables_for_up(void *dummy)
  472 {
  473         mp_ncpus = 1;
  474         mp_maxid = PCPU_GET(cpuid);
  475         all_cpus = PCPU_GET(cpumask);
  476         KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
  477 }
  478 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
  479     mp_setvariables_for_up, NULL);
  480 
  481 void
  482 smp_rendezvous_cpus(cpumask_t map,
  483         void (*setup_func)(void *), 
  484         void (*action_func)(void *),
  485         void (*teardown_func)(void *),
  486         void *arg)
  487 {
  488         if (setup_func != NULL)
  489                 setup_func(arg);
  490         if (action_func != NULL)
  491                 action_func(arg);
  492         if (teardown_func != NULL)
  493                 teardown_func(arg);
  494 }
  495 
  496 void
  497 smp_rendezvous(void (*setup_func)(void *), 
  498                void (*action_func)(void *),
  499                void (*teardown_func)(void *),
  500                void *arg)
  501 {
  502 
  503         if (setup_func != NULL)
  504                 setup_func(arg);
  505         if (action_func != NULL)
  506                 action_func(arg);
  507         if (teardown_func != NULL)
  508                 teardown_func(arg);
  509 }
  510 #endif /* SMP */
  511 
  512 void
  513 smp_no_rendevous_barrier(void *dummy)
  514 {
  515 #ifdef SMP
  516         KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));
  517 #endif
  518 }

Cache object: f7fcc42578be6887a3a07028907d809a


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