Activate Virtualization On Demand v3
PATCH 1/1 Activate Virtualization On Demand v3
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 v3 changes:
- use spin_lock instead of atomics
- put locking to new functions hardware_{en,dis}able_all that get called on VM creation/destruction
- remove usage counter checks where not necessary
- return -EINVAL for IA64 slot < 0 case
--- 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 | 75 ++++++++++++++++++++++++++++++++++++------- 9 files changed, 90 insertions(+), 27 deletions(-)
arch/ia64/kvm/kvm-ia64.c +5 -3
@@ -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 -EINVAL ;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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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 +62 -13
156 lines not on this page · raw message
Signature · 1 line
1.6.0.2
Quoted 17 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.
Quoted 9 lines
v2 adds returns to non-x86
hardware_enablesand adds IA64 change v3 changes:
- usespin_lockinstead of atomics
- put locking to new functions hardware_{en,dis}able_all that get called on VM creation/destruction
- remove usage counter checks where not necessary
- return-EINVALfor IA64 slot < 0 case
Is this v3 with all the latest changes? it precedes some messages where you say you'll change things by about 40 minutes.
In any case, I'll defer applying until Eduardo's kdump/reboot changes go in, since they touch the same places, and Eduardo's changes are much harder to test.
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 29 lines · Avi Kivity
Quoted 17 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.
Quoted 10 lines
v2 adds returns to non-x86
hardware_enablesand adds IA64 change v3 changes:
- usespin_lockinstead of atomics
- put locking to new functions hardware_{en,dis}able_all that get
called
on VM creation/destruction
- remove usage counter checks where not necessary
- return-EINVALfor IA64 slot < 0 case
Quoted 2 lines
Is this v3 with all the latest changes? it precedes some messages where you say you'll change things by about 40 minutes.
Yeah, my clock was off. Somehow I get host time drifts sometimes when using KVM and my NTP client wasn't running. But maybe it's just me doing something wrong.
Quoted 3 lines
In any case, I'll defer applying until Eduardo's kdump/reboot changes go in, since they touch the same places, and Eduardo's changes are much harder to test.
I agree.
Apart from that, do I get an ACK for it, so I can at least put it into our package and rest assured nothing obvious is wrong :-)?
Alex
Quoted 9 lines · Alexander Graf
Quoted 4 lines
In any case, I'll defer applying until Eduardo's kdump/reboot changes go in, since they touch the same places, and Eduardo's changes are much harder to test.
I agree.
Apart from that, do I get an ACK for it, so I can at least put it into our package and rest assured nothing obvious is wrong :-)?
It seems fine.
(the part where you call on_each_cpu and have all functions write their reply into the same r is icky, but should work AFAICS)
Signature · 1 line
error compiling committee.c: too many arguments to function
Quoted 27 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_enablesand adds IA64 change v3 changes:
- usespin_lockinstead of atomics
- put locking to new functions hardware_{en,dis}able_all that get called on VM creation/destruction
- remove usage counter checks where not necessary
- return-EINVALfor IA64 slot < 0 caseSigned-off-by Alexander Graf <[email protected]>---
<snip>
Quoted 15 lines
virt/kvm/kvm_main.c +8
-1-static void hardware_enable (void *junk) +static void hardware_enable (void *_r) {int cpu =raw_smp_processor_id (); +int r; + +/* If enabling a previous CPU failed already, let's not continue */ +if (_r && *((int *)_r)) +return ;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 1804
Quoted 10 lines
virt/kvm/kvm_main.c +8
-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 ); +}
Doesn't on_each_cpu() run the function in parallel on all CPUs? If so, there is a race between checking *_r and setting *_r.
Quoted 14 lines
virt/kvm/kvm_main.c +12
-0+ +static int hardware_enable_all (void ) +{ +int r =0 ; + +spin_lock (&kvm_usage_lock); + kvm_usage_count++; +if (kvm_usage_count ==1 ) +on_each_cpu (hardware_enable, &r,1 ); +spin_unlock (&kvm_usage_lock); + +return r; }
<snip>
Signature · 1 line
Eduardo
Quoted 50 lines · Eduardo Habkost
Quoted 28 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_enablesand adds IA64 change v3 changes:
- usespin_lockinstead of atomics
- put locking to new functions hardware_{en,dis}able_all that get
called
on VM creation/destruction
- remove usage counter checks where not necessary
- return-EINVALfor IA64 slot < 0 caseSigned-off-by Alexander Graf <[email protected]>---
<snip>
Quoted 20 lines
virt/kvm/kvm_main.c +1
-1-static void hardware_enable (void *junk) +static void hardware_enable (void *_r){ int cpu = raw_smp_processor_id();virt/kvm/kvm_main.c +5
-0+int r; + +/* If enabling a previous CPU failed already, let's not continue */ +if (_r && *((int *)_r)) +return ;if (cpu_isset(cpu, cpus_hardware_enabled)) return;virt/kvm/kvm_main.c +7
-0+ r =kvm_arch_hardware_enable (NULL ); +if (_r) + *((int *)_r) = r; +if (r) { +printk (KERN_INFO "kvm: enabling virtualization on " +"CPU%d failed\n" , cpu); +return ;
Quoted 8 lines
virt/kvm/kvm_main.c static struct miscdevice kvm_dev = { · line 1804
Quoted 5 lines
(quoted diff) +2
-0+ } +cpu_set(cpu, cpus_hardware_enabled);virt/kvm/kvm_main.c +1
-1-kvm_arch_hardware_enable (NULL ); +}Doesn't
on_each_cpu()run the function in parallel on all CPUs? If so, there is a race between checking *_r and setting *_r.
Good question - it doesn't really hurt to write the value though, if we only write it on error. So I guess we could just remove the first check and check on if( r && _r) later on.
Alex
Quoted 66 lines · Alexander Graf
Quoted 54 lines · Eduardo Habkost
Quoted 32 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_enablesand adds IA64 change v3 changes:
- usespin_lockinstead of atomics
- put locking to new functions hardware_{en,dis}able_all that get
called
on VM creation/destruction
- remove usage counter checks where not necessary
- return-EINVALfor IA64 slot < 0 caseSigned-off-by Alexander Graf <[email protected]>---
<snip>
Quoted 20 lines
virt/kvm/kvm_main.c +1
-1-static void hardware_enable (void *junk) +static void hardware_enable (void *_r){ int cpu = raw_smp_processor_id();virt/kvm/kvm_main.c +5
-0+int r; + +/* If enabling a previous CPU failed already, let's not continue */ +if (_r && *((int *)_r)) +return ;if (cpu_isset(cpu, cpus_hardware_enabled)) return;virt/kvm/kvm_main.c +7
-0+ r =kvm_arch_hardware_enable (NULL ); +if (_r) + *((int *)_r) = r; +if (r) { +printk (KERN_INFO "kvm: enabling virtualization on " +"CPU%d failed\n" , cpu); +return ;Quoted 8 lines
virt/kvm/kvm_main.c static struct miscdevice kvm_dev = { · line 1804
Quoted 5 lines
(quoted diff) +2
-0+ } +cpu_set(cpu, cpus_hardware_enabled);virt/kvm/kvm_main.c +1
-1-kvm_arch_hardware_enable (NULL ); +}Doesn't
on_each_cpu()run the function in parallel on all CPUs? If so, there is a race between checking *_r and setting *_r.
Quoted 4 lines
Good question - it doesn't really hurt to write the value though, if we only write it on error.
So I guess we could just remove the first check and check onif( r && _r)later on.
I think the first check doesn't hurt, and using if(_r && r) on the second check should work. I am not sure if there are no pitfalls here due to memory ordering or delayed write on some arches, however.
Signature · 1 line
Eduardo