smp.c 35 KB

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