Skip to contentSkip to conversation

KVM: arm64: Preserve level IRQ re-pending

PATCH New Karl Mehltretter 2 participants 6 messages Oct 28

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover KVM: arm64: Preserve level IRQ re-pending

A guest can set a level interrupt pending again while handling it. The GIC records Active and Pending and delivers the interrupt after deactivation. KVM instead drops the second pending state for both the GICv2 and GICv3 backends.

The VGIC keeps software pending state outside the LR because commit 67b5b673ad4d stopped placing Pending and Active together for level interrupts. When the LR is folded, KVM cannot distinguish the pending state used to populate the LR from a later ISPENDR write.

Synchronize the target vCPU's LRs in vgic_mmio_write_spending() with vgic_access_active_prepare() and vgic_access_active_finish() under config_lock before setting pending_latch, as suggested by Oliver Upton. When a vCPU writes its own GICR_ISPENDR0, this synchronization is a no-op. For shared interrupts or remote redistributor writes, stopping the guest folds any prior ack from the target vCPU's LRs before the new pending state is recorded. In the v2 and v3 fold paths, clear pending_latch when an LR populated for an inactive interrupt no longer reports a pending state, and preserve a latch written while the interrupt was active.

The last patch adds a two-vCPU GICv3 selftest covering same-vCPU and cross-vCPU re-pending while active, as well as a cross-vCPU ISPENDR write before the ack.

The issue was reproduced with Zephyr, NuttX and RT-Thread test harnesses that configure an unused SPI as level-sensitive, pend it via GICD_ISPENDR, and re-pend it once from the ISR before deactivation. On a Raspberry Pi 400 host with a GIC-400, each GICv2 guest handled the SPI once on the baseline and twice with the RFC fix. A native RT-Thread run on the same GIC-400 also delivered the re-pended interrupt.

The v2 change was A/B tested with RT-Thread's direct and mailbox-pic reproducers on Linux/KVM hosts running under QEMU TCG with GICv2 and GICv3. The baseline lost the second event and v2 delivered it. The new GICv3 selftest also passes in that setup. I do not have a physical GICv3 host, so the GICv3 testing is emulated.

Changes in v2:
- Synchronize target vCPU LRs in vgic_mmio_write_spending() via vgic_access_active_prepare() and vgic_access_active_finish() under config_lock instead of tracking on-LR writes with a separate latch_written flag (Oliver Upton).
- Extend the selftest to verify that a cross-vCPU GICD_ISPENDR write before the ack merges with the initial pending state without causing a second delivery.
- Drop RFC prefix.
- Link to v1: https://lore.kernel.org/r/[email protected]

Karl Mehltretter (3):
KVM: arm64: vgic-v2: Don't drop pending writes to active level IRQs
KVM: arm64: vgic-v3: Don't drop pending writes to active level IRQs
KVM: selftests: arm64: Test pending writes to active level IRQs

 arch/arm64/kvm/vgic/vgic-mmio.c              |  76 +++++-----
 arch/arm64/kvm/vgic/vgic-v2.c                |  10 +-
 arch/arm64/kvm/vgic/vgic-v3.c                |  10 +-
 tools/testing/selftests/kvm/arm64/vgic_irq.c | 140 +++++++++++++++++++
 4 files changed, 197 insertions(+), 39 deletions(-)

base-commit: dd0bc00e3e9201fd49b62ff9cc07890c89003afe

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH v2 1/3 KVM: arm64: vgic-v2: Don't drop pending writes to active level IRQs

A write to GICD_ISPENDR for an active level interrupt makes the interrupt Active and Pending. It must be delivered again after it is deactivated.

KVM loses the second pending state. vgic_v2_populate_lr() leaves pending_latch set for a level interrupt, while vgic_v2_compute_lr() keeps Pending and Active out of the same LR. vgic_v2_fold_lr() clears the latch after the LR becomes empty. At that point it cannot tell the original latch from one written while the interrupt was active, and clears both.

