2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
4 * This code is licenced under the GPL.
6 #include <linux/proc_fs.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>
16 #include <linux/mutex.h>
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.
24 cpumask_t cpu_present_map __read_mostly
;
25 EXPORT_SYMBOL(cpu_present_map
);
30 * Represents all cpu's that are currently online.
32 cpumask_t cpu_online_map __read_mostly
= CPU_MASK_ALL
;
33 EXPORT_SYMBOL(cpu_online_map
);
35 cpumask_t cpu_possible_map __read_mostly
= CPU_MASK_ALL
;
36 EXPORT_SYMBOL(cpu_possible_map
);
38 #else /* CONFIG_SMP */
40 /* Serializes the updates to cpu_online_map, cpu_present_map */
41 static DEFINE_MUTEX(cpu_add_remove_lock
);
43 static __cpuinitdata
RAW_NOTIFIER_HEAD(cpu_chain
);
45 /* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
46 * Should always be manipulated under cpu_add_remove_lock
48 static int cpu_hotplug_disabled
;
51 struct task_struct
*active_writer
;
52 struct mutex lock
; /* Synchronizes accesses to refcount, */
54 * Also blocks the new readers during
55 * an ongoing cpu hotplug operation.
60 void __init
cpu_hotplug_init(void)
62 cpu_hotplug
.active_writer
= NULL
;
63 mutex_init(&cpu_hotplug
.lock
);
64 cpu_hotplug
.refcount
= 0;
67 #ifdef CONFIG_HOTPLUG_CPU
69 void get_online_cpus(void)
72 if (cpu_hotplug
.active_writer
== current
)
74 mutex_lock(&cpu_hotplug
.lock
);
75 cpu_hotplug
.refcount
++;
76 mutex_unlock(&cpu_hotplug
.lock
);
79 EXPORT_SYMBOL_GPL(get_online_cpus
);
81 void put_online_cpus(void)
83 if (cpu_hotplug
.active_writer
== current
)
85 mutex_lock(&cpu_hotplug
.lock
);
86 if (!--cpu_hotplug
.refcount
&& unlikely(cpu_hotplug
.active_writer
))
87 wake_up_process(cpu_hotplug
.active_writer
);
88 mutex_unlock(&cpu_hotplug
.lock
);
91 EXPORT_SYMBOL_GPL(put_online_cpus
);
93 #endif /* CONFIG_HOTPLUG_CPU */
96 * The following two API's must be used when attempting
97 * to serialize the updates to cpu_online_map, cpu_present_map.
99 void cpu_maps_update_begin(void)
101 mutex_lock(&cpu_add_remove_lock
);
104 void cpu_maps_update_done(void)
106 mutex_unlock(&cpu_add_remove_lock
);
110 * This ensures that the hotplug operation can begin only when the
111 * refcount goes to zero.
113 * Note that during a cpu-hotplug operation, the new readers, if any,
114 * will be blocked by the cpu_hotplug.lock
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.
119 * Note that theoretically, there is a possibility of a livelock:
120 * - Refcount goes to zero, last reader wakes up the sleeping
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.
127 * However, this is very difficult to achieve in practice since
128 * get_online_cpus() not an api which is called all that often.
131 static void cpu_hotplug_begin(void)
133 cpu_hotplug
.active_writer
= current
;
136 mutex_lock(&cpu_hotplug
.lock
);
137 if (likely(!cpu_hotplug
.refcount
))
139 __set_current_state(TASK_UNINTERRUPTIBLE
);
140 mutex_unlock(&cpu_hotplug
.lock
);
145 static void cpu_hotplug_done(void)
147 cpu_hotplug
.active_writer
= NULL
;
148 mutex_unlock(&cpu_hotplug
.lock
);
150 /* Need to know about CPUs going up/down? */
151 int __ref
register_cpu_notifier(struct notifier_block
*nb
)
154 cpu_maps_update_begin();
155 ret
= raw_notifier_chain_register(&cpu_chain
, nb
);
156 cpu_maps_update_done();
160 #ifdef CONFIG_HOTPLUG_CPU
162 EXPORT_SYMBOL(register_cpu_notifier
);
164 void __ref
unregister_cpu_notifier(struct notifier_block
*nb
)
166 cpu_maps_update_begin();
167 raw_notifier_chain_unregister(&cpu_chain
, nb
);
168 cpu_maps_update_done();
170 EXPORT_SYMBOL(unregister_cpu_notifier
);
172 static inline void check_for_tasks(int cpu
)
174 struct task_struct
*p
;
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\
182 (state = %ld, flags = %x) \n",
183 p
->comm
, task_pid_nr(p
), cpu
,
186 write_unlock_irq(&tasklist_lock
);
189 struct take_cpu_down_param
{
194 /* Take this CPU down. */
195 static int __ref
take_cpu_down(void *_param
)
197 struct take_cpu_down_param
*param
= _param
;
200 raw_notifier_call_chain(&cpu_chain
, CPU_DYING
| param
->mod
,
202 /* Ensure this CPU doesn't handle any more interrupts. */
203 err
= __cpu_disable();
207 /* Force idle task to run as soon as we yield: it should
208 immediately notice cpu is offline and die quickly. */
213 /* Requires cpu_add_remove_lock to be held */
214 static int __ref
_cpu_down(unsigned int cpu
, int tasks_frozen
)
216 int err
, nr_calls
= 0;
217 struct task_struct
*p
;
218 cpumask_t old_allowed
, tmp
;
219 void *hcpu
= (void *)(long)cpu
;
220 unsigned long mod
= tasks_frozen
? CPU_TASKS_FROZEN
: 0;
221 struct take_cpu_down_param tcd_param
= {
226 if (num_online_cpus() == 1)
229 if (!cpu_online(cpu
))
233 err
= __raw_notifier_call_chain(&cpu_chain
, CPU_DOWN_PREPARE
| mod
,
234 hcpu
, -1, &nr_calls
);
235 if (err
== NOTIFY_BAD
) {
237 __raw_notifier_call_chain(&cpu_chain
, CPU_DOWN_FAILED
| mod
,
238 hcpu
, nr_calls
, NULL
);
239 printk("%s: attempt to take down CPU %u failed\n",
245 /* Ensure that we are not runnable on dying cpu */
246 old_allowed
= current
->cpus_allowed
;
249 set_cpus_allowed_ptr(current
, &tmp
);
251 p
= __stop_machine_run(take_cpu_down
, &tcd_param
, cpu
);
253 if (IS_ERR(p
) || cpu_online(cpu
)) {
254 /* CPU didn't die: tell everyone. Can't complain. */
255 if (raw_notifier_call_chain(&cpu_chain
, CPU_DOWN_FAILED
| mod
,
266 /* Wait for it to sleep (leaving idle task). */
267 while (!idle_cpu(cpu
))
270 /* This actually kills the CPU. */
273 /* CPU is completely dead: tell everyone. Too late to complain. */
274 if (raw_notifier_call_chain(&cpu_chain
, CPU_DEAD
| mod
,
278 check_for_tasks(cpu
);
281 err
= kthread_stop(p
);
283 set_cpus_allowed_ptr(current
, &old_allowed
);
289 int __ref
cpu_down(unsigned int cpu
)
293 cpu_maps_update_begin();
294 if (cpu_hotplug_disabled
)
297 err
= _cpu_down(cpu
, 0);
299 cpu_maps_update_done();
302 EXPORT_SYMBOL(cpu_down
);
303 #endif /*CONFIG_HOTPLUG_CPU*/
305 /* Requires cpu_add_remove_lock to be held */
306 static int __cpuinit
_cpu_up(unsigned int cpu
, int tasks_frozen
)
308 int ret
, nr_calls
= 0;
309 void *hcpu
= (void *)(long)cpu
;
310 unsigned long mod
= tasks_frozen
? CPU_TASKS_FROZEN
: 0;
312 if (cpu_online(cpu
) || !cpu_present(cpu
))
316 ret
= __raw_notifier_call_chain(&cpu_chain
, CPU_UP_PREPARE
| mod
, hcpu
,
318 if (ret
== NOTIFY_BAD
) {
320 printk("%s: attempt to bring up CPU %u failed\n",
326 /* Arch-specific enabling code. */
330 BUG_ON(!cpu_online(cpu
));
332 /* Now call notifier in preparation. */
333 raw_notifier_call_chain(&cpu_chain
, CPU_ONLINE
| mod
, hcpu
);
337 __raw_notifier_call_chain(&cpu_chain
,
338 CPU_UP_CANCELED
| mod
, hcpu
, nr_calls
, NULL
);
344 int __cpuinit
cpu_up(unsigned int cpu
)
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 "
357 cpu_maps_update_begin();
358 if (cpu_hotplug_disabled
)
361 err
= _cpu_up(cpu
, 0);
363 cpu_maps_update_done();
367 #ifdef CONFIG_PM_SLEEP_SMP
368 static cpumask_t frozen_cpus
;
370 int disable_nonboot_cpus(void)
372 int cpu
, first_cpu
, error
= 0;
374 cpu_maps_update_begin();
375 first_cpu
= first_cpu(cpu_online_map
);
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
379 cpus_clear(frozen_cpus
);
380 printk("Disabling non-boot CPUs ...\n");
381 for_each_online_cpu(cpu
) {
382 if (cpu
== first_cpu
)
384 error
= _cpu_down(cpu
, 1);
386 cpu_set(cpu
, frozen_cpus
);
387 printk("CPU%d is down\n", cpu
);
389 printk(KERN_ERR
"Error taking CPU%d down: %d\n",
395 BUG_ON(num_online_cpus() > 1);
396 /* Make sure the CPUs won't be enabled by someone else */
397 cpu_hotplug_disabled
= 1;
399 printk(KERN_ERR
"Non-boot CPUs are not disabled\n");
401 cpu_maps_update_done();
405 void __ref
enable_nonboot_cpus(void)
409 /* Allow everyone to use the CPU hotplug again */
410 cpu_maps_update_begin();
411 cpu_hotplug_disabled
= 0;
412 if (cpus_empty(frozen_cpus
))
415 printk("Enabling non-boot CPUs ...\n");
416 for_each_cpu_mask_nr(cpu
, frozen_cpus
) {
417 error
= _cpu_up(cpu
, 1);
419 printk("CPU%d is up\n", cpu
);
422 printk(KERN_WARNING
"Error taking CPU%d up: %d\n", cpu
, error
);
424 cpus_clear(frozen_cpus
);
426 cpu_maps_update_done();
428 #endif /* CONFIG_PM_SLEEP_SMP */
430 #endif /* CONFIG_SMP */