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>
27 #include <linux/jump_label.h>
29 #include <trace/events/ipi.h>
32 #include "sched/smp.h"
34 #define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
36 struct call_function_data {
37 call_single_data_t __percpu *csd;
38 cpumask_var_t cpumask;
39 cpumask_var_t cpumask_ipi;
42 static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
44 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
46 static void __flush_smp_call_function_queue(bool warn_cpu_offline);
48 int smpcfd_prepare_cpu(unsigned int cpu)
50 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
52 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
55 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
57 free_cpumask_var(cfd->cpumask);
60 cfd->csd = alloc_percpu(call_single_data_t);
62 free_cpumask_var(cfd->cpumask);
63 free_cpumask_var(cfd->cpumask_ipi);
70 int smpcfd_dead_cpu(unsigned int cpu)
72 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
74 free_cpumask_var(cfd->cpumask);
75 free_cpumask_var(cfd->cpumask_ipi);
76 free_percpu(cfd->csd);
80 int smpcfd_dying_cpu(unsigned int cpu)
83 * The IPIs for the smp-call-function callbacks queued by other
84 * CPUs might arrive late, either due to hardware latencies or
85 * because this CPU disabled interrupts (inside stop-machine)
86 * before the IPIs were sent. So flush out any pending callbacks
87 * explicitly (without waiting for the IPIs to arrive), to
88 * ensure that the outgoing CPU doesn't go offline with work
91 __flush_smp_call_function_queue(false);
96 void __init call_function_init(void)
100 for_each_possible_cpu(i)
101 init_llist_head(&per_cpu(call_single_queue, i));
103 smpcfd_prepare_cpu(smp_processor_id());
106 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
108 static DEFINE_STATIC_KEY_MAYBE(CONFIG_CSD_LOCK_WAIT_DEBUG_DEFAULT, csdlock_debug_enabled);
111 * Parse the csdlock_debug= kernel boot parameter.
113 * If you need to restore the old "ext" value that once provided
114 * additional debugging information, reapply the following commits:
116 * de7b09ef658d ("locking/csd_lock: Prepare more CSD lock debugging")
117 * a5aabace5fb8 ("locking/csd_lock: Add more data to CSD lock debugging")
119 static int __init csdlock_debug(char *str)
122 unsigned int val = 0;
124 ret = get_option(&str, &val);
127 static_branch_enable(&csdlock_debug_enabled);
129 static_branch_disable(&csdlock_debug_enabled);
134 __setup("csdlock_debug=", csdlock_debug);
136 static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
137 static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
138 static DEFINE_PER_CPU(void *, cur_csd_info);
140 static ulong csd_lock_timeout = 5000; /* CSD lock timeout in milliseconds. */
141 module_param(csd_lock_timeout, ulong, 0444);
143 static atomic_t csd_bug_count = ATOMIC_INIT(0);
145 /* Record current CSD work for current CPU, NULL to erase. */
146 static void __csd_lock_record(struct __call_single_data *csd)
149 smp_mb(); /* NULL cur_csd after unlock. */
150 __this_cpu_write(cur_csd, NULL);
153 __this_cpu_write(cur_csd_func, csd->func);
154 __this_cpu_write(cur_csd_info, csd->info);
155 smp_wmb(); /* func and info before csd. */
156 __this_cpu_write(cur_csd, csd);
157 smp_mb(); /* Update cur_csd before function call. */
158 /* Or before unlock, as the case may be. */
161 static __always_inline void csd_lock_record(struct __call_single_data *csd)
163 if (static_branch_unlikely(&csdlock_debug_enabled))
164 __csd_lock_record(csd);
167 static int csd_lock_wait_getcpu(struct __call_single_data *csd)
169 unsigned int csd_type;
171 csd_type = CSD_TYPE(csd);
172 if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
173 return csd->node.dst; /* Other CSD_TYPE_ values might not have ->dst. */
178 * Complain if too much time spent waiting. Note that only
179 * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU,
180 * so waiting on other types gets much less information.
182 static bool csd_lock_wait_toolong(struct __call_single_data *csd, u64 ts0, u64 *ts1, int *bug_id)
188 call_single_data_t *cpu_cur_csd;
189 unsigned int flags = READ_ONCE(csd->node.u_flags);
190 unsigned long long csd_lock_timeout_ns = csd_lock_timeout * NSEC_PER_MSEC;
192 if (!(flags & CSD_FLAG_LOCK)) {
193 if (!unlikely(*bug_id))
195 cpu = csd_lock_wait_getcpu(csd);
196 pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
197 *bug_id, raw_smp_processor_id(), cpu);
202 ts_delta = ts2 - *ts1;
203 if (likely(ts_delta <= csd_lock_timeout_ns || csd_lock_timeout_ns == 0))
206 firsttime = !*bug_id;
208 *bug_id = atomic_inc_return(&csd_bug_count);
209 cpu = csd_lock_wait_getcpu(csd);
210 if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
214 cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); /* Before func and info. */
215 pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %llu ns for CPU#%02d %pS(%ps).\n",
216 firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), ts2 - ts0,
217 cpu, csd->func, csd->info);
218 if (cpu_cur_csd && csd != cpu_cur_csd) {
219 pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
220 *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
221 READ_ONCE(per_cpu(cur_csd_info, cpux)));
223 pr_alert("\tcsd: CSD lock (#%d) %s.\n",
224 *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
229 pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
230 arch_send_call_function_single_ipi(cpu);
240 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
242 * For non-synchronous ipi calls the csd can still be in use by the
243 * previous function call. For multi-cpu calls its even more interesting
244 * as we'll have to ensure no other cpu is observing our csd.
246 static void __csd_lock_wait(struct __call_single_data *csd)
251 ts1 = ts0 = sched_clock();
253 if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id))
257 smp_acquire__after_ctrl_dep();
260 static __always_inline void csd_lock_wait(struct __call_single_data *csd)
262 if (static_branch_unlikely(&csdlock_debug_enabled)) {
263 __csd_lock_wait(csd);
267 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
270 static void csd_lock_record(struct __call_single_data *csd)
274 static __always_inline void csd_lock_wait(struct __call_single_data *csd)
276 smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
280 static __always_inline void csd_lock(struct __call_single_data *csd)
283 csd->node.u_flags |= CSD_FLAG_LOCK;
286 * prevent CPU from reordering the above assignment
287 * to ->flags with any subsequent assignments to other
288 * fields of the specified call_single_data_t structure:
293 static __always_inline void csd_unlock(struct __call_single_data *csd)
295 WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
298 * ensure we're all done before releasing data:
300 smp_store_release(&csd->node.u_flags, 0);
303 static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
305 void __smp_call_single_queue(int cpu, struct llist_node *node)
308 * The list addition should be visible before sending the IPI
309 * handler locks the list to pull the entry off it because of
310 * normal cache coherency rules implied by spinlocks.
312 * If IPIs can go out of order to the cache coherency protocol
313 * in an architecture, sufficient synchronisation should be added
314 * to arch code to make it appear to obey cache coherency WRT
315 * locking and barrier primitives. Generic code isn't really
316 * equipped to do the right thing...
318 if (llist_add(node, &per_cpu(call_single_queue, cpu)))
319 send_call_function_single_ipi(cpu);
323 * Insert a previously allocated call_single_data_t element
324 * for execution on the given CPU. data must already have
325 * ->func, ->info, and ->flags set.
327 static int generic_exec_single(int cpu, struct __call_single_data *csd)
329 if (cpu == smp_processor_id()) {
330 smp_call_func_t func = csd->func;
331 void *info = csd->info;
335 * We can unlock early even for the synchronous on-stack case,
336 * since we're doing this from the same CPU..
338 csd_lock_record(csd);
340 local_irq_save(flags);
342 csd_lock_record(NULL);
343 local_irq_restore(flags);
347 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
352 __smp_call_single_queue(cpu, &csd->node.llist);
358 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
360 * Invoked by arch to handle an IPI for call function single.
361 * Must be called with interrupts disabled.
363 void generic_smp_call_function_single_interrupt(void)
365 __flush_smp_call_function_queue(true);
369 * __flush_smp_call_function_queue - Flush pending smp-call-function callbacks
371 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
372 * offline CPU. Skip this check if set to 'false'.
374 * Flush any pending smp-call-function callbacks queued on this CPU. This is
375 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
376 * to ensure that all pending IPI callbacks are run before it goes completely
379 * Loop through the call_single_queue and run all the queued callbacks.
380 * Must be called with interrupts disabled.
382 static void __flush_smp_call_function_queue(bool warn_cpu_offline)
384 call_single_data_t *csd, *csd_next;
385 struct llist_node *entry, *prev;
386 struct llist_head *head;
389 lockdep_assert_irqs_disabled();
391 head = this_cpu_ptr(&call_single_queue);
392 entry = llist_del_all(head);
393 entry = llist_reverse_order(entry);
395 /* There shouldn't be any pending callbacks on an offline CPU. */
396 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
397 !warned && entry != NULL)) {
399 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
402 * We don't have to use the _safe() variant here
403 * because we are not invoking the IPI handlers yet.
405 llist_for_each_entry(csd, entry, node.llist) {
406 switch (CSD_TYPE(csd)) {
409 case CSD_TYPE_IRQ_WORK:
410 pr_warn("IPI callback %pS sent to offline CPU\n",
415 pr_warn("IPI task-wakeup sent to offline CPU\n");
419 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
427 * First; run all SYNC callbacks, people are waiting for us.
430 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
431 /* Do we wait until *after* callback? */
432 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
433 smp_call_func_t func = csd->func;
434 void *info = csd->info;
437 prev->next = &csd_next->node.llist;
439 entry = &csd_next->node.llist;
442 csd_lock_record(csd);
445 csd_lock_record(NULL);
447 prev = &csd->node.llist;
455 * Second; run all !SYNC callbacks.
458 llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
459 int type = CSD_TYPE(csd);
461 if (type != CSD_TYPE_TTWU) {
463 prev->next = &csd_next->node.llist;
465 entry = &csd_next->node.llist;
468 if (type == CSD_TYPE_ASYNC) {
469 smp_call_func_t func = csd->func;
470 void *info = csd->info;
472 csd_lock_record(csd);
475 csd_lock_record(NULL);
476 } else if (type == CSD_TYPE_IRQ_WORK) {
477 irq_work_single(csd);
481 prev = &csd->node.llist;
486 * Third; only CSD_TYPE_TTWU is left, issue those.
489 sched_ttwu_pending(entry);
494 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
495 * from task context (idle, migration thread)
497 * When TIF_POLLING_NRFLAG is supported and a CPU is in idle and has it
498 * set, then remote CPUs can avoid sending IPIs and wake the idle CPU by
499 * setting TIF_NEED_RESCHED. The idle task on the woken up CPU has to
500 * handle queued SMP function calls before scheduling.
502 * The migration thread has to ensure that an eventually pending wakeup has
503 * been handled before it migrates a task.
505 void flush_smp_call_function_queue(void)
507 unsigned int was_pending;
510 if (llist_empty(this_cpu_ptr(&call_single_queue)))
513 local_irq_save(flags);
514 /* Get the already pending soft interrupts for RT enabled kernels */
515 was_pending = local_softirq_pending();
516 __flush_smp_call_function_queue(true);
517 if (local_softirq_pending())
518 do_softirq_post_smp_call_flush(was_pending);
520 local_irq_restore(flags);
524 * smp_call_function_single - Run a function on a specific CPU
525 * @func: The function to run. This must be fast and non-blocking.
526 * @info: An arbitrary pointer to pass to the function.
527 * @wait: If true, wait until function has completed on other CPUs.
529 * Returns 0 on success, else a negative status code.
531 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
534 call_single_data_t *csd;
535 call_single_data_t csd_stack = {
536 .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
542 * prevent preemption and reschedule on another processor,
543 * as well as CPU removal
545 this_cpu = get_cpu();
548 * Can deadlock when called with interrupts disabled.
549 * We allow cpu's that are not yet online though, as no one else can
550 * send smp call function interrupt to this cpu and as such deadlocks
553 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
554 && !oops_in_progress);
557 * When @wait we can deadlock when we interrupt between llist_add() and
558 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
559 * csd_lock() on because the interrupt context uses the same csd
562 WARN_ON_ONCE(!in_task());
566 csd = this_cpu_ptr(&csd_data);
572 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
573 csd->node.src = smp_processor_id();
577 err = generic_exec_single(cpu, csd);
586 EXPORT_SYMBOL(smp_call_function_single);
589 * smp_call_function_single_async() - Run an asynchronous function on a
591 * @cpu: The CPU to run on.
592 * @csd: Pre-allocated and setup data structure
594 * Like smp_call_function_single(), but the call is asynchonous and
595 * can thus be done from contexts with disabled interrupts.
597 * The caller passes his own pre-allocated data structure
598 * (ie: embedded in an object) and is responsible for synchronizing it
599 * such that the IPIs performed on the @csd are strictly serialized.
601 * If the function is called with one csd which has not yet been
602 * processed by previous call to smp_call_function_single_async(), the
603 * function will return immediately with -EBUSY showing that the csd
604 * object is still in progress.
606 * NOTE: Be careful, there is unfortunately no current debugging facility to
607 * validate the correctness of this serialization.
609 * Return: %0 on success or negative errno value on error
611 int smp_call_function_single_async(int cpu, struct __call_single_data *csd)
617 if (csd->node.u_flags & CSD_FLAG_LOCK) {
622 csd->node.u_flags = CSD_FLAG_LOCK;
625 err = generic_exec_single(cpu, csd);
632 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
635 * smp_call_function_any - Run a function on any of the given cpus
636 * @mask: The mask of cpus it can run on.
637 * @func: The function to run. This must be fast and non-blocking.
638 * @info: An arbitrary pointer to pass to the function.
639 * @wait: If true, wait until function has completed.
641 * Returns 0 on success, else a negative status code (if no cpus were online).
643 * Selection preference:
644 * 1) current cpu if in @mask
645 * 2) any cpu of current node if in @mask
646 * 3) any other online cpu in @mask
648 int smp_call_function_any(const struct cpumask *mask,
649 smp_call_func_t func, void *info, int wait)
652 const struct cpumask *nodemask;
655 /* Try for same CPU (cheapest) */
657 if (cpumask_test_cpu(cpu, mask))
660 /* Try for same node. */
661 nodemask = cpumask_of_node(cpu_to_node(cpu));
662 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
663 cpu = cpumask_next_and(cpu, nodemask, mask)) {
668 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
669 cpu = cpumask_any_and(mask, cpu_online_mask);
671 ret = smp_call_function_single(cpu, func, info, wait);
675 EXPORT_SYMBOL_GPL(smp_call_function_any);
678 * Flags to be used as scf_flags argument of smp_call_function_many_cond().
680 * %SCF_WAIT: Wait until function execution is completed
681 * %SCF_RUN_LOCAL: Run also locally if local cpu is set in cpumask
683 #define SCF_WAIT (1U << 0)
684 #define SCF_RUN_LOCAL (1U << 1)
686 static void smp_call_function_many_cond(const struct cpumask *mask,
687 smp_call_func_t func, void *info,
688 unsigned int scf_flags,
689 smp_cond_func_t cond_func)
691 int cpu, last_cpu, this_cpu = smp_processor_id();
692 struct call_function_data *cfd;
693 bool wait = scf_flags & SCF_WAIT;
694 bool run_remote = false;
695 bool run_local = false;
698 lockdep_assert_preemption_disabled();
701 * Can deadlock when called with interrupts disabled.
702 * We allow cpu's that are not yet online though, as no one else can
703 * send smp call function interrupt to this cpu and as such deadlocks
706 if (cpu_online(this_cpu) && !oops_in_progress &&
707 !early_boot_irqs_disabled)
708 lockdep_assert_irqs_enabled();
711 * When @wait we can deadlock when we interrupt between llist_add() and
712 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
713 * csd_lock() on because the interrupt context uses the same csd
716 WARN_ON_ONCE(!in_task());
718 /* Check if we need local execution. */
719 if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
722 /* Check if we need remote execution, i.e., any CPU excluding this one. */
723 cpu = cpumask_first_and(mask, cpu_online_mask);
725 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
726 if (cpu < nr_cpu_ids)
730 cfd = this_cpu_ptr(&cfd_data);
731 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
732 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
734 cpumask_clear(cfd->cpumask_ipi);
735 for_each_cpu(cpu, cfd->cpumask) {
736 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
738 if (cond_func && !cond_func(cpu, info))
743 csd->node.u_flags |= CSD_TYPE_SYNC;
746 #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
747 csd->node.src = smp_processor_id();
750 if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) {
751 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
758 * Choose the most efficient way to send an IPI. Note that the
759 * number of CPUs might be zero due to concurrent changes to the
763 send_call_function_single_ipi(last_cpu);
764 else if (likely(nr_cpus > 1))
765 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
768 if (run_local && (!cond_func || cond_func(this_cpu, info))) {
771 local_irq_save(flags);
773 local_irq_restore(flags);
776 if (run_remote && wait) {
777 for_each_cpu(cpu, cfd->cpumask) {
778 call_single_data_t *csd;
780 csd = per_cpu_ptr(cfd->csd, cpu);
787 * smp_call_function_many(): Run a function on a set of CPUs.
788 * @mask: The set of cpus to run on (only runs on online subset).
789 * @func: The function to run. This must be fast and non-blocking.
790 * @info: An arbitrary pointer to pass to the function.
791 * @wait: Bitmask that controls the operation. If %SCF_WAIT is set, wait
792 * (atomically) until function has completed on other CPUs. If
793 * %SCF_RUN_LOCAL is set, the function will also be run locally
794 * if the local CPU is set in the @cpumask.
796 * If @wait is true, then returns once @func has returned.
798 * You must not call this function with disabled interrupts or from a
799 * hardware interrupt handler or from a bottom half handler. Preemption
800 * must be disabled when calling this function.
802 void smp_call_function_many(const struct cpumask *mask,
803 smp_call_func_t func, void *info, bool wait)
805 smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
807 EXPORT_SYMBOL(smp_call_function_many);
810 * smp_call_function(): Run a function on all other CPUs.
811 * @func: The function to run. This must be fast and non-blocking.
812 * @info: An arbitrary pointer to pass to the function.
813 * @wait: If true, wait (atomically) until function has completed
818 * If @wait is true, then returns once @func has returned; otherwise
819 * it returns just before the target cpu calls @func.
821 * You must not call this function with disabled interrupts or from a
822 * hardware interrupt handler or from a bottom half handler.
824 void smp_call_function(smp_call_func_t func, void *info, int wait)
827 smp_call_function_many(cpu_online_mask, func, info, wait);
830 EXPORT_SYMBOL(smp_call_function);
832 /* Setup configured maximum number of CPUs to activate */
833 unsigned int setup_max_cpus = NR_CPUS;
834 EXPORT_SYMBOL(setup_max_cpus);
838 * Setup routine for controlling SMP activation
840 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
841 * activation entirely (the MPS table probe still happens, though).
843 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
844 * greater than 0, limits the maximum number of CPUs activated in
848 void __weak arch_disable_smp_support(void) { }
850 static int __init nosmp(char *str)
853 arch_disable_smp_support();
858 early_param("nosmp", nosmp);
860 /* this is hard limit */
861 static int __init nrcpus(char *str)
865 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
866 set_nr_cpu_ids(nr_cpus);
871 early_param("nr_cpus", nrcpus);
873 static int __init maxcpus(char *str)
875 get_option(&str, &setup_max_cpus);
876 if (setup_max_cpus == 0)
877 arch_disable_smp_support();
882 early_param("maxcpus", maxcpus);
884 #if (NR_CPUS > 1) && !defined(CONFIG_FORCE_NR_CPUS)
885 /* Setup number of possible processor ids */
886 unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
887 EXPORT_SYMBOL(nr_cpu_ids);
890 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
891 void __init setup_nr_cpu_ids(void)
893 set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + 1);
896 /* Called by boot processor to activate the rest. */
897 void __init smp_init(void)
899 int num_nodes, num_cpus;
902 cpuhp_threads_init();
904 pr_info("Bringing up secondary CPUs ...\n");
906 bringup_nonboot_cpus(setup_max_cpus);
908 num_nodes = num_online_nodes();
909 num_cpus = num_online_cpus();
910 pr_info("Brought up %d node%s, %d CPU%s\n",
911 num_nodes, (num_nodes > 1 ? "s" : ""),
912 num_cpus, (num_cpus > 1 ? "s" : ""));
914 /* Any cleanup work */
915 smp_cpus_done(setup_max_cpus);
919 * on_each_cpu_cond(): Call a function on each processor for which
920 * the supplied function cond_func returns true, optionally waiting
921 * for all the required CPUs to finish. This may include the local
923 * @cond_func: A callback function that is passed a cpu id and
924 * the info parameter. The function is called
925 * with preemption disabled. The function should
926 * return a blooean value indicating whether to IPI
928 * @func: The function to run on all applicable CPUs.
929 * This must be fast and non-blocking.
930 * @info: An arbitrary pointer to pass to both functions.
931 * @wait: If true, wait (atomically) until function has
932 * completed on other CPUs.
934 * Preemption is disabled to protect against CPUs going offline but not online.
935 * CPUs going online during the call will not be seen or sent an IPI.
937 * You must not call this function with disabled interrupts or
938 * from a hardware interrupt handler or from a bottom half handler.
940 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
941 void *info, bool wait, const struct cpumask *mask)
943 unsigned int scf_flags = SCF_RUN_LOCAL;
946 scf_flags |= SCF_WAIT;
949 smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
952 EXPORT_SYMBOL(on_each_cpu_cond_mask);
954 static void do_nothing(void *unused)
959 * kick_all_cpus_sync - Force all cpus out of idle
961 * Used to synchronize the update of pm_idle function pointer. It's
962 * called after the pointer is updated and returns after the dummy
963 * callback function has been executed on all cpus. The execution of
964 * the function can only happen on the remote cpus after they have
965 * left the idle function which had been called via pm_idle function
966 * pointer. So it's guaranteed that nothing uses the previous pointer
969 void kick_all_cpus_sync(void)
971 /* Make sure the change is visible before we kick the cpus */
973 smp_call_function(do_nothing, NULL, 1);
975 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
978 * wake_up_all_idle_cpus - break all cpus out of idle
979 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
980 * including idle polling cpus, for non-idle cpus, we will do nothing
983 void wake_up_all_idle_cpus(void)
987 for_each_possible_cpu(cpu) {
989 if (cpu != smp_processor_id() && cpu_online(cpu))
990 wake_up_if_idle(cpu);
994 EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
997 * struct smp_call_on_cpu_struct - Call a function on a specific CPU
998 * @work: &work_struct
999 * @done: &completion to signal
1000 * @func: function to call
1001 * @data: function's data argument
1002 * @ret: return value from @func
1003 * @cpu: target CPU (%-1 for any CPU)
1005 * Used to call a function on a specific cpu and wait for it to return.
1006 * Optionally make sure the call is done on a specified physical cpu via vcpu
1007 * pinning in order to support virtualized environments.
1009 struct smp_call_on_cpu_struct {
1010 struct work_struct work;
1011 struct completion done;
1012 int (*func)(void *);
1018 static void smp_call_on_cpu_callback(struct work_struct *work)
1020 struct smp_call_on_cpu_struct *sscs;
1022 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
1024 hypervisor_pin_vcpu(sscs->cpu);
1025 sscs->ret = sscs->func(sscs->data);
1027 hypervisor_pin_vcpu(-1);
1029 complete(&sscs->done);
1032 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
1034 struct smp_call_on_cpu_struct sscs = {
1035 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
1038 .cpu = phys ? cpu : -1,
1041 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
1043 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
1046 queue_work_on(cpu, system_wq, &sscs.work);
1047 wait_for_completion(&sscs.done);
1051 EXPORT_SYMBOL_GPL(smp_call_on_cpu);