@@ -1581,6 +1581,8 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id);
int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu);
void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
+struct kvm_vcpu *kvm_arch_vcpu_alloc(void);
+void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu);
#ifdef CONFIG_HAVE_KVM_PM_NOTIFIER
int kvm_arch_pm_notifier(struct kvm *kvm, unsigned long state);
@@ -476,7 +476,7 @@ static void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
put_pid(vcpu->pid);
free_page((unsigned long)vcpu->run);
- kmem_cache_free(kvm_vcpu_cache, vcpu);
+ kvm_arch_vcpu_free(vcpu);
}
void kvm_destroy_vcpus(struct kvm *kvm)
@@ -4067,6 +4067,16 @@ static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
}
#endif
+struct kvm_vcpu __attribute__((weak)) *kvm_arch_vcpu_alloc(void)
+{
+ return kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
+}
+
+void __attribute__((weak)) kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
+{
+ return kmem_cache_free(kvm_vcpu_cache, vcpu);
+}
+
/*
* Creates some virtual cpus. Good luck creating more than one.
*/
@@ -4103,7 +4113,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
kvm->created_vcpus++;
mutex_unlock(&kvm->lock);
- vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
+ vcpu = kvm_arch_vcpu_alloc();
if (!vcpu) {
r = -ENOMEM;
goto vcpu_decrement;
@@ -4182,7 +4192,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, unsigned long id)
vcpu_free_run_page:
free_page((unsigned long)vcpu->run);
vcpu_free:
- kmem_cache_free(kvm_vcpu_cache, vcpu);
+ kvm_arch_vcpu_free(vcpu);
vcpu_decrement:
mutex_lock(&kvm->lock);
kvm->created_vcpus--;
Gunyah KVM backend [1] requires custom vCPU allocation to associate architecture-specific state with each virtual CPU. The generic KVM core currently allocates vCPUs directly using the kvm_vcpu_cache slab, which does not allow architecture code to intervene in the allocation process. Introduce two weakly-defined functions, kvm_arch_vcpu_alloc() and kvm_arch_vcpu_free(), which default to using kmem_cache_zalloc() and kmem_cache_free() respectively. Architectures can override these functions to implement custom vCPU allocation behavior. Replace all direct allocations and frees of vCPUs in kvm_main.c with calls to these helper functions to allow arch-specific substitution. This change is required to support architectures such as Gunyah that must allocate architecture-private state along with the vCPU. [1] https://github.com/quic/gunyah-hypervisor Signed-off-by: Karim Manaouil <karim.manaouil@linaro.org> --- include/linux/kvm_host.h | 2 ++ virt/kvm/kvm_main.c | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-)