Skip to contentSkip to conversation

KVM: arm64: First batch of vgic-v5 related fixes

PATCH Superseded Marc Zyngier 3 participants 24 messages Mar 26 – Apr 1

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Marc Zyngier

author maintainer
#0 lore raw

cover KVM: arm64: First batch of vgic-v5 related fixes

Well, merging the first batch of vgic-v5 patches didn't go smoothly at all. We initially found a couple of regressions, but most of the crap was actually uncovered by everyone's new best friend (enemy?), the AI bot sitting behind sashiko.dev [1].

While not all of the remarks were valid, a bunch of them were actually extremely pertinent, and resulted in me frantically hacking away at the series. Hopefully the bot doesn't find even more issues in the fixes. Note that the first patch has already been posted and merged, and is only here for reference.

Note that given the amount of rework, I'm really in two minds between adding these patches on top, or pulling the whole series for another cycle. I guess time will tell.

[1] https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

Marc Zyngier (15):
KVM: arm64: vgic: Don't reset cpuif/redist addresses at finalize time
KVM: arm64: Don't skip per-vcpu NV initialisation
arm64: Fix field references for ICH_PPI_DVIR[01]_EL2
KVM: arm64: Fix writeable mask for ID_AA64PFR2_EL1
KVM: arm64: Account for RESx bits in __compute_fgt()
KVM: arm64: vgic-v5: Hold config_lock while finalizing GICv5 PPIs
KVM: arm64: vgic-v5: Transfer edge pending state to ICH_PPI_PENDRx_EL2
KVM: arm64: vgic-v5: Cast vgic_apr to u32 to avoid undefined
behaviours
KVM: arm64: vgic-v5: align priority comparison with other GICs
KVM: arm64: vgic-v5: Correctly set dist->ready once initialised
KVM: arm64: Kill arch_timer_context::direct field
KVM: arm64: Remove evaluation of timer state in
kvm_cpu_has_pending_timer()
KVM: arm64: Move GICv5 timer PPI validation into
timer_irqs_are_valid()
KVM: arm64: Correctly plumb ID_AA64PFR2_EL1 into pkvm idreg handling
KVM: arm64: Don't advertises GICv3 in ID_PFR1_EL1 if AArch32 isn't
supported

 arch/arm64/kvm/arch_timer.c        | 32 +++++++++++++-----------------
 arch/arm64/kvm/config.c            |  4 ++--
 arch/arm64/kvm/hyp/nvhe/sys_regs.c |  2 +-
 arch/arm64/kvm/sys_regs.c          | 20 +++++++++----------
 arch/arm64/kvm/vgic/vgic-init.c    | 32 ++++++++++++++++++++----------
 arch/arm64/kvm/vgic/vgic-v5.c      | 24 +++++++++++++++++-----
 arch/arm64/tools/sysreg            |  4 ++--
 include/kvm/arm_arch_timer.h       |  3 ---
 8 files changed, 70 insertions(+), 51 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/15 KVM: arm64: vgic: Don't reset cpuif/redist addresses at finalize time

Although we are OK with rewriting idregs at finalize time, resetting the guest's cpuif (GICv3) or redistributor (GICv3) addresses once we start running the guest is a pretty bad idea.

Move back this initialisation to vgic creation time.

Fixes a258a383b9177 ("KVM: arm64: gic-v5: Sanitize ID_AA64PFR2_EL1.GCIE")
---
 arch/arm64/kvm/vgic/vgic-init.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +9 -2
index 47169604100f2..fd872079f2a24 100644
@@ -147,6 +147,15 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)
 	kvm->arch.vgic.implementation_rev = KVM_VGIC_IMP_REV_LATEST;
 	kvm->arch.vgic.vgic_dist_base = VGIC_ADDR_UNDEF;
 
