blob: d47300a46d7b74c7a023678cc38f79bf825fec2c [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
Amerigo Wang351f8f82011-01-12 16:59:39 -080016#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010017static struct {
18 struct list_head queue;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010019 raw_spinlock_t lock;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010020} call_function __cacheline_aligned_in_smp =
21 {
22 .queue = LIST_HEAD_INIT(call_function.queue),
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010023 .lock = __RAW_SPIN_LOCK_UNLOCKED(call_function.lock),
Ingo Molnar0b13fda2009-02-25 16:52:11 +010024 };
Jens Axboe3d442232008-06-26 11:21:34 +020025
26enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010027 CSD_FLAG_LOCK = 0x01,
Jens Axboe3d442232008-06-26 11:21:34 +020028};
29
30struct call_function_data {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010031 struct call_single_data csd;
Xiao Guangrong54fdade2009-09-22 16:43:39 -070032 atomic_t refs;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010033 cpumask_var_t cpumask;
Wang YanQing28a077b2013-01-26 07:53:57 +000034 cpumask_var_t cpumask_ipi;
Jens Axboe3d442232008-06-26 11:21:34 +020035};
36
Milton Millere03bcb62010-01-18 13:00:51 +110037static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
38
Jens Axboe3d442232008-06-26 11:21:34 +020039struct call_single_queue {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010040 struct list_head list;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010041 raw_spinlock_t lock;
Jens Axboe3d442232008-06-26 11:21:34 +020042};
43
Milton Millere03bcb62010-01-18 13:00:51 +110044static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010045
46static int
47hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
48{
49 long cpu = (long)hcpu;
50 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
51
52 switch (action) {
53 case CPU_UP_PREPARE:
54 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070055 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010056 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070057 return notifier_from_errno(-ENOMEM);
Wang YanQing28a077b2013-01-26 07:53:57 +000058 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
59 cpu_to_node(cpu)))
60 return notifier_from_errno(-ENOMEM);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010061 break;
62
Xiao Guangrong69dd6472009-08-06 15:07:29 -070063#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010064 case CPU_UP_CANCELED:
65 case CPU_UP_CANCELED_FROZEN:
66
67 case CPU_DEAD:
68 case CPU_DEAD_FROZEN:
69 free_cpumask_var(cfd->cpumask);
Wang YanQing28a077b2013-01-26 07:53:57 +000070 free_cpumask_var(cfd->cpumask_ipi);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010071 break;
72#endif
73 };
74
75 return NOTIFY_OK;
76}
77
78static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010079 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010080};
81
Takao Indohd8ad7d12011-03-29 12:35:04 -040082void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020083{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010084 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020085 int i;
86
87 for_each_possible_cpu(i) {
88 struct call_single_queue *q = &per_cpu(call_single_queue, i);
89
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010090 raw_spin_lock_init(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +020091 INIT_LIST_HEAD(&q->list);
92 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010093
94 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
95 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020096}
97
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010098/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010099 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
100 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100101 * For non-synchronous ipi calls the csd can still be in use by the
102 * previous function call. For multi-cpu calls its even more interesting
103 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100104 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100105static void csd_lock_wait(struct call_single_data *data)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100106{
107 while (data->flags & CSD_FLAG_LOCK)
108 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100109}
110
111static void csd_lock(struct call_single_data *data)
112{
113 csd_lock_wait(data);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100114 data->flags = CSD_FLAG_LOCK;
115
116 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100117 * prevent CPU from reordering the above assignment
118 * to ->flags with any subsequent assignments to other
119 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100120 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100121 smp_mb();
122}
123
124static void csd_unlock(struct call_single_data *data)
125{
126 WARN_ON(!(data->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100127
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100128 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100129 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100130 */
131 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100132
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100133 data->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200134}
135
136/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100137 * Insert a previously allocated call_single_data element
138 * for execution on the given CPU. data must already have
139 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200140 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100141static
142void generic_exec_single(int cpu, struct call_single_data *data, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200143{
144 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
Jens Axboe3d442232008-06-26 11:21:34 +0200145 unsigned long flags;
Peter Zijlstra6e275632009-02-25 13:59:48 +0100146 int ipi;
Jens Axboe3d442232008-06-26 11:21:34 +0200147
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100148 raw_spin_lock_irqsave(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200149 ipi = list_empty(&dst->list);
150 list_add_tail(&data->list, &dst->list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100151 raw_spin_unlock_irqrestore(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200152
Suresh Siddha561920a02008-10-30 18:28:41 +0100153 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100154 * The list addition should be visible before sending the IPI
155 * handler locks the list to pull the entry off it because of
156 * normal cache coherency rules implied by spinlocks.
157 *
158 * If IPIs can go out of order to the cache coherency protocol
159 * in an architecture, sufficient synchronisation should be added
160 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100161 * locking and barrier primitives. Generic code isn't really
162 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100163 */
Jens Axboe3d442232008-06-26 11:21:34 +0200164 if (ipi)
165 arch_send_call_function_single_ipi(cpu);
166
167 if (wait)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100168 csd_lock_wait(data);
Jens Axboe3d442232008-06-26 11:21:34 +0200169}
170
171/*
172 * Invoked by arch to handle an IPI for call function. Must be called with
173 * interrupts disabled.
174 */
175void generic_smp_call_function_interrupt(void)
176{
177 struct call_function_data *data;
Xiao Guangrongc0f68c22009-12-14 18:00:16 -0800178 int cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200179
180 /*
Suresh Siddha269c8612009-08-19 18:05:35 -0700181 * Shouldn't receive this interrupt on a cpu that is not yet online.
182 */
183 WARN_ON_ONCE(!cpu_online(cpu));
184
185 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100186 * Ensure entry is visible on call_function_queue after we have
187 * entered the IPI. See comment in smp_call_function_many.
188 * If we don't have this, then we may miss an entry on the list
189 * and never get another IPI to process it.
190 */
191 smp_mb();
192
193 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100194 * It's ok to use list_for_each_rcu() here even though we may
195 * delete 'pos', since list_del_rcu() doesn't clear ->next
Jens Axboe3d442232008-06-26 11:21:34 +0200196 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100197 list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
Jens Axboe3d442232008-06-26 11:21:34 +0200198 int refs;
Milton Millerc8def552011-03-15 13:27:17 -0600199 smp_call_func_t func;
Jens Axboe3d442232008-06-26 11:21:34 +0200200
Anton Blanchard6dc19892011-01-20 14:44:33 -0800201 /*
202 * Since we walk the list without any locks, we might
203 * see an entry that was completed, removed from the
204 * list and is in the process of being reused.
205 *
206 * We must check that the cpu is in the cpumask before
207 * checking the refs, and both must be set before
208 * executing the callback on this cpu.
209 */
210
211 if (!cpumask_test_cpu(cpu, data->cpumask))
212 continue;
213
214 smp_rmb();
215
216 if (atomic_read(&data->refs) == 0)
217 continue;
218
Milton Millerc8def552011-03-15 13:27:17 -0600219 func = data->csd.func; /* save for later warn */
220 func(data->csd.info);
Jens Axboe3d442232008-06-26 11:21:34 +0200221
Milton Miller225c8e02011-01-20 14:44:34 -0800222 /*
Milton Millerc8def552011-03-15 13:27:17 -0600223 * If the cpu mask is not still set then func enabled
224 * interrupts (BUG), and this cpu took another smp call
225 * function interrupt and executed func(info) twice
226 * on this cpu. That nested execution decremented refs.
Milton Miller225c8e02011-01-20 14:44:34 -0800227 */
228 if (!cpumask_test_and_clear_cpu(cpu, data->cpumask)) {
Milton Millerc8def552011-03-15 13:27:17 -0600229 WARN(1, "%pf enabled interrupts and double executed\n", func);
Milton Miller225c8e02011-01-20 14:44:34 -0800230 continue;
231 }
232
Xiao Guangrong54fdade2009-09-22 16:43:39 -0700233 refs = atomic_dec_return(&data->refs);
234 WARN_ON(refs < 0);
Jens Axboe3d442232008-06-26 11:21:34 +0200235
236 if (refs)
237 continue;
238
Milton Miller225c8e02011-01-20 14:44:34 -0800239 WARN_ON(!cpumask_empty(data->cpumask));
240
241 raw_spin_lock(&call_function.lock);
242 list_del_rcu(&data->csd.list);
243 raw_spin_unlock(&call_function.lock);
244
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100245 csd_unlock(&data->csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200246 }
Jens Axboe3d442232008-06-26 11:21:34 +0200247
Jens Axboe3d442232008-06-26 11:21:34 +0200248}
249
250/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100251 * Invoked by arch to handle an IPI for call function single. Must be
252 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200253 */
254void generic_smp_call_function_single_interrupt(void)
255{
256 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100257 unsigned int data_flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100258 LIST_HEAD(list);
Jens Axboe3d442232008-06-26 11:21:34 +0200259
Suresh Siddha269c8612009-08-19 18:05:35 -0700260 /*
261 * Shouldn't receive this interrupt on a cpu that is not yet online.
262 */
263 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
264
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100265 raw_spin_lock(&q->lock);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100266 list_replace_init(&q->list, &list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100267 raw_spin_unlock(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200268
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100269 while (!list_empty(&list)) {
270 struct call_single_data *data;
Jens Axboe3d442232008-06-26 11:21:34 +0200271
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100272 data = list_entry(list.next, struct call_single_data, list);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100273 list_del(&data->list);
Jens Axboe3d442232008-06-26 11:21:34 +0200274
Jens Axboe3d442232008-06-26 11:21:34 +0200275 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100276 * 'data' can be invalid after this call if flags == 0
277 * (when called through generic_exec_single()),
278 * so save them away before making the call:
Jens Axboe3d442232008-06-26 11:21:34 +0200279 */
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100280 data_flags = data->flags;
281
282 data->func(data->info);
283
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100284 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100285 * Unlocked CSDs are valid through generic_exec_single():
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100286 */
287 if (data_flags & CSD_FLAG_LOCK)
288 csd_unlock(data);
Jens Axboe3d442232008-06-26 11:21:34 +0200289 }
290}
291
Milton Millere03bcb62010-01-18 13:00:51 +1100292static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500293
Jens Axboe3d442232008-06-26 11:21:34 +0200294/*
295 * smp_call_function_single - Run a function on a specific CPU
296 * @func: The function to run. This must be fast and non-blocking.
297 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200298 * @wait: If true, wait until function has completed on other CPUs.
299 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800300 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200301 */
David Howells3a5f65d2010-10-27 17:28:36 +0100302int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200303 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200304{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100305 struct call_single_data d = {
306 .flags = 0,
307 };
Jens Axboe3d442232008-06-26 11:21:34 +0200308 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100309 int this_cpu;
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700310 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200311
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100312 /*
313 * prevent preemption and reschedule on another processor,
314 * as well as CPU removal
315 */
316 this_cpu = get_cpu();
317
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100318 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200319 local_irq_save(flags);
320 func(info);
321 local_irq_restore(flags);
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700322 } else {
Matt Wagantall61675fc2013-09-23 15:19:49 -0700323 /*
324 * Can deadlock when called with interrupts disabled.
325 * We allow cpu's that are not yet online though, as no one else
326 * can send smp call function interrupt to this cpu and as such
327 * deadlocks can't happen.
328 */
329 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
330 && !oops_in_progress);
331
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100332 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
333 struct call_single_data *data = &d;
334
335 if (!wait)
336 data = &__get_cpu_var(csd_data);
337
338 csd_lock(data);
339
340 data->func = func;
341 data->info = info;
342 generic_exec_single(cpu, data, wait);
343 } else {
344 err = -ENXIO; /* CPU not online */
345 }
Jens Axboe3d442232008-06-26 11:21:34 +0200346 }
347
348 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100349
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700350 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200351}
352EXPORT_SYMBOL(smp_call_function_single);
353
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800354/*
355 * smp_call_function_any - Run a function on any of the given cpus
356 * @mask: The mask of cpus it can run on.
357 * @func: The function to run. This must be fast and non-blocking.
358 * @info: An arbitrary pointer to pass to the function.
359 * @wait: If true, wait until function has completed.
360 *
361 * Returns 0 on success, else a negative status code (if no cpus were online).
362 * Note that @wait will be implicitly turned on in case of allocation failures,
363 * since we fall back to on-stack allocation.
364 *
365 * Selection preference:
366 * 1) current cpu if in @mask
367 * 2) any cpu of current node if in @mask
368 * 3) any other online cpu in @mask
369 */
370int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100371 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800372{
373 unsigned int cpu;
374 const struct cpumask *nodemask;
375 int ret;
376
377 /* Try for same CPU (cheapest) */
378 cpu = get_cpu();
379 if (cpumask_test_cpu(cpu, mask))
380 goto call;
381
382 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800383 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800384 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
385 cpu = cpumask_next_and(cpu, nodemask, mask)) {
386 if (cpu_online(cpu))
387 goto call;
388 }
389
390 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
391 cpu = cpumask_any_and(mask, cpu_online_mask);
392call:
393 ret = smp_call_function_single(cpu, func, info, wait);
394 put_cpu();
395 return ret;
396}
397EXPORT_SYMBOL_GPL(smp_call_function_any);
398
Jens Axboe3d442232008-06-26 11:21:34 +0200399/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200400 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200401 * @cpu: The CPU to run on.
402 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200403 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200404 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100405 * Like smp_call_function_single(), but allow caller to pass in a
406 * pre-allocated data structure. Useful for embedding @data inside
407 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200408 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100409void __smp_call_function_single(int cpu, struct call_single_data *data,
410 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200411{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200412 unsigned int this_cpu;
413 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200414
Heiko Carstens27c379f2010-09-10 13:47:29 +0200415 this_cpu = get_cpu();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100416
Heiko Carstens27c379f2010-09-10 13:47:29 +0200417 if (cpu == this_cpu) {
418 local_irq_save(flags);
419 data->func(data->info);
420 local_irq_restore(flags);
421 } else {
Matt Wagantall61675fc2013-09-23 15:19:49 -0700422 /*
423 * Can deadlock when called with interrupts disabled.
424 * We allow cpu's that are not yet online though, as no one else
425 * can send smp call function interrupt to this cpu and as such
426 * deadlocks can't happen.
427 */
428 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait
429 && irqs_disabled() && !oops_in_progress);
430
Heiko Carstens27c379f2010-09-10 13:47:29 +0200431 csd_lock(data);
432 generic_exec_single(cpu, data, wait);
433 }
434 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200435}
436
437/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030438 * smp_call_function_many(): Run a function on a set of other CPUs.
439 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200440 * @func: The function to run. This must be fast and non-blocking.
441 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100442 * @wait: If true, wait (atomically) until function has completed
443 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200444 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800445 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200446 *
447 * You must not call this function with disabled interrupts or from a
448 * hardware interrupt handler or from a bottom half handler. Preemption
449 * must be disabled when calling this function.
450 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030451void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100452 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200453{
Rusty Russell54b11e62008-12-30 09:05:16 +1030454 struct call_function_data *data;
Jens Axboe3d442232008-06-26 11:21:34 +0200455 unsigned long flags;
Milton Miller723aae22011-03-15 13:27:17 -0600456 int refs, cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200457
Suresh Siddha269c8612009-08-19 18:05:35 -0700458 /*
459 * Can deadlock when called with interrupts disabled.
460 * We allow cpu's that are not yet online though, as no one else can
461 * send smp call function interrupt to this cpu and as such deadlocks
462 * can't happen.
463 */
464 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100465 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200466
Milton Miller723aae22011-03-15 13:27:17 -0600467 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030468 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100469 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030470 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100471
Rusty Russell54b11e62008-12-30 09:05:16 +1030472 /* No online cpus? We're done. */
473 if (cpu >= nr_cpu_ids)
474 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200475
Rusty Russell54b11e62008-12-30 09:05:16 +1030476 /* Do we have another CPU which isn't us? */
477 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100478 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030479 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
480
481 /* Fastpath: do that cpu by itself. */
482 if (next_cpu >= nr_cpu_ids) {
483 smp_call_function_single(cpu, func, info, wait);
484 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200485 }
486
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100487 data = &__get_cpu_var(cfd_data);
488 csd_lock(&data->csd);
Milton Miller45a57912011-03-15 13:27:16 -0600489
490 /* This BUG_ON verifies our reuse assertions and can be removed */
Anton Blanchard6dc19892011-01-20 14:44:33 -0800491 BUG_ON(atomic_read(&data->refs) || !cpumask_empty(data->cpumask));
Jens Axboe3d442232008-06-26 11:21:34 +0200492
Milton Miller45a57912011-03-15 13:27:16 -0600493 /*
494 * The global call function queue list add and delete are protected
495 * by a lock, but the list is traversed without any lock, relying
496 * on the rcu list add and delete to allow safe concurrent traversal.
497 * We reuse the call function data without waiting for any grace
498 * period after some other cpu removes it from the global queue.
499 * This means a cpu might find our data block as it is being
500 * filled out.
501 *
502 * We hold off the interrupt handler on the other cpu by
503 * ordering our writes to the cpu mask vs our setting of the
504 * refs counter. We assert only the cpu owning the data block
505 * will set a bit in cpumask, and each bit will only be cleared
506 * by the subject cpu. Each cpu must first find its bit is
507 * set and then check that refs is set indicating the element is
508 * ready to be processed, otherwise it must skip the entry.
509 *
510 * On the previous iteration refs was set to 0 by another cpu.
511 * To avoid the use of transitivity, set the counter to 0 here
512 * so the wmb will pair with the rmb in the interrupt handler.
513 */
514 atomic_set(&data->refs, 0); /* convert 3rd to 1st party write */
515
Jens Axboe3d442232008-06-26 11:21:34 +0200516 data->csd.func = func;
517 data->csd.info = info;
Milton Miller45a57912011-03-15 13:27:16 -0600518
519 /* Ensure 0 refs is visible before mask. Also orders func and info */
520 smp_wmb();
521
522 /* We rely on the "and" being processed before the store */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100523 cpumask_and(data->cpumask, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100524 cpumask_clear_cpu(this_cpu, data->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600525 refs = cpumask_weight(data->cpumask);
526
527 /* Some callers race with other cpus changing the passed mask */
528 if (unlikely(!refs)) {
529 csd_unlock(&data->csd);
530 return;
531 }
Anton Blanchard6dc19892011-01-20 14:44:33 -0800532
Wang YanQing28a077b2013-01-26 07:53:57 +0000533 /*
534 * After we put an entry into the list, data->cpumask
535 * may be cleared again when another CPU sends another IPI for
536 * a SMP function call, so data->cpumask will be zero.
537 */
538 cpumask_copy(data->cpumask_ipi, data->cpumask);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100539 raw_spin_lock_irqsave(&call_function.lock, flags);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100540 /*
541 * Place entry at the _HEAD_ of the list, so that any cpu still
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100542 * observing the entry in generic_smp_call_function_interrupt()
543 * will not miss any other list entries:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100544 */
545 list_add_rcu(&data->csd.list, &call_function.queue);
Milton Millere6cd1e02011-03-15 13:27:16 -0600546 /*
Milton Miller45a57912011-03-15 13:27:16 -0600547 * We rely on the wmb() in list_add_rcu to complete our writes
548 * to the cpumask before this write to refs, which indicates
549 * data is on the list and is ready to be processed.
Milton Millere6cd1e02011-03-15 13:27:16 -0600550 */
Milton Miller723aae22011-03-15 13:27:17 -0600551 atomic_set(&data->refs, refs);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100552 raw_spin_unlock_irqrestore(&call_function.lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200553
Suresh Siddha561920a02008-10-30 18:28:41 +0100554 /*
555 * Make the list addition visible before sending the ipi.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100556 * (IPIs must obey or appear to obey normal Linux cache
557 * coherency rules -- see comment in generic_exec_single).
Suresh Siddha561920a02008-10-30 18:28:41 +0100558 */
559 smp_mb();
560
Jens Axboe3d442232008-06-26 11:21:34 +0200561 /* Send a message to all CPUs in the map */
Wang YanQing28a077b2013-01-26 07:53:57 +0000562 arch_send_call_function_ipi_mask(data->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200563
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100564 /* Optionally wait for the CPUs to complete */
Rusty Russell54b11e62008-12-30 09:05:16 +1030565 if (wait)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100566 csd_lock_wait(&data->csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200567}
Rusty Russell54b11e62008-12-30 09:05:16 +1030568EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200569
570/**
571 * smp_call_function(): Run a function on all other CPUs.
572 * @func: The function to run. This must be fast and non-blocking.
573 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100574 * @wait: If true, wait (atomically) until function has completed
575 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200576 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030577 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200578 *
579 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800580 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200581 *
582 * You must not call this function with disabled interrupts or from a
583 * hardware interrupt handler or from a bottom half handler.
584 */
David Howells3a5f65d2010-10-27 17:28:36 +0100585int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200586{
Jens Axboe3d442232008-06-26 11:21:34 +0200587 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030588 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200589 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100590
Rusty Russell54b11e62008-12-30 09:05:16 +1030591 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200592}
593EXPORT_SYMBOL(smp_call_function);
594
595void ipi_call_lock(void)
596{
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100597 raw_spin_lock(&call_function.lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200598}
599
600void ipi_call_unlock(void)
601{
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100602 raw_spin_unlock(&call_function.lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200603}
604
605void ipi_call_lock_irq(void)
606{
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100607 raw_spin_lock_irq(&call_function.lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200608}
609
610void ipi_call_unlock_irq(void)
611{
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100612 raw_spin_unlock_irq(&call_function.lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200613}
Amerigo Wang351f8f82011-01-12 16:59:39 -0800614#endif /* USE_GENERIC_SMP_HELPERS */
615
Amerigo Wang34db18a02011-03-22 16:34:06 -0700616/* Setup configured maximum number of CPUs to activate */
617unsigned int setup_max_cpus = NR_CPUS;
618EXPORT_SYMBOL(setup_max_cpus);
619
620
621/*
622 * Setup routine for controlling SMP activation
623 *
624 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
625 * activation entirely (the MPS table probe still happens, though).
626 *
627 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
628 * greater than 0, limits the maximum number of CPUs activated in
629 * SMP mode to <NUM>.
630 */
631
632void __weak arch_disable_smp_support(void) { }
633
634static int __init nosmp(char *str)
635{
636 setup_max_cpus = 0;
637 arch_disable_smp_support();
638
639 return 0;
640}
641
642early_param("nosmp", nosmp);
643
644/* this is hard limit */
645static int __init nrcpus(char *str)
646{
647 int nr_cpus;
648
649 get_option(&str, &nr_cpus);
650 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
651 nr_cpu_ids = nr_cpus;
652
653 return 0;
654}
655
656early_param("nr_cpus", nrcpus);
657
658static int __init maxcpus(char *str)
659{
660 get_option(&str, &setup_max_cpus);
661 if (setup_max_cpus == 0)
662 arch_disable_smp_support();
663
664 return 0;
665}
666
667early_param("maxcpus", maxcpus);
668
669/* Setup number of possible processor ids */
670int nr_cpu_ids __read_mostly = NR_CPUS;
671EXPORT_SYMBOL(nr_cpu_ids);
672
673/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
674void __init setup_nr_cpu_ids(void)
675{
676 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
677}
678
679/* Called by boot processor to activate the rest. */
680void __init smp_init(void)
681{
682 unsigned int cpu;
683
684 /* FIXME: This should be done in userspace --RR */
685 for_each_present_cpu(cpu) {
686 if (num_online_cpus() >= setup_max_cpus)
687 break;
688 if (!cpu_online(cpu))
689 cpu_up(cpu);
690 }
691
692 /* Any cleanup work */
693 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
694 smp_cpus_done(setup_max_cpus);
695}
696
Amerigo Wang351f8f82011-01-12 16:59:39 -0800697/*
Tejun Heobd924e82011-01-20 12:07:13 +0100698 * Call a function on all processors. May be used during early boot while
699 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
700 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800701 */
702int on_each_cpu(void (*func) (void *info), void *info, int wait)
703{
Tejun Heobd924e82011-01-20 12:07:13 +0100704 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800705 int ret = 0;
706
707 preempt_disable();
708 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100709 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800710 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100711 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800712 preempt_enable();
713 return ret;
714}
715EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700716
717/**
718 * on_each_cpu_mask(): Run a function on processors specified by
719 * cpumask, which may include the local processor.
720 * @mask: The set of cpus to run on (only runs on online subset).
721 * @func: The function to run. This must be fast and non-blocking.
722 * @info: An arbitrary pointer to pass to the function.
723 * @wait: If true, wait (atomically) until function has completed
724 * on other CPUs.
725 *
726 * If @wait is true, then returns once @func has returned.
727 *
728 * You must not call this function with disabled interrupts or
729 * from a hardware interrupt handler or from a bottom half handler.
730 */
731void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
732 void *info, bool wait)
733{
734 int cpu = get_cpu();
735
736 smp_call_function_many(mask, func, info, wait);
737 if (cpumask_test_cpu(cpu, mask)) {
738 local_irq_disable();
739 func(info);
740 local_irq_enable();
741 }
742 put_cpu();
743}
744EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700745
746/*
747 * on_each_cpu_cond(): Call a function on each processor for which
748 * the supplied function cond_func returns true, optionally waiting
749 * for all the required CPUs to finish. This may include the local
750 * processor.
751 * @cond_func: A callback function that is passed a cpu id and
752 * the the info parameter. The function is called
753 * with preemption disabled. The function should
754 * return a blooean value indicating whether to IPI
755 * the specified CPU.
756 * @func: The function to run on all applicable CPUs.
757 * This must be fast and non-blocking.
758 * @info: An arbitrary pointer to pass to both functions.
759 * @wait: If true, wait (atomically) until function has
760 * completed on other CPUs.
761 * @gfp_flags: GFP flags to use when allocating the cpumask
762 * used internally by the function.
763 *
764 * The function might sleep if the GFP flags indicates a non
765 * atomic allocation is allowed.
766 *
767 * Preemption is disabled to protect against CPUs going offline but not online.
768 * CPUs going online during the call will not be seen or sent an IPI.
769 *
770 * You must not call this function with disabled interrupts or
771 * from a hardware interrupt handler or from a bottom half handler.
772 */
773void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
774 smp_call_func_t func, void *info, bool wait,
775 gfp_t gfp_flags)
776{
777 cpumask_var_t cpus;
778 int cpu, ret;
779
780 might_sleep_if(gfp_flags & __GFP_WAIT);
781
782 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
783 preempt_disable();
784 for_each_online_cpu(cpu)
785 if (cond_func(cpu, info))
786 cpumask_set_cpu(cpu, cpus);
787 on_each_cpu_mask(cpus, func, info, wait);
788 preempt_enable();
789 free_cpumask_var(cpus);
790 } else {
791 /*
792 * No free cpumask, bother. No matter, we'll
793 * just have to IPI them one by one.
794 */
795 preempt_disable();
796 for_each_online_cpu(cpu)
797 if (cond_func(cpu, info)) {
798 ret = smp_call_function_single(cpu, func,
799 info, wait);
800 WARN_ON_ONCE(!ret);
801 }
802 preempt_enable();
803 }
804}
805EXPORT_SYMBOL(on_each_cpu_cond);