smp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. static int
  30. hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
  31. {
  32. long cpu = (long)hcpu;
  33. struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
  34. switch (action) {
  35. case CPU_UP_PREPARE:
  36. case CPU_UP_PREPARE_FROZEN:
  37. if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  38. cpu_to_node(cpu)))
  39. return notifier_from_errno(-ENOMEM);
  40. cfd->csd = alloc_percpu(struct call_single_data);
  41. if (!cfd->csd) {
  42. free_cpumask_var(cfd->cpumask);
  43. return notifier_from_errno(-ENOMEM);
  44. }
  45. break;
  46. #ifdef CONFIG_HOTPLUG_CPU
  47. case CPU_UP_CANCELED:
  48. case CPU_UP_CANCELED_FROZEN:
  49. /* Fall-through to the CPU_DEAD[_FROZEN] case. */
  50. case CPU_DEAD:
  51. case CPU_DEAD_FROZEN:
  52. free_cpumask_var(cfd->cpumask);
  53. free_percpu(cfd->csd);
  54. break;
  55. case CPU_DYING:
  56. case CPU_DYING_FROZEN:
  57. /*
  58. * The IPIs for the smp-call-function callbacks queued by other
  59. * CPUs might arrive late, either due to hardware latencies or
  60. * because this CPU disabled interrupts (inside stop-machine)
  61. * before the IPIs were sent. So flush out any pending callbacks
  62. * explicitly (without waiting for the IPIs to arrive), to
  63. * ensure that the outgoing CPU doesn't go offline with work
  64. * still pending.
  65. */
  66. flush_smp_call_function_queue(false);
  67. break;
  68. #endif
  69. };
  70. return NOTIFY_OK;
  71. }
  72. static struct notifier_block hotplug_cfd_notifier = {
  73. .notifier_call = hotplug_cfd,
  74. };
  75. void __init call_function_init(void)
  76. {
  77. void *cpu = (void *)(long)smp_processor_id();
  78. int i;
  79. for_each_possible_cpu(i)
  80. init_llist_head(&per_cpu(call_single_queue, i));
  81. hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
  82. register_cpu_notifier(&hotplug_cfd_notifier);
  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 void csd_lock_wait(struct call_single_data *csd)
  92. {
  93. while (smp_load_acquire(&csd->flags) & CSD_FLAG_LOCK)
  94. cpu_relax();
  95. }
  96. static void csd_lock(struct call_single_data *csd)
  97. {
  98. csd_lock_wait(csd);
  99. csd->flags |= CSD_FLAG_LOCK;
  100. /*
  101. * prevent CPU from reordering the above assignment
  102. * to ->flags with any subsequent assignments to other
  103. * fields of the specified call_single_data structure:
  104. */
  105. smp_wmb();
  106. }
  107. static void csd_unlock(struct call_single_data *csd)
  108. {
  109. WARN_ON(!(csd->flags & CSD_FLAG_LOCK));
  110. /*
  111. * ensure we're all done before releasing data:
  112. */
  113. smp_store_release(&csd->flags, 0);
  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)
  123. {
  124. if (cpu == smp_processor_id()) {
  125. unsigned long flags;
  126. /*
  127. * We can unlock early even for the synchronous on-stack case,
  128. * since we're doing this from the same CPU..
  129. */
  130. csd_unlock(csd);
  131. local_irq_save(flags);
  132. func(info);
  133. local_irq_restore(flags);
  134. return 0;
  135. }
  136. if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
  137. csd_unlock(csd);
  138. return -ENXIO;
  139. }
  140. csd->func = func;
  141. csd->info = info;
  142. /*
  143. * The list addition should be visible before sending the IPI
  144. * handler locks the list to pull the entry off it because of
  145. * normal cache coherency rules implied by spinlocks.
  146. *
  147. * If IPIs can go out of order to the cache coherency protocol
  148. * in an architecture, sufficient synchronisation should be added
  149. * to arch code to make it appear to obey cache coherency WRT
  150. * locking and barrier primitives. Generic code isn't really
  151. * equipped to do the right thing...
  152. */
  153. if (llist_add(&csd->llist, &per_cpu(call_single_queue, cpu)))
  154. arch_send_call_function_single_ipi(cpu);
  155. return 0;
  156. }
  157. /**
  158. * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
  159. *
  160. * Invoked by arch to handle an IPI for call function single.
  161. * Must be called with interrupts disabled.
  162. */
  163. void generic_smp_call_function_single_interrupt(void)
  164. {
  165. flush_smp_call_function_queue(true);
  166. }
  167. /**
  168. * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
  169. *
  170. * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
  171. * offline CPU. Skip this check if set to 'false'.
  172. *
  173. * Flush any pending smp-call-function callbacks queued on this CPU. This is
  174. * invoked by the generic IPI handler, as well as by a CPU about to go offline,
  175. * to ensure that all pending IPI callbacks are run before it goes completely
  176. * offline.
  177. *
  178. * Loop through the call_single_queue and run all the queued callbacks.
  179. * Must be called with interrupts disabled.
  180. */
  181. static void flush_smp_call_function_queue(bool warn_cpu_offline)
  182. {
  183. struct llist_head *head;
  184. struct llist_node *entry;
  185. struct call_single_data *csd, *csd_next;
  186. static bool warned;
  187. WARN_ON(!irqs_disabled());
  188. head = this_cpu_ptr(&call_single_queue);
  189. entry = llist_del_all(head);
  190. entry = llist_reverse_order(entry);
  191. /* There shouldn't be any pending callbacks on an offline CPU. */
  192. if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
  193. !warned && !llist_empty(head))) {
  194. warned = true;
  195. WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
  196. /*
  197. * We don't have to use the _safe() variant here
  198. * because we are not invoking the IPI handlers yet.
  199. */
  200. llist_for_each_entry(csd, entry, llist)
  201. pr_warn("IPI callback %pS sent to offline CPU\n",
  202. csd->func);
  203. }
  204. llist_for_each_entry_safe(csd, csd_next, entry, llist) {
  205. smp_call_func_t func = csd->func;
  206. void *info = csd->info;
  207. /* Do we wait until *after* callback? */
  208. if (csd->flags & CSD_FLAG_SYNCHRONOUS) {
  209. func(info);
  210. csd_unlock(csd);
  211. } else {
  212. csd_unlock(csd);
  213. func(info);
  214. }
  215. }
  216. /*
  217. * Handle irq works queued remotely by irq_work_queue_on().
  218. * Smp functions above are typically synchronous so they
  219. * better run first since some other CPUs may be busy waiting
  220. * for them.
  221. */
  222. irq_work_run();
  223. }
  224. /*
  225. * smp_call_function_single - Run a function on a specific CPU
  226. * @func: The function to run. This must be fast and non-blocking.
  227. * @info: An arbitrary pointer to pass to the function.
  228. * @wait: If true, wait until function has completed on other CPUs.
  229. *
  230. * Returns 0 on success, else a negative status code.
  231. */
  232. int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
  233. int wait)
  234. {
  235. struct call_single_data *csd;
  236. struct call_single_data csd_stack = { .flags = CSD_FLAG_LOCK | CSD_FLAG_SYNCHRONOUS };
  237. int this_cpu;
  238. int err;
  239. /*
  240. * prevent preemption and reschedule on another processor,
  241. * as well as CPU removal
  242. */
  243. this_cpu = get_cpu();
  244. /*
  245. * Can deadlock when called with interrupts disabled.
  246. * We allow cpu's that are not yet online though, as no one else can
  247. * send smp call function interrupt to this cpu and as such deadlocks
  248. * can't happen.
  249. */
  250. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  251. && !oops_in_progress);
  252. csd = &csd_stack;
  253. if (!wait) {
  254. csd = this_cpu_ptr(&csd_data);
  255. csd_lock(csd);
  256. }
  257. err = generic_exec_single(cpu, csd, func, info);
  258. if (wait)
  259. csd_lock_wait(csd);
  260. put_cpu();
  261. return err;
  262. }
  263. EXPORT_SYMBOL(smp_call_function_single);
  264. /**
  265. * smp_call_function_single_async(): Run an asynchronous function on a
  266. * specific CPU.
  267. * @cpu: The CPU to run on.
  268. * @csd: Pre-allocated and setup data structure
  269. *
  270. * Like smp_call_function_single(), but the call is asynchonous and
  271. * can thus be done from contexts with disabled interrupts.
  272. *
  273. * The caller passes his own pre-allocated data structure
  274. * (ie: embedded in an object) and is responsible for synchronizing it
  275. * such that the IPIs performed on the @csd are strictly serialized.
  276. *
  277. * NOTE: Be careful, there is unfortunately no current debugging facility to
  278. * validate the correctness of this serialization.
  279. */
  280. int smp_call_function_single_async(int cpu, struct call_single_data *csd)
  281. {
  282. int err = 0;
  283. preempt_disable();
  284. /* We could deadlock if we have to wait here with interrupts disabled! */
  285. if (WARN_ON_ONCE(csd->flags & CSD_FLAG_LOCK))
  286. csd_lock_wait(csd);
  287. csd->flags = CSD_FLAG_LOCK;
  288. smp_wmb();
  289. err = generic_exec_single(cpu, csd, csd->func, csd->info);
  290. preempt_enable();
  291. return err;
  292. }
  293. EXPORT_SYMBOL_GPL(smp_call_function_single_async);
  294. /*
  295. * smp_call_function_any - Run a function on any of the given cpus
  296. * @mask: The mask of cpus it can run on.
  297. * @func: The function to run. This must be fast and non-blocking.
  298. * @info: An arbitrary pointer to pass to the function.
  299. * @wait: If true, wait until function has completed.
  300. *
  301. * Returns 0 on success, else a negative status code (if no cpus were online).
  302. *
  303. * Selection preference:
  304. * 1) current cpu if in @mask
  305. * 2) any cpu of current node if in @mask
  306. * 3) any other online cpu in @mask
  307. */
  308. int smp_call_function_any(const struct cpumask *mask,
  309. smp_call_func_t func, void *info, int wait)
  310. {
  311. unsigned int cpu;
  312. const struct cpumask *nodemask;
  313. int ret;
  314. /* Try for same CPU (cheapest) */
  315. cpu = get_cpu();
  316. if (cpumask_test_cpu(cpu, mask))
  317. goto call;
  318. /* Try for same node. */
  319. nodemask = cpumask_of_node(cpu_to_node(cpu));
  320. for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
  321. cpu = cpumask_next_and(cpu, nodemask, mask)) {
  322. if (cpu_online(cpu))
  323. goto call;
  324. }
  325. /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
  326. cpu = cpumask_any_and(mask, cpu_online_mask);
  327. call:
  328. ret = smp_call_function_single(cpu, func, info, wait);
  329. put_cpu();
  330. return ret;
  331. }
  332. EXPORT_SYMBOL_GPL(smp_call_function_any);
  333. /**
  334. * smp_call_function_many(): Run a function on a set of other CPUs.
  335. * @mask: The set of cpus to run on (only runs on online subset).
  336. * @func: The function to run. This must be fast and non-blocking.
  337. * @info: An arbitrary pointer to pass to the function.
  338. * @wait: If true, wait (atomically) until function has completed
  339. * on other CPUs.
  340. *
  341. * If @wait is true, then returns once @func has returned.
  342. *
  343. * You must not call this function with disabled interrupts or from a
  344. * hardware interrupt handler or from a bottom half handler. Preemption
  345. * must be disabled when calling this function.
  346. */
  347. void smp_call_function_many(const struct cpumask *mask,
  348. smp_call_func_t func, void *info, bool wait)
  349. {
  350. struct call_function_data *cfd;
  351. int cpu, next_cpu, this_cpu = smp_processor_id();
  352. /*
  353. * Can deadlock when called with interrupts disabled.
  354. * We allow cpu's that are not yet online though, as no one else can
  355. * send smp call function interrupt to this cpu and as such deadlocks
  356. * can't happen.
  357. */
  358. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  359. && !oops_in_progress && !early_boot_irqs_disabled);
  360. /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
  361. cpu = cpumask_first_and(mask, cpu_online_mask);
  362. if (cpu == this_cpu)
  363. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  364. /* No online cpus? We're done. */
  365. if (cpu >= nr_cpu_ids)
  366. return;
  367. /* Do we have another CPU which isn't us? */
  368. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  369. if (next_cpu == this_cpu)
  370. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  371. /* Fastpath: do that cpu by itself. */
  372. if (next_cpu >= nr_cpu_ids) {
  373. smp_call_function_single(cpu, func, info, wait);
  374. return;
  375. }
  376. cfd = this_cpu_ptr(&cfd_data);
  377. cpumask_and(cfd->cpumask, mask, cpu_online_mask);
  378. cpumask_clear_cpu(this_cpu, cfd->cpumask);
  379. /* Some callers race with other cpus changing the passed mask */
  380. if (unlikely(!cpumask_weight(cfd->cpumask)))
  381. return;
  382. for_each_cpu(cpu, cfd->cpumask) {
  383. struct call_single_data *csd = per_cpu_ptr(cfd->csd, cpu);
  384. csd_lock(csd);
  385. if (wait)
  386. csd->flags |= CSD_FLAG_SYNCHRONOUS;
  387. csd->func = func;
  388. csd->info = info;
  389. llist_add(&csd->llist, &per_cpu(call_single_queue, cpu));
  390. }
  391. /* Send a message to all CPUs in the map */
  392. arch_send_call_function_ipi_mask(cfd->cpumask);
  393. if (wait) {
  394. for_each_cpu(cpu, cfd->cpumask) {
  395. struct call_single_data *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. 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. void __weak smp_announce(void)
  473. {
  474. printk(KERN_INFO "Brought up %d CPUs\n", num_online_cpus());
  475. }
  476. /* Called by boot processor to activate the rest. */
  477. void __init smp_init(void)
  478. {
  479. unsigned int cpu;
  480. idle_threads_init();
  481. /* FIXME: This should be done in userspace --RR */
  482. for_each_present_cpu(cpu) {
  483. if (num_online_cpus() >= setup_max_cpus)
  484. break;
  485. if (!cpu_online(cpu))
  486. cpu_up(cpu);
  487. }
  488. /* Any cleanup work */
  489. smp_announce();
  490. smp_cpus_done(setup_max_cpus);
  491. }
  492. /*
  493. * Call a function on all processors. May be used during early boot while
  494. * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
  495. * of local_irq_disable/enable().
  496. */
  497. int on_each_cpu(void (*func) (void *info), void *info, int wait)
  498. {
  499. unsigned long flags;
  500. int ret = 0;
  501. preempt_disable();
  502. ret = smp_call_function(func, info, wait);
  503. local_irq_save(flags);
  504. func(info);
  505. local_irq_restore(flags);
  506. preempt_enable();
  507. return ret;
  508. }
  509. EXPORT_SYMBOL(on_each_cpu);
  510. /**
  511. * on_each_cpu_mask(): Run a function on processors specified by
  512. * cpumask, which may include the local processor.
  513. * @mask: The set of cpus to run on (only runs on online subset).
  514. * @func: The function to run. This must be fast and non-blocking.
  515. * @info: An arbitrary pointer to pass to the function.
  516. * @wait: If true, wait (atomically) until function has completed
  517. * on other CPUs.
  518. *
  519. * If @wait is true, then returns once @func has returned.
  520. *
  521. * You must not call this function with disabled interrupts or from a
  522. * hardware interrupt handler or from a bottom half handler. The
  523. * exception is that it may be used during early boot while
  524. * early_boot_irqs_disabled is set.
  525. */
  526. void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
  527. void *info, bool wait)
  528. {
  529. int cpu = get_cpu();
  530. smp_call_function_many(mask, func, info, wait);
  531. if (cpumask_test_cpu(cpu, mask)) {
  532. unsigned long flags;
  533. local_irq_save(flags);
  534. func(info);
  535. local_irq_restore(flags);
  536. }
  537. put_cpu();
  538. }
  539. EXPORT_SYMBOL(on_each_cpu_mask);
  540. /*
  541. * on_each_cpu_cond(): Call a function on each processor for which
  542. * the supplied function cond_func returns true, optionally waiting
  543. * for all the required CPUs to finish. This may include the local
  544. * processor.
  545. * @cond_func: A callback function that is passed a cpu id and
  546. * the the info parameter. The function is called
  547. * with preemption disabled. The function should
  548. * return a blooean value indicating whether to IPI
  549. * the specified CPU.
  550. * @func: The function to run on all applicable CPUs.
  551. * This must be fast and non-blocking.
  552. * @info: An arbitrary pointer to pass to both functions.
  553. * @wait: If true, wait (atomically) until function has
  554. * completed on other CPUs.
  555. * @gfp_flags: GFP flags to use when allocating the cpumask
  556. * used internally by the function.
  557. *
  558. * The function might sleep if the GFP flags indicates a non
  559. * atomic allocation is allowed.
  560. *
  561. * Preemption is disabled to protect against CPUs going offline but not online.
  562. * CPUs going online during the call will not be seen or sent an IPI.
  563. *
  564. * You must not call this function with disabled interrupts or
  565. * from a hardware interrupt handler or from a bottom half handler.
  566. */
  567. void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
  568. smp_call_func_t func, void *info, bool wait,
  569. gfp_t gfp_flags)
  570. {
  571. cpumask_var_t cpus;
  572. int cpu, ret;
  573. might_sleep_if(gfpflags_allow_blocking(gfp_flags));
  574. if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
  575. preempt_disable();
  576. for_each_online_cpu(cpu)
  577. if (cond_func(cpu, info))
  578. cpumask_set_cpu(cpu, cpus);
  579. on_each_cpu_mask(cpus, func, info, wait);
  580. preempt_enable();
  581. free_cpumask_var(cpus);
  582. } else {
  583. /*
  584. * No free cpumask, bother. No matter, we'll
  585. * just have to IPI them one by one.
  586. */
  587. preempt_disable();
  588. for_each_online_cpu(cpu)
  589. if (cond_func(cpu, info)) {
  590. ret = smp_call_function_single(cpu, func,
  591. info, wait);
  592. WARN_ON_ONCE(ret);
  593. }
  594. preempt_enable();
  595. }
  596. }
  597. EXPORT_SYMBOL(on_each_cpu_cond);
  598. static void do_nothing(void *unused)
  599. {
  600. }
  601. /**
  602. * kick_all_cpus_sync - Force all cpus out of idle
  603. *
  604. * Used to synchronize the update of pm_idle function pointer. It's
  605. * called after the pointer is updated and returns after the dummy
  606. * callback function has been executed on all cpus. The execution of
  607. * the function can only happen on the remote cpus after they have
  608. * left the idle function which had been called via pm_idle function
  609. * pointer. So it's guaranteed that nothing uses the previous pointer
  610. * anymore.
  611. */
  612. void kick_all_cpus_sync(void)
  613. {
  614. /* Make sure the change is visible before we kick the cpus */
  615. smp_mb();
  616. smp_call_function(do_nothing, NULL, 1);
  617. }
  618. EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
  619. /**
  620. * wake_up_all_idle_cpus - break all cpus out of idle
  621. * wake_up_all_idle_cpus try to break all cpus which is in idle state even
  622. * including idle polling cpus, for non-idle cpus, we will do nothing
  623. * for them.
  624. */
  625. void wake_up_all_idle_cpus(void)
  626. {
  627. int cpu;
  628. preempt_disable();
  629. for_each_online_cpu(cpu) {
  630. if (cpu == smp_processor_id())
  631. continue;
  632. wake_up_if_idle(cpu);
  633. }
  634. preempt_enable();
  635. }
  636. EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);