smp.c 17 KB

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