blob: 4ad913e7c253b3cbdc55c685319e8324f7d564db [file] [log] [blame]
Jens Axboe3d442232008-06-26 11:21:34 +02001/*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
Jens Axboe3d442232008-06-26 11:21:34 +02005 */
Jens Axboe3d442232008-06-26 11:21:34 +02006#include <linux/rcupdate.h>
Linus Torvalds59190f42008-07-15 14:02:33 -07007#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +01008#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010010#include <linux/percpu.h>
11#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020013#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010014#include <linux/cpu.h>
Jens Axboe3d442232008-06-26 11:21:34 +020015
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070016#include "smpboot.h"
17
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;
Wang YanQingf44310b2013-01-26 15:53:57 +080026 cpumask_var_t cpumask_ipi;
Jens Axboe3d442232008-06-26 11:21:34 +020027};
28
Milton Millere03bcb62010-01-18 13:00:51 +110029static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
30
Christoph Hellwig6897fc22014-01-30 15:45:47 -080031static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010032
33static int
34hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
35{
36 long cpu = (long)hcpu;
37 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
38
39 switch (action) {
40 case CPU_UP_PREPARE:
41 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070042 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010043 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070044 return notifier_from_errno(-ENOMEM);
Wang YanQingf44310b2013-01-26 15:53:57 +080045 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
Chen Gang60c32362013-09-11 14:23:22 -070046 cpu_to_node(cpu))) {
47 free_cpumask_var(cfd->cpumask);
Wang YanQingf44310b2013-01-26 15:53:57 +080048 return notifier_from_errno(-ENOMEM);
Chen Gang60c32362013-09-11 14:23:22 -070049 }
Shaohua Li9a46ad62013-02-21 16:43:03 -080050 cfd->csd = alloc_percpu(struct call_single_data);
51 if (!cfd->csd) {
Chen Gang60c32362013-09-11 14:23:22 -070052 free_cpumask_var(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -080053 free_cpumask_var(cfd->cpumask);
54 return notifier_from_errno(-ENOMEM);
55 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010056 break;
57
Xiao Guangrong69dd6472009-08-06 15:07:29 -070058#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010059 case CPU_UP_CANCELED:
60 case CPU_UP_CANCELED_FROZEN:
61
62 case CPU_DEAD:
63 case CPU_DEAD_FROZEN:
64 free_cpumask_var(cfd->cpumask);
Wang YanQingf44310b2013-01-26 15:53:57 +080065 free_cpumask_var(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -080066 free_percpu(cfd->csd);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010067 break;
68#endif
69 };
70
71 return NOTIFY_OK;
72}
73
Paul Gortmaker0db06282013-06-19 14:53:51 -040074static struct notifier_block hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010075 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010076};
77
Takao Indohd8ad7d12011-03-29 12:35:04 -040078void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020079{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010080 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020081 int i;
82
Christoph Hellwig6897fc22014-01-30 15:45:47 -080083 for_each_possible_cpu(i)
84 init_llist_head(&per_cpu(call_single_queue, i));
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010085
86 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
87 register_cpu_notifier(&hotplug_cfd_notifier);
Jens Axboe3d442232008-06-26 11:21:34 +020088}
89
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010090/*
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010091 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
92 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +010093 * For non-synchronous ipi calls the csd can still be in use by the
94 * previous function call. For multi-cpu calls its even more interesting
95 * as we'll have to ensure no other cpu is observing our csd.
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010096 */
Andrew Mortone1d12f32013-04-30 15:27:28 -070097static void csd_lock_wait(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010098{
Andrew Mortone1d12f32013-04-30 15:27:28 -070099 while (csd->flags & CSD_FLAG_LOCK)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100100 cpu_relax();
Peter Zijlstra6e275632009-02-25 13:59:48 +0100101}
102
Andrew Mortone1d12f32013-04-30 15:27:28 -0700103static void csd_lock(struct call_single_data *csd)
Peter Zijlstra6e275632009-02-25 13:59:48 +0100104{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700105 csd_lock_wait(csd);
106 csd->flags |= CSD_FLAG_LOCK;
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100107
108 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100109 * prevent CPU from reordering the above assignment
110 * to ->flags with any subsequent assignments to other
111 * fields of the specified call_single_data structure:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100112 */
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100113 smp_mb();
114}
115
Andrew Mortone1d12f32013-04-30 15:27:28 -0700116static void csd_unlock(struct call_single_data *csd)
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100117{
Jens Axboec84a83e2013-05-17 09:58:43 +0200118 WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK));
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100119
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100120 /*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100121 * ensure we're all done before releasing data:
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100122 */
123 smp_mb();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100124
Andrew Mortone1d12f32013-04-30 15:27:28 -0700125 csd->flags &= ~CSD_FLAG_LOCK;
Jens Axboe3d442232008-06-26 11:21:34 +0200126}
127
128/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100129 * Insert a previously allocated call_single_data element
130 * for execution on the given CPU. data must already have
131 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200132 */
Christoph Hellwigca5ecd62013-11-14 14:32:10 -0800133static void generic_exec_single(int cpu, struct call_single_data *csd, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200134{
Jens Axboec84a83e2013-05-17 09:58:43 +0200135 if (wait)
136 csd->flags |= CSD_FLAG_WAIT;
137
Suresh Siddha561920a02008-10-30 18:28:41 +0100138 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100139 * The list addition should be visible before sending the IPI
140 * handler locks the list to pull the entry off it because of
141 * normal cache coherency rules implied by spinlocks.
142 *
143 * If IPIs can go out of order to the cache coherency protocol
144 * in an architecture, sufficient synchronisation should be added
145 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100146 * locking and barrier primitives. Generic code isn't really
147 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100148 */
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800149 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
Jens Axboe3d442232008-06-26 11:21:34 +0200150 arch_send_call_function_single_ipi(cpu);
151
152 if (wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700153 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200154}
155
156/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100157 * Invoked by arch to handle an IPI for call function single. Must be
158 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200159 */
160void generic_smp_call_function_single_interrupt(void)
161{
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800162 struct llist_node *entry, *next;
Jens Axboe3d442232008-06-26 11:21:34 +0200163
Suresh Siddha269c8612009-08-19 18:05:35 -0700164 /*
165 * Shouldn't receive this interrupt on a cpu that is not yet online.
166 */
167 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
168
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800169 entry = llist_del_all(&__get_cpu_var(call_single_queue));
170 entry = llist_reverse_order(entry);
Jens Axboe3d442232008-06-26 11:21:34 +0200171
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800172 while (entry) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700173 struct call_single_data *csd;
Jens Axboe3d442232008-06-26 11:21:34 +0200174
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800175 next = entry->next;
Jens Axboe3d442232008-06-26 11:21:34 +0200176
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800177 csd = llist_entry(entry, struct call_single_data, llist);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700178 csd->func(csd->info);
Xie XiuQi46591962013-07-30 11:06:09 +0800179 csd_unlock(csd);
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800180
181 entry = next;
Jens Axboe3d442232008-06-26 11:21:34 +0200182 }
183}
184
Milton Millere03bcb62010-01-18 13:00:51 +1100185static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500186
Jens Axboe3d442232008-06-26 11:21:34 +0200187/*
188 * smp_call_function_single - Run a function on a specific CPU
189 * @func: The function to run. This must be fast and non-blocking.
190 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200191 * @wait: If true, wait until function has completed on other CPUs.
192 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800193 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200194 */
David Howells3a5f65d2010-10-27 17:28:36 +0100195int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200196 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200197{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100198 struct call_single_data d = {
199 .flags = 0,
200 };
Jens Axboe3d442232008-06-26 11:21:34 +0200201 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100202 int this_cpu;
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700203 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200204
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100205 /*
206 * prevent preemption and reschedule on another processor,
207 * as well as CPU removal
208 */
209 this_cpu = get_cpu();
210
Suresh Siddha269c8612009-08-19 18:05:35 -0700211 /*
212 * Can deadlock when called with interrupts disabled.
213 * We allow cpu's that are not yet online though, as no one else can
214 * send smp call function interrupt to this cpu and as such deadlocks
215 * can't happen.
216 */
217 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
218 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200219
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100220 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200221 local_irq_save(flags);
222 func(info);
223 local_irq_restore(flags);
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700224 } else {
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100225 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700226 struct call_single_data *csd = &d;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100227
228 if (!wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700229 csd = &__get_cpu_var(csd_data);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100230
Andrew Mortone1d12f32013-04-30 15:27:28 -0700231 csd_lock(csd);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100232
Andrew Mortone1d12f32013-04-30 15:27:28 -0700233 csd->func = func;
234 csd->info = info;
235 generic_exec_single(cpu, csd, wait);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100236 } else {
237 err = -ENXIO; /* CPU not online */
238 }
Jens Axboe3d442232008-06-26 11:21:34 +0200239 }
240
241 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100242
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700243 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200244}
245EXPORT_SYMBOL(smp_call_function_single);
246
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800247/*
248 * smp_call_function_any - Run a function on any of the given cpus
249 * @mask: The mask of cpus it can run on.
250 * @func: The function to run. This must be fast and non-blocking.
251 * @info: An arbitrary pointer to pass to the function.
252 * @wait: If true, wait until function has completed.
253 *
254 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800255 *
256 * Selection preference:
257 * 1) current cpu if in @mask
258 * 2) any cpu of current node if in @mask
259 * 3) any other online cpu in @mask
260 */
261int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100262 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800263{
264 unsigned int cpu;
265 const struct cpumask *nodemask;
266 int ret;
267
268 /* Try for same CPU (cheapest) */
269 cpu = get_cpu();
270 if (cpumask_test_cpu(cpu, mask))
271 goto call;
272
273 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800274 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800275 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
276 cpu = cpumask_next_and(cpu, nodemask, mask)) {
277 if (cpu_online(cpu))
278 goto call;
279 }
280
281 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
282 cpu = cpumask_any_and(mask, cpu_online_mask);
283call:
284 ret = smp_call_function_single(cpu, func, info, wait);
285 put_cpu();
286 return ret;
287}
288EXPORT_SYMBOL_GPL(smp_call_function_any);
289
Jens Axboe3d442232008-06-26 11:21:34 +0200290/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200291 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200292 * @cpu: The CPU to run on.
293 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200294 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200295 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100296 * Like smp_call_function_single(), but allow caller to pass in a
297 * pre-allocated data structure. Useful for embedding @data inside
298 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200299 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700300void __smp_call_function_single(int cpu, struct call_single_data *csd,
Peter Zijlstra6e275632009-02-25 13:59:48 +0100301 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200302{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200303 unsigned int this_cpu;
304 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200305
Heiko Carstens27c379f2010-09-10 13:47:29 +0200306 this_cpu = get_cpu();
Suresh Siddha269c8612009-08-19 18:05:35 -0700307 /*
308 * Can deadlock when called with interrupts disabled.
309 * We allow cpu's that are not yet online though, as no one else can
310 * send smp call function interrupt to this cpu and as such deadlocks
311 * can't happen.
312 */
313 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
314 && !oops_in_progress);
Peter Zijlstra6e275632009-02-25 13:59:48 +0100315
Heiko Carstens27c379f2010-09-10 13:47:29 +0200316 if (cpu == this_cpu) {
317 local_irq_save(flags);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700318 csd->func(csd->info);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200319 local_irq_restore(flags);
320 } else {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700321 csd_lock(csd);
322 generic_exec_single(cpu, csd, wait);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200323 }
324 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200325}
Jens Axboee3daab62013-10-25 11:45:35 +0100326EXPORT_SYMBOL_GPL(__smp_call_function_single);
Jens Axboe3d442232008-06-26 11:21:34 +0200327
328/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030329 * smp_call_function_many(): Run a function on a set of other CPUs.
330 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200331 * @func: The function to run. This must be fast and non-blocking.
332 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100333 * @wait: If true, wait (atomically) until function has completed
334 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200335 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800336 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200337 *
338 * You must not call this function with disabled interrupts or from a
339 * hardware interrupt handler or from a bottom half handler. Preemption
340 * must be disabled when calling this function.
341 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030342void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100343 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200344{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700345 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800346 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200347
Suresh Siddha269c8612009-08-19 18:05:35 -0700348 /*
349 * Can deadlock when called with interrupts disabled.
350 * We allow cpu's that are not yet online though, as no one else can
351 * send smp call function interrupt to this cpu and as such deadlocks
352 * can't happen.
353 */
354 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100355 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200356
Milton Miller723aae22011-03-15 13:27:17 -0600357 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030358 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100359 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030360 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100361
Rusty Russell54b11e62008-12-30 09:05:16 +1030362 /* No online cpus? We're done. */
363 if (cpu >= nr_cpu_ids)
364 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200365
Rusty Russell54b11e62008-12-30 09:05:16 +1030366 /* Do we have another CPU which isn't us? */
367 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100368 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030369 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
370
371 /* Fastpath: do that cpu by itself. */
372 if (next_cpu >= nr_cpu_ids) {
373 smp_call_function_single(cpu, func, info, wait);
374 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200375 }
376
Andrew Mortone1d12f32013-04-30 15:27:28 -0700377 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600378
Andrew Mortone1d12f32013-04-30 15:27:28 -0700379 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
380 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600381
382 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700383 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600384 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800385
Wang YanQingf44310b2013-01-26 15:53:57 +0800386 /*
Andrew Mortone1d12f32013-04-30 15:27:28 -0700387 * After we put an entry into the list, cfd->cpumask may be cleared
388 * again when another CPU sends another IPI for a SMP function call, so
389 * cfd->cpumask will be zero.
Wang YanQingf44310b2013-01-26 15:53:57 +0800390 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700391 cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200392
Andrew Mortone1d12f32013-04-30 15:27:28 -0700393 for_each_cpu(cpu, cfd->cpumask) {
394 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800395
396 csd_lock(csd);
397 csd->func = func;
398 csd->info = info;
Christoph Hellwig6897fc22014-01-30 15:45:47 -0800399 llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
Shaohua Li9a46ad62013-02-21 16:43:03 -0800400 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100401
Jens Axboe3d442232008-06-26 11:21:34 +0200402 /* Send a message to all CPUs in the map */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700403 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200404
Shaohua Li9a46ad62013-02-21 16:43:03 -0800405 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700406 for_each_cpu(cpu, cfd->cpumask) {
407 struct call_single_data *csd;
408
409 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800410 csd_lock_wait(csd);
411 }
412 }
Jens Axboe3d442232008-06-26 11:21:34 +0200413}
Rusty Russell54b11e62008-12-30 09:05:16 +1030414EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200415
416/**
417 * smp_call_function(): Run a function on all other CPUs.
418 * @func: The function to run. This must be fast and non-blocking.
419 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100420 * @wait: If true, wait (atomically) until function has completed
421 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200422 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030423 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200424 *
425 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800426 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200427 *
428 * You must not call this function with disabled interrupts or from a
429 * hardware interrupt handler or from a bottom half handler.
430 */
David Howells3a5f65d2010-10-27 17:28:36 +0100431int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200432{
Jens Axboe3d442232008-06-26 11:21:34 +0200433 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030434 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200435 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100436
Rusty Russell54b11e62008-12-30 09:05:16 +1030437 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200438}
439EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800440
Amerigo Wang34db18a02011-03-22 16:34:06 -0700441/* Setup configured maximum number of CPUs to activate */
442unsigned int setup_max_cpus = NR_CPUS;
443EXPORT_SYMBOL(setup_max_cpus);
444
445
446/*
447 * Setup routine for controlling SMP activation
448 *
449 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
450 * activation entirely (the MPS table probe still happens, though).
451 *
452 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
453 * greater than 0, limits the maximum number of CPUs activated in
454 * SMP mode to <NUM>.
455 */
456
457void __weak arch_disable_smp_support(void) { }
458
459static int __init nosmp(char *str)
460{
461 setup_max_cpus = 0;
462 arch_disable_smp_support();
463
464 return 0;
465}
466
467early_param("nosmp", nosmp);
468
469/* this is hard limit */
470static int __init nrcpus(char *str)
471{
472 int nr_cpus;
473
474 get_option(&str, &nr_cpus);
475 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
476 nr_cpu_ids = nr_cpus;
477
478 return 0;
479}
480
481early_param("nr_cpus", nrcpus);
482
483static int __init maxcpus(char *str)
484{
485 get_option(&str, &setup_max_cpus);
486 if (setup_max_cpus == 0)
487 arch_disable_smp_support();
488
489 return 0;
490}
491
492early_param("maxcpus", maxcpus);
493
494/* Setup number of possible processor ids */
495int nr_cpu_ids __read_mostly = NR_CPUS;
496EXPORT_SYMBOL(nr_cpu_ids);
497
498/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
499void __init setup_nr_cpu_ids(void)
500{
501 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
502}
503
Borislav Petkova17bce42013-09-30 11:56:24 +0200504void __weak smp_announce(void)
505{
506 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
507}
508
Amerigo Wang34db18a02011-03-22 16:34:06 -0700509/* Called by boot processor to activate the rest. */
510void __init smp_init(void)
511{
512 unsigned int cpu;
513
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700514 idle_threads_init();
515
Amerigo Wang34db18a02011-03-22 16:34:06 -0700516 /* FIXME: This should be done in userspace --RR */
517 for_each_present_cpu(cpu) {
518 if (num_online_cpus() >= setup_max_cpus)
519 break;
520 if (!cpu_online(cpu))
521 cpu_up(cpu);
522 }
523
524 /* Any cleanup work */
Borislav Petkova17bce42013-09-30 11:56:24 +0200525 smp_announce();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700526 smp_cpus_done(setup_max_cpus);
527}
528
Amerigo Wang351f8f82011-01-12 16:59:39 -0800529/*
Tejun Heobd924e82011-01-20 12:07:13 +0100530 * Call a function on all processors. May be used during early boot while
531 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
532 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800533 */
534int on_each_cpu(void (*func) (void *info), void *info, int wait)
535{
Tejun Heobd924e82011-01-20 12:07:13 +0100536 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800537 int ret = 0;
538
539 preempt_disable();
540 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100541 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800542 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100543 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800544 preempt_enable();
545 return ret;
546}
547EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700548
549/**
550 * on_each_cpu_mask(): Run a function on processors specified by
551 * cpumask, which may include the local processor.
552 * @mask: The set of cpus to run on (only runs on online subset).
553 * @func: The function to run. This must be fast and non-blocking.
554 * @info: An arbitrary pointer to pass to the function.
555 * @wait: If true, wait (atomically) until function has completed
556 * on other CPUs.
557 *
558 * If @wait is true, then returns once @func has returned.
559 *
David Daney202da402013-09-11 14:23:29 -0700560 * You must not call this function with disabled interrupts or from a
561 * hardware interrupt handler or from a bottom half handler. The
562 * exception is that it may be used during early boot while
563 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700564 */
565void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
566 void *info, bool wait)
567{
568 int cpu = get_cpu();
569
570 smp_call_function_many(mask, func, info, wait);
571 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700572 unsigned long flags;
573 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700574 func(info);
David Daney202da402013-09-11 14:23:29 -0700575 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700576 }
577 put_cpu();
578}
579EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700580
581/*
582 * on_each_cpu_cond(): Call a function on each processor for which
583 * the supplied function cond_func returns true, optionally waiting
584 * for all the required CPUs to finish. This may include the local
585 * processor.
586 * @cond_func: A callback function that is passed a cpu id and
587 * the the info parameter. The function is called
588 * with preemption disabled. The function should
589 * return a blooean value indicating whether to IPI
590 * the specified CPU.
591 * @func: The function to run on all applicable CPUs.
592 * This must be fast and non-blocking.
593 * @info: An arbitrary pointer to pass to both functions.
594 * @wait: If true, wait (atomically) until function has
595 * completed on other CPUs.
596 * @gfp_flags: GFP flags to use when allocating the cpumask
597 * used internally by the function.
598 *
599 * The function might sleep if the GFP flags indicates a non
600 * atomic allocation is allowed.
601 *
602 * Preemption is disabled to protect against CPUs going offline but not online.
603 * CPUs going online during the call will not be seen or sent an IPI.
604 *
605 * You must not call this function with disabled interrupts or
606 * from a hardware interrupt handler or from a bottom half handler.
607 */
608void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
609 smp_call_func_t func, void *info, bool wait,
610 gfp_t gfp_flags)
611{
612 cpumask_var_t cpus;
613 int cpu, ret;
614
615 might_sleep_if(gfp_flags & __GFP_WAIT);
616
617 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
618 preempt_disable();
619 for_each_online_cpu(cpu)
620 if (cond_func(cpu, info))
621 cpumask_set_cpu(cpu, cpus);
622 on_each_cpu_mask(cpus, func, info, wait);
623 preempt_enable();
624 free_cpumask_var(cpus);
625 } else {
626 /*
627 * No free cpumask, bother. No matter, we'll
628 * just have to IPI them one by one.
629 */
630 preempt_disable();
631 for_each_online_cpu(cpu)
632 if (cond_func(cpu, info)) {
633 ret = smp_call_function_single(cpu, func,
634 info, wait);
635 WARN_ON_ONCE(!ret);
636 }
637 preempt_enable();
638 }
639}
640EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000641
642static void do_nothing(void *unused)
643{
644}
645
646/**
647 * kick_all_cpus_sync - Force all cpus out of idle
648 *
649 * Used to synchronize the update of pm_idle function pointer. It's
650 * called after the pointer is updated and returns after the dummy
651 * callback function has been executed on all cpus. The execution of
652 * the function can only happen on the remote cpus after they have
653 * left the idle function which had been called via pm_idle function
654 * pointer. So it's guaranteed that nothing uses the previous pointer
655 * anymore.
656 */
657void kick_all_cpus_sync(void)
658{
659 /* Make sure the change is visible before we kick the cpus */
660 smp_mb();
661 smp_call_function(do_nothing, NULL, 1);
662}
663EXPORT_SYMBOL_GPL(kick_all_cpus_sync);