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