Skip to content

Commit

Permalink
Merge branch 'smp-hotplug-for-linus' of git://git.kernel.org/pub/scm/…
Browse files Browse the repository at this point in the history
…linux/kernel/git/tip/tip

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
  ...
  • Loading branch information
torvalds committed Mar 15, 2016
2 parents df2e37c + d10ef6f commit 710d60c
Show file tree
Hide file tree
Showing 35 changed files with 1,294 additions and 236 deletions.
2 changes: 1 addition & 1 deletion arch/alpha/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ smp_callin(void)
cpuid, current, current->active_mm));

preempt_disable();
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

/* Wait until hwrpb->txrdy is clear for cpu. Return -1 on timeout. */
Expand Down
2 changes: 1 addition & 1 deletion arch/arc/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void start_kernel_secondary(void)

local_irq_enable();
preempt_disable();
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion arch/arm/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ asmlinkage void secondary_start_kernel(void)
/*
* OK, it's off to the idle thread for us
*/
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

void __init smp_cpus_done(unsigned int max_cpus)
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ asmlinkage void secondary_start_kernel(void)
/*
* OK, it's off to the idle thread for us
*/
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

#ifdef CONFIG_HOTPLUG_CPU
Expand Down
2 changes: 1 addition & 1 deletion arch/blackfin/mach-common/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ void secondary_start_kernel(void)

/* We are done with local CPU inits, unblock the boot CPU. */
set_cpu_online(cpu, true);
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

void __init smp_prepare_boot_cpu(void)
Expand Down
2 changes: 1 addition & 1 deletion arch/hexagon/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void start_secondary(void)

local_irq_enable();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}


Expand Down
2 changes: 1 addition & 1 deletion arch/ia64/kernel/smpboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ start_secondary (void *unused)
preempt_disable();
smp_callin();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion arch/m32r/kernel/smpboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ int __init start_secondary(void *unused)
*/
local_flush_tlb_all();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion arch/metag/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ asmlinkage void secondary_start_kernel(void)
/*
* OK, it's off to the idle thread for us
*/
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

void __init smp_cpus_done(unsigned int max_cpus)
Expand Down
2 changes: 1 addition & 1 deletion arch/mips/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ asmlinkage void start_secondary(void)
WARN_ON_ONCE(!irqs_disabled());
mp_ops->smp_finish();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

static void stop_this_cpu(void *dummy)
Expand Down
2 changes: 1 addition & 1 deletion arch/mn10300/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ int __init start_secondary(void *unused)
#ifdef CONFIG_GENERIC_CLOCKEVENTS
init_clockevents();
#endif
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion arch/parisc/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void __init smp_callin(void)

local_irq_enable(); /* Interrupts have been off until now */

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);

/* NOTREACHED */
panic("smp_callin() AAAAaaaaahhhh....\n");
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ void start_secondary(void *unused)

local_irq_enable();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);

BUG();
}
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ static void smp_start_secondary(void *cpuvoid)
set_cpu_online(smp_processor_id(), true);
inc_irq_stat(CPU_RST);
local_irq_enable();
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

/* Upping and downing of CPUs */
Expand Down
2 changes: 1 addition & 1 deletion arch/sh/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ asmlinkage void start_secondary(void)
set_cpu_online(cpu, true);
per_cpu(cpu_state, cpu) = CPU_ONLINE;

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

extern struct {
Expand Down
2 changes: 1 addition & 1 deletion arch/sparc/kernel/smp_32.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ static void sparc_start_secondary(void *arg)
local_irq_enable();

wmb();
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);

/* We should never reach here! */
BUG();
Expand Down
2 changes: 1 addition & 1 deletion arch/sparc/kernel/smp_64.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void smp_callin(void)

local_irq_enable();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

void cpu_panic(void)
Expand Down
2 changes: 1 addition & 1 deletion arch/tile/kernel/smpboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void online_secondary(void)
/* Set up tile-timer clock-event device on this cpu */
setup_tile_timer();

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

