Skip to content

Commit 6d3cfbe

Browse files
pm215chazy
authored andcommitted
arm/arm64: KVM: vgic: move reset initialization into vgic_init_maps()
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>
1 parent 957db10 commit 6d3cfbe

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
@@ -428,11 +428,11 @@ static int kvm_vcpu_first_run_init(struct kvm_vcpu *vcpu)
428428
vcpu->arch.has_run_once = true;
429429

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

‎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);
@@ -1732,39 +1733,14 @@ static int vgic_vcpu_init_maps(struct kvm_vcpu *vcpu, int nr_irqs)
17321733

17331734
int sz = (nr_irqs - VGIC_NR_PRIVATE_IRQS) / 8;
17341735
vgic_cpu->pending_shared = kzalloc(sz, GFP_KERNEL);
1735-
vgic_cpu->vgic_irq_lr_map = kzalloc(nr_irqs, GFP_KERNEL);
1736+
vgic_cpu->vgic_irq_lr_map = kmalloc(nr_irqs, GFP_KERNEL);
17361737

17371738
if (!vgic_cpu->pending_shared || !vgic_cpu->vgic_irq_lr_map) {
17381739
kvm_vgic_vcpu_destroy(vcpu);
17391740
return -ENOMEM;
17401741
}
17411742

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

17691745
/*
17701746
* Store the number of LRs per vcpu, so we don't have to go
@@ -1773,7 +1749,7 @@ static void kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)
17731749
*/
17741750
vgic_cpu->nr_lr = vgic->nr_lr;
17751751

1776-
vgic_enable(vcpu);
1752+
return 0;
17771753
}
17781754

17791755
void kvm_vgic_destroy(struct kvm *kvm)
@@ -1810,12 +1786,12 @@ void kvm_vgic_destroy(struct kvm *kvm)
18101786
* Allocate and initialize the various data structures. Must be called
18111787
* with kvm->lock held!
18121788
*/
1813-
static int vgic_init_maps(struct kvm *kvm)
1789+
static int vgic_init(struct kvm *kvm)
18141790
{
18151791
struct vgic_dist *dist = &kvm->arch.vgic;
18161792
struct kvm_vcpu *vcpu;
18171793
int nr_cpus, nr_irqs;
1818-
int ret, i;
1794+
int ret, i, vcpu_id;
18191795

18201796
if (dist->nr_cpus) /* Already allocated */
18211797
return 0;
@@ -1865,16 +1841,28 @@ static int vgic_init_maps(struct kvm *kvm)
18651841
if (ret)
18661842
goto out;
18671843

1868-
kvm_for_each_vcpu(i, vcpu, kvm) {
1844+
for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1845+
vgic_set_target_reg(kvm, 0, i);
1846+
1847+
kvm_for_each_vcpu(vcpu_id, vcpu, kvm) {
18691848
ret = vgic_vcpu_init_maps(vcpu, nr_irqs);
18701849
if (ret) {
18711850
kvm_err("VGIC: Failed to allocate vcpu memory\n");
18721851
break;
18731852
}
1874-
}
18751853

1876-
for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_irqs; i += 4)
1877-
vgic_set_target_reg(kvm, 0, i);
1854+
for (i = 0; i < dist->nr_irqs; i++) {
1855+
if (i < VGIC_NR_PPIS)
1856+
vgic_bitmap_set_irq_val(&dist->irq_enabled,
1857+
vcpu->vcpu_id, i, 1);
1858+
if (i < VGIC_NR_PRIVATE_IRQS)
1859+
vgic_bitmap_set_irq_val(&dist->irq_cfg,
1860+
vcpu->vcpu_id, i,
1861+
VGIC_CFG_EDGE);
1862+
}
1863+
1864+
vgic_enable(vcpu);
1865+
}
18781866

18791867
out:
18801868
if (ret)
@@ -1884,18 +1872,16 @@ static int vgic_init_maps(struct kvm *kvm)
18841872
}
18851873

18861874
/**
1887-
* kvm_vgic_init - Initialize global VGIC state before running any VCPUs
1875+
* kvm_vgic_map_resources - Configure global VGIC state before running any VCPUs
18881876
* @kvm: pointer to the kvm struct
18891877
*
18901878
* Map the virtual CPU interface into the VM before running any VCPUs. We
18911879
* can't do this at creation time, because user space must first set the
1892-
* virtual CPU interface address in the guest physical address space. Also
1893-
* initialize the ITARGETSRn regs to 0 on the emulated distributor.
1880+
* virtual CPU interface address in the guest physical address space.
18941881
*/
1895-
int kvm_vgic_init(struct kvm *kvm)
1882+
int kvm_vgic_map_resources(struct kvm *kvm)
18961883
{
1897-
struct kvm_vcpu *vcpu;
1898-
int ret = 0, i;
1884+
int ret = 0;
18991885

19001886
if (!irqchip_in_kernel(kvm))
19011887
return 0;
@@ -1912,7 +1898,11 @@ int kvm_vgic_init(struct kvm *kvm)
19121898
goto out;
19131899
}
19141900

1915-
ret = vgic_init_maps(kvm);
1901+
/*
1902+
* Initialize the vgic if this hasn't already been done on demand by
1903+
* accessing the vgic state from userspace.
1904+
*/
1905+
ret = vgic_init(kvm);
19161906
if (ret) {
19171907
kvm_err("Unable to allocate maps\n");
19181908
goto out;
@@ -1926,9 +1916,6 @@ int kvm_vgic_init(struct kvm *kvm)
19261916
goto out;
19271917
}
19281918

1929-
kvm_for_each_vcpu(i, vcpu, kvm)
1930-
kvm_vgic_vcpu_init(vcpu);
1931-
19321919
kvm->arch.vgic.ready = true;
19331920
out:
19341921
if (ret)
@@ -2173,7 +2160,7 @@ static int vgic_attr_regs_access(struct kvm_device *dev,
21732160

21742161
mutex_lock(&dev->kvm->lock);
21752162

2176-
ret = vgic_init_maps(dev->kvm);
2163+
ret = vgic_init(dev->kvm);
21772164
if (ret)
21782165
goto out;
21792166

0 commit comments

Comments
 (0)