]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - kernel/smp.c
Merge remote-tracking branches 'spi/topic/adi-v3', 'spi/topic/atmel', 'spi/topic...
[mirror_ubuntu-artful-kernel.git] / kernel / smp.c
1 /*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
5 */
6 #include <linux/rcupdate.h>
7 #include <linux/rculist.h>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/percpu.h>
11 #include <linux/init.h>
12 #include <linux/gfp.h>
13 #include <linux/smp.h>
14 #include <linux/cpu.h>
15
16 #include "smpboot.h"
17
18 enum {
19 CSD_FLAG_LOCK = 0x01,
20 CSD_FLAG_WAIT = 0x02,
21 };
22
23 struct call_function_data {
24 struct call_single_data __percpu *csd;
25 cpumask_var_t cpumask;
26 };
27
28 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
29
30 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
31
32 static void flush_smp_call_function_queue(bool warn_cpu_offline);
33
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:
43 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
44 cpu_to_node(cpu)))
45 return notifier_from_errno(-ENOMEM);
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 }
51 break;
52
53 #ifdef CONFIG_HOTPLUG_CPU
54 case CPU_UP_CANCELED:
55 case CPU_UP_CANCELED_FROZEN:
56 /* Fall-through to the CPU_DEAD[_FROZEN] case. */
57
58 case CPU_DEAD:
59 case CPU_DEAD_FROZEN:
60 free_cpumask_var(cfd->cpumask);
61 free_percpu(cfd->csd);
62 break;
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;
77 #endif
78 };
79
80 return NOTIFY_OK;
81 }
82
83 static struct notifier_block hotplug_cfd_notifier = {
84 .notifier_call = hotplug_cfd,
85 };
86
87 void __init call_function_init(void)
88 {
89 void *cpu = (void *)(long)smp_processor_id();
90 int i;
91
92 for_each_possible_cpu(i)
93 init_llist_head(&per_cpu(call_single_queue, i));
94
95 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
96 register_cpu_notifier(&hotplug_cfd_notifier);
97 }
98
99 /*
100 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
101 *
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.
105 */
106 static void csd_lock_wait(struct call_single_data *csd)
107 {
108 while (csd->flags & CSD_FLAG_LOCK)
109 cpu_relax();
110 }
111
112 static void csd_lock(struct call_single_data *csd)
113 {
114 csd_lock_wait(csd);
115 csd->flags |= CSD_FLAG_LOCK;
116
117 /*
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:
121 */
122 smp_mb();
123 }
124
125 static void csd_unlock(struct call_single_data *csd)
126 {
127 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK));
128
129 /*
130 * ensure we're all done before releasing data:
131 */
132 smp_mb();
133
134 csd->flags &= ~CSD_FLAG_LOCK;
135 }
136
137 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
138
139 /*
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.
143 */
144 static int generic_exec_single(int cpu, struct call_single_data *csd,
145 smp_call_func_t func, void *info, int wait)
146 {
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
174 if (wait)
175 csd->flags |= CSD_FLAG_WAIT;
176
177 /*
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
185 * locking and barrier primitives. Generic code isn't really
186 * equipped to do the right thing...
187 */
188 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
189 arch_send_call_function_single_ipi(cpu);
190
191 if (wait)
192 csd_lock_wait(csd);
193
194 return 0;
195 }
196
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.
202 */
203 void generic_smp_call_function_single_interrupt(void)
204 {
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;
225 struct llist_node *entry;
226 struct call_single_data *csd, *csd_next;
227 static bool warned;
228
229 WARN_ON(!irqs_disabled());
230
231 head = &__get_cpu_var(call_single_queue);
232 entry = llist_del_all(head);
233 entry = llist_reverse_order(entry);
234
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))) {
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 }
249
250 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
251 csd->func(csd->info);
252 csd_unlock(csd);
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.
260 * @wait: If true, wait until function has completed on other CPUs.
261 *
262 * Returns 0 on success, else a negative status code.
263 */
264 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
265 int wait)
266 {
267 int this_cpu;
268 int err;
269
270 /*
271 * prevent preemption and reschedule on another processor,
272 * as well as CPU removal
273 */
274 this_cpu = get_cpu();
275
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);
284
285 err = generic_exec_single(cpu, NULL, func, info, wait);
286
287 put_cpu();
288
289 return err;
290 }
291 EXPORT_SYMBOL(smp_call_function_single);
292
293 /**
294 * smp_call_function_single_async(): Run an asynchronous function on a
295 * specific CPU.
296 * @cpu: The CPU to run on.
297 * @csd: Pre-allocated and setup data structure
298 *
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.
308 */
309 int smp_call_function_single_async(int cpu, struct call_single_data *csd)
310 {
311 int err = 0;
312
313 preempt_disable();
314 err = generic_exec_single(cpu, csd, csd->func, csd->info, 0);
315 preempt_enable();
316
317 return err;
318 }
319 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
320
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).
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,
336 smp_call_func_t func, void *info, int wait)
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. */
348 nodemask = cpumask_of_node(cpu_to_node(cpu));
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
364 /**
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).
367 * @func: The function to run. This must be fast and non-blocking.
368 * @info: An arbitrary pointer to pass to the function.
369 * @wait: If true, wait (atomically) until function has completed
370 * on other CPUs.
371 *
372 * If @wait is true, then returns once @func has returned.
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 */
378 void smp_call_function_many(const struct cpumask *mask,
379 smp_call_func_t func, void *info, bool wait)
380 {
381 struct call_function_data *cfd;
382 int cpu, next_cpu, this_cpu = smp_processor_id();
383
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()
391 && !oops_in_progress && !early_boot_irqs_disabled);
392
393 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
394 cpu = cpumask_first_and(mask, cpu_online_mask);
395 if (cpu == this_cpu)
396 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
397
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);
404 if (next_cpu == this_cpu)
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;
411 }
412
413 cfd = &__get_cpu_var(cfd_data);
414
415 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
416 cpumask_clear_cpu(this_cpu, cfd->cpumask);
417
418 /* Some callers race with other cpus changing the passed mask */
419 if (unlikely(!cpumask_weight(cfd->cpumask)))
420 return;
421
422 for_each_cpu(cpu, cfd->cpumask) {
423 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
424
425 csd_lock(csd);
426 csd->func = func;
427 csd->info = info;
428 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
429 }
430
431 /* Send a message to all CPUs in the map */
432 arch_send_call_function_ipi_mask(cfd->cpumask);
433
434 if (wait) {
435 for_each_cpu(cpu, cfd->cpumask) {
436 struct call_single_data *csd;
437
438 csd = per_cpu_ptr(cfd->csd, cpu);
439 csd_lock_wait(csd);
440 }
441 }
442 }
443 EXPORT_SYMBOL(smp_call_function_many);
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.
449 * @wait: If true, wait (atomically) until function has completed
450 * on other CPUs.
451 *
452 * Returns 0.
453 *
454 * If @wait is true, then returns once @func has returned; otherwise
455 * it returns just before the target cpu calls @func.
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 */
460 int smp_call_function(smp_call_func_t func, void *info, int wait)
461 {
462 preempt_disable();
463 smp_call_function_many(cpu_online_mask, func, info, wait);
464 preempt_enable();
465
466 return 0;
467 }
468 EXPORT_SYMBOL(smp_call_function);
469
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
533 void __weak smp_announce(void)
534 {
535 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
536 }
537
538 /* Called by boot processor to activate the rest. */
539 void __init smp_init(void)
540 {
541 unsigned int cpu;
542
543 idle_threads_init();
544
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 */
554 smp_announce();
555 smp_cpus_done(setup_max_cpus);
556 }
557
558 /*
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().
562 */
563 int on_each_cpu(void (*func) (void *info), void *info, int wait)
564 {
565 unsigned long flags;
566 int ret = 0;
567
568 preempt_disable();
569 ret = smp_call_function(func, info, wait);
570 local_irq_save(flags);
571 func(info);
572 local_irq_restore(flags);
573 preempt_enable();
574 return ret;
575 }
576 EXPORT_SYMBOL(on_each_cpu);
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 *
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.
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)) {
601 unsigned long flags;
602 local_irq_save(flags);
603 func(info);
604 local_irq_restore(flags);
605 }
606 put_cpu();
607 }
608 EXPORT_SYMBOL(on_each_cpu_mask);
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);
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);