]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - kernel/cpu.c
x86/speculation: Provide IBPB always command line options
[thirdparty/kernel/stable.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>
19a284b2 11#include <linux/sched/smt.h>
1da177e4
LT
12#include <linux/unistd.h>
13#include <linux/cpu.h>
cb79295e
AV
14#include <linux/oom.h>
15#include <linux/rcupdate.h>
9984de1a 16#include <linux/export.h>
e4cc2f87 17#include <linux/bug.h>
1da177e4
LT
18#include <linux/kthread.h>
19#include <linux/stop_machine.h>
81615b62 20#include <linux/mutex.h>
5a0e3ad6 21#include <linux/gfp.h>
79cfbdfa 22#include <linux/suspend.h>
a19423b9 23#include <linux/lockdep.h>
bb3632c6 24#include <trace/events/power.h>
1da177e4 25
38498a67
TG
26#include "smpboot.h"
27
98a79d6a 28#ifdef CONFIG_SMP
b3199c02 29/* Serializes the updates to cpu_online_mask, cpu_present_mask */
aa953877 30static DEFINE_MUTEX(cpu_add_remove_lock);
1da177e4 31
79a6cdeb 32/*
93ae4f97
SB
33 * The following two APIs (cpu_maps_update_begin/done) must be used when
34 * attempting to serialize the updates to cpu_online_mask & cpu_present_mask.
35 * The APIs cpu_notifier_register_begin/done() must be used to protect CPU
36 * hotplug callback (un)registration performed using __register_cpu_notifier()
37 * or __unregister_cpu_notifier().
79a6cdeb
LJ
38 */
39void cpu_maps_update_begin(void)
40{
41 mutex_lock(&cpu_add_remove_lock);
42}
93ae4f97 43EXPORT_SYMBOL(cpu_notifier_register_begin);
79a6cdeb
LJ
44
45void cpu_maps_update_done(void)
46{
47 mutex_unlock(&cpu_add_remove_lock);
48}
93ae4f97 49EXPORT_SYMBOL(cpu_notifier_register_done);
79a6cdeb 50
5c113fbe 51static RAW_NOTIFIER_HEAD(cpu_chain);
1da177e4 52
e3920fb4
RW
53/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
54 * Should always be manipulated under cpu_add_remove_lock
55 */
56static int cpu_hotplug_disabled;
57
79a6cdeb
LJ
58#ifdef CONFIG_HOTPLUG_CPU
59
d221938c
GS
60static struct {
61 struct task_struct *active_writer;
62 struct mutex lock; /* Synchronizes accesses to refcount, */
63 /*
64 * Also blocks the new readers during
65 * an ongoing cpu hotplug operation.
66 */
67 int refcount;
a19423b9
GS
68
69#ifdef CONFIG_DEBUG_LOCK_ALLOC
70 struct lockdep_map dep_map;
71#endif
31950eb6
LT
72} cpu_hotplug = {
73 .active_writer = NULL,
74 .lock = __MUTEX_INITIALIZER(cpu_hotplug.lock),
75 .refcount = 0,
a19423b9
GS
76#ifdef CONFIG_DEBUG_LOCK_ALLOC
77 .dep_map = {.name = "cpu_hotplug.lock" },
78#endif
31950eb6 79};
d221938c 80
a19423b9
GS
81/* Lockdep annotations for get/put_online_cpus() and cpu_hotplug_begin/end() */
82#define cpuhp_lock_acquire_read() lock_map_acquire_read(&cpu_hotplug.dep_map)
83#define cpuhp_lock_acquire() lock_map_acquire(&cpu_hotplug.dep_map)
84#define cpuhp_lock_release() lock_map_release(&cpu_hotplug.dep_map)
85
86ef5c9a 86void get_online_cpus(void)
a9d9baa1 87{
d221938c
GS
88 might_sleep();
89 if (cpu_hotplug.active_writer == current)
aa953877 90 return;
a19423b9 91 cpuhp_lock_acquire_read();
d221938c
GS
92 mutex_lock(&cpu_hotplug.lock);
93 cpu_hotplug.refcount++;
94 mutex_unlock(&cpu_hotplug.lock);
95
a9d9baa1 96}
86ef5c9a 97EXPORT_SYMBOL_GPL(get_online_cpus);
90d45d17 98
86ef5c9a 99void put_online_cpus(void)
a9d9baa1 100{
d221938c 101 if (cpu_hotplug.active_writer == current)
aa953877 102 return;
d221938c 103 mutex_lock(&cpu_hotplug.lock);
075663d1
SB
104
105 if (WARN_ON(!cpu_hotplug.refcount))
106 cpu_hotplug.refcount++; /* try to fix things up */
107
d2ba7e2a
ON
108 if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
109 wake_up_process(cpu_hotplug.active_writer);
d221938c 110 mutex_unlock(&cpu_hotplug.lock);
a19423b9 111 cpuhp_lock_release();
d221938c 112
a9d9baa1 113}
86ef5c9a 114EXPORT_SYMBOL_GPL(put_online_cpus);
a9d9baa1 115
d221938c
GS
116/*
117 * This ensures that the hotplug operation can begin only when the
118 * refcount goes to zero.
119 *
120 * Note that during a cpu-hotplug operation, the new readers, if any,
121 * will be blocked by the cpu_hotplug.lock
122 *
d2ba7e2a
ON
123 * Since cpu_hotplug_begin() is always called after invoking
124 * cpu_maps_update_begin(), we can be sure that only one writer is active.
d221938c
GS
125 *
126 * Note that theoretically, there is a possibility of a livelock:
127 * - Refcount goes to zero, last reader wakes up the sleeping
128 * writer.
129 * - Last reader unlocks the cpu_hotplug.lock.
130 * - A new reader arrives at this moment, bumps up the refcount.
131 * - The writer acquires the cpu_hotplug.lock finds the refcount
132 * non zero and goes to sleep again.
133 *
134 * However, this is very difficult to achieve in practice since
86ef5c9a 135 * get_online_cpus() not an api which is called all that often.
d221938c
GS
136 *
137 */
b9d10be7 138void cpu_hotplug_begin(void)
d221938c 139{
d221938c 140 cpu_hotplug.active_writer = current;
d2ba7e2a 141
a19423b9 142 cpuhp_lock_acquire();
d2ba7e2a
ON
143 for (;;) {
144 mutex_lock(&cpu_hotplug.lock);
145 if (likely(!cpu_hotplug.refcount))
146 break;
147 __set_current_state(TASK_UNINTERRUPTIBLE);
d221938c
GS
148 mutex_unlock(&cpu_hotplug.lock);
149 schedule();
d221938c 150 }
d221938c
GS
151}
152
b9d10be7 153void cpu_hotplug_done(void)
d221938c
GS
154{
155 cpu_hotplug.active_writer = NULL;
156 mutex_unlock(&cpu_hotplug.lock);
a19423b9 157 cpuhp_lock_release();
d221938c 158}
79a6cdeb 159
16e53dbf
SB
160/*
161 * Wait for currently running CPU hotplug operations to complete (if any) and
162 * disable future CPU hotplug (from sysfs). The 'cpu_add_remove_lock' protects
163 * the 'cpu_hotplug_disabled' flag. The same lock is also acquired by the
164 * hotplug path before performing hotplug operations. So acquiring that lock
165 * guarantees mutual exclusion from any currently running hotplug operations.
166 */
167void cpu_hotplug_disable(void)
168{
169 cpu_maps_update_begin();
170 cpu_hotplug_disabled = 1;
171 cpu_maps_update_done();
172}
173
174void cpu_hotplug_enable(void)
175{
176 cpu_maps_update_begin();
177 cpu_hotplug_disabled = 0;
178 cpu_maps_update_done();
179}
180
b9d10be7 181#endif /* CONFIG_HOTPLUG_CPU */
79a6cdeb 182
19a284b2
TG
183/*
184 * Architectures that need SMT-specific errata handling during SMT hotplug
185 * should override this.
186 */
187void __weak arch_smt_update(void) { }
188
1da177e4 189/* Need to know about CPUs going up/down? */
f7b16c10 190int __ref register_cpu_notifier(struct notifier_block *nb)
1da177e4 191{
bd5349cf 192 int ret;
d221938c 193 cpu_maps_update_begin();
bd5349cf 194 ret = raw_notifier_chain_register(&cpu_chain, nb);
d221938c 195 cpu_maps_update_done();
bd5349cf 196 return ret;
1da177e4 197}
65edc68c 198
93ae4f97
SB
199int __ref __register_cpu_notifier(struct notifier_block *nb)
200{
201 return raw_notifier_chain_register(&cpu_chain, nb);
202}
203
e9fb7631
AM
204static int __cpu_notify(unsigned long val, void *v, int nr_to_call,
205 int *nr_calls)
206{
e6bde73b
AM
207 int ret;
208
209 ret = __raw_notifier_call_chain(&cpu_chain, val, v, nr_to_call,
e9fb7631 210 nr_calls);
e6bde73b
AM
211
212 return notifier_to_errno(ret);
e9fb7631
AM
213}
214
215static int cpu_notify(unsigned long val, void *v)
216{
217 return __cpu_notify(val, v, -1, NULL);
218}
219
1da177e4 220EXPORT_SYMBOL(register_cpu_notifier);
93ae4f97 221EXPORT_SYMBOL(__register_cpu_notifier);
1da177e4 222
9647155f 223void __ref unregister_cpu_notifier(struct notifier_block *nb)
1da177e4 224{
d221938c 225 cpu_maps_update_begin();
bd5349cf 226 raw_notifier_chain_unregister(&cpu_chain, nb);
d221938c 227 cpu_maps_update_done();
1da177e4
LT
228}
229EXPORT_SYMBOL(unregister_cpu_notifier);
230
93ae4f97
SB
231void __ref __unregister_cpu_notifier(struct notifier_block *nb)
232{
233 raw_notifier_chain_unregister(&cpu_chain, nb);
234}
235EXPORT_SYMBOL(__unregister_cpu_notifier);
236
9ad4ebf4
MH
237#ifdef CONFIG_HOTPLUG_CPU
238
239static void cpu_notify_nofail(unsigned long val, void *v)
240{
241 BUG_ON(cpu_notify(val, v));
242}
243
e4cc2f87
AV
244/**
245 * clear_tasks_mm_cpumask - Safely clear tasks' mm_cpumask for a CPU
246 * @cpu: a CPU id
247 *
248 * This function walks all processes, finds a valid mm struct for each one and
249 * then clears a corresponding bit in mm's cpumask. While this all sounds
250 * trivial, there are various non-obvious corner cases, which this function
251 * tries to solve in a safe manner.
252 *
253 * Also note that the function uses a somewhat relaxed locking scheme, so it may
254 * be called only for an already offlined CPU.
255 */
cb79295e
AV
256void clear_tasks_mm_cpumask(int cpu)
257{
258 struct task_struct *p;
259
260 /*
261 * This function is called after the cpu is taken down and marked
262 * offline, so its not like new tasks will ever get this cpu set in
263 * their mm mask. -- Peter Zijlstra
264 * Thus, we may use rcu_read_lock() here, instead of grabbing
265 * full-fledged tasklist_lock.
266 */
e4cc2f87 267 WARN_ON(cpu_online(cpu));
cb79295e
AV
268 rcu_read_lock();
269 for_each_process(p) {
270 struct task_struct *t;
271
e4cc2f87
AV
272 /*
273 * Main thread might exit, but other threads may still have
274 * a valid mm. Find one.
275 */
cb79295e
AV
276 t = find_lock_task_mm(p);
277 if (!t)
278 continue;
279 cpumask_clear_cpu(cpu, mm_cpumask(t->mm));
280 task_unlock(t);
281 }
282 rcu_read_unlock();
283}
284
1da177e4
LT
285static inline void check_for_tasks(int cpu)
286{
287 struct task_struct *p;
6fac4829 288 cputime_t utime, stime;
1da177e4
LT
289
290 write_lock_irq(&tasklist_lock);
291 for_each_process(p) {
6fac4829 292 task_cputime(p, &utime, &stime);
11854247 293 if (task_cpu(p) == cpu && p->state == TASK_RUNNING &&
6fac4829 294 (utime || stime))
84117da5 295 pr_warn("Task %s (pid = %d) is on cpu %d (state = %ld, flags = %x)\n",
9d3cfc4c
FP
296 p->comm, task_pid_nr(p), cpu,
297 p->state, p->flags);
1da177e4
LT
298 }
299 write_unlock_irq(&tasklist_lock);
300}
301
db912f96
AK
302struct take_cpu_down_param {
303 unsigned long mod;
304 void *hcpu;
305};
306
1da177e4 307/* Take this CPU down. */
514a20a5 308static int __ref take_cpu_down(void *_param)
1da177e4 309{
db912f96 310 struct take_cpu_down_param *param = _param;
1da177e4
LT
311 int err;
312
1da177e4
LT
313 /* Ensure this CPU doesn't handle any more interrupts. */
314 err = __cpu_disable();
315 if (err < 0)
f3705136 316 return err;
1da177e4 317
e9fb7631 318 cpu_notify(CPU_DYING | param->mod, param->hcpu);
14e568e7
TG
319 /* Park the stopper thread */
320 kthread_park(current);
f3705136 321 return 0;
1da177e4
LT
322}
323
e3920fb4 324/* Requires cpu_add_remove_lock to be held */
514a20a5 325static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
1da177e4 326{
e7407dcc 327 int err, nr_calls = 0;
e7407dcc 328 void *hcpu = (void *)(long)cpu;
8bb78442 329 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
db912f96
AK
330 struct take_cpu_down_param tcd_param = {
331 .mod = mod,
332 .hcpu = hcpu,
333 };
1da177e4 334
e3920fb4
RW
335 if (num_online_cpus() == 1)
336 return -EBUSY;
1da177e4 337
e3920fb4
RW
338 if (!cpu_online(cpu))
339 return -EINVAL;
1da177e4 340
d221938c 341 cpu_hotplug_begin();
4d51985e 342
e9fb7631 343 err = __cpu_notify(CPU_DOWN_PREPARE | mod, hcpu, -1, &nr_calls);
e6bde73b 344 if (err) {
a0d8cdb6 345 nr_calls--;
e9fb7631 346 __cpu_notify(CPU_DOWN_FAILED | mod, hcpu, nr_calls, NULL);
84117da5
FF
347 pr_warn("%s: attempt to take down CPU %u failed\n",
348 __func__, cpu);
baaca49f 349 goto out_release;
1da177e4
LT
350 }
351
6acce3ef
PZ
352 /*
353 * By now we've cleared cpu_active_mask, wait for all preempt-disabled
354 * and RCU users of this state to go away such that all new such users
355 * will observe it.
356 *
357 * For CONFIG_PREEMPT we have preemptible RCU and its sync_rcu() might
358 * not imply sync_sched(), so explicitly call both.
106dd5af
M
359 *
360 * Do sync before park smpboot threads to take care the rcu boost case.
6acce3ef
PZ
361 */
362#ifdef CONFIG_PREEMPT
363 synchronize_sched();
364#endif
365 synchronize_rcu();
366
106dd5af
M
367 smpboot_park_threads(cpu);
368
6acce3ef
PZ
369 /*
370 * So now all preempt/rcu users must observe !cpu_active().
371 */
372
e0b582ec 373 err = __stop_machine(take_cpu_down, &tcd_param, cpumask_of(cpu));
04321587 374 if (err) {
1da177e4 375 /* CPU didn't die: tell everyone. Can't complain. */
f97f8f06 376 smpboot_unpark_threads(cpu);
e9fb7631 377 cpu_notify_nofail(CPU_DOWN_FAILED | mod, hcpu);
6a1bdc1b 378 goto out_release;
8fa1d7d3 379 }
04321587 380 BUG_ON(cpu_online(cpu));
1da177e4 381
48c5ccae
PZ
382 /*
383 * The migration_call() CPU_DYING callback will have removed all
384 * runnable tasks from the cpu, there's only the idle task left now
385 * that the migration thread is done doing the stop_machine thing.
51a96c77
PZ
386 *
387 * Wait for the stop thread to go away.
48c5ccae 388 */
51a96c77
PZ
389 while (!idle_cpu(cpu))
390 cpu_relax();
1da177e4
LT
391
392 /* This actually kills the CPU. */
393 __cpu_die(cpu);
394
1da177e4 395 /* CPU is completely dead: tell everyone. Too late to complain. */
e9fb7631 396 cpu_notify_nofail(CPU_DEAD | mod, hcpu);
1da177e4
LT
397
398 check_for_tasks(cpu);
399
baaca49f 400out_release:
d221938c 401 cpu_hotplug_done();
e9fb7631
AM
402 if (!err)
403 cpu_notify_nofail(CPU_POST_DEAD | mod, hcpu);
19a284b2 404 arch_smt_update();
e3920fb4
RW
405 return err;
406}
407
514a20a5 408int __ref cpu_down(unsigned int cpu)
e3920fb4 409{
9ea09af3 410 int err;
e3920fb4 411
d221938c 412 cpu_maps_update_begin();
e761b772
MK
413
414 if (cpu_hotplug_disabled) {
e3920fb4 415 err = -EBUSY;
e761b772
MK
416 goto out;
417 }
418
e761b772 419 err = _cpu_down(cpu, 0);
e3920fb4 420
e761b772 421out:
d221938c 422 cpu_maps_update_done();
1da177e4
LT
423 return err;
424}
b62b8ef9 425EXPORT_SYMBOL(cpu_down);
1da177e4
LT
426#endif /*CONFIG_HOTPLUG_CPU*/
427
477a64e6
PM
428/*
429 * Unpark per-CPU smpboot kthreads at CPU-online time.
430 */
431static int smpboot_thread_call(struct notifier_block *nfb,
432 unsigned long action, void *hcpu)
433{
434 int cpu = (long)hcpu;
435
436 switch (action & ~CPU_TASKS_FROZEN) {
437
438 case CPU_ONLINE:
439 smpboot_unpark_threads(cpu);
440 break;
441
442 default:
443 break;
444 }
445
446 return NOTIFY_OK;
447}
448
449static struct notifier_block smpboot_thread_notifier = {
450 .notifier_call = smpboot_thread_call,
451 .priority = CPU_PRI_SMPBOOT,
452};
453
454void __cpuinit smpboot_thread_init(void)
455{
456 register_cpu_notifier(&smpboot_thread_notifier);
457}
458
e3920fb4 459/* Requires cpu_add_remove_lock to be held */
0db0628d 460static int _cpu_up(unsigned int cpu, int tasks_frozen)
1da177e4 461{
baaca49f 462 int ret, nr_calls = 0;
1da177e4 463 void *hcpu = (void *)(long)cpu;
8bb78442 464 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
3bb5d2ee 465 struct task_struct *idle;
1da177e4 466
d221938c 467 cpu_hotplug_begin();
38498a67 468
5e5041f3
YI
469 if (cpu_online(cpu) || !cpu_present(cpu)) {
470 ret = -EINVAL;
471 goto out;
472 }
473
3bb5d2ee
SS
474 idle = idle_thread_get(cpu);
475 if (IS_ERR(idle)) {
476 ret = PTR_ERR(idle);
38498a67 477 goto out;
3bb5d2ee 478 }
38498a67 479
f97f8f06
TG
480 ret = smpboot_create_threads(cpu);
481 if (ret)
482 goto out;
483
e9fb7631 484 ret = __cpu_notify(CPU_UP_PREPARE | mod, hcpu, -1, &nr_calls);
e6bde73b 485 if (ret) {
a0d8cdb6 486 nr_calls--;
84117da5
FF
487 pr_warn("%s: attempt to bring up CPU %u failed\n",
488 __func__, cpu);
1da177e4
LT
489 goto out_notify;
490 }
491
492 /* Arch-specific enabling code. */
3bb5d2ee 493 ret = __cpu_up(cpu, idle);
1da177e4
LT
494 if (ret != 0)
495 goto out_notify;
6978c705 496 BUG_ON(!cpu_online(cpu));
1da177e4
LT
497
498 /* Now call notifier in preparation. */
e9fb7631 499 cpu_notify(CPU_ONLINE | mod, hcpu);
1da177e4
LT
500
501out_notify:
502 if (ret != 0)
e9fb7631 503 __cpu_notify(CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
38498a67 504out:
d221938c 505 cpu_hotplug_done();
19a284b2 506 arch_smt_update();
e3920fb4
RW
507 return ret;
508}
509
0db0628d 510int cpu_up(unsigned int cpu)
e3920fb4
RW
511{
512 int err = 0;
cf23422b 513
e0b582ec 514 if (!cpu_possible(cpu)) {
84117da5
FF
515 pr_err("can't online cpu %d because it is not configured as may-hotadd at boot time\n",
516 cpu);
87d5e023 517#if defined(CONFIG_IA64)
84117da5 518 pr_err("please check additional_cpus= boot parameter\n");
73e753a5
KH
519#endif
520 return -EINVAL;
521 }
e3920fb4 522
01b0f197
TK
523 err = try_online_node(cpu_to_node(cpu));
524 if (err)
525 return err;
cf23422b 526
d221938c 527 cpu_maps_update_begin();
e761b772
MK
528
529 if (cpu_hotplug_disabled) {
e3920fb4 530 err = -EBUSY;
e761b772
MK
531 goto out;
532 }
533
534 err = _cpu_up(cpu, 0);
535
e761b772 536out:
d221938c 537 cpu_maps_update_done();
e3920fb4
RW
538 return err;
539}
a513f6ba 540EXPORT_SYMBOL_GPL(cpu_up);
e3920fb4 541
f3de4be9 542#ifdef CONFIG_PM_SLEEP_SMP
e0b582ec 543static cpumask_var_t frozen_cpus;
e3920fb4
RW
544
545int disable_nonboot_cpus(void)
546{
e9a5f426 547 int cpu, first_cpu, error = 0;
e3920fb4 548
d221938c 549 cpu_maps_update_begin();
e0b582ec 550 first_cpu = cpumask_first(cpu_online_mask);
9ee349ad
XF
551 /*
552 * We take down all of the non-boot CPUs in one shot to avoid races
e3920fb4
RW
553 * with the userspace trying to use the CPU hotplug at the same time
554 */
e0b582ec 555 cpumask_clear(frozen_cpus);
6ad4c188 556
84117da5 557 pr_info("Disabling non-boot CPUs ...\n");
e3920fb4
RW
558 for_each_online_cpu(cpu) {
559 if (cpu == first_cpu)
560 continue;
bb3632c6 561 trace_suspend_resume(TPS("CPU_OFF"), cpu, true);
8bb78442 562 error = _cpu_down(cpu, 1);
bb3632c6 563 trace_suspend_resume(TPS("CPU_OFF"), cpu, false);
feae3203 564 if (!error)
e0b582ec 565 cpumask_set_cpu(cpu, frozen_cpus);
feae3203 566 else {
84117da5 567 pr_err("Error taking CPU%d down: %d\n", cpu, error);
e3920fb4
RW
568 break;
569 }
570 }
86886e55 571
e3920fb4
RW
572 if (!error) {
573 BUG_ON(num_online_cpus() > 1);
574 /* Make sure the CPUs won't be enabled by someone else */
575 cpu_hotplug_disabled = 1;
576 } else {
84117da5 577 pr_err("Non-boot CPUs are not disabled\n");
e3920fb4 578 }
d221938c 579 cpu_maps_update_done();
e3920fb4
RW
580 return error;
581}
582
d0af9eed
SS
583void __weak arch_enable_nonboot_cpus_begin(void)
584{
585}
586
587void __weak arch_enable_nonboot_cpus_end(void)
588{
589}
590
fa7303e2 591void __ref enable_nonboot_cpus(void)
e3920fb4
RW
592{
593 int cpu, error;
594
595 /* Allow everyone to use the CPU hotplug again */
d221938c 596 cpu_maps_update_begin();
e3920fb4 597 cpu_hotplug_disabled = 0;
e0b582ec 598 if (cpumask_empty(frozen_cpus))
1d64b9cb 599 goto out;
e3920fb4 600
84117da5 601 pr_info("Enabling non-boot CPUs ...\n");
d0af9eed
SS
602
603 arch_enable_nonboot_cpus_begin();
604
e0b582ec 605 for_each_cpu(cpu, frozen_cpus) {
bb3632c6 606 trace_suspend_resume(TPS("CPU_ON"), cpu, true);
8bb78442 607 error = _cpu_up(cpu, 1);
bb3632c6 608 trace_suspend_resume(TPS("CPU_ON"), cpu, false);
e3920fb4 609 if (!error) {
84117da5 610 pr_info("CPU%d is up\n", cpu);
e3920fb4
RW
611 continue;
612 }
84117da5 613 pr_warn("Error taking CPU%d up: %d\n", cpu, error);
e3920fb4 614 }
d0af9eed
SS
615
616 arch_enable_nonboot_cpus_end();
617
e0b582ec 618 cpumask_clear(frozen_cpus);
1d64b9cb 619out:
d221938c 620 cpu_maps_update_done();
1da177e4 621}
e0b582ec 622
d7268a31 623static int __init alloc_frozen_cpus(void)
e0b582ec
RR
624{
625 if (!alloc_cpumask_var(&frozen_cpus, GFP_KERNEL|__GFP_ZERO))
626 return -ENOMEM;
627 return 0;
628}
629core_initcall(alloc_frozen_cpus);
79cfbdfa 630
79cfbdfa
SB
631/*
632 * When callbacks for CPU hotplug notifications are being executed, we must
633 * ensure that the state of the system with respect to the tasks being frozen
634 * or not, as reported by the notification, remains unchanged *throughout the
635 * duration* of the execution of the callbacks.
636 * Hence we need to prevent the freezer from racing with regular CPU hotplug.
637 *
638 * This synchronization is implemented by mutually excluding regular CPU
639 * hotplug and Suspend/Hibernate call paths by hooking onto the Suspend/
640 * Hibernate notifications.
641 */
642static int
643cpu_hotplug_pm_callback(struct notifier_block *nb,
644 unsigned long action, void *ptr)
645{
646 switch (action) {
647
648 case PM_SUSPEND_PREPARE:
649 case PM_HIBERNATION_PREPARE:
16e53dbf 650 cpu_hotplug_disable();
79cfbdfa
SB
651 break;
652
653 case PM_POST_SUSPEND:
654 case PM_POST_HIBERNATION:
16e53dbf 655 cpu_hotplug_enable();
79cfbdfa
SB
656 break;
657
658 default:
659 return NOTIFY_DONE;
660 }
661
662 return NOTIFY_OK;
663}
664
665
d7268a31 666static int __init cpu_hotplug_pm_sync_init(void)
79cfbdfa 667{
6e32d479
FY
668 /*
669 * cpu_hotplug_pm_callback has higher priority than x86
670 * bsp_pm_callback which depends on cpu_hotplug_pm_callback
671 * to disable cpu hotplug to avoid cpu hotplug race.
672 */
79cfbdfa
SB
673 pm_notifier(cpu_hotplug_pm_callback, 0);
674 return 0;
675}
676core_initcall(cpu_hotplug_pm_sync_init);
677
f3de4be9 678#endif /* CONFIG_PM_SLEEP_SMP */
68f4f1ec 679
e545a614
MS
680/**
681 * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
682 * @cpu: cpu that just started
683 *
684 * This function calls the cpu_chain notifiers with CPU_STARTING.
685 * It must be called by the arch code on the new cpu, before the new cpu
686 * enables interrupts and before the "boot" cpu returns from __cpu_up().
687 */
0db0628d 688void notify_cpu_starting(unsigned int cpu)
e545a614
MS
689{
690 unsigned long val = CPU_STARTING;
691
692#ifdef CONFIG_PM_SLEEP_SMP
e0b582ec 693 if (frozen_cpus != NULL && cpumask_test_cpu(cpu, frozen_cpus))
e545a614
MS
694 val = CPU_STARTING_FROZEN;
695#endif /* CONFIG_PM_SLEEP_SMP */
e9fb7631 696 cpu_notify(val, (void *)(long)cpu);
e545a614
MS
697}
698
68f4f1ec 699#endif /* CONFIG_SMP */
b8d317d1 700
e56b3bc7
LT
701/*
702 * cpu_bit_bitmap[] is a special, "compressed" data structure that
703 * represents all NR_CPUS bits binary values of 1<<nr.
704 *
e0b582ec 705 * It is used by cpumask_of() to get a constant address to a CPU
e56b3bc7
LT
706 * mask value that has a single bit set only.
707 */
b8d317d1 708
e56b3bc7 709/* cpu_bit_bitmap[0] is empty - so we can back into it */
4d51985e 710#define MASK_DECLARE_1(x) [x+1][0] = (1UL << (x))
e56b3bc7
LT
711#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
712#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
713#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
b8d317d1 714
e56b3bc7
LT
715const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
716
717 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
718 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
719#if BITS_PER_LONG > 32
720 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
721 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
b8d317d1
MT
722#endif
723};
e56b3bc7 724EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2d3854a3
RR
725
726const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
727EXPORT_SYMBOL(cpu_all_bits);
b3199c02
RR
728
729#ifdef CONFIG_INIT_ALL_POSSIBLE
730static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly
731 = CPU_BITS_ALL;
732#else
733static DECLARE_BITMAP(cpu_possible_bits, CONFIG_NR_CPUS) __read_mostly;
734#endif
735const struct cpumask *const cpu_possible_mask = to_cpumask(cpu_possible_bits);
736EXPORT_SYMBOL(cpu_possible_mask);
737
738static DECLARE_BITMAP(cpu_online_bits, CONFIG_NR_CPUS) __read_mostly;
739const struct cpumask *const cpu_online_mask = to_cpumask(cpu_online_bits);
740EXPORT_SYMBOL(cpu_online_mask);
741
742static DECLARE_BITMAP(cpu_present_bits, CONFIG_NR_CPUS) __read_mostly;
743const struct cpumask *const cpu_present_mask = to_cpumask(cpu_present_bits);
744EXPORT_SYMBOL(cpu_present_mask);
745
746static DECLARE_BITMAP(cpu_active_bits, CONFIG_NR_CPUS) __read_mostly;
747const struct cpumask *const cpu_active_mask = to_cpumask(cpu_active_bits);
748EXPORT_SYMBOL(cpu_active_mask);
3fa41520
RR
749
750void set_cpu_possible(unsigned int cpu, bool possible)
751{
752 if (possible)
753 cpumask_set_cpu(cpu, to_cpumask(cpu_possible_bits));
754 else
755 cpumask_clear_cpu(cpu, to_cpumask(cpu_possible_bits));
756}
757
758void set_cpu_present(unsigned int cpu, bool present)
759{
760 if (present)
761 cpumask_set_cpu(cpu, to_cpumask(cpu_present_bits));
762 else
763 cpumask_clear_cpu(cpu, to_cpumask(cpu_present_bits));
764}
765
766void set_cpu_online(unsigned int cpu, bool online)
767{
6acbfb96 768 if (online) {
3fa41520 769 cpumask_set_cpu(cpu, to_cpumask(cpu_online_bits));
6acbfb96
LJ
770 cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
771 } else {
3fa41520 772 cpumask_clear_cpu(cpu, to_cpumask(cpu_online_bits));
6acbfb96 773 }
3fa41520
RR
774}
775
776void set_cpu_active(unsigned int cpu, bool active)
777{
778 if (active)
779 cpumask_set_cpu(cpu, to_cpumask(cpu_active_bits));
780 else
781 cpumask_clear_cpu(cpu, to_cpumask(cpu_active_bits));
782}
783
784void init_cpu_present(const struct cpumask *src)
785{
786 cpumask_copy(to_cpumask(cpu_present_bits), src);
787}
788
789void init_cpu_possible(const struct cpumask *src)
790{
791 cpumask_copy(to_cpumask(cpu_possible_bits), src);
792}
793
794void init_cpu_online(const struct cpumask *src)
795{
796 cpumask_copy(to_cpumask(cpu_online_bits), src);
797}