blob: aff8aa14f54795faa42ae9482da75e3ea9610a82 [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 Torvalds59190f4212008-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>
Jens Axboe3d442232008-06-26 11:21:34 +020016
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070017#include "smpboot.h"
18
Jens Axboe3d442232008-06-26 11:21:34 +020019enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010020 CSD_FLAG_LOCK = 0x01,
Jens Axboec84a83e2013-05-17 09:58:43 +020021 CSD_FLAG_WAIT = 0x02,
Jens Axboe3d442232008-06-26 11:21:34 +020022};
23
24struct call_function_data {
Shaohua Li9a46ad62013-02-21 16:43:03 -080025 struct call_single_data __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010026 cpumask_var_t cpumask;
Jens Axboe3d442232008-06-26 11:21:34 +020027};
28
Milton Millere03bcb62010-01-18 13:00:51 +110029static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
30
Christoph Hellwig6897fc22014-01-30 15:45:47 -080031static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010032
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070033static void flush_smp_call_function_queue(bool warn_cpu_offline);
34
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010035static int
36hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
37{
38 long cpu = (long)hcpu;
39 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
40
41 switch (action) {
42 case CPU_UP_PREPARE:
43 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070044 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010045 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070046 return notifier_from_errno(-ENOMEM);
Shaohua Li9a46ad62013-02-21 16:43:03 -080047 cfd->csd = alloc_percpu(struct call_single_data);
48 if (!cfd->csd) {
49 free_cpumask_var(cfd->cpumask);
50 return notifier_from_errno(-ENOMEM);
51 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010052 break;
53
Xiao Guangrong69dd6472009-08-06 15:07:29 -070054#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010055 case CPU_UP_CANCELED:
56 case CPU_UP_CANCELED_FROZEN:
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070057 /* Fall-through to the CPU_DEAD[_FROZEN] case. */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010058
59 case CPU_DEAD:
60 case CPU_DEAD_FROZEN:
61 free_cpumask_var(cfd->cpumask);
Shaohua Li9a46ad62013-02-21 16:43:03 -080062 free_percpu(cfd->csd);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010063 break;
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070064
65 case CPU_DYING:
66 case CPU_DYING_FROZEN:
67 /*
68 * The IPIs for the smp-call-function callbacks queued by other
69 * CPUs might arrive late, either due to hardware latencies or
70 * because this CPU disabled interrupts (inside stop-machine)
71 * before the IPIs were sent. So flush out any pending callbacks
72 * explicitly (without waiting for the IPIs to arrive), to
73 * ensure that the outgoing CPU doesn't go offline with work
74 * still pending.
75 */
76 flush_smp_call_function_queue(false);
77 break;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010078#endif
79 };
80
81 return NOTIFY_OK;
82}
83
Paul Gortmaker0db06282013-06-19 14:53:51 -040084static struct notifier_block hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010085 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010086};
87
Takao Indohd8ad7d12011-03-29 12:35:04 -040088void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020089{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010090 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020091 int i;
92
Christoph Hellwig6897fc22014-01-30 15:45:47 -080093 for_each_possible_cpu(i)
94 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010095
96 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
97 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020098}
99
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100100/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100101 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
102 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100103 * For non-synchronous ipi calls the csd can still be in use by the
104 * previous function call. For multi-cpu calls its even more interesting
105 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100106 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700107static void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100108{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700109 while (csd->flags & CSD_FLAG_LOCK)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100110 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100111}
112
Andrew Mortone1d12f32013-04-30 15:27:28 -0700113static void csd_lock(struct call_single_data *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
121 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100122 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100123 smp_mb();
124}
125
Andrew Mortone1d12f32013-04-30 15:27:28 -0700126static void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100127{
Jens Axboec84a83e2013-05-17 09:58:43 +0200128 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(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 */
133 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100134
Andrew Mortone1d12f32013-04-30 15:27:28 -0700135 csd->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200136}
137
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100138static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
139
Jens Axboe3d442232008-06-26 11:21:34 +0200140/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100141 * Insert a previously allocated call_single_data element
142 * for execution on the given CPU. data must already have
143 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200144 */
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100145static int generic_exec_single(int cpu, struct call_single_data *csd,
146 smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200147{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100148 struct call_single_data csd_stack = { .flags = 0 };
149 unsigned long flags;
150
151
152 if (cpu == smp_processor_id()) {
153 local_irq_save(flags);
154 func(info);
155 local_irq_restore(flags);
156 return 0;
157 }
158
159
160 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu))
161 return -ENXIO;
162
163
164 if (!csd) {
165 csd = &csd_stack;
166 if (!wait)
167 csd = &__get_cpu_var(csd_data);
168 }
169
170 csd_lock(csd);
171
172 csd->func = func;
173 csd->info = info;
174
Jens Axboec84a83e2013-05-17 09:58:43 +0200175 if (wait)
176 csd->flags |= CSD_FLAG_WAIT;
177
Suresh Siddha561920a02008-10-30 18:28:41 +0100178 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100179 * The list addition should be visible before sending the IPI
180 * handler locks the list to pull the entry off it because of
181 * normal cache coherency rules implied by spinlocks.
182 *
183 * If IPIs can go out of order to the cache coherency protocol
184 * in an architecture, sufficient synchronisation should be added
185 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100186 * locking and barrier primitives. Generic code isn't really
187 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100188 */
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800189 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Jens Axboe3d442232008-06-26 11:21:34 +0200190 arch_send_call_function_single_ipi(cpu);
191
192 if (wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700193 csd_lock_wait(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100194
195 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200196}
197
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700198/**
199 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
200 *
201 * Invoked by arch to handle an IPI for call function single.
202 * Must be called with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200203 */
204void generic_smp_call_function_single_interrupt(void)
205{
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700206 flush_smp_call_function_queue(true);
207}
208
209/**
210 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
211 *
212 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
213 * offline CPU. Skip this check if set to 'false'.
214 *
215 * Flush any pending smp-call-function callbacks queued on this CPU. This is
216 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
217 * to ensure that all pending IPI callbacks are run before it goes completely
218 * offline.
219 *
220 * Loop through the call_single_queue and run all the queued callbacks.
221 * Must be called with interrupts disabled.
222 */
223static void flush_smp_call_function_queue(bool warn_cpu_offline)
224{
225 struct llist_head *head;
Jan Kara5fd77592014-02-24 16:39:55 +0100226 struct llist_node *entry;
227 struct call_single_data *csd, *csd_next;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700228 static bool warned;
229
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700230 WARN_ON(!irqs_disabled());
231
232 head = &__get_cpu_var(call_single_queue);
233 entry = llist_del_all(head);
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700234 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200235
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700236 /* There shouldn't be any pending callbacks on an offline CPU. */
237 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
238 !warned && !llist_empty(head))) {
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700239 warned = true;
240 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700241
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700242 /*
243 * We don't have to use the _safe() variant here
244 * because we are not invoking the IPI handlers yet.
245 */
246 llist_for_each_entry(csd, entry, llist)
247 pr_warn("IPI callback %pS sent to offline CPU\n",
248 csd->func);
249 }
Jens Axboe3d442232008-06-26 11:21:34 +0200250
Jan Kara5fd77592014-02-24 16:39:55 +0100251 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700252 csd->func(csd->info);
Xie XiuQi46591962013-07-30 11:06:09 +0800253 csd_unlock(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200254 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200255
256 /*
257 * Handle irq works queued remotely by irq_work_queue_on().
258 * Smp functions above are typically synchronous so they
259 * better run first since some other CPUs may be busy waiting
260 * for them.
261 */
262 irq_work_run();
Jens Axboe3d442232008-06-26 11:21:34 +0200263}
264
265/*
266 * smp_call_function_single - Run a function on a specific CPU
267 * @func: The function to run. This must be fast and non-blocking.
268 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200269 * @wait: If true, wait until function has completed on other CPUs.
270 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800271 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200272 */
David Howells3a5f65df2010-10-27 17:28:36 +0100273int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200274 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200275{
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100276 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100277 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200278
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100279 /*
280 * prevent preemption and reschedule on another processor,
281 * as well as CPU removal
282 */
283 this_cpu = get_cpu();
284
Suresh Siddha269c8612009-08-19 18:05:35 -0700285 /*
286 * Can deadlock when called with interrupts disabled.
287 * We allow cpu's that are not yet online though, as no one else can
288 * send smp call function interrupt to this cpu and as such deadlocks
289 * can't happen.
290 */
291 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
292 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200293
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100294 err = generic_exec_single(cpu, NULL, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200295
296 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100297
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700298 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200299}
300EXPORT_SYMBOL(smp_call_function_single);
301
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100302/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100303 * smp_call_function_single_async(): Run an asynchronous function on a
304 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100305 * @cpu: The CPU to run on.
306 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100307 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100308 * Like smp_call_function_single(), but the call is asynchonous and
309 * can thus be done from contexts with disabled interrupts.
310 *
311 * The caller passes his own pre-allocated data structure
312 * (ie: embedded in an object) and is responsible for synchronizing it
313 * such that the IPIs performed on the @csd are strictly serialized.
314 *
315 * NOTE: Be careful, there is unfortunately no current debugging facility to
316 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100317 */
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100318int smp_call_function_single_async(int cpu, struct call_single_data *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100319{
320 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100321
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100322 preempt_disable();
323 err = generic_exec_single(cpu, csd, csd->func, csd->info, 0);
324 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100325
326 return err;
327}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100328EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100329
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800330/*
331 * smp_call_function_any - Run a function on any of the given cpus
332 * @mask: The mask of cpus it can run on.
333 * @func: The function to run. This must be fast and non-blocking.
334 * @info: An arbitrary pointer to pass to the function.
335 * @wait: If true, wait until function has completed.
336 *
337 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800338 *
339 * Selection preference:
340 * 1) current cpu if in @mask
341 * 2) any cpu of current node if in @mask
342 * 3) any other online cpu in @mask
343 */
344int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100345 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800346{
347 unsigned int cpu;
348 const struct cpumask *nodemask;
349 int ret;
350
351 /* Try for same CPU (cheapest) */
352 cpu = get_cpu();
353 if (cpumask_test_cpu(cpu, mask))
354 goto call;
355
356 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800357 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800358 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
359 cpu = cpumask_next_and(cpu, nodemask, mask)) {
360 if (cpu_online(cpu))
361 goto call;
362 }
363
364 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
365 cpu = cpumask_any_and(mask, cpu_online_mask);
366call:
367 ret = smp_call_function_single(cpu, func, info, wait);
368 put_cpu();
369 return ret;
370}
371EXPORT_SYMBOL_GPL(smp_call_function_any);
372
Jens Axboe3d442232008-06-26 11:21:34 +0200373/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030374 * smp_call_function_many(): Run a function on a set of other CPUs.
375 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200376 * @func: The function to run. This must be fast and non-blocking.
377 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100378 * @wait: If true, wait (atomically) until function has completed
379 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200380 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800381 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200382 *
383 * You must not call this function with disabled interrupts or from a
384 * hardware interrupt handler or from a bottom half handler. Preemption
385 * must be disabled when calling this function.
386 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030387void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100388 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200389{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700390 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800391 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200392
Suresh Siddha269c8612009-08-19 18:05:35 -0700393 /*
394 * Can deadlock when called with interrupts disabled.
395 * We allow cpu's that are not yet online though, as no one else can
396 * send smp call function interrupt to this cpu and as such deadlocks
397 * can't happen.
398 */
399 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100400 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200401
Milton Miller723aae22011-03-15 13:27:17 -0600402 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030403 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100404 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030405 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100406
Rusty Russell54b11e62008-12-30 09:05:16 +1030407 /* No online cpus? We're done. */
408 if (cpu >= nr_cpu_ids)
409 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200410
Rusty Russell54b11e62008-12-30 09:05:16 +1030411 /* Do we have another CPU which isn't us? */
412 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100413 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030414 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
415
416 /* Fastpath: do that cpu by itself. */
417 if (next_cpu >= nr_cpu_ids) {
418 smp_call_function_single(cpu, func, info, wait);
419 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200420 }
421
Andrew Mortone1d12f32013-04-30 15:27:28 -0700422 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600423
Andrew Mortone1d12f32013-04-30 15:27:28 -0700424 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
425 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600426
427 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700428 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600429 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800430
Andrew Mortone1d12f32013-04-30 15:27:28 -0700431 for_each_cpu(cpu, cfd->cpumask) {
432 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800433
434 csd_lock(csd);
435 csd->func = func;
436 csd->info = info;
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800437 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
Shaohua Li9a46ad62013-02-21 16:43:03 -0800438 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100439
Jens Axboe3d442232008-06-26 11:21:34 +0200440 /* Send a message to all CPUs in the map */
Roman Gushchin73f94552014-01-30 15:45:48 -0800441 arch_send_call_function_ipi_mask(cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200442
Shaohua Li9a46ad62013-02-21 16:43:03 -0800443 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700444 for_each_cpu(cpu, cfd->cpumask) {
445 struct call_single_data *csd;
446
447 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800448 csd_lock_wait(csd);
449 }
450 }
Jens Axboe3d442232008-06-26 11:21:34 +0200451}
Rusty Russell54b11e62008-12-30 09:05:16 +1030452EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200453
454/**
455 * smp_call_function(): Run a function on all other CPUs.
456 * @func: The function to run. This must be fast and non-blocking.
457 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100458 * @wait: If true, wait (atomically) until function has completed
459 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200460 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030461 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200462 *
463 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800464 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200465 *
466 * You must not call this function with disabled interrupts or from a
467 * hardware interrupt handler or from a bottom half handler.
468 */
David Howells3a5f65df2010-10-27 17:28:36 +0100469int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200470{
Jens Axboe3d442232008-06-26 11:21:34 +0200471 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030472 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200473 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100474
Rusty Russell54b11e62008-12-30 09:05:16 +1030475 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200476}
477EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800478
Amerigo Wang34db18a02011-03-22 16:34:06 -0700479/* Setup configured maximum number of CPUs to activate */
480unsigned int setup_max_cpus = NR_CPUS;
481EXPORT_SYMBOL(setup_max_cpus);
482
483
484/*
485 * Setup routine for controlling SMP activation
486 *
487 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
488 * activation entirely (the MPS table probe still happens, though).
489 *
490 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
491 * greater than 0, limits the maximum number of CPUs activated in
492 * SMP mode to <NUM>.
493 */
494
495void __weak arch_disable_smp_support(void) { }
496
497static int __init nosmp(char *str)
498{
499 setup_max_cpus = 0;
500 arch_disable_smp_support();
501
502 return 0;
503}
504
505early_param("nosmp", nosmp);
506
507/* this is hard limit */
508static int __init nrcpus(char *str)
509{
510 int nr_cpus;
511
512 get_option(&str, &nr_cpus);
513 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
514 nr_cpu_ids = nr_cpus;
515
516 return 0;
517}
518
519early_param("nr_cpus", nrcpus);
520
521static int __init maxcpus(char *str)
522{
523 get_option(&str, &setup_max_cpus);
524 if (setup_max_cpus == 0)
525 arch_disable_smp_support();
526
527 return 0;
528}
529
530early_param("maxcpus", maxcpus);
531
532/* Setup number of possible processor ids */
533int nr_cpu_ids __read_mostly = NR_CPUS;
534EXPORT_SYMBOL(nr_cpu_ids);
535
536/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
537void __init setup_nr_cpu_ids(void)
538{
539 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
540}
541
Borislav Petkova17bce42013-09-30 11:56:24 +0200542void __weak smp_announce(void)
543{
544 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
545}
546
Amerigo Wang34db18a02011-03-22 16:34:06 -0700547/* Called by boot processor to activate the rest. */
548void __init smp_init(void)
549{
550 unsigned int cpu;
551
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700552 idle_threads_init();
553
Amerigo Wang34db18a02011-03-22 16:34:06 -0700554 /* FIXME: This should be done in userspace --RR */
555 for_each_present_cpu(cpu) {
556 if (num_online_cpus() >= setup_max_cpus)
557 break;
558 if (!cpu_online(cpu))
559 cpu_up(cpu);
560 }
561
562 /* Any cleanup work */
Borislav Petkova17bce42013-09-30 11:56:24 +0200563 smp_announce();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700564 smp_cpus_done(setup_max_cpus);
565}
566
Amerigo Wang351f8f82011-01-12 16:59:39 -0800567/*
Tejun Heobd924e82011-01-20 12:07:13 +0100568 * Call a function on all processors. May be used during early boot while
569 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
570 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800571 */
572int on_each_cpu(void (*func) (void *info), void *info, int wait)
573{
Tejun Heobd924e82011-01-20 12:07:13 +0100574 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800575 int ret = 0;
576
577 preempt_disable();
578 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100579 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800580 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100581 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800582 preempt_enable();
583 return ret;
584}
585EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700586
587/**
588 * on_each_cpu_mask(): Run a function on processors specified by
589 * cpumask, which may include the local processor.
590 * @mask: The set of cpus to run on (only runs on online subset).
591 * @func: The function to run. This must be fast and non-blocking.
592 * @info: An arbitrary pointer to pass to the function.
593 * @wait: If true, wait (atomically) until function has completed
594 * on other CPUs.
595 *
596 * If @wait is true, then returns once @func has returned.
597 *
David Daney202da402013-09-11 14:23:29 -0700598 * You must not call this function with disabled interrupts or from a
599 * hardware interrupt handler or from a bottom half handler. The
600 * exception is that it may be used during early boot while
601 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700602 */
603void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
604 void *info, bool wait)
605{
606 int cpu = get_cpu();
607
608 smp_call_function_many(mask, func, info, wait);
609 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700610 unsigned long flags;
611 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700612 func(info);
David Daney202da402013-09-11 14:23:29 -0700613 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700614 }
615 put_cpu();
616}
617EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700618
619/*
620 * on_each_cpu_cond(): Call a function on each processor for which
621 * the supplied function cond_func returns true, optionally waiting
622 * for all the required CPUs to finish. This may include the local
623 * processor.
624 * @cond_func: A callback function that is passed a cpu id and
625 * the the info parameter. The function is called
626 * with preemption disabled. The function should
627 * return a blooean value indicating whether to IPI
628 * the specified CPU.
629 * @func: The function to run on all applicable CPUs.
630 * This must be fast and non-blocking.
631 * @info: An arbitrary pointer to pass to both functions.
632 * @wait: If true, wait (atomically) until function has
633 * completed on other CPUs.
634 * @gfp_flags: GFP flags to use when allocating the cpumask
635 * used internally by the function.
636 *
637 * The function might sleep if the GFP flags indicates a non
638 * atomic allocation is allowed.
639 *
640 * Preemption is disabled to protect against CPUs going offline but not online.
641 * CPUs going online during the call will not be seen or sent an IPI.
642 *
643 * You must not call this function with disabled interrupts or
644 * from a hardware interrupt handler or from a bottom half handler.
645 */
646void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
647 smp_call_func_t func, void *info, bool wait,
648 gfp_t gfp_flags)
649{
650 cpumask_var_t cpus;
651 int cpu, ret;
652
653 might_sleep_if(gfp_flags & __GFP_WAIT);
654
655 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
656 preempt_disable();
657 for_each_online_cpu(cpu)
658 if (cond_func(cpu, info))
659 cpumask_set_cpu(cpu, cpus);
660 on_each_cpu_mask(cpus, func, info, wait);
661 preempt_enable();
662 free_cpumask_var(cpus);
663 } else {
664 /*
665 * No free cpumask, bother. No matter, we'll
666 * just have to IPI them one by one.
667 */
668 preempt_disable();
669 for_each_online_cpu(cpu)
670 if (cond_func(cpu, info)) {
671 ret = smp_call_function_single(cpu, func,
672 info, wait);
Sasha Levin618fde872014-08-06 16:08:14 -0700673 WARN_ON_ONCE(ret);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700674 }
675 preempt_enable();
676 }
677}
678EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000679
680static void do_nothing(void *unused)
681{
682}
683
684/**
685 * kick_all_cpus_sync - Force all cpus out of idle
686 *
687 * Used to synchronize the update of pm_idle function pointer. It's
688 * called after the pointer is updated and returns after the dummy
689 * callback function has been executed on all cpus. The execution of
690 * the function can only happen on the remote cpus after they have
691 * left the idle function which had been called via pm_idle function
692 * pointer. So it's guaranteed that nothing uses the previous pointer
693 * anymore.
694 */
695void kick_all_cpus_sync(void)
696{
697 /* Make sure the change is visible before we kick the cpus */
698 smp_mb();
699 smp_call_function(do_nothing, NULL, 1);
700}
701EXPORT_SYMBOL_GPL(kick_all_cpus_sync);