blob: 70fbf2e83766abb527d84bc6c4ddc30646036609 [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>
Ingo Molnar81615b62006-06-26 00:24:32 -070016#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/* This protects CPUs going up and down... */
Ingo Molnar81615b62006-06-26 00:24:32 -070019static DEFINE_MUTEX(cpucontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Chandra Seetharaman65edc682006-06-27 02:54:08 -070021static __cpuinitdata BLOCKING_NOTIFIER_HEAD(cpu_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
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)
Ingo Molnar81615b62006-06-26 00:24:32 -070033 ret = mutex_lock_interruptible(&cpucontrol);
Ashok Raja9d9baa2005-11-28 13:43:46 -080034 else
Ingo Molnar81615b62006-06-26 00:24:32 -070035 mutex_lock(&cpucontrol);
Ashok Raja9d9baa2005-11-28 13:43:46 -080036 }
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;
Ingo Molnar81615b62006-06-26 00:24:32 -070059 mutex_unlock(&cpucontrol);
Ashok Raja9d9baa2005-11-28 13:43:46 -080060 }
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? */
Chandra Seetharaman65edc682006-06-27 02:54:08 -070072int __cpuinit register_cpu_notifier(struct notifier_block *nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Alan Sterne041c682006-03-27 01:16:30 -080074 return blocking_notifier_chain_register(&cpu_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Chandra Seetharaman65edc682006-06-27 02:54:08 -070076
77#ifdef CONFIG_HOTPLUG_CPU
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079EXPORT_SYMBOL(register_cpu_notifier);
80
81void unregister_cpu_notifier(struct notifier_block *nb)
82{
Alan Sterne041c682006-03-27 01:16:30 -080083 blocking_notifier_chain_unregister(&cpu_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85EXPORT_SYMBOL(unregister_cpu_notifier);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static inline void check_for_tasks(int cpu)
88{
89 struct task_struct *p;
90
91 write_lock_irq(&tasklist_lock);
92 for_each_process(p) {
93 if (task_cpu(p) == cpu &&
94 (!cputime_eq(p->utime, cputime_zero) ||
95 !cputime_eq(p->stime, cputime_zero)))
96 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
97 (state = %ld, flags = %lx) \n",
98 p->comm, p->pid, cpu, p->state, p->flags);
99 }
100 write_unlock_irq(&tasklist_lock);
101}
102
103/* Take this CPU down. */
104static int take_cpu_down(void *unused)
105{
106 int err;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* Ensure this CPU doesn't handle any more interrupts. */
109 err = __cpu_disable();
110 if (err < 0)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700111 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700113 /* Force idle task to run as soon as we yield: it should
114 immediately notice cpu is offline and die quickly. */
115 sched_idle_next();
116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
119int cpu_down(unsigned int cpu)
120{
121 int err;
122 struct task_struct *p;
123 cpumask_t old_allowed, tmp;
124
125 if ((err = lock_cpu_hotplug_interruptible()) != 0)
126 return err;
127
128 if (num_online_cpus() == 1) {
129 err = -EBUSY;
130 goto out;
131 }
132
133 if (!cpu_online(cpu)) {
134 err = -EINVAL;
135 goto out;
136 }
137
Alan Sterne041c682006-03-27 01:16:30 -0800138 err = blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 (void *)(long)cpu);
140 if (err == NOTIFY_BAD) {
141 printk("%s: attempt to take down CPU %u failed\n",
142 __FUNCTION__, cpu);
143 err = -EINVAL;
144 goto out;
145 }
146
147 /* Ensure that we are not runnable on dying cpu */
148 old_allowed = current->cpus_allowed;
149 tmp = CPU_MASK_ALL;
150 cpu_clear(cpu, tmp);
151 set_cpus_allowed(current, tmp);
152
153 p = __stop_machine_run(take_cpu_down, NULL, cpu);
154 if (IS_ERR(p)) {
155 /* CPU didn't die: tell everyone. Can't complain. */
Alan Sterne041c682006-03-27 01:16:30 -0800156 if (blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 (void *)(long)cpu) == NOTIFY_BAD)
158 BUG();
159
160 err = PTR_ERR(p);
161 goto out_allowed;
162 }
163
164 if (cpu_online(cpu))
165 goto out_thread;
166
167 /* Wait for it to sleep (leaving idle task). */
168 while (!idle_cpu(cpu))
169 yield();
170
171 /* This actually kills the CPU. */
172 __cpu_die(cpu);
173
174 /* Move it here so it can run. */
175 kthread_bind(p, get_cpu());
176 put_cpu();
177
178 /* CPU is completely dead: tell everyone. Too late to complain. */
Alan Sterne041c682006-03-27 01:16:30 -0800179 if (blocking_notifier_call_chain(&cpu_chain, CPU_DEAD,
180 (void *)(long)cpu) == NOTIFY_BAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 BUG();
182
183 check_for_tasks(cpu);
184
185out_thread:
186 err = kthread_stop(p);
187out_allowed:
188 set_cpus_allowed(current, old_allowed);
189out:
190 unlock_cpu_hotplug();
191 return err;
192}
193#endif /*CONFIG_HOTPLUG_CPU*/
194
195int __devinit cpu_up(unsigned int cpu)
196{
197 int ret;
198 void *hcpu = (void *)(long)cpu;
199
Ashok Raja9d9baa2005-11-28 13:43:46 -0800200 if ((ret = lock_cpu_hotplug_interruptible()) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return ret;
202
203 if (cpu_online(cpu) || !cpu_present(cpu)) {
204 ret = -EINVAL;
205 goto out;
206 }
Ashok Raj90d45d12005-11-08 21:34:24 -0800207
Alan Sterne041c682006-03-27 01:16:30 -0800208 ret = blocking_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (ret == NOTIFY_BAD) {
210 printk("%s: attempt to bring up CPU %u failed\n",
211 __FUNCTION__, cpu);
212 ret = -EINVAL;
213 goto out_notify;
214 }
215
216 /* Arch-specific enabling code. */
217 ret = __cpu_up(cpu);
218 if (ret != 0)
219 goto out_notify;
Eric Sesterhenn6978c702006-03-24 18:45:21 +0100220 BUG_ON(!cpu_online(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Now call notifier in preparation. */
Alan Sterne041c682006-03-27 01:16:30 -0800223 blocking_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225out_notify:
226 if (ret != 0)
Alan Sterne041c682006-03-27 01:16:30 -0800227 blocking_notifier_call_chain(&cpu_chain,
228 CPU_UP_CANCELED, hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229out:
Ashok Raja9d9baa2005-11-28 13:43:46 -0800230 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return ret;
232}