Synchronize the target vCPU's LRs in vgic_mmio_write_spending() with vgic_access_active_prepare() and vgic_access_active_finish() under config_lock before setting pending_latch. When the interrupt is in an LR on another vCPU, this folds any prior ack into the VGIC state before the new pending state is recorded.

In vgic_v2_fold_lr(), clear the original latch when an LR populated for an inactive interrupt no longer reports a pending state, and preserve a latch written while the interrupt was active.

Fixes 67b5b673ad4d ("KVM: arm/arm64: vgic: Disallow Active+Pending for level interrupts")
Assisted-by LLM
---
 arch/arm64/kvm/vgic/vgic-mmio.c | 76 +++++++++++++++++++--------------
 arch/arm64/kvm/vgic/vgic-v2.c   | 10 +++--
 2 files changed, 50 insertions(+), 36 deletions(-)
arch/arm64/kvm/vgic/vgic-mmio.c +43 -33
index fddb9da0403d5..e42eb10d502b2 100644
@@ -350,11 +350,54 @@ static void __set_pending(struct kvm_vcpu *vcpu, gpa_t addr, unsigned int len,
 	}
 }
 
+/*
+ * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
+ * is not queued on some running VCPU's LRs, because then the change to the
+ * active state can be overwritten when the VCPU's state is synced coming back
+ * from the guest. Setting an IRQ pending also requires the target VCPU's LRs
+ * to be synced first so that any prior ack in the LR is folded
+ * before pending_latch is set.
+ *
+ * For shared interrupts as well as GICv3 private interrupts accessed from the
+ * non-owning CPU, we have to stop all the VCPUs because interrupts can be
+ * migrated while we don't hold the IRQ locks and we don't want to be chasing
+ * moving targets.
+ *
+ * For GICv2 private interrupts we don't have to do anything because
+ * userspace accesses to the VGIC state already require all VCPUs to be
+ * stopped, and only the VCPU itself can modify its private interrupts
+ * active state, which guarantees that the VCPU is not running.
+ */
+static void vgic_access_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
+{
+	if ((vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 &&
+	     vcpu != kvm_get_running_vcpu()) ||
+	    intid >= VGIC_NR_PRIVATE_IRQS)
+		kvm_arm_halt_guest(vcpu->kvm);
+}
+
+/* See vgic_access_active_prepare */
+static void vgic_access_active_finish(struct kvm_vcpu *vcpu, u32 intid)
+{
+	if ((vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 &&
+	     vcpu != kvm_get_running_vcpu()) ||
+	    intid >= VGIC_NR_PRIVATE_IRQS)
+		kvm_arm_resume_guest(vcpu->kvm);
+}
+
 void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
 			      gpa_t addr, unsigned int len,
 			      unsigned long val)
 {
+	u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
+
+	mutex_lock(&vcpu->kvm->arch.config_lock);
+	vgic_access_active_prepare(vcpu, intid);
+
 	__set_pending(vcpu, addr, len, val, false);
+
+	vgic_access_active_finish(vcpu, intid);
+	mutex_unlock(&vcpu->kvm->arch.config_lock);
 }
 
 int vgic_uaccess_write_spending(struct kvm_vcpu *vcpu,
@@ -452,39 +495,6 @@ int vgic_uaccess_write_cpending(struct kvm_vcpu *vcpu,
 	return 0;
 }
 
-/*
- * If we are fiddling with an IRQ's active state, we have to make sure the IRQ
- * is not queued on some running VCPU's LRs, because then the change to the
- * active state can be overwritten when the VCPU's state is synced coming back
- * from the guest.
- *
- * For shared interrupts as well as GICv3 private interrupts accessed from the
- * non-owning CPU, we have to stop all the VCPUs because interrupts can be
- * migrated while we don't hold the IRQ locks and we don't want to be chasing
- * moving targets.
- *
- * For GICv2 private interrupts we don't have to do anything because
- * userspace accesses to the VGIC state already require all VCPUs to be
- * stopped, and only the VCPU itself can modify its private interrupts
- * active state, which guarantees that the VCPU is not running.
- */
-static void vgic_access_active_prepare(struct kvm_vcpu *vcpu, u32 intid)
-{
-	if ((vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 &&
-	     vcpu != kvm_get_running_vcpu()) ||
-	    intid >= VGIC_NR_PRIVATE_IRQS)
-		kvm_arm_halt_guest(vcpu->kvm);
-}
-
-/* See vgic_access_active_prepare */
-static void vgic_access_active_finish(struct kvm_vcpu *vcpu, u32 intid)
-{
-	if ((vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3 &&
-	     vcpu != kvm_get_running_vcpu()) ||
-	    intid >= VGIC_NR_PRIVATE_IRQS)
-		kvm_arm_resume_guest(vcpu->kvm);
-}
-
 static unsigned long __vgic_mmio_read_active(struct kvm_vcpu *vcpu,
 					     gpa_t addr, unsigned int len)
 {
arch/arm64/kvm/vgic/vgic-v2.c +7 -3
index 7182f63fc9382..f3ba3177dd97b 100644
@@ -57,7 +57,7 @@ static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val)
 {
 	u32 cpuid, intid = val & GICH_LR_VIRTUALID;
 	struct vgic_irq *irq;
-	bool deactivated;
+	bool deactivated, was_active;
 
 	/* Extract the source vCPU id from the LR */
 	cpuid = FIELD_GET(GICH_LR_PHYSID_CPUID, val) & 7;
@@ -71,7 +71,8 @@ static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val)
 
 	scoped_guard(raw_spinlock, &irq->irq_lock) {
 		/* Always preserve the active bit, note deactivation */
-		deactivated = irq->active && !(val & GICH_LR_ACTIVE_BIT);
+		was_active = irq->active;
+		deactivated = was_active && !(val & GICH_LR_ACTIVE_BIT);
 		irq->active = !!(val & GICH_LR_ACTIVE_BIT);
 
 		if (irq->active && vgic_irq_is_sgi(intid))
@@ -88,8 +89,11 @@ static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val)
 
 		/*
 		 * Clear soft pending state when level irqs have been acked.
+		 * A latch written while the irq was active is a new pending
+		 * state and was not represented in the LR, so preserve it.
 		 */
-		if (irq->config == VGIC_CONFIG_LEVEL && !(val & GICH_LR_STATE))
+		if (irq->config == VGIC_CONFIG_LEVEL &&
+		    !(val & GICH_LR_PENDING_BIT) && !was_active)
 			irq->pending_latch = false;
 
 		/* Handle resampling for mapped interrupts if required */
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

Re: [PATCH v2 1/3] KVM: arm64: vgic-v2: Don't drop pending writes to active level IRQs

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] KVM: arm64: vgic-v2: GICH_LR_HW pending LR programmed with inactive phys IRQ - [Medium] KVM…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] KVM: arm64: vgic-v2: GICH_LR_HW pending LR programmed with inactive phys IRQ
- [Medium] KVM: arm64: vgic-v2: spurious level IRQ after save/restore in vgic_v2_fold_lr() --

