Merge branch 'smp-hotplug-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
authorLinus Torvalds <torvalds@linux-foundation.org>
Tue, 15 Mar 2016 20:50:29 +0000 (13:50 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 15 Mar 2016 20:50:29 +0000 (13:50 -0700)
Pull cpu hotplug updates from Thomas Gleixner:
 "This is the first part of the ongoing cpu hotplug rework:

   - Initial implementation of the state machine

   - Runs all online and prepare down callbacks on the plugged cpu and
     not on some random processor

   - Replaces busy loop waiting with completions

   - Adds tracepoints so the states can be followed"

More detailed commentary on this work from an earlier email:
 "What's wrong with the current cpu hotplug infrastructure?

   - Asymmetry

     The hotplug notifier mechanism is asymmetric versus the bringup and
     teardown.  This is mostly caused by the notifier mechanism.

   - Largely undocumented dependencies

     While some notifiers use explicitely defined notifier priorities,
     we have quite some notifiers which use numerical priorities to
     express dependencies without any documentation why.

   - Control processor driven

     Most of the bringup/teardown of a cpu is driven by a control
     processor.  While it is understandable, that preperatory steps,
     like idle thread creation, memory allocation for and initialization
     of essential facilities needs to be done before a cpu can boot,
     there is no reason why everything else must run on a control
     processor.  Before this patch series, bringup looks like this:

       Control CPU                     Booting CPU

       do preparatory steps
       kick cpu into life

                                       do low level init

       sync with booting cpu           sync with control cpu

       bring the rest up

   - All or nothing approach

     There is no way to do partial bringups.  That's something which is
     really desired because we waste e.g.  at boot substantial amount of
     time just busy waiting that the cpu comes to life.  That's stupid
     as we could very well do preparatory steps and the initial IPI for
     other cpus and then go back and do the necessary low level
     synchronization with the freshly booted cpu.

   - Minimal debuggability

     Due to the notifier based design, it's impossible to switch between
     two stages of the bringup/teardown back and forth in order to test
     the correctness.  So in many hotplug notifiers the cancel
     mechanisms are either not existant or completely untested.

   - Notifier [un]registering is tedious

     To [un]register notifiers we need to protect against hotplug at
     every callsite.  There is no mechanism that bringup/teardown
     callbacks are issued on the online cpus, so every caller needs to
     do it itself.  That also includes error rollback.

  What's the new design?

     The base of the new design is a symmetric state machine, where both
     the control processor and the booting/dying cpu execute a well
     defined set of states.  Each state is symmetric in the end, except
     for some well defined exceptions, and the bringup/teardown can be
     stopped and reversed at almost all states.

     So the bringup of a cpu will look like this in the future:

       Control CPU                     Booting CPU

       do preparatory steps
       kick cpu into life

                                       do low level init

       sync with booting cpu           sync with control cpu

                                       bring itself up

     The synchronization step does not require the control cpu to wait.
     That mechanism can be done asynchronously via a worker or some
     other mechanism.

     The teardown can be made very similar, so that the dying cpu cleans
     up and brings itself down.  Cleanups which need to be done after
     the cpu is gone, can be scheduled asynchronously as well.

  There is a long way to this, as we need to refactor the notion when a
  cpu is available.  Today we set the cpu online right after it comes
  out of the low level bringup, which is not really correct.

  The proper mechanism is to set it to available, i.e. cpu local
  threads, like softirqd, hotplug thread etc. can be scheduled on that
  cpu, and once it finished all booting steps, it's set to online, so
  general workloads can be scheduled on it.  The reverse happens on
  teardown.  First thing to do is to forbid scheduling of general
  workloads, then teardown all the per cpu resources and finally shut it
  off completely.

  This patch series implements the basic infrastructure for this at the
  core level.  This includes the following:

   - Basic state machine implementation with well defined states, so
     ordering and prioritization can be expressed.

   - Interfaces to [un]register state callbacks

     This invokes the bringup/teardown callback on all online cpus with
     the proper protection in place and [un]installs the callbacks in
     the state machine array.

     For callbacks which have no particular ordering requirement we have
     a dynamic state space, so that drivers don't have to register an
     explicit hotplug state.

     If a callback fails, the code automatically does a rollback to the
     previous state.

   - Sysfs interface to drive the state machine to a particular step.

     This is only partially functional today.  Full functionality and
     therefor testability will be achieved once we converted all
     existing hotplug notifiers over to the new scheme.

   - Run all CPU_ONLINE/DOWN_PREPARE notifiers on the booting/dying
     processor:

       Control CPU                     Booting CPU

       do preparatory steps
       kick cpu into life

                                       do low level init

       sync with booting cpu           sync with control cpu
       wait for boot
                                       bring itself up

                                       Signal completion to control cpu

     In a previous step of this work we've done a full tree mechanical
     conversion of all hotplug notifiers to the new scheme.  The balance
     is a net removal of about 4000 lines of code.

     This is not included in this series, as we decided to take a
     different approach.  Instead of mechanically converting everything
     over, we will do a proper overhaul of the usage sites one by one so
     they nicely fit into the symmetric callback scheme.

     I decided to do that after I looked at the ugliness of some of the
     converted sites and figured out that their hotplug mechanism is
     completely buggered anyway.  So there is no point to do a
     mechanical conversion first as we need to go through the usage
     sites one by one again in order to achieve a full symmetric and
     testable behaviour"

* 'smp-hotplug-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (23 commits)
  cpu/hotplug: Document states better
  cpu/hotplug: Fix smpboot thread ordering
  cpu/hotplug: Remove redundant state check
  cpu/hotplug: Plug death reporting race
  rcu: Make CPU_DYING_IDLE an explicit call
  cpu/hotplug: Make wait for dead cpu completion based
  cpu/hotplug: Let upcoming cpu bring itself fully up
  arch/hotplug: Call into idle with a proper state
  cpu/hotplug: Move online calls to hotplugged cpu
  cpu/hotplug: Create hotplug threads
  cpu/hotplug: Split out the state walk into functions
  cpu/hotplug: Unpark smpboot threads from the state machine
  cpu/hotplug: Move scheduler cpu_online notifier to hotplug core
  cpu/hotplug: Implement setup/removal interface
  cpu/hotplug: Make target state writeable
  cpu/hotplug: Add sysfs state interface
  cpu/hotplug: Hand in target state to _cpu_up/down
  cpu/hotplug: Convert the hotplugged cpu work to a state machine
  cpu/hotplug: Convert to a state machine for the control processor
  cpu/hotplug: Add tracepoints
  ...

1  2 
arch/mips/kernel/smp.c
arch/x86/kernel/smpboot.c
include/linux/rcupdate.h
init/main.c
kernel/rcu/tree.c
kernel/sched/core.c
kernel/smp.c

Simple merge
@@@ -256,100 -248,9 +256,100 @@@ static void notrace start_secondary(voi
        x86_cpuinit.setup_percpu_clockev();
  
        wmb();
-       cpu_startup_entry(CPUHP_ONLINE);
+       cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  }
  
 +int topology_update_package_map(unsigned int apicid, unsigned int cpu)
 +{
 +      unsigned int new, pkg = apicid >> boot_cpu_data.x86_coreid_bits;
 +
 +      /* Called from early boot ? */
 +      if (!physical_package_map)
 +              return 0;
 +
 +      if (pkg >= max_physical_pkg_id)
 +              return -EINVAL;
 +
 +      /* Set the logical package id */
 +      if (test_and_set_bit(pkg, physical_package_map))
 +              goto found;
 +
 +      if (pkg < __max_logical_packages) {
 +              set_bit(pkg, logical_package_map);
 +              physical_to_logical_pkg[pkg] = pkg;
 +              goto found;
 +      }
 +      new = find_first_zero_bit(logical_package_map, __max_logical_packages);
 +      if (new >= __max_logical_packages) {
 +              physical_to_logical_pkg[pkg] = -1;
 +              pr_warn("APIC(%x) Package %u exceeds logical package map\n",
 +                      apicid, pkg);
 +              return -ENOSPC;
 +      }
 +      set_bit(new, logical_package_map);
 +      pr_info("APIC(%x) Converting physical %u to logical package %u\n",
 +              apicid, pkg, new);
 +      physical_to_logical_pkg[pkg] = new;
 +
 +found:
 +      cpu_data(cpu).logical_proc_id = physical_to_logical_pkg[pkg];
 +      return 0;
 +}
 +
 +/**
 + * topology_phys_to_logical_pkg - Map a physical package id to a logical
 + *
 + * Returns logical package id or -1 if not found
 + */
 +int topology_phys_to_logical_pkg(unsigned int phys_pkg)
 +{
 +      if (phys_pkg >= max_physical_pkg_id)
 +              return -1;
 +      return physical_to_logical_pkg[phys_pkg];
 +}
 +EXPORT_SYMBOL(topology_phys_to_logical_pkg);
 +
 +static void __init smp_init_package_map(void)
 +{
 +      unsigned int ncpus, cpu;
 +      size_t size;
 +
 +      /*
 +       * Today neither Intel nor AMD support heterogenous systems. That
 +       * might change in the future....
 +       */
 +      ncpus = boot_cpu_data.x86_max_cores * smp_num_siblings;
 +      __max_logical_packages = DIV_ROUND_UP(nr_cpu_ids, ncpus);
 +
 +      /*
 +       * Possibly larger than what we need as the number of apic ids per
 +       * package can be smaller than the actual used apic ids.
 +       */
 +      max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
 +      size = max_physical_pkg_id * sizeof(unsigned int);
 +      physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
 +      memset(physical_to_logical_pkg, 0xff, size);
 +      size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
 +      physical_package_map = kzalloc(size, GFP_KERNEL);
 +      size = BITS_TO_LONGS(__max_logical_packages) * sizeof(unsigned long);
 +      logical_package_map = kzalloc(size, GFP_KERNEL);
 +
 +      pr_info("Max logical packages: %u\n", __max_logical_packages);
 +
 +      for_each_present_cpu(cpu) {
 +              unsigned int apicid = apic->cpu_present_to_apicid(cpu);
 +
 +              if (apicid == BAD_APICID || !apic->apic_id_valid(apicid))
 +                      continue;
 +              if (!topology_update_package_map(apicid, cpu))
 +                      continue;
 +              pr_warn("CPU %u APICId %x disabled\n", cpu, apicid);
 +              per_cpu(x86_bios_cpu_apicid, cpu) = BAD_APICID;
 +              set_cpu_possible(cpu, false);
 +              set_cpu_present(cpu, false);
 +      }
 +}
 +
  void __init smp_store_boot_cpu_info(void)
  {
        int id = 0; /* CPU 0 */
Simple merge
diff --cc init/main.c
Simple merge
@@@ -4246,6 -4225,43 +4224,46 @@@ static void rcu_prepare_cpu(int cpu
                rcu_init_percpu_data(cpu, rsp);
  }
  
 -      raw_spin_unlock_irqrestore(&rnp->lock, flags);
+ #ifdef CONFIG_HOTPLUG_CPU
+ /*
++ * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
++ * function.  We now remove it from the rcu_node tree's ->qsmaskinit
++ * bit masks.
+  * The CPU is exiting the idle loop into the arch_cpu_idle_dead()
+  * function.  We now remove it from the rcu_node tree's ->qsmaskinit
+  * bit masks.
+  */
+ static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp)
+ {
+       unsigned long flags;
+       unsigned long mask;
+       struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu);
+       struct rcu_node *rnp = rdp->mynode;  /* Outgoing CPU's rdp & rnp. */
+       if (!IS_ENABLED(CONFIG_HOTPLUG_CPU))
+               return;
+       /* Remove outgoing CPU from mask in the leaf rcu_node structure. */
+       mask = rdp->grpmask;
+       raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */
+       rnp->qsmaskinitnext &= ~mask;
++      raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
+ }
+ void rcu_report_dead(unsigned int cpu)
+ {
+       struct rcu_state *rsp;
+       /* QS for any half-done expedited RCU-sched GP. */
+       preempt_disable();
+       rcu_report_exp_rdp(&rcu_sched_state,
+                          this_cpu_ptr(rcu_sched_state.rda), true);
+       preempt_enable();
+       for_each_rcu_flavor(rsp)
+               rcu_cleanup_dying_idle_cpu(cpu, rsp);
+ }
+ #endif
  /*
   * Handle CPU online/offline notification events.
   */
Simple merge
diff --cc kernel/smp.c
Simple merge