Skip to content

Commit 4956f2b

Browse files
Marc Zyngierchazy
authored andcommitted
arm/arm64: KVM: vgic: delay vgic allocation until init time
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>
1 parent 5fb66da commit 4956f2b

3 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎arch/arm/kvm/arm.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,9 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
261261

262262
int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
263263
{
264-
int ret;
265-
266264
/* Force users to call KVM_ARM_VCPU_INIT */
267265
vcpu->arch.target = -1;
268266

269-
/* Set up VGIC */
270-
ret = kvm_vgic_vcpu_init(vcpu);
271-
if (ret)
272-
return ret;
273-
274267
/* Set up the timer */
275268
kvm_timer_vcpu_init(vcpu);
276269

‎include/kvm/arm_vgic.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ int kvm_vgic_hyp_init(void);
277277
int kvm_vgic_init(struct kvm *kvm);
278278
int kvm_vgic_create(struct kvm *kvm);
279279
void kvm_vgic_destroy(struct kvm *kvm);
280-
int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu);
281280
void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
282281
void kvm_vgic_flush_hwstate(struct kvm_vcpu *vcpu);
283282
void kvm_vgic_sync_hwstate(struct kvm_vcpu *vcpu);

‎virt/kvm/arm/vgic.c‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,15 +1729,12 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
17291729
* Initialize the vgic_cpu struct and vgic_dist struct fields pertaining to
17301730
* this vcpu and enable the VGIC for this VCPU
17311731
*/
1732-
int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
1732+
static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
17331733
{
17341734
struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
17351735
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
17361736
int i;
17371737

1738-
if (vcpu->vcpu_id >= dist->nr_cpus)
1739-
return -EBUSY;
1740-
17411738
for (i = 0; i < dist->nr_irqs; i++) {
17421739
if (i < VGIC_NR_PPIS)
17431740
vgic_bitmap_set_irq_val(&dist->irq_enabled,
@@ -1757,8 +1754,6 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
17571754
vgic_cpu->nr_lr = vgic->nr_lr;
17581755

17591756
vgic_enable(vcpu);
1760-
1761-
return 0;
17621757
}
17631758

17641759
void kvm_vgic_destroy(struct kvm *kvm)
@@ -1802,8 +1797,17 @@ static int vgic_init_maps(struct kvm *kvm)
18021797
int nr_cpus, nr_irqs;
18031798
int ret, i;
18041799

1805-
nr_cpus = dist->nr_cpus = KVM_MAX_VCPUS;
1800+
if (dist->nr_cpus) /* Already allocated */
1801+
return 0;
1802+
1803+
nr_cpus = dist->nr_cpus = atomic_read(&kvm->online_vcpus);
1804+
if (!nr_cpus) /* No vcpus? Can't be good... */
1805+
return -EINVAL;
18061806

1807+
/*
1808+
* If nobody configured the number of interrupts, use the
1809+
* legacy one.
1810+
*/
18071811
if (!dist->nr_irqs)
18081812
dist->nr_irqs = VGIC_NR_IRQS_LEGACY;
18091813

@@ -1849,6 +1853,9 @@ static int vgic_init_maps(struct kvm *kvm)
18491853
}
18501854
}
18511855

1856+
for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1857+
vgic_set_target_reg(kvm, 0, i);
1858+
18521859
out:
18531860
if (ret)
18541861
kvm_vgic_destroy(kvm);
@@ -1867,6 +1874,7 @@ static int vgic_init_maps(struct kvm *kvm)
18671874
*/
18681875
int kvm_vgic_init(struct kvm *kvm)
18691876
{
1877+
struct kvm_vcpu *vcpu;
18701878
int ret = 0, i;
18711879

18721880
if (!irqchip_in_kernel(kvm))
@@ -1884,18 +1892,26 @@ int kvm_vgic_init(struct kvm *kvm)
18841892
goto out;
18851893
}
18861894

1895+
ret = vgic_init_maps(kvm);
1896+
if (ret) {
1897+
kvm_err("Unable to allocate maps\n");
1898+
goto out;
1899+
}
1900+
18871901
ret = kvm_phys_addr_ioremap(kvm, kvm->arch.vgic.vgic_cpu_base,
18881902
vgic->vcpu_base, KVM_VGIC_V2_CPU_SIZE);
18891903
if (ret) {
18901904
kvm_err("Unable to remap VGIC CPU to VCPU\n");
18911905
goto out;
18921906
}
18931907

1894-
for (i = VGIC_NR_PRIVATE_IRQS; i < kvm->arch.vgic.nr_irqs; i += 4)
1895-
vgic_set_target_reg(kvm, 0, i);
1908+
kvm_for_each_vcpu(i, vcpu, kvm)
1909+
kvm_vgic_vcpu_init(vcpu);
18961910

18971911
kvm->arch.vgic.ready = true;
18981912
out:
1913+
if (ret)
1914+
kvm_vgic_destroy(kvm);
18991915
mutex_unlock(&kvm->lock);
19001916
return ret;
19011917
}
@@ -1936,10 +1952,6 @@ int kvm_vgic_create(struct kvm *kvm)
19361952
kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
19371953
kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
19381954

1939-
ret = vgic_init_maps(kvm);
1940-
if (ret)
1941-
kvm_err("Unable to allocate maps\n");
1942-
19431955
out_unlock:
19441956
for (; vcpu_lock_idx >= 0; vcpu_lock_idx--) {
19451957
vcpu = kvm_get_vcpu(kvm, vcpu_lock_idx);
@@ -2140,6 +2152,10 @@ static int vgic_attr_regs_access(struct kvm_device *dev,
21402152

21412153
mutex_lock(&dev->kvm->lock);
21422154

2155+
ret = vgic_init_maps(dev->kvm);
2156+
if (ret)
2157+
goto out;
2158+
21432159
if (cpuid >= atomic_read(&dev->kvm->online_vcpus)) {
21442160
ret = -EINVAL;
21452161
goto out;

0 commit comments

Comments
 (0)