blob: bae131a1211b55a9051bfe581dd74df8e72dc248 [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
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -070027/*
28 * Represents all cpu's that are currently online.
29 */
Rusty Russell98a79d62008-12-13 21:19:41 +103030cpumask_t cpu_online_map __read_mostly;
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -070031EXPORT_SYMBOL(cpu_online_map);
32
Rusty Russell98a79d62008-12-13 21:19:41 +103033#ifdef CONFIG_INIT_ALL_POSSIBLE
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -070034cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
Rusty Russell98a79d62008-12-13 21:19:41 +103035#else
36cpumask_t cpu_possible_map __read_mostly;
37#endif
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -070038EXPORT_SYMBOL(cpu_possible_map);
39
Rusty Russell98a79d62008-12-13 21:19:41 +103040#ifdef CONFIG_SMP
Gautham R Shenoyd2219382008-01-25 21:08:01 +010041/* Serializes the updates to cpu_online_map, cpu_present_map */
Linus Torvaldsaa953872006-07-23 12:12:16 -070042static DEFINE_MUTEX(cpu_add_remove_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Neil Brownbd5349c2006-10-17 00:10:35 -070044static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -070046/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
47 * Should always be manipulated under cpu_add_remove_lock
48 */
49static int cpu_hotplug_disabled;
50
Gautham R Shenoyd2219382008-01-25 21:08:01 +010051static struct {
52 struct task_struct *active_writer;
53 struct mutex lock; /* Synchronizes accesses to refcount, */
54 /*
55 * Also blocks the new readers during
56 * an ongoing cpu hotplug operation.
57 */
58 int refcount;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010059} cpu_hotplug;
Ashok Raj90d45d12005-11-08 21:34:24 -080060
Gautham R Shenoyd2219382008-01-25 21:08:01 +010061void __init cpu_hotplug_init(void)
62{
63 cpu_hotplug.active_writer = NULL;
64 mutex_init(&cpu_hotplug.lock);
65 cpu_hotplug.refcount = 0;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010066}
67
Max Krasnyanskye761b772008-07-15 04:43:49 -070068cpumask_t cpu_active_map;
69
Gautham R Shenoyd2219382008-01-25 21:08:01 +010070#ifdef CONFIG_HOTPLUG_CPU
Ashok Raj90d45d12005-11-08 21:34:24 -080071
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010072void get_online_cpus(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -080073{
Gautham R Shenoyd2219382008-01-25 21:08:01 +010074 might_sleep();
75 if (cpu_hotplug.active_writer == current)
Linus Torvaldsaa953872006-07-23 12:12:16 -070076 return;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010077 mutex_lock(&cpu_hotplug.lock);
78 cpu_hotplug.refcount++;
79 mutex_unlock(&cpu_hotplug.lock);
80
Ashok Raja9d9baa2005-11-28 13:43:46 -080081}
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010082EXPORT_SYMBOL_GPL(get_online_cpus);
Ashok Raj90d45d12005-11-08 21:34:24 -080083
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010084void put_online_cpus(void)
Ashok Raja9d9baa2005-11-28 13:43:46 -080085{
Gautham R Shenoyd2219382008-01-25 21:08:01 +010086 if (cpu_hotplug.active_writer == current)
Linus Torvaldsaa953872006-07-23 12:12:16 -070087 return;
Gautham R Shenoyd2219382008-01-25 21:08:01 +010088 mutex_lock(&cpu_hotplug.lock);
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -070089 if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
90 wake_up_process(cpu_hotplug.active_writer);
Gautham R Shenoyd2219382008-01-25 21:08:01 +010091 mutex_unlock(&cpu_hotplug.lock);
92
Ashok Raja9d9baa2005-11-28 13:43:46 -080093}
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +010094EXPORT_SYMBOL_GPL(put_online_cpus);
Ashok Raja9d9baa2005-11-28 13:43:46 -080095
Ashok Raja9d9baa2005-11-28 13:43:46 -080096#endif /* CONFIG_HOTPLUG_CPU */
Ashok Raj90d45d12005-11-08 21:34:24 -080097
Gautham R Shenoyd2219382008-01-25 21:08:01 +010098/*
99 * The following two API's must be used when attempting
100 * to serialize the updates to cpu_online_map, cpu_present_map.
101 */
102void cpu_maps_update_begin(void)
103{
104 mutex_lock(&cpu_add_remove_lock);
105}
106
107void cpu_maps_update_done(void)
108{
109 mutex_unlock(&cpu_add_remove_lock);
110}
111
112/*
113 * This ensures that the hotplug operation can begin only when the
114 * refcount goes to zero.
115 *
116 * Note that during a cpu-hotplug operation, the new readers, if any,
117 * will be blocked by the cpu_hotplug.lock
118 *
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700119 * Since cpu_hotplug_begin() is always called after invoking
120 * cpu_maps_update_begin(), we can be sure that only one writer is active.
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100121 *
122 * Note that theoretically, there is a possibility of a livelock:
123 * - Refcount goes to zero, last reader wakes up the sleeping
124 * writer.
125 * - Last reader unlocks the cpu_hotplug.lock.
126 * - A new reader arrives at this moment, bumps up the refcount.
127 * - The writer acquires the cpu_hotplug.lock finds the refcount
128 * non zero and goes to sleep again.
129 *
130 * However, this is very difficult to achieve in practice since
Gautham R Shenoy86ef5c92008-01-25 21:08:02 +0100131 * get_online_cpus() not an api which is called all that often.
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100132 *
133 */
134static void cpu_hotplug_begin(void)
135{
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100136 cpu_hotplug.active_writer = current;
Oleg Nesterovd2ba7e22008-04-29 01:00:29 -0700137
138 for (;;) {
139 mutex_lock(&cpu_hotplug.lock);
140 if (likely(!cpu_hotplug.refcount))
141 break;
142 __set_current_state(TASK_UNINTERRUPTIBLE);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100143 mutex_unlock(&cpu_hotplug.lock);
144 schedule();
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100145 }
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100146}
147
148static void cpu_hotplug_done(void)
149{
150 cpu_hotplug.active_writer = NULL;
151 mutex_unlock(&cpu_hotplug.lock);
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Need to know about CPUs going up/down? */
Sam Ravnborgf7b16c12008-04-29 00:58:51 -0700154int __ref register_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Neil Brownbd5349c2006-10-17 00:10:35 -0700156 int ret;
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100157 cpu_maps_update_begin();
Neil Brownbd5349c2006-10-17 00:10:35 -0700158 ret = raw_notifier_chain_register(&cpu_chain, nb);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100159 cpu_maps_update_done();
Neil Brownbd5349c2006-10-17 00:10:35 -0700160 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
Chandra Seetharaman65edc682006-06-27 02:54:08 -0700162
163#ifdef CONFIG_HOTPLUG_CPU
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165EXPORT_SYMBOL(register_cpu_notifier);
166
Sam Ravnborg96471552008-04-29 00:58:48 -0700167void __ref unregister_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100169 cpu_maps_update_begin();
Neil Brownbd5349c2006-10-17 00:10:35 -0700170 raw_notifier_chain_unregister(&cpu_chain, nb);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100171 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173EXPORT_SYMBOL(unregister_cpu_notifier);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static inline void check_for_tasks(int cpu)
176{
177 struct task_struct *p;
178
179 write_lock_irq(&tasklist_lock);
180 for_each_process(p) {
181 if (task_cpu(p) == cpu &&
182 (!cputime_eq(p->utime, cputime_zero) ||
183 !cputime_eq(p->stime, cputime_zero)))
184 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
Heiko Carstense7407dc2007-05-09 02:34:04 -0700185 (state = %ld, flags = %x) \n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700186 p->comm, task_pid_nr(p), cpu,
187 p->state, p->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 write_unlock_irq(&tasklist_lock);
190}
191
Avi Kivitydb912f92007-05-24 12:23:10 +0300192struct take_cpu_down_param {
193 unsigned long mod;
194 void *hcpu;
195};
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* Take this CPU down. */
Sam Ravnborg514a20a2008-04-29 00:58:50 -0700198static int __ref take_cpu_down(void *_param)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Avi Kivitydb912f92007-05-24 12:23:10 +0300200 struct take_cpu_down_param *param = _param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int err;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* Ensure this CPU doesn't handle any more interrupts. */
204 err = __cpu_disable();
205 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700206 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Manfred Spraul3ba35572008-08-31 19:58:49 +0200208 raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
209 param->hcpu);
210
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700211 /* Force idle task to run as soon as we yield: it should
212 immediately notice cpu is offline and die quickly. */
213 sched_idle_next();
214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700217/* Requires cpu_add_remove_lock to be held */
Sam Ravnborg514a20a2008-04-29 00:58:50 -0700218static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Heiko Carstense7407dc2007-05-09 02:34:04 -0700220 int err, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 cpumask_t old_allowed, tmp;
Heiko Carstense7407dc2007-05-09 02:34:04 -0700222 void *hcpu = (void *)(long)cpu;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700223 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
Avi Kivitydb912f92007-05-24 12:23:10 +0300224 struct take_cpu_down_param tcd_param = {
225 .mod = mod,
226 .hcpu = hcpu,
227 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700229 if (num_online_cpus() == 1)
230 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700232 if (!cpu_online(cpu))
233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100235 cpu_hotplug_begin();
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700236 err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
Heiko Carstense7407dc2007-05-09 02:34:04 -0700237 hcpu, -1, &nr_calls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (err == NOTIFY_BAD) {
Akinobu Mitaa0d8cdb2007-10-18 03:05:12 -0700239 nr_calls--;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700240 __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
241 hcpu, nr_calls, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 printk("%s: attempt to take down CPU %u failed\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700243 __func__, cpu);
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700244 err = -EINVAL;
245 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
248 /* Ensure that we are not runnable on dying cpu */
249 old_allowed = current->cpus_allowed;
Mike Travisf70316d2008-04-04 18:11:06 -0700250 cpus_setall(tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 cpu_clear(cpu, tmp);
Mike Travisf70316d2008-04-04 18:11:06 -0700252 set_cpus_allowed_ptr(current, &tmp);
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500253 tmp = cpumask_of_cpu(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Rusty Russelleeec4fa2008-07-28 12:16:30 -0500255 err = __stop_machine(take_cpu_down, &tcd_param, &tmp);
Rusty Russell04321582008-07-28 12:16:29 -0500256 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* CPU didn't die: tell everyone. Can't complain. */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700258 if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
Heiko Carstense7407dc2007-05-09 02:34:04 -0700259 hcpu) == NOTIFY_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 BUG();
261
Rusty Russellffdb5972008-07-28 12:16:28 -0500262 goto out_allowed;
Satoru Takeuchi8fa1d7d2006-10-28 10:38:57 -0700263 }
Rusty Russell04321582008-07-28 12:16:29 -0500264 BUG_ON(cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Wait for it to sleep (leaving idle task). */
267 while (!idle_cpu(cpu))
268 yield();
269
270 /* This actually kills the CPU. */
271 __cpu_die(cpu);
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* CPU is completely dead: tell everyone. Too late to complain. */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700274 if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
275 hcpu) == NOTIFY_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 BUG();
277
278 check_for_tasks(cpu);
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280out_allowed:
Mike Travisf70316d2008-04-04 18:11:06 -0700281 set_cpus_allowed_ptr(current, &old_allowed);
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700282out_release:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100283 cpu_hotplug_done();
Oleg Nesterov3da1c842008-07-25 01:47:50 -0700284 if (!err) {
285 if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
286 hcpu) == NOTIFY_BAD)
287 BUG();
288 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700289 return err;
290}
291
Sam Ravnborg514a20a2008-04-29 00:58:50 -0700292int __ref cpu_down(unsigned int cpu)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700293{
294 int err = 0;
295
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100296 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700297
Max Krasnyanskye761b772008-07-15 04:43:49 -0700298 if (cpu_hotplug_disabled) {
299 err = -EBUSY;
300 goto out;
301 }
302
303 cpu_clear(cpu, cpu_active_map);
304
Max Krasnyansky39b0fad2008-07-15 20:56:26 -0700305 /*
306 * Make sure the all cpus did the reschedule and are not
307 * using stale version of the cpu_active_map.
308 * This is not strictly necessary becuase stop_machine()
309 * that we run down the line already provides the required
310 * synchronization. But it's really a side effect and we do not
311 * want to depend on the innards of the stop_machine here.
312 */
313 synchronize_sched();
314
Max Krasnyanskye761b772008-07-15 04:43:49 -0700315 err = _cpu_down(cpu, 0);
316
317 if (cpu_online(cpu))
318 cpu_set(cpu, cpu_active_map);
319
320out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100321 cpu_maps_update_done();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return err;
323}
Zhang Ruib62b8ef2008-04-29 02:35:56 -0400324EXPORT_SYMBOL(cpu_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#endif /*CONFIG_HOTPLUG_CPU*/
326
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700327/* Requires cpu_add_remove_lock to be held */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700328static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700330 int ret, nr_calls = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 void *hcpu = (void *)(long)cpu;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700332 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700334 if (cpu_online(cpu) || !cpu_present(cpu))
335 return -EINVAL;
Ashok Raj90d45d12005-11-08 21:34:24 -0800336
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100337 cpu_hotplug_begin();
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700338 ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700339 -1, &nr_calls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (ret == NOTIFY_BAD) {
Akinobu Mitaa0d8cdb2007-10-18 03:05:12 -0700341 nr_calls--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 printk("%s: attempt to bring up CPU %u failed\n",
Harvey Harrisonaf1f16d2008-04-30 00:55:08 -0700343 __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 ret = -EINVAL;
345 goto out_notify;
346 }
347
348 /* Arch-specific enabling code. */
349 ret = __cpu_up(cpu);
350 if (ret != 0)
351 goto out_notify;
Eric Sesterhenn6978c702006-03-24 18:45:21 +0100352 BUG_ON(!cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dmitry Adamushko279ef6b2008-07-30 12:34:04 +0200354 cpu_set(cpu, cpu_active_map);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* Now call notifier in preparation. */
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700357 raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359out_notify:
360 if (ret != 0)
Gautham R Shenoybaaca492007-05-09 02:34:03 -0700361 __raw_notifier_call_chain(&cpu_chain,
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700362 CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100363 cpu_hotplug_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ret;
366}
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700367
Gautham R Shenoyb282b6f2007-01-10 23:15:34 -0800368int __cpuinit cpu_up(unsigned int cpu)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700369{
370 int err = 0;
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -0700371 if (!cpu_isset(cpu, cpu_possible_map)) {
372 printk(KERN_ERR "can't online cpu %d because it is not "
373 "configured as may-hotadd at boot time\n", cpu);
Heiko Carstens3ee10622008-08-12 15:08:40 -0700374#if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
KAMEZAWA Hiroyuki73e753a2007-10-18 23:40:47 -0700375 printk(KERN_ERR "please check additional_cpus= boot "
376 "parameter\n");
377#endif
378 return -EINVAL;
379 }
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700380
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100381 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700382
Max Krasnyanskye761b772008-07-15 04:43:49 -0700383 if (cpu_hotplug_disabled) {
384 err = -EBUSY;
385 goto out;
386 }
387
388 err = _cpu_up(cpu, 0);
389
Max Krasnyanskye761b772008-07-15 04:43:49 -0700390out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100391 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700392 return err;
393}
394
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700395#ifdef CONFIG_PM_SLEEP_SMP
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700396static cpumask_t frozen_cpus;
397
398int disable_nonboot_cpus(void)
399{
Ingo Molnare1d9fd22006-12-23 16:55:29 +0100400 int cpu, first_cpu, error = 0;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700401
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100402 cpu_maps_update_begin();
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700403 first_cpu = first_cpu(cpu_online_map);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700404 /* We take down all of the non-boot CPUs in one shot to avoid races
405 * with the userspace trying to use the CPU hotplug at the same time
406 */
407 cpus_clear(frozen_cpus);
408 printk("Disabling non-boot CPUs ...\n");
409 for_each_online_cpu(cpu) {
410 if (cpu == first_cpu)
411 continue;
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700412 error = _cpu_down(cpu, 1);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700413 if (!error) {
414 cpu_set(cpu, frozen_cpus);
415 printk("CPU%d is down\n", cpu);
416 } else {
417 printk(KERN_ERR "Error taking CPU%d down: %d\n",
418 cpu, error);
419 break;
420 }
421 }
422 if (!error) {
423 BUG_ON(num_online_cpus() > 1);
424 /* Make sure the CPUs won't be enabled by someone else */
425 cpu_hotplug_disabled = 1;
426 } else {
Ingo Molnare1d9fd22006-12-23 16:55:29 +0100427 printk(KERN_ERR "Non-boot CPUs are not disabled\n");
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700428 }
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100429 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700430 return error;
431}
432
Sam Ravnborgfa7303e2008-02-08 04:21:55 -0800433void __ref enable_nonboot_cpus(void)
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700434{
435 int cpu, error;
436
437 /* Allow everyone to use the CPU hotplug again */
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100438 cpu_maps_update_begin();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700439 cpu_hotplug_disabled = 0;
Rafael J. Wysockied746e32007-02-10 01:43:32 -0800440 if (cpus_empty(frozen_cpus))
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700441 goto out;
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700442
443 printk("Enabling non-boot CPUs ...\n");
Mike Travis363ab6f2008-05-12 21:21:13 +0200444 for_each_cpu_mask_nr(cpu, frozen_cpus) {
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700445 error = _cpu_up(cpu, 1);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700446 if (!error) {
447 printk("CPU%d is up\n", cpu);
448 continue;
449 }
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700450 printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700451 }
452 cpus_clear(frozen_cpus);
Rafael J. Wysocki1d64b9c2007-04-01 23:49:49 -0700453out:
Gautham R Shenoyd2219382008-01-25 21:08:01 +0100454 cpu_maps_update_done();
Rafael J. Wysockie3920fb2006-09-25 23:32:48 -0700455}
Rafael J. Wysockif3de4be2007-08-30 23:56:29 -0700456#endif /* CONFIG_PM_SLEEP_SMP */
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -0700457
Manfred Spraule545a612008-09-07 16:57:22 +0200458/**
459 * notify_cpu_starting(cpu) - call the CPU_STARTING notifiers
460 * @cpu: cpu that just started
461 *
462 * This function calls the cpu_chain notifiers with CPU_STARTING.
463 * It must be called by the arch code on the new cpu, before the new cpu
464 * enables interrupts and before the "boot" cpu returns from __cpu_up().
465 */
Al Viro84196412008-11-22 17:36:44 +0000466void __cpuinit notify_cpu_starting(unsigned int cpu)
Manfred Spraule545a612008-09-07 16:57:22 +0200467{
468 unsigned long val = CPU_STARTING;
469
470#ifdef CONFIG_PM_SLEEP_SMP
471 if (cpu_isset(cpu, frozen_cpus))
472 val = CPU_STARTING_FROZEN;
473#endif /* CONFIG_PM_SLEEP_SMP */
474 raw_notifier_call_chain(&cpu_chain, val, (void *)(long)cpu);
475}
476
Max Krasnyansky68f4f1e2008-05-29 11:17:02 -0700477#endif /* CONFIG_SMP */
Mike Travisb8d317d2008-07-24 18:21:29 -0700478
Linus Torvaldse56b3bc2008-07-28 11:32:33 -0700479/*
480 * cpu_bit_bitmap[] is a special, "compressed" data structure that
481 * represents all NR_CPUS bits binary values of 1<<nr.
482 *
483 * It is used by cpumask_of_cpu() to get a constant address to a CPU
484 * mask value that has a single bit set only.
485 */
Mike Travisb8d317d2008-07-24 18:21:29 -0700486
Linus Torvaldse56b3bc2008-07-28 11:32:33 -0700487/* cpu_bit_bitmap[0] is empty - so we can back into it */
488#define MASK_DECLARE_1(x) [x+1][0] = 1UL << (x)
489#define MASK_DECLARE_2(x) MASK_DECLARE_1(x), MASK_DECLARE_1(x+1)
490#define MASK_DECLARE_4(x) MASK_DECLARE_2(x), MASK_DECLARE_2(x+2)
491#define MASK_DECLARE_8(x) MASK_DECLARE_4(x), MASK_DECLARE_4(x+4)
Mike Travisb8d317d2008-07-24 18:21:29 -0700492
Linus Torvaldse56b3bc2008-07-28 11:32:33 -0700493const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
Mike Travisb8d317d2008-07-24 18:21:29 -0700494
Linus Torvaldse56b3bc2008-07-28 11:32:33 -0700495 MASK_DECLARE_8(0), MASK_DECLARE_8(8),
496 MASK_DECLARE_8(16), MASK_DECLARE_8(24),
497#if BITS_PER_LONG > 32
498 MASK_DECLARE_8(32), MASK_DECLARE_8(40),
499 MASK_DECLARE_8(48), MASK_DECLARE_8(56),
Mike Travisb8d317d2008-07-24 18:21:29 -0700500#endif
501};
Linus Torvaldse56b3bc2008-07-28 11:32:33 -0700502EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
Rusty Russell2d3854a2008-11-05 13:39:10 +1100503
504const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
505EXPORT_SYMBOL(cpu_all_bits);