blob: 23ccc67dcbb261b6a7dd127c919fe61692ecb97d [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>
Chris Redpath5ecaba32013-10-11 11:45:01 +010015#define CREATE_TRACE_POINTS
16#include <trace/events/smp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020017
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070018#include "smpboot.h"
19
Amerigo Wang351f8f82011-01-12 16:59:39 -080020#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
Jens Axboe3d442232008-06-26 11:21:34 +020021enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010022 CSD_FLAG_LOCK = 0x01,
Jens Axboe3d442232008-06-26 11:21:34 +020023};
24
25struct call_function_data {
Shaohua Li9a46ad62013-02-21 16:43:03 -080026 struct call_single_data __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010027 cpumask_var_t cpumask;
Wang YanQingf44310b2013-01-26 15:53:57 +080028 cpumask_var_t cpumask_ipi;
Jens Axboe3d442232008-06-26 11:21:34 +020029};
30
Milton Millere03bcb62010-01-18 13:00:51 +110031static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
32
Jens Axboe3d442232008-06-26 11:21:34 +020033struct call_single_queue {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010034 struct list_head list;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010035 raw_spinlock_t lock;
Jens Axboe3d442232008-06-26 11:21:34 +020036};
37
Milton Millere03bcb62010-01-18 13:00:51 +110038static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010039
40static int
41hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
42{
43 long cpu = (long)hcpu;
44 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
45
46 switch (action) {
47 case CPU_UP_PREPARE:
48 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070049 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010050 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070051 return notifier_from_errno(-ENOMEM);
Wang YanQingf44310b2013-01-26 15:53:57 +080052 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
53 cpu_to_node(cpu)))
54 return notifier_from_errno(-ENOMEM);
Shaohua Li9a46ad62013-02-21 16:43:03 -080055 cfd->csd = alloc_percpu(struct call_single_data);
56 if (!cfd->csd) {
57 free_cpumask_var(cfd->cpumask);
58 return notifier_from_errno(-ENOMEM);
59 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010060 break;
61
Xiao Guangrong69dd6472009-08-06 15:07:29 -070062#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010063 case CPU_UP_CANCELED:
64 case CPU_UP_CANCELED_FROZEN:
65
66 case CPU_DEAD:
67 case CPU_DEAD_FROZEN:
68 free_cpumask_var(cfd->cpumask);
Wang YanQingf44310b2013-01-26 15:53:57 +080069 free_cpumask_var(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -080070 free_percpu(cfd->csd);
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 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700105static void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100106{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700107 while (csd->flags & CSD_FLAG_LOCK)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100108 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100109}
110
Andrew Mortone1d12f32013-04-30 15:27:28 -0700111static void csd_lock(struct call_single_data *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100112{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700113 csd_lock_wait(csd);
114 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100115
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
Andrew Mortone1d12f32013-04-30 15:27:28 -0700124static void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100125{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700126 WARN_ON(!(csd->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
Andrew Mortone1d12f32013-04-30 15:27:28 -0700133 csd->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
Andrew Mortone1d12f32013-04-30 15:27:28 -0700142void generic_exec_single(int cpu, struct call_single_data *csd, 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);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700150 list_add_tail(&csd->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 */
Chris Redpath5ecaba32013-10-11 11:45:01 +0100164 if (ipi) {
165 trace_smp_call_func_send(csd->func, cpu);
Jens Axboe3d442232008-06-26 11:21:34 +0200166 arch_send_call_function_single_ipi(cpu);
Chris Redpath5ecaba32013-10-11 11:45:01 +0100167 }
Jens Axboe3d442232008-06-26 11:21:34 +0200168
169 if (wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700170 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200171}
172
173/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100174 * Invoked by arch to handle an IPI for call function single. Must be
175 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200176 */
177void generic_smp_call_function_single_interrupt(void)
178{
179 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100180 LIST_HEAD(list);
Jens Axboe3d442232008-06-26 11:21:34 +0200181
Suresh Siddha269c8612009-08-19 18:05:35 -0700182 /*
183 * Shouldn't receive this interrupt on a cpu that is not yet online.
184 */
185 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
186
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100187 raw_spin_lock(&q->lock);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100188 list_replace_init(&q->list, &list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100189 raw_spin_unlock(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200190
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100191 while (!list_empty(&list)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700192 struct call_single_data *csd;
193 unsigned int csd_flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200194
Andrew Mortone1d12f32013-04-30 15:27:28 -0700195 csd = list_entry(list.next, struct call_single_data, list);
196 list_del(&csd->list);
Jens Axboe3d442232008-06-26 11:21:34 +0200197
Jens Axboe3d442232008-06-26 11:21:34 +0200198 /*
Andrew Mortone1d12f32013-04-30 15:27:28 -0700199 * 'csd' can be invalid after this call if flags == 0
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100200 * (when called through generic_exec_single()),
201 * so save them away before making the call:
Jens Axboe3d442232008-06-26 11:21:34 +0200202 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700203 csd_flags = csd->flags;
Chris Redpath5ecaba32013-10-11 11:45:01 +0100204 trace_smp_call_func_entry(csd->func);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700205 csd->func(csd->info);
Chris Redpath5ecaba32013-10-11 11:45:01 +0100206 trace_smp_call_func_exit(csd->func);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100207
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100208 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100209 * Unlocked CSDs are valid through generic_exec_single():
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100210 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700211 if (csd_flags & CSD_FLAG_LOCK)
212 csd_unlock(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200213 }
214}
215
Milton Millere03bcb62010-01-18 13:00:51 +1100216static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500217
Jens Axboe3d442232008-06-26 11:21:34 +0200218/*
219 * smp_call_function_single - Run a function on a specific CPU
220 * @func: The function to run. This must be fast and non-blocking.
221 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200222 * @wait: If true, wait until function has completed on other CPUs.
223 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800224 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200225 */
David Howells3a5f65d2010-10-27 17:28:36 +0100226int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200227 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200228{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100229 struct call_single_data d = {
230 .flags = 0,
231 };
Jens Axboe3d442232008-06-26 11:21:34 +0200232 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100233 int this_cpu;
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700234 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200235
Chris Redpath5ecaba32013-10-11 11:45:01 +0100236 trace_smp_call_func_send(func, cpu);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100237 /*
238 * prevent preemption and reschedule on another processor,
239 * as well as CPU removal
240 */
241 this_cpu = get_cpu();
242
Suresh Siddha269c8612009-08-19 18:05:35 -0700243 /*
244 * Can deadlock when called with interrupts disabled.
245 * We allow cpu's that are not yet online though, as no one else can
246 * send smp call function interrupt to this cpu and as such deadlocks
247 * can't happen.
248 */
249 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
250 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200251
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100252 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200253 local_irq_save(flags);
Chris Redpath5ecaba32013-10-11 11:45:01 +0100254 trace_smp_call_func_entry(func);
Jens Axboe3d442232008-06-26 11:21:34 +0200255 func(info);
Chris Redpath5ecaba32013-10-11 11:45:01 +0100256 trace_smp_call_func_exit(func);
Jens Axboe3d442232008-06-26 11:21:34 +0200257 local_irq_restore(flags);
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700258 } else {
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100259 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700260 struct call_single_data *csd = &d;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100261
262 if (!wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700263 csd = &__get_cpu_var(csd_data);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100264
Andrew Mortone1d12f32013-04-30 15:27:28 -0700265 csd_lock(csd);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100266
Andrew Mortone1d12f32013-04-30 15:27:28 -0700267 csd->func = func;
268 csd->info = info;
269 generic_exec_single(cpu, csd, wait);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100270 } else {
271 err = -ENXIO; /* CPU not online */
272 }
Jens Axboe3d442232008-06-26 11:21:34 +0200273 }
274
275 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100276
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700277 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200278}
279EXPORT_SYMBOL(smp_call_function_single);
280
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800281/*
282 * smp_call_function_any - Run a function on any of the given cpus
283 * @mask: The mask of cpus it can run on.
284 * @func: The function to run. This must be fast and non-blocking.
285 * @info: An arbitrary pointer to pass to the function.
286 * @wait: If true, wait until function has completed.
287 *
288 * Returns 0 on success, else a negative status code (if no cpus were online).
289 * Note that @wait will be implicitly turned on in case of allocation failures,
290 * since we fall back to on-stack allocation.
291 *
292 * Selection preference:
293 * 1) current cpu if in @mask
294 * 2) any cpu of current node if in @mask
295 * 3) any other online cpu in @mask
296 */
297int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100298 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800299{
300 unsigned int cpu;
301 const struct cpumask *nodemask;
302 int ret;
303
304 /* Try for same CPU (cheapest) */
305 cpu = get_cpu();
306 if (cpumask_test_cpu(cpu, mask))
307 goto call;
308
309 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800310 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800311 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
312 cpu = cpumask_next_and(cpu, nodemask, mask)) {
313 if (cpu_online(cpu))
314 goto call;
315 }
316
317 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
318 cpu = cpumask_any_and(mask, cpu_online_mask);
319call:
320 ret = smp_call_function_single(cpu, func, info, wait);
321 put_cpu();
322 return ret;
323}
324EXPORT_SYMBOL_GPL(smp_call_function_any);
325
Jens Axboe3d442232008-06-26 11:21:34 +0200326/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200327 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200328 * @cpu: The CPU to run on.
329 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200330 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200331 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100332 * Like smp_call_function_single(), but allow caller to pass in a
333 * pre-allocated data structure. Useful for embedding @data inside
334 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200335 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700336void __smp_call_function_single(int cpu, struct call_single_data *csd,
Peter Zijlstra6e275632009-02-25 13:59:48 +0100337 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200338{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200339 unsigned int this_cpu;
340 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200341
Heiko Carstens27c379f2010-09-10 13:47:29 +0200342 this_cpu = get_cpu();
Suresh Siddha269c8612009-08-19 18:05:35 -0700343 /*
344 * Can deadlock when called with interrupts disabled.
345 * We allow cpu's that are not yet online though, as no one else can
346 * send smp call function interrupt to this cpu and as such deadlocks
347 * can't happen.
348 */
349 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
350 && !oops_in_progress);
Peter Zijlstra6e275632009-02-25 13:59:48 +0100351
Heiko Carstens27c379f2010-09-10 13:47:29 +0200352 if (cpu == this_cpu) {
353 local_irq_save(flags);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700354 csd->func(csd->info);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200355 local_irq_restore(flags);
356 } else {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700357 csd_lock(csd);
358 generic_exec_single(cpu, csd, wait);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200359 }
360 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200361}
362
363/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030364 * smp_call_function_many(): Run a function on a set of other CPUs.
365 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200366 * @func: The function to run. This must be fast and non-blocking.
367 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100368 * @wait: If true, wait (atomically) until function has completed
369 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200370 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800371 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200372 *
373 * You must not call this function with disabled interrupts or from a
374 * hardware interrupt handler or from a bottom half handler. Preemption
375 * must be disabled when calling this function.
376 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030377void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100378 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200379{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700380 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800381 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200382
Suresh Siddha269c8612009-08-19 18:05:35 -0700383 /*
384 * Can deadlock when called with interrupts disabled.
385 * We allow cpu's that are not yet online though, as no one else can
386 * send smp call function interrupt to this cpu and as such deadlocks
387 * can't happen.
388 */
389 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100390 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200391
Milton Miller723aae22011-03-15 13:27:17 -0600392 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030393 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100394 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030395 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100396
Rusty Russell54b11e62008-12-30 09:05:16 +1030397 /* No online cpus? We're done. */
398 if (cpu >= nr_cpu_ids)
399 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200400
Rusty Russell54b11e62008-12-30 09:05:16 +1030401 /* Do we have another CPU which isn't us? */
402 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100403 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030404 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
405
406 /* Fastpath: do that cpu by itself. */
407 if (next_cpu >= nr_cpu_ids) {
408 smp_call_function_single(cpu, func, info, wait);
409 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200410 }
411
Andrew Mortone1d12f32013-04-30 15:27:28 -0700412 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600413
Andrew Mortone1d12f32013-04-30 15:27:28 -0700414 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
415 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600416
417 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700418 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600419 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800420
Wang YanQingf44310b2013-01-26 15:53:57 +0800421 /*
Andrew Mortone1d12f32013-04-30 15:27:28 -0700422 * After we put an entry into the list, cfd->cpumask may be cleared
423 * again when another CPU sends another IPI for a SMP function call, so
424 * cfd->cpumask will be zero.
Wang YanQingf44310b2013-01-26 15:53:57 +0800425 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700426 cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200427
Andrew Mortone1d12f32013-04-30 15:27:28 -0700428 for_each_cpu(cpu, cfd->cpumask) {
429 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800430 struct call_single_queue *dst =
431 &per_cpu(call_single_queue, cpu);
432 unsigned long flags;
433
434 csd_lock(csd);
435 csd->func = func;
436 csd->info = info;
437
438 raw_spin_lock_irqsave(&dst->lock, flags);
439 list_add_tail(&csd->list, &dst->list);
440 raw_spin_unlock_irqrestore(&dst->lock, flags);
441 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100442
Jens Axboe3d442232008-06-26 11:21:34 +0200443 /* Send a message to all CPUs in the map */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700444 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200445
Shaohua Li9a46ad62013-02-21 16:43:03 -0800446 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700447 for_each_cpu(cpu, cfd->cpumask) {
448 struct call_single_data *csd;
449
450 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800451 csd_lock_wait(csd);
452 }
453 }
Jens Axboe3d442232008-06-26 11:21:34 +0200454}
Rusty Russell54b11e62008-12-30 09:05:16 +1030455EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200456
457/**
458 * smp_call_function(): Run a function on all other CPUs.
459 * @func: The function to run. This must be fast and non-blocking.
460 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100461 * @wait: If true, wait (atomically) until function has completed
462 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200463 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030464 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200465 *
466 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800467 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200468 *
469 * You must not call this function with disabled interrupts or from a
470 * hardware interrupt handler or from a bottom half handler.
471 */
David Howells3a5f65d2010-10-27 17:28:36 +0100472int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200473{
Jens Axboe3d442232008-06-26 11:21:34 +0200474 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030475 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200476 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100477
Rusty Russell54b11e62008-12-30 09:05:16 +1030478 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200479}
480EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800481#endif /* USE_GENERIC_SMP_HELPERS */
482
Amerigo Wang34db18a02011-03-22 16:34:06 -0700483/* Setup configured maximum number of CPUs to activate */
484unsigned int setup_max_cpus = NR_CPUS;
485EXPORT_SYMBOL(setup_max_cpus);
486
487
488/*
489 * Setup routine for controlling SMP activation
490 *
491 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
492 * activation entirely (the MPS table probe still happens, though).
493 *
494 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
495 * greater than 0, limits the maximum number of CPUs activated in
496 * SMP mode to <NUM>.
497 */
498
499void __weak arch_disable_smp_support(void) { }
500
501static int __init nosmp(char *str)
502{
503 setup_max_cpus = 0;
504 arch_disable_smp_support();
505
506 return 0;
507}
508
509early_param("nosmp", nosmp);
510
511/* this is hard limit */
512static int __init nrcpus(char *str)
513{
514 int nr_cpus;
515
516 get_option(&str, &nr_cpus);
517 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
518 nr_cpu_ids = nr_cpus;
519
520 return 0;
521}
522
523early_param("nr_cpus", nrcpus);
524
525static int __init maxcpus(char *str)
526{
527 get_option(&str, &setup_max_cpus);
528 if (setup_max_cpus == 0)
529 arch_disable_smp_support();
530
531 return 0;
532}
533
534early_param("maxcpus", maxcpus);
535
536/* Setup number of possible processor ids */
537int nr_cpu_ids __read_mostly = NR_CPUS;
538EXPORT_SYMBOL(nr_cpu_ids);
539
540/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
541void __init setup_nr_cpu_ids(void)
542{
543 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
544}
545
546/* Called by boot processor to activate the rest. */
547void __init smp_init(void)
548{
549 unsigned int cpu;
550
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700551 idle_threads_init();
552
Amerigo Wang34db18a02011-03-22 16:34:06 -0700553 /* FIXME: This should be done in userspace --RR */
554 for_each_present_cpu(cpu) {
555 if (num_online_cpus() >= setup_max_cpus)
556 break;
557 if (!cpu_online(cpu))
558 cpu_up(cpu);
559 }
560
561 /* Any cleanup work */
562 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
563 smp_cpus_done(setup_max_cpus);
564}
565
Amerigo Wang351f8f82011-01-12 16:59:39 -0800566/*
Tejun Heobd924e82011-01-20 12:07:13 +0100567 * Call a function on all processors. May be used during early boot while
568 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
569 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800570 */
571int on_each_cpu(void (*func) (void *info), void *info, int wait)
572{
Tejun Heobd924e82011-01-20 12:07:13 +0100573 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800574 int ret = 0;
575
576 preempt_disable();
577 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100578 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800579 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100580 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800581 preempt_enable();
582 return ret;
583}
584EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700585
586/**
587 * on_each_cpu_mask(): Run a function on processors specified by
588 * cpumask, which may include the local processor.
589 * @mask: The set of cpus to run on (only runs on online subset).
590 * @func: The function to run. This must be fast and non-blocking.
591 * @info: An arbitrary pointer to pass to the function.
592 * @wait: If true, wait (atomically) until function has completed
593 * on other CPUs.
594 *
595 * If @wait is true, then returns once @func has returned.
596 *
597 * You must not call this function with disabled interrupts or
598 * from a hardware interrupt handler or from a bottom half handler.
599 */
600void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
601 void *info, bool wait)
602{
603 int cpu = get_cpu();
604
605 smp_call_function_many(mask, func, info, wait);
606 if (cpumask_test_cpu(cpu, mask)) {
607 local_irq_disable();
608 func(info);
609 local_irq_enable();
610 }
611 put_cpu();
612}
613EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700614
615/*
616 * on_each_cpu_cond(): Call a function on each processor for which
617 * the supplied function cond_func returns true, optionally waiting
618 * for all the required CPUs to finish. This may include the local
619 * processor.
620 * @cond_func: A callback function that is passed a cpu id and
621 * the the info parameter. The function is called
622 * with preemption disabled. The function should
623 * return a blooean value indicating whether to IPI
624 * the specified CPU.
625 * @func: The function to run on all applicable CPUs.
626 * This must be fast and non-blocking.
627 * @info: An arbitrary pointer to pass to both functions.
628 * @wait: If true, wait (atomically) until function has
629 * completed on other CPUs.
630 * @gfp_flags: GFP flags to use when allocating the cpumask
631 * used internally by the function.
632 *
633 * The function might sleep if the GFP flags indicates a non
634 * atomic allocation is allowed.
635 *
636 * Preemption is disabled to protect against CPUs going offline but not online.
637 * CPUs going online during the call will not be seen or sent an IPI.
638 *
639 * You must not call this function with disabled interrupts or
640 * from a hardware interrupt handler or from a bottom half handler.
641 */
642void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
643 smp_call_func_t func, void *info, bool wait,
644 gfp_t gfp_flags)
645{
646 cpumask_var_t cpus;
647 int cpu, ret;
648
649 might_sleep_if(gfp_flags & __GFP_WAIT);
650
651 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
652 preempt_disable();
653 for_each_online_cpu(cpu)
654 if (cond_func(cpu, info))
655 cpumask_set_cpu(cpu, cpus);
656 on_each_cpu_mask(cpus, func, info, wait);
657 preempt_enable();
658 free_cpumask_var(cpus);
659 } else {
660 /*
661 * No free cpumask, bother. No matter, we'll
662 * just have to IPI them one by one.
663 */
664 preempt_disable();
665 for_each_online_cpu(cpu)
666 if (cond_func(cpu, info)) {
667 ret = smp_call_function_single(cpu, func,
668 info, wait);
669 WARN_ON_ONCE(!ret);
670 }
671 preempt_enable();
672 }
673}
674EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000675
676static void do_nothing(void *unused)
677{
678}
679
680/**
681 * kick_all_cpus_sync - Force all cpus out of idle
682 *
683 * Used to synchronize the update of pm_idle function pointer. It's
684 * called after the pointer is updated and returns after the dummy
685 * callback function has been executed on all cpus. The execution of
686 * the function can only happen on the remote cpus after they have
687 * left the idle function which had been called via pm_idle function
688 * pointer. So it's guaranteed that nothing uses the previous pointer
689 * anymore.
690 */
691void kick_all_cpus_sync(void)
692{
693 /* Make sure the change is visible before we kick the cpus */
694 smp_mb();
695 smp_call_function(do_nothing, NULL, 1);
696}
697EXPORT_SYMBOL_GPL(kick_all_cpus_sync);