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