Skip to content

Commit 2da33f7

Browse files
pm215sashalevin
authored andcommitted
arm/arm64: KVM: vgic: move reset initialization into vgic_init_maps()
commit 6d3cfbe upstream. VGIC initialization currently happens in three phases: (1) kvm_vgic_create() (triggered by userspace GIC creation) (2) vgic_init_maps() (triggered by userspace GIC register read/write requests, or from kvm_vgic_init() if not already run) (3) kvm_vgic_init() (triggered by first VM run) We were doing initialization of some state to correspond with the state of a freshly-reset GIC in kvm_vgic_init(); this is too late, since it will overwrite changes made by userspace using the register access APIs before the VM is run. Move this initialization earlier, into the vgic_init_maps() phase. This fixes a bug where QEMU could successfully restore a saved VM state snapshot into a VM that had already been run, but could not restore it "from cold" using the -loadvm command line option (the symptoms being that the restored VM would run but interrupts were ignored). Finally rename vgic_init_maps to vgic_init and renamed kvm_vgic_init to kvm_vgic_map_resources. [ This patch is originally written by Peter Maydell, but I have modified it somewhat heavily, renaming various bits and moving code around. If something is broken, I am to be blamed. - Christoffer ] Acked-by: Marc Zyngier <marc.zyngier@arm.com> Reviewed-by: Eric Auger <eric.auger@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org> Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent 63d4dc9 commit 2da33f7

3 files changed

Lines changed: 37 additions & 50 deletions

File tree

‎arch/arm/kvm/arm.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,11 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
427427
vcpu->arch.has_run_once = true;
428428

429429
/*
430-
* Initialize the VGIC before running a vcpu the first time on
431-
* this VM.
430+
* Map the VGIC hardware resources before running a vcpu the first
431+
* time on this VM.
432432
*/
433433
if (unlikely(!vgic_initialized(vcpu->kvm))) {
434-
ret = kvm_vgic_init(vcpu->kvm);
434+
ret = kvm_vgic_map_resources(vcpu->kvm);
435435
if (ret)
436436
return ret;
437437
}

‎include/kvm/arm_vgic.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ struct kvm_exit_mmio;
274274
#ifdef CONFIG_KVM_ARM_VGIC
275275
int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr, bool write);
276276
int kvm_vgic_hyp_init(void);
277-
int kvm_vgic_init(struct kvm *kvm);
277+
int kvm_vgic_map_resources(struct kvm *kvm);
278278
int kvm_vgic_create(struct kvm *kvm);
279279
void kvm_vgic_destroy(struct kvm *kvm);
280280
void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu);
@@ -321,7 +321,7 @@ static inline int kvm_vgic_addr(struct kvm *kvm, unsigned long type, u64 *addr,
321321
return -ENXIO;
322322
}
323323

324-
static inline int kvm_vgic_init(struct kvm *kvm)
324+
static inline int kvm_vgic_map_resources(struct kvm *kvm)
325325
{
326326
return 0;
327327
}

‎virt/kvm/arm/vgic.c‎

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
#define ACCESS_WRITE_VALUE (3 << 1)
9292
#define ACCESS_WRITE_MASK(x) ((x) & (3 << 1))
9393

94+
static int vgic_init(struct kvm *kvm);
9495
static void vgic_retire_disabled_irqs(struct kvm_vcpu *vcpu);
9596
static void vgic_retire_lr(int lr_nr, int irq, struct kvm_vcpu *vcpu);
9697
static void vgic_update_state(struct kvm *kvm);
@@ -1726,39 +1727,14 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
17261727

17271728
int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
17281729
vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
1729-
vgic_cpu->vgic_irq_lr_map = kzalloc(nr_irqs, GFP_KERNEL);
1730+
vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
17301731

17311732
if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
17321733
kvm_vgic_vcpu_destroy(vcpu);
17331734
return -ENOMEM;
17341735
}
17351736

1736-
return 0;
1737-
}
1738-
1739-
/**
1740-
* kvm_vgic_vcpu_init - Initialize per-vcpu VGIC state
1741-
* @vcpu: pointer to the vcpu struct
1742-
*
1743-
* Initialize the vgic_cpu struct and vgic_dist struct fields pertaining to
1744-
* this vcpu and enable the VGIC for this VCPU
1745-
*/
1746-
static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
1747-
{
1748-
struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
1749-
struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
1750-
int i;
1751-
1752-
for (i = 0; i < dist->nr_irqs; i++) {
1753-
if (i < VGIC_NR_PPIS)
1754-
vgic_bitmap_set_irq_val(&dist->irq_enabled,
1755-
vcpu->vcpu_id, i, 1);
1756-
if (i < VGIC_NR_PRIVATE_IRQS)
1757-
vgic_bitmap_set_irq_val(&dist->irq_cfg,
1758-
vcpu->vcpu_id, i, VGIC_CFG_EDGE);
1759-
1760-
vgic_cpu->vgic_irq_lr_map[i] = LR_EMPTY;
1761-
}
1737+
memset(vgic_cpu->vgic_irq_lr_map, LR_EMPTY, nr_irqs);
17621738

17631739
/*
17641740
* Store the number of LRs per vcpu, so we don't have to go
@@ -1767,7 +1743,7 @@ static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
17671743
*/
17681744
vgic_cpu->nr_lr = vgic->nr_lr;
17691745

1770-
vgic_enable(vcpu);
1746+
return 0;
17711747
}
17721748

