blob: a1812d184aed05142808ce03126b84306bebddfb [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
33static int
34hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
35{
36 long cpu = (long)hcpu;
37 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
38
39 switch (action) {
40 case CPU_UP_PREPARE:
41 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070042 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010043 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070044 return notifier_from_errno(-ENOMEM);
Shaohua Li9a46ad62013-02-21 16:43:03 -080045 cfd->csd = alloc_percpu(struct call_single_data);
46 if (!cfd->csd) {
47 free_cpumask_var(cfd->cpumask);
48 return notifier_from_errno(-ENOMEM);
49 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010050 break;
51
Xiao Guangrong69dd6472009-08-06 15:07:29 -070052#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010053 case CPU_UP_CANCELED:
54 case CPU_UP_CANCELED_FROZEN:
55
56 case CPU_DEAD:
57 case CPU_DEAD_FROZEN:
58 free_cpumask_var(cfd->cpumask);
Shaohua Li9a46ad62013-02-21 16:43:03 -080059 free_percpu(cfd->csd);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010060 break;
61#endif
62 };
63
64 return NOTIFY_OK;
65}
66
Paul Gortmaker0db06282013-06-19 14:53:51 -040067static struct notifier_block hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010068 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010069};
70
Takao Indohd8ad7d12011-03-29 12:35:04 -040071void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020072{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010073 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020074 int i;
75
Christoph Hellwig6897fc22014-01-30 15:45:47 -080076 for_each_possible_cpu(i)
77 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010078
79 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
80 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020081}
82
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010083/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010084 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
85 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +010086 * For non-synchronous ipi calls the csd can still be in use by the
87 * previous function call. For multi-cpu calls its even more interesting
88 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010089 */
Andrew Mortone1d12f32013-04-30 15:27:28 -070090static void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010091{
Andrew Mortone1d12f32013-04-30 15:27:28 -070092 while (csd->flags & CSD_FLAG_LOCK)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010093 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +010094}
95
Andrew Mortone1d12f32013-04-30 15:27:28 -070096static void csd_lock(struct call_single_data *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +010097{
Andrew Mortone1d12f32013-04-30 15:27:28 -070098 csd_lock_wait(csd);
99 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100100
101 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100102 * prevent CPU from reordering the above assignment
103 * to ->flags with any subsequent assignments to other
104 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100105 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100106 smp_mb();
107}
108
Andrew Mortone1d12f32013-04-30 15:27:28 -0700109static void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100110{
Jens Axboec84a83e2013-05-17 09:58:43 +0200111 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100112
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100113 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100114 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100115 */
116 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100117
Andrew Mortone1d12f32013-04-30 15:27:28 -0700118 csd->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200119}
120
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100121static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
122
Jens Axboe3d442232008-06-26 11:21:34 +0200123/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100124 * Insert a previously allocated call_single_data element
125 * for execution on the given CPU. data must already have
126 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200127 */
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100128static int generic_exec_single(int cpu, struct call_single_data *csd,
129 smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200130{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100131 struct call_single_data csd_stack = { .flags = 0 };
132 unsigned long flags;
133
134
135 if (cpu == smp_processor_id()) {
136 local_irq_save(flags);
137 func(info);
138 local_irq_restore(flags);
139 return 0;
140 }
141
142
143 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu))
144 return -ENXIO;
145
146
147 if (!csd) {
148 csd = &csd_stack;
149 if (!wait)
150 csd = &__get_cpu_var(csd_data);
151 }
152
153 csd_lock(csd);
154
155 csd->func = func;
156 csd->info = info;
157
Jens Axboec84a83e2013-05-17 09:58:43 +0200158 if (wait)
159 csd->flags |= CSD_FLAG_WAIT;
160
Suresh Siddha561920a02008-10-30 18:28:41 +0100161 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100162 * The list addition should be visible before sending the IPI
163 * handler locks the list to pull the entry off it because of
164 * normal cache coherency rules implied by spinlocks.
165 *
166 * If IPIs can go out of order to the cache coherency protocol
167 * in an architecture, sufficient synchronisation should be added
168 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100169 * locking and barrier primitives. Generic code isn't really
170 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100171 */
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800172 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Jens Axboe3d442232008-06-26 11:21:34 +0200173 arch_send_call_function_single_ipi(cpu);
174
175 if (wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700176 csd_lock_wait(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100177
178 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200179}
180
181/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100182 * Invoked by arch to handle an IPI for call function single. Must be
183 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200184 */
185void generic_smp_call_function_single_interrupt(void)
186{
Jan Kara5fd77592014-02-24 16:39:55 +0100187 struct llist_node *entry;
188 struct call_single_data *csd, *csd_next;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700189 static bool warned;
190
191 entry = llist_del_all(&__get_cpu_var(call_single_queue));
192 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200193
Suresh Siddha269c8612009-08-19 18:05:35 -0700194 /*
195 * Shouldn't receive this interrupt on a cpu that is not yet online.
196 */
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700197 if (unlikely(!cpu_online(smp_processor_id()) && !warned)) {
198 warned = true;
199 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700200
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700201 /*
202 * We don't have to use the _safe() variant here
203 * because we are not invoking the IPI handlers yet.
204 */
205 llist_for_each_entry(csd, entry, llist)
206 pr_warn("IPI callback %pS sent to offline CPU\n",
207 csd->func);
208 }
Jens Axboe3d442232008-06-26 11:21:34 +0200209
Jan Kara5fd77592014-02-24 16:39:55 +0100210 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700211 csd->func(csd->info);
Xie XiuQi46591962013-07-30 11:06:09 +0800212 csd_unlock(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200213 }
Frederic Weisbecker47885012014-05-08 01:37:48 +0200214
215 /*
216 * Handle irq works queued remotely by irq_work_queue_on().
217 * Smp functions above are typically synchronous so they
218 * better run first since some other CPUs may be busy waiting
219 * for them.
220 */
221 irq_work_run();
Jens Axboe3d442232008-06-26 11:21:34 +0200222}
223
224/*
225 * smp_call_function_single - Run a function on a specific CPU
226 * @func: The function to run. This must be fast and non-blocking.
227 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200228 * @wait: If true, wait until function has completed on other CPUs.
229 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800230 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200231 */
David Howells3a5f65df2010-10-27 17:28:36 +0100232int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200233 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200234{
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100235 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100236 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200237
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100238 /*
239 * prevent preemption and reschedule on another processor,
240 * as well as CPU removal
241 */
242 this_cpu = get_cpu();
243
Suresh Siddha269c8612009-08-19 18:05:35 -0700244 /*
245 * Can deadlock when called with interrupts disabled.
246 * We allow cpu's that are not yet online though, as no one else can
247 * send smp call function interrupt to this cpu and as such deadlocks
248 * can't happen.
249 */
250 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
251 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200252
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100253 err = generic_exec_single(cpu, NULL, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200254
255 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100256
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700257 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200258}
259EXPORT_SYMBOL(smp_call_function_single);
260
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100261/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100262 * smp_call_function_single_async(): Run an asynchronous function on a
263 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100264 * @cpu: The CPU to run on.
265 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100266 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100267 * Like smp_call_function_single(), but the call is asynchonous and
268 * can thus be done from contexts with disabled interrupts.
269 *
270 * The caller passes his own pre-allocated data structure
271 * (ie: embedded in an object) and is responsible for synchronizing it
272 * such that the IPIs performed on the @csd are strictly serialized.
273 *
274 * NOTE: Be careful, there is unfortunately no current debugging facility to
275 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100276 */
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100277int smp_call_function_single_async(int cpu, struct call_single_data *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100278{
279 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100280
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100281 preempt_disable();
282 err = generic_exec_single(cpu, csd, csd->func, csd->info, 0);
283 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100284
285 return err;
286}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100287EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100288
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800289/*
290 * smp_call_function_any - Run a function on any of the given cpus
291 * @mask: The mask of cpus it can run on.
292 * @func: The function to run. This must be fast and non-blocking.
293 * @info: An arbitrary pointer to pass to the function.
294 * @wait: If true, wait until function has completed.
295 *
296 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800297 *
298 * Selection preference:
299 * 1) current cpu if in @mask
300 * 2) any cpu of current node if in @mask
301 * 3) any other online cpu in @mask
302 */
303int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100304 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800305{
306 unsigned int cpu;
307 const struct cpumask *nodemask;
308 int ret;
309
310 /* Try for same CPU (cheapest) */
311 cpu = get_cpu();
312 if (cpumask_test_cpu(cpu, mask))
313 goto call;
314
315 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800316 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800317 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
318 cpu = cpumask_next_and(cpu, nodemask, mask)) {
319 if (cpu_online(cpu))
320 goto call;
321 }
322
323 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
324 cpu = cpumask_any_and(mask, cpu_online_mask);
325call:
326 ret = smp_call_function_single(cpu, func, info, wait);
327 put_cpu();
328 return ret;
329}
330EXPORT_SYMBOL_GPL(smp_call_function_any);
331
Jens Axboe3d442232008-06-26 11:21:34 +0200332/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030333 * smp_call_function_many(): Run a function on a set of other CPUs.
334 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200335 * @func: The function to run. This must be fast and non-blocking.
336 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100337 * @wait: If true, wait (atomically) until function has completed
338 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200339 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800340 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200341 *
342 * You must not call this function with disabled interrupts or from a
343 * hardware interrupt handler or from a bottom half handler. Preemption
344 * must be disabled when calling this function.
345 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030346void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100347 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200348{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700349 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800350 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200351
Suresh Siddha269c8612009-08-19 18:05:35 -0700352 /*
353 * Can deadlock when called with interrupts disabled.
354 * We allow cpu's that are not yet online though, as no one else can
355 * send smp call function interrupt to this cpu and as such deadlocks
356 * can't happen.
357 */
358 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100359 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200360
Milton Miller723aae22011-03-15 13:27:17 -0600361 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030362 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100363 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030364 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100365
Rusty Russell54b11e62008-12-30 09:05:16 +1030366 /* No online cpus? We're done. */
367 if (cpu >= nr_cpu_ids)
368 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200369
Rusty Russell54b11e62008-12-30 09:05:16 +1030370 /* Do we have another CPU which isn't us? */
371 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100372 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030373 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
374
375 /* Fastpath: do that cpu by itself. */
376 if (next_cpu >= nr_cpu_ids) {
377 smp_call_function_single(cpu, func, info, wait);
378 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200379 }
380
Andrew Mortone1d12f32013-04-30 15:27:28 -0700381 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600382
Andrew Mortone1d12f32013-04-30 15:27:28 -0700383 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
384 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600385
386 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700387 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600388 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800389
Andrew Mortone1d12f32013-04-30 15:27:28 -0700390 for_each_cpu(cpu, cfd->cpumask) {
391 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800392
393 csd_lock(csd);
394 csd->func = func;
395 csd->info = info;
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800396 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
Shaohua Li9a46ad62013-02-21 16:43:03 -0800397 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100398
Jens Axboe3d442232008-06-26 11:21:34 +0200399 /* Send a message to all CPUs in the map */
Roman Gushchin73f94552014-01-30 15:45:48 -0800400 arch_send_call_function_ipi_mask(cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200401
Shaohua Li9a46ad62013-02-21 16:43:03 -0800402 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700403 for_each_cpu(cpu, cfd->cpumask) {
404 struct call_single_data *csd;
405
406 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800407 csd_lock_wait(csd);
408 }
409 }
Jens Axboe3d442232008-06-26 11:21:34 +0200410}
Rusty Russell54b11e62008-12-30 09:05:16 +1030411EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200412
413/**
414 * smp_call_function(): Run a function on all other CPUs.
415 * @func: The function to run. This must be fast and non-blocking.
416 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100417 * @wait: If true, wait (atomically) until function has completed
418 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200419 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030420 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200421 *
422 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800423 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200424 *
425 * You must not call this function with disabled interrupts or from a
426 * hardware interrupt handler or from a bottom half handler.
427 */
David Howells3a5f65df2010-10-27 17:28:36 +0100428int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200429{
Jens Axboe3d442232008-06-26 11:21:34 +0200430 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030431 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200432 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100433
Rusty Russell54b11e62008-12-30 09:05:16 +1030434 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200435}
436EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800437
Amerigo Wang34db18a02011-03-22 16:34:06 -0700438/* Setup configured maximum number of CPUs to activate */
439unsigned int setup_max_cpus = NR_CPUS;
440EXPORT_SYMBOL(setup_max_cpus);
441
442
443/*
444 * Setup routine for controlling SMP activation
445 *
446 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
447 * activation entirely (the MPS table probe still happens, though).
448 *
449 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
450 * greater than 0, limits the maximum number of CPUs activated in
451 * SMP mode to <NUM>.
452 */
453
454void __weak arch_disable_smp_support(void) { }
455
456static int __init nosmp(char *str)
457{
458 setup_max_cpus = 0;
459 arch_disable_smp_support();
460
461 return 0;
462}
463
464early_param("nosmp", nosmp);
465
466/* this is hard limit */
467static int __init nrcpus(char *str)
468{
469 int nr_cpus;
470
471 get_option(&str, &nr_cpus);
472 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
473 nr_cpu_ids = nr_cpus;
474
475 return 0;
476}
477
478early_param("nr_cpus", nrcpus);
479
480static int __init maxcpus(char *str)
481{
482 get_option(&str, &setup_max_cpus);
483 if (setup_max_cpus == 0)
484 arch_disable_smp_support();
485
486 return 0;
487}
488
489early_param("maxcpus", maxcpus);
490
491/* Setup number of possible processor ids */
492int nr_cpu_ids __read_mostly = NR_CPUS;
493EXPORT_SYMBOL(nr_cpu_ids);
494
495/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
496void __init setup_nr_cpu_ids(void)
497{
498 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
499}
500
Borislav Petkova17bce42013-09-30 11:56:24 +0200501void __weak smp_announce(void)
502{
503 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
504}
505
Amerigo Wang34db18a02011-03-22 16:34:06 -0700506/* Called by boot processor to activate the rest. */
507void __init smp_init(void)
508{
509 unsigned int cpu;
510
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700511 idle_threads_init();
512
Amerigo Wang34db18a02011-03-22 16:34:06 -0700513 /* FIXME: This should be done in userspace --RR */
514 for_each_present_cpu(cpu) {
515 if (num_online_cpus() >= setup_max_cpus)
516 break;
517 if (!cpu_online(cpu))
518 cpu_up(cpu);
519 }
520
521 /* Any cleanup work */
Borislav Petkova17bce42013-09-30 11:56:24 +0200522 smp_announce();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700523 smp_cpus_done(setup_max_cpus);
524}
525
Amerigo Wang351f8f82011-01-12 16:59:39 -0800526/*
Tejun Heobd924e82011-01-20 12:07:13 +0100527 * Call a function on all processors. May be used during early boot while
528 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
529 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800530 */
531int on_each_cpu(void (*func) (void *info), void *info, int wait)
532{
Tejun Heobd924e82011-01-20 12:07:13 +0100533 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800534 int ret = 0;
535
536 preempt_disable();
537 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100538 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800539 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100540 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800541 preempt_enable();
542 return ret;
543}
544EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700545
546/**
547 * on_each_cpu_mask(): Run a function on processors specified by
548 * cpumask, which may include the local processor.
549 * @mask: The set of cpus to run on (only runs on online subset).
550 * @func: The function to run. This must be fast and non-blocking.
551 * @info: An arbitrary pointer to pass to the function.
552 * @wait: If true, wait (atomically) until function has completed
553 * on other CPUs.
554 *
555 * If @wait is true, then returns once @func has returned.
556 *
David Daney202da402013-09-11 14:23:29 -0700557 * You must not call this function with disabled interrupts or from a
558 * hardware interrupt handler or from a bottom half handler. The
559 * exception is that it may be used during early boot while
560 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700561 */
562void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
563 void *info, bool wait)
564{
565 int cpu = get_cpu();
566
567 smp_call_function_many(mask, func, info, wait);
568 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700569 unsigned long flags;
570 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700571 func(info);
David Daney202da402013-09-11 14:23:29 -0700572 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700573 }
574 put_cpu();
575}
576EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700577
578/*
579 * on_each_cpu_cond(): Call a function on each processor for which
580 * the supplied function cond_func returns true, optionally waiting
581 * for all the required CPUs to finish. This may include the local
582 * processor.
583 * @cond_func: A callback function that is passed a cpu id and
584 * the the info parameter. The function is called
585 * with preemption disabled. The function should
586 * return a blooean value indicating whether to IPI
587 * the specified CPU.
588 * @func: The function to run on all applicable CPUs.
589 * This must be fast and non-blocking.
590 * @info: An arbitrary pointer to pass to both functions.
591 * @wait: If true, wait (atomically) until function has
592 * completed on other CPUs.
593 * @gfp_flags: GFP flags to use when allocating the cpumask
594 * used internally by the function.
595 *
596 * The function might sleep if the GFP flags indicates a non
597 * atomic allocation is allowed.
598 *
599 * Preemption is disabled to protect against CPUs going offline but not online.
600 * CPUs going online during the call will not be seen or sent an IPI.
601 *
602 * You must not call this function with disabled interrupts or
603 * from a hardware interrupt handler or from a bottom half handler.
604 */
605void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
606 smp_call_func_t func, void *info, bool wait,
607 gfp_t gfp_flags)
608{
609 cpumask_var_t cpus;
610 int cpu, ret;
611
612 might_sleep_if(gfp_flags & __GFP_WAIT);
613
614 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
615 preempt_disable();
616 for_each_online_cpu(cpu)
617 if (cond_func(cpu, info))
618 cpumask_set_cpu(cpu, cpus);
619 on_each_cpu_mask(cpus, func, info, wait);
620 preempt_enable();
621 free_cpumask_var(cpus);
622 } else {
623 /*
624 * No free cpumask, bother. No matter, we'll
625 * just have to IPI them one by one.
626 */
627 preempt_disable();
628 for_each_online_cpu(cpu)
629 if (cond_func(cpu, info)) {
630 ret = smp_call_function_single(cpu, func,
631 info, wait);
632 WARN_ON_ONCE(!ret);
633 }
634 preempt_enable();
635 }
636}
637EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000638
639static void do_nothing(void *unused)
640{
641}
642
643/**
644 * kick_all_cpus_sync - Force all cpus out of idle
645 *
646 * Used to synchronize the update of pm_idle function pointer. It's
647 * called after the pointer is updated and returns after the dummy
648 * callback function has been executed on all cpus. The execution of
649 * the function can only happen on the remote cpus after they have
650 * left the idle function which had been called via pm_idle function
651 * pointer. So it's guaranteed that nothing uses the previous pointer
652 * anymore.
653 */
654void kick_all_cpus_sync(void)
655{
656 /* Make sure the change is visible before we kick the cpus */
657 smp_mb();
658 smp_call_function(do_nothing, NULL, 1);
659}
660EXPORT_SYMBOL_GPL(kick_all_cpus_sync);