2 * Generic helpers for smp ipi calls
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
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 #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
18 struct list_head queue
;
20 } call_function __cacheline_aligned_in_smp
=
22 .queue
= LIST_HEAD_INIT(call_function
.queue
),
23 .lock
= __RAW_SPIN_LOCK_UNLOCKED(call_function
.lock
),
30 struct call_function_data
{
31 struct call_single_data csd
;
33 cpumask_var_t cpumask
;
34 cpumask_var_t cpumask_ipi
;
37 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data
, cfd_data
);
39 struct call_single_queue
{
40 struct list_head list
;
44 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue
, call_single_queue
);
47 hotplug_cfd(struct notifier_block
*nfb
, unsigned long action
, void *hcpu
)
49 long cpu
= (long)hcpu
;
50 struct call_function_data
*cfd
= &per_cpu(cfd_data
, cpu
);
54 case CPU_UP_PREPARE_FROZEN
:
55 if (!zalloc_cpumask_var_node(&cfd
->cpumask
, GFP_KERNEL
,
57 return notifier_from_errno(-ENOMEM
);
58 if (!zalloc_cpumask_var_node(&cfd
->cpumask_ipi
, GFP_KERNEL
,
60 return notifier_from_errno(-ENOMEM
);
63 #ifdef CONFIG_HOTPLUG_CPU
65 case CPU_UP_CANCELED_FROZEN
:
69 free_cpumask_var(cfd
->cpumask
);
70 free_cpumask_var(cfd
->cpumask_ipi
);
78 static struct notifier_block __cpuinitdata hotplug_cfd_notifier
= {
79 .notifier_call
= hotplug_cfd
,
82 void __init
call_function_init(void)
84 void *cpu
= (void *)(long)smp_processor_id();
87 for_each_possible_cpu(i
) {
88 struct call_single_queue
*q
= &per_cpu(call_single_queue
, i
);
90 raw_spin_lock_init(&q
->lock
);
91 INIT_LIST_HEAD(&q
->list
);
94 hotplug_cfd(&hotplug_cfd_notifier
, CPU_UP_PREPARE
, cpu
);
95 register_cpu_notifier(&hotplug_cfd_notifier
);
99 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
101 * For non-synchronous ipi calls the csd can still be in use by the
102 * previous function call. For multi-cpu calls its even more interesting
103 * as we'll have to ensure no other cpu is observing our csd.
105 static void csd_lock_wait(struct call_single_data
*data
)
107 while (data
->flags
& CSD_FLAG_LOCK
)
111 static void csd_lock(struct call_single_data
*data
)
114 data
->flags
= CSD_FLAG_LOCK
;
117 * prevent CPU from reordering the above assignment
118 * to ->flags with any subsequent assignments to other
119 * fields of the specified call_single_data structure:
124 static void csd_unlock(struct call_single_data
*data
)
126 WARN_ON(!(data
->flags
& CSD_FLAG_LOCK
));
129 * ensure we're all done before releasing data:
133 data
->flags
&= ~CSD_FLAG_LOCK
;
137 * Insert a previously allocated call_single_data element
138 * for execution on the given CPU. data must already have
139 * ->func, ->info, and ->flags set.
142 void generic_exec_single(int cpu
, struct call_single_data
*data
, int wait
)
144 struct call_single_queue
*dst
= &per_cpu(call_single_queue
, cpu
);
148 raw_spin_lock_irqsave(&dst
->lock
, flags
);
149 ipi
= list_empty(&dst
->list
);
150 list_add_tail(&data
->list
, &dst
->list
);
151 raw_spin_unlock_irqrestore(&dst
->lock
, flags
);
154 * The list addition should be visible before sending the IPI
155 * handler locks the list to pull the entry off it because of
156 * normal cache coherency rules implied by spinlocks.
158 * If IPIs can go out of order to the cache coherency protocol
159 * in an architecture, sufficient synchronisation should be added
160 * to arch code to make it appear to obey cache coherency WRT
161 * locking and barrier primitives. Generic code isn't really
162 * equipped to do the right thing...
165 arch_send_call_function_single_ipi(cpu
);
172 * Invoked by arch to handle an IPI for call function. Must be called with
173 * interrupts disabled.
175 void generic_smp_call_function_interrupt(void)
177 struct call_function_data
*data
;
178 int cpu
= smp_processor_id();
181 * Shouldn't receive this interrupt on a cpu that is not yet online.
183 WARN_ON_ONCE(!cpu_online(cpu
));
186 * Ensure entry is visible on call_function_queue after we have
187 * entered the IPI. See comment in smp_call_function_many.
188 * If we don't have this, then we may miss an entry on the list
189 * and never get another IPI to process it.
194 * It's ok to use list_for_each_rcu() here even though we may
195 * delete 'pos', since list_del_rcu() doesn't clear ->next
197 list_for_each_entry_rcu(data
, &call_function
.queue
, csd
.list
) {
199 smp_call_func_t func
;
202 * Since we walk the list without any locks, we might
203 * see an entry that was completed, removed from the
204 * list and is in the process of being reused.
206 * We must check that the cpu is in the cpumask before
207 * checking the refs, and both must be set before
208 * executing the callback on this cpu.
211 if (!cpumask_test_cpu(cpu
, data
->cpumask
))
216 if (atomic_read(&data
->refs
) == 0)
219 func
= data
->csd
.func
; /* save for later warn */
220 func(data
->csd
.info
);
223 * If the cpu mask is not still set then func enabled
224 * interrupts (BUG), and this cpu took another smp call
225 * function interrupt and executed func(info) twice
226 * on this cpu. That nested execution decremented refs.
228 if (!cpumask_test_and_clear_cpu(cpu
, data
->cpumask
)) {
229 WARN(1, "%pf enabled interrupts and double executed\n", func
);
233 refs
= atomic_dec_return(&data
->refs
);
239 WARN_ON(!cpumask_empty(data
->cpumask
));
241 raw_spin_lock(&call_function
.lock
);
242 list_del_rcu(&data
->csd
.list
);
243 raw_spin_unlock(&call_function
.lock
);
245 csd_unlock(&data
->csd
);
251 * Invoked by arch to handle an IPI for call function single. Must be
252 * called from the arch with interrupts disabled.
254 void generic_smp_call_function_single_interrupt(void)
256 struct call_single_queue
*q
= &__get_cpu_var(call_single_queue
);
257 unsigned int data_flags
;
261 * Shouldn't receive this interrupt on a cpu that is not yet online.
263 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
265 raw_spin_lock(&q
->lock
);
266 list_replace_init(&q
->list
, &list
);
267 raw_spin_unlock(&q
->lock
);
269 while (!list_empty(&list
)) {
270 struct call_single_data
*data
;
272 data
= list_entry(list
.next
, struct call_single_data
, list
);
273 list_del(&data
->list
);
276 * 'data' can be invalid after this call if flags == 0
277 * (when called through generic_exec_single()),
278 * so save them away before making the call:
280 data_flags
= data
->flags
;
282 data
->func(data
->info
);
285 * Unlocked CSDs are valid through generic_exec_single():
287 if (data_flags
& CSD_FLAG_LOCK
)
292 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data
, csd_data
);
295 * smp_call_function_single - Run a function on a specific CPU
296 * @func: The function to run. This must be fast and non-blocking.
297 * @info: An arbitrary pointer to pass to the function.
298 * @wait: If true, wait until function has completed on other CPUs.
300 * Returns 0 on success, else a negative status code.
302 int smp_call_function_single(int cpu
, smp_call_func_t func
, void *info
,
305 struct call_single_data d
= {
313 * prevent preemption and reschedule on another processor,
314 * as well as CPU removal
316 this_cpu
= get_cpu();
319 * Can deadlock when called with interrupts disabled.
320 * We allow cpu's that are not yet online though, as no one else can
321 * send smp call function interrupt to this cpu and as such deadlocks
324 WARN_ON_ONCE(cpu_online(this_cpu
) && irqs_disabled()
325 && !oops_in_progress
);
327 if (cpu
== this_cpu
) {
328 local_irq_save(flags
);
330 local_irq_restore(flags
);
332 if ((unsigned)cpu
< nr_cpu_ids
&& cpu_online(cpu
)) {
333 struct call_single_data
*data
= &d
;
336 data
= &__get_cpu_var(csd_data
);
342 generic_exec_single(cpu
, data
, wait
);
344 err
= -ENXIO
; /* CPU not online */
352 EXPORT_SYMBOL(smp_call_function_single
);
355 * smp_call_function_any - Run a function on any of the given cpus
356 * @mask: The mask of cpus it can run on.
357 * @func: The function to run. This must be fast and non-blocking.
358 * @info: An arbitrary pointer to pass to the function.
359 * @wait: If true, wait until function has completed.
361 * Returns 0 on success, else a negative status code (if no cpus were online).
362 * Note that @wait will be implicitly turned on in case of allocation failures,
363 * since we fall back to on-stack allocation.
365 * Selection preference:
366 * 1) current cpu if in @mask
367 * 2) any cpu of current node if in @mask
368 * 3) any other online cpu in @mask
370 int smp_call_function_any(const struct cpumask
*mask
,
371 smp_call_func_t func
, void *info
, int wait
)
374 const struct cpumask
*nodemask
;
377 /* Try for same CPU (cheapest) */
379 if (cpumask_test_cpu(cpu
, mask
))
382 /* Try for same node. */
383 nodemask
= cpumask_of_node(cpu_to_node(cpu
));
384 for (cpu
= cpumask_first_and(nodemask
, mask
); cpu
< nr_cpu_ids
;
385 cpu
= cpumask_next_and(cpu
, nodemask
, mask
)) {
390 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
391 cpu
= cpumask_any_and(mask
, cpu_online_mask
);
393 ret
= smp_call_function_single(cpu
, func
, info
, wait
);
397 EXPORT_SYMBOL_GPL(smp_call_function_any
);
400 * __smp_call_function_single(): Run a function on a specific CPU
401 * @cpu: The CPU to run on.
402 * @data: Pre-allocated and setup data structure
403 * @wait: If true, wait until function has completed on specified CPU.
405 * Like smp_call_function_single(), but allow caller to pass in a
406 * pre-allocated data structure. Useful for embedding @data inside
407 * other structures, for instance.
409 void __smp_call_function_single(int cpu
, struct call_single_data
*data
,
412 unsigned int this_cpu
;
415 this_cpu
= get_cpu();
417 * Can deadlock when called with interrupts disabled.
418 * We allow cpu's that are not yet online though, as no one else can
419 * send smp call function interrupt to this cpu and as such deadlocks
422 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait
&& irqs_disabled()
423 && !oops_in_progress
);
425 if (cpu
== this_cpu
) {
426 local_irq_save(flags
);
427 data
->func(data
->info
);
428 local_irq_restore(flags
);
431 generic_exec_single(cpu
, data
, wait
);
437 * smp_call_function_many(): Run a function on a set of other CPUs.
438 * @mask: The set of cpus to run on (only runs on online subset).
439 * @func: The function to run. This must be fast and non-blocking.
440 * @info: An arbitrary pointer to pass to the function.
441 * @wait: If true, wait (atomically) until function has completed
444 * If @wait is true, then returns once @func has returned.
446 * You must not call this function with disabled interrupts or from a
447 * hardware interrupt handler or from a bottom half handler. Preemption
448 * must be disabled when calling this function.
450 void smp_call_function_many(const struct cpumask
*mask
,
451 smp_call_func_t func
, void *info
, bool wait
)
453 struct call_function_data
*data
;
455 int refs
, cpu
, next_cpu
, this_cpu
= smp_processor_id();
458 * Can deadlock when called with interrupts disabled.
459 * We allow cpu's that are not yet online though, as no one else can
460 * send smp call function interrupt to this cpu and as such deadlocks
463 WARN_ON_ONCE(cpu_online(this_cpu
) && irqs_disabled()
464 && !oops_in_progress
&& !early_boot_irqs_disabled
);
466 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
467 cpu
= cpumask_first_and(mask
, cpu_online_mask
);
469 cpu
= cpumask_next_and(cpu
, mask
, cpu_online_mask
);
471 /* No online cpus? We're done. */
472 if (cpu
>= nr_cpu_ids
)
475 /* Do we have another CPU which isn't us? */
476 next_cpu
= cpumask_next_and(cpu
, mask
, cpu_online_mask
);
477 if (next_cpu
== this_cpu
)
478 next_cpu
= cpumask_next_and(next_cpu
, mask
, cpu_online_mask
);
480 /* Fastpath: do that cpu by itself. */
481 if (next_cpu
>= nr_cpu_ids
) {
482 smp_call_function_single(cpu
, func
, info
, wait
);
486 data
= &__get_cpu_var(cfd_data
);
487 csd_lock(&data
->csd
);
489 /* This BUG_ON verifies our reuse assertions and can be removed */
490 BUG_ON(atomic_read(&data
->refs
) || !cpumask_empty(data
->cpumask
));
493 * The global call function queue list add and delete are protected
494 * by a lock, but the list is traversed without any lock, relying
495 * on the rcu list add and delete to allow safe concurrent traversal.
496 * We reuse the call function data without waiting for any grace
497 * period after some other cpu removes it from the global queue.
498 * This means a cpu might find our data block as it is being
501 * We hold off the interrupt handler on the other cpu by
502 * ordering our writes to the cpu mask vs our setting of the
503 * refs counter. We assert only the cpu owning the data block
504 * will set a bit in cpumask, and each bit will only be cleared
505 * by the subject cpu. Each cpu must first find its bit is
506 * set and then check that refs is set indicating the element is
507 * ready to be processed, otherwise it must skip the entry.
509 * On the previous iteration refs was set to 0 by another cpu.
510 * To avoid the use of transitivity, set the counter to 0 here
511 * so the wmb will pair with the rmb in the interrupt handler.
513 atomic_set(&data
->refs
, 0); /* convert 3rd to 1st party write */
515 data
->csd
.func
= func
;
516 data
->csd
.info
= info
;
518 /* Ensure 0 refs is visible before mask. Also orders func and info */
521 /* We rely on the "and" being processed before the store */
522 cpumask_and(data
->cpumask
, mask
, cpu_online_mask
);
523 cpumask_clear_cpu(this_cpu
, data
->cpumask
);
524 refs
= cpumask_weight(data
->cpumask
);
526 /* Some callers race with other cpus changing the passed mask */
527 if (unlikely(!refs
)) {
528 csd_unlock(&data
->csd
);
533 * After we put an entry into the list, data->cpumask
534 * may be cleared again when another CPU sends another IPI for
535 * a SMP function call, so data->cpumask will be zero.
537 cpumask_copy(data
->cpumask_ipi
, data
->cpumask
);
538 raw_spin_lock_irqsave(&call_function
.lock
, flags
);
540 * Place entry at the _HEAD_ of the list, so that any cpu still
541 * observing the entry in generic_smp_call_function_interrupt()
542 * will not miss any other list entries:
544 list_add_rcu(&data
->csd
.list
, &call_function
.queue
);
546 * We rely on the wmb() in list_add_rcu to complete our writes
547 * to the cpumask before this write to refs, which indicates
548 * data is on the list and is ready to be processed.
550 atomic_set(&data
->refs
, refs
);
551 raw_spin_unlock_irqrestore(&call_function
.lock
, flags
);
554 * Make the list addition visible before sending the ipi.
555 * (IPIs must obey or appear to obey normal Linux cache
556 * coherency rules -- see comment in generic_exec_single).
560 /* Send a message to all CPUs in the map */
561 arch_send_call_function_ipi_mask(data
->cpumask_ipi
);
563 /* Optionally wait for the CPUs to complete */
565 csd_lock_wait(&data
->csd
);
567 EXPORT_SYMBOL(smp_call_function_many
);
570 * smp_call_function(): Run a function on all other CPUs.
571 * @func: The function to run. This must be fast and non-blocking.
572 * @info: An arbitrary pointer to pass to the function.
573 * @wait: If true, wait (atomically) until function has completed
578 * If @wait is true, then returns once @func has returned; otherwise
579 * it returns just before the target cpu calls @func.
581 * You must not call this function with disabled interrupts or from a
582 * hardware interrupt handler or from a bottom half handler.
584 int smp_call_function(smp_call_func_t func
, void *info
, int wait
)
587 smp_call_function_many(cpu_online_mask
, func
, info
, wait
);
592 EXPORT_SYMBOL(smp_call_function
);
594 void ipi_call_lock(void)
596 raw_spin_lock(&call_function
.lock
);
599 void ipi_call_unlock(void)
601 raw_spin_unlock(&call_function
.lock
);
604 void ipi_call_lock_irq(void)
606 raw_spin_lock_irq(&call_function
.lock
);
609 void ipi_call_unlock_irq(void)
611 raw_spin_unlock_irq(&call_function
.lock
);
613 #endif /* USE_GENERIC_SMP_HELPERS */
615 /* Setup configured maximum number of CPUs to activate */
616 unsigned int setup_max_cpus
= NR_CPUS
;
617 EXPORT_SYMBOL(setup_max_cpus
);
621 * Setup routine for controlling SMP activation
623 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
624 * activation entirely (the MPS table probe still happens, though).
626 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
627 * greater than 0, limits the maximum number of CPUs activated in
631 void __weak
arch_disable_smp_support(void) { }
633 static int __init
nosmp(char *str
)
636 arch_disable_smp_support();
641 early_param("nosmp", nosmp
);
643 /* this is hard limit */
644 static int __init
nrcpus(char *str
)
648 get_option(&str
, &nr_cpus
);
649 if (nr_cpus
> 0 && nr_cpus
< nr_cpu_ids
)
650 nr_cpu_ids
= nr_cpus
;
655 early_param("nr_cpus", nrcpus
);
657 static int __init
maxcpus(char *str
)
659 get_option(&str
, &setup_max_cpus
);
660 if (setup_max_cpus
== 0)
661 arch_disable_smp_support();
666 early_param("maxcpus", maxcpus
);
668 /* Setup number of possible processor ids */
669 int nr_cpu_ids __read_mostly
= NR_CPUS
;
670 EXPORT_SYMBOL(nr_cpu_ids
);
672 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
673 void __init
setup_nr_cpu_ids(void)
675 nr_cpu_ids
= find_last_bit(cpumask_bits(cpu_possible_mask
),NR_CPUS
) + 1;
678 /* Called by boot processor to activate the rest. */
679 void __init
smp_init(void)
683 /* FIXME: This should be done in userspace --RR */
684 for_each_present_cpu(cpu
) {
685 if (num_online_cpus() >= setup_max_cpus
)
687 if (!cpu_online(cpu
))
691 /* Any cleanup work */
692 printk(KERN_INFO
"Brought up %ld CPUs\n", (long)num_online_cpus());
693 smp_cpus_done(setup_max_cpus
);
697 * Call a function on all processors. May be used during early boot while
698 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
699 * of local_irq_disable/enable().
701 int on_each_cpu(void (*func
) (void *info
), void *info
, int wait
)
707 ret
= smp_call_function(func
, info
, wait
);
708 local_irq_save(flags
);
710 local_irq_restore(flags
);
714 EXPORT_SYMBOL(on_each_cpu
);
717 * on_each_cpu_mask(): Run a function on processors specified by
718 * cpumask, which may include the local processor.
719 * @mask: The set of cpus to run on (only runs on online subset).
720 * @func: The function to run. This must be fast and non-blocking.
721 * @info: An arbitrary pointer to pass to the function.
722 * @wait: If true, wait (atomically) until function has completed
725 * If @wait is true, then returns once @func has returned.
727 * You must not call this function with disabled interrupts or
728 * from a hardware interrupt handler or from a bottom half handler.
730 void on_each_cpu_mask(const struct cpumask
*mask
, smp_call_func_t func
,
731 void *info
, bool wait
)
735 smp_call_function_many(mask
, func
, info
, wait
);
736 if (cpumask_test_cpu(cpu
, mask
)) {
743 EXPORT_SYMBOL(on_each_cpu_mask
);
746 * on_each_cpu_cond(): Call a function on each processor for which
747 * the supplied function cond_func returns true, optionally waiting
748 * for all the required CPUs to finish. This may include the local
750 * @cond_func: A callback function that is passed a cpu id and
751 * the the info parameter. The function is called
752 * with preemption disabled. The function should
753 * return a blooean value indicating whether to IPI
755 * @func: The function to run on all applicable CPUs.
756 * This must be fast and non-blocking.
757 * @info: An arbitrary pointer to pass to both functions.
758 * @wait: If true, wait (atomically) until function has
759 * completed on other CPUs.
760 * @gfp_flags: GFP flags to use when allocating the cpumask
761 * used internally by the function.
763 * The function might sleep if the GFP flags indicates a non
764 * atomic allocation is allowed.
766 * Preemption is disabled to protect against CPUs going offline but not online.
767 * CPUs going online during the call will not be seen or sent an IPI.
769 * You must not call this function with disabled interrupts or
770 * from a hardware interrupt handler or from a bottom half handler.
772 void on_each_cpu_cond(bool (*cond_func
)(int cpu
, void *info
),
773 smp_call_func_t func
, void *info
, bool wait
,
779 might_sleep_if(gfp_flags
& __GFP_WAIT
);
781 if (likely(zalloc_cpumask_var(&cpus
, (gfp_flags
|__GFP_NOWARN
)))) {
783 for_each_online_cpu(cpu
)
784 if (cond_func(cpu
, info
))
785 cpumask_set_cpu(cpu
, cpus
);
786 on_each_cpu_mask(cpus
, func
, info
, wait
);
788 free_cpumask_var(cpus
);
791 * No free cpumask, bother. No matter, we'll
792 * just have to IPI them one by one.
795 for_each_online_cpu(cpu
)
796 if (cond_func(cpu
, info
)) {
797 ret
= smp_call_function_single(cpu
, func
,
804 EXPORT_SYMBOL(on_each_cpu_cond
);