kvm: make vcpu life cycle separated from kvm instance
PATCH v8 1/1 kvm: make vcpu life cycle separated from kvm instance
From: Liu Ping Fan <[email protected]>
Currently, vcpu will be destructed only after kvm instance is destroyed. This result to vcpu keep idle in kernel, but can not be freed when it is unplugged in guest.
Change this to vcpu's destruction before kvm instance, so vcpu CAN be destroyed before kvm instance. By this way, we can remove vcpu when guest does not need it any longer.
Changelog v7->v8
-Instead of using srcu linked list, resort to kvm->vcpus_array to store
vcpu pointers. The vcpus_array is split into two areas, one for cur, the other for rcu updating.
TBD
-Choose better name for kvm_vcpu_destruct. Maybe we can rename
kvm_arch_vcpu_destory() for the fail path in kvm_vm_ioctl_create_vcpu() to kvm_arch_vcpu_abort(). Any suggestion?
--- arch/x86/kvm/i8254.c | 4 +- arch/x86/kvm/i8259.c | 14 ++++++---- arch/x86/kvm/x86.c | 39 ++++++++++-------------------- include/linux/kvm_host.h | 15 +++++++---- virt/kvm/irq_comm.c | 4 +- virt/kvm/kvm_main.c | 58 +++++++++++++++++++++++++++++++++++++-------- 6 files changed, 81 insertions(+), 53 deletions(-)
arch/x86/kvm/i8254.c +2 -2
@@ -288,7 +288,7 @@ static void pit_do_work(struct work_struct *work) { struct kvm_pit *pit = container_of(work, struct kvm_pit, expired);struct kvm *kvm = pit->kvm; -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu, *vcpus;int i;struct kvm_kpit_state *ps = &pit->pit_state;int inject =0 ;
@@ -316,7 +316,7 @@ static void pit_do_work(struct work_struct *work)* VCPU0, and only if its LVT0 is in EXTINT mode. */ if (kvm->arch.vapics_in_nmi_mode >0 ) -kvm_for_each_vcpu (i, vcpu, kvm) +kvm_for_each_vcpu (i, vcpu, kvm, vcpus)kvm_apic_nmi_wd_deliver (vcpu); } }
arch/x86/kvm/i8259.c +8 -6
@@ -49,7 +49,7 @@ static void pic_unlock(struct kvm_pic *s) __releases(&s->lock) {bool wakeup = s->wakeup_needed; -struct kvm_vcpu *vcpu, *found =NULL ; +struct kvm_vcpu *vcpu, *vcpus, *found =NULL ;int i; s->wakeup_needed = false;
@@ -57,13 +57,14 @@ static void pic_unlock(struct kvm_pic *s)spin_unlock (&s->lock);if (wakeup) { -kvm_for_each_vcpu (i, vcpu, s->kvm) { +rcu_read_lock (); +kvm_for_each_vcpu (i, vcpu, s->kvm, vcpus) {if (kvm_apic_accept_pic_intr (vcpu)) { found = vcpu;break ; } } - +rcu_read_unlock ();if (!found)return ;
@@ -263,7 +264,7 @@ int kvm_pic_read_irq(struct kvm *kvm)void kvm_pic_reset (struct kvm_kpic_state *s) {int irq, i; -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu, *vcpus;u8 irr = s->irr, isr = s->imr;bool found = false;
@@ -282,12 +283,13 @@ void kvm_pic_reset(struct kvm_kpic_state *s) s->special_fully_nested_mode =0 ; s->init4 =0 ; -kvm_for_each_vcpu (i, vcpu, s->pics_state->kvm) +rcu_read_lock (); +kvm_for_each_vcpu (i, vcpu, s->pics_state->kvm, vcpus)if (kvm_apic_accept_pic_intr (vcpu)) { found = true;break ; } - +rcu_read_unlock ();if (!found)return ;
arch/x86/kvm/x86.c +13 -26
@@ -1814,8 +1814,8 @@ static int get_msr_hyperv(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)switch (msr) {case HV_X64_MSR_VP_INDEX : {int r; -struct kvm_vcpu *v; -kvm_for_each_vcpu (r, v, vcpu->kvm) +struct kvm_vcpu *v, *vcpus; +kvm_for_each_vcpu (r, v, vcpu->kvm, vcpus)if (v == vcpu) data = r;break ;
@@ -4607,7 +4607,7 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long va {struct cpufreq_freqs *freq = data;struct kvm *kvm; -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu, *vcpus;int i, send_ipi =0 ;/*
@@ -4658,7 +4658,7 @@ static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long varaw_spin_lock (&kvm_lock);list_for_each_entry (kvm, &vm_list, vm_list) { -kvm_for_each_vcpu (i, vcpu, kvm) { +kvm_for_each_vcpu (i, vcpu, kvm, vcpus) {if (vcpu->cpu != freq->cpu)continue ;kvm_make_request (KVM_REQ_CLOCK_UPDATE , vcpu);
@@ -5950,12 +5950,12 @@ int kvm_arch_vcpu_reset(struct kvm_vcpu *vcpu)int kvm_arch_hardware_enable (void *garbage) {struct kvm *kvm; -struct kvm_vcpu *vcpu; +struct kvm_vcpu *vcpu, *vcpus;int i;kvm_shared_msr_cpu_online ();list_for_each_entry (kvm, &vm_list, vm_list) -kvm_for_each_vcpu (i, vcpu, kvm) +kvm_for_each_vcpu (i, vcpu, kvm, vcpus)if (vcpu->cpu ==smp_processor_id ())kvm_make_request (KVM_REQ_CLOCK_UPDATE , vcpu);return kvm_x86_ops->hardware_enable (garbage);
@@ -6079,27 +6079,14 @@ static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)vcpu_put (vcpu); } -static void kvm_free_vcpus (struct kvm *kvm) +void kvm_arch_vcpu_destruct (struct kvm_vcpu *vcpu) { -unsigned int i; -struct kvm_vcpu *vcpu; - -/* -* Unpin any mmu pages first. -*/ -kvm_for_each_vcpu (i, vcpu, kvm) { -kvm_clear_async_pf_completion_queue (vcpu); -kvm_unload_vcpu_mmu (vcpu); - } -kvm_for_each_vcpu (i, vcpu, kvm) -kvm_arch_vcpu_free (vcpu); - -mutex_lock (&kvm->lock); -for (i =0 ; i <atomic_read (&kvm->online_vcpus); i++) - kvm->vcpus[i] =NULL ; +struct kvm *kvm = vcpu->kvm; -atomic_set (&kvm->online_vcpus,0 ); -mutex_unlock (&kvm->lock); +kvm_clear_async_pf_completion_queue (vcpu); +kvm_unload_vcpu_mmu (vcpu); +kvm_arch_vcpu_free (vcpu); +kvm_put_kvm (kvm); }void kvm_arch_sync_events (struct kvm *kvm)
@@ -6113,7 +6100,7 @@ void kvm_arch_destroy_vm(struct kvm *kvm)kvm_iommu_unmap_guest (kvm);kfree (kvm->arch.vpic);kfree (kvm->arch.vioapic); -kvm_free_vcpus (kvm); +kfree (kvm->vcpus_array);if (kvm->arch.apic_access_page)put_page (kvm->arch.apic_access_page);if (kvm->arch.ept_identity_pagetable)
include/linux/kvm_host.h +9 -6
@@ -254,7 +254,8 @@ struct kvm {#ifdef CONFIG_KVM_APIC_ARCHITECTURE u32 bsp_vcpu_id;#endif -struct kvm_vcpu *vcpus[KVM_MAX_VCPUS ]; +struct kvm_vcpu *vcpus; +struct kvm_vcpu *vcpus_array;atomic_t online_vcpus;int last_boosted_vcpu;struct list_head vm_list;
@@ -303,16 +304,18 @@ struct kvm { #define kvm_printf(kvm, fmt ...) printk(KERN_DEBUG fmt) #define vcpu_printf(vcpu, fmt...) kvm_printf(vcpu->kvm, fmt) -static inline struct kvm_vcpu *kvm_get_vcpu (struct kvm *kvm,int i) +void kvm_arch_vcpu_destruct (struct kvm_vcpu *vcpu); + +static inline struct kvm_vcpu *kvm_get_vcpu (struct kvm_vcpu *vcpus,int i) {smp_rmb (); -return kvm->vcpus[i]; +return vcpus + i; } -#define kvm_for_each_vcpu (idx, vcpup, kvm) \ - for (idx = 0; \ +#define kvm_for_each_vcpu (idx, vcpup, kvm, vcpus) \ + for (vcpus = rcu_dereference(kvm->vcpus), idx = 0; \ idx < atomic_read(&kvm->online_vcpus) && \ - (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \ + (vcpup = kvm_get_vcpu(vcpus, idx)) != NULL; \ idx++)#define kvm_for_each_memslot (memslot, slots) \
virt/kvm/irq_comm.c +2 -2
@@ -82,13 +82,13 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq) {int i, r =-1 ; -struct kvm_vcpu *vcpu, *lowest =NULL ; +struct kvm_vcpu *vcpu, *vcpus, *lowest =NULL ;if (irq->dest_mode ==0 && irq->dest_id ==0xff &&kvm_is_dm_lowest_prio (irq))printk (KERN_INFO "kvm: apic: phys broadcast and lowest prio\n" ); -kvm_for_each_vcpu (i, vcpu, kvm) { +kvm_for_each_vcpu (i, vcpu, kvm, vcpus) {if (!kvm_apic_present (vcpu))continue ;
virt/kvm/kvm_main.c +47 -11
142 lines not on this page · raw message
Signature · 1 line
1.7.4.4
Quoted 1 line · Liu Ping Fan
static int
kvm_vcpu_release(struct inode *inode, struct file *filp)
Is this a hot path?
If no, do you really need to pre-allocate the space for the next vcpus?
Quoted 11 lines
{virt/kvm/kvm_main.c +9
-0+int i;struct kvm_vcpu *vcpu = filp->private_data; +struct kvm *kvm = vcpu->kvm; +struct kvm_vcpu *vcpus_next; + filp->private_data =NULL ; + +for (i =0 ; i<atomic_read (&kvm->online_vcpus); i++) { +if (vcpu == kvm->vcpus+i) +break ; + }
virt/kvm/kvm_main.c static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) · line 1709
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+mutex_lock (&kvm->lock); + vcpus_next = kvm->vcpus_array + + ((kvm->vcpus - kvm->vcpus_array) ?0 :KVM_MAX_VCPUS ); + +memset (vcpus_next,0 ,KVM_MAX_VCPUS *sizeof (struct kvm_vcpu *)); +memcpy (vcpus_next, kvm->vcpus, i*sizeof (struct kvm_vcpu *)); +memcpy (vcpus_next+i, kvm->vcpus+i+1 , + (atomic_read (&kvm->online_vcpus)-i)*sizeof (struct kvm_vcpu *)); +atomic_dec (&kvm->online_vcpus); +/* Removed vcpu can not be seen from vcpus[] */
This comment is confusing.
virt/kvm/kvm_main.c static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) · line 1715
Quoted 7 lines
virt/kvm/kvm_main.c +5
-1+rcu_assign_pointer (kvm->vcpus, vcpus_next); +mutex_unlock (&kvm->lock); + +synchronize_rcu (); -kvm_put_kvm (vcpu->kvm); +/* vcpu is out of list,drop it safely */
Ditto.
Do you mean something like
"now that there is no reader of it we can safely free this" ?
(Please do not trust me: I am not a native English speaker as you know.)
virt/kvm/kvm_main.c static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma) · line 1718
Quoted 3 lines
virt/kvm/kvm_main.c +1
-0+kvm_vcpu_destruct (vcpu);return 0 ; }
Takuya
2012/2/7 Takuya Yoshikawa <[email protected]>:
Quoted 7 lines
Quoted 1 line · Liu Ping Fan
static int
kvm_vcpu_release(struct inode *inode, struct file *filp)Is this a hot path?
If no, do you really need to pre-allocate the space for the next vcpus?
No, it is not a hot path, I will try your way in next version.
Quoted 15 lines
Quoted 15 lines
{
+ int i;
structkvm_vcpu*vcpu =filp->private_data;
+ struct kvm *kvm =vcpu->kvm;
+ structkvm_vcpu*vcpus_next;
+filp->private_data= NULL;virt/kvm/kvm_main.c +9
-0+ + for (i = 0; i< atomic_read(&kvm->online_vcpus); i++) { + if (vcpu == kvm->vcpus+i) + break; + } + mutex_lock(&kvm->lock); + vcpus_next = kvm->vcpus_array + + ((kvm->vcpus - kvm->vcpus_array) ? 0 : KVM_MAX_VCPUS); +
Quoted 9 lines
Quoted 6 lines
+
memset(vcpus_next, 0, KVM_MAX_VCPUS*sizeof(struct kvm_vcpu *));
+memcpy(vcpus_next, kvm->vcpus, i*sizeof(struct kvm_vcpu *));
+ memcpy(vcpus_next+i, kvm->vcpus+i+1,
+ (atomic_read(&kvm->online_vcpus)-i)*sizeof(structkvm_vcpu*));
+atomic_dec(&kvm->online_vcpus);
+ /* Removed vcpu can not be seen from vcpus[] */This comment is confusing.
I mean after assigning pointer, vcpu which to be removed can not be seen from vcpus[]. I will fix this comment.
Quoted 5 lines
Quoted 5 lines
+
rcu_assign_pointer(kvm->vcpus, vcpus_next);
+mutex_unlock(&kvm->lock);
+
+synchronize_rcu();
Quoted 8 lines
Quoted 2 lines
-
kvm_put_kvm(vcpu->kvm);
+ /* vcpu is out of list,drop it safely */Ditto.
Do you mean something like
"now that there is no reader of it we can safely free this" ?
Yes, that is exactly what I mean
Thanks and regards,
ping fan
Quoted 8 lines
(Please do not trust me: I am not a native English speaker as you know.)
Quoted 3 lines
+
kvm_vcpu_destruct(vcpu);
return 0;
}Takuya