generic-ipi: remove CSD_FLAG_WAIT
[linux-2.6-block.git] / kernel / smp.c
CommitLineData
3d442233
JA
1/*
2 * Generic helpers for smp ipi calls
3 *
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
5 *
6 */
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/percpu.h>
10#include <linux/rcupdate.h>
59190f42 11#include <linux/rculist.h>
3d442233 12#include <linux/smp.h>
8969a5ed 13#include <linux/cpu.h>
3d442233
JA
14
15static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
8969a5ed
PZ
16
17static struct {
18 struct list_head queue;
19 spinlock_t lock;
20} call_function __cacheline_aligned_in_smp = {
21 .queue = LIST_HEAD_INIT(call_function.queue),
22 .lock = __SPIN_LOCK_UNLOCKED(call_function.lock),
23};
3d442233
JA
24
25enum {
6e275637 26 CSD_FLAG_LOCK = 0x01,
3d442233
JA
27};
28
29struct call_function_data {
30 struct call_single_data csd;
31 spinlock_t lock;
32 unsigned int refs;
8969a5ed 33 cpumask_var_t cpumask;
3d442233
JA
34};
35
36struct call_single_queue {
37 struct list_head list;
38 spinlock_t lock;
39};
40
8969a5ed
PZ
41static DEFINE_PER_CPU(struct call_function_data, cfd_data) = {
42 .lock = __SPIN_LOCK_UNLOCKED(cfd_data.lock),
43};
44
45static int
46hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
47{
48 long cpu = (long)hcpu;
49 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
50
51 switch (action) {
52 case CPU_UP_PREPARE:
53 case CPU_UP_PREPARE_FROZEN:
54 if (!alloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
55 cpu_to_node(cpu)))
56 return NOTIFY_BAD;
57 break;
58
59#ifdef CONFIG_CPU_HOTPLUG
60 case CPU_UP_CANCELED:
61 case CPU_UP_CANCELED_FROZEN:
62
63 case CPU_DEAD:
64 case CPU_DEAD_FROZEN:
65 free_cpumask_var(cfd->cpumask);
66 break;
67#endif
68 };
69
70 return NOTIFY_OK;
71}
72
73static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
74 .notifier_call = hotplug_cfd,
75};
76
7babe8db 77static int __cpuinit init_call_single_data(void)
3d442233 78{
8969a5ed 79 void *cpu = (void *)(long)smp_processor_id();
3d442233
JA
80 int i;
81
82 for_each_possible_cpu(i) {
83 struct call_single_queue *q = &per_cpu(call_single_queue, i);
84
85 spin_lock_init(&q->lock);
86 INIT_LIST_HEAD(&q->list);
87 }
8969a5ed
PZ
88
89 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
90 register_cpu_notifier(&hotplug_cfd_notifier);
91
7babe8db 92 return 0;
3d442233 93}
7babe8db 94early_initcall(init_call_single_data);
3d442233 95
8969a5ed
PZ
96/*
97 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
98 *
99 * For non-synchronous ipi calls the csd can still be in use by the previous
100 * function call. For multi-cpu calls its even more interesting as we'll have
101 * to ensure no other cpu is observing our csd.
102 */
6e275637 103static void csd_lock_wait(struct call_single_data *data)
8969a5ed
PZ
104{
105 while (data->flags & CSD_FLAG_LOCK)
106 cpu_relax();
6e275637
PZ
107}
108
109static void csd_lock(struct call_single_data *data)
110{
111 csd_lock_wait(data);
8969a5ed
PZ
112 data->flags = CSD_FLAG_LOCK;
113
114 /*
115 * prevent CPU from reordering the above assignment to ->flags
116 * with any subsequent assignments to other fields of the
117 * specified call_single_data structure.
118 */
119
120 smp_mb();
121}
122
123static void csd_unlock(struct call_single_data *data)
124{
125 WARN_ON(!(data->flags & CSD_FLAG_LOCK));
126 /*
127 * ensure we're all done before releasing data
128 */
129 smp_mb();
130 data->flags &= ~CSD_FLAG_LOCK;
3d442233
JA
131}
132
133/*
134 * Insert a previously allocated call_single_data element for execution
135 * on the given CPU. data must already have ->func, ->info, and ->flags set.
136 */
6e275637
PZ
137static
138void generic_exec_single(int cpu, struct call_single_data *data, int wait)
3d442233
JA
139{
140 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
3d442233 141 unsigned long flags;
6e275637 142 int ipi;
3d442233
JA
143
144 spin_lock_irqsave(&dst->lock, flags);
145 ipi = list_empty(&dst->list);
146 list_add_tail(&data->list, &dst->list);
147 spin_unlock_irqrestore(&dst->lock, flags);
148
561920a0 149 /*
15d0d3b3
NP
150 * The list addition should be visible before sending the IPI
151 * handler locks the list to pull the entry off it because of
152 * normal cache coherency rules implied by spinlocks.
153 *
154 * If IPIs can go out of order to the cache coherency protocol
155 * in an architecture, sufficient synchronisation should be added
156 * to arch code to make it appear to obey cache coherency WRT
157 * locking and barrier primitives. Generic code isn't really equipped
158 * to do the right thing...
561920a0 159 */
561920a0 160
3d442233
JA
161 if (ipi)
162 arch_send_call_function_single_ipi(cpu);
163
164 if (wait)
6e275637 165 csd_lock_wait(data);
3d442233
JA
166}
167
168/*
169 * Invoked by arch to handle an IPI for call function. Must be called with
170 * interrupts disabled.
171 */
172void generic_smp_call_function_interrupt(void)
173{
174 struct call_function_data *data;
175 int cpu = get_cpu();
176
15d0d3b3
NP
177 /*
178 * Ensure entry is visible on call_function_queue after we have
179 * entered the IPI. See comment in smp_call_function_many.
180 * If we don't have this, then we may miss an entry on the list
181 * and never get another IPI to process it.
182 */
183 smp_mb();
184
3d442233
JA
185 /*
186 * It's ok to use list_for_each_rcu() here even though we may delete
187 * 'pos', since list_del_rcu() doesn't clear ->next
188 */
8969a5ed 189 list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
3d442233
JA
190 int refs;
191
8969a5ed
PZ
192 spin_lock(&data->lock);
193 if (!cpumask_test_cpu(cpu, data->cpumask)) {
194 spin_unlock(&data->lock);
3d442233 195 continue;
8969a5ed
PZ
196 }
197 cpumask_clear_cpu(cpu, data->cpumask);
198 spin_unlock(&data->lock);
3d442233
JA
199
200 data->csd.func(data->csd.info);
201
202 spin_lock(&data->lock);
3d442233 203 WARN_ON(data->refs == 0);
8969a5ed
PZ
204 refs = --data->refs;
205 if (!refs) {
206 spin_lock(&call_function.lock);
207 list_del_rcu(&data->csd.list);
208 spin_unlock(&call_function.lock);
209 }
3d442233
JA
210 spin_unlock(&data->lock);
211
212 if (refs)
213 continue;
214
8969a5ed 215 csd_unlock(&data->csd);
3d442233 216 }
3d442233
JA
217
218 put_cpu();
219}
220
221/*
222 * Invoked by arch to handle an IPI for call function single. Must be called
223 * from the arch with interrupts disabled.
224 */
225void generic_smp_call_function_single_interrupt(void)
226{
227 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
228 LIST_HEAD(list);
15d0d3b3 229 unsigned int data_flags;
3d442233 230
15d0d3b3
NP
231 spin_lock(&q->lock);
232 list_replace_init(&q->list, &list);
233 spin_unlock(&q->lock);
3d442233 234
15d0d3b3
NP
235 while (!list_empty(&list)) {
236 struct call_single_data *data;
3d442233 237
15d0d3b3
NP
238 data = list_entry(list.next, struct call_single_data,
239 list);
240 list_del(&data->list);
3d442233 241
3d442233 242 /*
15d0d3b3
NP
243 * 'data' can be invalid after this call if
244 * flags == 0 (when called through
245 * generic_exec_single(), so save them away before
246 * making the call.
3d442233 247 */
15d0d3b3
NP
248 data_flags = data->flags;
249
250 data->func(data->info);
251
8969a5ed
PZ
252 /*
253 * Unlocked CSDs are valid through generic_exec_single()
254 */
255 if (data_flags & CSD_FLAG_LOCK)
256 csd_unlock(data);
3d442233
JA
257 }
258}
259
d7240b98
SR
260static DEFINE_PER_CPU(struct call_single_data, csd_data);
261
3d442233
JA
262/*
263 * smp_call_function_single - Run a function on a specific CPU
264 * @func: The function to run. This must be fast and non-blocking.
265 * @info: An arbitrary pointer to pass to the function.
3d442233
JA
266 * @wait: If true, wait until function has completed on other CPUs.
267 *
268 * Returns 0 on success, else a negative status code. Note that @wait
269 * will be implicitly turned on in case of allocation failures, since
270 * we fall back to on-stack allocation.
271 */
272int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
8691e5a8 273 int wait)
3d442233 274{
8969a5ed
PZ
275 struct call_single_data d = {
276 .flags = 0,
277 };
3d442233 278 unsigned long flags;
f73be6de
PA
279 /* prevent preemption and reschedule on another processor,
280 as well as CPU removal */
3d442233 281 int me = get_cpu();
f73be6de 282 int err = 0;
3d442233
JA
283
284 /* Can deadlock when called with interrupts disabled */
285 WARN_ON(irqs_disabled());
286
287 if (cpu == me) {
288 local_irq_save(flags);
289 func(info);
290 local_irq_restore(flags);
4f4b6c1a 291 } else if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
6e275637 292 struct call_single_data *data = &d;
3d442233 293
6e275637 294 if (!wait)
8969a5ed 295 data = &__get_cpu_var(csd_data);
6e275637
PZ
296
297 csd_lock(data);
3d442233
JA
298
299 data->func = func;
300 data->info = info;
6e275637 301 generic_exec_single(cpu, data, wait);
f73be6de
PA
302 } else {
303 err = -ENXIO; /* CPU not online */
3d442233
JA
304 }
305
306 put_cpu();
f73be6de 307 return err;
3d442233
JA
308}
309EXPORT_SYMBOL(smp_call_function_single);
310
311/**
312 * __smp_call_function_single(): Run a function on another CPU
313 * @cpu: The CPU to run on.
314 * @data: Pre-allocated and setup data structure
315 *
316 * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
317 * data structure. Useful for embedding @data inside other structures, for
318 * instance.
319 *
320 */
6e275637
PZ
321void __smp_call_function_single(int cpu, struct call_single_data *data,
322 int wait)
3d442233 323{
6e275637
PZ
324 csd_lock(data);
325
3d442233 326 /* Can deadlock when called with interrupts disabled */
6e275637 327 WARN_ON(wait && irqs_disabled());
3d442233 328
6e275637 329 generic_exec_single(cpu, data, wait);
3d442233
JA
330}
331
ce47d974
RR
332/* FIXME: Shim for archs using old arch_send_call_function_ipi API. */
333#ifndef arch_send_call_function_ipi_mask
334#define arch_send_call_function_ipi_mask(maskp) \
335 arch_send_call_function_ipi(*(maskp))
336#endif
337
3d442233 338/**
54b11e6d
RR
339 * smp_call_function_many(): Run a function on a set of other CPUs.
340 * @mask: The set of cpus to run on (only runs on online subset).
3d442233
JA
341 * @func: The function to run. This must be fast and non-blocking.
342 * @info: An arbitrary pointer to pass to the function.
343 * @wait: If true, wait (atomically) until function has completed on other CPUs.
344 *
3d442233
JA
345 * If @wait is true, then returns once @func has returned. Note that @wait
346 * will be implicitly turned on in case of allocation failures, since
347 * we fall back to on-stack allocation.
348 *
349 * You must not call this function with disabled interrupts or from a
350 * hardware interrupt handler or from a bottom half handler. Preemption
351 * must be disabled when calling this function.
352 */
54b11e6d
RR
353void smp_call_function_many(const struct cpumask *mask,
354 void (*func)(void *), void *info,
355 bool wait)
3d442233 356{
54b11e6d 357 struct call_function_data *data;
3d442233 358 unsigned long flags;
8969a5ed 359 int cpu, next_cpu, me = smp_processor_id();
3d442233
JA
360
361 /* Can deadlock when called with interrupts disabled */
362 WARN_ON(irqs_disabled());
363
54b11e6d
RR
364 /* So, what's a CPU they want? Ignoring this one. */
365 cpu = cpumask_first_and(mask, cpu_online_mask);
8969a5ed 366 if (cpu == me)
54b11e6d
RR
367 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
368 /* No online cpus? We're done. */
369 if (cpu >= nr_cpu_ids)
370 return;
371
372 /* Do we have another CPU which isn't us? */
373 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
8969a5ed 374 if (next_cpu == me)
54b11e6d
RR
375 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
376
377 /* Fastpath: do that cpu by itself. */
378 if (next_cpu >= nr_cpu_ids) {
379 smp_call_function_single(cpu, func, info, wait);
380 return;
3d442233
JA
381 }
382
8969a5ed
PZ
383 data = &__get_cpu_var(cfd_data);
384 csd_lock(&data->csd);
3d442233 385
8969a5ed 386 spin_lock_irqsave(&data->lock, flags);
3d442233
JA
387 data->csd.func = func;
388 data->csd.info = info;
8969a5ed
PZ
389 cpumask_and(data->cpumask, mask, cpu_online_mask);
390 cpumask_clear_cpu(me, data->cpumask);
391 data->refs = cpumask_weight(data->cpumask);
3d442233 392
8969a5ed
PZ
393 spin_lock(&call_function.lock);
394 /*
395 * Place entry at the _HEAD_ of the list, so that any cpu still
396 * observing the entry in generic_smp_call_function_interrupt() will
397 * not miss any other list entries.
398 */
399 list_add_rcu(&data->csd.list, &call_function.queue);
400 spin_unlock(&call_function.lock);
401 spin_unlock_irqrestore(&data->lock, flags);
3d442233 402
561920a0
SS
403 /*
404 * Make the list addition visible before sending the ipi.
15d0d3b3
NP
405 * (IPIs must obey or appear to obey normal Linux cache coherency
406 * rules -- see comment in generic_exec_single).
561920a0
SS
407 */
408 smp_mb();
409
3d442233 410 /* Send a message to all CPUs in the map */
8969a5ed 411 arch_send_call_function_ipi_mask(data->cpumask);
3d442233
JA
412
413 /* optionally wait for the CPUs to complete */
54b11e6d 414 if (wait)
6e275637 415 csd_lock_wait(&data->csd);
3d442233 416}
54b11e6d 417EXPORT_SYMBOL(smp_call_function_many);
3d442233
JA
418
419/**
420 * smp_call_function(): Run a function on all other CPUs.
421 * @func: The function to run. This must be fast and non-blocking.
422 * @info: An arbitrary pointer to pass to the function.
3d442233
JA
423 * @wait: If true, wait (atomically) until function has completed on other CPUs.
424 *
54b11e6d 425 * Returns 0.
3d442233
JA
426 *
427 * If @wait is true, then returns once @func has returned; otherwise
428 * it returns just before the target cpu calls @func. In case of allocation
429 * failure, @wait will be implicitly turned on.
430 *
431 * You must not call this function with disabled interrupts or from a
432 * hardware interrupt handler or from a bottom half handler.
433 */
8691e5a8 434int smp_call_function(void (*func)(void *), void *info, int wait)
3d442233 435{
3d442233 436 preempt_disable();
54b11e6d 437 smp_call_function_many(cpu_online_mask, func, info, wait);
3d442233 438 preempt_enable();
54b11e6d 439 return 0;
3d442233
JA
440}
441EXPORT_SYMBOL(smp_call_function);
442
443void ipi_call_lock(void)
444{
8969a5ed 445 spin_lock(&call_function.lock);
3d442233
JA
446}
447
448void ipi_call_unlock(void)
449{
8969a5ed 450 spin_unlock(&call_function.lock);
3d442233
JA
451}
452
453void ipi_call_lock_irq(void)
454{
8969a5ed 455 spin_lock_irq(&call_function.lock);
3d442233
JA
456}
457
458void ipi_call_unlock_irq(void)
459{
8969a5ed 460 spin_unlock_irq(&call_function.lock);
3d442233 461}