smp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic helpers for smp ipi calls
  4. *
  5. * (C) Jens Axboe <jens.axboe@oracle.com> 2008
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/irq_work.h>
  9. #include <linux/rcupdate.h>
  10. #include <linux/rculist.h>
  11. #include <linux/kernel.h>
  12. #include <linux/export.h>
  13. #include <linux/percpu.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/gfp.h>
  17. #include <linux/smp.h>
  18. #include <linux/cpu.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/idle.h>
  21. #include <linux/hypervisor.h>
  22. #include <linux/sched/clock.h>
  23. #include <linux/nmi.h>
  24. #include <linux/sched/debug.h>
  25. #include <linux/jump_label.h>
  26. #include <linux/string_choices.h>
  27. #include <trace/events/ipi.h>
  28. #define CREATE_TRACE_POINTS
  29. #include <trace/events/csd.h>
  30. #undef CREATE_TRACE_POINTS
  31. #include "smpboot.h"
  32. #include "sched/smp.h"
  33. #define CSD_TYPE(_csd) ((_csd)->node.u_flags & CSD_FLAG_TYPE_MASK)
  34. struct call_function_data {
  35. call_single_data_t __percpu *csd;
  36. cpumask_var_t cpumask;
  37. cpumask_var_t cpumask_ipi;
  38. };
  39. static DEFINE_PER_CPU_ALIGNED(struct call_function_data, cfd_data);
  40. static DEFINE_PER_CPU_SHARED_ALIGNED(struct llist_head, call_single_queue);
  41. static DEFINE_PER_CPU(atomic_t, trigger_backtrace) = ATOMIC_INIT(1);
  42. static void __flush_smp_call_function_queue(bool warn_cpu_offline);
  43. int smpcfd_prepare_cpu(unsigned int cpu)
  44. {
  45. struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
  46. if (!zalloc_cpumask_var_node(&cfd->cpumask, GFP_KERNEL,
  47. cpu_to_node(cpu)))
  48. return -ENOMEM;
  49. if (!zalloc_cpumask_var_node(&cfd->cpumask_ipi, GFP_KERNEL,
  50. cpu_to_node(cpu))) {
  51. free_cpumask_var(cfd->cpumask);
  52. return -ENOMEM;
  53. }
  54. cfd->csd = alloc_percpu(call_single_data_t);
  55. if (!cfd->csd) {
  56. free_cpumask_var(cfd->cpumask);
  57. free_cpumask_var(cfd->cpumask_ipi);
  58. return -ENOMEM;
  59. }
  60. return 0;
  61. }
  62. int smpcfd_dead_cpu(unsigned int cpu)
  63. {
  64. struct call_function_data *cfd = &per_cpu(cfd_data, cpu);
  65. free_cpumask_var(cfd->cpumask);
  66. free_cpumask_var(cfd->cpumask_ipi);
  67. free_percpu(cfd->csd);
  68. return 0;
  69. }
  70. int smpcfd_dying_cpu(unsigned int cpu)
  71. {
  72. /*
  73. * The IPIs for the smp-call-function callbacks queued by other
  74. * CPUs might arrive late, either due to hardware latencies or
  75. * because this CPU disabled interrupts (inside stop-machine)
  76. * before the IPIs were sent. So flush out any pending callbacks
  77. * explicitly (without waiting for the IPIs to arrive), to
  78. * ensure that the outgoing CPU doesn't go offline with work
  79. * still pending.
  80. */
  81. __flush_smp_call_function_queue(false);
  82. irq_work_run();
  83. return 0;
  84. }
  85. void __init call_function_init(void)
  86. {
  87. int i;
  88. for_each_possible_cpu(i)
  89. init_llist_head(&per_cpu(call_single_queue, i));
  90. smpcfd_prepare_cpu(smp_processor_id());
  91. }
  92. static __always_inline void
  93. send_call_function_single_ipi(int cpu)
  94. {
  95. if (call_function_single_prep_ipi(cpu)) {
  96. trace_ipi_send_cpu(cpu, _RET_IP_,
  97. generic_smp_call_function_single_interrupt);
  98. arch_send_call_function_single_ipi(cpu);
  99. }
  100. }
  101. static __always_inline void
  102. send_call_function_ipi_mask(struct cpumask *mask)
  103. {
  104. trace_ipi_send_cpumask(mask, _RET_IP_,
  105. generic_smp_call_function_single_interrupt);
  106. arch_send_call_function_ipi_mask(mask);
  107. }
  108. static __always_inline void
  109. csd_do_func(smp_call_func_t func, void *info, call_single_data_t *csd)
  110. {
  111. trace_csd_function_entry(func, csd);
  112. func(info);
  113. trace_csd_function_exit(func, csd);
  114. }
  115. #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
  116. static DEFINE_STATIC_KEY_MAYBE(CONFIG_CSD_LOCK_WAIT_DEBUG_DEFAULT, csdlock_debug_enabled);
  117. /*
  118. * Parse the csdlock_debug= kernel boot parameter.
  119. *
  120. * If you need to restore the old "ext" value that once provided
  121. * additional debugging information, reapply the following commits:
  122. *
  123. * de7b09ef658d ("locking/csd_lock: Prepare more CSD lock debugging")
  124. * a5aabace5fb8 ("locking/csd_lock: Add more data to CSD lock debugging")
  125. */
  126. static int __init csdlock_debug(char *str)
  127. {
  128. int ret;
  129. unsigned int val = 0;
  130. ret = get_option(&str, &val);
  131. if (ret) {
  132. if (val)
  133. static_branch_enable(&csdlock_debug_enabled);
  134. else
  135. static_branch_disable(&csdlock_debug_enabled);
  136. }
  137. return 1;
  138. }
  139. __setup("csdlock_debug=", csdlock_debug);
  140. static DEFINE_PER_CPU(call_single_data_t *, cur_csd);
  141. static DEFINE_PER_CPU(smp_call_func_t, cur_csd_func);
  142. static DEFINE_PER_CPU(void *, cur_csd_info);
  143. static ulong csd_lock_timeout = 5000; /* CSD lock timeout in milliseconds. */
  144. module_param(csd_lock_timeout, ulong, 0444);
  145. static int panic_on_ipistall; /* CSD panic timeout in milliseconds, 300000 for five minutes. */
  146. module_param(panic_on_ipistall, int, 0444);
  147. static atomic_t csd_bug_count = ATOMIC_INIT(0);
  148. /* Record current CSD work for current CPU, NULL to erase. */
  149. static void __csd_lock_record(call_single_data_t *csd)
  150. {
  151. if (!csd) {
  152. smp_mb(); /* NULL cur_csd after unlock. */
  153. __this_cpu_write(cur_csd, NULL);
  154. return;
  155. }
  156. __this_cpu_write(cur_csd_func, csd->func);
  157. __this_cpu_write(cur_csd_info, csd->info);
  158. smp_wmb(); /* func and info before csd. */
  159. __this_cpu_write(cur_csd, csd);
  160. smp_mb(); /* Update cur_csd before function call. */
  161. /* Or before unlock, as the case may be. */
  162. }
  163. static __always_inline void csd_lock_record(call_single_data_t *csd)
  164. {
  165. if (static_branch_unlikely(&csdlock_debug_enabled))
  166. __csd_lock_record(csd);
  167. }
  168. static int csd_lock_wait_getcpu(call_single_data_t *csd)
  169. {
  170. unsigned int csd_type;
  171. csd_type = CSD_TYPE(csd);
  172. if (csd_type == CSD_TYPE_ASYNC || csd_type == CSD_TYPE_SYNC)
  173. return csd->node.dst; /* Other CSD_TYPE_ values might not have ->dst. */
  174. return -1;
  175. }
  176. static atomic_t n_csd_lock_stuck;
  177. /**
  178. * csd_lock_is_stuck - Has a CSD-lock acquisition been stuck too long?
  179. *
  180. * Returns @true if a CSD-lock acquisition is stuck and has been stuck
  181. * long enough for a "non-responsive CSD lock" message to be printed.
  182. */
  183. bool csd_lock_is_stuck(void)
  184. {
  185. return !!atomic_read(&n_csd_lock_stuck);
  186. }
  187. /*
  188. * Complain if too much time spent waiting. Note that only
  189. * the CSD_TYPE_SYNC/ASYNC types provide the destination CPU,
  190. * so waiting on other types gets much less information.
  191. */
  192. static bool csd_lock_wait_toolong(call_single_data_t *csd, u64 ts0, u64 *ts1, int *bug_id, unsigned long *nmessages)
  193. {
  194. int cpu = -1;
  195. int cpux;
  196. bool firsttime;
  197. u64 ts2, ts_delta;
  198. call_single_data_t *cpu_cur_csd;
  199. unsigned int flags = READ_ONCE(csd->node.u_flags);
  200. unsigned long long csd_lock_timeout_ns = csd_lock_timeout * NSEC_PER_MSEC;
  201. if (!(flags & CSD_FLAG_LOCK)) {
  202. if (!unlikely(*bug_id))
  203. return true;
  204. cpu = csd_lock_wait_getcpu(csd);
  205. pr_alert("csd: CSD lock (#%d) got unstuck on CPU#%02d, CPU#%02d released the lock.\n",
  206. *bug_id, raw_smp_processor_id(), cpu);
  207. atomic_dec(&n_csd_lock_stuck);
  208. return true;
  209. }
  210. ts2 = sched_clock();
  211. /* How long since we last checked for a stuck CSD lock.*/
  212. ts_delta = ts2 - *ts1;
  213. if (likely(ts_delta <= csd_lock_timeout_ns * (*nmessages + 1) *
  214. (!*nmessages ? 1 : (ilog2(num_online_cpus()) / 2 + 1)) ||
  215. csd_lock_timeout_ns == 0))
  216. return false;
  217. if (ts0 > ts2) {
  218. /* Our own sched_clock went backward; don't blame another CPU. */
  219. ts_delta = ts0 - ts2;
  220. pr_alert("sched_clock on CPU %d went backward by %llu ns\n", raw_smp_processor_id(), ts_delta);
  221. *ts1 = ts2;
  222. return false;
  223. }
  224. firsttime = !*bug_id;
  225. if (firsttime)
  226. *bug_id = atomic_inc_return(&csd_bug_count);
  227. cpu = csd_lock_wait_getcpu(csd);
  228. if (WARN_ONCE(cpu < 0 || cpu >= nr_cpu_ids, "%s: cpu = %d\n", __func__, cpu))
  229. cpux = 0;
  230. else
  231. cpux = cpu;
  232. cpu_cur_csd = smp_load_acquire(&per_cpu(cur_csd, cpux)); /* Before func and info. */
  233. /* How long since this CSD lock was stuck. */
  234. ts_delta = ts2 - ts0;
  235. pr_alert("csd: %s non-responsive CSD lock (#%d) on CPU#%d, waiting %lld ns for CPU#%02d %pS(%ps).\n",
  236. firsttime ? "Detected" : "Continued", *bug_id, raw_smp_processor_id(), (s64)ts_delta,
  237. cpu, csd->func, csd->info);
  238. (*nmessages)++;
  239. if (firsttime)
  240. atomic_inc(&n_csd_lock_stuck);
  241. /*
  242. * If the CSD lock is still stuck after 5 minutes, it is unlikely
  243. * to become unstuck. Use a signed comparison to avoid triggering
  244. * on underflows when the TSC is out of sync between sockets.
  245. */
  246. BUG_ON(panic_on_ipistall > 0 && (s64)ts_delta > ((s64)panic_on_ipistall * NSEC_PER_MSEC));
  247. if (cpu_cur_csd && csd != cpu_cur_csd) {
  248. pr_alert("\tcsd: CSD lock (#%d) handling prior %pS(%ps) request.\n",
  249. *bug_id, READ_ONCE(per_cpu(cur_csd_func, cpux)),
  250. READ_ONCE(per_cpu(cur_csd_info, cpux)));
  251. } else {
  252. pr_alert("\tcsd: CSD lock (#%d) %s.\n",
  253. *bug_id, !cpu_cur_csd ? "unresponsive" : "handling this request");
  254. }
  255. if (cpu >= 0) {
  256. if (atomic_cmpxchg_acquire(&per_cpu(trigger_backtrace, cpu), 1, 0))
  257. dump_cpu_task(cpu);
  258. if (!cpu_cur_csd) {
  259. pr_alert("csd: Re-sending CSD lock (#%d) IPI from CPU#%02d to CPU#%02d\n", *bug_id, raw_smp_processor_id(), cpu);
  260. arch_send_call_function_single_ipi(cpu);
  261. }
  262. }
  263. if (firsttime)
  264. dump_stack();
  265. *ts1 = ts2;
  266. return false;
  267. }
  268. /*
  269. * csd_lock/csd_unlock used to serialize access to per-cpu csd resources
  270. *
  271. * For non-synchronous ipi calls the csd can still be in use by the
  272. * previous function call. For multi-cpu calls its even more interesting
  273. * as we'll have to ensure no other cpu is observing our csd.
  274. */
  275. static void __csd_lock_wait(call_single_data_t *csd)
  276. {
  277. unsigned long nmessages = 0;
  278. int bug_id = 0;
  279. u64 ts0, ts1;
  280. ts1 = ts0 = sched_clock();
  281. for (;;) {
  282. if (csd_lock_wait_toolong(csd, ts0, &ts1, &bug_id, &nmessages))
  283. break;
  284. cpu_relax();
  285. }
  286. smp_acquire__after_ctrl_dep();
  287. }
  288. static __always_inline void csd_lock_wait(call_single_data_t *csd)
  289. {
  290. if (static_branch_unlikely(&csdlock_debug_enabled)) {
  291. __csd_lock_wait(csd);
  292. return;
  293. }
  294. smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
  295. }
  296. #else
  297. static void csd_lock_record(call_single_data_t *csd)
  298. {
  299. }
  300. static __always_inline void csd_lock_wait(call_single_data_t *csd)
  301. {
  302. smp_cond_load_acquire(&csd->node.u_flags, !(VAL & CSD_FLAG_LOCK));
  303. }
  304. #endif
  305. static __always_inline void csd_lock(call_single_data_t *csd)
  306. {
  307. csd_lock_wait(csd);
  308. csd->node.u_flags |= CSD_FLAG_LOCK;
  309. /*
  310. * prevent CPU from reordering the above assignment
  311. * to ->flags with any subsequent assignments to other
  312. * fields of the specified call_single_data_t structure:
  313. */
  314. smp_wmb();
  315. }
  316. static __always_inline void csd_unlock(call_single_data_t *csd)
  317. {
  318. WARN_ON(!(csd->node.u_flags & CSD_FLAG_LOCK));
  319. /*
  320. * ensure we're all done before releasing data:
  321. */
  322. smp_store_release(&csd->node.u_flags, 0);
  323. }
  324. static DEFINE_PER_CPU_SHARED_ALIGNED(call_single_data_t, csd_data);
  325. void __smp_call_single_queue(int cpu, struct llist_node *node)
  326. {
  327. /*
  328. * We have to check the type of the CSD before queueing it, because
  329. * once queued it can have its flags cleared by
  330. * flush_smp_call_function_queue()
  331. * even if we haven't sent the smp_call IPI yet (e.g. the stopper
  332. * executes migration_cpu_stop() on the remote CPU).
  333. */
  334. if (trace_csd_queue_cpu_enabled()) {
  335. call_single_data_t *csd;
  336. smp_call_func_t func;
  337. csd = container_of(node, call_single_data_t, node.llist);
  338. func = CSD_TYPE(csd) == CSD_TYPE_TTWU ?
  339. sched_ttwu_pending : csd->func;
  340. trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);
  341. }
  342. /*
  343. * The list addition should be visible to the target CPU when it pops
  344. * the head of the list to pull the entry off it in the IPI handler
  345. * because of normal cache coherency rules implied by the underlying
  346. * llist ops.
  347. *
  348. * If IPIs can go out of order to the cache coherency protocol
  349. * in an architecture, sufficient synchronisation should be added
  350. * to arch code to make it appear to obey cache coherency WRT
  351. * locking and barrier primitives. Generic code isn't really
  352. * equipped to do the right thing...
  353. */
  354. if (llist_add(node, &per_cpu(call_single_queue, cpu)))
  355. send_call_function_single_ipi(cpu);
  356. }
  357. /*
  358. * Insert a previously allocated call_single_data_t element
  359. * for execution on the given CPU. data must already have
  360. * ->func, ->info, and ->flags set.
  361. */
  362. static int generic_exec_single(int cpu, call_single_data_t *csd)
  363. {
  364. if (cpu == smp_processor_id()) {
  365. smp_call_func_t func = csd->func;
  366. void *info = csd->info;
  367. unsigned long flags;
  368. /*
  369. * We can unlock early even for the synchronous on-stack case,
  370. * since we're doing this from the same CPU..
  371. */
  372. csd_lock_record(csd);
  373. csd_unlock(csd);
  374. local_irq_save(flags);
  375. csd_do_func(func, info, NULL);
  376. csd_lock_record(NULL);
  377. local_irq_restore(flags);
  378. return 0;
  379. }
  380. if ((unsigned)cpu >= nr_cpu_ids || !cpu_online(cpu)) {
  381. csd_unlock(csd);
  382. return -ENXIO;
  383. }
  384. __smp_call_single_queue(cpu, &csd->node.llist);
  385. return 0;
  386. }
  387. /**
  388. * generic_smp_call_function_single_interrupt - Execute SMP IPI callbacks
  389. *
  390. * Invoked by arch to handle an IPI for call function single.
  391. * Must be called with interrupts disabled.
  392. */
  393. void generic_smp_call_function_single_interrupt(void)
  394. {
  395. __flush_smp_call_function_queue(true);
  396. }
  397. /**
  398. * __flush_smp_call_function_queue - Flush pending smp-call-function callbacks
  399. *
  400. * @warn_cpu_offline: If set to 'true', warn if callbacks were queued on an
  401. * offline CPU. Skip this check if set to 'false'.
  402. *
  403. * Flush any pending smp-call-function callbacks queued on this CPU. This is
  404. * invoked by the generic IPI handler, as well as by a CPU about to go offline,
  405. * to ensure that all pending IPI callbacks are run before it goes completely
  406. * offline.
  407. *
  408. * Loop through the call_single_queue and run all the queued callbacks.
  409. * Must be called with interrupts disabled.
  410. */
  411. static void __flush_smp_call_function_queue(bool warn_cpu_offline)
  412. {
  413. call_single_data_t *csd, *csd_next;
  414. struct llist_node *entry, *prev;
  415. struct llist_head *head;
  416. static bool warned;
  417. atomic_t *tbt;
  418. lockdep_assert_irqs_disabled();
  419. /* Allow waiters to send backtrace NMI from here onwards */
  420. tbt = this_cpu_ptr(&trigger_backtrace);
  421. atomic_set_release(tbt, 1);
  422. head = this_cpu_ptr(&call_single_queue);
  423. entry = llist_del_all(head);
  424. entry = llist_reverse_order(entry);
  425. /* There shouldn't be any pending callbacks on an offline CPU. */
  426. if (unlikely(warn_cpu_offline && !cpu_online(smp_processor_id()) &&
  427. !warned && entry != NULL)) {
  428. warned = true;
  429. WARN(1, "IPI on offline CPU %d\n", smp_processor_id());
  430. /*
  431. * We don't have to use the _safe() variant here
  432. * because we are not invoking the IPI handlers yet.
  433. */
  434. llist_for_each_entry(csd, entry, node.llist) {
  435. switch (CSD_TYPE(csd)) {
  436. case CSD_TYPE_ASYNC:
  437. case CSD_TYPE_SYNC:
  438. case CSD_TYPE_IRQ_WORK:
  439. pr_warn("IPI callback %pS sent to offline CPU\n",
  440. csd->func);
  441. break;
  442. case CSD_TYPE_TTWU:
  443. pr_warn("IPI task-wakeup sent to offline CPU\n");
  444. break;
  445. default:
  446. pr_warn("IPI callback, unknown type %d, sent to offline CPU\n",
  447. CSD_TYPE(csd));
  448. break;
  449. }
  450. }
  451. }
  452. /*
  453. * First; run all SYNC callbacks, people are waiting for us.
  454. */
  455. prev = NULL;
  456. llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
  457. /* Do we wait until *after* callback? */
  458. if (CSD_TYPE(csd) == CSD_TYPE_SYNC) {
  459. smp_call_func_t func = csd->func;
  460. void *info = csd->info;
  461. if (prev) {
  462. prev->next = &csd_next->node.llist;
  463. } else {
  464. entry = &csd_next->node.llist;
  465. }
  466. csd_lock_record(csd);
  467. csd_do_func(func, info, csd);
  468. csd_unlock(csd);
  469. csd_lock_record(NULL);
  470. } else {
  471. prev = &csd->node.llist;
  472. }
  473. }
  474. if (!entry)
  475. return;
  476. /*
  477. * Second; run all !SYNC callbacks.
  478. */
  479. prev = NULL;
  480. llist_for_each_entry_safe(csd, csd_next, entry, node.llist) {
  481. int type = CSD_TYPE(csd);
  482. if (type != CSD_TYPE_TTWU) {
  483. if (prev) {
  484. prev->next = &csd_next->node.llist;
  485. } else {
  486. entry = &csd_next->node.llist;
  487. }
  488. if (type == CSD_TYPE_ASYNC) {
  489. smp_call_func_t func = csd->func;
  490. void *info = csd->info;
  491. csd_lock_record(csd);
  492. csd_unlock(csd);
  493. csd_do_func(func, info, csd);
  494. csd_lock_record(NULL);
  495. } else if (type == CSD_TYPE_IRQ_WORK) {
  496. irq_work_single(csd);
  497. }
  498. } else {
  499. prev = &csd->node.llist;
  500. }
  501. }
  502. /*
  503. * Third; only CSD_TYPE_TTWU is left, issue those.
  504. */
  505. if (entry) {
  506. csd = llist_entry(entry, typeof(*csd), node.llist);
  507. csd_do_func(sched_ttwu_pending, entry, csd);
  508. }
  509. }
  510. /**
  511. * flush_smp_call_function_queue - Flush pending smp-call-function callbacks
  512. * from task context (idle, migration thread)
  513. *
  514. * When TIF_POLLING_NRFLAG is supported and a CPU is in idle and has it
  515. * set, then remote CPUs can avoid sending IPIs and wake the idle CPU by
  516. * setting TIF_NEED_RESCHED. The idle task on the woken up CPU has to
  517. * handle queued SMP function calls before scheduling.
  518. *
  519. * The migration thread has to ensure that an eventually pending wakeup has
  520. * been handled before it migrates a task.
  521. */
  522. void flush_smp_call_function_queue(void)
  523. {
  524. unsigned int was_pending;
  525. unsigned long flags;
  526. if (llist_empty(this_cpu_ptr(&call_single_queue)))
  527. return;
  528. local_irq_save(flags);
  529. /* Get the already pending soft interrupts for RT enabled kernels */
  530. was_pending = local_softirq_pending();
  531. __flush_smp_call_function_queue(true);
  532. if (local_softirq_pending())
  533. do_softirq_post_smp_call_flush(was_pending);
  534. local_irq_restore(flags);
  535. }
  536. /*
  537. * smp_call_function_single - Run a function on a specific CPU
  538. * @func: The function to run. This must be fast and non-blocking.
  539. * @info: An arbitrary pointer to pass to the function.
  540. * @wait: If true, wait until function has completed on other CPUs.
  541. *
  542. * Returns 0 on success, else a negative status code.
  543. */
  544. int smp_call_function_single(int cpu, smp_call_func_t func, void *info,
  545. int wait)
  546. {
  547. call_single_data_t *csd;
  548. call_single_data_t csd_stack = {
  549. .node = { .u_flags = CSD_FLAG_LOCK | CSD_TYPE_SYNC, },
  550. };
  551. int this_cpu;
  552. int err;
  553. /*
  554. * prevent preemption and reschedule on another processor,
  555. * as well as CPU removal
  556. */
  557. this_cpu = get_cpu();
  558. /*
  559. * Can deadlock when called with interrupts disabled.
  560. * We allow cpu's that are not yet online though, as no one else can
  561. * send smp call function interrupt to this cpu and as such deadlocks
  562. * can't happen.
  563. */
  564. WARN_ON_ONCE(cpu_online(this_cpu) && irqs_disabled()
  565. && !oops_in_progress);
  566. /*
  567. * When @wait we can deadlock when we interrupt between llist_add() and
  568. * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
  569. * csd_lock() on because the interrupt context uses the same csd
  570. * storage.
  571. */
  572. WARN_ON_ONCE(!in_task());
  573. csd = &csd_stack;
  574. if (!wait) {
  575. csd = this_cpu_ptr(&csd_data);
  576. csd_lock(csd);
  577. }
  578. csd->func = func;
  579. csd->info = info;
  580. #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
  581. csd->node.src = smp_processor_id();
  582. csd->node.dst = cpu;
  583. #endif
  584. err = generic_exec_single(cpu, csd);
  585. if (wait)
  586. csd_lock_wait(csd);
  587. put_cpu();
  588. return err;
  589. }
  590. EXPORT_SYMBOL(smp_call_function_single);
  591. /**
  592. * smp_call_function_single_async() - Run an asynchronous function on a
  593. * specific CPU.
  594. * @cpu: The CPU to run on.
  595. * @csd: Pre-allocated and setup data structure
  596. *
  597. * Like smp_call_function_single(), but the call is asynchonous and
  598. * can thus be done from contexts with disabled interrupts.
  599. *
  600. * The caller passes his own pre-allocated data structure
  601. * (ie: embedded in an object) and is responsible for synchronizing it
  602. * such that the IPIs performed on the @csd are strictly serialized.
  603. *
  604. * If the function is called with one csd which has not yet been
  605. * processed by previous call to smp_call_function_single_async(), the
  606. * function will return immediately with -EBUSY showing that the csd
  607. * object is still in progress.
  608. *
  609. * NOTE: Be careful, there is unfortunately no current debugging facility to
  610. * validate the correctness of this serialization.
  611. *
  612. * Return: %0 on success or negative errno value on error
  613. */
  614. int smp_call_function_single_async(int cpu, call_single_data_t *csd)
  615. {
  616. int err = 0;
  617. preempt_disable();
  618. if (csd->node.u_flags & CSD_FLAG_LOCK) {
  619. err = -EBUSY;
  620. goto out;
  621. }
  622. csd->node.u_flags = CSD_FLAG_LOCK;
  623. smp_wmb();
  624. err = generic_exec_single(cpu, csd);
  625. out:
  626. preempt_enable();
  627. return err;
  628. }
  629. EXPORT_SYMBOL_GPL(smp_call_function_single_async);
  630. /*
  631. * smp_call_function_any - Run a function on any of the given cpus
  632. * @mask: The mask of cpus it can run on.
  633. * @func: The function to run. This must be fast and non-blocking.
  634. * @info: An arbitrary pointer to pass to the function.
  635. * @wait: If true, wait until function has completed.
  636. *
  637. * Returns 0 on success, else a negative status code (if no cpus were online).
  638. *
  639. * Selection preference:
  640. * 1) current cpu if in @mask
  641. * 2) any cpu of current node if in @mask
  642. * 3) any other online cpu in @mask
  643. */
  644. int smp_call_function_any(const struct cpumask *mask,
  645. smp_call_func_t func, void *info, int wait)
  646. {
  647. unsigned int cpu;
  648. const struct cpumask *nodemask;
  649. int ret;
  650. /* Try for same CPU (cheapest) */
  651. cpu = get_cpu();
  652. if (cpumask_test_cpu(cpu, mask))
  653. goto call;
  654. /* Try for same node. */
  655. nodemask = cpumask_of_node(cpu_to_node(cpu));
  656. for (cpu = cpumask_first_and(nodemask, mask); cpu < nr_cpu_ids;
  657. cpu = cpumask_next_and(cpu, nodemask, mask)) {
  658. if (cpu_online(cpu))
  659. goto call;
  660. }
  661. /* Any online will do: smp_call_function_single handles nr_cpu_ids. */
  662. cpu = cpumask_any_and(mask, cpu_online_mask);
  663. call:
  664. ret = smp_call_function_single(cpu, func, info, wait);
  665. put_cpu();
  666. return ret;
  667. }
  668. EXPORT_SYMBOL_GPL(smp_call_function_any);
  669. /*
  670. * Flags to be used as scf_flags argument of smp_call_function_many_cond().
  671. *
  672. * %SCF_WAIT: Wait until function execution is completed
  673. * %SCF_RUN_LOCAL: Run also locally if local cpu is set in cpumask
  674. */
  675. #define SCF_WAIT (1U << 0)
  676. #define SCF_RUN_LOCAL (1U << 1)
  677. static void smp_call_function_many_cond(const struct cpumask *mask,
  678. smp_call_func_t func, void *info,
  679. unsigned int scf_flags,
  680. smp_cond_func_t cond_func)
  681. {
  682. int cpu, last_cpu, this_cpu = smp_processor_id();
  683. struct call_function_data *cfd;
  684. bool wait = scf_flags & SCF_WAIT;
  685. int nr_cpus = 0;
  686. bool run_remote = false;
  687. bool run_local = false;
  688. lockdep_assert_preemption_disabled();
  689. /*
  690. * Can deadlock when called with interrupts disabled.
  691. * We allow cpu's that are not yet online though, as no one else can
  692. * send smp call function interrupt to this cpu and as such deadlocks
  693. * can't happen.
  694. */
  695. if (cpu_online(this_cpu) && !oops_in_progress &&
  696. !early_boot_irqs_disabled)
  697. lockdep_assert_irqs_enabled();
  698. /*
  699. * When @wait we can deadlock when we interrupt between llist_add() and
  700. * arch_send_call_function_ipi*(); when !@wait we can deadlock due to
  701. * csd_lock() on because the interrupt context uses the same csd
  702. * storage.
  703. */
  704. WARN_ON_ONCE(!in_task());
  705. /* Check if we need local execution. */
  706. if ((scf_flags & SCF_RUN_LOCAL) && cpumask_test_cpu(this_cpu, mask))
  707. run_local = true;
  708. /* Check if we need remote execution, i.e., any CPU excluding this one. */
  709. cpu = cpumask_first_and(mask, cpu_online_mask);
  710. if (cpu == this_cpu)
  711. cpu = cpumask_next_and(cpu, mask, cpu_online_mask);
  712. if (cpu < nr_cpu_ids)
  713. run_remote = true;
  714. if (run_remote) {
  715. cfd = this_cpu_ptr(&cfd_data);
  716. cpumask_and(cfd->cpumask, mask, cpu_online_mask);
  717. __cpumask_clear_cpu(this_cpu, cfd->cpumask);
  718. cpumask_clear(cfd->cpumask_ipi);
  719. for_each_cpu(cpu, cfd->cpumask) {
  720. call_single_data_t *csd = per_cpu_ptr(cfd->csd, cpu);
  721. if (cond_func && !cond_func(cpu, info)) {
  722. __cpumask_clear_cpu(cpu, cfd->cpumask);
  723. continue;
  724. }
  725. csd_lock(csd);
  726. if (wait)
  727. csd->node.u_flags |= CSD_TYPE_SYNC;
  728. csd->func = func;
  729. csd->info = info;
  730. #ifdef CONFIG_CSD_LOCK_WAIT_DEBUG
  731. csd->node.src = smp_processor_id();
  732. csd->node.dst = cpu;
  733. #endif
  734. trace_csd_queue_cpu(cpu, _RET_IP_, func, csd);
  735. if (llist_add(&csd->node.llist, &per_cpu(call_single_queue, cpu))) {
  736. __cpumask_set_cpu(cpu, cfd->cpumask_ipi);
  737. nr_cpus++;
  738. last_cpu = cpu;
  739. }
  740. }
  741. /*
  742. * Choose the most efficient way to send an IPI. Note that the
  743. * number of CPUs might be zero due to concurrent changes to the
  744. * provided mask.
  745. */
  746. if (nr_cpus == 1)
  747. send_call_function_single_ipi(last_cpu);
  748. else if (likely(nr_cpus > 1))
  749. send_call_function_ipi_mask(cfd->cpumask_ipi);
  750. }
  751. if (run_local && (!cond_func || cond_func(this_cpu, info))) {
  752. unsigned long flags;
  753. local_irq_save(flags);
  754. csd_do_func(func, info, NULL);
  755. local_irq_restore(flags);
  756. }
  757. if (run_remote && wait) {
  758. for_each_cpu(cpu, cfd->cpumask) {
  759. call_single_data_t *csd;
  760. csd = per_cpu_ptr(cfd->csd, cpu);
  761. csd_lock_wait(csd);
  762. }
  763. }
  764. }
  765. /**
  766. * smp_call_function_many(): Run a function on a set of CPUs.
  767. * @mask: The set of cpus to run on (only runs on online subset).
  768. * @func: The function to run. This must be fast and non-blocking.
  769. * @info: An arbitrary pointer to pass to the function.
  770. * @wait: If true, wait (atomically) until function has completed
  771. * on other CPUs.
  772. *
  773. * You must not call this function with disabled interrupts or from a
  774. * hardware interrupt handler or from a bottom half handler. Preemption
  775. * must be disabled when calling this function.
  776. *
  777. * @func is not called on the local CPU even if @mask contains it. Consider
  778. * using on_each_cpu_cond_mask() instead if this is not desirable.
  779. */
  780. void smp_call_function_many(const struct cpumask *mask,
  781. smp_call_func_t func, void *info, bool wait)
  782. {
  783. smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
  784. }
  785. EXPORT_SYMBOL(smp_call_function_many);
  786. /**
  787. * smp_call_function(): Run a function on all other CPUs.
  788. * @func: The function to run. This must be fast and non-blocking.
  789. * @info: An arbitrary pointer to pass to the function.
  790. * @wait: If true, wait (atomically) until function has completed
  791. * on other CPUs.
  792. *
  793. * Returns 0.
  794. *
  795. * If @wait is true, then returns once @func has returned; otherwise
  796. * it returns just before the target cpu calls @func.
  797. *
  798. * You must not call this function with disabled interrupts or from a
  799. * hardware interrupt handler or from a bottom half handler.
  800. */
  801. void smp_call_function(smp_call_func_t func, void *info, int wait)
  802. {
  803. preempt_disable();
  804. smp_call_function_many(cpu_online_mask, func, info, wait);
  805. preempt_enable();
  806. }
  807. EXPORT_SYMBOL(smp_call_function);
  808. /* Setup configured maximum number of CPUs to activate */
  809. unsigned int setup_max_cpus = NR_CPUS;
  810. EXPORT_SYMBOL(setup_max_cpus);
  811. /*
  812. * Setup routine for controlling SMP activation
  813. *
  814. * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
  815. * activation entirely (the MPS table probe still happens, though).
  816. *
  817. * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
  818. * greater than 0, limits the maximum number of CPUs activated in
  819. * SMP mode to <NUM>.
  820. */
  821. void __weak __init arch_disable_smp_support(void) { }
  822. static int __init nosmp(char *str)
  823. {
  824. setup_max_cpus = 0;
  825. arch_disable_smp_support();
  826. return 0;
  827. }
  828. early_param("nosmp", nosmp);
  829. /* this is hard limit */
  830. static int __init nrcpus(char *str)
  831. {
  832. int nr_cpus;
  833. if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
  834. set_nr_cpu_ids(nr_cpus);
  835. return 0;
  836. }
  837. early_param("nr_cpus", nrcpus);
  838. static int __init maxcpus(char *str)
  839. {
  840. get_option(&str, &setup_max_cpus);
  841. if (setup_max_cpus == 0)
  842. arch_disable_smp_support();
  843. return 0;
  844. }
  845. early_param("maxcpus", maxcpus);
  846. #if (NR_CPUS > 1) && !defined(CONFIG_FORCE_NR_CPUS)
  847. /* Setup number of possible processor ids */
  848. unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
  849. EXPORT_SYMBOL(nr_cpu_ids);
  850. #endif
  851. /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
  852. void __init setup_nr_cpu_ids(void)
  853. {
  854. set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + 1);
  855. }
  856. /* Called by boot processor to activate the rest. */
  857. void __init smp_init(void)
  858. {
  859. int num_nodes, num_cpus;
  860. idle_threads_init();
  861. cpuhp_threads_init();
  862. pr_info("Bringing up secondary CPUs ...\n");
  863. bringup_nonboot_cpus(setup_max_cpus);
  864. num_nodes = num_online_nodes();
  865. num_cpus = num_online_cpus();
  866. pr_info("Brought up %d node%s, %d CPU%s\n",
  867. num_nodes, str_plural(num_nodes), num_cpus, str_plural(num_cpus));
  868. /* Any cleanup work */
  869. smp_cpus_done(setup_max_cpus);
  870. }
  871. /*
  872. * on_each_cpu_cond(): Call a function on each processor for which
  873. * the supplied function cond_func returns true, optionally waiting
  874. * for all the required CPUs to finish. This may include the local
  875. * processor.
  876. * @cond_func: A callback function that is passed a cpu id and
  877. * the info parameter. The function is called
  878. * with preemption disabled. The function should
  879. * return a blooean value indicating whether to IPI
  880. * the specified CPU.
  881. * @func: The function to run on all applicable CPUs.
  882. * This must be fast and non-blocking.
  883. * @info: An arbitrary pointer to pass to both functions.
  884. * @wait: If true, wait (atomically) until function has
  885. * completed on other CPUs.
  886. *
  887. * Preemption is disabled to protect against CPUs going offline but not online.
  888. * CPUs going online during the call will not be seen or sent an IPI.
  889. *
  890. * You must not call this function with disabled interrupts or
  891. * from a hardware interrupt handler or from a bottom half handler.
  892. */
  893. void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
  894. void *info, bool wait, const struct cpumask *mask)
  895. {
  896. unsigned int scf_flags = SCF_RUN_LOCAL;
  897. if (wait)
  898. scf_flags |= SCF_WAIT;
  899. preempt_disable();
  900. smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
  901. preempt_enable();
  902. }
  903. EXPORT_SYMBOL(on_each_cpu_cond_mask);
  904. static void do_nothing(void *unused)
  905. {
  906. }
  907. /**
  908. * kick_all_cpus_sync - Force all cpus out of idle
  909. *
  910. * Used to synchronize the update of pm_idle function pointer. It's
  911. * called after the pointer is updated and returns after the dummy
  912. * callback function has been executed on all cpus. The execution of
  913. * the function can only happen on the remote cpus after they have
  914. * left the idle function which had been called via pm_idle function
  915. * pointer. So it's guaranteed that nothing uses the previous pointer
  916. * anymore.
  917. */
  918. void kick_all_cpus_sync(void)
  919. {
  920. /* Make sure the change is visible before we kick the cpus */
  921. smp_mb();
  922. smp_call_function(do_nothing, NULL, 1);
  923. }
  924. EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
  925. /**
  926. * wake_up_all_idle_cpus - break all cpus out of idle
  927. * wake_up_all_idle_cpus try to break all cpus which is in idle state even
  928. * including idle polling cpus, for non-idle cpus, we will do nothing
  929. * for them.
  930. */
  931. void wake_up_all_idle_cpus(void)
  932. {
  933. int cpu;
  934. for_each_possible_cpu(cpu) {
  935. preempt_disable();
  936. if (cpu != smp_processor_id() && cpu_online(cpu))
  937. wake_up_if_idle(cpu);
  938. preempt_enable();
  939. }
  940. }
  941. EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
  942. /**
  943. * struct smp_call_on_cpu_struct - Call a function on a specific CPU
  944. * @work: &work_struct
  945. * @done: &completion to signal
  946. * @func: function to call
  947. * @data: function's data argument
  948. * @ret: return value from @func
  949. * @cpu: target CPU (%-1 for any CPU)
  950. *
  951. * Used to call a function on a specific cpu and wait for it to return.
  952. * Optionally make sure the call is done on a specified physical cpu via vcpu
  953. * pinning in order to support virtualized environments.
  954. */
  955. struct smp_call_on_cpu_struct {
  956. struct work_struct work;
  957. struct completion done;
  958. int (*func)(void *);
  959. void *data;
  960. int ret;
  961. int cpu;
  962. };
  963. static void smp_call_on_cpu_callback(struct work_struct *work)
  964. {
  965. struct smp_call_on_cpu_struct *sscs;
  966. sscs = container_of(work, struct smp_call_on_cpu_struct, work);
  967. if (sscs->cpu >= 0)
  968. hypervisor_pin_vcpu(sscs->cpu);
  969. sscs->ret = sscs->func(sscs->data);
  970. if (sscs->cpu >= 0)
  971. hypervisor_pin_vcpu(-1);
  972. complete(&sscs->done);
  973. }
  974. int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
  975. {
  976. struct smp_call_on_cpu_struct sscs = {
  977. .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
  978. .func = func,
  979. .data = par,
  980. .cpu = phys ? cpu : -1,
  981. };
  982. INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
  983. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  984. return -ENXIO;
  985. queue_work_on(cpu, system_wq, &sscs.work);
  986. wait_for_completion(&sscs.done);
  987. destroy_work_on_stack(&sscs.work);
  988. return sscs.ret;
  989. }
  990. EXPORT_SYMBOL_GPL(smp_call_on_cpu);