]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/cpu.c
drm/msm/gpu: fix building without debugfs
[mirror_ubuntu-bionic-kernel.git] / kernel / cpu.c
CommitLineData
1da177e4
LT
1/* CPU control.
2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
3 *
4 * This code is licenced under the GPL.
5 */
6#include <linux/proc_fs.h>
7#include <linux/smp.h>
8#include <linux/init.h>
9#include <linux/notifier.h>
3f07c014 10#include <linux/sched/signal.h>
ef8bd77f 11#include <linux/sched/hotplug.h>
29930025 12#include <linux/sched/task.h>
fc8944e3 13#include <linux/sched/smt.h>
1da177e4
LT
14#include <linux/unistd.h>
15#include <linux/cpu.h>
cb79295e
AV
16#include <linux/oom.h>
17#include <linux/rcupdate.h>
9984de1a 18#include <linux/export.h>
e4cc2f87 19#include <linux/bug.h>
1da177e4
LT
20#include <linux/kthread.h>
21#include <linux/stop_machine.h>
81615b62 22#include <linux/mutex.h>
5a0e3ad6 23#include <linux/gfp.h>
79cfbdfa 24#include <linux/suspend.h>
a19423b9 25#include <linux/lockdep.h>
345527b1 26#include <linux/tick.h>
a8994181 27#include <linux/irq.h>
941154bd 28#include <linux/nmi.h>
4cb28ced 29#include <linux/smpboot.h>
e6d4989a 30#include <linux/relay.h>
6731d4f1 31#include <linux/slab.h>
fc8dffd3 32#include <linux/percpu-rwsem.h>
cff7d378 33
bb3632c6 34#include <trace/events/power.h>
cff7d378
TG
35#define CREATE_TRACE_POINTS
36#include <trace/events/cpuhp.h>
1da177e4 37
38498a67
TG
38#include "smpboot.h"
39
cff7d378
TG
40/**
41 * cpuhp_cpu_state - Per cpu hotplug state storage
42 * @state: The current cpu state
43 * @target: The target state
4cb28ced
TG
44 * @thread: Pointer to the hotplug thread
45 * @should_run: Thread should execute
3b9d6da6 46 * @rollback: Perform a rollback
a724632c
TG
47 * @single: Single callback invocation
48 * @bringup: Single callback bringup or teardown selector
49 * @cb_state: The state for a single callback (install/uninstall)
4cb28ced 50 * @result: Result of the operation
5ebe7742
PZ
51 * @done_up: Signal completion to the issuer of the task for cpu-up
52 * @done_down: Signal completion to the issuer of the task for cpu-down
cff7d378
TG
53 */
54struct cpuhp_cpu_state {
55 enum cpuhp_state state;
56 enum cpuhp_state target;
1db49484 57 enum cpuhp_state fail;
4cb28ced
TG
58#ifdef CONFIG_SMP
59 struct task_struct *thread;
60 bool should_run;
3b9d6da6 61 bool rollback;
a724632c
TG
62 bool single;
63 bool bringup;
3762ba90 64 bool booted_once;
cf392d10 65 struct hlist_node *node;
4dddfb5f 66 struct hlist_node *last;
4cb28ced 67 enum cpuhp_state cb_state;
4cb28ced 68 int result;
5ebe7742
PZ
69 struct completion done_up;
70 struct completion done_down;
4cb28ced 71#endif
cff7d378
TG
72};
73
1db49484
PZ
74static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state) = {
75 .fail = CPUHP_INVALID,
76};
cff7d378 77
49dfe2a6 78#if defined(CONFIG_LOCKDEP) && defined(CONFIG_SMP)
5f4b55e1
PZ
79static struct lockdep_map cpuhp_state_up_map =
80 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-up", &cpuhp_state_up_map);
81static struct lockdep_map cpuhp_state_down_map =
82 STATIC_LOCKDEP_MAP_INIT("cpuhp_state-down", &cpuhp_state_down_map);
83
84
76dc6c09 85static inline void cpuhp_lock_acquire(bool bringup)
5f4b55e1
PZ
86{
87 lock_map_acquire(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
88}
89
76dc6c09 90static inline void cpuhp_lock_release(bool bringup)
5f4b55e1
PZ
91{
92 lock_map_release(bringup ? &cpuhp_state_up_map : &cpuhp_state_down_map);
93}
94#else
95
76dc6c09
MM
96static inline void cpuhp_lock_acquire(bool bringup) { }
97static inline void cpuhp_lock_release(bool bringup) { }
5f4b55e1 98
49dfe2a6
TG
99#endif
100
cff7d378
TG
101/**
102 * cpuhp_step - Hotplug state machine step
103 * @name: Name of the step
104 * @startup: Startup function of the step
105 * @teardown: Teardown function of the step
106 * @skip_onerr: Do not invoke the functions on error rollback
107 * Will go away once the notifiers are gone
757c989b 108 * @cant_stop: Bringup/teardown can't be stopped at this step
cff7d378
TG
109 */
110struct cpuhp_step {
cf392d10
TG
111 const char *name;
112 union {
3c1627e9
TG
113 int (*single)(unsigned int cpu);
114 int (*multi)(unsigned int cpu,
115 struct hlist_node *node);
116 } startup;
cf392d10 117 union {
3c1627e9
TG
118 int (*single)(unsigned int cpu);
119 int (*multi)(unsigned int cpu,
120 struct hlist_node *node);
121 } teardown;
cf392d10
TG
122 struct hlist_head list;
123 bool skip_onerr;
124 bool cant_stop;
125 bool multi_instance;
cff7d378
TG
126};
127
98f8cdce 128static DEFINE_MUTEX(cpuhp_state_mutex);
cff7d378 129static struct cpuhp_step cpuhp_bp_states[];
4baa0afc 130static struct cpuhp_step cpuhp_ap_states[];
cff7d378 131
a724632c
TG
132static bool cpuhp_is_ap_state(enum cpuhp_state state)
133{
134 /*
135 * The extra check for CPUHP_TEARDOWN_CPU is only for documentation
136 * purposes as that state is handled explicitly in cpu_down.
137 */
138 return state > CPUHP_BRINGUP_CPU && state != CPUHP_TEARDOWN_CPU;
139}
140
141static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
142{
143 struct cpuhp_step *sp;
144
145 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
146 return sp + state;
147}
148
cff7d378
TG
149/**
150 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
151 * @cpu: The cpu for which the callback should be invoked
96abb968 152 * @state: The state to do callbacks for
a724632c 153 * @bringup: True if the bringup callback should be invoked
96abb968
PZ
154 * @node: For multi-instance, do a single entry callback for install/remove
155 * @lastp: For multi-instance rollback, remember how far we got
cff7d378 156 *
cf392d10 157 * Called from cpu hotplug and from the state register machinery.
cff7d378 158 */
a724632c 159static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state state,
96abb968
PZ
160 bool bringup, struct hlist_node *node,
161 struct hlist_node **lastp)
cff7d378
TG
162{
163 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
a724632c 164 struct cpuhp_step *step = cpuhp_get_step(state);
cf392d10
TG
165 int (*cbm)(unsigned int cpu, struct hlist_node *node);
166 int (*cb)(unsigned int cpu);
167 int ret, cnt;
168
1db49484
PZ
169 if (st->fail == state) {
170 st->fail = CPUHP_INVALID;
171
172 if (!(bringup ? step->startup.single : step->teardown.single))
173 return 0;
174
175 return -EAGAIN;
176 }
177
cf392d10 178 if (!step->multi_instance) {
96abb968 179 WARN_ON_ONCE(lastp && *lastp);
3c1627e9 180 cb = bringup ? step->startup.single : step->teardown.single;
cf392d10
TG
181 if (!cb)
182 return 0;
a724632c 183 trace_cpuhp_enter(cpu, st->target, state, cb);
cff7d378 184 ret = cb(cpu);
a724632c 185 trace_cpuhp_exit(cpu, st->state, state, ret);
cf392d10
TG
186 return ret;
187 }
3c1627e9 188 cbm = bringup ? step->startup.multi : step->teardown.multi;
cf392d10
TG
189 if (!cbm)
190 return 0;
191
192 /* Single invocation for instance add/remove */
193 if (node) {
96abb968 194 WARN_ON_ONCE(lastp && *lastp);
cf392d10
TG
195 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
196 ret = cbm(cpu, node);
197 trace_cpuhp_exit(cpu, st->state, state, ret);
198 return ret;
199 }
200
201 /* State transition. Invoke on all instances */
202 cnt = 0;
203 hlist_for_each(node, &step->list) {
96abb968
PZ
204 if (lastp && node == *lastp)
205 break;
206
cf392d10
TG
207 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
208 ret = cbm(cpu, node);
209 trace_cpuhp_exit(cpu, st->state, state, ret);
96abb968
PZ
210 if (ret) {
211 if (!lastp)
212 goto err;
213
214 *lastp = node;
215 return ret;
216 }
cf392d10
TG
217 cnt++;
218 }
96abb968
PZ
219 if (lastp)
220 *lastp = NULL;
cf392d10
TG
221 return 0;
222err:
223 /* Rollback the instances if one failed */
3c1627e9 224 cbm = !bringup ? step->startup.multi : step->teardown.multi;
cf392d10
TG
225 if (!cbm)
226 return ret;
227
228 hlist_for_each(node, &step->list) {
229 if (!cnt--)
230 break;
724a8688
PZ
231
232 trace_cpuhp_multi_enter(cpu, st->target, state, cbm, node);
233 ret = cbm(cpu, node);
234 trace_cpuhp_exit(cpu, st->state, state, ret);
235 /*
236 * Rollback must not fail,
237 */
238 WARN_ON_ONCE(ret);
cff7d378
TG
239 }
240 return ret;
241}
242
98a79d6a 243#ifdef CONFIG_SMP
5ebe7742
PZ
244static inline void wait_for_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
245{
246 struct completion *done = bringup ? &st->done_up : &st->done_down;
247 wait_for_completion(done);
248}
249
250static inline void complete_ap_thread(struct cpuhp_cpu_state *st, bool bringup)
251{
252 struct completion *done = bringup ? &st->done_up : &st->done_down;
253 complete(done);
254}
255
256/*
257 * The former STARTING/DYING states, ran with IRQs disabled and must not fail.
258 */
259static bool cpuhp_is_atomic_state(enum cpuhp_state state)
260{
261 return CPUHP_AP_IDLE_DEAD <= state && state < CPUHP_AP_ONLINE;
262}
263
b3199c02 264/* Serializes the updates to cpu_online_mask, cpu_present_mask */
aa953877 265static DEFINE_MUTEX(cpu_add_remove_lock);
090e77c3
TG
266bool cpuhp_tasks_frozen;
267EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
1da177e4 268
79a6cdeb 269/*
93ae4f97
SB
270 * The following two APIs (cpu_maps_update_begin/done) must be used when
271 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
79a6cdeb
LJ
272 */
273void cpu_maps_update_begin(void)
274{
275 mutex_lock(&cpu_add_remove_lock);
276}
277
278void cpu_maps_update_done(void)
279{
280 mutex_unlock(&cpu_add_remove_lock);
281}
1da177e4 282
fc8dffd3
TG
283/*
284 * If set, cpu_up and cpu_down will return -EBUSY and do nothing.
e3920fb4
RW
285 * Should always be manipulated under cpu_add_remove_lock
286 */
287static int cpu_hotplug_disabled;
288
79a6cdeb
LJ
289#ifdef CONFIG_HOTPLUG_CPU
290
fc8dffd3 291DEFINE_STATIC_PERCPU_RWSEM(cpu_hotplug_lock);
a19423b9 292
8f553c49 293void cpus_read_lock(void)
a9d9baa1 294{
fc8dffd3 295 percpu_down_read(&cpu_hotplug_lock);
a9d9baa1 296}
8f553c49 297EXPORT_SYMBOL_GPL(cpus_read_lock);
90d45d17 298
8f553c49 299void cpus_read_unlock(void)
a9d9baa1 300{
fc8dffd3 301 percpu_up_read(&cpu_hotplug_lock);
a9d9baa1 302}
8f553c49 303EXPORT_SYMBOL_GPL(cpus_read_unlock);
a9d9baa1 304
8f553c49 305void cpus_write_lock(void)
d221938c 306{
fc8dffd3 307 percpu_down_write(&cpu_hotplug_lock);
d221938c 308}
87af9e7f 309
8f553c49 310void cpus_write_unlock(void)
d221938c 311{
fc8dffd3 312 percpu_up_write(&cpu_hotplug_lock);
d221938c
GS
313}
314
fc8dffd3 315void lockdep_assert_cpus_held(void)
d221938c 316{
fc8dffd3 317 percpu_rwsem_assert_held(&cpu_hotplug_lock);
d221938c 318}
79a6cdeb 319
16e53dbf
SB
320/*
321 * Wait for currently running CPU hotplug operations to complete (if any) and
322 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
323 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
324 * hotplug path before performing hotplug operations. So acquiring that lock
325 * guarantees mutual exclusion from any currently running hotplug operations.
326 */
327void cpu_hotplug_disable(void)
328{
329 cpu_maps_update_begin();
89af7ba5 330 cpu_hotplug_disabled++;
16e53dbf
SB
331 cpu_maps_update_done();
332}
32145c46 333EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
16e53dbf 334
01b41159
LW
335static void __cpu_hotplug_enable(void)
336{
337 if (WARN_ONCE(!cpu_hotplug_disabled, "Unbalanced cpu hotplug enable\n"))
338 return;
339 cpu_hotplug_disabled--;
340}
341
16e53dbf
SB
342void cpu_hotplug_enable(void)
343{
344 cpu_maps_update_begin();
01b41159 345 __cpu_hotplug_enable();
16e53dbf
SB
346 cpu_maps_update_done();
347}
32145c46 348EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
b9d10be7 349#endif /* CONFIG_HOTPLUG_CPU */
79a6cdeb 350
fc8944e3
TG
351/*
352 * Architectures that need SMT-specific errata handling during SMT hotplug
353 * should override this.
354 */
355void __weak arch_smt_update(void) { }
356
3762ba90
TG
357#ifdef CONFIG_HOTPLUG_SMT
358enum cpuhp_smt_control cpu_smt_control __read_mostly = CPU_SMT_ENABLED;
275b72a9 359EXPORT_SYMBOL_GPL(cpu_smt_control);
3762ba90 360
ade31b9e
TG
361static bool cpu_smt_available __read_mostly;
362
44219799 363void __init cpu_smt_disable(bool force)
3762ba90 364{
44219799
JK
365 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED ||
366 cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
367 return;
368
369 if (force) {
3762ba90
TG
370 pr_info("SMT: Force disabled\n");
371 cpu_smt_control = CPU_SMT_FORCE_DISABLED;
44219799
JK
372 } else {
373 cpu_smt_control = CPU_SMT_DISABLED;
3762ba90 374 }
44219799
JK
375}
376
483ec3c6
TG
377/*
378 * The decision whether SMT is supported can only be done after the full
ade31b9e
TG
379 * CPU identification. Called from architecture code before non boot CPUs
380 * are brought up.
483ec3c6 381 */
ade31b9e 382void __init cpu_smt_check_topology_early(void)
483ec3c6
TG
383{
384 if (!topology_smt_supported())
385 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
386}
387
ade31b9e
TG
388/*
389 * If SMT was disabled by BIOS, detect it here, after the CPUs have been
390 * brought online. This ensures the smt/l1tf sysfs entries are consistent
391 * with reality. cpu_smt_available is set to true during the bringup of non
392 * boot CPUs when a SMT sibling is detected. Note, this may overwrite
393 * cpu_smt_control's previous setting.
394 */
395void __init cpu_smt_check_topology(void)
396{
397 if (!cpu_smt_available)
398 cpu_smt_control = CPU_SMT_NOT_SUPPORTED;
399}
400
44219799
JK
401static int __init smt_cmdline_disable(char *str)
402{
403 cpu_smt_disable(str && !strcmp(str, "force"));
3762ba90
TG
404 return 0;
405}
406early_param("nosmt", smt_cmdline_disable);
407
408static inline bool cpu_smt_allowed(unsigned int cpu)
409{
ade31b9e 410 if (topology_is_primary_thread(cpu))
3762ba90
TG
411 return true;
412
ade31b9e
TG
413 /*
414 * If the CPU is not a 'primary' thread and the booted_once bit is
415 * set then the processor has SMT support. Store this information
416 * for the late check of SMT support in cpu_smt_check_topology().
417 */
418 if (per_cpu(cpuhp_state, cpu).booted_once)
419 cpu_smt_available = true;
420
421 if (cpu_smt_control == CPU_SMT_ENABLED)
3762ba90
TG
422 return true;
423
424 /*
425 * On x86 it's required to boot all logical CPUs at least once so
426 * that the init code can get a chance to set CR4.MCE on each
427 * CPU. Otherwise, a broadacasted MCE observing CR4.MCE=0b on any
428 * core will shutdown the machine.
429 */
430 return !per_cpu(cpuhp_state, cpu).booted_once;
431}
432#else
433static inline bool cpu_smt_allowed(unsigned int cpu) { return true; }
434#endif
435
4dddfb5f
PZ
436static inline enum cpuhp_state
437cpuhp_set_state(struct cpuhp_cpu_state *st, enum cpuhp_state target)
438{
439 enum cpuhp_state prev_state = st->state;
440
441 st->rollback = false;
442 st->last = NULL;
443
444 st->target = target;
445 st->single = false;
446 st->bringup = st->state < target;
447
448 return prev_state;
449}
450
451static inline void
452cpuhp_reset_state(struct cpuhp_cpu_state *st, enum cpuhp_state prev_state)
453{
454 st->rollback = true;
455
456 /*
457 * If we have st->last we need to undo partial multi_instance of this
458 * state first. Otherwise start undo at the previous state.
459 */
460 if (!st->last) {
461 if (st->bringup)
462 st->state--;
463 else
464 st->state++;
465 }
466
467 st->target = prev_state;
468 st->bringup = !st->bringup;
469}
470
471/* Regular hotplug invocation of the AP hotplug thread */
472static void __cpuhp_kick_ap(struct cpuhp_cpu_state *st)
473{
474 if (!st->single && st->state == st->target)
475 return;
476
477 st->result = 0;
478 /*
479 * Make sure the above stores are visible before should_run becomes
480 * true. Paired with the mb() above in cpuhp_thread_fun()
481 */
482 smp_mb();
483 st->should_run = true;
484 wake_up_process(st->thread);
5ebe7742 485 wait_for_ap_thread(st, st->bringup);
4dddfb5f
PZ
486}
487
488static int cpuhp_kick_ap(struct cpuhp_cpu_state *st, enum cpuhp_state target)
489{
490 enum cpuhp_state prev_state;
491 int ret;
492
493 prev_state = cpuhp_set_state(st, target);
494 __cpuhp_kick_ap(st);
495 if ((ret = st->result)) {
496 cpuhp_reset_state(st, prev_state);
497 __cpuhp_kick_ap(st);
498 }
499
500 return ret;
501}
9cd4f1a4 502
8df3e07e
TG
503static int bringup_wait_for_ap(unsigned int cpu)
504{
505 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
506
9cd4f1a4 507 /* Wait for the CPU to reach CPUHP_AP_ONLINE_IDLE */
5ebe7742 508 wait_for_ap_thread(st, true);
dea1d0f5
TG
509 if (WARN_ON_ONCE((!cpu_online(cpu))))
510 return -ECANCELED;
9cd4f1a4
TG
511
512 /* Unpark the stopper thread and the hotplug thread of the target cpu */
513 stop_machine_unpark(cpu);
514 kthread_unpark(st->thread);
515
3762ba90
TG
516 /*
517 * SMT soft disabling on X86 requires to bring the CPU out of the
518 * BIOS 'wait for SIPI' state in order to set the CR4.MCE bit. The
519 * CPU marked itself as booted_once in cpu_notify_starting() so the
520 * cpu_smt_allowed() check will now return false if this is not the
521 * primary sibling.
522 */
523 if (!cpu_smt_allowed(cpu))
524 return -ECANCELED;
525
4dddfb5f
PZ
526 if (st->target <= CPUHP_AP_ONLINE_IDLE)
527 return 0;
528
529 return cpuhp_kick_ap(st, st->target);
8df3e07e
TG
530}
531
ba997462
TG
532static int bringup_cpu(unsigned int cpu)
533{
534 struct task_struct *idle = idle_thread_get(cpu);
535 int ret;
536
aa877175
BO
537 /*
538 * Some architectures have to walk the irq descriptors to
539 * setup the vector space for the cpu which comes online.
540 * Prevent irq alloc/free across the bringup.
541 */
542 irq_lock_sparse();
543
ba997462
TG
544 /* Arch-specific enabling code. */
545 ret = __cpu_up(cpu, idle);
aa877175 546 irq_unlock_sparse();
530e9b76 547 if (ret)
ba997462 548 return ret;
9cd4f1a4 549 return bringup_wait_for_ap(cpu);
ba997462
TG
550}
551
2e1a3483
TG
552/*
553 * Hotplug state machine related functions
554 */
2e1a3483 555
a724632c 556static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
2e1a3483
TG
557{
558 for (st->state--; st->state > st->target; st->state--) {
a724632c 559 struct cpuhp_step *step = cpuhp_get_step(st->state);
2e1a3483
TG
560
561 if (!step->skip_onerr)
96abb968 562 cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
2e1a3483
TG
563 }
564}
565
566static int cpuhp_up_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
a724632c 567 enum cpuhp_state target)
2e1a3483
TG
568{
569 enum cpuhp_state prev_state = st->state;
570 int ret = 0;
571
572 while (st->state < target) {
2e1a3483 573 st->state++;
96abb968 574 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
2e1a3483
TG
575 if (ret) {
576 st->target = prev_state;
a724632c 577 undo_cpu_up(cpu, st);
2e1a3483
TG
578 break;
579 }
580 }
581 return ret;
582}
583
4cb28ced
TG
584/*
585 * The cpu hotplug threads manage the bringup and teardown of the cpus
586 */
587static void cpuhp_create(unsigned int cpu)
588{
589 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
590
5ebe7742
PZ
591 init_completion(&st->done_up);
592 init_completion(&st->done_down);
4cb28ced
TG
593}
594
595static int cpuhp_should_run(unsigned int cpu)
596{
597 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
598
599 return st->should_run;
600}
601
4cb28ced
TG
602/*
603 * Execute teardown/startup callbacks on the plugged cpu. Also used to invoke
604 * callbacks when a state gets [un]installed at runtime.
4dddfb5f
PZ
605 *
606 * Each invocation of this function by the smpboot thread does a single AP
607 * state callback.
608 *
609 * It has 3 modes of operation:
610 * - single: runs st->cb_state
611 * - up: runs ++st->state, while st->state < st->target
612 * - down: runs st->state--, while st->state > st->target
613 *
614 * When complete or on error, should_run is cleared and the completion is fired.
4cb28ced
TG
615 */
616static void cpuhp_thread_fun(unsigned int cpu)
617{
618 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
4dddfb5f
PZ
619 bool bringup = st->bringup;
620 enum cpuhp_state state;
4cb28ced 621
027c9910
NU
622 if (WARN_ON_ONCE(!st->should_run))
623 return;
624
4cb28ced 625 /*
4dddfb5f
PZ
626 * ACQUIRE for the cpuhp_should_run() load of ->should_run. Ensures
627 * that if we see ->should_run we also see the rest of the state.
4cb28ced
TG
628 */
629 smp_mb();
4cb28ced 630
5f4b55e1 631 cpuhp_lock_acquire(bringup);
4dddfb5f 632
a724632c 633 if (st->single) {
4dddfb5f
PZ
634 state = st->cb_state;
635 st->should_run = false;
636 } else {
637 if (bringup) {
638 st->state++;
639 state = st->state;
640 st->should_run = (st->state < st->target);
641 WARN_ON_ONCE(st->state > st->target);
4cb28ced 642 } else {
4dddfb5f
PZ
643 state = st->state;
644 st->state--;
645 st->should_run = (st->state > st->target);
646 WARN_ON_ONCE(st->state < st->target);
4cb28ced 647 }
4dddfb5f
PZ
648 }
649
650 WARN_ON_ONCE(!cpuhp_is_ap_state(state));
651
652 if (st->rollback) {
653 struct cpuhp_step *step = cpuhp_get_step(state);
654 if (step->skip_onerr)
655 goto next;
656 }
657
658 if (cpuhp_is_atomic_state(state)) {
659 local_irq_disable();
660 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
661 local_irq_enable();
3b9d6da6 662
4dddfb5f
PZ
663 /*
664 * STARTING/DYING must not fail!
665 */
666 WARN_ON_ONCE(st->result);
4cb28ced 667 } else {
4dddfb5f
PZ
668 st->result = cpuhp_invoke_callback(cpu, state, bringup, st->node, &st->last);
669 }
670
671 if (st->result) {
672 /*
673 * If we fail on a rollback, we're up a creek without no
674 * paddle, no way forward, no way back. We loose, thanks for
675 * playing.
676 */
677 WARN_ON_ONCE(st->rollback);
678 st->should_run = false;
4cb28ced 679 }
4dddfb5f
PZ
680
681next:
5f4b55e1 682 cpuhp_lock_release(bringup);
4dddfb5f
PZ
683
684 if (!st->should_run)
5ebe7742 685 complete_ap_thread(st, bringup);
4cb28ced
TG
686}
687
688/* Invoke a single callback on a remote cpu */
a724632c 689static int
cf392d10
TG
690cpuhp_invoke_ap_callback(int cpu, enum cpuhp_state state, bool bringup,
691 struct hlist_node *node)
4cb28ced
TG
692{
693 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
4dddfb5f 694 int ret;
4cb28ced
TG
695
696 if (!cpu_online(cpu))
697 return 0;
698
5f4b55e1
PZ
699 cpuhp_lock_acquire(false);
700 cpuhp_lock_release(false);
701
702 cpuhp_lock_acquire(true);
703 cpuhp_lock_release(true);
49dfe2a6 704
6a4e2451
TG
705 /*
706 * If we are up and running, use the hotplug thread. For early calls
707 * we invoke the thread function directly.
708 */
709 if (!st->thread)
96abb968 710 return cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
6a4e2451 711
4dddfb5f
PZ
712 st->rollback = false;
713 st->last = NULL;
714
715 st->node = node;
716 st->bringup = bringup;
4cb28ced 717 st->cb_state = state;
a724632c 718 st->single = true;
a724632c 719
4dddfb5f 720 __cpuhp_kick_ap(st);
4cb28ced 721
4cb28ced 722 /*
4dddfb5f 723 * If we failed and did a partial, do a rollback.
4cb28ced 724 */
4dddfb5f
PZ
725 if ((ret = st->result) && st->last) {
726 st->rollback = true;
727 st->bringup = !bringup;
728
729 __cpuhp_kick_ap(st);
730 }
731
1f7c70d6
TG
732 /*
733 * Clean up the leftovers so the next hotplug operation wont use stale
734 * data.
735 */
736 st->node = st->last = NULL;
4dddfb5f 737 return ret;
1cf4f629
TG
738}
739
740static int cpuhp_kick_ap_work(unsigned int cpu)
741{
742 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
4dddfb5f
PZ
743 enum cpuhp_state prev_state = st->state;
744 int ret;
1cf4f629 745
5f4b55e1
PZ
746 cpuhp_lock_acquire(false);
747 cpuhp_lock_release(false);
748
749 cpuhp_lock_acquire(true);
750 cpuhp_lock_release(true);
4dddfb5f
PZ
751
752 trace_cpuhp_enter(cpu, st->target, prev_state, cpuhp_kick_ap_work);
753 ret = cpuhp_kick_ap(st, st->target);
754 trace_cpuhp_exit(cpu, st->state, prev_state, ret);
755
756 return ret;
4cb28ced
TG
757}
758
759static struct smp_hotplug_thread cpuhp_threads = {
760 .store = &cpuhp_state.thread,
761 .create = &cpuhp_create,
762 .thread_should_run = cpuhp_should_run,
763 .thread_fn = cpuhp_thread_fun,
764 .thread_comm = "cpuhp/%u",
765 .selfparking = true,
766};
767
768void __init cpuhp_threads_init(void)
769{
770 BUG_ON(smpboot_register_percpu_thread(&cpuhp_threads));
771 kthread_unpark(this_cpu_read(cpuhp_state.thread));
772}
773
777c6e0d 774#ifdef CONFIG_HOTPLUG_CPU
e4cc2f87
AV
775/**
776 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
777 * @cpu: a CPU id
778 *
779 * This function walks all processes, finds a valid mm struct for each one and
780 * then clears a corresponding bit in mm's cpumask. While this all sounds
781 * trivial, there are various non-obvious corner cases, which this function
782 * tries to solve in a safe manner.
783 *
784 * Also note that the function uses a somewhat relaxed locking scheme, so it may
785 * be called only for an already offlined CPU.
786 */
cb79295e
AV
787void clear_tasks_mm_cpumask(int cpu)
788{
789 struct task_struct *p;
790
791 /*
792 * This function is called after the cpu is taken down and marked
793 * offline, so its not like new tasks will ever get this cpu set in
794 * their mm mask. -- Peter Zijlstra
795 * Thus, we may use rcu_read_lock() here, instead of grabbing
796 * full-fledged tasklist_lock.
797 */
e4cc2f87 798 WARN_ON(cpu_online(cpu));
cb79295e
AV
799 rcu_read_lock();
800 for_each_process(p) {
801 struct task_struct *t;
802
e4cc2f87
AV
803 /*
804 * Main thread might exit, but other threads may still have
805 * a valid mm. Find one.
806 */
cb79295e
AV
807 t = find_lock_task_mm(p);
808 if (!t)
809 continue;
810 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
811 task_unlock(t);
812 }
813 rcu_read_unlock();
814}
815
1da177e4 816/* Take this CPU down. */
71cf5aee 817static int take_cpu_down(void *_param)
1da177e4 818{
4baa0afc
TG
819 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
820 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
090e77c3 821 int err, cpu = smp_processor_id();
724a8688 822 int ret;
1da177e4 823
1da177e4
LT
824 /* Ensure this CPU doesn't handle any more interrupts. */
825 err = __cpu_disable();
826 if (err < 0)
f3705136 827 return err;
1da177e4 828
a724632c
TG
829 /*
830 * We get here while we are in CPUHP_TEARDOWN_CPU state and we must not
831 * do this step again.
832 */
833 WARN_ON(st->state != CPUHP_TEARDOWN_CPU);
834 st->state--;
4baa0afc 835 /* Invoke the former CPU_DYING callbacks */
724a8688
PZ
836 for (; st->state > target; st->state--) {
837 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
838 /*
839 * DYING must not fail!
840 */
841 WARN_ON_ONCE(ret);
842 }
4baa0afc 843
52c063d1
TG
844 /* Give up timekeeping duties */
845 tick_handover_do_timer();
14e568e7 846 /* Park the stopper thread */
090e77c3 847 stop_machine_park(cpu);
f3705136 848 return 0;
1da177e4
LT
849}
850
98458172 851static int takedown_cpu(unsigned int cpu)
1da177e4 852{
e69aab13 853 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
98458172 854 int err;
1da177e4 855
2a58c527 856 /* Park the smpboot threads */
1cf4f629
TG
857 kthread_park(per_cpu_ptr(&cpuhp_state, cpu)->thread);
858
6acce3ef 859 /*
a8994181
TG
860 * Prevent irq alloc/free while the dying cpu reorganizes the
861 * interrupt affinities.
6acce3ef 862 */
a8994181 863 irq_lock_sparse();
6acce3ef 864
a8994181
TG
865 /*
866 * So now all preempt/rcu users must observe !cpu_active().
867 */
210e2133 868 err = stop_machine_cpuslocked(take_cpu_down, NULL, cpumask_of(cpu));
04321587 869 if (err) {
3b9d6da6 870 /* CPU refused to die */
a8994181 871 irq_unlock_sparse();
3b9d6da6
SAS
872 /* Unpark the hotplug thread so we can rollback there */
873 kthread_unpark(per_cpu_ptr(&cpuhp_state, cpu)->thread);
98458172 874 return err;
8fa1d7d3 875 }
04321587 876 BUG_ON(cpu_online(cpu));
1da177e4 877
48c5ccae 878 /*
5b1ead68
BJ
879 * The teardown callback for CPUHP_AP_SCHED_STARTING will have removed
880 * all runnable tasks from the CPU, there's only the idle task left now
48c5ccae 881 * that the migration thread is done doing the stop_machine thing.
51a96c77
PZ
882 *
883 * Wait for the stop thread to go away.
48c5ccae 884 */
5ebe7742 885 wait_for_ap_thread(st, false);
e69aab13 886 BUG_ON(st->state != CPUHP_AP_IDLE_DEAD);
1da177e4 887
a8994181
TG
888 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
889 irq_unlock_sparse();
890
345527b1 891 hotplug_cpu__broadcast_tick_pull(cpu);
1da177e4
LT
892 /* This actually kills the CPU. */
893 __cpu_die(cpu);
894
a49b116d 895 tick_cleanup_dead_cpu(cpu);
a58163d8 896 rcutree_migrate_callbacks(cpu);
98458172
TG
897 return 0;
898}
1da177e4 899
71f87b2f
TG
900static void cpuhp_complete_idle_dead(void *arg)
901{
902 struct cpuhp_cpu_state *st = arg;
903
5ebe7742 904 complete_ap_thread(st, false);
71f87b2f
TG
905}
906
e69aab13
TG
907void cpuhp_report_idle_dead(void)
908{
909 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
910
911 BUG_ON(st->state != CPUHP_AP_OFFLINE);
27d50c7e 912 rcu_report_dead(smp_processor_id());
71f87b2f
TG
913 st->state = CPUHP_AP_IDLE_DEAD;
914 /*
915 * We cannot call complete after rcu_report_dead() so we delegate it
916 * to an online cpu.
917 */
918 smp_call_function_single(cpumask_first(cpu_online_mask),
919 cpuhp_complete_idle_dead, st, 0);
e69aab13
TG
920}
921
4dddfb5f
PZ
922static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
923{
924 for (st->state++; st->state < st->target; st->state++) {
925 struct cpuhp_step *step = cpuhp_get_step(st->state);
cff7d378 926
4dddfb5f
PZ
927 if (!step->skip_onerr)
928 cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
929 }
930}
931
932static int cpuhp_down_callbacks(unsigned int cpu, struct cpuhp_cpu_state *st,
933 enum cpuhp_state target)
934{
935 enum cpuhp_state prev_state = st->state;
936 int ret = 0;
937
938 for (; st->state > target; st->state--) {
939 ret = cpuhp_invoke_callback(cpu, st->state, false, NULL, NULL);
940 if (ret) {
941 st->target = prev_state;
b49a4576
TG
942 if (st->state < prev_state)
943 undo_cpu_down(cpu, st);
4dddfb5f
PZ
944 break;
945 }
946 }
947 return ret;
948}
cff7d378 949
98458172 950/* Requires cpu_add_remove_lock to be held */
af1f4045
TG
951static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
952 enum cpuhp_state target)
98458172 953{
cff7d378
TG
954 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
955 int prev_state, ret = 0;
98458172
TG
956
957 if (num_online_cpus() == 1)
958 return -EBUSY;
959
757c989b 960 if (!cpu_present(cpu))
98458172
TG
961 return -EINVAL;
962
8f553c49 963 cpus_write_lock();
98458172
TG
964
965 cpuhp_tasks_frozen = tasks_frozen;
966
4dddfb5f 967 prev_state = cpuhp_set_state(st, target);
1cf4f629
TG
968 /*
969 * If the current CPU state is in the range of the AP hotplug thread,
970 * then we need to kick the thread.
971 */
8df3e07e 972 if (st->state > CPUHP_TEARDOWN_CPU) {
4dddfb5f 973 st->target = max((int)target, CPUHP_TEARDOWN_CPU);
1cf4f629
TG
974 ret = cpuhp_kick_ap_work(cpu);
975 /*
976 * The AP side has done the error rollback already. Just
977 * return the error code..
978 */
979 if (ret)
980 goto out;
981
982 /*
983 * We might have stopped still in the range of the AP hotplug
984 * thread. Nothing to do anymore.
985 */
8df3e07e 986 if (st->state > CPUHP_TEARDOWN_CPU)
1cf4f629 987 goto out;
4dddfb5f
PZ
988
989 st->target = target;
1cf4f629
TG
990 }
991 /*
8df3e07e 992 * The AP brought itself down to CPUHP_TEARDOWN_CPU. So we need
1cf4f629
TG
993 * to do the further cleanups.
994 */
a724632c 995 ret = cpuhp_down_callbacks(cpu, st, target);
b49a4576 996 if (ret && st->state == CPUHP_TEARDOWN_CPU && st->state < prev_state) {
4dddfb5f
PZ
997 cpuhp_reset_state(st, prev_state);
998 __cpuhp_kick_ap(st);
3b9d6da6 999 }
98458172 1000
1cf4f629 1001out:
8f553c49 1002 cpus_write_unlock();
941154bd
TG
1003 /*
1004 * Do post unplug cleanup. This is still protected against
1005 * concurrent CPU hotplug via cpu_add_remove_lock.
1006 */
1007 lockup_detector_cleanup();
fc8944e3 1008 arch_smt_update();
cff7d378 1009 return ret;
e3920fb4
RW
1010}
1011
97127f50
TG
1012static int cpu_down_maps_locked(unsigned int cpu, enum cpuhp_state target)
1013{
1014 if (cpu_hotplug_disabled)
1015 return -EBUSY;
1016 return _cpu_down(cpu, 0, target);
1017}
1018
af1f4045 1019static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
e3920fb4 1020{
9ea09af3 1021 int err;
e3920fb4 1022
d221938c 1023 cpu_maps_update_begin();
97127f50 1024 err = cpu_down_maps_locked(cpu, target);
d221938c 1025 cpu_maps_update_done();
1da177e4
LT
1026 return err;
1027}
4dddfb5f 1028
af1f4045
TG
1029int cpu_down(unsigned int cpu)
1030{
1031 return do_cpu_down(cpu, CPUHP_OFFLINE);
1032}
b62b8ef9 1033EXPORT_SYMBOL(cpu_down);
4dddfb5f
PZ
1034
1035#else
1036#define takedown_cpu NULL
1da177e4
LT
1037#endif /*CONFIG_HOTPLUG_CPU*/
1038
4baa0afc 1039/**
ee1e714b 1040 * notify_cpu_starting(cpu) - Invoke the callbacks on the starting CPU
4baa0afc
TG
1041 * @cpu: cpu that just started
1042 *
4baa0afc
TG
1043 * It must be called by the arch code on the new cpu, before the new cpu
1044 * enables interrupts and before the "boot" cpu returns from __cpu_up().
1045 */
1046void notify_cpu_starting(unsigned int cpu)
1047{
1048 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1049 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
724a8688 1050 int ret;
4baa0afc 1051
0c6d4576 1052 rcu_cpu_starting(cpu); /* Enables RCU usage on this CPU. */
3762ba90 1053 st->booted_once = true;
4baa0afc 1054 while (st->state < target) {
4baa0afc 1055 st->state++;
724a8688
PZ
1056 ret = cpuhp_invoke_callback(cpu, st->state, true, NULL, NULL);
1057 /*
1058 * STARTING must not fail!
1059 */
1060 WARN_ON_ONCE(ret);
4baa0afc
TG
1061 }
1062}
1063
949338e3 1064/*
9cd4f1a4
TG
1065 * Called from the idle task. Wake up the controlling task which brings the
1066 * stopper and the hotplug thread of the upcoming CPU up and then delegates
1067 * the rest of the online bringup to the hotplug thread.
949338e3 1068 */
8df3e07e 1069void cpuhp_online_idle(enum cpuhp_state state)
949338e3 1070{
8df3e07e 1071 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
8df3e07e
TG
1072
1073 /* Happens for the boot cpu */
1074 if (state != CPUHP_AP_ONLINE_IDLE)
1075 return;
1076
1077 st->state = CPUHP_AP_ONLINE_IDLE;
5ebe7742 1078 complete_ap_thread(st, true);
949338e3
TG
1079}
1080
e3920fb4 1081/* Requires cpu_add_remove_lock to be held */
af1f4045 1082static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1da177e4 1083{
cff7d378 1084 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
3bb5d2ee 1085 struct task_struct *idle;
2e1a3483 1086 int ret = 0;
1da177e4 1087
8f553c49 1088 cpus_write_lock();
38498a67 1089
757c989b 1090 if (!cpu_present(cpu)) {
5e5041f3
YI
1091 ret = -EINVAL;
1092 goto out;
1093 }
1094
757c989b
TG
1095 /*
1096 * The caller of do_cpu_up might have raced with another
1097 * caller. Ignore it for now.
1098 */
1099 if (st->state >= target)
38498a67 1100 goto out;
757c989b
TG
1101
1102 if (st->state == CPUHP_OFFLINE) {
1103 /* Let it fail before we try to bring the cpu up */
1104 idle = idle_thread_get(cpu);
1105 if (IS_ERR(idle)) {
1106 ret = PTR_ERR(idle);
1107 goto out;
1108 }
3bb5d2ee 1109 }
38498a67 1110
ba997462
TG
1111 cpuhp_tasks_frozen = tasks_frozen;
1112
4dddfb5f 1113 cpuhp_set_state(st, target);
1cf4f629
TG
1114 /*
1115 * If the current CPU state is in the range of the AP hotplug thread,
1116 * then we need to kick the thread once more.
1117 */
8df3e07e 1118 if (st->state > CPUHP_BRINGUP_CPU) {
1cf4f629
TG
1119 ret = cpuhp_kick_ap_work(cpu);
1120 /*
1121 * The AP side has done the error rollback already. Just
1122 * return the error code..
1123 */
1124 if (ret)
1125 goto out;
1126 }
1127
1128 /*
1129 * Try to reach the target state. We max out on the BP at
8df3e07e 1130 * CPUHP_BRINGUP_CPU. After that the AP hotplug thread is
1cf4f629
TG
1131 * responsible for bringing it up to the target state.
1132 */
8df3e07e 1133 target = min((int)target, CPUHP_BRINGUP_CPU);
a724632c 1134 ret = cpuhp_up_callbacks(cpu, st, target);
38498a67 1135out:
8f553c49 1136 cpus_write_unlock();
fc8944e3 1137 arch_smt_update();
e3920fb4
RW
1138 return ret;
1139}
1140
af1f4045 1141static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
e3920fb4
RW
1142{
1143 int err = 0;
cf23422b 1144
e0b582ec 1145 if (!cpu_possible(cpu)) {
84117da5
FF
1146 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
1147 cpu);
87d5e023 1148#if defined(CONFIG_IA64)
84117da5 1149 pr_err("please check additional_cpus= boot parameter\n");
73e753a5
KH
1150#endif
1151 return -EINVAL;
1152 }
e3920fb4 1153
01b0f197
TK
1154 err = try_online_node(cpu_to_node(cpu));
1155 if (err)
1156 return err;
cf23422b 1157
d221938c 1158 cpu_maps_update_begin();
e761b772
MK
1159
1160 if (cpu_hotplug_disabled) {
e3920fb4 1161 err = -EBUSY;
e761b772
MK
1162 goto out;
1163 }
47b4c679
TG
1164 if (!cpu_smt_allowed(cpu)) {
1165 err = -EPERM;
1166 goto out;
1167 }
e761b772 1168
af1f4045 1169 err = _cpu_up(cpu, 0, target);
e761b772 1170out:
d221938c 1171 cpu_maps_update_done();
e3920fb4
RW
1172 return err;
1173}
af1f4045
TG
1174
1175int cpu_up(unsigned int cpu)
1176{
1177 return do_cpu_up(cpu, CPUHP_ONLINE);
1178}
a513f6ba 1179EXPORT_SYMBOL_GPL(cpu_up);
e3920fb4 1180
f3de4be9 1181#ifdef CONFIG_PM_SLEEP_SMP
e0b582ec 1182static cpumask_var_t frozen_cpus;
e3920fb4 1183
d391e552 1184int freeze_secondary_cpus(int primary)
e3920fb4 1185{
d391e552 1186 int cpu, error = 0;
e3920fb4 1187
d221938c 1188 cpu_maps_update_begin();
d391e552
JM
1189 if (!cpu_online(primary))
1190 primary = cpumask_first(cpu_online_mask);
9ee349ad
XF
1191 /*
1192 * We take down all of the non-boot CPUs in one shot to avoid races
e3920fb4
RW
1193 * with the userspace trying to use the CPU hotplug at the same time
1194 */
e0b582ec 1195 cpumask_clear(frozen_cpus);
6ad4c188 1196
84117da5 1197 pr_info("Disabling non-boot CPUs ...\n");
e3920fb4 1198 for_each_online_cpu(cpu) {
d391e552 1199 if (cpu == primary)
e3920fb4 1200 continue;
bb3632c6 1201 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
af1f4045 1202 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
bb3632c6 1203 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
feae3203 1204 if (!error)
e0b582ec 1205 cpumask_set_cpu(cpu, frozen_cpus);
feae3203 1206 else {
84117da5 1207 pr_err("Error taking CPU%d down: %d\n", cpu, error);
e3920fb4
RW
1208 break;
1209 }
1210 }
86886e55 1211
89af7ba5 1212 if (!error)
e3920fb4 1213 BUG_ON(num_online_cpus() > 1);
89af7ba5 1214 else
84117da5 1215 pr_err("Non-boot CPUs are not disabled\n");
89af7ba5
VK
1216
1217 /*
1218 * Make sure the CPUs won't be enabled by someone else. We need to do
1219 * this even in case of failure as all disable_nonboot_cpus() users are
1220 * supposed to do enable_nonboot_cpus() on the failure path.
1221 */
1222 cpu_hotplug_disabled++;
1223
d221938c 1224 cpu_maps_update_done();
e3920fb4
RW
1225 return error;
1226}
1227
d0af9eed
SS
1228void __weak arch_enable_nonboot_cpus_begin(void)
1229{
1230}
1231
1232void __weak arch_enable_nonboot_cpus_end(void)
1233{
1234}
1235
71cf5aee 1236void enable_nonboot_cpus(void)
e3920fb4
RW
1237{
1238 int cpu, error;
1239
1240 /* Allow everyone to use the CPU hotplug again */
d221938c 1241 cpu_maps_update_begin();
01b41159 1242 __cpu_hotplug_enable();
e0b582ec 1243 if (cpumask_empty(frozen_cpus))
1d64b9cb 1244 goto out;
e3920fb4 1245
84117da5 1246 pr_info("Enabling non-boot CPUs ...\n");
d0af9eed
SS
1247
1248 arch_enable_nonboot_cpus_begin();
1249
e0b582ec 1250 for_each_cpu(cpu, frozen_cpus) {
bb3632c6 1251 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
af1f4045 1252 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
bb3632c6 1253 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
e3920fb4 1254 if (!error) {
84117da5 1255 pr_info("CPU%d is up\n", cpu);
e3920fb4
RW
1256 continue;
1257 }
84117da5 1258 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
e3920fb4 1259 }
d0af9eed
SS
1260
1261 arch_enable_nonboot_cpus_end();
1262
e0b582ec 1263 cpumask_clear(frozen_cpus);
1d64b9cb 1264out:
d221938c 1265 cpu_maps_update_done();
1da177e4 1266}
e0b582ec 1267
d7268a31 1268static int __init alloc_frozen_cpus(void)
e0b582ec
RR
1269{
1270 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
1271 return -ENOMEM;
1272 return 0;
1273}
1274core_initcall(alloc_frozen_cpus);
79cfbdfa 1275
79cfbdfa
SB
1276/*
1277 * When callbacks for CPU hotplug notifications are being executed, we must
1278 * ensure that the state of the system with respect to the tasks being frozen
1279 * or not, as reported by the notification, remains unchanged *throughout the
1280 * duration* of the execution of the callbacks.
1281 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
1282 *
1283 * This synchronization is implemented by mutually excluding regular CPU
1284 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
1285 * Hibernate notifications.
1286 */
1287static int
1288cpu_hotplug_pm_callback(struct notifier_block *nb,
1289 unsigned long action, void *ptr)
1290{
1291 switch (action) {
1292
1293 case PM_SUSPEND_PREPARE:
1294 case PM_HIBERNATION_PREPARE:
16e53dbf 1295 cpu_hotplug_disable();
79cfbdfa
SB
1296 break;
1297
1298 case PM_POST_SUSPEND:
1299 case PM_POST_HIBERNATION:
16e53dbf 1300 cpu_hotplug_enable();
79cfbdfa
SB
1301 break;
1302
1303 default:
1304 return NOTIFY_DONE;
1305 }
1306
1307 return NOTIFY_OK;
1308}
1309
1310
d7268a31 1311static int __init cpu_hotplug_pm_sync_init(void)
79cfbdfa 1312{
6e32d479
FY
1313 /*
1314 * cpu_hotplug_pm_callback has higher priority than x86
1315 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
1316 * to disable cpu hotplug to avoid cpu hotplug race.
1317 */
79cfbdfa
SB
1318 pm_notifier(cpu_hotplug_pm_callback, 0);
1319 return 0;
1320}
1321core_initcall(cpu_hotplug_pm_sync_init);
1322
f3de4be9 1323#endif /* CONFIG_PM_SLEEP_SMP */
68f4f1ec 1324
8ce371f9
PZ
1325int __boot_cpu_id;
1326
68f4f1ec 1327#endif /* CONFIG_SMP */
b8d317d1 1328
cff7d378
TG
1329/* Boot processor state steps */
1330static struct cpuhp_step cpuhp_bp_states[] = {
1331 [CPUHP_OFFLINE] = {
1332 .name = "offline",
3c1627e9
TG
1333 .startup.single = NULL,
1334 .teardown.single = NULL,
cff7d378
TG
1335 },
1336#ifdef CONFIG_SMP
1337 [CPUHP_CREATE_THREADS]= {
677f6646 1338 .name = "threads:prepare",
3c1627e9
TG
1339 .startup.single = smpboot_create_threads,
1340 .teardown.single = NULL,
757c989b 1341 .cant_stop = true,
cff7d378 1342 },
00e16c3d 1343 [CPUHP_PERF_PREPARE] = {
3c1627e9
TG
1344 .name = "perf:prepare",
1345 .startup.single = perf_event_init_cpu,
1346 .teardown.single = perf_event_exit_cpu,
00e16c3d 1347 },
7ee681b2 1348 [CPUHP_WORKQUEUE_PREP] = {
3c1627e9
TG
1349 .name = "workqueue:prepare",
1350 .startup.single = workqueue_prepare_cpu,
1351 .teardown.single = NULL,
7ee681b2 1352 },
27590dc1 1353 [CPUHP_HRTIMERS_PREPARE] = {
3c1627e9
TG
1354 .name = "hrtimers:prepare",
1355 .startup.single = hrtimers_prepare_cpu,
1356 .teardown.single = hrtimers_dead_cpu,
27590dc1 1357 },
31487f83 1358 [CPUHP_SMPCFD_PREPARE] = {
677f6646 1359 .name = "smpcfd:prepare",
3c1627e9
TG
1360 .startup.single = smpcfd_prepare_cpu,
1361 .teardown.single = smpcfd_dead_cpu,
31487f83 1362 },
e6d4989a
RW
1363 [CPUHP_RELAY_PREPARE] = {
1364 .name = "relay:prepare",
1365 .startup.single = relay_prepare_cpu,
1366 .teardown.single = NULL,
1367 },
6731d4f1
SAS
1368 [CPUHP_SLAB_PREPARE] = {
1369 .name = "slab:prepare",
1370 .startup.single = slab_prepare_cpu,
1371 .teardown.single = slab_dead_cpu,
31487f83 1372 },
4df83742 1373 [CPUHP_RCUTREE_PREP] = {
677f6646 1374 .name = "RCU/tree:prepare",
3c1627e9
TG
1375 .startup.single = rcutree_prepare_cpu,
1376 .teardown.single = rcutree_dead_cpu,
4df83742 1377 },
4fae16df
RC
1378 /*
1379 * On the tear-down path, timers_dead_cpu() must be invoked
1380 * before blk_mq_queue_reinit_notify() from notify_dead(),
1381 * otherwise a RCU stall occurs.
1382 */
26456f87 1383 [CPUHP_TIMERS_PREPARE] = {
3c1627e9 1384 .name = "timers:dead",
26456f87 1385 .startup.single = timers_prepare_cpu,
3c1627e9 1386 .teardown.single = timers_dead_cpu,
4fae16df 1387 },
d10ef6f9 1388 /* Kicks the plugged cpu into life */
cff7d378
TG
1389 [CPUHP_BRINGUP_CPU] = {
1390 .name = "cpu:bringup",
3c1627e9
TG
1391 .startup.single = bringup_cpu,
1392 .teardown.single = NULL,
757c989b 1393 .cant_stop = true,
4baa0afc 1394 },
d10ef6f9
TG
1395 /*
1396 * Handled on controll processor until the plugged processor manages
1397 * this itself.
1398 */
4baa0afc
TG
1399 [CPUHP_TEARDOWN_CPU] = {
1400 .name = "cpu:teardown",
3c1627e9
TG
1401 .startup.single = NULL,
1402 .teardown.single = takedown_cpu,
757c989b 1403 .cant_stop = true,
cff7d378 1404 },
a7c73414
TG
1405#else
1406 [CPUHP_BRINGUP_CPU] = { },
cff7d378 1407#endif
cff7d378
TG
1408};
1409
4baa0afc
TG
1410/* Application processor state steps */
1411static struct cpuhp_step cpuhp_ap_states[] = {
1412#ifdef CONFIG_SMP
d10ef6f9
TG
1413 /* Final state before CPU kills itself */
1414 [CPUHP_AP_IDLE_DEAD] = {
1415 .name = "idle:dead",
1416 },
1417 /*
1418 * Last state before CPU enters the idle loop to die. Transient state
1419 * for synchronization.
1420 */
1421 [CPUHP_AP_OFFLINE] = {
1422 .name = "ap:offline",
1423 .cant_stop = true,
1424 },
9cf7243d
TG
1425 /* First state is scheduler control. Interrupts are disabled */
1426 [CPUHP_AP_SCHED_STARTING] = {
1427 .name = "sched:starting",
3c1627e9
TG
1428 .startup.single = sched_cpu_starting,
1429 .teardown.single = sched_cpu_dying,
9cf7243d 1430 },
4df83742 1431 [CPUHP_AP_RCUTREE_DYING] = {
677f6646 1432 .name = "RCU/tree:dying",
3c1627e9
TG
1433 .startup.single = NULL,
1434 .teardown.single = rcutree_dying_cpu,
4baa0afc 1435 },
46febd37
LJ
1436 [CPUHP_AP_SMPCFD_DYING] = {
1437 .name = "smpcfd:dying",
1438 .startup.single = NULL,
1439 .teardown.single = smpcfd_dying_cpu,
1440 },
d10ef6f9
TG
1441 /* Entry state on starting. Interrupts enabled from here on. Transient
1442 * state for synchronsization */
1443 [CPUHP_AP_ONLINE] = {
1444 .name = "ap:online",
1445 },
1446 /* Handle smpboot threads park/unpark */
1cf4f629 1447 [CPUHP_AP_SMPBOOT_THREADS] = {
677f6646 1448 .name = "smpboot/threads:online",
3c1627e9 1449 .startup.single = smpboot_unpark_threads,
a82cd3a3 1450 .teardown.single = smpboot_park_threads,
1cf4f629 1451 },
c5cb83bb
TG
1452 [CPUHP_AP_IRQ_AFFINITY_ONLINE] = {
1453 .name = "irq/affinity:online",
1454 .startup.single = irq_affinity_online_cpu,
1455 .teardown.single = NULL,
1456 },
00e16c3d 1457 [CPUHP_AP_PERF_ONLINE] = {
3c1627e9
TG
1458 .name = "perf:online",
1459 .startup.single = perf_event_init_cpu,
1460 .teardown.single = perf_event_exit_cpu,
00e16c3d 1461 },
7ee681b2 1462 [CPUHP_AP_WORKQUEUE_ONLINE] = {
3c1627e9
TG
1463 .name = "workqueue:online",
1464 .startup.single = workqueue_online_cpu,
1465 .teardown.single = workqueue_offline_cpu,
7ee681b2 1466 },
4df83742 1467 [CPUHP_AP_RCUTREE_ONLINE] = {
677f6646 1468 .name = "RCU/tree:online",
3c1627e9
TG
1469 .startup.single = rcutree_online_cpu,
1470 .teardown.single = rcutree_offline_cpu,
4df83742 1471 },
4baa0afc 1472#endif
d10ef6f9
TG
1473 /*
1474 * The dynamically registered state space is here
1475 */
1476
aaddd7d1
TG
1477#ifdef CONFIG_SMP
1478 /* Last state is scheduler control setting the cpu active */
1479 [CPUHP_AP_ACTIVE] = {
1480 .name = "sched:active",
3c1627e9
TG
1481 .startup.single = sched_cpu_activate,
1482 .teardown.single = sched_cpu_deactivate,
aaddd7d1
TG
1483 },
1484#endif
1485
d10ef6f9 1486 /* CPU is fully up and running. */
4baa0afc
TG
1487 [CPUHP_ONLINE] = {
1488 .name = "online",
3c1627e9
TG
1489 .startup.single = NULL,
1490 .teardown.single = NULL,
4baa0afc
TG
1491 },
1492};
1493
5b7aa87e
TG
1494/* Sanity check for callbacks */
1495static int cpuhp_cb_check(enum cpuhp_state state)
1496{
1497 if (state <= CPUHP_OFFLINE || state >= CPUHP_ONLINE)
1498 return -EINVAL;
1499 return 0;
1500}
1501
dc280d93
TG
1502/*
1503 * Returns a free for dynamic slot assignment of the Online state. The states
1504 * are protected by the cpuhp_slot_states mutex and an empty slot is identified
1505 * by having no name assigned.
1506 */
1507static int cpuhp_reserve_state(enum cpuhp_state state)
1508{
4205e478
TG
1509 enum cpuhp_state i, end;
1510 struct cpuhp_step *step;
dc280d93 1511
4205e478
TG
1512 switch (state) {
1513 case CPUHP_AP_ONLINE_DYN:
1514 step = cpuhp_ap_states + CPUHP_AP_ONLINE_DYN;
1515 end = CPUHP_AP_ONLINE_DYN_END;
1516 break;
1517 case CPUHP_BP_PREPARE_DYN:
1518 step = cpuhp_bp_states + CPUHP_BP_PREPARE_DYN;
1519 end = CPUHP_BP_PREPARE_DYN_END;
1520 break;
1521 default:
1522 return -EINVAL;
1523 }
1524
1525 for (i = state; i <= end; i++, step++) {
1526 if (!step->name)
dc280d93
TG
1527 return i;
1528 }
1529 WARN(1, "No more dynamic states available for CPU hotplug\n");
1530 return -ENOSPC;
1531}
1532
1533static int cpuhp_store_callbacks(enum cpuhp_state state, const char *name,
1534 int (*startup)(unsigned int cpu),
1535 int (*teardown)(unsigned int cpu),
1536 bool multi_instance)
5b7aa87e
TG
1537{
1538 /* (Un)Install the callbacks for further cpu hotplug operations */
1539 struct cpuhp_step *sp;
dc280d93 1540 int ret = 0;
5b7aa87e 1541
0c96b273
EB
1542 /*
1543 * If name is NULL, then the state gets removed.
1544 *
1545 * CPUHP_AP_ONLINE_DYN and CPUHP_BP_PREPARE_DYN are handed out on
1546 * the first allocation from these dynamic ranges, so the removal
1547 * would trigger a new allocation and clear the wrong (already
1548 * empty) state, leaving the callbacks of the to be cleared state
1549 * dangling, which causes wreckage on the next hotplug operation.
1550 */
1551 if (name && (state == CPUHP_AP_ONLINE_DYN ||
1552 state == CPUHP_BP_PREPARE_DYN)) {
dc280d93
TG
1553 ret = cpuhp_reserve_state(state);
1554 if (ret < 0)
dc434e05 1555 return ret;
dc280d93
TG
1556 state = ret;
1557 }
5b7aa87e 1558 sp = cpuhp_get_step(state);
dc434e05
SAS
1559 if (name && sp->name)
1560 return -EBUSY;
1561
3c1627e9
TG
1562 sp->startup.single = startup;
1563 sp->teardown.single = teardown;
5b7aa87e 1564 sp->name = name;
cf392d10
TG
1565 sp->multi_instance = multi_instance;
1566 INIT_HLIST_HEAD(&sp->list);
dc280d93 1567 return ret;
5b7aa87e
TG
1568}
1569
1570static void *cpuhp_get_teardown_cb(enum cpuhp_state state)
1571{
3c1627e9 1572 return cpuhp_get_step(state)->teardown.single;
5b7aa87e
TG
1573}
1574
5b7aa87e
TG
1575/*
1576 * Call the startup/teardown function for a step either on the AP or
1577 * on the current CPU.
1578 */
cf392d10
TG
1579static int cpuhp_issue_call(int cpu, enum cpuhp_state state, bool bringup,
1580 struct hlist_node *node)
5b7aa87e 1581{
a724632c 1582 struct cpuhp_step *sp = cpuhp_get_step(state);
5b7aa87e
TG
1583 int ret;
1584
4dddfb5f
PZ
1585 /*
1586 * If there's nothing to do, we done.
1587 * Relies on the union for multi_instance.
1588 */
3c1627e9
TG
1589 if ((bringup && !sp->startup.single) ||
1590 (!bringup && !sp->teardown.single))
5b7aa87e 1591 return 0;
5b7aa87e
TG
1592 /*
1593 * The non AP bound callbacks can fail on bringup. On teardown
1594 * e.g. module removal we crash for now.
1595 */
1cf4f629
TG
1596#ifdef CONFIG_SMP
1597 if (cpuhp_is_ap_state(state))
cf392d10 1598 ret = cpuhp_invoke_ap_callback(cpu, state, bringup, node);
1cf4f629 1599 else
96abb968 1600 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1cf4f629 1601#else
96abb968 1602 ret = cpuhp_invoke_callback(cpu, state, bringup, node, NULL);
1cf4f629 1603#endif
5b7aa87e
TG
1604 BUG_ON(ret && !bringup);
1605 return ret;
1606}
1607
1608/*
1609 * Called from __cpuhp_setup_state on a recoverable failure.
1610 *
1611 * Note: The teardown callbacks for rollback are not allowed to fail!
1612 */
1613static void cpuhp_rollback_install(int failedcpu, enum cpuhp_state state,
cf392d10 1614 struct hlist_node *node)
5b7aa87e
TG
1615{
1616 int cpu;
1617
5b7aa87e
TG
1618 /* Roll back the already executed steps on the other cpus */
1619 for_each_present_cpu(cpu) {
1620 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1621 int cpustate = st->state;
1622
1623 if (cpu >= failedcpu)
1624 break;
1625
1626 /* Did we invoke the startup call on that cpu ? */
1627 if (cpustate >= state)
cf392d10 1628 cpuhp_issue_call(cpu, state, false, node);
5b7aa87e
TG
1629 }
1630}
1631
9805c673
TG
1632int __cpuhp_state_add_instance_cpuslocked(enum cpuhp_state state,
1633 struct hlist_node *node,
1634 bool invoke)
cf392d10
TG
1635{
1636 struct cpuhp_step *sp;
1637 int cpu;
1638 int ret;
1639
9805c673
TG
1640 lockdep_assert_cpus_held();
1641
cf392d10
TG
1642 sp = cpuhp_get_step(state);
1643 if (sp->multi_instance == false)
1644 return -EINVAL;
1645
dc434e05 1646 mutex_lock(&cpuhp_state_mutex);
cf392d10 1647
3c1627e9 1648 if (!invoke || !sp->startup.multi)
cf392d10
TG
1649 goto add_node;
1650
1651 /*
1652 * Try to call the startup callback for each present cpu
1653 * depending on the hotplug state of the cpu.
1654 */
1655 for_each_present_cpu(cpu) {
1656 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1657 int cpustate = st->state;
1658
1659 if (cpustate < state)
1660 continue;
1661
1662 ret = cpuhp_issue_call(cpu, state, true, node);
1663 if (ret) {
3c1627e9 1664 if (sp->teardown.multi)
cf392d10 1665 cpuhp_rollback_install(cpu, state, node);
dc434e05 1666 goto unlock;
cf392d10
TG
1667 }
1668 }
1669add_node:
1670 ret = 0;
cf392d10 1671 hlist_add_head(node, &sp->list);
dc434e05 1672unlock:
cf392d10 1673 mutex_unlock(&cpuhp_state_mutex);
9805c673
TG
1674 return ret;
1675}
1676
1677int __cpuhp_state_add_instance(enum cpuhp_state state, struct hlist_node *node,
1678 bool invoke)
1679{
1680 int ret;
1681
1682 cpus_read_lock();
1683 ret = __cpuhp_state_add_instance_cpuslocked(state, node, invoke);
8f553c49 1684 cpus_read_unlock();
cf392d10
TG
1685 return ret;
1686}
1687EXPORT_SYMBOL_GPL(__cpuhp_state_add_instance);
1688
5b7aa87e 1689/**
71def423 1690 * __cpuhp_setup_state_cpuslocked - Setup the callbacks for an hotplug machine state
dc280d93
TG
1691 * @state: The state to setup
1692 * @invoke: If true, the startup function is invoked for cpus where
1693 * cpu state >= @state
1694 * @startup: startup callback function
1695 * @teardown: teardown callback function
1696 * @multi_instance: State is set up for multiple instances which get
1697 * added afterwards.
5b7aa87e 1698 *
71def423 1699 * The caller needs to hold cpus read locked while calling this function.
512f0980
BO
1700 * Returns:
1701 * On success:
1702 * Positive state number if @state is CPUHP_AP_ONLINE_DYN
1703 * 0 for all other states
1704 * On failure: proper (negative) error code
5b7aa87e 1705 */
71def423
SAS
1706int __cpuhp_setup_state_cpuslocked(enum cpuhp_state state,
1707 const char *name, bool invoke,
1708 int (*startup)(unsigned int cpu),
1709 int (*teardown)(unsigned int cpu),
1710 bool multi_instance)
5b7aa87e
TG
1711{
1712 int cpu, ret = 0;
b9d9d691 1713 bool dynstate;
5b7aa87e 1714
71def423
SAS
1715 lockdep_assert_cpus_held();
1716
5b7aa87e
TG
1717 if (cpuhp_cb_check(state) || !name)
1718 return -EINVAL;
1719
dc434e05 1720 mutex_lock(&cpuhp_state_mutex);
5b7aa87e 1721
dc280d93
TG
1722 ret = cpuhp_store_callbacks(state, name, startup, teardown,
1723 multi_instance);
5b7aa87e 1724
b9d9d691
TG
1725 dynstate = state == CPUHP_AP_ONLINE_DYN;
1726 if (ret > 0 && dynstate) {
1727 state = ret;
1728 ret = 0;
1729 }
1730
dc280d93 1731 if (ret || !invoke || !startup)
5b7aa87e
TG
1732 goto out;
1733
1734 /*
1735 * Try to call the startup callback for each present cpu
1736 * depending on the hotplug state of the cpu.
1737 */
1738 for_each_present_cpu(cpu) {
1739 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1740 int cpustate = st->state;
1741
1742 if (cpustate < state)
1743 continue;
1744
cf392d10 1745 ret = cpuhp_issue_call(cpu, state, true, NULL);
5b7aa87e 1746 if (ret) {
a724632c 1747 if (teardown)
cf392d10
TG
1748 cpuhp_rollback_install(cpu, state, NULL);
1749 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
5b7aa87e
TG
1750 goto out;
1751 }
1752 }
1753out:
dc434e05 1754 mutex_unlock(&cpuhp_state_mutex);
dc280d93
TG
1755 /*
1756 * If the requested state is CPUHP_AP_ONLINE_DYN, return the
1757 * dynamically allocated state in case of success.
1758 */
b9d9d691 1759 if (!ret && dynstate)
5b7aa87e
TG
1760 return state;
1761 return ret;
1762}
71def423
SAS
1763EXPORT_SYMBOL(__cpuhp_setup_state_cpuslocked);
1764
1765int __cpuhp_setup_state(enum cpuhp_state state,
1766 const char *name, bool invoke,
1767 int (*startup)(unsigned int cpu),
1768 int (*teardown)(unsigned int cpu),
1769 bool multi_instance)
1770{
1771 int ret;
1772
1773 cpus_read_lock();
1774 ret = __cpuhp_setup_state_cpuslocked(state, name, invoke, startup,
1775 teardown, multi_instance);
1776 cpus_read_unlock();
1777 return ret;
1778}
5b7aa87e
TG
1779EXPORT_SYMBOL(__cpuhp_setup_state);
1780
cf392d10
TG
1781int __cpuhp_state_remove_instance(enum cpuhp_state state,
1782 struct hlist_node *node, bool invoke)
1783{
1784 struct cpuhp_step *sp = cpuhp_get_step(state);
1785 int cpu;
1786
1787 BUG_ON(cpuhp_cb_check(state));
1788
1789 if (!sp->multi_instance)
1790 return -EINVAL;
1791
8f553c49 1792 cpus_read_lock();
dc434e05
SAS
1793 mutex_lock(&cpuhp_state_mutex);
1794
cf392d10
TG
1795 if (!invoke || !cpuhp_get_teardown_cb(state))
1796 goto remove;
1797 /*
1798 * Call the teardown callback for each present cpu depending
1799 * on the hotplug state of the cpu. This function is not
1800 * allowed to fail currently!
1801 */
1802 for_each_present_cpu(cpu) {
1803 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1804 int cpustate = st->state;
1805
1806 if (cpustate >= state)
1807 cpuhp_issue_call(cpu, state, false, node);
1808 }
1809
1810remove:
cf392d10
TG
1811 hlist_del(node);
1812 mutex_unlock(&cpuhp_state_mutex);
8f553c49 1813 cpus_read_unlock();
cf392d10
TG
1814
1815 return 0;
1816}
1817EXPORT_SYMBOL_GPL(__cpuhp_state_remove_instance);
dc434e05 1818
5b7aa87e 1819/**
71def423 1820 * __cpuhp_remove_state_cpuslocked - Remove the callbacks for an hotplug machine state
5b7aa87e
TG
1821 * @state: The state to remove
1822 * @invoke: If true, the teardown function is invoked for cpus where
1823 * cpu state >= @state
1824 *
71def423 1825 * The caller needs to hold cpus read locked while calling this function.
5b7aa87e
TG
1826 * The teardown callback is currently not allowed to fail. Think
1827 * about module removal!
1828 */
71def423 1829void __cpuhp_remove_state_cpuslocked(enum cpuhp_state state, bool invoke)
5b7aa87e 1830{
cf392d10 1831 struct cpuhp_step *sp = cpuhp_get_step(state);
5b7aa87e
TG
1832 int cpu;
1833
1834 BUG_ON(cpuhp_cb_check(state));
1835
71def423 1836 lockdep_assert_cpus_held();
5b7aa87e 1837
dc434e05 1838 mutex_lock(&cpuhp_state_mutex);
cf392d10
TG
1839 if (sp->multi_instance) {
1840 WARN(!hlist_empty(&sp->list),
1841 "Error: Removing state %d which has instances left.\n",
1842 state);
1843 goto remove;
1844 }
1845
a724632c 1846 if (!invoke || !cpuhp_get_teardown_cb(state))
5b7aa87e
TG
1847 goto remove;
1848
1849 /*
1850 * Call the teardown callback for each present cpu depending
1851 * on the hotplug state of the cpu. This function is not
1852 * allowed to fail currently!
1853 */
1854 for_each_present_cpu(cpu) {
1855 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
1856 int cpustate = st->state;
1857
1858 if (cpustate >= state)
cf392d10 1859 cpuhp_issue_call(cpu, state, false, NULL);
5b7aa87e
TG
1860 }
1861remove:
cf392d10 1862 cpuhp_store_callbacks(state, NULL, NULL, NULL, false);
dc434e05 1863 mutex_unlock(&cpuhp_state_mutex);
71def423
SAS
1864}
1865EXPORT_SYMBOL(__cpuhp_remove_state_cpuslocked);
1866
1867void __cpuhp_remove_state(enum cpuhp_state state, bool invoke)
1868{
1869 cpus_read_lock();
1870 __cpuhp_remove_state_cpuslocked(state, invoke);
8f553c49 1871 cpus_read_unlock();
5b7aa87e
TG
1872}
1873EXPORT_SYMBOL(__cpuhp_remove_state);
1874
98f8cdce
TG
1875#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
1876static ssize_t show_cpuhp_state(struct device *dev,
1877 struct device_attribute *attr, char *buf)
1878{
1879 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1880
1881 return sprintf(buf, "%d\n", st->state);
1882}
1883static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
1884
757c989b
TG
1885static ssize_t write_cpuhp_target(struct device *dev,
1886 struct device_attribute *attr,
1887 const char *buf, size_t count)
1888{
1889 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1890 struct cpuhp_step *sp;
1891 int target, ret;
1892
1893 ret = kstrtoint(buf, 10, &target);
1894 if (ret)
1895 return ret;
1896
1897#ifdef CONFIG_CPU_HOTPLUG_STATE_CONTROL
1898 if (target < CPUHP_OFFLINE || target > CPUHP_ONLINE)
1899 return -EINVAL;
1900#else
1901 if (target != CPUHP_OFFLINE && target != CPUHP_ONLINE)
1902 return -EINVAL;
1903#endif
1904
1905 ret = lock_device_hotplug_sysfs();
1906 if (ret)
1907 return ret;
1908
1909 mutex_lock(&cpuhp_state_mutex);
1910 sp = cpuhp_get_step(target);
1911 ret = !sp->name || sp->cant_stop ? -EINVAL : 0;
1912 mutex_unlock(&cpuhp_state_mutex);
1913 if (ret)
40da1b11 1914 goto out;
757c989b
TG
1915
1916 if (st->state < target)
1917 ret = do_cpu_up(dev->id, target);
1918 else
1919 ret = do_cpu_down(dev->id, target);
40da1b11 1920out:
757c989b
TG
1921 unlock_device_hotplug();
1922 return ret ? ret : count;
1923}
1924
98f8cdce
TG
1925static ssize_t show_cpuhp_target(struct device *dev,
1926 struct device_attribute *attr, char *buf)
1927{
1928 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1929
1930 return sprintf(buf, "%d\n", st->target);
1931}
757c989b 1932static DEVICE_ATTR(target, 0644, show_cpuhp_target, write_cpuhp_target);
98f8cdce 1933
1db49484
PZ
1934
1935static ssize_t write_cpuhp_fail(struct device *dev,
1936 struct device_attribute *attr,
1937 const char *buf, size_t count)
1938{
1939 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1940 struct cpuhp_step *sp;
1941 int fail, ret;
1942
1943 ret = kstrtoint(buf, 10, &fail);
1944 if (ret)
1945 return ret;
1946
1947 /*
1948 * Cannot fail STARTING/DYING callbacks.
1949 */
1950 if (cpuhp_is_atomic_state(fail))
1951 return -EINVAL;
1952
1953 /*
1954 * Cannot fail anything that doesn't have callbacks.
1955 */
1956 mutex_lock(&cpuhp_state_mutex);
1957 sp = cpuhp_get_step(fail);
1958 if (!sp->startup.single && !sp->teardown.single)
1959 ret = -EINVAL;
1960 mutex_unlock(&cpuhp_state_mutex);
1961 if (ret)
1962 return ret;
1963
1964 st->fail = fail;
1965
1966 return count;
1967}
1968
1969static ssize_t show_cpuhp_fail(struct device *dev,
1970 struct device_attribute *attr, char *buf)
1971{
1972 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
1973
1974 return sprintf(buf, "%d\n", st->fail);
1975}
1976
1977static DEVICE_ATTR(fail, 0644, show_cpuhp_fail, write_cpuhp_fail);
1978
98f8cdce
TG
1979static struct attribute *cpuhp_cpu_attrs[] = {
1980 &dev_attr_state.attr,
1981 &dev_attr_target.attr,
1db49484 1982 &dev_attr_fail.attr,
98f8cdce
TG
1983 NULL
1984};
1985
993647a2 1986static const struct attribute_group cpuhp_cpu_attr_group = {
98f8cdce
TG
1987 .attrs = cpuhp_cpu_attrs,
1988 .name = "hotplug",
1989 NULL
1990};
1991
1992static ssize_t show_cpuhp_states(struct device *dev,
1993 struct device_attribute *attr, char *buf)
1994{
1995 ssize_t cur, res = 0;
1996 int i;
1997
1998 mutex_lock(&cpuhp_state_mutex);
757c989b 1999 for (i = CPUHP_OFFLINE; i <= CPUHP_ONLINE; i++) {
98f8cdce
TG
2000 struct cpuhp_step *sp = cpuhp_get_step(i);
2001
2002 if (sp->name) {
2003 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
2004 buf += cur;
2005 res += cur;
2006 }
2007 }
2008 mutex_unlock(&cpuhp_state_mutex);
2009 return res;
2010}
2011static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
2012
2013static struct attribute *cpuhp_cpu_root_attrs[] = {
2014 &dev_attr_states.attr,
2015 NULL
2016};
2017
993647a2 2018static const struct attribute_group cpuhp_cpu_root_attr_group = {
98f8cdce
TG
2019 .attrs = cpuhp_cpu_root_attrs,
2020 .name = "hotplug",
2021 NULL
2022};
2023
47b4c679
TG
2024#ifdef CONFIG_HOTPLUG_SMT
2025
2026static const char *smt_states[] = {
2027 [CPU_SMT_ENABLED] = "on",
2028 [CPU_SMT_DISABLED] = "off",
2029 [CPU_SMT_FORCE_DISABLED] = "forceoff",
2030 [CPU_SMT_NOT_SUPPORTED] = "notsupported",
2031};
2032
2033static ssize_t
2034show_smt_control(struct device *dev, struct device_attribute *attr, char *buf)
2035{
2036 return snprintf(buf, PAGE_SIZE - 2, "%s\n", smt_states[cpu_smt_control]);
2037}
2038
2039static void cpuhp_offline_cpu_device(unsigned int cpu)
2040{
2041 struct device *dev = get_cpu_device(cpu);
2042
2043 dev->offline = true;
2044 /* Tell user space about the state change */
2045 kobject_uevent(&dev->kobj, KOBJ_OFFLINE);
2046}
2047
b582c9f2
TG
2048static void cpuhp_online_cpu_device(unsigned int cpu)
2049{
2050 struct device *dev = get_cpu_device(cpu);
2051
2052 dev->offline = false;
2053 /* Tell user space about the state change */
2054 kobject_uevent(&dev->kobj, KOBJ_ONLINE);
2055}
2056
47b4c679
TG
2057static int cpuhp_smt_disable(enum cpuhp_smt_control ctrlval)
2058{
2059 int cpu, ret = 0;
2060
2061 cpu_maps_update_begin();
2062 for_each_online_cpu(cpu) {
2063 if (topology_is_primary_thread(cpu))
2064 continue;
2065 ret = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
2066 if (ret)
2067 break;
2068 /*
2069 * As this needs to hold the cpu maps lock it's impossible
2070 * to call device_offline() because that ends up calling
2071 * cpu_down() which takes cpu maps lock. cpu maps lock
2072 * needs to be held as this might race against in kernel
2073 * abusers of the hotplug machinery (thermal management).
2074 *
2075 * So nothing would update device:offline state. That would
2076 * leave the sysfs entry stale and prevent onlining after
2077 * smt control has been changed to 'off' again. This is
2078 * called under the sysfs hotplug lock, so it is properly
2079 * serialized against the regular offline usage.
2080 */
2081 cpuhp_offline_cpu_device(cpu);
2082 }
7011f443 2083 if (!ret) {
47b4c679 2084 cpu_smt_control = ctrlval;
7011f443
JK
2085 arch_smt_update();
2086 }
47b4c679
TG
2087 cpu_maps_update_done();
2088 return ret;
2089}
2090
b582c9f2 2091static int cpuhp_smt_enable(void)
47b4c679 2092{
b582c9f2
TG
2093 int cpu, ret = 0;
2094
47b4c679
TG
2095 cpu_maps_update_begin();
2096 cpu_smt_control = CPU_SMT_ENABLED;
7011f443 2097 arch_smt_update();
b582c9f2
TG
2098 for_each_present_cpu(cpu) {
2099 /* Skip online CPUs and CPUs on offline nodes */
2100 if (cpu_online(cpu) || !node_online(cpu_to_node(cpu)))
2101 continue;
2102 ret = _cpu_up(cpu, 0, CPUHP_ONLINE);
2103 if (ret)
2104 break;
2105 /* See comment in cpuhp_smt_disable() */
2106 cpuhp_online_cpu_device(cpu);
2107 }
47b4c679 2108 cpu_maps_update_done();
b582c9f2 2109 return ret;
47b4c679
TG
2110}
2111
2112static ssize_t
2113store_smt_control(struct device *dev, struct device_attribute *attr,
2114 const char *buf, size_t count)
2115{
2116 int ctrlval, ret;
2117
2118 if (sysfs_streq(buf, "on"))
2119 ctrlval = CPU_SMT_ENABLED;
2120 else if (sysfs_streq(buf, "off"))
2121 ctrlval = CPU_SMT_DISABLED;
2122 else if (sysfs_streq(buf, "forceoff"))
2123 ctrlval = CPU_SMT_FORCE_DISABLED;
2124 else
2125 return -EINVAL;
2126
2127 if (cpu_smt_control == CPU_SMT_FORCE_DISABLED)
2128 return -EPERM;
2129
2130 if (cpu_smt_control == CPU_SMT_NOT_SUPPORTED)
2131 return -ENODEV;
2132
2133 ret = lock_device_hotplug_sysfs();
2134 if (ret)
2135 return ret;
2136
2137 if (ctrlval != cpu_smt_control) {
2138 switch (ctrlval) {
2139 case CPU_SMT_ENABLED:
b582c9f2 2140 ret = cpuhp_smt_enable();
47b4c679
TG
2141 break;
2142 case CPU_SMT_DISABLED:
2143 case CPU_SMT_FORCE_DISABLED:
2144 ret = cpuhp_smt_disable(ctrlval);
2145 break;
2146 }
2147 }
2148
2149 unlock_device_hotplug();
2150 return ret ? ret : count;
2151}
2152static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
2153
2154static ssize_t
2155show_smt_active(struct device *dev, struct device_attribute *attr, char *buf)
2156{
2157 bool active = topology_max_smt_threads() > 1;
2158
2159 return snprintf(buf, PAGE_SIZE - 2, "%d\n", active);
2160}
2161static DEVICE_ATTR(active, 0444, show_smt_active, NULL);
2162
2163static struct attribute *cpuhp_smt_attrs[] = {
2164 &dev_attr_control.attr,
2165 &dev_attr_active.attr,
2166 NULL
2167};
2168
2169static const struct attribute_group cpuhp_smt_attr_group = {
2170 .attrs = cpuhp_smt_attrs,
2171 .name = "smt",
2172 NULL
2173};
2174
2175static int __init cpu_smt_state_init(void)
2176{
47b4c679
TG
2177 return sysfs_create_group(&cpu_subsys.dev_root->kobj,
2178 &cpuhp_smt_attr_group);
2179}
2180
2181#else
2182static inline int cpu_smt_state_init(void) { return 0; }
2183#endif
2184
98f8cdce
TG
2185static int __init cpuhp_sysfs_init(void)
2186{
2187 int cpu, ret;
2188
47b4c679
TG
2189 ret = cpu_smt_state_init();
2190 if (ret)
2191 return ret;
2192
98f8cdce
TG
2193 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
2194 &cpuhp_cpu_root_attr_group);
2195 if (ret)
2196 return ret;
2197
2198 for_each_possible_cpu(cpu) {
2199 struct device *dev = get_cpu_device(cpu);
2200
2201 if (!dev)
2202 continue;
2203 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
2204 if (ret)
2205 return ret;
2206 }
2207 return 0;
2208}
2209device_initcall(cpuhp_sysfs_init);
2210#endif
2211
e56b3bc7
LT
2212/*
2213 * cpu_bit_bitmap[] is a special, "compressed" data structure that
2214 * represents all NR_CPUS bits binary values of 1<<nr.
2215 *
e0b582ec 2216 * It is used by cpumask_of() to get a constant address to a CPU
e56b3bc7
LT
2217 * mask value that has a single bit set only.
2218 */
b8d317d1 2219
e56b3bc7 2220/* cpu_bit_bitmap[0] is empty - so we can back into it */
4d51985e 2221#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
e56b3bc7
LT
2222#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
2223#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
2224#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
b8d317d1 2225
e56b3bc7
LT
2226const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
2227
2228 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
2229 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
2230#if BITS_PER_LONG > 32
2231 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
2232 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
b8d317d1
MT
2233#endif
2234};
e56b3bc7 2235EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2d3854a3
RR
2236
2237const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
2238EXPORT_SYMBOL(cpu_all_bits);
b3199c02
RR
2239
2240#ifdef CONFIG_INIT_ALL_POSSIBLE
4b804c85 2241struct cpumask __cpu_possible_mask __read_mostly
c4c54dd1 2242 = {CPU_BITS_ALL};
b3199c02 2243#else
4b804c85 2244struct cpumask __cpu_possible_mask __read_mostly;
b3199c02 2245#endif
4b804c85 2246EXPORT_SYMBOL(__cpu_possible_mask);
b3199c02 2247
4b804c85
RV
2248struct cpumask __cpu_online_mask __read_mostly;
2249EXPORT_SYMBOL(__cpu_online_mask);
b3199c02 2250
4b804c85
RV
2251struct cpumask __cpu_present_mask __read_mostly;
2252EXPORT_SYMBOL(__cpu_present_mask);
b3199c02 2253
4b804c85
RV
2254struct cpumask __cpu_active_mask __read_mostly;
2255EXPORT_SYMBOL(__cpu_active_mask);
3fa41520 2256
3fa41520
RR
2257void init_cpu_present(const struct cpumask *src)
2258{
c4c54dd1 2259 cpumask_copy(&__cpu_present_mask, src);
3fa41520
RR
2260}
2261
2262void init_cpu_possible(const struct cpumask *src)
2263{
c4c54dd1 2264 cpumask_copy(&__cpu_possible_mask, src);
3fa41520
RR
2265}
2266
2267void init_cpu_online(const struct cpumask *src)
2268{
c4c54dd1 2269 cpumask_copy(&__cpu_online_mask, src);
3fa41520 2270}
cff7d378
TG
2271
2272/*
2273 * Activate the first processor.
2274 */
2275void __init boot_cpu_init(void)
2276{
2277 int cpu = smp_processor_id();
2278
2279 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
2280 set_cpu_online(cpu, true);
2281 set_cpu_active(cpu, true);
2282 set_cpu_present(cpu, true);
2283 set_cpu_possible(cpu, true);
8ce371f9
PZ
2284
2285#ifdef CONFIG_SMP
2286 __boot_cpu_id = cpu;
2287#endif
cff7d378
TG
2288}
2289
2290/*
2291 * Must be called _AFTER_ setting up the per_cpu areas
2292 */
d403c57a 2293void __init boot_cpu_hotplug_init(void)
cff7d378 2294{
59b2bd3d 2295#ifdef CONFIG_SMP
c1f7608e 2296 per_cpu_ptr(&cpuhp_state, smp_processor_id())->booted_once = true;
59b2bd3d 2297#endif
c1f7608e 2298 per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
cff7d378 2299}
658dad68
JP
2300
2301enum cpu_mitigations cpu_mitigations __ro_after_init = CPU_MITIGATIONS_AUTO;
2302
2303static int __init mitigations_parse_cmdline(char *arg)
2304{
2305 if (!strcmp(arg, "off"))
2306 cpu_mitigations = CPU_MITIGATIONS_OFF;
2307 else if (!strcmp(arg, "auto"))
2308 cpu_mitigations = CPU_MITIGATIONS_AUTO;
2309 else if (!strcmp(arg, "auto,nosmt"))
2310 cpu_mitigations = CPU_MITIGATIONS_AUTO_NOSMT;
2311
2312 return 0;
2313}
2314early_param("mitigations", mitigations_parse_cmdline);