mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes
@ 2026-12-06 13:53 Jia He
  2026-12-06 13:53 ` [PATCH 01/10] KVM: arm64: Preserve vSGI state when pending queries fail Jia He
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

This series fixes GICv4.1 vSGI error handling and VGIC state
synchronisation bugs.

Patches 1-4 handle ITS and redistributor failures in the vSGI code.
Previously, KVM ignored failed pending queries and only logged command
queue errors, including failures to clear pending state when moving a
vSGI to SW. On failure, KVM now keeps interrupt state consistent and
marks the VM dead.

Patches 5-7 fix missed or unwanted interrupts when enabling the
distributor or switching vSGIs between HW and SW, for both guest writes
and userspace restore:

- Patch 5: interrupts made pending while the distributor is disabled
  were not delivered after re-enabling it.
- Patch 6: SGI enable/disable writes from the guest or userspace did
  not reach the ITS for direct vSGIs.
- Patch 7: userspace writes to GICD_CTLR did not switch vSGIs between
  HW and SW (depends on patch 6 because userspace restores GICD_CTLR
  before the SGI enable bits).

Patches 8-10 update the KVM selftests: patch 8 fixes GIC write-one
register helpers in the selftest library, and patches 9 and 10 add
selftests for the fixes above.

Patch 2 touches drivers/irqchip and is needed by the following KVM
patches. I'd suggest taking the whole series through the kvmarm tree,
with an ack from the irqchip side.

Testing (on GIC-700 / GICv4.1):

- Existing and new vgic selftests plus guest boot and vSGI traffic pass.
- vgic_dist_reenable fails without patch 5 (SPI, vtimer PPI, and LPI
  are not delivered).
- vgic_sgi_restore fails without patch 6 or 7: without patch 6 the
  guest takes a disabled SGI in enable-warm and enable-guest (and in
  enable-cold with patch 7 applied). Patch 8 was verified separately.
- Patches 1-4 and patch 6's error paths were tested with local fault
  injection failing the Nth pending query, activation, pending
  transfer, or VSGI command on a 4-vCPU VM (triggered by userspace
  GICD_CTLR write or guest toggle under concurrent SGI traffic).
  Without patch 4 or patch 6's error handling, the guest takes an SGI
  twice after switching to SW or takes a disabled SGI. With them, the
  userspace write or KVM_RUN returns -EIO, the VM is marked dead, and
  no warnings or leaked IRQs occur.

Jia He (10):
  KVM: arm64: Preserve vSGI state when pending queries fail
  irqchip/gic-v3-its: Propagate vSGI command queue failures
  KVM: arm64: Stop vSGI enablement on state transfer errors
  KVM: arm64: Check clearing the pending state of vSGIs moved to SW
  KVM: arm64: vgic: Queue pending interrupts when the distributor is
    enabled
  KVM: arm64: vgic: Pass the enable state of HW SGIs to the ITS
  KVM: arm64: Synchronize vSGI state when restoring GICD_CTLR
  KVM: arm64: selftests: Avoid read-modify-write for GIC write-one
    registers
  KVM: arm64: selftests: Add vgic_dist_reenable test
  KVM: arm64: selftests: Check SGI delivery after VGIC restore

 arch/arm64/kvm/vgic/vgic-mmio-v2.c            |   4 +-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c            |  36 ++-
 arch/arm64/kvm/vgic/vgic-mmio.c               |  69 ++--
 arch/arm64/kvm/vgic/vgic-v4.c                 |  71 ++--
 arch/arm64/kvm/vgic/vgic.c                    |  38 +++
 arch/arm64/kvm/vgic/vgic.h                    |   3 +-
 drivers/irqchip/irq-gic-v3-its.c              |  26 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +
 .../selftests/kvm/arm64/vgic_dist_reenable.c  | 228 +++++++++++++
 .../selftests/kvm/arm64/vgic_sgi_restore.c    | 306 ++++++++++++++++++
 .../testing/selftests/kvm/lib/arm64/gic_v3.c  |  14 +-
 11 files changed, 740 insertions(+), 57 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/arm64/vgic_dist_reenable.c
 create mode 100644 tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c


base-commit: eedaa7d61f1ec8ae053a9ece0d553785350ac486
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 01/10] KVM: arm64: Preserve vSGI state when pending queries fail
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 02/10] irqchip/gic-v3-its: Propagate vSGI command queue failures Jia He
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

vgic_v4_disable_vsgis() clears irq->hw before reading the pending state
from the GIC. On failure, an uninitialised 'pending' is copied into
pending_latch and the SGI is deactivated anyway.

Switch the SGI to SW only after reading its pending state. On failure,
leave the SGI in HW, drop the lock and reference, and abort the switch.
Because rolling back already-switched SGIs would rely on the same
failing ITS/redistributor operations, mark the VM dead before resuming
the guest.

Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/vgic/vgic-v4.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-v4.c b/arch/arm64/kvm/vgic/vgic-v4.c
index ed236f083f0d..b4f8e5653bf9 100644
--- a/arch/arm64/kvm/vgic/vgic-v4.c
+++ b/arch/arm64/kvm/vgic/vgic-v4.c
@@ -155,28 +155,28 @@ static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
 	}
 }
 
-static void vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
+static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
 {
-	int i;
+	int i, ret = 0;
 
 	for (i = 0; i < VGIC_NR_SGIS; i++) {
 		struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, i);
 		struct irq_desc *desc;
 		unsigned long flags;
 		bool pending;
-		int ret;
 
 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
 
 		if (!irq->hw)
 			goto unlock;
 
-		irq->hw = false;
 		ret = irq_get_irqchip_state(irq->host_irq,
 					    IRQCHIP_STATE_PENDING,
 					    &pending);
-		WARN_ON(ret);
+		if (ret)
+			goto unlock;
 
+		irq->hw = false;
 		irq->pending_latch = pending;
 
 		desc = irq_to_desc(irq->host_irq);
@@ -184,7 +184,11 @@ static void vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
 	unlock:
 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
 		vgic_put_irq(vcpu->kvm, irq);
+		if (ret)
+			return ret;
 	}
+
+	return 0;
 }
 
 void vgic_v4_configure_vsgis(struct kvm *kvm)
@@ -200,8 +204,11 @@ void vgic_v4_configure_vsgis(struct kvm *kvm)
 	kvm_for_each_vcpu(i, vcpu, kvm) {
 		if (dist->nassgireq)
 			vgic_v4_enable_vsgis(vcpu);
-		else
-			vgic_v4_disable_vsgis(vcpu);
+		else if (vgic_v4_disable_vsgis(vcpu)) {
+			/* There is no way back from a partial switch */
+			kvm_vm_dead(kvm);
+			break;
+		}
 	}
 
 	kvm_arm_resume_guest(kvm);
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 02/10] irqchip/gic-v3-its: Propagate vSGI command queue failures
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
  2026-12-06 13:53 ` [PATCH 01/10] KVM: arm64: Preserve vSGI state when pending queries fail Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 03/10] KVM: arm64: Stop vSGI enablement on state transfer errors Jia He
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

The single command helpers only log an error when the command queue is
full or a command times out. The vSGI CLEAR, PROP_UPDATE_VSGI, and
domain activate callbacks therefore return 0 even when the VSGI command
or its trailing SYNC/VSYNC failed.

Return the error from the single command helpers and propagate it
through its_configure_sgi() to these three callbacks, so
irq_domain_activate_irq() does not mark an IRQ activated after a failed
configuration and KVM callers observe failures. Command posting
behaviour and other callers are unchanged.

An error only means completion could not be confirmed; a timed-out
command may still execute later. Existing WARN_ON() and
WARN_RATELIMIT() in the KVM vSGI MMIO handlers now trigger on ITS
failures instead of only logging.

Signed-off-by: Jia He <justin.he@arm.com>
---
 drivers/irqchip/irq-gic-v3-its.c | 26 +++++++++++++++-----------
 1 file changed, 15 insertions(+), 11 deletions(-)

