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>
15 #define CREATE_TRACE_POINTS
16 #include <trace/events/smp.h>
20 #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
25 struct call_function_data {
26 struct call_single_data __percpu *csd;
27 cpumask_var_t cpumask;
28 cpumask_var_t cpumask_ipi;
31 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
33 struct call_single_queue {
34 struct list_head list;
38 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
41 hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
43 long cpu = (long)hcpu;
44 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
48 case CPU_UP_PREPARE_FROZEN:
49 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
51 return notifier_from_errno(-ENOMEM);
52 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
54 return notifier_from_errno(-ENOMEM);
55 cfd->csd = alloc_percpu(struct call_single_data);
57 free_cpumask_var(cfd->cpumask);
58 return notifier_from_errno(-ENOMEM);
62 #ifdef CONFIG_HOTPLUG_CPU
64 case CPU_UP_CANCELED_FROZEN:
68 free_cpumask_var(cfd->cpumask);
69 free_cpumask_var(cfd->cpumask_ipi);
70 free_percpu(cfd->csd);
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 *csd)
107 while (csd->flags & CSD_FLAG_LOCK)
111 static void csd_lock(struct call_single_data *csd)
114 csd->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 *csd)
126 WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
129 * ensure we're all done before releasing data:
133 csd->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 *csd, 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(&csd->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 trace_smp_call_func_send(csd->func, cpu);
166 arch_send_call_function_single_ipi(cpu);
174 * Invoked by arch to handle an IPI for call function single. Must be
175 * called from the arch with interrupts disabled.
177 void generic_smp_call_function_single_interrupt(void)
179 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
183 * Shouldn't receive this interrupt on a cpu that is not yet online.
185 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
187 raw_spin_lock(&q->lock);
188 list_replace_init(&q->list, &list);
189 raw_spin_unlock(&q->lock);
191 while (!list_empty(&list)) {
192 struct call_single_data *csd;
193 unsigned int csd_flags;
195 csd = list_entry(list.next, struct call_single_data, list);
196 list_del(&csd->list);
199 * 'csd' can be invalid after this call if flags == 0
200 * (when called through generic_exec_single()),
201 * so save them away before making the call:
203 csd_flags = csd->flags;
204 trace_smp_call_func_entry(csd->func);
205 csd->func(csd->info);
206 trace_smp_call_func_exit(csd->func);
209 * Unlocked CSDs are valid through generic_exec_single():
211 if (csd_flags & CSD_FLAG_LOCK)
216 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
219 * smp_call_function_single - Run a function on a specific CPU
220 * @func: The function to run. This must be fast and non-blocking.
221 * @info: An arbitrary pointer to pass to the function.
222 * @wait: If true, wait until function has completed on other CPUs.
224 * Returns 0 on success, else a negative status code.
226 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
229 struct call_single_data d = {
236 trace_smp_call_func_send(func, cpu);
238 * prevent preemption and reschedule on another processor,
239 * as well as CPU removal
241 this_cpu = get_cpu();
244 * Can deadlock when called with interrupts disabled.
245 * We allow cpu's that are not yet online though, as no one else can
246 * send smp call function interrupt to this cpu and as such deadlocks
249 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
250 && !oops_in_progress);
252 if (cpu == this_cpu) {
253 local_irq_save(flags);
254 trace_smp_call_func_entry(func);
256 trace_smp_call_func_exit(func);
257 local_irq_restore(flags);
259 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
260 struct call_single_data *csd = &d;
263 csd = &__get_cpu_var(csd_data);
269 generic_exec_single(cpu, csd, wait);
271 err = -ENXIO; /* CPU not online */
279 EXPORT_SYMBOL(smp_call_function_single);
282 * smp_call_function_any - Run a function on any of the given cpus
283 * @mask: The mask of cpus it can run on.
284 * @func: The function to run. This must be fast and non-blocking.
285 * @info: An arbitrary pointer to pass to the function.
286 * @wait: If true, wait until function has completed.
288 * Returns 0 on success, else a negative status code (if no cpus were online).
289 * Note that @wait will be implicitly turned on in case of allocation failures,
290 * since we fall back to on-stack allocation.
292 * Selection preference:
293 * 1) current cpu if in @mask
294 * 2) any cpu of current node if in @mask
295 * 3) any other online cpu in @mask
297 int smp_call_function_any(const struct cpumask *mask,
298 smp_call_func_t func, void *info, int wait)
301 const struct cpumask *nodemask;
304 /* Try for same CPU (cheapest) */
306 if (cpumask_test_cpu(cpu, mask))
309 /* Try for same node. */
310 nodemask = cpumask_of_node(cpu_to_node(cpu));
311 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
312 cpu = cpumask_next_and(cpu, nodemask, mask)) {
317 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
318 cpu = cpumask_any_and(mask, cpu_online_mask);
320 ret = smp_call_function_single(cpu, func, info, wait);
324 EXPORT_SYMBOL_GPL(smp_call_function_any);
327 * __smp_call_function_single(): Run a function on a specific CPU
328 * @cpu: The CPU to run on.
329 * @data: Pre-allocated and setup data structure
330 * @wait: If true, wait until function has completed on specified CPU.
332 * Like smp_call_function_single(), but allow caller to pass in a
333 * pre-allocated data structure. Useful for embedding @data inside
334 * other structures, for instance.
336 void __smp_call_function_single(int cpu, struct call_single_data *csd,
339 unsigned int this_cpu;
342 this_cpu = get_cpu();
344 * Can deadlock when called with interrupts disabled.
345 * We allow cpu's that are not yet online though, as no one else can
346 * send smp call function interrupt to this cpu and as such deadlocks
349 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
350 && !oops_in_progress);
352 if (cpu == this_cpu) {
353 local_irq_save(flags);
354 csd->func(csd->info);
355 local_irq_restore(flags);
358 generic_exec_single(cpu, csd, wait);
364 * smp_call_function_many(): Run a function on a set of other CPUs.
365 * @mask: The set of cpus to run on (only runs on online subset).
366 * @func: The function to run. This must be fast and non-blocking.
367 * @info: An arbitrary pointer to pass to the function.
368 * @wait: If true, wait (atomically) until function has completed
371 * If @wait is true, then returns once @func has returned.
373 * You must not call this function with disabled interrupts or from a
374 * hardware interrupt handler or from a bottom half handler. Preemption
375 * must be disabled when calling this function.
377 void smp_call_function_many(const struct cpumask *mask,
378 smp_call_func_t func, void *info, bool wait)
380 struct call_function_data *cfd;
381 int cpu, next_cpu, this_cpu = smp_processor_id();
384 * Can deadlock when called with interrupts disabled.
385 * We allow cpu's that are not yet online though, as no one else can
386 * send smp call function interrupt to this cpu and as such deadlocks
389 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
390 && !oops_in_progress && !early_boot_irqs_disabled);
392 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
393 cpu = cpumask_first_and(mask, cpu_online_mask);
395 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
397 /* No online cpus? We're done. */
398 if (cpu >= nr_cpu_ids)
401 /* Do we have another CPU which isn't us? */
402 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
403 if (next_cpu == this_cpu)
404 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
406 /* Fastpath: do that cpu by itself. */
407 if (next_cpu >= nr_cpu_ids) {
408 smp_call_function_single(cpu, func, info, wait);
412 cfd = &__get_cpu_var(cfd_data);
414 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
415 cpumask_clear_cpu(this_cpu, cfd->cpumask);
417 /* Some callers race with other cpus changing the passed mask */
418 if (unlikely(!cpumask_weight(cfd->cpumask)))
422 * After we put an entry into the list, cfd->cpumask may be cleared
423 * again when another CPU sends another IPI for a SMP function call, so
424 * cfd->cpumask will be zero.
426 cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
428 for_each_cpu(cpu, cfd->cpumask) {
429 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
430 struct call_single_queue *dst =
431 &per_cpu(call_single_queue, cpu);
438 raw_spin_lock_irqsave(&dst->lock, flags);
439 list_add_tail(&csd->list, &dst->list);
440 raw_spin_unlock_irqrestore(&dst->lock, flags);
443 /* Send a message to all CPUs in the map */
444 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
447 for_each_cpu(cpu, cfd->cpumask) {
448 struct call_single_data *csd;
450 csd = per_cpu_ptr(cfd->csd, cpu);
455 EXPORT_SYMBOL(smp_call_function_many);
458 * smp_call_function(): Run a function on all other CPUs.
459 * @func: The function to run. This must be fast and non-blocking.
460 * @info: An arbitrary pointer to pass to the function.
461 * @wait: If true, wait (atomically) until function has completed
466 * If @wait is true, then returns once @func has returned; otherwise
467 * it returns just before the target cpu calls @func.
469 * You must not call this function with disabled interrupts or from a
470 * hardware interrupt handler or from a bottom half handler.
472 int smp_call_function(smp_call_func_t func, void *info, int wait)
475 smp_call_function_many(cpu_online_mask, func, info, wait);
480 EXPORT_SYMBOL(smp_call_function);
481 #endif /* USE_GENERIC_SMP_HELPERS */
483 /* Setup configured maximum number of CPUs to activate */
484 unsigned int setup_max_cpus = NR_CPUS;
485 EXPORT_SYMBOL(setup_max_cpus);
489 * Setup routine for controlling SMP activation
491 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
492 * activation entirely (the MPS table probe still happens, though).
494 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
495 * greater than 0, limits the maximum number of CPUs activated in
499 void __weak arch_disable_smp_support(void) { }
501 static int __init nosmp(char *str)
504 arch_disable_smp_support();
509 early_param("nosmp", nosmp);
511 /* this is hard limit */
512 static int __init nrcpus(char *str)
516 get_option(&str, &nr_cpus);
517 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
518 nr_cpu_ids = nr_cpus;
523 early_param("nr_cpus", nrcpus);
525 static int __init maxcpus(char *str)
527 get_option(&str, &setup_max_cpus);
528 if (setup_max_cpus == 0)
529 arch_disable_smp_support();
534 early_param("maxcpus", maxcpus);
536 /* Setup number of possible processor ids */
537 int nr_cpu_ids __read_mostly = NR_CPUS;
538 EXPORT_SYMBOL(nr_cpu_ids);
540 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
541 void __init setup_nr_cpu_ids(void)
543 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
546 /* Called by boot processor to activate the rest. */
547 void __init smp_init(void)
553 /* FIXME: This should be done in userspace --RR */
554 for_each_present_cpu(cpu) {
555 if (num_online_cpus() >= setup_max_cpus)
557 if (!cpu_online(cpu))
561 /* Any cleanup work */
562 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
563 smp_cpus_done(setup_max_cpus);
567 * Call a function on all processors. May be used during early boot while
568 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
569 * of local_irq_disable/enable().
571 int on_each_cpu(void (*func) (void *info), void *info, int wait)
577 ret = smp_call_function(func, info, wait);
578 local_irq_save(flags);
580 local_irq_restore(flags);
584 EXPORT_SYMBOL(on_each_cpu);
587 * on_each_cpu_mask(): Run a function on processors specified by
588 * cpumask, which may include the local processor.
589 * @mask: The set of cpus to run on (only runs on online subset).
590 * @func: The function to run. This must be fast and non-blocking.
591 * @info: An arbitrary pointer to pass to the function.
592 * @wait: If true, wait (atomically) until function has completed
595 * If @wait is true, then returns once @func has returned.
597 * You must not call this function with disabled interrupts or
598 * from a hardware interrupt handler or from a bottom half handler.
600 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
601 void *info, bool wait)
605 smp_call_function_many(mask, func, info, wait);
606 if (cpumask_test_cpu(cpu, mask)) {
613 EXPORT_SYMBOL(on_each_cpu_mask);
616 * on_each_cpu_cond(): Call a function on each processor for which
617 * the supplied function cond_func returns true, optionally waiting
618 * for all the required CPUs to finish. This may include the local
620 * @cond_func: A callback function that is passed a cpu id and
621 * the the info parameter. The function is called
622 * with preemption disabled. The function should
623 * return a blooean value indicating whether to IPI
625 * @func: The function to run on all applicable CPUs.
626 * This must be fast and non-blocking.
627 * @info: An arbitrary pointer to pass to both functions.
628 * @wait: If true, wait (atomically) until function has
629 * completed on other CPUs.
630 * @gfp_flags: GFP flags to use when allocating the cpumask
631 * used internally by the function.
633 * The function might sleep if the GFP flags indicates a non
634 * atomic allocation is allowed.
636 * Preemption is disabled to protect against CPUs going offline but not online.
637 * CPUs going online during the call will not be seen or sent an IPI.
639 * You must not call this function with disabled interrupts or
640 * from a hardware interrupt handler or from a bottom half handler.
642 void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
643 smp_call_func_t func, void *info, bool wait,
649 might_sleep_if(gfp_flags & __GFP_WAIT);
651 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
653 for_each_online_cpu(cpu)
654 if (cond_func(cpu, info))
655 cpumask_set_cpu(cpu, cpus);
656 on_each_cpu_mask(cpus, func, info, wait);
658 free_cpumask_var(cpus);
661 * No free cpumask, bother. No matter, we'll
662 * just have to IPI them one by one.
665 for_each_online_cpu(cpu)
666 if (cond_func(cpu, info)) {
667 ret = smp_call_function_single(cpu, func,
674 EXPORT_SYMBOL(on_each_cpu_cond);
676 static void do_nothing(void *unused)
681 * kick_all_cpus_sync - Force all cpus out of idle
683 * Used to synchronize the update of pm_idle function pointer. It's
684 * called after the pointer is updated and returns after the dummy
685 * callback function has been executed on all cpus. The execution of
686 * the function can only happen on the remote cpus after they have
687 * left the idle function which had been called via pm_idle function
688 * pointer. So it's guaranteed that nothing uses the previous pointer
691 void kick_all_cpus_sync(void)
693 /* Make sure the change is visible before we kick the cpus */
695 smp_call_function(do_nothing, NULL, 1);
697 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);