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