[PATCH 22/26] KVM: x86: asi: Stabilize CR3 when potentially accessing with ASI

From: Brendan Jackman
Date: Fri Jul 12 2024 - 13:08:11 EST


nested_vmx_check_vmentry_hw() does a VM Enter as a "dry run" to check
the VMCS. It's important that we VM Exit back into the correct CR3 in
order to avoid going out of sync with ASI state. Under ASI, CR3 is
unstable even when interrupts are disabled, except
a) during the ASI critical section and
b) when the address space is unrestricted. We can take advantage of case
b) here to make sure the VM Enter is safe.

Signed-off-by: Brendan Jackman <jackmanb@xxxxxxxxxx>
---
arch/x86/kvm/vmx/nested.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index d05ddf751491..ffca468f8197 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -3147,6 +3147,14 @@ static int nested_vmx_check_vmentry_hw(struct kvm_vcpu *vcpu)
*/
vmcs_writel(GUEST_RFLAGS, 0);

+ /*
+ * Stabilize CR3 to ensure the VM Exit returns to the correct address
+ * space. This is costly; at the expense of complexity it could be
+ * optimized away by instead doing an asi_enter() to create an ASI
+ * critical section, in the case that we are currently restricted.
+ */
+ asi_exit();
+
cr3 = __get_current_cr3_fast();
if (unlikely(cr3 != vmx->loaded_vmcs->host_state.cr3)) {
vmcs_writel(HOST_CR3, cr3);

--
2.45.2.993.g49e7a77208-goog