blob: 8e451f3ff51b818796bf5ca4e4b3189a215b367a [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 */
Jens Axboe3d442232008-06-26 11:21:34 +02006#include <linux/rcupdate.h>
Linus Torvalds59190f42008-07-15 14:02:33 -07007#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +01008#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010010#include <linux/percpu.h>
11#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020013#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010014#include <linux/cpu.h>
Jens Axboe3d442232008-06-26 11:21:34 +020015
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070016#include "smpboot.h"
17
Amerigo Wang351f8f82011-01-12 16:59:39 -080018#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
Jens Axboe3d442232008-06-26 11:21:34 +020019enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010020 CSD_FLAG_LOCK = 0x01,
Jens Axboe3d442232008-06-26 11:21:34 +020021};
22
23struct call_function_data {
Shaohua Li9a46ad62013-02-21 16:43:03 -080024 struct call_single_data __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010025 cpumask_var_t cpumask;
Wang YanQingf44310b2013-01-26 15:53:57 +080026 cpumask_var_t cpumask_ipi;
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
Jens Axboe3d442232008-06-26 11:21:34 +020031struct call_single_queue {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010032 struct list_head list;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010033 raw_spinlock_t lock;
Jens Axboe3d442232008-06-26 11:21:34 +020034};
35
Milton Millere03bcb62010-01-18 13:00:51 +110036static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010037
38static int
39hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
40{
41 long cpu = (long)hcpu;
42 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
43
44 switch (action) {
45 case CPU_UP_PREPARE:
46 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070047 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010048 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070049 return notifier_from_errno(-ENOMEM);
Wang YanQingf44310b2013-01-26 15:53:57 +080050 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
51 cpu_to_node(cpu)))
52 return notifier_from_errno(-ENOMEM);
Shaohua Li9a46ad62013-02-21 16:43:03 -080053 cfd->csd = alloc_percpu(struct call_single_data);
54 if (!cfd->csd) {
55 free_cpumask_var(cfd->cpumask);
56 return notifier_from_errno(-ENOMEM);
57 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010058 break;
59
Xiao Guangrong69dd6472009-08-06 15:07:29 -070060#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010061 case CPU_UP_CANCELED:
62 case CPU_UP_CANCELED_FROZEN:
63
64 case CPU_DEAD:
65 case CPU_DEAD_FROZEN:
66 free_cpumask_var(cfd->cpumask);
Wang YanQingf44310b2013-01-26 15:53:57 +080067 free_cpumask_var(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -080068 free_percpu(cfd->csd);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010069 break;
70#endif
71 };
72
73 return NOTIFY_OK;
74}
75
76static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010077 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010078};
79
Takao Indohd8ad7d12011-03-29 12:35:04 -040080void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020081{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010082 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020083 int i;
84
85 for_each_possible_cpu(i) {
86 struct call_single_queue *q = &per_cpu(call_single_queue, i);
87
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010088 raw_spin_lock_init(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +020089 INIT_LIST_HEAD(&q->list);
90 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010091
92 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
93 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020094}
95
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010096/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010097 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
98 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +010099 * For non-synchronous ipi calls the csd can still be in use by the
100 * previous function call. For multi-cpu calls its even more interesting
101 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100102 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100103static void csd_lock_wait(struct call_single_data *data)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100104{
105 while (data->flags & CSD_FLAG_LOCK)
106 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100107}
108
109static void csd_lock(struct call_single_data *data)
110{
111 csd_lock_wait(data);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100112 data->flags = CSD_FLAG_LOCK;
113
114 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100115 * prevent CPU from reordering the above assignment
116 * to ->flags with any subsequent assignments to other
117 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100118 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100119 smp_mb();
120}
121
122static void csd_unlock(struct call_single_data *data)
123{
124 WARN_ON(!(data->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100125
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100126 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100127 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100128 */
129 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100130
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100131 data->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200132}
133
134/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100135 * Insert a previously allocated call_single_data element
136 * for execution on the given CPU. data must already have
137 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200138 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100139static
140void generic_exec_single(int cpu, struct call_single_data *data, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200141{
142 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
Jens Axboe3d442232008-06-26 11:21:34 +0200143 unsigned long flags;
Peter Zijlstra6e275632009-02-25 13:59:48 +0100144 int ipi;
Jens Axboe3d442232008-06-26 11:21:34 +0200145
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100146 raw_spin_lock_irqsave(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200147 ipi = list_empty(&dst->list);
148 list_add_tail(&data->list, &dst->list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100149 raw_spin_unlock_irqrestore(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200150
Suresh Siddha561920a02008-10-30 18:28:41 +0100151 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100152 * The list addition should be visible before sending the IPI
153 * handler locks the list to pull the entry off it because of
154 * normal cache coherency rules implied by spinlocks.
155 *
156 * If IPIs can go out of order to the cache coherency protocol
157 * in an architecture, sufficient synchronisation should be added
158 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100159 * locking and barrier primitives. Generic code isn't really
160 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100161 */
Jens Axboe3d442232008-06-26 11:21:34 +0200162 if (ipi)
163 arch_send_call_function_single_ipi(cpu);
164
165 if (wait)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100166 csd_lock_wait(data);
Jens Axboe3d442232008-06-26 11:21:34 +0200167}
168
169/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100170 * Invoked by arch to handle an IPI for call function single. Must be
171 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200172 */
173void generic_smp_call_function_single_interrupt(void)
174{
175 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100176 unsigned int data_flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100177 LIST_HEAD(list);
Jens Axboe3d442232008-06-26 11:21:34 +0200178
Suresh Siddha269c8612009-08-19 18:05:35 -0700179 /*
180 * Shouldn't receive this interrupt on a cpu that is not yet online.
181 */
182 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
183
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100184 raw_spin_lock(&q->lock);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100185 list_replace_init(&q->list, &list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100186 raw_spin_unlock(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200187
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100188 while (!list_empty(&list)) {
189 struct call_single_data *data;
Jens Axboe3d442232008-06-26 11:21:34 +0200190
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100191 data = list_entry(list.next, struct call_single_data, list);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100192 list_del(&data->list);
Jens Axboe3d442232008-06-26 11:21:34 +0200193
Jens Axboe3d442232008-06-26 11:21:34 +0200194 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100195 * 'data' can be invalid after this call if flags == 0
196 * (when called through generic_exec_single()),
197 * so save them away before making the call:
Jens Axboe3d442232008-06-26 11:21:34 +0200198 */
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100199 data_flags = data->flags;
200
201 data->func(data->info);
202
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100203 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100204 * Unlocked CSDs are valid through generic_exec_single():
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100205 */
206 if (data_flags & CSD_FLAG_LOCK)
207 csd_unlock(data);
Jens Axboe3d442232008-06-26 11:21:34 +0200208 }
209}
210
Milton Millere03bcb62010-01-18 13:00:51 +1100211static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500212
Jens Axboe3d442232008-06-26 11:21:34 +0200213/*
214 * smp_call_function_single - Run a function on a specific CPU
215 * @func: The function to run. This must be fast and non-blocking.
216 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200217 * @wait: If true, wait until function has completed on other CPUs.
218 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800219 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200220 */
David Howells3a5f65d2010-10-27 17:28:36 +0100221int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200222 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200223{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100224 struct call_single_data d = {
225 .flags = 0,
226 };
Jens Axboe3d442232008-06-26 11:21:34 +0200227 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100228 int this_cpu;
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700229 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200230
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100231 /*
232 * prevent preemption and reschedule on another processor,
233 * as well as CPU removal
234 */
235 this_cpu = get_cpu();
236
Suresh Siddha269c8612009-08-19 18:05:35 -0700237 /*
238 * Can deadlock when called with interrupts disabled.
239 * We allow cpu's that are not yet online though, as no one else can
240 * send smp call function interrupt to this cpu and as such deadlocks
241 * can't happen.
242 */
243 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
244 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200245
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100246 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200247 local_irq_save(flags);
248 func(info);
249 local_irq_restore(flags);
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700250 } else {
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100251 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
252 struct call_single_data *data = &d;
253
254 if (!wait)
255 data = &__get_cpu_var(csd_data);
256
257 csd_lock(data);
258
259 data->func = func;
260 data->info = info;
261 generic_exec_single(cpu, data, wait);
262 } else {
263 err = -ENXIO; /* CPU not online */
264 }
Jens Axboe3d442232008-06-26 11:21:34 +0200265 }
266
267 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100268
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700269 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200270}
271EXPORT_SYMBOL(smp_call_function_single);
272
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800273/*
274 * smp_call_function_any - Run a function on any of the given cpus
275 * @mask: The mask of cpus it can run on.
276 * @func: The function to run. This must be fast and non-blocking.
277 * @info: An arbitrary pointer to pass to the function.
278 * @wait: If true, wait until function has completed.
279 *
280 * Returns 0 on success, else a negative status code (if no cpus were online).
281 * Note that @wait will be implicitly turned on in case of allocation failures,
282 * since we fall back to on-stack allocation.
283 *
284 * Selection preference:
285 * 1) current cpu if in @mask
286 * 2) any cpu of current node if in @mask
287 * 3) any other online cpu in @mask
288 */
289int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100290 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800291{
292 unsigned int cpu;
293 const struct cpumask *nodemask;
294 int ret;
295
296 /* Try for same CPU (cheapest) */
297 cpu = get_cpu();
298 if (cpumask_test_cpu(cpu, mask))
299 goto call;
300
301 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800302 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800303 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
304 cpu = cpumask_next_and(cpu, nodemask, mask)) {
305 if (cpu_online(cpu))
306 goto call;
307 }
308
309 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
310 cpu = cpumask_any_and(mask, cpu_online_mask);
311call:
312 ret = smp_call_function_single(cpu, func, info, wait);
313 put_cpu();
314 return ret;
315}
316EXPORT_SYMBOL_GPL(smp_call_function_any);
317
Jens Axboe3d442232008-06-26 11:21:34 +0200318/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200319 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200320 * @cpu: The CPU to run on.
321 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200322 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200323 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100324 * Like smp_call_function_single(), but allow caller to pass in a
325 * pre-allocated data structure. Useful for embedding @data inside
326 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200327 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100328void __smp_call_function_single(int cpu, struct call_single_data *data,
329 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200330{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200331 unsigned int this_cpu;
332 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200333
Heiko Carstens27c379f2010-09-10 13:47:29 +0200334 this_cpu = get_cpu();
Suresh Siddha269c8612009-08-19 18:05:35 -0700335 /*
336 * Can deadlock when called with interrupts disabled.
337 * We allow cpu's that are not yet online though, as no one else can
338 * send smp call function interrupt to this cpu and as such deadlocks
339 * can't happen.
340 */
341 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
342 && !oops_in_progress);
Peter Zijlstra6e275632009-02-25 13:59:48 +0100343
Heiko Carstens27c379f2010-09-10 13:47:29 +0200344 if (cpu == this_cpu) {
345 local_irq_save(flags);
346 data->func(data->info);
347 local_irq_restore(flags);
348 } else {
349 csd_lock(data);
350 generic_exec_single(cpu, data, wait);
351 }
352 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200353}
354
355/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030356 * smp_call_function_many(): Run a function on a set of other CPUs.
357 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200358 * @func: The function to run. This must be fast and non-blocking.
359 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100360 * @wait: If true, wait (atomically) until function has completed
361 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200362 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800363 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200364 *
365 * You must not call this function with disabled interrupts or from a
366 * hardware interrupt handler or from a bottom half handler. Preemption
367 * must be disabled when calling this function.
368 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030369void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100370 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200371{
Rusty Russell54b11e62008-12-30 09:05:16 +1030372 struct call_function_data *data;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800373 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200374
Suresh Siddha269c8612009-08-19 18:05:35 -0700375 /*
376 * Can deadlock when called with interrupts disabled.
377 * We allow cpu's that are not yet online though, as no one else can
378 * send smp call function interrupt to this cpu and as such deadlocks
379 * can't happen.
380 */
381 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100382 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200383
Milton Miller723aae22011-03-15 13:27:17 -0600384 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030385 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100386 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030387 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100388
Rusty Russell54b11e62008-12-30 09:05:16 +1030389 /* No online cpus? We're done. */
390 if (cpu >= nr_cpu_ids)
391 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200392
Rusty Russell54b11e62008-12-30 09:05:16 +1030393 /* Do we have another CPU which isn't us? */
394 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100395 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030396 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
397
398 /* Fastpath: do that cpu by itself. */
399 if (next_cpu >= nr_cpu_ids) {
400 smp_call_function_single(cpu, func, info, wait);
401 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200402 }
403
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100404 data = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600405
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100406 cpumask_and(data->cpumask, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100407 cpumask_clear_cpu(this_cpu, data->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600408
409 /* Some callers race with other cpus changing the passed mask */
Shaohua Li9a46ad62013-02-21 16:43:03 -0800410 if (unlikely(!cpumask_weight(data->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600411 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800412
Wang YanQingf44310b2013-01-26 15:53:57 +0800413 /*
414 * After we put an entry into the list, data->cpumask
415 * may be cleared again when another CPU sends another IPI for
416 * a SMP function call, so data->cpumask will be zero.
417 */
418 cpumask_copy(data->cpumask_ipi, data->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200419
Shaohua Li9a46ad62013-02-21 16:43:03 -0800420 for_each_cpu(cpu, data->cpumask) {
421 struct call_single_data *csd = per_cpu_ptr(data->csd, cpu);
422 struct call_single_queue *dst =
423 &per_cpu(call_single_queue, cpu);
424 unsigned long flags;
425
426 csd_lock(csd);
427 csd->func = func;
428 csd->info = info;
429
430 raw_spin_lock_irqsave(&dst->lock, flags);
431 list_add_tail(&csd->list, &dst->list);
432 raw_spin_unlock_irqrestore(&dst->lock, flags);
433 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100434
Jens Axboe3d442232008-06-26 11:21:34 +0200435 /* Send a message to all CPUs in the map */
Wang YanQingf44310b2013-01-26 15:53:57 +0800436 arch_send_call_function_ipi_mask(data->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200437
Shaohua Li9a46ad62013-02-21 16:43:03 -0800438 if (wait) {
439 for_each_cpu(cpu, data->cpumask) {
440 struct call_single_data *csd =
441 per_cpu_ptr(data->csd, cpu);
442 csd_lock_wait(csd);
443 }
444 }
Jens Axboe3d442232008-06-26 11:21:34 +0200445}
Rusty Russell54b11e62008-12-30 09:05:16 +1030446EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200447
448/**
449 * smp_call_function(): Run a function on all other CPUs.
450 * @func: The function to run. This must be fast and non-blocking.
451 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100452 * @wait: If true, wait (atomically) until function has completed
453 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200454 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030455 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200456 *
457 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800458 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200459 *
460 * You must not call this function with disabled interrupts or from a
461 * hardware interrupt handler or from a bottom half handler.
462 */
David Howells3a5f65d2010-10-27 17:28:36 +0100463int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200464{
Jens Axboe3d442232008-06-26 11:21:34 +0200465 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030466 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200467 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100468
Rusty Russell54b11e62008-12-30 09:05:16 +1030469 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200470}
471EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800472#endif /* USE_GENERIC_SMP_HELPERS */
473
Amerigo Wang34db18a02011-03-22 16:34:06 -0700474/* Setup configured maximum number of CPUs to activate */
475unsigned int setup_max_cpus = NR_CPUS;
476EXPORT_SYMBOL(setup_max_cpus);
477
478
479/*
480 * Setup routine for controlling SMP activation
481 *
482 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
483 * activation entirely (the MPS table probe still happens, though).
484 *
485 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
486 * greater than 0, limits the maximum number of CPUs activated in
487 * SMP mode to <NUM>.
488 */
489
490void __weak arch_disable_smp_support(void) { }
491
492static int __init nosmp(char *str)
493{
494 setup_max_cpus = 0;
495 arch_disable_smp_support();
496
497 return 0;
498}
499
500early_param("nosmp", nosmp);
501
502/* this is hard limit */
503static int __init nrcpus(char *str)
504{
505 int nr_cpus;
506
507 get_option(&str, &nr_cpus);
508 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
509 nr_cpu_ids = nr_cpus;
510
511 return 0;
512}
513
514early_param("nr_cpus", nrcpus);
515
516static int __init maxcpus(char *str)
517{
518 get_option(&str, &setup_max_cpus);
519 if (setup_max_cpus == 0)
520 arch_disable_smp_support();
521
522 return 0;
523}
524
525early_param("maxcpus", maxcpus);
526
527/* Setup number of possible processor ids */
528int nr_cpu_ids __read_mostly = NR_CPUS;
529EXPORT_SYMBOL(nr_cpu_ids);
530
531/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
532void __init setup_nr_cpu_ids(void)
533{
534 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
535}
536
537/* Called by boot processor to activate the rest. */
538void __init smp_init(void)
539{
540 unsigned int cpu;
541
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700542 idle_threads_init();
543
Amerigo Wang34db18a02011-03-22 16:34:06 -0700544 /* FIXME: This should be done in userspace --RR */
545 for_each_present_cpu(cpu) {
546 if (num_online_cpus() >= setup_max_cpus)
547 break;
548 if (!cpu_online(cpu))
549 cpu_up(cpu);
550 }
551
552 /* Any cleanup work */
553 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
554 smp_cpus_done(setup_max_cpus);
555}
556
Amerigo Wang351f8f82011-01-12 16:59:39 -0800557/*
Tejun Heobd924e82011-01-20 12:07:13 +0100558 * Call a function on all processors. May be used during early boot while
559 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
560 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800561 */
562int on_each_cpu(void (*func) (void *info), void *info, int wait)
563{
Tejun Heobd924e82011-01-20 12:07:13 +0100564 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800565 int ret = 0;
566
567 preempt_disable();
568 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100569 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800570 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100571 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800572 preempt_enable();
573 return ret;
574}
575EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700576
577/**
578 * on_each_cpu_mask(): Run a function on processors specified by
579 * cpumask, which may include the local processor.
580 * @mask: The set of cpus to run on (only runs on online subset).
581 * @func: The function to run. This must be fast and non-blocking.
582 * @info: An arbitrary pointer to pass to the function.
583 * @wait: If true, wait (atomically) until function has completed
584 * on other CPUs.
585 *
586 * If @wait is true, then returns once @func has returned.
587 *
588 * You must not call this function with disabled interrupts or
589 * from a hardware interrupt handler or from a bottom half handler.
590 */
591void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
592 void *info, bool wait)
593{
594 int cpu = get_cpu();
595
596 smp_call_function_many(mask, func, info, wait);
597 if (cpumask_test_cpu(cpu, mask)) {
598 local_irq_disable();
599 func(info);
600 local_irq_enable();
601 }
602 put_cpu();
603}
604EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700605
606/*
607 * on_each_cpu_cond(): Call a function on each processor for which
608 * the supplied function cond_func returns true, optionally waiting
609 * for all the required CPUs to finish. This may include the local
610 * processor.
611 * @cond_func: A callback function that is passed a cpu id and
612 * the the info parameter. The function is called
613 * with preemption disabled. The function should
614 * return a blooean value indicating whether to IPI
615 * the specified CPU.
616 * @func: The function to run on all applicable CPUs.
617 * This must be fast and non-blocking.
618 * @info: An arbitrary pointer to pass to both functions.
619 * @wait: If true, wait (atomically) until function has
620 * completed on other CPUs.
621 * @gfp_flags: GFP flags to use when allocating the cpumask
622 * used internally by the function.
623 *
624 * The function might sleep if the GFP flags indicates a non
625 * atomic allocation is allowed.
626 *
627 * Preemption is disabled to protect against CPUs going offline but not online.
628 * CPUs going online during the call will not be seen or sent an IPI.
629 *
630 * You must not call this function with disabled interrupts or
631 * from a hardware interrupt handler or from a bottom half handler.
632 */
633void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
634 smp_call_func_t func, void *info, bool wait,
635 gfp_t gfp_flags)
636{
637 cpumask_var_t cpus;
638 int cpu, ret;
639
640 might_sleep_if(gfp_flags & __GFP_WAIT);
641
642 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
643 preempt_disable();
644 for_each_online_cpu(cpu)
645 if (cond_func(cpu, info))
646 cpumask_set_cpu(cpu, cpus);
647 on_each_cpu_mask(cpus, func, info, wait);
648 preempt_enable();
649 free_cpumask_var(cpus);
650 } else {
651 /*
652 * No free cpumask, bother. No matter, we'll
653 * just have to IPI them one by one.
654 */
655 preempt_disable();
656 for_each_online_cpu(cpu)
657 if (cond_func(cpu, info)) {
658 ret = smp_call_function_single(cpu, func,
659 info, wait);
660 WARN_ON_ONCE(!ret);
661 }
662 preempt_enable();
663 }
664}
665EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000666
667static void do_nothing(void *unused)
668{
669}
670
671/**
672 * kick_all_cpus_sync - Force all cpus out of idle
673 *
674 * Used to synchronize the update of pm_idle function pointer. It's
675 * called after the pointer is updated and returns after the dummy
676 * callback function has been executed on all cpus. The execution of
677 * the function can only happen on the remote cpus after they have
678 * left the idle function which had been called via pm_idle function
679 * pointer. So it's guaranteed that nothing uses the previous pointer
680 * anymore.
681 */
682void kick_all_cpus_sync(void)
683{
684 /* Make sure the change is visible before we kick the cpus */
685 smp_mb();
686 smp_call_function(do_nothing, NULL, 1);
687}
688EXPORT_SYMBOL_GPL(kick_all_cpus_sync);