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 |
| 5 | * |
| 6 | */ |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/percpu.h> |
| 10 | #include <linux/rcupdate.h> |
Linus Torvalds | 59190f421 | 2008-07-15 14:02:33 -0700 | [diff] [blame] | 11 | #include <linux/rculist.h> |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 12 | #include <linux/smp.h> |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 13 | #include <linux/cpu.h> |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 14 | |
| 15 | static DEFINE_PER_CPU(struct call_single_queue, call_single_queue); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 16 | |
| 17 | static struct { |
| 18 | struct list_head queue; |
| 19 | spinlock_t lock; |
| 20 | } call_function __cacheline_aligned_in_smp = { |
| 21 | .queue = LIST_HEAD_INIT(call_function.queue), |
| 22 | .lock = __SPIN_LOCK_UNLOCKED(call_function.lock), |
| 23 | }; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 24 | |
| 25 | enum { |
| 26 | CSD_FLAG_WAIT = 0x01, |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 27 | CSD_FLAG_LOCK = 0x02, |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | struct call_function_data { |
| 31 | struct call_single_data csd; |
| 32 | spinlock_t lock; |
| 33 | unsigned int refs; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 34 | cpumask_var_t cpumask; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct call_single_queue { |
| 38 | struct list_head list; |
| 39 | spinlock_t lock; |
| 40 | }; |
| 41 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 42 | static DEFINE_PER_CPU(struct call_function_data, cfd_data) = { |
| 43 | .lock = __SPIN_LOCK_UNLOCKED(cfd_data.lock), |
| 44 | }; |
| 45 | |
| 46 | static int |
| 47 | hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu) |
| 48 | { |
| 49 | long cpu = (long)hcpu; |
| 50 | struct call_function_data *cfd = &per_cpu(cfd_data, cpu); |
| 51 | |
| 52 | switch (action) { |
| 53 | case CPU_UP_PREPARE: |
| 54 | case CPU_UP_PREPARE_FROZEN: |
| 55 | if (!alloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL, |
| 56 | cpu_to_node(cpu))) |
| 57 | return NOTIFY_BAD; |
| 58 | break; |
| 59 | |
| 60 | #ifdef CONFIG_CPU_HOTPLUG |
| 61 | case CPU_UP_CANCELED: |
| 62 | case CPU_UP_CANCELED_FROZEN: |
| 63 | |
| 64 | case CPU_DEAD: |
| 65 | case CPU_DEAD_FROZEN: |
| 66 | free_cpumask_var(cfd->cpumask); |
| 67 | break; |
| 68 | #endif |
| 69 | }; |
| 70 | |
| 71 | return NOTIFY_OK; |
| 72 | } |
| 73 | |
| 74 | static struct notifier_block __cpuinitdata hotplug_cfd_notifier = { |
| 75 | .notifier_call = hotplug_cfd, |
| 76 | }; |
| 77 | |
Eduard - Gabriel Munteanu | 7babe8d | 2008-07-25 19:45:11 -0700 | [diff] [blame] | 78 | static int __cpuinit init_call_single_data(void) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 79 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 80 | void *cpu = (void *)(long)smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 81 | int i; |
| 82 | |
| 83 | for_each_possible_cpu(i) { |
| 84 | struct call_single_queue *q = &per_cpu(call_single_queue, i); |
| 85 | |
| 86 | spin_lock_init(&q->lock); |
| 87 | INIT_LIST_HEAD(&q->list); |
| 88 | } |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 89 | |
| 90 | hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu); |
| 91 | register_cpu_notifier(&hotplug_cfd_notifier); |
| 92 | |
Eduard - Gabriel Munteanu | 7babe8d | 2008-07-25 19:45:11 -0700 | [diff] [blame] | 93 | return 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 94 | } |
Eduard - Gabriel Munteanu | 7babe8d | 2008-07-25 19:45:11 -0700 | [diff] [blame] | 95 | early_initcall(init_call_single_data); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 96 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 97 | /* |
| 98 | * csd_wait/csd_complete are used for synchronous ipi calls |
| 99 | */ |
| 100 | static void csd_wait_prepare(struct call_single_data *data) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 101 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 102 | data->flags |= CSD_FLAG_WAIT; |
| 103 | } |
| 104 | |
| 105 | static void csd_complete(struct call_single_data *data) |
| 106 | { |
| 107 | if (data->flags & CSD_FLAG_WAIT) { |
| 108 | /* |
| 109 | * ensure we're all done before saying we are |
| 110 | */ |
| 111 | smp_mb(); |
| 112 | data->flags &= ~CSD_FLAG_WAIT; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static void csd_wait(struct call_single_data *data) |
| 117 | { |
| 118 | while (data->flags & CSD_FLAG_WAIT) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 119 | cpu_relax(); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * csd_lock/csd_unlock used to serialize access to per-cpu csd resources |
| 124 | * |
| 125 | * For non-synchronous ipi calls the csd can still be in use by the previous |
| 126 | * function call. For multi-cpu calls its even more interesting as we'll have |
| 127 | * to ensure no other cpu is observing our csd. |
| 128 | */ |
| 129 | static void csd_lock(struct call_single_data *data) |
| 130 | { |
| 131 | while (data->flags & CSD_FLAG_LOCK) |
| 132 | cpu_relax(); |
| 133 | data->flags = CSD_FLAG_LOCK; |
| 134 | |
| 135 | /* |
| 136 | * prevent CPU from reordering the above assignment to ->flags |
| 137 | * with any subsequent assignments to other fields of the |
| 138 | * specified call_single_data structure. |
| 139 | */ |
| 140 | |
| 141 | smp_mb(); |
| 142 | } |
| 143 | |
| 144 | static void csd_unlock(struct call_single_data *data) |
| 145 | { |
| 146 | WARN_ON(!(data->flags & CSD_FLAG_LOCK)); |
| 147 | /* |
| 148 | * ensure we're all done before releasing data |
| 149 | */ |
| 150 | smp_mb(); |
| 151 | data->flags &= ~CSD_FLAG_LOCK; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Insert a previously allocated call_single_data element for execution |
| 156 | * on the given CPU. data must already have ->func, ->info, and ->flags set. |
| 157 | */ |
| 158 | static void generic_exec_single(int cpu, struct call_single_data *data) |
| 159 | { |
| 160 | struct call_single_queue *dst = &per_cpu(call_single_queue, cpu); |
| 161 | int wait = data->flags & CSD_FLAG_WAIT, ipi; |
| 162 | unsigned long flags; |
| 163 | |
| 164 | spin_lock_irqsave(&dst->lock, flags); |
| 165 | ipi = list_empty(&dst->list); |
| 166 | list_add_tail(&data->list, &dst->list); |
| 167 | spin_unlock_irqrestore(&dst->lock, flags); |
| 168 | |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 169 | /* |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 170 | * The list addition should be visible before sending the IPI |
| 171 | * handler locks the list to pull the entry off it because of |
| 172 | * normal cache coherency rules implied by spinlocks. |
| 173 | * |
| 174 | * If IPIs can go out of order to the cache coherency protocol |
| 175 | * in an architecture, sufficient synchronisation should be added |
| 176 | * to arch code to make it appear to obey cache coherency WRT |
| 177 | * locking and barrier primitives. Generic code isn't really equipped |
| 178 | * to do the right thing... |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 179 | */ |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 180 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 181 | if (ipi) |
| 182 | arch_send_call_function_single_ipi(cpu); |
| 183 | |
| 184 | if (wait) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 185 | csd_wait(data); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Invoked by arch to handle an IPI for call function. Must be called with |
| 190 | * interrupts disabled. |
| 191 | */ |
| 192 | void generic_smp_call_function_interrupt(void) |
| 193 | { |
| 194 | struct call_function_data *data; |
| 195 | int cpu = get_cpu(); |
| 196 | |
| 197 | /* |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 198 | * Ensure entry is visible on call_function_queue after we have |
| 199 | * entered the IPI. See comment in smp_call_function_many. |
| 200 | * If we don't have this, then we may miss an entry on the list |
| 201 | * and never get another IPI to process it. |
| 202 | */ |
| 203 | smp_mb(); |
| 204 | |
| 205 | /* |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 206 | * It's ok to use list_for_each_rcu() here even though we may delete |
| 207 | * 'pos', since list_del_rcu() doesn't clear ->next |
| 208 | */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 209 | list_for_each_entry_rcu(data, &call_function.queue, csd.list) { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 210 | int refs; |
| 211 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 212 | spin_lock(&data->lock); |
| 213 | if (!cpumask_test_cpu(cpu, data->cpumask)) { |
| 214 | spin_unlock(&data->lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 215 | continue; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 216 | } |
| 217 | cpumask_clear_cpu(cpu, data->cpumask); |
| 218 | spin_unlock(&data->lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 219 | |
| 220 | data->csd.func(data->csd.info); |
| 221 | |
| 222 | spin_lock(&data->lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 223 | WARN_ON(data->refs == 0); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 224 | refs = --data->refs; |
| 225 | if (!refs) { |
| 226 | spin_lock(&call_function.lock); |
| 227 | list_del_rcu(&data->csd.list); |
| 228 | spin_unlock(&call_function.lock); |
| 229 | } |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 230 | spin_unlock(&data->lock); |
| 231 | |
| 232 | if (refs) |
| 233 | continue; |
| 234 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 235 | csd_complete(&data->csd); |
| 236 | csd_unlock(&data->csd); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 237 | } |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 238 | |
| 239 | put_cpu(); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Invoked by arch to handle an IPI for call function single. Must be called |
| 244 | * from the arch with interrupts disabled. |
| 245 | */ |
| 246 | void generic_smp_call_function_single_interrupt(void) |
| 247 | { |
| 248 | struct call_single_queue *q = &__get_cpu_var(call_single_queue); |
| 249 | LIST_HEAD(list); |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 250 | unsigned int data_flags; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 251 | |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 252 | spin_lock(&q->lock); |
| 253 | list_replace_init(&q->list, &list); |
| 254 | spin_unlock(&q->lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 255 | |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 256 | while (!list_empty(&list)) { |
| 257 | struct call_single_data *data; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 258 | |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 259 | data = list_entry(list.next, struct call_single_data, |
| 260 | list); |
| 261 | list_del(&data->list); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 262 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 263 | /* |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 264 | * 'data' can be invalid after this call if |
| 265 | * flags == 0 (when called through |
| 266 | * generic_exec_single(), so save them away before |
| 267 | * making the call. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 268 | */ |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 269 | data_flags = data->flags; |
| 270 | |
| 271 | data->func(data->info); |
| 272 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 273 | if (data_flags & CSD_FLAG_WAIT) |
| 274 | csd_complete(data); |
| 275 | |
| 276 | /* |
| 277 | * Unlocked CSDs are valid through generic_exec_single() |
| 278 | */ |
| 279 | if (data_flags & CSD_FLAG_LOCK) |
| 280 | csd_unlock(data); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
Steven Rostedt | d7240b9 | 2009-01-29 10:08:01 -0500 | [diff] [blame] | 284 | static DEFINE_PER_CPU(struct call_single_data, csd_data); |
| 285 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 286 | /* |
| 287 | * smp_call_function_single - Run a function on a specific CPU |
| 288 | * @func: The function to run. This must be fast and non-blocking. |
| 289 | * @info: An arbitrary pointer to pass to the function. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 290 | * @wait: If true, wait until function has completed on other CPUs. |
| 291 | * |
| 292 | * Returns 0 on success, else a negative status code. Note that @wait |
| 293 | * will be implicitly turned on in case of allocation failures, since |
| 294 | * we fall back to on-stack allocation. |
| 295 | */ |
| 296 | int smp_call_function_single(int cpu, void (*func) (void *info), void *info, |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 297 | int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 298 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 299 | struct call_single_data d = { |
| 300 | .flags = 0, |
| 301 | }; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 302 | unsigned long flags; |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 303 | /* prevent preemption and reschedule on another processor, |
| 304 | as well as CPU removal */ |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 305 | int me = get_cpu(); |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 306 | int err = 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 307 | |
| 308 | /* Can deadlock when called with interrupts disabled */ |
| 309 | WARN_ON(irqs_disabled()); |
| 310 | |
| 311 | if (cpu == me) { |
| 312 | local_irq_save(flags); |
| 313 | func(info); |
| 314 | local_irq_restore(flags); |
Rusty Russell | 4f4b6c1 | 2009-01-01 10:12:15 +1030 | [diff] [blame] | 315 | } else if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) { |
Steven Rostedt | d7240b9 | 2009-01-29 10:08:01 -0500 | [diff] [blame] | 316 | struct call_single_data *data; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 317 | |
| 318 | if (!wait) { |
Steven Rostedt | d7240b9 | 2009-01-29 10:08:01 -0500 | [diff] [blame] | 319 | /* |
| 320 | * We are calling a function on a single CPU |
| 321 | * and we are not going to wait for it to finish. |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 322 | * We use a per cpu data to pass the information to |
| 323 | * that CPU. Since all callers of this code will |
| 324 | * use the same data, we must synchronize the |
| 325 | * callers to prevent a new caller from corrupting |
| 326 | * the data before the callee can access it. |
Steven Rostedt | d7240b9 | 2009-01-29 10:08:01 -0500 | [diff] [blame] | 327 | * |
| 328 | * The CSD_FLAG_LOCK is used to let us know when |
| 329 | * the IPI handler is done with the data. |
| 330 | * The first caller will set it, and the callee |
| 331 | * will clear it. The next caller must wait for |
| 332 | * it to clear before we set it again. This |
| 333 | * will make sure the callee is done with the |
| 334 | * data before a new caller will use it. |
| 335 | */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 336 | data = &__get_cpu_var(csd_data); |
| 337 | csd_lock(data); |
Steven Rostedt | d7240b9 | 2009-01-29 10:08:01 -0500 | [diff] [blame] | 338 | } else { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 339 | data = &d; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 340 | csd_wait_prepare(data); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | data->func = func; |
| 344 | data->info = info; |
| 345 | generic_exec_single(cpu, data); |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 346 | } else { |
| 347 | err = -ENXIO; /* CPU not online */ |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | put_cpu(); |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 351 | return err; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 352 | } |
| 353 | EXPORT_SYMBOL(smp_call_function_single); |
| 354 | |
| 355 | /** |
| 356 | * __smp_call_function_single(): Run a function on another CPU |
| 357 | * @cpu: The CPU to run on. |
| 358 | * @data: Pre-allocated and setup data structure |
| 359 | * |
| 360 | * Like smp_call_function_single(), but allow caller to pass in a pre-allocated |
| 361 | * data structure. Useful for embedding @data inside other structures, for |
| 362 | * instance. |
| 363 | * |
| 364 | */ |
| 365 | void __smp_call_function_single(int cpu, struct call_single_data *data) |
| 366 | { |
| 367 | /* Can deadlock when called with interrupts disabled */ |
| 368 | WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled()); |
| 369 | |
| 370 | generic_exec_single(cpu, data); |
| 371 | } |
| 372 | |
Rusty Russell | ce47d97 | 2008-12-30 09:05:17 +1030 | [diff] [blame] | 373 | /* FIXME: Shim for archs using old arch_send_call_function_ipi API. */ |
| 374 | #ifndef arch_send_call_function_ipi_mask |
| 375 | #define arch_send_call_function_ipi_mask(maskp) \ |
| 376 | arch_send_call_function_ipi(*(maskp)) |
| 377 | #endif |
| 378 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 379 | /** |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 380 | * smp_call_function_many(): Run a function on a set of other CPUs. |
| 381 | * @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] | 382 | * @func: The function to run. This must be fast and non-blocking. |
| 383 | * @info: An arbitrary pointer to pass to the function. |
| 384 | * @wait: If true, wait (atomically) until function has completed on other CPUs. |
| 385 | * |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 386 | * If @wait is true, then returns once @func has returned. Note that @wait |
| 387 | * will be implicitly turned on in case of allocation failures, since |
| 388 | * we fall back to on-stack allocation. |
| 389 | * |
| 390 | * You must not call this function with disabled interrupts or from a |
| 391 | * hardware interrupt handler or from a bottom half handler. Preemption |
| 392 | * must be disabled when calling this function. |
| 393 | */ |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 394 | void smp_call_function_many(const struct cpumask *mask, |
| 395 | void (*func)(void *), void *info, |
| 396 | bool wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 397 | { |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 398 | struct call_function_data *data; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 399 | unsigned long flags; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 400 | int cpu, next_cpu, me = smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 401 | |
| 402 | /* Can deadlock when called with interrupts disabled */ |
| 403 | WARN_ON(irqs_disabled()); |
| 404 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 405 | /* So, what's a CPU they want? Ignoring this one. */ |
| 406 | cpu = cpumask_first_and(mask, cpu_online_mask); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 407 | if (cpu == me) |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 408 | cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
| 409 | /* No online cpus? We're done. */ |
| 410 | if (cpu >= nr_cpu_ids) |
| 411 | return; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 412 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 413 | /* Do we have another CPU which isn't us? */ |
| 414 | next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 415 | if (next_cpu == me) |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 416 | next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask); |
| 417 | |
| 418 | /* Fastpath: do that cpu by itself. */ |
| 419 | if (next_cpu >= nr_cpu_ids) { |
| 420 | smp_call_function_single(cpu, func, info, wait); |
| 421 | return; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 424 | data = &__get_cpu_var(cfd_data); |
| 425 | csd_lock(&data->csd); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 426 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 427 | spin_lock_irqsave(&data->lock, flags); |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 428 | if (wait) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 429 | csd_wait_prepare(&data->csd); |
| 430 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 431 | data->csd.func = func; |
| 432 | data->csd.info = info; |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 433 | cpumask_and(data->cpumask, mask, cpu_online_mask); |
| 434 | cpumask_clear_cpu(me, data->cpumask); |
| 435 | data->refs = cpumask_weight(data->cpumask); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 436 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 437 | spin_lock(&call_function.lock); |
| 438 | /* |
| 439 | * Place entry at the _HEAD_ of the list, so that any cpu still |
| 440 | * observing the entry in generic_smp_call_function_interrupt() will |
| 441 | * not miss any other list entries. |
| 442 | */ |
| 443 | list_add_rcu(&data->csd.list, &call_function.queue); |
| 444 | spin_unlock(&call_function.lock); |
| 445 | spin_unlock_irqrestore(&data->lock, flags); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 446 | |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 447 | /* |
| 448 | * Make the list addition visible before sending the ipi. |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 449 | * (IPIs must obey or appear to obey normal Linux cache coherency |
| 450 | * rules -- see comment in generic_exec_single). |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 451 | */ |
| 452 | smp_mb(); |
| 453 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 454 | /* Send a message to all CPUs in the map */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 455 | arch_send_call_function_ipi_mask(data->cpumask); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 456 | |
| 457 | /* optionally wait for the CPUs to complete */ |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 458 | if (wait) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 459 | csd_wait(&data->csd); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 460 | } |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 461 | EXPORT_SYMBOL(smp_call_function_many); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 462 | |
| 463 | /** |
| 464 | * smp_call_function(): Run a function on all other CPUs. |
| 465 | * @func: The function to run. This must be fast and non-blocking. |
| 466 | * @info: An arbitrary pointer to pass to the function. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 467 | * @wait: If true, wait (atomically) until function has completed on other CPUs. |
| 468 | * |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 469 | * Returns 0. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 470 | * |
| 471 | * If @wait is true, then returns once @func has returned; otherwise |
| 472 | * it returns just before the target cpu calls @func. In case of allocation |
| 473 | * failure, @wait will be implicitly turned on. |
| 474 | * |
| 475 | * You must not call this function with disabled interrupts or from a |
| 476 | * hardware interrupt handler or from a bottom half handler. |
| 477 | */ |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 478 | int smp_call_function(void (*func)(void *), void *info, int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 479 | { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 480 | preempt_disable(); |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 481 | smp_call_function_many(cpu_online_mask, func, info, wait); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 482 | preempt_enable(); |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 483 | return 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 484 | } |
| 485 | EXPORT_SYMBOL(smp_call_function); |
| 486 | |
| 487 | void ipi_call_lock(void) |
| 488 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 489 | spin_lock(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | void ipi_call_unlock(void) |
| 493 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 494 | spin_unlock(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | void ipi_call_lock_irq(void) |
| 498 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 499 | spin_lock_irq(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | void ipi_call_unlock_irq(void) |
| 503 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame^] | 504 | spin_unlock_irq(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 505 | } |