Skip to contentSkip to conversation

KVM: arm64: vgic-its: Make the ITS table save reliable

PATCH Superseded Fuad Tabba 4 participants 11 messages Aug 19 – Aug 20

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: arm64: vgic-its: Make the ITS table save reliable

Hi folks,

Marc pointed out on the v2 ITS series [1] 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 [2] 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. 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.

Note that 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 (57e7cf13ac26b).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] 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                |  44 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/vgic_its_save.c       | 441 ++++++++++++++++++
 3 files changed, 467 insertions(+), 19 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/vgic_its_save.c

base-commit: 57e7cf13ac26bf1a3dba6cfa601f7b2481811575

Signature · 1 line
2.39.5
author reviewer
#1 lore raw

PATCH 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 table format is not architected, so a write with a different value is allowed to lose what it describes. Free the list whenever the stored value changes.

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.

Fixes 36d6961c2b481 ("KVM: arm/arm64: vgic-its: Free caches when GITS_BASER Valid bit is cleared")
---
 arch/arm64/kvm/vgic/vgic-its.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +6 -3
index f6538b1976f9b..3339d9977af27 100644
@@ -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) {
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes

Hi Fuad,

Quoted 5 lines · Fuad Tabba

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 table format is not architected, so a write with a different value is allowed to lose what it describes. Free the list whenever the

I don't really get "a write with a different value is allowed to lose what it describes". A write at which place, in the collection table?

Quoted 40 lines

stored value changes.

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.

Fixes 36d6961c2b481 ("KVM: arm/arm64: vgic-its: Free caches when GITS_BASER Valid bit is cleared")
---
 arch/arm64/kvm/vgic/vgic-its.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +4 -2
index f6538b1976f9b..3339d9977af27 100644
@@ -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;
 

arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1691

Quoted 9 lines
arch/arm64/kvm/vgic/vgic-its.c +2 -1
@@ -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) {

One question: There is no vgic_its_invalidate_cache() in the function. Is it OK?

Besides out of curiosity, why don't we go further and remove ite entries that refer to removed collections in vgic_its_free_collection()?

Thanks

Eric

maintainer
#3 lore raw

Re: [PATCH 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes

Hi Eric,

Quoted 11 lines · Eric Auger

Hi Fuad,

Quoted 5 lines · Fuad Tabba

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 table format is not architected, so a write with a different value is allowed to lose what it describes. Free the list whenever the

I don't really get "a write with a different value is allowed to lose what it describes". A write at which place, in the collection table?

A write to the GITS_BASERn register describing the pointer to the collection table.

The additional clarification is that because the *content* of the table is IMPDEF, if you point the ITS to a different location or size in memory, then there is no guarantee that the caches (the KVM internal data structures) are up to date. In this case, the proposed course of action is to invalidate the caches and start afresh.

Quoted 43 lines
Quoted 43 lines

stored value changes.

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.

Fixes 36d6961c2b481 ("KVM: arm/arm64: vgic-its: Free caches when GITS_BASER Valid bit is cleared")
---
 arch/arm64/kvm/vgic/vgic-its.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +4 -2
index f6538b1976f9b..3339d9977af27 100644
@@ -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;
 
Quoted 8 lines

arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1691

Quoted 6 lines
arch/arm64/kvm/vgic/vgic-its.c +2 -1
-	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) {

One question: There is no vgic_its_invalidate_cache() in the function. Is it OK?

Probably not. We should make sure that the translation cache is gone as well so that we retranslate and avoid signalling LPIs that have undergone such invalidation. Thanks for spotting this.

Quoted 2 lines

Besides out of curiosity, why don't we go further and remove ite entries that refer to removed collections in vgic_its_free_collection()?

The current policy is to keep the LPI alive as long as it is mapped. The only thing is that we can't signal it, obviously. But it would be legal to drop them altogether, only more work.

An additional question is whether we should consider doing a reload of the collection table or not. I'm not keen on it, but I can also see how a guest could want to do this. Feels a bit over the top though.

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#4 lore raw

Re: [PATCH 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes

Hi Eric, Marc,

Quoted 17 lines · Marc Zyngier

Hi Eric,

Quoted 11 lines · Eric Auger

Hi Fuad,

Quoted 5 lines · Fuad Tabba

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 table format is not architected, so a write with a different value is allowed to lose what it describes. Free the list whenever the

I don't really get "a write with a different value is allowed to lose what it describes". A write at which place, in the collection table?

Quoted 8 lines

A write to the GITS_BASERn register describing the pointer to the collection table.

The additional clarification is that because the *content* of the table is IMPDEF, if you point the ITS to a different location or size in memory, then there is no guarantee that the caches (the KVM internal data structures) are up to date. In this case, the proposed course of action is to invalidate the caches and start afresh.

My sentence should have said what the write was to. I'll spell it out in v2 along the lines of your reply.

...

Quoted 6 lines
Quoted 2 lines

One question: There is no vgic_its_invalidate_cache() in the function. Is it OK?

Probably not. We should make sure that the translation cache is gone as well so that we retranslate and avoid signalling LPIs that have undergone such invalidation. Thanks for spotting this.

I'll add it in v2. The device branch already gets it through vgic_its_free_device(), but the collection branch doesn't.

Thank you both,
/fuad

Quoted 17 lines
Quoted 2 lines

Besides out of curiosity, why don't we go further and remove ite entries that refer to removed collections in vgic_its_free_collection()?

The current policy is to keep the LPI alive as long as it is mapped. The only thing is that we can't signal it, obviously. But it would be legal to drop them altogether, only more work.

An additional question is whether we should consider doing a reload of the collection table or not. I'm not keen on it, but I can also see how a guest could want to do this. Feels a bit over the top though.

Thanks,

        M.

--
Jazz isn't dead. It just smells funny.

#5 lore raw

Re: [PATCH 1/4] KVM: arm64: vgic-its: Free the caches when GITS_BASER changes

In reply to Marc Zyngier
Quoted 16 lines · Marc Zyngier

Hi Eric,

Quoted 11 lines · Eric Auger

Hi Fuad,

Quoted 5 lines · Fuad Tabba

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 table format is not architected, so a write with a different value is allowed to lose what it describes. Free the list whenever the

I don't really get "a write with a different value is allowed to lose what it describes". A write at which place, in the collection table?

Quoted 8 lines

A write to the GITS_BASERn register describing the pointer to the collection table.

The additional clarification is that because the *content* of the table is IMPDEF, if you point the ITS to a different location or size in memory, then there is no guarantee that the caches (the KVM internal data structures) are up to date. In this case, the proposed course of action is to invalidate the caches and start afresh.

OK thanks, this definitively clarifies the above sentence.

Quoted 57 lines
Quoted 44 lines
Quoted 44 lines

stored value changes.

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.

Fixes 36d6961c2b481 ("KVM: arm/arm64: vgic-its: Free caches when GITS_BASER Valid bit is cleared")
---
 arch/arm64/kvm/vgic/vgic-its.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +4 -2
index f6538b1976f9b..3339d9977af27 100644
@@ -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;
 
Quoted 8 lines

arch/arm64/kvm/vgic/vgic-its.c static void vgic_mmio_write_its_baser(struct kvm *kvm, · line 1691

Quoted 6 lines
arch/arm64/kvm/vgic/vgic-its.c +2 -1
-	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) {

One question: There is no vgic_its_invalidate_cache() in the function. Is it OK?

Probably not. We should make sure that the translation cache is gone as well so that we retranslate and avoid signalling LPIs that have undergone such invalidation. Thanks for spotting this.

Quoted 6 lines
Quoted 2 lines

Besides out of curiosity, why don't we go further and remove ite entries that refer to removed collections in vgic_its_free_collection()?

The current policy is to keep the LPI alive as long as it is mapped. The only thing is that we can't signal it, obviously. But it would be legal to drop them altogether, only more work.

OK

Quoted 4 lines

An additional question is whether we should consider doing a reload of the collection table or not. I'm not keen on it, but I can also see how a guest could want to do this. Feels a bit over the top though.

Agreed.

Thanks

Eric

Quoted 5 lines

Thanks,

	M.
author reviewer
#6 lore raw

PATCH 2/4 Revert "KVM: arm64: vgic-its: Don't save collections the table cannot hold"

In reply to Fuad Tabba

This 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
index 3339d9977af27..cc6ea5c3409ae 100644
@@ -2535,9 +2535,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
author reviewer
#7 lore raw

PATCH 3/4 KVM: arm64: vgic-its: Skip unreachable devices instead of failing the save

In reply to Fuad Tabba

vgic_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.

Fixes 57a9a117154c9 ("KVM: arm64: vgic-its: Device table save/restore")
---
 arch/arm64/kvm/vgic/vgic-its.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +19 -13
index cc6ea5c3409ae..717fbd563fb01 100644
@@ -2013,18 +2013,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)
@@ -2265,17 +2269,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) |
@@ -2374,15 +2379,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
author reviewer
#8 lore raw

PATCH 4/4 KVM: arm64: selftests: Add ITS table save tests

In reply to Fuad Tabba

Cover 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.

Assisted-by Antigravity:gemini-3.1-pro
---
 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
index 5f8d17ee5f677..67a18e6d13177 100644
@@ -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
#9 lore raw

Re: [PATCH 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] The guest code writes L1 device table entries using native endianness instead of little-endian…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The guest code writes L1 device table entries using native endianness instead of little-endian, which will cause corrupted data on big-endian guests.
- [Medium] The guest code modifies GITS_BASER immediately after clearing GITS_CTLR.Enable without polling for the GITS_CTLR.Quiescent bit. --

--- Patch [4]: [PATCH 4/4] KVM: arm64: selftests: Add ITS table save tests --- commit 277abfaff8f849d670b2c74f45ab3607f657788b
Author: Fuad Tabba <[email protected]>

KVM: arm64: selftests: Add ITS table save tests

This commit adds selftests for KVM_DEV_ARM_ITS_SAVE_TABLES to handle two cases where a guest leaves a table in a modified state. It covers a GITS_BASER write that drops described collections, and an invalidated L2 block that causes a device to be skipped.

Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +0 -0
new file mode 100644

[ ... ]

tools/testing/selftests/kvm/arm64/vgic_its_save.c line 130

Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +8 -0
+static void guest_shrink_coll_table(void)
+{
+	unsigned long offset = its_baser_offset(GITS_BASER_TYPE_COLLECTION);
+	u64 baser;
+
+	its_set_enable(false);
+
+	baser = readq_relaxed(GITS_BASE_GVA + offset);

[Severity: Medium]
Is it architecturally valid to read and modify GITS_BASER immediately after clearing GITS_CTLR_ENABLE without first polling for the GITS_CTLR.Quiescent bit?

While KVM's synchronous implementation might tolerate this sequence today, could this missing poll cause unpredictability on asynchronous hardware or future emulators?

Quoted 3 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +3 -0
+	baser &= ~GITS_BASER_PAGES_MASK;
+	writeq_relaxed(baser, GITS_BASE_GVA + offset);
+}

[ ... ]

tools/testing/selftests/kvm/arm64/vgic_its_save.c line 163

Quoted 9 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +9 -0
+/* Turn the already installed device table into an indirect one. */
+static void guest_make_device_table_indirect(void)
+{
+	unsigned long offset = its_baser_offset(GITS_BASER_TYPE_DEVICE);
+	u64 baser;
+
+	its_set_enable(false);
+
+	baser = readq_relaxed(GITS_BASE_GVA + offset);

[Severity: Medium]
Similarly here in guest_make_device_table_indirect(), does this need a poll for the Quiescent bit after disabling the ITS and before accessing GITS_BASER?

Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +8 -0
+	writeq_relaxed(baser | GITS_BASER_INDIRECT, GITS_BASE_GVA + offset);
+
+	its_set_enable(true);
+}
+
+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]
Since the host code later reads this memory using le64toh(), will writing these L1 device table entries in native endianness cause data corruption when running the selftest on big-endian guests?

Should these entries be converted using cpu_to_le64() before assignment?

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);

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author reviewer
#10 lore raw

