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 static DECLARE_MUTEX(cpucontrol
);
21 static struct notifier_block
*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
= down_interruptible(&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
;
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 register_cpu_notifier(struct notifier_block
*nb
)
76 if ((ret
= lock_cpu_hotplug_interruptible()) != 0)
78 ret
= notifier_chain_register(&cpu_chain
, nb
);
82 EXPORT_SYMBOL(register_cpu_notifier
);
84 void unregister_cpu_notifier(struct notifier_block
*nb
)
87 notifier_chain_unregister(&cpu_chain
, nb
);
90 EXPORT_SYMBOL(unregister_cpu_notifier
);
92 #ifdef CONFIG_HOTPLUG_CPU
93 static inline void check_for_tasks(int cpu
)
95 struct task_struct
*p
;
97 write_lock_irq(&tasklist_lock
);
99 if (task_cpu(p
) == cpu
&&
100 (!cputime_eq(p
->utime
, cputime_zero
) ||
101 !cputime_eq(p
->stime
, cputime_zero
)))
102 printk(KERN_WARNING
"Task %s (pid = %d) is on cpu %d\
103 (state = %ld, flags = %lx) \n",
104 p
->comm
, p
->pid
, cpu
, p
->state
, p
->flags
);
106 write_unlock_irq(&tasklist_lock
);
109 /* Take this CPU down. */
110 static int take_cpu_down(void *unused
)
114 /* Ensure this CPU doesn't handle any more interrupts. */
115 err
= __cpu_disable();
119 /* Force idle task to run as soon as we yield: it should
120 immediately notice cpu is offline and die quickly. */
125 int cpu_down(unsigned int cpu
)
128 struct task_struct
*p
;
129 cpumask_t old_allowed
, tmp
;
131 if ((err
= lock_cpu_hotplug_interruptible()) != 0)
134 if (num_online_cpus() == 1) {
139 if (!cpu_online(cpu
)) {
144 err
= notifier_call_chain(&cpu_chain
, CPU_DOWN_PREPARE
,
146 if (err
== NOTIFY_BAD
) {
147 printk("%s: attempt to take down CPU %u failed\n",
153 /* Ensure that we are not runnable on dying cpu */
154 old_allowed
= current
->cpus_allowed
;
157 set_cpus_allowed(current
, tmp
);
159 p
= __stop_machine_run(take_cpu_down
, NULL
, cpu
);
161 /* CPU didn't die: tell everyone. Can't complain. */
162 if (notifier_call_chain(&cpu_chain
, CPU_DOWN_FAILED
,
163 (void *)(long)cpu
) == NOTIFY_BAD
)
173 /* Wait for it to sleep (leaving idle task). */
174 while (!idle_cpu(cpu
))
177 /* This actually kills the CPU. */
180 /* Move it here so it can run. */
181 kthread_bind(p
, get_cpu());
184 /* CPU is completely dead: tell everyone. Too late to complain. */
185 if (notifier_call_chain(&cpu_chain
, CPU_DEAD
, (void *)(long)cpu
)
189 check_for_tasks(cpu
);
192 err
= kthread_stop(p
);
194 set_cpus_allowed(current
, old_allowed
);
196 unlock_cpu_hotplug();
199 #endif /*CONFIG_HOTPLUG_CPU*/
201 int __devinit
cpu_up(unsigned int cpu
)
204 void *hcpu
= (void *)(long)cpu
;
206 if ((ret
= lock_cpu_hotplug_interruptible()) != 0)
209 if (cpu_online(cpu
) || !cpu_present(cpu
)) {
214 ret
= notifier_call_chain(&cpu_chain
, CPU_UP_PREPARE
, hcpu
);
215 if (ret
== NOTIFY_BAD
) {
216 printk("%s: attempt to bring up CPU %u failed\n",
222 /* Arch-specific enabling code. */
226 if (!cpu_online(cpu
))
229 /* Now call notifier in preparation. */
230 notifier_call_chain(&cpu_chain
, CPU_ONLINE
, hcpu
);
234 notifier_call_chain(&cpu_chain
, CPU_UP_CANCELED
, hcpu
);
236 unlock_cpu_hotplug();