smp.c 21 KB

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