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