9f1181375141eb58468fc308a9677c7f0e4a426f
[platform/kernel/linux-rpi.git] / kernel / smp.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Generic helpers for smp ipi calls
4  *
5  * (C) Jens Axboe <jens.axboe@oracle.com> 2008
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/irq_work.h>
11 #include <linux/rcupdate.h>
12 #include <linux/rculist.h>
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/percpu.h>
16 #include <linux/init.h>
17 #include <linux/gfp.h>
18 #include <linux/smp.h>
19 #include <linux/cpu.h>
20 #include <linux/sched.h>
21 #include <linux/sched/idle.h>
22 #include <linux/hypervisor.h>
23
24 #include "smpboot.h"
25
26 enum {
27         CSD_FLAG_LOCK           = 0x01,
28         CSD_FLAG_SYNCHRONOUS    = 0x02,
29 };
30
31 struct call_function_data {
32         call_single_data_t      __percpu *csd;
33         cpumask_var_t           cpumask;
34         cpumask_var_t           cpumask_ipi;
35 };
36
37 static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
38
39 static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
40
41 static void flush_smp_call_function_queue(bool warn_cpu_offline);
42
43 int smpcfd_prepare_cpu(unsigned int cpu)
44 {
45         struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
46
47         if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
48                                      cpu_to_node(cpu)))
49                 return -ENOMEM;
50         if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
51                                      cpu_to_node(cpu))) {
52                 free_cpumask_var(cfd->cpumask);
53                 return -ENOMEM;
54         }
55         cfd->csd = alloc_percpu(call_single_data_t);
56         if (!cfd->csd) {
57                 free_cpumask_var(cfd->cpumask);
58                 free_cpumask_var(cfd->cpumask_ipi);
59                 return -ENOMEM;
60         }
61
62         return 0;
63 }
64
65 int smpcfd_dead_cpu(unsigned int cpu)
66 {
67         struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
68
69         free_cpumask_var(cfd->cpumask);
70         free_cpumask_var(cfd->cpumask_ipi);
71         free_percpu(cfd->csd);
72         return 0;
73 }
74
75 int smpcfd_dying_cpu(unsigned int cpu)
76 {
77         /*
78          * The IPIs for the smp-call-function callbacks queued by other
79          * CPUs might arrive late, either due to hardware latencies or
80          * because this CPU disabled interrupts (inside stop-machine)
81          * before the IPIs were sent. So flush out any pending callbacks
82          * explicitly (without waiting for the IPIs to arrive), to
83          * ensure that the outgoing CPU doesn't go offline with work
84          * still pending.
85          */
86         flush_smp_call_function_queue(false);
87         irq_work_run();
88         return 0;
89 }
90
91 void __init call_function_init(void)
92 {
93         int i;
94
95         for_each_possible_cpu(i)
96                 init_llist_head(&per_cpu(call_single_queue, i));
97
98         smpcfd_prepare_cpu(smp_processor_id());
99 }
100
101 /*
102  * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
103  *
104  * For non-synchronous ipi calls the csd can still be in use by the
105  * previous function call. For multi-cpu calls its even more interesting
106  * as we'll have to ensure no other cpu is observing our csd.
107  */
108 static __always_inline void csd_lock_wait(call_single_data_t *csd)
109 {
110         smp_cond_load_acquire(&csd->flags, !(VAL & CSD_FLAG_LOCK));
111 }
112
113 static __always_inline void csd_lock(call_single_data_t *csd)
114 {
115         csd_lock_wait(csd);
116         csd->flags |= CSD_FLAG_LOCK;
117
118         /*
119          * prevent CPU from reordering the above assignment
120          * to ->flags with any subsequent assignments to other
121          * fields of the specified call_single_data_t structure:
122          */
123         smp_wmb();
124 }
125
126 static __always_inline void csd_unlock(call_single_data_t *csd)
127 {
128         WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
129
130         /*
131          * ensure we're all done before releasing data:
132          */
133         smp_store_release(&csd->flags, 0);
134 }
135
136 static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
137
138 extern void send_call_function_single_ipi(int cpu);
139
140 /*
141  * Insert a previously allocated call_single_data_t element
142  * for execution on the given CPU. data must already have
143  * ->func, ->info, and ->flags set.
144  */
145 static int generic_exec_single(int cpu, call_single_data_t *csd,
146                                smp_call_func_t func, void *info)
147 {
148         if (cpu == smp_processor_id()) {
149                 unsigned long flags;
150
151                 /*
152                  * We can unlock early even for the synchronous on-stack case,
153                  * since we're doing this from the same CPU..
154                  */
155                 csd_unlock(csd);
156                 local_irq_save(flags);
157                 func(info);
158                 local_irq_restore(flags);
159                 return 0;
160         }
161
162
163         if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
164                 csd_unlock(csd);
165                 return -ENXIO;
166         }
167
168         csd->func = func;
169         csd->info = info;
170
171         /*
172          * The list addition should be visible before sending the IPI
173          * handler locks the list to pull the entry off it because of
174          * normal cache coherency rules implied by spinlocks.
175          *
176          * If IPIs can go out of order to the cache coherency protocol
177          * in an architecture, sufficient synchronisation should be added
178          * to arch code to make it appear to obey cache coherency WRT
179          * locking and barrier primitives. Generic code isn't really
180          * equipped to do the right thing...
181          */
182         if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
183                 send_call_function_single_ipi(cpu);
184
185         return 0;
186 }
187
188 /**
189  * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
190  *
191  * Invoked by arch to handle an IPI for call function single.
192  * Must be called with interrupts disabled.
193  */
194 void generic_smp_call_function_single_interrupt(void)
195 {
196         flush_smp_call_function_queue(true);
197
198         /*
199          * Handle irq works queued remotely by irq_work_queue_on().
200          * Smp functions above are typically synchronous so they
201          * better run first since some other CPUs may be busy waiting
202          * for them.
203          */
204         irq_work_run();
205 }
206
207 /**
208  * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
209  *
210  * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
211  *                    offline CPU. Skip this check if set to 'false'.
212  *
213  * Flush any pending smp-call-function callbacks queued on this CPU. This is
214  * invoked by the generic IPI handler, as well as by a CPU about to go offline,
215  * to ensure that all pending IPI callbacks are run before it goes completely
216  * offline.
217  *
218  * Loop through the call_single_queue and run all the queued callbacks.
219  * Must be called with interrupts disabled.
220  */
221 static void flush_smp_call_function_queue(bool warn_cpu_offline)
222 {
223         call_single_data_t *csd, *csd_next;
224         struct llist_node *entry, *prev;
225         struct llist_head *head;
226         static bool warned;
227
228         lockdep_assert_irqs_disabled();
229
230         head = this_cpu_ptr(&call_single_queue);
231         entry = llist_del_all(head);
232         entry = llist_reverse_order(entry);
233
234         /* There shouldn't be any pending callbacks on an offline CPU. */
235         if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
236                      !warned && !llist_empty(head))) {
237                 warned = true;
238                 WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
239
240                 /*
241                  * We don't have to use the _safe() variant here
242                  * because we are not invoking the IPI handlers yet.
243                  */
244                 llist_for_each_entry(csd, entry, llist)
245                         pr_warn("IPI callback %pS sent to offline CPU\n",
246                                 csd->func);
247         }
248
249         /*
250          * First; run all SYNC callbacks, people are waiting for us.
251          */
252         prev = NULL;
253         llist_for_each_entry_safe(csd, csd_next, entry, llist) {
254                 smp_call_func_t func = csd->func;
255                 void *info = csd->info;
256
257                 /* Do we wait until *after* callback? */
258                 if (csd->flags & CSD_FLAG_SYNCHRONOUS) {
259                         if (prev) {
260                                 prev->next = &csd_next->llist;
261                         } else {
262                                 entry = &csd_next->llist;
263                         }
264                         func(info);
265                         csd_unlock(csd);
266                 } else {
267                         prev = &csd->llist;
268                 }
269         }
270
271         /*
272          * Second; run all !SYNC callbacks.
273          */
274         llist_for_each_entry_safe(csd, csd_next, entry, llist) {
275                 smp_call_func_t func = csd->func;
276                 void *info = csd->info;
277
278                 csd_unlock(csd);
279                 func(info);
280         }
281 }
282
283 void flush_smp_call_function_from_idle(void)
284 {
285         unsigned long flags;
286
287         if (llist_empty(this_cpu_ptr(&call_single_queue)))
288                 return;
289
290         local_irq_save(flags);
291         flush_smp_call_function_queue(true);
292         local_irq_restore(flags);
293 }
294
295 /*
296  * smp_call_function_single - Run a function on a specific CPU
297  * @func: The function to run. This must be fast and non-blocking.
298  * @info: An arbitrary pointer to pass to the function.
299  * @wait: If true, wait until function has completed on other CPUs.
300  *
301  * Returns 0 on success, else a negative status code.
302  */
303 int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
304                              int wait)
305 {
306         call_single_data_t *csd;
307         call_single_data_t csd_stack = {
308                 .flags = CSD_FLAG_LOCK | CSD_FLAG_SYNCHRONOUS,
309         };
310         int this_cpu;
311         int err;
312
313         /*
314          * prevent preemption and reschedule on another processor,
315          * as well as CPU removal
316          */
317         this_cpu = get_cpu();
318
319         /*
320          * Can deadlock when called with interrupts disabled.
321          * We allow cpu's that are not yet online though, as no one else can
322          * send smp call function interrupt to this cpu and as such deadlocks
323          * can't happen.
324          */
325         WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
326                      && !oops_in_progress);
327
328         /*
329          * When @wait we can deadlock when we interrupt between llist_add() and
330          * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
331          * csd_lock() on because the interrupt context uses the same csd
332          * storage.
333          */
334         WARN_ON_ONCE(!in_task());
335
336         csd = &csd_stack;
337         if (!wait) {
338                 csd = this_cpu_ptr(&csd_data);
339                 csd_lock(csd);
340         }
341
342         err = generic_exec_single(cpu, csd, func, info);
343
344         if (wait)
345                 csd_lock_wait(csd);
346
347         put_cpu();
348
349         return err;
350 }
351 EXPORT_SYMBOL(smp_call_function_single);
352
353 /**
354  * smp_call_function_single_async(): Run an asynchronous function on a
355  *                               specific CPU.
356  * @cpu: The CPU to run on.
357  * @csd: Pre-allocated and setup data structure
358  *
359  * Like smp_call_function_single(), but the call is asynchonous and
360  * can thus be done from contexts with disabled interrupts.
361  *
362  * The caller passes his own pre-allocated data structure
363  * (ie: embedded in an object) and is responsible for synchronizing it
364  * such that the IPIs performed on the @csd are strictly serialized.
365  *
366  * If the function is called with one csd which has not yet been
367  * processed by previous call to smp_call_function_single_async(), the
368  * function will return immediately with -EBUSY showing that the csd
369  * object is still in progress.
370  *
371  * NOTE: Be careful, there is unfortunately no current debugging facility to
372  * validate the correctness of this serialization.
373  */
374 int smp_call_function_single_async(int cpu, call_single_data_t *csd)
375 {
376         int err = 0;
377
378         preempt_disable();
379
380         if (csd->flags & CSD_FLAG_LOCK) {
381                 err = -EBUSY;
382                 goto out;
383         }
384
385         csd->flags = CSD_FLAG_LOCK;
386         smp_wmb();
387
388         err = generic_exec_single(cpu, csd, csd->func, csd->info);
389
390 out:
391         preempt_enable();
392
393         return err;
394 }
395 EXPORT_SYMBOL_GPL(smp_call_function_single_async);
396
397 /*
398  * smp_call_function_any - Run a function on any of the given cpus
399  * @mask: The mask of cpus it can run on.
400  * @func: The function to run. This must be fast and non-blocking.
401  * @info: An arbitrary pointer to pass to the function.
402  * @wait: If true, wait until function has completed.
403  *
404  * Returns 0 on success, else a negative status code (if no cpus were online).
405  *
406  * Selection preference:
407  *      1) current cpu if in @mask
408  *      2) any cpu of current node if in @mask
409  *      3) any other online cpu in @mask
410  */
411 int smp_call_function_any(const struct cpumask *mask,
412                           smp_call_func_t func, void *info, int wait)
413 {
414         unsigned int cpu;
415         const struct cpumask *nodemask;
416         int ret;
417
418         /* Try for same CPU (cheapest) */
419         cpu = get_cpu();
420         if (cpumask_test_cpu(cpu, mask))
421                 goto call;
422
423         /* Try for same node. */
424         nodemask = cpumask_of_node(cpu_to_node(cpu));
425         for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
426              cpu = cpumask_next_and(cpu, nodemask, mask)) {
427                 if (cpu_online(cpu))
428                         goto call;
429         }
430
431         /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
432         cpu = cpumask_any_and(mask, cpu_online_mask);
433 call:
434         ret = smp_call_function_single(cpu, func, info, wait);
435         put_cpu();
436         return ret;
437 }
438 EXPORT_SYMBOL_GPL(smp_call_function_any);
439
440 static void smp_call_function_many_cond(const struct cpumask *mask,
441                                         smp_call_func_t func, void *info,
442                                         bool wait, smp_cond_func_t cond_func)
443 {
444         struct call_function_data *cfd;
445         int cpu, next_cpu, this_cpu = smp_processor_id();
446
447         /*
448          * Can deadlock when called with interrupts disabled.
449          * We allow cpu's that are not yet online though, as no one else can
450          * send smp call function interrupt to this cpu and as such deadlocks
451          * can't happen.
452          */
453         WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
454                      && !oops_in_progress && !early_boot_irqs_disabled);
455
456         /*
457          * When @wait we can deadlock when we interrupt between llist_add() and
458          * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
459          * csd_lock() on because the interrupt context uses the same csd
460          * storage.
461          */
462         WARN_ON_ONCE(!in_task());
463
464         /* Try to fastpath.  So, what's a CPU they want? Ignoring this one. */
465         cpu = cpumask_first_and(mask, cpu_online_mask);
466         if (cpu == this_cpu)
467                 cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
468
469         /* No online cpus?  We're done. */
470         if (cpu >= nr_cpu_ids)
471                 return;
472
473         /* Do we have another CPU which isn't us? */
474         next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
475         if (next_cpu == this_cpu)
476                 next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
477
478         /* Fastpath: do that cpu by itself. */
479         if (next_cpu >= nr_cpu_ids) {
480                 if (!cond_func || cond_func(cpu, info))
481                         smp_call_function_single(cpu, func, info, wait);
482                 return;
483         }
484
485         cfd = this_cpu_ptr(&cfd_data);
486
487         cpumask_and(cfd->cpumask, mask, cpu_online_mask);
488         __cpumask_clear_cpu(this_cpu, cfd->cpumask);
489
490         /* Some callers race with other cpus changing the passed mask */
491         if (unlikely(!cpumask_weight(cfd->cpumask)))
492                 return;
493
494         cpumask_clear(cfd->cpumask_ipi);
495         for_each_cpu(cpu, cfd->cpumask) {
496                 call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
497
498                 if (cond_func && !cond_func(cpu, info))
499                         continue;
500
501                 csd_lock(csd);
502                 if (wait)
503                         csd->flags |= CSD_FLAG_SYNCHRONOUS;
504                 csd->func = func;
505                 csd->info = info;
506                 if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
507                         __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
508         }
509
510         /* Send a message to all CPUs in the map */
511         arch_send_call_function_ipi_mask(cfd->cpumask_ipi);
512
513         if (wait) {
514                 for_each_cpu(cpu, cfd->cpumask) {
515                         call_single_data_t *csd;
516
517                         csd = per_cpu_ptr(cfd->csd, cpu);
518                         csd_lock_wait(csd);
519                 }
520         }
521 }
522
523 /**
524  * smp_call_function_many(): Run a function on a set of other CPUs.
525  * @mask: The set of cpus to run on (only runs on online subset).
526  * @func: The function to run. This must be fast and non-blocking.
527  * @info: An arbitrary pointer to pass to the function.
528  * @wait: If true, wait (atomically) until function has completed
529  *        on other CPUs.
530  *
531  * If @wait is true, then returns once @func has returned.
532  *
533  * You must not call this function with disabled interrupts or from a
534  * hardware interrupt handler or from a bottom half handler. Preemption
535  * must be disabled when calling this function.
536  */
537 void smp_call_function_many(const struct cpumask *mask,
538                             smp_call_func_t func, void *info, bool wait)
539 {
540         smp_call_function_many_cond(mask, func, info, wait, NULL);
541 }
542 EXPORT_SYMBOL(smp_call_function_many);
543
544 /**
545  * smp_call_function(): Run a function on all other CPUs.
546  * @func: The function to run. This must be fast and non-blocking.
547  * @info: An arbitrary pointer to pass to the function.
548  * @wait: If true, wait (atomically) until function has completed
549  *        on other CPUs.
550  *
551  * Returns 0.
552  *
553  * If @wait is true, then returns once @func has returned; otherwise
554  * it returns just before the target cpu calls @func.
555  *
556  * You must not call this function with disabled interrupts or from a
557  * hardware interrupt handler or from a bottom half handler.
558  */
559 void smp_call_function(smp_call_func_t func, void *info, int wait)
560 {
561         preempt_disable();
562         smp_call_function_many(cpu_online_mask, func, info, wait);
563         preempt_enable();
564 }
565 EXPORT_SYMBOL(smp_call_function);
566
567 /* Setup configured maximum number of CPUs to activate */
568 unsigned int setup_max_cpus = NR_CPUS;
569 EXPORT_SYMBOL(setup_max_cpus);
570
571
572 /*
573  * Setup routine for controlling SMP activation
574  *
575  * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
576  * activation entirely (the MPS table probe still happens, though).
577  *
578  * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
579  * greater than 0, limits the maximum number of CPUs activated in
580  * SMP mode to <NUM>.
581  */
582
583 void __weak arch_disable_smp_support(void) { }
584
585 static int __init nosmp(char *str)
586 {
587         setup_max_cpus = 0;
588         arch_disable_smp_support();
589
590         return 0;
591 }
592
593 early_param("nosmp", nosmp);
594
595 /* this is hard limit */
596 static int __init nrcpus(char *str)
597 {
598         int nr_cpus;
599
600         get_option(&str, &nr_cpus);
601         if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
602                 nr_cpu_ids = nr_cpus;
603
604         return 0;
605 }
606
607 early_param("nr_cpus", nrcpus);
608
609 static int __init maxcpus(char *str)
610 {
611         get_option(&str, &setup_max_cpus);
612         if (setup_max_cpus == 0)
613                 arch_disable_smp_support();
614
615         return 0;
616 }
617
618 early_param("maxcpus", maxcpus);
619
620 /* Setup number of possible processor ids */
621 unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
622 EXPORT_SYMBOL(nr_cpu_ids);
623
624 /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
625 void __init setup_nr_cpu_ids(void)
626 {
627         nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
628 }
629
630 /* Called by boot processor to activate the rest. */
631 void __init smp_init(void)
632 {
633         int num_nodes, num_cpus;
634
635         idle_threads_init();
636         cpuhp_threads_init();
637
638         pr_info("Bringing up secondary CPUs ...\n");
639
640         bringup_nonboot_cpus(setup_max_cpus);
641
642         num_nodes = num_online_nodes();
643         num_cpus  = num_online_cpus();
644         pr_info("Brought up %d node%s, %d CPU%s\n",
645                 num_nodes, (num_nodes > 1 ? "s" : ""),
646                 num_cpus,  (num_cpus  > 1 ? "s" : ""));
647
648         /* Any cleanup work */
649         smp_cpus_done(setup_max_cpus);
650 }
651
652 /*
653  * Call a function on all processors.  May be used during early boot while
654  * early_boot_irqs_disabled is set.  Use local_irq_save/restore() instead
655  * of local_irq_disable/enable().
656  */
657 void on_each_cpu(void (*func) (void *info), void *info, int wait)
658 {
659         unsigned long flags;
660
661         preempt_disable();
662         smp_call_function(func, info, wait);
663         local_irq_save(flags);
664         func(info);
665         local_irq_restore(flags);
666         preempt_enable();
667 }
668 EXPORT_SYMBOL(on_each_cpu);
669
670 /**
671  * on_each_cpu_mask(): Run a function on processors specified by
672  * cpumask, which may include the local processor.
673  * @mask: The set of cpus to run on (only runs on online subset).
674  * @func: The function to run. This must be fast and non-blocking.
675  * @info: An arbitrary pointer to pass to the function.
676  * @wait: If true, wait (atomically) until function has completed
677  *        on other CPUs.
678  *
679  * If @wait is true, then returns once @func has returned.
680  *
681  * You must not call this function with disabled interrupts or from a
682  * hardware interrupt handler or from a bottom half handler.  The
683  * exception is that it may be used during early boot while
684  * early_boot_irqs_disabled is set.
685  */
686 void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
687                         void *info, bool wait)
688 {
689         int cpu = get_cpu();
690
691         smp_call_function_many(mask, func, info, wait);
692         if (cpumask_test_cpu(cpu, mask)) {
693                 unsigned long flags;
694                 local_irq_save(flags);
695                 func(info);
696                 local_irq_restore(flags);
697         }
698         put_cpu();
699 }
700 EXPORT_SYMBOL(on_each_cpu_mask);
701
702 /*
703  * on_each_cpu_cond(): Call a function on each processor for which
704  * the supplied function cond_func returns true, optionally waiting
705  * for all the required CPUs to finish. This may include the local
706  * processor.
707  * @cond_func:  A callback function that is passed a cpu id and
708  *              the the info parameter. The function is called
709  *              with preemption disabled. The function should
710  *              return a blooean value indicating whether to IPI
711  *              the specified CPU.
712  * @func:       The function to run on all applicable CPUs.
713  *              This must be fast and non-blocking.
714  * @info:       An arbitrary pointer to pass to both functions.
715  * @wait:       If true, wait (atomically) until function has
716  *              completed on other CPUs.
717  *
718  * Preemption is disabled to protect against CPUs going offline but not online.
719  * CPUs going online during the call will not be seen or sent an IPI.
720  *
721  * You must not call this function with disabled interrupts or
722  * from a hardware interrupt handler or from a bottom half handler.
723  */
724 void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
725                            void *info, bool wait, const struct cpumask *mask)
726 {
727         int cpu = get_cpu();
728
729         smp_call_function_many_cond(mask, func, info, wait, cond_func);
730         if (cpumask_test_cpu(cpu, mask) && cond_func(cpu, info)) {
731                 unsigned long flags;
732
733                 local_irq_save(flags);
734                 func(info);
735                 local_irq_restore(flags);
736         }
737         put_cpu();
738 }
739 EXPORT_SYMBOL(on_each_cpu_cond_mask);
740
741 void on_each_cpu_cond(smp_cond_func_t cond_func, smp_call_func_t func,
742                       void *info, bool wait)
743 {
744         on_each_cpu_cond_mask(cond_func, func, info, wait, cpu_online_mask);
745 }
746 EXPORT_SYMBOL(on_each_cpu_cond);
747
748 static void do_nothing(void *unused)
749 {
750 }
751
752 /**
753  * kick_all_cpus_sync - Force all cpus out of idle
754  *
755  * Used to synchronize the update of pm_idle function pointer. It's
756  * called after the pointer is updated and returns after the dummy
757  * callback function has been executed on all cpus. The execution of
758  * the function can only happen on the remote cpus after they have
759  * left the idle function which had been called via pm_idle function
760  * pointer. So it's guaranteed that nothing uses the previous pointer
761  * anymore.
762  */
763 void kick_all_cpus_sync(void)
764 {
765         /* Make sure the change is visible before we kick the cpus */
766         smp_mb();
767         smp_call_function(do_nothing, NULL, 1);
768 }
769 EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
770
771 /**
772  * wake_up_all_idle_cpus - break all cpus out of idle
773  * wake_up_all_idle_cpus try to break all cpus which is in idle state even
774  * including idle polling cpus, for non-idle cpus, we will do nothing
775  * for them.
776  */
777 void wake_up_all_idle_cpus(void)
778 {
779         int cpu;
780
781         preempt_disable();
782         for_each_online_cpu(cpu) {
783                 if (cpu == smp_processor_id())
784                         continue;
785
786                 wake_up_if_idle(cpu);
787         }
788         preempt_enable();
789 }
790 EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
791
792 /**
793  * smp_call_on_cpu - Call a function on a specific cpu
794  *
795  * Used to call a function on a specific cpu and wait for it to return.
796  * Optionally make sure the call is done on a specified physical cpu via vcpu
797  * pinning in order to support virtualized environments.
798  */
799 struct smp_call_on_cpu_struct {
800         struct work_struct      work;
801         struct completion       done;
802         int                     (*func)(void *);
803         void                    *data;
804         int                     ret;
805         int                     cpu;
806 };
807
808 static void smp_call_on_cpu_callback(struct work_struct *work)
809 {
810         struct smp_call_on_cpu_struct *sscs;
811
812         sscs = container_of(work, struct smp_call_on_cpu_struct, work);
813         if (sscs->cpu >= 0)
814                 hypervisor_pin_vcpu(sscs->cpu);
815         sscs->ret = sscs->func(sscs->data);
816         if (sscs->cpu >= 0)
817                 hypervisor_pin_vcpu(-1);
818
819         complete(&sscs->done);
820 }
821
822 int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
823 {
824         struct smp_call_on_cpu_struct sscs = {
825                 .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
826                 .func = func,
827                 .data = par,
828                 .cpu  = phys ? cpu : -1,
829         };
830
831         INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
832
833         if (cpu >= nr_cpu_ids || !cpu_online(cpu))
834                 return -ENXIO;
835
836         queue_work_on(cpu, system_wq, &sscs.work);
837         wait_for_completion(&sscs.done);
838
839         return sscs.ret;
840 }
841 EXPORT_SYMBOL_GPL(smp_call_on_cpu);