smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * Generic helpers for smp ipi calls
  3. *
  4. * (C) Jens Axboe <jens.axboe@oracle.com> 2008
  5. */
  6. #include <linux/rcupdate.h>
  7. #include <linux/rculist.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/percpu.h>
  11. #include <linux/init.h>
  12. #include <linux/smp.h>
  13. #include <linux/cpu.h>
  14. static DEFINE_PER_CPU(struct call_single_queue, call_single_queue);
  15. static struct {
  16. struct list_head queue;
  17. spinlock_t lock;
  18. } call_function __cacheline_aligned_in_smp =
  19. {
  20. .queue = LIST_HEAD_INIT(call_function.queue),
  21. .lock = __SPIN_LOCK_UNLOCKED(call_function.lock),
  22. };
  23. enum {
  24. CSD_FLAG_LOCK = 0x01,
  25. };
  26. struct call_function_data {
  27. struct call_single_data csd;
  28. spinlock_t lock;
  29. unsigned int refs;
  30. cpumask_var_t cpumask;
  31. };
  32. struct call_single_queue {
  33. struct list_head list;
  34. spinlock_t lock;
  35. };
  36. static DEFINE_PER_CPU(struct call_function_data, cfd_data) = {
  37. .lock = __SPIN_LOCK_UNLOCKED(cfd_data.lock),
  38. };
  39. static int
  40. hotplug_cfd(struct notifier_block *nfb, unsigned long action, void *hcpu)
  41. {
  42. long cpu = (long)hcpu;
  43. struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
  44. switch (action) {
  45. case CPU_UP_PREPARE:
  46. case CPU_UP_PREPARE_FROZEN:
  47. if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  48. cpu_to_node(cpu)))
  49. return NOTIFY_BAD;
  50. break;
  51. #ifdef CONFIG_HOTPLUG_CPU
  52. case CPU_UP_CANCELED:
  53. case CPU_UP_CANCELED_FROZEN:
  54. case CPU_DEAD:
  55. case CPU_DEAD_FROZEN:
  56. free_cpumask_var(cfd->cpumask);
  57. break;
  58. #endif
  59. };
  60. return NOTIFY_OK;
  61. }
  62. static struct notifier_block __cpuinitdata hotplug_cfd_notifier = {
  63. .notifier_call = hotplug_cfd,
  64. };
  65. static int __cpuinit init_call_single_data(void)
  66. {
  67. void *cpu = (void *)(long)smp_processor_id();
  68. int i;
  69. for_each_possible_cpu(i) {
  70. struct call_single_queue *q = &per_cpu(call_single_queue, i);
  71. spin_lock_init(&q->lock);
  72. INIT_LIST_HEAD(&q->list);
  73. }
  74. hotplug_cfd(&hotplug_cfd_notifier, CPU_UP_PREPARE, cpu);
  75. register_cpu_notifier(&hotplug_cfd_notifier);
  76. return 0;
  77. }
  78. early_initcall(init_call_single_data);
  79. /*
  80. * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
  81. *
  82. * For non-synchronous ipi calls the csd can still be in use by the
  83. * previous function call. For multi-cpu calls its even more interesting
  84. * as we'll have to ensure no other cpu is observing our csd.
  85. */
  86. static void csd_lock_wait(struct call_single_data *data)
  87. {
  88. while (data->flags & CSD_FLAG_LOCK)
  89. cpu_relax();
  90. }
  91. static void csd_lock(struct call_single_data *data)
  92. {
  93. csd_lock_wait(data);
  94. data->flags = CSD_FLAG_LOCK;
  95. /*
  96. * prevent CPU from reordering the above assignment
  97. * to ->flags with any subsequent assignments to other
  98. * fields of the specified call_single_data structure:
  99. */
  100. smp_mb();
  101. }
  102. static void csd_unlock(struct call_single_data *data)
  103. {
  104. WARN_ON(!(data->flags & CSD_FLAG_LOCK));
  105. /*
  106. * ensure we're all done before releasing data:
  107. */
  108. smp_mb();
  109. data->flags &= ~CSD_FLAG_LOCK;
  110. }
  111. /*
  112. * Insert a previously allocated call_single_data element
  113. * for execution on the given CPU. data must already have
  114. * ->func, ->info, and ->flags set.
  115. */
  116. static
  117. void generic_exec_single(int cpu, struct call_single_data *data, int wait)
  118. {
  119. struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
  120. unsigned long flags;
  121. int ipi;
  122. spin_lock_irqsave(&dst->lock, flags);
  123. ipi = list_empty(&dst->list);
  124. list_add_tail(&data->list, &dst->list);
  125. spin_unlock_irqrestore(&dst->lock, flags);
  126. /*
  127. * The list addition should be visible before sending the IPI
  128. * handler locks the list to pull the entry off it because of
  129. * normal cache coherency rules implied by spinlocks.
  130. *
  131. * If IPIs can go out of order to the cache coherency protocol
  132. * in an architecture, sufficient synchronisation should be added
  133. * to arch code to make it appear to obey cache coherency WRT
  134. * locking and barrier primitives. Generic code isn't really
  135. * equipped to do the right thing...
  136. */
  137. if (ipi)
  138. arch_send_call_function_single_ipi(cpu);
  139. if (wait)
  140. csd_lock_wait(data);
  141. }
  142. /*
  143. * Invoked by arch to handle an IPI for call function. Must be called with
  144. * interrupts disabled.
  145. */
  146. void generic_smp_call_function_interrupt(void)
  147. {
  148. struct call_function_data *data;
  149. int cpu = get_cpu();
  150. /*
  151. * Ensure entry is visible on call_function_queue after we have
  152. * entered the IPI. See comment in smp_call_function_many.
  153. * If we don't have this, then we may miss an entry on the list
  154. * and never get another IPI to process it.
  155. */
  156. smp_mb();
  157. /*
  158. * It's ok to use list_for_each_rcu() here even though we may
  159. * delete 'pos', since list_del_rcu() doesn't clear ->next
  160. */
  161. list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
  162. int refs;
  163. spin_lock(&data->lock);
  164. if (!cpumask_test_cpu(cpu, data->cpumask)) {
  165. spin_unlock(&data->lock);
  166. continue;
  167. }
  168. cpumask_clear_cpu(cpu, data->cpumask);
  169. spin_unlock(&data->lock);
  170. data->csd.func(data->csd.info);
  171. spin_lock(&data->lock);
  172. WARN_ON(data->refs == 0);
  173. refs = --data->refs;
  174. if (!refs) {
  175. spin_lock(&call_function.lock);
  176. list_del_rcu(&data->csd.list);
  177. spin_unlock(&call_function.lock);
  178. }
  179. spin_unlock(&data->lock);
  180. if (refs)
  181. continue;
  182. csd_unlock(&data->csd);
  183. }
  184. put_cpu();
  185. }
  186. /*
  187. * Invoked by arch to handle an IPI for call function single. Must be
  188. * called from the arch with interrupts disabled.
  189. */
  190. void generic_smp_call_function_single_interrupt(void)
  191. {
  192. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  193. unsigned int data_flags;
  194. LIST_HEAD(list);
  195. spin_lock(&q->lock);
  196. list_replace_init(&q->list, &list);
  197. spin_unlock(&q->lock);
  198. while (!list_empty(&list)) {
  199. struct call_single_data *data;
  200. data = list_entry(list.next, struct call_single_data, list);
  201. list_del(&data->list);
  202. /*
  203. * 'data' can be invalid after this call if flags == 0
  204. * (when called through generic_exec_single()),
  205. * so save them away before making the call:
  206. */
  207. data_flags = data->flags;
  208. data->func(data->info);
  209. /*
  210. * Unlocked CSDs are valid through generic_exec_single():
  211. */
  212. if (data_flags & CSD_FLAG_LOCK)
  213. csd_unlock(data);
  214. }
  215. }
  216. static DEFINE_PER_CPU(struct call_single_data, csd_data);
  217. /*
  218. * smp_call_function_single - Run a function on a specific CPU
  219. * @func: The function to run. This must be fast and non-blocking.
  220. * @info: An arbitrary pointer to pass to the function.
  221. * @wait: If true, wait until function has completed on other CPUs.
  222. *
  223. * Returns 0 on success, else a negative status code. Note that @wait
  224. * will be implicitly turned on in case of allocation failures, since
  225. * we fall back to on-stack allocation.
  226. */
  227. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  228. int wait)
  229. {
  230. struct call_single_data d = {
  231. .flags = 0,
  232. };
  233. unsigned long flags;
  234. int this_cpu;
  235. int err = 0;
  236. /*
  237. * prevent preemption and reschedule on another processor,
  238. * as well as CPU removal
  239. */
  240. this_cpu = get_cpu();
  241. /* Can deadlock when called with interrupts disabled */
  242. WARN_ON_ONCE(irqs_disabled() && !oops_in_progress);
  243. if (cpu == this_cpu) {
  244. local_irq_save(flags);
  245. func(info);
  246. local_irq_restore(flags);
  247. } else {
  248. if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
  249. struct call_single_data *data = &d;
  250. if (!wait)
  251. data = &__get_cpu_var(csd_data);
  252. csd_lock(data);
  253. data->func = func;
  254. data->info = info;
  255. generic_exec_single(cpu, data, wait);
  256. } else {
  257. err = -ENXIO; /* CPU not online */
  258. }
  259. }
  260. put_cpu();
  261. return err;
  262. }
  263. EXPORT_SYMBOL(smp_call_function_single);
  264. /**
  265. * __smp_call_function_single(): Run a function on another CPU
  266. * @cpu: The CPU to run on.
  267. * @data: Pre-allocated and setup data structure
  268. *
  269. * Like smp_call_function_single(), but allow caller to pass in a
  270. * pre-allocated data structure. Useful for embedding @data inside
  271. * other structures, for instance.
  272. */
  273. void __smp_call_function_single(int cpu, struct call_single_data *data,
  274. int wait)
  275. {
  276. csd_lock(data);
  277. /* Can deadlock when called with interrupts disabled */
  278. WARN_ON_ONCE(wait && irqs_disabled() && !oops_in_progress);
  279. generic_exec_single(cpu, data, wait);
  280. }
  281. /* Deprecated: shim for archs using old arch_send_call_function_ipi API. */
  282. #ifndef arch_send_call_function_ipi_mask
  283. # define arch_send_call_function_ipi_mask(maskp) \
  284. arch_send_call_function_ipi(*(maskp))
  285. #endif
  286. /**
  287. * smp_call_function_many(): Run a function on a set of other CPUs.
  288. * @mask: The set of cpus to run on (only runs on online subset).
  289. * @func: The function to run. This must be fast and non-blocking.
  290. * @info: An arbitrary pointer to pass to the function.
  291. * @wait: If true, wait (atomically) until function has completed
  292. * on other CPUs.
  293. *
  294. * If @wait is true, then returns once @func has returned. Note that @wait
  295. * will be implicitly turned on in case of allocation failures, since
  296. * we fall back to on-stack allocation.
  297. *
  298. * You must not call this function with disabled interrupts or from a
  299. * hardware interrupt handler or from a bottom half handler. Preemption
  300. * must be disabled when calling this function.
  301. */
  302. void smp_call_function_many(const struct cpumask *mask,
  303. void (*func)(void *), void *info, bool wait)
  304. {
  305. struct call_function_data *data;
  306. unsigned long flags;
  307. int cpu, next_cpu, this_cpu = smp_processor_id();
  308. /* Can deadlock when called with interrupts disabled */
  309. WARN_ON_ONCE(irqs_disabled() && !oops_in_progress);
  310. /* So, what's a CPU they want? Ignoring this one. */
  311. cpu = cpumask_first_and(mask, cpu_online_mask);
  312. if (cpu == this_cpu)
  313. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  314. /* No online cpus? We're done. */
  315. if (cpu >= nr_cpu_ids)
  316. return;
  317. /* Do we have another CPU which isn't us? */
  318. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  319. if (next_cpu == this_cpu)
  320. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  321. /* Fastpath: do that cpu by itself. */
  322. if (next_cpu >= nr_cpu_ids) {
  323. smp_call_function_single(cpu, func, info, wait);
  324. return;
  325. }
  326. data = &__get_cpu_var(cfd_data);
  327. csd_lock(&data->csd);
  328. spin_lock_irqsave(&data->lock, flags);
  329. data->csd.func = func;
  330. data->csd.info = info;
  331. cpumask_and(data->cpumask, mask, cpu_online_mask);
  332. cpumask_clear_cpu(this_cpu, data->cpumask);
  333. data->refs = cpumask_weight(data->cpumask);
  334. spin_lock(&call_function.lock);
  335. /*
  336. * Place entry at the _HEAD_ of the list, so that any cpu still
  337. * observing the entry in generic_smp_call_function_interrupt()
  338. * will not miss any other list entries:
  339. */
  340. list_add_rcu(&data->csd.list, &call_function.queue);
  341. spin_unlock(&call_function.lock);
  342. spin_unlock_irqrestore(&data->lock, flags);
  343. /*
  344. * Make the list addition visible before sending the ipi.
  345. * (IPIs must obey or appear to obey normal Linux cache
  346. * coherency rules -- see comment in generic_exec_single).
  347. */
  348. smp_mb();
  349. /* Send a message to all CPUs in the map */
  350. arch_send_call_function_ipi_mask(data->cpumask);
  351. /* Optionally wait for the CPUs to complete */
  352. if (wait)
  353. csd_lock_wait(&data->csd);
  354. }
  355. EXPORT_SYMBOL(smp_call_function_many);
  356. /**
  357. * smp_call_function(): Run a function on all other CPUs.
  358. * @func: The function to run. This must be fast and non-blocking.
  359. * @info: An arbitrary pointer to pass to the function.
  360. * @wait: If true, wait (atomically) until function has completed
  361. * on other CPUs.
  362. *
  363. * Returns 0.
  364. *
  365. * If @wait is true, then returns once @func has returned; otherwise
  366. * it returns just before the target cpu calls @func. In case of allocation
  367. * failure, @wait will be implicitly turned on.
  368. *
  369. * You must not call this function with disabled interrupts or from a
  370. * hardware interrupt handler or from a bottom half handler.
  371. */
  372. int smp_call_function(void (*func)(void *), void *info, int wait)
  373. {
  374. preempt_disable();
  375. smp_call_function_many(cpu_online_mask, func, info, wait);
  376. preempt_enable();
  377. return 0;
  378. }
  379. EXPORT_SYMBOL(smp_call_function);
  380. void ipi_call_lock(void)
  381. {
  382. spin_lock(&call_function.lock);
  383. }
  384. void ipi_call_unlock(void)
  385. {
  386. spin_unlock(&call_function.lock);
  387. }
  388. void ipi_call_lock_irq(void)
  389. {
  390. spin_lock_irq(&call_function.lock);
  391. }
  392. void ipi_call_unlock_irq(void)
  393. {
  394. spin_unlock_irq(&call_function.lock);
  395. }