int __cpu_up(unsigned int cpu, struct task_struct *tidle)
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/kernel/smpboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static void notrace start_secondary(void *unused)
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)
Expand Down
2 changes: 1 addition & 1 deletion arch/x86/xen/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ asmlinkage __visible void cpu_bringup_and_idle(int cpu)
xen_pvh_secondary_vcpu_init(cpu);
#endif
cpu_bringup();
cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

static void xen_smp_intr_free(unsigned int cpu)
Expand Down
2 changes: 1 addition & 1 deletion arch/xtensa/kernel/smp.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void secondary_start_kernel(void)

complete(&cpu_running);

cpu_startup_entry(CPUHP_ONLINE);
cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}

static void mx_cpu_start(void *p)
Expand Down
27 changes: 11 additions & 16 deletions include/linux/cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linux/node.h>
#include <linux/compiler.h>
#include <linux/cpumask.h>
#include <linux/cpuhotplug.h>

struct device;
struct device_node;
Expand All @@ -27,6 +28,9 @@ struct cpu {
struct device dev;
};

extern void boot_cpu_init(void);
extern void boot_cpu_state_init(void);

extern int register_cpu(struct cpu *cpu, int num);
extern struct device *get_cpu_device(unsigned cpu);
extern bool cpu_is_hotpluggable(unsigned cpu);
Expand Down Expand Up @@ -74,7 +78,7 @@ enum {
/* migration should happen before other stuff but after perf */
CPU_PRI_PERF = 20,
CPU_PRI_MIGRATION = 10,
CPU_PRI_SMPBOOT = 9,

/* bring up workqueues before normal notifiers and down after */
CPU_PRI_WORKQUEUE_UP = 5,
CPU_PRI_WORKQUEUE_DOWN = -5,
Expand All @@ -97,9 +101,7 @@ enum {
* Called on the new cpu, just before
* enabling interrupts. Must not sleep,
* must not fail */
#define CPU_DYING_IDLE 0x000B /* CPU (unsigned)v dying, reached
* idle loop. */
#define CPU_BROKEN 0x000C /* CPU (unsigned)v did not die properly,
#define CPU_BROKEN 0x000B /* CPU (unsigned)v did not die properly,
* perhaps due to preemption. */

/* Used for CPU hotplug events occurring while tasks are frozen due to a suspend
Expand All @@ -118,6 +120,7 @@ enum {


#ifdef CONFIG_SMP
extern bool cpuhp_tasks_frozen;
/* Need to know about CPUs going up/down? */
#if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE)
#define cpu_notifier(fn, pri) { \
Expand Down Expand Up @@ -167,7 +170,6 @@ static inline void __unregister_cpu_notifier(struct notifier_block *nb)
}
#endif

void smpboot_thread_init(void);
int cpu_up(unsigned int cpu);
void notify_cpu_starting(unsigned int cpu);
extern void cpu_maps_update_begin(void);
Expand All @@ -177,6 +179,7 @@ extern void cpu_maps_update_done(void);
#define cpu_notifier_register_done cpu_maps_update_done

#else /* CONFIG_SMP */
#define cpuhp_tasks_frozen 0

#define cpu_notifier(fn, pri) do { (void)(fn); } while (0)
#define __cpu_notifier(fn, pri) do { (void)(fn); } while (0)
Expand Down Expand Up @@ -215,10 +218,6 @@ static inline void cpu_notifier_register_done(void)
{
}

static inline void smpboot_thread_init(void)
{
}

#endif /* CONFIG_SMP */
extern struct bus_type cpu_subsys;

Expand Down Expand Up @@ -265,11 +264,6 @@ static inline int disable_nonboot_cpus(void) { return 0; }
static inline void enable_nonboot_cpus(void) {}
#endif /* !CONFIG_PM_SLEEP_SMP */

enum cpuhp_state {
CPUHP_OFFLINE,
CPUHP_ONLINE,
};

void cpu_startup_entry(enum cpuhp_state state);

void cpu_idle_poll_ctrl(bool enable);
Expand All @@ -280,14 +274,15 @@ void arch_cpu_idle_enter(void);
void arch_cpu_idle_exit(void);
void arch_cpu_idle_dead(void);

DECLARE_PER_CPU(bool, cpu_dead_idle);

int cpu_report_state(int cpu);
int cpu_check_up_prepare(int cpu);
void cpu_set_state_online(int cpu);
#ifdef CONFIG_HOTPLUG_CPU
bool cpu_wait_death(unsigned int cpu, int seconds);
bool cpu_report_death(void);
void cpuhp_report_idle_dead(void);
#else
static inline void cpuhp_report_idle_dead(void) { }
#endif /* #ifdef CONFIG_HOTPLUG_CPU */

#endif /* _LINUX_CPU_H_ */
93 changes: 93 additions & 0 deletions include/linux/cpuhotplug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifndef __CPUHOTPLUG_H
#define __CPUHOTPLUG_H

enum cpuhp_state {
CPUHP_OFFLINE,
CPUHP_CREATE_THREADS,
CPUHP_NOTIFY_PREPARE,
CPUHP_BRINGUP_CPU,
CPUHP_AP_IDLE_DEAD,
CPUHP_AP_OFFLINE,
CPUHP_AP_NOTIFY_STARTING,
CPUHP_AP_ONLINE,
CPUHP_TEARDOWN_CPU,
CPUHP_AP_ONLINE_IDLE,
CPUHP_AP_SMPBOOT_THREADS,
CPUHP_AP_NOTIFY_ONLINE,
CPUHP_AP_ONLINE_DYN,
CPUHP_AP_ONLINE_DYN_END = CPUHP_AP_ONLINE_DYN + 30,
CPUHP_ONLINE,
};

int __cpuhp_setup_state(enum cpuhp_state state, const char *name, bool invoke,
int (*startup)(unsigned int cpu),
int (*teardown)(unsigned int cpu));

/**
* cpuhp_setup_state - Setup hotplug state callbacks with calling the callbacks
* @state: The state for which the calls are installed
* @name: Name of the callback (will be used in debug output)
* @startup: startup callback function
* @teardown: teardown callback function
*
* Installs the callback functions and invokes the startup callback on
* the present cpus which have already reached the @state.
*/
static inline int cpuhp_setup_state(enum cpuhp_state state,
const char *name,
int (*startup)(unsigned int cpu),
int (*teardown)(unsigned int cpu))
{
return __cpuhp_setup_state(state, name, true, startup, teardown);
}

/**
* cpuhp_setup_state_nocalls - Setup hotplug state callbacks without calling the
* callbacks
* @state: The state for which the calls are installed
* @name: Name of the callback.
* @startup: startup callback function
* @teardown: teardown callback function
*
* Same as @cpuhp_setup_state except that no calls are executed are invoked
* during installation of this callback. NOP if SMP=n or HOTPLUG_CPU=n.
*/
static inline int cpuhp_setup_state_nocalls(enum cpuhp_state state,
const char *name,
int (*startup)(unsigned int cpu),
int (*teardown)(unsigned int cpu))
{
return __cpuhp_setup_state(state, name, false, startup, teardown);
}

void __cpuhp_remove_state(enum cpuhp_state state, bool invoke);

/**
* cpuhp_remove_state - Remove hotplug state callbacks and invoke the teardown
* @state: The state for which the calls are removed
*
* Removes the callback functions and invokes the teardown callback on
* the present cpus which have already reached the @state.
*/
static inline void cpuhp_remove_state(enum cpuhp_state state)
{
__cpuhp_remove_state(state, true);
}

/**
* cpuhp_remove_state_nocalls - Remove hotplug state callbacks without invoking
* teardown
* @state: The state for which the calls are removed
*/
static inline void cpuhp_remove_state_nocalls(enum cpuhp_state state)
{
__cpuhp_remove_state(state, false);
}

#ifdef CONFIG_SMP
void cpuhp_online_idle(enum cpuhp_state state);
#else
static inline void cpuhp_online_idle(enum cpuhp_state state) { }
#endif

#endif
2 changes: 2 additions & 0 deletions include/linux/notifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
* runtime initialization.
*/

struct notifier_block;

typedef int (*notifier_fn_t)(struct notifier_block *nb,
unsigned long action, void *data);

Expand Down
Loading

0 comments on commit 710d60c

Please sign in to comment.