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 | |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 18 | #ifdef CONFIG_USE_GENERIC_SMP_HELPERS |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 19 | static struct { |
| 20 | struct list_head queue; |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 21 | raw_spinlock_t lock; |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 22 | } call_function __cacheline_aligned_in_smp = |
| 23 | { |
| 24 | .queue = LIST_HEAD_INIT(call_function.queue), |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 25 | .lock = __RAW_SPIN_LOCK_UNLOCKED(call_function.lock), |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 26 | }; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 27 | |
| 28 | enum { |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 29 | CSD_FLAG_LOCK = 0x01, |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct call_function_data { |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 33 | struct call_single_data csd; |
Xiao Guangrong | 54fdade | 2009-09-22 16:43:39 -0700 | [diff] [blame] | 34 | atomic_t refs; |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 35 | cpumask_var_t cpumask; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Milton Miller | e03bcb6 | 2010-01-18 13:00:51 +1100 | [diff] [blame] | 38 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data); |
| 39 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 40 | struct call_single_queue { |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 41 | struct list_head list; |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 42 | raw_spinlock_t lock; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
Milton Miller | e03bcb6 | 2010-01-18 13:00:51 +1100 | [diff] [blame] | 45 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 46 | |
| 47 | static int |
| 48 | hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu) |
| 49 | { |
| 50 | long cpu = (long)hcpu; |
| 51 | struct call_function_data *cfd = &per_cpu(cfd_data, cpu); |
| 52 | |
| 53 | switch (action) { |
| 54 | case CPU_UP_PREPARE: |
| 55 | case CPU_UP_PREPARE_FROZEN: |
Yinghai Lu | eaa9584 | 2009-06-06 14:51:36 -0700 | [diff] [blame] | 56 | if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL, |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 57 | cpu_to_node(cpu))) |
Akinobu Mita | 80b5184 | 2010-05-26 14:43:32 -0700 | [diff] [blame] | 58 | return notifier_from_errno(-ENOMEM); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 59 | break; |
| 60 | |
Xiao Guangrong | 69dd647 | 2009-08-06 15:07:29 -0700 | [diff] [blame] | 61 | #ifdef CONFIG_HOTPLUG_CPU |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 62 | case CPU_UP_CANCELED: |
| 63 | case CPU_UP_CANCELED_FROZEN: |
| 64 | |
| 65 | case CPU_DEAD: |
| 66 | case CPU_DEAD_FROZEN: |
| 67 | free_cpumask_var(cfd->cpumask); |
| 68 | break; |
| 69 | #endif |
| 70 | }; |
| 71 | |
| 72 | return NOTIFY_OK; |
| 73 | } |
| 74 | |
| 75 | static struct notifier_block __cpuinitdata hotplug_cfd_notifier = { |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 76 | .notifier_call = hotplug_cfd, |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Takao Indoh | d8ad7d1 | 2011-03-29 12:35:04 -0400 | [diff] [blame] | 79 | void __init call_function_init(void) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 80 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 81 | void *cpu = (void *)(long)smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 82 | int i; |
| 83 | |
| 84 | for_each_possible_cpu(i) { |
| 85 | struct call_single_queue *q = &per_cpu(call_single_queue, i); |
| 86 | |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 87 | raw_spin_lock_init(&q->lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 88 | INIT_LIST_HEAD(&q->list); |
| 89 | } |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 90 | |
| 91 | hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu); |
| 92 | register_cpu_notifier(&hotplug_cfd_notifier); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 95 | /* |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 96 | * csd_lock/csd_unlock used to serialize access to per-cpu csd resources |
| 97 | * |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 98 | * For non-synchronous ipi calls the csd can still be in use by the |
| 99 | * previous function call. For multi-cpu calls its even more interesting |
| 100 | * 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] | 101 | */ |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 102 | static void csd_lock_wait(struct call_single_data *data) |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 103 | { |
| 104 | while (data->flags & CSD_FLAG_LOCK) |
| 105 | cpu_relax(); |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static void csd_lock(struct call_single_data *data) |
| 109 | { |
| 110 | csd_lock_wait(data); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 111 | data->flags = CSD_FLAG_LOCK; |
| 112 | |
| 113 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 114 | * prevent CPU from reordering the above assignment |
| 115 | * to ->flags with any subsequent assignments to other |
| 116 | * fields of the specified call_single_data structure: |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 117 | */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 118 | smp_mb(); |
| 119 | } |
| 120 | |
| 121 | static void csd_unlock(struct call_single_data *data) |
| 122 | { |
| 123 | WARN_ON(!(data->flags & CSD_FLAG_LOCK)); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 124 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 125 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 126 | * ensure we're all done before releasing data: |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 127 | */ |
| 128 | smp_mb(); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 129 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 130 | data->flags &= ~CSD_FLAG_LOCK; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 134 | * Insert a previously allocated call_single_data element |
| 135 | * for execution on the given CPU. data must already have |
| 136 | * ->func, ->info, and ->flags set. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 137 | */ |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 138 | static |
| 139 | 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] | 140 | { |
| 141 | struct call_single_queue *dst = &per_cpu(call_single_queue, cpu); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 142 | unsigned long flags; |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 143 | int ipi; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 144 | |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 145 | raw_spin_lock_irqsave(&dst->lock, flags); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 146 | ipi = list_empty(&dst->list); |
| 147 | list_add_tail(&data->list, &dst->list); |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 148 | raw_spin_unlock_irqrestore(&dst->lock, flags); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 149 | |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 150 | /* |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 151 | * The list addition should be visible before sending the IPI |
| 152 | * handler locks the list to pull the entry off it because of |
| 153 | * normal cache coherency rules implied by spinlocks. |
| 154 | * |
| 155 | * If IPIs can go out of order to the cache coherency protocol |
| 156 | * in an architecture, sufficient synchronisation should be added |
| 157 | * to arch code to make it appear to obey cache coherency WRT |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 158 | * locking and barrier primitives. Generic code isn't really |
| 159 | * equipped to do the right thing... |
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; |
Xiao Guangrong | c0f68c2 | 2009-12-14 18:00:16 -0800 | [diff] [blame] | 175 | int cpu = smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 176 | |
| 177 | /* |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 178 | * Shouldn't receive this interrupt on a cpu that is not yet online. |
| 179 | */ |
| 180 | WARN_ON_ONCE(!cpu_online(cpu)); |
| 181 | |
| 182 | /* |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 183 | * Ensure entry is visible on call_function_queue after we have |
| 184 | * entered the IPI. See comment in smp_call_function_many. |
| 185 | * If we don't have this, then we may miss an entry on the list |
| 186 | * and never get another IPI to process it. |
| 187 | */ |
| 188 | smp_mb(); |
| 189 | |
| 190 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 191 | * It's ok to use list_for_each_rcu() here even though we may |
| 192 | * delete 'pos', since list_del_rcu() doesn't clear ->next |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 193 | */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 194 | list_for_each_entry_rcu(data, &call_function.queue, csd.list) { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 195 | int refs; |
Milton Miller | c8def55 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 196 | smp_call_func_t func; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 197 | |
Anton Blanchard | 6dc1989 | 2011-01-20 14:44:33 -0800 | [diff] [blame] | 198 | /* |
| 199 | * Since we walk the list without any locks, we might |
| 200 | * see an entry that was completed, removed from the |
| 201 | * list and is in the process of being reused. |
| 202 | * |
| 203 | * We must check that the cpu is in the cpumask before |
| 204 | * checking the refs, and both must be set before |
| 205 | * executing the callback on this cpu. |
| 206 | */ |
| 207 | |
| 208 | if (!cpumask_test_cpu(cpu, data->cpumask)) |
| 209 | continue; |
| 210 | |
| 211 | smp_rmb(); |
| 212 | |
| 213 | if (atomic_read(&data->refs) == 0) |
| 214 | continue; |
| 215 | |
Milton Miller | c8def55 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 216 | func = data->csd.func; /* save for later warn */ |
| 217 | func(data->csd.info); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 218 | |
Milton Miller | 225c8e010 | 2011-01-20 14:44:34 -0800 | [diff] [blame] | 219 | /* |
Milton Miller | c8def55 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 220 | * If the cpu mask is not still set then func enabled |
| 221 | * interrupts (BUG), and this cpu took another smp call |
| 222 | * function interrupt and executed func(info) twice |
| 223 | * on this cpu. That nested execution decremented refs. |
Milton Miller | 225c8e010 | 2011-01-20 14:44:34 -0800 | [diff] [blame] | 224 | */ |
| 225 | if (!cpumask_test_and_clear_cpu(cpu, data->cpumask)) { |
Milton Miller | c8def55 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 226 | WARN(1, "%pf enabled interrupts and double executed\n", func); |
Milton Miller | 225c8e010 | 2011-01-20 14:44:34 -0800 | [diff] [blame] | 227 | continue; |
| 228 | } |
| 229 | |
Xiao Guangrong | 54fdade | 2009-09-22 16:43:39 -0700 | [diff] [blame] | 230 | refs = atomic_dec_return(&data->refs); |
| 231 | WARN_ON(refs < 0); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 232 | |
| 233 | if (refs) |
| 234 | continue; |
| 235 | |
Milton Miller | 225c8e010 | 2011-01-20 14:44:34 -0800 | [diff] [blame] | 236 | WARN_ON(!cpumask_empty(data->cpumask)); |
| 237 | |
| 238 | raw_spin_lock(&call_function.lock); |
| 239 | list_del_rcu(&data->csd.list); |
| 240 | raw_spin_unlock(&call_function.lock); |
| 241 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 242 | csd_unlock(&data->csd); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 243 | } |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 244 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 248 | * Invoked by arch to handle an IPI for call function single. Must be |
| 249 | * called from the arch with interrupts disabled. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 250 | */ |
| 251 | void generic_smp_call_function_single_interrupt(void) |
| 252 | { |
| 253 | struct call_single_queue *q = &__get_cpu_var(call_single_queue); |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 254 | unsigned int data_flags; |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 255 | LIST_HEAD(list); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 256 | |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 257 | /* |
| 258 | * Shouldn't receive this interrupt on a cpu that is not yet online. |
| 259 | */ |
| 260 | WARN_ON_ONCE(!cpu_online(smp_processor_id())); |
| 261 | |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 262 | raw_spin_lock(&q->lock); |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 263 | list_replace_init(&q->list, &list); |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 264 | raw_spin_unlock(&q->lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 265 | |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 266 | while (!list_empty(&list)) { |
| 267 | struct call_single_data *data; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 268 | |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 269 | data = list_entry(list.next, struct call_single_data, list); |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 270 | list_del(&data->list); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 271 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 272 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 273 | * 'data' can be invalid after this call if flags == 0 |
| 274 | * (when called through generic_exec_single()), |
| 275 | * so save them away before making the call: |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 276 | */ |
Nick Piggin | 15d0d3b | 2009-02-25 06:22:45 +0100 | [diff] [blame] | 277 | data_flags = data->flags; |
| 278 | |
| 279 | data->func(data->info); |
| 280 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 281 | /* |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 282 | * Unlocked CSDs are valid through generic_exec_single(): |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 283 | */ |
| 284 | if (data_flags & CSD_FLAG_LOCK) |
| 285 | csd_unlock(data); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Milton Miller | e03bcb6 | 2010-01-18 13:00:51 +1100 | [diff] [blame] | 289 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data); |
Steven Rostedt | d7240b9 | 2009-01-29 10:08:01 -0500 | [diff] [blame] | 290 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 291 | /* |
| 292 | * smp_call_function_single - Run a function on a specific CPU |
| 293 | * @func: The function to run. This must be fast and non-blocking. |
| 294 | * @info: An arbitrary pointer to pass to the function. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 295 | * @wait: If true, wait until function has completed on other CPUs. |
| 296 | * |
Sheng Yang | 72f279b | 2009-10-22 19:19:34 +0800 | [diff] [blame] | 297 | * Returns 0 on success, else a negative status code. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 298 | */ |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 299 | 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] | 300 | int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 301 | { |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 302 | struct call_single_data d = { |
| 303 | .flags = 0, |
| 304 | }; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 305 | unsigned long flags; |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 306 | int this_cpu; |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 307 | int err = 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 308 | |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 309 | /* |
| 310 | * prevent preemption and reschedule on another processor, |
| 311 | * as well as CPU removal |
| 312 | */ |
| 313 | this_cpu = get_cpu(); |
| 314 | |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 315 | /* |
| 316 | * Can deadlock when called with interrupts disabled. |
| 317 | * We allow cpu's that are not yet online though, as no one else can |
| 318 | * send smp call function interrupt to this cpu and as such deadlocks |
| 319 | * can't happen. |
| 320 | */ |
| 321 | WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() |
| 322 | && !oops_in_progress); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 323 | |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 324 | if (cpu == this_cpu) { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 325 | local_irq_save(flags); |
| 326 | func(info); |
| 327 | local_irq_restore(flags); |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 328 | } else { |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 329 | if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) { |
| 330 | struct call_single_data *data = &d; |
| 331 | |
| 332 | if (!wait) |
| 333 | data = &__get_cpu_var(csd_data); |
| 334 | |
| 335 | csd_lock(data); |
| 336 | |
| 337 | data->func = func; |
| 338 | data->info = info; |
| 339 | generic_exec_single(cpu, data, wait); |
| 340 | } else { |
| 341 | err = -ENXIO; /* CPU not online */ |
| 342 | } |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | put_cpu(); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 346 | |
H. Peter Anvin | f73be6de | 2008-08-25 17:07:14 -0700 | [diff] [blame] | 347 | return err; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 348 | } |
| 349 | EXPORT_SYMBOL(smp_call_function_single); |
| 350 | |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 351 | /* |
| 352 | * smp_call_function_any - Run a function on any of the given cpus |
| 353 | * @mask: The mask of cpus it can run on. |
| 354 | * @func: The function to run. This must be fast and non-blocking. |
| 355 | * @info: An arbitrary pointer to pass to the function. |
| 356 | * @wait: If true, wait until function has completed. |
| 357 | * |
| 358 | * Returns 0 on success, else a negative status code (if no cpus were online). |
| 359 | * Note that @wait will be implicitly turned on in case of allocation failures, |
| 360 | * since we fall back to on-stack allocation. |
| 361 | * |
| 362 | * Selection preference: |
| 363 | * 1) current cpu if in @mask |
| 364 | * 2) any cpu of current node if in @mask |
| 365 | * 3) any other online cpu in @mask |
| 366 | */ |
| 367 | int smp_call_function_any(const struct cpumask *mask, |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 368 | smp_call_func_t func, void *info, int wait) |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 369 | { |
| 370 | unsigned int cpu; |
| 371 | const struct cpumask *nodemask; |
| 372 | int ret; |
| 373 | |
| 374 | /* Try for same CPU (cheapest) */ |
| 375 | cpu = get_cpu(); |
| 376 | if (cpumask_test_cpu(cpu, mask)) |
| 377 | goto call; |
| 378 | |
| 379 | /* Try for same node. */ |
David John | af2422c | 2010-01-15 17:01:23 -0800 | [diff] [blame] | 380 | nodemask = cpumask_of_node(cpu_to_node(cpu)); |
Rusty Russell | 2ea6dec | 2009-11-17 14:27:27 -0800 | [diff] [blame] | 381 | for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids; |
| 382 | cpu = cpumask_next_and(cpu, nodemask, mask)) { |
| 383 | if (cpu_online(cpu)) |
| 384 | goto call; |
| 385 | } |
| 386 | |
| 387 | /* Any online will do: smp_call_function_single handles nr_cpu_ids. */ |
| 388 | cpu = cpumask_any_and(mask, cpu_online_mask); |
| 389 | call: |
| 390 | ret = smp_call_function_single(cpu, func, info, wait); |
| 391 | put_cpu(); |
| 392 | return ret; |
| 393 | } |
| 394 | EXPORT_SYMBOL_GPL(smp_call_function_any); |
| 395 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 396 | /** |
Heiko Carstens | 27c379f | 2010-09-10 13:47:29 +0200 | [diff] [blame] | 397 | * __smp_call_function_single(): Run a function on a specific CPU |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 398 | * @cpu: The CPU to run on. |
| 399 | * @data: Pre-allocated and setup data structure |
Heiko Carstens | 27c379f | 2010-09-10 13:47:29 +0200 | [diff] [blame] | 400 | * @wait: If true, wait until function has completed on specified CPU. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 401 | * |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 402 | * Like smp_call_function_single(), but allow caller to pass in a |
| 403 | * pre-allocated data structure. Useful for embedding @data inside |
| 404 | * other structures, for instance. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 405 | */ |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 406 | void __smp_call_function_single(int cpu, struct call_single_data *data, |
| 407 | int wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 408 | { |
Heiko Carstens | 27c379f | 2010-09-10 13:47:29 +0200 | [diff] [blame] | 409 | unsigned int this_cpu; |
| 410 | unsigned long flags; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 411 | |
Heiko Carstens | 27c379f | 2010-09-10 13:47:29 +0200 | [diff] [blame] | 412 | this_cpu = get_cpu(); |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 413 | /* |
| 414 | * Can deadlock when called with interrupts disabled. |
| 415 | * We allow cpu's that are not yet online though, as no one else can |
| 416 | * send smp call function interrupt to this cpu and as such deadlocks |
| 417 | * can't happen. |
| 418 | */ |
| 419 | WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled() |
| 420 | && !oops_in_progress); |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 421 | |
Heiko Carstens | 27c379f | 2010-09-10 13:47:29 +0200 | [diff] [blame] | 422 | if (cpu == this_cpu) { |
| 423 | local_irq_save(flags); |
| 424 | data->func(data->info); |
| 425 | local_irq_restore(flags); |
| 426 | } else { |
| 427 | csd_lock(data); |
| 428 | generic_exec_single(cpu, data, wait); |
| 429 | } |
| 430 | put_cpu(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /** |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 434 | * smp_call_function_many(): Run a function on a set of other CPUs. |
| 435 | * @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] | 436 | * @func: The function to run. This must be fast and non-blocking. |
| 437 | * @info: An arbitrary pointer to pass to the function. |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 438 | * @wait: If true, wait (atomically) until function has completed |
| 439 | * on other CPUs. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 440 | * |
Sheng Yang | 72f279b | 2009-10-22 19:19:34 +0800 | [diff] [blame] | 441 | * If @wait is true, then returns once @func has returned. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 442 | * |
| 443 | * You must not call this function with disabled interrupts or from a |
| 444 | * hardware interrupt handler or from a bottom half handler. Preemption |
| 445 | * must be disabled when calling this function. |
| 446 | */ |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 447 | void smp_call_function_many(const struct cpumask *mask, |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 448 | smp_call_func_t func, void *info, bool wait) |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 449 | { |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 450 | struct call_function_data *data; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 451 | unsigned long flags; |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 452 | int refs, cpu, next_cpu, this_cpu = smp_processor_id(); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 453 | |
Suresh Siddha | 269c861 | 2009-08-19 18:05:35 -0700 | [diff] [blame] | 454 | /* |
| 455 | * Can deadlock when called with interrupts disabled. |
| 456 | * We allow cpu's that are not yet online though, as no one else can |
| 457 | * send smp call function interrupt to this cpu and as such deadlocks |
| 458 | * can't happen. |
| 459 | */ |
| 460 | WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled() |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 461 | && !oops_in_progress && !early_boot_irqs_disabled); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 462 | |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 463 | /* 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] | 464 | cpu = cpumask_first_and(mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 465 | if (cpu == this_cpu) |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 466 | cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 467 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 468 | /* No online cpus? We're done. */ |
| 469 | if (cpu >= nr_cpu_ids) |
| 470 | return; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 471 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 472 | /* Do we have another CPU which isn't us? */ |
| 473 | next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 474 | if (next_cpu == this_cpu) |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 475 | next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask); |
| 476 | |
| 477 | /* Fastpath: do that cpu by itself. */ |
| 478 | if (next_cpu >= nr_cpu_ids) { |
| 479 | smp_call_function_single(cpu, func, info, wait); |
| 480 | return; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 483 | data = &__get_cpu_var(cfd_data); |
| 484 | csd_lock(&data->csd); |
Milton Miller | 45a5791 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 485 | |
| 486 | /* This BUG_ON verifies our reuse assertions and can be removed */ |
Anton Blanchard | 6dc1989 | 2011-01-20 14:44:33 -0800 | [diff] [blame] | 487 | BUG_ON(atomic_read(&data->refs) || !cpumask_empty(data->cpumask)); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 488 | |
Milton Miller | 45a5791 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 489 | /* |
| 490 | * The global call function queue list add and delete are protected |
| 491 | * by a lock, but the list is traversed without any lock, relying |
| 492 | * on the rcu list add and delete to allow safe concurrent traversal. |
| 493 | * We reuse the call function data without waiting for any grace |
| 494 | * period after some other cpu removes it from the global queue. |
| 495 | * This means a cpu might find our data block as it is being |
| 496 | * filled out. |
| 497 | * |
| 498 | * We hold off the interrupt handler on the other cpu by |
| 499 | * ordering our writes to the cpu mask vs our setting of the |
| 500 | * refs counter. We assert only the cpu owning the data block |
| 501 | * will set a bit in cpumask, and each bit will only be cleared |
| 502 | * by the subject cpu. Each cpu must first find its bit is |
| 503 | * set and then check that refs is set indicating the element is |
| 504 | * ready to be processed, otherwise it must skip the entry. |
| 505 | * |
| 506 | * On the previous iteration refs was set to 0 by another cpu. |
| 507 | * To avoid the use of transitivity, set the counter to 0 here |
| 508 | * so the wmb will pair with the rmb in the interrupt handler. |
| 509 | */ |
| 510 | atomic_set(&data->refs, 0); /* convert 3rd to 1st party write */ |
| 511 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 512 | data->csd.func = func; |
| 513 | data->csd.info = info; |
Milton Miller | 45a5791 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 514 | |
| 515 | /* Ensure 0 refs is visible before mask. Also orders func and info */ |
| 516 | smp_wmb(); |
| 517 | |
| 518 | /* We rely on the "and" being processed before the store */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 519 | cpumask_and(data->cpumask, mask, cpu_online_mask); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 520 | cpumask_clear_cpu(this_cpu, data->cpumask); |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 521 | refs = cpumask_weight(data->cpumask); |
| 522 | |
| 523 | /* Some callers race with other cpus changing the passed mask */ |
| 524 | if (unlikely(!refs)) { |
| 525 | csd_unlock(&data->csd); |
| 526 | return; |
| 527 | } |
Anton Blanchard | 6dc1989 | 2011-01-20 14:44:33 -0800 | [diff] [blame] | 528 | |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 529 | raw_spin_lock_irqsave(&call_function.lock, flags); |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 530 | /* |
| 531 | * Place entry at the _HEAD_ of the list, so that any cpu still |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 532 | * observing the entry in generic_smp_call_function_interrupt() |
| 533 | * will not miss any other list entries: |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 534 | */ |
| 535 | list_add_rcu(&data->csd.list, &call_function.queue); |
Milton Miller | e6cd1e0 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 536 | /* |
Milton Miller | 45a5791 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 537 | * We rely on the wmb() in list_add_rcu to complete our writes |
| 538 | * to the cpumask before this write to refs, which indicates |
| 539 | * data is on the list and is ready to be processed. |
Milton Miller | e6cd1e0 | 2011-03-15 13:27:16 -0600 | [diff] [blame] | 540 | */ |
Milton Miller | 723aae2 | 2011-03-15 13:27:17 -0600 | [diff] [blame] | 541 | atomic_set(&data->refs, refs); |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 542 | raw_spin_unlock_irqrestore(&call_function.lock, flags); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 543 | |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 544 | /* |
| 545 | * Make the list addition visible before sending the ipi. |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 546 | * (IPIs must obey or appear to obey normal Linux cache |
| 547 | * coherency rules -- see comment in generic_exec_single). |
Suresh Siddha | 561920a0 | 2008-10-30 18:28:41 +0100 | [diff] [blame] | 548 | */ |
| 549 | smp_mb(); |
| 550 | |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 551 | /* Send a message to all CPUs in the map */ |
Peter Zijlstra | 8969a5e | 2009-02-25 13:59:47 +0100 | [diff] [blame] | 552 | arch_send_call_function_ipi_mask(data->cpumask); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 553 | |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 554 | /* Optionally wait for the CPUs to complete */ |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 555 | if (wait) |
Peter Zijlstra | 6e27563 | 2009-02-25 13:59:48 +0100 | [diff] [blame] | 556 | csd_lock_wait(&data->csd); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 557 | } |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 558 | EXPORT_SYMBOL(smp_call_function_many); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 559 | |
| 560 | /** |
| 561 | * smp_call_function(): Run a function on all other CPUs. |
| 562 | * @func: The function to run. This must be fast and non-blocking. |
| 563 | * @info: An arbitrary pointer to pass to the function. |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 564 | * @wait: If true, wait (atomically) until function has completed |
| 565 | * on other CPUs. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 566 | * |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 567 | * Returns 0. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 568 | * |
| 569 | * If @wait is true, then returns once @func has returned; otherwise |
Sheng Yang | 72f279b | 2009-10-22 19:19:34 +0800 | [diff] [blame] | 570 | * it returns just before the target cpu calls @func. |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 571 | * |
| 572 | * You must not call this function with disabled interrupts or from a |
| 573 | * hardware interrupt handler or from a bottom half handler. |
| 574 | */ |
David Howells | 3a5f65df | 2010-10-27 17:28:36 +0100 | [diff] [blame] | 575 | 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] | 576 | { |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 577 | preempt_disable(); |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 578 | smp_call_function_many(cpu_online_mask, func, info, wait); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 579 | preempt_enable(); |
Ingo Molnar | 0b13fda | 2009-02-25 16:52:11 +0100 | [diff] [blame] | 580 | |
Rusty Russell | 54b11e6 | 2008-12-30 09:05:16 +1030 | [diff] [blame] | 581 | return 0; |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 582 | } |
| 583 | EXPORT_SYMBOL(smp_call_function); |
| 584 | |
| 585 | void ipi_call_lock(void) |
| 586 | { |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 587 | raw_spin_lock(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | void ipi_call_unlock(void) |
| 591 | { |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 592 | raw_spin_unlock(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | void ipi_call_lock_irq(void) |
| 596 | { |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 597 | raw_spin_lock_irq(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | void ipi_call_unlock_irq(void) |
| 601 | { |
Thomas Gleixner | 9f5a562 | 2009-11-17 15:40:01 +0100 | [diff] [blame] | 602 | raw_spin_unlock_irq(&call_function.lock); |
Jens Axboe | 3d44223 | 2008-06-26 11:21:34 +0200 | [diff] [blame] | 603 | } |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 604 | #endif /* USE_GENERIC_SMP_HELPERS */ |
| 605 | |
Amerigo Wang | 34db18a0 | 2011-03-22 16:34:06 -0700 | [diff] [blame] | 606 | /* Setup configured maximum number of CPUs to activate */ |
| 607 | unsigned int setup_max_cpus = NR_CPUS; |
| 608 | EXPORT_SYMBOL(setup_max_cpus); |
| 609 | |
| 610 | |
| 611 | /* |
| 612 | * Setup routine for controlling SMP activation |
| 613 | * |
| 614 | * Command-line option of "nosmp" or "maxcpus=0" will disable SMP |
| 615 | * activation entirely (the MPS table probe still happens, though). |
| 616 | * |
| 617 | * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer |
| 618 | * greater than 0, limits the maximum number of CPUs activated in |
| 619 | * SMP mode to <NUM>. |
| 620 | */ |
| 621 | |
| 622 | void __weak arch_disable_smp_support(void) { } |
| 623 | |
| 624 | static int __init nosmp(char *str) |
| 625 | { |
| 626 | setup_max_cpus = 0; |
| 627 | arch_disable_smp_support(); |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | early_param("nosmp", nosmp); |
| 633 | |
| 634 | /* this is hard limit */ |
| 635 | static int __init nrcpus(char *str) |
| 636 | { |
| 637 | int nr_cpus; |
| 638 | |
| 639 | get_option(&str, &nr_cpus); |
| 640 | if (nr_cpus > 0 && nr_cpus < nr_cpu_ids) |
| 641 | nr_cpu_ids = nr_cpus; |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | early_param("nr_cpus", nrcpus); |
| 647 | |
| 648 | static int __init maxcpus(char *str) |
| 649 | { |
| 650 | get_option(&str, &setup_max_cpus); |
| 651 | if (setup_max_cpus == 0) |
| 652 | arch_disable_smp_support(); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | early_param("maxcpus", maxcpus); |
| 658 | |
| 659 | /* Setup number of possible processor ids */ |
| 660 | int nr_cpu_ids __read_mostly = NR_CPUS; |
| 661 | EXPORT_SYMBOL(nr_cpu_ids); |
| 662 | |
| 663 | /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */ |
| 664 | void __init setup_nr_cpu_ids(void) |
| 665 | { |
| 666 | nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1; |
| 667 | } |
| 668 | |
| 669 | /* Called by boot processor to activate the rest. */ |
| 670 | void __init smp_init(void) |
| 671 | { |
| 672 | unsigned int cpu; |
| 673 | |
Suresh Siddha | 3bb5d2e | 2012-04-20 17:08:50 -0700 | [diff] [blame^] | 674 | idle_threads_init(); |
| 675 | |
Amerigo Wang | 34db18a0 | 2011-03-22 16:34:06 -0700 | [diff] [blame] | 676 | /* FIXME: This should be done in userspace --RR */ |
| 677 | for_each_present_cpu(cpu) { |
| 678 | if (num_online_cpus() >= setup_max_cpus) |
| 679 | break; |
| 680 | if (!cpu_online(cpu)) |
| 681 | cpu_up(cpu); |
| 682 | } |
| 683 | |
| 684 | /* Any cleanup work */ |
| 685 | printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus()); |
| 686 | smp_cpus_done(setup_max_cpus); |
| 687 | } |
| 688 | |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 689 | /* |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 690 | * Call a function on all processors. May be used during early boot while |
| 691 | * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead |
| 692 | * of local_irq_disable/enable(). |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 693 | */ |
| 694 | int on_each_cpu(void (*func) (void *info), void *info, int wait) |
| 695 | { |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 696 | unsigned long flags; |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 697 | int ret = 0; |
| 698 | |
| 699 | preempt_disable(); |
| 700 | ret = smp_call_function(func, info, wait); |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 701 | local_irq_save(flags); |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 702 | func(info); |
Tejun Heo | bd924e8 | 2011-01-20 12:07:13 +0100 | [diff] [blame] | 703 | local_irq_restore(flags); |
Amerigo Wang | 351f8f8 | 2011-01-12 16:59:39 -0800 | [diff] [blame] | 704 | preempt_enable(); |
| 705 | return ret; |
| 706 | } |
| 707 | EXPORT_SYMBOL(on_each_cpu); |
Gilad Ben-Yossef | 3fc498f | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 708 | |
| 709 | /** |
| 710 | * on_each_cpu_mask(): Run a function on processors specified by |
| 711 | * cpumask, which may include the local processor. |
| 712 | * @mask: The set of cpus to run on (only runs on online subset). |
| 713 | * @func: The function to run. This must be fast and non-blocking. |
| 714 | * @info: An arbitrary pointer to pass to the function. |
| 715 | * @wait: If true, wait (atomically) until function has completed |
| 716 | * on other CPUs. |
| 717 | * |
| 718 | * If @wait is true, then returns once @func has returned. |
| 719 | * |
| 720 | * You must not call this function with disabled interrupts or |
| 721 | * from a hardware interrupt handler or from a bottom half handler. |
| 722 | */ |
| 723 | void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func, |
| 724 | void *info, bool wait) |
| 725 | { |
| 726 | int cpu = get_cpu(); |
| 727 | |
| 728 | smp_call_function_many(mask, func, info, wait); |
| 729 | if (cpumask_test_cpu(cpu, mask)) { |
| 730 | local_irq_disable(); |
| 731 | func(info); |
| 732 | local_irq_enable(); |
| 733 | } |
| 734 | put_cpu(); |
| 735 | } |
| 736 | EXPORT_SYMBOL(on_each_cpu_mask); |
Gilad Ben-Yossef | b3a7e98 | 2012-03-28 14:42:43 -0700 | [diff] [blame] | 737 | |
| 738 | /* |
| 739 | * on_each_cpu_cond(): Call a function on each processor for which |
| 740 | * the supplied function cond_func returns true, optionally waiting |
| 741 | * for all the required CPUs to finish. This may include the local |
| 742 | * processor. |
| 743 | * @cond_func: A callback function that is passed a cpu id and |
| 744 | * the the info parameter. The function is called |
| 745 | * with preemption disabled. The function should |
| 746 | * return a blooean value indicating whether to IPI |
| 747 | * the specified CPU. |
| 748 | * @func: The function to run on all applicable CPUs. |
| 749 | * This must be fast and non-blocking. |
| 750 | * @info: An arbitrary pointer to pass to both functions. |
| 751 | * @wait: If true, wait (atomically) until function has |
| 752 | * completed on other CPUs. |
| 753 | * @gfp_flags: GFP flags to use when allocating the cpumask |
| 754 | * used internally by the function. |
| 755 | * |
| 756 | * The function might sleep if the GFP flags indicates a non |
| 757 | * atomic allocation is allowed. |
| 758 | * |
| 759 | * Preemption is disabled to protect against CPUs going offline but not online. |
| 760 | * CPUs going online during the call will not be seen or sent an IPI. |
| 761 | * |
| 762 | * You must not call this function with disabled interrupts or |
| 763 | * from a hardware interrupt handler or from a bottom half handler. |
| 764 | */ |
| 765 | void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info), |
| 766 | smp_call_func_t func, void *info, bool wait, |
| 767 | gfp_t gfp_flags) |
| 768 | { |
| 769 | cpumask_var_t cpus; |
| 770 | int cpu, ret; |
| 771 | |
| 772 | might_sleep_if(gfp_flags & __GFP_WAIT); |
| 773 | |
| 774 | if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) { |
| 775 | preempt_disable(); |
| 776 | for_each_online_cpu(cpu) |
| 777 | if (cond_func(cpu, info)) |
| 778 | cpumask_set_cpu(cpu, cpus); |
| 779 | on_each_cpu_mask(cpus, func, info, wait); |
| 780 | preempt_enable(); |
| 781 | free_cpumask_var(cpus); |
| 782 | } else { |
| 783 | /* |
| 784 | * No free cpumask, bother. No matter, we'll |
| 785 | * just have to IPI them one by one. |
| 786 | */ |
| 787 | preempt_disable(); |
| 788 | for_each_online_cpu(cpu) |
| 789 | if (cond_func(cpu, info)) { |
| 790 | ret = smp_call_function_single(cpu, func, |
| 791 | info, wait); |
| 792 | WARN_ON_ONCE(!ret); |
| 793 | } |
| 794 | preempt_enable(); |
| 795 | } |
| 796 | } |
| 797 | EXPORT_SYMBOL(on_each_cpu_cond); |