]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: vgic: Rely on RCU protection in vgic_get_lpi()
authorOliver Upton <oliver.upton@linux.dev>
Wed, 21 Feb 2024 05:42:51 +0000 (05:42 +0000)
committerOliver Upton <oliver.upton@linux.dev>
Fri, 23 Feb 2024 21:46:02 +0000 (21:46 +0000)
Stop acquiring the lpi_list_lock in favor of RCU for protecting
the read-side critical section in vgic_get_lpi(). In order for this to
be safe, we also need to be careful not to take a reference on an irq
with a refcount of 0, as it is about to be freed.

Reviewed-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20240221054253.3848076-9-oliver.upton@linux.dev
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
arch/arm64/kvm/vgic/vgic.c
arch/arm64/kvm/vgic/vgic.h

index 3fedc58e663a8704b732f562e2fbd76c9a8cf381..76abf3d946fee39b7c93bb5371664f4386b0030c 100644 (file)
@@ -63,15 +63,14 @@ static struct vgic_irq *vgic_get_lpi(struct kvm *kvm, u32 intid)
 {
        struct vgic_dist *dist = &kvm->arch.vgic;
        struct vgic_irq *irq = NULL;
-       unsigned long flags;
 
-       raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
+       rcu_read_lock();
 
        irq = xa_load(&dist->lpi_xa, intid);
-       if (irq)
-               vgic_get_irq_kref(irq);
+       if (!vgic_try_get_irq_kref(irq))
+               irq = NULL;
 
-       raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
+       rcu_read_unlock();
 
        return irq;
 }
index 8d134569d0a1f6c26f9a4735aa0c89847d9add48..f874b9932c5a6c22fe0b806d242e2ab6c0ecf9bb 100644 (file)
@@ -220,12 +220,20 @@ void vgic_v2_vmcr_sync(struct kvm_vcpu *vcpu);
 void vgic_v2_save_state(struct kvm_vcpu *vcpu);
 void vgic_v2_restore_state(struct kvm_vcpu *vcpu);
 
-static inline void vgic_get_irq_kref(struct vgic_irq *irq)
+static inline bool vgic_try_get_irq_kref(struct vgic_irq *irq)
 {
+       if (!irq)
+               return false;
+
        if (irq->intid < VGIC_MIN_LPI)
-               return;
+               return true;
 
-       kref_get(&irq->refcount);
+       return kref_get_unless_zero(&irq->refcount);
+}
+
+static inline void vgic_get_irq_kref(struct vgic_irq *irq)
+{
+       WARN_ON_ONCE(!vgic_try_get_irq_kref(irq));
 }
 
 void vgic_v3_fold_lr_state(struct kvm_vcpu *vcpu);