]>
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> | |
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 | */ | |
24 | cpumask_t cpu_present_map __read_mostly; | |
25 | EXPORT_SYMBOL(cpu_present_map); | |
26 | ||
27 | #ifndef CONFIG_SMP | |
28 | ||
29 | /* | |
30 | * Represents all cpu's that are currently online. | |
31 | */ | |
32 | cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL; | |
33 | EXPORT_SYMBOL(cpu_online_map); | |
34 | ||
35 | cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL; | |
36 | EXPORT_SYMBOL(cpu_possible_map); | |
37 | ||
38 | #else /* CONFIG_SMP */ | |
39 | ||
d221938c | 40 | /* Serializes the updates to cpu_online_map, cpu_present_map */ |
aa953877 | 41 | static DEFINE_MUTEX(cpu_add_remove_lock); |
1da177e4 | 42 | |
bd5349cf | 43 | static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain); |
1da177e4 | 44 | |
e3920fb4 RW |
45 | /* If set, cpu_up and cpu_down will return -EBUSY and do nothing. |
46 | * Should always be manipulated under cpu_add_remove_lock | |
47 | */ | |
48 | static int cpu_hotplug_disabled; | |
49 | ||
d221938c GS |
50 | static 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; | |
d221938c | 58 | } cpu_hotplug; |
90d45d17 | 59 | |
d221938c GS |
60 | void __init cpu_hotplug_init(void) |
61 | { | |
62 | cpu_hotplug.active_writer = NULL; | |
63 | mutex_init(&cpu_hotplug.lock); | |
64 | cpu_hotplug.refcount = 0; | |
d221938c GS |
65 | } |
66 | ||
67 | #ifdef CONFIG_HOTPLUG_CPU | |
90d45d17 | 68 | |
86ef5c9a | 69 | void get_online_cpus(void) |
a9d9baa1 | 70 | { |
d221938c GS |
71 | might_sleep(); |
72 | if (cpu_hotplug.active_writer == current) | |
aa953877 | 73 | return; |
d221938c GS |
74 | mutex_lock(&cpu_hotplug.lock); |
75 | cpu_hotplug.refcount++; | |
76 | mutex_unlock(&cpu_hotplug.lock); | |
77 | ||
a9d9baa1 | 78 | } |
86ef5c9a | 79 | EXPORT_SYMBOL_GPL(get_online_cpus); |
90d45d17 | 80 | |
86ef5c9a | 81 | void put_online_cpus(void) |
a9d9baa1 | 82 | { |
d221938c | 83 | if (cpu_hotplug.active_writer == current) |
aa953877 | 84 | return; |
d221938c | 85 | mutex_lock(&cpu_hotplug.lock); |
d2ba7e2a ON |
86 | if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer)) |
87 | wake_up_process(cpu_hotplug.active_writer); | |
d221938c GS |
88 | mutex_unlock(&cpu_hotplug.lock); |
89 | ||
a9d9baa1 | 90 | } |
86ef5c9a | 91 | EXPORT_SYMBOL_GPL(put_online_cpus); |
a9d9baa1 | 92 | |
a9d9baa1 | 93 | #endif /* CONFIG_HOTPLUG_CPU */ |
90d45d17 | 94 | |
d221938c GS |
95 | /* |
96 | * The following two API's must be used when attempting | |
97 | * to serialize the updates to cpu_online_map, cpu_present_map. | |
98 | */ | |
99 | void cpu_maps_update_begin(void) | |
100 | { | |
101 | mutex_lock(&cpu_add_remove_lock); | |
102 | } | |
103 | ||
104 | void cpu_maps_update_done(void) | |
105 | { | |
106 | mutex_unlock(&cpu_add_remove_lock); | |
107 | } | |
108 | ||
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 | */ | |
131 | static void cpu_hotplug_begin(void) | |
132 | { | |
d221938c | 133 | cpu_hotplug.active_writer = current; |
d2ba7e2a ON |
134 | |
135 | for (;;) { | |
136 | mutex_lock(&cpu_hotplug.lock); | |
137 | if (likely(!cpu_hotplug.refcount)) | |
138 | break; | |
139 | __set_current_state(TASK_UNINTERRUPTIBLE); | |
d221938c GS |
140 | mutex_unlock(&cpu_hotplug.lock); |
141 | schedule(); | |
d221938c | 142 | } |
d221938c GS |
143 | } |
144 | ||
145 | static void cpu_hotplug_done(void) | |
146 | { | |
147 | cpu_hotplug.active_writer = NULL; | |
148 | mutex_unlock(&cpu_hotplug.lock); | |
149 | } | |
1da177e4 | 150 | /* Need to know about CPUs going up/down? */ |
f7b16c10 | 151 | int __ref register_cpu_notifier(struct notifier_block *nb) |
1da177e4 | 152 | { |
bd5349cf | 153 | int ret; |
d221938c | 154 | cpu_maps_update_begin(); |
bd5349cf | 155 | ret = raw_notifier_chain_register(&cpu_chain, nb); |
d221938c | 156 | cpu_maps_update_done(); |
bd5349cf | 157 | return ret; |
1da177e4 | 158 | } |
65edc68c CS |
159 | |
160 | #ifdef CONFIG_HOTPLUG_CPU | |
161 | ||
1da177e4 LT |
162 | EXPORT_SYMBOL(register_cpu_notifier); |
163 | ||
9647155f | 164 | void __ref unregister_cpu_notifier(struct notifier_block *nb) |
1da177e4 | 165 | { |
d221938c | 166 | cpu_maps_update_begin(); |
bd5349cf | 167 | raw_notifier_chain_unregister(&cpu_chain, nb); |
d221938c | 168 | cpu_maps_update_done(); |
1da177e4 LT |
169 | } |
170 | EXPORT_SYMBOL(unregister_cpu_notifier); | |
171 | ||
1da177e4 LT |
172 | static inline void check_for_tasks(int cpu) |
173 | { | |
174 | struct task_struct *p; | |
175 | ||
176 | write_lock_irq(&tasklist_lock); | |
177 | for_each_process(p) { | |
178 | if (task_cpu(p) == cpu && | |
179 | (!cputime_eq(p->utime, cputime_zero) || | |
180 | !cputime_eq(p->stime, cputime_zero))) | |
181 | printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\ | |
e7407dcc | 182 | (state = %ld, flags = %x) \n", |
ba25f9dc PE |
183 | p->comm, task_pid_nr(p), cpu, |
184 | p->state, p->flags); | |
1da177e4 LT |
185 | } |
186 | write_unlock_irq(&tasklist_lock); | |
187 | } | |
188 | ||
db912f96 AK |
189 | struct take_cpu_down_param { |
190 | unsigned long mod; | |
191 | void *hcpu; | |
192 | }; | |
193 | ||
1da177e4 | 194 | /* Take this CPU down. */ |
514a20a5 | 195 | static int __ref take_cpu_down(void *_param) |
1da177e4 | 196 | { |
db912f96 | 197 | struct take_cpu_down_param *param = _param; |
1da177e4 LT |
198 | int err; |
199 | ||
db912f96 AK |
200 | raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod, |
201 | param->hcpu); | |
1da177e4 LT |
202 | /* Ensure this CPU doesn't handle any more interrupts. */ |
203 | err = __cpu_disable(); | |
204 | if (err < 0) | |
f3705136 | 205 | return err; |
1da177e4 | 206 | |
f3705136 ZM |
207 | /* Force idle task to run as soon as we yield: it should |
208 | immediately notice cpu is offline and die quickly. */ | |
209 | sched_idle_next(); | |
210 | return 0; | |
1da177e4 LT |
211 | } |
212 | ||
e3920fb4 | 213 | /* Requires cpu_add_remove_lock to be held */ |
514a20a5 | 214 | static int __ref _cpu_down(unsigned int cpu, int tasks_frozen) |
1da177e4 | 215 | { |
e7407dcc | 216 | int err, nr_calls = 0; |
1da177e4 LT |
217 | struct task_struct *p; |
218 | cpumask_t old_allowed, tmp; | |
e7407dcc | 219 | void *hcpu = (void *)(long)cpu; |
8bb78442 | 220 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
db912f96 AK |
221 | struct take_cpu_down_param tcd_param = { |
222 | .mod = mod, | |
223 | .hcpu = hcpu, | |
224 | }; | |
1da177e4 | 225 | |
e3920fb4 RW |
226 | if (num_online_cpus() == 1) |
227 | return -EBUSY; | |
1da177e4 | 228 | |
e3920fb4 RW |
229 | if (!cpu_online(cpu)) |
230 | return -EINVAL; | |
1da177e4 | 231 | |
d221938c | 232 | cpu_hotplug_begin(); |
8bb78442 | 233 | err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod, |
e7407dcc | 234 | hcpu, -1, &nr_calls); |
1da177e4 | 235 | if (err == NOTIFY_BAD) { |
a0d8cdb6 | 236 | nr_calls--; |
8bb78442 RW |
237 | __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod, |
238 | hcpu, nr_calls, NULL); | |
1da177e4 | 239 | printk("%s: attempt to take down CPU %u failed\n", |
af1f16d0 | 240 | __func__, cpu); |
baaca49f GS |
241 | err = -EINVAL; |
242 | goto out_release; | |
1da177e4 LT |
243 | } |
244 | ||
245 | /* Ensure that we are not runnable on dying cpu */ | |
246 | old_allowed = current->cpus_allowed; | |
f70316da | 247 | cpus_setall(tmp); |
1da177e4 | 248 | cpu_clear(cpu, tmp); |
f70316da | 249 | set_cpus_allowed_ptr(current, &tmp); |
1da177e4 | 250 | |
db912f96 | 251 | p = __stop_machine_run(take_cpu_down, &tcd_param, cpu); |
aa953877 | 252 | |
8fa1d7d3 | 253 | if (IS_ERR(p) || cpu_online(cpu)) { |
1da177e4 | 254 | /* CPU didn't die: tell everyone. Can't complain. */ |
8bb78442 | 255 | if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod, |
e7407dcc | 256 | hcpu) == NOTIFY_BAD) |
1da177e4 LT |
257 | BUG(); |
258 | ||
8fa1d7d3 ST |
259 | if (IS_ERR(p)) { |
260 | err = PTR_ERR(p); | |
261 | goto out_allowed; | |
262 | } | |
1da177e4 | 263 | goto out_thread; |
8fa1d7d3 | 264 | } |
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 | ||
280 | out_thread: | |
281 | err = kthread_stop(p); | |
282 | out_allowed: | |
f70316da | 283 | set_cpus_allowed_ptr(current, &old_allowed); |
baaca49f | 284 | out_release: |
d221938c | 285 | cpu_hotplug_done(); |
e3920fb4 RW |
286 | return err; |
287 | } | |
288 | ||
514a20a5 | 289 | int __ref cpu_down(unsigned int cpu) |
e3920fb4 RW |
290 | { |
291 | int err = 0; | |
292 | ||
d221938c | 293 | cpu_maps_update_begin(); |
e3920fb4 RW |
294 | if (cpu_hotplug_disabled) |
295 | err = -EBUSY; | |
296 | else | |
8bb78442 | 297 | err = _cpu_down(cpu, 0); |
e3920fb4 | 298 | |
d221938c | 299 | cpu_maps_update_done(); |
1da177e4 LT |
300 | return err; |
301 | } | |
b62b8ef9 | 302 | EXPORT_SYMBOL(cpu_down); |
1da177e4 LT |
303 | #endif /*CONFIG_HOTPLUG_CPU*/ |
304 | ||
e3920fb4 | 305 | /* Requires cpu_add_remove_lock to be held */ |
8bb78442 | 306 | static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen) |
1da177e4 | 307 | { |
baaca49f | 308 | int ret, nr_calls = 0; |
1da177e4 | 309 | void *hcpu = (void *)(long)cpu; |
8bb78442 | 310 | unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0; |
1da177e4 | 311 | |
e3920fb4 RW |
312 | if (cpu_online(cpu) || !cpu_present(cpu)) |
313 | return -EINVAL; | |
90d45d17 | 314 | |
d221938c | 315 | cpu_hotplug_begin(); |
8bb78442 | 316 | ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu, |
baaca49f | 317 | -1, &nr_calls); |
1da177e4 | 318 | if (ret == NOTIFY_BAD) { |
a0d8cdb6 | 319 | nr_calls--; |
1da177e4 | 320 | printk("%s: attempt to bring up CPU %u failed\n", |
af1f16d0 | 321 | __func__, cpu); |
1da177e4 LT |
322 | ret = -EINVAL; |
323 | goto out_notify; | |
324 | } | |
325 | ||
326 | /* Arch-specific enabling code. */ | |
327 | ret = __cpu_up(cpu); | |
328 | if (ret != 0) | |
329 | goto out_notify; | |
6978c705 | 330 | BUG_ON(!cpu_online(cpu)); |
1da177e4 LT |
331 | |
332 | /* Now call notifier in preparation. */ | |
8bb78442 | 333 | raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu); |
1da177e4 LT |
334 | |
335 | out_notify: | |
336 | if (ret != 0) | |
baaca49f | 337 | __raw_notifier_call_chain(&cpu_chain, |
8bb78442 | 338 | CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL); |
d221938c | 339 | cpu_hotplug_done(); |
e3920fb4 RW |
340 | |
341 | return ret; | |
342 | } | |
343 | ||
b282b6f8 | 344 | int __cpuinit cpu_up(unsigned int cpu) |
e3920fb4 RW |
345 | { |
346 | int err = 0; | |
73e753a5 KH |
347 | if (!cpu_isset(cpu, cpu_possible_map)) { |
348 | printk(KERN_ERR "can't online cpu %d because it is not " | |
349 | "configured as may-hotadd at boot time\n", cpu); | |
350 | #if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390) | |
351 | printk(KERN_ERR "please check additional_cpus= boot " | |
352 | "parameter\n"); | |
353 | #endif | |
354 | return -EINVAL; | |
355 | } | |
e3920fb4 | 356 | |
d221938c | 357 | cpu_maps_update_begin(); |
e3920fb4 RW |
358 | if (cpu_hotplug_disabled) |
359 | err = -EBUSY; | |
360 | else | |
8bb78442 | 361 | err = _cpu_up(cpu, 0); |
e3920fb4 | 362 | |
d221938c | 363 | cpu_maps_update_done(); |
e3920fb4 RW |
364 | return err; |
365 | } | |
366 | ||
f3de4be9 | 367 | #ifdef CONFIG_PM_SLEEP_SMP |
e3920fb4 RW |
368 | static cpumask_t frozen_cpus; |
369 | ||
370 | int disable_nonboot_cpus(void) | |
371 | { | |
e1d9fd2e | 372 | int cpu, first_cpu, error = 0; |
e3920fb4 | 373 | |
d221938c | 374 | cpu_maps_update_begin(); |
1d64b9cb | 375 | first_cpu = first_cpu(cpu_online_map); |
e3920fb4 RW |
376 | /* We take down all of the non-boot CPUs in one shot to avoid races |
377 | * with the userspace trying to use the CPU hotplug at the same time | |
378 | */ | |
379 | cpus_clear(frozen_cpus); | |
380 | printk("Disabling non-boot CPUs ...\n"); | |
381 | for_each_online_cpu(cpu) { | |
382 | if (cpu == first_cpu) | |
383 | continue; | |
8bb78442 | 384 | error = _cpu_down(cpu, 1); |
e3920fb4 RW |
385 | if (!error) { |
386 | cpu_set(cpu, frozen_cpus); | |
387 | printk("CPU%d is down\n", cpu); | |
388 | } else { | |
389 | printk(KERN_ERR "Error taking CPU%d down: %d\n", | |
390 | cpu, error); | |
391 | break; | |
392 | } | |
393 | } | |
394 | if (!error) { | |
395 | BUG_ON(num_online_cpus() > 1); | |
396 | /* Make sure the CPUs won't be enabled by someone else */ | |
397 | cpu_hotplug_disabled = 1; | |
398 | } else { | |
e1d9fd2e | 399 | printk(KERN_ERR "Non-boot CPUs are not disabled\n"); |
e3920fb4 | 400 | } |
d221938c | 401 | cpu_maps_update_done(); |
e3920fb4 RW |
402 | return error; |
403 | } | |
404 | ||
fa7303e2 | 405 | void __ref enable_nonboot_cpus(void) |
e3920fb4 RW |
406 | { |
407 | int cpu, error; | |
408 | ||
409 | /* Allow everyone to use the CPU hotplug again */ | |
d221938c | 410 | cpu_maps_update_begin(); |
e3920fb4 | 411 | cpu_hotplug_disabled = 0; |
ed746e3b | 412 | if (cpus_empty(frozen_cpus)) |
1d64b9cb | 413 | goto out; |
e3920fb4 RW |
414 | |
415 | printk("Enabling non-boot CPUs ...\n"); | |
416 | for_each_cpu_mask(cpu, frozen_cpus) { | |
8bb78442 | 417 | error = _cpu_up(cpu, 1); |
e3920fb4 RW |
418 | if (!error) { |
419 | printk("CPU%d is up\n", cpu); | |
420 | continue; | |
421 | } | |
1d64b9cb | 422 | printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error); |
e3920fb4 RW |
423 | } |
424 | cpus_clear(frozen_cpus); | |
1d64b9cb | 425 | out: |
d221938c | 426 | cpu_maps_update_done(); |
1da177e4 | 427 | } |
f3de4be9 | 428 | #endif /* CONFIG_PM_SLEEP_SMP */ |
68f4f1ec MK |
429 | |
430 | #endif /* CONFIG_SMP */ |