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/kmod.h> /* for hotplug_path */
15 #include <linux/kthread.h>
16 #include <linux/stop_machine.h>
17 #include <asm/semaphore.h>
19 /* This protects CPUs going up and down... */
20 DECLARE_MUTEX(cpucontrol
);
22 static struct notifier_block
*cpu_chain
;
24 /* Need to know about CPUs going up/down? */
25 int register_cpu_notifier(struct notifier_block
*nb
)
29 if ((ret
= down_interruptible(&cpucontrol
)) != 0)
31 ret
= notifier_chain_register(&cpu_chain
, nb
);
35 EXPORT_SYMBOL(register_cpu_notifier
);
37 void unregister_cpu_notifier(struct notifier_block
*nb
)
40 notifier_chain_unregister(&cpu_chain
, nb
);
43 EXPORT_SYMBOL(unregister_cpu_notifier
);
45 #ifdef CONFIG_HOTPLUG_CPU
46 static inline void check_for_tasks(int cpu
)
48 struct task_struct
*p
;
50 write_lock_irq(&tasklist_lock
);
52 if (task_cpu(p
) == cpu
&& (p
->utime
!= 0 || p
->stime
!= 0))
53 printk(KERN_WARNING
"Task %s (pid = %d) is on cpu %d\
54 (state = %ld, flags = %lx) \n",
55 p
->comm
, p
->pid
, cpu
, p
->state
, p
->flags
);
57 write_unlock_irq(&tasklist_lock
);
60 /* Notify userspace when a cpu event occurs, by running '/sbin/hotplug
61 * cpu' with certain environment variables set. */
62 static int cpu_run_sbin_hotplug(unsigned int cpu
, const char *action
)
64 char *argv
[3], *envp
[6], cpu_str
[12], action_str
[32], devpath_str
[40];
67 sprintf(cpu_str
, "CPU=%d", cpu
);
68 sprintf(action_str
, "ACTION=%s", action
);
69 sprintf(devpath_str
, "DEVPATH=devices/system/cpu/cpu%d", cpu
);
72 argv
[i
++] = hotplug_path
;
77 /* minimal command environment */
79 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
81 envp
[i
++] = action_str
;
82 envp
[i
++] = devpath_str
;
85 return call_usermodehelper(argv
[0], argv
, envp
, 0);
88 /* Take this CPU down. */
89 static int take_cpu_down(void *unused
)
93 /* Take offline: makes arch_cpu_down somewhat easier. */
94 cpu_clear(smp_processor_id(), cpu_online_map
);
96 /* Ensure this CPU doesn't handle any more interrupts. */
97 err
= __cpu_disable();
99 cpu_set(smp_processor_id(), cpu_online_map
);
101 /* Force idle task to run as soon as we yield: it should
102 immediately notice cpu is offline and die quickly. */
108 int cpu_down(unsigned int cpu
)
111 struct task_struct
*p
;
112 cpumask_t old_allowed
, tmp
;
114 if ((err
= lock_cpu_hotplug_interruptible()) != 0)
117 if (num_online_cpus() == 1) {
122 if (!cpu_online(cpu
)) {
127 /* Ensure that we are not runnable on dying cpu */
128 old_allowed
= current
->cpus_allowed
;
131 set_cpus_allowed(current
, tmp
);
133 p
= __stop_machine_run(take_cpu_down
, NULL
, cpu
);
142 /* Wait for it to sleep (leaving idle task). */
143 while (!idle_cpu(cpu
))
146 /* This actually kills the CPU. */
149 /* Move it here so it can run. */
150 kthread_bind(p
, smp_processor_id());
152 /* CPU is completely dead: tell everyone. Too late to complain. */
153 if (notifier_call_chain(&cpu_chain
, CPU_DEAD
, (void *)(long)cpu
)
157 check_for_tasks(cpu
);
159 cpu_run_sbin_hotplug(cpu
, "offline");
162 err
= kthread_stop(p
);
164 set_cpus_allowed(current
, old_allowed
);
166 unlock_cpu_hotplug();
170 static inline int cpu_run_sbin_hotplug(unsigned int cpu
, const char *action
)
174 #endif /*CONFIG_HOTPLUG_CPU*/
176 int __devinit
cpu_up(unsigned int cpu
)
179 void *hcpu
= (void *)(long)cpu
;
181 if ((ret
= down_interruptible(&cpucontrol
)) != 0)
184 if (cpu_online(cpu
) || !cpu_present(cpu
)) {
188 ret
= notifier_call_chain(&cpu_chain
, CPU_UP_PREPARE
, hcpu
);
189 if (ret
== NOTIFY_BAD
) {
190 printk("%s: attempt to bring up CPU %u failed\n",
196 /* Arch-specific enabling code. */
200 if (!cpu_online(cpu
))
203 /* Now call notifier in preparation. */
204 notifier_call_chain(&cpu_chain
, CPU_ONLINE
, hcpu
);
208 notifier_call_chain(&cpu_chain
, CPU_UP_CANCELED
, hcpu
);