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[5], cpu_str[12], action_str[32];
67 sprintf(cpu_str, "CPU=%d", cpu);
68 sprintf(action_str, "ACTION=%s", action);
69 /* FIXME: Add DEVPATH. --RR */
72 argv[i++] = hotplug_path;
77 /* minimal command environment */
79 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
81 envp[i++] = action_str;
84 return call_usermodehelper(argv[0], argv, envp, 0);
87 /* Take this CPU down. */
88 static int take_cpu_down(void *unused)
92 /* Take offline: makes arch_cpu_down somewhat easier. */
93 cpu_clear(smp_processor_id(), cpu_online_map);
95 /* Ensure this CPU doesn't handle any more interrupts. */
96 err = __cpu_disable();
98 cpu_set(smp_processor_id(), cpu_online_map);
100 /* Force idle task to run as soon as we yield: it should
101 immediately notice cpu is offline and die quickly. */
107 int cpu_down(unsigned int cpu)
110 struct task_struct *p;
111 cpumask_t old_allowed, tmp;
113 if ((err = lock_cpu_hotplug_interruptible()) != 0)
116 if (num_online_cpus() == 1) {
121 if (!cpu_online(cpu)) {
126 /* Ensure that we are not runnable on dying cpu */
127 old_allowed = current->cpus_allowed;
130 set_cpus_allowed(current, tmp);
132 p = __stop_machine_run(take_cpu_down, NULL, cpu);
141 /* Wait for it to sleep (leaving idle task). */
142 while (!idle_cpu(cpu))
145 /* This actually kills the CPU. */
148 /* Move it here so it can run. */
149 kthread_bind(p, smp_processor_id());
151 /* CPU is completely dead: tell everyone. Too late to complain. */
152 if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
156 check_for_tasks(cpu);
158 cpu_run_sbin_hotplug(cpu, "offline");
161 err = kthread_stop(p);
163 set_cpus_allowed(current, old_allowed);
165 unlock_cpu_hotplug();
169 static inline int cpu_run_sbin_hotplug(unsigned int cpu, const char *action)
173 #endif /*CONFIG_HOTPLUG_CPU*/
175 int __devinit cpu_up(unsigned int cpu)
178 void *hcpu = (void *)(long)cpu;
180 if ((ret = down_interruptible(&cpucontrol)) != 0)
183 if (cpu_online(cpu) || !cpu_present(cpu)) {
187 ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
188 if (ret == NOTIFY_BAD) {
189 printk("%s: attempt to bring up CPU %u failed\n",
195 /* Arch-specific enabling code. */
199 if (!cpu_online(cpu))
202 /* Now call notifier in preparation. */
203 notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
207 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);