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