17731749
void kvm_vgic_destroy(struct kvm *kvm)
@@ -1804,12 +1780,12 @@ void kvm_vgic_destroy(struct kvm *kvm)
18041780
* Allocate and initialize the various data structures. Must be called
18051781
* with kvm->lock held!
18061782
*/
1807-
static int vgic_init_maps(struct kvm *kvm)
1783+
static int vgic_init(struct kvm *kvm)
18081784
{
18091785
struct vgic_dist *dist = &kvm->arch.vgic;
18101786
struct kvm_vcpu *vcpu;
18111787
int nr_cpus, nr_irqs;
1812-
int ret, i;
1788+
int ret, i, vcpu_id;
18131789

18141790
if (dist->nr_cpus) /* Already allocated */
18151791
return 0;
@@ -1859,16 +1835,28 @@ static int vgic_init_maps(struct kvm *kvm)
18591835
if (ret)
18601836
goto out;
18611837

1862-
kvm_for_each_vcpu(i, vcpu, kvm) {
1838+
for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1839+
vgic_set_target_reg(kvm, 0, i);
1840+
1841+
kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
18631842
ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
18641843
if (ret) {
18651844
kvm_err("VGIC: Failed to allocate vcpu memory\n");
18661845
break;
18671846
}
1868-
}
18691847

1870-
for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1871-
vgic_set_target_reg(kvm, 0, i);
1848+
for (i = 0; i < dist->nr_irqs; i++) {
1849+
if (i < VGIC_NR_PPIS)
1850+
vgic_bitmap_set_irq_val(&dist->irq_enabled,
1851+
vcpu->vcpu_id, i, 1);
1852+
if (i < VGIC_NR_PRIVATE_IRQS)
1853+
vgic_bitmap_set_irq_val(&dist->irq_cfg,
1854+
vcpu->vcpu_id, i,
1855+
VGIC_CFG_EDGE);
1856+
}
1857+
1858+
vgic_enable(vcpu);
1859+
}
18721860

18731861
out:
18741862
if (ret)
@@ -1878,18 +1866,16 @@ static int vgic_init_maps(struct kvm *kvm)
18781866
}
18791867

18801868
/**
1881-
* kvm_vgic_init - Initialize global VGIC state before running any VCPUs
1869+
* kvm_vgic_map_resources - Configure global VGIC state before running any VCPUs
18821870
* @kvm: pointer to the kvm struct
18831871
*
18841872
* Map the virtual CPU interface into the VM before running any VCPUs. We
18851873
* can't do this at creation time, because user space must first set the
1886-
* virtual CPU interface address in the guest physical address space. Also
1887-
* initialize the ITARGETSRn regs to 0 on the emulated distributor.
1874+
* virtual CPU interface address in the guest physical address space.
18881875
*/
1889-
int kvm_vgic_init(struct kvm *kvm)
1876+
int kvm_vgic_map_resources(struct kvm *kvm)
18901877
{
1891-
struct kvm_vcpu *vcpu;
1892-
int ret = 0, i;
1878+
int ret = 0;
18931879

18941880
if (!irqchip_in_kernel(kvm))
18951881
return 0;
@@ -1906,7 +1892,11 @@ int kvm_vgic_init(struct kvm *kvm)
19061892
goto out;
19071893
}
19081894

1909-
ret = vgic_init_maps(kvm);
1895+
/*
1896+
* Initialize the vgic if this hasn't already been done on demand by
1897+
* accessing the vgic state from userspace.
1898+
*/
1899+
ret = vgic_init(kvm);
19101900
if (ret) {
19111901
kvm_err("Unable to allocate maps\n");
19121902
goto out;
@@ -1920,9 +1910,6 @@ int kvm_vgic_init(struct kvm *kvm)
19201910
goto out;
19211911
}
19221912

1923-
kvm_for_each_vcpu(i, vcpu, kvm)
1924-
kvm_vgic_vcpu_init(vcpu);
1925-
19261913
kvm->arch.vgic.ready = true;
19271914
out:
19281915
if (ret)
@@ -2167,7 +2154,7 @@ static int vgic_attr_regs_access(struct kvm_device *dev,
21672154

21682155
mutex_lock(&dev->kvm->lock);
21692156

2170-
ret = vgic_init_maps(dev->kvm);
2157+
ret = vgic_init(dev->kvm);
21712158
if (ret)
21722159
goto out;
21732160

0 commit comments

Comments
 (0)