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