KVM: arm64: vgic-v3: Expose GICR_CTLR.RWP when disabling LPIs
authorMarc Zyngier <maz@kernel.org>
Tue, 5 Apr 2022 18:23:25 +0000 (19:23 +0100)
committerMarc Zyngier <maz@kernel.org>
Wed, 4 May 2022 13:09:53 +0000 (14:09 +0100)
When disabling LPIs, a guest needs to poll GICR_CTLR.RWP in order
to be sure that the write has taken effect. We so far reported it
as 0, as we didn't advertise that LPIs could be turned off the
first place.

Start tracking this state during which LPIs are being disabled,
and expose the 'in progress' state via the RWP bit.

We also take this opportunity to disallow enabling LPIs and programming
GICR_{PEND,PROP}BASER while LPI disabling is in progress, as allowed by
the architecture (UNPRED behaviour).

We don't advertise the feature to the guest yet (which is allowed by
the architecture).

Reviewed-by: Oliver Upton <oupton@google.com>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20220405182327.205520-3-maz@kernel.org
arch/arm64/kvm/vgic/vgic-its.c
arch/arm64/kvm/vgic/vgic-mmio-v3.c
arch/arm64/kvm/vgic/vgic.h
include/kvm/arm_vgic.h

index 2e13402be3bd24654962c3eccb0c0ab56d5cd420..9d11072964fb2a4777dd5f172ac6b69cca190a80 100644 (file)
@@ -683,7 +683,7 @@ int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
        if (!vcpu)
                return E_ITS_INT_UNMAPPED_INTERRUPT;
 
-       if (!vcpu->arch.vgic_cpu.lpis_enabled)
+       if (!vgic_lpis_enabled(vcpu))
                return -EBUSY;
 
        vgic_its_cache_translation(kvm, its, devid, eventid, ite->irq);
index 58e40b4874f86b72935961bebb86b341b92b8d6a..b5ef7e15bb66347b9304b7429260d04f1422d3d1 100644 (file)
@@ -221,6 +221,13 @@ static void vgic_mmio_write_irouter(struct kvm_vcpu *vcpu,
        vgic_put_irq(vcpu->kvm, irq);
 }
 
+bool vgic_lpis_enabled(struct kvm_vcpu *vcpu)
+{
+       struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
+
+       return atomic_read(&vgic_cpu->ctlr) == GICR_CTLR_ENABLE_LPIS;
+}
+
 static unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu,
                                             gpa_t addr, unsigned int len)
 {
@@ -229,26 +236,38 @@ static unsigned long vgic_mmio_read_v3r_ctlr(struct kvm_vcpu *vcpu,
        return vgic_cpu->lpis_enabled ? GICR_CTLR_ENABLE_LPIS : 0;
 }
 
-
 static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,
                                     gpa_t addr, unsigned int len,
                                     unsigned long val)
 {
        struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
-       bool was_enabled = vgic_cpu->lpis_enabled;
+       u32 ctlr;
 
        if (!vgic_has_its(vcpu->kvm))
                return;
 
-       vgic_cpu->lpis_enabled = val & GICR_CTLR_ENABLE_LPIS;
+       if (!(val & GICR_CTLR_ENABLE_LPIS)) {
+               /*
+                * Don't disable if RWP is set, as there already an
+                * ongoing disable. Funky guest...
+                */
+               ctlr = atomic_cmpxchg_acquire(&vgic_cpu->ctlr,
+                                             GICR_CTLR_ENABLE_LPIS,
+                                             GICR_CTLR_RWP);
+               if (ctlr != GICR_CTLR_ENABLE_LPIS)
+                       return;
 
-       if (was_enabled && !vgic_cpu->lpis_enabled) {
                vgic_flush_pending_lpis(vcpu);
                vgic_its_invalidate_cache(vcpu->kvm);
-       }
+               atomic_set_release(&vgic_cpu->ctlr, 0);
+       } else {
+               ctlr = atomic_cmpxchg_acquire(&vgic_cpu->ctlr, 0,
+                                             GICR_CTLR_ENABLE_LPIS);
+               if (ctlr != 0)
+                       return;
 
-       if (!was_enabled && vgic_cpu->lpis_enabled)
                vgic_enable_lpis(vcpu);
+       }
 }
 
 static bool vgic_mmio_vcpu_rdist_is_last(struct kvm_vcpu *vcpu)
@@ -478,11 +497,10 @@ static void vgic_mmio_write_propbase(struct kvm_vcpu *vcpu,
                                     unsigned long val)
 {
        struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
-       struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
        u64 old_propbaser, propbaser;
 
        /* Storing a value with LPIs already enabled is undefined */
-       if (vgic_cpu->lpis_enabled)
+       if (vgic_lpis_enabled(vcpu))
                return;
 
        do {
@@ -513,7 +531,7 @@ static void vgic_mmio_write_pendbase(struct kvm_vcpu *vcpu,
        u64 old_pendbaser, pendbaser;
 
        /* Storing a value with LPIs already enabled is undefined */
-       if (vgic_cpu->lpis_enabled)
+       if (vgic_lpis_enabled(vcpu))
                return;
 
        do {
index 3fd6c86a7ef3418e6a013fa0c54e783f0f7bbb9f..a21e9b602ff2ec3584274f72a53e29f1a1a81efa 100644 (file)
@@ -308,6 +308,7 @@ static inline bool vgic_dist_overlap(struct kvm *kvm, gpa_t base, size_t size)
                (base < d->vgic_dist_base + KVM_VGIC_V3_DIST_SIZE);
 }
 
+bool vgic_lpis_enabled(struct kvm_vcpu *vcpu);
 int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr);
 int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
                         u32 devid, u32 eventid, struct vgic_irq **irq);
index bb30a6803d9f04770490fb2fec39ef58a7c8dde9..fdf1c2c322e53275af9fe1d99644f797c8cfebb4 100644 (file)
@@ -347,8 +347,8 @@ struct vgic_cpu {
 
        /* Contains the attributes and gpa of the LPI pending tables. */
        u64 pendbaser;
-
-       bool lpis_enabled;
+       /* GICR_CTLR.{ENABLE_LPIS,RWP} */
+       atomic_t ctlr;
 
        /* Cache guest priority bits */
        u32 num_pri_bits;