diff --git a/drivers/irqchip/irq-gic-v3-its.c b/drivers/irqchip/irq-gic-v3-its.c
index 282e4bafb084..b302d1ea446c 100644
--- a/drivers/irqchip/irq-gic-v3-its.c
+++ b/drivers/irqchip/irq-gic-v3-its.c
@@ -1212,7 +1212,7 @@ static int its_wait_for_range_completion(struct its_node *its,
 
 /* Warning, macro hell follows */
 #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn)	\
-void name(struct its_node *its,						\
+int name(struct its_node *its,						\
 	  buildtype builder,						\
 	  struct its_cmd_desc *desc)					\
 {									\
@@ -1220,21 +1220,24 @@ void name(struct its_node *its,						\
 	synctype *sync_obj;						\
 	unsigned long flags;						\
 	u64 rd_idx;							\
+	int ret = 0;							\
 									\
 	raw_spin_lock_irqsave(&its->lock, flags);			\
 									\
 	cmd = its_allocate_entry(its);					\
 	if (!cmd) {		/* We're soooooo screewed... */		\
 		raw_spin_unlock_irqrestore(&its->lock, flags);		\
-		return;							\
+		return -EBUSY;						\
 	}								\
 	sync_obj = builder(its, cmd, desc);				\
 	its_flush_cmd(its, cmd);					\
 									\
 	if (sync_obj) {							\
 		sync_cmd = its_allocate_entry(its);			\
-		if (!sync_cmd)						\
+		if (!sync_cmd) {					\
+			ret = -EBUSY;					\
 			goto post;					\
+		}							\
 									\
 		buildfn(its, sync_cmd, sync_obj);			\
 		its_flush_cmd(its, sync_cmd);				\
@@ -1245,8 +1248,11 @@ post:									\
 	next_cmd = its_post_commands(its);				\
 	raw_spin_unlock_irqrestore(&its->lock, flags);			\
 									\
-	if (its_wait_for_range_completion(its, rd_idx, next_cmd))	\
+	if (its_wait_for_range_completion(its, rd_idx, next_cmd)) {	\
 		pr_err_ratelimited("ITS cmd %ps failed\n", builder);	\
+		ret = -ETIMEDOUT;					\
+	}								\
+	return ret;							\
 }
 
 static void its_build_sync_cmd(struct its_node *its,
@@ -4333,7 +4339,7 @@ static struct irq_chip its_vpe_4_1_irq_chip = {
 	.irq_set_vcpu_affinity	= its_vpe_4_1_set_vcpu_affinity,
 };
 
-static void its_configure_sgi(struct irq_data *d, bool clear)
+static int its_configure_sgi(struct irq_data *d, bool clear)
 {
 	struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
 	struct its_cmd_desc desc;
@@ -4350,7 +4356,7 @@ static void its_configure_sgi(struct irq_data *d, bool clear)
 	 * destination VPE is mapped there. Since we map them eagerly at
 	 * activation time, we're pretty sure the first GICv4.1 ITS will do.
 	 */
-	its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
+	return its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
 }
 
 static void its_sgi_mask_irq(struct irq_data *d)
@@ -4398,7 +4404,7 @@ static int its_sgi_set_irqchip_state(struct irq_data *d,
 		val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq);
 		writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K);
 	} else {
-		its_configure_sgi(d, true);
+		return its_configure_sgi(d, true);
 	}
 
 	return 0;
@@ -4466,8 +4472,7 @@ static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
 	case PROP_UPDATE_VSGI:
 		vpe->sgi_config[d->hwirq].priority = info->priority;
 		vpe->sgi_config[d->hwirq].group = info->group;
-		its_configure_sgi(d, false);
-		return 0;
+		return its_configure_sgi(d, false);
 
 	default:
 		return -EINVAL;
@@ -4518,8 +4523,7 @@ static int its_sgi_irq_domain_activate(struct irq_domain *domain,
 				       struct irq_data *d, bool reserve)
 {
 	/* Write out the initial SGI configuration */
-	its_configure_sgi(d, false);
-	return 0;
+	return its_configure_sgi(d, false);
 }
 
 static void its_sgi_irq_domain_deactivate(struct irq_domain *domain,
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 03/10] KVM: arm64: Stop vSGI enablement on state transfer errors
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
  2026-12-06 13:53 ` [PATCH 01/10] KVM: arm64: Preserve vSGI state when pending queries fail Jia He
  2026-12-06 13:53 ` [PATCH 02/10] irqchip/gic-v3-its: Propagate vSGI command queue failures Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 04/10] KVM: arm64: Check clearing the pending state of vSGIs moved to SW Jia He
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

vgic_v4_enable_vsgis() sets irq->hw before activating the SGI and
clears pending_latch even if transferring the pending state fails,
leaving the guest with partially switched SGIs.

Set irq->hw only after activation succeeds, and clear pending_latch
only after the pending transfer succeeds. On error, drop the lock and
reference and return it so the caller marks the VM dead before resuming
the guest.

After a failed activation, irq->hw stays clear to match the IRQ core.
If the pending transfer fails after activation, irq->hw stays set and
pending_latch is kept; only clearing pending can fail (setting it is a
write to GITS_SGIR), so a late CLEAR cannot cause a duplicate SGI.

Fixes: bacf2c60548b ("KVM: arm64: GICv4.1: Allow SGIs to switch between HW and SW interrupts")
Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/vgic/vgic-v4.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-v4.c b/arch/arm64/kvm/vgic/vgic-v4.c
index b4f8e5653bf9..fd0e7b2521c8 100644
--- a/arch/arm64/kvm/vgic/vgic-v4.c
+++ b/arch/arm64/kvm/vgic/vgic-v4.c
@@ -112,10 +112,10 @@ static void vgic_v4_sync_sgi_config(struct its_vpe *vpe, struct vgic_irq *irq)
 	vpe->sgi_config[irq->intid].priority	= irq->priority;
 }
 
-static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
+static int vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
 {
 	struct its_vpe *vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
-	int i;
+	int i, ret = 0;
 
 	/*
 	 * With GICv4.1, every virtual SGI can be directly injected. So
@@ -126,14 +126,12 @@ static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
 		struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, i);
 		struct irq_desc *desc;
 		unsigned long flags;
-		int ret;
 
 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
 
 		if (irq->hw)
 			goto unlock;
 
-		irq->hw = true;
 		irq->host_irq = irq_find_mapping(vpe->sgi_domain, i);
 
 		/* Transfer the full irq state to the vPE */
@@ -141,18 +139,24 @@ static void vgic_v4_enable_vsgis(struct kvm_vcpu *vcpu)
 		desc = irq_to_desc(irq->host_irq);
 		ret = irq_domain_activate_irq(irq_desc_get_irq_data(desc),
 					      false);
-		if (!WARN_ON(ret)) {
-			/* Transfer pending state */
-			ret = irq_set_irqchip_state(irq->host_irq,
-						    IRQCHIP_STATE_PENDING,
-						    irq->pending_latch);
-			WARN_ON(ret);
+		if (ret)
+			goto unlock;
+
+		irq->hw = true;
+		/* Transfer the pending state, keep the latch if that fails */
+		ret = irq_set_irqchip_state(irq->host_irq,
+					    IRQCHIP_STATE_PENDING,
+					    irq->pending_latch);
+		if (!ret)
 			irq->pending_latch = false;
-		}
 	unlock:
 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
 		vgic_put_irq(vcpu->kvm, irq);
+		if (ret)
+			return ret;
 	}
+
+	return 0;
 }
 
 static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
@@ -196,6 +200,7 @@ void vgic_v4_configure_vsgis(struct kvm *kvm)
 	struct vgic_dist *dist = &kvm->arch.vgic;
 	struct kvm_vcpu *vcpu;
 	unsigned long i;
+	int ret;
 
 	lockdep_assert_held(&kvm->arch.config_lock);
 
