arm/arm64: KVM: vgic: delay vgic allocation until init time
authorMarc Zyngier <marc.zyngier@arm.com>
Tue, 8 Jul 2014 11:09:06 +0000 (12:09 +0100)
committerChristoffer Dall <christoffer.dall@linaro.org>
Thu, 2 Oct 2014 15:19:23 +0000 (17:19 +0200)
It is now quite easy to delay the allocation of the vgic tables
until we actually require it to be up and running (when the first
vcpu is kicking around, or someones tries to access the GIC registers).

This allow us to allocate memory for the exact number of CPUs we
have. As nobody configures the number of interrupts just yet,
use a fallback to VGIC_NR_IRQS_LEGACY.

Reviewed-by: Christoffer Dall <christoffer.dall@linaro.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
(cherry picked from commit 4956f2bc1fdee4bc336532f3f34635a8534cedfd)
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>
arch/arm/kvm/arm.c
include/kvm/arm_vgic.h
virt/kvm/arm/vgic.c

index 9e374158363a0ba5fffb7f1a3a268e7d51b2a0c9..072a2084005ca77b6ee889d0ba7b4d812b3b1501 100644 (file)
@@ -261,16 +261,9 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 
 int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
 {
-       int ret;
-
        /* Force users to call KVM_ARM_VCPU_INIT */
        vcpu->arch.target = -1;
 
-       /* Set up VGIC */
-       ret = kvm_vgic_vcpu_init(vcpu);
-       if (ret)
-               return ret;
-
        /* Set up the timer */
        kvm_timer_vcpu_init(vcpu);
 
index aa20d4a7242fce8bd2f5121ad34bd7eae5686dc7..2f2aac8448a44e5a50caf8034ba066ed1c60212f 100644 (file)
@@ -277,7 +277,6 @@ int kvm_vgic_hyp_init(void);
 int kvm_vgic_init(struct kvm *kvm);
 int kvm_vgic_create(struct kvm *kvm);
 void kvm_vgic_destroy(struct kvm *kvm);
-int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
 void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
 void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
 void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);
index 49501bbc2709e9e72f72135074230925101e3628..e7bca4bb7fd1e3aaee7a7fcc0b0f0a98a05aeb7d 100644 (file)
@@ -1729,15 +1729,12 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
  * Initialize the vgic_cpu struct and vgic_dist struct fields pertaining to
  * this vcpu and enable the VGIC for this VCPU
  */
-int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
+static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
 {
        struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
        struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
        int i;
 
-       if (vcpu->vcpu_id >= dist->nr_cpus)
-               return -EBUSY;
-
        for (i = 0; i < dist->nr_irqs; i++) {
                if (i < VGIC_NR_PPIS)
                        vgic_bitmap_set_irq_val(&dist->irq_enabled,
@@ -1757,8 +1754,6 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
        vgic_cpu->nr_lr = vgic->nr_lr;
 
        vgic_enable(vcpu);
-
-       return 0;
 }
 
 void kvm_vgic_destroy(struct kvm *kvm)
@@ -1802,8 +1797,17 @@ static int vgic_init_maps(struct kvm *kvm)
        int nr_cpus, nr_irqs;
        int ret, i;
 
-       nr_cpus = dist->nr_cpus = KVM_MAX_VCPUS;
+       if (dist->nr_cpus)      /* Already allocated */
+               return 0;
+
+       nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
+       if (!nr_cpus)           /* No vcpus? Can't be good... */
+               return -EINVAL;
 
+       /*
+        * If nobody configured the number of interrupts, use the
+        * legacy one.
+        */
        if (!dist->nr_irqs)
                dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
 
@@ -1849,6 +1853,9 @@ static int vgic_init_maps(struct kvm *kvm)
                }
        }
 
+       for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
+               vgic_set_target_reg(kvm, 0, i);
+
 out:
        if (ret)
                kvm_vgic_destroy(kvm);
@@ -1867,6 +1874,7 @@ out:
  */
 int kvm_vgic_init(struct kvm *kvm)
 {
+       struct kvm_vcpu *vcpu;
        int ret = 0, i;
 
        if (!irqchip_in_kernel(kvm))
@@ -1884,6 +1892,12 @@ int kvm_vgic_init(struct kvm *kvm)
                goto out;
        }
 
+       ret = vgic_init_maps(kvm);
+       if (ret) {
+               kvm_err("Unable to allocate maps\n");
+               goto out;
+       }
+
        ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
                                    vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE);
        if (ret) {
@@ -1891,11 +1905,13 @@ int kvm_vgic_init(struct kvm *kvm)
                goto out;
        }
 
-       for (i = VGIC_NR_PRIVATE_IRQS; i < kvm->arch.vgic.nr_irqs; i += 4)
-               vgic_set_target_reg(kvm, 0, i);
+       kvm_for_each_vcpu(i, vcpu, kvm)
+               kvm_vgic_vcpu_init(vcpu);
 
        kvm->arch.vgic.ready = true;
 out:
+       if (ret)
+               kvm_vgic_destroy(kvm);
        mutex_unlock(&kvm->lock);
        return ret;
 }
@@ -1936,10 +1952,6 @@ int kvm_vgic_create(struct kvm *kvm)
        kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
        kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
 
-       ret = vgic_init_maps(kvm);
-       if (ret)
-               kvm_err("Unable to allocate maps\n");
-
 out_unlock:
        for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
                vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
@@ -2140,6 +2152,10 @@ static int vgic_attr_regs_access(struct kvm_device *dev,
 
        mutex_lock(&dev->kvm->lock);
 
+       ret = vgic_init_maps(dev->kvm);
+       if (ret)
+               goto out;
+
        if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
                ret = -EINVAL;
                goto out;