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