@@ -203,8 +208,10 @@ void vgic_v4_configure_vsgis(struct kvm *kvm)
 
 	kvm_for_each_vcpu(i, vcpu, kvm) {
 		if (dist->nassgireq)
-			vgic_v4_enable_vsgis(vcpu);
-		else if (vgic_v4_disable_vsgis(vcpu)) {
+			ret = vgic_v4_enable_vsgis(vcpu);
+		else
+			ret = vgic_v4_disable_vsgis(vcpu);
+		if (ret) {
 			/* There is no way back from a partial switch */
 			kvm_vm_dead(kvm);
 			break;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 04/10] KVM: arm64: Check clearing the pending state of vSGIs moved to SW
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (2 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 03/10] KVM: arm64: Stop vSGI enablement on state transfer errors Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 05/10] KVM: arm64: vgic: Queue pending interrupts when the distributor is enabled Jia He
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

vgic_v4_disable_vsgis() copies each vSGI's pending state into
pending_latch and then calls irq_domain_deactivate_irq(). Deactivation
sends two VSGI commands (disable and clear pending) and cannot return
an error. If both fail, the SGI stays enabled and pending in the ITS
while also pending in SW, so the guest takes it twice once the vPE
becomes resident again.

Clear the pending state of a pending SGI with irq_set_irqchip_state()
before deactivating it. On failure, leave the SGI in HW and return the
error so the VM is marked dead. All vCPUs are halted during the switch,
so the SGI cannot become pending again in between.

Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/vgic/vgic-v4.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/arch/arm64/kvm/vgic/vgic-v4.c b/arch/arm64/kvm/vgic/vgic-v4.c
index fd0e7b2521c8..b2fd23366aff 100644
--- a/arch/arm64/kvm/vgic/vgic-v4.c
+++ b/arch/arm64/kvm/vgic/vgic-v4.c
@@ -180,6 +180,17 @@ static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
 		if (ret)
 			goto unlock;
 
+		/*
+		 * Deactivation can't report a failure to clear the pending
+		 * state, so clear it first.
+		 */
+		if (pending) {
+			ret = irq_set_irqchip_state(irq->host_irq,
+						    IRQCHIP_STATE_PENDING, false);
+			if (ret)
+				goto unlock;
+		}
+
 		irq->hw = false;
 		irq->pending_latch = pending;
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 05/10] KVM: arm64: vgic: Queue pending interrupts when the distributor is enabled
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (3 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 04/10] KVM: arm64: Check clearing the pending state of vSGIs moved to SW Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 06/10] KVM: arm64: vgic: Pass the enable state of HW SGIs to the ITS Jia He
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

While the distributor is disabled, vgic_target_oracle() returns NULL,
so vgic_queue_irq_unlock() does not put pending interrupts on any
ap_list. Enabling the distributor later only kicks vCPUs and never
re-queues them.

A level interrupt whose line stays high is then never delivered because
vgic_validate_injection() drops repeated injections. The virtual timer
PPI similarly stays active on the host and stops firing until
reprogrammed, and an LPI stays pending until another MSI arrives.

When the distributor transitions from disabled to enabled (via a guest
or userspace write), walk private interrupts, SPIs, and LPIs and queue
any pending ones. vgic_queue_irq_unlock() skips already-queued,
disabled, or non-pending interrupts and kicks the target vCPU. LPIs are
looked up with vgic_get_irq() to avoid racing with teardown.

Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/vgic/vgic-mmio-v2.c |  4 +++-
 arch/arm64/kvm/vgic/vgic-mmio-v3.c | 11 ++++++++-
 arch/arm64/kvm/vgic/vgic.c         | 38 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/vgic/vgic.h         |  1 +
 4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v2.c b/arch/arm64/kvm/vgic/vgic-mmio-v2.c
index 0643e333db35..96f917d2a2e5 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio-v2.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio-v2.c
@@ -59,8 +59,10 @@ static void vgic_mmio_write_v2_misc(struct kvm_vcpu *vcpu,
 	switch (addr & 0x0c) {
 	case GIC_DIST_CTRL:
 		dist->enabled = val & GICD_ENABLE;
-		if (!was_enabled && dist->enabled)
+		if (!was_enabled && dist->enabled) {
+			vgic_queue_pending_irqs(vcpu->kvm);
 			vgic_kick_vcpus(vcpu->kvm);
+		}
 		break;
 	case GIC_DIST_CTR:
 	case GIC_DIST_IIDR:
diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v3.c b/arch/arm64/kvm/vgic/vgic-mmio-v3.c
index f9ff553bbbf1..f97bf8fc3a79 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio-v3.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio-v3.c
@@ -147,6 +147,9 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,
 		if (is_hwsgi != dist->nassgireq)
 			vgic_v4_configure_vsgis(vcpu->kvm);
 
+		if (!was_enabled && dist->enabled)
+			vgic_queue_pending_irqs(vcpu->kvm);
+
 		if (vgic_supports_direct_sgis(vcpu->kvm) &&
 		    was_enabled != dist->enabled)
 			kvm_make_all_cpus_request(vcpu->kvm, KVM_REQ_RELOAD_GICv4);
@@ -200,15 +203,21 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
 		default:
 			return -EINVAL;
 		}
-	case GICD_CTLR:
+	case GICD_CTLR: {
+		bool was_enabled = dist->enabled;
+
 		/* Not a GICv4.1? No HW SGIs */
 		if (!vgic_supports_direct_sgis(vcpu->kvm))
 			val &= ~GICD_CTLR_nASSGIreq;
 
 		dist->enabled = val & GICD_CTLR_ENABLE_SS_G1;
 		dist->nassgireq = val & GICD_CTLR_nASSGIreq;
+
+		if (!was_enabled && dist->enabled)
+			vgic_queue_pending_irqs(vcpu->kvm);
 		return 0;
 	}
+	}
 
 	vgic_mmio_write_v3_misc(vcpu, addr, len, val);
 	return 0;
diff --git a/arch/arm64/kvm/vgic/vgic.c b/arch/arm64/kvm/vgic/vgic.c
index bea848c93a99..ff84bf7a0a07 100644
--- a/arch/arm64/kvm/vgic/vgic.c
+++ b/arch/arm64/kvm/vgic/vgic.c
@@ -1288,6 +1288,44 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu)
 	return pending;
 }
 
+static void vgic_queue_pending_irq(struct kvm *kvm, struct vgic_irq *irq)
+{
+	unsigned long flags;
+
+	raw_spin_lock_irqsave(&irq->irq_lock, flags);
+	vgic_queue_irq_unlock(kvm, irq, flags);
+	vgic_put_irq(kvm, irq);
+}
+
+/*
+ * Nothing gets queued while the distributor is disabled, and a level interrupt
+ * whose line stays high is never injected again, as vgic_validate_injection()
+ * drops repeated levels. Queue whatever became pending in the meantime once the
+ * distributor is enabled. vgic_queue_irq_unlock() skips interrupts that are
+ * already queued, disabled or not pending.
+ */
+void vgic_queue_pending_irqs(struct kvm *kvm)
+{
+	struct vgic_dist *dist = &kvm->arch.vgic;
+	struct vgic_irq *irq, *lpi;
+	struct kvm_vcpu *vcpu;
+	unsigned long c, intid;
+	int i;
+
+	kvm_for_each_vcpu(c, vcpu, kvm)
+		for (i = 0; i < VGIC_NR_PRIVATE_IRQS; i++)
+			vgic_queue_pending_irq(kvm, vgic_get_vcpu_irq(vcpu, i));
+
+	for (i = VGIC_NR_PRIVATE_IRQS; i < dist->nr_spis + VGIC_NR_PRIVATE_IRQS; i++)
+		vgic_queue_pending_irq(kvm, vgic_get_irq(kvm, i));
+
+	xa_for_each(&dist->lpi_xa, intid, lpi) {
+		irq = vgic_get_irq(kvm, intid);
+		if (irq)
+			vgic_queue_pending_irq(kvm, irq);
+	}
+}
+
 void vgic_kick_vcpus(struct kvm *kvm)
 {
 	struct kvm_vcpu *vcpu;
diff --git a/arch/arm64/kvm/vgic/vgic.h b/arch/arm64/kvm/vgic/vgic.h
index 97fc3bdf520e..595552aef444 100644
--- a/arch/arm64/kvm/vgic/vgic.h
+++ b/arch/arm64/kvm/vgic/vgic.h
@@ -288,6 +288,7 @@ void vgic_irq_set_phys_active(struct vgic_irq *irq, bool active);
 bool vgic_queue_irq_unlock(struct kvm *kvm, struct vgic_irq *irq,
 			   unsigned long flags) __releases(&irq->irq_lock);
 void vgic_kick_vcpus(struct kvm *kvm);
+void vgic_queue_pending_irqs(struct kvm *kvm);
 void vgic_irq_handle_resampling(struct vgic_irq *irq,
 				bool lr_deactivated, bool lr_pending);
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 06/10] KVM: arm64: vgic: Pass the enable state of HW SGIs to the ITS
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (4 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 05/10] KVM: arm64: vgic: Queue pending interrupts when the distributor is enabled Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 07/10] KVM: arm64: Synchronize vSGI state when restoring GICD_CTLR Jia He
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

With direct vSGIs, the ITS holds each SGI's enable state in the vPE's
sgi_config. Guest writes to GICR_ISENABLER0/ICENABLER0 currently use
enable_irq()/disable_irq_nosync() on the host IRQ, while userspace
accessors do not update the ITS at all.

Because vgic_v4_enable_vsgis() only activates the host IRQs, their
descriptors remain disabled and masked. disable_irq_nosync() therefore
only bumps the disable depth and never reaches the ITS, leaving the SGI
enabled in hardware.

