Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'smp-hotplug-for-linus' of git://git.kernel.org/pub/scm/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
...

+1304 -246
+1 -1
arch/alpha/kernel/smp.c
··· 168 168 cpuid, current, current->active_mm)); 169 169 170 170 preempt_disable(); 171 - cpu_startup_entry(CPUHP_ONLINE); 171 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 172 172 } 173 173 174 174 /* Wait until hwrpb->txrdy is clear for cpu. Return -1 on timeout. */
+1 -1
arch/arc/kernel/smp.c
··· 142 142 143 143 local_irq_enable(); 144 144 preempt_disable(); 145 - cpu_startup_entry(CPUHP_ONLINE); 145 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 146 146 } 147 147 148 148 /*
+1 -1
arch/arm/kernel/smp.c
··· 409 409 /* 410 410 * OK, it's off to the idle thread for us 411 411 */ 412 - cpu_startup_entry(CPUHP_ONLINE); 412 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 413 413 } 414 414 415 415 void __init smp_cpus_done(unsigned int max_cpus)
+1 -1
arch/arm64/kernel/smp.c
··· 195 195 /* 196 196 * OK, it's off to the idle thread for us 197 197 */ 198 - cpu_startup_entry(CPUHP_ONLINE); 198 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 199 199 } 200 200 201 201 #ifdef CONFIG_HOTPLUG_CPU
+1 -1
arch/blackfin/mach-common/smp.c
··· 333 333 334 334 /* We are done with local CPU inits, unblock the boot CPU. */ 335 335 set_cpu_online(cpu, true); 336 - cpu_startup_entry(CPUHP_ONLINE); 336 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 337 337 } 338 338 339 339 void __init smp_prepare_boot_cpu(void)
+1 -1
arch/hexagon/kernel/smp.c
··· 180 180 181 181 local_irq_enable(); 182 182 183 - cpu_startup_entry(CPUHP_ONLINE); 183 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 184 184 } 185 185 186 186
+1 -1
arch/ia64/kernel/smpboot.c
··· 454 454 preempt_disable(); 455 455 smp_callin(); 456 456 457 - cpu_startup_entry(CPUHP_ONLINE); 457 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 458 458 return 0; 459 459 } 460 460
+1 -1
arch/m32r/kernel/smpboot.c
··· 432 432 */ 433 433 local_flush_tlb_all(); 434 434 435 - cpu_startup_entry(CPUHP_ONLINE); 435 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 436 436 return 0; 437 437 } 438 438
+1 -1
arch/metag/kernel/smp.c
··· 396 396 /* 397 397 * OK, it's off to the idle thread for us 398 398 */ 399 - cpu_startup_entry(CPUHP_ONLINE); 399 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 400 400 } 401 401 402 402 void __init smp_cpus_done(unsigned int max_cpus)
+1 -1
arch/mips/kernel/smp.c
··· 328 328 WARN_ON_ONCE(!irqs_disabled()); 329 329 mp_ops->smp_finish(); 330 330 331 - cpu_startup_entry(CPUHP_ONLINE); 331 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 332 332 } 333 333 334 334 static void stop_this_cpu(void *dummy)
+1 -1
arch/mn10300/kernel/smp.c
··· 675 675 #ifdef CONFIG_GENERIC_CLOCKEVENTS 676 676 init_clockevents(); 677 677 #endif 678 - cpu_startup_entry(CPUHP_ONLINE); 678 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 679 679 return 0; 680 680 } 681 681
+1 -1
arch/parisc/kernel/smp.c
··· 305 305 306 306 local_irq_enable(); /* Interrupts have been off until now */ 307 307 308 - cpu_startup_entry(CPUHP_ONLINE); 308 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 309 309 310 310 /* NOTREACHED */ 311 311 panic("smp_callin() AAAAaaaaahhhh....\n");
+1 -1
arch/powerpc/kernel/smp.c
··· 727 727 728 728 local_irq_enable(); 729 729 730 - cpu_startup_entry(CPUHP_ONLINE); 730 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 731 731 732 732 BUG(); 733 733 }
+1 -1
arch/s390/kernel/smp.c
··· 798 798 set_cpu_online(smp_processor_id(), true); 799 799 inc_irq_stat(CPU_RST); 800 800 local_irq_enable(); 801 - cpu_startup_entry(CPUHP_ONLINE); 801 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 802 802 } 803 803 804 804 /* Upping and downing of CPUs */
+1 -1
arch/sh/kernel/smp.c
··· 203 203 set_cpu_online(cpu, true); 204 204 per_cpu(cpu_state, cpu) = CPU_ONLINE; 205 205 206 - cpu_startup_entry(CPUHP_ONLINE); 206 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 207 207 } 208 208 209 209 extern struct {
+1 -1
arch/sparc/kernel/smp_32.c
··· 364 364 local_irq_enable(); 365 365 366 366 wmb(); 367 - cpu_startup_entry(CPUHP_ONLINE); 367 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 368 368 369 369 /* We should never reach here! */ 370 370 BUG();
+1 -1
arch/sparc/kernel/smp_64.c
··· 134 134 135 135 local_irq_enable(); 136 136 137 - cpu_startup_entry(CPUHP_ONLINE); 137 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 138 138 } 139 139 140 140 void cpu_panic(void)
+1 -1
arch/tile/kernel/smpboot.c
··· 208 208 /* Set up tile-timer clock-event device on this cpu */ 209 209 setup_tile_timer(); 210 210 211 - cpu_startup_entry(CPUHP_ONLINE); 211 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 212 212 } 213 213 214 214 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
+1 -1
arch/x86/kernel/smpboot.c
··· 256 256 x86_cpuinit.setup_percpu_clockev(); 257 257 258 258 wmb(); 259 - cpu_startup_entry(CPUHP_ONLINE); 259 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 260 260 } 261 261 262 262 int topology_update_package_map(unsigned int apicid, unsigned int cpu)
+1 -1
arch/x86/xen/smp.c
··· 112 112 xen_pvh_secondary_vcpu_init(cpu); 113 113 #endif 114 114 cpu_bringup(); 115 - cpu_startup_entry(CPUHP_ONLINE); 115 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 116 116 } 117 117 118 118 static void xen_smp_intr_free(unsigned int cpu)
+1 -1
arch/xtensa/kernel/smp.c
··· 157 157 158 158 complete(&cpu_running); 159 159 160 - cpu_startup_entry(CPUHP_ONLINE); 160 + cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 161 161 } 162 162 163 163 static void mx_cpu_start(void *p)
+11 -16
include/linux/cpu.h
··· 16 16 #include <linux/node.h> 17 17 #include <linux/compiler.h> 18 18 #include <linux/cpumask.h> 19 + #include <linux/cpuhotplug.h> 19 20 20 21 struct device; 21 22 struct device_node; ··· 27 26 int hotpluggable; /* creates sysfs control file if hotpluggable */ 28 27 struct device dev; 29 28 }; 29 + 30 + extern void boot_cpu_init(void); 31 + extern void boot_cpu_state_init(void); 30 32 31 33 extern int register_cpu(struct cpu *cpu, int num); 32 34 extern struct device *get_cpu_device(unsigned cpu); ··· 78 74 /* migration should happen before other stuff but after perf */ 79 75 CPU_PRI_PERF = 20, 80 76 CPU_PRI_MIGRATION = 10, 81 - CPU_PRI_SMPBOOT = 9, 77 + 82 78 /* bring up workqueues before normal notifiers and down after */ 83 79 CPU_PRI_WORKQUEUE_UP = 5, 84 80 CPU_PRI_WORKQUEUE_DOWN = -5, ··· 101 97 * Called on the new cpu, just before 102 98 * enabling interrupts. Must not sleep, 103 99 * must not fail */ 104 - #define CPU_DYING_IDLE 0x000B /* CPU (unsigned)v dying, reached 105 - * idle loop. */ 106 - #define CPU_BROKEN 0x000C /* CPU (unsigned)v did not die properly, 100 + #define CPU_BROKEN 0x000B /* CPU (unsigned)v did not die properly, 107 101 * perhaps due to preemption. */ 108 102 109 103 /* Used for CPU hotplug events occurring while tasks are frozen due to a suspend ··· 120 118 121 119 122 120 #ifdef CONFIG_SMP 121 + extern bool cpuhp_tasks_frozen; 123 122 /* Need to know about CPUs going up/down? */ 124 123 #if defined(CONFIG_HOTPLUG_CPU) || !defined(MODULE) 125 124 #define cpu_notifier(fn, pri) { \ ··· 170 167 } 171 168 #endif 172 169 173 - void smpboot_thread_init(void); 174 170 int cpu_up(unsigned int cpu); 175 171 void notify_cpu_starting(unsigned int cpu); 176 172 extern void cpu_maps_update_begin(void); ··· 179 177 #define cpu_notifier_register_done cpu_maps_update_done 180 178 181 179 #else /* CONFIG_SMP */ 180 + #define cpuhp_tasks_frozen 0 182 181 183 182 #define cpu_notifier(fn, pri) do { (void)(fn); } while (0) 184 183 #define __cpu_notifier(fn, pri) do { (void)(fn); } while (0) ··· 215 212 } 216 213 217 214 static inline void cpu_notifier_register_done(void) 218 - { 219 - } 220 - 221 - static inline void smpboot_thread_init(void) 222 215 { 223 216 } 224 217 ··· 264 265 static inline void enable_nonboot_cpus(void) {} 265 266 #endif /* !CONFIG_PM_SLEEP_SMP */ 266 267 267 - enum cpuhp_state { 268 - CPUHP_OFFLINE, 269 - CPUHP_ONLINE, 270 - }; 271 - 272 268 void cpu_startup_entry(enum cpuhp_state state); 273 269 274 270 void cpu_idle_poll_ctrl(bool enable); ··· 274 280 void arch_cpu_idle_exit(void); 275 281 void arch_cpu_idle_dead(void); 276 282 277 - DECLARE_PER_CPU(bool, cpu_dead_idle); 278 - 279 283 int cpu_report_state(int cpu); 280 284 int cpu_check_up_prepare(int cpu); 281 285 void cpu_set_state_online(int cpu); 282 286 #ifdef CONFIG_HOTPLUG_CPU 283 287 bool cpu_wait_death(unsigned int cpu, int seconds); 284 288 bool cpu_report_death(void); 289 + void cpuhp_report_idle_dead(void); 290 + #else 291 + static inline void cpuhp_report_idle_dead(void) { } 285 292 #endif /* #ifdef CONFIG_HOTPLUG_CPU */ 286 293 287 294 #endif /* _LINUX_CPU_H_ */
+93
include/linux/cpuhotplug.h
··· 1 + #ifndef __CPUHOTPLUG_H 2 + #define __CPUHOTPLUG_H 3 + 4 + enum cpuhp_state { 5 + CPUHP_OFFLINE, 6 + CPUHP_CREATE_THREADS, 7 + CPUHP_NOTIFY_PREPARE, 8 + CPUHP_BRINGUP_CPU, 9 + CPUHP_AP_IDLE_DEAD, 10 + CPUHP_AP_OFFLINE, 11 + CPUHP_AP_NOTIFY_STARTING, 12 + CPUHP_AP_ONLINE, 13 + CPUHP_TEARDOWN_CPU, 14 + CPUHP_AP_ONLINE_IDLE, 15 + CPUHP_AP_SMPBOOT_THREADS, 16 + CPUHP_AP_NOTIFY_ONLINE, 17 + CPUHP_AP_ONLINE_DYN, 18 + CPUHP_AP_ONLINE_DYN_END = CPUHP_AP_ONLINE_DYN + 30, 19 + CPUHP_ONLINE, 20 + }; 21 + 22 + int __cpuhp_setup_state(enum cpuhp_state state, const char *name, bool invoke, 23 + int (*startup)(unsigned int cpu), 24 + int (*teardown)(unsigned int cpu)); 25 + 26 + /** 27 + * cpuhp_setup_state - Setup hotplug state callbacks with calling the callbacks 28 + * @state: The state for which the calls are installed 29 + * @name: Name of the callback (will be used in debug output) 30 + * @startup: startup callback function 31 + * @teardown: teardown callback function 32 + * 33 + * Installs the callback functions and invokes the startup callback on 34 + * the present cpus which have already reached the @state. 35 + */ 36 + static inline int cpuhp_setup_state(enum cpuhp_state state, 37 + const char *name, 38 + int (*startup)(unsigned int cpu), 39 + int (*teardown)(unsigned int cpu)) 40 + { 41 + return __cpuhp_setup_state(state, name, true, startup, teardown); 42 + } 43 + 44 + /** 45 + * cpuhp_setup_state_nocalls - Setup hotplug state callbacks without calling the 46 + * callbacks 47 + * @state: The state for which the calls are installed 48 + * @name: Name of the callback. 49 + * @startup: startup callback function 50 + * @teardown: teardown callback function 51 + * 52 + * Same as @cpuhp_setup_state except that no calls are executed are invoked 53 + * during installation of this callback. NOP if SMP=n or HOTPLUG_CPU=n. 54 + */ 55 + static inline int cpuhp_setup_state_nocalls(enum cpuhp_state state, 56 + const char *name, 57 + int (*startup)(unsigned int cpu), 58 + int (*teardown)(unsigned int cpu)) 59 + { 60 + return __cpuhp_setup_state(state, name, false, startup, teardown); 61 + } 62 + 63 + void __cpuhp_remove_state(enum cpuhp_state state, bool invoke); 64 + 65 + /** 66 + * cpuhp_remove_state - Remove hotplug state callbacks and invoke the teardown 67 + * @state: The state for which the calls are removed 68 + * 69 + * Removes the callback functions and invokes the teardown callback on 70 + * the present cpus which have already reached the @state. 71 + */ 72 + static inline void cpuhp_remove_state(enum cpuhp_state state) 73 + { 74 + __cpuhp_remove_state(state, true); 75 + } 76 + 77 + /** 78 + * cpuhp_remove_state_nocalls - Remove hotplug state callbacks without invoking 79 + * teardown 80 + * @state: The state for which the calls are removed 81 + */ 82 + static inline void cpuhp_remove_state_nocalls(enum cpuhp_state state) 83 + { 84 + __cpuhp_remove_state(state, false); 85 + } 86 + 87 + #ifdef CONFIG_SMP 88 + void cpuhp_online_idle(enum cpuhp_state state); 89 + #else 90 + static inline void cpuhp_online_idle(enum cpuhp_state state) { } 91 + #endif 92 + 93 + #endif
+2
include/linux/notifier.h
··· 47 47 * runtime initialization. 48 48 */ 49 49 50 + struct notifier_block; 51 + 50 52 typedef int (*notifier_fn_t)(struct notifier_block *nb, 51 53 unsigned long action, void *data); 52 54
+1 -3
include/linux/rcupdate.h
··· 332 332 void rcu_sched_qs(void); 333 333 void rcu_bh_qs(void); 334 334 void rcu_check_callbacks(int user); 335 - struct notifier_block; 336 - int rcu_cpu_notify(struct notifier_block *self, 337 - unsigned long action, void *hcpu); 335 + void rcu_report_dead(unsigned int cpu); 338 336 339 337 #ifndef CONFIG_TINY_RCU 340 338 void rcu_end_inkernel_boot(void);
+66
include/trace/events/cpuhp.h
··· 1 + #undef TRACE_SYSTEM 2 + #define TRACE_SYSTEM cpuhp 3 + 4 + #if !defined(_TRACE_CPUHP_H) || defined(TRACE_HEADER_MULTI_READ) 5 + #define _TRACE_CPUHP_H 6 + 7 + #include <linux/tracepoint.h> 8 + 9 + TRACE_EVENT(cpuhp_enter, 10 + 11 + TP_PROTO(unsigned int cpu, 12 + int target, 13 + int idx, 14 + int (*fun)(unsigned int)), 15 + 16 + TP_ARGS(cpu, target, idx, fun), 17 + 18 + TP_STRUCT__entry( 19 + __field( unsigned int, cpu ) 20 + __field( int, target ) 21 + __field( int, idx ) 22 + __field( void *, fun ) 23 + ), 24 + 25 + TP_fast_assign( 26 + __entry->cpu = cpu; 27 + __entry->target = target; 28 + __entry->idx = idx; 29 + __entry->fun = fun; 30 + ), 31 + 32 + TP_printk("cpu: %04u target: %3d step: %3d (%pf)", 33 + __entry->cpu, __entry->target, __entry->idx, __entry->fun) 34 + ); 35 + 36 + TRACE_EVENT(cpuhp_exit, 37 + 38 + TP_PROTO(unsigned int cpu, 39 + int state, 40 + int idx, 41 + int ret), 42 + 43 + TP_ARGS(cpu, state, idx, ret), 44 + 45 + TP_STRUCT__entry( 46 + __field( unsigned int, cpu ) 47 + __field( int, state ) 48 + __field( int, idx ) 49 + __field( int, ret ) 50 + ), 51 + 52 + TP_fast_assign( 53 + __entry->cpu = cpu; 54 + __entry->state = state; 55 + __entry->idx = idx; 56 + __entry->ret = ret; 57 + ), 58 + 59 + TP_printk(" cpu: %04u state: %3d step: %3d ret: %d", 60 + __entry->cpu, __entry->state, __entry->idx, __entry->ret) 61 + ); 62 + 63 + #endif 64 + 65 + /* This part must be outside protection */ 66 + #include <trace/define_trace.h>
+1 -15
init/main.c
··· 385 385 int pid; 386 386 387 387 rcu_scheduler_starting(); 388 - smpboot_thread_init(); 389 388 /* 390 389 * We need to spawn init first so that it obtains pid 1, however 391 390 * the init task will end up wanting to create kthreads, which, if ··· 448 449 done = 1; 449 450 } 450 451 451 - /* 452 - * Activate the first processor. 453 - */ 454 - 455 - static void __init boot_cpu_init(void) 456 - { 457 - int cpu = smp_processor_id(); 458 - /* Mark the boot cpu "present", "online" etc for SMP and UP case */ 459 - set_cpu_online(cpu, true); 460 - set_cpu_active(cpu, true); 461 - set_cpu_present(cpu, true); 462 - set_cpu_possible(cpu, true); 463 - } 464 - 465 452 void __init __weak smp_setup_processor_id(void) 466 453 { 467 454 } ··· 507 522 setup_command_line(command_line); 508 523 setup_nr_cpu_ids(); 509 524 setup_per_cpu_areas(); 525 + boot_cpu_state_init(); 510 526 smp_prepare_boot_cpu(); /* arch-specific boot-cpu hooks */ 511 527 512 528 build_all_zonelists(NULL, NULL);
+1044 -138
kernel/cpu.c
··· 22 22 #include <linux/lockdep.h> 23 23 #include <linux/tick.h> 24 24 #include <linux/irq.h> 25 + #include <linux/smpboot.h> 26 + 25 27 #include <trace/events/power.h> 28 + #define CREATE_TRACE_POINTS 29 + #include <trace/events/cpuhp.h> 26 30 27 31 #include "smpboot.h" 32 + 33 + /** 34 + * cpuhp_cpu_state - Per cpu hotplug state storage 35 + * @state: The current cpu state 36 + * @target: The target state 37 + * @thread: Pointer to the hotplug thread 38 + * @should_run: Thread should execute 39 + * @cb_stat: The state for a single callback (install/uninstall) 40 + * @cb: Single callback function (install/uninstall) 41 + * @result: Result of the operation 42 + * @done: Signal completion to the issuer of the task 43 + */ 44 + struct cpuhp_cpu_state { 45 + enum cpuhp_state state; 46 + enum cpuhp_state target; 47 + #ifdef CONFIG_SMP 48 + struct task_struct *thread; 49 + bool should_run; 50 + enum cpuhp_state cb_state; 51 + int (*cb)(unsigned int cpu); 52 + int result; 53 + struct completion done; 54 + #endif 55 + }; 56 + 57 + static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state); 58 + 59 + /** 60 + * cpuhp_step - Hotplug state machine step 61 + * @name: Name of the step 62 + * @startup: Startup function of the step 63 + * @teardown: Teardown function of the step 64 + * @skip_onerr: Do not invoke the functions on error rollback 65 + * Will go away once the notifiers are gone 66 + * @cant_stop: Bringup/teardown can't be stopped at this step 67 + */ 68 + struct cpuhp_step { 69 + const char *name; 70 + int (*startup)(unsigned int cpu); 71 + int (*teardown)(unsigned int cpu); 72 + bool skip_onerr; 73 + bool cant_stop; 74 + }; 75 + 76 + static DEFINE_MUTEX(cpuhp_state_mutex); 77 + static struct cpuhp_step cpuhp_bp_states[]; 78 + static struct cpuhp_step cpuhp_ap_states[]; 79 + 80 + /** 81 + * cpuhp_invoke_callback _ Invoke the callbacks for a given state 82 + * @cpu: The cpu for which the callback should be invoked 83 + * @step: The step in the state machine 84 + * @cb: The callback function to invoke 85 + * 86 + * Called from cpu hotplug and from the state register machinery 87 + */ 88 + static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state step, 89 + int (*cb)(unsigned int)) 90 + { 91 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 92 + int ret = 0; 93 + 94 + if (cb) { 95 + trace_cpuhp_enter(cpu, st->target, step, cb); 96 + ret = cb(cpu); 97 + trace_cpuhp_exit(cpu, st->state, step, ret); 98 + } 99 + return ret; 100 + } 28 101 29 102 #ifdef CONFIG_SMP 30 103 /* Serializes the updates to cpu_online_mask, cpu_present_mask */ 31 104 static DEFINE_MUTEX(cpu_add_remove_lock); 105 + bool cpuhp_tasks_frozen; 106 + EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen); 32 107 33 108 /* 34 109 * The following two APIs (cpu_maps_update_begin/done) must be used when ··· 282 207 return raw_notifier_chain_register(&cpu_chain, nb); 283 208 } 284 209 285 - static int __cpu_notify(unsigned long val, void *v, int nr_to_call, 210 + static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call, 286 211 int *nr_calls) 287 212 { 213 + unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0; 214 + void *hcpu = (void *)(long)cpu; 215 + 288 216 int ret; 289 217 290 - ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call, 218 + ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call, 291 219 nr_calls); 292 220 293 221 return notifier_to_errno(ret); 294 222 } 295 223 296 - static int cpu_notify(unsigned long val, void *v) 224 + static int cpu_notify(unsigned long val, unsigned int cpu) 297 225 { 298 - return __cpu_notify(val, v, -1, NULL); 226 + return __cpu_notify(val, cpu, -1, NULL); 227 + } 228 + 229 + /* Notifier wrappers for transitioning to state machine */ 230 + static int notify_prepare(unsigned int cpu) 231 + { 232 + int nr_calls = 0; 233 + int ret; 234 + 235 + ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls); 236 + if (ret) { 237 + nr_calls--; 238 + printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n", 239 + __func__, cpu); 240 + __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL); 241 + } 242 + return ret; 243 + } 244 + 245 + static int notify_online(unsigned int cpu) 246 + { 247 + cpu_notify(CPU_ONLINE, cpu); 248 + return 0; 249 + } 250 + 251 + static int notify_starting(unsigned int cpu) 252 + { 253 + cpu_notify(CPU_STARTING, cpu); 254 + return 0; 255 + } 256 + 257 + static int bringup_wait_for_ap(unsigned int cpu) 258 + { 259 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 260 + 261 + wait_for_completion(&st->done); 262 + return st->result; 263 + } 264 + 265 + static int bringup_cpu(unsigned int cpu) 266 + { 267 + struct task_struct *idle = idle_thread_get(cpu); 268 + int ret; 269 + 270 + /* Arch-specific enabling code. */ 271 + ret = __cpu_up(cpu, idle); 272 + if (ret) { 273 + cpu_notify(CPU_UP_CANCELED, cpu); 274 + return ret; 275 + } 276 + ret = bringup_wait_for_ap(cpu); 277 + BUG_ON(!cpu_online(cpu)); 278 + return ret; 279 + } 280 + 281 + /* 282 + * Hotplug state machine related functions 283 + */ 284 + static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st, 285 + struct cpuhp_step *steps) 286 + { 287 + for (st->state++; st->state < st->target; st->state++) { 288 + struct cpuhp_step *step = steps + st->state; 289 + 290 + if (!step->skip_onerr) 291 + cpuhp_invoke_callback(cpu, st->state, step->startup); 292 + } 293 + } 294 + 295 + static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st, 296 + struct cpuhp_step *steps, enum cpuhp_state target) 297 + { 298 + enum cpuhp_state prev_state = st->state; 299 + int ret = 0; 300 + 301 + for (; st->state > target; st->state--) { 302 + struct cpuhp_step *step = steps + st->state; 303 + 304 + ret = cpuhp_invoke_callback(cpu, st->state, step->teardown); 305 + if (ret) { 306 + st->target = prev_state; 307 + undo_cpu_down(cpu, st, steps); 308 + break; 309 + } 310 + } 311 + return ret; 312 + } 313 + 314 + static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st, 315 + struct cpuhp_step *steps) 316 + { 317 + for (st->state--; st->state > st->target; st->state--) { 318 + struct cpuhp_step *step = steps + st->state; 319 + 320 + if (!step->skip_onerr) 321 + cpuhp_invoke_callback(cpu, st->state, step->teardown); 322 + } 323 + } 324 + 325 + static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st, 326 + struct cpuhp_step *steps, enum cpuhp_state target) 327 + { 328 + enum cpuhp_state prev_state = st->state; 329 + int ret = 0; 330 + 331 + while (st->state < target) { 332 + struct cpuhp_step *step; 333 + 334 + st->state++; 335 + step = steps + st->state; 336 + ret = cpuhp_invoke_callback(cpu, st->state, step->startup); 337 + if (ret) { 338 + st->target = prev_state; 339 + undo_cpu_up(cpu, st, steps); 340 + break; 341 + } 342 + } 343 + return ret; 344 + } 345 + 346 + /* 347 + * The cpu hotplug threads manage the bringup and teardown of the cpus 348 + */ 349 + static void cpuhp_create(unsigned int cpu) 350 + { 351 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 352 + 353 + init_completion(&st->done); 354 + } 355 + 356 + static int cpuhp_should_run(unsigned int cpu) 357 + { 358 + struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 359 + 360 + return st->should_run; 361 + } 362 + 363 + /* Execute the teardown callbacks. Used to be CPU_DOWN_PREPARE */ 364 + static int cpuhp_ap_offline(unsigned int cpu, struct cpuhp_cpu_state *st) 365 + { 366 + enum cpuhp_state target = max((int)st->target, CPUHP_TEARDOWN_CPU); 367 + 368 + return cpuhp_down_callbacks(cpu, st, cpuhp_ap_states, target); 369 + } 370 + 371 + /* Execute the online startup callbacks. Used to be CPU_ONLINE */ 372 + static int cpuhp_ap_online(unsigned int cpu, struct cpuhp_cpu_state *st) 373 + { 374 + return cpuhp_up_callbacks(cpu, st, cpuhp_ap_states, st->target); 375 + } 376 + 377 + /* 378 + * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke 379 + * callbacks when a state gets [un]installed at runtime. 380 + */ 381 + static void cpuhp_thread_fun(unsigned int cpu) 382 + { 383 + struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 384 + int ret = 0; 385 + 386 + /* 387 + * Paired with the mb() in cpuhp_kick_ap_work and 388 + * cpuhp_invoke_ap_callback, so the work set is consistent visible. 389 + */ 390 + smp_mb(); 391 + if (!st->should_run) 392 + return; 393 + 394 + st->should_run = false; 395 + 396 + /* Single callback invocation for [un]install ? */ 397 + if (st->cb) { 398 + if (st->cb_state < CPUHP_AP_ONLINE) { 399 + local_irq_disable(); 400 + ret = cpuhp_invoke_callback(cpu, st->cb_state, st->cb); 401 + local_irq_enable(); 402 + } else { 403 + ret = cpuhp_invoke_callback(cpu, st->cb_state, st->cb); 404 + } 405 + } else { 406 + /* Cannot happen .... */ 407 + BUG_ON(st->state < CPUHP_AP_ONLINE_IDLE); 408 + 409 + /* Regular hotplug work */ 410 + if (st->state < st->target) 411 + ret = cpuhp_ap_online(cpu, st); 412 + else if (st->state > st->target) 413 + ret = cpuhp_ap_offline(cpu, st); 414 + } 415 + st->result = ret; 416 + complete(&st->done); 417 + } 418 + 419 + /* Invoke a single callback on a remote cpu */ 420 + static int cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, 421 + int (*cb)(unsigned int)) 422 + { 423 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 424 + 425 + if (!cpu_online(cpu)) 426 + return 0; 427 + 428 + st->cb_state = state; 429 + st->cb = cb; 430 + /* 431 + * Make sure the above stores are visible before should_run becomes 432 + * true. Paired with the mb() above in cpuhp_thread_fun() 433 + */ 434 + smp_mb(); 435 + st->should_run = true; 436 + wake_up_process(st->thread); 437 + wait_for_completion(&st->done); 438 + return st->result; 439 + } 440 + 441 + /* Regular hotplug invocation of the AP hotplug thread */ 442 + static void __cpuhp_kick_ap_work(struct cpuhp_cpu_state *st) 443 + { 444 + st->result = 0; 445 + st->cb = NULL; 446 + /* 447 + * Make sure the above stores are visible before should_run becomes 448 + * true. Paired with the mb() above in cpuhp_thread_fun() 449 + */ 450 + smp_mb(); 451 + st->should_run = true; 452 + wake_up_process(st->thread); 453 + } 454 + 455 + static int cpuhp_kick_ap_work(unsigned int cpu) 456 + { 457 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 458 + enum cpuhp_state state = st->state; 459 + 460 + trace_cpuhp_enter(cpu, st->target, state, cpuhp_kick_ap_work); 461 + __cpuhp_kick_ap_work(st); 462 + wait_for_completion(&st->done); 463 + trace_cpuhp_exit(cpu, st->state, state, st->result); 464 + return st->result; 465 + } 466 + 467 + static struct smp_hotplug_thread cpuhp_threads = { 468 + .store = &cpuhp_state.thread, 469 + .create = &cpuhp_create, 470 + .thread_should_run = cpuhp_should_run, 471 + .thread_fn = cpuhp_thread_fun, 472 + .thread_comm = "cpuhp/%u", 473 + .selfparking = true, 474 + }; 475 + 476 + void __init cpuhp_threads_init(void) 477 + { 478 + BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads)); 479 + kthread_unpark(this_cpu_read(cpuhp_state.thread)); 299 480 } 300 481 301 482 #ifdef CONFIG_HOTPLUG_CPU 302 - 303 - static void cpu_notify_nofail(unsigned long val, void *v) 304 - { 305 - BUG_ON(cpu_notify(val, v)); 306 - } 307 483 EXPORT_SYMBOL(register_cpu_notifier); 308 484 EXPORT_SYMBOL(__register_cpu_notifier); 309 - 310 485 void unregister_cpu_notifier(struct notifier_block *nb) 311 486 { 312 487 cpu_maps_update_begin(); ··· 636 311 read_unlock(&tasklist_lock); 637 312 } 638 313 639 - struct take_cpu_down_param { 640 - unsigned long mod; 641 - void *hcpu; 642 - }; 314 + static void cpu_notify_nofail(unsigned long val, unsigned int cpu) 315 + { 316 + BUG_ON(cpu_notify(val, cpu)); 317 + } 318 + 319 + static int notify_down_prepare(unsigned int cpu) 320 + { 321 + int err, nr_calls = 0; 322 + 323 + err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls); 324 + if (err) { 325 + nr_calls--; 326 + __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL); 327 + pr_warn("%s: attempt to take down CPU %u failed\n", 328 + __func__, cpu); 329 + } 330 + return err; 331 + } 332 + 333 + static int notify_dying(unsigned int cpu) 334 + { 335 + cpu_notify(CPU_DYING, cpu); 336 + return 0; 337 + } 643 338 644 339 /* Take this CPU down. */ 645 340 static int take_cpu_down(void *_param) 646 341 { 647 - struct take_cpu_down_param *param = _param; 648 - int err; 342 + struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 343 + enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE); 344 + int err, cpu = smp_processor_id(); 649 345 650 346 /* Ensure this CPU doesn't handle any more interrupts. */ 651 347 err = __cpu_disable(); 652 348 if (err < 0) 653 349 return err; 654 350 655 - cpu_notify(CPU_DYING | param->mod, param->hcpu); 351 + /* Invoke the former CPU_DYING callbacks */ 352 + for (; st->state > target; st->state--) { 353 + struct cpuhp_step *step = cpuhp_ap_states + st->state; 354 + 355 + cpuhp_invoke_callback(cpu, st->state, step->teardown); 356 + } 656 357 /* Give up timekeeping duties */ 657 358 tick_handover_do_timer(); 658 359 /* Park the stopper thread */ 659 - stop_machine_park((long)param->hcpu); 360 + stop_machine_park(cpu); 660 361 return 0; 661 362 } 662 363 663 - /* Requires cpu_add_remove_lock to be held */ 664 - static int _cpu_down(unsigned int cpu, int tasks_frozen) 364 + static int takedown_cpu(unsigned int cpu) 665 365 { 666 - int err, nr_calls = 0; 667 - void *hcpu = (void *)(long)cpu; 668 - unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; 669 - struct take_cpu_down_param tcd_param = { 670 - .mod = mod, 671 - .hcpu = hcpu, 672 - }; 673 - 674 - if (num_online_cpus() == 1) 675 - return -EBUSY; 676 - 677 - if (!cpu_online(cpu)) 678 - return -EINVAL; 679 - 680 - cpu_hotplug_begin(); 681 - 682 - err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls); 683 - if (err) { 684 - nr_calls--; 685 - __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL); 686 - pr_warn("%s: attempt to take down CPU %u failed\n", 687 - __func__, cpu); 688 - goto out_release; 689 - } 366 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 367 + int err; 690 368 691 369 /* 692 370 * By now we've cleared cpu_active_mask, wait for all preempt-disabled ··· 706 378 else 707 379 synchronize_rcu(); 708 380 381 + /* Park the smpboot threads */ 382 + kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread); 709 383 smpboot_park_threads(cpu); 710 384 711 385 /* ··· 719 389 /* 720 390 * So now all preempt/rcu users must observe !cpu_active(). 721 391 */ 722 - err = stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu)); 392 + err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu)); 723 393 if (err) { 724 394 /* CPU didn't die: tell everyone. Can't complain. */ 725 - cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu); 395 + cpu_notify_nofail(CPU_DOWN_FAILED, cpu); 726 396 irq_unlock_sparse(); 727 - goto out_release; 397 + return err; 728 398 } 729 399 BUG_ON(cpu_online(cpu)); 730 400 ··· 735 405 * 736 406 * Wait for the stop thread to go away. 737 407 */ 738 - while (!per_cpu(cpu_dead_idle, cpu)) 739 - cpu_relax(); 740 - smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */ 741 - per_cpu(cpu_dead_idle, cpu) = false; 408 + wait_for_completion(&st->done); 409 + BUG_ON(st->state != CPUHP_AP_IDLE_DEAD); 742 410 743 411 /* Interrupts are moved away from the dying cpu, reenable alloc/free */ 744 412 irq_unlock_sparse(); ··· 745 417 /* This actually kills the CPU. */ 746 418 __cpu_die(cpu); 747 419 748 - /* CPU is completely dead: tell everyone. Too late to complain. */ 749 420 tick_cleanup_dead_cpu(cpu); 750 - cpu_notify_nofail(CPU_DEAD | mod, hcpu); 751 - 752 - check_for_tasks(cpu); 753 - 754 - out_release: 755 - cpu_hotplug_done(); 756 - if (!err) 757 - cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu); 758 - return err; 421 + return 0; 759 422 } 760 423 761 - int cpu_down(unsigned int cpu) 424 + static int notify_dead(unsigned int cpu) 425 + { 426 + cpu_notify_nofail(CPU_DEAD, cpu); 427 + check_for_tasks(cpu); 428 + return 0; 429 + } 430 + 431 + static void cpuhp_complete_idle_dead(void *arg) 432 + { 433 + struct cpuhp_cpu_state *st = arg; 434 + 435 + complete(&st->done); 436 + } 437 + 438 + void cpuhp_report_idle_dead(void) 439 + { 440 + struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 441 + 442 + BUG_ON(st->state != CPUHP_AP_OFFLINE); 443 + rcu_report_dead(smp_processor_id()); 444 + st->state = CPUHP_AP_IDLE_DEAD; 445 + /* 446 + * We cannot call complete after rcu_report_dead() so we delegate it 447 + * to an online cpu. 448 + */ 449 + smp_call_function_single(cpumask_first(cpu_online_mask), 450 + cpuhp_complete_idle_dead, st, 0); 451 + } 452 + 453 + #else 454 + #define notify_down_prepare NULL 455 + #define takedown_cpu NULL 456 + #define notify_dead NULL 457 + #define notify_dying NULL 458 + #endif 459 + 460 + #ifdef CONFIG_HOTPLUG_CPU 461 + 462 + /* Requires cpu_add_remove_lock to be held */ 463 + static int __ref _cpu_down(unsigned int cpu, int tasks_frozen, 464 + enum cpuhp_state target) 465 + { 466 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 467 + int prev_state, ret = 0; 468 + bool hasdied = false; 469 + 470 + if (num_online_cpus() == 1) 471 + return -EBUSY; 472 + 473 + if (!cpu_present(cpu)) 474 + return -EINVAL; 475 + 476 + cpu_hotplug_begin(); 477 + 478 + cpuhp_tasks_frozen = tasks_frozen; 479 + 480 + prev_state = st->state; 481 + st->target = target; 482 + /* 483 + * If the current CPU state is in the range of the AP hotplug thread, 484 + * then we need to kick the thread. 485 + */ 486 + if (st->state > CPUHP_TEARDOWN_CPU) { 487 + ret = cpuhp_kick_ap_work(cpu); 488 + /* 489 + * The AP side has done the error rollback already. Just 490 + * return the error code.. 491 + */ 492 + if (ret) 493 + goto out; 494 + 495 + /* 496 + * We might have stopped still in the range of the AP hotplug 497 + * thread. Nothing to do anymore. 498 + */ 499 + if (st->state > CPUHP_TEARDOWN_CPU) 500 + goto out; 501 + } 502 + /* 503 + * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need 504 + * to do the further cleanups. 505 + */ 506 + ret = cpuhp_down_callbacks(cpu, st, cpuhp_bp_states, target); 507 + 508 + hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE; 509 + out: 510 + cpu_hotplug_done(); 511 + /* This post dead nonsense must die */ 512 + if (!ret && hasdied) 513 + cpu_notify_nofail(CPU_POST_DEAD, cpu); 514 + return ret; 515 + } 516 + 517 + static int do_cpu_down(unsigned int cpu, enum cpuhp_state target) 762 518 { 763 519 int err; 764 520 ··· 853 441 goto out; 854 442 } 855 443 856 - err = _cpu_down(cpu, 0); 444 + err = _cpu_down(cpu, 0, target); 857 445 858 446 out: 859 447 cpu_maps_update_done(); 860 448 return err; 861 449 } 450 + int cpu_down(unsigned int cpu) 451 + { 452 + return do_cpu_down(cpu, CPUHP_OFFLINE); 453 + } 862 454 EXPORT_SYMBOL(cpu_down); 863 455 #endif /*CONFIG_HOTPLUG_CPU*/ 864 456 865 - /* 866 - * Unpark per-CPU smpboot kthreads at CPU-online time. 457 + /** 458 + * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers 459 + * @cpu: cpu that just started 460 + * 461 + * This function calls the cpu_chain notifiers with CPU_STARTING. 462 + * It must be called by the arch code on the new cpu, before the new cpu 463 + * enables interrupts and before the "boot" cpu returns from __cpu_up(). 867 464 */ 868 - static int smpboot_thread_call(struct notifier_block *nfb, 869 - unsigned long action, void *hcpu) 465 + void notify_cpu_starting(unsigned int cpu) 870 466 { 871 - int cpu = (long)hcpu; 467 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 468 + enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE); 872 469 873 - switch (action & ~CPU_TASKS_FROZEN) { 470 + while (st->state < target) { 471 + struct cpuhp_step *step; 874 472 875 - case CPU_DOWN_FAILED: 876 - case CPU_ONLINE: 877 - smpboot_unpark_threads(cpu); 878 - break; 879 - 880 - default: 881 - break; 473 + st->state++; 474 + step = cpuhp_ap_states + st->state; 475 + cpuhp_invoke_callback(cpu, st->state, step->startup); 882 476 } 883 - 884 - return NOTIFY_OK; 885 477 } 886 478 887 - static struct notifier_block smpboot_thread_notifier = { 888 - .notifier_call = smpboot_thread_call, 889 - .priority = CPU_PRI_SMPBOOT, 890 - }; 891 - 892 - void smpboot_thread_init(void) 479 + /* 480 + * Called from the idle task. We need to set active here, so we can kick off 481 + * the stopper thread and unpark the smpboot threads. If the target state is 482 + * beyond CPUHP_AP_ONLINE_IDLE we kick cpuhp thread and let it bring up the 483 + * cpu further. 484 + */ 485 + void cpuhp_online_idle(enum cpuhp_state state) 893 486 { 894 - register_cpu_notifier(&smpboot_thread_notifier); 487 + struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state); 488 + unsigned int cpu = smp_processor_id(); 489 + 490 + /* Happens for the boot cpu */ 491 + if (state != CPUHP_AP_ONLINE_IDLE) 492 + return; 493 + 494 + st->state = CPUHP_AP_ONLINE_IDLE; 495 + 496 + /* The cpu is marked online, set it active now */ 497 + set_cpu_active(cpu, true); 498 + /* Unpark the stopper thread and the hotplug thread of this cpu */ 499 + stop_machine_unpark(cpu); 500 + kthread_unpark(st->thread); 501 + 502 + /* Should we go further up ? */ 503 + if (st->target > CPUHP_AP_ONLINE_IDLE) 504 + __cpuhp_kick_ap_work(st); 505 + else 506 + complete(&st->done); 895 507 } 896 508 897 509 /* Requires cpu_add_remove_lock to be held */ 898 - static int _cpu_up(unsigned int cpu, int tasks_frozen) 510 + static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target) 899 511 { 900 - int ret, nr_calls = 0; 901 - void *hcpu = (void *)(long)cpu; 902 - unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; 512 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 903 513 struct task_struct *idle; 514 + int ret = 0; 904 515 905 516 cpu_hotplug_begin(); 906 517 907 - if (cpu_online(cpu) || !cpu_present(cpu)) { 518 + if (!cpu_present(cpu)) { 908 519 ret = -EINVAL; 909 520 goto out; 910 521 } 911 522 912 - idle = idle_thread_get(cpu); 913 - if (IS_ERR(idle)) { 914 - ret = PTR_ERR(idle); 915 - goto out; 916 - } 917 - 918 - ret = smpboot_create_threads(cpu); 919 - if (ret) 523 + /* 524 + * The caller of do_cpu_up might have raced with another 525 + * caller. Ignore it for now. 526 + */ 527 + if (st->state >= target) 920 528 goto out; 921 529 922 - ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls); 923 - if (ret) { 924 - nr_calls--; 925 - pr_warn("%s: attempt to bring up CPU %u failed\n", 926 - __func__, cpu); 927 - goto out_notify; 530 + if (st->state == CPUHP_OFFLINE) { 531 + /* Let it fail before we try to bring the cpu up */ 532 + idle = idle_thread_get(cpu); 533 + if (IS_ERR(idle)) { 534 + ret = PTR_ERR(idle); 535 + goto out; 536 + } 928 537 } 929 538 930 - /* Arch-specific enabling code. */ 931 - ret = __cpu_up(cpu, idle); 539 + cpuhp_tasks_frozen = tasks_frozen; 932 540 933 - if (ret != 0) 934 - goto out_notify; 935 - BUG_ON(!cpu_online(cpu)); 541 + st->target = target; 542 + /* 543 + * If the current CPU state is in the range of the AP hotplug thread, 544 + * then we need to kick the thread once more. 545 + */ 546 + if (st->state > CPUHP_BRINGUP_CPU) { 547 + ret = cpuhp_kick_ap_work(cpu); 548 + /* 549 + * The AP side has done the error rollback already. Just 550 + * return the error code.. 551 + */ 552 + if (ret) 553 + goto out; 554 + } 936 555 937 - /* Now call notifier in preparation. */ 938 - cpu_notify(CPU_ONLINE | mod, hcpu); 939 - 940 - out_notify: 941 - if (ret != 0) 942 - __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); 556 + /* 557 + * Try to reach the target state. We max out on the BP at 558 + * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is 559 + * responsible for bringing it up to the target state. 560 + */ 561 + target = min((int)target, CPUHP_BRINGUP_CPU); 562 + ret = cpuhp_up_callbacks(cpu, st, cpuhp_bp_states, target); 943 563 out: 944 564 cpu_hotplug_done(); 945 - 946 565 return ret; 947 566 } 948 567 949 - int cpu_up(unsigned int cpu) 568 + static int do_cpu_up(unsigned int cpu, enum cpuhp_state target) 950 569 { 951 570 int err = 0; 952 571 ··· 1001 558 goto out; 1002 559 } 1003 560 1004 - err = _cpu_up(cpu, 0); 1005 - 561 + err = _cpu_up(cpu, 0, target); 1006 562 out: 1007 563 cpu_maps_update_done(); 1008 564 return err; 565 + } 566 + 567 + int cpu_up(unsigned int cpu) 568 + { 569 + return do_cpu_up(cpu, CPUHP_ONLINE); 1009 570 } 1010 571 EXPORT_SYMBOL_GPL(cpu_up); 1011 572 ··· 1033 586 if (cpu == first_cpu) 1034 587 continue; 1035 588 trace_suspend_resume(TPS("CPU_OFF"), cpu, true); 1036 - error = _cpu_down(cpu, 1); 589 + error = _cpu_down(cpu, 1, CPUHP_OFFLINE); 1037 590 trace_suspend_resume(TPS("CPU_OFF"), cpu, false); 1038 591 if (!error) 1039 592 cpumask_set_cpu(cpu, frozen_cpus); ··· 1083 636 1084 637 for_each_cpu(cpu, frozen_cpus) { 1085 638 trace_suspend_resume(TPS("CPU_ON"), cpu, true); 1086 - error = _cpu_up(cpu, 1); 639 + error = _cpu_up(cpu, 1, CPUHP_ONLINE); 1087 640 trace_suspend_resume(TPS("CPU_ON"), cpu, false); 1088 641 if (!error) { 1089 642 pr_info("CPU%d is up\n", cpu); ··· 1156 709 1157 710 #endif /* CONFIG_PM_SLEEP_SMP */ 1158 711 1159 - /** 1160 - * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers 1161 - * @cpu: cpu that just started 1162 - * 1163 - * This function calls the cpu_chain notifiers with CPU_STARTING. 1164 - * It must be called by the arch code on the new cpu, before the new cpu 1165 - * enables interrupts and before the "boot" cpu returns from __cpu_up(). 1166 - */ 1167 - void notify_cpu_starting(unsigned int cpu) 1168 - { 1169 - unsigned long val = CPU_STARTING; 712 + #endif /* CONFIG_SMP */ 1170 713 1171 - #ifdef CONFIG_PM_SLEEP_SMP 1172 - if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus)) 1173 - val = CPU_STARTING_FROZEN; 1174 - #endif /* CONFIG_PM_SLEEP_SMP */ 1175 - cpu_notify(val, (void *)(long)cpu); 714 + /* Boot processor state steps */ 715 + static struct cpuhp_step cpuhp_bp_states[] = { 716 + [CPUHP_OFFLINE] = { 717 + .name = "offline", 718 + .startup = NULL, 719 + .teardown = NULL, 720 + }, 721 + #ifdef CONFIG_SMP 722 + [CPUHP_CREATE_THREADS]= { 723 + .name = "threads:create", 724 + .startup = smpboot_create_threads, 725 + .teardown = NULL, 726 + .cant_stop = true, 727 + }, 728 + /* 729 + * Preparatory and dead notifiers. Will be replaced once the notifiers 730 + * are converted to states. 731 + */ 732 + [CPUHP_NOTIFY_PREPARE] = { 733 + .name = "notify:prepare", 734 + .startup = notify_prepare, 735 + .teardown = notify_dead, 736 + .skip_onerr = true, 737 + .cant_stop = true, 738 + }, 739 + /* Kicks the plugged cpu into life */ 740 + [CPUHP_BRINGUP_CPU] = { 741 + .name = "cpu:bringup", 742 + .startup = bringup_cpu, 743 + .teardown = NULL, 744 + .cant_stop = true, 745 + }, 746 + /* 747 + * Handled on controll processor until the plugged processor manages 748 + * this itself. 749 + */ 750 + [CPUHP_TEARDOWN_CPU] = { 751 + .name = "cpu:teardown", 752 + .startup = NULL, 753 + .teardown = takedown_cpu, 754 + .cant_stop = true, 755 + }, 756 + #endif 757 + }; 758 + 759 + /* Application processor state steps */ 760 + static struct cpuhp_step cpuhp_ap_states[] = { 761 + #ifdef CONFIG_SMP 762 + /* Final state before CPU kills itself */ 763 + [CPUHP_AP_IDLE_DEAD] = { 764 + .name = "idle:dead", 765 + }, 766 + /* 767 + * Last state before CPU enters the idle loop to die. Transient state 768 + * for synchronization. 769 + */ 770 + [CPUHP_AP_OFFLINE] = { 771 + .name = "ap:offline", 772 + .cant_stop = true, 773 + }, 774 + /* 775 + * Low level startup/teardown notifiers. Run with interrupts 776 + * disabled. Will be removed once the notifiers are converted to 777 + * states. 778 + */ 779 + [CPUHP_AP_NOTIFY_STARTING] = { 780 + .name = "notify:starting", 781 + .startup = notify_starting, 782 + .teardown = notify_dying, 783 + .skip_onerr = true, 784 + .cant_stop = true, 785 + }, 786 + /* Entry state on starting. Interrupts enabled from here on. Transient 787 + * state for synchronsization */ 788 + [CPUHP_AP_ONLINE] = { 789 + .name = "ap:online", 790 + }, 791 + /* Handle smpboot threads park/unpark */ 792 + [CPUHP_AP_SMPBOOT_THREADS] = { 793 + .name = "smpboot:threads", 794 + .startup = smpboot_unpark_threads, 795 + .teardown = NULL, 796 + }, 797 + /* 798 + * Online/down_prepare notifiers. Will be removed once the notifiers 799 + * are converted to states. 800 + */ 801 + [CPUHP_AP_NOTIFY_ONLINE] = { 802 + .name = "notify:online", 803 + .startup = notify_online, 804 + .teardown = notify_down_prepare, 805 + }, 806 + #endif 807 + /* 808 + * The dynamically registered state space is here 809 + */ 810 + 811 + /* CPU is fully up and running. */ 812 + [CPUHP_ONLINE] = { 813 + .name = "online", 814 + .startup = NULL, 815 + .teardown = NULL, 816 + }, 817 + }; 818 + 819 + /* Sanity check for callbacks */ 820 + static int cpuhp_cb_check(enum cpuhp_state state) 821 + { 822 + if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE) 823 + return -EINVAL; 824 + return 0; 1176 825 } 1177 826 1178 - #endif /* CONFIG_SMP */ 827 + static bool cpuhp_is_ap_state(enum cpuhp_state state) 828 + { 829 + /* 830 + * The extra check for CPUHP_TEARDOWN_CPU is only for documentation 831 + * purposes as that state is handled explicitely in cpu_down. 832 + */ 833 + return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU; 834 + } 835 + 836 + static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state) 837 + { 838 + struct cpuhp_step *sp; 839 + 840 + sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states; 841 + return sp + state; 842 + } 843 + 844 + static void cpuhp_store_callbacks(enum cpuhp_state state, 845 + const char *name, 846 + int (*startup)(unsigned int cpu), 847 + int (*teardown)(unsigned int cpu)) 848 + { 849 + /* (Un)Install the callbacks for further cpu hotplug operations */ 850 + struct cpuhp_step *sp; 851 + 852 + mutex_lock(&cpuhp_state_mutex); 853 + sp = cpuhp_get_step(state); 854 + sp->startup = startup; 855 + sp->teardown = teardown; 856 + sp->name = name; 857 + mutex_unlock(&cpuhp_state_mutex); 858 + } 859 + 860 + static void *cpuhp_get_teardown_cb(enum cpuhp_state state) 861 + { 862 + return cpuhp_get_step(state)->teardown; 863 + } 864 + 865 + /* 866 + * Call the startup/teardown function for a step either on the AP or 867 + * on the current CPU. 868 + */ 869 + static int cpuhp_issue_call(int cpu, enum cpuhp_state state, 870 + int (*cb)(unsigned int), bool bringup) 871 + { 872 + int ret; 873 + 874 + if (!cb) 875 + return 0; 876 + /* 877 + * The non AP bound callbacks can fail on bringup. On teardown 878 + * e.g. module removal we crash for now. 879 + */ 880 + #ifdef CONFIG_SMP 881 + if (cpuhp_is_ap_state(state)) 882 + ret = cpuhp_invoke_ap_callback(cpu, state, cb); 883 + else 884 + ret = cpuhp_invoke_callback(cpu, state, cb); 885 + #else 886 + ret = cpuhp_invoke_callback(cpu, state, cb); 887 + #endif 888 + BUG_ON(ret && !bringup); 889 + return ret; 890 + } 891 + 892 + /* 893 + * Called from __cpuhp_setup_state on a recoverable failure. 894 + * 895 + * Note: The teardown callbacks for rollback are not allowed to fail! 896 + */ 897 + static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state, 898 + int (*teardown)(unsigned int cpu)) 899 + { 900 + int cpu; 901 + 902 + if (!teardown) 903 + return; 904 + 905 + /* Roll back the already executed steps on the other cpus */ 906 + for_each_present_cpu(cpu) { 907 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 908 + int cpustate = st->state; 909 + 910 + if (cpu >= failedcpu) 911 + break; 912 + 913 + /* Did we invoke the startup call on that cpu ? */ 914 + if (cpustate >= state) 915 + cpuhp_issue_call(cpu, state, teardown, false); 916 + } 917 + } 918 + 919 + /* 920 + * Returns a free for dynamic slot assignment of the Online state. The states 921 + * are protected by the cpuhp_slot_states mutex and an empty slot is identified 922 + * by having no name assigned. 923 + */ 924 + static int cpuhp_reserve_state(enum cpuhp_state state) 925 + { 926 + enum cpuhp_state i; 927 + 928 + mutex_lock(&cpuhp_state_mutex); 929 + for (i = CPUHP_AP_ONLINE_DYN; i <= CPUHP_AP_ONLINE_DYN_END; i++) { 930 + if (cpuhp_ap_states[i].name) 931 + continue; 932 + 933 + cpuhp_ap_states[i].name = "Reserved"; 934 + mutex_unlock(&cpuhp_state_mutex); 935 + return i; 936 + } 937 + mutex_unlock(&cpuhp_state_mutex); 938 + WARN(1, "No more dynamic states available for CPU hotplug\n"); 939 + return -ENOSPC; 940 + } 941 + 942 + /** 943 + * __cpuhp_setup_state - Setup the callbacks for an hotplug machine state 944 + * @state: The state to setup 945 + * @invoke: If true, the startup function is invoked for cpus where 946 + * cpu state >= @state 947 + * @startup: startup callback function 948 + * @teardown: teardown callback function 949 + * 950 + * Returns 0 if successful, otherwise a proper error code 951 + */ 952 + int __cpuhp_setup_state(enum cpuhp_state state, 953 + const char *name, bool invoke, 954 + int (*startup)(unsigned int cpu), 955 + int (*teardown)(unsigned int cpu)) 956 + { 957 + int cpu, ret = 0; 958 + int dyn_state = 0; 959 + 960 + if (cpuhp_cb_check(state) || !name) 961 + return -EINVAL; 962 + 963 + get_online_cpus(); 964 + 965 + /* currently assignments for the ONLINE state are possible */ 966 + if (state == CPUHP_AP_ONLINE_DYN) { 967 + dyn_state = 1; 968 + ret = cpuhp_reserve_state(state); 969 + if (ret < 0) 970 + goto out; 971 + state = ret; 972 + } 973 + 974 + cpuhp_store_callbacks(state, name, startup, teardown); 975 + 976 + if (!invoke || !startup) 977 + goto out; 978 + 979 + /* 980 + * Try to call the startup callback for each present cpu 981 + * depending on the hotplug state of the cpu. 982 + */ 983 + for_each_present_cpu(cpu) { 984 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 985 + int cpustate = st->state; 986 + 987 + if (cpustate < state) 988 + continue; 989 + 990 + ret = cpuhp_issue_call(cpu, state, startup, true); 991 + if (ret) { 992 + cpuhp_rollback_install(cpu, state, teardown); 993 + cpuhp_store_callbacks(state, NULL, NULL, NULL); 994 + goto out; 995 + } 996 + } 997 + out: 998 + put_online_cpus(); 999 + if (!ret && dyn_state) 1000 + return state; 1001 + return ret; 1002 + } 1003 + EXPORT_SYMBOL(__cpuhp_setup_state); 1004 + 1005 + /** 1006 + * __cpuhp_remove_state - Remove the callbacks for an hotplug machine state 1007 + * @state: The state to remove 1008 + * @invoke: If true, the teardown function is invoked for cpus where 1009 + * cpu state >= @state 1010 + * 1011 + * The teardown callback is currently not allowed to fail. Think 1012 + * about module removal! 1013 + */ 1014 + void __cpuhp_remove_state(enum cpuhp_state state, bool invoke) 1015 + { 1016 + int (*teardown)(unsigned int cpu) = cpuhp_get_teardown_cb(state); 1017 + int cpu; 1018 + 1019 + BUG_ON(cpuhp_cb_check(state)); 1020 + 1021 + get_online_cpus(); 1022 + 1023 + if (!invoke || !teardown) 1024 + goto remove; 1025 + 1026 + /* 1027 + * Call the teardown callback for each present cpu depending 1028 + * on the hotplug state of the cpu. This function is not 1029 + * allowed to fail currently! 1030 + */ 1031 + for_each_present_cpu(cpu) { 1032 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu); 1033 + int cpustate = st->state; 1034 + 1035 + if (cpustate >= state) 1036 + cpuhp_issue_call(cpu, state, teardown, false); 1037 + } 1038 + remove: 1039 + cpuhp_store_callbacks(state, NULL, NULL, NULL); 1040 + put_online_cpus(); 1041 + } 1042 + EXPORT_SYMBOL(__cpuhp_remove_state); 1043 + 1044 + #if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU) 1045 + static ssize_t show_cpuhp_state(struct device *dev, 1046 + struct device_attribute *attr, char *buf) 1047 + { 1048 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 1049 + 1050 + return sprintf(buf, "%d\n", st->state); 1051 + } 1052 + static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL); 1053 + 1054 + static ssize_t write_cpuhp_target(struct device *dev, 1055 + struct device_attribute *attr, 1056 + const char *buf, size_t count) 1057 + { 1058 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 1059 + struct cpuhp_step *sp; 1060 + int target, ret; 1061 + 1062 + ret = kstrtoint(buf, 10, &target); 1063 + if (ret) 1064 + return ret; 1065 + 1066 + #ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL 1067 + if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE) 1068 + return -EINVAL; 1069 + #else 1070 + if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE) 1071 + return -EINVAL; 1072 + #endif 1073 + 1074 + ret = lock_device_hotplug_sysfs(); 1075 + if (ret) 1076 + return ret; 1077 + 1078 + mutex_lock(&cpuhp_state_mutex); 1079 + sp = cpuhp_get_step(target); 1080 + ret = !sp->name || sp->cant_stop ? -EINVAL : 0; 1081 + mutex_unlock(&cpuhp_state_mutex); 1082 + if (ret) 1083 + return ret; 1084 + 1085 + if (st->state < target) 1086 + ret = do_cpu_up(dev->id, target); 1087 + else 1088 + ret = do_cpu_down(dev->id, target); 1089 + 1090 + unlock_device_hotplug(); 1091 + return ret ? ret : count; 1092 + } 1093 + 1094 + static ssize_t show_cpuhp_target(struct device *dev, 1095 + struct device_attribute *attr, char *buf) 1096 + { 1097 + struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id); 1098 + 1099 + return sprintf(buf, "%d\n", st->target); 1100 + } 1101 + static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target); 1102 + 1103 + static struct attribute *cpuhp_cpu_attrs[] = { 1104 + &dev_attr_state.attr, 1105 + &dev_attr_target.attr, 1106 + NULL 1107 + }; 1108 + 1109 + static struct attribute_group cpuhp_cpu_attr_group = { 1110 + .attrs = cpuhp_cpu_attrs, 1111 + .name = "hotplug", 1112 + NULL 1113 + }; 1114 + 1115 + static ssize_t show_cpuhp_states(struct device *dev, 1116 + struct device_attribute *attr, char *buf) 1117 + { 1118 + ssize_t cur, res = 0; 1119 + int i; 1120 + 1121 + mutex_lock(&cpuhp_state_mutex); 1122 + for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) { 1123 + struct cpuhp_step *sp = cpuhp_get_step(i); 1124 + 1125 + if (sp->name) { 1126 + cur = sprintf(buf, "%3d: %s\n", i, sp->name); 1127 + buf += cur; 1128 + res += cur; 1129 + } 1130 + } 1131 + mutex_unlock(&cpuhp_state_mutex); 1132 + return res; 1133 + } 1134 + static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL); 1135 + 1136 + static struct attribute *cpuhp_cpu_root_attrs[] = { 1137 + &dev_attr_states.attr, 1138 + NULL 1139 + }; 1140 + 1141 + static struct attribute_group cpuhp_cpu_root_attr_group = { 1142 + .attrs = cpuhp_cpu_root_attrs, 1143 + .name = "hotplug", 1144 + NULL 1145 + }; 1146 + 1147 + static int __init cpuhp_sysfs_init(void) 1148 + { 1149 + int cpu, ret; 1150 + 1151 + ret = sysfs_create_group(&cpu_subsys.dev_root->kobj, 1152 + &cpuhp_cpu_root_attr_group); 1153 + if (ret) 1154 + return ret; 1155 + 1156 + for_each_possible_cpu(cpu) { 1157 + struct device *dev = get_cpu_device(cpu); 1158 + 1159 + if (!dev) 1160 + continue; 1161 + ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group); 1162 + if (ret) 1163 + return ret; 1164 + } 1165 + return 0; 1166 + } 1167 + device_initcall(cpuhp_sysfs_init); 1168 + #endif 1179 1169 1180 1170 /* 1181 1171 * cpu_bit_bitmap[] is a special, "compressed" data structure that ··· 1672 788 void init_cpu_online(const struct cpumask *src) 1673 789 { 1674 790 cpumask_copy(&__cpu_online_mask, src); 791 + } 792 + 793 + /* 794 + * Activate the first processor. 795 + */ 796 + void __init boot_cpu_init(void) 797 + { 798 + int cpu = smp_processor_id(); 799 + 800 + /* Mark the boot cpu "present", "online" etc for SMP and UP case */ 801 + set_cpu_online(cpu, true); 802 + set_cpu_active(cpu, true); 803 + set_cpu_present(cpu, true); 804 + set_cpu_possible(cpu, true); 805 + } 806 + 807 + /* 808 + * Must be called _AFTER_ setting up the per_cpu areas 809 + */ 810 + void __init boot_cpu_state_init(void) 811 + { 812 + per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE; 1675 813 }
+40 -33
kernel/rcu/tree.c
··· 2606 2606 } 2607 2607 2608 2608 /* 2609 - * The CPU is exiting the idle loop into the arch_cpu_idle_dead() 2610 - * function. We now remove it from the rcu_node tree's ->qsmaskinit 2611 - * bit masks. 2612 - */ 2613 - static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp) 2614 - { 2615 - unsigned long flags; 2616 - unsigned long mask; 2617 - struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu); 2618 - struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */ 2619 - 2620 - if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) 2621 - return; 2622 - 2623 - /* Remove outgoing CPU from mask in the leaf rcu_node structure. */ 2624 - mask = rdp->grpmask; 2625 - raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */ 2626 - rnp->qsmaskinitnext &= ~mask; 2627 - raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 2628 - } 2629 - 2630 - /* 2631 2609 * The CPU has been completely removed, and some other CPU is reporting 2632 2610 * this fact from process context. Do the remainder of the cleanup, 2633 2611 * including orphaning the outgoing CPU's RCU callbacks, and also ··· 4224 4246 rcu_init_percpu_data(cpu, rsp); 4225 4247 } 4226 4248 4249 + #ifdef CONFIG_HOTPLUG_CPU 4250 + /* 4251 + * The CPU is exiting the idle loop into the arch_cpu_idle_dead() 4252 + * function. We now remove it from the rcu_node tree's ->qsmaskinit 4253 + * bit masks. 4254 + * The CPU is exiting the idle loop into the arch_cpu_idle_dead() 4255 + * function. We now remove it from the rcu_node tree's ->qsmaskinit 4256 + * bit masks. 4257 + */ 4258 + static void rcu_cleanup_dying_idle_cpu(int cpu, struct rcu_state *rsp) 4259 + { 4260 + unsigned long flags; 4261 + unsigned long mask; 4262 + struct rcu_data *rdp = per_cpu_ptr(rsp->rda, cpu); 4263 + struct rcu_node *rnp = rdp->mynode; /* Outgoing CPU's rdp & rnp. */ 4264 + 4265 + if (!IS_ENABLED(CONFIG_HOTPLUG_CPU)) 4266 + return; 4267 + 4268 + /* Remove outgoing CPU from mask in the leaf rcu_node structure. */ 4269 + mask = rdp->grpmask; 4270 + raw_spin_lock_irqsave_rcu_node(rnp, flags); /* Enforce GP memory-order guarantee. */ 4271 + rnp->qsmaskinitnext &= ~mask; 4272 + raw_spin_unlock_irqrestore_rcu_node(rnp, flags); 4273 + } 4274 + 4275 + void rcu_report_dead(unsigned int cpu) 4276 + { 4277 + struct rcu_state *rsp; 4278 + 4279 + /* QS for any half-done expedited RCU-sched GP. */ 4280 + preempt_disable(); 4281 + rcu_report_exp_rdp(&rcu_sched_state, 4282 + this_cpu_ptr(rcu_sched_state.rda), true); 4283 + preempt_enable(); 4284 + for_each_rcu_flavor(rsp) 4285 + rcu_cleanup_dying_idle_cpu(cpu, rsp); 4286 + } 4287 + #endif 4288 + 4227 4289 /* 4228 4290 * Handle CPU online/offline notification events. 4229 4291 */ ··· 4294 4276 case CPU_DYING_FROZEN: 4295 4277 for_each_rcu_flavor(rsp) 4296 4278 rcu_cleanup_dying_cpu(rsp); 4297 - break; 4298 - case CPU_DYING_IDLE: 4299 - /* QS for any half-done expedited RCU-sched GP. */ 4300 - preempt_disable(); 4301 - rcu_report_exp_rdp(&rcu_sched_state, 4302 - this_cpu_ptr(rcu_sched_state.rda), true); 4303 - preempt_enable(); 4304 - 4305 - for_each_rcu_flavor(rsp) { 4306 - rcu_cleanup_dying_idle_cpu(cpu, rsp); 4307 - } 4308 4279 break; 4309 4280 case CPU_DEAD: 4310 4281 case CPU_DEAD_FROZEN:
-10
kernel/sched/core.c
··· 5434 5434 set_cpu_rq_start_time(); 5435 5435 return NOTIFY_OK; 5436 5436 5437 - case CPU_ONLINE: 5438 - /* 5439 - * At this point a starting CPU has marked itself as online via 5440 - * set_cpu_online(). But it might not yet have marked itself 5441 - * as active, which is essential from here on. 5442 - */ 5443 - set_cpu_active(cpu, true); 5444 - stop_machine_unpark(cpu); 5445 - return NOTIFY_OK; 5446 - 5447 5437 case CPU_DOWN_FAILED: 5448 5438 set_cpu_active(cpu, true); 5449 5439 return NOTIFY_OK;
+3 -6
kernel/sched/idle.c
··· 4 4 #include <linux/sched.h> 5 5 #include <linux/cpu.h> 6 6 #include <linux/cpuidle.h> 7 + #include <linux/cpuhotplug.h> 7 8 #include <linux/tick.h> 8 9 #include <linux/mm.h> 9 10 #include <linux/stackprotector.h> ··· 194 193 rcu_idle_exit(); 195 194 } 196 195 197 - DEFINE_PER_CPU(bool, cpu_dead_idle); 198 - 199 196 /* 200 197 * Generic idle loop implementation 201 198 * ··· 220 221 rmb(); 221 222 222 223 if (cpu_is_offline(smp_processor_id())) { 223 - rcu_cpu_notify(NULL, CPU_DYING_IDLE, 224 - (void *)(long)smp_processor_id()); 225 - smp_mb(); /* all activity before dead. */ 226 - this_cpu_write(cpu_dead_idle, true); 224 + cpuhp_report_idle_dead(); 227 225 arch_cpu_idle_dead(); 228 226 } 229 227 ··· 287 291 boot_init_stack_canary(); 288 292 #endif 289 293 arch_cpu_idle_prepare(); 294 + cpuhp_online_idle(state); 290 295 cpu_idle_loop(); 291 296 }
+1
kernel/smp.c
··· 568 568 unsigned int cpu; 569 569 570 570 idle_threads_init(); 571 + cpuhp_threads_init(); 571 572 572 573 /* FIXME: This should be done in userspace --RR */ 573 574 for_each_present_cpu(cpu) {
+4 -2
kernel/smpboot.c
··· 226 226 kthread_unpark(tsk); 227 227 } 228 228 229 - void smpboot_unpark_threads(unsigned int cpu) 229 + int smpboot_unpark_threads(unsigned int cpu) 230 230 { 231 231 struct smp_hotplug_thread *cur; 232 232 ··· 235 235 if (cpumask_test_cpu(cpu, cur->cpumask)) 236 236 smpboot_unpark_thread(cur, cpu); 237 237 mutex_unlock(&smpboot_threads_lock); 238 + return 0; 238 239 } 239 240 240 241 static void smpboot_park_thread(struct smp_hotplug_thread *ht, unsigned int cpu) ··· 246 245 kthread_park(tsk); 247 246 } 248 247 249 - void smpboot_park_threads(unsigned int cpu) 248 + int smpboot_park_threads(unsigned int cpu) 250 249 { 251 250 struct smp_hotplug_thread *cur; 252 251 ··· 254 253 list_for_each_entry_reverse(cur, &hotplug_threads, list) 255 254 smpboot_park_thread(cur, cpu); 256 255 mutex_unlock(&smpboot_threads_lock); 256 + return 0; 257 257 } 258 258 259 259 static void smpboot_destroy_threads(struct smp_hotplug_thread *ht)
+4 -2
kernel/smpboot.h
··· 14 14 #endif 15 15 16 16 int smpboot_create_threads(unsigned int cpu); 17 - void smpboot_park_threads(unsigned int cpu); 18 - void smpboot_unpark_threads(unsigned int cpu); 17 + int smpboot_park_threads(unsigned int cpu); 18 + int smpboot_unpark_threads(unsigned int cpu); 19 + 20 + void __init cpuhp_threads_init(void); 19 21 20 22 #endif
+13
lib/Kconfig.debug
··· 1442 1442 1443 1443 Say N if you are unsure. 1444 1444 1445 + config CPU_HOTPLUG_STATE_CONTROL 1446 + bool "Enable CPU hotplug state control" 1447 + depends on DEBUG_KERNEL 1448 + depends on HOTPLUG_CPU 1449 + default n 1450 + help 1451 + Allows to write steps between "offline" and "online" to the CPUs 1452 + sysfs target file so states can be stepped granular. This is a debug 1453 + option for now as the hotplug machinery cannot be stopped and 1454 + restarted at arbitrary points yet. 1455 + 1456 + Say N if your are unsure. 1457 + 1445 1458 config NOTIFIER_ERROR_INJECTION 1446 1459 tristate "Notifier error injection" 1447 1460 depends on DEBUG_KERNEL