smp.c 22 KB

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