[PATCH] cpu hotplug: revert initdata patch submitted for 2.6.17
[linux-2.6-block.git] / kernel / cpu.c
CommitLineData
1da177e4
LT
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/kthread.h>
15#include <linux/stop_machine.h>
81615b62 16#include <linux/mutex.h>
1da177e4
LT
17
18/* This protects CPUs going up and down... */
81615b62 19static DEFINE_MUTEX(cpucontrol);
1da177e4 20
e041c683 21static BLOCKING_NOTIFIER_HEAD(cpu_chain);
1da177e4 22
a9d9baa1
AR
23#ifdef CONFIG_HOTPLUG_CPU
24static struct task_struct *lock_cpu_hotplug_owner;
25static int lock_cpu_hotplug_depth;
90d45d17 26
a9d9baa1 27static int __lock_cpu_hotplug(int interruptible)
90d45d17 28{
a9d9baa1
AR
29 int ret = 0;
30
31 if (lock_cpu_hotplug_owner != current) {
32 if (interruptible)
81615b62 33 ret = mutex_lock_interruptible(&cpucontrol);
a9d9baa1 34 else
81615b62 35 mutex_lock(&cpucontrol);
a9d9baa1
AR
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;
90d45d17
AR
47}
48
a9d9baa1
AR
49void lock_cpu_hotplug(void)
50{
51 __lock_cpu_hotplug(0);
52}
53EXPORT_SYMBOL_GPL(lock_cpu_hotplug);
90d45d17 54
a9d9baa1
AR
55void unlock_cpu_hotplug(void)
56{
57 if (--lock_cpu_hotplug_depth == 0) {
58 lock_cpu_hotplug_owner = NULL;
81615b62 59 mutex_unlock(&cpucontrol);
a9d9baa1
AR
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 */
90d45d17 70
1da177e4
LT
71/* Need to know about CPUs going up/down? */
72int register_cpu_notifier(struct notifier_block *nb)
73{
e041c683 74 return blocking_notifier_chain_register(&cpu_chain, nb);
1da177e4
LT
75}
76EXPORT_SYMBOL(register_cpu_notifier);
77
78void unregister_cpu_notifier(struct notifier_block *nb)
79{
e041c683 80 blocking_notifier_chain_unregister(&cpu_chain, nb);
1da177e4
LT
81}
82EXPORT_SYMBOL(unregister_cpu_notifier);
83
84#ifdef CONFIG_HOTPLUG_CPU
85static inline void check_for_tasks(int cpu)
86{
87 struct task_struct *p;
88
89 write_lock_irq(&tasklist_lock);
90 for_each_process(p) {
91 if (task_cpu(p) == cpu &&
92 (!cputime_eq(p->utime, cputime_zero) ||
93 !cputime_eq(p->stime, cputime_zero)))
94 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
95 (state = %ld, flags = %lx) \n",
96 p->comm, p->pid, cpu, p->state, p->flags);
97 }
98 write_unlock_irq(&tasklist_lock);
99}
100
101/* Take this CPU down. */
102static int take_cpu_down(void *unused)
103{
104 int err;
105
1da177e4
LT
106 /* Ensure this CPU doesn't handle any more interrupts. */
107 err = __cpu_disable();
108 if (err < 0)
f3705136 109 return err;
1da177e4 110
f3705136
ZM
111 /* Force idle task to run as soon as we yield: it should
112 immediately notice cpu is offline and die quickly. */
113 sched_idle_next();
114 return 0;
1da177e4
LT
115}
116
117int cpu_down(unsigned int cpu)
118{
119 int err;
120 struct task_struct *p;
121 cpumask_t old_allowed, tmp;
122
123 if ((err = lock_cpu_hotplug_interruptible()) != 0)
124 return err;
125
126 if (num_online_cpus() == 1) {
127 err = -EBUSY;
128 goto out;
129 }
130
131 if (!cpu_online(cpu)) {
132 err = -EINVAL;
133 goto out;
134 }
135
e041c683 136 err = blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
1da177e4
LT
137 (void *)(long)cpu);
138 if (err == NOTIFY_BAD) {
139 printk("%s: attempt to take down CPU %u failed\n",
140 __FUNCTION__, cpu);
141 err = -EINVAL;
142 goto out;
143 }
144
145 /* Ensure that we are not runnable on dying cpu */
146 old_allowed = current->cpus_allowed;
147 tmp = CPU_MASK_ALL;
148 cpu_clear(cpu, tmp);
149 set_cpus_allowed(current, tmp);
150
151 p = __stop_machine_run(take_cpu_down, NULL, cpu);
152 if (IS_ERR(p)) {
153 /* CPU didn't die: tell everyone. Can't complain. */
e041c683 154 if (blocking_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
1da177e4
LT
155 (void *)(long)cpu) == NOTIFY_BAD)
156 BUG();
157
158 err = PTR_ERR(p);
159 goto out_allowed;
160 }
161
162 if (cpu_online(cpu))
163 goto out_thread;
164
165 /* Wait for it to sleep (leaving idle task). */
166 while (!idle_cpu(cpu))
167 yield();
168
169 /* This actually kills the CPU. */
170 __cpu_die(cpu);
171
172 /* Move it here so it can run. */
173 kthread_bind(p, get_cpu());
174 put_cpu();
175
176 /* CPU is completely dead: tell everyone. Too late to complain. */
e041c683
AS
177 if (blocking_notifier_call_chain(&cpu_chain, CPU_DEAD,
178 (void *)(long)cpu) == NOTIFY_BAD)
1da177e4
LT
179 BUG();
180
181 check_for_tasks(cpu);
182
183out_thread:
184 err = kthread_stop(p);
185out_allowed:
186 set_cpus_allowed(current, old_allowed);
187out:
188 unlock_cpu_hotplug();
189 return err;
190}
191#endif /*CONFIG_HOTPLUG_CPU*/
192
193int __devinit cpu_up(unsigned int cpu)
194{
195 int ret;
196 void *hcpu = (void *)(long)cpu;
197
a9d9baa1 198 if ((ret = lock_cpu_hotplug_interruptible()) != 0)
1da177e4
LT
199 return ret;
200
201 if (cpu_online(cpu) || !cpu_present(cpu)) {
202 ret = -EINVAL;
203 goto out;
204 }
90d45d17 205
e041c683 206 ret = blocking_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
1da177e4
LT
207 if (ret == NOTIFY_BAD) {
208 printk("%s: attempt to bring up CPU %u failed\n",
209 __FUNCTION__, cpu);
210 ret = -EINVAL;
211 goto out_notify;
212 }
213
214 /* Arch-specific enabling code. */
215 ret = __cpu_up(cpu);
216 if (ret != 0)
217 goto out_notify;
6978c705 218 BUG_ON(!cpu_online(cpu));
1da177e4
LT
219
220 /* Now call notifier in preparation. */
e041c683 221 blocking_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
1da177e4
LT
222
223out_notify:
224 if (ret != 0)
e041c683
AS
225 blocking_notifier_call_chain(&cpu_chain,
226 CPU_UP_CANCELED, hcpu);
1da177e4 227out:
a9d9baa1 228 unlock_cpu_hotplug();
1da177e4
LT
229 return ret;
230}