smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. }
  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_mb();
  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_mb();
  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. int me = get_cpu();
  182. /* Can deadlock when called with interrupts disabled */
  183. WARN_ON(irqs_disabled());
  184. if (cpu == me) {
  185. local_irq_save(flags);
  186. func(info);
  187. local_irq_restore(flags);
  188. } else {
  189. struct call_single_data *data = NULL;
  190. if (!wait) {
  191. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  192. if (data)
  193. data->flags = CSD_FLAG_ALLOC;
  194. }
  195. if (!data) {
  196. data = &d;
  197. data->flags = CSD_FLAG_WAIT;
  198. }
  199. data->func = func;
  200. data->info = info;
  201. generic_exec_single(cpu, data);
  202. }
  203. put_cpu();
  204. return 0;
  205. }
  206. EXPORT_SYMBOL(smp_call_function_single);
  207. /**
  208. * __smp_call_function_single(): Run a function on another CPU
  209. * @cpu: The CPU to run on.
  210. * @data: Pre-allocated and setup data structure
  211. *
  212. * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
  213. * data structure. Useful for embedding @data inside other structures, for
  214. * instance.
  215. *
  216. */
  217. void __smp_call_function_single(int cpu, struct call_single_data *data)
  218. {
  219. /* Can deadlock when called with interrupts disabled */
  220. WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled());
  221. generic_exec_single(cpu, data);
  222. }
  223. /* Dummy function */
  224. static void quiesce_dummy(void *unused)
  225. {
  226. }
  227. /*
  228. * Ensure stack based data used in call function mask is safe to free.
  229. *
  230. * This is needed by smp_call_function_mask when using on-stack data, because
  231. * a single call function queue is shared by all CPUs, and any CPU may pick up
  232. * the data item on the queue at any time before it is deleted. So we need to
  233. * ensure that all CPUs have transitioned through a quiescent state after
  234. * this call.
  235. *
  236. * This is a very slow function, implemented by sending synchronous IPIs to
  237. * all possible CPUs. For this reason, we have to alloc data rather than use
  238. * stack based data even in the case of synchronous calls. The stack based
  239. * data is then just used for deadlock/oom fallback which will be very rare.
  240. *
  241. * If a faster scheme can be made, we could go back to preferring stack based
  242. * data -- the data allocation/free is non-zero cost.
  243. */
  244. static void smp_call_function_mask_quiesce_stack(cpumask_t mask)
  245. {
  246. struct call_single_data data;
  247. int cpu;
  248. data.func = quiesce_dummy;
  249. data.info = NULL;
  250. for_each_cpu_mask(cpu, mask) {
  251. data.flags = CSD_FLAG_WAIT;
  252. generic_exec_single(cpu, &data);
  253. }
  254. }
  255. /**
  256. * smp_call_function_mask(): Run a function on a set of other CPUs.
  257. * @mask: The set of cpus to run on.
  258. * @func: The function to run. This must be fast and non-blocking.
  259. * @info: An arbitrary pointer to pass to the function.
  260. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  261. *
  262. * Returns 0 on success, else a negative status code.
  263. *
  264. * If @wait is true, then returns once @func has returned. Note that @wait
  265. * will be implicitly turned on in case of allocation failures, since
  266. * we fall back to on-stack allocation.
  267. *
  268. * You must not call this function with disabled interrupts or from a
  269. * hardware interrupt handler or from a bottom half handler. Preemption
  270. * must be disabled when calling this function.
  271. */
  272. int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
  273. int wait)
  274. {
  275. struct call_function_data d;
  276. struct call_function_data *data = NULL;
  277. cpumask_t allbutself;
  278. unsigned long flags;
  279. int cpu, num_cpus;
  280. int slowpath = 0;
  281. /* Can deadlock when called with interrupts disabled */
  282. WARN_ON(irqs_disabled());
  283. cpu = smp_processor_id();
  284. allbutself = cpu_online_map;
  285. cpu_clear(cpu, allbutself);
  286. cpus_and(mask, mask, allbutself);
  287. num_cpus = cpus_weight(mask);
  288. /*
  289. * If zero CPUs, return. If just a single CPU, turn this request
  290. * into a targetted single call instead since it's faster.
  291. */
  292. if (!num_cpus)
  293. return 0;
  294. else if (num_cpus == 1) {
  295. cpu = first_cpu(mask);
  296. return smp_call_function_single(cpu, func, info, wait);
  297. }
  298. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  299. if (data) {
  300. data->csd.flags = CSD_FLAG_ALLOC;
  301. if (wait)
  302. data->csd.flags |= CSD_FLAG_WAIT;
  303. } else {
  304. data = &d;
  305. data->csd.flags = CSD_FLAG_WAIT;
  306. wait = 1;
  307. slowpath = 1;
  308. }
  309. spin_lock_init(&data->lock);
  310. data->csd.func = func;
  311. data->csd.info = info;
  312. data->refs = num_cpus;
  313. data->cpumask = mask;
  314. spin_lock_irqsave(&call_function_lock, flags);
  315. list_add_tail_rcu(&data->csd.list, &call_function_queue);
  316. spin_unlock_irqrestore(&call_function_lock, flags);
  317. /* Send a message to all CPUs in the map */
  318. arch_send_call_function_ipi(mask);
  319. /* optionally wait for the CPUs to complete */
  320. if (wait) {
  321. csd_flag_wait(&data->csd);
  322. if (unlikely(slowpath))
  323. smp_call_function_mask_quiesce_stack(mask);
  324. }
  325. return 0;
  326. }
  327. EXPORT_SYMBOL(smp_call_function_mask);
  328. /**
  329. * smp_call_function(): Run a function on all other CPUs.
  330. * @func: The function to run. This must be fast and non-blocking.
  331. * @info: An arbitrary pointer to pass to the function.
  332. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  333. *
  334. * Returns 0 on success, else a negative status code.
  335. *
  336. * If @wait is true, then returns once @func has returned; otherwise
  337. * it returns just before the target cpu calls @func. In case of allocation
  338. * failure, @wait will be implicitly turned on.
  339. *
  340. * You must not call this function with disabled interrupts or from a
  341. * hardware interrupt handler or from a bottom half handler.
  342. */
  343. int smp_call_function(void (*func)(void *), void *info, int wait)
  344. {
  345. int ret;
  346. preempt_disable();
  347. ret = smp_call_function_mask(cpu_online_map, func, info, wait);
  348. preempt_enable();
  349. return ret;
  350. }
  351. EXPORT_SYMBOL(smp_call_function);
  352. void ipi_call_lock(void)
  353. {
  354. spin_lock(&call_function_lock);
  355. }
  356. void ipi_call_unlock(void)
  357. {
  358. spin_unlock(&call_function_lock);
  359. }
  360. void ipi_call_lock_irq(void)
  361. {
  362. spin_lock_irq(&call_function_lock);
  363. }
  364. void ipi_call_unlock_irq(void)
  365. {
  366. spin_unlock_irq(&call_function_lock);
  367. }