smp.c 17 KB

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