smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. cpumask_t cpumask;
  25. struct rcu_head rcu_head;
  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. /*
  47. * We need to see the flags store in the IPI handler
  48. */
  49. smp_mb();
  50. if (!(data->flags & CSD_FLAG_WAIT))
  51. break;
  52. cpu_relax();
  53. } while (1);
  54. }
  55. /*
  56. * Insert a previously allocated call_single_data element for execution
  57. * on the given CPU. data must already have ->func, ->info, and ->flags set.
  58. */
  59. static void generic_exec_single(int cpu, struct call_single_data *data)
  60. {
  61. struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
  62. int wait = data->flags & CSD_FLAG_WAIT, ipi;
  63. unsigned long flags;
  64. spin_lock_irqsave(&dst->lock, flags);
  65. ipi = list_empty(&dst->list);
  66. list_add_tail(&data->list, &dst->list);
  67. spin_unlock_irqrestore(&dst->lock, flags);
  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 (!cpu_isset(cpu, data->cpumask))
  95. continue;
  96. data->csd.func(data->csd.info);
  97. spin_lock(&data->lock);
  98. cpu_clear(cpu, data->cpumask);
  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. } else
  116. call_rcu(&data->rcu_head, rcu_free_call_data);
  117. }
  118. rcu_read_unlock();
  119. put_cpu();
  120. }
  121. /*
  122. * Invoked by arch to handle an IPI for call function single. Must be called
  123. * from the arch with interrupts disabled.
  124. */
  125. void generic_smp_call_function_single_interrupt(void)
  126. {
  127. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  128. LIST_HEAD(list);
  129. /*
  130. * Need to see other stores to list head for checking whether
  131. * list is empty without holding q->lock
  132. */
  133. smp_mb();
  134. while (!list_empty(&q->list)) {
  135. unsigned int data_flags;
  136. spin_lock(&q->lock);
  137. list_replace_init(&q->list, &list);
  138. spin_unlock(&q->lock);
  139. while (!list_empty(&list)) {
  140. struct call_single_data *data;
  141. data = list_entry(list.next, struct call_single_data,
  142. list);
  143. list_del(&data->list);
  144. /*
  145. * 'data' can be invalid after this call if
  146. * flags == 0 (when called through
  147. * generic_exec_single(), so save them away before
  148. * making the call.
  149. */
  150. data_flags = data->flags;
  151. data->func(data->info);
  152. if (data_flags & CSD_FLAG_WAIT) {
  153. smp_wmb();
  154. data->flags &= ~CSD_FLAG_WAIT;
  155. } else if (data_flags & CSD_FLAG_ALLOC)
  156. kfree(data);
  157. }
  158. /*
  159. * See comment on outer loop
  160. */
  161. smp_mb();
  162. }
  163. }
  164. /*
  165. * smp_call_function_single - Run a function on a specific CPU
  166. * @func: The function to run. This must be fast and non-blocking.
  167. * @info: An arbitrary pointer to pass to the function.
  168. * @wait: If true, wait until function has completed on other CPUs.
  169. *
  170. * Returns 0 on success, else a negative status code. Note that @wait
  171. * will be implicitly turned on in case of allocation failures, since
  172. * we fall back to on-stack allocation.
  173. */
  174. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  175. int wait)
  176. {
  177. struct call_single_data d;
  178. unsigned long flags;
  179. /* prevent preemption and reschedule on another processor */
  180. int me = get_cpu();
  181. /* Can deadlock when called with interrupts disabled */
  182. WARN_ON(irqs_disabled());
  183. if (cpu == me) {
  184. local_irq_save(flags);
  185. func(info);
  186. local_irq_restore(flags);
  187. } else {
  188. struct call_single_data *data = NULL;
  189. if (!wait) {
  190. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  191. if (data)
  192. data->flags = CSD_FLAG_ALLOC;
  193. }
  194. if (!data) {
  195. data = &d;
  196. data->flags = CSD_FLAG_WAIT;
  197. }
  198. data->func = func;
  199. data->info = info;
  200. generic_exec_single(cpu, data);
  201. }
  202. put_cpu();
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(smp_call_function_single);
  206. /**
  207. * __smp_call_function_single(): Run a function on another CPU
  208. * @cpu: The CPU to run on.
  209. * @data: Pre-allocated and setup data structure
  210. *
  211. * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
  212. * data structure. Useful for embedding @data inside other structures, for
  213. * instance.
  214. *
  215. */
  216. void __smp_call_function_single(int cpu, struct call_single_data *data)
  217. {
  218. /* Can deadlock when called with interrupts disabled */
  219. WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled());
  220. generic_exec_single(cpu, data);
  221. }
  222. /* Dummy function */
  223. static void quiesce_dummy(void *unused)
  224. {
  225. }
  226. /*
  227. * Ensure stack based data used in call function mask is safe to free.
  228. *
  229. * This is needed by smp_call_function_mask when using on-stack data, because
  230. * a single call function queue is shared by all CPUs, and any CPU may pick up
  231. * the data item on the queue at any time before it is deleted. So we need to
  232. * ensure that all CPUs have transitioned through a quiescent state after
  233. * this call.
  234. *
  235. * This is a very slow function, implemented by sending synchronous IPIs to
  236. * all possible CPUs. For this reason, we have to alloc data rather than use
  237. * stack based data even in the case of synchronous calls. The stack based
  238. * data is then just used for deadlock/oom fallback which will be very rare.
  239. *
  240. * If a faster scheme can be made, we could go back to preferring stack based
  241. * data -- the data allocation/free is non-zero cost.
  242. */
  243. static void smp_call_function_mask_quiesce_stack(cpumask_t mask)
  244. {
  245. struct call_single_data data;
  246. int cpu;
  247. data.func = quiesce_dummy;
  248. data.info = NULL;
  249. data.flags = CSD_FLAG_WAIT;
  250. for_each_cpu_mask(cpu, mask)
  251. generic_exec_single(cpu, &data);
  252. }
  253. /**
  254. * smp_call_function_mask(): Run a function on a set of other CPUs.
  255. * @mask: The set of cpus to run on.
  256. * @func: The function to run. This must be fast and non-blocking.
  257. * @info: An arbitrary pointer to pass to the function.
  258. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  259. *
  260. * Returns 0 on success, else a negative status code.
  261. *
  262. * If @wait is true, then returns once @func has returned. Note that @wait
  263. * will be implicitly turned on in case of allocation failures, since
  264. * we fall back to on-stack allocation.
  265. *
  266. * You must not call this function with disabled interrupts or from a
  267. * hardware interrupt handler or from a bottom half handler. Preemption
  268. * must be disabled when calling this function.
  269. */
  270. int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
  271. int wait)
  272. {
  273. struct call_function_data d;
  274. struct call_function_data *data = NULL;
  275. cpumask_t allbutself;
  276. unsigned long flags;
  277. int cpu, num_cpus;
  278. int slowpath = 0;
  279. /* Can deadlock when called with interrupts disabled */
  280. WARN_ON(irqs_disabled());
  281. cpu = smp_processor_id();
  282. allbutself = cpu_online_map;
  283. cpu_clear(cpu, allbutself);
  284. cpus_and(mask, mask, allbutself);
  285. num_cpus = cpus_weight(mask);
  286. /*
  287. * If zero CPUs, return. If just a single CPU, turn this request
  288. * into a targetted single call instead since it's faster.
  289. */
  290. if (!num_cpus)
  291. return 0;
  292. else if (num_cpus == 1) {
  293. cpu = first_cpu(mask);
  294. return smp_call_function_single(cpu, func, info, wait);
  295. }
  296. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  297. if (data) {
  298. data->csd.flags = CSD_FLAG_ALLOC;
  299. if (wait)
  300. data->csd.flags |= CSD_FLAG_WAIT;
  301. } else {
  302. data = &d;
  303. data->csd.flags = CSD_FLAG_WAIT;
  304. wait = 1;
  305. slowpath = 1;
  306. }
  307. spin_lock_init(&data->lock);
  308. data->csd.func = func;
  309. data->csd.info = info;
  310. data->refs = num_cpus;
  311. data->cpumask = mask;
  312. spin_lock_irqsave(&call_function_lock, flags);
  313. list_add_tail_rcu(&data->csd.list, &call_function_queue);
  314. spin_unlock_irqrestore(&call_function_lock, flags);
  315. /* Send a message to all CPUs in the map */
  316. arch_send_call_function_ipi(mask);
  317. /* optionally wait for the CPUs to complete */
  318. if (wait) {
  319. csd_flag_wait(&data->csd);
  320. if (unlikely(slowpath))
  321. smp_call_function_mask_quiesce_stack(allbutself);
  322. }
  323. return 0;
  324. }
  325. EXPORT_SYMBOL(smp_call_function_mask);
  326. /**
  327. * smp_call_function(): Run a function on all other CPUs.
  328. * @func: The function to run. This must be fast and non-blocking.
  329. * @info: An arbitrary pointer to pass to the function.
  330. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  331. *
  332. * Returns 0 on success, else a negative status code.
  333. *
  334. * If @wait is true, then returns once @func has returned; otherwise
  335. * it returns just before the target cpu calls @func. In case of allocation
  336. * failure, @wait will be implicitly turned on.
  337. *
  338. * You must not call this function with disabled interrupts or from a
  339. * hardware interrupt handler or from a bottom half handler.
  340. */
  341. int smp_call_function(void (*func)(void *), void *info, int wait)
  342. {
  343. int ret;
  344. preempt_disable();
  345. ret = smp_call_function_mask(cpu_online_map, func, info, wait);
  346. preempt_enable();
  347. return ret;
  348. }
  349. EXPORT_SYMBOL(smp_call_function);
  350. void ipi_call_lock(void)
  351. {
  352. spin_lock(&call_function_lock);
  353. }
  354. void ipi_call_unlock(void)
  355. {
  356. spin_unlock(&call_function_lock);
  357. }
  358. void ipi_call_lock_irq(void)
  359. {
  360. spin_lock_irq(&call_function_lock);
  361. }
  362. void ipi_call_unlock_irq(void)
  363. {
  364. spin_unlock_irq(&call_function_lock);
  365. }