smp.c 21 KB

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