blob: f5768b0c816a6baf02148c535b33e40b373f72fb [file] [log] [blame]
Jens Axboe3d442232008-06-26 11:21:34 +02001/*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
Jens Axboe3d442232008-06-26 11:21:34 +02005 */
Jens Axboe3d442232008-06-26 11:21:34 +02006#include <linux/rcupdate.h>
Linus Torvalds59190f42008-07-15 14:02:33 -07007#include <linux/rculist.h>
Ingo Molnar641cd4c2009-03-13 10:47:34 +01008#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04009#include <linux/export.h>
Ingo Molnar0b13fda2009-02-25 16:52:11 +010010#include <linux/percpu.h>
11#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Jens Axboe3d442232008-06-26 11:21:34 +020013#include <linux/smp.h>
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010014#include <linux/cpu.h>
Jens Axboe3d442232008-06-26 11:21:34 +020015
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -070016#include "smpboot.h"
17
Amerigo Wang351f8f82011-01-12 16:59:39 -080018#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
Jens Axboe3d442232008-06-26 11:21:34 +020019enum {
Peter Zijlstra6e275632009-02-25 13:59:48 +010020 CSD_FLAG_LOCK = 0x01,
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
Jens Axboe3d442232008-06-26 11:21:34 +020031struct call_single_queue {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010032 struct list_head list;
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010033 raw_spinlock_t lock;
Jens Axboe3d442232008-06-26 11:21:34 +020034};
35
Milton Millere03bcb62010-01-18 13:00:51 +110036static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010037
38static int
39hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
40{
41 long cpu = (long)hcpu;
42 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
43
44 switch (action) {
45 case CPU_UP_PREPARE:
46 case CPU_UP_PREPARE_FROZEN:
Yinghai Lueaa95842009-06-06 14:51:36 -070047 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010048 cpu_to_node(cpu)))
Akinobu Mita80b51842010-05-26 14:43:32 -070049 return notifier_from_errno(-ENOMEM);
Wang YanQingf44310b2013-01-26 15:53:57 +080050 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
Chen Gang60c32362013-09-11 14:23:22 -070051 cpu_to_node(cpu))) {
52 free_cpumask_var(cfd->cpumask);
Wang YanQingf44310b2013-01-26 15:53:57 +080053 return notifier_from_errno(-ENOMEM);
Chen Gang60c32362013-09-11 14:23:22 -070054 }
Shaohua Li9a46ad62013-02-21 16:43:03 -080055 cfd->csd = alloc_percpu(struct call_single_data);
56 if (!cfd->csd) {
Chen Gang60c32362013-09-11 14:23:22 -070057 free_cpumask_var(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -080058 free_cpumask_var(cfd->cpumask);
59 return notifier_from_errno(-ENOMEM);
60 }
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010061 break;
62
Xiao Guangrong69dd6472009-08-06 15:07:29 -070063#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010064 case CPU_UP_CANCELED:
65 case CPU_UP_CANCELED_FROZEN:
66
67 case CPU_DEAD:
68 case CPU_DEAD_FROZEN:
69 free_cpumask_var(cfd->cpumask);
Wang YanQingf44310b2013-01-26 15:53:57 +080070 free_cpumask_var(cfd->cpumask_ipi);
Shaohua Li9a46ad62013-02-21 16:43:03 -080071 free_percpu(cfd->csd);
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010072 break;
73#endif
74 };
75
76 return NOTIFY_OK;
77}
78
Paul Gortmaker0db06282013-06-19 14:53:51 -040079static struct notifier_block hotplug_cfd_notifier = {
Ingo Molnar0b13fda2009-02-25 16:52:11 +010080 .notifier_call = hotplug_cfd,
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010081};
82
Takao Indohd8ad7d12011-03-29 12:35:04 -040083void __init call_function_init(void)
Jens Axboe3d442232008-06-26 11:21:34 +020084{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +010085 void *cpu = (void *)(long)smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +020086 int i;
87
88 for_each_possible_cpu(i) {
89 struct call_single_queue *q = &per_cpu(call_single_queue, i);
90
Thomas Gleixner9f5a5622009-11-17 15:40:01 +010091 raw_spin_lock_init(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +020092 INIT_LIST_HEAD(&q->list);
93 }
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{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700127 WARN_ON(!(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
137/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100138 * Insert a previously allocated call_single_data element
139 * for execution on the given CPU. data must already have
140 * ->func, ->info, and ->flags set.
Jens Axboe3d442232008-06-26 11:21:34 +0200141 */
Peter Zijlstra6e275632009-02-25 13:59:48 +0100142static
Andrew Mortone1d12f32013-04-30 15:27:28 -0700143void generic_exec_single(int cpu, struct call_single_data *csd, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200144{
145 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
Jens Axboe3d442232008-06-26 11:21:34 +0200146 unsigned long flags;
Peter Zijlstra6e275632009-02-25 13:59:48 +0100147 int ipi;
Jens Axboe3d442232008-06-26 11:21:34 +0200148
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100149 raw_spin_lock_irqsave(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200150 ipi = list_empty(&dst->list);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700151 list_add_tail(&csd->list, &dst->list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100152 raw_spin_unlock_irqrestore(&dst->lock, flags);
Jens Axboe3d442232008-06-26 11:21:34 +0200153
Suresh Siddha561920a02008-10-30 18:28:41 +0100154 /*
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100155 * The list addition should be visible before sending the IPI
156 * handler locks the list to pull the entry off it because of
157 * normal cache coherency rules implied by spinlocks.
158 *
159 * If IPIs can go out of order to the cache coherency protocol
160 * in an architecture, sufficient synchronisation should be added
161 * to arch code to make it appear to obey cache coherency WRT
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100162 * locking and barrier primitives. Generic code isn't really
163 * equipped to do the right thing...
Suresh Siddha561920a02008-10-30 18:28:41 +0100164 */
Jens Axboe3d442232008-06-26 11:21:34 +0200165 if (ipi)
166 arch_send_call_function_single_ipi(cpu);
167
168 if (wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700169 csd_lock_wait(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200170}
171
172/*
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100173 * Invoked by arch to handle an IPI for call function single. Must be
174 * called from the arch with interrupts disabled.
Jens Axboe3d442232008-06-26 11:21:34 +0200175 */
176void generic_smp_call_function_single_interrupt(void)
177{
178 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100179 LIST_HEAD(list);
Jens Axboe3d442232008-06-26 11:21:34 +0200180
Suresh Siddha269c8612009-08-19 18:05:35 -0700181 /*
182 * Shouldn't receive this interrupt on a cpu that is not yet online.
183 */
184 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
185
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100186 raw_spin_lock(&q->lock);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100187 list_replace_init(&q->list, &list);
Thomas Gleixner9f5a5622009-11-17 15:40:01 +0100188 raw_spin_unlock(&q->lock);
Jens Axboe3d442232008-06-26 11:21:34 +0200189
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100190 while (!list_empty(&list)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700191 struct call_single_data *csd;
Jens Axboe3d442232008-06-26 11:21:34 +0200192
Andrew Mortone1d12f32013-04-30 15:27:28 -0700193 csd = list_entry(list.next, struct call_single_data, list);
194 list_del(&csd->list);
Jens Axboe3d442232008-06-26 11:21:34 +0200195
Andrew Mortone1d12f32013-04-30 15:27:28 -0700196 csd->func(csd->info);
Nick Piggin15d0d3b2009-02-25 06:22:45 +0100197
Xie XiuQi46591962013-07-30 11:06:09 +0800198 csd_unlock(csd);
Jens Axboe3d442232008-06-26 11:21:34 +0200199 }
200}
201
Milton Millere03bcb62010-01-18 13:00:51 +1100202static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
Steven Rostedtd7240b92009-01-29 10:08:01 -0500203
Jens Axboe3d442232008-06-26 11:21:34 +0200204/*
205 * smp_call_function_single - Run a function on a specific CPU
206 * @func: The function to run. This must be fast and non-blocking.
207 * @info: An arbitrary pointer to pass to the function.
Jens Axboe3d442232008-06-26 11:21:34 +0200208 * @wait: If true, wait until function has completed on other CPUs.
209 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800210 * Returns 0 on success, else a negative status code.
Jens Axboe3d442232008-06-26 11:21:34 +0200211 */
David Howells3a5f65d2010-10-27 17:28:36 +0100212int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
Jens Axboe8691e5a2008-06-06 11:18:06 +0200213 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200214{
Peter Zijlstra8969a5e2009-02-25 13:59:47 +0100215 struct call_single_data d = {
216 .flags = 0,
217 };
Jens Axboe3d442232008-06-26 11:21:34 +0200218 unsigned long flags;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100219 int this_cpu;
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700220 int err = 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200221
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100222 /*
223 * prevent preemption and reschedule on another processor,
224 * as well as CPU removal
225 */
226 this_cpu = get_cpu();
227
Suresh Siddha269c8612009-08-19 18:05:35 -0700228 /*
229 * Can deadlock when called with interrupts disabled.
230 * We allow cpu's that are not yet online though, as no one else can
231 * send smp call function interrupt to this cpu and as such deadlocks
232 * can't happen.
233 */
234 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
235 && !oops_in_progress);
Jens Axboe3d442232008-06-26 11:21:34 +0200236
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100237 if (cpu == this_cpu) {
Jens Axboe3d442232008-06-26 11:21:34 +0200238 local_irq_save(flags);
239 func(info);
240 local_irq_restore(flags);
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700241 } else {
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100242 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700243 struct call_single_data *csd = &d;
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100244
245 if (!wait)
Andrew Mortone1d12f32013-04-30 15:27:28 -0700246 csd = &__get_cpu_var(csd_data);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100247
Andrew Mortone1d12f32013-04-30 15:27:28 -0700248 csd_lock(csd);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100249
Andrew Mortone1d12f32013-04-30 15:27:28 -0700250 csd->func = func;
251 csd->info = info;
252 generic_exec_single(cpu, csd, wait);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100253 } else {
254 err = -ENXIO; /* CPU not online */
255 }
Jens Axboe3d442232008-06-26 11:21:34 +0200256 }
257
258 put_cpu();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100259
H. Peter Anvinf73be6d2008-08-25 17:07:14 -0700260 return err;
Jens Axboe3d442232008-06-26 11:21:34 +0200261}
262EXPORT_SYMBOL(smp_call_function_single);
263
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800264/*
265 * smp_call_function_any - Run a function on any of the given cpus
266 * @mask: The mask of cpus it can run on.
267 * @func: The function to run. This must be fast and non-blocking.
268 * @info: An arbitrary pointer to pass to the function.
269 * @wait: If true, wait until function has completed.
270 *
271 * Returns 0 on success, else a negative status code (if no cpus were online).
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800272 *
273 * Selection preference:
274 * 1) current cpu if in @mask
275 * 2) any cpu of current node if in @mask
276 * 3) any other online cpu in @mask
277 */
278int smp_call_function_any(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100279 smp_call_func_t func, void *info, int wait)
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800280{
281 unsigned int cpu;
282 const struct cpumask *nodemask;
283 int ret;
284
285 /* Try for same CPU (cheapest) */
286 cpu = get_cpu();
287 if (cpumask_test_cpu(cpu, mask))
288 goto call;
289
290 /* Try for same node. */
David Johnaf2422c2010-01-15 17:01:23 -0800291 nodemask = cpumask_of_node(cpu_to_node(cpu));
Rusty Russell2ea6dec2009-11-17 14:27:27 -0800292 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
293 cpu = cpumask_next_and(cpu, nodemask, mask)) {
294 if (cpu_online(cpu))
295 goto call;
296 }
297
298 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
299 cpu = cpumask_any_and(mask, cpu_online_mask);
300call:
301 ret = smp_call_function_single(cpu, func, info, wait);
302 put_cpu();
303 return ret;
304}
305EXPORT_SYMBOL_GPL(smp_call_function_any);
306
Jens Axboe3d442232008-06-26 11:21:34 +0200307/**
Heiko Carstens27c379f2010-09-10 13:47:29 +0200308 * __smp_call_function_single(): Run a function on a specific CPU
Jens Axboe3d442232008-06-26 11:21:34 +0200309 * @cpu: The CPU to run on.
310 * @data: Pre-allocated and setup data structure
Heiko Carstens27c379f2010-09-10 13:47:29 +0200311 * @wait: If true, wait until function has completed on specified CPU.
Jens Axboe3d442232008-06-26 11:21:34 +0200312 *
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100313 * Like smp_call_function_single(), but allow caller to pass in a
314 * pre-allocated data structure. Useful for embedding @data inside
315 * other structures, for instance.
Jens Axboe3d442232008-06-26 11:21:34 +0200316 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700317void __smp_call_function_single(int cpu, struct call_single_data *csd,
Peter Zijlstra6e275632009-02-25 13:59:48 +0100318 int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200319{
Heiko Carstens27c379f2010-09-10 13:47:29 +0200320 unsigned int this_cpu;
321 unsigned long flags;
Jens Axboe3d442232008-06-26 11:21:34 +0200322
Heiko Carstens27c379f2010-09-10 13:47:29 +0200323 this_cpu = get_cpu();
Suresh Siddha269c8612009-08-19 18:05:35 -0700324 /*
325 * Can deadlock when called with interrupts disabled.
326 * We allow cpu's that are not yet online though, as no one else can
327 * send smp call function interrupt to this cpu and as such deadlocks
328 * can't happen.
329 */
330 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
331 && !oops_in_progress);
Peter Zijlstra6e275632009-02-25 13:59:48 +0100332
Heiko Carstens27c379f2010-09-10 13:47:29 +0200333 if (cpu == this_cpu) {
334 local_irq_save(flags);
Andrew Mortone1d12f32013-04-30 15:27:28 -0700335 csd->func(csd->info);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200336 local_irq_restore(flags);
337 } else {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700338 csd_lock(csd);
339 generic_exec_single(cpu, csd, wait);
Heiko Carstens27c379f2010-09-10 13:47:29 +0200340 }
341 put_cpu();
Jens Axboe3d442232008-06-26 11:21:34 +0200342}
343
344/**
Rusty Russell54b11e62008-12-30 09:05:16 +1030345 * smp_call_function_many(): Run a function on a set of other CPUs.
346 * @mask: The set of cpus to run on (only runs on online subset).
Jens Axboe3d442232008-06-26 11:21:34 +0200347 * @func: The function to run. This must be fast and non-blocking.
348 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100349 * @wait: If true, wait (atomically) until function has completed
350 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200351 *
Sheng Yang72f279b2009-10-22 19:19:34 +0800352 * If @wait is true, then returns once @func has returned.
Jens Axboe3d442232008-06-26 11:21:34 +0200353 *
354 * You must not call this function with disabled interrupts or from a
355 * hardware interrupt handler or from a bottom half handler. Preemption
356 * must be disabled when calling this function.
357 */
Rusty Russell54b11e62008-12-30 09:05:16 +1030358void smp_call_function_many(const struct cpumask *mask,
David Howells3a5f65d2010-10-27 17:28:36 +0100359 smp_call_func_t func, void *info, bool wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200360{
Andrew Mortone1d12f32013-04-30 15:27:28 -0700361 struct call_function_data *cfd;
Shaohua Li9a46ad62013-02-21 16:43:03 -0800362 int cpu, next_cpu, this_cpu = smp_processor_id();
Jens Axboe3d442232008-06-26 11:21:34 +0200363
Suresh Siddha269c8612009-08-19 18:05:35 -0700364 /*
365 * Can deadlock when called with interrupts disabled.
366 * We allow cpu's that are not yet online though, as no one else can
367 * send smp call function interrupt to this cpu and as such deadlocks
368 * can't happen.
369 */
370 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
Tejun Heobd924e82011-01-20 12:07:13 +0100371 && !oops_in_progress && !early_boot_irqs_disabled);
Jens Axboe3d442232008-06-26 11:21:34 +0200372
Milton Miller723aae22011-03-15 13:27:17 -0600373 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
Rusty Russell54b11e62008-12-30 09:05:16 +1030374 cpu = cpumask_first_and(mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100375 if (cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030376 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100377
Rusty Russell54b11e62008-12-30 09:05:16 +1030378 /* No online cpus? We're done. */
379 if (cpu >= nr_cpu_ids)
380 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200381
Rusty Russell54b11e62008-12-30 09:05:16 +1030382 /* Do we have another CPU which isn't us? */
383 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100384 if (next_cpu == this_cpu)
Rusty Russell54b11e62008-12-30 09:05:16 +1030385 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
386
387 /* Fastpath: do that cpu by itself. */
388 if (next_cpu >= nr_cpu_ids) {
389 smp_call_function_single(cpu, func, info, wait);
390 return;
Jens Axboe3d442232008-06-26 11:21:34 +0200391 }
392
Andrew Mortone1d12f32013-04-30 15:27:28 -0700393 cfd = &__get_cpu_var(cfd_data);
Milton Miller45a57912011-03-15 13:27:16 -0600394
Andrew Mortone1d12f32013-04-30 15:27:28 -0700395 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
396 cpumask_clear_cpu(this_cpu, cfd->cpumask);
Milton Miller723aae22011-03-15 13:27:17 -0600397
398 /* Some callers race with other cpus changing the passed mask */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700399 if (unlikely(!cpumask_weight(cfd->cpumask)))
Milton Miller723aae22011-03-15 13:27:17 -0600400 return;
Anton Blanchard6dc19892011-01-20 14:44:33 -0800401
Wang YanQingf44310b2013-01-26 15:53:57 +0800402 /*
Andrew Mortone1d12f32013-04-30 15:27:28 -0700403 * After we put an entry into the list, cfd->cpumask may be cleared
404 * again when another CPU sends another IPI for a SMP function call, so
405 * cfd->cpumask will be zero.
Wang YanQingf44310b2013-01-26 15:53:57 +0800406 */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700407 cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
Jens Axboe3d442232008-06-26 11:21:34 +0200408
Andrew Mortone1d12f32013-04-30 15:27:28 -0700409 for_each_cpu(cpu, cfd->cpumask) {
410 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800411 struct call_single_queue *dst =
412 &per_cpu(call_single_queue, cpu);
413 unsigned long flags;
414
415 csd_lock(csd);
416 csd->func = func;
417 csd->info = info;
418
419 raw_spin_lock_irqsave(&dst->lock, flags);
420 list_add_tail(&csd->list, &dst->list);
421 raw_spin_unlock_irqrestore(&dst->lock, flags);
422 }
Suresh Siddha561920a02008-10-30 18:28:41 +0100423
Jens Axboe3d442232008-06-26 11:21:34 +0200424 /* Send a message to all CPUs in the map */
Andrew Mortone1d12f32013-04-30 15:27:28 -0700425 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
Jens Axboe3d442232008-06-26 11:21:34 +0200426
Shaohua Li9a46ad62013-02-21 16:43:03 -0800427 if (wait) {
Andrew Mortone1d12f32013-04-30 15:27:28 -0700428 for_each_cpu(cpu, cfd->cpumask) {
429 struct call_single_data *csd;
430
431 csd = per_cpu_ptr(cfd->csd, cpu);
Shaohua Li9a46ad62013-02-21 16:43:03 -0800432 csd_lock_wait(csd);
433 }
434 }
Jens Axboe3d442232008-06-26 11:21:34 +0200435}
Rusty Russell54b11e62008-12-30 09:05:16 +1030436EXPORT_SYMBOL(smp_call_function_many);
Jens Axboe3d442232008-06-26 11:21:34 +0200437
438/**
439 * smp_call_function(): Run a function on all other CPUs.
440 * @func: The function to run. This must be fast and non-blocking.
441 * @info: An arbitrary pointer to pass to the function.
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100442 * @wait: If true, wait (atomically) until function has completed
443 * on other CPUs.
Jens Axboe3d442232008-06-26 11:21:34 +0200444 *
Rusty Russell54b11e62008-12-30 09:05:16 +1030445 * Returns 0.
Jens Axboe3d442232008-06-26 11:21:34 +0200446 *
447 * If @wait is true, then returns once @func has returned; otherwise
Sheng Yang72f279b2009-10-22 19:19:34 +0800448 * it returns just before the target cpu calls @func.
Jens Axboe3d442232008-06-26 11:21:34 +0200449 *
450 * You must not call this function with disabled interrupts or from a
451 * hardware interrupt handler or from a bottom half handler.
452 */
David Howells3a5f65d2010-10-27 17:28:36 +0100453int smp_call_function(smp_call_func_t func, void *info, int wait)
Jens Axboe3d442232008-06-26 11:21:34 +0200454{
Jens Axboe3d442232008-06-26 11:21:34 +0200455 preempt_disable();
Rusty Russell54b11e62008-12-30 09:05:16 +1030456 smp_call_function_many(cpu_online_mask, func, info, wait);
Jens Axboe3d442232008-06-26 11:21:34 +0200457 preempt_enable();
Ingo Molnar0b13fda2009-02-25 16:52:11 +0100458
Rusty Russell54b11e62008-12-30 09:05:16 +1030459 return 0;
Jens Axboe3d442232008-06-26 11:21:34 +0200460}
461EXPORT_SYMBOL(smp_call_function);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800462#endif /* USE_GENERIC_SMP_HELPERS */
463
Amerigo Wang34db18a02011-03-22 16:34:06 -0700464/* Setup configured maximum number of CPUs to activate */
465unsigned int setup_max_cpus = NR_CPUS;
466EXPORT_SYMBOL(setup_max_cpus);
467
468
469/*
470 * Setup routine for controlling SMP activation
471 *
472 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
473 * activation entirely (the MPS table probe still happens, though).
474 *
475 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
476 * greater than 0, limits the maximum number of CPUs activated in
477 * SMP mode to <NUM>.
478 */
479
480void __weak arch_disable_smp_support(void) { }
481
482static int __init nosmp(char *str)
483{
484 setup_max_cpus = 0;
485 arch_disable_smp_support();
486
487 return 0;
488}
489
490early_param("nosmp", nosmp);
491
492/* this is hard limit */
493static int __init nrcpus(char *str)
494{
495 int nr_cpus;
496
497 get_option(&str, &nr_cpus);
498 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
499 nr_cpu_ids = nr_cpus;
500
501 return 0;
502}
503
504early_param("nr_cpus", nrcpus);
505
506static int __init maxcpus(char *str)
507{
508 get_option(&str, &setup_max_cpus);
509 if (setup_max_cpus == 0)
510 arch_disable_smp_support();
511
512 return 0;
513}
514
515early_param("maxcpus", maxcpus);
516
517/* Setup number of possible processor ids */
518int nr_cpu_ids __read_mostly = NR_CPUS;
519EXPORT_SYMBOL(nr_cpu_ids);
520
521/* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
522void __init setup_nr_cpu_ids(void)
523{
524 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
525}
526
Borislav Petkova17bce42013-09-30 11:56:24 +0200527void __weak smp_announce(void)
528{
529 printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
530}
531
Amerigo Wang34db18a02011-03-22 16:34:06 -0700532/* Called by boot processor to activate the rest. */
533void __init smp_init(void)
534{
535 unsigned int cpu;
536
Suresh Siddha3bb5d2e2012-04-20 17:08:50 -0700537 idle_threads_init();
538
Amerigo Wang34db18a02011-03-22 16:34:06 -0700539 /* FIXME: This should be done in userspace --RR */
540 for_each_present_cpu(cpu) {
541 if (num_online_cpus() >= setup_max_cpus)
542 break;
543 if (!cpu_online(cpu))
544 cpu_up(cpu);
545 }
546
547 /* Any cleanup work */
Borislav Petkova17bce42013-09-30 11:56:24 +0200548 smp_announce();
Amerigo Wang34db18a02011-03-22 16:34:06 -0700549 smp_cpus_done(setup_max_cpus);
550}
551
Amerigo Wang351f8f82011-01-12 16:59:39 -0800552/*
Tejun Heobd924e82011-01-20 12:07:13 +0100553 * Call a function on all processors. May be used during early boot while
554 * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
555 * of local_irq_disable/enable().
Amerigo Wang351f8f82011-01-12 16:59:39 -0800556 */
557int on_each_cpu(void (*func) (void *info), void *info, int wait)
558{
Tejun Heobd924e82011-01-20 12:07:13 +0100559 unsigned long flags;
Amerigo Wang351f8f82011-01-12 16:59:39 -0800560 int ret = 0;
561
562 preempt_disable();
563 ret = smp_call_function(func, info, wait);
Tejun Heobd924e82011-01-20 12:07:13 +0100564 local_irq_save(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800565 func(info);
Tejun Heobd924e82011-01-20 12:07:13 +0100566 local_irq_restore(flags);
Amerigo Wang351f8f82011-01-12 16:59:39 -0800567 preempt_enable();
568 return ret;
569}
570EXPORT_SYMBOL(on_each_cpu);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700571
572/**
573 * on_each_cpu_mask(): Run a function on processors specified by
574 * cpumask, which may include the local processor.
575 * @mask: The set of cpus to run on (only runs on online subset).
576 * @func: The function to run. This must be fast and non-blocking.
577 * @info: An arbitrary pointer to pass to the function.
578 * @wait: If true, wait (atomically) until function has completed
579 * on other CPUs.
580 *
581 * If @wait is true, then returns once @func has returned.
582 *
David Daney202da402013-09-11 14:23:29 -0700583 * You must not call this function with disabled interrupts or from a
584 * hardware interrupt handler or from a bottom half handler. The
585 * exception is that it may be used during early boot while
586 * early_boot_irqs_disabled is set.
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700587 */
588void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
589 void *info, bool wait)
590{
591 int cpu = get_cpu();
592
593 smp_call_function_many(mask, func, info, wait);
594 if (cpumask_test_cpu(cpu, mask)) {
David Daney202da402013-09-11 14:23:29 -0700595 unsigned long flags;
596 local_irq_save(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700597 func(info);
David Daney202da402013-09-11 14:23:29 -0700598 local_irq_restore(flags);
Gilad Ben-Yossef3fc498f2012-03-28 14:42:43 -0700599 }
600 put_cpu();
601}
602EXPORT_SYMBOL(on_each_cpu_mask);
Gilad Ben-Yossefb3a7e982012-03-28 14:42:43 -0700603
604/*
605 * on_each_cpu_cond(): Call a function on each processor for which
606 * the supplied function cond_func returns true, optionally waiting
607 * for all the required CPUs to finish. This may include the local
608 * processor.
609 * @cond_func: A callback function that is passed a cpu id and
610 * the the info parameter. The function is called
611 * with preemption disabled. The function should
612 * return a blooean value indicating whether to IPI
613 * the specified CPU.
614 * @func: The function to run on all applicable CPUs.
615 * This must be fast and non-blocking.
616 * @info: An arbitrary pointer to pass to both functions.
617 * @wait: If true, wait (atomically) until function has
618 * completed on other CPUs.
619 * @gfp_flags: GFP flags to use when allocating the cpumask
620 * used internally by the function.
621 *
622 * The function might sleep if the GFP flags indicates a non
623 * atomic allocation is allowed.
624 *
625 * Preemption is disabled to protect against CPUs going offline but not online.
626 * CPUs going online during the call will not be seen or sent an IPI.
627 *
628 * You must not call this function with disabled interrupts or
629 * from a hardware interrupt handler or from a bottom half handler.
630 */
631void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
632 smp_call_func_t func, void *info, bool wait,
633 gfp_t gfp_flags)
634{
635 cpumask_var_t cpus;
636 int cpu, ret;
637
638 might_sleep_if(gfp_flags & __GFP_WAIT);
639
640 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
641 preempt_disable();
642 for_each_online_cpu(cpu)
643 if (cond_func(cpu, info))
644 cpumask_set_cpu(cpu, cpus);
645 on_each_cpu_mask(cpus, func, info, wait);
646 preempt_enable();
647 free_cpumask_var(cpus);
648 } else {
649 /*
650 * No free cpumask, bother. No matter, we'll
651 * just have to IPI them one by one.
652 */
653 preempt_disable();
654 for_each_online_cpu(cpu)
655 if (cond_func(cpu, info)) {
656 ret = smp_call_function_single(cpu, func,
657 info, wait);
658 WARN_ON_ONCE(!ret);
659 }
660 preempt_enable();
661 }
662}
663EXPORT_SYMBOL(on_each_cpu_cond);
Thomas Gleixnerf37f4352012-05-07 17:59:48 +0000664
665static void do_nothing(void *unused)
666{
667}
668
669/**
670 * kick_all_cpus_sync - Force all cpus out of idle
671 *
672 * Used to synchronize the update of pm_idle function pointer. It's
673 * called after the pointer is updated and returns after the dummy
674 * callback function has been executed on all cpus. The execution of
675 * the function can only happen on the remote cpus after they have
676 * left the idle function which had been called via pm_idle function
677 * pointer. So it's guaranteed that nothing uses the previous pointer
678 * anymore.
679 */
680void kick_all_cpus_sync(void)
681{
682 /* Make sure the change is visible before we kick the cpus */
683 smp_mb();
684 smp_call_function(do_nothing, NULL, 1);
685}
686EXPORT_SYMBOL_GPL(kick_all_cpus_sync);