smp.c 17 KB

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