Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/shaggy...
[wrt350n-kernel.git] / kernel / cpu.c
blob53d8263ae12ebc00483a3ad999177799003467e2
1 /* CPU control.
2 * (C) 2001, 2002, 2003, 2004 Rusty Russell
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>
18 /* This protects CPUs going up and down... */
19 DECLARE_MUTEX(cpucontrol);
21 static struct notifier_block *cpu_chain;
23 /* Need to know about CPUs going up/down? */
24 int register_cpu_notifier(struct notifier_block *nb)
26 int ret;
28 if ((ret = down_interruptible(&cpucontrol)) != 0)
29 return ret;
30 ret = notifier_chain_register(&cpu_chain, nb);
31 up(&cpucontrol);
32 return ret;
34 EXPORT_SYMBOL(register_cpu_notifier);
36 void unregister_cpu_notifier(struct notifier_block *nb)
38 down(&cpucontrol);
39 notifier_chain_unregister(&cpu_chain, nb);
40 up(&cpucontrol);
42 EXPORT_SYMBOL(unregister_cpu_notifier);
44 #ifdef CONFIG_HOTPLUG_CPU
45 static inline void check_for_tasks(int cpu)
47 struct task_struct *p;
49 write_lock_irq(&tasklist_lock);
50 for_each_process(p) {
51 if (task_cpu(p) == cpu &&
52 (!cputime_eq(p->utime, cputime_zero) ||
53 !cputime_eq(p->stime, cputime_zero)))
54 printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\
55 (state = %ld, flags = %lx) \n",
56 p->comm, p->pid, cpu, p->state, p->flags);
58 write_unlock_irq(&tasklist_lock);
61 /* Take this CPU down. */
62 static int take_cpu_down(void *unused)
64 int err;
66 /* Ensure this CPU doesn't handle any more interrupts. */
67 err = __cpu_disable();
68 if (err < 0)
69 return err;
71 /* Force idle task to run as soon as we yield: it should
72 immediately notice cpu is offline and die quickly. */
73 sched_idle_next();
74 return 0;
77 int cpu_down(unsigned int cpu)
79 int err;
80 struct task_struct *p;
81 cpumask_t old_allowed, tmp;
83 if ((err = lock_cpu_hotplug_interruptible()) != 0)
84 return err;
86 if (num_online_cpus() == 1) {
87 err = -EBUSY;
88 goto out;
91 if (!cpu_online(cpu)) {
92 err = -EINVAL;
93 goto out;
96 err = notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE,
97 (void *)(long)cpu);
98 if (err == NOTIFY_BAD) {
99 printk("%s: attempt to take down CPU %u failed\n",
100 __FUNCTION__, cpu);
101 err = -EINVAL;
102 goto out;
105 /* Ensure that we are not runnable on dying cpu */
106 old_allowed = current->cpus_allowed;
107 tmp = CPU_MASK_ALL;
108 cpu_clear(cpu, tmp);
109 set_cpus_allowed(current, tmp);
111 p = __stop_machine_run(take_cpu_down, NULL, cpu);
112 if (IS_ERR(p)) {
113 /* CPU didn't die: tell everyone. Can't complain. */
114 if (notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED,
115 (void *)(long)cpu) == NOTIFY_BAD)
116 BUG();
118 err = PTR_ERR(p);
119 goto out_allowed;
122 if (cpu_online(cpu))
123 goto out_thread;
125 /* Wait for it to sleep (leaving idle task). */
126 while (!idle_cpu(cpu))
127 yield();
129 /* This actually kills the CPU. */
130 __cpu_die(cpu);
132 /* Move it here so it can run. */
133 kthread_bind(p, get_cpu());
134 put_cpu();
136 /* CPU is completely dead: tell everyone. Too late to complain. */
137 if (notifier_call_chain(&cpu_chain, CPU_DEAD, (void *)(long)cpu)
138 == NOTIFY_BAD)
139 BUG();
141 check_for_tasks(cpu);
143 out_thread:
144 err = kthread_stop(p);
145 out_allowed:
146 set_cpus_allowed(current, old_allowed);
147 out:
148 unlock_cpu_hotplug();
149 return err;
151 #endif /*CONFIG_HOTPLUG_CPU*/
153 int __devinit cpu_up(unsigned int cpu)
155 int ret;
156 void *hcpu = (void *)(long)cpu;
158 if ((ret = down_interruptible(&cpucontrol)) != 0)
159 return ret;
161 if (cpu_online(cpu) || !cpu_present(cpu)) {
162 ret = -EINVAL;
163 goto out;
165 ret = notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu);
166 if (ret == NOTIFY_BAD) {
167 printk("%s: attempt to bring up CPU %u failed\n",
168 __FUNCTION__, cpu);
169 ret = -EINVAL;
170 goto out_notify;
173 /* Arch-specific enabling code. */
174 ret = __cpu_up(cpu);
175 if (ret != 0)
176 goto out_notify;
177 if (!cpu_online(cpu))
178 BUG();
180 /* Now call notifier in preparation. */
181 notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu);
183 out_notify:
184 if (ret != 0)
185 notifier_call_chain(&cpu_chain, CPU_UP_CANCELED, hcpu);
186 out:
187 up(&cpucontrol);
188 return ret;