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