History

KVM: Move vcpu alloc and init invocation to common code

Now that all architectures tightly couple vcpu allocation/free with the mandatory calls to kvm_{un}init_vcpu(), move the sequences verbatim to common KVM code. Move both allocation and initialization in a single patch to eliminate thrash in arch specific code. The bisection benefits of moving the two pieces in separate patches is marginal at best, whereas the odds of introducing a transient arch specific bug are non-zero.

Sean Christopherson committed 6 years ago

Commit e529ef61 parent 4543bdc7 files · +43 −138Browse files

Unified · Split

7 files
  1. arch/mips/kvm/mips.c +6 −27
  2. arch/powerpc/kvm/powerpc.c +4 −23
  3. arch/s390/kvm/kvm-s390.c +10 −31
  4. arch/x86/kvm/x86.c +2 −26
  5. include/linux/kvm_host.h +1 −1
  6. virt/kvm/arm/arm.c +2 −27
  7. virt/kvm/kvm_main.c +18 −3
arch/mips/kvm/mips.c+6−27
@@ -285,25 +285,14 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 284 unmodified lines
285285 return 0;
286286 }
287287
288−struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
288+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
289289 {
290290 int err, size;
291291 void *gebase, *p, *handler, *refill_start, *refill_end;
292292 int i;
293293
294− struct kvm_vcpu *vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
295−
296− if (!vcpu) {
297− err = -ENOMEM;
298− goto out;
299− }
300−
301− err = kvm_vcpu_init(vcpu, kvm, id);
302−
303− if (err)
304− goto out_free_cpu;
305−
306− kvm_debug("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu);
294+ kvm_debug("kvm @ %p: create cpu %d at %p\n",
295+ vcpu->kvm, vcpu->vcpu_id, vcpu);
307296
308297 /*
309298 * Allocate space for host mode exception handlers that handle
@@ -318,7 +307,7 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) · 8 unmodified lines
318307
319308 if (!gebase) {
320309 err = -ENOMEM;
321− goto out_uninit_cpu;
310+ goto out;
322311 }
323312 kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n",
324313 ALIGN(size, PAGE_SIZE), gebase);
@@ -397,19 +386,12 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) · 72 unmodified lines
397386 vcpu->arch.last_sched_cpu = -1;
398387 vcpu->arch.last_exec_cpu = -1;
399388
400− return vcpu;
389+ return 0;
401390
402391 out_free_gebase:
403392 kfree(gebase);
404−
405−out_uninit_cpu:
406− kvm_vcpu_uninit(vcpu);
407−
408−out_free_cpu:
409− kmem_cache_free(kvm_vcpu_cache, vcpu);
410−
411393 out:
412− return ERR_PTR(err);
394+ return err;
413395 }
414396
415397 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
@@ -421,9 +403,6 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) · 5 unmodified lines
421403 kvm_mmu_free_memory_caches(vcpu);
422404 kfree(vcpu->arch.guest_ebase);
423405 kfree(vcpu->arch.kseg0_commpage);
424−
425− kvm_vcpu_uninit(vcpu);
426− kmem_cache_free(kvm_vcpu_cache, vcpu);
427406 }
428407
429408 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
arch/powerpc/kvm/powerpc.c+4−23
@@ -725,32 +725,17 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 724 unmodified lines
725725 return 0;
726726 }
727727
728−struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
728+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
729729 {
730− struct kvm_vcpu *vcpu;
731730 int err;
732731
733− vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
734− if (!vcpu)
735− return ERR_PTR(-ENOMEM);
736−
737− err = kvm_vcpu_init(vcpu, kvm, id);
738− if (err)
739− goto free_vcpu;
740−
741732 err = kvmppc_core_vcpu_create(vcpu);
742733 if (err)
743− goto uninit_vcpu;
734+ return err;
744735
745736 vcpu->arch.wqp = &vcpu->wq;
746− kvmppc_create_vcpu_debugfs(vcpu, id);
747− return vcpu;
748−
749−uninit_vcpu:
750− kvm_vcpu_uninit(vcpu);
751−free_vcpu:
752− kmem_cache_free(kvm_vcpu_cache, vcpu);
753− return ERR_PTR(err);
737+ kvmppc_create_vcpu_debugfs(vcpu, vcpu->vcpu_id);
738+ return 0;
754739 }
755740
756741 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
@@ -780,10 +765,6 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) · 23 unmodified lines
780765 }
781766
782767 kvmppc_core_vcpu_free(vcpu);
783−
784− kvm_vcpu_uninit(vcpu);
785−
786− kmem_cache_free(kvm_vcpu_cache, vcpu);
787768 }
788769
789770 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
arch/s390/kvm/kvm-s390.c+10−31
@@ -2530,9 +2530,6 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) · 2529 unmodified lines
25302530 if (vcpu->kvm->arch.use_cmma)
25312531 kvm_s390_vcpu_unsetup_cmma(vcpu);
25322532 free_page((unsigned long)(vcpu->arch.sie_block));
2533−
2534− kvm_vcpu_uninit(vcpu);
2535− kmem_cache_free(kvm_vcpu_cache, vcpu);
25362533 }
25372534
25382535 static void kvm_free_vcpus(struct kvm *kvm)
@@ -3014,29 +3011,15 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 475 unmodified lines
30143011 return 0;
30153012 }
30163013
3017−struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
3018− unsigned int id)
3014+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
30193015 {
3020− struct kvm_vcpu *vcpu;
30213016 struct sie_page *sie_page;
30223017 int rc;
30233018
3024− rc = -ENOMEM;
3025−
3026− vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
3027− if (!vcpu)
3028− goto out;
3029−
3030− rc = kvm_vcpu_init(vcpu, kvm, id);
3031− if (rc)
3032− goto out_free_cpu;
3033−
3034− rc = -ENOMEM;
3035−
30363019 BUILD_BUG_ON(sizeof(struct sie_page) != 4096);
30373020 sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL);
30383021 if (!sie_page)
3039− goto out_uninit_vcpu;
3022+ return -ENOMEM;
30403023
30413024 vcpu->arch.sie_block = &sie_page->sie_block;
30423025 vcpu->arch.sie_block->itdba = (unsigned long) &sie_page->itdb;
@@ -3045,9 +3028,9 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, · 2 unmodified lines
30453028 vcpu->arch.sie_block->mso = 0;
30463029 vcpu->arch.sie_block->msl = sclp.hamax;
30473030
3048− vcpu->arch.sie_block->icpua = id;
3031+ vcpu->arch.sie_block->icpua = vcpu->vcpu_id;
30493032 spin_lock_init(&vcpu->arch.local_int.lock);
3050− vcpu->arch.sie_block->gd = (u32)(u64)kvm->arch.gisa_int.origin;
3033+ vcpu->arch.sie_block->gd = (u32)(u64)vcpu->kvm->arch.gisa_int.origin;
30513034 if (vcpu->arch.sie_block->gd && sclp.has_gisaf)
30523035 vcpu->arch.sie_block->gd |= GISA_FORMAT1;
30533036 seqcount_init(&vcpu->arch.cputm_seqcount);
@@ -3083,19 +3066,15 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, · 29 unmodified lines
30833066 goto out_free_sie_block;
30843067 }
30853068
3086− VM_EVENT(kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK", id, vcpu,
3087− vcpu->arch.sie_block);
3088− trace_kvm_s390_create_vcpu(id, vcpu, vcpu->arch.sie_block);
3069+ VM_EVENT(vcpu->kvm, 3, "create cpu %d at 0x%pK, sie block at 0x%pK",
3070+ vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3071+ trace_kvm_s390_create_vcpu(vcpu->vcpu_id, vcpu, vcpu->arch.sie_block);
3072+
3073+ return 0;
30893074
3090− return vcpu;
30913075 out_free_sie_block:
30923076 free_page((unsigned long)(vcpu->arch.sie_block));
3093−out_uninit_vcpu:
3094− kvm_vcpu_uninit(vcpu);
3095−out_free_cpu:
3096− kmem_cache_free(kvm_vcpu_cache, vcpu);
3097−out:
3098− return ERR_PTR(rc);
3077+ return rc;
30993078 }
31003079
31013080 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.c+2−26
@@ -9179,30 +9179,9 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 9178 unmodified lines
91799179 return 0;
91809180 }
91819181
9182−struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
9183− unsigned int id)
9182+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
91849183 {
9185− struct kvm_vcpu *vcpu;
9186− int r;
9187−
9188− vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL_ACCOUNT);
9189− if (!vcpu)
9190− return ERR_PTR(-ENOMEM);
9191−
9192− r = kvm_vcpu_init(vcpu, kvm, id);
9193− if (r)
9194− goto free_vcpu;
9195−
9196− r = kvm_x86_ops->vcpu_create(vcpu);
9197− if (r)
9198− goto uninit_vcpu;
9199− return vcpu;
9200−
9201−uninit_vcpu:
9202− kvm_vcpu_uninit(vcpu);
9203−free_vcpu:
9204− kmem_cache_free(kvm_vcpu_cache, vcpu);
9205− return ERR_PTR(r);
9184+ return kvm_x86_ops->vcpu_create(vcpu);
92069185 }
92079186
92089187 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
@@ -9254,9 +9233,6 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) · 45 unmodified lines
92549233 free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
92559234 kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu);
92569235 kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu);
9257−
9258− kvm_vcpu_uninit(vcpu);
9259− kmem_cache_free(kvm_vcpu_cache, vcpu);
92609236 }
92619237
92629238 void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
include/linux/kvm_host.h+1−1
@@ -876,7 +876,7 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu); · 875 unmodified lines
876876 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu);
877877 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu);
878878 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id);
879−struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id);
879+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu);
880880 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu);
881881 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu);
882882 void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu);
virt/kvm/arm/arm.c+2−27
@@ -290,32 +290,9 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 289 unmodified lines
290290 return 0;
291291 }
292292
293−struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id)
293+int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)
294294 {
295− int err;
296− struct kvm_vcpu *vcpu;
297−
298− vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
299− if (!vcpu) {
300− err = -ENOMEM;
301− goto out;
302− }
303−
304− err = kvm_vcpu_init(vcpu, kvm, id);
305− if (err)
306− goto free_vcpu;
307−
308− err = create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
309− if (err)
310− goto vcpu_uninit;
311−
312− return vcpu;
313−vcpu_uninit:
314− kvm_vcpu_uninit(vcpu);
315−free_vcpu:
316− kmem_cache_free(kvm_vcpu_cache, vcpu);
317−out:
318− return ERR_PTR(err);
295+ return create_hyp_mappings(vcpu, vcpu + 1, PAGE_HYP);
319296 }
320297
321298 void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
@@ -330,8 +307,6 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) · 8 unmodified lines
330307 kvm_mmu_free_memory_caches(vcpu);
331308 kvm_timer_vcpu_terminate(vcpu);
332309 kvm_pmu_vcpu_destroy(vcpu);
333− kvm_vcpu_uninit(vcpu);
334− kmem_cache_free(kvm_vcpu_cache, vcpu);
335310 }
336311
337312 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
virt/kvm/kvm_main.c+18−3
@@ -378,6 +378,9 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); · 377 unmodified lines
378378 void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
379379 {
380380 kvm_arch_vcpu_destroy(vcpu);
381+
382+ kvm_vcpu_uninit(vcpu);
383+ kmem_cache_free(kvm_vcpu_cache, vcpu);
381384 }
382385 EXPORT_SYMBOL_GPL(kvm_vcpu_destroy);
383386
@@ -2738,12 +2741,20 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) · 2354 unmodified lines
27382741 if (r)
27392742 goto vcpu_decrement;
27402743
2741− vcpu = kvm_arch_vcpu_create(kvm, id);
2742− if (IS_ERR(vcpu)) {
2743− r = PTR_ERR(vcpu);
2744+ vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
2745+ if (!vcpu) {
2746+ r = -ENOMEM;
27442747 goto vcpu_decrement;
27452748 }
27462749
2750+ r = kvm_vcpu_init(vcpu, kvm, id);
2751+ if (r)
2752+ goto vcpu_free;
2753+
2754+ r = kvm_arch_vcpu_create(vcpu);
2755+ if (r)
2756+ goto vcpu_uninit;
2757+
27472758 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
27482759
27492760 r = kvm_arch_vcpu_setup(vcpu);
@@ -2787,6 +2798,10 @@ unlock_vcpu_destroy: · 37 unmodified lines
27872798 debugfs_remove_recursive(vcpu->debugfs_dentry);
27882799 vcpu_destroy:
27892800 kvm_arch_vcpu_destroy(vcpu);
2801+vcpu_uninit:
2802+ kvm_vcpu_uninit(vcpu);
2803+vcpu_free:
2804+ kmem_cache_free(kvm_vcpu_cache, vcpu);
27902805 vcpu_decrement:
27912806 mutex_lock(&kvm->lock);
27922807 kvm->created_vcpus--;