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 <asm/semaphore.h>
18 /* This protects CPUs going up and down... */
19 DECLARE_MUTEX(cpucontrol);
21 static struct notifier_block *cpu_chain;
23 /* Need to know about CPUs going up/down? */
24 int register_cpu_notifier(struct notifier_block *nb)
28 if ((ret = down_interruptible(&cpucontrol)) != 0)
30 ret = notifier_chain_register(&cpu_chain, nb);
34 EXPORT_SYMBOL(register_cpu_notifier);
36 void unregister_cpu_notifier(struct notifier_block *nb)
39 notifier_chain_unregister(&cpu_chain, nb);
42 EXPORT_SYMBOL(unregister_cpu_notifier);
44 #ifdef CONFIG_HOTPLUG_CPU
45 static inline void check_for_tasks(int cpu)
47 struct task_struct *p;
49 write_lock_irq(&tasklist_lock);
51 if (task_cpu(p) == cpu && (p->utime != 0 || p->stime != 0))
52 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
53 (state = %ld, flags = %lx) \n",
54 p->comm, p->pid, cpu, p->state, p->flags);
56 write_unlock_irq(&tasklist_lock);
59 /* Take this CPU down. */
60 static int take_cpu_down(void *unused)
64 /* Take offline: makes arch_cpu_down somewhat easier. */
65 cpu_clear(smp_processor_id(), cpu_online_map);
67 /* Ensure this CPU doesn't handle any more interrupts. */
68 err = __cpu_disable();
70 cpu_set(smp_processor_id(), cpu_online_map);
72 /* Force idle task to run as soon as we yield: it should
73 immediately notice cpu is offline and die quickly. */
79 int cpu_down(unsigned int cpu)
82 struct task_struct *p;
83 cpumask_t old_allowed, tmp;
85 if ((err = lock_cpu_hotplug_interruptible()) != 0)
88 if (num_online_cpus() == 1) {
93 if (!cpu_online(cpu)) {
98 err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
100 if (err == NOTIFY_BAD) {
101 printk("%s: attempt to take down CPU %u failed\n",
107 /* Ensure that we are not runnable on dying cpu */
108 old_allowed = current->cpus_allowed;
111 set_cpus_allowed(current, tmp);
113 p = __stop_machine_run(take_cpu_down, NULL, cpu);
115 /* CPU didn't die: tell everyone. Can't complain. */
116 if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
117 (void *)(long)cpu) == NOTIFY_BAD)
127 /* Wait for it to sleep (leaving idle task). */
128 while (!idle_cpu(cpu))
131 /* This actually kills the CPU. */
134 /* Move it here so it can run. */
135 kthread_bind(p, smp_processor_id());
137 /* CPU is completely dead: tell everyone. Too late to complain. */
138 if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
142 check_for_tasks(cpu);
145 err = kthread_stop(p);
147 set_cpus_allowed(current, old_allowed);
149 unlock_cpu_hotplug();
152 #endif /*CONFIG_HOTPLUG_CPU*/
154 int __devinit cpu_up(unsigned int cpu)
157 void *hcpu = (void *)(long)cpu;
159 if ((ret = down_interruptible(&cpucontrol)) != 0)
162 if (cpu_online(cpu) || !cpu_present(cpu)) {
166 ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
167 if (ret == NOTIFY_BAD) {
168 printk("%s: attempt to bring up CPU %u failed\n",
174 /* Arch-specific enabling code. */
178 if (!cpu_online(cpu))
181 /* Now call notifier in preparation. */
182 notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
186 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);