Commit d813a8ba authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: x86: Move allocation of pio_data page down a few lines

Allocate the pio_data page after creating the MMU and local APIC so that
all direct memory allocations are grouped together.  This allows setting
the return value to -ENOMEM prior to starting the allocations instead of
setting it in the fail path for every allocation.

The pio_data page is only consumed when KVM_RUN is invoked, i.e. moving
its allocation has no real functional impact.
Signed-off-by: default avatarSean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent fc6e2a18
...@@ -9510,18 +9510,11 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) ...@@ -9510,18 +9510,11 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
else else
vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
page = alloc_page(GFP_KERNEL | __GFP_ZERO);
if (!page) {
r = -ENOMEM;
goto fail;
}
vcpu->arch.pio_data = page_address(page);
kvm_set_tsc_khz(vcpu, max_tsc_khz); kvm_set_tsc_khz(vcpu, max_tsc_khz);
r = kvm_mmu_create(vcpu); r = kvm_mmu_create(vcpu);
if (r < 0) if (r < 0)
goto fail_free_pio_data; return r;
if (irqchip_in_kernel(vcpu->kvm)) { if (irqchip_in_kernel(vcpu->kvm)) {
vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu->kvm); vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu->kvm);
...@@ -9531,25 +9524,27 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) ...@@ -9531,25 +9524,27 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
} else } else
static_key_slow_inc(&kvm_no_apic_vcpu); static_key_slow_inc(&kvm_no_apic_vcpu);
r = -ENOMEM;
page = alloc_page(GFP_KERNEL | __GFP_ZERO);
if (!page)
goto fail_free_lapic;
vcpu->arch.pio_data = page_address(page);
vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
GFP_KERNEL_ACCOUNT); GFP_KERNEL_ACCOUNT);
if (!vcpu->arch.mce_banks) { if (!vcpu->arch.mce_banks)
r = -ENOMEM; goto fail_free_pio_data;
goto fail_free_lapic;
}
vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
GFP_KERNEL_ACCOUNT)) { GFP_KERNEL_ACCOUNT))
r = -ENOMEM;
goto fail_free_mce_banks; goto fail_free_mce_banks;
}
vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache,
GFP_KERNEL_ACCOUNT); GFP_KERNEL_ACCOUNT);
if (!vcpu->arch.user_fpu) { if (!vcpu->arch.user_fpu) {
pr_err("kvm: failed to allocate userspace's fpu\n"); pr_err("kvm: failed to allocate userspace's fpu\n");
r = -ENOMEM;
goto free_wbinvd_dirty_mask; goto free_wbinvd_dirty_mask;
} }
...@@ -9557,7 +9552,6 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) ...@@ -9557,7 +9552,6 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
GFP_KERNEL_ACCOUNT); GFP_KERNEL_ACCOUNT);
if (!vcpu->arch.guest_fpu) { if (!vcpu->arch.guest_fpu) {
pr_err("kvm: failed to allocate vcpu's fpu\n"); pr_err("kvm: failed to allocate vcpu's fpu\n");
r = -ENOMEM;
goto free_user_fpu; goto free_user_fpu;
} }
fx_init(vcpu); fx_init(vcpu);
...@@ -9584,13 +9578,12 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) ...@@ -9584,13 +9578,12 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
fail_free_mce_banks: fail_free_mce_banks:
kfree(vcpu->arch.mce_banks); kfree(vcpu->arch.mce_banks);
fail_free_pio_data:
free_page((unsigned long)vcpu->arch.pio_data);
fail_free_lapic: fail_free_lapic:
kvm_free_lapic(vcpu); kvm_free_lapic(vcpu);
fail_mmu_destroy: fail_mmu_destroy:
kvm_mmu_destroy(vcpu); kvm_mmu_destroy(vcpu);
fail_free_pio_data:
free_page((unsigned long)vcpu->arch.pio_data);
fail:
return r; return r;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment