Skip to contentSkip to conversation

Activate Virtualization On Demand v2

PATCH In review Alexander Graf 5 participants 12 messages Nov 5, 2008

Subsystems (7)
x86 KVM PowerPC s390 +3 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Activate Virtualization On Demand v2

X86 CPUs need to have some magic happening to enable the virtualization extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB entries (svm).

Currently KVM activates virtualization when the respective kernel module is loaded. This blocks us from autoloading KVM modules without breaking other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

So using this, KVM can be easily autoloaded, while keeping other hypervisors usable.

v2 adds returns to non-x86 hardware_enables and adds IA64 change

---
 arch/ia64/kvm/kvm-ia64.c   |    8 +++--
 arch/powerpc/kvm/powerpc.c |    3 +-
 arch/s390/kvm/kvm-s390.c   |    3 +-
 arch/x86/kvm/svm.c         |   13 +++++--
 arch/x86/kvm/vmx.c         |    7 ++++-
 arch/x86/kvm/x86.c         |    4 +-
 include/asm-x86/kvm_host.h |    2 +-
 include/linux/kvm_host.h   |    2 +-
 virt/kvm/kvm_main.c        |   72 +++++++++++++++++++++++++++++++------------
 9 files changed, 80 insertions(+), 34 deletions(-)
arch/ia64/kvm/kvm-ia64.c +5 -3
index c25c75b..2bd79db 100644
@@ -110,7 +110,7 @@ long ia64_pal_vp_create(u64 *vpd, u64 *host_iva, u64 *opt_handler)
 
 static  DEFINE_SPINLOCK(vp_lock);
 
-void kvm_arch_hardware_enable(void *garbage)
+int kvm_arch_hardware_enable(void *garbage)
 {
 	long  status;
 	long  tmp_base;
@@ -124,7 +124,7 @@ void kvm_arch_hardware_enable(void *garbage)
 	slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
 	local_irq_restore(saved_psr);
 	if (slot < 0)
-		return;
+		return -ENOMEM;
 
 	spin_lock(&vp_lock);
 	status = ia64_pal_vp_init_env(kvm_vsa_base ?
@@ -132,7 +132,7 @@ void kvm_arch_hardware_enable(void *garbage)
 			__pa(kvm_vm_buffer), KVM_VM_BUFFER_BASE, &tmp_base);
 	if (status != 0) {
 		printk(KERN_WARNING"kvm: Failed to Enable VT Support!!!!\n");
-		return ;
+		return -EINVAL;
 	}
 
 	if (!kvm_vsa_base) {
@@ -141,6 +141,8 @@ void kvm_arch_hardware_enable(void *garbage)
 	}
 	spin_unlock(&vp_lock);
 	ia64_ptr_entry(0x3, slot);
+
+	return 0;
 }
 
 void kvm_arch_hardware_disable(void *garbage)
arch/powerpc/kvm/powerpc.c +2 -1
index 90a6fc4..dce664b 100644
@@ -79,8 +79,9 @@ int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu)
 	return r;
 }
 
-void kvm_arch_hardware_enable(void *garbage)
+int kvm_arch_hardware_enable(void *garbage)
 {
+	return 0;
 }
 
 void kvm_arch_hardware_disable(void *garbage)
arch/s390/kvm/kvm-s390.c +2 -1
index 8b00eb2..91e08e5 100644
@@ -70,9 +70,10 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {
 
 
 /* Section: not file related */
-void kvm_arch_hardware_enable(void *garbage)
+int kvm_arch_hardware_enable(void *garbage)
 {
 	/* every s390 is virtualization enabled ;-) */
+	return 0;
 }
 
 void kvm_arch_hardware_disable(void *garbage)
arch/x86/kvm/svm.c +9 -4
index 05efc4e..3a8a820 100644
@@ -275,7 +275,7 @@ static void svm_hardware_disable(void *garbage)
 	wrmsrl(MSR_EFER, efer & ~MSR_EFER_SVME_MASK);
 }
 
