]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/smp.c
smp: Give WARN()ing when calling smp_call_function_many()/single() in serving irq
[karo-tx-linux.git] / kernel / smp.c
1 /*
2  * Generic helpers for smp ipi calls
3  *
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>
15 #include <linux/hardirq.h>
16
17 #include "smpboot.h"
18
19 #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
20 enum {
21         CSD_FLAG_LOCK           = 0x01,
22 };
23
24 struct call_function_data {
25         struct call_single_data __percpu *csd;
26         cpumask_var_t           cpumask;
27         cpumask_var_t           cpumask_ipi;
28 };
29
30 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
31
32 struct call_single_queue {
33         struct list_head        list;
34         raw_spinlock_t          lock;
35 };
36
37 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
38
39 static int
40 hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
41 {
42         long cpu = (long)hcpu;
43         struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
44
45         switch (action) {
46         case CPU_UP_PREPARE:
47         case CPU_UP_PREPARE_FROZEN:
48                 if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
49                                 cpu_to_node(cpu)))
50                         return notifier_from_errno(-ENOMEM);
51                 if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
52                                 cpu_to_node(cpu)))
53                         return notifier_from_errno(-ENOMEM);
54                 cfd->csd = alloc_percpu(struct call_single_data);
55                 if (!cfd->csd) {
56                         free_cpumask_var(cfd->cpumask);
57                         return notifier_from_errno(-ENOMEM);
58                 }
59                 break;
60
61 #ifdef CONFIG_HOTPLUG_CPU
62         case CPU_UP_CANCELED:
63         case CPU_UP_CANCELED_FROZEN:
64
65         case CPU_DEAD:
66         case CPU_DEAD_FROZEN:
67                 free_cpumask_var(cfd->cpumask);
68                 free_cpumask_var(cfd->cpumask_ipi);
69                 free_percpu(cfd->csd);
70                 break;
71 #endif
72         };
73
74         return NOTIFY_OK;
75 }
76
77 static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
78         .notifier_call          = hotplug_cfd,
79 };
80
81 void __init call_function_init(void)
82 {
83         void *cpu = (void *)(long)smp_processor_id();
84         int i;
85
86         for_each_possible_cpu(i) {
87                 struct call_single_queue *q = &per_cpu(call_single_queue, i);
88
89                 raw_spin_lock_init(&q->lock);
90                 INIT_LIST_HEAD(&q->list);
91         }
92
93         hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
94         register_cpu_notifier(&hotplug_cfd_notifier);
95 }
96
97 /*
98  * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
99  *
100  * For non-synchronous ipi calls the csd can still be in use by the
101  * previous function call. For multi-cpu calls its even more interesting
102  * as we'll have to ensure no other cpu is observing our csd.
103  */
104 static void csd_lock_wait(struct call_single_data *data)
105 {
106         while (data->flags & CSD_FLAG_LOCK)
107                 cpu_relax();
108 }
109
110 static void csd_lock(struct call_single_data *data)
111 {
112         csd_lock_wait(data);
113         data->flags = CSD_FLAG_LOCK;
114
115         /*
116          * prevent CPU from reordering the above assignment
117          * to ->flags with any subsequent assignments to other
118          * fields of the specified call_single_data structure:
119          */
120         smp_mb();
121 }
122
123 static void csd_unlock(struct call_single_data *data)
124 {
125         WARN_ON(!(data->flags & CSD_FLAG_LOCK));
126
127         /*
128          * ensure we're all done before releasing data:
129          */
130         smp_mb();
131
132         data->flags &= ~CSD_FLAG_LOCK;
133 }
134
135 /*
136  * Insert a previously allocated call_single_data element
137  * for execution on the given CPU. data must already have
138  * ->func, ->info, and ->flags set.
139  */
140 static
141 void generic_exec_single(int cpu, struct call_single_data *data, int wait)
142 {
143         struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
144         unsigned long flags;
145         int ipi;
146
147         raw_spin_lock_irqsave(&dst->lock, flags);
148         ipi = list_empty(&dst->list);
149         list_add_tail(&data->list, &dst->list);
150         raw_spin_unlock_irqrestore(&dst->lock, flags);
151
152         /*
153          * The list addition should be visible before sending the IPI
154          * handler locks the list to pull the entry off it because of
155          * normal cache coherency rules implied by spinlocks.
156          *
157          * If IPIs can go out of order to the cache coherency protocol
158          * in an architecture, sufficient synchronisation should be added
159          * to arch code to make it appear to obey cache coherency WRT
160          * locking and barrier primitives. Generic code isn't really
161          * equipped to do the right thing...
162          */
163         if (ipi)
164                 arch_send_call_function_single_ipi(cpu);
165
166         if (wait)
167                 csd_lock_wait(data);
168 }
169
170 /*
171  * Invoked by arch to handle an IPI for call function single. Must be
172  * called from the arch with interrupts disabled.
173  */
174 void generic_smp_call_function_single_interrupt(void)
175 {
176         struct call_single_queue *q = &__get_cpu_var(call_single_queue);
177         unsigned int data_flags;
178         LIST_HEAD(list);
179
180         /*
181          * Shouldn't receive this interrupt on a cpu that is not yet online.
182          */
183         WARN_ON_ONCE(!cpu_online(smp_processor_id()));
184
185         raw_spin_lock(&q->lock);
186         list_replace_init(&q->list, &list);
187         raw_spin_unlock(&q->lock);
188
189         while (!list_empty(&list)) {
190                 struct call_single_data *data;
191
192                 data = list_entry(list.next, struct call_single_data, list);
193                 list_del(&data->list);
194
195                 /*
196                  * 'data' can be invalid after this call if flags == 0
197                  * (when called through generic_exec_single()),
198                  * so save them away before making the call:
199                  */
200                 data_flags = data->flags;
201
202                 data->func(data->info);
203
204                 /*
205                  * Unlocked CSDs are valid through generic_exec_single():
206                  */
207                 if (data_flags & CSD_FLAG_LOCK)
208                         csd_unlock(data);
209         }
210 }
211
212 static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
213
214 /*
215  * smp_call_function_single - Run a function on a specific CPU
216  * @func: The function to run. This must be fast and non-blocking.
217  * @info: An arbitrary pointer to pass to the function.
218  * @wait: If true, wait until function has completed on other CPUs.
219  *
220  * Returns 0 on success, else a negative status code.
221  */
222 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
223                              int wait)
224 {
225         struct call_single_data d = {
226                 .flags = 0,
227         };
228         unsigned long flags;
229         int this_cpu;
230         int err = 0;
231
232         /*
233          * prevent preemption and reschedule on another processor,
234          * as well as CPU removal
235          */
236         this_cpu = get_cpu();
237
238         /*
239          * Can deadlock when called with interrupts disabled.
240          * We allow cpu's that are not yet online though, as no one else can
241          * send smp call function interrupt to this cpu and as such deadlocks
242          * can't happen.
243          */
244         WARN_ON_ONCE(cpu_online(this_cpu)
245                 && (irqs_disabled() || in_serving_irq())
246                 && !oops_in_progress);
247
248         if (cpu == this_cpu) {
249                 local_irq_save(flags);
250                 func(info);
251                 local_irq_restore(flags);
252         } else {
253                 if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
254                         struct call_single_data *data = &d;
255
256                         if (!wait)
257                                 data = &__get_cpu_var(csd_data);
258
259                         csd_lock(data);
260
261                         data->func = func;
262                         data->info = info;
263                         generic_exec_single(cpu, data, wait);
264                 } else {
265                         err = -ENXIO;   /* CPU not online */
266                 }
267         }
268
269         put_cpu();
270
271         return err;
272 }
273 EXPORT_SYMBOL(smp_call_function_single);
274
275 /*
276  * smp_call_function_any - Run a function on any of the given cpus
277  * @mask: The mask of cpus it can run on.
278  * @func: The function to run. This must be fast and non-blocking.
279  * @info: An arbitrary pointer to pass to the function.
280  * @wait: If true, wait until function has completed.
281  *
282  * Returns 0 on success, else a negative status code (if no cpus were online).
283  * Note that @wait will be implicitly turned on in case of allocation failures,
284  * since we fall back to on-stack allocation.
285  *
286  * Selection preference:
287  *      1) current cpu if in @mask
288  *      2) any cpu of current node if in @mask
289  *      3) any other online cpu in @mask
290  */
291 int smp_call_function_any(const struct cpumask *mask,
292                           smp_call_func_t func, void *info, int wait)
293 {
294         unsigned int cpu;
295         const struct cpumask *nodemask;
296         int ret;
297
298         /* Try for same CPU (cheapest) */
299         cpu = get_cpu();
300         if (cpumask_test_cpu(cpu, mask))
301                 goto call;
302
303         /* Try for same node. */
304         nodemask = cpumask_of_node(cpu_to_node(cpu));
305         for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
306              cpu = cpumask_next_and(cpu, nodemask, mask)) {
307                 if (cpu_online(cpu))
308                         goto call;
309         }
310
311         /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
312         cpu = cpumask_any_and(mask, cpu_online_mask);
313 call:
314         ret = smp_call_function_single(cpu, func, info, wait);
315         put_cpu();
316         return ret;
317 }
318 EXPORT_SYMBOL_GPL(smp_call_function_any);
319
320 /**
321  * __smp_call_function_single(): Run a function on a specific CPU
322  * @cpu: The CPU to run on.
323  * @data: Pre-allocated and setup data structure
324  * @wait: If true, wait until function has completed on specified CPU.
325  *
326  * Like smp_call_function_single(), but allow caller to pass in a
327  * pre-allocated data structure. Useful for embedding @data inside
328  * other structures, for instance.
329  */
330 void __smp_call_function_single(int cpu, struct call_single_data *data,
331                                 int wait)
332 {
333         unsigned int this_cpu;
334         unsigned long flags;
335
336         this_cpu = get_cpu();
337         /*
338          * Can deadlock when called with interrupts disabled.
339          * We allow cpu's that are not yet online though, as no one else can
340          * send smp call function interrupt to this cpu and as such deadlocks
341          * can't happen.
342          */
343         WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
344                      && !oops_in_progress);
345
346         if (cpu == this_cpu) {
347                 local_irq_save(flags);
348                 data->func(data->info);
349                 local_irq_restore(flags);
350         } else {
351                 csd_lock(data);
352                 generic_exec_single(cpu, data, wait);
353         }
354         put_cpu();
355 }
356
357 /**
358  * smp_call_function_many(): Run a function on a set of other CPUs.
359  * @mask: The set of cpus to run on (only runs on online subset).
360  * @func: The function to run. This must be fast and non-blocking.
361  * @info: An arbitrary pointer to pass to the function.
362  * @wait: If true, wait (atomically) until function has completed
363  *        on other CPUs.
364  *
365  * If @wait is true, then returns once @func has returned.
366  *
367  * You must not call this function with disabled interrupts or from a
368  * hardware interrupt handler or from a bottom half handler. Preemption
369  * must be disabled when calling this function.
370  */
371 void smp_call_function_many(const struct cpumask *mask,
372                             smp_call_func_t func, void *info, bool wait)
373 {
374         struct call_function_data *data;
375         int cpu, next_cpu, this_cpu = smp_processor_id();
376
377         /*
378          * Can deadlock when called with interrupts disabled.
379          * We allow cpu's that are not yet online though, as no one else can
380          * send smp call function interrupt to this cpu and as such deadlocks
381          * can't happen.
382          */
383         WARN_ON_ONCE(cpu_online(this_cpu)
384                 && (irqs_disabled() || in_serving_irq())
385                 && !oops_in_progress && !early_boot_irqs_disabled);
386
387         /* Try to fastpath.  So, what's a CPU they want? Ignoring this one. */
388         cpu = cpumask_first_and(mask, cpu_online_mask);
389         if (cpu == this_cpu)
390                 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
391
392         /* No online cpus?  We're done. */
393         if (cpu >= nr_cpu_ids)
394                 return;
395
396         /* Do we have another CPU which isn't us? */
397         next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
398         if (next_cpu == this_cpu)
399                 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
400
401         /* Fastpath: do that cpu by itself. */
402         if (next_cpu >= nr_cpu_ids) {
403                 smp_call_function_single(cpu, func, info, wait);
404                 return;
405         }
406
407         data = &__get_cpu_var(cfd_data);
408
409         cpumask_and(data->cpumask, mask, cpu_online_mask);
410         cpumask_clear_cpu(this_cpu, data->cpumask);
411
412         /* Some callers race with other cpus changing the passed mask */
413         if (unlikely(!cpumask_weight(data->cpumask)))
414                 return;
415
416         /*
417          * After we put an entry into the list, data->cpumask
418          * may be cleared again when another CPU sends another IPI for
419          * a SMP function call, so data->cpumask will be zero.
420          */
421         cpumask_copy(data->cpumask_ipi, data->cpumask);
422
423         for_each_cpu(cpu, data->cpumask) {
424                 struct call_single_data *csd = per_cpu_ptr(data->csd, cpu);
425                 struct call_single_queue *dst =
426                                         &per_cpu(call_single_queue, cpu);
427                 unsigned long flags;
428
429                 csd_lock(csd);
430                 csd->func = func;
431                 csd->info = info;
432
433                 raw_spin_lock_irqsave(&dst->lock, flags);
434                 list_add_tail(&csd->list, &dst->list);
435                 raw_spin_unlock_irqrestore(&dst->lock, flags);
436         }
437
438         /* Send a message to all CPUs in the map */
439         arch_send_call_function_ipi_mask(data->cpumask_ipi);
440
441         if (wait) {
442                 for_each_cpu(cpu, data->cpumask) {
443                         struct call_single_data *csd =
444                                         per_cpu_ptr(data->csd, cpu);
445                         csd_lock_wait(csd);
446                 }
447         }
448 }
449 EXPORT_SYMBOL(smp_call_function_many);
450
451 /**
452  * smp_call_function(): Run a function on all other CPUs.
453  * @func: The function to run. This must be fast and non-blocking.
454  * @info: An arbitrary pointer to pass to the function.
455  * @wait: If true, wait (atomically) until function has completed
456  *        on other CPUs.
457  *
458  * Returns 0.
459  *
460  * If @wait is true, then returns once @func has returned; otherwise
461  * it returns just before the target cpu calls @func.
462  *
463  * You must not call this function with disabled interrupts or from a
464  * hardware interrupt handler or from a bottom half handler.
465  */
466 int smp_call_function(smp_call_func_t func, void *info, int wait)
467 {
468         preempt_disable();
469         smp_call_function_many(cpu_online_mask, func, info, wait);
470         preempt_enable();
471
472         return 0;
473 }
474 EXPORT_SYMBOL(smp_call_function);
475 #endif /* USE_GENERIC_SMP_HELPERS */
476
477 /* Setup configured maximum number of CPUs to activate */
478 unsigned int setup_max_cpus = NR_CPUS;
479 EXPORT_SYMBOL(setup_max_cpus);
480
481
482 /*
483  * Setup routine for controlling SMP activation
484  *
485  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
486  * activation entirely (the MPS table probe still happens, though).
487  *
488  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
489  * greater than 0, limits the maximum number of CPUs activated in
490  * SMP mode to <NUM>.
491  */
492
493 void __weak arch_disable_smp_support(void) { }
494
495 static int __init nosmp(char *str)
496 {
497         setup_max_cpus = 0;
498         arch_disable_smp_support();
499
500         return 0;
501 }
502
503 early_param("nosmp", nosmp);
504
505 /* this is hard limit */
506 static int __init nrcpus(char *str)
507 {
508         int nr_cpus;
509
510         get_option(&str, &nr_cpus);
511         if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
512                 nr_cpu_ids = nr_cpus;
513
514         return 0;
515 }
516
517 early_param("nr_cpus", nrcpus);
518
519 static int __init maxcpus(char *str)
520 {
521         get_option(&str, &setup_max_cpus);
522         if (setup_max_cpus == 0)
523                 arch_disable_smp_support();
524
525         return 0;
526 }
527
528 early_param("maxcpus", maxcpus);
529
530 /* Setup number of possible processor ids */
531 int nr_cpu_ids __read_mostly = NR_CPUS;
532 EXPORT_SYMBOL(nr_cpu_ids);
533
534 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
535 void __init setup_nr_cpu_ids(void)
536 {
537         nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
538 }
539
540 /* Called by boot processor to activate the rest. */
541 void __init smp_init(void)
542 {
543         unsigned int cpu;
544
545         idle_threads_init();
546
547         /* FIXME: This should be done in userspace --RR */
548         for_each_present_cpu(cpu) {
549                 if (num_online_cpus() >= setup_max_cpus)
550                         break;
551                 if (!cpu_online(cpu))
552                         cpu_up(cpu);
553         }
554
555         /* Any cleanup work */
556         printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
557         smp_cpus_done(setup_max_cpus);
558 }
559
560 /*
561  * Call a function on all processors.  May be used during early boot while
562  * early_boot_irqs_disabled is set.  Use local_irq_save/restore() instead
563  * of local_irq_disable/enable().
564  */
565 int on_each_cpu(void (*func) (void *info), void *info, int wait)
566 {
567         unsigned long flags;
568         int ret = 0;
569
570         preempt_disable();
571         ret = smp_call_function(func, info, wait);
572         local_irq_save(flags);
573         func(info);
574         local_irq_restore(flags);
575         preempt_enable();
576         return ret;
577 }
578 EXPORT_SYMBOL(on_each_cpu);
579
580 /**
581  * on_each_cpu_mask(): Run a function on processors specified by
582  * cpumask, which may include the local processor.
583  * @mask: The set of cpus to run on (only runs on online subset).
584  * @func: The function to run. This must be fast and non-blocking.
585  * @info: An arbitrary pointer to pass to the function.
586  * @wait: If true, wait (atomically) until function has completed
587  *        on other CPUs.
588  *
589  * If @wait is true, then returns once @func has returned.
590  *
591  * You must not call this function with disabled interrupts or
592  * from a hardware interrupt handler or from a bottom half handler.
593  */
594 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
595                         void *info, bool wait)
596 {
597         int cpu = get_cpu();
598
599         smp_call_function_many(mask, func, info, wait);
600         if (cpumask_test_cpu(cpu, mask)) {
601                 local_irq_disable();
602                 func(info);
603                 local_irq_enable();
604         }
605         put_cpu();
606 }
607 EXPORT_SYMBOL(on_each_cpu_mask);
608
609 /*
610  * on_each_cpu_cond(): Call a function on each processor for which
611  * the supplied function cond_func returns true, optionally waiting
612  * for all the required CPUs to finish. This may include the local
613  * processor.
614  * @cond_func:  A callback function that is passed a cpu id and
615  *              the the info parameter. The function is called
616  *              with preemption disabled. The function should
617  *              return a blooean value indicating whether to IPI
618  *              the specified CPU.
619  * @func:       The function to run on all applicable CPUs.
620  *              This must be fast and non-blocking.
621  * @info:       An arbitrary pointer to pass to both functions.
622  * @wait:       If true, wait (atomically) until function has
623  *              completed on other CPUs.
624  * @gfp_flags:  GFP flags to use when allocating the cpumask
625  *              used internally by the function.
626  *
627  * The function might sleep if the GFP flags indicates a non
628  * atomic allocation is allowed.
629  *
630  * Preemption is disabled to protect against CPUs going offline but not online.
631  * CPUs going online during the call will not be seen or sent an IPI.
632  *
633  * You must not call this function with disabled interrupts or
634  * from a hardware interrupt handler or from a bottom half handler.
635  */
636 void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
637                         smp_call_func_t func, void *info, bool wait,
638                         gfp_t gfp_flags)
639 {
640         cpumask_var_t cpus;
641         int cpu, ret;
642
643         might_sleep_if(gfp_flags & __GFP_WAIT);
644
645         if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
646                 preempt_disable();
647                 for_each_online_cpu(cpu)
648                         if (cond_func(cpu, info))
649                                 cpumask_set_cpu(cpu, cpus);
650                 on_each_cpu_mask(cpus, func, info, wait);
651                 preempt_enable();
652                 free_cpumask_var(cpus);
653         } else {
654                 /*
655                  * No free cpumask, bother. No matter, we'll
656                  * just have to IPI them one by one.
657                  */
658                 preempt_disable();
659                 for_each_online_cpu(cpu)
660                         if (cond_func(cpu, info)) {
661                                 ret = smp_call_function_single(cpu, func,
662                                                                 info, wait);
663                                 WARN_ON_ONCE(!ret);
664                         }
665                 preempt_enable();
666         }
667 }
668 EXPORT_SYMBOL(on_each_cpu_cond);
669
670 static void do_nothing(void *unused)
671 {
672 }
673
674 /**
675  * kick_all_cpus_sync - Force all cpus out of idle
676  *
677  * Used to synchronize the update of pm_idle function pointer. It's
678  * called after the pointer is updated and returns after the dummy
679  * callback function has been executed on all cpus. The execution of
680  * the function can only happen on the remote cpus after they have
681  * left the idle function which had been called via pm_idle function
682  * pointer. So it's guaranteed that nothing uses the previous pointer
683  * anymore.
684  */
685 void kick_all_cpus_sync(void)
686 {
687         /* Make sure the change is visible before we kick the cpus */
688         smp_mb();
689         smp_call_function(do_nothing, NULL, 1);
690 }
691 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);