[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250424141341.841734-2-karim.manaouil@linaro.org>
Date: Thu, 24 Apr 2025 15:13:08 +0100
From: Karim Manaouil <karim.manaouil@...aro.org>
To: linux-kernel@...r.kernel.org,
kvm@...r.kernel.org,
linux-arm-msm@...r.kernel.org,
linux-arm-kernel@...ts.infradead.org,
kvmarm@...ts.linux.dev
Cc: Karim Manaouil <karim.manaouil@...aro.org>,
Alexander Graf <graf@...zon.com>,
Alex Elder <elder@...nel.org>,
Catalin Marinas <catalin.marinas@....com>,
Fuad Tabba <tabba@...gle.com>,
Joey Gouly <joey.gouly@....com>,
Jonathan Corbet <corbet@....net>,
Marc Zyngier <maz@...nel.org>,
Mark Brown <broonie@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Oliver Upton <oliver.upton@...ux.dev>,
Paolo Bonzini <pbonzini@...hat.com>,
Prakruthi Deepak Heragu <quic_pheragu@...cinc.com>,
Quentin Perret <qperret@...gle.com>,
Rob Herring <robh@...nel.org>,
Srinivas Kandagatla <srini@...nel.org>,
Srivatsa Vaddagiri <quic_svaddagi@...cinc.com>,
Will Deacon <will@...nel.org>,
Haripranesh S <haripran@....qualcomm.com>,
Carl van Schaik <cvanscha@....qualcomm.com>,
Murali Nalajala <mnalajal@...cinc.com>,
Sreenivasulu Chalamcharla <sreeniva@....qualcomm.com>,
Trilok Soni <tsoni@...cinc.com>,
Stefan Schmidt <stefan.schmidt@...aro.org>
Subject: [RFC PATCH 01/34] KVM: Allow arch-specific vCPU allocation and freeing
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@...aro.org>
---
include/linux/kvm_host.h | 2 ++
virt/kvm/kvm_main.c | 16 +++++++++++++---
2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index 1dedc421b3e3..3461346b37e0 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -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);
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 69782df3617f..dbb7ed95523f 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -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--;
--
2.39.5
Powered by blists - more mailing lists