blob: a1ac7ea245d7ba96727ae3647552564cefab120d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
13#include <linux/module.h>
14#include <linux/kthread.h>
15#include <linux/stop_machine.h>
Ingo Molnar81615b62006-06-26 00:24:32 -070016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -070018/*
19 * Represents all cpu's present in the system
20 * In systems capable of hotplug, this map could dynamically grow
21 * as new cpu's are detected in the system via any platform specific
22 * method, such as ACPI for e.g.
23 */
24cpumask_t cpu_present_map __read_mostly;
25EXPORT_SYMBOL(cpu_present_map);
26
27#ifndef CONFIG_SMP
28
29/*
30 * Represents all cpu's that are currently online.
31 */
32cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
33EXPORT_SYMBOL(cpu_online_map);
34
35cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
36EXPORT_SYMBOL(cpu_possible_map);
37
38#else /* CONFIG_SMP */
39
Gautham R Shenoyd2219382008-01-25 21:08:01 +010040/* Serializes the updates to cpu_online_map, cpu_present_map */
Linus Torvaldsaa953872006-07-23 12:12:16 -070041static DEFINE_MUTEX(cpu_add_remove_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Neil Brownbd5349c2006-10-17 00:10:35 -070043static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070045/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
46 * Should always be manipulated under cpu_add_remove_lock
47 */
48static int cpu_hotplug_disabled;
49
Gautham R Shenoyd2219382008-01-25 21:08:01 +010050static struct {
51 struct task_struct *active_writer;
52 struct mutex lock; /* Synchronizes accesses to refcount, */
53 /*
54 * Also blocks the new readers during
55 * an ongoing cpu hotplug operation.
56 */
57 int refcount;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010058} cpu_hotplug;
Ashok Raj90d45d12005-11-08 21:34:24 -080059
Gautham R Shenoyd2219382008-01-25 21:08:01 +010060void __init cpu_hotplug_init(void)
61{
62 cpu_hotplug.active_writer = NULL;
63 mutex_init(&cpu_hotplug.lock);
64 cpu_hotplug.refcount = 0;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010065}
66
Max Krasnyanskye761b772008-07-15 04:43:49 -070067cpumask_t cpu_active_map;
68
Gautham R Shenoyd2219382008-01-25 21:08:01 +010069#ifdef CONFIG_HOTPLUG_CPU
Ashok Raj90d45d12005-11-08 21:34:24 -080070
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010071void get_online_cpus(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -080072{
Gautham R Shenoyd2219382008-01-25 21:08:01 +010073 might_sleep();
74 if (cpu_hotplug.active_writer == current)
Linus Torvaldsaa953872006-07-23 12:12:16 -070075 return;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010076 mutex_lock(&cpu_hotplug.lock);
77 cpu_hotplug.refcount++;
78 mutex_unlock(&cpu_hotplug.lock);
79
Ashok Raja9d9baa2005-11-28 13:43:46 -080080}
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010081EXPORT_SYMBOL_GPL(get_online_cpus);
Ashok Raj90d45d12005-11-08 21:34:24 -080082
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010083void put_online_cpus(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -080084{
Gautham R Shenoyd2219382008-01-25 21:08:01 +010085 if (cpu_hotplug.active_writer == current)
Linus Torvaldsaa953872006-07-23 12:12:16 -070086 return;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010087 mutex_lock(&cpu_hotplug.lock);
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -070088 if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
89 wake_up_process(cpu_hotplug.active_writer);
Gautham R Shenoyd2219382008-01-25 21:08:01 +010090 mutex_unlock(&cpu_hotplug.lock);
91
Ashok Raja9d9baa2005-11-28 13:43:46 -080092}
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010093EXPORT_SYMBOL_GPL(put_online_cpus);
Ashok Raja9d9baa2005-11-28 13:43:46 -080094
Ashok Raja9d9baa2005-11-28 13:43:46 -080095#endif /* CONFIG_HOTPLUG_CPU */
Ashok Raj90d45d12005-11-08 21:34:24 -080096
Gautham R Shenoyd2219382008-01-25 21:08:01 +010097/*
98 * The following two API's must be used when attempting
99 * to serialize the updates to cpu_online_map, cpu_present_map.
100 */
101void cpu_maps_update_begin(void)
102{
103 mutex_lock(&cpu_add_remove_lock);
104}
105
106void cpu_maps_update_done(void)
107{
108 mutex_unlock(&cpu_add_remove_lock);
109}
110
111/*
112 * This ensures that the hotplug operation can begin only when the
113 * refcount goes to zero.
114 *
115 * Note that during a cpu-hotplug operation, the new readers, if any,
116 * will be blocked by the cpu_hotplug.lock
117 *
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700118 * Since cpu_hotplug_begin() is always called after invoking
119 * cpu_maps_update_begin(), we can be sure that only one writer is active.
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100120 *
121 * Note that theoretically, there is a possibility of a livelock:
122 * - Refcount goes to zero, last reader wakes up the sleeping
123 * writer.
124 * - Last reader unlocks the cpu_hotplug.lock.
125 * - A new reader arrives at this moment, bumps up the refcount.
126 * - The writer acquires the cpu_hotplug.lock finds the refcount
127 * non zero and goes to sleep again.
128 *
129 * However, this is very difficult to achieve in practice since
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100130 * get_online_cpus() not an api which is called all that often.
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100131 *
132 */
133static void cpu_hotplug_begin(void)
134{
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100135 cpu_hotplug.active_writer = current;
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700136
137 for (;;) {
138 mutex_lock(&cpu_hotplug.lock);
139 if (likely(!cpu_hotplug.refcount))
140 break;
141 __set_current_state(TASK_UNINTERRUPTIBLE);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100142 mutex_unlock(&cpu_hotplug.lock);
143 schedule();
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100144 }
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100145}
146
147static void cpu_hotplug_done(void)
148{
149 cpu_hotplug.active_writer = NULL;
150 mutex_unlock(&cpu_hotplug.lock);
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Need to know about CPUs going up/down? */
Sam Ravnborgf7b16c12008-04-29 00:58:51 -0700153int __ref register_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Neil Brownbd5349c2006-10-17 00:10:35 -0700155 int ret;
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100156 cpu_maps_update_begin();
Neil Brownbd5349c2006-10-17 00:10:35 -0700157 ret = raw_notifier_chain_register(&cpu_chain, nb);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100158 cpu_maps_update_done();
Neil Brownbd5349c2006-10-17 00:10:35 -0700159 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
Chandra Seetharaman65edc682006-06-27 02:54:08 -0700161
162#ifdef CONFIG_HOTPLUG_CPU
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164EXPORT_SYMBOL(register_cpu_notifier);
165
Sam Ravnborg96471552008-04-29 00:58:48 -0700166void __ref unregister_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100168 cpu_maps_update_begin();
Neil Brownbd5349c2006-10-17 00:10:35 -0700169 raw_notifier_chain_unregister(&cpu_chain, nb);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100170 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172EXPORT_SYMBOL(unregister_cpu_notifier);
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static inline void check_for_tasks(int cpu)
175{
176 struct task_struct *p;
177
178 write_lock_irq(&tasklist_lock);
179 for_each_process(p) {
180 if (task_cpu(p) == cpu &&
181 (!cputime_eq(p->utime, cputime_zero) ||
182 !cputime_eq(p->stime, cputime_zero)))
183 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
Heiko Carstense7407dc2007-05-09 02:34:04 -0700184 (state = %ld, flags = %x) \n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700185 p->comm, task_pid_nr(p), cpu,
186 p->state, p->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 write_unlock_irq(&tasklist_lock);
189}
190
Avi Kivitydb912f92007-05-24 12:23:10 +0300191struct take_cpu_down_param {
192 unsigned long mod;
193 void *hcpu;
194};
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/* Take this CPU down. */
Sam Ravnborg514a20a2008-04-29 00:58:50 -0700197static int __ref take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
Avi Kivitydb912f92007-05-24 12:23:10 +0300199 struct take_cpu_down_param *param = _param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int err;
201
Avi Kivitydb912f92007-05-24 12:23:10 +0300202 raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
203 param->hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Ensure this CPU doesn't handle any more interrupts. */
205 err = __cpu_disable();
206 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700209 /* Force idle task to run as soon as we yield: it should
210 immediately notice cpu is offline and die quickly. */
211 sched_idle_next();
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700215/* Requires cpu_add_remove_lock to be held */
Sam Ravnborg514a20a2008-04-29 00:58:50 -0700216static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Heiko Carstense7407dc2007-05-09 02:34:04 -0700218 int err, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct task_struct *p;
220 cpumask_t old_allowed, tmp;
Heiko Carstense7407dc2007-05-09 02:34:04 -0700221 void *hcpu = (void *)(long)cpu;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700222 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
Avi Kivitydb912f92007-05-24 12:23:10 +0300223 struct take_cpu_down_param tcd_param = {
224 .mod = mod,
225 .hcpu = hcpu,
226 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700228 if (num_online_cpus() == 1)
229 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700231 if (!cpu_online(cpu))
232 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100234 cpu_hotplug_begin();
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700235 err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
Heiko Carstense7407dc2007-05-09 02:34:04 -0700236 hcpu, -1, &nr_calls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (err == NOTIFY_BAD) {
Akinobu Mitaa0d8cdb2007-10-18 03:05:12 -0700238 nr_calls--;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700239 __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
240 hcpu, nr_calls, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 printk("%s: attempt to take down CPU %u failed\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700242 __func__, cpu);
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700243 err = -EINVAL;
244 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 /* Ensure that we are not runnable on dying cpu */
248 old_allowed = current->cpus_allowed;
Mike Travisf70316d2008-04-04 18:11:06 -0700249 cpus_setall(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cpu_clear(cpu, tmp);
Mike Travisf70316d2008-04-04 18:11:06 -0700251 set_cpus_allowed_ptr(current, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Avi Kivitydb912f92007-05-24 12:23:10 +0300253 p = __stop_machine_run(take_cpu_down, &tcd_param, cpu);
Linus Torvaldsaa953872006-07-23 12:12:16 -0700254
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700255 if (IS_ERR(p) || cpu_online(cpu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* CPU didn't die: tell everyone. Can't complain. */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700257 if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
Heiko Carstense7407dc2007-05-09 02:34:04 -0700258 hcpu) == NOTIFY_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 BUG();
260
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700261 if (IS_ERR(p)) {
262 err = PTR_ERR(p);
263 goto out_allowed;
264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 goto out_thread;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 /* Wait for it to sleep (leaving idle task). */
269 while (!idle_cpu(cpu))
270 yield();
271
272 /* This actually kills the CPU. */
273 __cpu_die(cpu);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* CPU is completely dead: tell everyone. Too late to complain. */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700276 if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
277 hcpu) == NOTIFY_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 BUG();
279
280 check_for_tasks(cpu);
281
282out_thread:
283 err = kthread_stop(p);
284out_allowed:
Mike Travisf70316d2008-04-04 18:11:06 -0700285 set_cpus_allowed_ptr(current, &old_allowed);
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700286out_release:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100287 cpu_hotplug_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700288 return err;
289}
290
Sam Ravnborg514a20a2008-04-29 00:58:50 -0700291int __ref cpu_down(unsigned int cpu)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700292{
293 int err = 0;
294
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100295 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700296
Max Krasnyanskye761b772008-07-15 04:43:49 -0700297 if (cpu_hotplug_disabled) {
298 err = -EBUSY;
299 goto out;
300 }
301
302 cpu_clear(cpu, cpu_active_map);
303
304 err = _cpu_down(cpu, 0);
305
306 if (cpu_online(cpu))
307 cpu_set(cpu, cpu_active_map);
308
309out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100310 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return err;
312}
Zhang Ruib62b8ef2008-04-29 02:35:56 -0400313EXPORT_SYMBOL(cpu_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314#endif /*CONFIG_HOTPLUG_CPU*/
315
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700316/* Requires cpu_add_remove_lock to be held */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700317static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700319 int ret, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 void *hcpu = (void *)(long)cpu;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700321 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700323 if (cpu_online(cpu) || !cpu_present(cpu))
324 return -EINVAL;
Ashok Raj90d45d12005-11-08 21:34:24 -0800325
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100326 cpu_hotplug_begin();
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700327 ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700328 -1, &nr_calls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (ret == NOTIFY_BAD) {
Akinobu Mitaa0d8cdb2007-10-18 03:05:12 -0700330 nr_calls--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 printk("%s: attempt to bring up CPU %u failed\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700332 __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 ret = -EINVAL;
334 goto out_notify;
335 }
336
337 /* Arch-specific enabling code. */
338 ret = __cpu_up(cpu);
339 if (ret != 0)
340 goto out_notify;
Eric Sesterhenn6978c702006-03-24 18:45:21 +0100341 BUG_ON(!cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Now call notifier in preparation. */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700344 raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346out_notify:
347 if (ret != 0)
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700348 __raw_notifier_call_chain(&cpu_chain,
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700349 CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100350 cpu_hotplug_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
353}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700354
Gautham R Shenoyb282b6f2007-01-10 23:15:34 -0800355int __cpuinit cpu_up(unsigned int cpu)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700356{
357 int err = 0;
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -0700358 if (!cpu_isset(cpu, cpu_possible_map)) {
359 printk(KERN_ERR "can't online cpu %d because it is not "
360 "configured as may-hotadd at boot time\n", cpu);
361#if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390)
362 printk(KERN_ERR "please check additional_cpus= boot "
363 "parameter\n");
364#endif
365 return -EINVAL;
366 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700367
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100368 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700369
Max Krasnyanskye761b772008-07-15 04:43:49 -0700370 if (cpu_hotplug_disabled) {
371 err = -EBUSY;
372 goto out;
373 }
374
375 err = _cpu_up(cpu, 0);
376
377 if (cpu_online(cpu))
378 cpu_set(cpu, cpu_active_map);
379
380out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100381 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700382 return err;
383}
384
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700385#ifdef CONFIG_PM_SLEEP_SMP
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700386static cpumask_t frozen_cpus;
387
388int disable_nonboot_cpus(void)
389{
Ingo Molnare1d9fd22006-12-23 16:55:29 +0100390 int cpu, first_cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700391
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100392 cpu_maps_update_begin();
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700393 first_cpu = first_cpu(cpu_online_map);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700394 /* We take down all of the non-boot CPUs in one shot to avoid races
395 * with the userspace trying to use the CPU hotplug at the same time
396 */
397 cpus_clear(frozen_cpus);
398 printk("Disabling non-boot CPUs ...\n");
399 for_each_online_cpu(cpu) {
400 if (cpu == first_cpu)
401 continue;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700402 error = _cpu_down(cpu, 1);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700403 if (!error) {
404 cpu_set(cpu, frozen_cpus);
405 printk("CPU%d is down\n", cpu);
406 } else {
407 printk(KERN_ERR "Error taking CPU%d down: %d\n",
408 cpu, error);
409 break;
410 }
411 }
412 if (!error) {
413 BUG_ON(num_online_cpus() > 1);
414 /* Make sure the CPUs won't be enabled by someone else */
415 cpu_hotplug_disabled = 1;
416 } else {
Ingo Molnare1d9fd22006-12-23 16:55:29 +0100417 printk(KERN_ERR "Non-boot CPUs are not disabled\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700418 }
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100419 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700420 return error;
421}
422
Sam Ravnborgfa7303e2008-02-08 04:21:55 -0800423void __ref enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700424{
425 int cpu, error;
426
427 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100428 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700429 cpu_hotplug_disabled = 0;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800430 if (cpus_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700431 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700432
433 printk("Enabling non-boot CPUs ...\n");
434 for_each_cpu_mask(cpu, frozen_cpus) {
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700435 error = _cpu_up(cpu, 1);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700436 if (!error) {
437 printk("CPU%d is up\n", cpu);
438 continue;
439 }
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700440 printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700441 }
442 cpus_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700443out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100444 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700445}
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700446#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -0700447
448#endif /* CONFIG_SMP */