KVM: arm64: vgic: Drop stale LPI pending state
Subsystems (4)
Fuad Tabba
reviewercover KVM: arm64: vgic: Drop stale LPI pending state
Hi folks,
The GIC just keeps on giving. Three follow-ups to my LPI fixes for redistributors with LPIs disabled [1], to fix pre-existing bugs Sashiko found.
The first drops the pending state of every LPI targeting a redistributor when the guest disables its LPIs, not just of those on the AP list, so a re-enable with GICR_PENDBASER.PTZ set doesn't bring any of them back. Linux never sets PTZ.
The second stops the pending-table save from writing to the table of a redistributor while its LPIs are disabled.
The third stops KVM reading the pending table when mapping an LPI, so that mapping a discarded LPI's INTID again doesn't make it pending a second time. A Linux guest can hit that after its VMM pauses and resumes it. It also drops the copy that patch 2 of [1] takes, which is why it comes after the second. An LPI mapped after its redistributor enabled LPIs no longer takes a bit the guest wrote into the table before the enable. Keeping that needs a per-redistributor record of the pending state of unmapped INTIDs. I can add it on top if you think it's worth it.
A pending LPI that MOVI moves onto a redistributor with LPIs disabled, or that a device signals there through a translation cached before the move, can still be taken once after a PTZ-set enable. The architecture lets KVM drop it, but that's pending state moved or raised onto a redistributor with LPIs disabled, not state the guest gave up. I've left it alone, but can fix it on top if you think it's worth it.
Reproducers for all three fail on kvmarm/fixes and pass with the series. I'll fold them into vgic_lpi_stress separately.
Based on kvmarm/fixes (181a701fc14d3), which has [1].
Cheers,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
Fuad Tabba (3):
KVM: arm64: vgic: Drop the pending state of all LPIs when disabling
LPIs
KVM: arm64: vgic-v3: Don't save pending tables with LPIs disabled
KVM: arm64: vgic-its: Don't read the pending table when mapping an LPI
.../virt/kvm/devices/arm-vgic-v3.rst | 1 + arch/arm64/kvm/vgic/vgic-its.c | 33 ++++++++---- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 13 ++--- arch/arm64/kvm/vgic/vgic-v3.c | 50 ++----------------- arch/arm64/kvm/vgic/vgic.c | 16 +++++- arch/arm64/kvm/vgic/vgic.h | 1 - 6 files changed, 49 insertions(+), 65 deletions(-)
base-commit: 181a701fc14d3d468c603b5ecfb9f12115c5fb72
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 1/3 KVM: arm64: vgic: Drop the pending state of all LPIs when disabling LPIs
When the guest clears GICR_CTLR.EnableLPIs, vgic_flush_pending_lpis() retires the LPIs on the vCPU's AP list. However, an LPI disabled in the configuration table can be pending without being on that list. An LPI moved to the vCPU by MOVI can also still be in another vCPU's LRs, which pausing the vCPU doesn't fold back. These LPIs stay pending, and the guest takes them even after re-enabling LPIs with GICR_PENDBASER.PTZ set.
Halt the guest rather than the vCPU, so that no vCPU can fold such an LPI back from its LRs after the flush. Have the flush clear the pending state of every LPI targeting the vCPU. A forwarded vLPI's pending state is in its vPE's pending table, which the flush doesn't clear.
Invalidate the translation cache before the flush. Only an MSI racing with the disable can then make an LPI pending, or cache its translation for later ones.
--- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 13 +++++++------ arch/arm64/kvm/vgic/vgic.c | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 7 deletions(-)
arch/arm64/kvm/vgic/vgic-mmio-v3.c +7 -6
@@ -298,14 +298,15 @@ static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,/* * Yes, disabling LPIs is painful, since it can be done from * a *remote* vcpu! So let's not take any chance, and make -* sure that the target vCPU has written their LRs back to the +* sure that every vCPU has written their LRs back to the * irq structures, and release any reference they would have. -* Pause it before its LPIs read as disabled, so that the exit's -* prune leaves them for vgic_flush_pending_lpis() to retire. +* Halt them before the LPIs read as disabled, so that none can +* fold an LPI back from its LRs after vgic_flush_pending_lpis() +* has cleared it. * * If it hurts, don't do it. */ -kvm_pause_vcpu (vcpu); +kvm_arm_halt_guest (vcpu->kvm);/* * Don't disable if RWP is set, as there already an
@@ -315,12 +316,12 @@ static void vgic_mmio_write_v3r_ctlr(struct kvm_vcpu *vcpu,GICR_CTLR_ENABLE_LPIS , GICR_CTLR_RWP);if (ctlr ==GICR_CTLR_ENABLE_LPIS ) { -vgic_flush_pending_lpis (vcpu);vgic_its_invalidate_all_caches (vcpu->kvm); +vgic_flush_pending_lpis (vcpu);atomic_set_release (&vgic_cpu->ctlr,0 ); } -kvm_resume_vcpu (vcpu); +kvm_arm_resume_guest (vcpu->kvm); }else { ctlr =atomic_cmpxchg_acquire (&vgic_cpu->ctlr,0 ,GICR_CTLR_ENABLE_LPIS );
arch/arm64/kvm/vgic/vgic.c +15 -1
@@ -207,9 +207,10 @@ static void vgic_release_deleted_lpis(struct kvm *kvm)void vgic_flush_pending_lpis (struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; +struct vgic_dist *dist = &vcpu->kvm->arch.vgic;struct vgic_irq *irq, *tmp; +unsigned long intid, flags;bool deleted = false; -unsigned long flags;raw_spin_lock_irqsave (&vgic_cpu->ap_list_lock, flags);
@@ -228,6 +229,19 @@ void vgic_flush_pending_lpis(struct kvm_vcpu *vcpu)if (deleted)vgic_release_deleted_lpis (vcpu->kvm); + +/* Disabled LPIs and LPIs MOVI'd here can be pending off this AP list. */ +xa_for_each (&dist->lpi_xa, intid, irq) { + irq =vgic_get_irq (vcpu->kvm, intid); +if (!irq) +continue ; + +raw_spin_lock_irqsave (&irq->irq_lock, flags); +if (irq->target_vcpu == vcpu) + irq->pending_latch = false; +raw_spin_unlock_irqrestore (&irq->irq_lock, flags); +vgic_put_irq (vcpu->kvm, irq); + } }void vgic_irq_set_phys_pending (struct vgic_irq *irq,bool pending)
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 2/3 KVM: arm64: vgic-v3: Don't save pending tables with LPIs disabled
In reply to Fuad TabbaKVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES writes each LPI's pending state into its redistributor's pending table, clearing the bit of an LPI that isn't pending. However, once a redistributor has LPIs disabled and RWP reads 0, "there are no further accesses by the GIC to the LPI Pending table" (IHI 0069H.b, 5.1.2) until enabling LPIs reads it. A bit the guest sets there is cleared by the next save, which QEMU runs whenever it stops the VM, and the guest never takes the LPI.
Skip the LPIs of a redistributor with LPIs disabled, and document it. On GICv4.1 this drops the state of a forwarded vLPI there, which the disable is allowed to lose: "There is no guarantee that clearing GICR_CTLR.EnableLPIs causes the LPI Pending table to be updated in memory" (5.1.2).
--- Documentation/virt/kvm/devices/arm-vgic-v3.rst | 1 + arch/arm64/kvm/vgic/vgic-v3.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
Documentation/virt/kvm/devices/arm-vgic-v3.rst +1 -0
@@ -308,6 +308,7 @@ Groups: save all LPI pending bits into guest RAM pending tables. The first kB of the pending table is not altered by this operation. + Neither is the pending table of a redistributor with LPIs disabled. Errors:
arch/arm64/kvm/vgic/vgic-v3.c +3 -1
@@ -625,7 +625,9 @@ int vgic_v3_save_pending_tables(struct kvm *kvm)continue ; vcpu = irq->target_vcpu; -if (!vcpu) + +/* While LPIs are disabled, the pending table is the guest's. */ +if (!vcpu || !vgic_lpis_enabled (vcpu)) goto put_irq; pendbase =GICR_PENDBASER_ADDRESS (vcpu->arch.vgic_cpu.pendbaser);
Signature · 1 line
2.47.3
Fuad Tabba
reviewerPATCH 3/3 KVM: arm64: vgic-its: Don't read the pending table when mapping an LPI
In reply to Fuad TabbaMapping an LPI makes it pending if its bit in the target redistributor's pending table is set. While the redistributor has LPIs enabled, KVM doesn't keep that bit current. Saving the pending tables, as QEMU does whenever it stops the VM, sets the bit of each pending LPI, and the bit stays set once the guest has taken the LPI. Discarding the LPI and mapping its INTID again makes it pending a second time.
MAPTI (IHI 0069H.b, 5.3.12) doesn't read the table, so stop reading it when an LPI is created. Read it once the ITS tables are restored instead, for each redistributor with LPIs enabled, as enabling LPIs does (5.1.2). The documented restore order restores the redistributors first.
The copy that commit b4c9f30194a4 ("KVM: arm64: vgic-v3: Don't drop a pending LPI mapped with LPIs disabled") takes while LPIs are disabled goes too, now that the save leaves that bit alone.
An LPI mapped after its redistributor enabled LPIs no longer picks up a bit the table held at the enable. A GIC makes the LPI pending at the enable, mapped or not (5.1.2). KVM has no pending state for an unmapped INTID, and a bit the guest wrote is indistinguishable from one a save left behind. Linux enables LPIs with a zeroed table.
--- arch/arm64/kvm/vgic/vgic-its.c | 33 ++++++++++++++++-------- arch/arm64/kvm/vgic/vgic-v3.c | 46 ---------------------------------- arch/arm64/kvm/vgic/vgic.h | 1 - 3 files changed, 23 insertions(+), 57 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +23 -10
@@ -142,7 +142,7 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,* However we only have those structs for mapped IRQs, so we read in * the respective config data from memory here upon mapping the LPI. * -* Should any of these fail, behave as if we couldn't create the LPI +* Should that fail, behave as if we couldn't create the LPI * by dropping the refcount and returning the error. */ ret =update_lpi_config (kvm, irq,NULL , false);
@@ -151,12 +151,6 @@ static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,return ERR_PTR (ret); } - ret =vgic_v3_lpi_sync_pending_status (kvm, irq); -if (ret) { -vgic_put_irq (kvm, irq); -return ERR_PTR (ret); - } -return irq; }
@@ -2710,6 +2704,9 @@ static int vgic_its_save_tables_v0(struct vgic_its *its)*/ static int vgic_its_restore_tables_v0 (struct vgic_its *its) { +struct kvm *kvm = its->dev->kvm; +struct kvm_vcpu *vcpu; +unsigned long i;int ret; ret =vgic_its_restore_collection_table (its);
@@ -2717,9 +2714,25 @@ static int vgic_its_restore_tables_v0(struct vgic_its *its)return ret; ret =vgic_its_restore_device_tables (its); -if (ret) -vgic_its_free_collection_list (its->dev->kvm, its); -return ret; +if (ret) { +vgic_its_free_collection_list (kvm, its); +return ret; + } + +/* Enabling LPIs read the pending tables before these LPIs existed. */ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (!vgic_lpis_enabled (vcpu)) +continue ; + + ret =its_sync_lpi_pending_table (vcpu); +if (ret) { +vgic_its_free_device_list (kvm, its); +vgic_its_free_collection_list (kvm, its); +return ret; + } + } + +return 0 ; }static void vgic_its_commit_v0 (struct vgic_its *its)
arch/arm64/kvm/vgic/vgic-v3.c +0 -46
52 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic.h +0 -1
@@ -347,7 +347,6 @@ void vgic_v3_reset(struct kvm_vcpu *vcpu);void vgic_v3_enable_cpuif_traps (void );int vgic_v3_probe (const struct gic_kvm_info *info);int vgic_v3_map_resources (struct kvm *kvm); -int vgic_v3_lpi_sync_pending_status (struct kvm *kvm,struct vgic_irq *irq);int vgic_v3_save_pending_tables (struct kvm *kvm);int vgic_v3_set_redist_base (struct kvm *kvm,u32 index,u64 addr,u32 count);int vgic_register_redist_iodev (struct kvm_vcpu *vcpu);
Signature · 1 line
2.47.3
[email protected]
AI reviewRe: [PATCH 3/3] KVM: arm64: vgic-its: Don't read the pending table when mapping an LPI
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] KVM: arm64: vgic-its: its_sync_lpi_pending_table() reads unrelated LPIs - [Medium] KVM: arm64…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] KVM: arm64: vgic-its: its_sync_lpi_pending_table() reads unrelated LPIs
- [Medium] KVM: arm64: vgic-its: uncleaned queued LPIs in vgic_its_restore_tables_v0() --
commit 5977878e13acf988213f1898fd84a41cd479ed52
Author: Fuad Tabba <[email protected]>
KVM: arm64: vgic-its: Don't read the pending table when mapping an LPI
This commit stops reading the redistributor LPI pending table when mapping an LPI in vgic_add_lpi() and removes vgic_v3_lpi_sync_pending_status(). Instead, it synchronizes the pending tables for all vCPUs with LPIs enabled at the end of vgic_its_restore_tables_v0() via its_sync_lpi_pending_table().
Quoted 4 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0
[ ... ]
Quoted 18 lines
arch/arm64/kvm/vgic/vgic-its.c +4
-3@@ -2710,6 +2704,9 @@ static int vgic_its_save_tables_v0(struct vgic_its *its)*/ static int vgic_its_restore_tables_v0 (struct vgic_its *its) { +struct kvm *kvm = its->dev->kvm; +struct kvm_vcpu *vcpu; +unsigned long i;int ret; ret =vgic_its_restore_collection_table (its);@@ -2717,9 +2714,25 @@ static int vgic_its_restore_tables_v0(struct vgic_its *its)return ret; ret =vgic_its_restore_device_tables (its); -if (ret) -vgic_its_free_collection_list (its->dev->kvm, its); -return ret; +if (ret) {
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_restore_tables_v0(struct vgic_its *its) · line 2727
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-its.c +10
-0+vgic_its_free_collection_list (kvm, its); +return ret; + } + +/* Enabling LPIs read the pending tables before these LPIs existed. */ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (!vgic_lpis_enabled (vcpu)) +continue ; + + ret =its_sync_lpi_pending_table (vcpu);
[Severity: Medium]
Can calling its_sync_lpi_pending_table() here cause KVM_DEV_ARM_ITS_RESTORE_TABLES to fail with -EFAULT when a vCPU with LPIs enabled has a pending table that does not cover LPI INTIDs targeting other vCPUs or unmapped collections?
Previously, vgic_add_lpi() called vgic_v3_lpi_sync_pending_status(), which checked irq->target_vcpu (returning 0 if NULL) and only read the pending table of the redistributor that the LPI actually targeted.
In contrast, its_sync_lpi_pending_table() iterates over every LPI in dist->lpi_xa and reads from the current vCPU's pendbase before checking whether the LPI targets that vCPU:
arch/arm64/kvm/vgic/vgic-its.c:its_sync_lpi_pending_table() {
xa_for_each(&dist->lpi_xa, intid, irq) {
...
if (byte_offset != last_byte_offset) {
ret = kvm_read_guest_lock(vcpu->kvm,
pendbase + byte_offset,
&pendmask, 1);
if (ret)
return ret;
last_byte_offset = byte_offset;
}
irq = vgic_get_irq(vcpu->kvm, intid);
if (!irq)
continue;
raw_spin_lock_irqsave(&irq->irq_lock, flags);
if (irq->target_vcpu == vcpu)
irq->pending_latch = pendmask & (1U << bit_nr);
vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
vgic_put_irq(vcpu->kvm, irq);
}
}If a guest enables LPIs on a vCPU that has no LPIs targeting it (or only lower-INTID LPIs) with an unmapped or smaller pending table in GICR_PENDBASER, while LPIs in dist->lpi_xa target other vCPUs or unmapped collections, its_sync_lpi_pending_table() will read that vCPU's pending table at the byte offsets of those unrelated LPIs and return -EFAULT. That also performs O(nr_vcpus * nr_lpis) guest memory reads and vgic_queue_irq_unlock() calls across all vCPUs during restore.
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_restore_tables_v0(struct vgic_its *its) · line 2732
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-its.c +5
-0+if (ret) { +vgic_its_free_device_list (kvm, its); +vgic_its_free_collection_list (kvm, its); +return ret; + }
[Severity: Medium]
If its_sync_lpi_pending_table() fails partway through syncing the vCPUs' pending tables, does this error path leave already-queued LPIs referenced in dist->lpi_xa and on vcpu->arch.vgic_cpu.ap_list_head?
During the documented restore sequence, the distributor and redistributors are restored before KVM_DEV_ARM_ITS_RESTORE_TABLES is invoked. When its_sync_lpi_pending_table() sees a pending bit set for an enabled LPI, it sets irq->pending_latch and calls vgic_queue_irq_unlock(), which takes an additional reference via vgic_get_irq_ref() and adds irq->ap_list to vcpu->arch.vgic_cpu.ap_list_head.
If a later kvm_read_guest_lock() call fails (on a subsequent byte offset or a subsequent vCPU), vgic_its_free_device_list() frees the ITEs via its_free_ite():
arch/arm64/kvm/vgic/vgic-its.c:its_free_ite() {
if (irq) {
scoped_guard(raw_spinlock_irqsave, &irq->irq_lock) {
if (irq->hw)
its_unmap_vlpi(ite->irq->host_irq);
irq->hw = false;
irq->mapped = false;
}
vgic_put_irq(kvm, ite->irq);
}
}Because its_free_ite() only drops the single ITE reference without clearing irq->pending_latch or flushing ap_list_head, any LPI that was already queued retains its ap_list reference and stays in dist->lpi_xa and on ap_list_head with irq->mapped = false. If the vCPU runs, vgic_target_oracle() does not check irq->mapped and can inject the unmapped LPI, or if KVM_DEV_ARM_ITS_RESTORE_TABLES is retried, vgic_add_lpi() will find the existing vgic_irq in dist->lpi_xa and return it early without calling update_lpi_config().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Fuad Tabba
reviewerRe: [PATCH 3/3] KVM: arm64: vgic-its: Don't read the pending table when mapping an LPI
On Thu, 05 Nov 2026 11:23:34 +0000, [email protected] wrote:
[...]
Quoted 1 line
Quoted 1 line
arch/arm64/kvm/vgic/vgic-its.c +0
-0
[...]
Quoted 1 line
Quoted 1 line
arch/arm64/kvm/vgic/vgic-its.c +0
-0@@ -2717,9 +2714,25 @@ static int vgic_its_restore_tables_v0(struct vgic_its *its)
[...]
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_restore_tables_v0(struct vgic_its *its) · line 2727
Quoted 6 lines
arch/arm64/kvm/vgic/vgic-its.c +6
-0+/* Enabling LPIs read the pending tables before these LPIs existed. */ +kvm_for_each_vcpu (i, vcpu, kvm) { +if (!vgic_lpis_enabled (vcpu)) +continue ; + + ret =its_sync_lpi_pending_table (vcpu);[Severity: Medium]
Can callingits_sync_lpi_pending_table()here causeKVM_DEV_ARM_ITS_RESTORE_TABLESto fail with-EFAULTwhen a vCPU with LPIs enabled has a pending table that does not cover LPI INTIDs targeting other vCPUs or unmapped collections?
Yes. Unlike the map-time read, this reads each LPI-enabled redistributor's table at the bits of LPIs targeting others. Will fix in v2 by skipping those LPIs.
[...]
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_restore_tables_v0(struct vgic_its *its) · line 2732
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-its.c +5
-0+if (ret) { +vgic_its_free_device_list (kvm, its); +vgic_its_free_collection_list (kvm, its); +return ret; + }[Severity: Medium]
Ifits_sync_lpi_pending_table()fails partway through syncing the vCPUs' pending tables, does this error path leave already-queued LPIs referenced indist->lpi_xaand onvcpu->arch.vgic_cpu.ap_list_head?
Pre-existing. On kvmarm/fixes, vgic_add_lpi() already queues a pending LPI as the restore maps it, so a later device-table failure leaves the same state. The early return in vgic_add_lpi() on a retried restore is a separate bug.
Cheers,
/fuad
Marc Zyngier
maintainerQuoted 6 lines · Fuad Tabba
Hi folks,
The GIC just keeps on giving. Three follow-ups to my LPI fixes for redistributors with LPIs disabled [1], to fix pre-existing bugs Sashiko found.
Can you *please* come up with a full series rather than this constant "here's another set of patches" that address the same thing, one pothole at a time?
I'm now really tempted to drop all the patches I have queued on this so far, and wait for the "one true series".
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Fuad Tabba
reviewerQuoted 10 lines · Marc Zyngier
Quoted 6 lines · Fuad Tabba
Hi folks,
The GIC just keeps on giving. Three follow-ups to my LPI fixes for redistributors with LPIs disabled [1], to fix pre-existing bugs Sashiko found.
Quoted 6 lines
Can you *please* come up with a full series rather than this constant "here's another set of patches" that address the same thing, one pothole at a time?
I'm now really tempted to drop all the patches I have queued on this so far, and wait for the "one true series".
Sorry about that. Every time I think I'm done with it, Sashiko pokes more holes!
I'll sit on this a bit more, make sure all the gaps are closed, Sashiko it locally, before I respin.
Cheers,
/fuad
Quoted 5 lines
M.--
Without deviation from the norm, progress is not possible.