[PATCH 037/104] KVM: Use kmem cache for allocating vcpus

!MAILaRCHIVE_VOTE_RePLACE
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <kvm-devel@...>
Cc: <linux-kernel@...>, Rusty Russell <rusty@...>
Date: Monday, September 17, 2007 - 4:31 am

From: Rusty Russell <rusty@rustcorp.com.au>

Avi wants the allocations of vcpus centralized again.  The easiest way
is to add a "size" arg to kvm_init_arch, and expose the thus-prepared
cache to the modules.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Avi Kivity <avi@qumranet.com>
---
 drivers/kvm/kvm.h      |    4 +++-
 drivers/kvm/kvm_main.c |   16 +++++++++++++++-
 drivers/kvm/svm.c      |    5 +++--
 drivers/kvm/vmx.c      |    4 ++--
 4 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/kvm/kvm.h b/drivers/kvm/kvm.h
index 030b93b..b362e8f 100644
--- a/drivers/kvm/kvm.h
+++ b/drivers/kvm/kvm.h
@@ -141,6 +141,7 @@ struct kvm_mmu_page {
 };
 
 struct kvm_vcpu;
+extern struct kmem_cache *kvm_vcpu_cache;
 
 /*
  * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
@@ -483,7 +484,8 @@ extern struct kvm_arch_ops *kvm_arch_ops;
 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
 
-int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module);
+int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
+		  struct module *module);
 void kvm_exit_arch(void);
 
 int kvm_mmu_module_init(void);
diff --git a/drivers/kvm/kvm_main.c b/drivers/kvm/kvm_main.c
index 4bbd89e..4166a08 100644
--- a/drivers/kvm/kvm_main.c
+++ b/drivers/kvm/kvm_main.c
@@ -53,6 +53,8 @@ static LIST_HEAD(vm_list);
 static cpumask_t cpus_hardware_enabled;
 
 struct kvm_arch_ops *kvm_arch_ops;
+struct kmem_cache *kvm_vcpu_cache;
+EXPORT_SYMBOL_GPL(kvm_vcpu_cache);
 
 static __read_mostly struct preempt_ops kvm_preempt_ops;
 
@@ -3104,7 +3106,8 @@ static void kvm_sched_out(struct preempt_notifier *pn,
 	kvm_arch_ops->vcpu_put(vcpu);
 }
 
-int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
+int kvm_init_arch(struct kvm_arch_ops *ops, unsigned int vcpu_size,
+		  struct module *module)
 {
 	int r;
 
@@ -3142,6 +3145,14 @@ int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
 	if (r)
 		goto out_free_3;
 
+	/* A kmem cache lets us meet the alignment requirements of fx_save. */
+	kvm_vcpu_cache = kmem_cache_create("kvm_vcpu", vcpu_size,
+					   __alignof__(struct kvm_vcpu), 0, 0);
+	if (!kvm_vcpu_cache) {
+		r = -ENOMEM;
+		goto out_free_4;
+	}
+
 	kvm_chardev_ops.owner = module;
 
 	r = misc_register(&kvm_dev);
@@ -3156,6 +3167,8 @@ int kvm_init_arch(struct kvm_arch_ops *ops, struct module *module)
 	return r;
 
 out_free:
+	kmem_cache_destroy(kvm_vcpu_cache);
+out_free_4:
 	sysdev_unregister(&kvm_sysdev);
 out_free_3:
 	sysdev_class_unregister(&kvm_sysdev_class);
