blob: 399905fdfa3f85bbc167fd468d56166fb7b12b5c [file] [log] [blame]
Jens Axboe3d442232008-06-26 11:21:34 +02001/*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
Jens Axboe3d442232008-06-26 11:21:34 +02005 */
Frederic Weisbecker47885012014-05-08 01:37:48 +02006#include <linux/irq_work.h>
Jens Axboe3d442232008-06-26 11:21:34 +02007#include <linux/rcupdate.h>
Linus Torvalds59190f42008-07-15 14:02:33 -07008#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +01009#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010011#include <linux/percpu.h>
12#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020014#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010015#include <linux/cpu.h>
Chuansheng Liuc6f44592014-09-04 15:17:54 +080016#include <linux/sched.h>
Juergen Gross47ae4b02016-08-29 08:48:43 +020017#include <linux/hypervisor.h>
Jens Axboe3d442232008-06-26 11:21:34 +020018
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070019#include "smpboot.h"
20
Jens Axboe3d442232008-06-26 11:21:34 +020021enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010022 CSD_FLAG_LOCK = 0x01,
Linus Torvalds80538712015-02-11 12:42:10 -080023 CSD_FLAG_SYNCHRONOUS = 0x02,
Jens Axboe3d442232008-06-26 11:21:34 +020024};
25
26struct call_function_data {
Shaohua Li9a46ad62013-02-21 16:43:03 -080027 struct call_single_data __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010028 cpumask_var_t cpumask;
Jens Axboe3d442232008-06-26 11:21:34 +020029};
30
Milton Millere03bcb62010-01-18 13:00:51 +110031static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
32
Christoph Hellwig6897fc22014-01-30 15:45:47 -080033static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010034
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070035static void flush_smp_call_function_queue(bool warn_cpu_offline);
36
Richard Weinberger31487f82016-07-13 17:17:01 +000037int smpcfd_prepare_cpu(unsigned int cpu)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010038{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010039 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
40
Richard Weinberger31487f82016-07-13 17:17:01 +000041 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
42 cpu_to_node(cpu)))
43 return -ENOMEM;
44 cfd->csd = alloc_percpu(struct call_single_data);
45 if (!cfd->csd) {
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010046 free_cpumask_var(cfd->cpumask);
Richard Weinberger31487f82016-07-13 17:17:01 +000047 return -ENOMEM;
48 }
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070049
Richard Weinberger31487f82016-07-13 17:17:01 +000050 return 0;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010051}
52
Richard Weinberger31487f82016-07-13 17:17:01 +000053int smpcfd_dead_cpu(unsigned int cpu)
54{
55 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
56
57 free_cpumask_var(cfd->cpumask);
58 free_percpu(cfd->csd);
59 return 0;
60}
61
62int smpcfd_dying_cpu(unsigned int cpu)
63{
64 /*
65 * The IPIs for the smp-call-function callbacks queued by other
66 * CPUs might arrive late, either due to hardware latencies or
67 * because this CPU disabled interrupts (inside stop-machine)
68 * before the IPIs were sent. So flush out any pending callbacks
69 * explicitly (without waiting for the IPIs to arrive), to
70 * ensure that the outgoing CPU doesn't go offline with work
71 * still pending.
72 */
73 flush_smp_call_function_queue(false);
74 return 0;
75}
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010076
Takao Indohd8ad7d12011-03-29 12:35:04 -040077void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020078{
79 int i;
80
Christoph Hellwig6897fc22014-01-30 15:45:47 -080081 for_each_possible_cpu(i)
82 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010083
Richard Weinberger31487f82016-07-13 17:17:01 +000084 smpcfd_prepare_cpu(smp_processor_id());
Jens Axboe3d442232008-06-26 11:21:34 +020085}
86
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010087/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010088 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
89 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +010090 * For non-synchronous ipi calls the csd can still be in use by the
91 * previous function call. For multi-cpu calls its even more interesting
92 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010093 */
Davidlohr Bueso90d10982016-03-09 17:55:35 -080094static __always_inline void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010095{
Peter Zijlstra1f03e8d2016-04-04 10:57:12 +020096 smp_cond_load_acquire(&csd->flags, !(VAL & CSD_FLAG_LOCK));
Peter Zijlstra6e275632009-02-25 13:59:48 +010097}
98
Davidlohr Bueso90d10982016-03-09 17:55:35 -080099static __always_inline void csd_lock(struct call_single_data *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100100{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700101 csd_lock_wait(csd);
102 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100103
104 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100105 * prevent CPU from reordering the above assignment
106 * to ->flags with any subsequent assignments to other
107 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100108 */
Linus Torvalds80538712015-02-11 12:42:10 -0800109 smp_wmb();
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100110}
111
Davidlohr Bueso90d10982016-03-09 17:55:35 -0800112static __always_inline void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100113{
Linus Torvalds80538712015-02-11 12:42:10 -0800114 WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100115
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100116 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100117 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100118 */
Linus Torvalds80538712015-02-11 12:42:10 -0800119 smp_store_release(&csd->flags, 0);
Jens Axboe3d442232008-06-26 11:21:34 +0200120}
121
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100122static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
123
Jens Axboe3d442232008-06-26 11:21:34 +0200124/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100125 * Insert a previously allocated call_single_data element
126 * for execution on the given CPU. data must already have
127 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200128 */
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100129static int generic_exec_single(int cpu, struct call_single_data *csd,
Linus Torvalds80538712015-02-11 12:42:10 -0800130 smp_call_func_t func, void *info)
Jens Axboe3d442232008-06-26 11:21:34 +0200131{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100132 if (cpu == smp_processor_id()) {
Linus Torvalds80538712015-02-11 12:42:10 -0800133 unsigned long flags;
134
135 /*
136 * We can unlock early even for the synchronous on-stack case,
137 * since we're doing this from the same CPU..
138 */
139 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100140 local_irq_save(flags);
141 func(info);
142 local_irq_restore(flags);
143 return 0;
144 }
145
146
Linus Torvalds5224b962015-04-19 04:56:03 -0400147 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
148 csd_unlock(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100149 return -ENXIO;
Linus Torvalds5224b962015-04-19 04:56:03 -0400150 }
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100151
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100152 csd->func = func;
153 csd->info = info;
154
Suresh Siddha561920a02008-10-30 18:28:41 +0100155 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100156 * The list addition should be visible before sending the IPI
157 * handler locks the list to pull the entry off it because of
158 * normal cache coherency rules implied by spinlocks.
159 *
160 * If IPIs can go out of order to the cache coherency protocol
161 * in an architecture, sufficient synchronisation should be added
162 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100163 * locking and barrier primitives. Generic code isn't really
164 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100165 */
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800166 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Jens Axboe3d442232008-06-26 11:21:34 +0200167 arch_send_call_function_single_ipi(cpu);
168
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100169 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200170}
171
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700172/**
173 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
174 *
175 * Invoked by arch to handle an IPI for call function single.
176 * Must be called with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200177 */
178void generic_smp_call_function_single_interrupt(void)
179{
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700180 flush_smp_call_function_queue(true);
181}
182
183/**
184 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
185 *
186 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
187 * offline CPU. Skip this check if set to 'false'.
188 *
189 * Flush any pending smp-call-function callbacks queued on this CPU. This is
190 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
191 * to ensure that all pending IPI callbacks are run before it goes completely
192 * offline.
193 *
194 * Loop through the call_single_queue and run all the queued callbacks.
195 * Must be called with interrupts disabled.
196 */
197static void flush_smp_call_function_queue(bool warn_cpu_offline)
198{
199 struct llist_head *head;
Jan Kara5fd77592014-02-24 16:39:55 +0100200 struct llist_node *entry;
201 struct call_single_data *csd, *csd_next;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700202 static bool warned;
203
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700204 WARN_ON(!irqs_disabled());
205
Christoph Lameterbb964a92014-08-17 12:30:24 -0500206 head = this_cpu_ptr(&call_single_queue);
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700207 entry = llist_del_all(head);
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700208 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200209
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700210 /* There shouldn't be any pending callbacks on an offline CPU. */
211 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
212 !warned && !llist_empty(head))) {
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700213 warned = true;
214 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700215
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700216 /*
217 * We don't have to use the _safe() variant here
218 * because we are not invoking the IPI handlers yet.
219 */
220 llist_for_each_entry(csd, entry, llist)
221 pr_warn("IPI callback %pS sent to offline CPU\n",
222 csd->func);
223 }
Jens Axboe3d442232008-06-26 11:21:34 +0200224
Jan Kara5fd77592014-02-24 16:39:55 +0100225 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
Linus Torvalds80538712015-02-11 12:42:10 -0800226 smp_call_func_t func = csd->func;
227 void *info = csd->info;
228
229 /* Do we wait until *after* callback? */
230 if (csd->flags & CSD_FLAG_SYNCHRONOUS) {
231 func(info);
232 csd_unlock(csd);
233 } else {
234 csd_unlock(csd);
235 func(info);
236 }
Jens Axboe3d442232008-06-26 11:21:34 +0200237 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200238
239 /*
240 * Handle irq works queued remotely by irq_work_queue_on().
241 * Smp functions above are typically synchronous so they
242 * better run first since some other CPUs may be busy waiting
243 * for them.
244 */
245 irq_work_run();
Jens Axboe3d442232008-06-26 11:21:34 +0200246}
247
248/*
249 * smp_call_function_single - Run a function on a specific CPU
250 * @func: The function to run. This must be fast and non-blocking.
251 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200252 * @wait: If true, wait until function has completed on other CPUs.
253 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800254 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200255 */
David Howells3a5f65d2010-10-27 17:28:36 +0100256int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200257 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200258{
Linus Torvalds80538712015-02-11 12:42:10 -0800259 struct call_single_data *csd;
260 struct call_single_data csd_stack = { .flags = CSD_FLAG_LOCK | CSD_FLAG_SYNCHRONOUS };
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100261 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100262 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200263
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100264 /*
265 * prevent preemption and reschedule on another processor,
266 * as well as CPU removal
267 */
268 this_cpu = get_cpu();
269
Suresh Siddha269c8612009-08-19 18:05:35 -0700270 /*
271 * Can deadlock when called with interrupts disabled.
272 * We allow cpu's that are not yet online though, as no one else can
273 * send smp call function interrupt to this cpu and as such deadlocks
274 * can't happen.
275 */
276 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
277 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200278
Linus Torvalds80538712015-02-11 12:42:10 -0800279 csd = &csd_stack;
280 if (!wait) {
281 csd = this_cpu_ptr(&csd_data);
282 csd_lock(csd);
283 }
284
285 err = generic_exec_single(cpu, csd, func, info);
286
287 if (wait)
288 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200289
290 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100291
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700292 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200293}
294EXPORT_SYMBOL(smp_call_function_single);
295
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100296/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100297 * smp_call_function_single_async(): Run an asynchronous function on a
298 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100299 * @cpu: The CPU to run on.
300 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100301 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100302 * Like smp_call_function_single(), but the call is asynchonous and
303 * can thus be done from contexts with disabled interrupts.
304 *
305 * The caller passes his own pre-allocated data structure
306 * (ie: embedded in an object) and is responsible for synchronizing it
307 * such that the IPIs performed on the @csd are strictly serialized.
308 *
309 * NOTE: Be careful, there is unfortunately no current debugging facility to
310 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100311 */
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100312int smp_call_function_single_async(int cpu, struct call_single_data *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100313{
314 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100315
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100316 preempt_disable();
Linus Torvalds80538712015-02-11 12:42:10 -0800317
318 /* We could deadlock if we have to wait here with interrupts disabled! */
319 if (WARN_ON_ONCE(csd->flags & CSD_FLAG_LOCK))
320 csd_lock_wait(csd);
321
322 csd->flags = CSD_FLAG_LOCK;
323 smp_wmb();
324
325 err = generic_exec_single(cpu, csd, csd->func, csd->info);
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100326 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100327
328 return err;
329}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100330EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100331
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800332/*
333 * smp_call_function_any - Run a function on any of the given cpus
334 * @mask: The mask of cpus it can run on.
335 * @func: The function to run. This must be fast and non-blocking.
336 * @info: An arbitrary pointer to pass to the function.
337 * @wait: If true, wait until function has completed.
338 *
339 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800340 *
341 * Selection preference:
342 * 1) current cpu if in @mask
343 * 2) any cpu of current node if in @mask
344 * 3) any other online cpu in @mask
345 */
346int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100347 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800348{
349 unsigned int cpu;
350 const struct cpumask *nodemask;
351 int ret;
352
353 /* Try for same CPU (cheapest) */
354 cpu = get_cpu();
355 if (cpumask_test_cpu(cpu, mask))
356 goto call;
357
358 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800359 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800360 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
361 cpu = cpumask_next_and(cpu, nodemask, mask)) {
362 if (cpu_online(cpu))
363 goto call;
364 }
365
366 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
367 cpu = cpumask_any_and(mask, cpu_online_mask);
368call:
369 ret = smp_call_function_single(cpu, func, info, wait);
370 put_cpu();
371 return ret;
372}
373EXPORT_SYMBOL_GPL(smp_call_function_any);
374
Jens Axboe3d442232008-06-26 11:21:34 +0200375/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030376 * smp_call_function_many(): Run a function on a set of other CPUs.
377 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200378 * @func: The function to run. This must be fast and non-blocking.
379 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100380 * @wait: If true, wait (atomically) until function has completed
381 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200382 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800383 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200384 *
385 * You must not call this function with disabled interrupts or from a
386 * hardware interrupt handler or from a bottom half handler. Preemption
387 * must be disabled when calling this function.
388 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030389void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100390 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200391{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700392 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800393 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200394
Suresh Siddha269c8612009-08-19 18:05:35 -0700395 /*
396 * Can deadlock when called with interrupts disabled.
397 * We allow cpu's that are not yet online though, as no one else can
398 * send smp call function interrupt to this cpu and as such deadlocks
399 * can't happen.
400 */
401 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100402 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200403
Milton Miller723aae22011-03-15 13:27:17 -0600404 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030405 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100406 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030407 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100408
Rusty Russell54b11e62008-12-30 09:05:16 +1030409 /* No online cpus? We're done. */
410 if (cpu >= nr_cpu_ids)
411 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200412
Rusty Russell54b11e62008-12-30 09:05:16 +1030413 /* Do we have another CPU which isn't us? */
414 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100415 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030416 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
417
418 /* Fastpath: do that cpu by itself. */
419 if (next_cpu >= nr_cpu_ids) {
420 smp_call_function_single(cpu, func, info, wait);
421 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200422 }
423
Christoph Lameterbb964a92014-08-17 12:30:24 -0500424 cfd = this_cpu_ptr(&cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600425
Andrew Mortone1d12f32013-04-30 15:27:28 -0700426 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
427 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600428
429 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700430 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600431 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800432
Andrew Mortone1d12f32013-04-30 15:27:28 -0700433 for_each_cpu(cpu, cfd->cpumask) {
434 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800435
436 csd_lock(csd);
Linus Torvalds80538712015-02-11 12:42:10 -0800437 if (wait)
438 csd->flags |= CSD_FLAG_SYNCHRONOUS;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800439 csd->func = func;
440 csd->info = info;
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800441 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
Shaohua Li9a46ad62013-02-21 16:43:03 -0800442 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100443
Jens Axboe3d442232008-06-26 11:21:34 +0200444 /* Send a message to all CPUs in the map */
Roman Gushchin73f94552014-01-30 15:45:48 -0800445 arch_send_call_function_ipi_mask(cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200446
Shaohua Li9a46ad62013-02-21 16:43:03 -0800447 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700448 for_each_cpu(cpu, cfd->cpumask) {
449 struct call_single_data *csd;
450
451 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800452 csd_lock_wait(csd);
453 }
454 }
Jens Axboe3d442232008-06-26 11:21:34 +0200455}
Rusty Russell54b11e62008-12-30 09:05:16 +1030456EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200457
458/**
459 * smp_call_function(): Run a function on all other CPUs.
460 * @func: The function to run. This must be fast and non-blocking.
461 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100462 * @wait: If true, wait (atomically) until function has completed
463 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200464 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030465 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200466 *
467 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800468 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200469 *
470 * You must not call this function with disabled interrupts or from a
471 * hardware interrupt handler or from a bottom half handler.
472 */
David Howells3a5f65d2010-10-27 17:28:36 +0100473int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200474{
Jens Axboe3d442232008-06-26 11:21:34 +0200475 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030476 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200477 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100478
Rusty Russell54b11e62008-12-30 09:05:16 +1030479 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200480}
481EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800482
Amerigo Wang34db18a02011-03-22 16:34:06 -0700483/* Setup configured maximum number of CPUs to activate */
484unsigned int setup_max_cpus = NR_CPUS;
485EXPORT_SYMBOL(setup_max_cpus);
486
487
488/*
489 * Setup routine for controlling SMP activation
490 *
491 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
492 * activation entirely (the MPS table probe still happens, though).
493 *
494 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
495 * greater than 0, limits the maximum number of CPUs activated in
496 * SMP mode to <NUM>.
497 */
498
499void __weak arch_disable_smp_support(void) { }
500
501static int __init nosmp(char *str)
502{
503 setup_max_cpus = 0;
504 arch_disable_smp_support();
505
506 return 0;
507}
508
509early_param("nosmp", nosmp);
510
511/* this is hard limit */
512static int __init nrcpus(char *str)
513{
514 int nr_cpus;
515
516 get_option(&str, &nr_cpus);
517 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
518 nr_cpu_ids = nr_cpus;
519
520 return 0;
521}
522
523early_param("nr_cpus", nrcpus);
524
525static int __init maxcpus(char *str)
526{
527 get_option(&str, &setup_max_cpus);
528 if (setup_max_cpus == 0)
529 arch_disable_smp_support();
530
531 return 0;
532}
533
534early_param("maxcpus", maxcpus);
535
536/* Setup number of possible processor ids */
537int nr_cpu_ids __read_mostly = NR_CPUS;
538EXPORT_SYMBOL(nr_cpu_ids);
539
540/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
541void __init setup_nr_cpu_ids(void)
542{
543 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
544}
545
Borislav Petkova17bce42013-09-30 11:56:24 +0200546void __weak smp_announce(void)
547{
548 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
549}
550
Amerigo Wang34db18a02011-03-22 16:34:06 -0700551/* Called by boot processor to activate the rest. */
552void __init smp_init(void)
553{
554 unsigned int cpu;
555
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700556 idle_threads_init();
Thomas Gleixner4cb28ce2016-02-26 18:43:38 +0000557 cpuhp_threads_init();
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700558
Amerigo Wang34db18a02011-03-22 16:34:06 -0700559 /* FIXME: This should be done in userspace --RR */
560 for_each_present_cpu(cpu) {
561 if (num_online_cpus() >= setup_max_cpus)
562 break;
563 if (!cpu_online(cpu))
564 cpu_up(cpu);
565 }
566
Thomas Gleixnerc504b9f2018-08-07 08:19:57 +0200567 /* Final decision about SMT support */
568 cpu_smt_check_topology();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700569 /* Any cleanup work */
Borislav Petkova17bce42013-09-30 11:56:24 +0200570 smp_announce();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700571 smp_cpus_done(setup_max_cpus);
572}
573
Amerigo Wang351f8f82011-01-12 16:59:39 -0800574/*
Tejun Heobd924e82011-01-20 12:07:13 +0100575 * Call a function on all processors. May be used during early boot while
576 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
577 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800578 */
579int on_each_cpu(void (*func) (void *info), void *info, int wait)
580{
Tejun Heobd924e82011-01-20 12:07:13 +0100581 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800582 int ret = 0;
583
584 preempt_disable();
585 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100586 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800587 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100588 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800589 preempt_enable();
590 return ret;
591}
592EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700593
594/**
595 * on_each_cpu_mask(): Run a function on processors specified by
596 * cpumask, which may include the local processor.
597 * @mask: The set of cpus to run on (only runs on online subset).
598 * @func: The function to run. This must be fast and non-blocking.
599 * @info: An arbitrary pointer to pass to the function.
600 * @wait: If true, wait (atomically) until function has completed
601 * on other CPUs.
602 *
603 * If @wait is true, then returns once @func has returned.
604 *
David Daney202da402013-09-11 14:23:29 -0700605 * You must not call this function with disabled interrupts or from a
606 * hardware interrupt handler or from a bottom half handler. The
607 * exception is that it may be used during early boot while
608 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700609 */
610void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
611 void *info, bool wait)
612{
613 int cpu = get_cpu();
614
615 smp_call_function_many(mask, func, info, wait);
616 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700617 unsigned long flags;
618 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700619 func(info);
David Daney202da402013-09-11 14:23:29 -0700620 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700621 }
622 put_cpu();
623}
624EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700625
626/*
627 * on_each_cpu_cond(): Call a function on each processor for which
628 * the supplied function cond_func returns true, optionally waiting
629 * for all the required CPUs to finish. This may include the local
630 * processor.
631 * @cond_func: A callback function that is passed a cpu id and
632 * the the info parameter. The function is called
633 * with preemption disabled. The function should
634 * return a blooean value indicating whether to IPI
635 * the specified CPU.
636 * @func: The function to run on all applicable CPUs.
637 * This must be fast and non-blocking.
638 * @info: An arbitrary pointer to pass to both functions.
639 * @wait: If true, wait (atomically) until function has
640 * completed on other CPUs.
641 * @gfp_flags: GFP flags to use when allocating the cpumask
642 * used internally by the function.
643 *
644 * The function might sleep if the GFP flags indicates a non
645 * atomic allocation is allowed.
646 *
647 * Preemption is disabled to protect against CPUs going offline but not online.
648 * CPUs going online during the call will not be seen or sent an IPI.
649 *
650 * You must not call this function with disabled interrupts or
651 * from a hardware interrupt handler or from a bottom half handler.
652 */
653void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
654 smp_call_func_t func, void *info, bool wait,
655 gfp_t gfp_flags)
656{
657 cpumask_var_t cpus;
658 int cpu, ret;
659
Mel Gormand0164ad2015-11-06 16:28:21 -0800660 might_sleep_if(gfpflags_allow_blocking(gfp_flags));
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700661
662 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
663 preempt_disable();
664 for_each_online_cpu(cpu)
665 if (cond_func(cpu, info))
666 cpumask_set_cpu(cpu, cpus);
667 on_each_cpu_mask(cpus, func, info, wait);
668 preempt_enable();
669 free_cpumask_var(cpus);
670 } else {
671 /*
672 * No free cpumask, bother. No matter, we'll
673 * just have to IPI them one by one.
674 */
675 preempt_disable();
676 for_each_online_cpu(cpu)
677 if (cond_func(cpu, info)) {
678 ret = smp_call_function_single(cpu, func,
679 info, wait);
Sasha Levin618fde872014-08-06 16:08:14 -0700680 WARN_ON_ONCE(ret);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700681 }
682 preempt_enable();
683 }
684}
685EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000686
687static void do_nothing(void *unused)
688{
689}
690
691/**
692 * kick_all_cpus_sync - Force all cpus out of idle
693 *
694 * Used to synchronize the update of pm_idle function pointer. It's
695 * called after the pointer is updated and returns after the dummy
696 * callback function has been executed on all cpus. The execution of
697 * the function can only happen on the remote cpus after they have
698 * left the idle function which had been called via pm_idle function
699 * pointer. So it's guaranteed that nothing uses the previous pointer
700 * anymore.
701 */
702void kick_all_cpus_sync(void)
703{
704 /* Make sure the change is visible before we kick the cpus */
705 smp_mb();
706 smp_call_function(do_nothing, NULL, 1);
707}
708EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
Chuansheng Liuc6f44592014-09-04 15:17:54 +0800709
710/**
711 * wake_up_all_idle_cpus - break all cpus out of idle
712 * wake_up_all_idle_cpus try to break all cpus which is in idle state even
713 * including idle polling cpus, for non-idle cpus, we will do nothing
714 * for them.
715 */
716void wake_up_all_idle_cpus(void)
717{
718 int cpu;
719
720 preempt_disable();
721 for_each_online_cpu(cpu) {
722 if (cpu == smp_processor_id())
723 continue;
724
725 wake_up_if_idle(cpu);
726 }
727 preempt_enable();
728}
729EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200730
731/**
732 * smp_call_on_cpu - Call a function on a specific cpu
733 *
734 * Used to call a function on a specific cpu and wait for it to return.
735 * Optionally make sure the call is done on a specified physical cpu via vcpu
736 * pinning in order to support virtualized environments.
737 */
738struct smp_call_on_cpu_struct {
739 struct work_struct work;
740 struct completion done;
741 int (*func)(void *);
742 void *data;
743 int ret;
744 int cpu;
745};
746
747static void smp_call_on_cpu_callback(struct work_struct *work)
748{
749 struct smp_call_on_cpu_struct *sscs;
750
751 sscs = container_of(work, struct smp_call_on_cpu_struct, work);
752 if (sscs->cpu >= 0)
753 hypervisor_pin_vcpu(sscs->cpu);
754 sscs->ret = sscs->func(sscs->data);
755 if (sscs->cpu >= 0)
756 hypervisor_pin_vcpu(-1);
757
758 complete(&sscs->done);
759}
760
761int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
762{
763 struct smp_call_on_cpu_struct sscs = {
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200764 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
765 .func = func,
766 .data = par,
767 .cpu = phys ? cpu : -1,
768 };
769
Peter Zijlstra8db54942016-09-11 10:36:26 +0200770 INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
771
Juergen Grossdf8ce9d2016-08-29 08:48:44 +0200772 if (cpu >= nr_cpu_ids || !cpu_online(cpu))
773 return -ENXIO;
774
775 queue_work_on(cpu, system_wq, &sscs.work);
776 wait_for_completion(&sscs.done);
777
778 return sscs.ret;
779}
780EXPORT_SYMBOL_GPL(smp_call_on_cpu);