KVM: x86: ioapic: Update remote_irr only after successful delivery
PATCH v3 1/3 KVM: x86: ioapic: Update remote_irr only after successful delivery
From: Hao Zhang <[email protected]>
The I/O APIC sets remote_irr for level-triggered interrupts that have been accepted by a local APIC and are waiting for an EOI.
ioapic_service() currently treats any non-zero return from kvm_irq_delivery_to_apic() as successful delivery. But kvm_irq_delivery_to_apic() returns -1 when no destination is found. Setting remote_irr for that failed delivery leaves the pin blocked forever waiting for an EOI that will never be generated.
Update remote_irr only when the delivery result is positive, i.e. when at least one local APIC accepted the interrupt.
--- arch/x86/kvm/ioapic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/ioapic.c +1 -1
@@ -491,7 +491,7 @@ static int ioapic_service(struct kvm_ioapic *ioapic, int irq, bool line_status) }else ret =kvm_irq_delivery_to_apic (ioapic->kvm,NULL , &irqe); -if (ret && irqe.trig_mode ==IOAPIC_LEVEL_TRIG ) +if (ret >0 && irqe.trig_mode ==IOAPIC_LEVEL_TRIG ) entry->fields.remote_irr =1 ;return ret;
base-commit: c21bb4193868a8de71fc4693fa741e195fdf5d86
Signature · 1 line
2.15.0
PATCH v3 2/3 KVM: selftests: Verify failed IOAPIC delivery preserves state
From: Hao Zhang <[email protected]>
Add an x86 selftest for the in-kernel I/O APIC state that is exposed through KVM_GET_IRQCHIP.
Create a VM with an in-kernel irqchip and no vCPUs, route an unmasked level-triggered I/O APIC pin to a non-existent APIC ID, and verify that failed delivery does not set remote_irr.
--- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/x86/ioapic_state_test.c | 92 ++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/ioapic_state_test.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -86,6 +86,7 @@ TEST_GEN_PROGS_x86 += x86/hyperv_features TEST_GEN_PROGS_x86 += x86/hyperv_ipi TEST_GEN_PROGS_x86 += x86/hyperv_svm_test TEST_GEN_PROGS_x86 += x86/hyperv_tlb_flush +TEST_GEN_PROGS_x86 += x86/ioapic_state_test TEST_GEN_PROGS_x86 += x86/kvm_clock_test TEST_GEN_PROGS_x86 += x86/kvm_pv_test TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
tools/testing/selftests/kvm/x86/ioapic_state_test.c new file +92 -0
92 lines not on this page · raw message
Signature · 1 line
2.15.0
PATCH v3 3/3 KVM: x86: ioapic: Preserve irr_delivered for duplicate edge interrupts
In reply to Hao ZhangFrom: Hao Zhang <[email protected]>
For edge-triggered interrupts, ioapic_set_irq() clears irr_delivered before checking whether the new edge is a duplicate. A duplicate edge is then coalesced without being serviced, leaving the interrupt visible in KVM_GET_IRQCHIP state even though the original interrupt was already delivered.
Move the state update after the duplicate-edge check so duplicate edges do not make a delivered interrupt appear pending during VM migration.
Add a selftest to verify that a duplicate edge interrupt does not make an already-delivered interrupt visible in the saved IRR state.
--- arch/x86/kvm/ioapic.c | 2 +- .../testing/selftests/kvm/x86/ioapic_state_test.c | 47 ++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-)
arch/x86/kvm/ioapic.c +1 -1
@@ -230,11 +230,11 @@ static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq, old_irr = ioapic->irr; ioapic->irr |= mask;if (edge) { - ioapic->irr_delivered &= ~mask;if (old_irr == ioapic->irr) { ret =0 ; goto out; } + ioapic->irr_delivered &= ~mask; } ret =ioapic_service (ioapic, irq, line_status);
tools/testing/selftests/kvm/x86/ioapic_state_test.c +47 -0
69 lines not on this page · raw message
Signature · 1 line
2.15.0
[email protected]
AI reviewRe: [PATCH v3 3/3] KVM: x86: ioapic: Preserve irr_delivered for duplicate edge interrupts
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but changing an IOAPIC pin's trigger mode from edge-triggered to level-triggered via MMIO does not clear irr_delivered, causing subsequent level-triggered interrupts to be masked and lost across live migration. --
--- Patch [3]: [PATCH v3 3/3] KVM: x86: ioapic: Preserve irr_delivered for duplicate edge interrupts --- commit 591f7b002947a1810eb5f06b6ab21c5e4d90fcae
Author: Hao Zhang <[email protected]>
KVM: x86: ioapic: Preserve irr_delivered for duplicate edge interrupts
This commit moves the state update of irr_delivered after the duplicate edge check in ioapic_set_irq() to prevent duplicate edge interrupts from leaving the interrupt visible in the KVM_GET_IRQCHIP state. It also adds a selftest to verify this behavior.
Quoted 5 lines
arch/x86/kvm/ioapic.c +0
-0
arch/x86/kvm/ioapic.c static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq, · line 238
Quoted 10 lines
arch/x86/kvm/ioapic.c +1
-1@@ -230,11 +230,11 @@ static int ioapic_set_irq(struct kvm_ioapic *ioapic, unsigned int irq, old_irr = ioapic->irr; ioapic->irr |= mask;if (edge) { - ioapic->irr_delivered &= ~mask;if (old_irr == ioapic->irr) { ret =0 ; goto out; } + ioapic->irr_delivered &= ~mask; }
[Severity: High]
This is a pre-existing issue, but does changing an IOAPIC pin's trigger mode from edge-triggered to level-triggered via MMIO leave a stale irr_delivered bit?
If a guest configures a pin as edge-triggered, an interrupt sets the bit in ioapic->irr_delivered. If the guest then reconfigures the pin to level-triggered in ioapic_write_indirect(), the stale irr_delivered bit is not cleared.
When a subsequent level interrupt is asserted, ioapic_set_irq() sets the irr bit but skips clearing irr_delivered because the pin is now level-triggered and edge is false.
During VM migration, kvm_get_ioapic() extracts the state:
state->irr &= ~ioapic->irr_delivered;
Could this stale irr_delivered bit incorrectly mask out the pending level interrupt and permanently lose it across live migration?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3