+	switch (type) {
+	case KVM_DEV_TYPE_ARM_VGIC_V2:
+		kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
+		break;
+	case KVM_DEV_TYPE_ARM_VGIC_V3:
+		INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
+		break;
+	}
+	
 	/*
 	 * We've now created the GIC. Update the system register state
 	 * to accurately reflect what we've created.
@@ -684,10 +693,8 @@ void kvm_vgic_finalize_idregs(struct kvm *kvm)
 
 	switch (type) {
 	case KVM_DEV_TYPE_ARM_VGIC_V2:
-		kvm->arch.vgic.vgic_cpu_base = VGIC_ADDR_UNDEF;
 		break;
 	case KVM_DEV_TYPE_ARM_VGIC_V3:
-		INIT_LIST_HEAD(&kvm->arch.vgic.rd_regions);
 		aa64pfr0 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, GIC, IMP);
 		pfr1 |= SYS_FIELD_PREP_ENUM(ID_PFR1_EL1, GIC, GICv3);
 		break;
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/15 KVM: arm64: Don't skip per-vcpu NV initialisation

In reply to Marc Zyngier

Some GICv5-related rework have resulted in the NV sanitisation of registers being skipped for secondary vcpus, which is a pretty bad idea.

Hoist the NV init early so that it is always executed.

Fixes cbd8c958be54a ("KVM: arm64: Return early from kvm_finalize_sys_regs() if guest has run")
---
 arch/arm64/kvm/sys_regs.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
arch/arm64/kvm/sys_regs.c +6 -6
index e1001544d4f40..18e2d2fccedb8 100644
@@ -5772,6 +5772,12 @@ int kvm_finalize_sys_regs(struct kvm_vcpu *vcpu)
 
 	guard(mutex)(&kvm->arch.config_lock);
 
+	if (vcpu_has_nv(vcpu)) {
+		int ret = kvm_init_nv_sysregs(vcpu);
+		if (ret)
+			return ret;
+	}
+
 	if (kvm_vm_has_ran_once(kvm))
 		return 0;
 
@@ -5820,12 +5826,6 @@ int kvm_finalize_sys_regs(struct kvm_vcpu *vcpu)
 		kvm_vgic_finalize_idregs(kvm);
 	}
 
-	if (vcpu_has_nv(vcpu)) {
-		int ret = kvm_init_nv_sysregs(vcpu);
-		if (ret)
-			return ret;
-	}
-
 	return 0;
 }
 
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 3/15 arm64: Fix field references for ICH_PPI_DVIR[01]_EL2

In reply to Marc Zyngier

The ICH_PPI_DVIR[01]_EL2 registers should refer to the ICH_PPI_DVIRx_EL2 fields, instead of ICH_PPI_DVIx_EL2.

Fixes 2808a8337078f ("arm64/sysreg: Add remaining GICv5 ICC_ & ICH_ sysregs for KVM support")
---
 arch/arm64/tools/sysreg | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/tools/sysreg +2 -2
index 51dcca5b2fa6e..3b57cb692c5be 100644
@@ -4888,11 +4888,11 @@ Field	0	DVI0
 EndSysregFields
 
 Sysreg	ICH_PPI_DVIR0_EL2	3	4	12	10	0
-Fields ICH_PPI_DVIx_EL2
+Fields ICH_PPI_DVIRx_EL2
 EndSysreg
 
 Sysreg	ICH_PPI_DVIR1_EL2	3	4	12	10	1
-Fields ICH_PPI_DVIx_EL2
+Fields ICH_PPI_DVIRx_EL2
 EndSysreg
 
 SysregFields	ICH_PPI_ENABLERx_EL2
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH 4/15 KVM: arm64: Fix writeable mask for ID_AA64PFR2_EL1

In reply to Marc Zyngier

The writeable mask for fields in ID_AA64PFR2_EL1 has been accidentally inverted, which isn't a very good idea.

Restore the expected polarity.

Fixes a258a383b9177 ("KVM: arm64: gic-v5: Sanitize ID_AA64PFR2_EL1.GCIE")
---
 arch/arm64/kvm/sys_regs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/arm64/kvm/sys_regs.c +4 -4
index 18e2d2fccedb8..6a96cb7ba9a3c 100644
@@ -3304,10 +3304,10 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 				       ID_AA64PFR1_EL1_MPAM_frac |
 				       ID_AA64PFR1_EL1_MTE)),
 	ID_FILTERED(ID_AA64PFR2_EL1, id_aa64pfr2_el1,
-		    ~(ID_AA64PFR2_EL1_FPMR |
-		      ID_AA64PFR2_EL1_MTEFAR |
-		      ID_AA64PFR2_EL1_MTESTOREONLY |
-		      ID_AA64PFR2_EL1_GCIE)),
+		    (ID_AA64PFR2_EL1_FPMR		|
+		     ID_AA64PFR2_EL1_MTEFAR		|
+		     ID_AA64PFR2_EL1_MTESTOREONLY	|
+		     ID_AA64PFR2_EL1_GCIE)),
 	ID_UNALLOCATED(4,3),
 	ID_WRITABLE(ID_AA64ZFR0_EL1, ~ID_AA64ZFR0_EL1_RES0),
 	ID_HIDDEN(ID_AA64SMFR0_EL1),
Signature · 1 line
2.47.3
author maintainer
#5 lore raw

PATCH 5/15 KVM: arm64: Account for RESx bits in __compute_fgt()

In reply to Marc Zyngier

When computing Fine Grained Traps, it is preferable to account for the reserved bits. The HW will most probably ignore them, unless the bits have been repurposed to do something else.

Use caution, and fold our view of the reserved bits in,

Fixes c259d763e6b09 ("KVM: arm64: Account for RES1 bits in DECLARE_FEAT_MAP() and co")
---
 arch/arm64/kvm/config.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/config.c +2 -2
index e14685343191b..f35b8dddd7c1f 100644
@@ -1663,8 +1663,8 @@ static __always_inline void __compute_fgt(struct kvm_vcpu *vcpu, enum vcpu_sysre
 		clear |= ~nested & m->nmask;
 	}
 
-	val |= set;
-	val &= ~clear;
+	val |= set | m->res1;
+	val &= ~(clear | m->res0);
 	*vcpu_fgt(vcpu, reg) = val;
 }
 
Signature · 1 line
2.47.3
author maintainer
#6 lore raw

PATCH 6/15 KVM: arm64: vgic-v5: Hold config_lock while finalizing GICv5 PPIs

In reply to Marc Zyngier

Finalizing the PPI state is done without holding any lock, which means that two vcpus can race against each other and have one zeroing the state while another one is setting it, or even maybe using it.

Fixing this is done by:

- holding the config lock while performing the initialisation

- checking if SW_PPI has already been advertised, meaning that we have already completed the initialisation once

Fixes 8f1fbe2fd2792 ("KVM: arm64: gic-v5: Finalize GICv5 PPIs and generate mask")
---
 arch/arm64/kvm/vgic/vgic-v5.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/arm64/kvm/vgic/vgic-v5.c +10 -0
index 2b6cd5c3f9c2f..119d7d01d0e77 100644
@@ -172,6 +172,16 @@ int vgic_v5_finalize_ppi_state(struct kvm *kvm)
 	if (!vgic_is_v5(kvm))
 		return 0;
 
+	guard(mutex)(&kvm->arch.config_lock);
+
+	/*
+	 * If SW_PPI has been advertised, then we know we already
+	 * initialised the whole thing, and we can return early. Yes,
+	 * this is pretty hackish as far as state tracking goes...
+	 */
+	if (test_bit(GICV5_ARCH_PPI_SW_PPI, kvm->arch.vgic.gicv5_vm.vgic_ppi_mask))
+		return 0;
+
 	/* The PPI state for all VCPUs should be the same. Pick the first. */
 	vcpu0 = kvm_get_vcpu(kvm, 0);
 
Signature · 1 line
2.47.3
author maintainer
#7 lore raw

PATCH 7/15 KVM: arm64: vgic-v5: Transfer edge pending state to ICH_PPI_PENDRx_EL2

In reply to Marc Zyngier

While it is perfectly correct to leave the pending state of a level interrupt as is when queuing it (it is, after all, only driven by the line), edge pending state must be transfered, as nothing will lower it.

Fixes 4d591252bacb2 ("KVM: arm64: gic-v5: Implement PPI interrupt injection")
---
 arch/arm64/kvm/vgic/vgic-v5.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-v5.c +4 -1
index 119d7d01d0e77..422741c86c6a8 100644
@@ -445,8 +445,11 @@ void vgic_v5_flush_ppi_state(struct kvm_vcpu *vcpu)
 
 		irq = vgic_get_vcpu_irq(vcpu, intid);
 
-		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
+		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
 			__assign_bit(i, pendr, irq_is_pending(irq));
+			if (irq->config == VGIC_CONFIG_EDGE)
+				irq->pending_latch = false;
+		}
 
 		vgic_put_irq(vcpu->kvm, irq);
 	}
Signature · 1 line
2.47.3
author maintainer
#8 lore raw

PATCH 8/15 KVM: arm64: vgic-v5: Cast vgic_apr to u32 to avoid undefined behaviours

In reply to Marc Zyngier

Passing a u64 to __builtin_ctz() is odd, and requires some digging to figure out why this construct is indeed safe as long as the HW is correct.

But it is much easier to make it clear to the compiler by casting the u64 into an intermediate u32, and be done with the UD.

