backtrace: replace timer with tasklet + completions
[GitHub/mt8127/android_kernel_alcatel_ttab.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 17
d221938c 18/* Serializes the updates to cpu_online_map, cpu_present_map */
aa953877 19static DEFINE_MUTEX(cpu_add_remove_lock);
1da177e4 20
bd5349cf 21static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain);
1da177e4 22
e3920fb4
RW
23/* If set, cpu_up and cpu_down will return -EBUSY and do nothing.
24 * Should always be manipulated under cpu_add_remove_lock
25 */
26static int cpu_hotplug_disabled;
27
d221938c
GS
28static struct {
29 struct task_struct *active_writer;
30 struct mutex lock; /* Synchronizes accesses to refcount, */
31 /*
32 * Also blocks the new readers during
33 * an ongoing cpu hotplug operation.
34 */
35 int refcount;
d221938c 36} cpu_hotplug;
90d45d17 37
d221938c
GS
38void __init cpu_hotplug_init(void)
39{
40 cpu_hotplug.active_writer = NULL;
41 mutex_init(&cpu_hotplug.lock);
42 cpu_hotplug.refcount = 0;
d221938c
GS
43}
44
45#ifdef CONFIG_HOTPLUG_CPU
90d45d17 46
86ef5c9a 47void get_online_cpus(void)
a9d9baa1 48{
d221938c
GS
49 might_sleep();
50 if (cpu_hotplug.active_writer == current)
aa953877 51 return;
d221938c
GS
52 mutex_lock(&cpu_hotplug.lock);
53 cpu_hotplug.refcount++;
54 mutex_unlock(&cpu_hotplug.lock);
55
a9d9baa1 56}
86ef5c9a 57EXPORT_SYMBOL_GPL(get_online_cpus);
90d45d17 58
86ef5c9a 59void put_online_cpus(void)
a9d9baa1 60{
d221938c 61 if (cpu_hotplug.active_writer == current)
aa953877 62 return;
d221938c 63 mutex_lock(&cpu_hotplug.lock);
d2ba7e2a
ON
64 if (!--cpu_hotplug.refcount && unlikely(cpu_hotplug.active_writer))
65 wake_up_process(cpu_hotplug.active_writer);
d221938c
GS
66 mutex_unlock(&cpu_hotplug.lock);
67
a9d9baa1 68}
86ef5c9a 69EXPORT_SYMBOL_GPL(put_online_cpus);
a9d9baa1 70
a9d9baa1 71#endif /* CONFIG_HOTPLUG_CPU */
90d45d17 72
d221938c
GS
73/*
74 * The following two API's must be used when attempting
75 * to serialize the updates to cpu_online_map, cpu_present_map.
76 */
77void cpu_maps_update_begin(void)
78{
79 mutex_lock(&cpu_add_remove_lock);
80}
81
82void cpu_maps_update_done(void)
83{
84 mutex_unlock(&cpu_add_remove_lock);
85}
86
87/*
88 * This ensures that the hotplug operation can begin only when the
89 * refcount goes to zero.
90 *
91 * Note that during a cpu-hotplug operation, the new readers, if any,
92 * will be blocked by the cpu_hotplug.lock
93 *
d2ba7e2a
ON
94 * Since cpu_hotplug_begin() is always called after invoking
95 * cpu_maps_update_begin(), we can be sure that only one writer is active.
d221938c
GS
96 *
97 * Note that theoretically, there is a possibility of a livelock:
98 * - Refcount goes to zero, last reader wakes up the sleeping
99 * writer.
100 * - Last reader unlocks the cpu_hotplug.lock.
101 * - A new reader arrives at this moment, bumps up the refcount.
102 * - The writer acquires the cpu_hotplug.lock finds the refcount
103 * non zero and goes to sleep again.
104 *
105 * However, this is very difficult to achieve in practice since
86ef5c9a 106 * get_online_cpus() not an api which is called all that often.
d221938c
GS
107 *
108 */
109static void cpu_hotplug_begin(void)
110{
d221938c 111 cpu_hotplug.active_writer = current;
d2ba7e2a
ON
112
113 for (;;) {
114 mutex_lock(&cpu_hotplug.lock);
115 if (likely(!cpu_hotplug.refcount))
116 break;
117 __set_current_state(TASK_UNINTERRUPTIBLE);
d221938c
GS
118 mutex_unlock(&cpu_hotplug.lock);
119 schedule();
d221938c 120 }
d221938c
GS
121}
122
123static void cpu_hotplug_done(void)
124{
125 cpu_hotplug.active_writer = NULL;
126 mutex_unlock(&cpu_hotplug.lock);
127}
1da177e4 128/* Need to know about CPUs going up/down? */
f7b16c10 129int __ref register_cpu_notifier(struct notifier_block *nb)
1da177e4 130{
bd5349cf 131 int ret;
d221938c 132 cpu_maps_update_begin();
bd5349cf 133 ret = raw_notifier_chain_register(&cpu_chain, nb);
d221938c 134 cpu_maps_update_done();
bd5349cf 135 return ret;
1da177e4 136}
65edc68c
CS
137
138#ifdef CONFIG_HOTPLUG_CPU
139
1da177e4
LT
140EXPORT_SYMBOL(register_cpu_notifier);
141
9647155f 142void __ref unregister_cpu_notifier(struct notifier_block *nb)
1da177e4 143{
d221938c 144 cpu_maps_update_begin();
bd5349cf 145 raw_notifier_chain_unregister(&cpu_chain, nb);
d221938c 146 cpu_maps_update_done();
1da177e4
LT
147}
148EXPORT_SYMBOL(unregister_cpu_notifier);
149
1da177e4
LT
150static inline void check_for_tasks(int cpu)
151{
152 struct task_struct *p;
153
154 write_lock_irq(&tasklist_lock);
155 for_each_process(p) {
156 if (task_cpu(p) == cpu &&
157 (!cputime_eq(p->utime, cputime_zero) ||
158 !cputime_eq(p->stime, cputime_zero)))
159 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
e7407dcc 160 (state = %ld, flags = %x) \n",
ba25f9dc
PE
161 p->comm, task_pid_nr(p), cpu,
162 p->state, p->flags);
1da177e4
LT
163 }
164 write_unlock_irq(&tasklist_lock);
165}
166
db912f96
AK
167struct take_cpu_down_param {
168 unsigned long mod;
169 void *hcpu;
170};
171
1da177e4 172/* Take this CPU down. */
514a20a5 173static int __ref take_cpu_down(void *_param)
1da177e4 174{
db912f96 175 struct take_cpu_down_param *param = _param;
1da177e4
LT
176 int err;
177
db912f96
AK
178 raw_notifier_call_chain(&cpu_chain, CPU_DYING | param->mod,
179 param->hcpu);
1da177e4
LT
180 /* Ensure this CPU doesn't handle any more interrupts. */
181 err = __cpu_disable();
182 if (err < 0)
f3705136 183 return err;
1da177e4 184
f3705136
ZM
185 /* Force idle task to run as soon as we yield: it should
186 immediately notice cpu is offline and die quickly. */
187 sched_idle_next();
188 return 0;
1da177e4
LT
189}
190
e3920fb4 191/* Requires cpu_add_remove_lock to be held */
514a20a5 192static int __ref _cpu_down(unsigned int cpu, int tasks_frozen)
1da177e4 193{
e7407dcc 194 int err, nr_calls = 0;
1da177e4
LT
195 struct task_struct *p;
196 cpumask_t old_allowed, tmp;
e7407dcc 197 void *hcpu = (void *)(long)cpu;
8bb78442 198 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
db912f96
AK
199 struct take_cpu_down_param tcd_param = {
200 .mod = mod,
201 .hcpu = hcpu,
202 };
1da177e4 203
e3920fb4
RW
204 if (num_online_cpus() == 1)
205 return -EBUSY;
1da177e4 206
e3920fb4
RW
207 if (!cpu_online(cpu))
208 return -EINVAL;
1da177e4 209
d221938c 210 cpu_hotplug_begin();
8bb78442 211 err = __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE | mod,
e7407dcc 212 hcpu, -1, &nr_calls);
1da177e4 213 if (err == NOTIFY_BAD) {
a0d8cdb6 214 nr_calls--;
8bb78442
RW
215 __raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
216 hcpu, nr_calls, NULL);
1da177e4 217 printk("%s: attempt to take down CPU %u failed\n",
af1f16d0 218 __func__, cpu);
baaca49f
GS
219 err = -EINVAL;
220 goto out_release;
1da177e4
LT
221 }
222
223 /* Ensure that we are not runnable on dying cpu */
224 old_allowed = current->cpus_allowed;
f70316da 225 cpus_setall(tmp);
1da177e4 226 cpu_clear(cpu, tmp);
f70316da 227 set_cpus_allowed_ptr(current, &tmp);
1da177e4 228
db912f96 229 p = __stop_machine_run(take_cpu_down, &tcd_param, cpu);
aa953877 230
8fa1d7d3 231 if (IS_ERR(p) || cpu_online(cpu)) {
1da177e4 232 /* CPU didn't die: tell everyone. Can't complain. */
8bb78442 233 if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED | mod,
e7407dcc 234 hcpu) == NOTIFY_BAD)
1da177e4
LT
235 BUG();
236
8fa1d7d3
ST
237 if (IS_ERR(p)) {
238 err = PTR_ERR(p);
239 goto out_allowed;
240 }
1da177e4 241 goto out_thread;
8fa1d7d3 242 }
1da177e4
LT
243
244 /* Wait for it to sleep (leaving idle task). */
245 while (!idle_cpu(cpu))
246 yield();
247
248 /* This actually kills the CPU. */
249 __cpu_die(cpu);
250
1da177e4 251 /* CPU is completely dead: tell everyone. Too late to complain. */
8bb78442
RW
252 if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD | mod,
253 hcpu) == NOTIFY_BAD)
1da177e4
LT
254 BUG();
255
256 check_for_tasks(cpu);
257
258out_thread:
259 err = kthread_stop(p);
260out_allowed:
f70316da 261 set_cpus_allowed_ptr(current, &old_allowed);
baaca49f 262out_release:
d221938c 263 cpu_hotplug_done();
e3920fb4
RW
264 return err;
265}
266
514a20a5 267int __ref cpu_down(unsigned int cpu)
e3920fb4
RW
268{
269 int err = 0;
270
d221938c 271 cpu_maps_update_begin();
e3920fb4
RW
272 if (cpu_hotplug_disabled)
273 err = -EBUSY;
274 else
8bb78442 275 err = _cpu_down(cpu, 0);
e3920fb4 276
d221938c 277 cpu_maps_update_done();
1da177e4
LT
278 return err;
279}
280#endif /*CONFIG_HOTPLUG_CPU*/
281
e3920fb4 282/* Requires cpu_add_remove_lock to be held */
8bb78442 283static int __cpuinit _cpu_up(unsigned int cpu, int tasks_frozen)
1da177e4 284{
baaca49f 285 int ret, nr_calls = 0;
1da177e4 286 void *hcpu = (void *)(long)cpu;
8bb78442 287 unsigned long mod = tasks_frozen ? CPU_TASKS_FROZEN : 0;
1da177e4 288
e3920fb4
RW
289 if (cpu_online(cpu) || !cpu_present(cpu))
290 return -EINVAL;
90d45d17 291
d221938c 292 cpu_hotplug_begin();
8bb78442 293 ret = __raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE | mod, hcpu,
baaca49f 294 -1, &nr_calls);
1da177e4 295 if (ret == NOTIFY_BAD) {
a0d8cdb6 296 nr_calls--;
1da177e4 297 printk("%s: attempt to bring up CPU %u failed\n",
af1f16d0 298 __func__, cpu);
1da177e4
LT
299 ret = -EINVAL;
300 goto out_notify;
301 }
302
303 /* Arch-specific enabling code. */
304 ret = __cpu_up(cpu);
305 if (ret != 0)
306 goto out_notify;
6978c705 307 BUG_ON(!cpu_online(cpu));
1da177e4
LT
308
309 /* Now call notifier in preparation. */
8bb78442 310 raw_notifier_call_chain(&cpu_chain, CPU_ONLINE | mod, hcpu);
1da177e4
LT
311
312out_notify:
313 if (ret != 0)
baaca49f 314 __raw_notifier_call_chain(&cpu_chain,
8bb78442 315 CPU_UP_CANCELED | mod, hcpu, nr_calls, NULL);
d221938c 316 cpu_hotplug_done();
e3920fb4
RW
317
318 return ret;
319}
320
b282b6f8 321int __cpuinit cpu_up(unsigned int cpu)
e3920fb4
RW
322{
323 int err = 0;
73e753a5
KH
324 if (!cpu_isset(cpu, cpu_possible_map)) {
325 printk(KERN_ERR "can't online cpu %d because it is not "
326 "configured as may-hotadd at boot time\n", cpu);
327#if defined(CONFIG_IA64) || defined(CONFIG_X86_64) || defined(CONFIG_S390)
328 printk(KERN_ERR "please check additional_cpus= boot "
329 "parameter\n");
330#endif
331 return -EINVAL;
332 }
e3920fb4 333
d221938c 334 cpu_maps_update_begin();
e3920fb4
RW
335 if (cpu_hotplug_disabled)
336 err = -EBUSY;
337 else
8bb78442 338 err = _cpu_up(cpu, 0);
e3920fb4 339
d221938c 340 cpu_maps_update_done();
e3920fb4
RW
341 return err;
342}
343
f3de4be9 344#ifdef CONFIG_PM_SLEEP_SMP
e3920fb4
RW
345static cpumask_t frozen_cpus;
346
347int disable_nonboot_cpus(void)
348{
e1d9fd2e 349 int cpu, first_cpu, error = 0;
e3920fb4 350
d221938c 351 cpu_maps_update_begin();
1d64b9cb 352 first_cpu = first_cpu(cpu_online_map);
e3920fb4
RW
353 /* We take down all of the non-boot CPUs in one shot to avoid races
354 * with the userspace trying to use the CPU hotplug at the same time
355 */
356 cpus_clear(frozen_cpus);
357 printk("Disabling non-boot CPUs ...\n");
358 for_each_online_cpu(cpu) {
359 if (cpu == first_cpu)
360 continue;
8bb78442 361 error = _cpu_down(cpu, 1);
e3920fb4
RW
362 if (!error) {
363 cpu_set(cpu, frozen_cpus);
364 printk("CPU%d is down\n", cpu);
365 } else {
366 printk(KERN_ERR "Error taking CPU%d down: %d\n",
367 cpu, error);
368 break;
369 }
370 }
371 if (!error) {
372 BUG_ON(num_online_cpus() > 1);
373 /* Make sure the CPUs won't be enabled by someone else */
374 cpu_hotplug_disabled = 1;
375 } else {
e1d9fd2e 376 printk(KERN_ERR "Non-boot CPUs are not disabled\n");
e3920fb4 377 }
d221938c 378 cpu_maps_update_done();
e3920fb4
RW
379 return error;
380}
381
fa7303e2 382void __ref enable_nonboot_cpus(void)
e3920fb4
RW
383{
384 int cpu, error;
385
386 /* Allow everyone to use the CPU hotplug again */
d221938c 387 cpu_maps_update_begin();
e3920fb4 388 cpu_hotplug_disabled = 0;
ed746e3b 389 if (cpus_empty(frozen_cpus))
1d64b9cb 390 goto out;
e3920fb4
RW
391
392 printk("Enabling non-boot CPUs ...\n");
393 for_each_cpu_mask(cpu, frozen_cpus) {
8bb78442 394 error = _cpu_up(cpu, 1);
e3920fb4
RW
395 if (!error) {
396 printk("CPU%d is up\n", cpu);
397 continue;
398 }
1d64b9cb 399 printk(KERN_WARNING "Error taking CPU%d up: %d\n", cpu, error);
e3920fb4
RW
400 }
401 cpus_clear(frozen_cpus);
1d64b9cb 402out:
d221938c 403 cpu_maps_update_done();
1da177e4 404}
f3de4be9 405#endif /* CONFIG_PM_SLEEP_SMP */