bitmap: fix seq_bitmap and seq_cpumask to take const pointer
[linux-2.6-block.git] / kernel / cpu.c
CommitLineData
1da177e4
LT
1/* CPU control.
2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
3 *
4 * This code is licenced under the GPL.
5 */
6#include <linux/proc_fs.h>
7#include <linux/smp.h>
8#include <linux/init.h>
9#include <linux/notifier.h>
10#include <linux/sched.h>
11#include <linux/unistd.h>
12#include <linux/cpu.h>
13#include <linux/module.h>
14#include <linux/kthread.h>
15#include <linux/stop_machine.h>
81615b62 16#include <linux/mutex.h>
1da177e4 17
68f4f1ec
MK
18/*
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
68f4f1ec
MK
27/*
28 * Represents all cpu's that are currently online.
29 */
98a79d6a 30cpumask_t cpu_online_map __read_mostly;
68f4f1ec
MK
31EXPORT_SYMBOL(cpu_online_map);
32
98a79d6a 33#ifdef CONFIG_INIT_ALL_POSSIBLE
68f4f1ec 34cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
98a79d6a
RR
35#else
36cpumask_t cpu_possible_map __read_mostly;
37#endif
68f4f1ec
MK
38EXPORT_SYMBOL(cpu_possible_map);
39
98a79d6a 40#ifdef CONFIG_SMP
d221938c 41/* Serializes the updates to cpu_online_map, cpu_present_map */
aa953877 42static DEFINE_MUTEX(cpu_add_remove_lock);
1da177e4 43
bd5349cf 44static __cpuinitdata 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 */
49static int cpu_hotplug_disabled;
50
d221938c
GS
51static 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;
d221938c 59} cpu_hotplug;
90d45d17 60
d221938c
GS
61void __init cpu_hotplug_init(void)
62{
63 cpu_hotplug.active_writer = NULL;
64 mutex_init(&cpu_hotplug.lock);
65 cpu_hotplug.refcount = 0;
d221938c
GS
66}
67
e761b772
MK
68cpumask_t cpu_active_map;
69
d221938c 70#ifdef CONFIG_HOTPLUG_CPU
90d45d17 71
86ef5c9a 72void get_online_cpus(void)
a9d9baa1 73{
d221938c
GS
74 might_sleep();
75 if (cpu_hotplug.active_writer == current)
aa953877 76 return;
d221938c
GS
77 mutex_lock(&cpu_hotplug.lock);
78 cpu_hotplug.refcount++;
79 mutex_unlock(&cpu_hotplug.lock);
80
a9d9baa1 81}
86ef5c9a 82EXPORT_SYMBOL_GPL(get_online_cpus);
90d45d17 83
86ef5c9a 84void put_online_cpus(void)
a9d9baa1 85{
d221938c 86 if (cpu_hotplug.active_writer == current)
aa953877 87 return;
d221938c 88 mutex_lock(&cpu_hotplug.lock);
d2ba7e2a
ON
89 if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
90 wake_up_process(cpu_hotplug.active_writer);
d221938c
GS
91 mutex_unlock(&cpu_hotplug.lock);
92
a9d9baa1 93}
86ef5c9a 94EXPORT_SYMBOL_GPL(put_online_cpus);
a9d9baa1 95
a9d9baa1 96#endif /* CONFIG_HOTPLUG_CPU */
90d45d17 97
d221938c
GS
98/*
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 *
d2ba7e2a
ON
119 * Since cpu_hotplug_begin() is always called after invoking
120 * cpu_maps_update_begin(), we can be sure that only one writer is active.
d221938c
GS
121 *
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
86ef5c9a 131 * get_online_cpus() not an api which is called all that often.
d221938c
GS
132 *
133 */
134static void cpu_hotplug_begin(void)
135{
d221938c 136 cpu_hotplug.active_writer = current;
d2ba7e2a
ON
137
138 for (;;) {
139 mutex_lock(&cpu_hotplug.lock);
140 if (likely(!cpu_hotplug.refcount))
141 break;
142 __set_current_state(TASK_UNINTERRUPTIBLE);
d221938c
GS
143 mutex_unlock(&cpu_hotplug.lock);
144 schedule();
d221938c 145 }
d221938c
GS
146}
147
148static void cpu_hotplug_done(void)
149{
150 cpu_hotplug.active_writer = NULL;
151 mutex_unlock(&cpu_hotplug.lock);
152}
1da177e4 153/* Need to know about CPUs going up/down? */
f7b16c10 154int __ref register_cpu_notifier(struct notifier_block *nb)
1da177e4 155{
bd5349cf 156 int ret;
d221938c 157 cpu_maps_update_begin();
bd5349cf 158 ret = raw_notifier_chain_register(&cpu_chain, nb);
d221938c 159 cpu_maps_update_done();
bd5349cf 160 return ret;
1da177e4 161}
65edc68c
CS
162
163#ifdef CONFIG_HOTPLUG_CPU
164
1da177e4
LT
165EXPORT_SYMBOL(register_cpu_notifier);
166
9647155f 167void __ref unregister_cpu_notifier(struct notifier_block *nb)
1da177e4 168{
d221938c 169 cpu_maps_update_begin();
bd5349cf 170 raw_notifier_chain_unregister(&cpu_chain, nb);
d221938c 171 cpu_maps_update_done();
1da177e4
LT
172}
173EXPORT_SYMBOL(unregister_cpu_notifier);
174
1da177e4
LT
175static 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\
e7407dcc 185 (state = %ld, flags = %x) \n",
ba25f9dc
PE
186 p->comm, task_pid_nr(p), cpu,
187 p->state, p->flags);
1da177e4
LT
188 }
189 write_unlock_irq(&tasklist_lock);
190}
191
db912f96
AK
192struct take_cpu_down_param {
193 unsigned long mod;
194 void *hcpu;
195};
196
1da177e4 197/* Take this CPU down. */
514a20a5 198static int __ref take_cpu_down(void *_param)
1da177e4 199{
db912f96 200 struct take_cpu_down_param *param = _param;
1da177e4
LT
201 int err;
202
1da177e4
LT
203 /* Ensure this CPU doesn't handle any more interrupts. */
204 err = __cpu_disable();
205 if (err < 0)
f3705136 206 return err;
1da177e4 207
3ba35573
MS
208 raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
209 param->hcpu);
210
f3705136
ZM
211 /* 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;
1da177e4
LT
215}
216
e3920fb4 217/* Requires cpu_add_remove_lock to be held */
514a20a5 218static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
1da177e4 219{
e7407dcc 220 int err, nr_calls = 0;
1da177e4 221 cpumask_t old_allowed, tmp;
e7407dcc 222 void *hcpu = (void *)(long)cpu;
8bb78442 223 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
db912f96
AK
224 struct take_cpu_down_param tcd_param = {
225 .mod = mod,
226 .hcpu = hcpu,
227 };
1da177e4 228
e3920fb4
RW
229 if (num_online_cpus() == 1)
230 return -EBUSY;
1da177e4 231
e3920fb4
RW
232 if (!cpu_online(cpu))
233 return -EINVAL;
1da177e4 234
d221938c 235 cpu_hotplug_begin();
8bb78442 236 err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
e7407dcc 237 hcpu, -1, &nr_calls);
1da177e4 238 if (err == NOTIFY_BAD) {
a0d8cdb6 239 nr_calls--;
8bb78442
RW
240 __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
241 hcpu, nr_calls, NULL);
1da177e4 242 printk("%s: attempt to take down CPU %u failed\n",
af1f16d0 243 __func__, cpu);
baaca49f
GS
244 err = -EINVAL;
245 goto out_release;
1da177e4
LT
246 }
247
248 /* Ensure that we are not runnable on dying cpu */
249 old_allowed = current->cpus_allowed;
f70316da 250 cpus_setall(tmp);
1da177e4 251 cpu_clear(cpu, tmp);
f70316da 252 set_cpus_allowed_ptr(current, &tmp);
eeec4fad 253 tmp = cpumask_of_cpu(cpu);
1da177e4 254
eeec4fad 255 err = __stop_machine(take_cpu_down, &tcd_param, &tmp);
04321587 256 if (err) {
1da177e4 257 /* CPU didn't die: tell everyone. Can't complain. */
8bb78442 258 if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
e7407dcc 259 hcpu) == NOTIFY_BAD)
1da177e4
LT
260 BUG();
261
ffdb5976 262 goto out_allowed;
8fa1d7d3 263 }
04321587 264 BUG_ON(cpu_online(cpu));
1da177e4
LT
265
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
1da177e4 273 /* CPU is completely dead: tell everyone. Too late to complain. */
8bb78442
RW
274 if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
275 hcpu) == NOTIFY_BAD)
1da177e4
LT
276 BUG();
277
278 check_for_tasks(cpu);
279
1da177e4 280out_allowed:
f70316da 281 set_cpus_allowed_ptr(current, &old_allowed);
baaca49f 282out_release:
d221938c 283 cpu_hotplug_done();
3da1c84c
ON
284 if (!err) {
285 if (raw_notifier_call_chain(&cpu_chain, CPU_POST_DEAD | mod,
286 hcpu) == NOTIFY_BAD)
287 BUG();
288 }
e3920fb4
RW
289 return err;
290}
291
514a20a5 292int __ref cpu_down(unsigned int cpu)
e3920fb4
RW
293{
294 int err = 0;
295
d221938c 296 cpu_maps_update_begin();
e761b772
MK
297
298 if (cpu_hotplug_disabled) {
e3920fb4 299 err = -EBUSY;
e761b772
MK
300 goto out;
301 }
302
303 cpu_clear(cpu, cpu_active_map);
304
39b0fad7
MK
305 /*
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();
e3920fb4 314
e761b772 315 err = _cpu_down(cpu, 0);
e3920fb4 316
e761b772
MK
317 if (cpu_online(cpu))
318 cpu_set(cpu, cpu_active_map);
319
320out:
d221938c 321 cpu_maps_update_done();
1da177e4
LT
322 return err;
323}
b62b8ef9 324EXPORT_SYMBOL(cpu_down);
1da177e4
LT
325#endif /*CONFIG_HOTPLUG_CPU*/
326
e3920fb4 327/* Requires cpu_add_remove_lock to be held */
8bb78442 328static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
1da177e4 329{
baaca49f 330 int ret, nr_calls = 0;
1da177e4 331 void *hcpu = (void *)(long)cpu;
8bb78442 332 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
1da177e4 333
e3920fb4
RW
334 if (cpu_online(cpu) || !cpu_present(cpu))
335 return -EINVAL;
90d45d17 336
d221938c 337 cpu_hotplug_begin();
8bb78442 338 ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
baaca49f 339 -1, &nr_calls);
1da177e4 340 if (ret == NOTIFY_BAD) {
a0d8cdb6 341 nr_calls--;
1da177e4 342 printk("%s: attempt to bring up CPU %u failed\n",
af1f16d0 343 __func__, cpu);
1da177e4
LT
344 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;
6978c705 352 BUG_ON(!cpu_online(cpu));
1da177e4 353
279ef6bb
DA
354 cpu_set(cpu, cpu_active_map);
355
1da177e4 356 /* Now call notifier in preparation. */
8bb78442 357 raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
1da177e4
LT
358
359out_notify:
360 if (ret != 0)
baaca49f 361 __raw_notifier_call_chain(&cpu_chain,
8bb78442 362 CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
d221938c 363 cpu_hotplug_done();
e3920fb4
RW
364
365 return ret;
366}
367
b282b6f8 368int __cpuinit cpu_up(unsigned int cpu)
e3920fb4
RW
369{
370 int err = 0;
73e753a5
KH
371 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);
3ee1062b 374#if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
73e753a5
KH
375 printk(KERN_ERR "please check additional_cpus= boot "
376 "parameter\n");
377#endif
378 return -EINVAL;
379 }
e3920fb4 380
d221938c 381 cpu_maps_update_begin();
e761b772
MK
382
383 if (cpu_hotplug_disabled) {
e3920fb4 384 err = -EBUSY;
e761b772
MK
385 goto out;
386 }
387
388 err = _cpu_up(cpu, 0);
389
e761b772 390out:
d221938c 391 cpu_maps_update_done();
e3920fb4
RW
392 return err;
393}
394
f3de4be9 395#ifdef CONFIG_PM_SLEEP_SMP
e3920fb4
RW
396static cpumask_t frozen_cpus;
397
398int disable_nonboot_cpus(void)
399{
e1d9fd2e 400 int cpu, first_cpu, error = 0;
e3920fb4 401
d221938c 402 cpu_maps_update_begin();
1d64b9cb 403 first_cpu = first_cpu(cpu_online_map);
e3920fb4
RW
404 /* 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;
8bb78442 412 error = _cpu_down(cpu, 1);
e3920fb4
RW
413 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 {
e1d9fd2e 427 printk(KERN_ERR "Non-boot CPUs are not disabled\n");
e3920fb4 428 }
d221938c 429 cpu_maps_update_done();
e3920fb4
RW
430 return error;
431}
432
fa7303e2 433void __ref enable_nonboot_cpus(void)
e3920fb4
RW
434{
435 int cpu, error;
436
437 /* Allow everyone to use the CPU hotplug again */
d221938c 438 cpu_maps_update_begin();
e3920fb4 439 cpu_hotplug_disabled = 0;
ed746e3b 440 if (cpus_empty(frozen_cpus))
1d64b9cb 441 goto out;
e3920fb4
RW
442
443 printk("Enabling non-boot CPUs ...\n");
363ab6f1 444 for_each_cpu_mask_nr(cpu, frozen_cpus) {
8bb78442 445 error = _cpu_up(cpu, 1);
e3920fb4
RW
446 if (!error) {
447 printk("CPU%d is up\n", cpu);
448 continue;
449 }
1d64b9cb 450 printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
e3920fb4
RW
451 }
452 cpus_clear(frozen_cpus);
1d64b9cb 453out:
d221938c 454 cpu_maps_update_done();
1da177e4 455}
f3de4be9 456#endif /* CONFIG_PM_SLEEP_SMP */
68f4f1ec 457
e545a614
MS
458/**
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 */
84196414 466void __cpuinit notify_cpu_starting(unsigned int cpu)
e545a614
MS
467{
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
68f4f1ec 477#endif /* CONFIG_SMP */
b8d317d1 478
e56b3bc7
LT
479/*
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 */
b8d317d1 486
e56b3bc7
LT
487/* 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)
b8d317d1 492
e56b3bc7
LT
493const unsigned long cpu_bit_bitmap[BITS_PER_LONG+1][BITS_TO_LONGS(NR_CPUS)] = {
494
495 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),
b8d317d1
MT
500#endif
501};
e56b3bc7 502EXPORT_SYMBOL_GPL(cpu_bit_bitmap);
2d3854a3
RR
503
504const DECLARE_BITMAP(cpu_all_bits, NR_CPUS) = CPU_BITS_ALL;
505EXPORT_SYMBOL(cpu_all_bits);