Fixes 933e5288fa971 ("KVM: arm64: gic-v5: Check for pending PPIs")
---
 arch/arm64/kvm/vgic/vgic-v5.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +3 -2
index 422741c86c6a8..0f269321ece4b 100644
@@ -212,7 +212,7 @@ int vgic_v5_finalize_ppi_state(struct kvm *kvm)
 static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
 {
 	struct vgic_v5_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v5;
-	u32 highest_ap, priority_mask;
+	u32 highest_ap, priority_mask, apr;
 
 	/*
 	 * If the guest's CPU has not opted to receive interrupts, then the
@@ -227,7 +227,8 @@ static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
 	 * priority. Explicitly use the 32-bit version here as we have 32
 	 * priorities. 32 then means that there are no active priorities.
 	 */
-	highest_ap = cpu_if->vgic_apr ? __builtin_ctz(cpu_if->vgic_apr) : 32;
+	apr = cpu_if->vgic_apr;
+	highest_ap = apr ? __builtin_ctz(apr) : 32;
 
 	/*
 	 * An interrupt is of sufficient priority if it is equal to or
Signature · 1 line
2.47.3
author maintainer
#9 lore raw

PATCH 9/15 KVM: arm64: vgic-v5: align priority comparison with other GICs

In reply to Marc Zyngier

The way the effective priority mask is computed, and then compared to the priority of an interrupt to decide whether to wake-up or not, is slightly odd, and breaks at the limits.

This could result in spurious wake-ups that are undesirable.

Adopt the GICv[23] logic instead, which checks that the priority value is strictly lower than the mask.

Fixes 933e5288fa971 ("KVM: arm64: gic-v5: Check for pending PPIs")
---
 arch/arm64/kvm/vgic/vgic-v5.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +2 -2
index 0f269321ece4b..75372bbfb6a6a 100644
@@ -238,7 +238,7 @@ static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
 	 */
 	priority_mask = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_VPMR, cpu_if->vgic_vmcr);
 
-	return min(highest_ap, priority_mask + 1);
+	return min(highest_ap, priority_mask);
 }
 
 /*
@@ -367,7 +367,7 @@ bool vgic_v5_has_pending_ppi(struct kvm_vcpu *vcpu)
 
 		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
 			has_pending = (irq->enabled && irq_is_pending(irq) &&
-				       irq->priority <= priority_mask);
+				       irq->priority < priority_mask);
 
 		vgic_put_irq(vcpu->kvm, irq);
 
Signature · 1 line
2.47.3
#10 lore raw

Re: [PATCH 09/15] KVM: arm64: vgic-v5: align priority comparison with other GICs

Quoted 29 lines · Marc Zyngier

The way the effective priority mask is computed, and then compared to the priority of an interrupt to decide whether to wake-up or not, is slightly odd, and breaks at the limits.

This could result in spurious wake-ups that are undesirable.

Adopt the GICv[23] logic instead, which checks that the priority value
is strictly lower than the mask.

Fixes 933e5288fa971 ("KVM: arm64: gic-v5: Check for pending PPIs")

Link:
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

---
 arch/arm64/kvm/vgic/vgic-v5.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +0 -0
index 0f269321ece4b..75372bbfb6a6a 100644
@@ -238,7 +238,7 @@ static u32
 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
  	 */
  	priority_mask = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_VPMR,
 cpu_if->vgic_vmcr);

arch/arm64/kvm/vgic/vgic-v5.c static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu) · line 241

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5.c +1 -1
-	return min(highest_ap, priority_mask + 1);
+	return min(highest_ap, priority_mask);

Hi Marc,

This part of your change (dropping the - 1) is not correct for GICv5. The GICv[23] PMR works differently to the GICv5 PCR.

For GICv[23] the mask is exclusive, i.e., only higher priority (lower numerical value) interrupts are of sufficient priority to be signalled.

For GICv5, the priority of an interrupt can be equal to or higher than (numerically lower than) the mask. See DMSQKF in the GICv5 spec:

A physical interrupt has Sufficient priority to be signaled when all of the following are true:
* The priority of the interrupt is higher than the physical running priority for the Physical Interrupt Domain.
* The priority of the interrupt is equal to or higher than the Physical Priority Mask for the Physical Interrupt Domain.

Therefore, we require this + 1 for the priority_mask in order to allow us to combine the active priority and priority mask. Else, they operate on different scales.

I'd tried to explain this in a comment that lies just outside the diff, but hadn't explicitly called out that GICv5 operates differently to GICv[23] in this regard. Apologies.

Quoted 12 lines

 }

 /*

arch/arm64/kvm/vgic/vgic-v5.c +0 -0
@@ -367,7 +367,7 @@ bool vgic_v5_has_pending_ppi(struct kvm_vcpu
 *vcpu)


  scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) has_pending = (irq->enabled && irq_is_pending(irq) &&

arch/arm64/kvm/vgic/vgic-v5.c +0 -1
-				       irq->priority <=

priority_mask);

arch/arm64/kvm/vgic/vgic-v5.c +1 -0
+				       irq->priority <
Quoted 1 line

priority_mask);

I agree that this was wrong and should never have included the equality. This was definitely a bug!

Thanks,
Sascha

Quoted 3 lines


  vgic_put_irq(vcpu->kvm, irq);

author maintainer
#11 lore raw

Re: [PATCH 09/15] KVM: arm64: vgic-v5: align priority comparison with other GICs

Quoted 52 lines · Sascha Bischoff
Quoted 29 lines · Marc Zyngier

The way the effective priority mask is computed, and then compared to the priority of an interrupt to decide whether to wake-up or not, is slightly odd, and breaks at the limits.

This could result in spurious wake-ups that are undesirable.

Adopt the GICv[23] logic instead, which checks that the priority value
is strictly lower than the mask.

Fixes 933e5288fa971 ("KVM: arm64: gic-v5: Check for pending PPIs")

Link:
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

---
 arch/arm64/kvm/vgic/vgic-v5.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +0 -0
index 0f269321ece4b..75372bbfb6a6a 100644
@@ -238,7 +238,7 @@ static u32
 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
  	 */
  	priority_mask = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_VPMR,
 cpu_if->vgic_vmcr);

arch/arm64/kvm/vgic/vgic-v5.c static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu) · line 241

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5.c +1 -1
-	return min(highest_ap, priority_mask + 1);
+	return min(highest_ap, priority_mask);

Hi Marc,

This part of your change (dropping the - 1) is not correct for GICv5. The GICv[23] PMR works differently to the GICv5 PCR.

For GICv[23] the mask is exclusive, i.e., only higher priority (lower numerical value) interrupts are of sufficient priority to be signalled.

