smp.c 20 KB

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