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.
Unified · Split
| @@ -285,25 +285,14 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 284 unmodified lines | ||
| 285 | 285 | return 0; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | −struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) | |
| 288 | +int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) | |
| 289 | 289 | { |
| 290 | 290 | int err, size; |
| 291 | 291 | void *gebase, *p, *handler, *refill_start, *refill_end; |
| 292 | 292 | int i; |
| 293 | 293 | |
| 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); | |
| 307 | 296 | |
| 308 | 297 | /* |
| 309 | 298 | * 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 | ||
| 318 | 307 | |
| 319 | 308 | if (!gebase) { |
| 320 | 309 | err = -ENOMEM; |
| 321 | − goto out_uninit_cpu; | |
| 310 | + goto out; | |
| 322 | 311 | } |
| 323 | 312 | kvm_debug("Allocated %d bytes for KVM Exception Handlers @ %p\n", |
| 324 | 313 | 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 | ||
| 397 | 386 | vcpu->arch.last_sched_cpu = -1; |
| 398 | 387 | vcpu->arch.last_exec_cpu = -1; |
| 399 | 388 | |
| 400 | − return vcpu; | |
| 389 | + return 0; | |
| 401 | 390 | |
| 402 | 391 | out_free_gebase: |
| 403 | 392 | 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 | − | |
| 411 | 393 | out: |
| 412 | − return ERR_PTR(err); | |
| 394 | + return err; | |
| 413 | 395 | } |
| 414 | 396 | |
| 415 | 397 | 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 | ||
| 421 | 403 | kvm_mmu_free_memory_caches(vcpu); |
| 422 | 404 | kfree(vcpu->arch.guest_ebase); |
| 423 | 405 | kfree(vcpu->arch.kseg0_commpage); |
| 424 | − | |
| 425 | − kvm_vcpu_uninit(vcpu); | |
| 426 | − kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 427 | 406 | } |
| 428 | 407 | |
| 429 | 408 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| @@ -725,32 +725,17 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 724 unmodified lines | ||
| 725 | 725 | return 0; |
| 726 | 726 | } |
| 727 | 727 | |
| 728 | −struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) | |
| 728 | +int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) | |
| 729 | 729 | { |
| 730 | − struct kvm_vcpu *vcpu; | |
| 731 | 730 | int err; |
| 732 | 731 | |
| 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 | − | |
| 741 | 732 | err = kvmppc_core_vcpu_create(vcpu); |
| 742 | 733 | if (err) |
| 743 | − goto uninit_vcpu; | |
| 734 | + return err; | |
| 744 | 735 | |
| 745 | 736 | 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; | |
| 754 | 739 | } |
| 755 | 740 | |
| 756 | 741 | 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 | ||
| 780 | 765 | } |
| 781 | 766 | |
| 782 | 767 | kvmppc_core_vcpu_free(vcpu); |
| 783 | − | |
| 784 | − kvm_vcpu_uninit(vcpu); | |
| 785 | − | |
| 786 | − kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 787 | 768 | } |
| 788 | 769 | |
| 789 | 770 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| @@ -2530,9 +2530,6 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) · 2529 unmodified lines | ||
| 2530 | 2530 | if (vcpu->kvm->arch.use_cmma) |
| 2531 | 2531 | kvm_s390_vcpu_unsetup_cmma(vcpu); |
| 2532 | 2532 | free_page((unsigned long)(vcpu->arch.sie_block)); |
| 2533 | − | |
| 2534 | − kvm_vcpu_uninit(vcpu); | |
| 2535 | − kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 2536 | 2533 | } |
| 2537 | 2534 | |
| 2538 | 2535 | 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 | ||
| 3014 | 3011 | return 0; |
| 3015 | 3012 | } |
| 3016 | 3013 | |
| 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) | |
| 3019 | 3015 | { |
| 3020 | − struct kvm_vcpu *vcpu; | |
| 3021 | 3016 | struct sie_page *sie_page; |
| 3022 | 3017 | int rc; |
| 3023 | 3018 | |
| 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 | − | |
| 3036 | 3019 | BUILD_BUG_ON(sizeof(struct sie_page) != 4096); |
| 3037 | 3020 | sie_page = (struct sie_page *) get_zeroed_page(GFP_KERNEL); |
| 3038 | 3021 | if (!sie_page) |
| 3039 | − goto out_uninit_vcpu; | |
| 3022 | + return -ENOMEM; | |
| 3040 | 3023 | |
| 3041 | 3024 | vcpu->arch.sie_block = &sie_page->sie_block; |
| 3042 | 3025 | 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 | ||
| 3045 | 3028 | vcpu->arch.sie_block->mso = 0; |
| 3046 | 3029 | vcpu->arch.sie_block->msl = sclp.hamax; |
| 3047 | 3030 | |
| 3048 | − vcpu->arch.sie_block->icpua = id; | |
| 3031 | + vcpu->arch.sie_block->icpua = vcpu->vcpu_id; | |
| 3049 | 3032 | 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; | |
| 3051 | 3034 | if (vcpu->arch.sie_block->gd && sclp.has_gisaf) |
| 3052 | 3035 | vcpu->arch.sie_block->gd |= GISA_FORMAT1; |
| 3053 | 3036 | seqcount_init(&vcpu->arch.cputm_seqcount); |
| @@ -3083,19 +3066,15 @@ struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, · 29 unmodified lines | ||
| 3083 | 3066 | goto out_free_sie_block; |
| 3084 | 3067 | } |
| 3085 | 3068 | |
| 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; | |
| 3089 | 3074 | |
| 3090 | − return vcpu; | |
| 3091 | 3075 | out_free_sie_block: |
| 3092 | 3076 | 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; | |
| 3099 | 3078 | } |
| 3100 | 3079 | |
| 3101 | 3080 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| @@ -9179,30 +9179,9 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 9178 unmodified lines | ||
| 9179 | 9179 | return 0; |
| 9180 | 9180 | } |
| 9181 | 9181 | |
| 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) | |
| 9184 | 9183 | { |
| 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); | |
| 9206 | 9185 | } |
| 9207 | 9186 | |
| 9208 | 9187 | 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 | ||
| 9254 | 9233 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
| 9255 | 9234 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); |
| 9256 | 9235 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); |
| 9257 | − | |
| 9258 | − kvm_vcpu_uninit(vcpu); | |
| 9259 | − kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 9260 | 9236 | } |
| 9261 | 9237 | |
| 9262 | 9238 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
| @@ -876,7 +876,7 @@ void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu); · 875 unmodified lines | ||
| 876 | 876 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu); |
| 877 | 877 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu); |
| 878 | 878 | 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); | |
| 880 | 880 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu); |
| 881 | 881 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu); |
| 882 | 882 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu); |
| @@ -290,32 +290,9 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) · 289 unmodified lines | ||
| 290 | 290 | return 0; |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | −struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) | |
| 293 | +int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) | |
| 294 | 294 | { |
| 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); | |
| 319 | 296 | } |
| 320 | 297 | |
| 321 | 298 | 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 | ||
| 330 | 307 | kvm_mmu_free_memory_caches(vcpu); |
| 331 | 308 | kvm_timer_vcpu_terminate(vcpu); |
| 332 | 309 | kvm_pmu_vcpu_destroy(vcpu); |
| 333 | − kvm_vcpu_uninit(vcpu); | |
| 334 | − kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 335 | 310 | } |
| 336 | 311 | |
| 337 | 312 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| @@ -378,6 +378,9 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); · 377 unmodified lines | ||
| 378 | 378 | void kvm_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 379 | 379 | { |
| 380 | 380 | kvm_arch_vcpu_destroy(vcpu); |
| 381 | + | |
| 382 | + kvm_vcpu_uninit(vcpu); | |
| 383 | + kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 381 | 384 | } |
| 382 | 385 | EXPORT_SYMBOL_GPL(kvm_vcpu_destroy); |
| 383 | 386 | |
| @@ -2738,12 +2741,20 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id) · 2354 unmodified lines | ||
| 2738 | 2741 | if (r) |
| 2739 | 2742 | goto vcpu_decrement; |
| 2740 | 2743 | |
| 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; | |
| 2744 | 2747 | goto vcpu_decrement; |
| 2745 | 2748 | } |
| 2746 | 2749 | |
| 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 | + | |
| 2747 | 2758 | preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops); |
| 2748 | 2759 | |
| 2749 | 2760 | r = kvm_arch_vcpu_setup(vcpu); |
| @@ -2787,6 +2798,10 @@ unlock_vcpu_destroy: · 37 unmodified lines | ||
| 2787 | 2798 | debugfs_remove_recursive(vcpu->debugfs_dentry); |
| 2788 | 2799 | vcpu_destroy: |
| 2789 | 2800 | kvm_arch_vcpu_destroy(vcpu); |
| 2801 | +vcpu_uninit: | |
| 2802 | + kvm_vcpu_uninit(vcpu); | |
| 2803 | +vcpu_free: | |
| 2804 | + kmem_cache_free(kvm_vcpu_cache, vcpu); | |
| 2790 | 2805 | vcpu_decrement: |
| 2791 | 2806 | mutex_lock(&kvm->lock); |
| 2792 | 2807 | kvm->created_vcpus--; |