Update sgi_config and send a VSGI command for both guest and userspace
enable/disable writes, matching group and priority updates. On failure,
the hardware enable state is unknown, so mark the VM dead and return
-EIO to userspace (a guest vCPU exits on KVM_REQ_VM_DEAD). Group and
priority updates continue to only warn.

Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/vgic/vgic-mmio.c | 69 ++++++++++++++++++++++++---------
 1 file changed, 51 insertions(+), 18 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-mmio.c b/arch/arm64/kvm/vgic/vgic-mmio.c
index fddb9da0403d..af84f18d057b 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio.c
@@ -66,6 +66,22 @@ static void vgic_update_vsgi(struct vgic_irq *irq)
 	WARN_ON(its_prop_update_vsgi(irq->host_irq, irq->priority, irq->group));
 }
 
+/*
+ * Must be called with the irq_lock held. On failure, the caller must mark
+ * the VM dead once it has dropped the lock.
+ */
+static int vgic_set_hw_sgi_enable(struct vgic_irq *irq, bool enable)
+{
+	struct its_vpe *vpe = &irq->target_vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
+
+	if (irq->enabled == enable)
+		return 0;
+
+	irq->enabled = enable;
+	vpe->sgi_config[irq->intid].enabled = enable;
+	return its_prop_update_vsgi(irq->host_irq, irq->priority, irq->group);
+}
+
 void vgic_mmio_write_group(struct kvm_vcpu *vcpu, gpa_t addr,
 			   unsigned int len, unsigned long val)
 {
@@ -126,17 +142,14 @@ void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
 
 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
 		if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
-			if (!irq->enabled) {
-				struct irq_data *data;
-
-				irq->enabled = true;
-				data = &irq_to_desc(irq->host_irq)->irq_data;
-				while (irqd_irq_disabled(data))
-					enable_irq(irq->host_irq);
-			}
+			int ret = vgic_set_hw_sgi_enable(irq, true);
 
 			raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
 			vgic_put_irq(vcpu->kvm, irq);
+			if (ret) {
+				kvm_vm_dead(vcpu->kvm);
+				return;
+			}
 
 			continue;
 		} else if (vgic_irq_is_mapped_level(irq)) {
@@ -167,20 +180,24 @@ void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
 			     unsigned long val)
 {
 	u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
-	int i;
+	int i, ret = 0;
 	unsigned long flags;
 
 	for_each_set_bit(i, &val, len * 8) {
 		struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, intid + i);
 
 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
-		if (irq->hw && vgic_irq_is_sgi(irq->intid) && irq->enabled)
-			disable_irq_nosync(irq->host_irq);
-
-		irq->enabled = false;
+		if (irq->hw && vgic_irq_is_sgi(irq->intid))
+			ret = vgic_set_hw_sgi_enable(irq, false);
+		else
+			irq->enabled = false;
 
 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
 		vgic_put_irq(vcpu->kvm, irq);
+		if (ret) {
+			kvm_vm_dead(vcpu->kvm);
+			return;
+		}
 	}
 }
 
@@ -189,17 +206,26 @@ int vgic_uaccess_write_senable(struct kvm_vcpu *vcpu,
 			       unsigned long val)
 {
 	u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
-	int i;
+	int i, ret = 0;
 	unsigned long flags;
 
 	for_each_set_bit(i, &val, len * 8) {
 		struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, intid + i);
 
 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
-		irq->enabled = true;
-		vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
+		if (irq->hw && vgic_irq_is_sgi(irq->intid)) {
+			ret = vgic_set_hw_sgi_enable(irq, true);
+			raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
+		} else {
+			irq->enabled = true;
+			vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
+		}
 
 		vgic_put_irq(vcpu->kvm, irq);
+		if (ret) {
+			kvm_vm_dead(vcpu->kvm);
+			return -EIO;
+		}
 	}
 
 	return 0;
@@ -210,17 +236,24 @@ int vgic_uaccess_write_cenable(struct kvm_vcpu *vcpu,
 			       unsigned long val)
 {
 	u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
-	int i;
+	int i, ret = 0;
 	unsigned long flags;
 
 	for_each_set_bit(i, &val, len * 8) {
 		struct vgic_irq *irq = vgic_get_vcpu_irq(vcpu, intid + i);
 
 		raw_spin_lock_irqsave(&irq->irq_lock, flags);
-		irq->enabled = false;
+		if (irq->hw && vgic_irq_is_sgi(irq->intid))
+			ret = vgic_set_hw_sgi_enable(irq, false);
+		else
+			irq->enabled = false;
 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
 
 		vgic_put_irq(vcpu->kvm, irq);
+		if (ret) {
+			kvm_vm_dead(vcpu->kvm);
+			return -EIO;
+		}
 	}
 
 	return 0;
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 07/10] KVM: arm64: Synchronize vSGI state when restoring GICD_CTLR
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (5 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 06/10] KVM: arm64: vgic: Pass the enable state of HW SGIs to the ITS Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 08/10] KVM: arm64: selftests: Avoid read-modify-write for GIC write-one registers Jia He
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

Userspace writes to GICD_CTLR update dist->nassgireq without moving
vSGIs between HW and SW. On a VM that has already run (such as reset or
in-place restore), the restored nASSGIreq does not take effect until
the guest next toggles it.

Move the nASSGIreq switch and distributor-enable queueing into a shared
helper called by both guest and userspace writes. On a fresh VM, vSGIs
now move to HW before userspace restores redistributor registers, which
relies on the previous patch propagating SGI enable state to the ITS.

Unlike the guest, userspace can clear nASSGIreq while the distributor
remains enabled. Queue each SGI as soon as it moves to SW so pending
SGIs are not stranded.

Userspace writes hold kvm->lock, all vCPU mutexes, and config_lock, so
the switch runs in sleepable context with no vCPU running. Return -EIO
if vgic_v4_configure_vsgis() fails and marks the VM dead.

Fixes: 2291ff2f2a56 ("KVM: arm64: GICv4.1: Plumb SGI implementation selection in the distributor")
Signed-off-by: Jia He <justin.he@arm.com>
---
 arch/arm64/kvm/vgic/vgic-mmio-v3.c | 37 ++++++++++++++++++++++--------
 arch/arm64/kvm/vgic/vgic-v4.c      | 12 ++++++++--
 arch/arm64/kvm/vgic/vgic.h         |  2 +-
 3 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/arch/arm64/kvm/vgic/vgic-mmio-v3.c b/arch/arm64/kvm/vgic/vgic-mmio-v3.c
index f97bf8fc3a79..66d7dc71d7c7 100644
--- a/arch/arm64/kvm/vgic/vgic-mmio-v3.c
+++ b/arch/arm64/kvm/vgic/vgic-mmio-v3.c
@@ -115,6 +115,29 @@ static unsigned long vgic_mmio_read_v3_misc(struct kvm_vcpu *vcpu,
 	return value;
 }
 
+/*
+ * Called with config_lock held after the guest or userspace has written
+ * GICD_CTLR. If the vSGIs can't be switched, the VM is marked dead.
+ */
+static int vgic_v3_ctlr_updated(struct kvm *kvm, bool was_enabled,
+				bool was_hwsgi)
+{
+	struct vgic_dist *dist = &kvm->arch.vgic;
+	int ret;
+
+	/* Switching HW SGIs? */
+	if (was_hwsgi != dist->nassgireq) {
+		ret = vgic_v4_configure_vsgis(kvm);
+		if (ret)
+			return ret;
+	}
+
+	if (!was_enabled && dist->enabled)
+		vgic_queue_pending_irqs(kvm);
+
+	return 0;
+}
+
 static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,
 				    gpa_t addr, unsigned int len,
 				    unsigned long val)
@@ -142,13 +165,9 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,
 			val |= FIELD_PREP(GICD_CTLR_nASSGIreq, is_hwsgi);
 		}
 
-		/* Switching HW SGIs? */
 		dist->nassgireq = val & GICD_CTLR_nASSGIreq;
-		if (is_hwsgi != dist->nassgireq)
-			vgic_v4_configure_vsgis(vcpu->kvm);
-
-		if (!was_enabled && dist->enabled)
-			vgic_queue_pending_irqs(vcpu->kvm);
+		/* On failure, the vCPU exits on KVM_REQ_VM_DEAD */
+		vgic_v3_ctlr_updated(vcpu->kvm, was_enabled, is_hwsgi);
 
 		if (vgic_supports_direct_sgis(vcpu->kvm) &&
 		    was_enabled != dist->enabled)
