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