commit 48710a44e389b45084a926f40b58ded3edf3a4dd
Author: Karl Mehltretter <[email protected]>

KVM: arm64: vgic-v2: Don't drop pending writes to active level IRQs

This commit synchronizes the target vCPU's LRs in vgic_mmio_write_spending() and preserves irq->pending_latch in vgic_v2_fold_lr() when a level-triggered interrupt was already active, so that a pending state written via GICD_ISPENDR while the interrupt is active is not dropped on deactivation.

Quoted 24 lines
arch/arm64/kvm/vgic/vgic-v2.c +3 -2
index 7182f63fc9382..f3ba3177dd97b 100644
@@ -57,7 +57,7 @@ static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val)
 {
 	u32 cpuid, intid = val & GICH_LR_VIRTUALID;
 	struct vgic_irq *irq;
-	bool deactivated;
+	bool deactivated, was_active;
 
 	/* Extract the source vCPU id from the LR */
 	cpuid = FIELD_GET(GICH_LR_PHYSID_CPUID, val) & 7;
@@ -71,7 +71,8 @@ static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val)
 
 	scoped_guard(raw_spinlock, &irq->irq_lock) {
 		/* Always preserve the active bit, note deactivation */
-		deactivated = irq->active && !(val & GICH_LR_ACTIVE_BIT);
+		was_active = irq->active;
+		deactivated = was_active && !(val & GICH_LR_ACTIVE_BIT);
 		irq->active = !!(val & GICH_LR_ACTIVE_BIT);
 
 		if (irq->active && vgic_irq_is_sgi(intid))

arch/arm64/kvm/vgic/vgic-v2.c static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val) · line 97

Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v2.c +4 -1
@@ -88,8 +89,11 @@ static void vgic_v2_fold_lr(struct kvm_vcpu *vcpu, u32 val)
 
 		/*
 		 * Clear soft pending state when level irqs have been acked.
+		 * A latch written while the irq was active is a new pending
+		 * state and was not represented in the LR, so preserve it.
 		 */
