1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic helpers for smp ipi calls
5 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/irq_work.h>
11 #include <linux/rcupdate.h>
12 #include <linux/rculist.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/percpu.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/gfp.h>
19 #include <linux/smp.h>
20 #include <linux/cpu.h>
21 #include <linux/sched.h>
22 #include <linux/sched/idle.h>
23 #include <linux/hypervisor.h>
24 #include <linux/sched/clock.h>
25 #include <linux/nmi.h>
26 #include <linux/sched/debug.h>
29 #include "sched/smp.h"
31 #define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
33 struct call_function_data {
34 call_single_data_t __percpu *csd;
35 cpumask_var_t cpumask;
36 cpumask_var_t cpumask_ipi;
39 static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
41 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
43 static void flush_smp_call_function_queue(bool warn_cpu_offline);
45 int smpcfd_prepare_cpu(unsigned int cpu)
47 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
49 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
52 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
54 free_cpumask_var(cfd->cpumask);
57 cfd->csd = alloc_percpu(call_single_data_t);
59 free_cpumask_var(cfd->cpumask);
60 free_cpumask_var(cfd->cpumask_ipi);
67 int smpcfd_dead_cpu(unsigned int cpu)
69 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
71 free_cpumask_var(cfd->cpumask);
72 free_cpumask_var(cfd->cpumask_ipi);
73 free_percpu(cfd->csd);
77 int smpcfd_dying_cpu(unsigned int cpu)
80 * The IPIs for the smp-call-function callbacks queued by other
81 * CPUs might arrive late, either due to hardware latencies or
82 * because this CPU disabled interrupts (inside stop-machine)
83 * before the IPIs were sent. So flush out any pending callbacks
84 * explicitly (without waiting for the IPIs to arrive), to
85 * ensure that the outgoing CPU doesn't go offline with work
88 flush_smp_call_function_queue(false);
93 void __init call_function_init(void)
97 for_each_possible_cpu(i)
98 init_llist_head(&per_cpu(call_single_queue, i));
100 smpcfd_prepare_cpu(smp_processor_id());
103 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
105 static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
106 static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
107 static DEFINE_PER_CPU(void *, cur_csd_info);
109 #define CSD_LOCK_TIMEOUT (5ULL * NSEC_PER_SEC)
110 static atomic_t csd_bug_count = ATOMIC_INIT(0);
112 /* Record current CSD work for current CPU, NULL to erase. */
113 static void csd_lock_record(call_single_data_t *csd)
116 smp_mb(); /* NULL cur_csd after unlock. */
117 __this_cpu_write(cur_csd, NULL);
120 __this_cpu_write(cur_csd_func, csd->func);
121 __this_cpu_write(cur_csd_info, csd->info);
122 smp_wmb(); /* func and info before csd. */
123 __this_cpu_write(cur_csd, csd);
124 smp_mb(); /* Update cur_csd before function call. */
125 /* Or before unlock, as the case may be. */
128 static __always_inline int csd_lock_wait_getcpu(call_single_data_t *csd)
130 unsigned int csd_type;
132 csd_type = CSD_TYPE(csd);
133 if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
134 return csd->node.dst; /* Other CSD_TYPE_ values might not have ->dst. */
139 * Complain if too much time spent waiting. Note that only
140 * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU,
141 * so waiting on other types gets much less information.
143 static __always_inline bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, int *bug_id)
149 call_single_data_t *cpu_cur_csd;
150 unsigned int flags = READ_ONCE(csd->node.u_flags);
152 if (!(flags & CSD_FLAG_LOCK)) {
153 if (!unlikely(*bug_id))
155 cpu = csd_lock_wait_getcpu(csd);
156 pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
157 *bug_id, raw_smp_processor_id(), cpu);
162 ts_delta = ts2 - *ts1;
163 if (likely(ts_delta <= CSD_LOCK_TIMEOUT))
166 firsttime = !*bug_id;
168 *bug_id = atomic_inc_return(&csd_bug_count);
169 cpu = csd_lock_wait_getcpu(csd);
170 if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
174 cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); /* Before func and info. */
175 pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %llu ns for CPU#%02d %pS(%ps).\n",
176 firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), ts2 - ts0,
177 cpu, csd->func, csd->info);
178 if (cpu_cur_csd && csd != cpu_cur_csd) {
179 pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
180 *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
181 READ_ONCE(per_cpu(cur_csd_info, cpux)));
183 pr_alert("\tcsd: CSD lock (#%d) %s.\n",
184 *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
187 if (!trigger_single_cpu_backtrace(cpu))
190 pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
191 arch_send_call_function_single_ipi(cpu);
201 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
203 * For non-synchronous ipi calls the csd can still be in use by the
204 * previous function call. For multi-cpu calls its even more interesting
205 * as we'll have to ensure no other cpu is observing our csd.
207 static __always_inline void csd_lock_wait(call_single_data_t *csd)
212 ts1 = ts0 = sched_clock();
214 if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
218 smp_acquire__after_ctrl_dep();
222 static void csd_lock_record(call_single_data_t *csd)
226 static __always_inline void csd_lock_wait(call_single_data_t *csd)
228 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
232 static __always_inline void csd_lock(call_single_data_t *csd)
235 csd->node.u_flags |= CSD_FLAG_LOCK;
238 * prevent CPU from reordering the above assignment
239 * to ->flags with any subsequent assignments to other
240 * fields of the specified call_single_data_t structure:
245 static __always_inline void csd_unlock(call_single_data_t *csd)
247 WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
250 * ensure we're all done before releasing data:
252 smp_store_release(&csd->node.u_flags, 0);
255 static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
257 void __smp_call_single_queue(int cpu, struct llist_node *node)
260 * The list addition should be visible before sending the IPI
261 * handler locks the list to pull the entry off it because of
262 * normal cache coherency rules implied by spinlocks.
264 * If IPIs can go out of order to the cache coherency protocol
265 * in an architecture, sufficient synchronisation should be added
266 * to arch code to make it appear to obey cache coherency WRT
267 * locking and barrier primitives. Generic code isn't really
268 * equipped to do the right thing...
270 if (llist_add(node, &per_cpu(call_single_queue, cpu)))
271 send_call_function_single_ipi(cpu);
275 * Insert a previously allocated call_single_data_t element
276 * for execution on the given CPU. data must already have
277 * ->func, ->info, and ->flags set.
279 static int generic_exec_single(int cpu, call_single_data_t *csd)
281 if (cpu == smp_processor_id()) {
282 smp_call_func_t func = csd->func;
283 void *info = csd->info;
287 * We can unlock early even for the synchronous on-stack case,
288 * since we're doing this from the same CPU..
290 csd_lock_record(csd);
292 local_irq_save(flags);
294 csd_lock_record(NULL);
295 local_irq_restore(flags);
299 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
304 __smp_call_single_queue(cpu, &csd->node.llist);
310 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
312 * Invoked by arch to handle an IPI for call function single.
313 * Must be called with interrupts disabled.
315 void generic_smp_call_function_single_interrupt(void)
317 flush_smp_call_function_queue(true);
321 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
323 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
324 * offline CPU. Skip this check if set to 'false'.
326 * Flush any pending smp-call-function callbacks queued on this CPU. This is
327 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
328 * to ensure that all pending IPI callbacks are run before it goes completely
331 * Loop through the call_single_queue and run all the queued callbacks.
332 * Must be called with interrupts disabled.
334 static void flush_smp_call_function_queue(bool warn_cpu_offline)
336 call_single_data_t *csd, *csd_next;
337 struct llist_node *entry, *prev;
338 struct llist_head *head;
341 lockdep_assert_irqs_disabled();
343 head = this_cpu_ptr(&call_single_queue);
344 entry = llist_del_all(head);
345 entry = llist_reverse_order(entry);
347 /* There shouldn't be any pending callbacks on an offline CPU. */
348 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
349 !warned && !llist_empty(head))) {
351 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
354 * We don't have to use the _safe() variant here
355 * because we are not invoking the IPI handlers yet.
357 llist_for_each_entry(csd, entry, node.llist) {
358 switch (CSD_TYPE(csd)) {
361 case CSD_TYPE_IRQ_WORK:
362 pr_warn("IPI callback %pS sent to offline CPU\n",
367 pr_warn("IPI task-wakeup sent to offline CPU\n");
371 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
379 * First; run all SYNC callbacks, people are waiting for us.
382 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
383 /* Do we wait until *after* callback? */
384 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
385 smp_call_func_t func = csd->func;
386 void *info = csd->info;
389 prev->next = &csd_next->node.llist;
391 entry = &csd_next->node.llist;
394 csd_lock_record(csd);
397 csd_lock_record(NULL);
399 prev = &csd->node.llist;
407 * Second; run all !SYNC callbacks.
410 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
411 int type = CSD_TYPE(csd);
413 if (type != CSD_TYPE_TTWU) {
415 prev->next = &csd_next->node.llist;
417 entry = &csd_next->node.llist;
420 if (type == CSD_TYPE_ASYNC) {
421 smp_call_func_t func = csd->func;
422 void *info = csd->info;
424 csd_lock_record(csd);
427 csd_lock_record(NULL);
428 } else if (type == CSD_TYPE_IRQ_WORK) {
429 irq_work_single(csd);
433 prev = &csd->node.llist;
438 * Third; only CSD_TYPE_TTWU is left, issue those.
441 sched_ttwu_pending(entry);
444 void flush_smp_call_function_from_idle(void)
448 if (llist_empty(this_cpu_ptr(&call_single_queue)))
451 local_irq_save(flags);
452 flush_smp_call_function_queue(true);
453 if (local_softirq_pending())
456 local_irq_restore(flags);
460 * smp_call_function_single - Run a function on a specific CPU
461 * @func: The function to run. This must be fast and non-blocking.
462 * @info: An arbitrary pointer to pass to the function.
463 * @wait: If true, wait until function has completed on other CPUs.
465 * Returns 0 on success, else a negative status code.
467 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
470 call_single_data_t *csd;
471 call_single_data_t csd_stack = {
472 .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
478 * prevent preemption and reschedule on another processor,
479 * as well as CPU removal
481 this_cpu = get_cpu();
484 * Can deadlock when called with interrupts disabled.
485 * We allow cpu's that are not yet online though, as no one else can
486 * send smp call function interrupt to this cpu and as such deadlocks
489 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
490 && !oops_in_progress);
493 * When @wait we can deadlock when we interrupt between llist_add() and
494 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
495 * csd_lock() on because the interrupt context uses the same csd
498 WARN_ON_ONCE(!in_task());
502 csd = this_cpu_ptr(&csd_data);
508 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
509 csd->node.src = smp_processor_id();
513 err = generic_exec_single(cpu, csd);
522 EXPORT_SYMBOL(smp_call_function_single);
525 * smp_call_function_single_async(): Run an asynchronous function on a
527 * @cpu: The CPU to run on.
528 * @csd: Pre-allocated and setup data structure
530 * Like smp_call_function_single(), but the call is asynchonous and
531 * can thus be done from contexts with disabled interrupts.
533 * The caller passes his own pre-allocated data structure
534 * (ie: embedded in an object) and is responsible for synchronizing it
535 * such that the IPIs performed on the @csd are strictly serialized.
537 * If the function is called with one csd which has not yet been
538 * processed by previous call to smp_call_function_single_async(), the
539 * function will return immediately with -EBUSY showing that the csd
540 * object is still in progress.
542 * NOTE: Be careful, there is unfortunately no current debugging facility to
543 * validate the correctness of this serialization.
545 int smp_call_function_single_async(int cpu, call_single_data_t *csd)
551 if (csd->node.u_flags & CSD_FLAG_LOCK) {
556 csd->node.u_flags = CSD_FLAG_LOCK;
559 err = generic_exec_single(cpu, csd);
566 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
569 * smp_call_function_any - Run a function on any of the given cpus
570 * @mask: The mask of cpus it can run on.
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 until function has completed.
575 * Returns 0 on success, else a negative status code (if no cpus were online).
577 * Selection preference:
578 * 1) current cpu if in @mask
579 * 2) any cpu of current node if in @mask
580 * 3) any other online cpu in @mask
582 int smp_call_function_any(const struct cpumask *mask,
583 smp_call_func_t func, void *info, int wait)
586 const struct cpumask *nodemask;
589 /* Try for same CPU (cheapest) */
591 if (cpumask_test_cpu(cpu, mask))
594 /* Try for same node. */
595 nodemask = cpumask_of_node(cpu_to_node(cpu));
596 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
597 cpu = cpumask_next_and(cpu, nodemask, mask)) {
602 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
603 cpu = cpumask_any_and(mask, cpu_online_mask);
605 ret = smp_call_function_single(cpu, func, info, wait);
609 EXPORT_SYMBOL_GPL(smp_call_function_any);
612 * Flags to be used as scf_flags argument of smp_call_function_many_cond().
614 * %SCF_WAIT: Wait until function execution is completed
615 * %SCF_RUN_LOCAL: Run also locally if local cpu is set in cpumask
617 #define SCF_WAIT (1U << 0)
618 #define SCF_RUN_LOCAL (1U << 1)
620 static void smp_call_function_many_cond(const struct cpumask *mask,
621 smp_call_func_t func, void *info,
622 unsigned int scf_flags,
623 smp_cond_func_t cond_func)
625 int cpu, last_cpu, this_cpu = smp_processor_id();
626 struct call_function_data *cfd;
627 bool wait = scf_flags & SCF_WAIT;
628 bool run_remote = false;
629 bool run_local = false;
632 lockdep_assert_preemption_disabled();
635 * Can deadlock when called with interrupts disabled.
636 * We allow cpu's that are not yet online though, as no one else can
637 * send smp call function interrupt to this cpu and as such deadlocks
640 if (cpu_online(this_cpu) && !oops_in_progress &&
641 !early_boot_irqs_disabled)
642 lockdep_assert_irqs_enabled();
645 * When @wait we can deadlock when we interrupt between llist_add() and
646 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
647 * csd_lock() on because the interrupt context uses the same csd
650 WARN_ON_ONCE(!in_task());
652 /* Check if we need local execution. */
653 if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
656 /* Check if we need remote execution, i.e., any CPU excluding this one. */
657 cpu = cpumask_first_and(mask, cpu_online_mask);
659 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
660 if (cpu < nr_cpu_ids)
664 cfd = this_cpu_ptr(&cfd_data);
665 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
666 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
668 cpumask_clear(cfd->cpumask_ipi);
669 for_each_cpu(cpu, cfd->cpumask) {
670 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
672 if (cond_func && !cond_func(cpu, info))
677 csd->node.u_flags |= CSD_TYPE_SYNC;
680 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
681 csd->node.src = smp_processor_id();
684 if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) {
685 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
692 * Choose the most efficient way to send an IPI. Note that the
693 * number of CPUs might be zero due to concurrent changes to the
697 arch_send_call_function_single_ipi(last_cpu);
698 else if (likely(nr_cpus > 1))
699 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
702 if (run_local && (!cond_func || cond_func(this_cpu, info))) {
705 local_irq_save(flags);
707 local_irq_restore(flags);
710 if (run_remote && wait) {
711 for_each_cpu(cpu, cfd->cpumask) {
712 call_single_data_t *csd;
714 csd = per_cpu_ptr(cfd->csd, cpu);
721 * smp_call_function_many(): Run a function on a set of CPUs.
722 * @mask: The set of cpus to run on (only runs on online subset).
723 * @func: The function to run. This must be fast and non-blocking.
724 * @info: An arbitrary pointer to pass to the function.
725 * @flags: Bitmask that controls the operation. If %SCF_WAIT is set, wait
726 * (atomically) until function has completed on other CPUs. If
727 * %SCF_RUN_LOCAL is set, the function will also be run locally
728 * if the local CPU is set in the @cpumask.
730 * If @wait is true, then returns once @func has returned.
732 * You must not call this function with disabled interrupts or from a
733 * hardware interrupt handler or from a bottom half handler. Preemption
734 * must be disabled when calling this function.
736 void smp_call_function_many(const struct cpumask *mask,
737 smp_call_func_t func, void *info, bool wait)
739 smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
741 EXPORT_SYMBOL(smp_call_function_many);
744 * smp_call_function(): Run a function on all other CPUs.
745 * @func: The function to run. This must be fast and non-blocking.
746 * @info: An arbitrary pointer to pass to the function.
747 * @wait: If true, wait (atomically) until function has completed
752 * If @wait is true, then returns once @func has returned; otherwise
753 * it returns just before the target cpu calls @func.
755 * You must not call this function with disabled interrupts or from a
756 * hardware interrupt handler or from a bottom half handler.
758 void smp_call_function(smp_call_func_t func, void *info, int wait)
761 smp_call_function_many(cpu_online_mask, func, info, wait);
764 EXPORT_SYMBOL(smp_call_function);
766 /* Setup configured maximum number of CPUs to activate */
767 unsigned int setup_max_cpus = NR_CPUS;
768 EXPORT_SYMBOL(setup_max_cpus);
772 * Setup routine for controlling SMP activation
774 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
775 * activation entirely (the MPS table probe still happens, though).
777 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
778 * greater than 0, limits the maximum number of CPUs activated in
782 void __weak arch_disable_smp_support(void) { }
784 static int __init nosmp(char *str)
787 arch_disable_smp_support();
792 early_param("nosmp", nosmp);
794 /* this is hard limit */
795 static int __init nrcpus(char *str)
799 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
800 nr_cpu_ids = nr_cpus;
805 early_param("nr_cpus", nrcpus);
807 static int __init maxcpus(char *str)
809 get_option(&str, &setup_max_cpus);
810 if (setup_max_cpus == 0)
811 arch_disable_smp_support();
816 early_param("maxcpus", maxcpus);
818 /* Setup number of possible processor ids */
819 unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
820 EXPORT_SYMBOL(nr_cpu_ids);
822 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
823 void __init setup_nr_cpu_ids(void)
825 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
828 /* Called by boot processor to activate the rest. */
829 void __init smp_init(void)
831 int num_nodes, num_cpus;
834 cpuhp_threads_init();
836 pr_info("Bringing up secondary CPUs ...\n");
838 bringup_nonboot_cpus(setup_max_cpus);
840 num_nodes = num_online_nodes();
841 num_cpus = num_online_cpus();
842 pr_info("Brought up %d node%s, %d CPU%s\n",
843 num_nodes, (num_nodes > 1 ? "s" : ""),
844 num_cpus, (num_cpus > 1 ? "s" : ""));
846 /* Any cleanup work */
847 smp_cpus_done(setup_max_cpus);
851 * on_each_cpu_cond(): Call a function on each processor for which
852 * the supplied function cond_func returns true, optionally waiting
853 * for all the required CPUs to finish. This may include the local
855 * @cond_func: A callback function that is passed a cpu id and
856 * the info parameter. The function is called
857 * with preemption disabled. The function should
858 * return a blooean value indicating whether to IPI
860 * @func: The function to run on all applicable CPUs.
861 * This must be fast and non-blocking.
862 * @info: An arbitrary pointer to pass to both functions.
863 * @wait: If true, wait (atomically) until function has
864 * completed on other CPUs.
866 * Preemption is disabled to protect against CPUs going offline but not online.
867 * CPUs going online during the call will not be seen or sent an IPI.
869 * You must not call this function with disabled interrupts or
870 * from a hardware interrupt handler or from a bottom half handler.
872 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
873 void *info, bool wait, const struct cpumask *mask)
875 unsigned int scf_flags = SCF_RUN_LOCAL;
878 scf_flags |= SCF_WAIT;
881 smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
884 EXPORT_SYMBOL(on_each_cpu_cond_mask);
886 static void do_nothing(void *unused)
891 * kick_all_cpus_sync - Force all cpus out of idle
893 * Used to synchronize the update of pm_idle function pointer. It's
894 * called after the pointer is updated and returns after the dummy
895 * callback function has been executed on all cpus. The execution of
896 * the function can only happen on the remote cpus after they have
897 * left the idle function which had been called via pm_idle function
898 * pointer. So it's guaranteed that nothing uses the previous pointer
901 void kick_all_cpus_sync(void)
903 /* Make sure the change is visible before we kick the cpus */
905 smp_call_function(do_nothing, NULL, 1);
907 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
910 * wake_up_all_idle_cpus - break all cpus out of idle
911 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
912 * including idle polling cpus, for non-idle cpus, we will do nothing
915 void wake_up_all_idle_cpus(void)
920 for_each_online_cpu(cpu) {
921 if (cpu == smp_processor_id())
924 wake_up_if_idle(cpu);
928 EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
931 * smp_call_on_cpu - Call a function on a specific cpu
933 * Used to call a function on a specific cpu and wait for it to return.
934 * Optionally make sure the call is done on a specified physical cpu via vcpu
935 * pinning in order to support virtualized environments.
937 struct smp_call_on_cpu_struct {
938 struct work_struct work;
939 struct completion done;
946 static void smp_call_on_cpu_callback(struct work_struct *work)
948 struct smp_call_on_cpu_struct *sscs;
950 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
952 hypervisor_pin_vcpu(sscs->cpu);
953 sscs->ret = sscs->func(sscs->data);
955 hypervisor_pin_vcpu(-1);
957 complete(&sscs->done);
960 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
962 struct smp_call_on_cpu_struct sscs = {
963 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
966 .cpu = phys ? cpu : -1,
969 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
971 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
974 queue_work_on(cpu, system_wq, &sscs.work);
975 wait_for_completion(&sscs.done);
979 EXPORT_SYMBOL_GPL(smp_call_on_cpu);