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