blob: 7332697cd184194424369173a5675be90f34768f [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 Torvalds59190f4212008-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
Paul Gortmaker0db06282013-06-19 14:53:51 -040076static struct notifier_block 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 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700103static void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100104{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700105 while (csd->flags & CSD_FLAG_LOCK)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100106 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100107}
108
Andrew Mortone1d12f32013-04-30 15:27:28 -0700109static void csd_lock(struct call_single_data *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100110{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700111 csd_lock_wait(csd);
112 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100113
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
Andrew Mortone1d12f32013-04-30 15:27:28 -0700122static void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100123{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700124 WARN_ON(!(csd->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
Andrew Mortone1d12f32013-04-30 15:27:28 -0700131 csd->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
Andrew Mortone1d12f32013-04-30 15:27:28 -0700140void generic_exec_single(int cpu, struct call_single_data *csd, 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);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700148 list_add_tail(&csd->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)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700166 csd_lock_wait(csd);
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);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100176 LIST_HEAD(list);
Jens Axboe3d442232008-06-26 11:21:34 +0200177
Suresh Siddha269c8612009-08-19 18:05:35 -0700178 /*
179 * Shouldn't receive this interrupt on a cpu that is not yet online.
180 */
181 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
182
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100183 raw_spin_lock(&q->lock);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100184 list_replace_init(&q->list, &list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100185 raw_spin_unlock(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200186
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100187 while (!list_empty(&list)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700188 struct call_single_data *csd;
Jens Axboe3d442232008-06-26 11:21:34 +0200189
Andrew Mortone1d12f32013-04-30 15:27:28 -0700190 csd = list_entry(list.next, struct call_single_data, list);
191 list_del(&csd->list);
Jens Axboe3d442232008-06-26 11:21:34 +0200192
Andrew Mortone1d12f32013-04-30 15:27:28 -0700193 csd->func(csd->info);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100194
Xie XiuQi46591962013-07-30 11:06:09 +0800195 csd_unlock(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200196 }
197}
198
Milton Millere03bcb62010-01-18 13:00:51 +1100199static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500200
Jens Axboe3d442232008-06-26 11:21:34 +0200201/*
202 * smp_call_function_single - Run a function on a specific CPU
203 * @func: The function to run. This must be fast and non-blocking.
204 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200205 * @wait: If true, wait until function has completed on other CPUs.
206 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800207 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200208 */
David Howells3a5f65df2010-10-27 17:28:36 +0100209int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200210 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200211{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100212 struct call_single_data d = {
213 .flags = 0,
214 };
Jens Axboe3d442232008-06-26 11:21:34 +0200215 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100216 int this_cpu;
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700217 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200218
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100219 /*
220 * prevent preemption and reschedule on another processor,
221 * as well as CPU removal
222 */
223 this_cpu = get_cpu();
224
Suresh Siddha269c8612009-08-19 18:05:35 -0700225 /*
226 * Can deadlock when called with interrupts disabled.
227 * We allow cpu's that are not yet online though, as no one else can
228 * send smp call function interrupt to this cpu and as such deadlocks
229 * can't happen.
230 */
231 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
232 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200233
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100234 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200235 local_irq_save(flags);
236 func(info);
237 local_irq_restore(flags);
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700238 } else {
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100239 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700240 struct call_single_data *csd = &d;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100241
242 if (!wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700243 csd = &__get_cpu_var(csd_data);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100244
Andrew Mortone1d12f32013-04-30 15:27:28 -0700245 csd_lock(csd);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100246
Andrew Mortone1d12f32013-04-30 15:27:28 -0700247 csd->func = func;
248 csd->info = info;
249 generic_exec_single(cpu, csd, wait);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100250 } else {
251 err = -ENXIO; /* CPU not online */
252 }
Jens Axboe3d442232008-06-26 11:21:34 +0200253 }
254
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
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800261/*
262 * smp_call_function_any - Run a function on any of the given cpus
263 * @mask: The mask of cpus it can run on.
264 * @func: The function to run. This must be fast and non-blocking.
265 * @info: An arbitrary pointer to pass to the function.
266 * @wait: If true, wait until function has completed.
267 *
268 * Returns 0 on success, else a negative status code (if no cpus were online).
269 * Note that @wait will be implicitly turned on in case of allocation failures,
270 * since we fall back to on-stack allocation.
271 *
272 * Selection preference:
273 * 1) current cpu if in @mask
274 * 2) any cpu of current node if in @mask
275 * 3) any other online cpu in @mask
276 */
277int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100278 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800279{
280 unsigned int cpu;
281 const struct cpumask *nodemask;
282 int ret;
283
284 /* Try for same CPU (cheapest) */
285 cpu = get_cpu();
286 if (cpumask_test_cpu(cpu, mask))
287 goto call;
288
289 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800290 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800291 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
292 cpu = cpumask_next_and(cpu, nodemask, mask)) {
293 if (cpu_online(cpu))
294 goto call;
295 }
296
297 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
298 cpu = cpumask_any_and(mask, cpu_online_mask);
299call:
300 ret = smp_call_function_single(cpu, func, info, wait);
301 put_cpu();
302 return ret;
303}
304EXPORT_SYMBOL_GPL(smp_call_function_any);
305
Jens Axboe3d442232008-06-26 11:21:34 +0200306/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200307 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200308 * @cpu: The CPU to run on.
309 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200310 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200311 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100312 * Like smp_call_function_single(), but allow caller to pass in a
313 * pre-allocated data structure. Useful for embedding @data inside
314 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200315 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700316void __smp_call_function_single(int cpu, struct call_single_data *csd,
Peter Zijlstra6e275632009-02-25 13:59:48 +0100317 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200318{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200319 unsigned int this_cpu;
320 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200321
Heiko Carstens27c379f2010-09-10 13:47:29 +0200322 this_cpu = get_cpu();
Suresh Siddha269c8612009-08-19 18:05:35 -0700323 /*
324 * Can deadlock when called with interrupts disabled.
325 * We allow cpu's that are not yet online though, as no one else can
326 * send smp call function interrupt to this cpu and as such deadlocks
327 * can't happen.
328 */
329 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
330 && !oops_in_progress);
Peter Zijlstra6e275632009-02-25 13:59:48 +0100331
Heiko Carstens27c379f2010-09-10 13:47:29 +0200332 if (cpu == this_cpu) {
333 local_irq_save(flags);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700334 csd->func(csd->info);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200335 local_irq_restore(flags);
336 } else {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700337 csd_lock(csd);
338 generic_exec_single(cpu, csd, wait);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200339 }
340 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200341}
342
343/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030344 * smp_call_function_many(): Run a function on a set of other CPUs.
345 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200346 * @func: The function to run. This must be fast and non-blocking.
347 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100348 * @wait: If true, wait (atomically) until function has completed
349 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200350 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800351 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200352 *
353 * You must not call this function with disabled interrupts or from a
354 * hardware interrupt handler or from a bottom half handler. Preemption
355 * must be disabled when calling this function.
356 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030357void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100358 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200359{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700360 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800361 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200362
Suresh Siddha269c8612009-08-19 18:05:35 -0700363 /*
364 * Can deadlock when called with interrupts disabled.
365 * We allow cpu's that are not yet online though, as no one else can
366 * send smp call function interrupt to this cpu and as such deadlocks
367 * can't happen.
368 */
369 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100370 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200371
Milton Miller723aae22011-03-15 13:27:17 -0600372 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030373 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100374 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030375 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100376
Rusty Russell54b11e62008-12-30 09:05:16 +1030377 /* No online cpus? We're done. */
378 if (cpu >= nr_cpu_ids)
379 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200380
Rusty Russell54b11e62008-12-30 09:05:16 +1030381 /* Do we have another CPU which isn't us? */
382 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100383 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030384 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
385
386 /* Fastpath: do that cpu by itself. */
387 if (next_cpu >= nr_cpu_ids) {
388 smp_call_function_single(cpu, func, info, wait);
389 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200390 }
391
Andrew Mortone1d12f32013-04-30 15:27:28 -0700392 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600393
Andrew Mortone1d12f32013-04-30 15:27:28 -0700394 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
395 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600396
397 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700398 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600399 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800400
Wang YanQingf44310b2013-01-26 15:53:57 +0800401 /*
Andrew Mortone1d12f32013-04-30 15:27:28 -0700402 * After we put an entry into the list, cfd->cpumask may be cleared
403 * again when another CPU sends another IPI for a SMP function call, so
404 * cfd->cpumask will be zero.
Wang YanQingf44310b2013-01-26 15:53:57 +0800405 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700406 cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200407
Andrew Mortone1d12f32013-04-30 15:27:28 -0700408 for_each_cpu(cpu, cfd->cpumask) {
409 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800410 struct call_single_queue *dst =
411 &per_cpu(call_single_queue, cpu);
412 unsigned long flags;
413
414 csd_lock(csd);
415 csd->func = func;
416 csd->info = info;
417
418 raw_spin_lock_irqsave(&dst->lock, flags);
419 list_add_tail(&csd->list, &dst->list);
420 raw_spin_unlock_irqrestore(&dst->lock, flags);
421 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100422
Jens Axboe3d442232008-06-26 11:21:34 +0200423 /* Send a message to all CPUs in the map */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700424 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200425
Shaohua Li9a46ad62013-02-21 16:43:03 -0800426 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700427 for_each_cpu(cpu, cfd->cpumask) {
428 struct call_single_data *csd;
429
430 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800431 csd_lock_wait(csd);
432 }
433 }
Jens Axboe3d442232008-06-26 11:21:34 +0200434}
Rusty Russell54b11e62008-12-30 09:05:16 +1030435EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200436
437/**
438 * smp_call_function(): Run a function on all other CPUs.
439 * @func: The function to run. This must be fast and non-blocking.
440 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100441 * @wait: If true, wait (atomically) until function has completed
442 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200443 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030444 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200445 *
446 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800447 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200448 *
449 * You must not call this function with disabled interrupts or from a
450 * hardware interrupt handler or from a bottom half handler.
451 */
David Howells3a5f65df2010-10-27 17:28:36 +0100452int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200453{
Jens Axboe3d442232008-06-26 11:21:34 +0200454 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030455 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200456 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100457
Rusty Russell54b11e62008-12-30 09:05:16 +1030458 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200459}
460EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800461#endif /* USE_GENERIC_SMP_HELPERS */
462
Amerigo Wang34db18a02011-03-22 16:34:06 -0700463/* Setup configured maximum number of CPUs to activate */
464unsigned int setup_max_cpus = NR_CPUS;
465EXPORT_SYMBOL(setup_max_cpus);
466
467
468/*
469 * Setup routine for controlling SMP activation
470 *
471 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
472 * activation entirely (the MPS table probe still happens, though).
473 *
474 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
475 * greater than 0, limits the maximum number of CPUs activated in
476 * SMP mode to <NUM>.
477 */
478
479void __weak arch_disable_smp_support(void) { }
480
481static int __init nosmp(char *str)
482{
483 setup_max_cpus = 0;
484 arch_disable_smp_support();
485
486 return 0;
487}
488
489early_param("nosmp", nosmp);
490
491/* this is hard limit */
492static int __init nrcpus(char *str)
493{
494 int nr_cpus;
495
496 get_option(&str, &nr_cpus);
497 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
498 nr_cpu_ids = nr_cpus;
499
500 return 0;
501}
502
503early_param("nr_cpus", nrcpus);
504
505static int __init maxcpus(char *str)
506{
507 get_option(&str, &setup_max_cpus);
508 if (setup_max_cpus == 0)
509 arch_disable_smp_support();
510
511 return 0;
512}
513
514early_param("maxcpus", maxcpus);
515
516/* Setup number of possible processor ids */
517int nr_cpu_ids __read_mostly = NR_CPUS;
518EXPORT_SYMBOL(nr_cpu_ids);
519
520/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
521void __init setup_nr_cpu_ids(void)
522{
523 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
524}
525
526/* Called by boot processor to activate the rest. */
527void __init smp_init(void)
528{
529 unsigned int cpu;
530
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700531 idle_threads_init();
532
Amerigo Wang34db18a02011-03-22 16:34:06 -0700533 /* FIXME: This should be done in userspace --RR */
534 for_each_present_cpu(cpu) {
535 if (num_online_cpus() >= setup_max_cpus)
536 break;
537 if (!cpu_online(cpu))
538 cpu_up(cpu);
539 }
540
541 /* Any cleanup work */
542 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
543 smp_cpus_done(setup_max_cpus);
544}
545
Amerigo Wang351f8f82011-01-12 16:59:39 -0800546/*
Tejun Heobd924e82011-01-20 12:07:13 +0100547 * Call a function on all processors. May be used during early boot while
548 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
549 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800550 */
551int on_each_cpu(void (*func) (void *info), void *info, int wait)
552{
Tejun Heobd924e82011-01-20 12:07:13 +0100553 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800554 int ret = 0;
555
556 preempt_disable();
557 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100558 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800559 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100560 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800561 preempt_enable();
562 return ret;
563}
564EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700565
566/**
567 * on_each_cpu_mask(): Run a function on processors specified by
568 * cpumask, which may include the local processor.
569 * @mask: The set of cpus to run on (only runs on online subset).
570 * @func: The function to run. This must be fast and non-blocking.
571 * @info: An arbitrary pointer to pass to the function.
572 * @wait: If true, wait (atomically) until function has completed
573 * on other CPUs.
574 *
575 * If @wait is true, then returns once @func has returned.
576 *
577 * You must not call this function with disabled interrupts or
578 * from a hardware interrupt handler or from a bottom half handler.
579 */
580void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
581 void *info, bool wait)
582{
583 int cpu = get_cpu();
584
585 smp_call_function_many(mask, func, info, wait);
586 if (cpumask_test_cpu(cpu, mask)) {
587 local_irq_disable();
588 func(info);
589 local_irq_enable();
590 }
591 put_cpu();
592}
593EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700594
595/*
596 * on_each_cpu_cond(): Call a function on each processor for which
597 * the supplied function cond_func returns true, optionally waiting
598 * for all the required CPUs to finish. This may include the local
599 * processor.
600 * @cond_func: A callback function that is passed a cpu id and
601 * the the info parameter. The function is called
602 * with preemption disabled. The function should
603 * return a blooean value indicating whether to IPI
604 * the specified CPU.
605 * @func: The function to run on all applicable CPUs.
606 * This must be fast and non-blocking.
607 * @info: An arbitrary pointer to pass to both functions.
608 * @wait: If true, wait (atomically) until function has
609 * completed on other CPUs.
610 * @gfp_flags: GFP flags to use when allocating the cpumask
611 * used internally by the function.
612 *
613 * The function might sleep if the GFP flags indicates a non
614 * atomic allocation is allowed.
615 *
616 * Preemption is disabled to protect against CPUs going offline but not online.
617 * CPUs going online during the call will not be seen or sent an IPI.
618 *
619 * You must not call this function with disabled interrupts or
620 * from a hardware interrupt handler or from a bottom half handler.
621 */
622void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
623 smp_call_func_t func, void *info, bool wait,
624 gfp_t gfp_flags)
625{
626 cpumask_var_t cpus;
627 int cpu, ret;
628
629 might_sleep_if(gfp_flags & __GFP_WAIT);
630
631 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
632 preempt_disable();
633 for_each_online_cpu(cpu)
634 if (cond_func(cpu, info))
635 cpumask_set_cpu(cpu, cpus);
636 on_each_cpu_mask(cpus, func, info, wait);
637 preempt_enable();
638 free_cpumask_var(cpus);
639 } else {
640 /*
641 * No free cpumask, bother. No matter, we'll
642 * just have to IPI them one by one.
643 */
644 preempt_disable();
645 for_each_online_cpu(cpu)
646 if (cond_func(cpu, info)) {
647 ret = smp_call_function_single(cpu, func,
648 info, wait);
649 WARN_ON_ONCE(!ret);
650 }
651 preempt_enable();
652 }
653}
654EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000655
656static void do_nothing(void *unused)
657{
658}
659
660/**
661 * kick_all_cpus_sync - Force all cpus out of idle
662 *
663 * Used to synchronize the update of pm_idle function pointer. It's
664 * called after the pointer is updated and returns after the dummy
665 * callback function has been executed on all cpus. The execution of
666 * the function can only happen on the remote cpus after they have
667 * left the idle function which had been called via pm_idle function
668 * pointer. So it's guaranteed that nothing uses the previous pointer
669 * anymore.
670 */
671void kick_all_cpus_sync(void)
672{
673 /* Make sure the change is visible before we kick the cpus */
674 smp_mb();
675 smp_call_function(do_nothing, NULL, 1);
676}
677EXPORT_SYMBOL_GPL(kick_all_cpus_sync);