smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Generic helpers for smp ipi calls
  3. *
  4. * (C) Jens Axboe <jens.axboe@oracle.com> 2008
  5. *
  6. */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/percpu.h>
  10. #include <linux/rcupdate.h>
  11. #include <linux/rculist.h>
  12. #include <linux/smp.h>
  13. static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
  14. static LIST_HEAD(call_function_queue);
  15. __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_function_lock);
  16. enum {
  17. CSD_FLAG_WAIT = 0x01,
  18. CSD_FLAG_ALLOC = 0x02,
  19. };
  20. struct call_function_data {
  21. struct call_single_data csd;
  22. spinlock_t lock;
  23. unsigned int refs;
  24. struct rcu_head rcu_head;
  25. unsigned long cpumask_bits[];
  26. };
  27. struct call_single_queue {
  28. struct list_head list;
  29. spinlock_t lock;
  30. };
  31. static int __cpuinit init_call_single_data(void)
  32. {
  33. int i;
  34. for_each_possible_cpu(i) {
  35. struct call_single_queue *q = &per_cpu(call_single_queue, i);
  36. spin_lock_init(&q->lock);
  37. INIT_LIST_HEAD(&q->list);
  38. }
  39. return 0;
  40. }
  41. early_initcall(init_call_single_data);
  42. static void csd_flag_wait(struct call_single_data *data)
  43. {
  44. /* Wait for response */
  45. do {
  46. if (!(data->flags & CSD_FLAG_WAIT))
  47. break;
  48. cpu_relax();
  49. } while (1);
  50. }
  51. /*
  52. * Insert a previously allocated call_single_data element for execution
  53. * on the given CPU. data must already have ->func, ->info, and ->flags set.
  54. */
  55. static void generic_exec_single(int cpu, struct call_single_data *data)
  56. {
  57. struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
  58. int wait = data->flags & CSD_FLAG_WAIT, ipi;
  59. unsigned long flags;
  60. spin_lock_irqsave(&dst->lock, flags);
  61. ipi = list_empty(&dst->list);
  62. list_add_tail(&data->list, &dst->list);
  63. spin_unlock_irqrestore(&dst->lock, flags);
  64. /*
  65. * Make the list addition visible before sending the ipi.
  66. */
  67. smp_mb();
  68. if (ipi)
  69. arch_send_call_function_single_ipi(cpu);
  70. if (wait)
  71. csd_flag_wait(data);
  72. }
  73. static void rcu_free_call_data(struct rcu_head *head)
  74. {
  75. struct call_function_data *data;
  76. data = container_of(head, struct call_function_data, rcu_head);
  77. kfree(data);
  78. }
  79. /*
  80. * Invoked by arch to handle an IPI for call function. Must be called with
  81. * interrupts disabled.
  82. */
  83. void generic_smp_call_function_interrupt(void)
  84. {
  85. struct call_function_data *data;
  86. int cpu = get_cpu();
  87. /*
  88. * It's ok to use list_for_each_rcu() here even though we may delete
  89. * 'pos', since list_del_rcu() doesn't clear ->next
  90. */
  91. rcu_read_lock();
  92. list_for_each_entry_rcu(data, &call_function_queue, csd.list) {
  93. int refs;
  94. if (!cpumask_test_cpu(cpu, to_cpumask(data->cpumask_bits)))
  95. continue;
  96. data->csd.func(data->csd.info);
  97. spin_lock(&data->lock);
  98. cpumask_clear_cpu(cpu, to_cpumask(data->cpumask_bits));
  99. WARN_ON(data->refs == 0);
  100. data->refs--;
  101. refs = data->refs;
  102. spin_unlock(&data->lock);
  103. if (refs)
  104. continue;
  105. spin_lock(&call_function_lock);
  106. list_del_rcu(&data->csd.list);
  107. spin_unlock(&call_function_lock);
  108. if (data->csd.flags & CSD_FLAG_WAIT) {
  109. /*
  110. * serialize stores to data with the flag clear
  111. * and wakeup
  112. */
  113. smp_wmb();
  114. data->csd.flags &= ~CSD_FLAG_WAIT;
  115. }
  116. if (data->csd.flags & CSD_FLAG_ALLOC)
  117. call_rcu(&data->rcu_head, rcu_free_call_data);
  118. }
  119. rcu_read_unlock();
  120. put_cpu();
  121. }
  122. /*
  123. * Invoked by arch to handle an IPI for call function single. Must be called
  124. * from the arch with interrupts disabled.
  125. */
  126. void generic_smp_call_function_single_interrupt(void)
  127. {
  128. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  129. LIST_HEAD(list);
  130. /*
  131. * Need to see other stores to list head for checking whether
  132. * list is empty without holding q->lock
  133. */
  134. smp_read_barrier_depends();
  135. while (!list_empty(&q->list)) {
  136. unsigned int data_flags;
  137. spin_lock(&q->lock);
  138. list_replace_init(&q->list, &list);
  139. spin_unlock(&q->lock);
  140. while (!list_empty(&list)) {
  141. struct call_single_data *data;
  142. data = list_entry(list.next, struct call_single_data,
  143. list);
  144. list_del(&data->list);
  145. /*
  146. * 'data' can be invalid after this call if
  147. * flags == 0 (when called through
  148. * generic_exec_single(), so save them away before
  149. * making the call.
  150. */
  151. data_flags = data->flags;
  152. data->func(data->info);
  153. if (data_flags & CSD_FLAG_WAIT) {
  154. smp_wmb();
  155. data->flags &= ~CSD_FLAG_WAIT;
  156. } else if (data_flags & CSD_FLAG_ALLOC)
  157. kfree(data);
  158. }
  159. /*
  160. * See comment on outer loop
  161. */
  162. smp_read_barrier_depends();
  163. }
  164. }
  165. /*
  166. * smp_call_function_single - Run a function on a specific CPU
  167. * @func: The function to run. This must be fast and non-blocking.
  168. * @info: An arbitrary pointer to pass to the function.
  169. * @wait: If true, wait until function has completed on other CPUs.
  170. *
  171. * Returns 0 on success, else a negative status code. Note that @wait
  172. * will be implicitly turned on in case of allocation failures, since
  173. * we fall back to on-stack allocation.
  174. */
  175. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  176. int wait)
  177. {
  178. struct call_single_data d;
  179. unsigned long flags;
  180. /* prevent preemption and reschedule on another processor,
  181. as well as CPU removal */
  182. int me = get_cpu();
  183. int err = 0;
  184. /* Can deadlock when called with interrupts disabled */
  185. WARN_ON(irqs_disabled());
  186. if (cpu == me) {
  187. local_irq_save(flags);
  188. func(info);
  189. local_irq_restore(flags);
  190. } else if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
  191. struct call_single_data *data = NULL;
  192. if (!wait) {
  193. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  194. if (data)
  195. data->flags = CSD_FLAG_ALLOC;
  196. }
  197. if (!data) {
  198. data = &d;
  199. data->flags = CSD_FLAG_WAIT;
  200. }
  201. data->func = func;
  202. data->info = info;
  203. generic_exec_single(cpu, data);
  204. } else {
  205. err = -ENXIO; /* CPU not online */
  206. }
  207. put_cpu();
  208. return err;
  209. }
  210. EXPORT_SYMBOL(smp_call_function_single);
  211. /**
  212. * __smp_call_function_single(): Run a function on another CPU
  213. * @cpu: The CPU to run on.
  214. * @data: Pre-allocated and setup data structure
  215. *
  216. * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
  217. * data structure. Useful for embedding @data inside other structures, for
  218. * instance.
  219. *
  220. */
  221. void __smp_call_function_single(int cpu, struct call_single_data *data)
  222. {
  223. /* Can deadlock when called with interrupts disabled */
  224. WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled());
  225. generic_exec_single(cpu, data);
  226. }
  227. /* FIXME: Shim for archs using old arch_send_call_function_ipi API. */
  228. #ifndef arch_send_call_function_ipi_mask
  229. #define arch_send_call_function_ipi_mask(maskp) \
  230. arch_send_call_function_ipi(*(maskp))
  231. #endif
  232. /**
  233. * smp_call_function_many(): Run a function on a set of other CPUs.
  234. * @mask: The set of cpus to run on (only runs on online subset).
  235. * @func: The function to run. This must be fast and non-blocking.
  236. * @info: An arbitrary pointer to pass to the function.
  237. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  238. *
  239. * If @wait is true, then returns once @func has returned. Note that @wait
  240. * will be implicitly turned on in case of allocation failures, since
  241. * we fall back to on-stack allocation.
  242. *
  243. * You must not call this function with disabled interrupts or from a
  244. * hardware interrupt handler or from a bottom half handler. Preemption
  245. * must be disabled when calling this function.
  246. */
  247. void smp_call_function_many(const struct cpumask *mask,
  248. void (*func)(void *), void *info,
  249. bool wait)
  250. {
  251. struct call_function_data *data;
  252. unsigned long flags;
  253. int cpu, next_cpu;
  254. /* Can deadlock when called with interrupts disabled */
  255. WARN_ON(irqs_disabled());
  256. /* So, what's a CPU they want? Ignoring this one. */
  257. cpu = cpumask_first_and(mask, cpu_online_mask);
  258. if (cpu == smp_processor_id())
  259. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  260. /* No online cpus? We're done. */
  261. if (cpu >= nr_cpu_ids)
  262. return;
  263. /* Do we have another CPU which isn't us? */
  264. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  265. if (next_cpu == smp_processor_id())
  266. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  267. /* Fastpath: do that cpu by itself. */
  268. if (next_cpu >= nr_cpu_ids) {
  269. smp_call_function_single(cpu, func, info, wait);
  270. return;
  271. }
  272. data = kmalloc(sizeof(*data) + cpumask_size(), GFP_ATOMIC);
  273. if (unlikely(!data)) {
  274. /* Slow path. */
  275. for_each_online_cpu(cpu) {
  276. if (cpu == smp_processor_id())
  277. continue;
  278. if (cpumask_test_cpu(cpu, mask))
  279. smp_call_function_single(cpu, func, info, wait);
  280. }
  281. return;
  282. }
  283. spin_lock_init(&data->lock);
  284. data->csd.flags = CSD_FLAG_ALLOC;
  285. if (wait)
  286. data->csd.flags |= CSD_FLAG_WAIT;
  287. data->csd.func = func;
  288. data->csd.info = info;
  289. cpumask_and(to_cpumask(data->cpumask_bits), mask, cpu_online_mask);
  290. cpumask_clear_cpu(smp_processor_id(), to_cpumask(data->cpumask_bits));
  291. data->refs = cpumask_weight(to_cpumask(data->cpumask_bits));
  292. spin_lock_irqsave(&call_function_lock, flags);
  293. list_add_tail_rcu(&data->csd.list, &call_function_queue);
  294. spin_unlock_irqrestore(&call_function_lock, flags);
  295. /*
  296. * Make the list addition visible before sending the ipi.
  297. */
  298. smp_mb();
  299. /* Send a message to all CPUs in the map */
  300. arch_send_call_function_ipi_mask(to_cpumask(data->cpumask_bits));
  301. /* optionally wait for the CPUs to complete */
  302. if (wait)
  303. csd_flag_wait(&data->csd);
  304. }
  305. EXPORT_SYMBOL(smp_call_function_many);
  306. /**
  307. * smp_call_function(): Run a function on all other CPUs.
  308. * @func: The function to run. This must be fast and non-blocking.
  309. * @info: An arbitrary pointer to pass to the function.
  310. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  311. *
  312. * Returns 0.
  313. *
  314. * If @wait is true, then returns once @func has returned; otherwise
  315. * it returns just before the target cpu calls @func. In case of allocation
  316. * failure, @wait will be implicitly turned on.
  317. *
  318. * You must not call this function with disabled interrupts or from a
  319. * hardware interrupt handler or from a bottom half handler.
  320. */
  321. int smp_call_function(void (*func)(void *), void *info, int wait)
  322. {
  323. preempt_disable();
  324. smp_call_function_many(cpu_online_mask, func, info, wait);
  325. preempt_enable();
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(smp_call_function);
  329. void ipi_call_lock(void)
  330. {
  331. spin_lock(&call_function_lock);
  332. }
  333. void ipi_call_unlock(void)
  334. {
  335. spin_unlock(&call_function_lock);
  336. }
  337. void ipi_call_lock_irq(void)
  338. {
  339. spin_lock_irq(&call_function_lock);
  340. }
  341. void ipi_call_unlock_irq(void)
  342. {
  343. spin_unlock_irq(&call_function_lock);
  344. }