-		if (irq->config == VGIC_CONFIG_LEVEL && !(val & GICH_LR_STATE))
+		if (irq->config == VGIC_CONFIG_LEVEL &&
+		    !(val & GICH_LR_PENDING_BIT) && !was_active)
 			irq->pending_latch = false;

[Severity: High]
Can preserving irq->pending_latch here in vgic_v2_fold_lr() in arch/arm64/kvm/vgic/vgic-v2.c cause a GICH_LR_HW list register to be populated as pending while the physical interrupt is inactive in the host distributor?

If the guest writes to GICD_ISPENDR0 for a mapped level-triggered interrupt such as the architected timer PPI (irq->hw is true, irq->config is VGIC_CONFIG_LEVEL, and !vgic_irq_needs_resampling(irq)) while that interrupt is already active, vgic_mmio_write_spending() -> __set_pending() in arch/arm64/kvm/vgic/vgic-mmio.c sets irq->pending_latch and marks the physical interrupt active while the first instance is already active in hardware:

arch/arm64/kvm/vgic/vgic-mmio.c:__set_pending() {
    ...
		irq->pending_latch = true;
		if (irq->hw && !is_user)
			vgic_irq_set_phys_active(irq, true);
    ...
}

When the guest deactivates that first in-flight instance, the hardware deactivation clears the physical interrupt's active state in the host GIC distributor. On the subsequent exit, vgic_v2_fold_lr() sees was_active == true and deactivated == true, sets irq->active = false, and preserves irq->pending_latch = true without re-activating the physical interrupt.

On the next guest entry, vgic_v2_populate_lr() -> vgic_v2_compute_lr() in arch/arm64/kvm/vgic/vgic-v2.c sees irq->active == false and irq_is_pending(irq) == true via irq->pending_latch:

arch/arm64/kvm/vgic/vgic-v2.c:vgic_v2_compute_lr() {
    ...
	if (irq->hw && !vgic_irq_needs_resampling(irq)) {
		val |= GICH_LR_HW;
		val |= irq->hwintid << GICH_LR_PHYSID_CPUID_SHIFT;
		/*
		 * Never set pending+active on a HW interrupt, as the
		 * pending state is kept at the physical distributor
		 * level.
		 */
		if (irq->active)
			allow_pending = false;
	}
    ...
}

Wouldn't this program the list register with GICH_LR_HW | GICH_LR_PENDING_BIT while the corresponding physical interrupt in the host distributor is inactive?

[Severity: Medium]
Also, what happens across a GICv2 VM save and restore when a level-triggered device interrupt is active and its input line is still asserted (irq->active == true, irq->line_level == true, and irq->pending_latch == false)?

When userspace saves KVM_DEV_TYPE_ARM_VGIC_V2 distributor state via vgic_uaccess_read_pending() -> __read_pending() in arch/arm64/kvm/vgic/vgic-mmio.c, GICv2 reads irq_is_pending(irq):

