hv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/hyperv.h>
  16. #include <linux/random.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/export.h>
  21. #include <clocksource/hyperv_timer.h>
  22. #include <asm/mshyperv.h>
  23. #include <linux/set_memory.h>
  24. #include "hyperv_vmbus.h"
  25. /* The one and only */
  26. struct hv_context hv_context;
  27. EXPORT_SYMBOL_FOR_MODULES(hv_context, "mshv_vtl");
  28. /*
  29. * hv_init - Main initialization routine.
  30. *
  31. * This routine must be called before any other routines in here are called
  32. */
  33. int hv_init(void)
  34. {
  35. hv_context.cpu_context = alloc_percpu(struct hv_per_cpu_context);
  36. if (!hv_context.cpu_context)
  37. return -ENOMEM;
  38. return 0;
  39. }
  40. /*
  41. * hv_post_message - Post a message using the hypervisor message IPC.
  42. *
  43. * This involves a hypercall.
  44. */
  45. int hv_post_message(union hv_connection_id connection_id,
  46. enum hv_message_type message_type,
  47. void *payload, size_t payload_size)
  48. {
  49. struct hv_input_post_message *aligned_msg;
  50. unsigned long flags;
  51. u64 status;
  52. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
  53. return -EMSGSIZE;
  54. local_irq_save(flags);
  55. /*
  56. * A TDX VM with the paravisor must use the decrypted post_msg_page: see
  57. * the comment in struct hv_per_cpu_context. A SNP VM with the paravisor
  58. * can use the encrypted hyperv_pcpu_input_arg because it copies the
  59. * input into the GHCB page, which has been decrypted by the paravisor.
  60. */
  61. if (hv_isolation_type_tdx() && ms_hyperv.paravisor_present)
  62. aligned_msg = this_cpu_ptr(hv_context.cpu_context)->post_msg_page;
  63. else
  64. aligned_msg = *this_cpu_ptr(hyperv_pcpu_input_arg);
  65. aligned_msg->connectionid = connection_id;
  66. aligned_msg->reserved = 0;
  67. aligned_msg->message_type = message_type;
  68. aligned_msg->payload_size = payload_size;
  69. memcpy((void *)aligned_msg->payload, payload, payload_size);
  70. if (ms_hyperv.paravisor_present && !vmbus_is_confidential()) {
  71. /*
  72. * If the VMBus isn't confidential, use the CoCo-specific
  73. * mechanism to communicate with the hypervisor.
  74. */
  75. if (hv_isolation_type_tdx())
  76. status = hv_tdx_hypercall(HVCALL_POST_MESSAGE,
  77. virt_to_phys(aligned_msg), 0);
  78. else if (hv_isolation_type_snp())
  79. status = hv_ghcb_hypercall(HVCALL_POST_MESSAGE,
  80. aligned_msg, NULL,
  81. sizeof(*aligned_msg));
  82. else
  83. status = HV_STATUS_INVALID_PARAMETER;
  84. } else {
  85. u64 control = HVCALL_POST_MESSAGE;
  86. control |= hv_nested ? HV_HYPERCALL_NESTED : 0;
  87. /*
  88. * If there is no paravisor, this will go to the hypervisor.
  89. * In the Confidential VMBus case, there is the paravisor
  90. * to which this will trap.
  91. */
  92. status = hv_do_hypercall(control, aligned_msg, NULL);
  93. }
  94. local_irq_restore(flags);
  95. return hv_result(status);
  96. }
  97. EXPORT_SYMBOL_FOR_MODULES(hv_post_message, "mshv_vtl");
  98. static int hv_alloc_page(void **page, bool decrypt, const char *note)
  99. {
  100. int ret = 0;
  101. /*
  102. * After the page changes its encryption status, its contents might
  103. * appear scrambled on some hardware. Thus `get_zeroed_page` would
  104. * zero the page out in vain, so do that explicitly exactly once.
  105. *
  106. * By default, the page is allocated encrypted in a CoCo VM.
  107. */
  108. *page = (void *)__get_free_page(GFP_KERNEL);
  109. if (!*page)
  110. return -ENOMEM;
  111. if (decrypt)
  112. ret = set_memory_decrypted((unsigned long)*page, 1);
  113. if (ret)
  114. goto failed;
  115. memset(*page, 0, PAGE_SIZE);
  116. return 0;
  117. failed:
  118. /*
  119. * Report the failure but don't put the page back on the free list as
  120. * its encryption status is unknown.
  121. */
  122. pr_err("allocation failed for %s page, error %d, decrypted %d\n",
  123. note, ret, decrypt);
  124. *page = NULL;
  125. return ret;
  126. }
  127. static int hv_free_page(void **page, bool encrypt, const char *note)
  128. {
  129. int ret = 0;
  130. if (!*page)
  131. return 0;
  132. if (encrypt)
  133. ret = set_memory_encrypted((unsigned long)*page, 1);
  134. /*
  135. * In the case of the failure, the page is leaked. Something is wrong,
  136. * prefer to lose the page with the unknown encryption status and stay afloat.
  137. */
  138. if (ret)
  139. pr_err("deallocation failed for %s page, error %d, encrypt %d\n",
  140. note, ret, encrypt);
  141. else
  142. free_page((unsigned long)*page);
  143. *page = NULL;
  144. return ret;
  145. }
  146. int hv_synic_alloc(void)
  147. {
  148. int cpu, ret = -ENOMEM;
  149. struct hv_per_cpu_context *hv_cpu;
  150. const bool decrypt = !vmbus_is_confidential();
  151. /*
  152. * First, zero all per-cpu memory areas so hv_synic_free() can
  153. * detect what memory has been allocated and cleanup properly
  154. * after any failures.
  155. */
  156. for_each_present_cpu(cpu) {
  157. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  158. memset(hv_cpu, 0, sizeof(*hv_cpu));
  159. }
  160. hv_context.hv_numa_map = kzalloc_objs(struct cpumask, nr_node_ids);
  161. if (!hv_context.hv_numa_map) {
  162. pr_err("Unable to allocate NUMA map\n");
  163. goto err;
  164. }
  165. for_each_present_cpu(cpu) {
  166. hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu);
  167. tasklet_init(&hv_cpu->msg_dpc,
  168. vmbus_on_msg_dpc, (unsigned long)hv_cpu);
  169. if (ms_hyperv.paravisor_present && hv_isolation_type_tdx()) {
  170. ret = hv_alloc_page(&hv_cpu->post_msg_page,
  171. decrypt, "post msg");
  172. if (ret)
  173. goto err;
  174. }
  175. /*
  176. * If these SynIC pages are not allocated, SIEF and SIM pages
  177. * are configured using what the root partition or the paravisor
  178. * provides upon reading the SIEFP and SIMP registers.
  179. */
  180. if (!ms_hyperv.paravisor_present && !hv_root_partition()) {
  181. ret = hv_alloc_page(&hv_cpu->hyp_synic_message_page,
  182. decrypt, "hypervisor SynIC msg");
  183. if (ret)
  184. goto err;
  185. ret = hv_alloc_page(&hv_cpu->hyp_synic_event_page,
  186. decrypt, "hypervisor SynIC event");
  187. if (ret)
  188. goto err;
  189. }
  190. if (vmbus_is_confidential()) {
  191. ret = hv_alloc_page(&hv_cpu->para_synic_message_page,
  192. false, "paravisor SynIC msg");
  193. if (ret)
  194. goto err;
  195. ret = hv_alloc_page(&hv_cpu->para_synic_event_page,
  196. false, "paravisor SynIC event");
  197. if (ret)
  198. goto err;
  199. }
  200. }
  201. return 0;
  202. err:
  203. /*
  204. * Any memory allocations that succeeded will be freed when
  205. * the caller cleans up by calling hv_synic_free()
  206. */
  207. return ret;
  208. }
  209. void hv_synic_free(void)
  210. {
  211. int cpu;
  212. const bool encrypt = !vmbus_is_confidential();
  213. for_each_present_cpu(cpu) {
  214. struct hv_per_cpu_context *hv_cpu =
  215. per_cpu_ptr(hv_context.cpu_context, cpu);
  216. if (ms_hyperv.paravisor_present && hv_isolation_type_tdx())
  217. hv_free_page(&hv_cpu->post_msg_page,
  218. encrypt, "post msg");
  219. if (!ms_hyperv.paravisor_present && !hv_root_partition()) {
  220. hv_free_page(&hv_cpu->hyp_synic_event_page,
  221. encrypt, "hypervisor SynIC event");
  222. hv_free_page(&hv_cpu->hyp_synic_message_page,
  223. encrypt, "hypervisor SynIC msg");
  224. }
  225. if (vmbus_is_confidential()) {
  226. hv_free_page(&hv_cpu->para_synic_event_page,
  227. false, "paravisor SynIC event");
  228. hv_free_page(&hv_cpu->para_synic_message_page,
  229. false, "paravisor SynIC msg");
  230. }
  231. }
  232. kfree(hv_context.hv_numa_map);
  233. }
  234. /*
  235. * hv_hyp_synic_enable_regs - Initialize the Synthetic Interrupt Controller
  236. * with the hypervisor.
  237. */
  238. void hv_hyp_synic_enable_regs(unsigned int cpu)
  239. {
  240. struct hv_per_cpu_context *hv_cpu =
  241. per_cpu_ptr(hv_context.cpu_context, cpu);
  242. union hv_synic_simp simp;
  243. union hv_synic_siefp siefp;
  244. union hv_synic_sint shared_sint;
  245. /* Setup the Synic's message page with the hypervisor. */
  246. simp.as_uint64 = hv_get_msr(HV_MSR_SIMP);
  247. simp.simp_enabled = 1;
  248. if (ms_hyperv.paravisor_present || hv_root_partition()) {
  249. /* Mask out vTOM bit and map as decrypted */
  250. u64 base = (simp.base_simp_gpa << HV_HYP_PAGE_SHIFT) &
  251. ~ms_hyperv.shared_gpa_boundary;
  252. hv_cpu->hyp_synic_message_page =
  253. memremap(base, HV_HYP_PAGE_SIZE, MEMREMAP_WB | MEMREMAP_DEC);
  254. if (!hv_cpu->hyp_synic_message_page)
  255. pr_err("Fail to map synic message page.\n");
  256. } else {
  257. simp.base_simp_gpa = virt_to_phys(hv_cpu->hyp_synic_message_page)
  258. >> HV_HYP_PAGE_SHIFT;
  259. }
  260. hv_set_msr(HV_MSR_SIMP, simp.as_uint64);
  261. /* Setup the Synic's event page with the hypervisor. */
  262. siefp.as_uint64 = hv_get_msr(HV_MSR_SIEFP);
  263. siefp.siefp_enabled = 1;
  264. if (ms_hyperv.paravisor_present || hv_root_partition()) {
  265. /* Mask out vTOM bit and map as decrypted */
  266. u64 base = (siefp.base_siefp_gpa << HV_HYP_PAGE_SHIFT) &
  267. ~ms_hyperv.shared_gpa_boundary;
  268. hv_cpu->hyp_synic_event_page =
  269. memremap(base, HV_HYP_PAGE_SIZE, MEMREMAP_WB | MEMREMAP_DEC);
  270. if (!hv_cpu->hyp_synic_event_page)
  271. pr_err("Fail to map synic event page.\n");
  272. } else {
  273. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->hyp_synic_event_page)
  274. >> HV_HYP_PAGE_SHIFT;
  275. }
  276. hv_set_msr(HV_MSR_SIEFP, siefp.as_uint64);
  277. hv_enable_coco_interrupt(cpu, vmbus_interrupt, true);
  278. /* Setup the shared SINT. */
  279. if (vmbus_irq != -1)
  280. enable_percpu_irq(vmbus_irq, 0);
  281. shared_sint.as_uint64 = hv_get_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT);
  282. shared_sint.vector = vmbus_interrupt;
  283. shared_sint.masked = false;
  284. shared_sint.auto_eoi = hv_recommend_using_aeoi();
  285. hv_set_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  286. }
  287. static void hv_hyp_synic_enable_interrupts(void)
  288. {
  289. union hv_synic_scontrol sctrl;
  290. /* Enable the global synic bit */
  291. sctrl.as_uint64 = hv_get_msr(HV_MSR_SCONTROL);
  292. sctrl.enable = 1;
  293. hv_set_msr(HV_MSR_SCONTROL, sctrl.as_uint64);
  294. }
  295. static void hv_para_synic_enable_regs(unsigned int cpu)
  296. {
  297. union hv_synic_simp simp;
  298. union hv_synic_siefp siefp;
  299. struct hv_per_cpu_context *hv_cpu
  300. = per_cpu_ptr(hv_context.cpu_context, cpu);
  301. /* Setup the Synic's message page with the paravisor. */
  302. simp.as_uint64 = hv_para_get_synic_register(HV_MSR_SIMP);
  303. simp.simp_enabled = 1;
  304. simp.base_simp_gpa = virt_to_phys(hv_cpu->para_synic_message_page)
  305. >> HV_HYP_PAGE_SHIFT;
  306. hv_para_set_synic_register(HV_MSR_SIMP, simp.as_uint64);
  307. /* Setup the Synic's event page with the paravisor. */
  308. siefp.as_uint64 = hv_para_get_synic_register(HV_MSR_SIEFP);
  309. siefp.siefp_enabled = 1;
  310. siefp.base_siefp_gpa = virt_to_phys(hv_cpu->para_synic_event_page)
  311. >> HV_HYP_PAGE_SHIFT;
  312. hv_para_set_synic_register(HV_MSR_SIEFP, siefp.as_uint64);
  313. }
  314. static void hv_para_synic_enable_interrupts(void)
  315. {
  316. union hv_synic_scontrol sctrl;
  317. /* Enable the global synic bit */
  318. sctrl.as_uint64 = hv_para_get_synic_register(HV_MSR_SCONTROL);
  319. sctrl.enable = 1;
  320. hv_para_set_synic_register(HV_MSR_SCONTROL, sctrl.as_uint64);
  321. }
  322. int hv_synic_init(unsigned int cpu)
  323. {
  324. if (vmbus_is_confidential())
  325. hv_para_synic_enable_regs(cpu);
  326. /*
  327. * The SINT is set in hv_hyp_synic_enable_regs() by calling
  328. * hv_set_msr(). hv_set_msr() in turn has special case code for the
  329. * SINT MSRs that write to the hypervisor version of the MSR *and*
  330. * the paravisor version of the MSR (but *without* the proxy bit when
  331. * VMBus is confidential).
  332. *
  333. * Then enable interrupts via the paravisor if VMBus is confidential,
  334. * and otherwise via the hypervisor.
  335. */
  336. hv_hyp_synic_enable_regs(cpu);
  337. if (vmbus_is_confidential())
  338. hv_para_synic_enable_interrupts();
  339. else
  340. hv_hyp_synic_enable_interrupts();
  341. hv_stimer_legacy_init(cpu, VMBUS_MESSAGE_SINT);
  342. return 0;
  343. }
  344. void hv_hyp_synic_disable_regs(unsigned int cpu)
  345. {
  346. struct hv_per_cpu_context *hv_cpu =
  347. per_cpu_ptr(hv_context.cpu_context, cpu);
  348. union hv_synic_sint shared_sint;
  349. union hv_synic_simp simp;
  350. union hv_synic_siefp siefp;
  351. shared_sint.as_uint64 = hv_get_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT);
  352. shared_sint.masked = 1;
  353. /* Need to correctly cleanup in the case of SMP!!! */
  354. /* Disable the interrupt */
  355. hv_set_msr(HV_MSR_SINT0 + VMBUS_MESSAGE_SINT, shared_sint.as_uint64);
  356. hv_enable_coco_interrupt(cpu, vmbus_interrupt, false);
  357. simp.as_uint64 = hv_get_msr(HV_MSR_SIMP);
  358. /*
  359. * In Isolation VM, simp and sief pages are allocated by
  360. * paravisor. These pages also will be used by kdump
  361. * kernel. So just reset enable bit here and keep page
  362. * addresses.
  363. */
  364. simp.simp_enabled = 0;
  365. if (ms_hyperv.paravisor_present || hv_root_partition()) {
  366. if (hv_cpu->hyp_synic_message_page) {
  367. memunmap(hv_cpu->hyp_synic_message_page);
  368. hv_cpu->hyp_synic_message_page = NULL;
  369. }
  370. } else {
  371. simp.base_simp_gpa = 0;
  372. }
  373. hv_set_msr(HV_MSR_SIMP, simp.as_uint64);
  374. siefp.as_uint64 = hv_get_msr(HV_MSR_SIEFP);
  375. siefp.siefp_enabled = 0;
  376. if (ms_hyperv.paravisor_present || hv_root_partition()) {
  377. if (hv_cpu->hyp_synic_event_page) {
  378. memunmap(hv_cpu->hyp_synic_event_page);
  379. hv_cpu->hyp_synic_event_page = NULL;
  380. }
  381. } else {
  382. siefp.base_siefp_gpa = 0;
  383. }
  384. hv_set_msr(HV_MSR_SIEFP, siefp.as_uint64);
  385. }
  386. static void hv_hyp_synic_disable_interrupts(void)
  387. {
  388. union hv_synic_scontrol sctrl;
  389. /* Disable the global synic bit */
  390. sctrl.as_uint64 = hv_get_msr(HV_MSR_SCONTROL);
  391. sctrl.enable = 0;
  392. hv_set_msr(HV_MSR_SCONTROL, sctrl.as_uint64);
  393. }
  394. static void hv_para_synic_disable_regs(unsigned int cpu)
  395. {
  396. union hv_synic_simp simp;
  397. union hv_synic_siefp siefp;
  398. /* Disable SynIC's message page in the paravisor. */
  399. simp.as_uint64 = hv_para_get_synic_register(HV_MSR_SIMP);
  400. simp.simp_enabled = 0;
  401. hv_para_set_synic_register(HV_MSR_SIMP, simp.as_uint64);
  402. /* Disable SynIC's event page in the paravisor. */
  403. siefp.as_uint64 = hv_para_get_synic_register(HV_MSR_SIEFP);
  404. siefp.siefp_enabled = 0;
  405. hv_para_set_synic_register(HV_MSR_SIEFP, siefp.as_uint64);
  406. }
  407. static void hv_para_synic_disable_interrupts(void)
  408. {
  409. union hv_synic_scontrol sctrl;
  410. /* Disable the global synic bit */
  411. sctrl.as_uint64 = hv_para_get_synic_register(HV_MSR_SCONTROL);
  412. sctrl.enable = 0;
  413. hv_para_set_synic_register(HV_MSR_SCONTROL, sctrl.as_uint64);
  414. }
  415. #define HV_MAX_TRIES 3
  416. /*
  417. * Scan the event flags page of 'this' CPU looking for any bit that is set. If we find one
  418. * bit set, then wait for a few milliseconds. Repeat these steps for a maximum of 3 times.
  419. * Return 'true', if there is still any set bit after this operation; 'false', otherwise.
  420. *
  421. * If a bit is set, that means there is a pending channel interrupt. The expectation is
  422. * that the normal interrupt handling mechanism will find and process the channel interrupt
  423. * "very soon", and in the process clear the bit.
  424. */
  425. static bool __hv_synic_event_pending(union hv_synic_event_flags *event, int sint)
  426. {
  427. unsigned long *recv_int_page;
  428. bool pending;
  429. u32 relid;
  430. int tries = 0;
  431. if (!event)
  432. return false;
  433. event += sint;
  434. recv_int_page = event->flags; /* assumes VMBus version >= VERSION_WIN8 */
  435. retry:
  436. pending = false;
  437. for_each_set_bit(relid, recv_int_page, HV_EVENT_FLAGS_COUNT) {
  438. /* Special case - VMBus channel protocol messages */
  439. if (relid == 0)
  440. continue;
  441. pending = true;
  442. break;
  443. }
  444. if (pending && tries++ < HV_MAX_TRIES) {
  445. usleep_range(10000, 20000);
  446. goto retry;
  447. }
  448. return pending;
  449. }
  450. static bool hv_synic_event_pending(void)
  451. {
  452. struct hv_per_cpu_context *hv_cpu = this_cpu_ptr(hv_context.cpu_context);
  453. union hv_synic_event_flags *hyp_synic_event_page = hv_cpu->hyp_synic_event_page;
  454. union hv_synic_event_flags *para_synic_event_page = hv_cpu->para_synic_event_page;
  455. return
  456. __hv_synic_event_pending(hyp_synic_event_page, VMBUS_MESSAGE_SINT) ||
  457. __hv_synic_event_pending(para_synic_event_page, VMBUS_MESSAGE_SINT);
  458. }
  459. static int hv_pick_new_cpu(struct vmbus_channel *channel)
  460. {
  461. int ret = -EBUSY;
  462. int start;
  463. int cpu;
  464. lockdep_assert_cpus_held();
  465. lockdep_assert_held(&vmbus_connection.channel_mutex);
  466. /*
  467. * We can't assume that the relevant interrupts will be sent before
  468. * the cpu is offlined on older versions of hyperv.
  469. */
  470. if (vmbus_proto_version < VERSION_WIN10_V5_3)
  471. return -EBUSY;
  472. start = get_random_u32_below(nr_cpu_ids);
  473. for_each_cpu_wrap(cpu, cpu_online_mask, start) {
  474. if (channel->target_cpu == cpu ||
  475. channel->target_cpu == VMBUS_CONNECT_CPU)
  476. continue;
  477. ret = vmbus_channel_set_cpu(channel, cpu);
  478. if (!ret)
  479. break;
  480. }
  481. if (ret)
  482. ret = vmbus_channel_set_cpu(channel, VMBUS_CONNECT_CPU);
  483. return ret;
  484. }
  485. /*
  486. * hv_synic_cleanup - Cleanup routine for hv_synic_init().
  487. */
  488. int hv_synic_cleanup(unsigned int cpu)
  489. {
  490. struct vmbus_channel *channel, *sc;
  491. int ret = 0;
  492. if (vmbus_connection.conn_state != CONNECTED)
  493. goto always_cleanup;
  494. /*
  495. * Hyper-V does not provide a way to change the connect CPU once
  496. * it is set; we must prevent the connect CPU from going offline
  497. * while the VM is running normally. But in the panic or kexec()
  498. * path where the vmbus is already disconnected, the CPU must be
  499. * allowed to shut down.
  500. */
  501. if (cpu == VMBUS_CONNECT_CPU)
  502. return -EBUSY;
  503. /*
  504. * Search for channels which are bound to the CPU we're about to
  505. * cleanup.
  506. */
  507. mutex_lock(&vmbus_connection.channel_mutex);
  508. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  509. if (channel->target_cpu == cpu) {
  510. ret = hv_pick_new_cpu(channel);
  511. if (ret) {
  512. mutex_unlock(&vmbus_connection.channel_mutex);
  513. return ret;
  514. }
  515. }
  516. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  517. if (sc->target_cpu == cpu) {
  518. ret = hv_pick_new_cpu(sc);
  519. if (ret) {
  520. mutex_unlock(&vmbus_connection.channel_mutex);
  521. return ret;
  522. }
  523. }
  524. }
  525. }
  526. mutex_unlock(&vmbus_connection.channel_mutex);
  527. /*
  528. * Scan the event flags page looking for bits that are set and waiting
  529. * with a timeout for vmbus_chan_sched() to process such bits. If bits
  530. * are still set after this operation and VMBus is connected, fail the
  531. * CPU offlining operation.
  532. */
  533. if (vmbus_proto_version >= VERSION_WIN10_V4_1 && hv_synic_event_pending())
  534. return -EBUSY;
  535. always_cleanup:
  536. hv_stimer_legacy_cleanup(cpu);
  537. /*
  538. * First, disable the event and message pages
  539. * used for communicating with the host, and then
  540. * disable the host interrupts if VMBus is not
  541. * confidential.
  542. */
  543. hv_hyp_synic_disable_regs(cpu);
  544. if (!vmbus_is_confidential())
  545. hv_hyp_synic_disable_interrupts();
  546. /*
  547. * Perform the same steps for the Confidential VMBus.
  548. * The sequencing provides the guarantee that no data
  549. * may be posted for processing before disabling interrupts.
  550. */
  551. if (vmbus_is_confidential()) {
  552. hv_para_synic_disable_regs(cpu);
  553. hv_para_synic_disable_interrupts();
  554. }
  555. if (vmbus_irq != -1)
  556. disable_percpu_irq(vmbus_irq);
  557. return ret;
  558. }