smp.c 19 KB

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