Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Generic helpers for smp ipi calls |
| 3 | * |
| 4 | * (C) Jens Axboe <jens.axboe@oracle.com> 2008 |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 5 | */ |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 6 | #include <linux/rcupdate.h> |
Linus Torvalds | 59190f421 | 2008-07-15 14:02:33 -0700 | [diff] [blame] | 7 | #include <linux/rculist.h> |
Ingo Molnar | 641cd4c | 2009-03-13 10:47:34 +0100 | [diff] [blame] | 8 | #include <linux/kernel.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 9 | #include <linux/export.h> |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 10 | #include <linux/percpu.h> |
| 11 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 13 | #include <linux/smp.h> |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 14 | #include <linux/cpu.h> |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 15 | |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 16 | #include "smpboot.h" |
| 17 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 18 | enum { |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 19 | CSD_FLAG_LOCK = 0x01, |
Jens Axboe | c84a83e | 2013-05-17 09:58:43 +0200 | [diff] [blame] | 20 | CSD_FLAG_WAIT = 0x02, |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
| 23 | struct call_function_data { |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 24 | struct call_single_data __percpu *csd; |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 25 | cpumask_var_t cpumask; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Milton Miller | e03bcb6 | 2010-01-18 13:00:51 +1100 | [diff] [blame] | 28 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data); |
| 29 | |
Christoph Hellwig | 6897fc2 | 2014-01-30 15:45:47 -0800 | [diff] [blame] | 30 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 31 | |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 32 | static void flush_smp_call_function_queue(bool warn_cpu_offline); |
| 33 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 34 | static int |
| 35 | hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu) |
| 36 | { |
| 37 | long cpu = (long)hcpu; |
| 38 | struct call_function_data *cfd = &per_cpu(cfd_data, cpu); |
| 39 | |
| 40 | switch (action) { |
| 41 | case CPU_UP_PREPARE: |
| 42 | case CPU_UP_PREPARE_FROZEN: |
Yinghai Lu | eaa9584 | 2009-06-06 14:51:36 -0700 | [diff] [blame] | 43 | if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL, |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 44 | cpu_to_node(cpu))) |
Akinobu Mita | 80b5184 | 2010-05-26 14:43:32 -0700 | [diff] [blame] | 45 | return notifier_from_errno(-ENOMEM); |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 46 | cfd->csd = alloc_percpu(struct call_single_data); |
| 47 | if (!cfd->csd) { |
| 48 | free_cpumask_var(cfd->cpumask); |
| 49 | return notifier_from_errno(-ENOMEM); |
| 50 | } |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 51 | break; |
| 52 | |
Xiao Guangrong | 69dd647 | 2009-08-06 15:07:29 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_HOTPLUG_CPU |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 54 | case CPU_UP_CANCELED: |
| 55 | case CPU_UP_CANCELED_FROZEN: |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 56 | /* Fall-through to the CPU_DEAD[_FROZEN] case. */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 57 | |
| 58 | case CPU_DEAD: |
| 59 | case CPU_DEAD_FROZEN: |
| 60 | free_cpumask_var(cfd->cpumask); |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 61 | free_percpu(cfd->csd); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 62 | break; |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 63 | |
| 64 | case CPU_DYING: |
| 65 | case CPU_DYING_FROZEN: |
| 66 | /* |
| 67 | * The IPIs for the smp-call-function callbacks queued by other |
| 68 | * CPUs might arrive late, either due to hardware latencies or |
| 69 | * because this CPU disabled interrupts (inside stop-machine) |
| 70 | * before the IPIs were sent. So flush out any pending callbacks |
| 71 | * explicitly (without waiting for the IPIs to arrive), to |
| 72 | * ensure that the outgoing CPU doesn't go offline with work |
| 73 | * still pending. |
| 74 | */ |
| 75 | flush_smp_call_function_queue(false); |
| 76 | break; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 77 | #endif |
| 78 | }; |
| 79 | |
| 80 | return NOTIFY_OK; |
| 81 | } |
| 82 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 83 | static struct notifier_block hotplug_cfd_notifier = { |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 84 | .notifier_call = hotplug_cfd, |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
Takao Indoh | d8ad7d1 | 2011-03-29 12:35:04 -0400 | [diff] [blame] | 87 | void __init call_function_init(void) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 88 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 89 | void *cpu = (void *)(long)smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 90 | int i; |
| 91 | |
Christoph Hellwig | 6897fc2 | 2014-01-30 15:45:47 -0800 | [diff] [blame] | 92 | for_each_possible_cpu(i) |
| 93 | init_llist_head(&per_cpu(call_single_queue, i)); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 94 | |
| 95 | hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu); |
| 96 | register_cpu_notifier(&hotplug_cfd_notifier); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 97 | } |
| 98 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 99 | /* |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 100 | * csd_lock/csd_unlock used to serialize access to per-cpu csd resources |
| 101 | * |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 102 | * For non-synchronous ipi calls the csd can still be in use by the |
| 103 | * previous function call. For multi-cpu calls its even more interesting |
| 104 | * as we'll have to ensure no other cpu is observing our csd. |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 105 | */ |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 106 | static void csd_lock_wait(struct call_single_data *csd) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 107 | { |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 108 | while (csd->flags & CSD_FLAG_LOCK) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 109 | cpu_relax(); |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 112 | static void csd_lock(struct call_single_data *csd) |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 113 | { |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 114 | csd_lock_wait(csd); |
| 115 | csd->flags |= CSD_FLAG_LOCK; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 116 | |
| 117 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 118 | * prevent CPU from reordering the above assignment |
| 119 | * to ->flags with any subsequent assignments to other |
| 120 | * fields of the specified call_single_data structure: |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 121 | */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 122 | smp_mb(); |
| 123 | } |
| 124 | |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 125 | static void csd_unlock(struct call_single_data *csd) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 126 | { |
Jens Axboe | c84a83e | 2013-05-17 09:58:43 +0200 | [diff] [blame] | 127 | WARN_ON((csd->flags & CSD_FLAG_WAIT) && !(csd->flags & CSD_FLAG_LOCK)); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 128 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 129 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 130 | * ensure we're all done before releasing data: |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 131 | */ |
| 132 | smp_mb(); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 133 | |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 134 | csd->flags &= ~CSD_FLAG_LOCK; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Frederic Weisbecker | 8b28499 | 2014-02-24 16:39:58 +0100 | [diff] [blame] | 137 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); |
| 138 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 139 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 140 | * Insert a previously allocated call_single_data element |
| 141 | * for execution on the given CPU. data must already have |
| 142 | * ->func, ->info, and ->flags set. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 143 | */ |
Frederic Weisbecker | 8b28499 | 2014-02-24 16:39:58 +0100 | [diff] [blame] | 144 | static int generic_exec_single(int cpu, struct call_single_data *csd, |
| 145 | smp_call_func_t func, void *info, int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 146 | { |
Frederic Weisbecker | 8b28499 | 2014-02-24 16:39:58 +0100 | [diff] [blame] | 147 | struct call_single_data csd_stack = { .flags = 0 }; |
| 148 | unsigned long flags; |
| 149 | |
| 150 | |
| 151 | if (cpu == smp_processor_id()) { |
| 152 | local_irq_save(flags); |
| 153 | func(info); |
| 154 | local_irq_restore(flags); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) |
| 160 | return -ENXIO; |
| 161 | |
| 162 | |
| 163 | if (!csd) { |
| 164 | csd = &csd_stack; |
| 165 | if (!wait) |
| 166 | csd = &__get_cpu_var(csd_data); |
| 167 | } |
| 168 | |
| 169 | csd_lock(csd); |
| 170 | |
| 171 | csd->func = func; |
| 172 | csd->info = info; |
| 173 | |
Jens Axboe | c84a83e | 2013-05-17 09:58:43 +0200 | [diff] [blame] | 174 | if (wait) |
| 175 | csd->flags |= CSD_FLAG_WAIT; |
| 176 | |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 177 | /* |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 178 | * The list addition should be visible before sending the IPI |
| 179 | * handler locks the list to pull the entry off it because of |
| 180 | * normal cache coherency rules implied by spinlocks. |
| 181 | * |
| 182 | * If IPIs can go out of order to the cache coherency protocol |
| 183 | * in an architecture, sufficient synchronisation should be added |
| 184 | * to arch code to make it appear to obey cache coherency WRT |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 185 | * locking and barrier primitives. Generic code isn't really |
| 186 | * equipped to do the right thing... |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 187 | */ |
Christoph Hellwig | 6897fc2 | 2014-01-30 15:45:47 -0800 | [diff] [blame] | 188 | if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu))) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 189 | arch_send_call_function_single_ipi(cpu); |
| 190 | |
| 191 | if (wait) |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 192 | csd_lock_wait(csd); |
Frederic Weisbecker | 8b28499 | 2014-02-24 16:39:58 +0100 | [diff] [blame] | 193 | |
| 194 | return 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 195 | } |
| 196 | |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 197 | /** |
| 198 | * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks |
| 199 | * |
| 200 | * Invoked by arch to handle an IPI for call function single. |
| 201 | * Must be called with interrupts disabled. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 202 | */ |
| 203 | void generic_smp_call_function_single_interrupt(void) |
| 204 | { |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 205 | flush_smp_call_function_queue(true); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * flush_smp_call_function_queue - Flush pending smp-call-function callbacks |
| 210 | * |
| 211 | * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an |
| 212 | * offline CPU. Skip this check if set to 'false'. |
| 213 | * |
| 214 | * Flush any pending smp-call-function callbacks queued on this CPU. This is |
| 215 | * invoked by the generic IPI handler, as well as by a CPU about to go offline, |
| 216 | * to ensure that all pending IPI callbacks are run before it goes completely |
| 217 | * offline. |
| 218 | * |
| 219 | * Loop through the call_single_queue and run all the queued callbacks. |
| 220 | * Must be called with interrupts disabled. |
| 221 | */ |
| 222 | static void flush_smp_call_function_queue(bool warn_cpu_offline) |
| 223 | { |
| 224 | struct llist_head *head; |
Jan Kara | 5fd7759 | 2014-02-24 16:39:55 +0100 | [diff] [blame] | 225 | struct llist_node *entry; |
| 226 | struct call_single_data *csd, *csd_next; |
Srivatsa S. Bhat | a219ccf | 2014-06-06 14:37:05 -0700 | [diff] [blame] | 227 | static bool warned; |
| 228 | |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 229 | WARN_ON(!irqs_disabled()); |
| 230 | |
| 231 | head = &__get_cpu_var(call_single_queue); |
| 232 | entry = llist_del_all(head); |
Srivatsa S. Bhat | a219ccf | 2014-06-06 14:37:05 -0700 | [diff] [blame] | 233 | entry = llist_reverse_order(entry); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 234 | |
Srivatsa S. Bhat | 8d056c4 | 2014-06-23 13:22:02 -0700 | [diff] [blame^] | 235 | /* There shouldn't be any pending callbacks on an offline CPU. */ |
| 236 | if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) && |
| 237 | !warned && !llist_empty(head))) { |
Srivatsa S. Bhat | a219ccf | 2014-06-06 14:37:05 -0700 | [diff] [blame] | 238 | warned = true; |
| 239 | WARN(1, "IPI on offline CPU %d\n", smp_processor_id()); |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 240 | |
Srivatsa S. Bhat | a219ccf | 2014-06-06 14:37:05 -0700 | [diff] [blame] | 241 | /* |
| 242 | * We don't have to use the _safe() variant here |
| 243 | * because we are not invoking the IPI handlers yet. |
| 244 | */ |
| 245 | llist_for_each_entry(csd, entry, llist) |
| 246 | pr_warn("IPI callback %pS sent to offline CPU\n", |
| 247 | csd->func); |
| 248 | } |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 249 | |
Jan Kara | 5fd7759 | 2014-02-24 16:39:55 +0100 | [diff] [blame] | 250 | llist_for_each_entry_safe(csd, csd_next, entry, llist) { |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 251 | csd->func(csd->info); |
Xie XiuQi | 4659196 | 2013-07-30 11:06:09 +0800 | [diff] [blame] | 252 | csd_unlock(csd); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * smp_call_function_single - Run a function on a specific CPU |
| 258 | * @func: The function to run. This must be fast and non-blocking. |
| 259 | * @info: An arbitrary pointer to pass to the function. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 260 | * @wait: If true, wait until function has completed on other CPUs. |
| 261 | * |
Sheng Yang | 72f279b | 2009-10-22 19:19:34 +0800 | [diff] [blame] | 262 | * Returns 0 on success, else a negative status code. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 263 | */ |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 264 | int smp_call_function_single(int cpu, smp_call_func_t func, void *info, |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 265 | int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 266 | { |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 267 | int this_cpu; |
Frederic Weisbecker | 8b28499 | 2014-02-24 16:39:58 +0100 | [diff] [blame] | 268 | int err; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 269 | |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 270 | /* |
| 271 | * prevent preemption and reschedule on another processor, |
| 272 | * as well as CPU removal |
| 273 | */ |
| 274 | this_cpu = get_cpu(); |
| 275 | |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 276 | /* |
| 277 | * Can deadlock when called with interrupts disabled. |
| 278 | * We allow cpu's that are not yet online though, as no one else can |
| 279 | * send smp call function interrupt to this cpu and as such deadlocks |
| 280 | * can't happen. |
| 281 | */ |
| 282 | WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() |
| 283 | && !oops_in_progress); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 284 | |
Frederic Weisbecker | 8b28499 | 2014-02-24 16:39:58 +0100 | [diff] [blame] | 285 | err = generic_exec_single(cpu, NULL, func, info, wait); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 286 | |
| 287 | put_cpu(); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 288 | |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 289 | return err; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 290 | } |
| 291 | EXPORT_SYMBOL(smp_call_function_single); |
| 292 | |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 293 | /** |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 294 | * smp_call_function_single_async(): Run an asynchronous function on a |
| 295 | * specific CPU. |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 296 | * @cpu: The CPU to run on. |
| 297 | * @csd: Pre-allocated and setup data structure |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 298 | * |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 299 | * Like smp_call_function_single(), but the call is asynchonous and |
| 300 | * can thus be done from contexts with disabled interrupts. |
| 301 | * |
| 302 | * The caller passes his own pre-allocated data structure |
| 303 | * (ie: embedded in an object) and is responsible for synchronizing it |
| 304 | * such that the IPIs performed on the @csd are strictly serialized. |
| 305 | * |
| 306 | * NOTE: Be careful, there is unfortunately no current debugging facility to |
| 307 | * validate the correctness of this serialization. |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 308 | */ |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 309 | int smp_call_function_single_async(int cpu, struct call_single_data *csd) |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 310 | { |
| 311 | int err = 0; |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 312 | |
Frederic Weisbecker | fce8ad1 | 2014-02-24 16:40:01 +0100 | [diff] [blame] | 313 | preempt_disable(); |
| 314 | err = generic_exec_single(cpu, csd, csd->func, csd->info, 0); |
| 315 | preempt_enable(); |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 316 | |
| 317 | return err; |
| 318 | } |
Frederic Weisbecker | c46fff2 | 2014-02-24 16:40:02 +0100 | [diff] [blame] | 319 | EXPORT_SYMBOL_GPL(smp_call_function_single_async); |
Frederic Weisbecker | d7877c0 | 2014-02-24 16:39:59 +0100 | [diff] [blame] | 320 | |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 321 | /* |
| 322 | * smp_call_function_any - Run a function on any of the given cpus |
| 323 | * @mask: The mask of cpus it can run on. |
| 324 | * @func: The function to run. This must be fast and non-blocking. |
| 325 | * @info: An arbitrary pointer to pass to the function. |
| 326 | * @wait: If true, wait until function has completed. |
| 327 | * |
| 328 | * Returns 0 on success, else a negative status code (if no cpus were online). |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 329 | * |
| 330 | * Selection preference: |
| 331 | * 1) current cpu if in @mask |
| 332 | * 2) any cpu of current node if in @mask |
| 333 | * 3) any other online cpu in @mask |
| 334 | */ |
| 335 | int smp_call_function_any(const struct cpumask *mask, |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 336 | smp_call_func_t func, void *info, int wait) |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 337 | { |
| 338 | unsigned int cpu; |
| 339 | const struct cpumask *nodemask; |
| 340 | int ret; |
| 341 | |
| 342 | /* Try for same CPU (cheapest) */ |
| 343 | cpu = get_cpu(); |
| 344 | if (cpumask_test_cpu(cpu, mask)) |
| 345 | goto call; |
| 346 | |
| 347 | /* Try for same node. */ |
David John | af2422c | 2010-01-15 17:01:23 -0800 | [diff] [blame] | 348 | nodemask = cpumask_of_node(cpu_to_node(cpu)); |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 349 | for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids; |
| 350 | cpu = cpumask_next_and(cpu, nodemask, mask)) { |
| 351 | if (cpu_online(cpu)) |
| 352 | goto call; |
| 353 | } |
| 354 | |
| 355 | /* Any online will do: smp_call_function_single handles nr_cpu_ids. */ |
| 356 | cpu = cpumask_any_and(mask, cpu_online_mask); |
| 357 | call: |
| 358 | ret = smp_call_function_single(cpu, func, info, wait); |
| 359 | put_cpu(); |
| 360 | return ret; |
| 361 | } |
| 362 | EXPORT_SYMBOL_GPL(smp_call_function_any); |
| 363 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 364 | /** |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 365 | * smp_call_function_many(): Run a function on a set of other CPUs. |
| 366 | * @mask: The set of cpus to run on (only runs on online subset). |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 367 | * @func: The function to run. This must be fast and non-blocking. |
| 368 | * @info: An arbitrary pointer to pass to the function. |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 369 | * @wait: If true, wait (atomically) until function has completed |
| 370 | * on other CPUs. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 371 | * |
Sheng Yang | 72f279b | 2009-10-22 19:19:34 +0800 | [diff] [blame] | 372 | * If @wait is true, then returns once @func has returned. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 373 | * |
| 374 | * You must not call this function with disabled interrupts or from a |
| 375 | * hardware interrupt handler or from a bottom half handler. Preemption |
| 376 | * must be disabled when calling this function. |
| 377 | */ |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 378 | void smp_call_function_many(const struct cpumask *mask, |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 379 | smp_call_func_t func, void *info, bool wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 380 | { |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 381 | struct call_function_data *cfd; |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 382 | int cpu, next_cpu, this_cpu = smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 383 | |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 384 | /* |
| 385 | * Can deadlock when called with interrupts disabled. |
| 386 | * We allow cpu's that are not yet online though, as no one else can |
| 387 | * send smp call function interrupt to this cpu and as such deadlocks |
| 388 | * can't happen. |
| 389 | */ |
| 390 | WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 391 | && !oops_in_progress && !early_boot_irqs_disabled); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 392 | |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 393 | /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */ |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 394 | cpu = cpumask_first_and(mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 395 | if (cpu == this_cpu) |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 396 | cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 397 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 398 | /* No online cpus? We're done. */ |
| 399 | if (cpu >= nr_cpu_ids) |
| 400 | return; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 401 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 402 | /* Do we have another CPU which isn't us? */ |
| 403 | next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 404 | if (next_cpu == this_cpu) |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 405 | next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask); |
| 406 | |
| 407 | /* Fastpath: do that cpu by itself. */ |
| 408 | if (next_cpu >= nr_cpu_ids) { |
| 409 | smp_call_function_single(cpu, func, info, wait); |
| 410 | return; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 413 | cfd = &__get_cpu_var(cfd_data); |
Milton Miller | 45a5791 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 414 | |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 415 | cpumask_and(cfd->cpumask, mask, cpu_online_mask); |
| 416 | cpumask_clear_cpu(this_cpu, cfd->cpumask); |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 417 | |
| 418 | /* Some callers race with other cpus changing the passed mask */ |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 419 | if (unlikely(!cpumask_weight(cfd->cpumask))) |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 420 | return; |
Anton Blanchard | 6dc1989 | 2011-01-20 14:44:33 -0800 | [diff] [blame] | 421 | |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 422 | for_each_cpu(cpu, cfd->cpumask) { |
| 423 | struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu); |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 424 | |
| 425 | csd_lock(csd); |
| 426 | csd->func = func; |
| 427 | csd->info = info; |
Christoph Hellwig | 6897fc2 | 2014-01-30 15:45:47 -0800 | [diff] [blame] | 428 | llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)); |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 429 | } |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 430 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 431 | /* Send a message to all CPUs in the map */ |
Roman Gushchin | 73f9455 | 2014-01-30 15:45:48 -0800 | [diff] [blame] | 432 | arch_send_call_function_ipi_mask(cfd->cpumask); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 433 | |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 434 | if (wait) { |
Andrew Morton | e1d12f3 | 2013-04-30 15:27:28 -0700 | [diff] [blame] | 435 | for_each_cpu(cpu, cfd->cpumask) { |
| 436 | struct call_single_data *csd; |
| 437 | |
| 438 | csd = per_cpu_ptr(cfd->csd, cpu); |
Shaohua Li | 9a46ad6 | 2013-02-21 16:43:03 -0800 | [diff] [blame] | 439 | csd_lock_wait(csd); |
| 440 | } |
| 441 | } |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 442 | } |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 443 | EXPORT_SYMBOL(smp_call_function_many); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 444 | |
| 445 | /** |
| 446 | * smp_call_function(): Run a function on all other CPUs. |
| 447 | * @func: The function to run. This must be fast and non-blocking. |
| 448 | * @info: An arbitrary pointer to pass to the function. |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 449 | * @wait: If true, wait (atomically) until function has completed |
| 450 | * on other CPUs. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 451 | * |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 452 | * Returns 0. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 453 | * |
| 454 | * If @wait is true, then returns once @func has returned; otherwise |
Sheng Yang | 72f279b | 2009-10-22 19:19:34 +0800 | [diff] [blame] | 455 | * it returns just before the target cpu calls @func. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 456 | * |
| 457 | * You must not call this function with disabled interrupts or from a |
| 458 | * hardware interrupt handler or from a bottom half handler. |
| 459 | */ |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 460 | int smp_call_function(smp_call_func_t func, void *info, int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 461 | { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 462 | preempt_disable(); |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 463 | smp_call_function_many(cpu_online_mask, func, info, wait); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 464 | preempt_enable(); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 465 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 466 | return 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 467 | } |
| 468 | EXPORT_SYMBOL(smp_call_function); |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 469 | |
Amerigo Wang | 34db18a0 | 2011-03-22 16:34:06 -0700 | [diff] [blame] | 470 | /* Setup configured maximum number of CPUs to activate */ |
| 471 | unsigned int setup_max_cpus = NR_CPUS; |
| 472 | EXPORT_SYMBOL(setup_max_cpus); |
| 473 | |
| 474 | |
| 475 | /* |
| 476 | * Setup routine for controlling SMP activation |
| 477 | * |
| 478 | * Command-line option of "nosmp" or "maxcpus=0" will disable SMP |
| 479 | * activation entirely (the MPS table probe still happens, though). |
| 480 | * |
| 481 | * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer |
| 482 | * greater than 0, limits the maximum number of CPUs activated in |
| 483 | * SMP mode to <NUM>. |
| 484 | */ |
| 485 | |
| 486 | void __weak arch_disable_smp_support(void) { } |
| 487 | |
| 488 | static int __init nosmp(char *str) |
| 489 | { |
| 490 | setup_max_cpus = 0; |
| 491 | arch_disable_smp_support(); |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | early_param("nosmp", nosmp); |
| 497 | |
| 498 | /* this is hard limit */ |
| 499 | static int __init nrcpus(char *str) |
| 500 | { |
| 501 | int nr_cpus; |
| 502 | |
| 503 | get_option(&str, &nr_cpus); |
| 504 | if (nr_cpus > 0 && nr_cpus < nr_cpu_ids) |
| 505 | nr_cpu_ids = nr_cpus; |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | early_param("nr_cpus", nrcpus); |
| 511 | |
| 512 | static int __init maxcpus(char *str) |
| 513 | { |
| 514 | get_option(&str, &setup_max_cpus); |
| 515 | if (setup_max_cpus == 0) |
| 516 | arch_disable_smp_support(); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | early_param("maxcpus", maxcpus); |
| 522 | |
| 523 | /* Setup number of possible processor ids */ |
| 524 | int nr_cpu_ids __read_mostly = NR_CPUS; |
| 525 | EXPORT_SYMBOL(nr_cpu_ids); |
| 526 | |
| 527 | /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */ |
| 528 | void __init setup_nr_cpu_ids(void) |
| 529 | { |
| 530 | nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1; |
| 531 | } |
| 532 | |
Borislav Petkov | a17bce4 | 2013-09-30 11:56:24 +0200 | [diff] [blame] | 533 | void __weak smp_announce(void) |
| 534 | { |
| 535 | printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus()); |
| 536 | } |
| 537 | |
Amerigo Wang | 34db18a0 | 2011-03-22 16:34:06 -0700 | [diff] [blame] | 538 | /* Called by boot processor to activate the rest. */ |
| 539 | void __init smp_init(void) |
| 540 | { |
| 541 | unsigned int cpu; |
| 542 | |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame] | 543 | idle_threads_init(); |
| 544 | |
Amerigo Wang | 34db18a0 | 2011-03-22 16:34:06 -0700 | [diff] [blame] | 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); |
| 551 | } |
| 552 | |
| 553 | /* Any cleanup work */ |
Borislav Petkov | a17bce4 | 2013-09-30 11:56:24 +0200 | [diff] [blame] | 554 | smp_announce(); |
Amerigo Wang | 34db18a0 | 2011-03-22 16:34:06 -0700 | [diff] [blame] | 555 | smp_cpus_done(setup_max_cpus); |
| 556 | } |
| 557 | |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 558 | /* |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 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(). |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 562 | */ |
| 563 | int on_each_cpu(void (*func) (void *info), void *info, int wait) |
| 564 | { |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 565 | unsigned long flags; |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 566 | int ret = 0; |
| 567 | |
| 568 | preempt_disable(); |
| 569 | ret = smp_call_function(func, info, wait); |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 570 | local_irq_save(flags); |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 571 | func(info); |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 572 | local_irq_restore(flags); |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 573 | preempt_enable(); |
| 574 | return ret; |
| 575 | } |
| 576 | EXPORT_SYMBOL(on_each_cpu); |
Gilad Ben-Yossef | 3fc498f | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 577 | |
| 578 | /** |
| 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. |
| 586 | * |
| 587 | * If @wait is true, then returns once @func has returned. |
| 588 | * |
David Daney | 202da40 | 2013-09-11 14:23:29 -0700 | [diff] [blame] | 589 | * You must not call this function with disabled interrupts or from a |
| 590 | * hardware interrupt handler or from a bottom half handler. The |
| 591 | * exception is that it may be used during early boot while |
| 592 | * early_boot_irqs_disabled is set. |
Gilad Ben-Yossef | 3fc498f | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 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)) { |
David Daney | 202da40 | 2013-09-11 14:23:29 -0700 | [diff] [blame] | 601 | unsigned long flags; |
| 602 | local_irq_save(flags); |
Gilad Ben-Yossef | 3fc498f | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 603 | func(info); |
David Daney | 202da40 | 2013-09-11 14:23:29 -0700 | [diff] [blame] | 604 | local_irq_restore(flags); |
Gilad Ben-Yossef | 3fc498f | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 605 | } |
| 606 | put_cpu(); |
| 607 | } |
| 608 | EXPORT_SYMBOL(on_each_cpu_mask); |
Gilad Ben-Yossef | b3a7e98 | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 609 | |
| 610 | /* |
| 611 | * on_each_cpu_cond(): Call a function on each processor for which |
| 612 | * the supplied function cond_func returns true, optionally waiting |
| 613 | * for all the required CPUs to finish. This may include the local |
| 614 | * processor. |
| 615 | * @cond_func: A callback function that is passed a cpu id and |
| 616 | * the the info parameter. The function is called |
| 617 | * with preemption disabled. The function should |
| 618 | * return a blooean value indicating whether to IPI |
| 619 | * the specified CPU. |
| 620 | * @func: The function to run on all applicable CPUs. |
| 621 | * This must be fast and non-blocking. |
| 622 | * @info: An arbitrary pointer to pass to both functions. |
| 623 | * @wait: If true, wait (atomically) until function has |
| 624 | * completed on other CPUs. |
| 625 | * @gfp_flags: GFP flags to use when allocating the cpumask |
| 626 | * used internally by the function. |
| 627 | * |
| 628 | * The function might sleep if the GFP flags indicates a non |
| 629 | * atomic allocation is allowed. |
| 630 | * |
| 631 | * Preemption is disabled to protect against CPUs going offline but not online. |
| 632 | * CPUs going online during the call will not be seen or sent an IPI. |
| 633 | * |
| 634 | * You must not call this function with disabled interrupts or |
| 635 | * from a hardware interrupt handler or from a bottom half handler. |
| 636 | */ |
| 637 | void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), |
| 638 | smp_call_func_t func, void *info, bool wait, |
| 639 | gfp_t gfp_flags) |
| 640 | { |
| 641 | cpumask_var_t cpus; |
| 642 | int cpu, ret; |
| 643 | |
| 644 | might_sleep_if(gfp_flags & __GFP_WAIT); |
| 645 | |
| 646 | if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) { |
| 647 | preempt_disable(); |
| 648 | for_each_online_cpu(cpu) |
| 649 | if (cond_func(cpu, info)) |
| 650 | cpumask_set_cpu(cpu, cpus); |
| 651 | on_each_cpu_mask(cpus, func, info, wait); |
| 652 | preempt_enable(); |
| 653 | free_cpumask_var(cpus); |
| 654 | } else { |
| 655 | /* |
| 656 | * No free cpumask, bother. No matter, we'll |
| 657 | * just have to IPI them one by one. |
| 658 | */ |
| 659 | preempt_disable(); |
| 660 | for_each_online_cpu(cpu) |
| 661 | if (cond_func(cpu, info)) { |
| 662 | ret = smp_call_function_single(cpu, func, |
| 663 | info, wait); |
| 664 | WARN_ON_ONCE(!ret); |
| 665 | } |
| 666 | preempt_enable(); |
| 667 | } |
| 668 | } |
| 669 | EXPORT_SYMBOL(on_each_cpu_cond); |
Thomas Gleixner | f37f435 | 2012-05-07 17:59:48 +0000 | [diff] [blame] | 670 | |
| 671 | static void do_nothing(void *unused) |
| 672 | { |
| 673 | } |
| 674 | |
| 675 | /** |
| 676 | * kick_all_cpus_sync - Force all cpus out of idle |
| 677 | * |
| 678 | * Used to synchronize the update of pm_idle function pointer. It's |
| 679 | * called after the pointer is updated and returns after the dummy |
| 680 | * callback function has been executed on all cpus. The execution of |
| 681 | * the function can only happen on the remote cpus after they have |
| 682 | * left the idle function which had been called via pm_idle function |
| 683 | * pointer. So it's guaranteed that nothing uses the previous pointer |
| 684 | * anymore. |
| 685 | */ |
| 686 | void kick_all_cpus_sync(void) |
| 687 | { |
| 688 | /* Make sure the change is visible before we kick the cpus */ |
| 689 | smp_mb(); |
| 690 | smp_call_function(do_nothing, NULL, 1); |
| 691 | } |
| 692 | EXPORT_SYMBOL_GPL(kick_all_cpus_sync); |