smp.c 27 KB

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