blob: f720e38e880df795b31da4e4ce1a3768fda56fdf [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Jens Axboe3d442232008-06-26 11:21:34 +02002/*
3 * Generic helpers for smp ipi calls
4 *
5 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
Jens Axboe3d442232008-06-26 11:21:34 +02006 */
Michael Ellermanca7dfdb2016-10-26 16:37:53 +11007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Frederic Weisbecker47885012014-05-08 01:37:48 +020010#include <linux/irq_work.h>
Jens Axboe3d442232008-06-26 11:21:34 +020011#include <linux/rcupdate.h>
Linus Torvalds59190f4212008-07-15 14:02:33 -070012#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +010013#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010015#include <linux/percpu.h>
16#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020018#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010019#include <linux/cpu.h>
Chuansheng Liuc6f44592014-09-04 15:17:54 +080020#include <linux/sched.h>
Ingo Molnar4c822692017-02-01 16:36:40 +010021#include <linux/sched/idle.h>
Juergen Gross47ae4b02016-08-29 08:48:43 +020022#include <linux/hypervisor.h>
Jens Axboe3d442232008-06-26 11:21:34 +020023
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070024#include "smpboot.h"
25
Jens Axboe3d442232008-06-26 11:21:34 +020026enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010027 CSD_FLAG_LOCK = 0x01,
Linus Torvalds80538712015-02-11 12:42:10 -080028 CSD_FLAG_SYNCHRONOUS = 0x02,
Jens Axboe3d442232008-06-26 11:21:34 +020029};
30
31struct call_function_data {
Ying Huang966a9672017-08-08 12:30:00 +080032 call_single_data_t __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010033 cpumask_var_t cpumask;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080034 cpumask_var_t cpumask_ipi;
Jens Axboe3d442232008-06-26 11:21:34 +020035};
36
Nadav Amita22793c2019-06-12 23:48:11 -070037static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
Milton Millere03bcb62010-01-18 13:00:51 +110038
Christoph Hellwig6897fc22014-01-30 15:45:47 -080039static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010040
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070041static void flush_smp_call_function_queue(bool warn_cpu_offline);
42
Richard Weinberger31487f82016-07-13 17:17:01 +000043int smpcfd_prepare_cpu(unsigned int cpu)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010044{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010045 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
46
Richard Weinberger31487f82016-07-13 17:17:01 +000047 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
48 cpu_to_node(cpu)))
49 return -ENOMEM;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080050 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
51 cpu_to_node(cpu))) {
52 free_cpumask_var(cfd->cpumask);
53 return -ENOMEM;
54 }
Ying Huang966a9672017-08-08 12:30:00 +080055 cfd->csd = alloc_percpu(call_single_data_t);
Richard Weinberger31487f82016-07-13 17:17:01 +000056 if (!cfd->csd) {
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010057 free_cpumask_var(cfd->cpumask);
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080058 free_cpumask_var(cfd->cpumask_ipi);
Richard Weinberger31487f82016-07-13 17:17:01 +000059 return -ENOMEM;
60 }
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070061
Richard Weinberger31487f82016-07-13 17:17:01 +000062 return 0;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010063}
64
Richard Weinberger31487f82016-07-13 17:17:01 +000065int smpcfd_dead_cpu(unsigned int cpu)
66{
67 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
68
69 free_cpumask_var(cfd->cpumask);
Aaron Lu3fc5b3b2017-05-19 15:53:31 +080070 free_cpumask_var(cfd->cpumask_ipi);
Richard Weinberger31487f82016-07-13 17:17:01 +000071 free_percpu(cfd->csd);
72 return 0;
73}
74
75int smpcfd_dying_cpu(unsigned int cpu)
76{
77 /*
78 * The IPIs for the smp-call-function callbacks queued by other
79 * CPUs might arrive late, either due to hardware latencies or
80 * because this CPU disabled interrupts (inside stop-machine)
81 * before the IPIs were sent. So flush out any pending callbacks
82 * explicitly (without waiting for the IPIs to arrive), to
83 * ensure that the outgoing CPU doesn't go offline with work
84 * still pending.
85 */
86 flush_smp_call_function_queue(false);
Peter Zijlstraafaa6532020-05-26 18:11:00 +020087 irq_work_run();
Richard Weinberger31487f82016-07-13 17:17:01 +000088 return 0;
89}
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010090
Takao Indohd8ad7d12011-03-29 12:35:04 -040091void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020092{
93 int i;
94
Christoph Hellwig6897fc22014-01-30 15:45:47 -080095 for_each_possible_cpu(i)
96 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010097
Richard Weinberger31487f82016-07-13 17:17:01 +000098 smpcfd_prepare_cpu(smp_processor_id());
Jens Axboe3d442232008-06-26 11:21:34 +020099}
100
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100101/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100102 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
103 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100104 * For non-synchronous ipi calls the csd can still be in use by the
105 * previous function call. For multi-cpu calls its even more interesting
106 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100107 */
Ying Huang966a9672017-08-08 12:30:00 +0800108static __always_inline void csd_lock_wait(call_single_data_t *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100109{
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +0200110 smp_cond_load_acquire(&csd->flags, !(VAL & CSD_FLAG_LOCK));
Peter Zijlstra6e275632009-02-25 13:59:48 +0100111}
112
Ying Huang966a9672017-08-08 12:30:00 +0800113static __always_inline void csd_lock(call_single_data_t *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100114{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700115 csd_lock_wait(csd);
116 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100117
118 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100119 * prevent CPU from reordering the above assignment
120 * to ->flags with any subsequent assignments to other
Ying Huang966a9672017-08-08 12:30:00 +0800121 * fields of the specified call_single_data_t structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100122 */
Linus Torvalds80538712015-02-11 12:42:10 -0800123 smp_wmb();
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100124}
125
Ying Huang966a9672017-08-08 12:30:00 +0800126static __always_inline void csd_unlock(call_single_data_t *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100127{
Linus Torvalds80538712015-02-11 12:42:10 -0800128 WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100129
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100130 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100131 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100132 */
Linus Torvalds80538712015-02-11 12:42:10 -0800133 smp_store_release(&csd->flags, 0);
Jens Axboe3d442232008-06-26 11:21:34 +0200134}
135
Ying Huang966a9672017-08-08 12:30:00 +0800136static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100137
Jens Axboe3d442232008-06-26 11:21:34 +0200138/*
Ying Huang966a9672017-08-08 12:30:00 +0800139 * Insert a previously allocated call_single_data_t element
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100140 * for execution on the given CPU. data must already have
141 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200142 */
Ying Huang966a9672017-08-08 12:30:00 +0800143static int generic_exec_single(int cpu, call_single_data_t *csd,
Linus Torvalds80538712015-02-11 12:42:10 -0800144 smp_call_func_t func, void *info)
Jens Axboe3d442232008-06-26 11:21:34 +0200145{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100146 if (cpu == smp_processor_id()) {
Linus Torvalds80538712015-02-11 12:42:10 -0800147 unsigned long flags;
148
149 /*
150 * We can unlock early even for the synchronous on-stack case,
151 * since we're doing this from the same CPU..
152 */
153 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100154 local_irq_save(flags);
155 func(info);
156 local_irq_restore(flags);
157 return 0;
158 }
159
160
Linus Torvalds5224b962015-04-19 04:56:03 -0400161 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
162 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100163 return -ENXIO;
Linus Torvalds5224b962015-04-19 04:56:03 -0400164 }
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100165
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100166 csd->func = func;
167 csd->info = info;
168
Suresh Siddha561920a02008-10-30 18:28:41 +0100169 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100170 * The list addition should be visible before sending the IPI
171 * handler locks the list to pull the entry off it because of
172 * normal cache coherency rules implied by spinlocks.
173 *
174 * If IPIs can go out of order to the cache coherency protocol
175 * in an architecture, sufficient synchronisation should be added
176 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100177 * locking and barrier primitives. Generic code isn't really
178 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100179 */
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800180 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Jens Axboe3d442232008-06-26 11:21:34 +0200181 arch_send_call_function_single_ipi(cpu);
182
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100183 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200184}
185
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700186/**
187 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
188 *
189 * Invoked by arch to handle an IPI for call function single.
190 * Must be called with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200191 */
192void generic_smp_call_function_single_interrupt(void)
193{
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700194 flush_smp_call_function_queue(true);
Peter Zijlstraafaa6532020-05-26 18:11:00 +0200195
196 /*
197 * Handle irq works queued remotely by irq_work_queue_on().
198 * Smp functions above are typically synchronous so they
199 * better run first since some other CPUs may be busy waiting
200 * for them.
201 */
202 irq_work_run();
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700203}
204
205/**
206 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
207 *
208 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
209 * offline CPU. Skip this check if set to 'false'.
210 *
211 * Flush any pending smp-call-function callbacks queued on this CPU. This is
212 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
213 * to ensure that all pending IPI callbacks are run before it goes completely
214 * offline.
215 *
216 * Loop through the call_single_queue and run all the queued callbacks.
217 * Must be called with interrupts disabled.
218 */
219static void flush_smp_call_function_queue(bool warn_cpu_offline)
220{
Ying Huang966a9672017-08-08 12:30:00 +0800221 call_single_data_t *csd, *csd_next;
Peter Zijlstra52103be2020-05-26 18:10:59 +0200222 struct llist_node *entry, *prev;
223 struct llist_head *head;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700224 static bool warned;
225
Frederic Weisbecker83efcbd2017-11-06 16:01:22 +0100226 lockdep_assert_irqs_disabled();
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700227
Christoph Lameterbb964a92014-08-17 12:30:24 -0500228 head = this_cpu_ptr(&call_single_queue);
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700229 entry = llist_del_all(head);
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700230 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200231
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700232 /* There shouldn't be any pending callbacks on an offline CPU. */
233 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
234 !warned && !llist_empty(head))) {
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700235 warned = true;
236 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700237
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700238 /*
239 * We don't have to use the _safe() variant here
240 * because we are not invoking the IPI handlers yet.
241 */
242 llist_for_each_entry(csd, entry, llist)
243 pr_warn("IPI callback %pS sent to offline CPU\n",
244 csd->func);
245 }
Jens Axboe3d442232008-06-26 11:21:34 +0200246
Peter Zijlstra52103be2020-05-26 18:10:59 +0200247 /*
248 * First; run all SYNC callbacks, people are waiting for us.
249 */
250 prev = NULL;
Jan Kara5fd77592014-02-24 16:39:55 +0100251 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
Linus Torvalds80538712015-02-11 12:42:10 -0800252 smp_call_func_t func = csd->func;
253 void *info = csd->info;
254
255 /* Do we wait until *after* callback? */
256 if (csd->flags & CSD_FLAG_SYNCHRONOUS) {
Peter Zijlstra52103be2020-05-26 18:10:59 +0200257 if (prev) {
258 prev->next = &csd_next->llist;
259 } else {
260 entry = &csd_next->llist;
261 }
Linus Torvalds80538712015-02-11 12:42:10 -0800262 func(info);
263 csd_unlock(csd);
264 } else {
Peter Zijlstra52103be2020-05-26 18:10:59 +0200265 prev = &csd->llist;
Linus Torvalds80538712015-02-11 12:42:10 -0800266 }
Jens Axboe3d442232008-06-26 11:21:34 +0200267 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200268
269 /*
Peter Zijlstra52103be2020-05-26 18:10:59 +0200270 * Second; run all !SYNC callbacks.
271 */
272 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
273 smp_call_func_t func = csd->func;
274 void *info = csd->info;
275
276 csd_unlock(csd);
277 func(info);
278 }
Jens Axboe3d442232008-06-26 11:21:34 +0200279}
280
281/*
282 * smp_call_function_single - Run a function on a specific CPU
283 * @func: The function to run. This must be fast and non-blocking.
284 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200285 * @wait: If true, wait until function has completed on other CPUs.
286 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800287 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200288 */
David Howells3a5f65df2010-10-27 17:28:36 +0100289int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200290 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200291{
Ying Huang966a9672017-08-08 12:30:00 +0800292 call_single_data_t *csd;
293 call_single_data_t csd_stack = {
294 .flags = CSD_FLAG_LOCK | CSD_FLAG_SYNCHRONOUS,
295 };
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100296 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100297 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200298
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100299 /*
300 * prevent preemption and reschedule on another processor,
301 * as well as CPU removal
302 */
303 this_cpu = get_cpu();
304
Suresh Siddha269c8612009-08-19 18:05:35 -0700305 /*
306 * Can deadlock when called with interrupts disabled.
307 * We allow cpu's that are not yet online though, as no one else can
308 * send smp call function interrupt to this cpu and as such deadlocks
309 * can't happen.
310 */
311 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
312 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200313
Peter Zijlstra19dbdcb2019-07-18 11:20:09 +0200314 /*
315 * When @wait we can deadlock when we interrupt between llist_add() and
316 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
317 * csd_lock() on because the interrupt context uses the same csd
318 * storage.
319 */
320 WARN_ON_ONCE(!in_task());
321
Linus Torvalds80538712015-02-11 12:42:10 -0800322 csd = &csd_stack;
323 if (!wait) {
324 csd = this_cpu_ptr(&csd_data);
325 csd_lock(csd);
326 }
327
328 err = generic_exec_single(cpu, csd, func, info);
329
330 if (wait)
331 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200332
333 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100334
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700335 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200336}
337EXPORT_SYMBOL(smp_call_function_single);
338
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100339/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100340 * smp_call_function_single_async(): Run an asynchronous function on a
341 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100342 * @cpu: The CPU to run on.
343 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100344 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100345 * Like smp_call_function_single(), but the call is asynchonous and
346 * can thus be done from contexts with disabled interrupts.
347 *
348 * The caller passes his own pre-allocated data structure
349 * (ie: embedded in an object) and is responsible for synchronizing it
350 * such that the IPIs performed on the @csd are strictly serialized.
351 *
Peter Xu5a18cec2019-12-16 16:31:23 -0500352 * If the function is called with one csd which has not yet been
353 * processed by previous call to smp_call_function_single_async(), the
354 * function will return immediately with -EBUSY showing that the csd
355 * object is still in progress.
356 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100357 * NOTE: Be careful, there is unfortunately no current debugging facility to
358 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100359 */
Ying Huang966a9672017-08-08 12:30:00 +0800360int smp_call_function_single_async(int cpu, call_single_data_t *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100361{
362 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100363
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100364 preempt_disable();
Linus Torvalds80538712015-02-11 12:42:10 -0800365
Peter Xu5a18cec2019-12-16 16:31:23 -0500366 if (csd->flags & CSD_FLAG_LOCK) {
367 err = -EBUSY;
368 goto out;
369 }
Linus Torvalds80538712015-02-11 12:42:10 -0800370
371 csd->flags = CSD_FLAG_LOCK;
372 smp_wmb();
373
374 err = generic_exec_single(cpu, csd, csd->func, csd->info);
Peter Xu5a18cec2019-12-16 16:31:23 -0500375
376out:
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100377 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100378
379 return err;
380}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100381EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100382
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800383/*
384 * smp_call_function_any - Run a function on any of the given cpus
385 * @mask: The mask of cpus it can run on.
386 * @func: The function to run. This must be fast and non-blocking.
387 * @info: An arbitrary pointer to pass to the function.
388 * @wait: If true, wait until function has completed.
389 *
390 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800391 *
392 * Selection preference:
393 * 1) current cpu if in @mask
394 * 2) any cpu of current node if in @mask
395 * 3) any other online cpu in @mask
396 */
397int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100398 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800399{
400 unsigned int cpu;
401 const struct cpumask *nodemask;
402 int ret;
403
404 /* Try for same CPU (cheapest) */
405 cpu = get_cpu();
406 if (cpumask_test_cpu(cpu, mask))
407 goto call;
408
409 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800410 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800411 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
412 cpu = cpumask_next_and(cpu, nodemask, mask)) {
413 if (cpu_online(cpu))
414 goto call;
415 }
416
417 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
418 cpu = cpumask_any_and(mask, cpu_online_mask);
419call:
420 ret = smp_call_function_single(cpu, func, info, wait);
421 put_cpu();
422 return ret;
423}
424EXPORT_SYMBOL_GPL(smp_call_function_any);
425
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100426static void smp_call_function_many_cond(const struct cpumask *mask,
427 smp_call_func_t func, void *info,
428 bool wait, smp_cond_func_t cond_func)
Jens Axboe3d442232008-06-26 11:21:34 +0200429{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700430 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800431 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200432
Suresh Siddha269c8612009-08-19 18:05:35 -0700433 /*
434 * Can deadlock when called with interrupts disabled.
435 * We allow cpu's that are not yet online though, as no one else can
436 * send smp call function interrupt to this cpu and as such deadlocks
437 * can't happen.
438 */
439 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100440 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200441
Peter Zijlstra19dbdcb2019-07-18 11:20:09 +0200442 /*
443 * When @wait we can deadlock when we interrupt between llist_add() and
444 * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
445 * csd_lock() on because the interrupt context uses the same csd
446 * storage.
447 */
448 WARN_ON_ONCE(!in_task());
449
Milton Miller723aae22011-03-15 13:27:17 -0600450 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030451 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100452 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030453 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100454
Rusty Russell54b11e62008-12-30 09:05:16 +1030455 /* No online cpus? We're done. */
456 if (cpu >= nr_cpu_ids)
457 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200458
Rusty Russell54b11e62008-12-30 09:05:16 +1030459 /* Do we have another CPU which isn't us? */
460 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100461 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030462 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
463
464 /* Fastpath: do that cpu by itself. */
465 if (next_cpu >= nr_cpu_ids) {
Sebastian Andrzej Siewior25a3a152020-01-27 09:39:15 +0100466 if (!cond_func || cond_func(cpu, info))
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100467 smp_call_function_single(cpu, func, info, wait);
Rusty Russell54b11e62008-12-30 09:05:16 +1030468 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200469 }
470
Christoph Lameterbb964a92014-08-17 12:30:24 -0500471 cfd = this_cpu_ptr(&cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600472
Andrew Mortone1d12f32013-04-30 15:27:28 -0700473 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
Peter Zijlstra6c8557b2017-05-19 12:58:25 +0200474 __cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600475
476 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700477 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600478 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800479
Aaron Lu3fc5b3b2017-05-19 15:53:31 +0800480 cpumask_clear(cfd->cpumask_ipi);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700481 for_each_cpu(cpu, cfd->cpumask) {
Ying Huang966a9672017-08-08 12:30:00 +0800482 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800483
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100484 if (cond_func && !cond_func(cpu, info))
485 continue;
486
Shaohua Li9a46ad62013-02-21 16:43:03 -0800487 csd_lock(csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800488 if (wait)
489 csd->flags |= CSD_FLAG_SYNCHRONOUS;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800490 csd->func = func;
491 csd->info = info;
Aaron Lu3fc5b3b2017-05-19 15:53:31 +0800492 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Peter Zijlstra6c8557b2017-05-19 12:58:25 +0200493 __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800494 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100495
Jens Axboe3d442232008-06-26 11:21:34 +0200496 /* Send a message to all CPUs in the map */
Aaron Lu3fc5b3b2017-05-19 15:53:31 +0800497 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200498
Shaohua Li9a46ad62013-02-21 16:43:03 -0800499 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700500 for_each_cpu(cpu, cfd->cpumask) {
Ying Huang966a9672017-08-08 12:30:00 +0800501 call_single_data_t *csd;
Andrew Mortone1d12f32013-04-30 15:27:28 -0700502
503 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800504 csd_lock_wait(csd);
505 }
506 }
Jens Axboe3d442232008-06-26 11:21:34 +0200507}
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100508
509/**
510 * smp_call_function_many(): Run a function on a set of other CPUs.
511 * @mask: The set of cpus to run on (only runs on online subset).
512 * @func: The function to run. This must be fast and non-blocking.
513 * @info: An arbitrary pointer to pass to the function.
514 * @wait: If true, wait (atomically) until function has completed
515 * on other CPUs.
516 *
517 * If @wait is true, then returns once @func has returned.
518 *
519 * You must not call this function with disabled interrupts or from a
520 * hardware interrupt handler or from a bottom half handler. Preemption
521 * must be disabled when calling this function.
522 */
523void smp_call_function_many(const struct cpumask *mask,
524 smp_call_func_t func, void *info, bool wait)
525{
526 smp_call_function_many_cond(mask, func, info, wait, NULL);
527}
Rusty Russell54b11e62008-12-30 09:05:16 +1030528EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200529
530/**
531 * smp_call_function(): Run a function on all other CPUs.
532 * @func: The function to run. This must be fast and non-blocking.
533 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100534 * @wait: If true, wait (atomically) until function has completed
535 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200536 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030537 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200538 *
539 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800540 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200541 *
542 * You must not call this function with disabled interrupts or from a
543 * hardware interrupt handler or from a bottom half handler.
544 */
Nadav Amitcaa75932019-06-12 23:48:05 -0700545void smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200546{
Jens Axboe3d442232008-06-26 11:21:34 +0200547 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030548 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200549 preempt_enable();
Jens Axboe3d442232008-06-26 11:21:34 +0200550}
551EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800552
Amerigo Wang34db18a02011-03-22 16:34:06 -0700553/* Setup configured maximum number of CPUs to activate */
554unsigned int setup_max_cpus = NR_CPUS;
555EXPORT_SYMBOL(setup_max_cpus);
556
557
558/*
559 * Setup routine for controlling SMP activation
560 *
561 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
562 * activation entirely (the MPS table probe still happens, though).
563 *
564 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
565 * greater than 0, limits the maximum number of CPUs activated in
566 * SMP mode to <NUM>.
567 */
568
569void __weak arch_disable_smp_support(void) { }
570
571static int __init nosmp(char *str)
572{
573 setup_max_cpus = 0;
574 arch_disable_smp_support();
575
576 return 0;
577}
578
579early_param("nosmp", nosmp);
580
581/* this is hard limit */
582static int __init nrcpus(char *str)
583{
584 int nr_cpus;
585
586 get_option(&str, &nr_cpus);
587 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
588 nr_cpu_ids = nr_cpus;
589
590 return 0;
591}
592
593early_param("nr_cpus", nrcpus);
594
595static int __init maxcpus(char *str)
596{
597 get_option(&str, &setup_max_cpus);
598 if (setup_max_cpus == 0)
599 arch_disable_smp_support();
600
601 return 0;
602}
603
604early_param("maxcpus", maxcpus);
605
606/* Setup number of possible processor ids */
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -0700607unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
Amerigo Wang34db18a02011-03-22 16:34:06 -0700608EXPORT_SYMBOL(nr_cpu_ids);
609
610/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
611void __init setup_nr_cpu_ids(void)
612{
613 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
614}
615
616/* Called by boot processor to activate the rest. */
617void __init smp_init(void)
618{
Michael Ellerman92b23272016-10-26 16:37:54 +1100619 int num_nodes, num_cpus;
Amerigo Wang34db18a02011-03-22 16:34:06 -0700620
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700621 idle_threads_init();
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000622 cpuhp_threads_init();
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700623
Michael Ellerman51111dc2016-10-26 16:37:55 +1100624 pr_info("Bringing up secondary CPUs ...\n");
625
Qais Yousefb99a2652020-03-23 13:51:09 +0000626 bringup_nonboot_cpus(setup_max_cpus);
Amerigo Wang34db18a02011-03-22 16:34:06 -0700627
Michael Ellerman92b23272016-10-26 16:37:54 +1100628 num_nodes = num_online_nodes();
629 num_cpus = num_online_cpus();
630 pr_info("Brought up %d node%s, %d CPU%s\n",
631 num_nodes, (num_nodes > 1 ? "s" : ""),
632 num_cpus, (num_cpus > 1 ? "s" : ""));
633
Amerigo Wang34db18a02011-03-22 16:34:06 -0700634 /* Any cleanup work */
Amerigo Wang34db18a02011-03-22 16:34:06 -0700635 smp_cpus_done(setup_max_cpus);
636}
637
Amerigo Wang351f8f82011-01-12 16:59:39 -0800638/*
Tejun Heobd924e82011-01-20 12:07:13 +0100639 * Call a function on all processors. May be used during early boot while
640 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
641 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800642 */
Nadav Amitcaa75932019-06-12 23:48:05 -0700643void on_each_cpu(void (*func) (void *info), void *info, int wait)
Amerigo Wang351f8f82011-01-12 16:59:39 -0800644{
Tejun Heobd924e82011-01-20 12:07:13 +0100645 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800646
647 preempt_disable();
Nadav Amitcaa75932019-06-12 23:48:05 -0700648 smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100649 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800650 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100651 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800652 preempt_enable();
Amerigo Wang351f8f82011-01-12 16:59:39 -0800653}
654EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700655
656/**
657 * on_each_cpu_mask(): Run a function on processors specified by
658 * cpumask, which may include the local processor.
659 * @mask: The set of cpus to run on (only runs on online subset).
660 * @func: The function to run. This must be fast and non-blocking.
661 * @info: An arbitrary pointer to pass to the function.
662 * @wait: If true, wait (atomically) until function has completed
663 * on other CPUs.
664 *
665 * If @wait is true, then returns once @func has returned.
666 *
David Daney202da402013-09-11 14:23:29 -0700667 * You must not call this function with disabled interrupts or from a
668 * hardware interrupt handler or from a bottom half handler. The
669 * exception is that it may be used during early boot while
670 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700671 */
672void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
673 void *info, bool wait)
674{
675 int cpu = get_cpu();
676
677 smp_call_function_many(mask, func, info, wait);
678 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700679 unsigned long flags;
680 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700681 func(info);
David Daney202da402013-09-11 14:23:29 -0700682 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700683 }
684 put_cpu();
685}
686EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700687
688/*
689 * on_each_cpu_cond(): Call a function on each processor for which
690 * the supplied function cond_func returns true, optionally waiting
691 * for all the required CPUs to finish. This may include the local
692 * processor.
693 * @cond_func: A callback function that is passed a cpu id and
694 * the the info parameter. The function is called
695 * with preemption disabled. The function should
696 * return a blooean value indicating whether to IPI
697 * the specified CPU.
698 * @func: The function to run on all applicable CPUs.
699 * This must be fast and non-blocking.
700 * @info: An arbitrary pointer to pass to both functions.
701 * @wait: If true, wait (atomically) until function has
702 * completed on other CPUs.
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700703 *
704 * Preemption is disabled to protect against CPUs going offline but not online.
705 * CPUs going online during the call will not be seen or sent an IPI.
706 *
707 * You must not call this function with disabled interrupts or
708 * from a hardware interrupt handler or from a bottom half handler.
709 */
Sebastian Andrzej Siewior5671d812020-01-17 10:01:35 +0100710void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100711 void *info, bool wait, const struct cpumask *mask)
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700712{
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100713 int cpu = get_cpu();
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700714
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100715 smp_call_function_many_cond(mask, func, info, wait, cond_func);
716 if (cpumask_test_cpu(cpu, mask) && cond_func(cpu, info)) {
717 unsigned long flags;
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700718
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100719 local_irq_save(flags);
720 func(info);
721 local_irq_restore(flags);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700722 }
Sebastian Andrzej Siewior67719ef2020-01-17 10:01:36 +0100723 put_cpu();
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700724}
Rik van Riel7d49b282018-09-25 23:58:41 -0400725EXPORT_SYMBOL(on_each_cpu_cond_mask);
726
Sebastian Andrzej Siewior5671d812020-01-17 10:01:35 +0100727void on_each_cpu_cond(smp_cond_func_t cond_func, smp_call_func_t func,
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100728 void *info, bool wait)
Rik van Riel7d49b282018-09-25 23:58:41 -0400729{
Sebastian Andrzej Siewiorcb923152020-01-17 10:01:37 +0100730 on_each_cpu_cond_mask(cond_func, func, info, wait, cpu_online_mask);
Rik van Riel7d49b282018-09-25 23:58:41 -0400731}
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700732EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000733
734static void do_nothing(void *unused)
735{
736}
737
738/**
739 * kick_all_cpus_sync - Force all cpus out of idle
740 *
741 * Used to synchronize the update of pm_idle function pointer. It's
742 * called after the pointer is updated and returns after the dummy
743 * callback function has been executed on all cpus. The execution of
744 * the function can only happen on the remote cpus after they have
745 * left the idle function which had been called via pm_idle function
746 * pointer. So it's guaranteed that nothing uses the previous pointer
747 * anymore.
748 */
749void kick_all_cpus_sync(void)
750{
751 /* Make sure the change is visible before we kick the cpus */
752 smp_mb();
753 smp_call_function(do_nothing, NULL, 1);
754}
755EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
Chuansheng Liuc6f44592014-09-04 15:17:54 +0800756
757/**
758 * wake_up_all_idle_cpus - break all cpus out of idle
759 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
760 * including idle polling cpus, for non-idle cpus, we will do nothing
761 * for them.
762 */
763void wake_up_all_idle_cpus(void)
764{
765 int cpu;
766
767 preempt_disable();
768 for_each_online_cpu(cpu) {
769 if (cpu == smp_processor_id())
770 continue;
771
772 wake_up_if_idle(cpu);
773 }
774 preempt_enable();
775}
776EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200777
778/**
779 * smp_call_on_cpu - Call a function on a specific cpu
780 *
781 * Used to call a function on a specific cpu and wait for it to return.
782 * Optionally make sure the call is done on a specified physical cpu via vcpu
783 * pinning in order to support virtualized environments.
784 */
785struct smp_call_on_cpu_struct {
786 struct work_struct work;
787 struct completion done;
788 int (*func)(void *);
789 void *data;
790 int ret;
791 int cpu;
792};
793
794static void smp_call_on_cpu_callback(struct work_struct *work)
795{
796 struct smp_call_on_cpu_struct *sscs;
797
798 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
799 if (sscs->cpu >= 0)
800 hypervisor_pin_vcpu(sscs->cpu);
801 sscs->ret = sscs->func(sscs->data);
802 if (sscs->cpu >= 0)
803 hypervisor_pin_vcpu(-1);
804
805 complete(&sscs->done);
806}
807
808int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
809{
810 struct smp_call_on_cpu_struct sscs = {
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200811 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
812 .func = func,
813 .data = par,
814 .cpu = phys ? cpu : -1,
815 };
816
Peter Zijlstra8db54942016-09-11 10:36:26 +0200817 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
818
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200819 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
820 return -ENXIO;
821
822 queue_work_on(cpu, system_wq, &sscs.work);
823 wait_for_completion(&sscs.done);
824
825 return sscs.ret;
826}
827EXPORT_SYMBOL_GPL(smp_call_on_cpu);