KVM: Add max_vcpus field in common 'struct kvm'
authorSean Christopherson <sean.j.christopherson@intel.com>
Fri, 4 Mar 2022 19:48:38 +0000 (11:48 -0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Mon, 2 May 2022 15:42:42 +0000 (11:42 -0400)
For TDX guests, the maximum number of vcpus needs to be specified when the
TDX guest VM is initialized (creating the TDX data corresponding to TDX
guest) before creating vcpu.  It needs to record the maximum number of
vcpus on VM creation (KVM_CREATE_VM) and return error if the number of
vcpus exceeds it

Because there is already max_vcpu member in arm64 struct kvm_arch, move it
to common struct kvm and initialize it to KVM_MAX_VCPUS before
kvm_arch_init_vm() instead of adding it to x86 struct kvm_arch.

Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
Message-Id: <e53234cdee6a92357d06c80c03d77c19cdefb804.1646422845.git.isaku.yamahata@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/arm64/include/asm/kvm_host.h
arch/arm64/kvm/arm.c
arch/arm64/kvm/vgic/vgic-init.c
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index 94a27a7520f4740e64e202599c11fa75441b4e44..27ebb2929e0cd3307d7f9ed6c87b52f5558e7612 100644 (file)
@@ -107,9 +107,6 @@ struct kvm_arch {
        /* VTCR_EL2 value for this VM */
        u64    vtcr;
 
-       /* The maximum number of vCPUs depends on the used GIC model */
-       int max_vcpus;
-
        /* Interrupt controller */
        struct vgic_dist        vgic;
 
index 523bc934fe2f66687b2bb605776f4b239b6114d3..7fceb855fa710d0859ee761c05859e8cf846a062 100644 (file)
@@ -153,7 +153,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
        kvm_vgic_early_init(kvm);
 
        /* The maximum number of VCPUs is limited by the host's GIC model */
-       kvm->arch.max_vcpus = kvm_arm_default_max_vcpus();
+       kvm->max_vcpus = kvm_arm_default_max_vcpus();
 
        set_default_spectre(kvm);
 
@@ -230,7 +230,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
        case KVM_CAP_MAX_VCPUS:
        case KVM_CAP_MAX_VCPU_ID:
                if (kvm)
-                       r = kvm->arch.max_vcpus;
+                       r = kvm->max_vcpus;
                else
                        r = kvm_arm_default_max_vcpus();
                break;
@@ -306,7 +306,7 @@ int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
        if (irqchip_in_kernel(kvm) && vgic_initialized(kvm))
                return -EBUSY;
 
-       if (id >= kvm->arch.max_vcpus)
+       if (id >= kvm->max_vcpus)
                return -EINVAL;
 
        return 0;
index fc00304fe7d8a7f0863a389bab32f9a8ea73cad3..77feafd5c0e3f806b0d51e3e4ca3f1774a099ff1 100644 (file)
@@ -98,11 +98,11 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
        ret = 0;
 
        if (type == KVM_DEV_TYPE_ARM_VGIC_V2)
-               kvm->arch.max_vcpus = VGIC_V2_MAX_CPUS;
+               kvm->max_vcpus = VGIC_V2_MAX_CPUS;
        else
-               kvm->arch.max_vcpus = VGIC_V3_MAX_CPUS;
+               kvm->max_vcpus = VGIC_V3_MAX_CPUS;
 
-       if (atomic_read(&kvm->online_vcpus) > kvm->arch.max_vcpus) {
+       if (atomic_read(&kvm->online_vcpus) > kvm->max_vcpus) {
                ret = -E2BIG;
                goto out_unlock;
        }
index 252ee4a61b58b622f1f69380a84bd8d6f2575c50..f94f72bbd2d3ab2c12249e9f6e99b38fe9b44cb2 100644 (file)
@@ -725,6 +725,7 @@ struct kvm {
         * and is accessed atomically.
         */
        atomic_t online_vcpus;
+       int max_vcpus;
        int created_vcpus;
        int last_boosted_vcpu;
        struct list_head vm_list;
index ac57fc2c935fc40d0fcb8dfab4b5c2e7210cabfb..655365b2cbe818b5b2a81227a4391f729cec7f09 100644 (file)
@@ -1078,6 +1078,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
        spin_lock_init(&kvm->gpc_lock);
 
        INIT_LIST_HEAD(&kvm->devices);
+       kvm->max_vcpus = KVM_MAX_VCPUS;
 
        BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
 
@@ -3732,7 +3733,7 @@ static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
                return -EINVAL;
 
        mutex_lock(&kvm->lock);
-       if (kvm->created_vcpus == KVM_MAX_VCPUS) {
+       if (kvm->created_vcpus >= kvm->max_vcpus) {
                mutex_unlock(&kvm->lock);
                return -EINVAL;
        }