smp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Generic helpers for smp ipi calls
  3. *
  4. * (C) Jens Axboe <jens.axboe@oracle.com> 2008
  5. */
  6. #include <linux/rcupdate.h>
  7. #include <linux/rculist.h>
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/percpu.h>
  11. #include <linux/init.h>
  12. #include <linux/gfp.h>
  13. #include <linux/smp.h>
  14. #include <linux/cpu.h>
  15. #include "smpboot.h"
  16. #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
  17. static struct {
  18. struct list_head queue;
  19. raw_spinlock_t lock;
  20. } call_function __cacheline_aligned_in_smp =
  21. {
  22. .queue = LIST_HEAD_INIT(call_function.queue),
  23. .lock = __RAW_SPIN_LOCK_UNLOCKED(call_function.lock),
  24. };
  25. enum {
  26. CSD_FLAG_LOCK = 0x01,
  27. };
  28. struct call_function_data {
  29. struct call_single_data csd;
  30. atomic_t refs;
  31. cpumask_var_t cpumask;
  32. };
  33. static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
  34. struct call_single_queue {
  35. struct list_head list;
  36. raw_spinlock_t lock;
  37. };
  38. static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
  39. static int
  40. hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
  41. {
  42. long cpu = (long)hcpu;
  43. struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
  44. switch (action) {
  45. case CPU_UP_PREPARE:
  46. case CPU_UP_PREPARE_FROZEN:
  47. if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  48. cpu_to_node(cpu)))
  49. return notifier_from_errno(-ENOMEM);
  50. break;
  51. #ifdef CONFIG_HOTPLUG_CPU
  52. case CPU_UP_CANCELED:
  53. case CPU_UP_CANCELED_FROZEN:
  54. case CPU_DEAD:
  55. case CPU_DEAD_FROZEN:
  56. free_cpumask_var(cfd->cpumask);
  57. break;
  58. #endif
  59. };
  60. return NOTIFY_OK;
  61. }
  62. static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
  63. .notifier_call = hotplug_cfd,
  64. };
  65. void __init call_function_init(void)
  66. {
  67. void *cpu = (void *)(long)smp_processor_id();
  68. int i;
  69. for_each_possible_cpu(i) {
  70. struct call_single_queue *q = &per_cpu(call_single_queue, i);
  71. raw_spin_lock_init(&q->lock);
  72. INIT_LIST_HEAD(&q->list);
  73. }
  74. hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
  75. register_cpu_notifier(&hotplug_cfd_notifier);
  76. }
  77. /*
  78. * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
  79. *
  80. * For non-synchronous ipi calls the csd can still be in use by the
  81. * previous function call. For multi-cpu calls its even more interesting
  82. * as we'll have to ensure no other cpu is observing our csd.
  83. */
  84. static void csd_lock_wait(struct call_single_data *data)
  85. {
  86. while (data->flags & CSD_FLAG_LOCK)
  87. cpu_relax();
  88. }
  89. static void csd_lock(struct call_single_data *data)
  90. {
  91. csd_lock_wait(data);
  92. data->flags = CSD_FLAG_LOCK;
  93. /*
  94. * prevent CPU from reordering the above assignment
  95. * to ->flags with any subsequent assignments to other
  96. * fields of the specified call_single_data structure:
  97. */
  98. smp_mb();
  99. }
  100. static void csd_unlock(struct call_single_data *data)
  101. {
  102. WARN_ON(!(data->flags & CSD_FLAG_LOCK));
  103. /*
  104. * ensure we're all done before releasing data:
  105. */
  106. smp_mb();
  107. data->flags &= ~CSD_FLAG_LOCK;
  108. }
  109. /*
  110. * Insert a previously allocated call_single_data element
  111. * for execution on the given CPU. data must already have
  112. * ->func, ->info, and ->flags set.
  113. */
  114. static
  115. void generic_exec_single(int cpu, struct call_single_data *data, int wait)
  116. {
  117. struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
  118. unsigned long flags;
  119. int ipi;
  120. raw_spin_lock_irqsave(&dst->lock, flags);
  121. ipi = list_empty(&dst->list);
  122. list_add_tail(&data->list, &dst->list);
  123. raw_spin_unlock_irqrestore(&dst->lock, flags);
  124. /*
  125. * The list addition should be visible before sending the IPI
  126. * handler locks the list to pull the entry off it because of
  127. * normal cache coherency rules implied by spinlocks.
  128. *
  129. * If IPIs can go out of order to the cache coherency protocol
  130. * in an architecture, sufficient synchronisation should be added
  131. * to arch code to make it appear to obey cache coherency WRT
  132. * locking and barrier primitives. Generic code isn't really
  133. * equipped to do the right thing...
  134. */
  135. if (ipi)
  136. arch_send_call_function_single_ipi(cpu);
  137. if (wait)
  138. csd_lock_wait(data);
  139. }
  140. /*
  141. * Invoked by arch to handle an IPI for call function. Must be called with
  142. * interrupts disabled.
  143. */
  144. void generic_smp_call_function_interrupt(void)
  145. {
  146. struct call_function_data *data;
  147. int cpu = smp_processor_id();
  148. /*
  149. * Shouldn't receive this interrupt on a cpu that is not yet online.
  150. */
  151. WARN_ON_ONCE(!cpu_online(cpu));
  152. /*
  153. * Ensure entry is visible on call_function_queue after we have
  154. * entered the IPI. See comment in smp_call_function_many.
  155. * If we don't have this, then we may miss an entry on the list
  156. * and never get another IPI to process it.
  157. */
  158. smp_mb();
  159. /*
  160. * It's ok to use list_for_each_rcu() here even though we may
  161. * delete 'pos', since list_del_rcu() doesn't clear ->next
  162. */
  163. list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
  164. int refs;
  165. smp_call_func_t func;
  166. /*
  167. * Since we walk the list without any locks, we might
  168. * see an entry that was completed, removed from the
  169. * list and is in the process of being reused.
  170. *
  171. * We must check that the cpu is in the cpumask before
  172. * checking the refs, and both must be set before
  173. * executing the callback on this cpu.
  174. */
  175. if (!cpumask_test_cpu(cpu, data->cpumask))
  176. continue;
  177. smp_rmb();
  178. if (atomic_read(&data->refs) == 0)
  179. continue;
  180. func = data->csd.func; /* save for later warn */
  181. func(data->csd.info);
  182. /*
  183. * If the cpu mask is not still set then func enabled
  184. * interrupts (BUG), and this cpu took another smp call
  185. * function interrupt and executed func(info) twice
  186. * on this cpu. That nested execution decremented refs.
  187. */
  188. if (!cpumask_test_and_clear_cpu(cpu, data->cpumask)) {
  189. WARN(1, "%pf enabled interrupts and double executed\n", func);
  190. continue;
  191. }
  192. refs = atomic_dec_return(&data->refs);
  193. WARN_ON(refs < 0);
  194. if (refs)
  195. continue;
  196. WARN_ON(!cpumask_empty(data->cpumask));
  197. raw_spin_lock(&call_function.lock);
  198. list_del_rcu(&data->csd.list);
  199. raw_spin_unlock(&call_function.lock);
  200. csd_unlock(&data->csd);
  201. }
  202. }
  203. /*
  204. * Invoked by arch to handle an IPI for call function single. Must be
  205. * called from the arch with interrupts disabled.
  206. */
  207. void generic_smp_call_function_single_interrupt(void)
  208. {
  209. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  210. unsigned int data_flags;
  211. LIST_HEAD(list);
  212. /*
  213. * Shouldn't receive this interrupt on a cpu that is not yet online.
  214. */
  215. WARN_ON_ONCE(!cpu_online(smp_processor_id()));
  216. raw_spin_lock(&q->lock);
  217. list_replace_init(&q->list, &list);
  218. raw_spin_unlock(&q->lock);
  219. while (!list_empty(&list)) {
  220. struct call_single_data *data;
  221. data = list_entry(list.next, struct call_single_data, list);
  222. list_del(&data->list);
  223. /*
  224. * 'data' can be invalid after this call if flags == 0
  225. * (when called through generic_exec_single()),
  226. * so save them away before making the call:
  227. */
  228. data_flags = data->flags;
  229. data->func(data->info);
  230. /*
  231. * Unlocked CSDs are valid through generic_exec_single():
  232. */
  233. if (data_flags & CSD_FLAG_LOCK)
  234. csd_unlock(data);
  235. }
  236. }
  237. static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
  238. /*
  239. * smp_call_function_single - Run a function on a specific CPU
  240. * @func: The function to run. This must be fast and non-blocking.
  241. * @info: An arbitrary pointer to pass to the function.
  242. * @wait: If true, wait until function has completed on other CPUs.
  243. *
  244. * Returns 0 on success, else a negative status code.
  245. */
  246. int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
  247. int wait)
  248. {
  249. struct call_single_data d = {
  250. .flags = 0,
  251. };
  252. unsigned long flags;
  253. int this_cpu;
  254. int err = 0;
  255. /*
  256. * prevent preemption and reschedule on another processor,
  257. * as well as CPU removal
  258. */
  259. this_cpu = get_cpu();
  260. /*
  261. * Can deadlock when called with interrupts disabled.
  262. * We allow cpu's that are not yet online though, as no one else can
  263. * send smp call function interrupt to this cpu and as such deadlocks
  264. * can't happen.
  265. */
  266. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  267. && !oops_in_progress);
  268. if (cpu == this_cpu) {
  269. local_irq_save(flags);
  270. func(info);
  271. local_irq_restore(flags);
  272. } else {
  273. if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
  274. struct call_single_data *data = &d;
  275. if (!wait)
  276. data = &__get_cpu_var(csd_data);
  277. csd_lock(data);
  278. data->func = func;
  279. data->info = info;
  280. generic_exec_single(cpu, data, wait);
  281. } else {
  282. err = -ENXIO; /* CPU not online */
  283. }
  284. }
  285. put_cpu();
  286. return err;
  287. }
  288. EXPORT_SYMBOL(smp_call_function_single);
  289. /*
  290. * smp_call_function_any - Run a function on any of the given cpus
  291. * @mask: The mask of cpus it can run on.
  292. * @func: The function to run. This must be fast and non-blocking.
  293. * @info: An arbitrary pointer to pass to the function.
  294. * @wait: If true, wait until function has completed.
  295. *
  296. * Returns 0 on success, else a negative status code (if no cpus were online).
  297. * Note that @wait will be implicitly turned on in case of allocation failures,
  298. * since we fall back to on-stack allocation.
  299. *
  300. * Selection preference:
  301. * 1) current cpu if in @mask
  302. * 2) any cpu of current node if in @mask
  303. * 3) any other online cpu in @mask
  304. */
  305. int smp_call_function_any(const struct cpumask *mask,
  306. smp_call_func_t func, void *info, int wait)
  307. {
  308. unsigned int cpu;
  309. const struct cpumask *nodemask;
  310. int ret;
  311. /* Try for same CPU (cheapest) */
  312. cpu = get_cpu();
  313. if (cpumask_test_cpu(cpu, mask))
  314. goto call;
  315. /* Try for same node. */
  316. nodemask = cpumask_of_node(cpu_to_node(cpu));
  317. for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
  318. cpu = cpumask_next_and(cpu, nodemask, mask)) {
  319. if (cpu_online(cpu))
  320. goto call;
  321. }
  322. /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
  323. cpu = cpumask_any_and(mask, cpu_online_mask);
  324. call:
  325. ret = smp_call_function_single(cpu, func, info, wait);
  326. put_cpu();
  327. return ret;
  328. }
  329. EXPORT_SYMBOL_GPL(smp_call_function_any);
  330. /**
  331. * __smp_call_function_single(): Run a function on a specific CPU
  332. * @cpu: The CPU to run on.
  333. * @data: Pre-allocated and setup data structure
  334. * @wait: If true, wait until function has completed on specified CPU.
  335. *
  336. * Like smp_call_function_single(), but allow caller to pass in a
  337. * pre-allocated data structure. Useful for embedding @data inside
  338. * other structures, for instance.
  339. */
  340. void __smp_call_function_single(int cpu, struct call_single_data *data,
  341. int wait)
  342. {
  343. unsigned int this_cpu;
  344. unsigned long flags;
  345. this_cpu = get_cpu();
  346. /*
  347. * Can deadlock when called with interrupts disabled.
  348. * We allow cpu's that are not yet online though, as no one else can
  349. * send smp call function interrupt to this cpu and as such deadlocks
  350. * can't happen.
  351. */
  352. WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
  353. && !oops_in_progress);
  354. if (cpu == this_cpu) {
  355. local_irq_save(flags);
  356. data->func(data->info);
  357. local_irq_restore(flags);
  358. } else {
  359. csd_lock(data);
  360. generic_exec_single(cpu, data, wait);
  361. }
  362. put_cpu();
  363. }
  364. /**
  365. * smp_call_function_many(): Run a function on a set of other CPUs.
  366. * @mask: The set of cpus to run on (only runs on online subset).
  367. * @func: The function to run. This must be fast and non-blocking.
  368. * @info: An arbitrary pointer to pass to the function.
  369. * @wait: If true, wait (atomically) until function has completed
  370. * on other CPUs.
  371. *
  372. * If @wait is true, then returns once @func has returned.
  373. *
  374. * You must not call this function with disabled interrupts or from a
  375. * hardware interrupt handler or from a bottom half handler. Preemption
  376. * must be disabled when calling this function.
  377. */
  378. void smp_call_function_many(const struct cpumask *mask,
  379. smp_call_func_t func, void *info, bool wait)
  380. {
  381. struct call_function_data *data;
  382. unsigned long flags;
  383. int refs, cpu, next_cpu, this_cpu = smp_processor_id();
  384. /*
  385. * Can deadlock when called with interrupts disabled.
  386. * We allow cpu's that are not yet online though, as no one else can
  387. * send smp call function interrupt to this cpu and as such deadlocks
  388. * can't happen.
  389. */
  390. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  391. && !oops_in_progress && !early_boot_irqs_disabled);
  392. /* Try to fastpath. So, what's a CPU they want? Ignoring this one. */
  393. cpu = cpumask_first_and(mask, cpu_online_mask);
  394. if (cpu == this_cpu)
  395. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  396. /* No online cpus? We're done. */
  397. if (cpu >= nr_cpu_ids)
  398. return;
  399. /* Do we have another CPU which isn't us? */
  400. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  401. if (next_cpu == this_cpu)
  402. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  403. /* Fastpath: do that cpu by itself. */
  404. if (next_cpu >= nr_cpu_ids) {
  405. smp_call_function_single(cpu, func, info, wait);
  406. return;
  407. }
  408. data = &__get_cpu_var(cfd_data);
  409. csd_lock(&data->csd);
  410. /* This BUG_ON verifies our reuse assertions and can be removed */
  411. BUG_ON(atomic_read(&data->refs) || !cpumask_empty(data->cpumask));
  412. /*
  413. * The global call function queue list add and delete are protected
  414. * by a lock, but the list is traversed without any lock, relying
  415. * on the rcu list add and delete to allow safe concurrent traversal.
  416. * We reuse the call function data without waiting for any grace
  417. * period after some other cpu removes it from the global queue.
  418. * This means a cpu might find our data block as it is being
  419. * filled out.
  420. *
  421. * We hold off the interrupt handler on the other cpu by
  422. * ordering our writes to the cpu mask vs our setting of the
  423. * refs counter. We assert only the cpu owning the data block
  424. * will set a bit in cpumask, and each bit will only be cleared
  425. * by the subject cpu. Each cpu must first find its bit is
  426. * set and then check that refs is set indicating the element is
  427. * ready to be processed, otherwise it must skip the entry.
  428. *
  429. * On the previous iteration refs was set to 0 by another cpu.
  430. * To avoid the use of transitivity, set the counter to 0 here
  431. * so the wmb will pair with the rmb in the interrupt handler.
  432. */
  433. atomic_set(&data->refs, 0); /* convert 3rd to 1st party write */
  434. data->csd.func = func;
  435. data->csd.info = info;
  436. /* Ensure 0 refs is visible before mask. Also orders func and info */
  437. smp_wmb();
  438. /* We rely on the "and" being processed before the store */
  439. cpumask_and(data->cpumask, mask, cpu_online_mask);
  440. cpumask_clear_cpu(this_cpu, data->cpumask);
  441. refs = cpumask_weight(data->cpumask);
  442. /* Some callers race with other cpus changing the passed mask */
  443. if (unlikely(!refs)) {
  444. csd_unlock(&data->csd);
  445. return;
  446. }
  447. raw_spin_lock_irqsave(&call_function.lock, flags);
  448. /*
  449. * Place entry at the _HEAD_ of the list, so that any cpu still
  450. * observing the entry in generic_smp_call_function_interrupt()
  451. * will not miss any other list entries:
  452. */
  453. list_add_rcu(&data->csd.list, &call_function.queue);
  454. /*
  455. * We rely on the wmb() in list_add_rcu to complete our writes
  456. * to the cpumask before this write to refs, which indicates
  457. * data is on the list and is ready to be processed.
  458. */
  459. atomic_set(&data->refs, refs);
  460. raw_spin_unlock_irqrestore(&call_function.lock, flags);
  461. /*
  462. * Make the list addition visible before sending the ipi.
  463. * (IPIs must obey or appear to obey normal Linux cache
  464. * coherency rules -- see comment in generic_exec_single).
  465. */
  466. smp_mb();
  467. /* Send a message to all CPUs in the map */
  468. arch_send_call_function_ipi_mask(data->cpumask);
  469. /* Optionally wait for the CPUs to complete */
  470. if (wait)
  471. csd_lock_wait(&data->csd);
  472. }
  473. EXPORT_SYMBOL(smp_call_function_many);
  474. /**
  475. * smp_call_function(): Run a function on all other CPUs.
  476. * @func: The function to run. This must be fast and non-blocking.
  477. * @info: An arbitrary pointer to pass to the function.
  478. * @wait: If true, wait (atomically) until function has completed
  479. * on other CPUs.
  480. *
  481. * Returns 0.
  482. *
  483. * If @wait is true, then returns once @func has returned; otherwise
  484. * it returns just before the target cpu calls @func.
  485. *
  486. * You must not call this function with disabled interrupts or from a
  487. * hardware interrupt handler or from a bottom half handler.
  488. */
  489. int smp_call_function(smp_call_func_t func, void *info, int wait)
  490. {
  491. preempt_disable();
  492. smp_call_function_many(cpu_online_mask, func, info, wait);
  493. preempt_enable();
  494. return 0;
  495. }
  496. EXPORT_SYMBOL(smp_call_function);
  497. #endif /* USE_GENERIC_SMP_HELPERS */
  498. /* Setup configured maximum number of CPUs to activate */
  499. unsigned int setup_max_cpus = NR_CPUS;
  500. EXPORT_SYMBOL(setup_max_cpus);
  501. /*
  502. * Setup routine for controlling SMP activation
  503. *
  504. * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
  505. * activation entirely (the MPS table probe still happens, though).
  506. *
  507. * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
  508. * greater than 0, limits the maximum number of CPUs activated in
  509. * SMP mode to <NUM>.
  510. */
  511. void __weak arch_disable_smp_support(void) { }
  512. static int __init nosmp(char *str)
  513. {
  514. setup_max_cpus = 0;
  515. arch_disable_smp_support();
  516. return 0;
  517. }
  518. early_param("nosmp", nosmp);
  519. /* this is hard limit */
  520. static int __init nrcpus(char *str)
  521. {
  522. int nr_cpus;
  523. get_option(&str, &nr_cpus);
  524. if (nr_cpus > 0 && nr_cpus < nr_cpu_ids)
  525. nr_cpu_ids = nr_cpus;
  526. return 0;
  527. }
  528. early_param("nr_cpus", nrcpus);
  529. static int __init maxcpus(char *str)
  530. {
  531. get_option(&str, &setup_max_cpus);
  532. if (setup_max_cpus == 0)
  533. arch_disable_smp_support();
  534. return 0;
  535. }
  536. early_param("maxcpus", maxcpus);
  537. /* Setup number of possible processor ids */
  538. int nr_cpu_ids __read_mostly = NR_CPUS;
  539. EXPORT_SYMBOL(nr_cpu_ids);
  540. /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
  541. void __init setup_nr_cpu_ids(void)
  542. {
  543. nr_cpu_ids = find_last_bit(cpumask_bits(cpu_possible_mask),NR_CPUS) + 1;
  544. }
  545. /* Called by boot processor to activate the rest. */
  546. void __init smp_init(void)
  547. {
  548. unsigned int cpu;
  549. idle_threads_init();
  550. /* FIXME: This should be done in userspace --RR */
  551. for_each_present_cpu(cpu) {
  552. if (num_online_cpus() >= setup_max_cpus)
  553. break;
  554. if (!cpu_online(cpu))
  555. cpu_up(cpu);
  556. }
  557. /* Any cleanup work */
  558. printk(KERN_INFO "Brought up %ld CPUs\n", (long)num_online_cpus());
  559. smp_cpus_done(setup_max_cpus);
  560. }
  561. /*
  562. * Call a function on all processors. May be used during early boot while
  563. * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
  564. * of local_irq_disable/enable().
  565. */
  566. int on_each_cpu(void (*func) (void *info), void *info, int wait)
  567. {
  568. unsigned long flags;
  569. int ret = 0;
  570. preempt_disable();
  571. ret = smp_call_function(func, info, wait);
  572. local_irq_save(flags);
  573. func(info);
  574. local_irq_restore(flags);
  575. preempt_enable();
  576. return ret;
  577. }
  578. EXPORT_SYMBOL(on_each_cpu);
  579. /**
  580. * on_each_cpu_mask(): Run a function on processors specified by
  581. * cpumask, which may include the local processor.
  582. * @mask: The set of cpus to run on (only runs on online subset).
  583. * @func: The function to run. This must be fast and non-blocking.
  584. * @info: An arbitrary pointer to pass to the function.
  585. * @wait: If true, wait (atomically) until function has completed
  586. * on other CPUs.
  587. *
  588. * If @wait is true, then returns once @func has returned.
  589. *
  590. * You must not call this function with disabled interrupts or
  591. * from a hardware interrupt handler or from a bottom half handler.
  592. */
  593. void on_each_cpu_mask(const struct cpumask *mask, smp_call_func_t func,
  594. void *info, bool wait)
  595. {
  596. int cpu = get_cpu();
  597. smp_call_function_many(mask, func, info, wait);
  598. if (cpumask_test_cpu(cpu, mask)) {
  599. local_irq_disable();
  600. func(info);
  601. local_irq_enable();
  602. }
  603. put_cpu();
  604. }
  605. EXPORT_SYMBOL(on_each_cpu_mask);
  606. /*
  607. * on_each_cpu_cond(): Call a function on each processor for which
  608. * the supplied function cond_func returns true, optionally waiting
  609. * for all the required CPUs to finish. This may include the local
  610. * processor.
  611. * @cond_func: A callback function that is passed a cpu id and
  612. * the the info parameter. The function is called
  613. * with preemption disabled. The function should
  614. * return a blooean value indicating whether to IPI
  615. * the specified CPU.
  616. * @func: The function to run on all applicable CPUs.
  617. * This must be fast and non-blocking.
  618. * @info: An arbitrary pointer to pass to both functions.
  619. * @wait: If true, wait (atomically) until function has
  620. * completed on other CPUs.
  621. * @gfp_flags: GFP flags to use when allocating the cpumask
  622. * used internally by the function.
  623. *
  624. * The function might sleep if the GFP flags indicates a non
  625. * atomic allocation is allowed.
  626. *
  627. * Preemption is disabled to protect against CPUs going offline but not online.
  628. * CPUs going online during the call will not be seen or sent an IPI.
  629. *
  630. * You must not call this function with disabled interrupts or
  631. * from a hardware interrupt handler or from a bottom half handler.
  632. */
  633. void on_each_cpu_cond(bool (*cond_func)(int cpu, void *info),
  634. smp_call_func_t func, void *info, bool wait,
  635. gfp_t gfp_flags)
  636. {
  637. cpumask_var_t cpus;
  638. int cpu, ret;
  639. might_sleep_if(gfp_flags & __GFP_WAIT);
  640. if (likely(zalloc_cpumask_var(&cpus, (gfp_flags|__GFP_NOWARN)))) {
  641. preempt_disable();
  642. for_each_online_cpu(cpu)
  643. if (cond_func(cpu, info))
  644. cpumask_set_cpu(cpu, cpus);
  645. on_each_cpu_mask(cpus, func, info, wait);
  646. preempt_enable();
  647. free_cpumask_var(cpus);
  648. } else {
  649. /*
  650. * No free cpumask, bother. No matter, we'll
  651. * just have to IPI them one by one.
  652. */
  653. preempt_disable();
  654. for_each_online_cpu(cpu)
  655. if (cond_func(cpu, info)) {
  656. ret = smp_call_function_single(cpu, func,
  657. info, wait);
  658. WARN_ON_ONCE(!ret);
  659. }
  660. preempt_enable();
  661. }
  662. }
  663. EXPORT_SYMBOL(on_each_cpu_cond);
  664. static void do_nothing(void *unused)
  665. {
  666. }
  667. /**
  668. * kick_all_cpus_sync - Force all cpus out of idle
  669. *
  670. * Used to synchronize the update of pm_idle function pointer. It's
  671. * called after the pointer is updated and returns after the dummy
  672. * callback function has been executed on all cpus. The execution of
  673. * the function can only happen on the remote cpus after they have
  674. * left the idle function which had been called via pm_idle function
  675. * pointer. So it's guaranteed that nothing uses the previous pointer
  676. * anymore.
  677. */
  678. void kick_all_cpus_sync(void)
  679. {
  680. /* Make sure the change is visible before we kick the cpus */
  681. smp_mb();
  682. smp_call_function(do_nothing, NULL, 1);
  683. }
  684. EXPORT_SYMBOL_GPL(kick_all_cpus_sync);