cpu/hotplug: Add sysfs state interface
[linux-2.6-block.git] / kernel / cpu.c
CommitLineData
1da177e4
LT
1/* CPU control.
2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
3 *
4 * This code is licenced under the GPL.
5 */
6#include <linux/proc_fs.h>
7#include <linux/smp.h>
8#include <linux/init.h>
9#include <linux/notifier.h>
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>
cff7d378 25
bb3632c6 26#include <trace/events/power.h>
cff7d378
TG
27#define CREATE_TRACE_POINTS
28#include <trace/events/cpuhp.h>
1da177e4 29
38498a67
TG
30#include "smpboot.h"
31
cff7d378
TG
32/**
33 * cpuhp_cpu_state - Per cpu hotplug state storage
34 * @state: The current cpu state
35 * @target: The target state
36 */
37struct cpuhp_cpu_state {
38 enum cpuhp_state state;
39 enum cpuhp_state target;
40};
41
42static DEFINE_PER_CPU(struct cpuhp_cpu_state, cpuhp_state);
43
44/**
45 * cpuhp_step - Hotplug state machine step
46 * @name: Name of the step
47 * @startup: Startup function of the step
48 * @teardown: Teardown function of the step
49 * @skip_onerr: Do not invoke the functions on error rollback
50 * Will go away once the notifiers are gone
51 */
52struct cpuhp_step {
53 const char *name;
54 int (*startup)(unsigned int cpu);
55 int (*teardown)(unsigned int cpu);
56 bool skip_onerr;
57};
58
98f8cdce 59static DEFINE_MUTEX(cpuhp_state_mutex);
cff7d378 60static struct cpuhp_step cpuhp_bp_states[];
4baa0afc 61static struct cpuhp_step cpuhp_ap_states[];
cff7d378
TG
62
63/**
64 * cpuhp_invoke_callback _ Invoke the callbacks for a given state
65 * @cpu: The cpu for which the callback should be invoked
66 * @step: The step in the state machine
67 * @cb: The callback function to invoke
68 *
69 * Called from cpu hotplug and from the state register machinery
70 */
71static int cpuhp_invoke_callback(unsigned int cpu, enum cpuhp_state step,
72 int (*cb)(unsigned int))
73{
74 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
75 int ret = 0;
76
77 if (cb) {
78 trace_cpuhp_enter(cpu, st->target, step, cb);
79 ret = cb(cpu);
80 trace_cpuhp_exit(cpu, st->state, step, ret);
81 }
82 return ret;
83}
84
98a79d6a 85#ifdef CONFIG_SMP
b3199c02 86/* Serializes the updates to cpu_online_mask, cpu_present_mask */
aa953877 87static DEFINE_MUTEX(cpu_add_remove_lock);
090e77c3
TG
88bool cpuhp_tasks_frozen;
89EXPORT_SYMBOL_GPL(cpuhp_tasks_frozen);
1da177e4 90
79a6cdeb 91/*
93ae4f97
SB
92 * The following two APIs (cpu_maps_update_begin/done) must be used when
93 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
94 * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
95 * hotplug callback (un)registration performed using __register_cpu_notifier()
96 * or __unregister_cpu_notifier().
79a6cdeb
LJ
97 */
98void cpu_maps_update_begin(void)
99{
100 mutex_lock(&cpu_add_remove_lock);
101}
93ae4f97 102EXPORT_SYMBOL(cpu_notifier_register_begin);
79a6cdeb
LJ
103
104void cpu_maps_update_done(void)
105{
106 mutex_unlock(&cpu_add_remove_lock);
107}
93ae4f97 108EXPORT_SYMBOL(cpu_notifier_register_done);
79a6cdeb 109
5c113fbe 110static RAW_NOTIFIER_HEAD(cpu_chain);
1da177e4 111
e3920fb4
RW
112/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
113 * Should always be manipulated under cpu_add_remove_lock
114 */
115static int cpu_hotplug_disabled;
116
79a6cdeb
LJ
117#ifdef CONFIG_HOTPLUG_CPU
118
d221938c
GS
119static struct {
120 struct task_struct *active_writer;
87af9e7f
DH
121 /* wait queue to wake up the active_writer */
122 wait_queue_head_t wq;
123 /* verifies that no writer will get active while readers are active */
124 struct mutex lock;
d221938c
GS
125 /*
126 * Also blocks the new readers during
127 * an ongoing cpu hotplug operation.
128 */
87af9e7f 129 atomic_t refcount;
a19423b9
GS
130
131#ifdef CONFIG_DEBUG_LOCK_ALLOC
132 struct lockdep_map dep_map;
133#endif
31950eb6
LT
134} cpu_hotplug = {
135 .active_writer = NULL,
87af9e7f 136 .wq = __WAIT_QUEUE_HEAD_INITIALIZER(cpu_hotplug.wq),
31950eb6 137 .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
a19423b9
GS
138#ifdef CONFIG_DEBUG_LOCK_ALLOC
139 .dep_map = {.name = "cpu_hotplug.lock" },
140#endif
31950eb6 141};
d221938c 142
a19423b9
GS
143/* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
144#define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
dd56af42
PM
145#define cpuhp_lock_acquire_tryread() \
146 lock_map_acquire_tryread(&cpu_hotplug.dep_map)
a19423b9
GS
147#define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
148#define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
149
62db99f4 150
86ef5c9a 151void get_online_cpus(void)
a9d9baa1 152{
d221938c
GS
153 might_sleep();
154 if (cpu_hotplug.active_writer == current)
aa953877 155 return;
a19423b9 156 cpuhp_lock_acquire_read();
d221938c 157 mutex_lock(&cpu_hotplug.lock);
87af9e7f 158 atomic_inc(&cpu_hotplug.refcount);
d221938c 159 mutex_unlock(&cpu_hotplug.lock);
a9d9baa1 160}
86ef5c9a 161EXPORT_SYMBOL_GPL(get_online_cpus);
90d45d17 162
86ef5c9a 163void put_online_cpus(void)
a9d9baa1 164{
87af9e7f
DH
165 int refcount;
166
d221938c 167 if (cpu_hotplug.active_writer == current)
aa953877 168 return;
075663d1 169
87af9e7f
DH
170 refcount = atomic_dec_return(&cpu_hotplug.refcount);
171 if (WARN_ON(refcount < 0)) /* try to fix things up */
172 atomic_inc(&cpu_hotplug.refcount);
173
174 if (refcount <= 0 && waitqueue_active(&cpu_hotplug.wq))
175 wake_up(&cpu_hotplug.wq);
075663d1 176
a19423b9 177 cpuhp_lock_release();
d221938c 178
a9d9baa1 179}
86ef5c9a 180EXPORT_SYMBOL_GPL(put_online_cpus);
a9d9baa1 181
d221938c
GS
182/*
183 * This ensures that the hotplug operation can begin only when the
184 * refcount goes to zero.
185 *
186 * Note that during a cpu-hotplug operation, the new readers, if any,
187 * will be blocked by the cpu_hotplug.lock
188 *
d2ba7e2a
ON
189 * Since cpu_hotplug_begin() is always called after invoking
190 * cpu_maps_update_begin(), we can be sure that only one writer is active.
d221938c
GS
191 *
192 * Note that theoretically, there is a possibility of a livelock:
193 * - Refcount goes to zero, last reader wakes up the sleeping
194 * writer.
195 * - Last reader unlocks the cpu_hotplug.lock.
196 * - A new reader arrives at this moment, bumps up the refcount.
197 * - The writer acquires the cpu_hotplug.lock finds the refcount
198 * non zero and goes to sleep again.
199 *
200 * However, this is very difficult to achieve in practice since
86ef5c9a 201 * get_online_cpus() not an api which is called all that often.
d221938c
GS
202 *
203 */
b9d10be7 204void cpu_hotplug_begin(void)
d221938c 205{
87af9e7f 206 DEFINE_WAIT(wait);
d2ba7e2a 207
87af9e7f 208 cpu_hotplug.active_writer = current;
a19423b9 209 cpuhp_lock_acquire();
87af9e7f 210
d2ba7e2a
ON
211 for (;;) {
212 mutex_lock(&cpu_hotplug.lock);
87af9e7f
DH
213 prepare_to_wait(&cpu_hotplug.wq, &wait, TASK_UNINTERRUPTIBLE);
214 if (likely(!atomic_read(&cpu_hotplug.refcount)))
215 break;
d221938c
GS
216 mutex_unlock(&cpu_hotplug.lock);
217 schedule();
d221938c 218 }
87af9e7f 219 finish_wait(&cpu_hotplug.wq, &wait);
d221938c
GS
220}
221
b9d10be7 222void cpu_hotplug_done(void)
d221938c
GS
223{
224 cpu_hotplug.active_writer = NULL;
225 mutex_unlock(&cpu_hotplug.lock);
a19423b9 226 cpuhp_lock_release();
d221938c 227}
79a6cdeb 228
16e53dbf
SB
229/*
230 * Wait for currently running CPU hotplug operations to complete (if any) and
231 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
232 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
233 * hotplug path before performing hotplug operations. So acquiring that lock
234 * guarantees mutual exclusion from any currently running hotplug operations.
235 */
236void cpu_hotplug_disable(void)
237{
238 cpu_maps_update_begin();
89af7ba5 239 cpu_hotplug_disabled++;
16e53dbf
SB
240 cpu_maps_update_done();
241}
32145c46 242EXPORT_SYMBOL_GPL(cpu_hotplug_disable);
16e53dbf
SB
243
244void cpu_hotplug_enable(void)
245{
246 cpu_maps_update_begin();
89af7ba5 247 WARN_ON(--cpu_hotplug_disabled < 0);
16e53dbf
SB
248 cpu_maps_update_done();
249}
32145c46 250EXPORT_SYMBOL_GPL(cpu_hotplug_enable);
b9d10be7 251#endif /* CONFIG_HOTPLUG_CPU */
79a6cdeb 252
1da177e4 253/* Need to know about CPUs going up/down? */
71cf5aee 254int register_cpu_notifier(struct notifier_block *nb)
1da177e4 255{
bd5349cf 256 int ret;
d221938c 257 cpu_maps_update_begin();
bd5349cf 258 ret = raw_notifier_chain_register(&cpu_chain, nb);
d221938c 259 cpu_maps_update_done();
bd5349cf 260 return ret;
1da177e4 261}
65edc68c 262
71cf5aee 263int __register_cpu_notifier(struct notifier_block *nb)
93ae4f97
SB
264{
265 return raw_notifier_chain_register(&cpu_chain, nb);
266}
267
090e77c3 268static int __cpu_notify(unsigned long val, unsigned int cpu, int nr_to_call,
e9fb7631
AM
269 int *nr_calls)
270{
090e77c3
TG
271 unsigned long mod = cpuhp_tasks_frozen ? CPU_TASKS_FROZEN : 0;
272 void *hcpu = (void *)(long)cpu;
273
e6bde73b
AM
274 int ret;
275
090e77c3 276 ret = __raw_notifier_call_chain(&cpu_chain, val | mod, hcpu, nr_to_call,
e9fb7631 277 nr_calls);
e6bde73b
AM
278
279 return notifier_to_errno(ret);
e9fb7631
AM
280}
281
090e77c3 282static int cpu_notify(unsigned long val, unsigned int cpu)
e9fb7631 283{
090e77c3 284 return __cpu_notify(val, cpu, -1, NULL);
e9fb7631
AM
285}
286
ba997462
TG
287/* Notifier wrappers for transitioning to state machine */
288static int notify_prepare(unsigned int cpu)
289{
290 int nr_calls = 0;
291 int ret;
292
293 ret = __cpu_notify(CPU_UP_PREPARE, cpu, -1, &nr_calls);
294 if (ret) {
295 nr_calls--;
296 printk(KERN_WARNING "%s: attempt to bring up CPU %u failed\n",
297 __func__, cpu);
298 __cpu_notify(CPU_UP_CANCELED, cpu, nr_calls, NULL);
299 }
300 return ret;
301}
302
303static int notify_online(unsigned int cpu)
304{
305 cpu_notify(CPU_ONLINE, cpu);
306 return 0;
307}
308
4baa0afc
TG
309static int notify_starting(unsigned int cpu)
310{
311 cpu_notify(CPU_STARTING, cpu);
312 return 0;
313}
314
ba997462
TG
315static int bringup_cpu(unsigned int cpu)
316{
317 struct task_struct *idle = idle_thread_get(cpu);
318 int ret;
319
320 /* Arch-specific enabling code. */
321 ret = __cpu_up(cpu, idle);
322 if (ret) {
323 cpu_notify(CPU_UP_CANCELED, cpu);
324 return ret;
325 }
326 BUG_ON(!cpu_online(cpu));
327 return 0;
328}
329
00b9b0af 330#ifdef CONFIG_HOTPLUG_CPU
1da177e4 331EXPORT_SYMBOL(register_cpu_notifier);
93ae4f97 332EXPORT_SYMBOL(__register_cpu_notifier);
1da177e4 333
71cf5aee 334void unregister_cpu_notifier(struct notifier_block *nb)
1da177e4 335{
d221938c 336 cpu_maps_update_begin();
bd5349cf 337 raw_notifier_chain_unregister(&cpu_chain, nb);
d221938c 338 cpu_maps_update_done();
1da177e4
LT
339}
340EXPORT_SYMBOL(unregister_cpu_notifier);
341
71cf5aee 342void __unregister_cpu_notifier(struct notifier_block *nb)
93ae4f97
SB
343{
344 raw_notifier_chain_unregister(&cpu_chain, nb);
345}
346EXPORT_SYMBOL(__unregister_cpu_notifier);
347
e4cc2f87
AV
348/**
349 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
350 * @cpu: a CPU id
351 *
352 * This function walks all processes, finds a valid mm struct for each one and
353 * then clears a corresponding bit in mm's cpumask. While this all sounds
354 * trivial, there are various non-obvious corner cases, which this function
355 * tries to solve in a safe manner.
356 *
357 * Also note that the function uses a somewhat relaxed locking scheme, so it may
358 * be called only for an already offlined CPU.
359 */
cb79295e
AV
360void clear_tasks_mm_cpumask(int cpu)
361{
362 struct task_struct *p;
363
364 /*
365 * This function is called after the cpu is taken down and marked
366 * offline, so its not like new tasks will ever get this cpu set in
367 * their mm mask. -- Peter Zijlstra
368 * Thus, we may use rcu_read_lock() here, instead of grabbing
369 * full-fledged tasklist_lock.
370 */
e4cc2f87 371 WARN_ON(cpu_online(cpu));
cb79295e
AV
372 rcu_read_lock();
373 for_each_process(p) {
374 struct task_struct *t;
375
e4cc2f87
AV
376 /*
377 * Main thread might exit, but other threads may still have
378 * a valid mm. Find one.
379 */
cb79295e
AV
380 t = find_lock_task_mm(p);
381 if (!t)
382 continue;
383 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
384 task_unlock(t);
385 }
386 rcu_read_unlock();
387}
388
b728ca06 389static inline void check_for_tasks(int dead_cpu)
1da177e4 390{
b728ca06 391 struct task_struct *g, *p;
1da177e4 392
a75a6068
ON
393 read_lock(&tasklist_lock);
394 for_each_process_thread(g, p) {
b728ca06
KT
395 if (!p->on_rq)
396 continue;
397 /*
398 * We do the check with unlocked task_rq(p)->lock.
399 * Order the reading to do not warn about a task,
400 * which was running on this cpu in the past, and
401 * it's just been woken on another cpu.
402 */
403 rmb();
404 if (task_cpu(p) != dead_cpu)
405 continue;
406
407 pr_warn("Task %s (pid=%d) is on cpu %d (state=%ld, flags=%x)\n",
408 p->comm, task_pid_nr(p), dead_cpu, p->state, p->flags);
a75a6068
ON
409 }
410 read_unlock(&tasklist_lock);
1da177e4
LT
411}
412
98458172
TG
413static void cpu_notify_nofail(unsigned long val, unsigned int cpu)
414{
415 BUG_ON(cpu_notify(val, cpu));
416}
417
418static int notify_down_prepare(unsigned int cpu)
419{
420 int err, nr_calls = 0;
421
422 err = __cpu_notify(CPU_DOWN_PREPARE, cpu, -1, &nr_calls);
423 if (err) {
424 nr_calls--;
425 __cpu_notify(CPU_DOWN_FAILED, cpu, nr_calls, NULL);
426 pr_warn("%s: attempt to take down CPU %u failed\n",
427 __func__, cpu);
428 }
429 return err;
430}
431
4baa0afc
TG
432static int notify_dying(unsigned int cpu)
433{
434 cpu_notify(CPU_DYING, cpu);
435 return 0;
436}
437
1da177e4 438/* Take this CPU down. */
71cf5aee 439static int take_cpu_down(void *_param)
1da177e4 440{
4baa0afc
TG
441 struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
442 enum cpuhp_state target = max((int)st->target, CPUHP_AP_OFFLINE);
090e77c3 443 int err, cpu = smp_processor_id();
1da177e4 444
1da177e4
LT
445 /* Ensure this CPU doesn't handle any more interrupts. */
446 err = __cpu_disable();
447 if (err < 0)
f3705136 448 return err;
1da177e4 449
4baa0afc
TG
450 /* Invoke the former CPU_DYING callbacks */
451 for (; st->state > target; st->state--) {
452 struct cpuhp_step *step = cpuhp_ap_states + st->state;
453
454 cpuhp_invoke_callback(cpu, st->state, step->teardown);
455 }
52c063d1
TG
456 /* Give up timekeeping duties */
457 tick_handover_do_timer();
14e568e7 458 /* Park the stopper thread */
090e77c3 459 stop_machine_park(cpu);
f3705136 460 return 0;
1da177e4
LT
461}
462
98458172 463static int takedown_cpu(unsigned int cpu)
1da177e4 464{
98458172 465 int err;
1da177e4 466
6acce3ef
PZ
467 /*
468 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
469 * and RCU users of this state to go away such that all new such users
470 * will observe it.
471 *
472 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
779de6ce 473 * not imply sync_sched(), so wait for both.
106dd5af
M
474 *
475 * Do sync before park smpboot threads to take care the rcu boost case.
6acce3ef 476 */
779de6ce
PM
477 if (IS_ENABLED(CONFIG_PREEMPT))
478 synchronize_rcu_mult(call_rcu, call_rcu_sched);
479 else
480 synchronize_rcu();
6acce3ef 481
106dd5af
M
482 smpboot_park_threads(cpu);
483
6acce3ef 484 /*
a8994181
TG
485 * Prevent irq alloc/free while the dying cpu reorganizes the
486 * interrupt affinities.
6acce3ef 487 */
a8994181 488 irq_lock_sparse();
6acce3ef 489
a8994181
TG
490 /*
491 * So now all preempt/rcu users must observe !cpu_active().
492 */
090e77c3 493 err = stop_machine(take_cpu_down, NULL, cpumask_of(cpu));
04321587 494 if (err) {
1da177e4 495 /* CPU didn't die: tell everyone. Can't complain. */
090e77c3 496 cpu_notify_nofail(CPU_DOWN_FAILED, cpu);
a8994181 497 irq_unlock_sparse();
98458172 498 return err;
8fa1d7d3 499 }
04321587 500 BUG_ON(cpu_online(cpu));
1da177e4 501
48c5ccae
PZ
502 /*
503 * The migration_call() CPU_DYING callback will have removed all
504 * runnable tasks from the cpu, there's only the idle task left now
505 * that the migration thread is done doing the stop_machine thing.
51a96c77
PZ
506 *
507 * Wait for the stop thread to go away.
48c5ccae 508 */
528a25b0 509 while (!per_cpu(cpu_dead_idle, cpu))
51a96c77 510 cpu_relax();
528a25b0
PM
511 smp_mb(); /* Read from cpu_dead_idle before __cpu_die(). */
512 per_cpu(cpu_dead_idle, cpu) = false;
1da177e4 513
a8994181
TG
514 /* Interrupts are moved away from the dying cpu, reenable alloc/free */
515 irq_unlock_sparse();
516
345527b1 517 hotplug_cpu__broadcast_tick_pull(cpu);
1da177e4
LT
518 /* This actually kills the CPU. */
519 __cpu_die(cpu);
520
a49b116d 521 tick_cleanup_dead_cpu(cpu);
98458172
TG
522 return 0;
523}
1da177e4 524
98458172
TG
525static int notify_dead(unsigned int cpu)
526{
527 cpu_notify_nofail(CPU_DEAD, cpu);
1da177e4 528 check_for_tasks(cpu);
98458172
TG
529 return 0;
530}
531
cff7d378
TG
532#else
533#define notify_down_prepare NULL
534#define takedown_cpu NULL
535#define notify_dead NULL
4baa0afc 536#define notify_dying NULL
cff7d378
TG
537#endif
538
539#ifdef CONFIG_HOTPLUG_CPU
540static void undo_cpu_down(unsigned int cpu, struct cpuhp_cpu_state *st)
541{
542 for (st->state++; st->state < st->target; st->state++) {
543 struct cpuhp_step *step = cpuhp_bp_states + st->state;
544
545 if (!step->skip_onerr)
546 cpuhp_invoke_callback(cpu, st->state, step->startup);
547 }
548}
549
98458172 550/* Requires cpu_add_remove_lock to be held */
af1f4045
TG
551static int __ref _cpu_down(unsigned int cpu, int tasks_frozen,
552 enum cpuhp_state target)
98458172 553{
cff7d378
TG
554 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
555 int prev_state, ret = 0;
556 bool hasdied = false;
98458172
TG
557
558 if (num_online_cpus() == 1)
559 return -EBUSY;
560
561 if (!cpu_online(cpu))
562 return -EINVAL;
563
564 cpu_hotplug_begin();
565
566 cpuhp_tasks_frozen = tasks_frozen;
567
cff7d378 568 prev_state = st->state;
af1f4045 569 st->target = target;
cff7d378
TG
570 for (; st->state > st->target; st->state--) {
571 struct cpuhp_step *step = cpuhp_bp_states + st->state;
98458172 572
cff7d378
TG
573 ret = cpuhp_invoke_callback(cpu, st->state, step->teardown);
574 if (ret) {
575 st->target = prev_state;
576 undo_cpu_down(cpu, st);
577 break;
578 }
579 }
580 hasdied = prev_state != st->state && st->state == CPUHP_OFFLINE;
1da177e4 581
d221938c 582 cpu_hotplug_done();
cff7d378
TG
583 /* This post dead nonsense must die */
584 if (!ret && hasdied)
090e77c3 585 cpu_notify_nofail(CPU_POST_DEAD, cpu);
cff7d378 586 return ret;
e3920fb4
RW
587}
588
af1f4045 589static int do_cpu_down(unsigned int cpu, enum cpuhp_state target)
e3920fb4 590{
9ea09af3 591 int err;
e3920fb4 592
d221938c 593 cpu_maps_update_begin();
e761b772
MK
594
595 if (cpu_hotplug_disabled) {
e3920fb4 596 err = -EBUSY;
e761b772
MK
597 goto out;
598 }
599
af1f4045 600 err = _cpu_down(cpu, 0, target);
e3920fb4 601
e761b772 602out:
d221938c 603 cpu_maps_update_done();
1da177e4
LT
604 return err;
605}
af1f4045
TG
606int cpu_down(unsigned int cpu)
607{
608 return do_cpu_down(cpu, CPUHP_OFFLINE);
609}
b62b8ef9 610EXPORT_SYMBOL(cpu_down);
1da177e4
LT
611#endif /*CONFIG_HOTPLUG_CPU*/
612
00df35f9
PM
613/*
614 * Unpark per-CPU smpboot kthreads at CPU-online time.
615 */
616static int smpboot_thread_call(struct notifier_block *nfb,
617 unsigned long action, void *hcpu)
618{
619 int cpu = (long)hcpu;
620
621 switch (action & ~CPU_TASKS_FROZEN) {
622
64eaf974 623 case CPU_DOWN_FAILED:
00df35f9
PM
624 case CPU_ONLINE:
625 smpboot_unpark_threads(cpu);
626 break;
627
628 default:
629 break;
630 }
631
632 return NOTIFY_OK;
633}
634
635static struct notifier_block smpboot_thread_notifier = {
636 .notifier_call = smpboot_thread_call,
637 .priority = CPU_PRI_SMPBOOT,
638};
639
927da9df 640void smpboot_thread_init(void)
00df35f9
PM
641{
642 register_cpu_notifier(&smpboot_thread_notifier);
643}
644
4baa0afc
TG
645/**
646 * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
647 * @cpu: cpu that just started
648 *
649 * This function calls the cpu_chain notifiers with CPU_STARTING.
650 * It must be called by the arch code on the new cpu, before the new cpu
651 * enables interrupts and before the "boot" cpu returns from __cpu_up().
652 */
653void notify_cpu_starting(unsigned int cpu)
654{
655 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
656 enum cpuhp_state target = min((int)st->target, CPUHP_AP_ONLINE);
657
658 while (st->state < target) {
659 struct cpuhp_step *step;
660
661 st->state++;
662 step = cpuhp_ap_states + st->state;
663 cpuhp_invoke_callback(cpu, st->state, step->startup);
664 }
665}
666
cff7d378
TG
667static void undo_cpu_up(unsigned int cpu, struct cpuhp_cpu_state *st)
668{
669 for (st->state--; st->state > st->target; st->state--) {
670 struct cpuhp_step *step = cpuhp_bp_states + st->state;
671
672 if (!step->skip_onerr)
673 cpuhp_invoke_callback(cpu, st->state, step->teardown);
674 }
675}
676
e3920fb4 677/* Requires cpu_add_remove_lock to be held */
af1f4045 678static int _cpu_up(unsigned int cpu, int tasks_frozen, enum cpuhp_state target)
1da177e4 679{
cff7d378 680 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, cpu);
3bb5d2ee 681 struct task_struct *idle;
cff7d378 682 int prev_state, ret = 0;
1da177e4 683
d221938c 684 cpu_hotplug_begin();
38498a67 685
5e5041f3
YI
686 if (cpu_online(cpu) || !cpu_present(cpu)) {
687 ret = -EINVAL;
688 goto out;
689 }
690
cff7d378 691 /* Let it fail before we try to bring the cpu up */
3bb5d2ee
SS
692 idle = idle_thread_get(cpu);
693 if (IS_ERR(idle)) {
694 ret = PTR_ERR(idle);
38498a67 695 goto out;
3bb5d2ee 696 }
38498a67 697
ba997462
TG
698 cpuhp_tasks_frozen = tasks_frozen;
699
cff7d378 700 prev_state = st->state;
af1f4045 701 st->target = target;
cff7d378
TG
702 while (st->state < st->target) {
703 struct cpuhp_step *step;
704
705 st->state++;
706 step = cpuhp_bp_states + st->state;
707 ret = cpuhp_invoke_callback(cpu, st->state, step->startup);
708 if (ret) {
709 st->target = prev_state;
710 undo_cpu_up(cpu, st);
711 break;
712 }
713 }
38498a67 714out:
d221938c 715 cpu_hotplug_done();
e3920fb4
RW
716 return ret;
717}
718
af1f4045 719static int do_cpu_up(unsigned int cpu, enum cpuhp_state target)
e3920fb4
RW
720{
721 int err = 0;
cf23422b 722
e0b582ec 723 if (!cpu_possible(cpu)) {
84117da5
FF
724 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
725 cpu);
87d5e023 726#if defined(CONFIG_IA64)
84117da5 727 pr_err("please check additional_cpus= boot parameter\n");
73e753a5
KH
728#endif
729 return -EINVAL;
730 }
e3920fb4 731
01b0f197
TK
732 err = try_online_node(cpu_to_node(cpu));
733 if (err)
734 return err;
cf23422b 735
d221938c 736 cpu_maps_update_begin();
e761b772
MK
737
738 if (cpu_hotplug_disabled) {
e3920fb4 739 err = -EBUSY;
e761b772
MK
740 goto out;
741 }
742
af1f4045 743 err = _cpu_up(cpu, 0, target);
e761b772 744out:
d221938c 745 cpu_maps_update_done();
e3920fb4
RW
746 return err;
747}
af1f4045
TG
748
749int cpu_up(unsigned int cpu)
750{
751 return do_cpu_up(cpu, CPUHP_ONLINE);
752}
a513f6ba 753EXPORT_SYMBOL_GPL(cpu_up);
e3920fb4 754
f3de4be9 755#ifdef CONFIG_PM_SLEEP_SMP
e0b582ec 756static cpumask_var_t frozen_cpus;
e3920fb4
RW
757
758int disable_nonboot_cpus(void)
759{
e9a5f426 760 int cpu, first_cpu, error = 0;
e3920fb4 761
d221938c 762 cpu_maps_update_begin();
e0b582ec 763 first_cpu = cpumask_first(cpu_online_mask);
9ee349ad
XF
764 /*
765 * We take down all of the non-boot CPUs in one shot to avoid races
e3920fb4
RW
766 * with the userspace trying to use the CPU hotplug at the same time
767 */
e0b582ec 768 cpumask_clear(frozen_cpus);
6ad4c188 769
84117da5 770 pr_info("Disabling non-boot CPUs ...\n");
e3920fb4
RW
771 for_each_online_cpu(cpu) {
772 if (cpu == first_cpu)
773 continue;
bb3632c6 774 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
af1f4045 775 error = _cpu_down(cpu, 1, CPUHP_OFFLINE);
bb3632c6 776 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
feae3203 777 if (!error)
e0b582ec 778 cpumask_set_cpu(cpu, frozen_cpus);
feae3203 779 else {
84117da5 780 pr_err("Error taking CPU%d down: %d\n", cpu, error);
e3920fb4
RW
781 break;
782 }
783 }
86886e55 784
89af7ba5 785 if (!error)
e3920fb4 786 BUG_ON(num_online_cpus() > 1);
89af7ba5 787 else
84117da5 788 pr_err("Non-boot CPUs are not disabled\n");
89af7ba5
VK
789
790 /*
791 * Make sure the CPUs won't be enabled by someone else. We need to do
792 * this even in case of failure as all disable_nonboot_cpus() users are
793 * supposed to do enable_nonboot_cpus() on the failure path.
794 */
795 cpu_hotplug_disabled++;
796
d221938c 797 cpu_maps_update_done();
e3920fb4
RW
798 return error;
799}
800
d0af9eed
SS
801void __weak arch_enable_nonboot_cpus_begin(void)
802{
803}
804
805void __weak arch_enable_nonboot_cpus_end(void)
806{
807}
808
71cf5aee 809void enable_nonboot_cpus(void)
e3920fb4
RW
810{
811 int cpu, error;
812
813 /* Allow everyone to use the CPU hotplug again */
d221938c 814 cpu_maps_update_begin();
89af7ba5 815 WARN_ON(--cpu_hotplug_disabled < 0);
e0b582ec 816 if (cpumask_empty(frozen_cpus))
1d64b9cb 817 goto out;
e3920fb4 818
84117da5 819 pr_info("Enabling non-boot CPUs ...\n");
d0af9eed
SS
820
821 arch_enable_nonboot_cpus_begin();
822
e0b582ec 823 for_each_cpu(cpu, frozen_cpus) {
bb3632c6 824 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
af1f4045 825 error = _cpu_up(cpu, 1, CPUHP_ONLINE);
bb3632c6 826 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
e3920fb4 827 if (!error) {
84117da5 828 pr_info("CPU%d is up\n", cpu);
e3920fb4
RW
829 continue;
830 }
84117da5 831 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
e3920fb4 832 }
d0af9eed
SS
833
834 arch_enable_nonboot_cpus_end();
835
e0b582ec 836 cpumask_clear(frozen_cpus);
1d64b9cb 837out:
d221938c 838 cpu_maps_update_done();
1da177e4 839}
e0b582ec 840
d7268a31 841static int __init alloc_frozen_cpus(void)
e0b582ec
RR
842{
843 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
844 return -ENOMEM;
845 return 0;
846}
847core_initcall(alloc_frozen_cpus);
79cfbdfa 848
79cfbdfa
SB
849/*
850 * When callbacks for CPU hotplug notifications are being executed, we must
851 * ensure that the state of the system with respect to the tasks being frozen
852 * or not, as reported by the notification, remains unchanged *throughout the
853 * duration* of the execution of the callbacks.
854 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
855 *
856 * This synchronization is implemented by mutually excluding regular CPU
857 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
858 * Hibernate notifications.
859 */
860static int
861cpu_hotplug_pm_callback(struct notifier_block *nb,
862 unsigned long action, void *ptr)
863{
864 switch (action) {
865
866 case PM_SUSPEND_PREPARE:
867 case PM_HIBERNATION_PREPARE:
16e53dbf 868 cpu_hotplug_disable();
79cfbdfa
SB
869 break;
870
871 case PM_POST_SUSPEND:
872 case PM_POST_HIBERNATION:
16e53dbf 873 cpu_hotplug_enable();
79cfbdfa
SB
874 break;
875
876 default:
877 return NOTIFY_DONE;
878 }
879
880 return NOTIFY_OK;
881}
882
883
d7268a31 884static int __init cpu_hotplug_pm_sync_init(void)
79cfbdfa 885{
6e32d479
FY
886 /*
887 * cpu_hotplug_pm_callback has higher priority than x86
888 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
889 * to disable cpu hotplug to avoid cpu hotplug race.
890 */
79cfbdfa
SB
891 pm_notifier(cpu_hotplug_pm_callback, 0);
892 return 0;
893}
894core_initcall(cpu_hotplug_pm_sync_init);
895
f3de4be9 896#endif /* CONFIG_PM_SLEEP_SMP */
68f4f1ec
MK
897
898#endif /* CONFIG_SMP */
b8d317d1 899
cff7d378
TG
900/* Boot processor state steps */
901static struct cpuhp_step cpuhp_bp_states[] = {
902 [CPUHP_OFFLINE] = {
903 .name = "offline",
904 .startup = NULL,
905 .teardown = NULL,
906 },
907#ifdef CONFIG_SMP
908 [CPUHP_CREATE_THREADS]= {
909 .name = "threads:create",
910 .startup = smpboot_create_threads,
911 .teardown = NULL,
912 },
913 [CPUHP_NOTIFY_PREPARE] = {
914 .name = "notify:prepare",
915 .startup = notify_prepare,
916 .teardown = notify_dead,
917 .skip_onerr = true,
918 },
919 [CPUHP_BRINGUP_CPU] = {
920 .name = "cpu:bringup",
921 .startup = bringup_cpu,
4baa0afc
TG
922 .teardown = NULL,
923 },
924 [CPUHP_TEARDOWN_CPU] = {
925 .name = "cpu:teardown",
926 .startup = NULL,
cff7d378 927 .teardown = takedown_cpu,
cff7d378
TG
928 },
929 [CPUHP_NOTIFY_ONLINE] = {
930 .name = "notify:online",
931 .startup = notify_online,
932 .teardown = notify_down_prepare,
933 },
934#endif
935 [CPUHP_ONLINE] = {
936 .name = "online",
937 .startup = NULL,
938 .teardown = NULL,
939 },
940};
941
4baa0afc
TG
942/* Application processor state steps */
943static struct cpuhp_step cpuhp_ap_states[] = {
944#ifdef CONFIG_SMP
945 [CPUHP_AP_NOTIFY_STARTING] = {
946 .name = "notify:starting",
947 .startup = notify_starting,
948 .teardown = notify_dying,
949 .skip_onerr = true,
950 },
951#endif
952 [CPUHP_ONLINE] = {
953 .name = "online",
954 .startup = NULL,
955 .teardown = NULL,
956 },
957};
958
98f8cdce
TG
959static bool cpuhp_is_ap_state(enum cpuhp_state state)
960{
961 return (state > CPUHP_AP_OFFLINE && state < CPUHP_AP_ONLINE);
962}
963
964static struct cpuhp_step *cpuhp_get_step(enum cpuhp_state state)
965{
966 struct cpuhp_step *sp;
967
968 sp = cpuhp_is_ap_state(state) ? cpuhp_ap_states : cpuhp_bp_states;
969 return sp + state;
970}
971
972#if defined(CONFIG_SYSFS) && defined(CONFIG_HOTPLUG_CPU)
973static ssize_t show_cpuhp_state(struct device *dev,
974 struct device_attribute *attr, char *buf)
975{
976 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
977
978 return sprintf(buf, "%d\n", st->state);
979}
980static DEVICE_ATTR(state, 0444, show_cpuhp_state, NULL);
981
982static ssize_t show_cpuhp_target(struct device *dev,
983 struct device_attribute *attr, char *buf)
984{
985 struct cpuhp_cpu_state *st = per_cpu_ptr(&cpuhp_state, dev->id);
986
987 return sprintf(buf, "%d\n", st->target);
988}
989static DEVICE_ATTR(target, 0444, show_cpuhp_target, NULL);
990
991static struct attribute *cpuhp_cpu_attrs[] = {
992 &dev_attr_state.attr,
993 &dev_attr_target.attr,
994 NULL
995};
996
997static struct attribute_group cpuhp_cpu_attr_group = {
998 .attrs = cpuhp_cpu_attrs,
999 .name = "hotplug",
1000 NULL
1001};
1002
1003static ssize_t show_cpuhp_states(struct device *dev,
1004 struct device_attribute *attr, char *buf)
1005{
1006 ssize_t cur, res = 0;
1007 int i;
1008
1009 mutex_lock(&cpuhp_state_mutex);
1010 for (i = 0; i <= CPUHP_ONLINE; i++) {
1011 struct cpuhp_step *sp = cpuhp_get_step(i);
1012
1013 if (sp->name) {
1014 cur = sprintf(buf, "%3d: %s\n", i, sp->name);
1015 buf += cur;
1016 res += cur;
1017 }
1018 }
1019 mutex_unlock(&cpuhp_state_mutex);
1020 return res;
1021}
1022static DEVICE_ATTR(states, 0444, show_cpuhp_states, NULL);
1023
1024static struct attribute *cpuhp_cpu_root_attrs[] = {
1025 &dev_attr_states.attr,
1026 NULL
1027};
1028
1029static struct attribute_group cpuhp_cpu_root_attr_group = {
1030 .attrs = cpuhp_cpu_root_attrs,
1031 .name = "hotplug",
1032 NULL
1033};
1034
1035static int __init cpuhp_sysfs_init(void)
1036{
1037 int cpu, ret;
1038
1039 ret = sysfs_create_group(&cpu_subsys.dev_root->kobj,
1040 &cpuhp_cpu_root_attr_group);
1041 if (ret)
1042 return ret;
1043
1044 for_each_possible_cpu(cpu) {
1045 struct device *dev = get_cpu_device(cpu);
1046
1047 if (!dev)
1048 continue;
1049 ret = sysfs_create_group(&dev->kobj, &cpuhp_cpu_attr_group);
1050 if (ret)
1051 return ret;
1052 }
1053 return 0;
1054}
1055device_initcall(cpuhp_sysfs_init);
1056#endif
1057
e56b3bc7
LT
1058/*
1059 * cpu_bit_bitmap[] is a special, "compressed" data structure that
1060 * represents all NR_CPUS bits binary values of 1<<nr.
1061 *
e0b582ec 1062 * It is used by cpumask_of() to get a constant address to a CPU
e56b3bc7
LT
1063 * mask value that has a single bit set only.
1064 */
b8d317d1 1065
e56b3bc7 1066/* cpu_bit_bitmap[0] is empty - so we can back into it */
4d51985e 1067#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
e56b3bc7
LT
1068#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
1069#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
1070#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
b8d317d1 1071
e56b3bc7
LT
1072const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
1073
1074 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
1075 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
1076#if BITS_PER_LONG > 32
1077 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
1078 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
b8d317d1
MT
1079#endif
1080};
e56b3bc7 1081EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2d3854a3
RR
1082
1083const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
1084EXPORT_SYMBOL(cpu_all_bits);
b3199c02
RR
1085
1086#ifdef CONFIG_INIT_ALL_POSSIBLE
4b804c85 1087struct cpumask __cpu_possible_mask __read_mostly
c4c54dd1 1088 = {CPU_BITS_ALL};
b3199c02 1089#else
4b804c85 1090struct cpumask __cpu_possible_mask __read_mostly;
b3199c02 1091#endif
4b804c85 1092EXPORT_SYMBOL(__cpu_possible_mask);
b3199c02 1093
4b804c85
RV
1094struct cpumask __cpu_online_mask __read_mostly;
1095EXPORT_SYMBOL(__cpu_online_mask);
b3199c02 1096
4b804c85
RV
1097struct cpumask __cpu_present_mask __read_mostly;
1098EXPORT_SYMBOL(__cpu_present_mask);
b3199c02 1099
4b804c85
RV
1100struct cpumask __cpu_active_mask __read_mostly;
1101EXPORT_SYMBOL(__cpu_active_mask);
3fa41520 1102
3fa41520
RR
1103void init_cpu_present(const struct cpumask *src)
1104{
c4c54dd1 1105 cpumask_copy(&__cpu_present_mask, src);
3fa41520
RR
1106}
1107
1108void init_cpu_possible(const struct cpumask *src)
1109{
c4c54dd1 1110 cpumask_copy(&__cpu_possible_mask, src);
3fa41520
RR
1111}
1112
1113void init_cpu_online(const struct cpumask *src)
1114{
c4c54dd1 1115 cpumask_copy(&__cpu_online_mask, src);
3fa41520 1116}
cff7d378
TG
1117
1118/*
1119 * Activate the first processor.
1120 */
1121void __init boot_cpu_init(void)
1122{
1123 int cpu = smp_processor_id();
1124
1125 /* Mark the boot cpu "present", "online" etc for SMP and UP case */
1126 set_cpu_online(cpu, true);
1127 set_cpu_active(cpu, true);
1128 set_cpu_present(cpu, true);
1129 set_cpu_possible(cpu, true);
1130}
1131
1132/*
1133 * Must be called _AFTER_ setting up the per_cpu areas
1134 */
1135void __init boot_cpu_state_init(void)
1136{
1137 per_cpu_ptr(&cpuhp_state, smp_processor_id())->state = CPUHP_ONLINE;
1138}