For GICv5, the priority of an interrupt can be equal to or higher than (numerically lower than) the mask. See DMSQKF in the GICv5 spec:

A physical interrupt has Sufficient priority to be signaled when all of the following are true:
* The priority of the interrupt is higher than the physical running priority for the Physical Interrupt Domain.
* The priority of the interrupt is equal to or higher than the Physical Priority Mask for the Physical Interrupt Domain.

Quoted 7 lines

Therefore, we require this + 1 for the priority_mask in order to allow us to combine the active priority and priority mask. Else, they operate on different scales.

I'd tried to explain this in a comment that lies just outside the diff, but hadn't explicitly called out that GICv5 operates differently to GICv[23] in this regard. Apologies.

Nothing to apologise about, this is me not being able to read.

Quoted 17 lines
Quoted 13 lines

 }

 /*

arch/arm64/kvm/vgic/vgic-v5.c +0 -0
@@ -367,7 +367,7 @@ bool vgic_v5_has_pending_ppi(struct kvm_vcpu
 *vcpu)


  scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) has_pending = (irq->enabled && irq_is_pending(irq) &&

arch/arm64/kvm/vgic/vgic-v5.c +0 -1
-				       irq->priority <=

priority_mask);

arch/arm64/kvm/vgic/vgic-v5.c +1 -0
+				       irq->priority <
Quoted 1 line

priority_mask);

I agree that this was wrong and should never have included the equality. This was definitely a bug!

Cool. I'll revert the revert of the first hunk and keep the second one.

Thanks!

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#12 lore raw

Re: [PATCH 09/15] KVM: arm64: vgic-v5: align priority comparison with other GICs

Quoted 95 lines · Marc Zyngier
Quoted 67 lines · Sascha Bischoff
Quoted 32 lines · Marc Zyngier

The way the effective priority mask is computed, and then compared
to the priority of an interrupt to decide whether to wake-up or not,
is slightly odd, and breaks at the limits.

This could result in spurious wake-ups that are undesirable.

Adopt the GICv[23] logic instead, which checks that the priority value
is strictly lower than the mask.

Fixes 933e5288fa971 ("KVM: arm64: gic-v5: Check for pending

PPIs")
Link:
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

---
 arch/arm64/kvm/vgic/vgic-v5.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-v5.c +0 -0
index 0f269321ece4b..75372bbfb6a6a 100644
@@ -238,7 +238,7 @@ static u32
 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu)
  	 */
  	priority_mask = FIELD_GET(FEAT_GCIE_ICH_VMCR_EL2_VPMR,
 cpu_if->vgic_vmcr);

arch/arm64/kvm/vgic/vgic-v5.c static u32 vgic_v5_get_effective_priority_mask(struct kvm_vcpu *vcpu) · line 241

Quoted 2 lines
arch/arm64/kvm/vgic/vgic-v5.c +1 -1
-	return min(highest_ap, priority_mask + 1);
+	return min(highest_ap, priority_mask);

Hi Marc,

This part of your change (dropping the - 1) is not correct for GICv5.
The GICv[23] PMR works differently to the GICv5 PCR.

For GICv[23] the mask is exclusive, i.e., only higher priority (lower
numerical value) interrupts are of sufficient priority to be signalled.

For GICv5, the priority of an interrupt can be equal to or higher than
(numerically lower than) the mask. See DMSQKF in the GICv5 spec:

A physical interrupt has Sufficient priority to be signaled when all of
the following are true:
   * The priority of the interrupt is higher than the physical running
   priority for the Physical Interrupt Domain.
   * The priority of the interrupt is equal to or higher than the Physical Priority Mask for the Physical Interrupt Domain.
Therefore, we require this + 1 for the priority_mask in order to allow
us to combine the active priority and priority mask. Else, they operate on
different scales.

Quoted 4 lines

I'd tried to explain this in a comment that lies just outside the diff,
but hadn't explicitly called out that GICv5 operates differently to GICv[23] in this regard. Apologies.

Nothing to apologise about, this is me not being able to read.

Quoted 9 lines

Quoted 8 lines

 }

 /*

arch/arm64/kvm/vgic/vgic-v5.c +0 -0
@@ -367,7 +367,7 @@ bool vgic_v5_has_pending_ppi(struct kvm_vcpu
 *vcpu)


  scoped_guard(raw_spinlock_irqsave, &irq-

Quoted 1 line

irq_lock)

Quoted 9 lines
Quoted 6 lines

  has_pending = (irq->enabled && irq_is_pending(irq) &&

arch/arm64/kvm/vgic/vgic-v5.c +0 -1
-				       irq->priority <=

priority_mask);

arch/arm64/kvm/vgic/vgic-v5.c +1 -0
+				       irq->priority <

priority_mask);

I agree that this was wrong and should never have included the equality. This was definitely a bug!

Quoted 2 lines

Cool. I'll revert the revert of the first hunk and keep the second one.

Sounds good. The commit message will need some rejigging too.

Thanks,
Sascha

Quoted 5 lines

Thanks!

	M.
author maintainer
#13 lore raw

PATCH 10/15 KVM: arm64: vgic-v5: Correctly set dist->ready once initialised

In reply to Marc Zyngier

kvm_vgic_map_resources() targetting a v5 model results in vgic->dist_ready never being set. This doesn't result in anything really bad, only some more heavy locking as we go and re-init something for no good reason.

Rejig the code to correctly set the ready flag in all non-failing cases.

Fixes f4d37c7c35769 ("KVM: arm64: gic-v5: Create and initialise vgic_v5")
---
 arch/arm64/kvm/vgic/vgic-init.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +11 -7
index fd872079f2a24..ecb0aea180327 100644
@@ -657,16 +657,20 @@ int kvm_vgic_map_resources(struct kvm *kvm)
 		needs_dist = false;
 	}
 
-	if (ret || !needs_dist)
+	if (ret)
 		goto out;
 
-	dist_base = dist->vgic_dist_base;
-	mutex_unlock(&kvm->arch.config_lock);
+	if (needs_dist) {
+		dist_base = dist->vgic_dist_base;
+		mutex_unlock(&kvm->arch.config_lock);
 
-	ret = vgic_register_dist_iodev(kvm, dist_base, type);
-	if (ret) {
-		kvm_err("Unable to register VGIC dist MMIO regions\n");
-		goto out_slots;
+		ret = vgic_register_dist_iodev(kvm, dist_base, type);
+		if (ret) {
+			kvm_err("Unable to register VGIC dist MMIO regions\n");
+			goto out_slots;
+		}
+	} else {
+		mutex_unlock(&kvm->arch.config_lock);
 	}
 
 	smp_store_release(&dist->ready, true);
Signature · 1 line
2.47.3
author maintainer
#14 lore raw

PATCH 11/15 KVM: arm64: Kill arch_timer_context::direct field

In reply to Marc Zyngier

The newly introduced arch_timer_context::direct field is a bit pointless, as it is always set on timers that are... err... direct, while we already have a way to get to that by doing a get_map() operation.

Additionally, this field is:

- only set when get_map() is called

- never cleared

and the single point where it is actually checked doesn't call get_map() at all.

At this stage, it is probably better to just kill it, and rely on get_map() to give us the correct information.

Fixes 9491c63b6cd7b ("KVM: arm64: gic-v5: Enlighten arch timer for GICv5")
---
 arch/arm64/kvm/arch_timer.c  | 15 +++++++++------
 include/kvm/arm_arch_timer.h |  3 ---
 2 files changed, 9 insertions(+), 9 deletions(-)
arch/arm64/kvm/arch_timer.c +9 -6
index 67b989671b410..37279f8748695 100644
@@ -183,10 +183,6 @@ void get_timer_map(struct kvm_vcpu *vcpu, struct timer_map *map)
 		map->emul_ptimer = vcpu_ptimer(vcpu);
 	}
 
-	map->direct_vtimer->direct = true;
-	if (map->direct_ptimer)
-		map->direct_ptimer->direct = true;
-
 	trace_kvm_get_timer_map(vcpu->vcpu_id, map);
 }
 
@@ -462,8 +458,15 @@ static void kvm_timer_update_irq(struct kvm_vcpu *vcpu, bool new_level,
 		return;
 
 	/* Skip injecting on GICv5 for directly injected (DVI'd) timers */
