blob: e882c6babf414854a53b615229e49005d3cc4d75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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/kthread.h>
15#include <linux/stop_machine.h>
16#include <asm/semaphore.h>
17
18/* This protects CPUs going up and down... */
Ashok Raja9d9baa2005-11-28 13:43:46 -080019static DECLARE_MUTEX(cpucontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21static struct notifier_block *cpu_chain;
22
Ashok Raja9d9baa2005-11-28 13:43:46 -080023#ifdef CONFIG_HOTPLUG_CPU
24static struct task_struct *lock_cpu_hotplug_owner;
25static int lock_cpu_hotplug_depth;
Ashok Raj90d45d12005-11-08 21:34:24 -080026
Ashok Raja9d9baa2005-11-28 13:43:46 -080027static int __lock_cpu_hotplug(int interruptible)
Ashok Raj90d45d12005-11-08 21:34:24 -080028{
Ashok Raja9d9baa2005-11-28 13:43:46 -080029 int ret = 0;
30
31 if (lock_cpu_hotplug_owner != current) {
32 if (interruptible)
33 ret = down_interruptible(&cpucontrol);
34 else
35 down(&cpucontrol);
36 }
37
38 /*
39 * Set only if we succeed in locking
40 */
41 if (!ret) {
42 lock_cpu_hotplug_depth++;
43 lock_cpu_hotplug_owner = current;
44 }
45
46 return ret;
Ashok Raj90d45d12005-11-08 21:34:24 -080047}
48
Ashok Raja9d9baa2005-11-28 13:43:46 -080049void lock_cpu_hotplug(void)
50{
51 __lock_cpu_hotplug(0);
52}
53EXPORT_SYMBOL_GPL(lock_cpu_hotplug);
Ashok Raj90d45d12005-11-08 21:34:24 -080054
Ashok Raja9d9baa2005-11-28 13:43:46 -080055void unlock_cpu_hotplug(void)
56{
57 if (--lock_cpu_hotplug_depth == 0) {
58 lock_cpu_hotplug_owner = NULL;
59 up(&cpucontrol);
60 }
61}
62EXPORT_SYMBOL_GPL(unlock_cpu_hotplug);
63
64int lock_cpu_hotplug_interruptible(void)
65{
66 return __lock_cpu_hotplug(1);
67}
68EXPORT_SYMBOL_GPL(lock_cpu_hotplug_interruptible);
69#endif /* CONFIG_HOTPLUG_CPU */
Ashok Raj90d45d12005-11-08 21:34:24 -080070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Need to know about CPUs going up/down? */
72int register_cpu_notifier(struct notifier_block *nb)
73{
74 int ret;
75
Ashok Raja9d9baa2005-11-28 13:43:46 -080076 if ((ret = lock_cpu_hotplug_interruptible()) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return ret;
78 ret = notifier_chain_register(&cpu_chain, nb);
Ashok Raja9d9baa2005-11-28 13:43:46 -080079 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return ret;
81}
82EXPORT_SYMBOL(register_cpu_notifier);
83
84void unregister_cpu_notifier(struct notifier_block *nb)
85{
Ashok Raja9d9baa2005-11-28 13:43:46 -080086 lock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 notifier_chain_unregister(&cpu_chain, nb);
Ashok Raja9d9baa2005-11-28 13:43:46 -080088 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90EXPORT_SYMBOL(unregister_cpu_notifier);
91
92#ifdef CONFIG_HOTPLUG_CPU
93static inline void check_for_tasks(int cpu)
94{
95 struct task_struct *p;
96
97 write_lock_irq(&tasklist_lock);
98 for_each_process(p) {
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);
105 }
106 write_unlock_irq(&tasklist_lock);
107}
108
109/* Take this CPU down. */
110static int take_cpu_down(void *unused)
111{
112 int err;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /* Ensure this CPU doesn't handle any more interrupts. */
115 err = __cpu_disable();
116 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700119 /* Force idle task to run as soon as we yield: it should
120 immediately notice cpu is offline and die quickly. */
121 sched_idle_next();
122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125int cpu_down(unsigned int cpu)
126{
127 int err;
128 struct task_struct *p;
129 cpumask_t old_allowed, tmp;
130
131 if ((err = lock_cpu_hotplug_interruptible()) != 0)
132 return err;
133
134 if (num_online_cpus() == 1) {
135 err = -EBUSY;
136 goto out;
137 }
138
139 if (!cpu_online(cpu)) {
140 err = -EINVAL;
141 goto out;
142 }
143
144 err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
145 (void *)(long)cpu);
146 if (err == NOTIFY_BAD) {
147 printk("%s: attempt to take down CPU %u failed\n",
148 __FUNCTION__, cpu);
149 err = -EINVAL;
150 goto out;
151 }
152
153 /* Ensure that we are not runnable on dying cpu */
154 old_allowed = current->cpus_allowed;
155 tmp = CPU_MASK_ALL;
156 cpu_clear(cpu, tmp);
157 set_cpus_allowed(current, tmp);
158
159 p = __stop_machine_run(take_cpu_down, NULL, cpu);
160 if (IS_ERR(p)) {
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)
164 BUG();
165
166 err = PTR_ERR(p);
167 goto out_allowed;
168 }
169
170 if (cpu_online(cpu))
171 goto out_thread;
172
173 /* Wait for it to sleep (leaving idle task). */
174 while (!idle_cpu(cpu))
175 yield();
176
177 /* This actually kills the CPU. */
178 __cpu_die(cpu);
179
180 /* Move it here so it can run. */
181 kthread_bind(p, get_cpu());
182 put_cpu();
183
184 /* CPU is completely dead: tell everyone. Too late to complain. */
185 if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
186 == NOTIFY_BAD)
187 BUG();
188
189 check_for_tasks(cpu);
190
191out_thread:
192 err = kthread_stop(p);
193out_allowed:
194 set_cpus_allowed(current, old_allowed);
195out:
196 unlock_cpu_hotplug();
197 return err;
198}
199#endif /*CONFIG_HOTPLUG_CPU*/
200
201int __devinit cpu_up(unsigned int cpu)
202{
203 int ret;
204 void *hcpu = (void *)(long)cpu;
205
Ashok Raja9d9baa2005-11-28 13:43:46 -0800206 if ((ret = lock_cpu_hotplug_interruptible()) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return ret;
208
209 if (cpu_online(cpu) || !cpu_present(cpu)) {
210 ret = -EINVAL;
211 goto out;
212 }
Ashok Raj90d45d12005-11-08 21:34:24 -0800213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 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",
217 __FUNCTION__, cpu);
218 ret = -EINVAL;
219 goto out_notify;
220 }
221
222 /* Arch-specific enabling code. */
223 ret = __cpu_up(cpu);
224 if (ret != 0)
225 goto out_notify;
226 if (!cpu_online(cpu))
227 BUG();
228
229 /* Now call notifier in preparation. */
230 notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
231
232out_notify:
233 if (ret != 0)
234 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
235out:
Ashok Raja9d9baa2005-11-28 13:43:46 -0800236 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return ret;
238}