smp.c 13 KB

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