Skip to contentSkip to conversation

x86/hyperv: Avoid using per-cpu output page in hv_apicid_to_vp_index()

PATCH Applied Michael Kelley 3 participants 3 messages Sep 2 – Sep 13

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/hyperv: Avoid using per-cpu output page in hv_apicid_to_vp_index()

hv_apicid_to_vp_index() currently uses the per-cpu hypercall input and output pages. This function is called when running in VTL2 and when running in an SEV-SNP CoCo VM with no paravisor. In the former case, the output page is allocated, but in the latter case it is not, so the hypervisor stores the output VP index in memory that has not been allocated by the guest.

Fix this by using the input page for both input and output. The hypercall has very small input and output, so sharing the same page for both is straightforward. An alternative fix is to allocate the per-cpu output page when running in an SEV-SNP CoCo guest, but this uses significantly more memory, particularly with larger vCPUs counts.

Fixes 86c48271e0d6 ("x86/hyperv: Fix APIC ID and VP index confusion in hv_snp_boot_ap()")

---
I'm not aware that this bug is causing any real problems because SEV-SNP CoCo VMs on Hyper-V are rarely, if ever, used without a paravisor. But it was on my list of little clean-ups to do, and a recent Sashiko analysis [1] flagged the issue. So the best thing to do is just fix it.

[1] https://lore.kernel.org/linux-hyperv/[email protected]/

Changes in v2:
* Make the new code a bit cleaner by dropping the unnecessary cast to u32 *

 arch/x86/hyperv/hv_init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/hyperv/hv_init.c +2 -1
index d5edc8530964..2a71c70d7e27 100644
@@ -731,7 +731,8 @@ int hv_apicid_to_vp_index(u32 apic_id)
 	input->partition_id = HV_PARTITION_ID_SELF;
 	input->apic_ids[0] = apic_id;
 
-	output = *this_cpu_ptr(hyperv_pcpu_output_arg);
+	/* Treat input as having 2 APIC IDs so output is 64-bit aligned */
+	output = (void *)input + struct_size(input, apic_ids, 2);
 
 	control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_INDEX_FROM_APIC_ID;
 	status = hv_do_hypercall(control, input, output);
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 15 lines · Michael Kelley

hv_apicid_to_vp_index() currently uses the per-cpu hypercall input and output pages. This function is called when running in VTL2 and when running in an SEV-SNP CoCo VM with no paravisor. In the former case, the output page is allocated, but in the latter case it is not, so the hypervisor stores the output VP index in memory that has not been allocated by the guest.

Fix this by using the input page for both input and output. The hypercall has very small input and output, so sharing the same page for both is straightforward. An alternative fix is to allocate the per-cpu output page when running in an SEV-SNP CoCo guest, but this uses significantly more memory, particularly with larger vCPUs counts.

Quoted 8 lines
Fixes 86c48271e0d6 ("x86/hyperv: Fix APIC ID and VP index confusion in hv_snp_boot_ap()")

---
I'm not aware that this bug is causing any real problems because SEV-SNP CoCo VMs on Hyper-V are rarely, if ever, used without a paravisor. But it was on my list of little clean-ups to do, and a recent Sashiko analysis [1] flagged the issue. So the best thing to do is just fix it.

Quoted 28 lines

[1] https://lore.kernel.org/linux-hyperv/[email protected]/

Changes in v2:
* Make the new code a bit cleaner by dropping the unnecessary cast to u32 *

 arch/x86/hyperv/hv_init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/hyperv/hv_init.c +2 -2
index d5edc8530964..2a71c70d7e27 100644
@@ -731,7 +731,8 @@ int hv_apicid_to_vp_index(u32 apic_id)
        input->partition_id = HV_PARTITION_ID_SELF;
        input->apic_ids[0] = apic_id;
 
-       output = *this_cpu_ptr(hyperv_pcpu_output_arg);
+       /* Treat input as having 2 APIC IDs so output is 64-bit aligned */
+       output = (void *)input + struct_size(input, apic_ids, 2);
 
        control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_INDEX_FROM_APIC_ID;
        status = hv_do_hypercall(control, input, output);
--

2.25.1

Signature · 2 lines
Thanks
Tianyu Lan
maintainer Applied
#2 lore raw
Quoted 24 lines · Tianyu Lan
Quoted 15 lines · Michael Kelley

hv_apicid_to_vp_index() currently uses the per-cpu hypercall input and output pages. This function is called when running in VTL2 and when running in an SEV-SNP CoCo VM with no paravisor. In the former case, the output page is allocated, but in the latter case it is not, so the hypervisor stores the output VP index in memory that has not been allocated by the guest.

Fix this by using the input page for both input and output. The hypercall has very small input and output, so sharing the same page for both is straightforward. An alternative fix is to allocate the per-cpu output page when running in an SEV-SNP CoCo guest, but this uses significantly more memory, particularly with larger vCPUs counts.

Quoted 8 lines
Fixes 86c48271e0d6 ("x86/hyperv: Fix APIC ID and VP index confusion in hv_snp_boot_ap()")

---
I'm not aware that this bug is causing any real problems because SEV-SNP CoCo VMs on Hyper-V are rarely, if ever, used without a paravisor. But it was on my list of little clean-ups to do, and a recent Sashiko analysis [1] flagged the issue. So the best thing to do is just fix it.

Quoted 1 line

Applied. Thanks.