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/gfp.h>
18 #include <linux/smp.h>
19 #include <linux/cpu.h>
20 #include <linux/sched.h>
21 #include <linux/sched/idle.h>
22 #include <linux/hypervisor.h>
25 #include "sched/smp.h"
27 #define CSD_TYPE(_csd) ((_csd)->flags & CSD_FLAG_TYPE_MASK)
29 struct call_function_data {
30 call_single_data_t __percpu *csd;
31 cpumask_var_t cpumask;
32 cpumask_var_t cpumask_ipi;
35 static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
37 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
39 static void flush_smp_call_function_queue(bool warn_cpu_offline);
41 int smpcfd_prepare_cpu(unsigned int cpu)
43 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
45 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
48 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
50 free_cpumask_var(cfd->cpumask);
53 cfd->csd = alloc_percpu(call_single_data_t);
55 free_cpumask_var(cfd->cpumask);
56 free_cpumask_var(cfd->cpumask_ipi);
63 int smpcfd_dead_cpu(unsigned int cpu)
65 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
67 free_cpumask_var(cfd->cpumask);
68 free_cpumask_var(cfd->cpumask_ipi);
69 free_percpu(cfd->csd);
73 int smpcfd_dying_cpu(unsigned int cpu)
76 * The IPIs for the smp-call-function callbacks queued by other
77 * CPUs might arrive late, either due to hardware latencies or
78 * because this CPU disabled interrupts (inside stop-machine)
79 * before the IPIs were sent. So flush out any pending callbacks
80 * explicitly (without waiting for the IPIs to arrive), to
81 * ensure that the outgoing CPU doesn't go offline with work
84 flush_smp_call_function_queue(false);
89 void __init call_function_init(void)
93 for_each_possible_cpu(i)
94 init_llist_head(&per_cpu(call_single_queue, i));
96 smpcfd_prepare_cpu(smp_processor_id());
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 __always_inline void csd_lock_wait(call_single_data_t *csd)
108 smp_cond_load_acquire(&csd->flags, !(VAL & CSD_FLAG_LOCK));
111 static __always_inline void csd_lock(call_single_data_t *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_t structure:
124 static __always_inline void csd_unlock(call_single_data_t *csd)
126 WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
129 * ensure we're all done before releasing data:
131 smp_store_release(&csd->flags, 0);
134 static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
136 void __smp_call_single_queue(int cpu, struct llist_node *node)
139 * The list addition should be visible before sending the IPI
140 * handler locks the list to pull the entry off it because of
141 * normal cache coherency rules implied by spinlocks.
143 * If IPIs can go out of order to the cache coherency protocol
144 * in an architecture, sufficient synchronisation should be added
145 * to arch code to make it appear to obey cache coherency WRT
146 * locking and barrier primitives. Generic code isn't really
147 * equipped to do the right thing...
149 if (llist_add(node, &per_cpu(call_single_queue, cpu)))
150 send_call_function_single_ipi(cpu);
154 * Insert a previously allocated call_single_data_t element
155 * for execution on the given CPU. data must already have
156 * ->func, ->info, and ->flags set.
158 static int generic_exec_single(int cpu, call_single_data_t *csd)
160 if (cpu == smp_processor_id()) {
161 smp_call_func_t func = csd->func;
162 void *info = csd->info;
166 * We can unlock early even for the synchronous on-stack case,
167 * since we're doing this from the same CPU..
170 local_irq_save(flags);
172 local_irq_restore(flags);
176 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
181 __smp_call_single_queue(cpu, &csd->llist);
187 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
189 * Invoked by arch to handle an IPI for call function single.
190 * Must be called with interrupts disabled.
192 void generic_smp_call_function_single_interrupt(void)
194 flush_smp_call_function_queue(true);
198 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
200 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
201 * offline CPU. Skip this check if set to 'false'.
203 * Flush any pending smp-call-function callbacks queued on this CPU. This is
204 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
205 * to ensure that all pending IPI callbacks are run before it goes completely
208 * Loop through the call_single_queue and run all the queued callbacks.
209 * Must be called with interrupts disabled.
211 static void flush_smp_call_function_queue(bool warn_cpu_offline)
213 call_single_data_t *csd, *csd_next;
214 struct llist_node *entry, *prev;
215 struct llist_head *head;
218 lockdep_assert_irqs_disabled();
220 head = this_cpu_ptr(&call_single_queue);
221 entry = llist_del_all(head);
222 entry = llist_reverse_order(entry);
224 /* There shouldn't be any pending callbacks on an offline CPU. */
225 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
226 !warned && !llist_empty(head))) {
228 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
231 * We don't have to use the _safe() variant here
232 * because we are not invoking the IPI handlers yet.
234 llist_for_each_entry(csd, entry, llist) {
235 switch (CSD_TYPE(csd)) {
238 case CSD_TYPE_IRQ_WORK:
239 pr_warn("IPI callback %pS sent to offline CPU\n",
244 pr_warn("IPI task-wakeup sent to offline CPU\n");
248 pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
256 * First; run all SYNC callbacks, people are waiting for us.
259 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
260 /* Do we wait until *after* callback? */
261 if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
262 smp_call_func_t func = csd->func;
263 void *info = csd->info;
266 prev->next = &csd_next->llist;
268 entry = &csd_next->llist;
282 * Second; run all !SYNC callbacks.
285 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
286 int type = CSD_TYPE(csd);
288 if (type != CSD_TYPE_TTWU) {
290 prev->next = &csd_next->llist;
292 entry = &csd_next->llist;
295 if (type == CSD_TYPE_ASYNC) {
296 smp_call_func_t func = csd->func;
297 void *info = csd->info;
301 } else if (type == CSD_TYPE_IRQ_WORK) {
302 irq_work_single(csd);
311 * Third; only CSD_TYPE_TTWU is left, issue those.
314 sched_ttwu_pending(entry);
317 void flush_smp_call_function_from_idle(void)
321 if (llist_empty(this_cpu_ptr(&call_single_queue)))
324 local_irq_save(flags);
325 flush_smp_call_function_queue(true);
326 local_irq_restore(flags);
330 * smp_call_function_single - Run a function on a specific CPU
331 * @func: The function to run. This must be fast and non-blocking.
332 * @info: An arbitrary pointer to pass to the function.
333 * @wait: If true, wait until function has completed on other CPUs.
335 * Returns 0 on success, else a negative status code.
337 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
340 call_single_data_t *csd;
341 call_single_data_t csd_stack = {
342 .flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC,
348 * prevent preemption and reschedule on another processor,
349 * as well as CPU removal
351 this_cpu = get_cpu();
354 * Can deadlock when called with interrupts disabled.
355 * We allow cpu's that are not yet online though, as no one else can
356 * send smp call function interrupt to this cpu and as such deadlocks
359 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
360 && !oops_in_progress);
363 * When @wait we can deadlock when we interrupt between llist_add() and
364 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
365 * csd_lock() on because the interrupt context uses the same csd
368 WARN_ON_ONCE(!in_task());
372 csd = this_cpu_ptr(&csd_data);
379 err = generic_exec_single(cpu, csd);
388 EXPORT_SYMBOL(smp_call_function_single);
391 * smp_call_function_single_async(): Run an asynchronous function on a
393 * @cpu: The CPU to run on.
394 * @csd: Pre-allocated and setup data structure
396 * Like smp_call_function_single(), but the call is asynchonous and
397 * can thus be done from contexts with disabled interrupts.
399 * The caller passes his own pre-allocated data structure
400 * (ie: embedded in an object) and is responsible for synchronizing it
401 * such that the IPIs performed on the @csd are strictly serialized.
403 * If the function is called with one csd which has not yet been
404 * processed by previous call to smp_call_function_single_async(), the
405 * function will return immediately with -EBUSY showing that the csd
406 * object is still in progress.
408 * NOTE: Be careful, there is unfortunately no current debugging facility to
409 * validate the correctness of this serialization.
411 int smp_call_function_single_async(int cpu, call_single_data_t *csd)
417 if (csd->flags & CSD_FLAG_LOCK) {
422 csd->flags = CSD_FLAG_LOCK;
425 err = generic_exec_single(cpu, csd);
432 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
435 * smp_call_function_any - Run a function on any of the given cpus
436 * @mask: The mask of cpus it can run on.
437 * @func: The function to run. This must be fast and non-blocking.
438 * @info: An arbitrary pointer to pass to the function.
439 * @wait: If true, wait until function has completed.
441 * Returns 0 on success, else a negative status code (if no cpus were online).
443 * Selection preference:
444 * 1) current cpu if in @mask
445 * 2) any cpu of current node if in @mask
446 * 3) any other online cpu in @mask
448 int smp_call_function_any(const struct cpumask *mask,
449 smp_call_func_t func, void *info, int wait)
452 const struct cpumask *nodemask;
455 /* Try for same CPU (cheapest) */
457 if (cpumask_test_cpu(cpu, mask))
460 /* Try for same node. */
461 nodemask = cpumask_of_node(cpu_to_node(cpu));
462 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
463 cpu = cpumask_next_and(cpu, nodemask, mask)) {
468 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
469 cpu = cpumask_any_and(mask, cpu_online_mask);
471 ret = smp_call_function_single(cpu, func, info, wait);
475 EXPORT_SYMBOL_GPL(smp_call_function_any);
477 static void smp_call_function_many_cond(const struct cpumask *mask,
478 smp_call_func_t func, void *info,
479 bool wait, smp_cond_func_t cond_func)
481 struct call_function_data *cfd;
482 int cpu, next_cpu, this_cpu = smp_processor_id();
485 * Can deadlock when called with interrupts disabled.
486 * We allow cpu's that are not yet online though, as no one else can
487 * send smp call function interrupt to this cpu and as such deadlocks
490 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
491 && !oops_in_progress && !early_boot_irqs_disabled);
494 * When @wait we can deadlock when we interrupt between llist_add() and
495 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
496 * csd_lock() on because the interrupt context uses the same csd
499 WARN_ON_ONCE(!in_task());
501 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
502 cpu = cpumask_first_and(mask, cpu_online_mask);
504 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
506 /* No online cpus? We're done. */
507 if (cpu >= nr_cpu_ids)
510 /* Do we have another CPU which isn't us? */
511 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
512 if (next_cpu == this_cpu)
513 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
515 /* Fastpath: do that cpu by itself. */
516 if (next_cpu >= nr_cpu_ids) {
517 if (!cond_func || cond_func(cpu, info))
518 smp_call_function_single(cpu, func, info, wait);
522 cfd = this_cpu_ptr(&cfd_data);
524 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
525 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
527 /* Some callers race with other cpus changing the passed mask */
528 if (unlikely(!cpumask_weight(cfd->cpumask)))
531 cpumask_clear(cfd->cpumask_ipi);
532 for_each_cpu(cpu, cfd->cpumask) {
533 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
535 if (cond_func && !cond_func(cpu, info))
540 csd->flags |= CSD_TYPE_SYNC;
543 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
544 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
547 /* Send a message to all CPUs in the map */
548 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
551 for_each_cpu(cpu, cfd->cpumask) {
552 call_single_data_t *csd;
554 csd = per_cpu_ptr(cfd->csd, cpu);
561 * smp_call_function_many(): Run a function on a set of other CPUs.
562 * @mask: The set of cpus to run on (only runs on online subset).
563 * @func: The function to run. This must be fast and non-blocking.
564 * @info: An arbitrary pointer to pass to the function.
565 * @wait: If true, wait (atomically) until function has completed
568 * If @wait is true, then returns once @func has returned.
570 * You must not call this function with disabled interrupts or from a
571 * hardware interrupt handler or from a bottom half handler. Preemption
572 * must be disabled when calling this function.
574 void smp_call_function_many(const struct cpumask *mask,
575 smp_call_func_t func, void *info, bool wait)
577 smp_call_function_many_cond(mask, func, info, wait, NULL);
579 EXPORT_SYMBOL(smp_call_function_many);
582 * smp_call_function(): Run a function on all other CPUs.
583 * @func: The function to run. This must be fast and non-blocking.
584 * @info: An arbitrary pointer to pass to the function.
585 * @wait: If true, wait (atomically) until function has completed
590 * If @wait is true, then returns once @func has returned; otherwise
591 * it returns just before the target cpu calls @func.
593 * You must not call this function with disabled interrupts or from a
594 * hardware interrupt handler or from a bottom half handler.
596 void smp_call_function(smp_call_func_t func, void *info, int wait)
599 smp_call_function_many(cpu_online_mask, func, info, wait);
602 EXPORT_SYMBOL(smp_call_function);
604 /* Setup configured maximum number of CPUs to activate */
605 unsigned int setup_max_cpus = NR_CPUS;
606 EXPORT_SYMBOL(setup_max_cpus);
610 * Setup routine for controlling SMP activation
612 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
613 * activation entirely (the MPS table probe still happens, though).
615 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
616 * greater than 0, limits the maximum number of CPUs activated in
620 void __weak arch_disable_smp_support(void) { }
622 static int __init nosmp(char *str)
625 arch_disable_smp_support();
630 early_param("nosmp", nosmp);
632 /* this is hard limit */
633 static int __init nrcpus(char *str)
637 if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
638 nr_cpu_ids = nr_cpus;
643 early_param("nr_cpus", nrcpus);
645 static int __init maxcpus(char *str)
647 get_option(&str, &setup_max_cpus);
648 if (setup_max_cpus == 0)
649 arch_disable_smp_support();
654 early_param("maxcpus", maxcpus);
656 /* Setup number of possible processor ids */
657 unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
658 EXPORT_SYMBOL(nr_cpu_ids);
660 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
661 void __init setup_nr_cpu_ids(void)
663 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
666 /* Called by boot processor to activate the rest. */
667 void __init smp_init(void)
669 int num_nodes, num_cpus;
672 cpuhp_threads_init();
674 pr_info("Bringing up secondary CPUs ...\n");
676 bringup_nonboot_cpus(setup_max_cpus);
678 num_nodes = num_online_nodes();
679 num_cpus = num_online_cpus();
680 pr_info("Brought up %d node%s, %d CPU%s\n",
681 num_nodes, (num_nodes > 1 ? "s" : ""),
682 num_cpus, (num_cpus > 1 ? "s" : ""));
684 /* Any cleanup work */
685 smp_cpus_done(setup_max_cpus);
689 * Call a function on all processors. May be used during early boot while
690 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
691 * of local_irq_disable/enable().
693 void on_each_cpu(smp_call_func_t func, void *info, int wait)
698 smp_call_function(func, info, wait);
699 local_irq_save(flags);
701 local_irq_restore(flags);
704 EXPORT_SYMBOL(on_each_cpu);
707 * on_each_cpu_mask(): Run a function on processors specified by
708 * cpumask, which may include the local processor.
709 * @mask: The set of cpus to run on (only runs on online subset).
710 * @func: The function to run. This must be fast and non-blocking.
711 * @info: An arbitrary pointer to pass to the function.
712 * @wait: If true, wait (atomically) until function has completed
715 * If @wait is true, then returns once @func has returned.
717 * You must not call this function with disabled interrupts or from a
718 * hardware interrupt handler or from a bottom half handler. The
719 * exception is that it may be used during early boot while
720 * early_boot_irqs_disabled is set.
722 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
723 void *info, bool wait)
727 smp_call_function_many(mask, func, info, wait);
728 if (cpumask_test_cpu(cpu, mask)) {
730 local_irq_save(flags);
732 local_irq_restore(flags);
736 EXPORT_SYMBOL(on_each_cpu_mask);
739 * on_each_cpu_cond(): Call a function on each processor for which
740 * the supplied function cond_func returns true, optionally waiting
741 * for all the required CPUs to finish. This may include the local
743 * @cond_func: A callback function that is passed a cpu id and
744 * the the info parameter. The function is called
745 * with preemption disabled. The function should
746 * return a blooean value indicating whether to IPI
748 * @func: The function to run on all applicable CPUs.
749 * This must be fast and non-blocking.
750 * @info: An arbitrary pointer to pass to both functions.
751 * @wait: If true, wait (atomically) until function has
752 * completed on other CPUs.
754 * Preemption is disabled to protect against CPUs going offline but not online.
755 * CPUs going online during the call will not be seen or sent an IPI.
757 * You must not call this function with disabled interrupts or
758 * from a hardware interrupt handler or from a bottom half handler.
760 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
761 void *info, bool wait, const struct cpumask *mask)
765 smp_call_function_many_cond(mask, func, info, wait, cond_func);
766 if (cpumask_test_cpu(cpu, mask) && cond_func(cpu, info)) {
769 local_irq_save(flags);
771 local_irq_restore(flags);
775 EXPORT_SYMBOL(on_each_cpu_cond_mask);
777 void on_each_cpu_cond(smp_cond_func_t cond_func, smp_call_func_t func,
778 void *info, bool wait)
780 on_each_cpu_cond_mask(cond_func, func, info, wait, cpu_online_mask);
782 EXPORT_SYMBOL(on_each_cpu_cond);
784 static void do_nothing(void *unused)
789 * kick_all_cpus_sync - Force all cpus out of idle
791 * Used to synchronize the update of pm_idle function pointer. It's
792 * called after the pointer is updated and returns after the dummy
793 * callback function has been executed on all cpus. The execution of
794 * the function can only happen on the remote cpus after they have
795 * left the idle function which had been called via pm_idle function
796 * pointer. So it's guaranteed that nothing uses the previous pointer
799 void kick_all_cpus_sync(void)
801 /* Make sure the change is visible before we kick the cpus */
803 smp_call_function(do_nothing, NULL, 1);
805 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
808 * wake_up_all_idle_cpus - break all cpus out of idle
809 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
810 * including idle polling cpus, for non-idle cpus, we will do nothing
813 void wake_up_all_idle_cpus(void)
818 for_each_online_cpu(cpu) {
819 if (cpu == smp_processor_id())
822 wake_up_if_idle(cpu);
826 EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
829 * smp_call_on_cpu - Call a function on a specific cpu
831 * Used to call a function on a specific cpu and wait for it to return.
832 * Optionally make sure the call is done on a specified physical cpu via vcpu
833 * pinning in order to support virtualized environments.
835 struct smp_call_on_cpu_struct {
836 struct work_struct work;
837 struct completion done;
844 static void smp_call_on_cpu_callback(struct work_struct *work)
846 struct smp_call_on_cpu_struct *sscs;
848 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
850 hypervisor_pin_vcpu(sscs->cpu);
851 sscs->ret = sscs->func(sscs->data);
853 hypervisor_pin_vcpu(-1);
855 complete(&sscs->done);
858 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
860 struct smp_call_on_cpu_struct sscs = {
861 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
864 .cpu = phys ? cpu : -1,
867 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
869 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
872 queue_work_on(cpu, system_wq, &sscs.work);
873 wait_for_completion(&sscs.done);
877 EXPORT_SYMBOL_GPL(smp_call_on_cpu);