Skip to content

Commit 8201d10

Browse files
ouptonMarc Zyngier
authored andcommitted
KVM: arm64: vgic-its: Maintain a translation cache per ITS
Within the context of a single ITS, it is possible to use an xarray to cache the device ID & event ID translation to a particular irq descriptor. Take advantage of this to build a translation cache capable of fitting all valid translations for a given ITS. Signed-off-by: Oliver Upton <oliver.upton@linux.dev> Link: https://lore.kernel.org/r/20240422200158.2606761-9-oliver.upton@linux.dev Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent c09c8ab commit 8201d10

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

arch/arm64/kvm/vgic/vgic-its.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
511511
return 0;
512512
}
513513

514+
static unsigned long vgic_its_cache_key(u32 devid, u32 eventid)
515+
{
516+
return (((unsigned long)devid) << VITS_TYPER_IDBITS) | eventid;
517+
}
518+
514519
static struct vgic_irq *__vgic_its_check_cache(struct vgic_dist *dist,
515520
phys_addr_t db,
516521
u32 devid, u32 eventid)
@@ -564,8 +569,10 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
564569
u32 devid, u32 eventid,
565570
struct vgic_irq *irq)
566571
{
572+
unsigned long cache_key = vgic_its_cache_key(devid, eventid);
567573
struct vgic_dist *dist = &kvm->arch.vgic;
568574
struct vgic_translation_cache_entry *cte;
575+
struct vgic_irq *old;
569576
unsigned long flags;
570577
phys_addr_t db;
571578

@@ -604,6 +611,15 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
604611
* its_lock, as the ITE (and the reference it holds) cannot be freed.
605612
*/
606613
lockdep_assert_held(&its->its_lock);
614+
615+
/*
616+
* Yes, two references are necessary at the moment:
617+
* - One for the global LPI translation cache
618+
* - Another for the translation cache belonging to @its
619+
*
620+
* This will soon disappear.
621+
*/
622+
vgic_get_irq_kref(irq);
607623
vgic_get_irq_kref(irq);
608624

609625
cte->db = db;
@@ -613,6 +629,16 @@ static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
613629

614630
/* Move the new translation to the head of the list */
615631
list_move(&cte->entry, &dist->lpi_translation_cache);
632+
raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
633+
634+
/*
635+
* The per-ITS cache is a perfect cache, so it may already have an
636+
* identical translation even if it were missing from the global
637+
* cache. Ensure we don't leak a reference if that is the case.
638+
*/
639+
old = xa_store(&its->translation_cache, cache_key, irq, GFP_KERNEL_ACCOUNT);
640+
if (old)
641+
vgic_put_irq(kvm, old);
616642

617643
out:
618644
raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
@@ -623,7 +649,8 @@ static void vgic_its_invalidate_cache(struct vgic_its *its)
623649
struct kvm *kvm = its->dev->kvm;
624650
struct vgic_dist *dist = &kvm->arch.vgic;
625651
struct vgic_translation_cache_entry *cte;
626-
unsigned long flags;
652+
unsigned long flags, idx;
653+
struct vgic_irq *irq;
627654

628655
raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
629656

@@ -640,6 +667,11 @@ static void vgic_its_invalidate_cache(struct vgic_its *its)
640667
}
641668

642669
raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
670+
671+
xa_for_each(&its->translation_cache, idx, irq) {
672+
xa_erase(&its->translation_cache, idx);
673+
vgic_put_irq(kvm, irq);
674+
}
643675
}
644676

645677
void vgic_its_invalidate_all_caches(struct kvm *kvm)
@@ -1962,6 +1994,7 @@ static int vgic_its_create(struct kvm_device *dev, u32 type)
19621994

19631995
INIT_LIST_HEAD(&its->device_list);
19641996
INIT_LIST_HEAD(&its->collection_list);
1997+
xa_init(&its->translation_cache);
19651998

19661999
dev->kvm->arch.vgic.msis_require_devid = true;
19672000
dev->kvm->arch.vgic.has_its = true;
@@ -1992,6 +2025,8 @@ static void vgic_its_destroy(struct kvm_device *kvm_dev)
19922025

19932026
vgic_its_free_device_list(kvm, its);
19942027
vgic_its_free_collection_list(kvm, its);
2028+
vgic_its_invalidate_cache(its);
2029+
xa_destroy(&its->translation_cache);
19952030

19962031
mutex_unlock(&its->its_lock);
19972032
kfree(its);

include/kvm/arm_vgic.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ struct vgic_its {
210210
struct mutex its_lock;
211211
struct list_head device_list;
212212
struct list_head collection_list;
213+
214+
/*
215+
* Caches the (device_id, event_id) -> vgic_irq translation for
216+
* LPIs that are mapped and enabled.
217+
*/
218+
struct xarray translation_cache;
213219
};
214220

215221
struct vgic_state_iter;

0 commit comments

Comments
 (0)