]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
kvm: Make VM ioctl do valloc for some archs
authorMarc Orr <marcorr@google.com>
Tue, 15 May 2018 11:37:37 +0000 (04:37 -0700)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 1 Jun 2018 17:18:26 +0000 (19:18 +0200)
The kvm struct has been bloating. For example, it's tens of kilo-bytes
for x86, which turns out to be a large amount of memory to allocate
contiguously via kzalloc. Thus, this patch does the following:
1. Uses architecture-specific routines to allocate the kvm struct via
   vzalloc for x86.
2. Switches arm to __KVM_HAVE_ARCH_VM_ALLOC so that it can use vzalloc
   when has_vhe() is true.

Other architectures continue to default to kalloc, as they have a
dependency on kalloc or have a small-enough struct kvm.

Signed-off-by: Marc Orr <marcorr@google.com>
Reviewed-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/arm/include/asm/kvm_host.h
arch/arm64/include/asm/kvm_host.h
arch/x86/kvm/svm.c
arch/x86/kvm/vmx.c
include/linux/kvm_host.h
virt/kvm/arm/arm.c

index f079a2039c8ae4db669acc2d2227628720b26b01..4b12f32f540c2c8d7e88adfa567cf6206927ae64 100644 (file)
@@ -324,4 +324,8 @@ static inline bool kvm_arm_harden_branch_predictor(void)
 static inline void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu) {}
 static inline void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu) {}
 
+#define __KVM_HAVE_ARCH_VM_ALLOC
+struct kvm *kvm_arch_alloc_vm(void);
+void kvm_arch_free_vm(struct kvm *kvm);
+
 #endif /* __ARM_KVM_HOST_H__ */
index a4ca202ff3f2a0f059d90566d18082c744ff9a06..c923d3e17ba3ce511239961749f64ad5b80cc1e5 100644 (file)
@@ -482,4 +482,8 @@ static inline bool kvm_arm_harden_branch_predictor(void)
 void kvm_vcpu_load_sysregs(struct kvm_vcpu *vcpu);
 void kvm_vcpu_put_sysregs(struct kvm_vcpu *vcpu);
 
+#define __KVM_HAVE_ARCH_VM_ALLOC
+struct kvm *kvm_arch_alloc_vm(void);
+void kvm_arch_free_vm(struct kvm *kvm);
+
 #endif /* __ARM64_KVM_HOST_H__ */
index de21d5c5168b865d6fb0e66363329a91c5d0aaff..d9305f1723f572740385a91b252232e00c328d0a 100644 (file)
@@ -1852,13 +1852,13 @@ static void __unregister_enc_region_locked(struct kvm *kvm,
 
 static struct kvm *svm_vm_alloc(void)
 {
-       struct kvm_svm *kvm_svm = kzalloc(sizeof(struct kvm_svm), GFP_KERNEL);
+       struct kvm_svm *kvm_svm = vzalloc(sizeof(struct kvm_svm));
        return &kvm_svm->kvm;
 }
 
 static void svm_vm_free(struct kvm *kvm)
 {
-       kfree(to_kvm_svm(kvm));
+       vfree(to_kvm_svm(kvm));
 }
 
 static void sev_vm_destroy(struct kvm *kvm)
index e50beb76d846ec315b6b29bd5cc1fe92ee2bd64b..d205e9246f991b8e329bb7c662710a0a7dc05c4e 100644 (file)
@@ -10139,13 +10139,13 @@ STACK_FRAME_NON_STANDARD(vmx_vcpu_run);
 
 static struct kvm *vmx_vm_alloc(void)
 {
-       struct kvm_vmx *kvm_vmx = kzalloc(sizeof(struct kvm_vmx), GFP_KERNEL);
+       struct kvm_vmx *kvm_vmx = vzalloc(sizeof(struct kvm_vmx));
        return &kvm_vmx->kvm;
 }
 
 static void vmx_vm_free(struct kvm *kvm)
 {
-       kfree(to_kvm_vmx(kvm));
+       vfree(to_kvm_vmx(kvm));
 }
 
 static void vmx_switch_vmcs(struct kvm_vcpu *vcpu, struct loaded_vmcs *vmcs)
index bca28637bcd4e98168cc778cc339eb937cad2816..4ee7bc548a833c1f5fa16186205dfdbe0557f795 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/preempt.h>
 #include <linux/msi.h>
 #include <linux/slab.h>
+#include <linux/vmalloc.h>
 #include <linux/rcupdate.h>
 #include <linux/ratelimit.h>
 #include <linux/err.h>
@@ -811,6 +812,10 @@ bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu);
 int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu);
 
 #ifndef __KVM_HAVE_ARCH_VM_ALLOC
+/*
+ * All architectures that want to use vzalloc currently also
+ * need their own kvm_arch_alloc_vm implementation.
+ */
 static inline struct kvm *kvm_arch_alloc_vm(void)
 {
        return kzalloc(sizeof(struct kvm), GFP_KERNEL);
index 3d92214c503826835c440c517edaf2ee6d484ad7..72be779cffe20ad8b09bd356bd42b65a39acaea2 100644 (file)
@@ -251,6 +251,21 @@ long kvm_arch_dev_ioctl(struct file *filp,
        return -EINVAL;
 }
 
+struct kvm *kvm_arch_alloc_vm(void)
+{
+       if (!has_vhe())
+               return kzalloc(sizeof(struct kvm), GFP_KERNEL);
+
+       return vzalloc(sizeof(struct kvm));
+}
+
+void kvm_arch_free_vm(struct kvm *kvm)
+{
+       if (!has_vhe())
+               kfree(kvm);
+       else
+               vfree(kvm);
+}
 
 struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
 {