-	if (vgic_is_v5(vcpu->kvm) && timer_ctx->direct)
-		return;
+	if (vgic_is_v5(vcpu->kvm)) {
+		struct timer_map map;
+
+		get_timer_map(vcpu, &map);
+
+		if (map.direct_ptimer == timer_ctx ||
+		    map.direct_vtimer == timer_ctx)
+			return;
+	}
 
 	kvm_vgic_inject_irq(vcpu->kvm, vcpu,
 			    timer_irq(timer_ctx),
include/kvm/arm_arch_timer.h +0 -3
index a7754e0a2ef41..bf8cc9589bd09 100644
@@ -76,9 +76,6 @@ struct arch_timer_context {
 
 	/* Duplicated state from arch_timer.c for convenience */
 	u32				host_timer_irq;
-
-	/* Is this a direct timer? */
-	bool				direct;
 };
 
 struct timer_map {
Signature · 1 line
2.47.3
author maintainer
#15 lore raw

PATCH 12/15 KVM: arm64: Remove evaluation of timer state in kvm_cpu_has_pending_timer()

In reply to Marc Zyngier

The vgic-v5 code added some evaluations of the timers in a helper funtion (kvm_cpu_has_pending_timer()) that is called to determine whether the vcpu can wake-up.

But looking at the timer there is wrong:

- we want to see timers that are signalling an interrupt to the vcpu, and not just that have a pending interrupt

- we already have kvm_arch_vcpu_runnable() that evaluates the state of interrupts

- kvm_cpu_has_pending_timer() really is about WFIT, as the timeout does not generate an interrupt, and is therefore distinct from the point above

As a consequence, revert these changes.

Fixes 9491c63b6cd7b ("KVM: arm64: gic-v5: Enlighten arch timer for GICv5")
---
 arch/arm64/kvm/arch_timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/kvm/arch_timer.c +1 -5
index 37279f8748695..6608c47d1f628 100644
@@ -402,11 +402,7 @@ static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
 
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
-	struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
-	struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
-
-	return kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer) ||
-	       (vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0);
+	return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;
 }
 
 /*
Signature · 1 line
2.47.3
#16 lore raw

Re: [PATCH 12/15] KVM: arm64: Remove evaluation of timer state in kvm_cpu_has_pending_timer()

Quoted 46 lines · Marc Zyngier

The vgic-v5 code added some evaluations of the timers in a helper funtion
(kvm_cpu_has_pending_timer()) that is called to determine whether the vcpu can wake-up.

But looking at the timer there is wrong:

- we want to see timers that are signalling an interrupt to the vcpu, and not just that have a pending interrupt

- we already have kvm_arch_vcpu_runnable() that evaluates the state of interrupts

- kvm_cpu_has_pending_timer() really is about WFIT, as the timeout does not generate an interrupt, and is therefore distinct from the point above

As a consequence, revert these changes.

Fixes 9491c63b6cd7b ("KVM: arm64: gic-v5: Enlighten arch timer for

GICv5")
Link:
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

---
 arch/arm64/kvm/arch_timer.c | 6 +-----

 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/kvm/arch_timer.c +0 -0
index 37279f8748695..6608c47d1f628 100644
@@ -402,11 +402,7 @@ static bool kvm_timer_should_fire(struct
 arch_timer_context *timer_ctx)


 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/arch_timer.c +0 -4
-	struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
-	struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
-
-	return kvm_timer_should_fire(vtimer) ||

kvm_timer_should_fire(ptimer) ||

arch/arm64/kvm/arch_timer.c +0 -1
-	       (vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) ==

0);

arch/arm64/kvm/arch_timer.c +1 -0
+	return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) ==
Quoted 4 lines

0;
 }

 /*

Hi Marc,

It appears that I'd misunderstood the intent of this function when I originally wrote this bit code. That is: I agree that these checks shouldn't be here.

However, said checks are needed somewhere. With GICv5, we directly inject the timer state (when possible, at least) which means that we never see the timer interrupt firing on the host, and don't track if it is pending or not in struct vgic_irq as the pending state is driven by the hardware itself. The result of this is that we explicitly need to check if the timer interrupt would be pending if the guest were running somewhere.

I've run with this complete series and have tested the following change. It is sufficient to catch this case, and does it as part of checking if there are pending interrupts, i.e., a more appropriate place called via kvm_arch_vcpu_runnable(). It is yet-another GICv5 special case, however. I'd love to hear your thoughts.

arch/arm64/kvm/arch_timer.c +8 -0
index cbea4d9ee9552..f8b95721857c3 100644
@@ -400,6 +400,14 @@ static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
        return cval <= now;
 }
 
+int kvm_cpu_timer_should_fire(struct kvm_vcpu *vcpu)
+{
+       struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
+       struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
+
+       return kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer);
+}
+
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
        return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;
arch/arm64/kvm/vgic/vgic.c +3 -0
index 7680ced92f715..ffb91f535efe8 100644
@@ -1238,6 +1238,9 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
                if (READ_ONCE(vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db_fired))
                        return true;
 
+               if (kvm_cpu_timer_should_fire(vcpu))
+                       return true;
+
                return vgic_v5_has_pending_ppi(vcpu);
        }
 
include/kvm/arm_vgic.h +1 -0
index f0e56ad36bf1f..1bd6f23979837 100644
@@ -825,6 +825,7 @@ int kvm_vgic_get_map(struct kvm_vcpu *vcpu, unsigned int vintid);
 bool kvm_vgic_map_is_active(struct kvm_vcpu *vcpu, unsigned int vintid);
 
 int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);
+int kvm_cpu_timer_should_fire(struct kvm_vcpu *vcpu);
 
 void kvm_vgic_load(struct kvm_vcpu *vcpu);
 void kvm_vgic_put(struct kvm_vcpu *vcpu);
author maintainer
#17 lore raw

Re: [PATCH 12/15] KVM: arm64: Remove evaluation of timer state in kvm_cpu_has_pending_timer()

Quoted 97 lines · Sascha Bischoff
Quoted 46 lines · Marc Zyngier

The vgic-v5 code added some evaluations of the timers in a helper funtion
(kvm_cpu_has_pending_timer()) that is called to determine whether the vcpu can wake-up.

But looking at the timer there is wrong:

- we want to see timers that are signalling an interrupt to the vcpu, and not just that have a pending interrupt

- we already have kvm_arch_vcpu_runnable() that evaluates the state of interrupts

- kvm_cpu_has_pending_timer() really is about WFIT, as the timeout does not generate an interrupt, and is therefore distinct from the point above

As a consequence, revert these changes.

Fixes 9491c63b6cd7b ("KVM: arm64: gic-v5: Enlighten arch timer for

GICv5")
Link:
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

---
 arch/arm64/kvm/arch_timer.c | 6 +-----

 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/kvm/arch_timer.c +0 -0
index 37279f8748695..6608c47d1f628 100644
@@ -402,11 +402,7 @@ static bool kvm_timer_should_fire(struct
 arch_timer_context *timer_ctx)


 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/arch_timer.c +0 -4
-	struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
-	struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
-
-	return kvm_timer_should_fire(vtimer) ||

kvm_timer_should_fire(ptimer) ||

arch/arm64/kvm/arch_timer.c +0 -1
-	       (vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) ==

0);

arch/arm64/kvm/arch_timer.c +1 -0
+	return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) ==
Quoted 4 lines

0;
 }

 /*

Hi Marc,

It appears that I'd misunderstood the intent of this function when I originally wrote this bit code. That is: I agree that these checks shouldn't be here.

However, said checks are needed somewhere. With GICv5, we directly inject the timer state (when possible, at least) which means that we never see the timer interrupt firing on the host, and don't track if it is pending or not in struct vgic_irq as the pending state is driven by the hardware itself. The result of this is that we explicitly need to check if the timer interrupt would be pending if the guest were running somewhere.

I've run with this complete series and have tested the following change. It is sufficient to catch this case, and does it as part of checking if there are pending interrupts, i.e., a more appropriate place called via kvm_arch_vcpu_runnable(). It is yet-another GICv5 special case, however. I'd love to hear your thoughts.

arch/arm64/kvm/arch_timer.c +8 -0
index cbea4d9ee9552..f8b95721857c3 100644
@@ -400,6 +400,14 @@ static bool kvm_timer_should_fire(struct arch_timer_context *timer_ctx)
        return cval <= now;
 }
 
+int kvm_cpu_timer_should_fire(struct kvm_vcpu *vcpu)
+{
+       struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
+       struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
+
+       return kvm_timer_should_fire(vtimer) || kvm_timer_should_fire(ptimer);
+}
+
 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
        return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;
arch/arm64/kvm/vgic/vgic.c +0 -0
index 7680ced92f715..ffb91f535efe8 100644

arch/arm64/kvm/vgic/vgic.c int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu) · line 1243

Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +3 -0
@@ -1238,6 +1238,9 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
                if (READ_ONCE(vcpu->arch.vgic_cpu.vgic_v5.gicv5_vpe.db_fired))
                        return true;
 
+               if (kvm_cpu_timer_should_fire(vcpu))
+                       return true;
+

This unfortunately seems to suffer from the exact same problem: you are evaluating the output of the timer independently of the enable bits gating the timer interrupt at the GIC level.

With this, you can disable the timers at the GIC level, arm the timers so that they are in a firing position, and enter WFI: the vcpu will exit WFI immediately, which is not the expected result.

I'd suggest something like this instead (compile tested only):

arch/arm64/kvm/vgic/vgic-v5.c +6 -2
index 75372bbfb6a6a..e7d23d0519e8b 100644
@@ -365,9 +365,13 @@ bool vgic_v5_has_pending_ppi(struct kvm_vcpu *vcpu)
 
 		irq = vgic_get_vcpu_irq(vcpu, intid);
 
-		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
-			has_pending = (irq->enabled && irq_is_pending(irq) &&
+		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
+			bool pending;
+
+			pending = irq->hw ? vgic_get_phys_line_level(irq) : irq_is_pending(irq);
+			has_pending = (irq->enabled && pending &&
 				       irq->priority < priority_mask);
+		}
 
 		vgic_put_irq(vcpu->kvm, irq);
 

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#18 lore raw

Re: [PATCH 12/15] KVM: arm64: Remove evaluation of timer state in kvm_cpu_has_pending_timer()

Quoted 156 lines · Marc Zyngier
Quoted 114 lines · Sascha Bischoff
Quoted 49 lines · Marc Zyngier

The vgic-v5 code added some evaluations of the timers in a helper funtion
(kvm_cpu_has_pending_timer()) that is called to determine whether the vcpu can wake-up.

But looking at the timer there is wrong:

- we want to see timers that are signalling an interrupt to the vcpu, and not just that have a pending interrupt

- we already have kvm_arch_vcpu_runnable() that evaluates the state of interrupts

- kvm_cpu_has_pending_timer() really is about WFIT, as the timeout
  does not generate an interrupt, and is therefore distinct from the point above

As a consequence, revert these changes.

Fixes 9491c63b6cd7b ("KVM: arm64: gic-v5: Enlighten arch timer

for
GICv5")
Link:
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

---
 arch/arm64/kvm/arch_timer.c | 6 +-----

 1 file changed, 1 insertion(+), 5 deletions(-)
arch/arm64/kvm/arch_timer.c +0 -0
index 37279f8748695..6608c47d1f628 100644
@@ -402,11 +402,7 @@ static bool kvm_timer_should_fire(struct
 arch_timer_context *timer_ctx)


 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) {

arch/arm64/kvm/arch_timer.c +0 -4
-	struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
-	struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
-
-	return kvm_timer_should_fire(vtimer) ||

kvm_timer_should_fire(ptimer) ||

arch/arm64/kvm/arch_timer.c +0 -1
-	       (vcpu_has_wfit_active(vcpu) &&

wfit_delay_ns(vcpu) ==
0);

arch/arm64/kvm/arch_timer.c +1 -0
+	return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu)
Quoted 5 lines

==
0;
 }

 /*

Hi Marc,

It appears that I'd misunderstood the intent of this function when I
originally wrote this bit code. That is: I agree that these checks shouldn't be here.

However, said checks are needed somewhere. With GICv5, we directly inject the timer state (when possible, at least) which means that we
never see the timer interrupt firing on the host, and don't track if it
is pending or not in struct vgic_irq as the pending state is driven by
the hardware itself. The result of this is that we explicitly need to
check if the timer interrupt would be pending if the guest were running
somewhere.

I've run with this complete series and have tested the following change. It is sufficient to catch this case, and does it as part of checking if there are pending interrupts, i.e., a more appropriate place called via kvm_arch_vcpu_runnable(). It is yet-another GICv5 special case, however. I'd love to hear your thoughts.

arch/arm64/kvm/arch_timer.c +0 -0
index cbea4d9ee9552..f8b95721857c3 100644
@@ -400,6 +400,14 @@ static bool kvm_timer_should_fire(struct
 arch_timer_context *timer_ctx)
         return cval <= now;
  }

arch/arm64/kvm/arch_timer.c +6 -0
+int kvm_cpu_timer_should_fire(struct kvm_vcpu *vcpu)
+{
+       struct arch_timer_context *vtimer = vcpu_vtimer(vcpu);
+       struct arch_timer_context *ptimer = vcpu_ptimer(vcpu);
+
+       return kvm_timer_should_fire(vtimer) ||

kvm_timer_should_fire(ptimer);

arch/arm64/kvm/arch_timer.c +2 -0
+}
+

 int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
 {
        return vcpu_has_wfit_active(vcpu) && wfit_delay_ns(vcpu) == 0;

arch/arm64/kvm/vgic/vgic.c +0 -0
index 7680ced92f715..ffb91f535efe8 100644
@@ -1238,6 +1238,9 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu
 *vcpu)

                if (READ_ONCE(vcpu-

Quoted 6 lines
Quoted 1 line

arch.vgic_cpu.vgic_v5.gicv5_vpe.db_fired))

                        return true;

+               if (kvm_cpu_timer_should_fire(vcpu)) +                       return true;
+

This unfortunately seems to suffer from the exact same problem: you are evaluating the output of the timer independently of the enable bits gating the timer interrupt at the GIC level.

With this, you can disable the timers at the GIC level, arm the timers
so that they are in a firing position, and enter WFI: the vcpu will exit WFI immediately, which is not the expected result.

I'd suggest something like this instead (compile tested only):

arch/arm64/kvm/vgic/vgic-v5.c +0 -0
index 75372bbfb6a6a..e7d23d0519e8b 100644
@@ -365,9 +365,13 @@ bool vgic_v5_has_pending_ppi(struct kvm_vcpu
 *vcpu)


  irq = vgic_get_vcpu_irq(vcpu, intid);

arch/arm64/kvm/vgic/vgic-v5.c +0 -2
-		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock)
-			has_pending = (irq->enabled &&

irq_is_pending(irq) &&

arch/arm64/kvm/vgic/vgic-v5.c +4 -0
+		scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
+			bool pending;
+
+			pending = irq->hw ?

vgic_get_phys_line_level(irq) : irq_is_pending(irq);

arch/arm64/kvm/vgic/vgic-v5.c +1 -0
+			has_pending = (irq->enabled && pending &&

         irq->priority < priority_mask);

arch/arm64/kvm/vgic/vgic-v5.c +1 -0
+		}
Quoted 3 lines


  vgic_put_irq(vcpu->kvm, irq);

I've just tested the above and can confirm that it does work as expected. It indeed makes a lot more sense than what I'd suggested.

Thanks,
Sascha

Quoted 5 lines

Thanks,

	M.
author maintainer
#19 lore raw

PATCH 13/15 KVM: arm64: Move GICv5 timer PPI validation into timer_irqs_are_valid()

In reply to Marc Zyngier

Userspace can set the timer PPI numbers way before a GIC has been created, leading to odd behaviours on GICv5 as we'd accept non architectural PPI numbers.

Move the v5 check into timer_irqs_are_valid(), which aligns the behaviour with the pre-v5 GICs, and is also guaranteed to run only once a GIC has been configured.

Fixes 9491c63b6cd7b ("KVM: arm64: gic-v5: Enlighten arch timer for GICv5")
---
 arch/arm64/kvm/arch_timer.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
arch/arm64/kvm/arch_timer.c +4 -7
index 6608c47d1f628..cbea4d9ee9552 100644
@@ -1543,6 +1543,10 @@ static bool timer_irqs_are_valid(struct kvm_vcpu *vcpu)
 		if (kvm_vgic_set_owner(vcpu, irq, ctx))
 			break;
 
+		/* With GICv5, the default PPI is what you get -- nothing else */
+		if (vgic_is_v5(vcpu->kvm) && irq != get_vgic_ppi(vcpu->kvm, default_ppi[i]))
+			break;
+
 		/*
 		 * We know by construction that we only have PPIs, so all values
 		 * are less than 32 for non-GICv5 VGICs. On GICv5, they are
@@ -1678,13 +1682,6 @@ int kvm_arm_timer_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 		return -ENXIO;
 	}
 
-	/*
-	 * The PPIs for the Arch Timers are architecturally defined for
-	 * GICv5. Reject anything that changes them from the specified value.
-	 */
-	if (vgic_is_v5(vcpu->kvm) && vcpu->kvm->arch.timer_data.ppi[idx] != irq)
-		return -EINVAL;
-
 	/*
 	 * We cannot validate the IRQ unicity before we run, so take it at
 	 * face value. The verdict will be given on first vcpu run, for each
Signature · 1 line
2.47.3
author maintainer
#20 lore raw

PATCH 14/15 KVM: arm64: Correctly plumb ID_AA64PFR2_EL1 into pkvm idreg handling

In reply to Marc Zyngier

While we now compute ID_AA64PFR2_EL1 to a glorious 0, we never use that data and instead return the 0 that corresponds to an allocated idreg. Not a big deal, but we might as well be consistent.

Fixes 5aefaf11f9af5 ("KVM: arm64: gic: Hide GICv5 for protected guests")
---
 arch/arm64/kvm/hyp/nvhe/sys_regs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/sys_regs.c +1 -1
index b40fd01ebf329..be6f420388a14 100644
@@ -439,7 +439,7 @@ static const struct sys_reg_desc pvm_sys_reg_descs[] = {
 	/* CRm=4 */
 	AARCH64(SYS_ID_AA64PFR0_EL1),
 	AARCH64(SYS_ID_AA64PFR1_EL1),
