blob: 29dd40a9f2f403ab86f39c96ddb505851b1d36c2 [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
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010019static struct {
20 struct list_head queue;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010021 raw_spinlock_t lock;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010022} call_function __cacheline_aligned_in_smp =
23 {
24 .queue = LIST_HEAD_INIT(call_function.queue),
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010025 .lock = __RAW_SPIN_LOCK_UNLOCKED(call_function.lock),
Ingo Molnar0b13fda2009-02-25 16:52:11 +010026 };
Jens Axboe3d442232008-06-26 11:21:34 +020027
28enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010029 CSD_FLAG_LOCK = 0x01,
Jens Axboe3d442232008-06-26 11:21:34 +020030};
31
32struct call_function_data {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010033 struct call_single_data csd;
Xiao Guangrong54fdade2009-09-22 16:43:39 -070034 atomic_t refs;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010035 cpumask_var_t cpumask;
Jens Axboe3d442232008-06-26 11:21:34 +020036};
37
Milton Millere03bcb62010-01-18 13:00:51 +110038static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
39
Jens Axboe3d442232008-06-26 11:21:34 +020040struct call_single_queue {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010041 struct list_head list;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010042 raw_spinlock_t lock;
Jens Axboe3d442232008-06-26 11:21:34 +020043};
44
Milton Millere03bcb62010-01-18 13:00:51 +110045static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010046
47static int
48hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
49{
50 long cpu = (long)hcpu;
51 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
52
53 switch (action) {
54 case CPU_UP_PREPARE:
55 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070056 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010057 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070058 return notifier_from_errno(-ENOMEM);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010059 break;
60
Xiao Guangrong69dd6472009-08-06 15:07:29 -070061#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010062 case CPU_UP_CANCELED:
63 case CPU_UP_CANCELED_FROZEN:
64
65 case CPU_DEAD:
66 case CPU_DEAD_FROZEN:
67 free_cpumask_var(cfd->cpumask);
68 break;
69#endif
70 };
71
72 return NOTIFY_OK;
73}
74
75static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010076 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010077};
78
Takao Indohd8ad7d12011-03-29 12:35:04 -040079void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020080{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010081 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020082 int i;
83
84 for_each_possible_cpu(i) {
85 struct call_single_queue *q = &per_cpu(call_single_queue, i);
86
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010087 raw_spin_lock_init(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +020088 INIT_LIST_HEAD(&q->list);
89 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010090
91 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
92 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020093}
94
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010095/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010096 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
97 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +010098 * For non-synchronous ipi calls the csd can still be in use by the
99 * previous function call. For multi-cpu calls its even more interesting
100 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100101 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100102static void csd_lock_wait(struct call_single_data *data)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100103{
104 while (data->flags & CSD_FLAG_LOCK)
105 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100106}
107
108static void csd_lock(struct call_single_data *data)
109{
110 csd_lock_wait(data);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100111 data->flags = CSD_FLAG_LOCK;
112
113 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100114 * prevent CPU from reordering the above assignment
115 * to ->flags with any subsequent assignments to other
116 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100117 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100118 smp_mb();
119}
120
121static void csd_unlock(struct call_single_data *data)
122{
123 WARN_ON(!(data->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100124
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100125 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100126 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100127 */
128 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100129
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100130 data->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200131}
132
133/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100134 * Insert a previously allocated call_single_data element
135 * for execution on the given CPU. data must already have
136 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200137 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100138static
139void generic_exec_single(int cpu, struct call_single_data *data, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200140{
141 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
Jens Axboe3d442232008-06-26 11:21:34 +0200142 unsigned long flags;
Peter Zijlstra6e275632009-02-25 13:59:48 +0100143 int ipi;
Jens Axboe3d442232008-06-26 11:21:34 +0200144
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100145 raw_spin_lock_irqsave(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200146 ipi = list_empty(&dst->list);
147 list_add_tail(&data->list, &dst->list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100148 raw_spin_unlock_irqrestore(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200149
Suresh Siddha561920a02008-10-30 18:28:41 +0100150 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100151 * The list addition should be visible before sending the IPI
152 * handler locks the list to pull the entry off it because of
153 * normal cache coherency rules implied by spinlocks.
154 *
155 * If IPIs can go out of order to the cache coherency protocol
156 * in an architecture, sufficient synchronisation should be added
157 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100158 * locking and barrier primitives. Generic code isn't really
159 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100160 */
Jens Axboe3d442232008-06-26 11:21:34 +0200161 if (ipi)
162 arch_send_call_function_single_ipi(cpu);
163
164 if (wait)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100165 csd_lock_wait(data);
Jens Axboe3d442232008-06-26 11:21:34 +0200166}
167
168/*
169 * Invoked by arch to handle an IPI for call function. Must be called with
170 * interrupts disabled.
171 */
172void generic_smp_call_function_interrupt(void)
173{
174 struct call_function_data *data;
Xiao Guangrongc0f68c22009-12-14 18:00:16 -0800175 int cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200176
177 /*
Suresh Siddha269c8612009-08-19 18:05:35 -0700178 * Shouldn't receive this interrupt on a cpu that is not yet online.
179 */
180 WARN_ON_ONCE(!cpu_online(cpu));
181
182 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100183 * Ensure entry is visible on call_function_queue after we have
184 * entered the IPI. See comment in smp_call_function_many.
185 * If we don't have this, then we may miss an entry on the list
186 * and never get another IPI to process it.
187 */
188 smp_mb();
189
190 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100191 * It's ok to use list_for_each_rcu() here even though we may
192 * delete 'pos', since list_del_rcu() doesn't clear ->next
Jens Axboe3d442232008-06-26 11:21:34 +0200193 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100194 list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
Jens Axboe3d442232008-06-26 11:21:34 +0200195 int refs;
Milton Millerc8def552011-03-15 13:27:17 -0600196 smp_call_func_t func;
Jens Axboe3d442232008-06-26 11:21:34 +0200197
Anton Blanchard6dc19892011-01-20 14:44:33 -0800198 /*
199 * Since we walk the list without any locks, we might
200 * see an entry that was completed, removed from the
201 * list and is in the process of being reused.
202 *
203 * We must check that the cpu is in the cpumask before
204 * checking the refs, and both must be set before
205 * executing the callback on this cpu.
206 */
207
208 if (!cpumask_test_cpu(cpu, data->cpumask))
209 continue;
210
211 smp_rmb();
212
213 if (atomic_read(&data->refs) == 0)
214 continue;
215
Milton Millerc8def552011-03-15 13:27:17 -0600216 func = data->csd.func; /* save for later warn */
217 func(data->csd.info);
Jens Axboe3d442232008-06-26 11:21:34 +0200218
Milton Miller225c8e02011-01-20 14:44:34 -0800219 /*
Milton Millerc8def552011-03-15 13:27:17 -0600220 * If the cpu mask is not still set then func enabled
221 * interrupts (BUG), and this cpu took another smp call
222 * function interrupt and executed func(info) twice
223 * on this cpu. That nested execution decremented refs.
Milton Miller225c8e02011-01-20 14:44:34 -0800224 */
225 if (!cpumask_test_and_clear_cpu(cpu, data->cpumask)) {
Milton Millerc8def552011-03-15 13:27:17 -0600226 WARN(1, "%pf enabled interrupts and double executed\n", func);
Milton Miller225c8e02011-01-20 14:44:34 -0800227 continue;
228 }
229
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700230 refs = atomic_dec_return(&data->refs);
231 WARN_ON(refs < 0);
Jens Axboe3d442232008-06-26 11:21:34 +0200232
233 if (refs)
234 continue;
235
Milton Miller225c8e02011-01-20 14:44:34 -0800236 WARN_ON(!cpumask_empty(data->cpumask));
237
238 raw_spin_lock(&call_function.lock);
239 list_del_rcu(&data->csd.list);
240 raw_spin_unlock(&call_function.lock);
241
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100242 csd_unlock(&data->csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200243 }
Jens Axboe3d442232008-06-26 11:21:34 +0200244
Jens Axboe3d442232008-06-26 11:21:34 +0200245}
246
247/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100248 * Invoked by arch to handle an IPI for call function single. Must be
249 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200250 */
251void generic_smp_call_function_single_interrupt(void)
252{
253 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100254 unsigned int data_flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100255 LIST_HEAD(list);
Jens Axboe3d442232008-06-26 11:21:34 +0200256
Suresh Siddha269c8612009-08-19 18:05:35 -0700257 /*
258 * Shouldn't receive this interrupt on a cpu that is not yet online.
259 */
260 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
261
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100262 raw_spin_lock(&q->lock);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100263 list_replace_init(&q->list, &list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100264 raw_spin_unlock(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200265
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100266 while (!list_empty(&list)) {
267 struct call_single_data *data;
Jens Axboe3d442232008-06-26 11:21:34 +0200268
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100269 data = list_entry(list.next, struct call_single_data, list);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100270 list_del(&data->list);
Jens Axboe3d442232008-06-26 11:21:34 +0200271
Jens Axboe3d442232008-06-26 11:21:34 +0200272 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100273 * 'data' can be invalid after this call if flags == 0
274 * (when called through generic_exec_single()),
275 * so save them away before making the call:
Jens Axboe3d442232008-06-26 11:21:34 +0200276 */
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100277 data_flags = data->flags;
278
279 data->func(data->info);
280
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100281 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100282 * Unlocked CSDs are valid through generic_exec_single():
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100283 */
284 if (data_flags & CSD_FLAG_LOCK)
285 csd_unlock(data);
Jens Axboe3d442232008-06-26 11:21:34 +0200286 }
287}
288
Milton Millere03bcb62010-01-18 13:00:51 +1100289static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500290
Jens Axboe3d442232008-06-26 11:21:34 +0200291/*
292 * smp_call_function_single - Run a function on a specific CPU
293 * @func: The function to run. This must be fast and non-blocking.
294 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200295 * @wait: If true, wait until function has completed on other CPUs.
296 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800297 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200298 */
David Howells3a5f65d2010-10-27 17:28:36 +0100299int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200300 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200301{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100302 struct call_single_data d = {
303 .flags = 0,
304 };
Jens Axboe3d442232008-06-26 11:21:34 +0200305 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100306 int this_cpu;
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700307 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200308
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100309 /*
310 * prevent preemption and reschedule on another processor,
311 * as well as CPU removal
312 */
313 this_cpu = get_cpu();
314
Suresh Siddha269c8612009-08-19 18:05:35 -0700315 /*
316 * Can deadlock when called with interrupts disabled.
317 * We allow cpu's that are not yet online though, as no one else can
318 * send smp call function interrupt to this cpu and as such deadlocks
319 * can't happen.
320 */
321 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
322 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200323
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100324 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200325 local_irq_save(flags);
326 func(info);
327 local_irq_restore(flags);
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700328 } else {
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100329 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
330 struct call_single_data *data = &d;
331
332 if (!wait)
333 data = &__get_cpu_var(csd_data);
334
335 csd_lock(data);
336
337 data->func = func;
338 data->info = info;
339 generic_exec_single(cpu, data, wait);
340 } else {
341 err = -ENXIO; /* CPU not online */
342 }
Jens Axboe3d442232008-06-26 11:21:34 +0200343 }
344
345 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100346
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700347 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200348}
349EXPORT_SYMBOL(smp_call_function_single);
350
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800351/*
352 * smp_call_function_any - Run a function on any of the given cpus
353 * @mask: The mask of cpus it can run on.
354 * @func: The function to run. This must be fast and non-blocking.
355 * @info: An arbitrary pointer to pass to the function.
356 * @wait: If true, wait until function has completed.
357 *
358 * Returns 0 on success, else a negative status code (if no cpus were online).
359 * Note that @wait will be implicitly turned on in case of allocation failures,
360 * since we fall back to on-stack allocation.
361 *
362 * Selection preference:
363 * 1) current cpu if in @mask
364 * 2) any cpu of current node if in @mask
365 * 3) any other online cpu in @mask
366 */
367int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100368 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800369{
370 unsigned int cpu;
371 const struct cpumask *nodemask;
372 int ret;
373
374 /* Try for same CPU (cheapest) */
375 cpu = get_cpu();
376 if (cpumask_test_cpu(cpu, mask))
377 goto call;
378
379 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800380 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800381 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
382 cpu = cpumask_next_and(cpu, nodemask, mask)) {
383 if (cpu_online(cpu))
384 goto call;
385 }
386
387 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
388 cpu = cpumask_any_and(mask, cpu_online_mask);
389call:
390 ret = smp_call_function_single(cpu, func, info, wait);
391 put_cpu();
392 return ret;
393}
394EXPORT_SYMBOL_GPL(smp_call_function_any);
395
Jens Axboe3d442232008-06-26 11:21:34 +0200396/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200397 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200398 * @cpu: The CPU to run on.
399 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200400 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200401 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100402 * Like smp_call_function_single(), but allow caller to pass in a
403 * pre-allocated data structure. Useful for embedding @data inside
404 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200405 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100406void __smp_call_function_single(int cpu, struct call_single_data *data,
407 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200408{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200409 unsigned int this_cpu;
410 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200411
Heiko Carstens27c379f2010-09-10 13:47:29 +0200412 this_cpu = get_cpu();
Suresh Siddha269c8612009-08-19 18:05:35 -0700413 /*
414 * Can deadlock when called with interrupts disabled.
415 * We allow cpu's that are not yet online though, as no one else can
416 * send smp call function interrupt to this cpu and as such deadlocks
417 * can't happen.
418 */
419 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
420 && !oops_in_progress);
Peter Zijlstra6e275632009-02-25 13:59:48 +0100421
Heiko Carstens27c379f2010-09-10 13:47:29 +0200422 if (cpu == this_cpu) {
423 local_irq_save(flags);
424 data->func(data->info);
425 local_irq_restore(flags);
426 } else {
427 csd_lock(data);
428 generic_exec_single(cpu, data, wait);
429 }
430 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200431}
432
433/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030434 * smp_call_function_many(): Run a function on a set of other CPUs.
435 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200436 * @func: The function to run. This must be fast and non-blocking.
437 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100438 * @wait: If true, wait (atomically) until function has completed
439 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200440 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800441 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200442 *
443 * You must not call this function with disabled interrupts or from a
444 * hardware interrupt handler or from a bottom half handler. Preemption
445 * must be disabled when calling this function.
446 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030447void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100448 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200449{
Rusty Russell54b11e62008-12-30 09:05:16 +1030450 struct call_function_data *data;
Jens Axboe3d442232008-06-26 11:21:34 +0200451 unsigned long flags;
Milton Miller723aae22011-03-15 13:27:17 -0600452 int refs, cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200453
Suresh Siddha269c8612009-08-19 18:05:35 -0700454 /*
455 * Can deadlock when called with interrupts disabled.
456 * We allow cpu's that are not yet online though, as no one else can
457 * send smp call function interrupt to this cpu and as such deadlocks
458 * can't happen.
459 */
460 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100461 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200462
Milton Miller723aae22011-03-15 13:27:17 -0600463 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030464 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100465 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030466 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100467
Rusty Russell54b11e62008-12-30 09:05:16 +1030468 /* No online cpus? We're done. */
469 if (cpu >= nr_cpu_ids)
470 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200471
Rusty Russell54b11e62008-12-30 09:05:16 +1030472 /* Do we have another CPU which isn't us? */
473 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100474 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030475 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
476
477 /* Fastpath: do that cpu by itself. */
478 if (next_cpu >= nr_cpu_ids) {
479 smp_call_function_single(cpu, func, info, wait);
480 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200481 }
482
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100483 data = &__get_cpu_var(cfd_data);
484 csd_lock(&data->csd);
Milton Miller45a57912011-03-15 13:27:16 -0600485
486 /* This BUG_ON verifies our reuse assertions and can be removed */
Anton Blanchard6dc19892011-01-20 14:44:33 -0800487 BUG_ON(atomic_read(&data->refs) || !cpumask_empty(data->cpumask));
Jens Axboe3d442232008-06-26 11:21:34 +0200488
Milton Miller45a57912011-03-15 13:27:16 -0600489 /*
490 * The global call function queue list add and delete are protected
491 * by a lock, but the list is traversed without any lock, relying
492 * on the rcu list add and delete to allow safe concurrent traversal.
493 * We reuse the call function data without waiting for any grace
494 * period after some other cpu removes it from the global queue.
495 * This means a cpu might find our data block as it is being
496 * filled out.
497 *
498 * We hold off the interrupt handler on the other cpu by
499 * ordering our writes to the cpu mask vs our setting of the
500 * refs counter. We assert only the cpu owning the data block
501 * will set a bit in cpumask, and each bit will only be cleared
502 * by the subject cpu. Each cpu must first find its bit is
503 * set and then check that refs is set indicating the element is
504 * ready to be processed, otherwise it must skip the entry.
505 *
506 * On the previous iteration refs was set to 0 by another cpu.
507 * To avoid the use of transitivity, set the counter to 0 here
508 * so the wmb will pair with the rmb in the interrupt handler.
509 */
510 atomic_set(&data->refs, 0); /* convert 3rd to 1st party write */
511
Jens Axboe3d442232008-06-26 11:21:34 +0200512 data->csd.func = func;
513 data->csd.info = info;
Milton Miller45a57912011-03-15 13:27:16 -0600514
515 /* Ensure 0 refs is visible before mask. Also orders func and info */
516 smp_wmb();
517
518 /* We rely on the "and" being processed before the store */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100519 cpumask_and(data->cpumask, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100520 cpumask_clear_cpu(this_cpu, data->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600521 refs = cpumask_weight(data->cpumask);
522
523 /* Some callers race with other cpus changing the passed mask */
524 if (unlikely(!refs)) {
525 csd_unlock(&data->csd);
526 return;
527 }
Anton Blanchard6dc19892011-01-20 14:44:33 -0800528
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100529 raw_spin_lock_irqsave(&call_function.lock, flags);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100530 /*
531 * Place entry at the _HEAD_ of the list, so that any cpu still
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100532 * observing the entry in generic_smp_call_function_interrupt()
533 * will not miss any other list entries:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100534 */
535 list_add_rcu(&data->csd.list, &call_function.queue);
Milton Millere6cd1e02011-03-15 13:27:16 -0600536 /*
Milton Miller45a57912011-03-15 13:27:16 -0600537 * We rely on the wmb() in list_add_rcu to complete our writes
538 * to the cpumask before this write to refs, which indicates
539 * data is on the list and is ready to be processed.
Milton Millere6cd1e02011-03-15 13:27:16 -0600540 */
Milton Miller723aae22011-03-15 13:27:17 -0600541 atomic_set(&data->refs, refs);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100542 raw_spin_unlock_irqrestore(&call_function.lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200543
Suresh Siddha561920a02008-10-30 18:28:41 +0100544 /*
545 * Make the list addition visible before sending the ipi.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100546 * (IPIs must obey or appear to obey normal Linux cache
547 * coherency rules -- see comment in generic_exec_single).
Suresh Siddha561920a02008-10-30 18:28:41 +0100548 */
549 smp_mb();
550
Jens Axboe3d442232008-06-26 11:21:34 +0200551 /* Send a message to all CPUs in the map */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100552 arch_send_call_function_ipi_mask(data->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200553
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100554 /* Optionally wait for the CPUs to complete */
Rusty Russell54b11e62008-12-30 09:05:16 +1030555 if (wait)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100556 csd_lock_wait(&data->csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200557}
Rusty Russell54b11e62008-12-30 09:05:16 +1030558EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200559
560/**
561 * smp_call_function(): Run a function on all other CPUs.
562 * @func: The function to run. This must be fast and non-blocking.
563 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100564 * @wait: If true, wait (atomically) until function has completed
565 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200566 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030567 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200568 *
569 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800570 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200571 *
572 * You must not call this function with disabled interrupts or from a
573 * hardware interrupt handler or from a bottom half handler.
574 */
David Howells3a5f65d2010-10-27 17:28:36 +0100575int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200576{
Jens Axboe3d442232008-06-26 11:21:34 +0200577 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030578 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200579 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100580
Rusty Russell54b11e62008-12-30 09:05:16 +1030581 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200582}
583EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800584#endif /* USE_GENERIC_SMP_HELPERS */
585
Amerigo Wang34db18a02011-03-22 16:34:06 -0700586/* Setup configured maximum number of CPUs to activate */
587unsigned int setup_max_cpus = NR_CPUS;
588EXPORT_SYMBOL(setup_max_cpus);
589
590
591/*
592 * Setup routine for controlling SMP activation
593 *
594 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
595 * activation entirely (the MPS table probe still happens, though).
596 *
597 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
598 * greater than 0, limits the maximum number of CPUs activated in
599 * SMP mode to <NUM>.
600 */
601
602void __weak arch_disable_smp_support(void) { }
603
604static int __init nosmp(char *str)
605{
606 setup_max_cpus = 0;
607 arch_disable_smp_support();
608
609 return 0;
610}
611
612early_param("nosmp", nosmp);
613
614/* this is hard limit */
615static int __init nrcpus(char *str)
616{
617 int nr_cpus;
618
619 get_option(&str, &nr_cpus);
620 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
621 nr_cpu_ids = nr_cpus;
622
623 return 0;
624}
625
626early_param("nr_cpus", nrcpus);
627
628static int __init maxcpus(char *str)
629{
630 get_option(&str, &setup_max_cpus);
631 if (setup_max_cpus == 0)
632 arch_disable_smp_support();
633
634 return 0;
635}
636
637early_param("maxcpus", maxcpus);
638
639/* Setup number of possible processor ids */
640int nr_cpu_ids __read_mostly = NR_CPUS;
641EXPORT_SYMBOL(nr_cpu_ids);
642
643/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
644void __init setup_nr_cpu_ids(void)
645{
646 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
647}
648
649/* Called by boot processor to activate the rest. */
650void __init smp_init(void)
651{
652 unsigned int cpu;
653
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700654 idle_threads_init();
655
Amerigo Wang34db18a02011-03-22 16:34:06 -0700656 /* FIXME: This should be done in userspace --RR */
657 for_each_present_cpu(cpu) {
658 if (num_online_cpus() >= setup_max_cpus)
659 break;
660 if (!cpu_online(cpu))
661 cpu_up(cpu);
662 }
663
664 /* Any cleanup work */
665 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
666 smp_cpus_done(setup_max_cpus);
667}
668
Amerigo Wang351f8f82011-01-12 16:59:39 -0800669/*
Tejun Heobd924e82011-01-20 12:07:13 +0100670 * Call a function on all processors. May be used during early boot while
671 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
672 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800673 */
674int on_each_cpu(void (*func) (void *info), void *info, int wait)
675{
Tejun Heobd924e82011-01-20 12:07:13 +0100676 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800677 int ret = 0;
678
679 preempt_disable();
680 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100681 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800682 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100683 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800684 preempt_enable();
685 return ret;
686}
687EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700688
689/**
690 * on_each_cpu_mask(): Run a function on processors specified by
691 * cpumask, which may include the local processor.
692 * @mask: The set of cpus to run on (only runs on online subset).
693 * @func: The function to run. This must be fast and non-blocking.
694 * @info: An arbitrary pointer to pass to the function.
695 * @wait: If true, wait (atomically) until function has completed
696 * on other CPUs.
697 *
698 * If @wait is true, then returns once @func has returned.
699 *
700 * You must not call this function with disabled interrupts or
701 * from a hardware interrupt handler or from a bottom half handler.
702 */
703void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
704 void *info, bool wait)
705{
706 int cpu = get_cpu();
707
708 smp_call_function_many(mask, func, info, wait);
709 if (cpumask_test_cpu(cpu, mask)) {
710 local_irq_disable();
711 func(info);
712 local_irq_enable();
713 }
714 put_cpu();
715}
716EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700717
718/*
719 * on_each_cpu_cond(): Call a function on each processor for which
720 * the supplied function cond_func returns true, optionally waiting
721 * for all the required CPUs to finish. This may include the local
722 * processor.
723 * @cond_func: A callback function that is passed a cpu id and
724 * the the info parameter. The function is called
725 * with preemption disabled. The function should
726 * return a blooean value indicating whether to IPI
727 * the specified CPU.
728 * @func: The function to run on all applicable CPUs.
729 * This must be fast and non-blocking.
730 * @info: An arbitrary pointer to pass to both functions.
731 * @wait: If true, wait (atomically) until function has
732 * completed on other CPUs.
733 * @gfp_flags: GFP flags to use when allocating the cpumask
734 * used internally by the function.
735 *
736 * The function might sleep if the GFP flags indicates a non
737 * atomic allocation is allowed.
738 *
739 * Preemption is disabled to protect against CPUs going offline but not online.
740 * CPUs going online during the call will not be seen or sent an IPI.
741 *
742 * You must not call this function with disabled interrupts or
743 * from a hardware interrupt handler or from a bottom half handler.
744 */
745void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
746 smp_call_func_t func, void *info, bool wait,
747 gfp_t gfp_flags)
748{
749 cpumask_var_t cpus;
750 int cpu, ret;
751
752 might_sleep_if(gfp_flags & __GFP_WAIT);
753
754 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
755 preempt_disable();
756 for_each_online_cpu(cpu)
757 if (cond_func(cpu, info))
758 cpumask_set_cpu(cpu, cpus);
759 on_each_cpu_mask(cpus, func, info, wait);
760 preempt_enable();
761 free_cpumask_var(cpus);
762 } else {
763 /*
764 * No free cpumask, bother. No matter, we'll
765 * just have to IPI them one by one.
766 */
767 preempt_disable();
768 for_each_online_cpu(cpu)
769 if (cond_func(cpu, info)) {
770 ret = smp_call_function_single(cpu, func,
771 info, wait);
772 WARN_ON_ONCE(!ret);
773 }
774 preempt_enable();
775 }
776}
777EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000778
779static void do_nothing(void *unused)
780{
781}
782
783/**
784 * kick_all_cpus_sync - Force all cpus out of idle
785 *
786 * Used to synchronize the update of pm_idle function pointer. It's
787 * called after the pointer is updated and returns after the dummy
788 * callback function has been executed on all cpus. The execution of
789 * the function can only happen on the remote cpus after they have
790 * left the idle function which had been called via pm_idle function
791 * pointer. So it's guaranteed that nothing uses the previous pointer
792 * anymore.
793 */
794void kick_all_cpus_sync(void)
795{
796 /* Make sure the change is visible before we kick the cpus */
797 smp_mb();
798 smp_call_function(do_nothing, NULL, 1);
799}
800EXPORT_SYMBOL_GPL(kick_all_cpus_sync);