Re: [PATCH 4/4] KVM: arm64: selftests: Add ITS table save tests

Quoted 3 lines · On Wed, 19 Aug 2026 at 11:39, <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The guest code writes L1 device table entries using native endianness instead of little-endian, which will cause corrupted data on big-endian guests.

arm64 big-endian has depended on BROKEN since 6.18, and is being removed [1], so this is fine for a selftest.

Quoted 1 line

- [Medium] The guest code modifies GITS_BASER immediately after clearing GITS_CTLR.Enable without polling for the GITS_CTLR.Quiescent bit.

KVM's emulated ITS permits the GITS_BASER write whenever GITS_CTLR.Enable is clear, so the poll is not needed here.

/fuad

[1] https://lore.kernel.org/all/[email protected]/

Quoted 95 lines

--

--- Patch [4]: [PATCH 4/4] KVM: arm64: selftests: Add ITS table save tests --- commit 277abfaff8f849d670b2c74f45ab3607f657788b
Author: Fuad Tabba <[email protected]>

KVM: arm64: selftests: Add ITS table save tests

This commit adds selftests for KVM_DEV_ARM_ITS_SAVE_TABLES to handle two cases where a guest leaves a table in a modified state. It covers a GITS_BASER write that drops described collections, and an invalidated L2 block that causes a device to be skipped.

