]>
Commit | Line | Data |
---|---|---|
3d442233 JA |
1 | /* |
2 | * Generic helpers for smp ipi calls | |
3 | * | |
4 | * (C) Jens Axboe <[email protected]> 2008 | |
3d442233 | 5 | */ |
3d442233 | 6 | #include <linux/rcupdate.h> |
59190f42 | 7 | #include <linux/rculist.h> |
641cd4cf | 8 | #include <linux/kernel.h> |
9984de1a | 9 | #include <linux/export.h> |
0b13fda1 IM |
10 | #include <linux/percpu.h> |
11 | #include <linux/init.h> | |
5a0e3ad6 | 12 | #include <linux/gfp.h> |
3d442233 | 13 | #include <linux/smp.h> |
8969a5ed | 14 | #include <linux/cpu.h> |
3d442233 | 15 | |
3bb5d2ee SS |
16 | #include "smpboot.h" |
17 | ||
3d442233 | 18 | enum { |
6e275637 | 19 | CSD_FLAG_LOCK = 0x01, |
c84a83e2 | 20 | CSD_FLAG_WAIT = 0x02, |
3d442233 JA |
21 | }; |
22 | ||
23 | struct call_function_data { | |
9a46ad6d | 24 | struct call_single_data __percpu *csd; |
0b13fda1 | 25 | cpumask_var_t cpumask; |
3d442233 JA |
26 | }; |
27 | ||
e03bcb68 MM |
28 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data); |
29 | ||
6897fc22 | 30 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue); |
8969a5ed PZ |
31 | |
32 | static int | |
33 | hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu) | |
34 | { | |
35 | long cpu = (long)hcpu; | |
36 | struct call_function_data *cfd = &per_cpu(cfd_data, cpu); | |
37 | ||
38 | switch (action) { | |
39 | case CPU_UP_PREPARE: | |
40 | case CPU_UP_PREPARE_FROZEN: | |
eaa95840 | 41 | if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL, |
8969a5ed | 42 | cpu_to_node(cpu))) |
80b5184c | 43 | return notifier_from_errno(-ENOMEM); |
9a46ad6d SL |
44 | cfd->csd = alloc_percpu(struct call_single_data); |
45 | if (!cfd->csd) { | |
46 | free_cpumask_var(cfd->cpumask); | |
47 | return notifier_from_errno(-ENOMEM); | |
48 | } | |
8969a5ed PZ |
49 | break; |
50 | ||
69dd647f | 51 | #ifdef CONFIG_HOTPLUG_CPU |
8969a5ed PZ |
52 | case CPU_UP_CANCELED: |
53 | case CPU_UP_CANCELED_FROZEN: | |
54 | ||
55 | case CPU_DEAD: | |
56 | case CPU_DEAD_FROZEN: | |
57 | free_cpumask_var(cfd->cpumask); | |
9a46ad6d | 58 | free_percpu(cfd->csd); |
8969a5ed PZ |
59 | break; |
60 | #endif | |
61 | }; | |
62 | ||
63 | return NOTIFY_OK; | |
64 | } | |
65 | ||
0db0628d | 66 | static struct notifier_block hotplug_cfd_notifier = { |
0b13fda1 | 67 | .notifier_call = hotplug_cfd, |
8969a5ed PZ |
68 | }; |
69 | ||
d8ad7d11 | 70 | void __init call_function_init(void) |
3d442233 | 71 | { |
8969a5ed | 72 | void *cpu = (void *)(long)smp_processor_id(); |
3d442233 JA |
73 | int i; |
74 | ||
6897fc22 CH |
75 | for_each_possible_cpu(i) |
76 | init_llist_head(&per_cpu(call_single_queue, i)); | |
8969a5ed PZ |
77 | |
78 | hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu); | |
79 | register_cpu_notifier(&hotplug_cfd_notifier); | |
3d442233 JA |
80 | } |
81 | ||
8969a5ed PZ |
82 | /* |
83 | * csd_lock/csd_unlock used to serialize access to per-cpu csd resources | |
84 | * | |
0b13fda1 IM |
85 | * For non-synchronous ipi calls the csd can still be in use by the |
86 | * previous function call. For multi-cpu calls its even more interesting | |
87 | * as we'll have to ensure no other cpu is observing our csd. | |
8969a5ed | 88 | */ |
e1d12f32 | 89 | static void csd_lock_wait(struct call_single_data *csd) |
8969a5ed | 90 | { |
e1d12f32 | 91 | while (csd->flags & CSD_FLAG_LOCK) |
8969a5ed | 92 | cpu_relax(); |
6e275637 PZ |
93 | } |
94 | ||
e1d12f32 | 95 | static void csd_lock(struct call_single_data *csd) |
6e275637 | 96 | { |
e1d12f32 AM |
97 | csd_lock_wait(csd); |
98 | csd->flags |= CSD_FLAG_LOCK; | |
8969a5ed PZ |
99 | |
100 | /* | |
0b13fda1 IM |
101 | * prevent CPU from reordering the above assignment |
102 | * to ->flags with any subsequent assignments to other | |
103 | * fields of the specified call_single_data structure: | |
8969a5ed | 104 | */ |
8969a5ed PZ |
105 | smp_mb(); |
106 | } | |
107 | ||
e1d12f32 | 108 | static void csd_unlock(struct call_single_data *csd) |
8969a5ed | 109 | { |
c84a83e2 | 110 | WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK)); |
0b13fda1 | 111 | |
8969a5ed | 112 | /* |
0b13fda1 | 113 | * ensure we're all done before releasing data: |
8969a5ed PZ |
114 | */ |
115 | smp_mb(); | |
0b13fda1 | 116 | |
e1d12f32 | 117 | csd->flags &= ~CSD_FLAG_LOCK; |
3d442233 JA |
118 | } |
119 | ||
120 | /* | |
0b13fda1 IM |
121 | * Insert a previously allocated call_single_data element |
122 | * for execution on the given CPU. data must already have | |
123 | * ->func, ->info, and ->flags set. | |
3d442233 | 124 | */ |
ca5ecd64 | 125 | static void generic_exec_single(int cpu, struct call_single_data *csd, int wait) |
3d442233 | 126 | { |
c84a83e2 JA |
127 | if (wait) |
128 | csd->flags |= CSD_FLAG_WAIT; | |
129 | ||
561920a0 | 130 | /* |
15d0d3b3 NP |
131 | * The list addition should be visible before sending the IPI |
132 | * handler locks the list to pull the entry off it because of | |
133 | * normal cache coherency rules implied by spinlocks. | |
134 | * | |
135 | * If IPIs can go out of order to the cache coherency protocol | |
136 | * in an architecture, sufficient synchronisation should be added | |
137 | * to arch code to make it appear to obey cache coherency WRT | |
0b13fda1 IM |
138 | * locking and barrier primitives. Generic code isn't really |
139 | * equipped to do the right thing... | |
561920a0 | 140 | */ |
6897fc22 | 141 | if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu))) |
3d442233 JA |
142 | arch_send_call_function_single_ipi(cpu); |
143 | ||
144 | if (wait) | |
e1d12f32 | 145 | csd_lock_wait(csd); |
3d442233 JA |
146 | } |
147 | ||
3d442233 | 148 | /* |
0b13fda1 IM |
149 | * Invoked by arch to handle an IPI for call function single. Must be |
150 | * called from the arch with interrupts disabled. | |
3d442233 JA |
151 | */ |
152 | void generic_smp_call_function_single_interrupt(void) | |
153 | { | |
6897fc22 | 154 | struct llist_node *entry, *next; |
3d442233 | 155 | |
269c861b SS |
156 | /* |
157 | * Shouldn't receive this interrupt on a cpu that is not yet online. | |
158 | */ | |
159 | WARN_ON_ONCE(!cpu_online(smp_processor_id())); | |
160 | ||
6897fc22 CH |
161 | entry = llist_del_all(&__get_cpu_var(call_single_queue)); |
162 | entry = llist_reverse_order(entry); | |
3d442233 | 163 | |
6897fc22 | 164 | while (entry) { |
e1d12f32 | 165 | struct call_single_data *csd; |
3d442233 | 166 | |
6897fc22 | 167 | next = entry->next; |
3d442233 | 168 | |
6897fc22 | 169 | csd = llist_entry(entry, struct call_single_data, llist); |
e1d12f32 | 170 | csd->func(csd->info); |
46591962 | 171 | csd_unlock(csd); |
6897fc22 CH |
172 | |
173 | entry = next; | |
3d442233 JA |
174 | } |
175 | } | |
176 | ||
e03bcb68 | 177 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); |
d7240b98 | 178 | |
3d442233 JA |
179 | /* |
180 | * smp_call_function_single - Run a function on a specific CPU | |
181 | * @func: The function to run. This must be fast and non-blocking. | |
182 | * @info: An arbitrary pointer to pass to the function. | |
3d442233 JA |
183 | * @wait: If true, wait until function has completed on other CPUs. |
184 | * | |
72f279b2 | 185 | * Returns 0 on success, else a negative status code. |
3d442233 | 186 | */ |
3a5f65df | 187 | int smp_call_function_single(int cpu, smp_call_func_t func, void *info, |
8691e5a8 | 188 | int wait) |
3d442233 | 189 | { |
8969a5ed PZ |
190 | struct call_single_data d = { |
191 | .flags = 0, | |
192 | }; | |
3d442233 | 193 | unsigned long flags; |
0b13fda1 | 194 | int this_cpu; |
f73be6de | 195 | int err = 0; |
3d442233 | 196 | |
0b13fda1 IM |
197 | /* |
198 | * prevent preemption and reschedule on another processor, | |
199 | * as well as CPU removal | |
200 | */ | |
201 | this_cpu = get_cpu(); | |
202 | ||
269c861b SS |
203 | /* |
204 | * Can deadlock when called with interrupts disabled. | |
205 | * We allow cpu's that are not yet online though, as no one else can | |
206 | * send smp call function interrupt to this cpu and as such deadlocks | |
207 | * can't happen. | |
208 | */ | |
209 | WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() | |
210 | && !oops_in_progress); | |
3d442233 | 211 | |
0b13fda1 | 212 | if (cpu == this_cpu) { |
3d442233 JA |
213 | local_irq_save(flags); |
214 | func(info); | |
215 | local_irq_restore(flags); | |
0b13fda1 IM |
216 | } else { |
217 | if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) { | |
e1d12f32 | 218 | struct call_single_data *csd = &d; |
3d442233 | 219 | |
0b13fda1 | 220 | if (!wait) |
e1d12f32 | 221 | csd = &__get_cpu_var(csd_data); |
6e275637 | 222 | |
e1d12f32 | 223 | csd_lock(csd); |
3d442233 | 224 | |
e1d12f32 AM |
225 | csd->func = func; |
226 | csd->info = info; | |
227 | generic_exec_single(cpu, csd, wait); | |
0b13fda1 IM |
228 | } else { |
229 | err = -ENXIO; /* CPU not online */ | |
230 | } | |
3d442233 JA |
231 | } |
232 | ||
233 | put_cpu(); | |
0b13fda1 | 234 | |
f73be6de | 235 | return err; |
3d442233 JA |
236 | } |
237 | EXPORT_SYMBOL(smp_call_function_single); | |
238 | ||
2ea6dec4 RR |
239 | /* |
240 | * smp_call_function_any - Run a function on any of the given cpus | |
241 | * @mask: The mask of cpus it can run on. | |
242 | * @func: The function to run. This must be fast and non-blocking. | |
243 | * @info: An arbitrary pointer to pass to the function. | |
244 | * @wait: If true, wait until function has completed. | |
245 | * | |
246 | * Returns 0 on success, else a negative status code (if no cpus were online). | |
2ea6dec4 RR |
247 | * |
248 | * Selection preference: | |
249 | * 1) current cpu if in @mask | |
250 | * 2) any cpu of current node if in @mask | |
251 | * 3) any other online cpu in @mask | |
252 | */ | |
253 | int smp_call_function_any(const struct cpumask *mask, | |
3a5f65df | 254 | smp_call_func_t func, void *info, int wait) |
2ea6dec4 RR |
255 | { |
256 | unsigned int cpu; | |
257 | const struct cpumask *nodemask; | |
258 | int ret; | |
259 | ||
260 | /* Try for same CPU (cheapest) */ | |
261 | cpu = get_cpu(); | |
262 | if (cpumask_test_cpu(cpu, mask)) | |
263 | goto call; | |
264 | ||
265 | /* Try for same node. */ | |
af2422c4 | 266 | nodemask = cpumask_of_node(cpu_to_node(cpu)); |
2ea6dec4 RR |
267 | for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids; |
268 | cpu = cpumask_next_and(cpu, nodemask, mask)) { | |
269 | if (cpu_online(cpu)) | |
270 | goto call; | |
271 | } | |
272 | ||
273 | /* Any online will do: smp_call_function_single handles nr_cpu_ids. */ | |
274 | cpu = cpumask_any_and(mask, cpu_online_mask); | |
275 | call: | |
276 | ret = smp_call_function_single(cpu, func, info, wait); | |
277 | put_cpu(); | |
278 | return ret; | |
279 | } | |
280 | EXPORT_SYMBOL_GPL(smp_call_function_any); | |
281 | ||
3d442233 | 282 | /** |
27c379f7 | 283 | * __smp_call_function_single(): Run a function on a specific CPU |
3d442233 JA |
284 | * @cpu: The CPU to run on. |
285 | * @data: Pre-allocated and setup data structure | |
27c379f7 | 286 | * @wait: If true, wait until function has completed on specified CPU. |
3d442233 | 287 | * |
0b13fda1 IM |
288 | * Like smp_call_function_single(), but allow caller to pass in a |
289 | * pre-allocated data structure. Useful for embedding @data inside | |
290 | * other structures, for instance. | |
3d442233 | 291 | */ |
e1d12f32 | 292 | void __smp_call_function_single(int cpu, struct call_single_data *csd, |
6e275637 | 293 | int wait) |
3d442233 | 294 | { |
27c379f7 HC |
295 | unsigned int this_cpu; |
296 | unsigned long flags; | |
6e275637 | 297 | |
27c379f7 | 298 | this_cpu = get_cpu(); |
269c861b SS |
299 | /* |
300 | * Can deadlock when called with interrupts disabled. | |
301 | * We allow cpu's that are not yet online though, as no one else can | |
302 | * send smp call function interrupt to this cpu and as such deadlocks | |
303 | * can't happen. | |
304 | */ | |
305 | WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled() | |
306 | && !oops_in_progress); | |
3d442233 | 307 | |
27c379f7 HC |
308 | if (cpu == this_cpu) { |
309 | local_irq_save(flags); | |
e1d12f32 | 310 | csd->func(csd->info); |
27c379f7 HC |
311 | local_irq_restore(flags); |
312 | } else { | |
e1d12f32 AM |
313 | csd_lock(csd); |
314 | generic_exec_single(cpu, csd, wait); | |
27c379f7 HC |
315 | } |
316 | put_cpu(); | |
3d442233 | 317 | } |
e3daab6c | 318 | EXPORT_SYMBOL_GPL(__smp_call_function_single); |
3d442233 JA |
319 | |
320 | /** | |
54b11e6d RR |
321 | * smp_call_function_many(): Run a function on a set of other CPUs. |
322 | * @mask: The set of cpus to run on (only runs on online subset). | |
3d442233 JA |
323 | * @func: The function to run. This must be fast and non-blocking. |
324 | * @info: An arbitrary pointer to pass to the function. | |
0b13fda1 IM |
325 | * @wait: If true, wait (atomically) until function has completed |
326 | * on other CPUs. | |
3d442233 | 327 | * |
72f279b2 | 328 | * If @wait is true, then returns once @func has returned. |
3d442233 JA |
329 | * |
330 | * You must not call this function with disabled interrupts or from a | |
331 | * hardware interrupt handler or from a bottom half handler. Preemption | |
332 | * must be disabled when calling this function. | |
333 | */ | |
54b11e6d | 334 | void smp_call_function_many(const struct cpumask *mask, |
3a5f65df | 335 | smp_call_func_t func, void *info, bool wait) |
3d442233 | 336 | { |
e1d12f32 | 337 | struct call_function_data *cfd; |
9a46ad6d | 338 | int cpu, next_cpu, this_cpu = smp_processor_id(); |
3d442233 | 339 | |
269c861b SS |
340 | /* |
341 | * Can deadlock when called with interrupts disabled. | |
342 | * We allow cpu's that are not yet online though, as no one else can | |
343 | * send smp call function interrupt to this cpu and as such deadlocks | |
344 | * can't happen. | |
345 | */ | |
346 | WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() | |
bd924e8c | 347 | && !oops_in_progress && !early_boot_irqs_disabled); |
3d442233 | 348 | |
723aae25 | 349 | /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */ |
54b11e6d | 350 | cpu = cpumask_first_and(mask, cpu_online_mask); |
0b13fda1 | 351 | if (cpu == this_cpu) |
54b11e6d | 352 | cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
0b13fda1 | 353 | |
54b11e6d RR |
354 | /* No online cpus? We're done. */ |
355 | if (cpu >= nr_cpu_ids) | |
356 | return; | |
357 | ||
358 | /* Do we have another CPU which isn't us? */ | |
359 | next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask); | |
0b13fda1 | 360 | if (next_cpu == this_cpu) |
54b11e6d RR |
361 | next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask); |
362 | ||
363 | /* Fastpath: do that cpu by itself. */ | |
364 | if (next_cpu >= nr_cpu_ids) { | |
365 | smp_call_function_single(cpu, func, info, wait); | |
366 | return; | |
3d442233 JA |
367 | } |
368 | ||
e1d12f32 | 369 | cfd = &__get_cpu_var(cfd_data); |
45a57919 | 370 | |
e1d12f32 AM |
371 | cpumask_and(cfd->cpumask, mask, cpu_online_mask); |
372 | cpumask_clear_cpu(this_cpu, cfd->cpumask); | |
723aae25 MM |
373 | |
374 | /* Some callers race with other cpus changing the passed mask */ | |
e1d12f32 | 375 | if (unlikely(!cpumask_weight(cfd->cpumask))) |
723aae25 | 376 | return; |
3d442233 | 377 | |
e1d12f32 AM |
378 | for_each_cpu(cpu, cfd->cpumask) { |
379 | struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu); | |
9a46ad6d SL |
380 | |
381 | csd_lock(csd); | |
382 | csd->func = func; | |
383 | csd->info = info; | |
6897fc22 | 384 | llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)); |
9a46ad6d | 385 | } |
561920a0 | 386 | |
3d442233 | 387 | /* Send a message to all CPUs in the map */ |
73f94550 | 388 | arch_send_call_function_ipi_mask(cfd->cpumask); |
3d442233 | 389 | |
9a46ad6d | 390 | if (wait) { |
e1d12f32 AM |
391 | for_each_cpu(cpu, cfd->cpumask) { |
392 | struct call_single_data *csd; | |
393 | ||
394 | csd = per_cpu_ptr(cfd->csd, cpu); | |
9a46ad6d SL |
395 | csd_lock_wait(csd); |
396 | } | |
397 | } | |
3d442233 | 398 | } |
54b11e6d | 399 | EXPORT_SYMBOL(smp_call_function_many); |
3d442233 JA |
400 | |
401 | /** | |
402 | * smp_call_function(): Run a function on all other CPUs. | |
403 | * @func: The function to run. This must be fast and non-blocking. | |
404 | * @info: An arbitrary pointer to pass to the function. | |
0b13fda1 IM |
405 | * @wait: If true, wait (atomically) until function has completed |
406 | * on other CPUs. | |
3d442233 | 407 | * |
54b11e6d | 408 | * Returns 0. |
3d442233 JA |
409 | * |
410 | * If @wait is true, then returns once @func has returned; otherwise | |
72f279b2 | 411 | * it returns just before the target cpu calls @func. |
3d442233 JA |
412 | * |
413 | * You must not call this function with disabled interrupts or from a | |
414 | * hardware interrupt handler or from a bottom half handler. | |
415 | */ | |
3a5f65df | 416 | int smp_call_function(smp_call_func_t func, void *info, int wait) |
3d442233 | 417 | { |
3d442233 | 418 | preempt_disable(); |
54b11e6d | 419 | smp_call_function_many(cpu_online_mask, func, info, wait); |
3d442233 | 420 | preempt_enable(); |
0b13fda1 | 421 | |
54b11e6d | 422 | return 0; |
3d442233 JA |
423 | } |
424 | EXPORT_SYMBOL(smp_call_function); | |
351f8f8e | 425 | |
34db18a0 AW |
426 | /* Setup configured maximum number of CPUs to activate */ |
427 | unsigned int setup_max_cpus = NR_CPUS; | |
428 | EXPORT_SYMBOL(setup_max_cpus); | |
429 | ||
430 | ||
431 | /* | |
432 | * Setup routine for controlling SMP activation | |
433 | * | |
434 | * Command-line option of "nosmp" or "maxcpus=0" will disable SMP | |
435 | * activation entirely (the MPS table probe still happens, though). | |
436 | * | |
437 | * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer | |
438 | * greater than 0, limits the maximum number of CPUs activated in | |
439 | * SMP mode to <NUM>. | |
440 | */ | |
441 | ||
442 | void __weak arch_disable_smp_support(void) { } | |
443 | ||
444 | static int __init nosmp(char *str) | |
445 | { | |
446 | setup_max_cpus = 0; | |
447 | arch_disable_smp_support(); | |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
452 | early_param("nosmp", nosmp); | |
453 | ||
454 | /* this is hard limit */ | |
455 | static int __init nrcpus(char *str) | |
456 | { | |
457 | int nr_cpus; | |
458 | ||
459 | get_option(&str, &nr_cpus); | |
460 | if (nr_cpus > 0 && nr_cpus < nr_cpu_ids) | |
461 | nr_cpu_ids = nr_cpus; | |
462 | ||
463 | return 0; | |
464 | } | |
465 | ||
466 | early_param("nr_cpus", nrcpus); | |
467 | ||
468 | static int __init maxcpus(char *str) | |
469 | { | |
470 | get_option(&str, &setup_max_cpus); | |
471 | if (setup_max_cpus == 0) | |
472 | arch_disable_smp_support(); | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
477 | early_param("maxcpus", maxcpus); | |
478 | ||
479 | /* Setup number of possible processor ids */ | |
480 | int nr_cpu_ids __read_mostly = NR_CPUS; | |
481 | EXPORT_SYMBOL(nr_cpu_ids); | |
482 | ||
483 | /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */ | |
484 | void __init setup_nr_cpu_ids(void) | |
485 | { | |
486 | nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1; | |
487 | } | |
488 | ||
a17bce4d BP |
489 | void __weak smp_announce(void) |
490 | { | |
491 | printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus()); | |
492 | } | |
493 | ||
34db18a0 AW |
494 | /* Called by boot processor to activate the rest. */ |
495 | void __init smp_init(void) | |
496 | { | |
497 | unsigned int cpu; | |
498 | ||
3bb5d2ee SS |
499 | idle_threads_init(); |
500 | ||
34db18a0 AW |
501 | /* FIXME: This should be done in userspace --RR */ |
502 | for_each_present_cpu(cpu) { | |
503 | if (num_online_cpus() >= setup_max_cpus) | |
504 | break; | |
505 | if (!cpu_online(cpu)) | |
506 | cpu_up(cpu); | |
507 | } | |
508 | ||
509 | /* Any cleanup work */ | |
a17bce4d | 510 | smp_announce(); |
34db18a0 AW |
511 | smp_cpus_done(setup_max_cpus); |
512 | } | |
513 | ||
351f8f8e | 514 | /* |
bd924e8c TH |
515 | * Call a function on all processors. May be used during early boot while |
516 | * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead | |
517 | * of local_irq_disable/enable(). | |
351f8f8e AW |
518 | */ |
519 | int on_each_cpu(void (*func) (void *info), void *info, int wait) | |
520 | { | |
bd924e8c | 521 | unsigned long flags; |
351f8f8e AW |
522 | int ret = 0; |
523 | ||
524 | preempt_disable(); | |
525 | ret = smp_call_function(func, info, wait); | |
bd924e8c | 526 | local_irq_save(flags); |
351f8f8e | 527 | func(info); |
bd924e8c | 528 | local_irq_restore(flags); |
351f8f8e AW |
529 | preempt_enable(); |
530 | return ret; | |
531 | } | |
532 | EXPORT_SYMBOL(on_each_cpu); | |
3fc498f1 GBY |
533 | |
534 | /** | |
535 | * on_each_cpu_mask(): Run a function on processors specified by | |
536 | * cpumask, which may include the local processor. | |
537 | * @mask: The set of cpus to run on (only runs on online subset). | |
538 | * @func: The function to run. This must be fast and non-blocking. | |
539 | * @info: An arbitrary pointer to pass to the function. | |
540 | * @wait: If true, wait (atomically) until function has completed | |
541 | * on other CPUs. | |
542 | * | |
543 | * If @wait is true, then returns once @func has returned. | |
544 | * | |
202da400 DD |
545 | * You must not call this function with disabled interrupts or from a |
546 | * hardware interrupt handler or from a bottom half handler. The | |
547 | * exception is that it may be used during early boot while | |
548 | * early_boot_irqs_disabled is set. | |
3fc498f1 GBY |
549 | */ |
550 | void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func, | |
551 | void *info, bool wait) | |
552 | { | |
553 | int cpu = get_cpu(); | |
554 | ||
555 | smp_call_function_many(mask, func, info, wait); | |
556 | if (cpumask_test_cpu(cpu, mask)) { | |
202da400 DD |
557 | unsigned long flags; |
558 | local_irq_save(flags); | |
3fc498f1 | 559 | func(info); |
202da400 | 560 | local_irq_restore(flags); |
3fc498f1 GBY |
561 | } |
562 | put_cpu(); | |
563 | } | |
564 | EXPORT_SYMBOL(on_each_cpu_mask); | |
b3a7e98e GBY |
565 | |
566 | /* | |
567 | * on_each_cpu_cond(): Call a function on each processor for which | |
568 | * the supplied function cond_func returns true, optionally waiting | |
569 | * for all the required CPUs to finish. This may include the local | |
570 | * processor. | |
571 | * @cond_func: A callback function that is passed a cpu id and | |
572 | * the the info parameter. The function is called | |
573 | * with preemption disabled. The function should | |
574 | * return a blooean value indicating whether to IPI | |
575 | * the specified CPU. | |
576 | * @func: The function to run on all applicable CPUs. | |
577 | * This must be fast and non-blocking. | |
578 | * @info: An arbitrary pointer to pass to both functions. | |
579 | * @wait: If true, wait (atomically) until function has | |
580 | * completed on other CPUs. | |
581 | * @gfp_flags: GFP flags to use when allocating the cpumask | |
582 | * used internally by the function. | |
583 | * | |
584 | * The function might sleep if the GFP flags indicates a non | |
585 | * atomic allocation is allowed. | |
586 | * | |
587 | * Preemption is disabled to protect against CPUs going offline but not online. | |
588 | * CPUs going online during the call will not be seen or sent an IPI. | |
589 | * | |
590 | * You must not call this function with disabled interrupts or | |
591 | * from a hardware interrupt handler or from a bottom half handler. | |
592 | */ | |
593 | void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), | |
594 | smp_call_func_t func, void *info, bool wait, | |
595 | gfp_t gfp_flags) | |
596 | { | |
597 | cpumask_var_t cpus; | |
598 | int cpu, ret; | |
599 | ||
600 | might_sleep_if(gfp_flags & __GFP_WAIT); | |
601 | ||
602 | if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) { | |
603 | preempt_disable(); | |
604 | for_each_online_cpu(cpu) | |
605 | if (cond_func(cpu, info)) | |
606 | cpumask_set_cpu(cpu, cpus); | |
607 | on_each_cpu_mask(cpus, func, info, wait); | |
608 | preempt_enable(); | |
609 | free_cpumask_var(cpus); | |
610 | } else { | |
611 | /* | |
612 | * No free cpumask, bother. No matter, we'll | |
613 | * just have to IPI them one by one. | |
614 | */ | |
615 | preempt_disable(); | |
616 | for_each_online_cpu(cpu) | |
617 | if (cond_func(cpu, info)) { | |
618 | ret = smp_call_function_single(cpu, func, | |
619 | info, wait); | |
620 | WARN_ON_ONCE(!ret); | |
621 | } | |
622 | preempt_enable(); | |
623 | } | |
624 | } | |
625 | EXPORT_SYMBOL(on_each_cpu_cond); | |
f37f435f TG |
626 | |
627 | static void do_nothing(void *unused) | |
628 | { | |
629 | } | |
630 | ||
631 | /** | |
632 | * kick_all_cpus_sync - Force all cpus out of idle | |
633 | * | |
634 | * Used to synchronize the update of pm_idle function pointer. It's | |
635 | * called after the pointer is updated and returns after the dummy | |
636 | * callback function has been executed on all cpus. The execution of | |
637 | * the function can only happen on the remote cpus after they have | |
638 | * left the idle function which had been called via pm_idle function | |
639 | * pointer. So it's guaranteed that nothing uses the previous pointer | |
640 | * anymore. | |
641 | */ | |
642 | void kick_all_cpus_sync(void) | |
643 | { | |
644 | /* Make sure the change is visible before we kick the cpus */ | |
645 | smp_mb(); | |
646 | smp_call_function(do_nothing, NULL, 1); | |
647 | } | |
648 | EXPORT_SYMBOL_GPL(kick_all_cpus_sync); |