Blame view
kernel/cpu.c
7.05 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* CPU control. * (C) 2001, 2002, 2003, 2004 Rusty Russell * * This code is licenced under the GPL. */ #include <linux/proc_fs.h> #include <linux/smp.h> #include <linux/init.h> #include <linux/notifier.h> #include <linux/sched.h> #include <linux/unistd.h> #include <linux/cpu.h> #include <linux/module.h> #include <linux/kthread.h> #include <linux/stop_machine.h> |
81615b624 [PATCH] Convert k... |
16 |
#include <linux/mutex.h> |
1da177e4c Linux-2.6.12-rc2 |
17 18 |
/* This protects CPUs going up and down... */ |
aa9538777 cpu hotplug: simp... |
19 20 |
static DEFINE_MUTEX(cpu_add_remove_lock); static DEFINE_MUTEX(cpu_bitmask_lock); |
1da177e4c Linux-2.6.12-rc2 |
21 |
|
bd5349cfd [PATCH] Convert c... |
22 |
static __cpuinitdata RAW_NOTIFIER_HEAD(cpu_chain); |
1da177e4c Linux-2.6.12-rc2 |
23 |
|
e3920fb42 [PATCH] Disable C... |
24 25 26 27 |
/* If set, cpu_up and cpu_down will return -EBUSY and do nothing. * Should always be manipulated under cpu_add_remove_lock */ static int cpu_hotplug_disabled; |
a9d9baa1e [PATCH] clean up ... |
28 |
#ifdef CONFIG_HOTPLUG_CPU |
90d45d17f [PATCH] cpu hotpl... |
29 |
|
aa9538777 cpu hotplug: simp... |
30 31 32 |
/* Crappy recursive lock-takers in cpufreq! Complain loudly about idiots */ static struct task_struct *recursive; static int recursive_depth; |
90d45d17f [PATCH] cpu hotpl... |
33 |
|
a9d9baa1e [PATCH] clean up ... |
34 35 |
void lock_cpu_hotplug(void) { |
aa9538777 cpu hotplug: simp... |
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
struct task_struct *tsk = current; if (tsk == recursive) { static int warnings = 10; if (warnings) { printk(KERN_ERR "Lukewarm IQ detected in hotplug locking "); WARN_ON(1); warnings--; } recursive_depth++; return; } mutex_lock(&cpu_bitmask_lock); recursive = tsk; |
a9d9baa1e [PATCH] clean up ... |
51 52 |
} EXPORT_SYMBOL_GPL(lock_cpu_hotplug); |
90d45d17f [PATCH] cpu hotpl... |
53 |
|
a9d9baa1e [PATCH] clean up ... |
54 55 |
void unlock_cpu_hotplug(void) { |
aa9538777 cpu hotplug: simp... |
56 57 58 59 |
WARN_ON(recursive != current); if (recursive_depth) { recursive_depth--; return; |
a9d9baa1e [PATCH] clean up ... |
60 |
} |
aa9538777 cpu hotplug: simp... |
61 |
recursive = NULL; |
4b96b1a10 [PATCH] Fix the s... |
62 |
mutex_unlock(&cpu_bitmask_lock); |
a9d9baa1e [PATCH] clean up ... |
63 64 |
} EXPORT_SYMBOL_GPL(unlock_cpu_hotplug); |
a9d9baa1e [PATCH] clean up ... |
65 |
#endif /* CONFIG_HOTPLUG_CPU */ |
90d45d17f [PATCH] cpu hotpl... |
66 |
|
1da177e4c Linux-2.6.12-rc2 |
67 |
/* Need to know about CPUs going up/down? */ |
65edc68c3 [PATCH] cpu hotpl... |
68 |
int __cpuinit register_cpu_notifier(struct notifier_block *nb) |
1da177e4c Linux-2.6.12-rc2 |
69 |
{ |
bd5349cfd [PATCH] Convert c... |
70 71 72 73 74 |
int ret; mutex_lock(&cpu_add_remove_lock); ret = raw_notifier_chain_register(&cpu_chain, nb); mutex_unlock(&cpu_add_remove_lock); return ret; |
1da177e4c Linux-2.6.12-rc2 |
75 |
} |
65edc68c3 [PATCH] cpu hotpl... |
76 77 |
#ifdef CONFIG_HOTPLUG_CPU |
1da177e4c Linux-2.6.12-rc2 |
78 79 80 81 |
EXPORT_SYMBOL(register_cpu_notifier); void unregister_cpu_notifier(struct notifier_block *nb) { |
bd5349cfd [PATCH] Convert c... |
82 83 84 |
mutex_lock(&cpu_add_remove_lock); raw_notifier_chain_unregister(&cpu_chain, nb); mutex_unlock(&cpu_add_remove_lock); |
1da177e4c Linux-2.6.12-rc2 |
85 86 |
} EXPORT_SYMBOL(unregister_cpu_notifier); |
1da177e4c Linux-2.6.12-rc2 |
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
static inline void check_for_tasks(int cpu) { struct task_struct *p; write_lock_irq(&tasklist_lock); for_each_process(p) { if (task_cpu(p) == cpu && (!cputime_eq(p->utime, cputime_zero) || !cputime_eq(p->stime, cputime_zero))) printk(KERN_WARNING "Task %s (pid = %d) is on cpu %d\ (state = %ld, flags = %lx) ", p->comm, p->pid, cpu, p->state, p->flags); } write_unlock_irq(&tasklist_lock); } /* Take this CPU down. */ static int take_cpu_down(void *unused) { int err; |
1da177e4c Linux-2.6.12-rc2 |
108 109 110 |
/* Ensure this CPU doesn't handle any more interrupts. */ err = __cpu_disable(); if (err < 0) |
f37051364 [PATCH] i386 CPU ... |
111 |
return err; |
1da177e4c Linux-2.6.12-rc2 |
112 |
|
f37051364 [PATCH] i386 CPU ... |
113 114 115 116 |
/* Force idle task to run as soon as we yield: it should immediately notice cpu is offline and die quickly. */ sched_idle_next(); return 0; |
1da177e4c Linux-2.6.12-rc2 |
117 |
} |
e3920fb42 [PATCH] Disable C... |
118 119 |
/* Requires cpu_add_remove_lock to be held */ static int _cpu_down(unsigned int cpu) |
1da177e4c Linux-2.6.12-rc2 |
120 121 122 123 |
{ int err; struct task_struct *p; cpumask_t old_allowed, tmp; |
e3920fb42 [PATCH] Disable C... |
124 125 |
if (num_online_cpus() == 1) return -EBUSY; |
1da177e4c Linux-2.6.12-rc2 |
126 |
|
e3920fb42 [PATCH] Disable C... |
127 128 |
if (!cpu_online(cpu)) return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
129 |
|
bd5349cfd [PATCH] Convert c... |
130 |
err = raw_notifier_call_chain(&cpu_chain, CPU_DOWN_PREPARE, |
1da177e4c Linux-2.6.12-rc2 |
131 132 133 134 135 |
(void *)(long)cpu); if (err == NOTIFY_BAD) { printk("%s: attempt to take down CPU %u failed ", __FUNCTION__, cpu); |
e3920fb42 [PATCH] Disable C... |
136 |
return -EINVAL; |
1da177e4c Linux-2.6.12-rc2 |
137 138 139 140 141 142 143 |
} /* Ensure that we are not runnable on dying cpu */ old_allowed = current->cpus_allowed; tmp = CPU_MASK_ALL; cpu_clear(cpu, tmp); set_cpus_allowed(current, tmp); |
aa9538777 cpu hotplug: simp... |
144 |
mutex_lock(&cpu_bitmask_lock); |
1da177e4c Linux-2.6.12-rc2 |
145 |
p = __stop_machine_run(take_cpu_down, NULL, cpu); |
aa9538777 cpu hotplug: simp... |
146 |
mutex_unlock(&cpu_bitmask_lock); |
8fa1d7d3b [PATCH] cpu-hotpl... |
147 |
if (IS_ERR(p) || cpu_online(cpu)) { |
1da177e4c Linux-2.6.12-rc2 |
148 |
/* CPU didn't die: tell everyone. Can't complain. */ |
bd5349cfd [PATCH] Convert c... |
149 |
if (raw_notifier_call_chain(&cpu_chain, CPU_DOWN_FAILED, |
1da177e4c Linux-2.6.12-rc2 |
150 151 |
(void *)(long)cpu) == NOTIFY_BAD) BUG(); |
8fa1d7d3b [PATCH] cpu-hotpl... |
152 153 154 155 |
if (IS_ERR(p)) { err = PTR_ERR(p); goto out_allowed; } |
1da177e4c Linux-2.6.12-rc2 |
156 |
goto out_thread; |
8fa1d7d3b [PATCH] cpu-hotpl... |
157 |
} |
1da177e4c Linux-2.6.12-rc2 |
158 159 160 161 162 163 164 165 166 167 168 169 170 |
/* Wait for it to sleep (leaving idle task). */ while (!idle_cpu(cpu)) yield(); /* This actually kills the CPU. */ __cpu_die(cpu); /* Move it here so it can run. */ kthread_bind(p, get_cpu()); put_cpu(); /* CPU is completely dead: tell everyone. Too late to complain. */ |
bd5349cfd [PATCH] Convert c... |
171 |
if (raw_notifier_call_chain(&cpu_chain, CPU_DEAD, |
e041c6834 [PATCH] Notifier ... |
172 |
(void *)(long)cpu) == NOTIFY_BAD) |
1da177e4c Linux-2.6.12-rc2 |
173 174 175 176 177 178 179 180 |
BUG(); check_for_tasks(cpu); out_thread: err = kthread_stop(p); out_allowed: set_cpus_allowed(current, old_allowed); |
e3920fb42 [PATCH] Disable C... |
181 182 183 184 185 186 187 188 189 190 191 192 |
return err; } int cpu_down(unsigned int cpu) { int err = 0; mutex_lock(&cpu_add_remove_lock); if (cpu_hotplug_disabled) err = -EBUSY; else err = _cpu_down(cpu); |
aa9538777 cpu hotplug: simp... |
193 |
mutex_unlock(&cpu_add_remove_lock); |
1da177e4c Linux-2.6.12-rc2 |
194 195 196 |
return err; } #endif /*CONFIG_HOTPLUG_CPU*/ |
e3920fb42 [PATCH] Disable C... |
197 |
/* Requires cpu_add_remove_lock to be held */ |
b282b6f8a [PATCH] Change cp... |
198 |
static int __cpuinit _cpu_up(unsigned int cpu) |
1da177e4c Linux-2.6.12-rc2 |
199 200 201 |
{ int ret; void *hcpu = (void *)(long)cpu; |
e3920fb42 [PATCH] Disable C... |
202 203 |
if (cpu_online(cpu) || !cpu_present(cpu)) return -EINVAL; |
90d45d17f [PATCH] cpu hotpl... |
204 |
|
bd5349cfd [PATCH] Convert c... |
205 |
ret = raw_notifier_call_chain(&cpu_chain, CPU_UP_PREPARE, hcpu); |
1da177e4c Linux-2.6.12-rc2 |
206 207 208 209 210 211 212 213 214 |
if (ret == NOTIFY_BAD) { printk("%s: attempt to bring up CPU %u failed ", __FUNCTION__, cpu); ret = -EINVAL; goto out_notify; } /* Arch-specific enabling code. */ |
aa9538777 cpu hotplug: simp... |
215 |
mutex_lock(&cpu_bitmask_lock); |
1da177e4c Linux-2.6.12-rc2 |
216 |
ret = __cpu_up(cpu); |
aa9538777 cpu hotplug: simp... |
217 |
mutex_unlock(&cpu_bitmask_lock); |
1da177e4c Linux-2.6.12-rc2 |
218 219 |
if (ret != 0) goto out_notify; |
6978c7052 BUG_ON() Conversi... |
220 |
BUG_ON(!cpu_online(cpu)); |
1da177e4c Linux-2.6.12-rc2 |
221 222 |
/* Now call notifier in preparation. */ |
bd5349cfd [PATCH] Convert c... |
223 |
raw_notifier_call_chain(&cpu_chain, CPU_ONLINE, hcpu); |
1da177e4c Linux-2.6.12-rc2 |
224 225 226 |
out_notify: if (ret != 0) |
bd5349cfd [PATCH] Convert c... |
227 |
raw_notifier_call_chain(&cpu_chain, |
e041c6834 [PATCH] Notifier ... |
228 |
CPU_UP_CANCELED, hcpu); |
e3920fb42 [PATCH] Disable C... |
229 230 231 |
return ret; } |
b282b6f8a [PATCH] Change cp... |
232 |
int __cpuinit cpu_up(unsigned int cpu) |
e3920fb42 [PATCH] Disable C... |
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
{ int err = 0; mutex_lock(&cpu_add_remove_lock); if (cpu_hotplug_disabled) err = -EBUSY; else err = _cpu_up(cpu); mutex_unlock(&cpu_add_remove_lock); return err; } #ifdef CONFIG_SUSPEND_SMP static cpumask_t frozen_cpus; int disable_nonboot_cpus(void) { |
e1d9fd2e3 [PATCH] suspend: ... |
251 |
int cpu, first_cpu, error = 0; |
e3920fb42 [PATCH] Disable C... |
252 253 254 255 256 257 258 259 260 261 262 263 |
mutex_lock(&cpu_add_remove_lock); first_cpu = first_cpu(cpu_present_map); if (!cpu_online(first_cpu)) { error = _cpu_up(first_cpu); if (error) { printk(KERN_ERR "Could not bring CPU%d up. ", first_cpu); goto out; } } |
112cecb2c [PATCH] suspend: ... |
264 |
|
e3920fb42 [PATCH] Disable C... |
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
/* We take down all of the non-boot CPUs in one shot to avoid races * with the userspace trying to use the CPU hotplug at the same time */ cpus_clear(frozen_cpus); printk("Disabling non-boot CPUs ... "); for_each_online_cpu(cpu) { if (cpu == first_cpu) continue; error = _cpu_down(cpu); if (!error) { cpu_set(cpu, frozen_cpus); printk("CPU%d is down ", cpu); } else { printk(KERN_ERR "Error taking CPU%d down: %d ", cpu, error); break; } } if (!error) { BUG_ON(num_online_cpus() > 1); /* Make sure the CPUs won't be enabled by someone else */ cpu_hotplug_disabled = 1; } else { |
e1d9fd2e3 [PATCH] suspend: ... |
291 292 |
printk(KERN_ERR "Non-boot CPUs are not disabled "); |
e3920fb42 [PATCH] Disable C... |
293 |
} |
1da177e4c Linux-2.6.12-rc2 |
294 |
out: |
aa9538777 cpu hotplug: simp... |
295 |
mutex_unlock(&cpu_add_remove_lock); |
e3920fb42 [PATCH] Disable C... |
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
return error; } void enable_nonboot_cpus(void) { int cpu, error; /* Allow everyone to use the CPU hotplug again */ mutex_lock(&cpu_add_remove_lock); cpu_hotplug_disabled = 0; mutex_unlock(&cpu_add_remove_lock); printk("Enabling non-boot CPUs ... "); for_each_cpu_mask(cpu, frozen_cpus) { error = cpu_up(cpu); if (!error) { printk("CPU%d is up ", cpu); continue; } printk(KERN_WARNING "Error taking CPU%d up: %d ", cpu, error); } cpus_clear(frozen_cpus); |
1da177e4c Linux-2.6.12-rc2 |
322 |
} |
e3920fb42 [PATCH] Disable C... |
323 |
#endif |