arch/arm64/kvm/vgic/vgic-mmio.c:__read_pending() {
    ...
			switch (vcpu->kvm->arch.vgic.vgic_model) {
			case KVM_DEV_TYPE_ARM_VGIC_V3:
				if (is_user) {
					val = irq->pending_latch;
					break;
				}
				fallthrough;
			default:
				val = irq_is_pending(irq);
				break;
			}
    ...
}

Because irq->line_level is true, the saved pending state is 1. On restore, userspace restores the active state via vgic_mmio_uaccess_write_sactive() (setting irq->active = true) and restores the pending state via vgic_uaccess_write_spending() -> __set_pending() (setting irq->pending_latch = true).

When the guest resumes, clears the interrupt condition at the device (setting irq->line_level = false), and writes GICC_EOIR to deactivate the interrupt, vgic_v2_fold_lr() runs with was_active == true and !(val & GICH_LR_STATE). With the new !was_active check in vgic_v2_fold_lr(), won't irq->pending_latch remain true on deactivation and cause a spurious second delivery of the level interrupt?

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

PATCH v2 2/3 KVM: arm64: vgic-v3: Don't drop pending writes to active level IRQs

In reply to Karl Mehltretter

The GICv3 backend also loses a level interrupt set pending through GICD_ISPENDR or GICR_ISPENDR0 while it is active. Its LR handling has the same split between pending_latch and an Active-only LR as GICv2, and clears the new pending state after deactivation.

With vgic_mmio_write_spending() synchronizing the target vCPU's LRs before setting pending_latch (which is a no-op when a vCPU writes its own GICR_ISPENDR0), apply the GICv2 fold rule to vgic_v3_fold_lr(). Clear the old latch when an LR populated for an inactive interrupt reports that the interrupt has been acknowledged, and preserve a latch written while the interrupt was active.

GICv3 permits an implementation to clear register-set pending state on ack. Doing so matches the GICv2 backend and makes a later ISPENDR write a distinct pending state.

Fixes 67b5b673ad4d ("KVM: arm/arm64: vgic: Disallow Active+Pending for level interrupts")
Assisted-by LLM
---
 arch/arm64/kvm/vgic/vgic-v3.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/arm64/kvm/vgic/vgic-v3.c +7 -3
index 726e20a1da6e7..4d802fc9c6dd5 100644
@@ -75,7 +75,7 @@ static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val)
 {
 	struct vgic_irq *irq;
 	bool is_v2_sgi = false;
-	bool deactivated;
+	bool deactivated, was_active;
 	u32 intid;
 
 	if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
@@ -93,7 +93,8 @@ static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val)
 		/* Always preserve the active bit for !LPIs, note deactivation */
 		if (irq->intid >= VGIC_MIN_LPI)
 			val &= ~ICH_LR_ACTIVE_BIT;
-		deactivated = irq->active && !(val & ICH_LR_ACTIVE_BIT);
+		was_active = irq->active;
+		deactivated = was_active && !(val & ICH_LR_ACTIVE_BIT);
 		irq->active = !!(val & ICH_LR_ACTIVE_BIT);
 
 		/* Edge is the only case where we preserve the pending bit */
@@ -103,8 +104,11 @@ static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val)
 
 		/*
 		 * Clear soft pending state when level irqs have been acked.
+		 * A latch written while the irq was active is a new pending
+		 * state and was not represented in the LR, so preserve it.
 		 */
-		if (irq->config == VGIC_CONFIG_LEVEL && !(val & ICH_LR_STATE))
+		if (irq->config == VGIC_CONFIG_LEVEL &&
+		    !(val & ICH_LR_PENDING_BIT) && !was_active)
 			irq->pending_latch = false;
 
 		if (is_v2_sgi) {
Signature · 1 line
2.39.5 (Apple Git-154)
#4 lore raw

Re: [PATCH v2 2/3] KVM: arm64: vgic-v3: Don't drop pending writes to active level IRQs

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] KVM: arm64: vgic-v3: vgic_v3_fold_lr() leaves HW IRQ inactive on repend --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] KVM: arm64: vgic-v3: vgic_v3_fold_lr() leaves HW IRQ inactive on repend --

