smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. #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. .queue = LIST_HEAD_INIT(call_function.queue),
  20. .lock = __SPIN_LOCK_UNLOCKED(call_function.lock),
  21. };
  22. enum {
  23. CSD_FLAG_WAIT = 0x01,
  24. CSD_FLAG_LOCK = 0x02,
  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 (!alloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  48. cpu_to_node(cpu)))
  49. return NOTIFY_BAD;
  50. break;
  51. #ifdef CONFIG_CPU_HOTPLUG
  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_wait/csd_complete are used for synchronous ipi calls
  81. */
  82. static void csd_wait_prepare(struct call_single_data *data)
  83. {
  84. data->flags |= CSD_FLAG_WAIT;
  85. }
  86. static void csd_complete(struct call_single_data *data)
  87. {
  88. if (data->flags & CSD_FLAG_WAIT) {
  89. /*
  90. * ensure we're all done before saying we are
  91. */
  92. smp_mb();
  93. data->flags &= ~CSD_FLAG_WAIT;
  94. }
  95. }
  96. static void csd_wait(struct call_single_data *data)
  97. {
  98. while (data->flags & CSD_FLAG_WAIT)
  99. cpu_relax();
  100. }
  101. /*
  102. * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
  103. *
  104. * For non-synchronous ipi calls the csd can still be in use by the previous
  105. * function call. For multi-cpu calls its even more interesting as we'll have
  106. * to ensure no other cpu is observing our csd.
  107. */
  108. static void csd_lock(struct call_single_data *data)
  109. {
  110. while (data->flags & CSD_FLAG_LOCK)
  111. cpu_relax();
  112. data->flags = CSD_FLAG_LOCK;
  113. /*
  114. * prevent CPU from reordering the above assignment to ->flags
  115. * with any subsequent assignments to other fields of the
  116. * specified call_single_data structure.
  117. */
  118. smp_mb();
  119. }
  120. static void csd_unlock(struct call_single_data *data)
  121. {
  122. WARN_ON(!(data->flags & CSD_FLAG_LOCK));
  123. /*
  124. * ensure we're all done before releasing data
  125. */
  126. smp_mb();
  127. data->flags &= ~CSD_FLAG_LOCK;
  128. }
  129. /*
  130. * Insert a previously allocated call_single_data element for execution
  131. * on the given CPU. data must already have ->func, ->info, and ->flags set.
  132. */
  133. static void generic_exec_single(int cpu, struct call_single_data *data)
  134. {
  135. struct call_single_queue *dst = &per_cpu(call_single_queue, cpu);
  136. int wait = data->flags & CSD_FLAG_WAIT, ipi;
  137. unsigned long flags;
  138. spin_lock_irqsave(&dst->lock, flags);
  139. ipi = list_empty(&dst->list);
  140. list_add_tail(&data->list, &dst->list);
  141. spin_unlock_irqrestore(&dst->lock, flags);
  142. /*
  143. * The list addition should be visible before sending the IPI
  144. * handler locks the list to pull the entry off it because of
  145. * normal cache coherency rules implied by spinlocks.
  146. *
  147. * If IPIs can go out of order to the cache coherency protocol
  148. * in an architecture, sufficient synchronisation should be added
  149. * to arch code to make it appear to obey cache coherency WRT
  150. * locking and barrier primitives. Generic code isn't really equipped
  151. * to do the right thing...
  152. */
  153. if (ipi)
  154. arch_send_call_function_single_ipi(cpu);
  155. if (wait)
  156. csd_wait(data);
  157. }
  158. /*
  159. * Invoked by arch to handle an IPI for call function. Must be called with
  160. * interrupts disabled.
  161. */
  162. void generic_smp_call_function_interrupt(void)
  163. {
  164. struct call_function_data *data;
  165. int cpu = get_cpu();
  166. /*
  167. * Ensure entry is visible on call_function_queue after we have
  168. * entered the IPI. See comment in smp_call_function_many.
  169. * If we don't have this, then we may miss an entry on the list
  170. * and never get another IPI to process it.
  171. */
  172. smp_mb();
  173. /*
  174. * It's ok to use list_for_each_rcu() here even though we may delete
  175. * 'pos', since list_del_rcu() doesn't clear ->next
  176. */
  177. list_for_each_entry_rcu(data, &call_function.queue, csd.list) {
  178. int refs;
  179. spin_lock(&data->lock);
  180. if (!cpumask_test_cpu(cpu, data->cpumask)) {
  181. spin_unlock(&data->lock);
  182. continue;
  183. }
  184. cpumask_clear_cpu(cpu, data->cpumask);
  185. spin_unlock(&data->lock);
  186. data->csd.func(data->csd.info);
  187. spin_lock(&data->lock);
  188. WARN_ON(data->refs == 0);
  189. refs = --data->refs;
  190. if (!refs) {
  191. spin_lock(&call_function.lock);
  192. list_del_rcu(&data->csd.list);
  193. spin_unlock(&call_function.lock);
  194. }
  195. spin_unlock(&data->lock);
  196. if (refs)
  197. continue;
  198. csd_complete(&data->csd);
  199. csd_unlock(&data->csd);
  200. }
  201. put_cpu();
  202. }
  203. /*
  204. * Invoked by arch to handle an IPI for call function single. Must be called
  205. * from the arch with interrupts disabled.
  206. */
  207. void generic_smp_call_function_single_interrupt(void)
  208. {
  209. struct call_single_queue *q = &__get_cpu_var(call_single_queue);
  210. LIST_HEAD(list);
  211. unsigned int data_flags;
  212. spin_lock(&q->lock);
  213. list_replace_init(&q->list, &list);
  214. spin_unlock(&q->lock);
  215. while (!list_empty(&list)) {
  216. struct call_single_data *data;
  217. data = list_entry(list.next, struct call_single_data,
  218. list);
  219. list_del(&data->list);
  220. /*
  221. * 'data' can be invalid after this call if
  222. * flags == 0 (when called through
  223. * generic_exec_single(), so save them away before
  224. * making the call.
  225. */
  226. data_flags = data->flags;
  227. data->func(data->info);
  228. if (data_flags & CSD_FLAG_WAIT)
  229. csd_complete(data);
  230. /*
  231. * Unlocked CSDs are valid through generic_exec_single()
  232. */
  233. if (data_flags & CSD_FLAG_LOCK)
  234. csd_unlock(data);
  235. }
  236. }
  237. static DEFINE_PER_CPU(struct call_single_data, csd_data);
  238. /*
  239. * smp_call_function_single - Run a function on a specific CPU
  240. * @func: The function to run. This must be fast and non-blocking.
  241. * @info: An arbitrary pointer to pass to the function.
  242. * @wait: If true, wait until function has completed on other CPUs.
  243. *
  244. * Returns 0 on success, else a negative status code. Note that @wait
  245. * will be implicitly turned on in case of allocation failures, since
  246. * we fall back to on-stack allocation.
  247. */
  248. int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
  249. int wait)
  250. {
  251. struct call_single_data d = {
  252. .flags = 0,
  253. };
  254. unsigned long flags;
  255. /* prevent preemption and reschedule on another processor,
  256. as well as CPU removal */
  257. int me = get_cpu();
  258. int err = 0;
  259. /* Can deadlock when called with interrupts disabled */
  260. WARN_ON(irqs_disabled());
  261. if (cpu == me) {
  262. local_irq_save(flags);
  263. func(info);
  264. local_irq_restore(flags);
  265. } else if ((unsigned)cpu < nr_cpu_ids && cpu_online(cpu)) {
  266. struct call_single_data *data;
  267. if (!wait) {
  268. /*
  269. * We are calling a function on a single CPU
  270. * and we are not going to wait for it to finish.
  271. * We use a per cpu data to pass the information to
  272. * that CPU. Since all callers of this code will
  273. * use the same data, we must synchronize the
  274. * callers to prevent a new caller from corrupting
  275. * the data before the callee can access it.
  276. *
  277. * The CSD_FLAG_LOCK is used to let us know when
  278. * the IPI handler is done with the data.
  279. * The first caller will set it, and the callee
  280. * will clear it. The next caller must wait for
  281. * it to clear before we set it again. This
  282. * will make sure the callee is done with the
  283. * data before a new caller will use it.
  284. */
  285. data = &__get_cpu_var(csd_data);
  286. csd_lock(data);
  287. } else {
  288. data = &d;
  289. csd_wait_prepare(data);
  290. }
  291. data->func = func;
  292. data->info = info;
  293. generic_exec_single(cpu, data);
  294. } else {
  295. err = -ENXIO; /* CPU not online */
  296. }
  297. put_cpu();
  298. return err;
  299. }
  300. EXPORT_SYMBOL(smp_call_function_single);
  301. /**
  302. * __smp_call_function_single(): Run a function on another CPU
  303. * @cpu: The CPU to run on.
  304. * @data: Pre-allocated and setup data structure
  305. *
  306. * Like smp_call_function_single(), but allow caller to pass in a pre-allocated
  307. * data structure. Useful for embedding @data inside other structures, for
  308. * instance.
  309. *
  310. */
  311. void __smp_call_function_single(int cpu, struct call_single_data *data)
  312. {
  313. /* Can deadlock when called with interrupts disabled */
  314. WARN_ON((data->flags & CSD_FLAG_WAIT) && irqs_disabled());
  315. generic_exec_single(cpu, data);
  316. }
  317. /* FIXME: Shim for archs using old arch_send_call_function_ipi API. */
  318. #ifndef arch_send_call_function_ipi_mask
  319. #define arch_send_call_function_ipi_mask(maskp) \
  320. arch_send_call_function_ipi(*(maskp))
  321. #endif
  322. /**
  323. * smp_call_function_many(): Run a function on a set of other CPUs.
  324. * @mask: The set of cpus to run on (only runs on online subset).
  325. * @func: The function to run. This must be fast and non-blocking.
  326. * @info: An arbitrary pointer to pass to the function.
  327. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  328. *
  329. * If @wait is true, then returns once @func has returned. Note that @wait
  330. * will be implicitly turned on in case of allocation failures, since
  331. * we fall back to on-stack allocation.
  332. *
  333. * You must not call this function with disabled interrupts or from a
  334. * hardware interrupt handler or from a bottom half handler. Preemption
  335. * must be disabled when calling this function.
  336. */
  337. void smp_call_function_many(const struct cpumask *mask,
  338. void (*func)(void *), void *info,
  339. bool wait)
  340. {
  341. struct call_function_data *data;
  342. unsigned long flags;
  343. int cpu, next_cpu, me = smp_processor_id();
  344. /* Can deadlock when called with interrupts disabled */
  345. WARN_ON(irqs_disabled());
  346. /* So, what's a CPU they want? Ignoring this one. */
  347. cpu = cpumask_first_and(mask, cpu_online_mask);
  348. if (cpu == me)
  349. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  350. /* No online cpus? We're done. */
  351. if (cpu >= nr_cpu_ids)
  352. return;
  353. /* Do we have another CPU which isn't us? */
  354. next_cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  355. if (next_cpu == me)
  356. next_cpu = cpumask_next_and(next_cpu, mask, cpu_online_mask);
  357. /* Fastpath: do that cpu by itself. */
  358. if (next_cpu >= nr_cpu_ids) {
  359. smp_call_function_single(cpu, func, info, wait);
  360. return;
  361. }
  362. data = &__get_cpu_var(cfd_data);
  363. csd_lock(&data->csd);
  364. spin_lock_irqsave(&data->lock, flags);
  365. if (wait)
  366. csd_wait_prepare(&data->csd);
  367. data->csd.func = func;
  368. data->csd.info = info;
  369. cpumask_and(data->cpumask, mask, cpu_online_mask);
  370. cpumask_clear_cpu(me, data->cpumask);
  371. data->refs = cpumask_weight(data->cpumask);
  372. spin_lock(&call_function.lock);
  373. /*
  374. * Place entry at the _HEAD_ of the list, so that any cpu still
  375. * observing the entry in generic_smp_call_function_interrupt() will
  376. * not miss any other list entries.
  377. */
  378. list_add_rcu(&data->csd.list, &call_function.queue);
  379. spin_unlock(&call_function.lock);
  380. spin_unlock_irqrestore(&data->lock, flags);
  381. /*
  382. * Make the list addition visible before sending the ipi.
  383. * (IPIs must obey or appear to obey normal Linux cache coherency
  384. * rules -- see comment in generic_exec_single).
  385. */
  386. smp_mb();
  387. /* Send a message to all CPUs in the map */
  388. arch_send_call_function_ipi_mask(data->cpumask);
  389. /* optionally wait for the CPUs to complete */
  390. if (wait)
  391. csd_wait(&data->csd);
  392. }
  393. EXPORT_SYMBOL(smp_call_function_many);
  394. /**
  395. * smp_call_function(): Run a function on all other CPUs.
  396. * @func: The function to run. This must be fast and non-blocking.
  397. * @info: An arbitrary pointer to pass to the function.
  398. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  399. *
  400. * Returns 0.
  401. *
  402. * If @wait is true, then returns once @func has returned; otherwise
  403. * it returns just before the target cpu calls @func. In case of allocation
  404. * failure, @wait will be implicitly turned on.
  405. *
  406. * You must not call this function with disabled interrupts or from a
  407. * hardware interrupt handler or from a bottom half handler.
  408. */
  409. int smp_call_function(void (*func)(void *), void *info, int wait)
  410. {
  411. preempt_disable();
  412. smp_call_function_many(cpu_online_mask, func, info, wait);
  413. preempt_enable();
  414. return 0;
  415. }
  416. EXPORT_SYMBOL(smp_call_function);
  417. void ipi_call_lock(void)
  418. {
  419. spin_lock(&call_function.lock);
  420. }
  421. void ipi_call_unlock(void)
  422. {
  423. spin_unlock(&call_function.lock);
  424. }
  425. void ipi_call_lock_irq(void)
  426. {
  427. spin_lock_irq(&call_function.lock);
  428. }
  429. void ipi_call_unlock_irq(void)
  430. {
  431. spin_unlock_irq(&call_function.lock);
  432. }