Re: [PATCH 5/6] kvm: allocate vcpu pointer array separately

From: Juergen Gross
Date: Mon Jul 26 2021 - 09:46:55 EST


On 26.07.21 15:40, Paolo Bonzini wrote:
On 01/07/21 17:41, Juergen Gross wrote:
  {
-    if (!has_vhe())
+    if (!has_vhe()) {
+        kfree(kvm->vcpus);
          kfree(kvm);
-    else
+    } else {
+        vfree(kvm->vcpus);
          vfree(kvm);
+    }
  }
  int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 79138c91f83d..39cbc4b6bffb 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1440,10 +1440,7 @@ static inline void kvm_ops_static_call_update(void)
  }
  #define __KVM_HAVE_ARCH_VM_ALLOC
-static inline struct kvm *kvm_arch_alloc_vm(void)
-{
-    return __vmalloc(kvm_x86_ops.vm_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
-}
+struct kvm *kvm_arch_alloc_vm(void);
  void kvm_arch_free_vm(struct kvm *kvm);
  #define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLB
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3af398ef1fc9..a9b0bb2221ea 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -10741,9 +10741,28 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
      static_call(kvm_x86_sched_in)(vcpu, cpu);
  }
+struct kvm *kvm_arch_alloc_vm(void)
+{
+    struct kvm *kvm;
+
+    kvm = __vmalloc(kvm_x86_ops.vm_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+    if (!kvm)
+        return NULL;
+
+    kvm->vcpus = __vmalloc(KVM_MAX_VCPUS * sizeof(void *),
+                   GFP_KERNEL_ACCOUNT | __GFP_ZERO);
+    if (!kvm->vcpus) {
+        vfree(kvm);
+        kvm = NULL;
+    }
+

Let's keep this cleaner:

1) use kvfree in the common version of kvm_arch_free_vm

2) split __KVM_HAVE_ARCH_VM_ALLOC and __KVM_HAVE_ARCH_VM_FREE (ARM does not need it once kvfree is used)

3) define a __kvm_arch_free_vm version that is defined even if !__KVM_HAVE_ARCH_VM_FREE, and which can be used on x86.

Okay, will do so.


Juergen

Attachment: OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature