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 * Represents all cpu's present in the system
25 * In systems capable of hotplug, this map could dynamically grow
26 * as new cpu's are detected in the system via any platform specific
27 * method, such as ACPI for e.g.
29 cpumask_t cpu_present_map;
30 EXPORT_SYMBOL(cpu_present_map);
32 /* Need to know about CPUs going up/down? */
33 int register_cpu_notifier(struct notifier_block *nb)
37 if ((ret = down_interruptible(&cpucontrol)) != 0)
39 ret = notifier_chain_register(&cpu_chain, nb);
43 EXPORT_SYMBOL(register_cpu_notifier);
45 void unregister_cpu_notifier(struct notifier_block *nb)
48 notifier_chain_unregister(&cpu_chain, nb);
51 EXPORT_SYMBOL(unregister_cpu_notifier);
53 #ifdef CONFIG_HOTPLUG_CPU
54 static inline void check_for_tasks(int cpu)
56 struct task_struct *p;
58 write_lock_irq(&tasklist_lock);
60 if (task_cpu(p) == cpu && (p->utime != 0 || p->stime != 0))
61 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
62 (state = %ld, flags = %lx) \n",
63 p->comm, p->pid, cpu, p->state, p->flags);
65 write_unlock_irq(&tasklist_lock);
68 /* Notify userspace when a cpu event occurs, by running '/sbin/hotplug
69 * cpu' with certain environment variables set. */
70 static int cpu_run_sbin_hotplug(unsigned int cpu, const char *action)
72 char *argv[3], *envp[5], cpu_str[12], action_str[32];
75 sprintf(cpu_str, "CPU=%d", cpu);
76 sprintf(action_str, "ACTION=%s", action);
77 /* FIXME: Add DEVPATH. --RR */
80 argv[i++] = hotplug_path;
85 /* minimal command environment */
87 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
89 envp[i++] = action_str;
92 return call_usermodehelper(argv[0], argv, envp, 0);
95 /* Take this CPU down. */
96 static int take_cpu_down(void *unused)
100 /* Take offline: makes arch_cpu_down somewhat easier. */
101 cpu_clear(smp_processor_id(), cpu_online_map);
103 /* Ensure this CPU doesn't handle any more interrupts. */
104 err = __cpu_disable();
106 cpu_set(smp_processor_id(), cpu_online_map);
108 /* Force idle task to run as soon as we yield: it should
109 immediately notice cpu is offline and die quickly. */
115 int cpu_down(unsigned int cpu)
118 struct task_struct *p;
119 cpumask_t old_allowed, tmp;
121 if ((err = lock_cpu_hotplug_interruptible()) != 0)
124 if (num_online_cpus() == 1) {
129 if (!cpu_online(cpu)) {
134 /* Ensure that we are not runnable on dying cpu */
135 old_allowed = current->cpus_allowed;
138 set_cpus_allowed(current, tmp);
140 p = __stop_machine_run(take_cpu_down, NULL, cpu);
149 /* Wait for it to sleep (leaving idle task). */
150 while (!idle_cpu(cpu))
153 /* This actually kills the CPU. */
156 /* Move it here so it can run. */
157 kthread_bind(p, smp_processor_id());
159 /* CPU is completely dead: tell everyone. Too late to complain. */
160 if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
164 check_for_tasks(cpu);
166 cpu_run_sbin_hotplug(cpu, "offline");
169 err = kthread_stop(p);
171 set_cpus_allowed(current, old_allowed);
173 unlock_cpu_hotplug();
177 static inline int cpu_run_sbin_hotplug(unsigned int cpu, const char *action)
181 #endif /*CONFIG_HOTPLUG_CPU*/
183 int __devinit cpu_up(unsigned int cpu)
186 void *hcpu = (void *)(long)cpu;
188 if ((ret = down_interruptible(&cpucontrol)) != 0)
191 if (cpu_online(cpu) || !cpu_present(cpu)) {
195 ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
196 if (ret == NOTIFY_BAD) {
197 printk("%s: attempt to bring up CPU %u failed\n",
203 /* Arch-specific enabling code. */
207 if (!cpu_online(cpu))
210 /* Now call notifier in preparation. */
211 notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
215 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);