commit a4fe64a3fc3df5e9a6845ababa512f31987bf69e
Author: Karl Mehltretter <[email protected]>

KVM: arm64: vgic-v3: Don't drop pending writes to active level IRQs

This commit preserves irq->pending_latch in vgic_v3_fold_lr() when a level-triggered interrupt was already active, so that a pending state set via GICD_ISPENDR or GICR_ISPENDR0 while the interrupt is active is not cleared upon deactivation.

Quoted 24 lines
arch/arm64/kvm/vgic/vgic-v3.c +3 -2
index 726e20a1da6e7..4d802fc9c6dd5 100644
@@ -75,7 +75,7 @@ static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val)
 {
 	struct vgic_irq *irq;
 	bool is_v2_sgi = false;
-	bool deactivated;
+	bool deactivated, was_active;
 	u32 intid;
 
 	if (vcpu->kvm->arch.vgic.vgic_model == KVM_DEV_TYPE_ARM_VGIC_V3) {
@@ -93,7 +93,8 @@ static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val)
 		/* Always preserve the active bit for !LPIs, note deactivation */
 		if (irq->intid >= VGIC_MIN_LPI)
 			val &= ~ICH_LR_ACTIVE_BIT;
-		deactivated = irq->active && !(val & ICH_LR_ACTIVE_BIT);
+		was_active = irq->active;
+		deactivated = was_active && !(val & ICH_LR_ACTIVE_BIT);
 		irq->active = !!(val & ICH_LR_ACTIVE_BIT);
 
 		/* Edge is the only case where we preserve the pending bit */

arch/arm64/kvm/vgic/vgic-v3.c static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val) · line 112

Quoted 10 lines
arch/arm64/kvm/vgic/vgic-v3.c +4 -1
@@ -103,8 +104,11 @@ static void vgic_v3_fold_lr(struct kvm_vcpu *vcpu, u64 val)
 
 		/*
 		 * Clear soft pending state when level irqs have been acked.
+		 * A latch written while the irq was active is a new pending
+		 * state and was not represented in the LR, so preserve it.
 		 */
-		if (irq->config == VGIC_CONFIG_LEVEL && !(val & ICH_LR_STATE))
+		if (irq->config == VGIC_CONFIG_LEVEL &&
+		    !(val & ICH_LR_PENDING_BIT) && !was_active)
 			irq->pending_latch = false;

[Severity: High]
What happens here for a mapped level-triggered interrupt where irq->hw is true and !vgic_irq_needs_resampling(irq), such as the architected timer PPI?

When that interrupt is already active in the guest (irq->active is true and the physical interrupt is active in the host GIC), a guest write to GICR_ISPENDR0 or GICD_ISPENDR calls:

vgic_mmio_write_spending() -> __set_pending() {
	irq->pending_latch = true;
	if (irq->hw && !is_user)
		vgic_irq_set_phys_active(irq, true);
}

Because the physical interrupt is already active in the host GIC for the in-flight activation, vgic_irq_set_phys_active(irq, true) does not add a second physical active state.

When the guest then deactivates the interrupt via ICC_EOIR1_EL1 or ICC_DIR_EL1 (or via vgic_v3_deactivate_phys()), hardware deactivation clears the physical active state in the host GIC. On the next VM exit, vgic_v3_fold_lr() sees was_active is true and !(val & ICH_LR_ACTIVE_BIT), sets irq->active = false, and preserves irq->pending_latch = true.

Then vgic_irq_handle_resampling() runs:

arch/arm64/kvm/vgic/vgic.c:vgic_irq_handle_resampling() {
	if (unlikely(vgic_irq_needs_resampling(irq))) {
		resample = !(irq->active || irq->pending_latch);
	} else if (lr_pending || (lr_deactivated && irq->line_level)) {
		irq->line_level = vgic_get_phys_line_level(irq);
		resample = !irq->line_level;
	}

	if (resample)
		vgic_irq_set_phys_active(irq, false);
}

