smp.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  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: Bitmask that controls the operation. If %SCF_WAIT is set, wait
  771. * (atomically) until function has completed on other CPUs. If
  772. * %SCF_RUN_LOCAL is set, the function will also be run locally
  773. * if the local CPU is set in the @cpumask.
  774. *
  775. * If @wait is true, then returns once @func has returned.
  776. *
  777. * You must not call this function with disabled interrupts or from a
  778. * hardware interrupt handler or from a bottom half handler. Preemption
  779. * must be disabled when calling this function.
  780. */
  781. void smp_call_function_many(const struct cpumask *mask,
  782. smp_call_func_t func, void *info, bool wait)
  783. {
  784. smp_call_function_many_cond(mask, func, info, wait * SCF_WAIT, NULL);
  785. }
  786. EXPORT_SYMBOL(smp_call_function_many);
  787. /**
  788. * smp_call_function(): Run a function on all other CPUs.
  789. * @func: The function to run. This must be fast and non-blocking.
  790. * @info: An arbitrary pointer to pass to the function.
  791. * @wait: If true, wait (atomically) until function has completed
  792. * on other CPUs.
  793. *
  794. * Returns 0.
  795. *
  796. * If @wait is true, then returns once @func has returned; otherwise
  797. * it returns just before the target cpu calls @func.
  798. *
  799. * You must not call this function with disabled interrupts or from a
  800. * hardware interrupt handler or from a bottom half handler.
  801. */
  802. void smp_call_function(smp_call_func_t func, void *info, int wait)
  803. {
  804. preempt_disable();
  805. smp_call_function_many(cpu_online_mask, func, info, wait);
  806. preempt_enable();
  807. }
  808. EXPORT_SYMBOL(smp_call_function);
  809. /* Setup configured maximum number of CPUs to activate */
  810. unsigned int setup_max_cpus = NR_CPUS;
  811. EXPORT_SYMBOL(setup_max_cpus);
  812. /*
  813. * Setup routine for controlling SMP activation
  814. *
  815. * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
  816. * activation entirely (the MPS table probe still happens, though).
  817. *
  818. * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
  819. * greater than 0, limits the maximum number of CPUs activated in
  820. * SMP mode to <NUM>.
  821. */
  822. void __weak __init arch_disable_smp_support(void) { }
  823. static int __init nosmp(char *str)
  824. {
  825. setup_max_cpus = 0;
  826. arch_disable_smp_support();
  827. return 0;
  828. }
  829. early_param("nosmp", nosmp);
  830. /* this is hard limit */
  831. static int __init nrcpus(char *str)
  832. {
  833. int nr_cpus;
  834. if (get_option(&str, &nr_cpus) && nr_cpus > 0 && nr_cpus < nr_cpu_ids)
  835. set_nr_cpu_ids(nr_cpus);
  836. return 0;
  837. }
  838. early_param("nr_cpus", nrcpus);
  839. static int __init maxcpus(char *str)
  840. {
  841. get_option(&str, &setup_max_cpus);
  842. if (setup_max_cpus == 0)
  843. arch_disable_smp_support();
  844. return 0;
  845. }
  846. early_param("maxcpus", maxcpus);
  847. #if (NR_CPUS > 1) && !defined(CONFIG_FORCE_NR_CPUS)
  848. /* Setup number of possible processor ids */
  849. unsigned int nr_cpu_ids __read_mostly = NR_CPUS;
  850. EXPORT_SYMBOL(nr_cpu_ids);
  851. #endif
  852. /* An arch may set nr_cpu_ids earlier if needed, so this would be redundant */
  853. void __init setup_nr_cpu_ids(void)
  854. {
  855. set_nr_cpu_ids(find_last_bit(cpumask_bits(cpu_possible_mask), NR_CPUS) + 1);
  856. }
  857. /* Called by boot processor to activate the rest. */
  858. void __init smp_init(void)
  859. {
  860. int num_nodes, num_cpus;
  861. idle_threads_init();
  862. cpuhp_threads_init();
  863. pr_info("Bringing up secondary CPUs ...\n");
  864. bringup_nonboot_cpus(setup_max_cpus);
  865. num_nodes = num_online_nodes();
  866. num_cpus = num_online_cpus();
  867. pr_info("Brought up %d node%s, %d CPU%s\n",
  868. num_nodes, str_plural(num_nodes), num_cpus, str_plural(num_cpus));
  869. /* Any cleanup work */
  870. smp_cpus_done(setup_max_cpus);
  871. }
  872. /*
  873. * on_each_cpu_cond(): Call a function on each processor for which
  874. * the supplied function cond_func returns true, optionally waiting
  875. * for all the required CPUs to finish. This may include the local
  876. * processor.
  877. * @cond_func: A callback function that is passed a cpu id and
  878. * the info parameter. The function is called
  879. * with preemption disabled. The function should
  880. * return a blooean value indicating whether to IPI
  881. * the specified CPU.
  882. * @func: The function to run on all applicable CPUs.
  883. * This must be fast and non-blocking.
  884. * @info: An arbitrary pointer to pass to both functions.
  885. * @wait: If true, wait (atomically) until function has
  886. * completed on other CPUs.
  887. *
  888. * Preemption is disabled to protect against CPUs going offline but not online.
  889. * CPUs going online during the call will not be seen or sent an IPI.
  890. *
  891. * You must not call this function with disabled interrupts or
  892. * from a hardware interrupt handler or from a bottom half handler.
  893. */
  894. void on_each_cpu_cond_mask(smp_cond_func_t cond_func, smp_call_func_t func,
  895. void *info, bool wait, const struct cpumask *mask)
  896. {
  897. unsigned int scf_flags = SCF_RUN_LOCAL;
  898. if (wait)
  899. scf_flags |= SCF_WAIT;
  900. preempt_disable();
  901. smp_call_function_many_cond(mask, func, info, scf_flags, cond_func);
  902. preempt_enable();
  903. }
  904. EXPORT_SYMBOL(on_each_cpu_cond_mask);
  905. static void do_nothing(void *unused)
  906. {
  907. }
  908. /**
  909. * kick_all_cpus_sync - Force all cpus out of idle
  910. *
  911. * Used to synchronize the update of pm_idle function pointer. It's
  912. * called after the pointer is updated and returns after the dummy
  913. * callback function has been executed on all cpus. The execution of
  914. * the function can only happen on the remote cpus after they have
  915. * left the idle function which had been called via pm_idle function
  916. * pointer. So it's guaranteed that nothing uses the previous pointer
  917. * anymore.
  918. */
  919. void kick_all_cpus_sync(void)
  920. {
  921. /* Make sure the change is visible before we kick the cpus */
  922. smp_mb();
  923. smp_call_function(do_nothing, NULL, 1);
  924. }
  925. EXPORT_SYMBOL_GPL(kick_all_cpus_sync);
  926. /**
  927. * wake_up_all_idle_cpus - break all cpus out of idle
  928. * wake_up_all_idle_cpus try to break all cpus which is in idle state even
  929. * including idle polling cpus, for non-idle cpus, we will do nothing
  930. * for them.
  931. */
  932. void wake_up_all_idle_cpus(void)
  933. {
  934. int cpu;
  935. for_each_possible_cpu(cpu) {
  936. preempt_disable();
  937. if (cpu != smp_processor_id() && cpu_online(cpu))
  938. wake_up_if_idle(cpu);
  939. preempt_enable();
  940. }
  941. }
  942. EXPORT_SYMBOL_GPL(wake_up_all_idle_cpus);
  943. /**
  944. * struct smp_call_on_cpu_struct - Call a function on a specific CPU
  945. * @work: &work_struct
  946. * @done: &completion to signal
  947. * @func: function to call
  948. * @data: function's data argument
  949. * @ret: return value from @func
  950. * @cpu: target CPU (%-1 for any CPU)
  951. *
  952. * Used to call a function on a specific cpu and wait for it to return.
  953. * Optionally make sure the call is done on a specified physical cpu via vcpu
  954. * pinning in order to support virtualized environments.
  955. */
  956. struct smp_call_on_cpu_struct {
  957. struct work_struct work;
  958. struct completion done;
  959. int (*func)(void *);
  960. void *data;
  961. int ret;
  962. int cpu;
  963. };
  964. static void smp_call_on_cpu_callback(struct work_struct *work)
  965. {
  966. struct smp_call_on_cpu_struct *sscs;
  967. sscs = container_of(work, struct smp_call_on_cpu_struct, work);
  968. if (sscs->cpu >= 0)
  969. hypervisor_pin_vcpu(sscs->cpu);
  970. sscs->ret = sscs->func(sscs->data);
  971. if (sscs->cpu >= 0)
  972. hypervisor_pin_vcpu(-1);
  973. complete(&sscs->done);
  974. }
  975. int smp_call_on_cpu(unsigned int cpu, int (*func)(void *), void *par, bool phys)
  976. {
  977. struct smp_call_on_cpu_struct sscs = {
  978. .done = COMPLETION_INITIALIZER_ONSTACK(sscs.done),
  979. .func = func,
  980. .data = par,
  981. .cpu = phys ? cpu : -1,
  982. };
  983. INIT_WORK_ONSTACK(&sscs.work, smp_call_on_cpu_callback);
  984. if (cpu >= nr_cpu_ids || !cpu_online(cpu))
  985. return -ENXIO;
  986. queue_work_on(cpu, system_wq, &sscs.work);
  987. wait_for_completion(&sscs.done);
  988. destroy_work_on_stack(&sscs.work);
  989. return sscs.ret;
  990. }
  991. EXPORT_SYMBOL_GPL(smp_call_on_cpu);