KVM: VMX: Save HOST_CR3 in vmx_set_host_fs_gs()
authorLai Jiangshan <laijs@linux.alibaba.com>
Thu, 16 Dec 2021 02:19:36 +0000 (10:19 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 7 Jan 2022 15:44:39 +0000 (10:44 -0500)
The host CR3 in the vcpu thread can only be changed when scheduling,
so commit 15ad9762d69f ("KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()")
changed vmx.c to only save it in vmx_prepare_switch_to_guest().

However, it also has to be synced in vmx_sync_vmcs_host_state() when switching VMCS.
vmx_set_host_fs_gs() is called in both places, so rename it to
vmx_set_vmcs_host_state() and make it update HOST_CR3.

Fixes: 15ad9762d69f ("KVM: VMX: Save HOST_CR3 in vmx_prepare_switch_to_guest()")
Signed-off-by: Lai Jiangshan <laijs@linux.alibaba.com>
Message-Id: <20211216021938.11752-2-jiangshanlai@gmail.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/vmx/nested.c
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/vmx/vmx.h

index 2f6f465..d07a7fa 100644 (file)
@@ -245,7 +245,8 @@ static void vmx_sync_vmcs_host_state(struct vcpu_vmx *vmx,
        src = &prev->host_state;
        dest = &vmx->loaded_vmcs->host_state;
 
-       vmx_set_host_fs_gs(dest, src->fs_sel, src->gs_sel, src->fs_base, src->gs_base);
+       vmx_set_vmcs_host_state(dest, src->cr3, src->fs_sel, src->gs_sel,
+                               src->fs_base, src->gs_base);
        dest->ldt_sel = src->ldt_sel;
 #ifdef CONFIG_X86_64
        dest->ds_sel = src->ds_sel;
index 9bf65e5..ebf511f 100644 (file)
@@ -1069,9 +1069,14 @@ static void pt_guest_exit(struct vcpu_vmx *vmx)
                wrmsrl(MSR_IA32_RTIT_CTL, vmx->pt_desc.host.ctl);
 }
 
-void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
-                       unsigned long fs_base, unsigned long gs_base)
+void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3,
+                            u16 fs_sel, u16 gs_sel,
+                            unsigned long fs_base, unsigned long gs_base)
 {
+       if (unlikely(cr3 != host->cr3)) {
+               vmcs_writel(HOST_CR3, cr3);
+               host->cr3 = cr3;
+       }
        if (unlikely(fs_sel != host->fs_sel)) {
                if (!(fs_sel & 7))
                        vmcs_write16(HOST_FS_SELECTOR, fs_sel);
@@ -1103,7 +1108,6 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
 #ifdef CONFIG_X86_64
        int cpu = raw_smp_processor_id();
 #endif
-       unsigned long cr3;
        unsigned long fs_base, gs_base;
        u16 fs_sel, gs_sel;
        int i;
@@ -1167,14 +1171,8 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu)
        gs_base = segment_base(gs_sel);
 #endif
 
-       vmx_set_host_fs_gs(host_state, fs_sel, gs_sel, fs_base, gs_base);
-
-       /* Host CR3 including its PCID is stable when guest state is loaded. */
-       cr3 = __get_current_cr3_fast();
-       if (unlikely(cr3 != host_state->cr3)) {
-               vmcs_writel(HOST_CR3, cr3);
-               host_state->cr3 = cr3;
-       }
+       vmx_set_vmcs_host_state(host_state, __get_current_cr3_fast(),
+                               fs_sel, gs_sel, fs_base, gs_base);
 
        vmx->guest_state_loaded = true;
 }
index 6c2c1af..1811136 100644 (file)
@@ -371,8 +371,9 @@ int allocate_vpid(void);
 void free_vpid(int vpid);
 void vmx_set_constant_host_state(struct vcpu_vmx *vmx);
 void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu);
-void vmx_set_host_fs_gs(struct vmcs_host_state *host, u16 fs_sel, u16 gs_sel,
-                       unsigned long fs_base, unsigned long gs_base);
+void vmx_set_vmcs_host_state(struct vmcs_host_state *host, unsigned long cr3,
+                            u16 fs_sel, u16 gs_sel,
+                            unsigned long fs_base, unsigned long gs_base);
 int vmx_get_cpl(struct kvm_vcpu *vcpu);
 bool vmx_emulation_required(struct kvm_vcpu *vcpu);
 unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);