@@ -205,6 +224,7 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
 		}
 	case GICD_CTLR: {
 		bool was_enabled = dist->enabled;
+		bool was_hwsgi = dist->nassgireq;
 
 		/* Not a GICv4.1? No HW SGIs */
 		if (!vgic_supports_direct_sgis(vcpu->kvm))
@@ -212,10 +232,7 @@ static int vgic_mmio_uaccess_write_v3_misc(struct kvm_vcpu *vcpu,
 
 		dist->enabled = val & GICD_CTLR_ENABLE_SS_G1;
 		dist->nassgireq = val & GICD_CTLR_nASSGIreq;
-
-		if (!was_enabled && dist->enabled)
-			vgic_queue_pending_irqs(vcpu->kvm);
-		return 0;
+		return vgic_v3_ctlr_updated(vcpu->kvm, was_enabled, was_hwsgi);
 	}
 	}
 
diff --git a/arch/arm64/kvm/vgic/vgic-v4.c b/arch/arm64/kvm/vgic/vgic-v4.c
index b2fd23366aff..d9b3ea783bbd 100644
--- a/arch/arm64/kvm/vgic/vgic-v4.c
+++ b/arch/arm64/kvm/vgic/vgic-v4.c
@@ -196,6 +196,11 @@ static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
 
 		desc = irq_to_desc(irq->host_irq);
 		irq_domain_deactivate_irq(irq_desc_get_irq_data(desc));
+
+		/* This is now a SW interrupt, queue it if it is pending */
+		vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
+		vgic_put_irq(vcpu->kvm, irq);
+		continue;
 	unlock:
 		raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
 		vgic_put_irq(vcpu->kvm, irq);
@@ -206,12 +211,12 @@ static int vgic_v4_disable_vsgis(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
-void vgic_v4_configure_vsgis(struct kvm *kvm)
+int vgic_v4_configure_vsgis(struct kvm *kvm)
 {
 	struct vgic_dist *dist = &kvm->arch.vgic;
 	struct kvm_vcpu *vcpu;
 	unsigned long i;
-	int ret;
+	int ret = 0;
 
 	lockdep_assert_held(&kvm->arch.config_lock);
 
@@ -225,11 +230,14 @@ void vgic_v4_configure_vsgis(struct kvm *kvm)
 		if (ret) {
 			/* There is no way back from a partial switch */
 			kvm_vm_dead(kvm);
+			ret = -EIO;
 			break;
 		}
 	}
 
 	kvm_arm_resume_guest(kvm);
+
+	return ret;
 }
 
 /*
diff --git a/arch/arm64/kvm/vgic/vgic.h b/arch/arm64/kvm/vgic/vgic.h
index 595552aef444..3d547ec5a581 100644
--- a/arch/arm64/kvm/vgic/vgic.h
+++ b/arch/arm64/kvm/vgic/vgic.h
@@ -483,7 +483,7 @@ int vgic_its_invall(struct kvm_vcpu *vcpu);
 
 int vgic_v4_init(struct kvm *kvm);
 void vgic_v4_teardown(struct kvm *kvm);
-void vgic_v4_configure_vsgis(struct kvm *kvm);
+int vgic_v4_configure_vsgis(struct kvm *kvm);
 void vgic_v4_get_vlpi_state(struct vgic_irq *irq, bool *val);
 int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq);
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 08/10] KVM: arm64: selftests: Avoid read-modify-write for GIC write-one registers
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (6 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 07/10] KVM: arm64: Synchronize vSGI state when restoring GICD_CTLR Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 09/10] KVM: arm64: selftests: Add vgic_dist_reenable test Jia He
  2026-12-06 13:53 ` [PATCH 10/10] KVM: arm64: selftests: Check SGI delivery after VGIC restore Jia He
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

The GICv3 field accessor in the selftest library does a
read-modify-write, which breaks write-one-to-set and write-one-to-clear
registers. For example, disabling SGI1 while SGI2 is enabled reads both
bits from ICENABLER0 and writes both back, disabling SGI2 as well.

Write only the requested bits for enable, pending, and active set/clear
registers, keeping read-modify-write for priority and trigger
configuration. Check the base register offset before adding the
per-INTID offset so SPI registers are covered too.

Fixes: 745068367ccb ("KVM: selftests: aarch64: Add function for accessing GICv3 dist and redist registers")
Signed-off-by: Jia He <justin.he@arm.com>
---
 tools/testing/selftests/kvm/lib/arm64/gic_v3.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/arm64/gic_v3.c b/tools/testing/selftests/kvm/lib/arm64/gic_v3.c
index a99a53accfe9..fc9d2a354748 100644
--- a/tools/testing/selftests/kvm/lib/arm64/gic_v3.c
+++ b/tools/testing/selftests/kvm/lib/arm64/gic_v3.c
@@ -173,6 +173,7 @@ static void gicv3_access_reg(u32 intid, u64 offset,
 	enum gicv3_intid_range intid_range = get_intid_range(intid);
 	u32 fields_per_reg, index, mask, shift;
 	u32 cpu_or_dist;
+	bool write_one;
 
 	GUEST_ASSERT(bits_per_field <= reg_bits);
 	GUEST_ASSERT(!write || *val < (1U << bits_per_field));
@@ -182,6 +183,11 @@ static void gicv3_access_reg(u32 intid, u64 offset,
 	 */
 	GUEST_ASSERT(reg_bits == 32);
 
+	/* Writing back the other bits of a W1S/W1C register would change them */
+	write_one = offset == GICD_ISENABLER || offset == GICD_ICENABLER ||
+		    offset == GICD_ISPENDR || offset == GICD_ICPENDR ||
+		    offset == GICD_ISACTIVER || offset == GICD_ICACTIVER;
+
 	fields_per_reg = reg_bits / bits_per_field;
 	index = intid % fields_per_reg;
 	shift = index * bits_per_field;
@@ -192,8 +198,12 @@ static void gicv3_access_reg(u32 intid, u64 offset,
 
 	cpu_or_dist = (intid_range == SPI_RANGE) ? DIST_BIT : cpu;
 
-	if (write)
-		gicv3_setl_fields(cpu_or_dist, offset, mask, *val << shift);
+	if (write) {
+		if (write_one)
+			gicv3_reg_writel(cpu_or_dist, offset, *val << shift);
+		else
+			gicv3_setl_fields(cpu_or_dist, offset, mask, *val << shift);
+	}
 	*val = gicv3_getl_fields(cpu_or_dist, offset, mask) >> shift;
 }
 
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 09/10] KVM: arm64: selftests: Add vgic_dist_reenable test
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (7 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 08/10] KVM: arm64: selftests: Avoid read-modify-write for GIC write-one registers Jia He
@ 2026-12-06 13:53 ` Jia He
  2026-12-06 13:53 ` [PATCH 10/10] KVM: arm64: selftests: Check SGI delivery after VGIC restore Jia He
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

Add a selftest checking that interrupts made pending while
GICD_CTLR.EnableGrp1 is clear are delivered once the distributor is
re-enabled. Covers a level SPI (KVM_IRQ_LINE), the virtual timer
(HW-mapped PPI), and an LPI (KVM_SIGNAL_MSI).

Signed-off-by: Jia He <justin.he@arm.com>
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/vgic_dist_reenable.c  | 228 ++++++++++++++++++
 2 files changed, 229 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/vgic_dist_reenable.c

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 17a4256e9081..b51cdf07cb9c 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -194,6 +194,7 @@ TEST_GEN_PROGS_arm64 += arm64/set_id_regs
 TEST_GEN_PROGS_arm64 += arm64/smccc_filter
 TEST_GEN_PROGS_arm64 += arm64/stage2_block_transitions
 TEST_GEN_PROGS_arm64 += arm64/vcpu_width_config
+TEST_GEN_PROGS_arm64 += arm64/vgic_dist_reenable
 TEST_GEN_PROGS_arm64 += arm64/vgic_init
 TEST_GEN_PROGS_arm64 += arm64/vgic_irq
 TEST_GEN_PROGS_arm64 += arm64/vgic_its_save