-static void svm_hardware_enable(void *garbage)
+static int svm_hardware_enable(void *garbage)
 {
 
 	struct svm_cpu_data *svm_data;
@@ -284,16 +284,20 @@ static void svm_hardware_enable(void *garbage)
 	struct desc_struct *gdt;
 	int me = raw_smp_processor_id();
 
+	rdmsrl(MSR_EFER, efer);
+	if (efer & MSR_EFER_SVME_MASK)
+		return -EBUSY;
+
 	if (!has_svm()) {
 		printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me);
-		return;
+		return -EINVAL;
 	}
 	svm_data = per_cpu(svm_data, me);
 
 	if (!svm_data) {
 		printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n",
 		       me);
-		return;
+		return -EINVAL;
 	}
 
 	svm_data->asid_generation = 1;
@@ -304,11 +308,12 @@ static void svm_hardware_enable(void *garbage)
 	gdt = (struct desc_struct *)gdt_descr.address;
 	svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
 
-	rdmsrl(MSR_EFER, efer);
 	wrmsrl(MSR_EFER, efer | MSR_EFER_SVME_MASK);
 
 	wrmsrl(MSR_VM_HSAVE_PA,
 	       page_to_pfn(svm_data->save_area) << PAGE_SHIFT);
+
+	return 0;
 }
 
 static void svm_cpu_uninit(int cpu)
arch/x86/kvm/vmx.c +6 -1
index 64e2439..9a4f47c 100644
@@ -1059,12 +1059,15 @@ static __init int vmx_disabled_by_bios(void)
 	/* locked but not enabled */
 }
 
