patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / kernel / cpu.c
1 /* CPU control.
2  * (C) 2001, 2002, 2003, 2004 Rusty Russell
3  *
4  * This code is licenced under the GPL.
5  */
6 #include <linux/proc_fs.h>
7 #include <linux/smp.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>
18
19 /* This protects CPUs going up and down... */
20 DECLARE_MUTEX(cpucontrol);
21
22 static struct notifier_block *cpu_chain;
23 /*
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.
28  */
29 cpumask_t       cpu_present_map;
30 EXPORT_SYMBOL(cpu_present_map);
31
32 /* Need to know about CPUs going up/down? */
33 int register_cpu_notifier(struct notifier_block *nb)
34 {
35         int ret;
36
37         if ((ret = down_interruptible(&cpucontrol)) != 0)
38                 return ret;
39         ret = notifier_chain_register(&cpu_chain, nb);
40         up(&cpucontrol);
41         return ret;
42 }
43 EXPORT_SYMBOL(register_cpu_notifier);
44
45 void unregister_cpu_notifier(struct notifier_block *nb)
46 {
47         down(&cpucontrol);
48         notifier_chain_unregister(&cpu_chain, nb);
49         up(&cpucontrol);
50 }
51 EXPORT_SYMBOL(unregister_cpu_notifier);
52
53 #ifdef CONFIG_HOTPLUG_CPU
54 static inline void check_for_tasks(int cpu)
55 {
56         struct task_struct *p;
57
58         write_lock_irq(&tasklist_lock);
59         for_each_process(p) {
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);
64         }
65         write_unlock_irq(&tasklist_lock);
66 }
67
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)
71 {
72         char *argv[3], *envp[5], cpu_str[12], action_str[32];
73         int i;
74
75         sprintf(cpu_str, "CPU=%d", cpu);
76         sprintf(action_str, "ACTION=%s", action);
77         /* FIXME: Add DEVPATH. --RR */
78
79         i = 0;
80         argv[i++] = hotplug_path;
81         argv[i++] = "cpu";
82         argv[i] = NULL;
83
84         i = 0;
85         /* minimal command environment */
86         envp[i++] = "HOME=/";
87         envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
88         envp[i++] = cpu_str;
89         envp[i++] = action_str;
90         envp[i] = NULL;
91
92         return call_usermodehelper(argv[0], argv, envp, 0);
93 }
94
95 /* Take this CPU down. */
96 static int take_cpu_down(void *unused)
97 {
98         int err;
99
100         /* Take offline: makes arch_cpu_down somewhat easier. */
101         cpu_clear(smp_processor_id(), cpu_online_map);
102
103         /* Ensure this CPU doesn't handle any more interrupts. */
104         err = __cpu_disable();
105         if (err < 0)
106                 cpu_set(smp_processor_id(), cpu_online_map);
107         else
108                 /* Force idle task to run as soon as we yield: it should
109                    immediately notice cpu is offline and die quickly. */
110                 sched_idle_next();
111
112         return err;
113 }
114
115 int cpu_down(unsigned int cpu)
116 {
117         int err;
118         struct task_struct *p;
119         cpumask_t old_allowed, tmp;
120
121         if ((err = lock_cpu_hotplug_interruptible()) != 0)
122                 return err;
123
124         if (num_online_cpus() == 1) {
125                 err = -EBUSY;
126                 goto out;
127         }
128
129         if (!cpu_online(cpu)) {
130                 err = -EINVAL;
131                 goto out;
132         }
133
134         /* Ensure that we are not runnable on dying cpu */
135         old_allowed = current->cpus_allowed;
136         tmp = CPU_MASK_ALL;
137         cpu_clear(cpu, tmp);
138         set_cpus_allowed(current, tmp);
139
140         p = __stop_machine_run(take_cpu_down, NULL, cpu);
141         if (IS_ERR(p)) {
142                 err = PTR_ERR(p);
143                 goto out_allowed;
144         }
145
146         if (cpu_online(cpu))
147                 goto out_thread;
148
149         /* Wait for it to sleep (leaving idle task). */
150         while (!idle_cpu(cpu))
151                 yield();
152
153         /* This actually kills the CPU. */
154         __cpu_die(cpu);
155
156         /* Move it here so it can run. */
157         kthread_bind(p, smp_processor_id());
158
159         /* CPU is completely dead: tell everyone.  Too late to complain. */
160         if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
161             == NOTIFY_BAD)
162                 BUG();
163
164         check_for_tasks(cpu);
165
166         cpu_run_sbin_hotplug(cpu, "offline");
167
168 out_thread:
169         err = kthread_stop(p);
170 out_allowed:
171         set_cpus_allowed(current, old_allowed);
172 out:
173         unlock_cpu_hotplug();
174         return err;
175 }
176 #else
177 static inline int cpu_run_sbin_hotplug(unsigned int cpu, const char *action)
178 {
179         return 0;
180 }
181 #endif /*CONFIG_HOTPLUG_CPU*/
182
183 int __devinit cpu_up(unsigned int cpu)
184 {
185         int ret;
186         void *hcpu = (void *)(long)cpu;
187
188         if ((ret = down_interruptible(&cpucontrol)) != 0)
189                 return ret;
190
191         if (cpu_online(cpu) || !cpu_present(cpu)) {
192                 ret = -EINVAL;
193                 goto out;
194         }
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",
198                                 __FUNCTION__, cpu);
199                 ret = -EINVAL;
200                 goto out_notify;
201         }
202
203         /* Arch-specific enabling code. */
204         ret = __cpu_up(cpu);
205         if (ret != 0)
206                 goto out_notify;
207         if (!cpu_online(cpu))
208                 BUG();
209
210         /* Now call notifier in preparation. */
211         notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
212
213 out_notify:
214         if (ret != 0)
215                 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
216 out:
217         up(&cpucontrol);
218         return ret;
219 }