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