smp.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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/smp.h>
  12. static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
  13. static LIST_HEAD(call_function_queue);
  14. __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_function_lock);
  15. enum {
  16. CSD_FLAG_WAIT = 0x01,
  17. CSD_FLAG_ALLOC = 0x02,
  18. };
  19. struct call_function_data {
  20. struct call_single_data csd;
  21. spinlock_t lock;
  22. unsigned int refs;
  23. cpumask_t cpumask;
  24. struct rcu_head rcu_head;
  25. };
  26. struct call_single_queue {
  27. struct list_head list;
  28. spinlock_t lock;
  29. };
  30. void __cpuinit init_call_single_data(void)
  31. {
  32. int i;
  33. for_each_possible_cpu(i) {
  34. struct call_single_queue *q = &per_cpu(call_single_queue, i);
  35. spin_lock_init(&q->lock);
  36. INIT_LIST_HEAD(&q->list);
  37. }
  38. }
  39. static void csd_flag_wait(struct call_single_data *data)
  40. {
  41. /* Wait for response */
  42. do {
  43. /*
  44. * We need to see the flags store in the IPI handler
  45. */
  46. smp_mb();
  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. if (ipi)
  66. arch_send_call_function_single_ipi(cpu);
  67. if (wait)
  68. csd_flag_wait(data);
  69. }
  70. static void rcu_free_call_data(struct rcu_head *head)
  71. {
  72. struct call_function_data *data;
  73. data = container_of(head, struct call_function_data, rcu_head);
  74. kfree(data);
  75. }
  76. /*
  77. * Invoked by arch to handle an IPI for call function. Must be called with
  78. * interrupts disabled.
  79. */
  80. void generic_smp_call_function_interrupt(void)
  81. {
  82. struct call_function_data *data;
  83. int cpu = get_cpu();
  84. /*
  85. * It's ok to use list_for_each_rcu() here even though we may delete
  86. * 'pos', since list_del_rcu() doesn't clear ->next
  87. */
  88. rcu_read_lock();
  89. list_for_each_entry_rcu(data, &call_function_queue, csd.list) {
  90. int refs;
  91. if (!cpu_isset(cpu, data->cpumask))
  92. continue;
  93. data->csd.func(data->csd.info);
  94. spin_lock(&data->lock);
  95. cpu_clear(cpu, data->cpumask);
  96. WARN_ON(data->refs == 0);
  97. data->refs--;
  98. refs = data->refs;
  99. spin_unlock(&data->lock);
  100. if (refs)
  101. continue;
  102. spin_lock(&call_function_lock);
  103. list_del_rcu(&data->csd.list);
  104. spin_unlock(&call_function_lock);
  105. if (data->csd.flags & CSD_FLAG_WAIT) {
  106. /*
  107. * serialize stores to data with the flag clear
  108. * and wakeup
  109. */
  110. smp_wmb();
  111. data->csd.flags &= ~CSD_FLAG_WAIT;
  112. } else
  113. call_rcu(&data->rcu_head, rcu_free_call_data);
  114. }
  115. rcu_read_unlock();
  116. put_cpu();
  117. }
  118. /*
  119. * Invoked by arch to handle an IPI for call function single. Must be called
  120. * from the arch with interrupts disabled.
  121. */
  122. void generic_smp_call_function_single_interrupt(void)
  123. {
  124. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  125. LIST_HEAD(list);
  126. /*
  127. * Need to see other stores to list head for checking whether
  128. * list is empty without holding q->lock
  129. */
  130. smp_mb();
  131. while (!list_empty(&q->list)) {
  132. unsigned int data_flags;
  133. spin_lock(&q->lock);
  134. list_replace_init(&q->list, &list);
  135. spin_unlock(&q->lock);
  136. while (!list_empty(&list)) {
  137. struct call_single_data *data;
  138. data = list_entry(list.next, struct call_single_data,
  139. list);
  140. list_del(&data->list);
  141. /*
  142. * 'data' can be invalid after this call if
  143. * flags == 0 (when called through
  144. * generic_exec_single(), so save them away before
  145. * making the call.
  146. */
  147. data_flags = data->flags;
  148. data->func(data->info);
  149. if (data_flags & CSD_FLAG_WAIT) {
  150. smp_wmb();
  151. data->flags &= ~CSD_FLAG_WAIT;
  152. } else if (data_flags & CSD_FLAG_ALLOC)
  153. kfree(data);
  154. }
  155. /*
  156. * See comment on outer loop
  157. */
  158. smp_mb();
  159. }
  160. }
  161. /*
  162. * smp_call_function_single - Run a function on a specific CPU
  163. * @func: The function to run. This must be fast and non-blocking.
  164. * @info: An arbitrary pointer to pass to the function.
  165. * @wait: If true, wait until function has completed on other CPUs.
  166. *
  167. * Returns 0 on success, else a negative status code. Note that @wait
  168. * will be implicitly turned on in case of allocation failures, since
  169. * we fall back to on-stack allocation.
  170. */
  171. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  172. int wait)
  173. {
  174. struct call_single_data d;
  175. unsigned long flags;
  176. /* prevent preemption and reschedule on another processor */
  177. int me = get_cpu();
  178. /* Can deadlock when called with interrupts disabled */
  179. WARN_ON(irqs_disabled());
  180. if (cpu == me) {
  181. local_irq_save(flags);
  182. func(info);
  183. local_irq_restore(flags);
  184. } else {
  185. struct call_single_data *data = NULL;
  186. if (!wait) {
  187. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  188. if (data)
  189. data->flags = CSD_FLAG_ALLOC;
  190. }
  191. if (!data) {
  192. data = &d;
  193. data->flags = CSD_FLAG_WAIT;
  194. }
  195. data->func = func;
  196. data->info = info;
  197. generic_exec_single(cpu, data);
  198. }
  199. put_cpu();
  200. return 0;
  201. }
  202. EXPORT_SYMBOL(smp_call_function_single);
  203. /**
  204. * __smp_call_function_single(): Run a function on another CPU
  205. * @cpu: The CPU to run on.
  206. * @data: Pre-allocated and setup data structure
  207. *
  208. * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
  209. * data structure. Useful for embedding @data inside other structures, for
  210. * instance.
  211. *
  212. */
  213. void __smp_call_function_single(int cpu, struct call_single_data *data)
  214. {
  215. /* Can deadlock when called with interrupts disabled */
  216. WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled());
  217. generic_exec_single(cpu, data);
  218. }
  219. /**
  220. * smp_call_function_mask(): Run a function on a set of other CPUs.
  221. * @mask: The set of cpus to run on.
  222. * @func: The function to run. This must be fast and non-blocking.
  223. * @info: An arbitrary pointer to pass to the function.
  224. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  225. *
  226. * Returns 0 on success, else a negative status code.
  227. *
  228. * If @wait is true, then returns once @func has returned. Note that @wait
  229. * will be implicitly turned on in case of allocation failures, since
  230. * we fall back to on-stack allocation.
  231. *
  232. * You must not call this function with disabled interrupts or from a
  233. * hardware interrupt handler or from a bottom half handler. Preemption
  234. * must be disabled when calling this function.
  235. */
  236. int smp_call_function_mask(cpumask_t mask, void (*func)(void *), void *info,
  237. int wait)
  238. {
  239. struct call_function_data d;
  240. struct call_function_data *data = NULL;
  241. cpumask_t allbutself;
  242. unsigned long flags;
  243. int cpu, num_cpus;
  244. /* Can deadlock when called with interrupts disabled */
  245. WARN_ON(irqs_disabled());
  246. cpu = smp_processor_id();
  247. allbutself = cpu_online_map;
  248. cpu_clear(cpu, allbutself);
  249. cpus_and(mask, mask, allbutself);
  250. num_cpus = cpus_weight(mask);
  251. /*
  252. * If zero CPUs, return. If just a single CPU, turn this request
  253. * into a targetted single call instead since it's faster.
  254. */
  255. if (!num_cpus)
  256. return 0;
  257. else if (num_cpus == 1) {
  258. cpu = first_cpu(mask);
  259. return smp_call_function_single(cpu, func, info, wait);
  260. }
  261. if (!wait) {
  262. data = kmalloc(sizeof(*data), GFP_ATOMIC);
  263. if (data)
  264. data->csd.flags = CSD_FLAG_ALLOC;
  265. }
  266. if (!data) {
  267. data = &d;
  268. data->csd.flags = CSD_FLAG_WAIT;
  269. }
  270. spin_lock_init(&data->lock);
  271. data->csd.func = func;
  272. data->csd.info = info;
  273. data->refs = num_cpus;
  274. data->cpumask = mask;
  275. spin_lock_irqsave(&call_function_lock, flags);
  276. list_add_tail_rcu(&data->csd.list, &call_function_queue);
  277. spin_unlock_irqrestore(&call_function_lock, flags);
  278. /* Send a message to all CPUs in the map */
  279. arch_send_call_function_ipi(mask);
  280. /* optionally wait for the CPUs to complete */
  281. if (wait)
  282. csd_flag_wait(&data->csd);
  283. return 0;
  284. }
  285. EXPORT_SYMBOL(smp_call_function_mask);
  286. /**
  287. * smp_call_function(): Run a function on all other CPUs.
  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 on other CPUs.
  291. *
  292. * Returns 0 on success, else a negative status code.
  293. *
  294. * If @wait is true, then returns once @func has returned; otherwise
  295. * it returns just before the target cpu calls @func. In case of allocation
  296. * failure, @wait will be implicitly turned on.
  297. *
  298. * You must not call this function with disabled interrupts or from a
  299. * hardware interrupt handler or from a bottom half handler.
  300. */
  301. int smp_call_function(void (*func)(void *), void *info, int wait)
  302. {
  303. int ret;
  304. preempt_disable();
  305. ret = smp_call_function_mask(cpu_online_map, func, info, wait);
  306. preempt_enable();
  307. return ret;
  308. }
  309. EXPORT_SYMBOL(smp_call_function);
  310. void ipi_call_lock(void)
  311. {
  312. spin_lock(&call_function_lock);
  313. }
  314. void ipi_call_unlock(void)
  315. {
  316. spin_unlock(&call_function_lock);
  317. }
  318. void ipi_call_lock_irq(void)
  319. {
  320. spin_lock_irq(&call_function_lock);
  321. }
  322. void ipi_call_unlock_irq(void)
  323. {
  324. spin_unlock_irq(&call_function_lock);
  325. }