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