Userspace writes to GICD_CTLR update dist->nassgireq without moving vSGIs between HW and SW. On a VM that has already run (such as reset or in-place restore), the restored nASSGIreq does not take effect until the guest next toggles it. Move the nASSGIreq switch and distributor-enable queueing into a shared helper called by both guest and userspace writes. On a fresh VM, vSGIs now move to HW before userspace restores redistributor registers, which relies on the previous patch propagating SGI enable state to the ITS. Unlike the guest, userspace can clear nASSGIreq while the distributor remains enabled. Queue each SGI as soon as it moves to SW so pending SGIs are not stranded. Userspace writes hold kvm->lock, all vCPU mutexes, and config_lock, so the switch runs in sleepable context with no vCPU running. Return -EIO if vgic_v4_configure_vsgis() fails and marks the VM dead. Fixes: 2291ff2f2a56 ("KVM: arm64: GICv4.1: Plumb SGI implementation selection in the distributor") Signed-off-by: Jia He --- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 37 ++++++++++++++++++++++-------- arch/arm64/kvm/vgic/vgic-v4.c | 12 ++++++++-- arch/arm64/kvm/vgic/vgic.h | 2 +- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v3.c b/arch/arm64/kvm/vgic/vgic-mmio-v3.c index f97bf8fc3a79..66d7dc71d7c7 100644 --- a/arch/arm64/kvm/vgic/vgic-mmio-v3.c +++ b/arch/arm64/kvm/vgic/vgic-mmio-v3.c @@ -115,6 +115,29 @@ static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu, return value; } +/* + * Called with config_lock held after the guest or userspace has written + * GICD_CTLR. If the vSGIs can't be switched, the VM is marked dead. + */ +static int vgic_v3_ctlr_updated(struct kvm *kvm, bool was_enabled, + bool was_hwsgi) +{ + struct vgic_dist *dist = &kvm->arch.vgic; + int ret; + + /* Switching HW SGIs? */ + if (was_hwsgi != dist->nassgireq) { + ret = vgic_v4_configure_vsgis(kvm); + if (ret) + return ret; + } + + if (!was_enabled && dist->enabled) + vgic_queue_pending_irqs(kvm); + + return 0; +} + static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len, unsigned long val) @@ -142,13 +165,9 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu, val |= FIELD_PREP(GICD_CTLR_nASSGIreq, is_hwsgi); } - /* Switching HW SGIs? */ dist->nassgireq = val & GICD_CTLR_nASSGIreq; - if (is_hwsgi != dist->nassgireq) - vgic_v4_configure_vsgis(vcpu->kvm); - - if (!was_enabled && dist->enabled) - vgic_queue_pending_irqs(vcpu->kvm); + /* On failure, the vCPU exits on KVM_REQ_VM_DEAD */ + vgic_v3_ctlr_updated(vcpu->kvm, was_enabled, is_hwsgi); if (vgic_supports_direct_sgis(vcpu->kvm) && was_enabled != dist->enabled) @@ -205,6 +224,7 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu, } case GICD_CTLR: { bool was_enabled = dist->enabled; + bool was_hwsgi = dist->nassgireq; /* Not a GICv4.1? No HW SGIs */ if (!vgic_supports_direct_sgis(vcpu->kvm)) @@ -212,10 +232,7 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu, dist->enabled = val & GICD_CTLR_ENABLE_SS_G1; dist->nassgireq = val & GICD_CTLR_nASSGIreq; - - if (!was_enabled && dist->enabled) - vgic_queue_pending_irqs(vcpu->kvm); - return 0; + return vgic_v3_ctlr_updated(vcpu->kvm, was_enabled, was_hwsgi); } } diff --git a/arch/arm64/kvm/vgic/vgic-v4.c b/arch/arm64/kvm/vgic/vgic-v4.c index b2fd23366aff..d9b3ea783bbd 100644 --- a/arch/arm64/kvm/vgic/vgic-v4.c +++ b/arch/arm64/kvm/vgic/vgic-v4.c @@ -196,6 +196,11 @@ static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu) desc = irq_to_desc(irq->host_irq); irq_domain_deactivate_irq(irq_desc_get_irq_data(desc)); + + /* This is now a SW interrupt, queue it if it is pending */ + vgic_queue_irq_unlock(vcpu->kvm, irq, flags); + vgic_put_irq(vcpu->kvm, irq); + continue; unlock: raw_spin_unlock_irqrestore(&irq->irq_lock, flags); vgic_put_irq(vcpu->kvm, irq); @@ -206,12 +211,12 @@ static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu) return 0; } -void vgic_v4_configure_vsgis(struct kvm *kvm) +int vgic_v4_configure_vsgis(struct kvm *kvm) { struct vgic_dist *dist = &kvm->arch.vgic; struct kvm_vcpu *vcpu; unsigned long i; - int ret; + int ret = 0; lockdep_assert_held(&kvm->arch.config_lock); @@ -225,11 +230,14 @@ void vgic_v4_configure_vsgis(struct kvm *kvm) if (ret) { /* There is no way back from a partial switch */ kvm_vm_dead(kvm); + ret = -EIO; break; } } kvm_arm_resume_guest(kvm); + + return ret; } /* diff --git a/arch/arm64/kvm/vgic/vgic.h b/arch/arm64/kvm/vgic/vgic.h index 595552aef444..3d547ec5a581 100644 --- a/arch/arm64/kvm/vgic/vgic.h +++ b/arch/arm64/kvm/vgic/vgic.h @@ -483,7 +483,7 @@ int vgic_its_invall(struct kvm_vcpu *vcpu); int vgic_v4_init(struct kvm *kvm); void vgic_v4_teardown(struct kvm *kvm); -void vgic_v4_configure_vsgis(struct kvm *kvm); +int vgic_v4_configure_vsgis(struct kvm *kvm); void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val); int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq); -- 2.34.1