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