serial/arc: Fix warning with CONSOLE_POLL
[linux-stable.git] / kernel / smp.c
blob80c33f8de14ffbdb83aaf6be0bc5c31c5d3e6351
1 /*
2 * Generic helpers for smp ipi calls
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>
16 #include "smpboot.h"
18 enum {
19 CSD_FLAG_LOCK = 0x01,
20 CSD_FLAG_WAIT = 0x02,
23 struct call_function_data {
24 struct call_single_data __percpu *csd;
25 cpumask_var_t cpumask;
28 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
30 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
32 static void flush_smp_call_function_queue(bool warn_cpu_offline);
34 static int
35 hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
37 long cpu = (long)hcpu;
38 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
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);
51 break;
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. */
58 case CPU_DEAD:
59 case CPU_DEAD_FROZEN:
60 free_cpumask_var(cfd->cpumask);
61 free_percpu(cfd->csd);
62 break;
64 case CPU_DYING:
65 case CPU_DYING_FROZEN:
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.
75 flush_smp_call_function_queue(false);
76 break;
77 #endif
80 return NOTIFY_OK;
83 static struct notifier_block hotplug_cfd_notifier = {
84 .notifier_call = hotplug_cfd,
87 void __init call_function_init(void)
89 void *cpu = (void *)(long)smp_processor_id();
90 int i;
92 for_each_possible_cpu(i)
93 init_llist_head(&per_cpu(call_single_queue, i));
95 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
96 register_cpu_notifier(&hotplug_cfd_notifier);
100 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
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.
106 static void csd_lock_wait(struct call_single_data *csd)
108 while (csd->flags & CSD_FLAG_LOCK)
109 cpu_relax();
112 static void csd_lock(struct call_single_data *csd)
114 csd_lock_wait(csd);
115 csd->flags |= CSD_FLAG_LOCK;
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:
122 smp_mb();
125 static void csd_unlock(struct call_single_data *csd)
127 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK));
130 * ensure we're all done before releasing data:
132 smp_mb();
134 csd->flags &= ~CSD_FLAG_LOCK;
137 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
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.
144 static int generic_exec_single(int cpu, struct call_single_data *csd,
145 smp_call_func_t func, void *info, int wait)
147 struct call_single_data csd_stack = { .flags = 0 };
148 unsigned long flags;
151 if (cpu == smp_processor_id()) {
152 local_irq_save(flags);
153 func(info);
154 local_irq_restore(flags);
155 return 0;
159 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu))
160 return -ENXIO;
163 if (!csd) {
164 csd = &csd_stack;
165 if (!wait)
166 csd = &__get_cpu_var(csd_data);
169 csd_lock(csd);
171 csd->func = func;
172 csd->info = info;
174 if (wait)
175 csd->flags |= CSD_FLAG_WAIT;
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.
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...
188 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
189 arch_send_call_function_single_ipi(cpu);
191 if (wait)
192 csd_lock_wait(csd);
194 return 0;
198 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
200 * Invoked by arch to handle an IPI for call function single.
201 * Must be called with interrupts disabled.
203 void generic_smp_call_function_single_interrupt(void)
205 flush_smp_call_function_queue(true);
209 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
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'.
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.
219 * Loop through the call_single_queue and run all the queued callbacks.
220 * Must be called with interrupts disabled.
222 static void flush_smp_call_function_queue(bool warn_cpu_offline)
224 struct llist_head *head;
225 struct llist_node *entry;
226 struct call_single_data *csd, *csd_next;
227 static bool warned;
229 WARN_ON(!irqs_disabled());
231 head = &__get_cpu_var(call_single_queue);
232 entry = llist_del_all(head);
233 entry = llist_reverse_order(entry);
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());
242 * We don't have to use the _safe() variant here
243 * because we are not invoking the IPI handlers yet.
245 llist_for_each_entry(csd, entry, llist)
246 pr_warn("IPI callback %pS sent to offline CPU\n",
247 csd->func);
250 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
251 csd->func(csd->info);
252 csd_unlock(csd);
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.
262 * Returns 0 on success, else a negative status code.
264 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
265 int wait)
267 int this_cpu;
268 int err;
271 * prevent preemption and reschedule on another processor,
272 * as well as CPU removal
274 this_cpu = get_cpu();
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.
282 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
283 && !oops_in_progress);
285 err = generic_exec_single(cpu, NULL, func, info, wait);
287 put_cpu();
289 return err;
291 EXPORT_SYMBOL(smp_call_function_single);
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
299 * Like smp_call_function_single(), but the call is asynchonous and
300 * can thus be done from contexts with disabled interrupts.
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.
306 * NOTE: Be careful, there is unfortunately no current debugging facility to
307 * validate the correctness of this serialization.
309 int smp_call_function_single_async(int cpu, struct call_single_data *csd)
311 int err = 0;
313 preempt_disable();
314 err = generic_exec_single(cpu, csd, csd->func, csd->info, 0);
315 preempt_enable();
317 return err;
319 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
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.
328 * Returns 0 on success, else a negative status code (if no cpus were online).
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
335 int smp_call_function_any(const struct cpumask *mask,
336 smp_call_func_t func, void *info, int wait)
338 unsigned int cpu;
339 const struct cpumask *nodemask;
340 int ret;
342 /* Try for same CPU (cheapest) */
343 cpu = get_cpu();
344 if (cpumask_test_cpu(cpu, mask))
345 goto call;
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;
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;
362 EXPORT_SYMBOL_GPL(smp_call_function_any);
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.
372 * If @wait is true, then returns once @func has returned.
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.
378 void smp_call_function_many(const struct cpumask *mask,
379 smp_call_func_t func, void *info, bool wait)
381 struct call_function_data *cfd;
382 int cpu, next_cpu, this_cpu = smp_processor_id();
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.
390 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
391 && !oops_in_progress && !early_boot_irqs_disabled);
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);
398 /* No online cpus? We're done. */
399 if (cpu >= nr_cpu_ids)
400 return;
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);
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;
413 cfd = &__get_cpu_var(cfd_data);
415 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
416 cpumask_clear_cpu(this_cpu, cfd->cpumask);
418 /* Some callers race with other cpus changing the passed mask */
419 if (unlikely(!cpumask_weight(cfd->cpumask)))
420 return;
422 for_each_cpu(cpu, cfd->cpumask) {
423 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
425 csd_lock(csd);
426 csd->func = func;
427 csd->info = info;
428 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
431 /* Send a message to all CPUs in the map */
432 arch_send_call_function_ipi_mask(cfd->cpumask);
434 if (wait) {
435 for_each_cpu(cpu, cfd->cpumask) {
436 struct call_single_data *csd;
438 csd = per_cpu_ptr(cfd->csd, cpu);
439 csd_lock_wait(csd);
443 EXPORT_SYMBOL(smp_call_function_many);
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.
452 * Returns 0.
454 * If @wait is true, then returns once @func has returned; otherwise
455 * it returns just before the target cpu calls @func.
457 * You must not call this function with disabled interrupts or from a
458 * hardware interrupt handler or from a bottom half handler.
460 int smp_call_function(smp_call_func_t func, void *info, int wait)
462 preempt_disable();
463 smp_call_function_many(cpu_online_mask, func, info, wait);
464 preempt_enable();
466 return 0;
468 EXPORT_SYMBOL(smp_call_function);
470 /* Setup configured maximum number of CPUs to activate */
471 unsigned int setup_max_cpus = NR_CPUS;
472 EXPORT_SYMBOL(setup_max_cpus);
476 * Setup routine for controlling SMP activation
478 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
479 * activation entirely (the MPS table probe still happens, though).
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>.
486 void __weak arch_disable_smp_support(void) { }
488 static int __init nosmp(char *str)
490 setup_max_cpus = 0;
491 arch_disable_smp_support();
493 return 0;
496 early_param("nosmp", nosmp);
498 /* this is hard limit */
499 static int __init nrcpus(char *str)
501 int nr_cpus;
503 get_option(&str, &nr_cpus);
504 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
505 nr_cpu_ids = nr_cpus;
507 return 0;
510 early_param("nr_cpus", nrcpus);
512 static int __init maxcpus(char *str)
514 get_option(&str, &setup_max_cpus);
515 if (setup_max_cpus == 0)
516 arch_disable_smp_support();
518 return 0;
521 early_param("maxcpus", maxcpus);
523 /* Setup number of possible processor ids */
524 int nr_cpu_ids __read_mostly = NR_CPUS;
525 EXPORT_SYMBOL(nr_cpu_ids);
527 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
528 void __init setup_nr_cpu_ids(void)
530 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
533 void __weak smp_announce(void)
535 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
538 /* Called by boot processor to activate the rest. */
539 void __init smp_init(void)
541 unsigned int cpu;
543 idle_threads_init();
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);
553 /* Any cleanup work */
554 smp_announce();
555 smp_cpus_done(setup_max_cpus);
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().
563 int on_each_cpu(void (*func) (void *info), void *info, int wait)
565 unsigned long flags;
566 int ret = 0;
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;
576 EXPORT_SYMBOL(on_each_cpu);
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.
587 * If @wait is true, then returns once @func has returned.
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.
594 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
595 void *info, bool wait)
597 int cpu = get_cpu();
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);
606 put_cpu();
608 EXPORT_SYMBOL(on_each_cpu_mask);
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.
628 * The function might sleep if the GFP flags indicates a non
629 * atomic allocation is allowed.
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.
634 * You must not call this function with disabled interrupts or
635 * from a hardware interrupt handler or from a bottom half handler.
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)
641 cpumask_var_t cpus;
642 int cpu, ret;
644 might_sleep_if(gfp_flags & __GFP_WAIT);
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 {
656 * No free cpumask, bother. No matter, we'll
657 * just have to IPI them one by one.
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);
666 preempt_enable();
669 EXPORT_SYMBOL(on_each_cpu_cond);
671 static void do_nothing(void *unused)
676 * kick_all_cpus_sync - Force all cpus out of idle
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.
686 void kick_all_cpus_sync(void)
688 /* Make sure the change is visible before we kick the cpus */
689 smp_mb();
690 smp_call_function(do_nothing, NULL, 1);
692 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);