smp.c 11 KB

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