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