@@ -3173,6 +3186,7 @@ out:
 void kvm_exit_arch(void)
 {
 	misc_deregister(&kvm_dev);
+	kmem_cache_destroy(kvm_vcpu_cache);
 	sysdev_unregister(&kvm_sysdev);
 	sysdev_class_unregister(&kvm_sysdev_class);
 	unregister_reboot_notifier(&kvm_reboot_notifier);
diff --git a/drivers/kvm/svm.c b/drivers/kvm/svm.c
index b25f4e1..8193651 100644
--- a/drivers/kvm/svm.c
+++ b/drivers/kvm/svm.c
@@ -577,7 +577,7 @@ static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
 	struct page *page;
 	int err;
 
-	svm = kzalloc(sizeof *svm, GFP_KERNEL);
+	svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
 	if (!svm) {
 		err = -ENOMEM;
 		goto out;
@@ -1849,7 +1849,8 @@ static struct kvm_arch_ops svm_arch_ops = {
 
 static int __init svm_init(void)
 {
-	return kvm_init_arch(&svm_arch_ops, THIS_MODULE);
+	return kvm_init_arch(&svm_arch_ops, sizeof(struct vcpu_svm),
+			      THIS_MODULE);
 }
 
 static void __exit svm_exit(void)
diff --git a/drivers/kvm/vmx.c b/drivers/kvm/vmx.c
index f770f55..2b30274 100644
--- a/drivers/kvm/vmx.c
+++ b/drivers/kvm/vmx.c
@@ -2365,7 +2365,7 @@ static void vmx_free_vcpu(struct kvm_vcpu *vcpu)
 static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 {
 	int err;
-	struct vcpu_vmx *vmx = kzalloc(sizeof(*vmx), GFP_KERNEL);
+	struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
 	int cpu;
 
 	if (!vmx)
@@ -2490,7 +2490,7 @@ static int __init vmx_init(void)
 	memset(iova, 0xff, PAGE_SIZE);
 	kunmap(vmx_io_bitmap_b);
 
-	r = kvm_init_arch(&vmx_arch_ops, THIS_MODULE);
+	r = kvm_init_arch(&vmx_arch_ops, sizeof(struct vcpu_vmx), THIS_MODULE);
 	if (r)
 		goto out1;
 
-- 
1.5.3

-
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
git-send-email creates duplicate Message-Id's, Adrian Bunk, (Mon Sep 17, 11:59 am)
Re: git-send-email creates duplicate Message-Id's, Junio C Hamano, (Mon Sep 17, 4:22 pm)
Re: git-send-email creates duplicate Message-Id's, Matti Aarnio, (Mon Sep 17, 4:47 pm)
[PATCH 023/104] KVM: load_pdptrs() cleanups, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 029/104] KVM: Convert vm lock to a mutex, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 036/104] KVM: Remove kvm_{read,write}_guest(), Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 046/104] KVM: Remove stat_set from debugfs, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 031/104] KVM: VMX: pass vcpu_vmx internally, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 074/104] KVM: pending irq save/restore, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 085/104] KVM: Keep control regs in sync, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 093/104] KVM: x86 emulator: push imm8, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 094/104] KVM: x86 emulator: call near, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 095/104] KVM: x86 emulator: pushf, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 103/104] KVM: x86 emulator: popf, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 100/104] KVM: x86 emulator: lea, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 101/104] KVM: x86 emulator: jmp abs, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 087/104] KVM: Simplify memory allocation, Avi Kivity, (Mon Sep 17, 4:32 am)
[PATCH 066/104] KVM: Emulate local APIC in kernel, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 061/104] KVM: Support more memory slots, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 067/104] KVM: In-kernel I/O APIC model, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 068/104] KVM: Emulate hlt in the kernel, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 025/104] KVM: Dynamically allocate vcpus, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 053/104] KVM: Clean up kvm_setup_pio(), Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 051/104] KVM: Remove useless assignment, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 033/104] KVM: SVM: de-containization, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 037/104] KVM: Use kmem cache for allocating vcpus, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 040/104] KVM: VMX: Add cpu consistency check, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 042/104] KVM: Cleanup mark_page_dirty, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 032/104] KVM: Remove three magic numbers, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 027/104] KVM: add hypercall nr to kvm_run, Avi Kivity, (Mon Sep 17, 4:31 am)
[PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 4:30 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Christoph Hellwig, (Mon Sep 17, 5:13 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 5:15 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 5:18 am)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Nick Piggin, (Sun Sep 16, 5:29 pm)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Mon Sep 17, 2:19 pm)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Nick Piggin, (Mon Sep 17, 1:17 pm)
Re: [PATCH 001/104] KVM: Fix *nopage() in kvm_main.c, Avi Kivity, (Tue Sep 18, 6:44 am)