smp.c 11 KB

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