smp.c 22 KB

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