smp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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/module.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. };
  32. static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_function_data, cfd_data);
  33. struct call_single_queue {
  34. struct list_head list;
  35. raw_spinlock_t lock;
  36. };
  37. static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_queue, call_single_queue);
  38. static int
  39. hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
  40. {
  41. long cpu = (long)hcpu;
  42. struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
  43. switch (action) {
  44. case CPU_UP_PREPARE:
  45. case CPU_UP_PREPARE_FROZEN:
  46. if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  47. cpu_to_node(cpu)))
  48. return notifier_from_errno(-ENOMEM);
  49. break;
  50. #ifdef CONFIG_HOTPLUG_CPU
  51. case CPU_UP_CANCELED:
  52. case CPU_UP_CANCELED_FROZEN:
  53. case CPU_DEAD:
  54. case CPU_DEAD_FROZEN:
  55. free_cpumask_var(cfd->cpumask);
  56. break;
  57. #endif
  58. };
  59. return NOTIFY_OK;
  60. }
  61. static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
  62. .notifier_call = hotplug_cfd,
  63. };
  64. static int __cpuinit init_call_single_data(void)
  65. {
  66. void *cpu = (void *)(long)smp_processor_id();
  67. int i;
  68. for_each_possible_cpu(i) {
  69. struct call_single_queue *q = &per_cpu(call_single_queue, i);
  70. raw_spin_lock_init(&q->lock);
  71. INIT_LIST_HEAD(&q->list);
  72. }
  73. hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
  74. register_cpu_notifier(&hotplug_cfd_notifier);
  75. return 0;
  76. }
  77. early_initcall(init_call_single_data);
  78. /*
  79. * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
  80. *
  81. * For non-synchronous ipi calls the csd can still be in use by the
  82. * previous function call. For multi-cpu calls its even more interesting
  83. * as we'll have to ensure no other cpu is observing our csd.
  84. */
  85. static void csd_lock_wait(struct call_single_data *data)
  86. {
  87. while (data->flags & CSD_FLAG_LOCK)
  88. cpu_relax();
  89. }
  90. static void csd_lock(struct call_single_data *data)
  91. {
  92. csd_lock_wait(data);
  93. data->flags = CSD_FLAG_LOCK;
  94. /*
  95. * prevent CPU from reordering the above assignment
  96. * to ->flags with any subsequent assignments to other
  97. * fields of the specified call_single_data structure:
  98. */
  99. smp_mb();
  100. }
  101. static void csd_unlock(struct call_single_data *data)
  102. {
  103. WARN_ON(!(data->flags & CSD_FLAG_LOCK));
  104. /*
  105. * ensure we're all done before releasing data:
  106. */
  107. smp_mb();
  108. data->flags &= ~CSD_FLAG_LOCK;
  109. }
  110. /*
  111. * Insert a previously allocated call_single_data element
  112. * for execution on the given CPU. data must already have
  113. * ->func, ->info, and ->flags set.
  114. */
  115. static
  116. void generic_exec_single(int cpu, struct call_single_data *data, int wait)
  117. {
  118. struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
  119. unsigned long flags;
  120. int ipi;
  121. raw_spin_lock_irqsave(&dst->lock, flags);
  122. ipi = list_empty(&dst->list);
  123. list_add_tail(&data->list, &dst->list);
  124. raw_spin_unlock_irqrestore(&dst->lock, flags);
  125. /*
  126. * The list addition should be visible before sending the IPI
  127. * handler locks the list to pull the entry off it because of
  128. * normal cache coherency rules implied by spinlocks.
  129. *
  130. * If IPIs can go out of order to the cache coherency protocol
  131. * in an architecture, sufficient synchronisation should be added
  132. * to arch code to make it appear to obey cache coherency WRT
  133. * locking and barrier primitives. Generic code isn't really
  134. * equipped to do the right thing...
  135. */
  136. if (ipi)
  137. arch_send_call_function_single_ipi(cpu);
  138. if (wait)
  139. csd_lock_wait(data);
  140. }
  141. /*
  142. * Invoked by arch to handle an IPI for call function. Must be called with
  143. * interrupts disabled.
  144. */
  145. void generic_smp_call_function_interrupt(void)
  146. {
  147. struct call_function_data *data;
  148. int cpu = smp_processor_id();
  149. /*
  150. * Shouldn't receive this interrupt on a cpu that is not yet online.
  151. */
  152. WARN_ON_ONCE(!cpu_online(cpu));
  153. /*
  154. * Ensure entry is visible on call_function_queue after we have
  155. * entered the IPI. See comment in smp_call_function_many.
  156. * If we don't have this, then we may miss an entry on the list
  157. * and never get another IPI to process it.
  158. */
  159. smp_mb();
  160. /*
  161. * It's ok to use list_for_each_rcu() here even though we may
  162. * delete 'pos', since list_del_rcu() doesn't clear ->next
  163. */
  164. list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
  165. int refs;
  166. void (*func) (void *info);
  167. /*
  168. * Since we walk the list without any locks, we might
  169. * see an entry that was completed, removed from the
  170. * list and is in the process of being reused.
  171. *
  172. * We must check that the cpu is in the cpumask before
  173. * checking the refs, and both must be set before
  174. * executing the callback on this cpu.
  175. */
  176. if (!cpumask_test_cpu(cpu, data->cpumask))
  177. continue;
  178. smp_rmb();
  179. if (atomic_read(&data->refs) == 0)
  180. continue;
  181. func = data->csd.func; /* for later warn */
  182. data->csd.func(data->csd.info);
  183. /*
  184. * If the cpu mask is not still set then it enabled interrupts,
  185. * we took another smp interrupt, and executed the function
  186. * twice on this cpu. In theory that copy decremented refs.
  187. */
  188. if (!cpumask_test_and_clear_cpu(cpu, data->cpumask)) {
  189. WARN(1, "%pS enabled interrupts and double executed\n",
  190. func);
  191. continue;
  192. }
  193. refs = atomic_dec_return(&data->refs);
  194. WARN_ON(refs < 0);
  195. if (refs)
  196. continue;
  197. WARN_ON(!cpumask_empty(data->cpumask));
  198. raw_spin_lock(&call_function.lock);
  199. list_del_rcu(&data->csd.list);
  200. raw_spin_unlock(&call_function.lock);
  201. csd_unlock(&data->csd);
  202. }
  203. }
  204. /*
  205. * Invoked by arch to handle an IPI for call function single. Must be
  206. * called from the arch with interrupts disabled.
  207. */
  208. void generic_smp_call_function_single_interrupt(void)
  209. {
  210. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  211. unsigned int data_flags;
  212. LIST_HEAD(list);
  213. /*
  214. * Shouldn't receive this interrupt on a cpu that is not yet online.
  215. */
  216. WARN_ON_ONCE(!cpu_online(smp_processor_id()));
  217. raw_spin_lock(&q->lock);
  218. list_replace_init(&q->list, &list);
  219. raw_spin_unlock(&q->lock);
  220. while (!list_empty(&list)) {
  221. struct call_single_data *data;
  222. data = list_entry(list.next, struct call_single_data, list);
  223. list_del(&data->list);
  224. /*
  225. * 'data' can be invalid after this call if flags == 0
  226. * (when called through generic_exec_single()),
  227. * so save them away before making the call:
  228. */
  229. data_flags = data->flags;
  230. data->func(data->info);
  231. /*
  232. * Unlocked CSDs are valid through generic_exec_single():
  233. */
  234. if (data_flags & CSD_FLAG_LOCK)
  235. csd_unlock(data);
  236. }
  237. }
  238. static DEFINE_PER_CPU_SHARED_ALIGNED(struct call_single_data, csd_data);
  239. /*
  240. * smp_call_function_single - Run a function on a specific CPU
  241. * @func: The function to run. This must be fast and non-blocking.
  242. * @info: An arbitrary pointer to pass to the function.
  243. * @wait: If true, wait until function has completed on other CPUs.
  244. *
  245. * Returns 0 on success, else a negative status code.
  246. */
  247. int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
  248. int wait)
  249. {
  250. struct call_single_data d = {
  251. .flags = 0,
  252. };
  253. unsigned long flags;
  254. int this_cpu;
  255. int err = 0;
  256. /*
  257. * prevent preemption and reschedule on another processor,
  258. * as well as CPU removal
  259. */
  260. this_cpu = get_cpu();
  261. /*
  262. * Can deadlock when called with interrupts disabled.
  263. * We allow cpu's that are not yet online though, as no one else can
  264. * send smp call function interrupt to this cpu and as such deadlocks
  265. * can't happen.
  266. */
  267. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  268. && !oops_in_progress);
  269. if (cpu == this_cpu) {
  270. local_irq_save(flags);
  271. func(info);
  272. local_irq_restore(flags);
  273. } else {
  274. if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
  275. struct call_single_data *data = &d;
  276. if (!wait)
  277. data = &__get_cpu_var(csd_data);
  278. csd_lock(data);
  279. data->func = func;
  280. data->info = info;
  281. generic_exec_single(cpu, data, wait);
  282. } else {
  283. err = -ENXIO; /* CPU not online */
  284. }
  285. }
  286. put_cpu();
  287. return err;
  288. }
  289. EXPORT_SYMBOL(smp_call_function_single);
  290. /*
  291. * smp_call_function_any - Run a function on any of the given cpus
  292. * @mask: The mask of cpus it can run on.
  293. * @func: The function to run. This must be fast and non-blocking.
  294. * @info: An arbitrary pointer to pass to the function.
  295. * @wait: If true, wait until function has completed.
  296. *
  297. * Returns 0 on success, else a negative status code (if no cpus were online).
  298. * Note that @wait will be implicitly turned on in case of allocation failures,
  299. * since we fall back to on-stack allocation.
  300. *
  301. * Selection preference:
  302. * 1) current cpu if in @mask
  303. * 2) any cpu of current node if in @mask
  304. * 3) any other online cpu in @mask
  305. */
  306. int smp_call_function_any(const struct cpumask *mask,
  307. smp_call_func_t func, void *info, int wait)
  308. {
  309. unsigned int cpu;
  310. const struct cpumask *nodemask;
  311. int ret;
  312. /* Try for same CPU (cheapest) */
  313. cpu = get_cpu();
  314. if (cpumask_test_cpu(cpu, mask))
  315. goto call;
  316. /* Try for same node. */
  317. nodemask = cpumask_of_node(cpu_to_node(cpu));
  318. for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
  319. cpu = cpumask_next_and(cpu, nodemask, mask)) {
  320. if (cpu_online(cpu))
  321. goto call;
  322. }
  323. /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
  324. cpu = cpumask_any_and(mask, cpu_online_mask);
  325. call:
  326. ret = smp_call_function_single(cpu, func, info, wait);
  327. put_cpu();
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(smp_call_function_any);
  331. /**
  332. * __smp_call_function_single(): Run a function on a specific CPU
  333. * @cpu: The CPU to run on.
  334. * @data: Pre-allocated and setup data structure
  335. * @wait: If true, wait until function has completed on specified CPU.
  336. *
  337. * Like smp_call_function_single(), but allow caller to pass in a
  338. * pre-allocated data structure. Useful for embedding @data inside
  339. * other structures, for instance.
  340. */
  341. void __smp_call_function_single(int cpu, struct call_single_data *data,
  342. int wait)
  343. {
  344. unsigned int this_cpu;
  345. unsigned long flags;
  346. this_cpu = get_cpu();
  347. /*
  348. * Can deadlock when called with interrupts disabled.
  349. * We allow cpu's that are not yet online though, as no one else can
  350. * send smp call function interrupt to this cpu and as such deadlocks
  351. * can't happen.
  352. */
  353. WARN_ON_ONCE(cpu_online(smp_processor_id()) && wait && irqs_disabled()
  354. && !oops_in_progress);
  355. if (cpu == this_cpu) {
  356. local_irq_save(flags);
  357. data->func(data->info);
  358. local_irq_restore(flags);
  359. } else {
  360. csd_lock(data);
  361. generic_exec_single(cpu, data, wait);
  362. }
  363. put_cpu();
  364. }
  365. /**
  366. * smp_call_function_many(): Run a function on a set of other CPUs.
  367. * @mask: The set of cpus to run on (only runs on online subset).
  368. * @func: The function to run. This must be fast and non-blocking.
  369. * @info: An arbitrary pointer to pass to the function.
  370. * @wait: If true, wait (atomically) until function has completed
  371. * on other CPUs.
  372. *
  373. * If @wait is true, then returns once @func has returned.
  374. *
  375. * You must not call this function with disabled interrupts or from a
  376. * hardware interrupt handler or from a bottom half handler. Preemption
  377. * must be disabled when calling this function.
  378. */
  379. void smp_call_function_many(const struct cpumask *mask,
  380. smp_call_func_t func, void *info, bool wait)
  381. {
  382. struct call_function_data *data;
  383. unsigned long flags;
  384. int cpu, next_cpu, this_cpu = smp_processor_id();
  385. /*
  386. * Can deadlock when called with interrupts disabled.
  387. * We allow cpu's that are not yet online though, as no one else can
  388. * send smp call function interrupt to this cpu and as such deadlocks
  389. * can't happen.
  390. */
  391. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  392. && !oops_in_progress && !early_boot_irqs_disabled);
  393. /* So, what's a CPU they want? Ignoring this one. */
  394. cpu = cpumask_first_and(mask, cpu_online_mask);
  395. if (cpu == this_cpu)
  396. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  397. /* No online cpus? We're done. */
  398. if (cpu >= nr_cpu_ids)
  399. return;
  400. /* Do we have another CPU which isn't us? */
  401. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  402. if (next_cpu == this_cpu)
  403. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  404. /* Fastpath: do that cpu by itself. */
  405. if (next_cpu >= nr_cpu_ids) {
  406. smp_call_function_single(cpu, func, info, wait);
  407. return;
  408. }
  409. data = &__get_cpu_var(cfd_data);
  410. csd_lock(&data->csd);
  411. BUG_ON(atomic_read(&data->refs) || !cpumask_empty(data->cpumask));
  412. data->csd.func = func;
  413. data->csd.info = info;
  414. cpumask_and(data->cpumask, mask, cpu_online_mask);
  415. cpumask_clear_cpu(this_cpu, data->cpumask);
  416. /*
  417. * To ensure the interrupt handler gets an complete view
  418. * we order the cpumask and refs writes and order the read
  419. * of them in the interrupt handler. In addition we may
  420. * only clear our own cpu bit from the mask.
  421. */
  422. smp_wmb();
  423. atomic_set(&data->refs, cpumask_weight(data->cpumask));
  424. raw_spin_lock_irqsave(&call_function.lock, flags);
  425. /*
  426. * Place entry at the _HEAD_ of the list, so that any cpu still
  427. * observing the entry in generic_smp_call_function_interrupt()
  428. * will not miss any other list entries:
  429. */
  430. list_add_rcu(&data->csd.list, &call_function.queue);
  431. raw_spin_unlock_irqrestore(&call_function.lock, flags);
  432. /*
  433. * Make the list addition visible before sending the ipi.
  434. * (IPIs must obey or appear to obey normal Linux cache
  435. * coherency rules -- see comment in generic_exec_single).
  436. */
  437. smp_mb();
  438. /* Send a message to all CPUs in the map */
  439. arch_send_call_function_ipi_mask(data->cpumask);
  440. /* Optionally wait for the CPUs to complete */
  441. if (wait)
  442. csd_lock_wait(&data->csd);
  443. }
  444. EXPORT_SYMBOL(smp_call_function_many);
  445. /**
  446. * smp_call_function(): Run a function on all other CPUs.
  447. * @func: The function to run. This must be fast and non-blocking.
  448. * @info: An arbitrary pointer to pass to the function.
  449. * @wait: If true, wait (atomically) until function has completed
  450. * on other CPUs.
  451. *
  452. * Returns 0.
  453. *
  454. * If @wait is true, then returns once @func has returned; otherwise
  455. * it returns just before the target cpu calls @func.
  456. *
  457. * You must not call this function with disabled interrupts or from a
  458. * hardware interrupt handler or from a bottom half handler.
  459. */
  460. int smp_call_function(smp_call_func_t func, void *info, int wait)
  461. {
  462. preempt_disable();
  463. smp_call_function_many(cpu_online_mask, func, info, wait);
  464. preempt_enable();
  465. return 0;
  466. }
  467. EXPORT_SYMBOL(smp_call_function);
  468. void ipi_call_lock(void)
  469. {
  470. raw_spin_lock(&call_function.lock);
  471. }
  472. void ipi_call_unlock(void)
  473. {
  474. raw_spin_unlock(&call_function.lock);
  475. }
  476. void ipi_call_lock_irq(void)
  477. {
  478. raw_spin_lock_irq(&call_function.lock);
  479. }
  480. void ipi_call_unlock_irq(void)
  481. {
  482. raw_spin_unlock_irq(&call_function.lock);
  483. }
  484. #endif /* USE_GENERIC_SMP_HELPERS */
  485. /*
  486. * Call a function on all processors. May be used during early boot while
  487. * early_boot_irqs_disabled is set. Use local_irq_save/restore() instead
  488. * of local_irq_disable/enable().
  489. */
  490. int on_each_cpu(void (*func) (void *info), void *info, int wait)
  491. {
  492. unsigned long flags;
  493. int ret = 0;
  494. preempt_disable();
  495. ret = smp_call_function(func, info, wait);
  496. local_irq_save(flags);
  497. func(info);
  498. local_irq_restore(flags);
  499. preempt_enable();
  500. return ret;
  501. }
  502. EXPORT_SYMBOL(on_each_cpu);