smp.c 17 KB

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