diff mbox series

[4/5] UBUNTU: SAUCE: Drivers: hv: vmbus: support >64 VPs for a TDX VM without the pavavisor

Message ID 20230724170017.17988-5-tim.gardner@canonical.com
State New
Headers show
Series Azure: TDX updates to support HCL | expand

Commit Message

Tim Gardner July 24, 2023, 5 p.m. UTC
From: Dexuan Cui <decui@microsoft.com>

BugLink: https://bugs.launchpad.net/bugs/2028286

Don't set *this_cpu_ptr(hyperv_pcpu_input_arg) before the call
set_memory_decrypted() returns, otherwise we can run into this ticky issue:

1. In hv_common_cpu_init(), *this_cpu_ptr(hyperv_pcpu_input_arg) is set to
   a private (i.e. encrypted) page.

2. hv_common_cpu_init() -> set_memory_decrypted() -> __set_memory_enc_dec ->
     __set_memory_enc_pgtable() -> cpa_flush() -> on_each_cpu() ->
    on_each_cpu_cond_mask() -> smp_call_function_many_cond() ->
    arch_send_call_function_ipi_mask() -> smp_ops.send_call_func_ipi() ->
    native_send_call_func_ipi() -> apic->send_IPI_allbutself() ->
    hv_send_ipi_allbutself() -> hv_send_ipi_mask_allbutself() ->
    __send_ipi_mask():

      When the VM has >64 vCPUs,  __send_ipi_mask_ex() is called:

         arg = (struct hv_send_ipi_ex **)this_cpu_ptr(hyperv_pcpu_input_arg);
         ipi_arg = *arg;
	 hv_do_rep_hypercall(HVCALL_SEND_IPI_EX, 0, nr_bank, ipi_arg, NULL);

          The hv_do_rep_hypercall() will trigger a fatal fault because Hyper-V
          requires that the 'ipi_arg' should point to a shared (i.e. decrypted) page.

Avoid the fatal fault by setting *this_cpu_ptr(hyperv_pcpu_input_arg) after
calling set_memory_decrypted() returns: by doing this, __send_ipi_mask_ex()
returns HV_STATUS_INVALID_PARAMETER because *this_cpu_ptr(hyperv_pcpu_input_arg)
is still NULL, and __send_ipi_mask() returns false, meaning hv_send_ipi_allbutself()
calls orig_apic.send_IPI_all(), i.e. x2apic_send_IPI_all), to send IPIs.
x2apic_send_IPI_all() doesn't depend on *this_cpu_ptr(hyperv_pcpu_input_arg.

Signed-off-by: Dexuan Cui <decui@microsoft.com>
(cherry picked from commit f1e61e384cae06a16b97c63fa2238313ad090a3c https://github.com/dcui/linux)
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 drivers/hv/hv_common.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c
index 6c1fcfc6894a..23871a9d02e9 100644
--- a/drivers/hv/hv_common.c
+++ b/drivers/hv/hv_common.c
@@ -129,6 +129,7 @@  int hv_common_cpu_init(unsigned int cpu)
 	u64 msr_vp_index;
 	gfp_t flags;
 	int pgcount = hv_root_partition ? 2 : 1;
+	void *mem;
 	int ret;
 
 	/* hv_cpu_init() can be called with IRQs disabled from hv_resume() */
@@ -141,25 +142,26 @@  int hv_common_cpu_init(unsigned int cpu)
 	 * allocated if this CPU was previously online and then taken offline
 	 */
 	if (!*inputarg) {
-		*inputarg = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
-		if (!(*inputarg))
+		mem = kmalloc(pgcount * HV_HYP_PAGE_SIZE, flags);
+		if (!mem)
 			return -ENOMEM;
 
 		if (hv_isolation_type_tdx()) {
-			ret = set_memory_decrypted((unsigned long)*inputarg, pgcount);
-			if (ret) {
-				/* It may be unsafe to free *inputarg */
-				*inputarg = NULL;
+			ret = set_memory_decrypted((unsigned long)mem, pgcount);
+
+			/* It may be unsafe to free mem upon error. */
+			if (ret)
 				return ret;
-			}
 
-			memset(*inputarg, 0x00, pgcount * HV_HYP_PAGE_SIZE);
+			memset(mem, 0x00, pgcount * HV_HYP_PAGE_SIZE);
 		}
 
 		if (hv_root_partition) {
 			outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
-			*outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE;
+			*outputarg = (char *)mem + HV_HYP_PAGE_SIZE;
 		}
+
+		*inputarg = mem;
 	}
 
 	msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);