smp.c 21 KB

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