diff --git a/tools/testing/selftests/kvm/arm64/vgic_dist_reenable.c b/tools/testing/selftests/kvm/arm64/vgic_dist_reenable.c
new file mode 100644
index 000000000000..c3a05cc9c007
--- /dev/null
+++ b/tools/testing/selftests/kvm/arm64/vgic_dist_reenable.c
@@ -0,0 +1,228 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * vgic_dist_reenable - check that interrupts made pending while the
+ * distributor is disabled are taken once the guest enables it again.
+ *
+ * Each interrupt becomes pending with GICD_CTLR.EnableGrp1 clear: a level SPI
+ * driven by userspace with KVM_IRQ_LINE, which stays high, the virtual timer,
+ * which is a HW-mapped PPI, and an LPI signalled with KVM_SIGNAL_MSI.
+ */
+#include <linux/sizes.h>
+#include "kvm_util.h"
+#include "processor.h"
+#include "arch_timer.h"
+#include "gic.h"
+#include "gic_v3.h"
+#include "gic_v3_its.h"
+#include "vgic.h"
+#include "ucall.h"
+
+#define SPI_ID		(MIN_SPI + 3)
+#define LPI_ID		8192
+#define WAIT_MS		100
+#define TEST_MEMSLOT	1
+
+enum { SYNC_SPI_RAISE = 1, SYNC_SPI_LOWER, SYNC_LPI };
+
+static unsigned int spi_irqs, timer_irqs, lpi_irqs;
+static u32 vtimer_irq;
+
+static struct {
+	gpa_t	device_table;
+	gpa_t	collection_table;
+	gpa_t	cmdq;
+	void	*cmdq_va;
+	gpa_t	itt;
+	gpa_t	prop_table;
+	gpa_t	pend_table;
+} its_data;
+
+static void guest_irq(struct ex_regs *regs)
+{
+	unsigned int intid = gic_get_and_ack_irq();
+
+	if (intid == IAR_SPURIOUS)
+		return;
+
+	if (intid == SPI_ID) {
+		/* Still high until userspace lowers it */
+		gic_irq_disable(SPI_ID);
+		WRITE_ONCE(spi_irqs, spi_irqs + 1);
+	} else if (intid == vtimer_irq) {
+		timer_set_ctl(VIRTUAL, CTL_IMASK);
+		WRITE_ONCE(timer_irqs, timer_irqs + 1);
+	} else if (intid == LPI_ID) {
+		WRITE_ONCE(lpi_irqs, lpi_irqs + 1);
+	} else {
+		GUEST_FAIL("unexpected IRQ %u", intid);
+	}
+	gic_set_eoi(intid);
+}
+
+static void dist_set(u32 ctlr)
+{
+	writel(ctlr, GICD_BASE_GVA + GICD_CTLR);
+	while (readl(GICD_BASE_GVA + GICD_CTLR) & GICD_CTLR_RWP)
+		cpu_relax();
+}
+
+static void spin_ms(unsigned int ms)
+{
+	u64 end = timer_get_cntct(VIRTUAL) + msec_to_cycles(ms);
+
+	while (timer_get_cntct(VIRTUAL) < end)
+		cpu_relax();
+}
+
+static void wait_for(unsigned int *count)
+{
+	u64 end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS);
+
+	while (!READ_ONCE(*count) && timer_get_cntct(VIRTUAL) < end)
+		cpu_relax();
+}
+
+/* Map event 0 of device 0 to LPI_ID on vCPU 0 */
+static void guest_setup_lpi(void)
+{
+	gic_rdist_enable_lpis(its_data.prop_table, SZ_64K, its_data.pend_table);
+	its_init(its_data.collection_table, SZ_64K, its_data.device_table, SZ_64K,
+		 its_data.cmdq, SZ_64K);
+	its_send_mapc_cmd(its_data.cmdq_va, 0, 0, true);
+	its_send_mapd_cmd(its_data.cmdq_va, 0, its_data.itt, SZ_64K, true);
+	its_send_mapti_cmd(its_data.cmdq_va, 0, 0, 0, LPI_ID);
+	its_send_invall_cmd(its_data.cmdq_va, 0);
+	its_send_sync_cmd(its_data.cmdq_va, 0);
+}
+
+static void guest_code(void)
+{
+	u32 ctlr;
+
+	gic_init(GIC_V3, 1);
+	guest_setup_lpi();
+	ctlr = readl(GICD_BASE_GVA + GICD_CTLR);
+	gic_irq_enable(SPI_ID);
+	gic_irq_enable(vtimer_irq);
+
+	/* SPI: raised by userspace while the distributor is off */
+	local_irq_disable();
+	dist_set(0);
+	GUEST_SYNC(SYNC_SPI_RAISE);
+	dist_set(ctlr);
+	local_irq_enable();
+	wait_for(&spi_irqs);
+	GUEST_SYNC(SYNC_SPI_LOWER);
+
+	/* vtimer: expires while the distributor is off */
+	local_irq_disable();
+	dist_set(0);
+	timer_set_cval(VIRTUAL, timer_get_cntct(VIRTUAL) + msec_to_cycles(1));
+	timer_set_ctl(VIRTUAL, CTL_ENABLE);
+	while (!(timer_get_ctl(VIRTUAL) & CTL_ISTATUS))
+		cpu_relax();
+	/* Let the host observe the expiry before the distributor comes back */
+	spin_ms(5);
+	dist_set(ctlr);
+	local_irq_enable();
+	wait_for(&timer_irqs);
+
+	/* LPI: an MSI is sent while the distributor is off */
+	local_irq_disable();
+	dist_set(0);
+	GUEST_SYNC(SYNC_LPI);
+	dist_set(ctlr);
+	local_irq_enable();
+	wait_for(&lpi_irqs);
+
+	__GUEST_ASSERT(spi_irqs == 1 && timer_irqs == 1 && lpi_irqs == 1,
+		       "raised while the distributor was disabled: SPI %u IRQs, vtimer %u IRQs, LPI %u IRQs",
+		       spi_irqs, timer_irqs, lpi_irqs);
+
+	GUEST_DONE();
+}
+
+static void setup_its_data(struct kvm_vm *vm)
+{
+	size_t pages_64k = vm_calc_num_guest_pages(vm->mode, SZ_64K);
+	/* Device, collection and LPI property tables, command queue, ITT, LPI pending table */
+	size_t sz = 6 * SZ_64K;
+	gpa_t base = ((vm_compute_max_gfn(vm) + 1) * vm->page_size) - sz;
+	u8 *prop;
+
+	vm_override_mem_region(vm, MEM_REGION_TEST_EXTRA, VM_MEM_SRC_ANONYMOUS,
+			       base, TEST_MEMSLOT, sz / vm->page_size);
+
+	its_data.device_table = vm_phy_pages_alloc(vm, pages_64k, MEM_REGION_TEST_EXTRA);
+	its_data.collection_table = vm_phy_pages_alloc(vm, pages_64k, MEM_REGION_TEST_EXTRA);
+	its_data.cmdq = vm_phy_pages_alloc(vm, pages_64k, MEM_REGION_TEST_EXTRA);
+	virt_map(vm, its_data.cmdq, its_data.cmdq, pages_64k);
+	its_data.cmdq_va = (void *)its_data.cmdq;
+	its_data.itt = vm_phy_pages_alloc(vm, pages_64k, MEM_REGION_TEST_EXTRA);
+	its_data.prop_table = vm_phy_pages_alloc(vm, pages_64k, MEM_REGION_TEST_EXTRA);
+	its_data.pend_table = vm_phy_pages_alloc(vm, pages_64k, MEM_REGION_TEST_EXTRA);
+
+	prop = addr_gpa2hva(vm, its_data.prop_table);
+	prop[LPI_ID - 8192] = 0xa0 | LPI_PROP_GROUP1 | LPI_PROP_ENABLED;
+
+	sync_global_to_guest(vm, its_data);
+}
+
+static void signal_lpi(struct kvm_vm *vm)
+{
+	gpa_t db = GITS_BASE_GPA + GITS_TRANSLATER;
+	struct kvm_msi msi = {
+		.address_lo	= db,
+		.address_hi	= db >> 32,
+		.data		= 0,
+		.devid		= 0,
+		.flags		= KVM_MSI_VALID_DEVID,
+	};
+
+	/* 1 means the MSI was translated by the ITS */
+	TEST_ASSERT(__vm_ioctl(vm, KVM_SIGNAL_MSI, &msi) == 1,
+		    "KVM_SIGNAL_MSI failed");
+}
+
+int main(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	int its_fd;
+
+	TEST_REQUIRE(kvm_supports_vgic_v3());
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	vm_init_descriptor_tables(vm);
+	vcpu_init_descriptor_tables(vcpu);
+	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq);
+	vtimer_irq = vcpu_get_vtimer_irq(vcpu);
+	sync_global_to_guest(vm, vtimer_irq);
+	its_fd = vgic_its_setup(vm);
+	setup_its_data(vm);
+
+	for (;;) {
+		vcpu_run(vcpu);
+		TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_MMIO);
+		switch (get_ucall(vcpu, &uc)) {
+		case UCALL_SYNC:
+			if (uc.args[1] == SYNC_LPI)
+				signal_lpi(vm);
+			else
+				kvm_arm_irq_line(vm, SPI_ID,
+						 uc.args[1] == SYNC_SPI_RAISE);
+			break;
+		case UCALL_ABORT:
+			REPORT_GUEST_ASSERT(uc);
+			break;
+		case UCALL_DONE:
+			pr_info("PASS: SPI, vtimer and LPI made pending while the distributor was disabled were delivered\n");
+			close(its_fd);
+			kvm_vm_free(vm);
+			return 0;
+		default:
+			TEST_FAIL("unexpected ucall %lu", uc.cmd);
+		}
+	}
+}
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 10/10] KVM: arm64: selftests: Check SGI delivery after VGIC restore
  2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
                   ` (8 preceding siblings ...)
  2026-12-06 13:53 ` [PATCH 09/10] KVM: arm64: selftests: Add vgic_dist_reenable test Jia He
