2 * Generic helpers for smp ipi calls
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
7 #include <linux/init.h>
8 #include <linux/module.h>
9 #include <linux/percpu.h>
10 #include <linux/rcupdate.h>
11 #include <linux/rculist.h>
12 #include <linux/smp.h>
14 static DEFINE_PER_CPU(struct call_single_queue
, call_single_queue
);
15 static LIST_HEAD(call_function_queue
);
16 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(call_function_lock
);
20 CSD_FLAG_ALLOC
= 0x02,
23 struct call_function_data
{
24 struct call_single_data csd
;
28 struct rcu_head rcu_head
;
31 struct call_single_queue
{
32 struct list_head list
;
36 static int __cpuinit
init_call_single_data(void)
40 for_each_possible_cpu(i
) {
41 struct call_single_queue
*q
= &per_cpu(call_single_queue
, i
);
43 spin_lock_init(&q
->lock
);
44 INIT_LIST_HEAD(&q
->list
);
48 early_initcall(init_call_single_data
);
50 static void csd_flag_wait(struct call_single_data
*data
)
52 /* Wait for response */
54 if (!(data
->flags
& CSD_FLAG_WAIT
))
61 * Insert a previously allocated call_single_data element for execution
62 * on the given CPU. data must already have ->func, ->info, and ->flags set.
64 static void generic_exec_single(int cpu
, struct call_single_data
*data
)
66 struct call_single_queue
*dst
= &per_cpu(call_single_queue
, cpu
);
67 int wait
= data
->flags
& CSD_FLAG_WAIT
, ipi
;
70 spin_lock_irqsave(&dst
->lock
, flags
);
71 ipi
= list_empty(&dst
->list
);
72 list_add_tail(&data
->list
, &dst
->list
);
73 spin_unlock_irqrestore(&dst
->lock
, flags
);
76 * Make the list addition visible before sending the ipi.
81 arch_send_call_function_single_ipi(cpu
);
87 static void rcu_free_call_data(struct rcu_head
*head
)
89 struct call_function_data
*data
;
91 data
= container_of(head
, struct call_function_data
, rcu_head
);
97 * Invoked by arch to handle an IPI for call function. Must be called with
98 * interrupts disabled.
100 void generic_smp_call_function_interrupt(void)
102 struct call_function_data
*data
;
106 * It's ok to use list_for_each_rcu() here even though we may delete
107 * 'pos', since list_del_rcu() doesn't clear ->next
110 list_for_each_entry_rcu(data
, &call_function_queue
, csd
.list
) {
113 if (!cpu_isset(cpu
, data
->cpumask
))
116 data
->csd
.func(data
->csd
.info
);
118 spin_lock(&data
->lock
);
119 cpu_clear(cpu
, data
->cpumask
);
120 WARN_ON(data
->refs
== 0);
123 spin_unlock(&data
->lock
);
128 spin_lock(&call_function_lock
);
129 list_del_rcu(&data
->csd
.list
);
130 spin_unlock(&call_function_lock
);
132 if (data
->csd
.flags
& CSD_FLAG_WAIT
) {
134 * serialize stores to data with the flag clear
138 data
->csd
.flags
&= ~CSD_FLAG_WAIT
;
140 if (data
->csd
.flags
& CSD_FLAG_ALLOC
)
141 call_rcu(&data
->rcu_head
, rcu_free_call_data
);
149 * Invoked by arch to handle an IPI for call function single. Must be called
150 * from the arch with interrupts disabled.
152 void generic_smp_call_function_single_interrupt(void)
154 struct call_single_queue
*q
= &__get_cpu_var(call_single_queue
);
158 * Need to see other stores to list head for checking whether
159 * list is empty without holding q->lock
161 smp_read_barrier_depends();
162 while (!list_empty(&q
->list
)) {
163 unsigned int data_flags
;
166 list_replace_init(&q
->list
, &list
);
167 spin_unlock(&q
->lock
);
169 while (!list_empty(&list
)) {
170 struct call_single_data
*data
;
172 data
= list_entry(list
.next
, struct call_single_data
,
174 list_del(&data
->list
);
177 * 'data' can be invalid after this call if
178 * flags == 0 (when called through
179 * generic_exec_single(), so save them away before
182 data_flags
= data
->flags
;
184 data
->func(data
->info
);
186 if (data_flags
& CSD_FLAG_WAIT
) {
188 data
->flags
&= ~CSD_FLAG_WAIT
;
189 } else if (data_flags
& CSD_FLAG_ALLOC
)
193 * See comment on outer loop
195 smp_read_barrier_depends();
200 * smp_call_function_single - Run a function on a specific CPU
201 * @func: The function to run. This must be fast and non-blocking.
202 * @info: An arbitrary pointer to pass to the function.
203 * @wait: If true, wait until function has completed on other CPUs.
205 * Returns 0 on success, else a negative status code. Note that @wait
206 * will be implicitly turned on in case of allocation failures, since
207 * we fall back to on-stack allocation.
209 int smp_call_function_single(int cpu
, void (*func
) (void *info
), void *info
,
212 struct call_single_data d
;
214 /* prevent preemption and reschedule on another processor,
215 as well as CPU removal */
219 /* Can deadlock when called with interrupts disabled */
220 WARN_ON(irqs_disabled());
223 local_irq_save(flags
);
225 local_irq_restore(flags
);
226 } else if ((unsigned)cpu
< NR_CPUS
&& cpu_online(cpu
)) {
227 struct call_single_data
*data
= NULL
;
230 data
= kmalloc(sizeof(*data
), GFP_ATOMIC
);
232 data
->flags
= CSD_FLAG_ALLOC
;
236 data
->flags
= CSD_FLAG_WAIT
;
241 generic_exec_single(cpu
, data
);
243 err
= -ENXIO
; /* CPU not online */
249 EXPORT_SYMBOL(smp_call_function_single
);
252 * __smp_call_function_single(): Run a function on another CPU
253 * @cpu: The CPU to run on.
254 * @data: Pre-allocated and setup data structure
256 * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
257 * data structure. Useful for embedding @data inside other structures, for
261 void __smp_call_function_single(int cpu
, struct call_single_data
*data
)
263 /* Can deadlock when called with interrupts disabled */
264 WARN_ON((data
->flags
& CSD_FLAG_WAIT
) && irqs_disabled());
266 generic_exec_single(cpu
, data
);
270 static void quiesce_dummy(void *unused
)
275 * Ensure stack based data used in call function mask is safe to free.
277 * This is needed by smp_call_function_mask when using on-stack data, because
278 * a single call function queue is shared by all CPUs, and any CPU may pick up
279 * the data item on the queue at any time before it is deleted. So we need to
280 * ensure that all CPUs have transitioned through a quiescent state after
283 * This is a very slow function, implemented by sending synchronous IPIs to
284 * all possible CPUs. For this reason, we have to alloc data rather than use
285 * stack based data even in the case of synchronous calls. The stack based
286 * data is then just used for deadlock/oom fallback which will be very rare.
288 * If a faster scheme can be made, we could go back to preferring stack based
289 * data -- the data allocation/free is non-zero cost.
291 static void smp_call_function_mask_quiesce_stack(cpumask_t mask
)
293 struct call_single_data data
;
296 data
.func
= quiesce_dummy
;
299 for_each_cpu_mask(cpu
, mask
) {
300 data
.flags
= CSD_FLAG_WAIT
;
301 generic_exec_single(cpu
, &data
);
306 * smp_call_function_mask(): Run a function on a set of other CPUs.
307 * @mask: The set of cpus to run on.
308 * @func: The function to run. This must be fast and non-blocking.
309 * @info: An arbitrary pointer to pass to the function.
310 * @wait: If true, wait (atomically) until function has completed on other CPUs.
312 * Returns 0 on success, else a negative status code.
314 * If @wait is true, then returns once @func has returned. Note that @wait
315 * will be implicitly turned on in case of allocation failures, since
316 * we fall back to on-stack allocation.
318 * You must not call this function with disabled interrupts or from a
319 * hardware interrupt handler or from a bottom half handler. Preemption
320 * must be disabled when calling this function.
322 int smp_call_function_mask(cpumask_t mask
, void (*func
)(void *), void *info
,
325 struct call_function_data d
;
326 struct call_function_data
*data
= NULL
;
327 cpumask_t allbutself
;
332 /* Can deadlock when called with interrupts disabled */
333 WARN_ON(irqs_disabled());
335 cpu
= smp_processor_id();
336 allbutself
= cpu_online_map
;
337 cpu_clear(cpu
, allbutself
);
338 cpus_and(mask
, mask
, allbutself
);
339 num_cpus
= cpus_weight(mask
);
342 * If zero CPUs, return. If just a single CPU, turn this request
343 * into a targetted single call instead since it's faster.
347 else if (num_cpus
== 1) {
348 cpu
= first_cpu(mask
);
349 return smp_call_function_single(cpu
, func
, info
, wait
);
352 data
= kmalloc(sizeof(*data
), GFP_ATOMIC
);
354 data
->csd
.flags
= CSD_FLAG_ALLOC
;
356 data
->csd
.flags
|= CSD_FLAG_WAIT
;
359 data
->csd
.flags
= CSD_FLAG_WAIT
;
364 spin_lock_init(&data
->lock
);
365 data
->csd
.func
= func
;
366 data
->csd
.info
= info
;
367 data
->refs
= num_cpus
;
368 data
->cpumask
= mask
;
370 spin_lock_irqsave(&call_function_lock
, flags
);
371 list_add_tail_rcu(&data
->csd
.list
, &call_function_queue
);
372 spin_unlock_irqrestore(&call_function_lock
, flags
);
375 * Make the list addition visible before sending the ipi.
379 /* Send a message to all CPUs in the map */
380 arch_send_call_function_ipi(mask
);
382 /* optionally wait for the CPUs to complete */
384 csd_flag_wait(&data
->csd
);
385 if (unlikely(slowpath
))
386 smp_call_function_mask_quiesce_stack(mask
);
391 EXPORT_SYMBOL(smp_call_function_mask
);
394 * smp_call_function(): Run a function on all other CPUs.
395 * @func: The function to run. This must be fast and non-blocking.
396 * @info: An arbitrary pointer to pass to the function.
397 * @wait: If true, wait (atomically) until function has completed on other CPUs.
399 * Returns 0 on success, else a negative status code.
401 * If @wait is true, then returns once @func has returned; otherwise
402 * it returns just before the target cpu calls @func. In case of allocation
403 * failure, @wait will be implicitly turned on.
405 * You must not call this function with disabled interrupts or from a
406 * hardware interrupt handler or from a bottom half handler.
408 int smp_call_function(void (*func
)(void *), void *info
, int wait
)
413 ret
= smp_call_function_mask(cpu_online_map
, func
, info
, wait
);
417 EXPORT_SYMBOL(smp_call_function
);
419 void ipi_call_lock(void)
421 spin_lock(&call_function_lock
);
424 void ipi_call_unlock(void)
426 spin_unlock(&call_function_lock
);
429 void ipi_call_lock_irq(void)
431 spin_lock_irq(&call_function_lock
);
434 void ipi_call_unlock_irq(void)
436 spin_unlock_irq(&call_function_lock
);