kvm: vmx: Stop wasting a page for guest_msrs
authorJim Mattson <jmattson@google.com>
Wed, 4 Dec 2019 00:24:42 +0000 (16:24 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Wed, 4 Dec 2019 11:23:27 +0000 (12:23 +0100)
We will never need more guest_msrs than there are indices in
vmx_msr_index. Thus, at present, the guest_msrs array will not exceed
168 bytes.

Signed-off-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Liran Alon <liran.alon@oracle.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/vmx/vmx.h

index d175429c91b0140666bc2348c8d49609b3dc3b3c..e7ea332ad1e8149ac788a5fabaceda1954d763ed 100644 (file)
@@ -6674,7 +6674,6 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
        free_vpid(vmx->vpid);
        nested_vmx_free_vcpu(vcpu);
        free_loaded_vmcs(vmx->loaded_vmcs);
-       kfree(vmx->guest_msrs);
        kvm_vcpu_uninit(vcpu);
        kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.user_fpu);
        kmem_cache_free(x86_fpu_cache, vmx->vcpu.arch.guest_fpu);
@@ -6731,12 +6730,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
                        goto uninit_vcpu;
        }
 
-       vmx->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL_ACCOUNT);
-       BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) * sizeof(vmx->guest_msrs[0])
-                    > PAGE_SIZE);
-
-       if (!vmx->guest_msrs)
-               goto free_pml;
+       BUILD_BUG_ON(ARRAY_SIZE(vmx_msr_index) != NR_SHARED_MSRS);
 
        for (i = 0; i < ARRAY_SIZE(vmx_msr_index); ++i) {
                u32 index = vmx_msr_index[i];
@@ -6768,7 +6762,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 
        err = alloc_loaded_vmcs(&vmx->vmcs01);
        if (err < 0)
-               goto free_msrs;
+               goto free_pml;
 
        msr_bitmap = vmx->vmcs01.msr_bitmap;
        vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_TSC, MSR_TYPE_R);
@@ -6830,8 +6824,6 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 
 free_vmcs:
        free_loaded_vmcs(vmx->loaded_vmcs);
-free_msrs:
-       kfree(vmx->guest_msrs);
 free_pml:
        vmx_destroy_pml_buffer(vmx);
 uninit_vcpu:
index 7c1b978b2df44efbcf154c0841141b833928b009..a4f7f737c5d44ba91e1a70e8677ed18fcd8ebd7a 100644 (file)
@@ -22,6 +22,12 @@ extern u32 get_umwait_control_msr(void);
 
 #define X2APIC_MSR(r) (APIC_BASE_MSR + ((r) >> 4))
 
+#ifdef CONFIG_X86_64
+#define NR_SHARED_MSRS 7
+#else
+#define NR_SHARED_MSRS 4
+#endif
+
 #define NR_LOADSTORE_MSRS 8
 
 struct vmx_msrs {
@@ -206,7 +212,7 @@ struct vcpu_vmx {
        u32                   idt_vectoring_info;
        ulong                 rflags;
 
-       struct shared_msr_entry *guest_msrs;
+       struct shared_msr_entry guest_msrs[NR_SHARED_MSRS];
        int                   nmsrs;
        int                   save_nmsrs;
        bool                  guest_msrs_ready;