@ 2026-12-06 13:53 ` Jia He
  9 siblings, 0 replies; 11+ messages in thread
From: Jia He @ 2026-12-06 13:53 UTC (permalink / raw)
  To: Marc Zyngier, Oliver Upton, Thomas Gleixner
  Cc: nd, Jia He, Joey Gouly, Suzuki K Poulose, Zenghui Yu, Fuad Tabba,
	Steffen Eiden, Radu Rendec, Paolo Bonzini, Sean Christopherson,
	Shuah Khan, Ricardo Koller, Andrew Jones, kvmarm,
	linux-arm-kernel, kvm, linux-kselftest, linux-kernel

Add a selftest covering userspace GICD_CTLR restore and SGI enable
state with direct vSGIs:

- native: restore nASSGIreq=1 with distributor disabled
- software: restore nASSGIreq=0 with distributor disabled
- software-enabled: restore nASSGIreq=0 with distributor enabled
- enable-cold: restore SGI disabled and pending before first KVM_RUN
- enable-warm: restore SGI disabled and pending in place after running
- enable-guest: guest disables and re-enables a direct SGI itself

The first three cases verify pending preservation across pending-table
save and check active state in the guest handler to distinguish direct
injection from LR injection. The last three verify a disabled pending
vSGI is not taken until re-enabled. A single case can be selected on
the command line.

Signed-off-by: Jia He <justin.he@arm.com>
---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/vgic_sgi_restore.c    | 306 ++++++++++++++++++
 2 files changed, 307 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c

diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index b51cdf07cb9c..8d4709ee6381 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -199,6 +199,7 @@ 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_sgi_restore
 TEST_GEN_PROGS_arm64 += arm64/vgic_v5
 TEST_GEN_PROGS_arm64 += arm64/vpmu_counter_access
 TEST_GEN_PROGS_arm64 += arm64/no-vgic
diff --git a/tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c b/tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c
new file mode 100644
index 000000000000..7fc17e8e50f9
--- /dev/null
+++ b/tools/testing/selftests/kvm/arm64/vgic_sgi_restore.c
@@ -0,0 +1,306 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/sizes.h>
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+#include "arch_timer.h"
+#include "gic.h"
+#include "gic_v3.h"
+#include "vgic.h"
+
+#define SGI 1
+#define SGI_MASK BIT(SGI)
+#define CTLR_BASE (GICD_CTLR_ARE_NS | GICD_CTLR_DS)
+#define WAIT_MS 100
+
+static bool irq_received;
+
+static void guest_irq_handler(struct ex_regs *regs)
+{
+	unsigned int intid = gic_get_and_ack_irq();
+
+	if (intid == IAR_SPURIOUS)
+		return;
+	GUEST_ASSERT(!READ_ONCE(irq_received));
+	GUEST_ASSERT_EQ(intid, SGI);
+	GUEST_SYNC(1);
+	gic_set_eoi(intid);
+	WRITE_ONCE(irq_received, true);
+}
+
+static void guest_code(bool initial_hardware, bool final_hardware)
+{
+	u64 end;
+
+	local_irq_disable();
+	gic_init(GIC_V3, 1);
+	gic_irq_enable(SGI);
+	writel(CTLR_BASE, GICD_BASE_GVA + GICD_CTLR);
+	dsb(sy);
+	writel(CTLR_BASE | (initial_hardware ? GICD_CTLR_nASSGIreq : 0),
+	       GICD_BASE_GVA + GICD_CTLR);
+	dsb(sy);
+	GUEST_SYNC(0);
+	GUEST_ASSERT_EQ(readl(GICD_BASE_GVA + GICD_CTLR) &
+			(GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1),
+			(final_hardware ? GICD_CTLR_nASSGIreq : 0) |
+			GICD_CTLR_ENABLE_SS_G1);
+	end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS);
+	local_irq_enable();
+	while (!READ_ONCE(irq_received) && timer_get_cntct(VIRTUAL) < end)
+		cpu_relax();
+	local_irq_disable();
+	GUEST_ASSERT(READ_ONCE(irq_received));
+	GUEST_ASSERT_EQ(read_sysreg_s(SYS_ICC_RPR_EL1), 0xff);
+	GUEST_ASSERT_EQ(gic_get_and_ack_irq(), IAR_SPURIOUS);
+	GUEST_DONE();
+}
+
+static u32 read_sgi_reg(int gic_fd, unsigned int offset)
+{
+	u32 value;
+
+	kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
+			    SZ_64K + offset, &value);
+	return value & 0xffff;
+}
+
+static void restore_ctlr(int gic_fd, bool hardware, bool enabled)
+{
+	u32 value = CTLR_BASE | (hardware ? GICD_CTLR_nASSGIreq : 0) |
+			 (enabled ? GICD_CTLR_ENABLE_SS_G1 : 0);
+
+	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, GICD_CTLR,
+			    &value);
+}
+
+static void run_to_sync(struct kvm_vcpu *vcpu, unsigned int stage)
+{
+	struct ucall uc;
+
+	vcpu_run(vcpu);
+	if (get_ucall(vcpu, &uc) == UCALL_ABORT)
+		REPORT_GUEST_ASSERT(uc);
+	TEST_ASSERT_EQ(uc.cmd, UCALL_SYNC);
+	TEST_ASSERT_EQ(uc.args[1], stage);
+}
+
+static void run_test(bool final_hardware, bool keep_enabled)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	struct ucall uc;
+	u32 value;
+	int gic_fd;
+
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code);
+	vm_init_descriptor_tables(vm);
+	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler);
+	vcpu_init_descriptor_tables(vcpu);
+	gic_fd = vgic_v3_setup(vm, 1, 64);
+	TEST_ASSERT(gic_fd >= 0, "Failed to create VGICv3");
+	kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, GICD_TYPER2,
+			    &value);
+	TEST_REQUIRE(value & GICD_TYPER2_nASSGIcap);
+	vcpu_args_set(vcpu, 2, !final_hardware, final_hardware);
+	run_to_sync(vcpu, 0);
+	value = SGI_MASK;
+	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
+			    SZ_64K + GICD_ISPENDR, &value);
+	TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), SGI_MASK);
+	if (keep_enabled)
+		restore_ctlr(gic_fd, true, true);
+	restore_ctlr(gic_fd, final_hardware, keep_enabled);
+	/* As for a migration. The SGI must still be pending afterwards. */
+	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
+			    KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES, NULL);
+	TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), SGI_MASK);
+	restore_ctlr(gic_fd, final_hardware, true);
+	run_to_sync(vcpu, 1);
+	TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), 0);
+	/*
+	 * The guest syncs from its handler, before the EOI. KVM only reports
+	 * the SW active state, as GICv4.1 can't report it for a vSGI, so a SW
+	 * SGI reads as active here and a directly injected one doesn't. This
+	 * tells us which path delivered the SGI.
+	 */
+	TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISACTIVER),
+		       final_hardware ? 0 : SGI_MASK);
+	vcpu_run(vcpu);
+	if (get_ucall(vcpu, &uc) == UCALL_ABORT)
+		REPORT_GUEST_ASSERT(uc);
+	TEST_ASSERT_EQ(uc.cmd, UCALL_DONE);
+	TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISPENDR), 0);
+	TEST_ASSERT_EQ(read_sgi_reg(gic_fd, GICD_ISACTIVER), 0);
+	close(gic_fd);
+	kvm_vm_free(vm);
+}
+
+/*
+ * The SGI is disabled and pending with direct vSGIs, and must not be taken
+ * until it is enabled again:
+ * - ENABLE_COLD: userspace restores it before the first KVM_RUN, so the guest
+ *   only sets up its CPU interface, and enables the SGI itself afterwards.
+ * - ENABLE_WARM: the guest has enabled the SGI with direct vSGIs, userspace
+ *   restores it in place, and enables it afterwards.
+ * - ENABLE_GUEST: the guest disables the SGI itself with direct vSGIs, and
+ *   enables it itself afterwards. Userspace only makes it pending.
+ */
+enum { ENABLE_COLD, ENABLE_WARM, ENABLE_GUEST };
+
+static void guest_irq_handler_raw(struct ex_regs *regs)
+{
+	u32 intid = read_sysreg_s(SYS_ICC_IAR1_EL1) & 0xffffff;
+
+	if (intid == IAR_SPURIOUS)
+		return;
+	GUEST_ASSERT_EQ(intid, SGI);
+	write_sysreg_s(intid, SYS_ICC_EOIR1_EL1);
+	isb();
+	WRITE_ONCE(irq_received, true);
+}
+
+static void guest_code_enable(int mode)
+{
+	u32 ctlr = CTLR_BASE | GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1;
+	u64 end;
+
+	local_irq_disable();
+	if (mode == ENABLE_COLD) {
+		write_sysreg_s(0xff, SYS_ICC_PMR_EL1);
+		write_sysreg_s(ICC_IGRPEN1_EL1_MASK, SYS_ICC_IGRPEN1_EL1);
+		isb();
+	} else {
+		gic_init(GIC_V3, 1);
+		gic_irq_enable(SGI);
+		writel(CTLR_BASE, GICD_BASE_GVA + GICD_CTLR);
+		writel(ctlr, GICD_BASE_GVA + GICD_CTLR);
+		if (mode == ENABLE_GUEST)
+			gic_irq_disable(SGI);
+		GUEST_SYNC(0);
+	}
+	GUEST_ASSERT_EQ(readl(GICD_BASE_GVA + GICD_CTLR) &
+			(GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1),
+			GICD_CTLR_nASSGIreq | GICD_CTLR_ENABLE_SS_G1);
+
+	end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS);
+	local_irq_enable();
+	while (timer_get_cntct(VIRTUAL) < end)
+		cpu_relax();
+	local_irq_disable();
+	__GUEST_ASSERT(!READ_ONCE(irq_received),
+		       "SGI %d taken while disabled", SGI);
+
+	if (mode == ENABLE_WARM)
+		GUEST_SYNC(1);
+	else
+		writel(SGI_MASK, GICR_BASE_GVA + SZ_64K + GICR_ISENABLER0);
+
+	end = timer_get_cntct(VIRTUAL) + msec_to_cycles(WAIT_MS);
+	local_irq_enable();
+	while (!READ_ONCE(irq_received) && timer_get_cntct(VIRTUAL) < end)
+		cpu_relax();
+	local_irq_disable();
+	__GUEST_ASSERT(READ_ONCE(irq_received), "SGI %d not taken once enabled", SGI);
+	GUEST_DONE();
+}
+
+static void run_vcpu(struct kvm_vcpu *vcpu, unsigned int cmd, unsigned int stage)
+{
+	struct ucall uc;
+
+	vcpu_run(vcpu);
+	if (get_ucall(vcpu, &uc) == UCALL_ABORT)
+		REPORT_GUEST_ASSERT(uc);
+	TEST_ASSERT_EQ(uc.cmd, cmd);
+	if (cmd == UCALL_SYNC)
+		TEST_ASSERT_EQ(uc.args[1], stage);
+}
+
+static void run_enable_test(int mode)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	u32 value;
+	int gic_fd;
+
+	irq_received = false;
+	vm = vm_create_with_one_vcpu(&vcpu, guest_code_enable);
+	vm_init_descriptor_tables(vm);
+	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler_raw);
+	vcpu_init_descriptor_tables(vcpu);
+	gic_fd = vgic_v3_setup(vm, 1, 64);
+	TEST_ASSERT(gic_fd >= 0, "Failed to create VGICv3");
+	kvm_device_attr_get(gic_fd, KVM_DEV_ARM_VGIC_GRP_DIST_REGS, GICD_TYPER2,
+			    &value);
+	TEST_REQUIRE(value & GICD_TYPER2_nASSGIcap);
+	vcpu_args_set(vcpu, 1, mode);
+	if (mode != ENABLE_COLD)
+		run_vcpu(vcpu, UCALL_SYNC, 0);
+
+	value = SGI_MASK;
+	if (mode != ENABLE_GUEST) {
+		/* In the order a VMM restores them: GICD_CTLR first */
+		restore_ctlr(gic_fd, true, true);
+		kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
+				    SZ_64K + GICD_ICENABLER, &value);
+	}
+	kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
+			    SZ_64K + GICD_ISPENDR, &value);
+
+	if (mode == ENABLE_WARM) {
+		run_vcpu(vcpu, UCALL_SYNC, 1);
+		kvm_device_attr_set(gic_fd, KVM_DEV_ARM_VGIC_GRP_REDIST_REGS,
+				    SZ_64K + GICD_ISENABLER, &value);
+	}
+	run_vcpu(vcpu, UCALL_DONE, 0);
+	close(gic_fd);
+	kvm_vm_free(vm);
+}
+
+int main(int argc, char **argv)
+{
+	static const struct {
+		const char *name;
+		bool hardware;
+		bool enabled;
+	} cases[] = {
+		{ "native", true, false },
+		{ "software", false, false },
+		{ "software-enabled", false, true },
+	};
+	static const struct {
+		const char *name;
+		int mode;
+	} enable_cases[] = {
+		{ "enable-cold", ENABLE_COLD },
+		{ "enable-warm", ENABLE_WARM },
+		{ "enable-guest", ENABLE_GUEST },
+	};
+	unsigned int completed = 0;
+
+	TEST_ASSERT(argc <= 2,
+		    "Usage: %s [native|software|software-enabled|enable-cold|enable-warm|enable-guest]",
+		    argv[0]);
+	TEST_REQUIRE(kvm_supports_vgic_v3());
+	test_disable_default_vgic();
+	for (unsigned int index = 0; index < ARRAY_SIZE(cases); index++) {
+		if (argc == 2 && strcmp(argv[1], cases[index].name))
+			continue;
+		run_test(cases[index].hardware, cases[index].enabled);
+		printf("PASS: %s pending restore and IRQ delivery\n",
+		       cases[index].name);
+		completed++;
+	}
+	for (unsigned int index = 0; index < ARRAY_SIZE(enable_cases); index++) {
+		if (argc == 2 && strcmp(argv[1], enable_cases[index].name))
+			continue;
+		run_enable_test(enable_cases[index].mode);
+		printf("PASS: %s\n", enable_cases[index].name);
+		completed++;
+	}
+	TEST_ASSERT(completed, "Unknown restore mode: %s",
+		    argc == 2 ? argv[1] : "");
+	return 0;
+}
-- 
2.34.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-12-06 13:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-06 13:53 [PATCH 00/10] KVM: arm64: GICv4.1 vSGI error handling and VGIC state fixes Jia He
2026-12-06 13:53 ` [PATCH 01/10] KVM: arm64: Preserve vSGI state when pending queries fail Jia He
2026-12-06 13:53 ` [PATCH 02/10] irqchip/gic-v3-its: Propagate vSGI command queue failures Jia He
2026-12-06 13:53 ` [PATCH 03/10] KVM: arm64: Stop vSGI enablement on state transfer errors Jia He
2026-12-06 13:53 ` [PATCH 04/10] KVM: arm64: Check clearing the pending state of vSGIs moved to SW Jia He
2026-12-06 13:53 ` [PATCH 05/10] KVM: arm64: vgic: Queue pending interrupts when the distributor is enabled Jia He
2026-12-06 13:53 ` [PATCH 06/10] KVM: arm64: vgic: Pass the enable state of HW SGIs to the ITS Jia He
2026-12-06 13:53 ` [PATCH 07/10] KVM: arm64: Synchronize vSGI state when restoring GICD_CTLR Jia He
2026-12-06 13:53 ` [PATCH 08/10] KVM: arm64: selftests: Avoid read-modify-write for GIC write-one registers Jia He
2026-12-06 13:53 ` [PATCH 09/10] KVM: arm64: selftests: Add vgic_dist_reenable test Jia He
2026-12-06 13:53 ` [PATCH 10/10] KVM: arm64: selftests: Check SGI delivery after VGIC restore Jia He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®