-	ID_UNALLOCATED(4,2),
+	AARCH64(SYS_ID_AA64PFR2_EL1),
 	ID_UNALLOCATED(4,3),
 	AARCH64(SYS_ID_AA64ZFR0_EL1),
 	ID_UNALLOCATED(4,5),
Signature · 1 line
2.47.3
author maintainer
#21 lore raw

PATCH 15/15 KVM: arm64: Don't advertises GICv3 in ID_PFR1_EL1 if AArch32 isn't supported

In reply to Marc Zyngier

Although the AArch32 ID regs are architecturally UNKNOWN when AArch32 isn't supported at any EL, KVM makes a point in making them RAZ.

Therefore, advertising GICv3 in ID_PFR1_EL1 must be gated on AArch32 being supported at least at EL0.

Fixes a258a383b9177 ("KVM: arm64: gic-v5: Sanitize ID_AA64PFR2_EL1.GCIE")
---
 arch/arm64/kvm/vgic/vgic-init.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-init.c +2 -1
index ecb0aea180327..5c684ecf79e66 100644
@@ -700,7 +700,8 @@ void kvm_vgic_finalize_idregs(struct kvm *kvm)
 		break;
 	case KVM_DEV_TYPE_ARM_VGIC_V3:
 		aa64pfr0 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, GIC, IMP);
