smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/module.h>
  9. #include <linux/percpu.h>
  10. #include <linux/init.h>
  11. #include <linux/smp.h>
  12. #include <linux/cpu.h>
  13. static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
  14. static struct {
  15. struct list_head queue;
  16. spinlock_t lock;
  17. } call_function __cacheline_aligned_in_smp =
  18. {
  19. .queue = LIST_HEAD_INIT(call_function.queue),
  20. .lock = __SPIN_LOCK_UNLOCKED(call_function.lock),
  21. };
  22. enum {
  23. CSD_FLAG_LOCK = 0x01,
  24. };
  25. struct call_function_data {
  26. struct call_single_data csd;
  27. spinlock_t lock;
  28. unsigned int refs;
  29. cpumask_var_t cpumask;
  30. };
  31. struct call_single_queue {
  32. struct list_head list;
  33. spinlock_t lock;
  34. };
  35. static DEFINE_PER_CPU(struct call_function_data, cfd_data) = {
  36. .lock = __SPIN_LOCK_UNLOCKED(cfd_data.lock),
  37. };
  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 (!alloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  47. cpu_to_node(cpu)))
  48. return NOTIFY_BAD;
  49. break;
  50. #ifdef CONFIG_CPU_HOTPLUG
  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. 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. spin_lock_irqsave(&dst->lock, flags);
  122. ipi = list_empty(&dst->list);
  123. list_add_tail(&data->list, &dst->list);
  124. 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 = get_cpu();
  149. /*
  150. * Ensure entry is visible on call_function_queue after we have
  151. * entered the IPI. See comment in smp_call_function_many.
  152. * If we don't have this, then we may miss an entry on the list
  153. * and never get another IPI to process it.
  154. */
  155. smp_mb();
  156. /*
  157. * It's ok to use list_for_each_rcu() here even though we may
  158. * delete 'pos', since list_del_rcu() doesn't clear ->next
  159. */
  160. list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
  161. int refs;
  162. spin_lock(&data->lock);
  163. if (!cpumask_test_cpu(cpu, data->cpumask)) {
  164. spin_unlock(&data->lock);
  165. continue;
  166. }
  167. cpumask_clear_cpu(cpu, data->cpumask);
  168. spin_unlock(&data->lock);
  169. data->csd.func(data->csd.info);
  170. spin_lock(&data->lock);
  171. WARN_ON(data->refs == 0);
  172. refs = --data->refs;
  173. if (!refs) {
  174. spin_lock(&call_function.lock);
  175. list_del_rcu(&data->csd.list);
  176. spin_unlock(&call_function.lock);
  177. }
  178. spin_unlock(&data->lock);
  179. if (refs)
  180. continue;
  181. csd_unlock(&data->csd);
  182. }
  183. put_cpu();
  184. }
  185. /*
  186. * Invoked by arch to handle an IPI for call function single. Must be
  187. * called from the arch with interrupts disabled.
  188. */
  189. void generic_smp_call_function_single_interrupt(void)
  190. {
  191. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  192. unsigned int data_flags;
  193. LIST_HEAD(list);
  194. spin_lock(&q->lock);
  195. list_replace_init(&q->list, &list);
  196. spin_unlock(&q->lock);
  197. while (!list_empty(&list)) {
  198. struct call_single_data *data;
  199. data = list_entry(list.next, struct call_single_data, list);
  200. list_del(&data->list);
  201. /*
  202. * 'data' can be invalid after this call if flags == 0
  203. * (when called through generic_exec_single()),
  204. * so save them away before making the call:
  205. */
  206. data_flags = data->flags;
  207. data->func(data->info);
  208. /*
  209. * Unlocked CSDs are valid through generic_exec_single():
  210. */
  211. if (data_flags & CSD_FLAG_LOCK)
  212. csd_unlock(data);
  213. }
  214. }
  215. static DEFINE_PER_CPU(struct call_single_data, csd_data);
  216. /*
  217. * smp_call_function_single - Run a function on a specific CPU
  218. * @func: The function to run. This must be fast and non-blocking.
  219. * @info: An arbitrary pointer to pass to the function.
  220. * @wait: If true, wait until function has completed on other CPUs.
  221. *
  222. * Returns 0 on success, else a negative status code. Note that @wait
  223. * will be implicitly turned on in case of allocation failures, since
  224. * we fall back to on-stack allocation.
  225. */
  226. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  227. int wait)
  228. {
  229. struct call_single_data d = {
  230. .flags = 0,
  231. };
  232. unsigned long flags;
  233. int this_cpu;
  234. int err = 0;
  235. /*
  236. * prevent preemption and reschedule on another processor,
  237. * as well as CPU removal
  238. */
  239. this_cpu = get_cpu();
  240. /* Can deadlock when called with interrupts disabled */
  241. WARN_ON(irqs_disabled());
  242. if (cpu == this_cpu) {
  243. local_irq_save(flags);
  244. func(info);
  245. local_irq_restore(flags);
  246. } else {
  247. if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
  248. struct call_single_data *data = &d;
  249. if (!wait)
  250. data = &__get_cpu_var(csd_data);
  251. csd_lock(data);
  252. data->func = func;
  253. data->info = info;
  254. generic_exec_single(cpu, data, wait);
  255. } else {
  256. err = -ENXIO; /* CPU not online */
  257. }
  258. }
  259. put_cpu();
  260. return err;
  261. }
  262. EXPORT_SYMBOL(smp_call_function_single);
  263. /**
  264. * __smp_call_function_single(): Run a function on another CPU
  265. * @cpu: The CPU to run on.
  266. * @data: Pre-allocated and setup data structure
  267. *
  268. * Like smp_call_function_single(), but allow caller to pass in a
  269. * pre-allocated data structure. Useful for embedding @data inside
  270. * other structures, for instance.
  271. */
  272. void __smp_call_function_single(int cpu, struct call_single_data *data,
  273. int wait)
  274. {
  275. csd_lock(data);
  276. /* Can deadlock when called with interrupts disabled */
  277. WARN_ON(wait && irqs_disabled());
  278. generic_exec_single(cpu, data, wait);
  279. }
  280. /* Deprecated: shim for archs using old arch_send_call_function_ipi API. */
  281. #ifndef arch_send_call_function_ipi_mask
  282. # define arch_send_call_function_ipi_mask(maskp) \
  283. arch_send_call_function_ipi(*(maskp))
  284. #endif
  285. /**
  286. * smp_call_function_many(): Run a function on a set of other CPUs.
  287. * @mask: The set of cpus to run on (only runs on online subset).
  288. * @func: The function to run. This must be fast and non-blocking.
  289. * @info: An arbitrary pointer to pass to the function.
  290. * @wait: If true, wait (atomically) until function has completed
  291. * on other CPUs.
  292. *
  293. * If @wait is true, then returns once @func has returned. Note that @wait
  294. * will be implicitly turned on in case of allocation failures, since
  295. * we fall back to on-stack allocation.
  296. *
  297. * You must not call this function with disabled interrupts or from a
  298. * hardware interrupt handler or from a bottom half handler. Preemption
  299. * must be disabled when calling this function.
  300. */
  301. void smp_call_function_many(const struct cpumask *mask,
  302. void (*func)(void *), void *info, bool wait)
  303. {
  304. struct call_function_data *data;
  305. unsigned long flags;
  306. int cpu, next_cpu, this_cpu = smp_processor_id();
  307. /* Can deadlock when called with interrupts disabled */
  308. WARN_ON(irqs_disabled());
  309. /* So, what's a CPU they want? Ignoring this one. */
  310. cpu = cpumask_first_and(mask, cpu_online_mask);
  311. if (cpu == this_cpu)
  312. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  313. /* No online cpus? We're done. */
  314. if (cpu >= nr_cpu_ids)
  315. return;
  316. /* Do we have another CPU which isn't us? */
  317. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  318. if (next_cpu == this_cpu)
  319. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  320. /* Fastpath: do that cpu by itself. */
  321. if (next_cpu >= nr_cpu_ids) {
  322. smp_call_function_single(cpu, func, info, wait);
  323. return;
  324. }
  325. data = &__get_cpu_var(cfd_data);
  326. csd_lock(&data->csd);
  327. spin_lock_irqsave(&data->lock, flags);
  328. data->csd.func = func;
  329. data->csd.info = info;
  330. cpumask_and(data->cpumask, mask, cpu_online_mask);
  331. cpumask_clear_cpu(this_cpu, data->cpumask);
  332. data->refs = cpumask_weight(data->cpumask);
  333. spin_lock(&call_function.lock);
  334. /*
  335. * Place entry at the _HEAD_ of the list, so that any cpu still
  336. * observing the entry in generic_smp_call_function_interrupt()
  337. * will not miss any other list entries:
  338. */
  339. list_add_rcu(&data->csd.list, &call_function.queue);
  340. spin_unlock(&call_function.lock);
  341. spin_unlock_irqrestore(&data->lock, flags);
  342. /*
  343. * Make the list addition visible before sending the ipi.
  344. * (IPIs must obey or appear to obey normal Linux cache
  345. * coherency rules -- see comment in generic_exec_single).
  346. */
  347. smp_mb();
  348. /* Send a message to all CPUs in the map */
  349. arch_send_call_function_ipi_mask(data->cpumask);
  350. /* Optionally wait for the CPUs to complete */
  351. if (wait)
  352. csd_lock_wait(&data->csd);
  353. }
  354. EXPORT_SYMBOL(smp_call_function_many);
  355. /**
  356. * smp_call_function(): Run a function on all other CPUs.
  357. * @func: The function to run. This must be fast and non-blocking.
  358. * @info: An arbitrary pointer to pass to the function.
  359. * @wait: If true, wait (atomically) until function has completed
  360. * on other CPUs.
  361. *
  362. * Returns 0.
  363. *
  364. * If @wait is true, then returns once @func has returned; otherwise
  365. * it returns just before the target cpu calls @func. In case of allocation
  366. * failure, @wait will be implicitly turned on.
  367. *
  368. * You must not call this function with disabled interrupts or from a
  369. * hardware interrupt handler or from a bottom half handler.
  370. */
  371. int smp_call_function(void (*func)(void *), void *info, int wait)
  372. {
  373. preempt_disable();
  374. smp_call_function_many(cpu_online_mask, func, info, wait);
  375. preempt_enable();
  376. return 0;
  377. }
  378. EXPORT_SYMBOL(smp_call_function);
  379. void ipi_call_lock(void)
  380. {
  381. spin_lock(&call_function.lock);
  382. }
  383. void ipi_call_unlock(void)
  384. {
  385. spin_unlock(&call_function.lock);
  386. }
  387. void ipi_call_lock_irq(void)
  388. {
  389. spin_lock_irq(&call_function.lock);
  390. }
  391. void ipi_call_unlock_irq(void)
  392. {
  393. spin_unlock_irq(&call_function.lock);
  394. }