Quoted 4 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +0 -0
new file mode 100644

[ ... ]

tools/testing/selftests/kvm/arm64/vgic_its_save.c line 130

Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +8 -0
+static void guest_shrink_coll_table(void)
+{
+     unsigned long offset = its_baser_offset(GITS_BASER_TYPE_COLLECTION);
+     u64 baser;
+
+     its_set_enable(false);
+
+     baser = readq_relaxed(GITS_BASE_GVA + offset);

[Severity: Medium]
Is it architecturally valid to read and modify GITS_BASER immediately after clearing GITS_CTLR_ENABLE without first polling for the GITS_CTLR.Quiescent bit?

While KVM's synchronous implementation might tolerate this sequence today, could this missing poll cause unpredictability on asynchronous hardware or future emulators?

Quoted 3 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +3 -0
+     baser &= ~GITS_BASER_PAGES_MASK;
+     writeq_relaxed(baser, GITS_BASE_GVA + offset);
+}

[ ... ]

tools/testing/selftests/kvm/arm64/vgic_its_save.c line 163

Quoted 9 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +9 -0
+/* Turn the already installed device table into an indirect one. */
+static void guest_make_device_table_indirect(void)
+{
+     unsigned long offset = its_baser_offset(GITS_BASER_TYPE_DEVICE);
+     u64 baser;
+
+     its_set_enable(false);
+
+     baser = readq_relaxed(GITS_BASE_GVA + offset);

[Severity: Medium]
Similarly here in guest_make_device_table_indirect(), does this need a poll for the Quiescent bit after disabling the ITS and before accessing GITS_BASER?

Quoted 8 lines
tools/testing/selftests/kvm/arm64/vgic_its_save.c new file +8 -0
+     writeq_relaxed(baser | GITS_BASER_INDIRECT, GITS_BASE_GVA + offset);
+
+     its_set_enable(true);
+}
+
+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]
Since the host code later reads this memory using le64toh(), will writing these L1 device table entries in native endianness cause data corruption when running the selftest on big-endian guests?

Should these entries be converted using cpu_to_le64() before assignment?

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);

[ ... ]

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4