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