KVM: arm64: vgic-its: Make the ITS table save reliable
Fuad Tabba
reviewerv2 cover KVM: arm64: vgic-its: Make the ITS table save reliable
Hi folks,
Changes since v1 [1]:
- Free the LPI translation cache in vgic_mmio_write_its_baser() too, inside the its_lock section. (Eric)
- Reworded patch 1 to name the register the write is to. (Eric)
- Rebased onto kvmarm/next.
Marc pointed out on the v2 ITS series [2] that patch 3 fixed the wrong end of the problem: KVM should drop the collections when the guest changes the table that described them, rather than validating them at save time. He also called that patch's -EINVAL a mistake, since it aborts a save userspace has to be able to issue. This series does both, following his answers [3] to the two open questions: purge the whole list, and skip the offending device rather than fail.
vgic_mmio_write_its_baser() now frees the cached devices or collections whenever the stored register value changes, not only when VALID is cleared, and drops the translation cache with them. It tests for a change rather than a write because the ITS driver rewrites an unchanged GITS_BASER<n> on resume. With that in place the check patch 3 added is unreachable, so it is reverted, and vgic_its_save_device_tables() skips a device the table can no longer address instead of failing the save.
compute_next_devid_offset() now calls vgic_its_check_id(), which reads guest memory for an indirect table, so the save does 2n reads rather than n. It stays linear, as the scan stops at the first reachable successor. Patch 1's Fixes: tag names a v4.15 commit, so it will be picked up for stable. Drop the tag if that is not wanted.
Patches 1 and 3 apply to v7.2 unchanged, but patch 2 reverts a commit only in kvmarm/next, so the series is based there.
Tested on QEMU: the new selftest, vgic_irq, vgic_lpi_stress and boots. Both cases of the new selftest reset and restore the tables they saved, and both fail without the series.
Based on kvmarm/next (aa8e5dc6a7a2a).
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (4):
KVM: arm64: vgic-its: Free the caches when GITS_BASER changes Revert "KVM: arm64: vgic-its: Don't save collections the table cannot
hold"
KVM: arm64: vgic-its: Skip unreachable devices instead of failing the
save
KVM: arm64: selftests: Add ITS table save tests
arch/arm64/kvm/vgic/vgic-its.c | 46 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/vgic_its_save.c | 441 ++++++++++++++++++ 3 files changed, 469 insertions(+), 19 deletions(-) create mode 100644 tools/testing/selftests/kvm/arm64/vgic_its_save.c
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH v2 1/4 KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
A guest that disables the ITS and re-points or shrinks GITS_BASER<n> with VALID still set keeps the devices and collections it mapped against the old table, as KVM frees them only when VALID is cleared. The contents of the table are IMPLEMENTATION DEFINED, so a write that gives GITS_BASER<n> a different address or size may lose whatever the old value described. Free the list whenever the stored value changes, and drop the translation cache with it.
The cache is not empty just because the ITS is disabled: its->enabled is written under the cmd_lock, while vgic_its_resolve_lpi() tests it under the its_lock, so an injection can still cache an entry after the ITS was disabled. Hence the invalidation inside the its_lock section.
Test for a change rather than a write: its_restore_enable() rewrites GITS_BASER<n> from its probe-time cache on resume, and KVM reports GITS_TYPER.HCC as 0, so nothing re-maps the boot CPU's collection afterwards.
--- arch/arm64/kvm/vgic/vgic-its.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +8 -3
@@ -1649,7 +1649,7 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm, unsigned long val) {const struct vgic_its_abi *abi =vgic_its_get_abi (its); -u64 entry_size, table_type; +u64 old, entry_size, table_type;u64 reg, *regptr, clearbits =0 ;/* When GITS_CTLR.Enable is 1, we ignore write accesses. */
@@ -1672,7 +1672,9 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,return ; } - reg =update_64bit_reg (*regptr, addr &7 , len, val); + old = *regptr; + + reg =update_64bit_reg (old, addr &7 , len, val); reg &= ~GITS_BASER_RO_MASK ; reg &= ~clearbits;
@@ -1682,7 +1684,8 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm, *regptr = reg; -if (!(reg &GITS_BASER_VALID )) { +/* The ITS driver rewrites an unchanged GITS_BASER<n> on resume. */ +if (reg != old) {/* Take the its_lock to prevent a race with a save/restore */ mutex_lock (&its->its_lock);switch (table_type) {
@@ -1693,6 +1696,8 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,vgic_its_free_collection_list (kvm, its);break ; } +/* A concurrent injection may have cached a translation. */ +vgic_its_invalidate_cache (its);mutex_unlock (&its->its_lock); } }
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Pre-existing race condition in ITS MMIO handler allows MSI injection while ITS is disabled due to a stale translation cache entry. --
--- Patch [1]: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes --- commit 98c89973da07ed23e9bd8348bfd2cf61333c4e66
Author: Fuad Tabba <[email protected]>
KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
A guest that disables the ITS and changes GITS_BASER<n> with VALID still set keeps the mapped devices and collections. This patch frees the list and drops the translation cache whenever the stored value changes.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1709
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-its.c +2
-0@@ -1702,6 +1705,8 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,vgic_its_free_collection_list (kvm, its);break ; } +/* A concurrent injection may have cached a translation. */ +vgic_its_invalidate_cache (its);
[Severity: High]
This is a pre-existing issue, but the commit message explicitly notes that an injection can still cache an entry after the ITS is disabled.
Is it possible for a subsequent MSI to trigger an unexpected LPI injection?
If Thread 1 (e.g. irqfd) calls vgic_its_inject_msi(), takes its_lock, and enters vgic_its_resolve_lpi() in arch/arm64/kvm/vgic/vgic-its.c, it reads its->enabled as true.
If Thread 2 (guest vCPU) concurrently disables the ITS via vgic_mmio_write_its_ctlr(), it acquires cmd_lock (but not its_lock), sets its->enabled to false, and clears the cache.
Thread 1 could then resume, finish resolving the LPI, and call vgic_its_cache_translation() to insert the entry into the cache, ignoring the fact that its->enabled is now false.
A subsequent MSI would then hit vgic_its_inject_cached_translation(), which does a fast-path lookup without checking its->enabled, successfully injecting the MSI.
Could this violate the GICv3 specification by delivering LPIs while the ITS is explicitly disabled, potentially crashing a guest OS during kexec?
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0mutex_unlock (&its->its_lock); } }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Fuad Tabba
reviewerRe: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
Sashiko, this one is real.
Quoted 37 lines · On Fri, 21 Aug 2026 at 07:57, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Pre-existing race condition in ITS MMIO handler allows MSI injection while ITS is disabled due to a stale translation cache entry. ----- Patch [1]: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when
GITS_BASERchanges --- commit98c89973da07ed23e9bd8348bfd2cf61333c4e66
Author: Fuad Tabba <[email protected]>KVM: arm64: vgic-its: Free the caches when
GITS_BASERchangesA guest that disables the ITS and changes GITS_BASER<n> with VALID still set keeps the mapped devices and collections. This patch frees the list and drops the translation cache whenever the stored value changes.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0[ ... ]
arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1709
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-its.c +2
-0@@ -1702,6 +1705,8 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,vgic_its_free_collection_list (kvm, its);break ; } +/* A concurrent injection may have cached a translation. */ +vgic_its_invalidate_cache (its);[Severity: High]
This is a pre-existing issue, but the commit message explicitly notes that an injection can still cache an entry after the ITS is disabled.
Quoted 1 line
Is it possible for a subsequent MSI to trigger an unexpected LPI injection?
Yes, and it violates the spec: with GITS_CTLR.Enabled == 0 writes to GITS_TRANSLATER are ignored, so nothing should be injected.
It was reported in June [1] and is being worked on.
Cheers,
/fuad
[1] https://lore.kernel.org/all/ah6Lsi4MfKUU6wBR@v4bel/
Quoted 26 lines
If Thread 1 (e.g. irqfd) calls
vgic_its_inject_msi(), takesits_lock, and entersvgic_its_resolve_lpi()inarch/arm64/kvm/vgic/vgic-its.c, it readsits->enabledas true.If Thread 2 (guest vCPU) concurrently disables the ITS via
vgic_mmio_write_its_ctlr(), it acquirescmd_lock(but notits_lock), setsits->enabledto false, and clears the cache.Thread 1 could then resume, finish resolving the LPI, and call
vgic_its_cache_translation()to insert the entry into the cache, ignoring the fact thatits->enabledis now false.A subsequent MSI would then hit
vgic_its_inject_cached_translation(), which does a fast-path lookup without checkingits->enabled, successfully injecting the MSI.Could this violate the GICv3 specification by delivering LPIs while the ITS is explicitly disabled, potentially crashing a guest OS during kexec?
arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1703
Quoted 3 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0mutex_unlock (&its->its_lock); } }--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Marc Zyngier
maintainerRe: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
Quoted 41 lines · Fuad Tabba
Sashiko, this one is real.
Quoted 37 lines · On Fri, 21 Aug 2026 at 07:57, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Pre-existing race condition in ITS MMIO handler allows MSI injection while ITS is disabled due to a stale translation cache entry. ----- Patch [1]: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when
GITS_BASERchanges --- commit98c89973da07ed23e9bd8348bfd2cf61333c4e66
Author: Fuad Tabba <[email protected]>KVM: arm64: vgic-its: Free the caches when
GITS_BASERchangesA guest that disables the ITS and changes GITS_BASER<n> with VALID still set keeps the mapped devices and collections. This patch frees the list and drops the translation cache whenever the stored value changes.
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0[ ... ]
arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1709
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-its.c +2
-0@@ -1702,6 +1705,8 @@ static void vgic_mmio_write_its_baser(struct kvm *kvm,vgic_its_free_collection_list (kvm, its);break ; } +/* A concurrent injection may have cached a translation. */ +vgic_its_invalidate_cache (its);[Severity: High]
This is a pre-existing issue, but the commit message explicitly notes that an injection can still cache an entry after the ITS is disabled.
Quoted 6 lines
Quoted 1 line
Is it possible for a subsequent MSI to trigger an unexpected LPI injection?
Yes, and it violates the spec: with GITS_CTLR.Enabled == 0 writes to
GITS_TRANSLATERare ignored, so nothing should be injected.It was reported in June [1] and is being worked on.
Is it? I'm worried this has stalled. I'll have a look...
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Fuad Tabba
reviewerRe: [PATCH v2 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
On Fri, 21 Aug 2026 at 09:50, Marc Zyngier <[email protected]> wrote:
...
Quoted 6 lines
Quoted 4 lines
Yes, and it violates the spec: with GITS_CTLR.Enabled == 0 writes to
GITS_TRANSLATERare ignored, so nothing should be injected.It was reported in June [1] and is being worked on.
Is it? I'm worried this has stalled. I'll have a look...
I actually noticed it the other day and way planning on picking it up later, since it has stalled, as you said. Trying to keep the fixes by theme...
/fuad
Quoted 5 lines
M.--
Without deviation from the norm, progress is not possible.
Fuad Tabba
reviewerPATCH v2 2/4 Revert "KVM: arm64: vgic-its: Don't save collections the table cannot hold"
In reply to Fuad TabbaThis reverts commit 9b10fb74e4b661543d188701bd4d024fc5c18f58.
Freeing the collections when GITS_BASER<coll> changes removes the state this check rejected: vgic_its_cmd_handle_mapi(), vgic_its_cmd_handle_mapc() and vgic_its_restore_cte() all validate the ID against the current table before allocating, and the table can no longer change under the list. What remains is a collection whose entry is not backed by a memslot, which the write fails on anyway, so the check costs a save userspace should be able to issue reliably and buys nothing.
The reverted commit credited the check with bounding the walk as well. It stays bounded without it: collection IDs are unique and each is below the table's capacity, so the list cannot be longer than the table.
--- arch/arm64/kvm/vgic/vgic-its.c | 3 --- 1 file changed, 3 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +0 -3
@@ -2537,9 +2537,6 @@ static int vgic_its_save_collection_table(struct vgic_its *its) max_size =GITS_BASER_NR_PAGES (baser) *SZ_64K ;list_for_each_entry (collection, &its->collection_list, coll_list) { -if (!vgic_its_check_id (its, baser, collection->collection_id,NULL )) -return -EINVAL ; - ret =vgic_its_save_cte (its, collection, gpa);if (ret)return ret;
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH v2 3/4 KVM: arm64: vgic-its: Skip unreachable devices instead of failing the save
In reply to Fuad Tabbavgic_its_save_device_tables() aborts with -EINVAL when a device's entry falls outside the device table, which a guest can arrange on its own: an indirect table lets it clear an L1 entry's valid bit without touching GITS_BASER. That fails a save userspace should be able to issue reliably.
Skip the device instead, and point the saved DTE chain past it, as commit ad1e686e2378d ("KVM: arm64: vgic-its: Point saved ITEs at the next valid entry") does for ITEs. compute_next_devid_offset() takes the next device off the list whether or not it was saved, so the predecessor would otherwise point at an entry the save never wrote. Restore follows that offset while it stays inside the table being scanned: within an L2 block, or anywhere in a flat table. Both need userspace to remove a memslot under the table, since dropping an L1 entry takes the whole block with it and scan_its_table() stops at the block boundary.
--- arch/arm64/kvm/vgic/vgic-its.c | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +19 -13
@@ -2015,18 +2015,22 @@ static int vgic_its_attr_regs_access(struct kvm_device *dev,return ret; } -static u32 compute_next_devid_offset (struct list_head *h, +static u32 compute_next_devid_offset (struct vgic_its *its,u64 baser,struct its_device *dev) { -struct its_device *next; -u32 next_offset; +struct its_device *next = dev; -if (list_is_last (&dev->dev_list, h)) -return 0 ; - next =list_next_entry (dev, dev_list); - next_offset = next->device_id - dev->device_id; +/* +* Point at the next device vgic_its_save_device_tables() saves. It +* sorts device_list first, so the subtraction cannot underflow. +*/ +list_for_each_entry_continue (next, &its->device_list, dev_list) { +if (vgic_its_check_id (its, baser, next->device_id,NULL )) +return min_t (u32, next->device_id - dev->device_id, +VITS_DTE_MAX_DEVID_OFFSET ); + } -return min_t (u32, next_offset,VITS_DTE_MAX_DEVID_OFFSET ); +return 0 ; }static u32 compute_next_eventid_offset (struct list_head *h,struct its_ite *ite)
@@ -2267,17 +2271,18 @@ static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)* vgic_its_save_dte - Save a device table entry at a given GPA * * @its: ITS handle +* @baser: GITS_BASER<dev> the caller is saving against * @dev: ITS device * @ptr: GPA */ -static int vgic_its_save_dte (struct vgic_its *its,struct its_device *dev, -gpa_t ptr) +static int vgic_its_save_dte (struct vgic_its *its,u64 baser, +struct its_device *dev,gpa_t ptr) {u64 val, itt_addr_field;u32 next_offset; itt_addr_field = dev->itt_addr >>8 ; - next_offset =compute_next_devid_offset (&its->device_list, dev); + next_offset =compute_next_devid_offset (its, baser, dev); val = (1ULL << KVM_ITS_DTE_VALID_SHIFT | ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) | (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
@@ -2376,15 +2381,16 @@ static int vgic_its_save_device_tables(struct vgic_its *its)int ret;gpa_t eaddr; +/* Don't fail a save that userspace must be able to issue. */ if (!vgic_its_check_id (its, baser, dev->device_id, &eaddr)) -return -EINVAL ; +continue ; ret =vgic_its_save_itt (its, dev);if (ret)return ret; - ret =vgic_its_save_dte (its, dev, eaddr); + ret =vgic_its_save_dte (its, baser, dev, eaddr);if (ret)return ret; }
Signature · 1 line
2.39.5
Fuad Tabba
reviewerPATCH v2 4/4 KVM: arm64: selftests: Add ITS table save tests
In reply to Fuad TabbaCover the two ways a guest can leave a table that KVM_DEV_ARM_ITS_SAVE_TABLES has to cope with: a GITS_BASER<coll> write that changes the table, which drops the collections it described, and a device whose L2 block the guest invalidated, which the save skips. Each case then resets and restores, which is what the save exists for.
Both fail without the preceding patches. The first save returns -EINVAL, and the second saves a DTE pointing 8192 entries ahead, at an entry the save never wrote.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/vgic_its_save.c | 441 ++++++++++++++++++ 2 files changed, 442 insertions(+) create mode 100644 tools/testing/selftests/kvm/arm64/vgic_its_save.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -184,6 +184,7 @@ TEST_GEN_PROGS_arm64 += arm64/stage2_block_transitions TEST_GEN_PROGS_arm64 += arm64/vcpu_width_config TEST_GEN_PROGS_arm64 += arm64/vgic_init TEST_GEN_PROGS_arm64 += arm64/vgic_irq +TEST_GEN_PROGS_arm64 += arm64/vgic_its_save TEST_GEN_PROGS_arm64 += arm64/vgic_lpi_stress TEST_GEN_PROGS_arm64 += arm64/vgic_v5 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +441 -0
441 lines not on this page · raw message
Signature · 1 line
2.39.5
[email protected]
AI reviewRe: [PATCH v2 4/4] KVM: arm64: selftests: Add ITS table save tests
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Unsafe DMA Buffer Configuration (ITT Size Miscalculation) - [Medium] Incorrect Endianness…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Unsafe DMA Buffer Configuration (ITT Size Miscalculation)
- [Medium] Incorrect Endianness Conversion for L1 Device Table
--
--- Patch [4]: [PATCH v2 4/4] KVM: arm64: selftests: Add ITS table save tests --- commit 00c3ff8ee303e906c6d0f346f17949dbb0abf5fc
Author: Fuad Tabba <[email protected]>
Subject: KVM: arm64: selftests: Add ITS table save tests
This commit adds selftests for KVM_DEV_ARM_ITS_SAVE_TABLES to cover cases where a guest alters tables. It tests handling of a GITS_BASER<coll> write that changes the table, and a device whose L2 block the guest invalidated. Each case is then reset and restored to verify the save functionality.
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +0
-0@@ -0,0 +1,441 @@
[ ... ]
tools/testing/selftests/kvm/arm64/vgic_its_save.c line 69
Quoted 7 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +7
-0+/* +* its_send_mapd_cmd() encodes ilog2(itt_size) - 1 as num_eventid_bits, and +* vgic_its_restore_itt() scans BIT_ULL(num_eventid_bits) * ESZ, so the size +* handed to MAPD has to match the ITT allocated for it. +*/ +#define ITT_EVENTID_BITS 13 +#define ITT_MAPD_SIZE BIT_ULL(ITT_EVENTID_BITS + 1)
[Severity: Medium]
Does this calculation configure the hardware with an ITT size of 128KB instead of the intended 64KB?
The test calculates ITT_MAPD_SIZE as BIT_ULL(ITT_EVENTID_BITS + 1) (evaluating to 16,384) to compensate for its_send_mapd_cmd() subtracting 1. However, KVM's its_cmd_get_size() adds 1 back, resulting in 14 bits of event IDs.
During restore, KVM computes the scan size as BIT_ULL(14) * 8 = 128KB, causing it to read 64KB past the end of the allocated ITT memory.
Quoted 1 line
+#define
ITT_SZ(BIT_ULL(ITT_EVENTID_BITS)* ESZ)
[ ... ]
Quoted 3 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +3
-0+static void guest_unreachable_device (void ) +{ +u64 *l1;
[ ... ]
tools/testing/selftests/kvm/arm64/vgic_its_save.c line 186
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +6
-0+guest_make_device_table_indirect (); + +/* Both L2 blocks present, so both MAPDs are in range. */ + l1 = test_data.device_l1_va; + l1[0 ] =L1E_VALID_MASK | (test_data.device_l2[0 ] &L1E_ADDR_MASK ); + l1[1 ] =L1E_VALID_MASK | (test_data.device_l2[1 ] &L1E_ADDR_MASK );
[Severity: Medium]
Will this fail on big-endian hosts? The L1 entries are being populated using native CPU endianness.
Since the ARM GICv3 ITS architecture and KVM emulator require these tables to be little-endian (using le64_to_cpu on read), shouldn't these use htole64() when writing them?
tools/testing/selftests/kvm/arm64/vgic_its_save.c line 191
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +5
-0+ +its_send_mapd_cmd (test_data.cmdq_base_va,DEVICE_A_ID , + test_data.itt_tables,ITT_MAPD_SIZE , true); +its_send_mapd_cmd (test_data.cmdq_base_va,DEVICE_B_ID , + test_data.itt_tables +ITT_SZ ,ITT_MAPD_SIZE , true);
[Severity: Medium]
This isn't a separate bug, but as a result of the ITT_MAPD_SIZE miscalculation above, doesn't this send MAPD commands configuring overlapping, out-of-bounds ITT tables for the two devices?
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +10
-0+ +/* +* Drop the block holding device B. No ITS command and no GITS_BASER +* write is involved, so nothing tells KVM the device is now +* unreachable. +*/ + l1[1 ] =0 ; + +GUEST_DONE (); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Fuad Tabba
reviewerRe: [PATCH v2 4/4] KVM: arm64: selftests: Add ITS table save tests
Quoted 3 lines · On Fri, 21 Aug 2026 at 08:00, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Unsafe DMA Buffer Configuration (ITT Size Miscalculation)
The oversized scan stays inside the memory the test allocated for the two ITTs, and both cases pass. If the series needs a respin I'll tidy this up, but I won't respin just for this.
Quoted 1 line
- [Medium] Incorrect Endianness Conversion for L1 Device Table
CPU_BIG_ENDIAN depends on BROKEN on arm64, being deprecated [1]
/fuad
[1] https://lore.kernel.org/all/[email protected]/
Quoted 83 lines
--
--- Patch [4]: [PATCH v2 4/4] KVM: arm64: selftests: Add ITS table save tests --- commit
00c3ff8ee303e906c6d0f346f17949dbb0abf5fc
Author: Fuad Tabba <[email protected]>
Subject: KVM: arm64: selftests: Add ITS table save testsThis commit adds selftests for
KVM_DEV_ARM_ITS_SAVE_TABLESto cover cases where a guest alters tables. It tests handling of a GITS_BASER<coll> write that changes the table, and a device whose L2 block the guest invalidated. Each case is then reset and restored to verify the save functionality.Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +0
-0@@ -0,0 +1,441 @@[ ... ]
tools/testing/selftests/kvm/arm64/vgic_its_save.c line 69
Quoted 7 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +7
-0+/* +* its_send_mapd_cmd() encodes ilog2(itt_size) - 1 as num_eventid_bits, and +* vgic_its_restore_itt() scans BIT_ULL(num_eventid_bits) * ESZ, so the size +* handed to MAPD has to match the ITT allocated for it. +*/ +#define ITT_EVENTID_BITS 13 +#define ITT_MAPD_SIZE BIT_ULL(ITT_EVENTID_BITS + 1)[Severity: Medium]
Does this calculation configure the hardware with an ITT size of 128KB instead of the intended 64KB?The test calculates
ITT_MAPD_SIZEasBIT_ULL(ITT_EVENTID_BITS + 1)(evaluating to 16,384) to compensate forits_send_mapd_cmd()subtracting 1. However, KVM'sits_cmd_get_size()adds 1 back, resulting in 14 bits of event IDs.During restore, KVM computes the scan size as
BIT_ULL(14)* 8 = 128KB, causing it to read 64KB past the end of the allocated ITT memory.Quoted 1 line
+#define
ITT_SZ(BIT_ULL(ITT_EVENTID_BITS)* ESZ)[ ... ]
Quoted 3 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +3
-0+static void guest_unreachable_device (void ) +{ +u64 *l1;[ ... ]
tools/testing/selftests/kvm/arm64/vgic_its_save.c line 186
Quoted 6 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +6
-0+guest_make_device_table_indirect (); + +/* Both L2 blocks present, so both MAPDs are in range. */ + l1 = test_data.device_l1_va; + l1[0 ] =L1E_VALID_MASK | (test_data.device_l2[0 ] &L1E_ADDR_MASK ); + l1[1 ] =L1E_VALID_MASK | (test_data.device_l2[1 ] &L1E_ADDR_MASK );[Severity: Medium]
Will this fail on big-endian hosts? The L1 entries are being populated using native CPU endianness.Since the ARM GICv3 ITS architecture and KVM emulator require these tables to be little-endian (using
le64_to_cpuon read), shouldn't these usehtole64()when writing them?tools/testing/selftests/kvm/arm64/vgic_its_save.c line 191
Quoted 5 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +5
-0+ +its_send_mapd_cmd (test_data.cmdq_base_va,DEVICE_A_ID , + test_data.itt_tables,ITT_MAPD_SIZE , true); +its_send_mapd_cmd (test_data.cmdq_base_va,DEVICE_B_ID , + test_data.itt_tables +ITT_SZ ,ITT_MAPD_SIZE , true);[Severity: Medium]
This isn't a separate bug, but as a result of theITT_MAPD_SIZEmiscalculation above, doesn't this send MAPD commands configuring overlapping, out-of-bounds ITT tables for the two devices?tools/testing/selftests/kvm/arm64/vgic_its_save.c line 201
Quoted 10 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +10
-0+ +/* +* Drop the block holding device B. No ITS command and no GITS_BASER +* write is involved, so nothing tells KVM the device is now +* unreachable. +*/ + l1[1 ] =0 ; + +GUEST_DONE (); +}--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Quoted 31 lines · Fuad Tabba
Hi folks,
Changes since v1 [1]:
- Free the LPI translation cache invgic_mmio_write_its_baser()too, inside theits_locksection. (Eric)
- Reworded patch 1 to name the register the write is to. (Eric)
- Rebased onto kvmarm/next.Marc pointed out on the v2 ITS series [2] that patch 3 fixed the wrong end of the problem: KVM should drop the collections when the guest changes the table that described them, rather than validating them at save time. He also called that patch's
-EINVALa mistake, since it aborts a save userspace has to be able to issue. This series does both, following his answers [3] to the two open questions: purge the whole list, and skip the offending device rather than fail.
vgic_mmio_write_its_baser()now frees the cached devices or collections whenever the stored register value changes, not only when VALID is cleared, and drops the translation cache with them. It tests for a change rather than a write because the ITS driver rewrites an unchanged GITS_BASER<n> on resume. With that in place the check patch 3 added is unreachable, so it is reverted, andvgic_its_save_device_tables()skips a device the table can no longer address instead of failing the save.
compute_next_devid_offset()now callsvgic_its_check_id(), which reads guest memory for an indirect table, so the save does 2n reads rather than n. It stays linear, as the scan stops at the first reachable successor. Patch 1's Fixes: tag names a v4.15 commit, so it will be picked up for stable. Drop the tag if that is not wanted.
Quoted 8 lines
Patches 1 and 3 apply to v7.2 unchanged, but patch 2 reverts a commit only in kvmarm/next, so the series is based there.
Tested on QEMU: the new selftest,
vgic_irq,vgic_lpi_stressand boots. Both cases of the new selftest reset and restore the tables they saved, and both fail without the series.Based on kvmarm/next (
aa8e5dc6a7a2a).
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 14 lines · Fuad Tabba
Changes since v1 [1]:
- Free the LPI translation cache invgic_mmio_write_its_baser()too, inside theits_locksection. (Eric)
- Reworded patch 1 to name the register the write is to. (Eric)
- Rebased onto kvmarm/next.Marc pointed out on the v2 ITS series [2] that patch 3 fixed the wrong end of the problem: KVM should drop the collections when the guest changes the table that described them, rather than validating them at save time. He also called that patch's
-EINVALa mistake, since it aborts a save userspace has to be able to issue. This series does both, following his answers [3] to the two open questions: purge the whole list, and skip the offending device rather than fail.
Quoted 1 line
[...]
Applied to fixes, thanks!
[1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes
https://git.kernel.org/kvmarm/kvmarm/c/8cd92f77ae4f [2/4] Revert "KVM: arm64: vgic-its: Don't save collections the table cannot hold" https://git.kernel.org/kvmarm/kvmarm/c/30908e727247 [3/4] KVM: arm64: vgic-its: Skip unreachable devices instead of failing the save https://git.kernel.org/kvmarm/kvmarm/c/cc5d96036e01 [4/4] KVM: arm64: selftests: Add ITS table save tests https://git.kernel.org/kvmarm/kvmarm/c/6f182db39fb0
--
Best,
Oliver