-		pfr1 |= SYS_FIELD_PREP_ENUM(ID_PFR1_EL1, GIC, GICv3);
+		if (kvm_supports_32bit_el0())
+			pfr1 |= SYS_FIELD_PREP_ENUM(ID_PFR1_EL1, GIC, GICv3);
 		break;
 	case KVM_DEV_TYPE_ARM_VGIC_V5:
 		aa64pfr2 |= SYS_FIELD_PREP_ENUM(ID_AA64PFR2_EL1, GCIE, IMP);
Signature · 1 line
2.47.3
Tested-by on the v1 series
#22 lore raw
In reply to Marc Zyngier
Quoted 4 lines · Marc Zyngier

Well, merging the first batch of vgic-v5 patches didn't go smoothly at all. We initially found a couple of regressions, but most of the crap was actually uncovered by everyone's new best friend (enemy?), the AI bot sitting behind sashiko.dev [1].

I threw this at my CI, nothing blew up though that's not testing any GICv5 stuff particuarly. FWIW:

Reviewed-by on the v1 series
#23 lore raw
In reply to Marc Zyngier
Quoted 13 lines · Marc Zyngier

Well, merging the first batch of vgic-v5 patches didn't go smoothly at
all. We initially found a couple of regressions, but most of the crap was actually uncovered by everyone's new best friend (enemy?), the AI bot sitting behind sashiko.dev [1].

