smp.c 22 KB

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