This does not re-establish the physical active state in the host GIC when irq->pending_latch remains true (and if irq->line_level was true while the timer line is now low, it explicitly clears the physical active state).

On the subsequent guest entry, vgic_v3_populate_lr() -> vgic_v3_compute_lr() sees irq->active is false and irq_is_pending(irq) is true via irq->pending_latch:

arch/arm64/kvm/vgic/vgic-v3.c:vgic_v3_compute_lr() {
	if (irq->hw && !vgic_irq_needs_resampling(irq)) {
		val |= ICH_LR_HW;
		val |= ((u64)irq->hwintid) << ICH_LR_PHYS_ID_SHIFT;
		if (irq->active)
			allow_pending = false;
	}
}

Wouldn't this populate the list register with ICH_LR_HW | ICH_LR_PENDING_BIT while the corresponding physical interrupt is inactive in the host GIC, violating the GICv3 list register hardware-bit consistency requirement and causing the subsequent guest deactivation to deactivate an inactive physical interrupt?

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

PATCH v2 3/3 KVM: selftests: arm64: Test pending writes to active level IRQs

In reply to Karl Mehltretter

Add a two-vCPU GICv3 test for a level SPI set pending through GICD_ISPENDR. Re-pend the interrupt while it is active, first from the handling vCPU and then from the other vCPU. Require a second delivery after deactivation in both cases.

Also check that the other vCPU observes the initial pending state before the interrupt is acknowledged, and that a remote GICD_ISPENDR write before the ack merges with the initial pending state without causing a second delivery.

Without the VGIC fix, the pending state written while active is cleared on deactivation and the test fails before the second delivery.

Assisted-by LLM
---
 tools/testing/selftests/kvm/arm64/vgic_irq.c | 140 +++++++++++++++++++
 1 file changed, 140 insertions(+)
tools/testing/selftests/kvm/arm64/vgic_irq.c +140 -0
index 3cb85932ee85c..83091e0384c22 100644
@@ -959,6 +959,145 @@ static void guest_code_timer_spi(struct test_args *args, int cpuid)
 	GUEST_DONE();
 }
 