While not all of the remarks were valid, a bunch of them were actually
extremely pertinent, and resulted in me frantically hacking away at the series. Hopefully the bot doesn't find even more issues in the fixes. Note that the first patch has already been posted and merged, and is only here for reference.

Quoted 6 lines

Note that given the amount of rework, I'm really in two minds between adding these patches on top, or pulling the whole series for another cycle. I guess time will tell.

[1]
https://sashiko.dev/#/patchset/20260319154937.3619520-1-sascha.bischoff%40arm.com

Hi Marc,

Thanks so much for taking the time to go through things, and to fix up these issues. Apologies for introducing them in the first place!

I'll spend some time going through the rest of the issues flagged at
[1] to see which others are true positives and provide fixes for them.

I've gone through this series, and am happy with everything except for 'KVM: arm64: vgic-v5: align priority comparison with other GICs', as I've mentioned in the thread there. In summary, the + 1 is needed due to GICv5's priority masking working a little different to that of GICv[23].

When it comes to 'KVM: arm64: Remove evaluation of timer state in kvm_cpu_has_pending_timer()', I agree with the change - doing the check in that location was definitely wrong. However, a similar check is required on GICv5 due to the DVI mechanism being used on GICv5. I've provided a potential (tested) fix in the thread there.

For all of these, with the exception of #9 ('KVM: arm64: vgic-v5: align priority comparison with other GICs'), I'm happy for you to consider them as:

Thanks,
Sascha

Quoted 37 lines

Marc Zyngier (15):
  KVM: arm64: vgic: Don't reset cpuif/redist addresses at finalize time
  KVM: arm64: Don't skip per-vcpu NV initialisation
  arm64: Fix field references for ICH_PPI_DVIR[01]_EL2
  KVM: arm64: Fix writeable mask for ID_AA64PFR2_EL1
  KVM: arm64: Account for RESx bits in __compute_fgt()
  KVM: arm64: vgic-v5: Hold config_lock while finalizing GICv5 PPIs
  KVM: arm64: vgic-v5: Transfer edge pending state to ICH_PPI_PENDRx_EL2
  KVM: arm64: vgic-v5: Cast vgic_apr to u32 to avoid undefined behaviours
  KVM: arm64: vgic-v5: align priority comparison with other GICs
  KVM: arm64: vgic-v5: Correctly set dist->ready once initialised
  KVM: arm64: Kill arch_timer_context::direct field
  KVM: arm64: Remove evaluation of timer state in kvm_cpu_has_pending_timer()
  KVM: arm64: Move GICv5 timer PPI validation into timer_irqs_are_valid()
  KVM: arm64: Correctly plumb ID_AA64PFR2_EL1 into pkvm idreg handling
  KVM: arm64: Don't advertises GICv3 in ID_PFR1_EL1 if AArch32 isn't supported

 arch/arm64/kvm/arch_timer.c        | 32 +++++++++++++--------------- --
 arch/arm64/kvm/config.c            |  4 ++-- arch/arm64/kvm/hyp/nvhe/sys_regs.c |  2 +- arch/arm64/kvm/sys_regs.c          | 20 +++++++++---------- arch/arm64/kvm/vgic/vgic-init.c    | 32 ++++++++++++++++++++-------- --
 arch/arm64/kvm/vgic/vgic-v5.c      | 24 +++++++++++++++++----- arch/arm64/tools/sysreg            |  4 ++-- include/kvm/arm_arch_timer.h       |  3 ---

 8 files changed, 70 insertions(+), 51 deletions(-)