blob: 80c33f8de14ffbdb83aaf6be0bc5c31c5d3e6351 [file] [log] [blame]
Jens Axboe3d442232008-06-26 11:21:34 +02001/*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
Jens Axboe3d442232008-06-26 11:21:34 +02005 */
Jens Axboe3d442232008-06-26 11:21:34 +02006#include <linux/rcupdate.h>
Linus Torvalds59190f4212008-07-15 14:02:33 -07007#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +01008#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010010#include <linux/percpu.h>
11#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020013#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010014#include <linux/cpu.h>
Jens Axboe3d442232008-06-26 11:21:34 +020015
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070016#include "smpboot.h"
17
Jens Axboe3d442232008-06-26 11:21:34 +020018enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010019 CSD_FLAG_LOCK = 0x01,
Jens Axboec84a83e2013-05-17 09:58:43 +020020 CSD_FLAG_WAIT = 0x02,
Jens Axboe3d442232008-06-26 11:21:34 +020021};
22
23struct call_function_data {
Shaohua Li9a46ad62013-02-21 16:43:03 -080024 struct call_single_data __percpu *csd;
Ingo Molnar0b13fda2009-02-25 16:52:11 +010025 cpumask_var_t cpumask;
Jens Axboe3d442232008-06-26 11:21:34 +020026};
27
Milton Millere03bcb62010-01-18 13:00:51 +110028static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
29
Christoph Hellwig6897fc22014-01-30 15:45:47 -080030static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010031
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070032static void flush_smp_call_function_queue(bool warn_cpu_offline);
33
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010034static int
35hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
36{
37 long cpu = (long)hcpu;
38 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
39
40 switch (action) {
41 case CPU_UP_PREPARE:
42 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070043 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010044 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070045 return notifier_from_errno(-ENOMEM);
Shaohua Li9a46ad62013-02-21 16:43:03 -080046 cfd->csd = alloc_percpu(struct call_single_data);
47 if (!cfd->csd) {
48 free_cpumask_var(cfd->cpumask);
49 return notifier_from_errno(-ENOMEM);
50 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010051 break;
52
Xiao Guangrong69dd6472009-08-06 15:07:29 -070053#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010054 case CPU_UP_CANCELED:
55 case CPU_UP_CANCELED_FROZEN:
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070056 /* Fall-through to the CPU_DEAD[_FROZEN] case. */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010057
58 case CPU_DEAD:
59 case CPU_DEAD_FROZEN:
60 free_cpumask_var(cfd->cpumask);
Shaohua Li9a46ad62013-02-21 16:43:03 -080061 free_percpu(cfd->csd);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010062 break;
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -070063
64 case CPU_DYING:
65 case CPU_DYING_FROZEN:
66 /*
67 * The IPIs for the smp-call-function callbacks queued by other
68 * CPUs might arrive late, either due to hardware latencies or
69 * because this CPU disabled interrupts (inside stop-machine)
70 * before the IPIs were sent. So flush out any pending callbacks
71 * explicitly (without waiting for the IPIs to arrive), to
72 * ensure that the outgoing CPU doesn't go offline with work
73 * still pending.
74 */
75 flush_smp_call_function_queue(false);
76 break;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010077#endif
78 };
79
80 return NOTIFY_OK;
81}
82
Paul Gortmaker0db06282013-06-19 14:53:51 -040083static struct notifier_block hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010084 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010085};
86
Takao Indohd8ad7d12011-03-29 12:35:04 -040087void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020088{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010089 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020090 int i;
91
Christoph Hellwig6897fc22014-01-30 15:45:47 -080092 for_each_possible_cpu(i)
93 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010094
95 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
96 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020097}
98
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010099/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100100 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
101 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100102 * For non-synchronous ipi calls the csd can still be in use by the
103 * previous function call. For multi-cpu calls its even more interesting
104 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100105 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700106static void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100107{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700108 while (csd->flags & CSD_FLAG_LOCK)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100109 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100110}
111
Andrew Mortone1d12f32013-04-30 15:27:28 -0700112static void csd_lock(struct call_single_data *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100113{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700114 csd_lock_wait(csd);
115 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100116
117 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100118 * prevent CPU from reordering the above assignment
119 * to ->flags with any subsequent assignments to other
120 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100121 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100122 smp_mb();
123}
124
Andrew Mortone1d12f32013-04-30 15:27:28 -0700125static void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100126{
Jens Axboec84a83e2013-05-17 09:58:43 +0200127 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100128
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100129 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100130 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100131 */
132 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100133
Andrew Mortone1d12f32013-04-30 15:27:28 -0700134 csd->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200135}
136
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100137static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
138
Jens Axboe3d442232008-06-26 11:21:34 +0200139/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100140 * Insert a previously allocated call_single_data element
141 * for execution on the given CPU. data must already have
142 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200143 */
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100144static int generic_exec_single(int cpu, struct call_single_data *csd,
145 smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200146{
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100147 struct call_single_data csd_stack = { .flags = 0 };
148 unsigned long flags;
149
150
151 if (cpu == smp_processor_id()) {
152 local_irq_save(flags);
153 func(info);
154 local_irq_restore(flags);
155 return 0;
156 }
157
158
159 if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu))
160 return -ENXIO;
161
162
163 if (!csd) {
164 csd = &csd_stack;
165 if (!wait)
166 csd = &__get_cpu_var(csd_data);
167 }
168
169 csd_lock(csd);
170
171 csd->func = func;
172 csd->info = info;
173
Jens Axboec84a83e2013-05-17 09:58:43 +0200174 if (wait)
175 csd->flags |= CSD_FLAG_WAIT;
176
Suresh Siddha561920a02008-10-30 18:28:41 +0100177 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100178 * The list addition should be visible before sending the IPI
179 * handler locks the list to pull the entry off it because of
180 * normal cache coherency rules implied by spinlocks.
181 *
182 * If IPIs can go out of order to the cache coherency protocol
183 * in an architecture, sufficient synchronisation should be added
184 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100185 * locking and barrier primitives. Generic code isn't really
186 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100187 */
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800188 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Jens Axboe3d442232008-06-26 11:21:34 +0200189 arch_send_call_function_single_ipi(cpu);
190
191 if (wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700192 csd_lock_wait(csd);
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100193
194 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200195}
196
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700197/**
198 * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
199 *
200 * Invoked by arch to handle an IPI for call function single.
201 * Must be called with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200202 */
203void generic_smp_call_function_single_interrupt(void)
204{
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700205 flush_smp_call_function_queue(true);
206}
207
208/**
209 * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
210 *
211 * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
212 * offline CPU. Skip this check if set to 'false'.
213 *
214 * Flush any pending smp-call-function callbacks queued on this CPU. This is
215 * invoked by the generic IPI handler, as well as by a CPU about to go offline,
216 * to ensure that all pending IPI callbacks are run before it goes completely
217 * offline.
218 *
219 * Loop through the call_single_queue and run all the queued callbacks.
220 * Must be called with interrupts disabled.
221 */
222static void flush_smp_call_function_queue(bool warn_cpu_offline)
223{
224 struct llist_head *head;
Jan Kara5fd77592014-02-24 16:39:55 +0100225 struct llist_node *entry;
226 struct call_single_data *csd, *csd_next;
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700227 static bool warned;
228
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700229 WARN_ON(!irqs_disabled());
230
231 head = &__get_cpu_var(call_single_queue);
232 entry = llist_del_all(head);
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700233 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200234
Srivatsa S. Bhat8d056c42014-06-23 13:22:02 -0700235 /* There shouldn't be any pending callbacks on an offline CPU. */
236 if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
237 !warned && !llist_empty(head))) {
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700238 warned = true;
239 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
Suresh Siddha269c8612009-08-19 18:05:35 -0700240
Srivatsa S. Bhata219ccf2014-06-06 14:37:05 -0700241 /*
242 * We don't have to use the _safe() variant here
243 * because we are not invoking the IPI handlers yet.
244 */
245 llist_for_each_entry(csd, entry, llist)
246 pr_warn("IPI callback %pS sent to offline CPU\n",
247 csd->func);
248 }
Jens Axboe3d442232008-06-26 11:21:34 +0200249
Jan Kara5fd77592014-02-24 16:39:55 +0100250 llist_for_each_entry_safe(csd, csd_next, entry, llist) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700251 csd->func(csd->info);
Xie XiuQi46591962013-07-30 11:06:09 +0800252 csd_unlock(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200253 }
254}
255
256/*
257 * smp_call_function_single - Run a function on a specific CPU
258 * @func: The function to run. This must be fast and non-blocking.
259 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200260 * @wait: If true, wait until function has completed on other CPUs.
261 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800262 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200263 */
David Howells3a5f65df2010-10-27 17:28:36 +0100264int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200265 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200266{
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100267 int this_cpu;
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100268 int err;
Jens Axboe3d442232008-06-26 11:21:34 +0200269
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100270 /*
271 * prevent preemption and reschedule on another processor,
272 * as well as CPU removal
273 */
274 this_cpu = get_cpu();
275
Suresh Siddha269c8612009-08-19 18:05:35 -0700276 /*
277 * Can deadlock when called with interrupts disabled.
278 * We allow cpu's that are not yet online though, as no one else can
279 * send smp call function interrupt to this cpu and as such deadlocks
280 * can't happen.
281 */
282 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
283 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200284
Frederic Weisbecker8b284992014-02-24 16:39:58 +0100285 err = generic_exec_single(cpu, NULL, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200286
287 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100288
H. Peter Anvinf73be6de2008-08-25 17:07:14 -0700289 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200290}
291EXPORT_SYMBOL(smp_call_function_single);
292
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100293/**
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100294 * smp_call_function_single_async(): Run an asynchronous function on a
295 * specific CPU.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100296 * @cpu: The CPU to run on.
297 * @csd: Pre-allocated and setup data structure
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100298 *
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100299 * Like smp_call_function_single(), but the call is asynchonous and
300 * can thus be done from contexts with disabled interrupts.
301 *
302 * The caller passes his own pre-allocated data structure
303 * (ie: embedded in an object) and is responsible for synchronizing it
304 * such that the IPIs performed on the @csd are strictly serialized.
305 *
306 * NOTE: Be careful, there is unfortunately no current debugging facility to
307 * validate the correctness of this serialization.
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100308 */
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100309int smp_call_function_single_async(int cpu, struct call_single_data *csd)
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100310{
311 int err = 0;
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100312
Frederic Weisbeckerfce8ad12014-02-24 16:40:01 +0100313 preempt_disable();
314 err = generic_exec_single(cpu, csd, csd->func, csd->info, 0);
315 preempt_enable();
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100316
317 return err;
318}
Frederic Weisbeckerc46fff22014-02-24 16:40:02 +0100319EXPORT_SYMBOL_GPL(smp_call_function_single_async);
Frederic Weisbeckerd7877c02014-02-24 16:39:59 +0100320
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800321/*
322 * smp_call_function_any - Run a function on any of the given cpus
323 * @mask: The mask of cpus it can run on.
324 * @func: The function to run. This must be fast and non-blocking.
325 * @info: An arbitrary pointer to pass to the function.
326 * @wait: If true, wait until function has completed.
327 *
328 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800329 *
330 * Selection preference:
331 * 1) current cpu if in @mask
332 * 2) any cpu of current node if in @mask
333 * 3) any other online cpu in @mask
334 */
335int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100336 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800337{
338 unsigned int cpu;
339 const struct cpumask *nodemask;
340 int ret;
341
342 /* Try for same CPU (cheapest) */
343 cpu = get_cpu();
344 if (cpumask_test_cpu(cpu, mask))
345 goto call;
346
347 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800348 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800349 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
350 cpu = cpumask_next_and(cpu, nodemask, mask)) {
351 if (cpu_online(cpu))
352 goto call;
353 }
354
355 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
356 cpu = cpumask_any_and(mask, cpu_online_mask);
357call:
358 ret = smp_call_function_single(cpu, func, info, wait);
359 put_cpu();
360 return ret;
361}
362EXPORT_SYMBOL_GPL(smp_call_function_any);
363
Jens Axboe3d442232008-06-26 11:21:34 +0200364/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030365 * smp_call_function_many(): Run a function on a set of other CPUs.
366 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200367 * @func: The function to run. This must be fast and non-blocking.
368 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100369 * @wait: If true, wait (atomically) until function has completed
370 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200371 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800372 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200373 *
374 * You must not call this function with disabled interrupts or from a
375 * hardware interrupt handler or from a bottom half handler. Preemption
376 * must be disabled when calling this function.
377 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030378void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65df2010-10-27 17:28:36 +0100379 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200380{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700381 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800382 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200383
Suresh Siddha269c8612009-08-19 18:05:35 -0700384 /*
385 * Can deadlock when called with interrupts disabled.
386 * We allow cpu's that are not yet online though, as no one else can
387 * send smp call function interrupt to this cpu and as such deadlocks
388 * can't happen.
389 */
390 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100391 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200392
Milton Miller723aae22011-03-15 13:27:17 -0600393 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030394 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100395 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030396 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100397
Rusty Russell54b11e62008-12-30 09:05:16 +1030398 /* No online cpus? We're done. */
399 if (cpu >= nr_cpu_ids)
400 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200401
Rusty Russell54b11e62008-12-30 09:05:16 +1030402 /* Do we have another CPU which isn't us? */
403 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100404 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030405 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
406
407 /* Fastpath: do that cpu by itself. */
408 if (next_cpu >= nr_cpu_ids) {
409 smp_call_function_single(cpu, func, info, wait);
410 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200411 }
412
Andrew Mortone1d12f32013-04-30 15:27:28 -0700413 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600414
Andrew Mortone1d12f32013-04-30 15:27:28 -0700415 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
416 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600417
418 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700419 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600420 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800421
Andrew Mortone1d12f32013-04-30 15:27:28 -0700422 for_each_cpu(cpu, cfd->cpumask) {
423 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800424
425 csd_lock(csd);
426 csd->func = func;
427 csd->info = info;
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800428 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
Shaohua Li9a46ad62013-02-21 16:43:03 -0800429 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100430
Jens Axboe3d442232008-06-26 11:21:34 +0200431 /* Send a message to all CPUs in the map */
Roman Gushchin73f94552014-01-30 15:45:48 -0800432 arch_send_call_function_ipi_mask(cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200433
Shaohua Li9a46ad62013-02-21 16:43:03 -0800434 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700435 for_each_cpu(cpu, cfd->cpumask) {
436 struct call_single_data *csd;
437
438 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800439 csd_lock_wait(csd);
440 }
441 }
Jens Axboe3d442232008-06-26 11:21:34 +0200442}
Rusty Russell54b11e62008-12-30 09:05:16 +1030443EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200444
445/**
446 * smp_call_function(): Run a function on all other CPUs.
447 * @func: The function to run. This must be fast and non-blocking.
448 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100449 * @wait: If true, wait (atomically) until function has completed
450 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200451 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030452 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200453 *
454 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800455 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200456 *
457 * You must not call this function with disabled interrupts or from a
458 * hardware interrupt handler or from a bottom half handler.
459 */
David Howells3a5f65df2010-10-27 17:28:36 +0100460int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200461{
Jens Axboe3d442232008-06-26 11:21:34 +0200462 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030463 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200464 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100465
Rusty Russell54b11e62008-12-30 09:05:16 +1030466 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200467}
468EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800469
Amerigo Wang34db18a02011-03-22 16:34:06 -0700470/* Setup configured maximum number of CPUs to activate */
471unsigned int setup_max_cpus = NR_CPUS;
472EXPORT_SYMBOL(setup_max_cpus);
473
474
475/*
476 * Setup routine for controlling SMP activation
477 *
478 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
479 * activation entirely (the MPS table probe still happens, though).
480 *
481 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
482 * greater than 0, limits the maximum number of CPUs activated in
483 * SMP mode to <NUM>.
484 */
485
486void __weak arch_disable_smp_support(void) { }
487
488static int __init nosmp(char *str)
489{
490 setup_max_cpus = 0;
491 arch_disable_smp_support();
492
493 return 0;
494}
495
496early_param("nosmp", nosmp);
497
498/* this is hard limit */
499static int __init nrcpus(char *str)
500{
501 int nr_cpus;
502
503 get_option(&str, &nr_cpus);
504 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
505 nr_cpu_ids = nr_cpus;
506
507 return 0;
508}
509
510early_param("nr_cpus", nrcpus);
511
512static int __init maxcpus(char *str)
513{
514 get_option(&str, &setup_max_cpus);
515 if (setup_max_cpus == 0)
516 arch_disable_smp_support();
517
518 return 0;
519}
520
521early_param("maxcpus", maxcpus);
522
523/* Setup number of possible processor ids */
524int nr_cpu_ids __read_mostly = NR_CPUS;
525EXPORT_SYMBOL(nr_cpu_ids);
526
527/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
528void __init setup_nr_cpu_ids(void)
529{
530 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
531}
532
Borislav Petkova17bce42013-09-30 11:56:24 +0200533void __weak smp_announce(void)
534{
535 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
536}
537
Amerigo Wang34db18a02011-03-22 16:34:06 -0700538/* Called by boot processor to activate the rest. */
539void __init smp_init(void)
540{
541 unsigned int cpu;
542
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700543 idle_threads_init();
544
Amerigo Wang34db18a02011-03-22 16:34:06 -0700545 /* FIXME: This should be done in userspace --RR */
546 for_each_present_cpu(cpu) {
547 if (num_online_cpus() >= setup_max_cpus)
548 break;
549 if (!cpu_online(cpu))
550 cpu_up(cpu);
551 }
552
553 /* Any cleanup work */
Borislav Petkova17bce42013-09-30 11:56:24 +0200554 smp_announce();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700555 smp_cpus_done(setup_max_cpus);
556}
557
Amerigo Wang351f8f82011-01-12 16:59:39 -0800558/*
Tejun Heobd924e82011-01-20 12:07:13 +0100559 * Call a function on all processors. May be used during early boot while
560 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
561 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800562 */
563int on_each_cpu(void (*func) (void *info), void *info, int wait)
564{
Tejun Heobd924e82011-01-20 12:07:13 +0100565 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800566 int ret = 0;
567
568 preempt_disable();
569 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100570 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800571 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100572 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800573 preempt_enable();
574 return ret;
575}
576EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700577
578/**
579 * on_each_cpu_mask(): Run a function on processors specified by
580 * cpumask, which may include the local processor.
581 * @mask: The set of cpus to run on (only runs on online subset).
582 * @func: The function to run. This must be fast and non-blocking.
583 * @info: An arbitrary pointer to pass to the function.
584 * @wait: If true, wait (atomically) until function has completed
585 * on other CPUs.
586 *
587 * If @wait is true, then returns once @func has returned.
588 *
David Daney202da402013-09-11 14:23:29 -0700589 * You must not call this function with disabled interrupts or from a
590 * hardware interrupt handler or from a bottom half handler. The
591 * exception is that it may be used during early boot while
592 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700593 */
594void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
595 void *info, bool wait)
596{
597 int cpu = get_cpu();
598
599 smp_call_function_many(mask, func, info, wait);
600 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700601 unsigned long flags;
602 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700603 func(info);
David Daney202da402013-09-11 14:23:29 -0700604 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700605 }
606 put_cpu();
607}
608EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700609
610/*
611 * on_each_cpu_cond(): Call a function on each processor for which
612 * the supplied function cond_func returns true, optionally waiting
613 * for all the required CPUs to finish. This may include the local
614 * processor.
615 * @cond_func: A callback function that is passed a cpu id and
616 * the the info parameter. The function is called
617 * with preemption disabled. The function should
618 * return a blooean value indicating whether to IPI
619 * the specified CPU.
620 * @func: The function to run on all applicable CPUs.
621 * This must be fast and non-blocking.
622 * @info: An arbitrary pointer to pass to both functions.
623 * @wait: If true, wait (atomically) until function has
624 * completed on other CPUs.
625 * @gfp_flags: GFP flags to use when allocating the cpumask
626 * used internally by the function.
627 *
628 * The function might sleep if the GFP flags indicates a non
629 * atomic allocation is allowed.
630 *
631 * Preemption is disabled to protect against CPUs going offline but not online.
632 * CPUs going online during the call will not be seen or sent an IPI.
633 *
634 * You must not call this function with disabled interrupts or
635 * from a hardware interrupt handler or from a bottom half handler.
636 */
637void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
638 smp_call_func_t func, void *info, bool wait,
639 gfp_t gfp_flags)
640{
641 cpumask_var_t cpus;
642 int cpu, ret;
643
644 might_sleep_if(gfp_flags & __GFP_WAIT);
645
646 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
647 preempt_disable();
648 for_each_online_cpu(cpu)
649 if (cond_func(cpu, info))
650 cpumask_set_cpu(cpu, cpus);
651 on_each_cpu_mask(cpus, func, info, wait);
652 preempt_enable();
653 free_cpumask_var(cpus);
654 } else {
655 /*
656 * No free cpumask, bother. No matter, we'll
657 * just have to IPI them one by one.
658 */
659 preempt_disable();
660 for_each_online_cpu(cpu)
661 if (cond_func(cpu, info)) {
662 ret = smp_call_function_single(cpu, func,
663 info, wait);
664 WARN_ON_ONCE(!ret);
665 }
666 preempt_enable();
667 }
668}
669EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000670
671static void do_nothing(void *unused)
672{
673}
674
675/**
676 * kick_all_cpus_sync - Force all cpus out of idle
677 *
678 * Used to synchronize the update of pm_idle function pointer. It's
679 * called after the pointer is updated and returns after the dummy
680 * callback function has been executed on all cpus. The execution of
681 * the function can only happen on the remote cpus after they have
682 * left the idle function which had been called via pm_idle function
683 * pointer. So it's guaranteed that nothing uses the previous pointer
684 * anymore.
685 */
686void kick_all_cpus_sync(void)
687{
688 /* Make sure the change is visible before we kick the cpus */
689 smp_mb();
690 smp_call_function(do_nothing, NULL, 1);
691}
692EXPORT_SYMBOL_GPL(kick_all_cpus_sync);