-static void hardware_enable(void *garbage)
+static int hardware_enable(void *garbage)
 {
 	int cpu = raw_smp_processor_id();
 	u64 phys_addr = __pa(per_cpu(vmxarea, cpu));
 	u64 old;
 
+	if (read_cr4() & X86_CR4_VMXE)
+		return -EBUSY;
+
 	INIT_LIST_HEAD(&per_cpu(vcpus_on_cpu, cpu));
 	rdmsrl(MSR_IA32_FEATURE_CONTROL, old);
 	if ((old & (FEATURE_CONTROL_LOCKED |
@@ -1079,6 +1082,8 @@ static void hardware_enable(void *garbage)
 	asm volatile (ASM_VMX_VMXON_RAX
 		      : : "a"(&phys_addr), "m"(phys_addr)
 		      : "memory", "cc");
+
+	return 0;
 }
 
 static void vmclear_local_vcpus(void)
arch/x86/kvm/x86.c +2 -2
index ceeac88..525cb67 100644
@@ -4030,9 +4030,9 @@ int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)
 	return kvm_x86_ops->vcpu_reset(vcpu);
 }
 
-void kvm_arch_hardware_enable(void *garbage)
+int kvm_arch_hardware_enable(void *garbage)
 {
-	kvm_x86_ops->hardware_enable(garbage);
+	return kvm_x86_ops->hardware_enable(garbage);
 }
 
 void kvm_arch_hardware_disable(void *garbage)
include/asm-x86/kvm_host.h +1 -1
index eebba6a..8d59d9f 100644
@@ -424,7 +424,7 @@ struct descriptor_table {
 struct kvm_x86_ops {
 	int (*cpu_has_kvm_support)(void);          /* __init */
 	int (*disabled_by_bios)(void);             /* __init */
-	void (*hardware_enable)(void *dummy);      /* __init */
+	int (*hardware_enable)(void *dummy);       /* __init */
 	void (*hardware_disable)(void *dummy);
 	void (*check_processor_compatibility)(void *rtn);
 	int (*hardware_setup)(void);               /* __init */
include/linux/kvm_host.h +1 -1
index 3a0fb77..d141741 100644
@@ -272,7 +272,7 @@ int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
 
 int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu);
-void kvm_arch_hardware_enable(void *garbage);
+int kvm_arch_hardware_enable(void *garbage);
 void kvm_arch_hardware_disable(void *garbage);
 int kvm_arch_hardware_setup(void);
 void kvm_arch_hardware_unsetup(void);
virt/kvm/kvm_main.c +52 -20

184 lines not on this page · raw message

Signature · 1 line
1.6.0.2
#1 lore raw
Quoted 30 lines · Alexander Graf

X86 CPUs need to have some magic happening to enable the virtualization extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB entries (svm).

Currently KVM activates virtualization when the respective kernel module is loaded. This blocks us from autoloading KVM modules without breaking other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

So using this, KVM can be easily autoloaded, while keeping other hypervisors usable.

v2 adds returns to non-x86 hardware_enables and adds IA64 change

virt/kvm/kvm_main.c +1 -0
@@ -563,19 +566,27 @@ static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
 
 static struct kvm *kvm_create_vm(void)
 {
+	int r;
 	struct kvm *kvm = kvm_arch_create_vm();
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
 	struct page *page;
 #endif
 
 	if (IS_ERR(kvm))

virt/kvm/kvm_main.c static const struct mmu_notifier_ops kvm_mmu_notifier_ops = { · line 584

Quoted 10 lines
virt/kvm/kvm_main.c +8 -1
-		goto out;
+		return kvm;
+
+	if (atomic_add_return(1, &kvm_usage_count) == 1) {
+		on_each_cpu(hardware_enable, &r, 1);
+
+		if (r)
+			goto out_err;
+	}
 

This can race -- if we're preempted immediately after atomic_add_return(), a second vm creation will see the count elevated and can start executing without virtualization enabled.

virt/kvm/kvm_main.c static struct kvm *kvm_create_vm(void) · line 630

Quoted 5 lines
virt/kvm/kvm_main.c +4 -0
+
+out_err:
+	if (atomic_dec_and_test(&kvm_usage_count))
+		on_each_cpu(hardware_disable, NULL, 1);
 

Similar race.

virt/kvm/kvm_main.c static void kvm_destroy_vm(struct kvm *kvm) · line 681

Quoted 9 lines
virt/kvm/kvm_main.c +2 -0
@@ -660,6 +674,8 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
 #endif
 	kvm_arch_destroy_vm(kvm);
+	if (atomic_dec_and_test(&kvm_usage_count))
+		on_each_cpu(hardware_disable, NULL, 1);
 	mmdrop(mm);
 }
 

And again. I suggest returning to spinlocks (and placing the duplicated disable code in a function).

Quoted 12 lines
virt/kvm/kvm_main.c +4 -1
-static void hardware_enable(void *junk)
+static void hardware_enable(void *_r)
 {
 	int cpu = raw_smp_processor_id();
+	int r;
 
 	if (cpu_isset(cpu, cpus_hardware_enabled))
 		return;
+	r = kvm_arch_hardware_enable(NULL);
+	if (_r)

virt/kvm/kvm_main.c static struct miscdevice kvm_dev = { · line 1803

Quoted 10 lines
virt/kvm/kvm_main.c +7 -1
+		*((int*)_r) = r;
+	if (r) {
+		printk(KERN_INFO "kvm: enabling virtualization on "
+				 "CPU%d failed\n", cpu);
+		return;
+	}
+
 	cpu_set(cpu, cpus_hardware_enabled);
-	kvm_arch_hardware_enable(NULL);
 }

We'll be in a nice fix if we can only enable virtualization on some processors; that's the reason hardware_enable() was originally specified as returning void.

I don't see an easy way out, but it's hardly a likely event.

Quoted 7 lines
virt/kvm/kvm_main.c +2 -1
 	case CPU_UP_CANCELED:
 		printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
 		       cpu);
-		smp_call_function_single(cpu, hardware_disable, NULL, 1);
+		if (atomic_read(&kvm_usage_count))
+			smp_call_function_single(cpu, hardware_disable,

virt/kvm/kvm_main.c static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, · line 1841

Quoted 10 lines
virt/kvm/kvm_main.c +4 -1
+					NULL, 1);
 		break;
 	case CPU_ONLINE:
 		printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
 		       cpu);
-		smp_call_function_single(cpu, hardware_enable, NULL, 1);
+		if (atomic_read(&kvm_usage_count))
+			smp_call_function_single(cpu, hardware_enable,
+						 NULL, 1);
 		break;

Are these called in a point where processes can't run? Otherwise there's a race here.

virt/kvm/kvm_main.c static void kvm_exit_debug(void) · line 1995

Quoted 8 lines

static int kvm_resume(struct sys_device *dev)
{

virt/kvm/kvm_main.c +2 -1
-	hardware_enable(NULL);
+	if (atomic_read(&kvm_usage_count))
+		hardware_enable(NULL);
 	return 0;
 }

Move the test to hardware_enable()? It's repeated too often.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 19 lines · Avi Kivity
Quoted 18 lines · Alexander Graf

X86 CPUs need to have some magic happening to enable the virtualization extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB entries (svm).

Currently KVM activates virtualization when the respective kernel module is loaded. This blocks us from autoloading KVM modules without breaking other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

So using this, KVM can be easily autoloaded, while keeping other hypervisors usable.

v2 adds returns to non-x86 hardware_enables and adds IA64 change

[snip]

Quoted 13 lines

virt/kvm/kvm_main.c static void kvm_destroy_vm(struct kvm *kvm) · line 681

Quoted 10 lines
virt/kvm/kvm_main.c +2 -0
@@ -660,6 +674,8 @@ static void kvm_destroy_vm(struct kvm *kvm)
     mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
 #endif
     kvm_arch_destroy_vm(kvm);
+    if (atomic_dec_and_test(&kvm_usage_count))
+        on_each_cpu(hardware_disable, NULL, 1);
     mmdrop(mm);
 }
 

And again. I suggest returning to spinlocks (and placing the duplicated disable code in a function).

OK.

Quoted 28 lines
Quoted 13 lines
virt/kvm/kvm_main.c +4 -1
-static void hardware_enable(void *junk)
+static void hardware_enable(void *_r)
 {
     int cpu = raw_smp_processor_id();
+    int r;
 
     if (cpu_isset(cpu, cpus_hardware_enabled))
         return;
+    r = kvm_arch_hardware_enable(NULL);
+    if (_r)

virt/kvm/kvm_main.c static struct miscdevice kvm_dev = { · line 1803

Quoted 10 lines
virt/kvm/kvm_main.c +7 -1
+        *((int*)_r) = r;
+    if (r) {
+        printk(KERN_INFO "kvm: enabling virtualization on "
+                 "CPU%d failed\n", cpu);
+        return;
+    }
+
     cpu_set(cpu, cpus_hardware_enabled);
-    kvm_arch_hardware_enable(NULL);
 }

We'll be in a nice fix if we can only enable virtualization on some processors; that's the reason hardware_enable() was originally specified as returning void.

Quoted 1 line

I don't see an easy way out, but it's hardly a likely event.

I don't think there's any way we can circumvent that.
What I've wanted to ask for some time already: How does suspend/resume work? I only see one suspend/resume hook that disables virt on the currently running CPU. Why don't we have to loop through the CPUs to enable/disable all of them?
At least for suspend-to-disk this sounds pretty necessary.

Quoted 16 lines
Quoted 6 lines

printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
cpu);

virt/kvm/kvm_main.c +2 -1
-        smp_call_function_single(cpu, hardware_disable, NULL, 1);
+        if (atomic_read(&kvm_usage_count))
+            smp_call_function_single(cpu, hardware_disable,

virt/kvm/kvm_main.c static int kvm_cpu_hotplug(struct notifier_block *notifier, unsigned long val, · line 1841

Quoted 10 lines
virt/kvm/kvm_main.c +4 -1
+                    NULL, 1);
         break;
     case CPU_ONLINE:
         printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
                cpu);
-        smp_call_function_single(cpu, hardware_enable, NULL, 1);
+        if (atomic_read(&kvm_usage_count))
+            smp_call_function_single(cpu, hardware_enable,
+                         NULL, 1);
         break;
Quoted 4 lines

case CPU_UP_CANCELED:

Are these called in a point where processes can't run? Otherwise there's a race here.

Yes.

static struct notifier_block kvm_cpu_notifier = {
.notifier_call = kvm_cpu_hotplug, .priority = 20, /* must be > scheduler priority */
};

Quoted 11 lines

virt/kvm/kvm_main.c static void kvm_exit_debug(void) · line 1995

Quoted 9 lines

static int kvm_resume(struct sys_device *dev)
{

virt/kvm/kvm_main.c +2 -1
-    hardware_enable(NULL);
+    if (atomic_read(&kvm_usage_count))
+        hardware_enable(NULL);
     return 0;
 }

Move the test to hardware_enable()? It's repeated too often.

What do we do about the on_each_cpu(hardware_enable) cases? We couldn't tell when to activate/deactive virtualization then, as that's semantically bound to "amount of VMs".

Alex

#3 lore raw
Quoted 9 lines · Alexander Graf
Quoted 6 lines

We'll be in a nice fix if we can only enable virtualization on some processors; that's the reason hardware_enable() was originally specified as returning void.

I don't see an easy way out, but it's hardly a likely event.

I don't think there's any way we can circumvent that.

No. We can live with it though.

Quoted 2 lines

What I've wanted to ask for some time already: How does suspend/resume work?

The question is important, even without the first word.

Quoted 6 lines

I only see one suspend/resume hook that disables virt on the currently running CPU. Why don't we have to loop through the CPUs to enable/disable all of them?
At least for suspend-to-disk this sounds pretty necessary.

Suspend first offlines all other cpus.

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

static int kvm_resume(struct sys_device *dev)
{

virt/kvm/kvm_main.c +2 -1
-    hardware_enable(NULL);
+    if (atomic_read(&kvm_usage_count))
+        hardware_enable(NULL);
     return 0;
 }
Quoted 7 lines
Quoted 2 lines

Move the test to hardware_enable()? It's repeated too often.

What do we do about the on_each_cpu(hardware_enable) cases? We couldn't tell when to activate/deactive virtualization then, as that's semantically bound to "amount of VMs".

I don't understand. Moving the test to within the IPI shouldn't affect anything.

Signature · 1 line
error compiling committee.c: too many arguments to function
#6 lore raw
In reply to Alexander Graf
Quoted 52 lines · Alexander Graf

X86 CPUs need to have some magic happening to enable the virtualization
extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB
entries (svm).

Currently KVM activates virtualization when the respective kernel module
is loaded. This blocks us from autoloading KVM modules without breaking
other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

So using this, KVM can be easily autoloaded, while keeping other hypervisors usable.

v2 adds returns to non-x86 hardware_enables and adds IA64 change

---
 arch/ia64/kvm/kvm-ia64.c   |    8 +++--
 arch/powerpc/kvm/powerpc.c |    3 +-
 arch/s390/kvm/kvm-s390.c   |    3 +-
 arch/x86/kvm/svm.c         |   13 +++++--
 arch/x86/kvm/vmx.c         |    7 ++++-
 arch/x86/kvm/x86.c         |    4 +-
 include/asm-x86/kvm_host.h |    2 +-
 include/linux/kvm_host.h   |    2 +-
 virt/kvm/kvm_main.c        |   72

+++++++++++++++++++++++++++++++------------ 9 files changed, 80
insertions(+), 34 deletions(-)

arch/ia64/kvm/kvm-ia64.c +1 -1
index c25c75b..2bd79db 100644
@@ -110,7 +110,7 @@ long ia64_pal_vp_create(u64 *vpd, u64 *host_iva,
 u64 *opt_handler) 
 
 static  DEFINE_SPINLOCK(vp_lock);
 
-void kvm_arch_hardware_enable(void *garbage)
+int kvm_arch_hardware_enable(void *garbage)
 {
 	long  status;
arch/ia64/kvm/kvm-ia64.c +0 -0
 	long  tmp_base;

arch/ia64/kvm/kvm-ia64.c void kvm_arch_hardware_enable(void *garbage) · line 127

Quoted 5 lines
arch/ia64/kvm/kvm-ia64.c +1 -1
@@ -124,7 +124,7 @@ void kvm_arch_hardware_enable(void *garbage)
 	slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
 	local_irq_restore(saved_psr);
 	if (slot < 0)
-		return;
+		return -ENOMEM;

Return -EINVAL maybe more accurate. Here slot < 0 means invalid or uncorrect parameters are passed to ia64_itr_entry. Xiantao

#4 lore raw
In reply to Avi Kivity
Quoted 21 lines · Avi Kivity
Quoted 9 lines · Alexander Graf
Quoted 6 lines

We'll be in a nice fix if we can only enable virtualization on some processors; that's the reason hardware_enable() was originally specified as returning void.

I don't see an easy way out, but it's hardly a likely event.

I don't think there's any way we can circumvent that.

No. We can live with it though.

Quoted 2 lines

What I've wanted to ask for some time already: How does suspend/resume work?

The question is important, even without the first word.

Quoted 8 lines
Quoted 6 lines

I only see one suspend/resume hook that disables virt on the currently running CPU. Why don't we have to loop through the CPUs to enable/disable all of them?
At least for suspend-to-disk this sounds pretty necessary.

Suspend first offlines all other cpus.

Ah, ok.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

virt/kvm/kvm_main.c static void kvm_exit_debug(void) · line 1995

Quoted 7 lines

{

virt/kvm/kvm_main.c +2 -1
-    hardware_enable(NULL);
+    if (atomic_read(&kvm_usage_count))
+        hardware_enable(NULL);
     return 0;
 }

Move the test to hardware_enable()? It's repeated too often.

Quoted 8 lines
Quoted 4 lines

What do we do about the on_each_cpu(hardware_enable) cases? We couldn't tell when to activate/deactive virtualization then, as that's semantically bound to "amount of VMs".

static int kvm_resume(struct sys_device *dev)

I don't understand. Moving the test to within the IPI shouldn't affect anything.

Oh, you only want the test to be in hardware_enable and hardware_disable. Now I see what you mean: modify and lock kvm_usage_count outside, but test inside of hardware_enable.

#7 lore raw
In reply to Zhang, Xiantao
Quoted 48 lines · Xiantao
Quoted 46 lines · Alexander Graf

X86 CPUs need to have some magic happening to enable the virtualization
extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB
entries (svm).

Currently KVM activates virtualization when the respective kernel module
is loaded. This blocks us from autoloading KVM modules without breaking
other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

So using this, KVM can be easily autoloaded, while keeping other hypervisors usable.

v2 adds returns to non-x86 hardware_enables and adds IA64 change

---
 arch/ia64/kvm/kvm-ia64.c   |    8 +++--
 arch/powerpc/kvm/powerpc.c |    3 +-
 arch/s390/kvm/kvm-s390.c   |    3 +-
 arch/x86/kvm/svm.c         |   13 +++++--
 arch/x86/kvm/vmx.c         |    7 ++++-
 arch/x86/kvm/x86.c         |    4 +-
 include/asm-x86/kvm_host.h |    2 +-
 include/linux/kvm_host.h   |    2 +-
 virt/kvm/kvm_main.c        |   72

+++++++++++++++++++++++++++++++------------ 9 files changed, 80
insertions(+), 34 deletions(-)

arch/ia64/kvm/kvm-ia64.c +0 -0
index c25c75b..2bd79db 100644
@@ -110,7 +110,7 @@ long ia64_pal_vp_create(u64 *vpd, u64 *host_iva,
 u64 *opt_handler) 
 
 static  DEFINE_SPINLOCK(vp_lock);
 
Quoted 16 lines

arch/ia64/kvm/kvm-ia64.c void kvm_arch_hardware_enable(void *garbage) · line 128

Quoted 12 lines
arch/ia64/kvm/kvm-ia64.c +1 -1
-void kvm_arch_hardware_enable(void *garbage)
+int kvm_arch_hardware_enable(void *garbage)
 {
 	long  status;
arch/ia64/kvm/kvm-ia64.c +1 -1
 	long  tmp_base;
@@ -124,7 +124,7 @@ void kvm_arch_hardware_enable(void *garbage)
 	slot = ia64_itr_entry(0x3, KVM_VMM_BASE, pte, KVM_VMM_SHIFT);
 	local_irq_restore(saved_psr);
 	if (slot < 0)
-		return;
+		return -ENOMEM;
 

Return -EINVAL maybe more accurate. Here slot < 0 means invalid or uncorrect parameters are passed to ia64_itr_entry. Xiantao

Okies.

#8 lore raw
In reply to Alexander Graf
Quoted 5 lines · Alexander Graf

X86 CPUs need to have some magic happening to enable the virtualization extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB entries (svm).

Quoted 8 lines

Currently KVM activates virtualization when the respective kernel module is loaded. This blocks us from autoloading KVM modules without breaking other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

Pardon my unfamiliarity with the code, but with this change applied, will we still get an explicit error at time of loading kvm-intel.ko if VMX were disabled in the BIOS ? Or will that error reporting be pushed off to time when VMX is first activated ?

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#9 lore raw
Quoted 17 lines · Daniel P. Berrange
Quoted 5 lines · Alexander Graf

X86 CPUs need to have some magic happening to enable the virtualization extensions on them. This magic can result in unpleasant results for users, like blocking other VMMs from working (vmx) or using invalid TLB entries (svm).

Quoted 9 lines

Currently KVM activates virtualization when the respective kernel module is loaded. This blocks us from autoloading KVM modules without breaking other VMMs.

To circumvent this problem at least a bit, this patch introduces on demand activation of virtualization. This means, that instead virtualization is enabled on creation of the first virtual machine and disabled on removal of the last one.

Quoted 5 lines

Pardon my unfamiliarity with the code, but with this change applied, will we still get an explicit error at time of loading kvm-intel.ko if VMX were disabled in the BIOS ? Or will that error reporting be pushed off to time when VMX is first activated ?

The BIOS and hardware support checks are in kvm_arch_init, which is still called at load-time of the module.

Alex

#5 lore raw
In reply to Avi Kivity
Quoted 1 line · Avi Kivity

Alexander Graf wrote:

[snip]

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

virt/kvm/kvm_main.c static void kvm_exit_debug(void) · line 1995

Quoted 8 lines

static int kvm_resume(struct sys_device *dev)
{

virt/kvm/kvm_main.c +2 -1
-    hardware_enable(NULL);
+    if (atomic_read(&kvm_usage_count))
+        hardware_enable(NULL);
     return 0;
 }

Move the test to hardware_enable()? It's repeated too often.

Quoted 8 lines
Quoted 4 lines

What do we do about the on_each_cpu(hardware_enable) cases? We couldn't tell when to activate/deactive virtualization then, as that's semantically bound to "amount of VMs".

I don't understand. Moving the test to within the IPI shouldn't affect anything.

Actually it's there already. Since we have the cpu_isset if here, we won't get disabling when it's disabled or enabling when it's enabled on a per-cpu basis. That code would've just saved us the IPIs :-).

So I'll add hardware_{en,dis}able_all functions that do the locking and increase / decrease the counter. Disable is used twice, while enable only once. But for the sake of code readability, I think it might be a good idea to have both of them be functions.

Also the locking isn't really needed in most cases (CPU hotplug, suspend, resume, reboot, exit), since we don't schedule there.

Alex

#10 lore raw
In reply to Alexander Graf

Am Mittwoch, 5. November 2008 schrieb Alexander Graf:

Quoted 2 lines
(quoted diff) +0 -0
 		printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
 		       cpu);

[...]

Quoted 2 lines
(quoted diff) +0 -0
 		printk(KERN_INFO "kvm: disabling virtualization on CPU%d\n",
 		       cpu);

[...]

Quoted 2 lines
virt/kvm/kvm_main.c +0 -0
 		printk(KERN_INFO "kvm: enabling virtualization on CPU%d\n",
 		       cpu);

[...]

Quoted 1 line

printk(KERN_INFO "kvm: exiting hardware virtualization\n");

When you are at it, could move these printk to the arches that atually enable/disable virtualization?

For example you could do something like

if (callback) {
	printk "...";
	callback();
}

And then you could remove kvm_arch_hardware_enable/disable from s390 and powerpc.

Havng these messages on s390 and powerpc makes absolutely no sense.

#11 lore raw
Quoted 11 lines · Christian Borntraeger

When you are at it, could move these printk to the arches that atually enable/disable virtualization?

For example you could do something like if (callback) {

	printk "...";
	callback();
}

And then you could remove kvm_arch_hardware_enable/disable from s390 and powerpc.

Havng these messages on s390 and powerpc makes absolutely no sense.

In a separate patch, this is big enough already.

We could do this using kconfig

config KVM_NEEDS_HARDWARE_DETECT
bool
default n

and override it for x86/ia64. This would remove the need to implement dummy callbacks in s390/ppc.

Signature · 1 line
error compiling committee.c: too many arguments to function