[PATCH v2 08/16] KVM: arm64: gic-v5: Add ITS translation cache maintenance
Wei-Lin Chang
weilin.chang at arm.com
Wed Nov 4 14:46:16 PST 2026
On Mon, Nov 02, 2026 at 07:44:46PM +0000, Sascha Bischoff wrote:
> Walking the guest DT and ITTs on every MSI or ITS event is unnecessary
> once a DeviceID/EventID pair has been resolved. Add an XArray indexed
> by the DeviceID/EventID pair to cache resolved translations as a
> disposable acceleration structure, avoiding repeated walks of guest
> memory.
>
> Cache entries hold references to the resolved struct vgic_irq objects
> stored in the VM-wide LPI xarray. Lookups use RCU to access entries in
> interrupt context without taking a raw spinlock, and take an IRQ
> reference before returning an entry to the caller. Cache eviction drops
> the reference held by the cache and defers freeing the entry until RCU
> readers have finished. This keeps the cache tied to the normal LPI
> object lifetime instead of storing raw LPI numbers.
>
> Invalidate cached translations when the table configuration or guest
> invalidation commands make them stale. Changes to DT_BASER or DT_CFGR
> drop the entire cache, while writes of unchanged values leave it intact.
> INV_EVENTR removes either one event or the events covered by an L1 ITT
> entry, and INV_DEVICER removes the translations covered by either an L2
> DT entry or an L1 DT entry.
>
> The cache is not part of any userspace ABI and is never authoritative:
> entries can always be rebuilt from guest memory on the next lookup.
>
> The vgic_irq objects for LPIs track valid translations rather than
> mirror interrupt state. Mirroring that state would require querying
> the GIC on every guest exit. Once direct injection is added, the
> objects also record whether an LPI is mapped into the host ITT.
>
> Signed-off-by: Sascha Bischoff <sascha.bischoff at arm.com>
> ---
> arch/arm64/kvm/vgic/vgic-its-v5.c | 219 ++++++++++++++++++++++++++++--
> include/kvm/arm_vgic.h | 3 +
> 2 files changed, 208 insertions(+), 14 deletions(-)
>
> diff --git a/arch/arm64/kvm/vgic/vgic-its-v5.c b/arch/arm64/kvm/vgic/vgic-its-v5.c
> index caa68c4680d02..9f4783b243221 100644
> --- a/arch/arm64/kvm/vgic/vgic-its-v5.c
> +++ b/arch/arm64/kvm/vgic/vgic-its-v5.c
> @@ -4,6 +4,7 @@
> */
>
> #include <linux/kvm_host.h>
> +#include <linux/rcupdate.h>
> #include <linux/slab.h>
>
> #include <asm/kvm_arm.h>
> @@ -19,6 +20,153 @@
> #define VITS_V5_DEVICE_ID_BITS 16
> #define VITS_V5_EVENT_ID_BITS 16
>
> +struct its_cache_translation_entry {
> + struct rcu_head rcu;
> + struct vgic_irq *irq;
> +};
> +
> +/* Put EventID in the low bits so each device's events form one range. */
> +static unsigned long its_cache_key(u32 device_id, u16 event_id)
> +{
> + return ((unsigned long)device_id << VITS_V5_EVENT_ID_BITS) | event_id;
> +}
> +
> +static void its_cache_drop_translation(struct kvm *kvm,
> + struct its_cache_translation_entry *entry)
> +{
> + struct vgic_irq *irq = entry->irq;
> +
> + vgic_put_irq(kvm, irq);
> + /* An interrupt-context lookup may still be using the erased entry. */
> + kfree_rcu(entry, rcu);
> +}
> +
drop and erase sound very similar, would its_cache_release_entry() make
this clearer?
> +static void its_cache_erase_translation(struct vgic_v5_its *its,
> + unsigned long key)
> +{
> + struct its_cache_translation_entry *entry;
> +
> + entry = xa_erase(&its->translation_cache, key);
> + if (entry)
> + its_cache_drop_translation(its->dev->kvm, entry);
> +}
> +
> +static void its_cache_erase_range(struct vgic_v5_its *its,
> + unsigned long first, unsigned long last)
> +{
> + struct its_cache_translation_entry *entry;
> + unsigned long key;
> +
> + xa_for_each_range(&its->translation_cache, key, entry, first, last)
> + its_cache_erase_translation(its, key);
> +}
> +
> +static void its_cache_erase_span(struct vgic_v5_its *its,
> + unsigned long key, unsigned int bits)
> +{
> + unsigned long mask = (1UL << bits) - 1;
nit: GENMASK(bits - 1, 0) ?
> + unsigned long first = key & ~mask;
> +
> + its_cache_erase_range(its, first, first | mask);
> +}
> +
> +static int its_cache_add_translation(struct kvm *kvm, struct vgic_v5_its *its,
> + u32 device_id, u16 event_id,
> + struct vgic_irq *irq)
> +{
> + unsigned long key = its_cache_key(device_id, event_id);
> + struct its_cache_translation_entry *entry;
> + struct its_cache_translation_entry *old;
> +
> + entry = kzalloc_obj(*entry, GFP_KERNEL_ACCOUNT);
> + if (!entry)
> + return -ENOMEM;
> +
> + entry->irq = irq;
> +
> + old = xa_store(&its->translation_cache, key, entry,
> + GFP_KERNEL_ACCOUNT);
> + if (xa_is_err(old)) {
> + kfree(entry);
> + return xa_err(old);
> + }
> +
> + if (old)
> + its_cache_drop_translation(kvm, old);
> +
> + return 0;
> +}
> +
> +static void its_cache_clear_translations(struct vgic_v5_its *its)
> +{
> + struct its_cache_translation_entry *entry;
> + unsigned long key;
> +
> + xa_for_each(&its->translation_cache, key, entry)
> + its_cache_erase_translation(its, key);
> +}
> +
> +static int its_cache_look_up_translation(struct vgic_v5_its *its,
> + u32 device_id, u16 event_id,
> + struct vgic_irq **irq)
> +{
> + unsigned long key = its_cache_key(device_id, event_id);
> + struct its_cache_translation_entry *entry;
> + int ret = -ENXIO;
> +
> + rcu_read_lock();
> + entry = xa_load(&its->translation_cache, key);
> + if (entry && vgic_try_get_irq_ref(entry->irq)) {
> + *irq = entry->irq;
> + ret = 0;
> + }
> + rcu_read_unlock();
> +
> + return ret;
> +}
> +
> +static int its_cache_l2sz_bits(u8 l2sz)
> +{
> + if (l2sz > GICV5_ITS_DT_ITT_CFGR_L2SZ_64k)
> + return -EINVAL;
> +
> + /* L2SZ 0, 1, and 2 encode 4K, 16K, and 64K tables of 8-byte entries. */
> + return 9 + 2 * l2sz;
> +}
> +
> +static void its_cache_handle_inv_devicer(struct vgic_v5_its *its)
> +{
> + unsigned int bits = its->inv_devicer.event_id_bits;
> + int l2_bits;
> +
> + if (its->inv_devicer.l1 && its->dt_cfgr.structure) {
> + l2_bits = its_cache_l2sz_bits(its->dt_cfgr.l2sz);
> + if (l2_bits < 0)
> + return;
> + /* An L1 DTE covers the L2 DT's devices and all their EventIDs. */
> + bits = l2_bits + VITS_V5_EVENT_ID_BITS;
> + } else if (bits > VITS_V5_EVENT_ID_BITS) {
> + bits = VITS_V5_EVENT_ID_BITS;
> + }
> +
> + its_cache_erase_span(its, its_cache_key(its->didr.device_id, 0), bits);
> +}
> +
> +static void its_cache_handle_inv_eventr(struct vgic_v5_its *its)
> +{
> + int bits = 0;
> +
> + if (its->inv_eventr.l1) {
> + /* An L1 ITTE covers the EventIDs in its L2 ITT. */
> + bits = its_cache_l2sz_bits(its->inv_eventr.itt_l2sz);
> + if (bits < 0)
> + return;
> + }
For the ITT_L2SZ field in the spec there is:
When programming a reserved value or an unsupported value, the ITS
behavior is CONSTRAINED UNPREDICTABLE to any behavior which could be
achieved by programming a valid and supported value.
So I think we should still invalidate when a ITT_L2SZ is programmed to
a reserved value, maybe just arbitrarily pick a defined value when this
happens?
Thanks,
Wei-Lin Chang
> +
> + its_cache_erase_span(its, its_cache_key(its->didr.device_id,
> + its->eidr.event_id), bits);
> +}
> +
> static int parse_linear_dt(struct kvm *kvm, gpa_t dt_base, u32 device_id,
> gpa_t *itt_base, bool *two_level_itt,
> unsigned int *event_id_bits, unsigned int *itt_l2sz)
[...]
More information about the linux-arm-kernel
mailing list