smp.c 21 KB

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