smp.c 9.1 KB

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