[media] videobuf2-core: Verify planes lengths for output buffers
[linux-2.6.git] / kernel / smp.c
blobfe9f773d71146c82ad9c543ab0456490c9bebd0f
1 /*
2 * Generic helpers for smp ipi calls
4 * (C) Jens Axboe <jens.axboe@oracle.com> 2008
5 */
6 #include <linux/rcupdate.h>
7 #include <linux/rculist.h>
8 #include <linux/kernel.h>
9 #include <linux/export.h>
10 #include <linux/percpu.h>
11 #include <linux/init.h>
12 #include <linux/gfp.h>
13 #include <linux/smp.h>
14 #include <linux/cpu.h>
16 #include "smpboot.h"
18 #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
19 enum {
20 CSD_FLAG_LOCK = 0x01,
23 struct call_function_data {
24 struct call_single_data __percpu *csd;
25 cpumask_var_t cpumask;
26 cpumask_var_t cpumask_ipi;
29 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
31 struct call_single_queue {
32 struct list_head list;
33 raw_spinlock_t lock;
36 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
38 static int
39 hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
41 long cpu = (long)hcpu;
42 struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
44 switch (action) {
45 case CPU_UP_PREPARE:
46 case CPU_UP_PREPARE_FROZEN:
47 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
48 cpu_to_node(cpu)))
49 return notifier_from_errno(-ENOMEM);
50 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
51 cpu_to_node(cpu)))
52 return notifier_from_errno(-ENOMEM);
53 cfd->csd = alloc_percpu(struct call_single_data);
54 if (!cfd->csd) {
55 free_cpumask_var(cfd->cpumask);
56 return notifier_from_errno(-ENOMEM);
58 break;
60 #ifdef CONFIG_HOTPLUG_CPU
61 case CPU_UP_CANCELED:
62 case CPU_UP_CANCELED_FROZEN:
64 case CPU_DEAD:
65 case CPU_DEAD_FROZEN:
66 free_cpumask_var(cfd->cpumask);
67 free_cpumask_var(cfd->cpumask_ipi);
68 free_percpu(cfd->csd);
69 break;
70 #endif
73 return NOTIFY_OK;
76 static struct notifier_block hotplug_cfd_notifier = {
77 .notifier_call = hotplug_cfd,
80 void __init call_function_init(void)
82 void *cpu = (void *)(long)smp_processor_id();
83 int i;
85 for_each_possible_cpu(i) {
86 struct call_single_queue *q = &per_cpu(call_single_queue, i);
88 raw_spin_lock_init(&q->lock);
89 INIT_LIST_HEAD(&q->list);
92 hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
93 register_cpu_notifier(&hotplug_cfd_notifier);
97 * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
99 * For non-synchronous ipi calls the csd can still be in use by the
100 * previous function call. For multi-cpu calls its even more interesting
101 * as we'll have to ensure no other cpu is observing our csd.
103 static void csd_lock_wait(struct call_single_data *csd)
105 while (csd->flags & CSD_FLAG_LOCK)
106 cpu_relax();
109 static void csd_lock(struct call_single_data *csd)
111 csd_lock_wait(csd);
112 csd->flags |= CSD_FLAG_LOCK;
115 * prevent CPU from reordering the above assignment
116 * to ->flags with any subsequent assignments to other
117 * fields of the specified call_single_data structure:
119 smp_mb();
122 static void csd_unlock(struct call_single_data *csd)
124 WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
127 * ensure we're all done before releasing data:
129 smp_mb();
131 csd->flags &= ~CSD_FLAG_LOCK;
135 * Insert a previously allocated call_single_data element
136 * for execution on the given CPU. data must already have
137 * ->func, ->info, and ->flags set.
139 static
140 void generic_exec_single(int cpu, struct call_single_data *csd, int wait)
142 struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
143 unsigned long flags;
144 int ipi;
146 raw_spin_lock_irqsave(&dst->lock, flags);
147 ipi = list_empty(&dst->list);
148 list_add_tail(&csd->list, &dst->list);
149 raw_spin_unlock_irqrestore(&dst->lock, flags);
152 * The list addition should be visible before sending the IPI
153 * handler locks the list to pull the entry off it because of
154 * normal cache coherency rules implied by spinlocks.
156 * If IPIs can go out of order to the cache coherency protocol
157 * in an architecture, sufficient synchronisation should be added
158 * to arch code to make it appear to obey cache coherency WRT
159 * locking and barrier primitives. Generic code isn't really
160 * equipped to do the right thing...
162 if (ipi)
163 arch_send_call_function_single_ipi(cpu);
165 if (wait)
166 csd_lock_wait(csd);
170 * Invoked by arch to handle an IPI for call function single. Must be
171 * called from the arch with interrupts disabled.
173 void generic_smp_call_function_single_interrupt(void)
175 struct call_single_queue *q = &__get_cpu_var(call_single_queue);
176 LIST_HEAD(list);
179 * Shouldn't receive this interrupt on a cpu that is not yet online.
181 WARN_ON_ONCE(!cpu_online(smp_processor_id()));
183 raw_spin_lock(&q->lock);
184 list_replace_init(&q->list, &list);
185 raw_spin_unlock(&q->lock);
187 while (!list_empty(&list)) {
188 struct call_single_data *csd;
189 unsigned int csd_flags;
191 csd = list_entry(list.next, struct call_single_data, list);
192 list_del(&csd->list);
195 * 'csd' can be invalid after this call if flags == 0
196 * (when called through generic_exec_single()),
197 * so save them away before making the call:
199 csd_flags = csd->flags;
201 csd->func(csd->info);
204 * Unlocked CSDs are valid through generic_exec_single():
206 if (csd_flags & CSD_FLAG_LOCK)
207 csd_unlock(csd);
211 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
214 * smp_call_function_single - Run a function on a specific CPU
215 * @func: The function to run. This must be fast and non-blocking.
216 * @info: An arbitrary pointer to pass to the function.
217 * @wait: If true, wait until function has completed on other CPUs.
219 * Returns 0 on success, else a negative status code.
221 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
222 int wait)
224 struct call_single_data d = {
225 .flags = 0,
227 unsigned long flags;
228 int this_cpu;
229 int err = 0;
232 * prevent preemption and reschedule on another processor,
233 * as well as CPU removal
235 this_cpu = get_cpu();
238 * Can deadlock when called with interrupts disabled.
239 * We allow cpu's that are not yet online though, as no one else can
240 * send smp call function interrupt to this cpu and as such deadlocks
241 * can't happen.
243 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
244 && !oops_in_progress);
246 if (cpu == this_cpu) {
247 local_irq_save(flags);
248 func(info);
249 local_irq_restore(flags);
250 } else {
251 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
252 struct call_single_data *csd = &d;
254 if (!wait)
255 csd = &__get_cpu_var(csd_data);
257 csd_lock(csd);
259 csd->func = func;
260 csd->info = info;
261 generic_exec_single(cpu, csd, wait);
262 } else {
263 err = -ENXIO; /* CPU not online */
267 put_cpu();
269 return err;
271 EXPORT_SYMBOL(smp_call_function_single);
274 * smp_call_function_any - Run a function on any of the given cpus
275 * @mask: The mask of cpus it can run on.
276 * @func: The function to run. This must be fast and non-blocking.
277 * @info: An arbitrary pointer to pass to the function.
278 * @wait: If true, wait until function has completed.
280 * Returns 0 on success, else a negative status code (if no cpus were online).
281 * Note that @wait will be implicitly turned on in case of allocation failures,
282 * since we fall back to on-stack allocation.
284 * Selection preference:
285 * 1) current cpu if in @mask
286 * 2) any cpu of current node if in @mask
287 * 3) any other online cpu in @mask
289 int smp_call_function_any(const struct cpumask *mask,
290 smp_call_func_t func, void *info, int wait)
292 unsigned int cpu;
293 const struct cpumask *nodemask;
294 int ret;
296 /* Try for same CPU (cheapest) */
297 cpu = get_cpu();
298 if (cpumask_test_cpu(cpu, mask))
299 goto call;
301 /* Try for same node. */
302 nodemask = cpumask_of_node(cpu_to_node(cpu));
303 for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
304 cpu = cpumask_next_and(cpu, nodemask, mask)) {
305 if (cpu_online(cpu))
306 goto call;
309 /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
310 cpu = cpumask_any_and(mask, cpu_online_mask);
311 call:
312 ret = smp_call_function_single(cpu, func, info, wait);
313 put_cpu();
314 return ret;
316 EXPORT_SYMBOL_GPL(smp_call_function_any);
319 * __smp_call_function_single(): Run a function on a specific CPU
320 * @cpu: The CPU to run on.
321 * @data: Pre-allocated and setup data structure
322 * @wait: If true, wait until function has completed on specified CPU.
324 * Like smp_call_function_single(), but allow caller to pass in a
325 * pre-allocated data structure. Useful for embedding @data inside
326 * other structures, for instance.
328 void __smp_call_function_single(int cpu, struct call_single_data *csd,
329 int wait)
331 unsigned int this_cpu;
332 unsigned long flags;
334 this_cpu = get_cpu();
336 * Can deadlock when called with interrupts disabled.
337 * We allow cpu's that are not yet online though, as no one else can
338 * send smp call function interrupt to this cpu and as such deadlocks
339 * can't happen.
341 WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
342 && !oops_in_progress);
344 if (cpu == this_cpu) {
345 local_irq_save(flags);
346 csd->func(csd->info);
347 local_irq_restore(flags);
348 } else {
349 csd_lock(csd);
350 generic_exec_single(cpu, csd, wait);
352 put_cpu();
356 * smp_call_function_many(): Run a function on a set of other CPUs.
357 * @mask: The set of cpus to run on (only runs on online subset).
358 * @func: The function to run. This must be fast and non-blocking.
359 * @info: An arbitrary pointer to pass to the function.
360 * @wait: If true, wait (atomically) until function has completed
361 * on other CPUs.
363 * If @wait is true, then returns once @func has returned.
365 * You must not call this function with disabled interrupts or from a
366 * hardware interrupt handler or from a bottom half handler. Preemption
367 * must be disabled when calling this function.
369 void smp_call_function_many(const struct cpumask *mask,
370 smp_call_func_t func, void *info, bool wait)
372 struct call_function_data *cfd;
373 int cpu, next_cpu, this_cpu = smp_processor_id();
376 * Can deadlock when called with interrupts disabled.
377 * We allow cpu's that are not yet online though, as no one else can
378 * send smp call function interrupt to this cpu and as such deadlocks
379 * can't happen.
381 WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
382 && !oops_in_progress && !early_boot_irqs_disabled);
384 /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
385 cpu = cpumask_first_and(mask, cpu_online_mask);
386 if (cpu == this_cpu)
387 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
389 /* No online cpus? We're done. */
390 if (cpu >= nr_cpu_ids)
391 return;
393 /* Do we have another CPU which isn't us? */
394 next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
395 if (next_cpu == this_cpu)
396 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
398 /* Fastpath: do that cpu by itself. */
399 if (next_cpu >= nr_cpu_ids) {
400 smp_call_function_single(cpu, func, info, wait);
401 return;
404 cfd = &__get_cpu_var(cfd_data);
406 cpumask_and(cfd->cpumask, mask, cpu_online_mask);
407 cpumask_clear_cpu(this_cpu, cfd->cpumask);
409 /* Some callers race with other cpus changing the passed mask */
410 if (unlikely(!cpumask_weight(cfd->cpumask)))
411 return;
414 * After we put an entry into the list, cfd->cpumask may be cleared
415 * again when another CPU sends another IPI for a SMP function call, so
416 * cfd->cpumask will be zero.
418 cpumask_copy(cfd->cpumask_ipi, cfd->cpumask);
420 for_each_cpu(cpu, cfd->cpumask) {
421 struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
422 struct call_single_queue *dst =
423 &per_cpu(call_single_queue, cpu);
424 unsigned long flags;
426 csd_lock(csd);
427 csd->func = func;
428 csd->info = info;
430 raw_spin_lock_irqsave(&dst->lock, flags);
431 list_add_tail(&csd->list, &dst->list);
432 raw_spin_unlock_irqrestore(&dst->lock, flags);
435 /* Send a message to all CPUs in the map */
436 arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
438 if (wait) {
439 for_each_cpu(cpu, cfd->cpumask) {
440 struct call_single_data *csd;
442 csd = per_cpu_ptr(cfd->csd, cpu);
443 csd_lock_wait(csd);
447 EXPORT_SYMBOL(smp_call_function_many);
450 * smp_call_function(): Run a function on all other CPUs.
451 * @func: The function to run. This must be fast and non-blocking.
452 * @info: An arbitrary pointer to pass to the function.
453 * @wait: If true, wait (atomically) until function has completed
454 * on other CPUs.
456 * Returns 0.
458 * If @wait is true, then returns once @func has returned; otherwise
459 * it returns just before the target cpu calls @func.
461 * You must not call this function with disabled interrupts or from a
462 * hardware interrupt handler or from a bottom half handler.
464 int smp_call_function(smp_call_func_t func, void *info, int wait)
466 preempt_disable();
467 smp_call_function_many(cpu_online_mask, func, info, wait);
468 preempt_enable();
470 return 0;
472 EXPORT_SYMBOL(smp_call_function);
473 #endif /* USE_GENERIC_SMP_HELPERS */
475 /* Setup configured maximum number of CPUs to activate */
476 unsigned int setup_max_cpus = NR_CPUS;
477 EXPORT_SYMBOL(setup_max_cpus);
481 * Setup routine for controlling SMP activation
483 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
484 * activation entirely (the MPS table probe still happens, though).
486 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
487 * greater than 0, limits the maximum number of CPUs activated in
488 * SMP mode to <NUM>.
491 void __weak arch_disable_smp_support(void) { }
493 static int __init nosmp(char *str)
495 setup_max_cpus = 0;
496 arch_disable_smp_support();
498 return 0;
501 early_param("nosmp", nosmp);
503 /* this is hard limit */
504 static int __init nrcpus(char *str)
506 int nr_cpus;
508 get_option(&str, &nr_cpus);
509 if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
510 nr_cpu_ids = nr_cpus;
512 return 0;
515 early_param("nr_cpus", nrcpus);
517 static int __init maxcpus(char *str)
519 get_option(&str, &setup_max_cpus);
520 if (setup_max_cpus == 0)
521 arch_disable_smp_support();
523 return 0;
526 early_param("maxcpus", maxcpus);
528 /* Setup number of possible processor ids */
529 int nr_cpu_ids __read_mostly = NR_CPUS;
530 EXPORT_SYMBOL(nr_cpu_ids);
532 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
533 void __init setup_nr_cpu_ids(void)
535 nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
538 /* Called by boot processor to activate the rest. */
539 void __init smp_init(void)
541 unsigned int cpu;
543 idle_threads_init();
545 /* 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);
553 /* Any cleanup work */
554 printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
555 smp_cpus_done(setup_max_cpus);
559 * 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().
563 int on_each_cpu(void (*func) (void *info), void *info, int wait)
565 unsigned long flags;
566 int ret = 0;
568 preempt_disable();
569 ret = smp_call_function(func, info, wait);
570 local_irq_save(flags);
571 func(info);
572 local_irq_restore(flags);
573 preempt_enable();
574 return ret;
576 EXPORT_SYMBOL(on_each_cpu);
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.
587 * If @wait is true, then returns once @func has returned.
589 * You must not call this function with disabled interrupts or
590 * from a hardware interrupt handler or from a bottom half handler.
592 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
593 void *info, bool wait)
595 int cpu = get_cpu();
597 smp_call_function_many(mask, func, info, wait);
598 if (cpumask_test_cpu(cpu, mask)) {
599 local_irq_disable();
600 func(info);
601 local_irq_enable();
603 put_cpu();
605 EXPORT_SYMBOL(on_each_cpu_mask);
608 * on_each_cpu_cond(): Call a function on each processor for which
609 * the supplied function cond_func returns true, optionally waiting
610 * for all the required CPUs to finish. This may include the local
611 * processor.
612 * @cond_func: A callback function that is passed a cpu id and
613 * the the info parameter. The function is called
614 * with preemption disabled. The function should
615 * return a blooean value indicating whether to IPI
616 * the specified CPU.
617 * @func: The function to run on all applicable CPUs.
618 * This must be fast and non-blocking.
619 * @info: An arbitrary pointer to pass to both functions.
620 * @wait: If true, wait (atomically) until function has
621 * completed on other CPUs.
622 * @gfp_flags: GFP flags to use when allocating the cpumask
623 * used internally by the function.
625 * The function might sleep if the GFP flags indicates a non
626 * atomic allocation is allowed.
628 * Preemption is disabled to protect against CPUs going offline but not online.
629 * CPUs going online during the call will not be seen or sent an IPI.
631 * You must not call this function with disabled interrupts or
632 * from a hardware interrupt handler or from a bottom half handler.
634 void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
635 smp_call_func_t func, void *info, bool wait,
636 gfp_t gfp_flags)
638 cpumask_var_t cpus;
639 int cpu, ret;
641 might_sleep_if(gfp_flags & __GFP_WAIT);
643 if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
644 preempt_disable();
645 for_each_online_cpu(cpu)
646 if (cond_func(cpu, info))
647 cpumask_set_cpu(cpu, cpus);
648 on_each_cpu_mask(cpus, func, info, wait);
649 preempt_enable();
650 free_cpumask_var(cpus);
651 } else {
653 * No free cpumask, bother. No matter, we'll
654 * just have to IPI them one by one.
656 preempt_disable();
657 for_each_online_cpu(cpu)
658 if (cond_func(cpu, info)) {
659 ret = smp_call_function_single(cpu, func,
660 info, wait);
661 WARN_ON_ONCE(!ret);
663 preempt_enable();
666 EXPORT_SYMBOL(on_each_cpu_cond);
668 static void do_nothing(void *unused)
673 * kick_all_cpus_sync - Force all cpus out of idle
675 * Used to synchronize the update of pm_idle function pointer. It's
676 * called after the pointer is updated and returns after the dummy
677 * callback function has been executed on all cpus. The execution of
678 * the function can only happen on the remote cpus after they have
679 * left the idle function which had been called via pm_idle function
680 * pointer. So it's guaranteed that nothing uses the previous pointer
681 * anymore.
683 void kick_all_cpus_sync(void)
685 /* Make sure the change is visible before we kick the cpus */
686 smp_mb();
687 smp_call_function(do_nothing, NULL, 1);
689 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);