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>
18 /* This protects CPUs going up and down... */
19 static DEFINE_MUTEX(cpucontrol
);
21 static __cpuinitdata
BLOCKING_NOTIFIER_HEAD(cpu_chain
);
23 #ifdef CONFIG_HOTPLUG_CPU
24 static struct task_struct
*lock_cpu_hotplug_owner
;
25 static int lock_cpu_hotplug_depth
;
27 static int __lock_cpu_hotplug(int interruptible
)
31 if (lock_cpu_hotplug_owner
!= current
) {
33 ret
= mutex_lock_interruptible(&cpucontrol
);
35 mutex_lock(&cpucontrol
);
39 * Set only if we succeed in locking
42 lock_cpu_hotplug_depth
++;
43 lock_cpu_hotplug_owner
= current
;
49 void lock_cpu_hotplug(void)
51 __lock_cpu_hotplug(0);
53 EXPORT_SYMBOL_GPL(lock_cpu_hotplug
);
55 void unlock_cpu_hotplug(void)
57 if (--lock_cpu_hotplug_depth
== 0) {
58 lock_cpu_hotplug_owner
= NULL
;
59 mutex_unlock(&cpucontrol
);
62 EXPORT_SYMBOL_GPL(unlock_cpu_hotplug
);
64 int lock_cpu_hotplug_interruptible(void)
66 return __lock_cpu_hotplug(1);
68 EXPORT_SYMBOL_GPL(lock_cpu_hotplug_interruptible
);
69 #endif /* CONFIG_HOTPLUG_CPU */
71 /* Need to know about CPUs going up/down? */
72 int __cpuinit
register_cpu_notifier(struct notifier_block
*nb
)
74 return blocking_notifier_chain_register(&cpu_chain
, nb
);
77 #ifdef CONFIG_HOTPLUG_CPU
79 EXPORT_SYMBOL(register_cpu_notifier
);
81 void unregister_cpu_notifier(struct notifier_block
*nb
)
83 blocking_notifier_chain_unregister(&cpu_chain
, nb
);
85 EXPORT_SYMBOL(unregister_cpu_notifier
);
87 static inline void check_for_tasks(int cpu
)
89 struct task_struct
*p
;
91 write_lock_irq(&tasklist_lock
);
93 if (task_cpu(p
) == cpu
&&
94 (!cputime_eq(p
->utime
, cputime_zero
) ||
95 !cputime_eq(p
->stime
, cputime_zero
)))
96 printk(KERN_WARNING
"Task %s (pid = %d) is on cpu %d\
97 (state = %ld, flags = %lx) \n",
98 p
->comm
, p
->pid
, cpu
, p
->state
, p
->flags
);
100 write_unlock_irq(&tasklist_lock
);
103 /* Take this CPU down. */
104 static int take_cpu_down(void *unused
)
108 /* Ensure this CPU doesn't handle any more interrupts. */
109 err
= __cpu_disable();
113 /* Force idle task to run as soon as we yield: it should
114 immediately notice cpu is offline and die quickly. */
119 int cpu_down(unsigned int cpu
)
122 struct task_struct
*p
;
123 cpumask_t old_allowed
, tmp
;
125 if ((err
= lock_cpu_hotplug_interruptible()) != 0)
128 if (num_online_cpus() == 1) {
133 if (!cpu_online(cpu
)) {
138 err
= blocking_notifier_call_chain(&cpu_chain
, CPU_DOWN_PREPARE
,
140 if (err
== NOTIFY_BAD
) {
141 printk("%s: attempt to take down CPU %u failed\n",
147 /* Ensure that we are not runnable on dying cpu */
148 old_allowed
= current
->cpus_allowed
;
151 set_cpus_allowed(current
, tmp
);
153 p
= __stop_machine_run(take_cpu_down
, NULL
, cpu
);
155 /* CPU didn't die: tell everyone. Can't complain. */
156 if (blocking_notifier_call_chain(&cpu_chain
, CPU_DOWN_FAILED
,
157 (void *)(long)cpu
) == NOTIFY_BAD
)
167 /* Wait for it to sleep (leaving idle task). */
168 while (!idle_cpu(cpu
))
171 /* This actually kills the CPU. */
174 /* Move it here so it can run. */
175 kthread_bind(p
, get_cpu());
178 /* CPU is completely dead: tell everyone. Too late to complain. */
179 if (blocking_notifier_call_chain(&cpu_chain
, CPU_DEAD
,
180 (void *)(long)cpu
) == NOTIFY_BAD
)
183 check_for_tasks(cpu
);
186 err
= kthread_stop(p
);
188 set_cpus_allowed(current
, old_allowed
);
190 unlock_cpu_hotplug();
193 #endif /*CONFIG_HOTPLUG_CPU*/
195 int __devinit
cpu_up(unsigned int cpu
)
198 void *hcpu
= (void *)(long)cpu
;
200 if ((ret
= lock_cpu_hotplug_interruptible()) != 0)
203 if (cpu_online(cpu
) || !cpu_present(cpu
)) {
208 ret
= blocking_notifier_call_chain(&cpu_chain
, CPU_UP_PREPARE
, hcpu
);
209 if (ret
== NOTIFY_BAD
) {
210 printk("%s: attempt to bring up CPU %u failed\n",
216 /* Arch-specific enabling code. */
220 BUG_ON(!cpu_online(cpu
));
222 /* Now call notifier in preparation. */
223 blocking_notifier_call_chain(&cpu_chain
, CPU_ONLINE
, hcpu
);
227 blocking_notifier_call_chain(&cpu_chain
,
228 CPU_UP_CANCELED
, hcpu
);
230 unlock_cpu_hotplug();