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