+#define REPEND_READ		1
+#define REPEND_READ_DONE	2
+#define REPEND_WRITE		3
+#define REPEND_PRE_ACK		4
+#define REPEND_PRE_ACK_DONE	5
+
+/*
+ * A level-sensitive SPI that is set pending through GICD_ISPENDR stays
+ * pending for every vCPU until it is acked. If it is set pending again while
+ * it is active, it becomes active and pending and must be taken a second time
+ * once it has been deactivated. The second pending state comes from the vCPU
+ * that handles the interrupt in the first round, and from the other vCPU in
+ * the second. A remote GICD_ISPENDR write before the interrupt is acked merges
+ * with the initial pending state without causing a second delivery.
+ */
+static void guest_code_level_repend(struct test_args *args, int cpuid)
+{
+	int i;
+
+	gic_init(GIC_V3, 2);
+
+	gic_set_eoi_split(1);
+	gic_set_priority_mask(CPU_PRIO_MASK);
+
+	if (cpuid == 0) {
+		u32 intid;
+
+		local_irq_disable();
+
+		gic_irq_set_config(MIN_SPI, false);
+		gic_set_priority(MIN_SPI, IRQ_DEFAULT_PRIO);
+		gic_irq_enable(MIN_SPI);
+
+		for (i = 0; i < 2; i++) {
+			gic_irq_set_pending(MIN_SPI);
+
+			if (i == 1) {
+				/* Not acked yet: the other vCPU sees it pending */
+				WRITE_ONCE(args->shared_data, REPEND_READ);
+				dsb(ishst);
+
+				do {
+					dsb(ishld);
+				} while (READ_ONCE(args->shared_data) != REPEND_READ_DONE);
+			}
+
+			GUEST_ASSERT(gic_irq_get_pending(MIN_SPI));
+			intid = wait_for_and_activate_irq();
+			GUEST_ASSERT_EQ(intid, MIN_SPI);
+
+			if (i == 0) {
+				gic_irq_set_pending(MIN_SPI);
+			} else {
+				WRITE_ONCE(args->shared_data, REPEND_WRITE);
+				dsb(ishst);
+
+				do {
+					dsb(ishld);
+				} while (READ_ONCE(args->shared_data) == REPEND_WRITE);
+			}
+
+			GUEST_ASSERT(gic_irq_get_active(MIN_SPI));
+			gic_set_eoi(intid);
+			gic_set_dir(intid);
+			isb();
+
+			/* The second pending state must not have been lost */
+			GUEST_ASSERT(gic_irq_get_pending(MIN_SPI));
+			intid = wait_for_and_activate_irq();
+			GUEST_ASSERT_EQ(intid, MIN_SPI);
+
+			gic_set_eoi(intid);
+			gic_set_dir(intid);
+			isb();
+
+			GUEST_ASSERT(!gic_irq_get_active(MIN_SPI));
+			GUEST_ASSERT(!gic_irq_get_pending(MIN_SPI));
+			GUEST_ASSERT_EQ(gic_get_and_ack_irq(), IAR_SPURIOUS);
+		}
+
+		/*
+		 * A remote ISPENDR write before the interrupt is acked merges
+		 * with the initial pending state and must not cause a second
+		 * delivery after deactivation.
+		 */
+		gic_irq_set_pending(MIN_SPI);
+
+		WRITE_ONCE(args->shared_data, REPEND_PRE_ACK);
+		dsb(ishst);
+
+		do {
+			dsb(ishld);
+		} while (READ_ONCE(args->shared_data) != REPEND_PRE_ACK_DONE);
+
+		GUEST_ASSERT(gic_irq_get_pending(MIN_SPI));
+		intid = wait_for_and_activate_irq();
+		GUEST_ASSERT_EQ(intid, MIN_SPI);
+
+		gic_set_eoi(intid);
+		gic_set_dir(intid);
+		isb();
+
+		GUEST_ASSERT(!gic_irq_get_active(MIN_SPI));
+		GUEST_ASSERT(!gic_irq_get_pending(MIN_SPI));
+		GUEST_ASSERT_EQ(gic_get_and_ack_irq(), IAR_SPURIOUS);
+	} else {
+		do {
+			dsb(ishld);
+		} while (READ_ONCE(args->shared_data) != REPEND_READ);
+
+		for (i = 0; i < 64; i++)
+			GUEST_ASSERT(gic_irq_get_pending(MIN_SPI));
+
+		WRITE_ONCE(args->shared_data, REPEND_READ_DONE);
+		dsb(ishst);
+
+		do {
+			dsb(ishld);
+		} while (READ_ONCE(args->shared_data) != REPEND_WRITE);
+
+		gic_irq_set_pending(MIN_SPI);
+
+		WRITE_ONCE(args->shared_data, 0);
+		dsb(ishst);
+
+		do {
+			dsb(ishld);
+		} while (READ_ONCE(args->shared_data) != REPEND_PRE_ACK);
+
+		GUEST_ASSERT(gic_irq_get_pending(MIN_SPI));
+		gic_irq_set_pending(MIN_SPI);
+
+		WRITE_ONCE(args->shared_data, REPEND_PRE_ACK_DONE);
+		dsb(ishst);
+	}
+
+	GUEST_DONE();
+}
+
 static void *test_vcpu_run(void *arg)
 {
 	struct kvm_vcpu *vcpu = arg;
@@ -1073,6 +1212,7 @@ int main(int argc, char **argv)
 		test_vgic_two_cpus(guest_code_asym_dir);
 		test_vgic_two_cpus(guest_code_group_en);
 		test_vgic_two_cpus(guest_code_timer_spi);
+		test_vgic_two_cpus(guest_code_level_repend);
 	} else {
 		test_vgic(nr_irqs, level_sensitive, eoi_split);
 	}
Signature · 1 line
2.39.5 (Apple Git-154)