Fixes for various kvm-arm vulnerabilities
Subsystems (6)
Paolo Bonzini
maintainercover Fixes for various kvm-arm vulnerabilities
- Bound PINTID during ITE table restore, avoiding the possibility that the guest plants an out-of-range LPI INTID in its own in-memory ITS tables prior to restore.
- Do not blindly trust the vINTID field coming from KVM, and reject anything that is out of the boundaries that the hardware ITS is configured to support.
- Add signal_pending checks to the scan_its_table() loop; device table entries with num_eventid_bits = 16 (or 65536 EventIDs per device) are valid but takes a long time to restore.
- Remove quadratic complexity from the algorithm used to prune IRQs sitting in the AP list has a quadratic complexity, and simplify the locking while at that.
Thanks,
Paolo
Marc Zyngier (2):
irqchip/gic-v4: Contain VMAPTI vINTID to the expected boundaries
KVM: arm64: vgic: Linearise AP list pruning process
Raghavendra Rao Ananta (2):
KVM: arm64: vgic-its: Bound PINTID during ITE table restore
KVM: arm64: vgic-its: Add signal checks in scan_its_table()
arch/arm64/kvm/vgic/vgic-its.c | 6 +- arch/arm64/kvm/vgic/vgic.c | 175 +++++++++++++++---------------- drivers/irqchip/irq-gic-v3-its.c | 3 +- 3 files changed, 94 insertions(+), 90 deletions(-)
Signature · 1 line
2.56.1
Paolo Bonzini
maintainerPATCH 1/4 KVM: arm64: vgic-its: Bound PINTID during ITE table restore
From: Raghavendra Rao Ananta <[email protected]>
During ITS table restore (KVM_DEV_ARM_ITS_RESTORE_TABLES), vgic_its_restore_ite() validates only that the restored interrupt ID satisfies lpi_id >= VGIC_MIN_LPI (8192) and fails to validate the upper bound against max_lpis_propbaser(propbaser).
Since the ITE PINTID field is 32-bit (bits 47:16), a guest can plant an out-of-range LPI INTID in its own in-memory ITS tables prior to restore. When direct MSI forwarding attaches (via kvm_vgic_v4_set_forwarding()), this unchecked intid causes two severe consequences:
1. Out-of-Bounds Memory Write:
In its_vlpi_map(), lpi_write_config() calculates the configuration byte target address as page_address(map->vm->vprop_page) + hwirq - 8192. Since vprop_page is a 64KB page-granular host allocation and hwirq is unbounded, this causes a 1-byte read-modify-write up to 4GB past the allocation into neighboring memory, with both the offset and written value controlled by the guest.
2. Hardware Command Queue Wedge (Host MSI/IRQ DoS):
Immediately following the write, its_vlpi_map() issues a physical VMAPTI command with virt_id = map->vintid. Because virt_id exceeds the hardware's architected ID limits, the physical GICv4.1 ITS command queue halts on the malformed descriptor. The hardware read pointer becomes stuck, causing all subsequent ITS commands to time out and permanently breaking host-wide MSI handling until reboot:
ITS cmd its_build_discard_cmd failed
ITS queue timeout (24768 24481)
ITS cmd its_build_mapd_cmd failed
ITS queue timeout (24800 24481)
ITS cmd its_build_vmapp_cmd failed
Fix this by validating the upper bound of lpi_id against max_lpis_propbaser() in vgic_its_restore_ite(), mirroring the check in vgic_its_cmd_handle_mapi().
--- arch/arm64/kvm/vgic/vgic-its.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-its.c +2 -1
@@ -2248,7 +2248,8 @@ static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,if (!lpi_id)return 1 ;/* invalid entry, no choice but to scan next entry */ -if (lpi_id <VGIC_MIN_LPI ) +if (lpi_id <VGIC_MIN_LPI || + lpi_id >=max_lpis_propbaser (kvm->arch.vgic.propbaser))return -EINVAL ; offset = val >>KVM_ITS_ITE_NEXT_SHIFT ;
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 1/4] KVM: arm64: vgic-its: Bound PINTID during ITE table restore
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: arm64: vgic_its_restore_ite() rejects saved ITEs after PROPBASER shrink --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: arm64: vgic_its_restore_ite() rejects saved ITEs after PROPBASER shrink
--
commit 350aaf29f4addc9485e47e0e35ae2fc1cf8fd8eb
Author: Raghavendra Rao Ananta <[email protected]>
KVM: arm64: vgic-its: Bound PINTID during ITE table restore
This commit adds an upper-bound check against max_lpis_propbaser() for lpi_id in vgic_its_restore_ite() during ITS table restore.
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id, · line 2253
Quoted 12 lines
arch/arm64/kvm/vgic/vgic-its.c +2
-1@@ -2248,7 +2248,8 @@ static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,if (!lpi_id)return 1 ;/* invalid entry, no choice but to scan next entry */ -if (lpi_id <VGIC_MIN_LPI ) +if (lpi_id <VGIC_MIN_LPI || + lpi_id >=max_lpis_propbaser (kvm->arch.vgic.propbaser))return -EINVAL ;
[Severity: Medium]
Can this check break KVM_DEV_ARM_ITS_RESTORE_TABLES on ITS tables legitimately saved by KVM_DEV_ARM_ITS_SAVE_TABLES?
A guest can map an LPI with MAPTI under a larger GICR_PROPBASER.IDbits and later shrink IDbits via vgic_mmio_write_propbase() on a vCPU with LPIs disabled. Shrinking dist->propbaser does not discard existing ITEs, leaving an ITE with intid >= max_lpis_propbaser(propbaser).
vgic_its_save_ite() saves every mapped ITE without checking max_lpis_propbaser(propbaser). When userspace restores the shrunken GICR_PROPBASER before KVM_DEV_ARM_ITS_RESTORE_TABLES, this check now returns -EINVAL and aborts live migration or snapshot restore.
Skipping ITEs outside max_lpis_propbaser(propbaser) in vgic_its_save_ite() and compute_next_eventid_offset() would preserve save/restore symmetry while keeping this bound check.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Paolo Bonzini
maintainerPATCH 2/4 irqchip/gic-v4: Contain VMAPTI vINTID to the expected boundaries
In reply to Paolo BonziniFrom: Marc Zyngier <[email protected]>
Do not blindly trust the vINTID field coming from KVM, and reject anything that is out of the boundaries that the hardware ITS is configured to support. These LPIs can still be injected as purely virtual interrupt.
--- drivers/irqchip/irq-gic-v3-its.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/irqchip/irq-gic-v3-its.c +2 -1
@@ -1922,7 +1922,8 @@ static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)struct its_device *its_dev =irq_data_get_irq_chip_data (d);u32 event =its_get_event_id (d); -if (!info->map) +if (!info->map || info->map->vintid <8192 || + info->map->vintid >=BIT_ULL (LPI_NRBITS ))return -EINVAL ;if (!its_dev->event_map.vm) {
Signature · 1 line
2.56.1
Paolo Bonzini
maintainerPATCH 3/4 KVM: arm64: vgic-its: Add signal checks in scan_its_table()
In reply to Paolo BonziniFrom: Raghavendra Rao Ananta <[email protected]>
During ITS table restore (KVM_DEV_ARM_ITS_RESTORE_TABLES), scan_its_table() walks guest RAM to restore Device Table Entries (DTEs) and Interrupt Translation Tables (ITTs) while holding kvm->lock, all vCPU mutexes, kvm->arch.config_lock, and its->its_lock.
Because a guest can plant valid DTEs with num_eventid_bits = 16 (size = 15, or 65,536 EventIDs per device) pointing to zeroed/sparse ITT memory (!lpi_id), vgic_its_restore_ite() returns +1 for each unmapped entry. This forces scan_its_table() to scan up to 65,536 entries per device (up to 4.30B reads for a 1-level flat table or 21.48B reads for a 2-level indirect table).
Because scan_its_table() lacks signal checks, executing KVM_DEV_ARM_ITS_RESTORE_TABLES monopolizes the host CPU in kernel space for tens to hundreds of seconds.
Fix this by checking signal_pending(current) at the start of the scan_its_table() loop to allow immediate cancellation (-EINTR) if a VMM timeout or termination signal arrives.
--- arch/arm64/kvm/vgic/vgic-its.c | 3 +++ 1 file changed, 3 insertions(+)
arch/arm64/kvm/vgic/vgic-its.c +3 -0
@@ -2171,6 +2171,9 @@ static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz,int next_offset;size_t byte_offset; +if (signal_pending (current)) +return -EINTR ; + ret =kvm_read_guest_lock (kvm, gpa, entry, esz);if (ret)return ret;
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 3/4] KVM: arm64: vgic-its: Add signal checks in scan_its_table()
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KVM: arm64: scan_its_table() returns raw -EINTR on signal_pending() - [Low] KVM: arm64: vgic-its…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: arm64: scan_its_table() returns raw -EINTR on signal_pending()
- [Low] KVM: arm64: vgic-its: document -EINTR for KVM_DEV_ARM_ITS_RESTORE_TABLES
--
commit c47af74ba02a050024193468999b27c29d77d85d
Author: Raghavendra Rao Ananta <[email protected]>
KVM: arm64: vgic-its: Add signal checks in scan_its_table()
This commit adds a signal_pending() check to the scan_its_table() loop so KVM_DEV_ARM_ITS_RESTORE_TABLES returns -EINTR when a signal is pending.
Quoted 5 lines
arch/arm64/kvm/vgic/vgic-its.c +0
-0
arch/arm64/kvm/vgic/vgic-its.c static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz, · line 2179
Quoted 9 lines
arch/arm64/kvm/vgic/vgic-its.c +3
-0@@ -2171,6 +2171,9 @@ static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz,int next_offset;size_t byte_offset; +if (signal_pending (current)) +return -EINTR ; + ret =kvm_read_guest_lock (kvm, gpa, entry, esz);if (ret)return ret;
[Severity: Medium]
Can returning raw -EINTR on signal_pending(current) cause spurious KVM_DEV_ARM_ITS_RESTORE_TABLES failures?
signal_pending() also triggers on TIF_NOTIFY_SIGNAL task_work (such as io_uring) and SA_RESTART signals. Because arch_do_signal_or_restart() only restarts -ERESTART* codes, raw -EINTR reaches userspace and aborts restore in VMMs that do not expect or retry -EINTR on this one-shot ioctl.
Switching to -ERESTARTSYS or retrying in userspace is also not idempotent:vgic_v3_lpi_sync_pending_status() clears pending bits in guest GICR_PENDBASER RAM during restore, and rollback via its_free_ite() -> vgic_put_irq() frees unqueued disabled LPIs, silently losing those pending interrupts on retry.
Checking fatal_signal_pending(current) instead avoids both spurious interruptions and non-idempotent retries.
[Severity: Low]
Should Documentation/virt/kvm/devices/arm-vgic-its.rst be updated to list -EINTR in the KVM_DEV_ARM_VGIC_GRP_CTRL Errors table if this ioctl can now return -EINTR to userspace?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Paolo Bonzini
maintainerPATCH 4/4 KVM: arm64: vgic: Linearise AP list pruning process
In reply to Paolo BonziniFrom: Marc Zyngier <[email protected]>
The algorithm used to prune IRQs sitting in the AP list has a quadratic complexity, as any concurrent change in the list results in a restart of the processing. The locking is also rather complicated, as we have to take the locks for both source and target vcpus, plus the per-irq lock.
While this has served us well so far, people are finding new and amusing ways to abuse the locking and perform a DoS on the host. Not fun.
Instead, switch to a linear approach where potentially moved IRQs are taken off the AP list and then dispatched onto their new targets. This simplifies the locking (only one vcpu lock at any given time), and only the interrupts that have been changed during that process are evaluated again. This allows the pruning process to converge much quicker.
--- arch/arm64/kvm/vgic/vgic.c | 175 ++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 88 deletions(-)
arch/arm64/kvm/vgic/vgic.c +87 -88
@@ -754,6 +754,29 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)return ret; } +/* +* This helper checks the target *and* does the laundry, nuking the irq off +* the ap_list when necessary, and doing the refcount drop on LPIs. -ENOENT +* is returned when LPI release is warranted. +*/ +static struct kvm_vcpu *vgic_predict_target (struct kvm *kvm,struct vgic_irq *irq) +{ +struct kvm_vcpu *target; + +lockdep_assert_held (&irq->irq_lock); + + target =vgic_target_oracle (irq); +if (!target) { +list_del (&irq->ap_list); + irq->vcpu =NULL ; + +if (vgic_put_irq_norelease (kvm, irq)) + target =ERR_PTR (-ENOENT ); + } + +return target; +} +/** * vgic_prune_ap_list - Remove non-relevant interrupts from the list *
@@ -765,109 +788,83 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)static void vgic_prune_ap_list (struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + DECLARE_BITMAP(pend_map, VGIC_V3_MAX_CPUS) = {}; + DECLARE_BITMAP(kick_map, VGIC_V3_MAX_CPUS) = {}; +struct kvm_vcpu *irq_target;struct vgic_irq *irq, *tmp;bool deleted_lpis = false; +LIST_HEAD (culling_list); +int i;DEBUG_SPINLOCK_BUG_ON (!irqs_disabled ()); -retry:raw_spin_lock (&vgic_cpu->ap_list_lock);list_for_each_entry_safe (irq, tmp, &vgic_cpu->ap_list_head, ap_list) { -struct kvm_vcpu *target_vcpu, *vcpuA, *vcpuB; -bool target_vcpu_needs_kick = false; - -raw_spin_lock (&irq->irq_lock); - +guard (raw_spinlock)(&irq->irq_lock);BUG_ON (vcpu != irq->vcpu); - target_vcpu =vgic_target_oracle (irq); - -if (!target_vcpu) { -/* -* We don't need to process this interrupt any -* further, move it off the list. -*/ -list_del (&irq->ap_list); - irq->vcpu =NULL ; -raw_spin_unlock (&irq->irq_lock); - -/* -* This vgic_put_irq call matches the -* vgic_get_irq_ref in vgic_queue_irq_unlock, -* where we added the LPI to the ap_list. As -* we remove the irq from the list, we drop -* also drop the refcount. -*/ - deleted_lpis |=vgic_put_irq_norelease (vcpu->kvm, irq); -continue ; - } - -if (target_vcpu == vcpu) { -/* We're on the right CPU */ -raw_spin_unlock (&irq->irq_lock); +/* +* We skip the processing of an irq in a few cases: +* - the irq is on the correct target: nothing to do. +* - the target is NULL: vgic_predict_target() will have +* culled the irq from the ap_list. +* - we get an error back: the LPI has been deleted. +*/ + irq_target =vgic_predict_target (vcpu->kvm, irq); +if (irq_target == vcpu ||IS_ERR_OR_NULL (irq_target)) { + deleted_lpis |=IS_ERR (irq_target);continue ; }/* -* This interrupt looks like it has to be migrated, -* make sure it is kept alive while locks are dropped. +* Snapshot the vcpus to visit (more may be added later), +* and move the IRQ onto the culling list. */ -vgic_get_irq_ref (irq); - -raw_spin_unlock (&irq->irq_lock); -raw_spin_unlock (&vgic_cpu->ap_list_lock); - -/* -* Ensure locking order by always locking the smallest -* ID first. -*/ -if (vcpu->vcpu_id < target_vcpu->vcpu_id) { - vcpuA = vcpu; - vcpuB = target_vcpu; - }else { - vcpuA = target_vcpu; - vcpuB = vcpu; - } - -raw_spin_lock (&vcpuA->arch.vgic_cpu.ap_list_lock); -raw_spin_lock_nested (&vcpuB->arch.vgic_cpu.ap_list_lock, -SINGLE_DEPTH_NESTING ); -raw_spin_lock (&irq->irq_lock); - -/* -* If the interrupt is still ours and its affinity has -* been preserved, move it around. Otherwise, it means -* things have changed while the interrupt was unlocked -* (it may even have been taken off the list with its -* affinity left untouched), and we need to replay this. -* -* In all cases, we cannot trust the list not to have -* changed, so we restart from the beginning. -*/ -if (irq->vcpu == vcpu && target_vcpu ==vgic_target_oracle (irq)) { -struct vgic_cpu *new_cpu = &target_vcpu->arch.vgic_cpu; - -list_del (&irq->ap_list); - irq->vcpu = target_vcpu; -list_add_tail (&irq->ap_list, &new_cpu->ap_list_head); - target_vcpu_needs_kick = true; - } - -raw_spin_unlock (&irq->irq_lock); -raw_spin_unlock (&vcpuB->arch.vgic_cpu.ap_list_lock); -raw_spin_unlock (&vcpuA->arch.vgic_cpu.ap_list_lock); - - deleted_lpis |=vgic_put_irq_norelease (vcpu->kvm, irq); - -if (target_vcpu_needs_kick) { -kvm_make_request (KVM_REQ_IRQ_PENDING , target_vcpu); -kvm_vcpu_kick (target_vcpu); - } - - goto retry; +__set_bit (irq_target->vcpu_id, pend_map); +list_move_tail (&irq->ap_list, &culling_list); } +raw_spin_unlock (&vgic_cpu->ap_list_lock); + +retry: +BUG_ON (bitmap_empty (pend_map,VGIC_V3_MAX_CPUS ) !=list_empty (&culling_list)); + +for_each_set_bit (i, pend_map,VGIC_V3_MAX_CPUS ) { +struct kvm_vcpu *target =kvm_get_vcpu_by_id (vcpu->kvm, i); + +guard (raw_spinlock)(&target->arch.vgic_cpu.ap_list_lock); + +list_for_each_entry_safe (irq, tmp, &culling_list, ap_list) { +guard (raw_spinlock)(&irq->irq_lock); + +/* +* Check again, now that we hold all the locks. If +* the irq has been moved again, leave it for later. +*/ + irq_target =vgic_predict_target (vcpu->kvm, irq); +if (IS_ERR_OR_NULL (irq_target)) { + deleted_lpis |=IS_ERR (irq_target); +continue ; + } +/* Catch a potential affinity change */ +if (irq_target != target) { +__set_bit (irq_target->vcpu_id, pend_map); +continue ; + } + + irq->vcpu = target; +list_move_tail (&irq->ap_list, &target->arch.vgic_cpu.ap_list_head); +__set_bit (target->vcpu_id, kick_map); + } + +__clear_bit (i, pend_map); + } + +/* Leftovers? Same player shoot again! */ +if (unlikely (!list_empty (&culling_list))) + goto retry; +/* * Fix the last_lr_irq refcount which was obtained while * populating the LRs. This can also result in the LPI being
@@ -879,10 +876,12 @@ static void vgic_prune_ap_list(struct kvm_vcpu *vcpu) *host_data_ptr (last_lr_irq) =NULL ; } -raw_spin_unlock (&vgic_cpu->ap_list_lock); -if (unlikely (deleted_lpis))vgic_release_deleted_lpis (vcpu->kvm); + +for_each_set_bit (i, kick_map,VGIC_V3_MAX_CPUS ) +kvm_make_request_and_kick (KVM_REQ_IRQ_PENDING , +kvm_get_vcpu_by_id (vcpu->kvm, i)); }static void vgic_fold_state (struct kvm_vcpu *vcpu)
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH 4/4] KVM: arm64: vgic: Linearise AP list pruning process
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [High] KVM: arm64: vgic: O(N * V) culling_list scan in vgic_prune_ap_list() - [High] KVM: arm64: vgic…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [High] KVM: arm64: vgic: O(N * V) culling_list scan in vgic_prune_ap_list()
- [High] KVM: arm64: vgic: unbounded retry livelock in vgic_prune_ap_list()
- [Medium] KVM: arm64: vgic: unconditional bitmap scans per exit in vgic_prune_ap_list()
- [Medium] KVM: arm64: vgic: O(N) kvm_get_vcpu_by_id() lookup in vgic_prune_ap_list()
- [Low] KVM: arm64: vgic: missing Fixes trailer for vgic_prune_ap_list() DoS fix
--
commit b2eeef66270a88825d1c1ca4b6594220f852f430
Author: Marc Zyngier <[email protected]>
Quoted 16 lines
KVM: arm64: vgic: Linearise AP list pruning process
The algorithm used to prune IRQs sitting in the AP list has a quadratic complexity, as any concurrent change in the list results in a restart of the processing. The locking is also rather complicated, as we have to take the locks for both source and target vcpus, plus the per-irq lock.
While this has served us well so far, people are finding new and amusing ways to abuse the locking and perform a DoS on the host. Not fun.
Instead, switch to a linear approach where potentially moved IRQs are taken off the AP list and then dispatched onto their new targets. This simplifies the locking (only one vcpu lock at any given time), and only the interrupts that have been changed during that process are evaluated again. This allows the pruning process to converge much quicker.
Quoted 3 lines
Signed-off-by Marc Zyngier <[email protected]>Reviewed-by Oliver Upton <[email protected]>
[Severity: Low]
This fixes a host DoS in vgic_prune_ap_list() and is tagged for stable. Should this include:
Quoted 4 lines
arch/arm64/kvm/vgic/vgic.c +0
-0
[ ... ]
arch/arm64/kvm/vgic/vgic.c int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) · line 792
Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +2
-0@@ -765,109 +788,83 @@ int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner)static void vgic_prune_ap_list (struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu; + DECLARE_BITMAP(pend_map, VGIC_V3_MAX_CPUS) = {}; + DECLARE_BITMAP(kick_map, VGIC_V3_MAX_CPUS) = {};
[Severity: Medium]
Does this add unnecessary work to every VM exit when no IRQs migrate?
kvm_vgic_sync_hwstate() calls vgic_prune_ap_list() on every non-nested GICv2/GICv3 VM exit with IRQs disabled. Zeroing both 512-bit bitmaps on the stack and unconditionally running bitmap_empty() and both for_each_set_bit() loops executes 128 bytes of stack zeroing and three out-of-line 512-bit scans on every exit even when culling_list is empty.
Guarding the bitmap initialization and dispatch/kick loops with !list_empty(&culling_list) would keep this off the common exit path.
[ ... ]
arch/arm64/kvm/vgic/vgic.c int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) · line 834
Quoted 2 lines
arch/arm64/kvm/vgic/vgic.c +2
-0+for_each_set_bit (i, pend_map,VGIC_V3_MAX_CPUS ) { +struct kvm_vcpu *target =kvm_get_vcpu_by_id (vcpu->kvm, i);
[Severity: Medium]
Can kvm_get_vcpu_by_id() fall back to a linear O(N) xarray walk here (and again in the kick_map loop) with IRQs disabled when userspace creates vCPUs with sparse or out-of-order vcpu_ids?
Since pend_map and kick_map are local to vgic_prune_ap_list(), indexing them by dense vcpu->vcpu_idx would allow O(1) kvm_get_vcpu() lookups and bounding the bitmap scans by atomic_read(&vcpu->kvm->online_vcpus) instead of VGIC_V3_MAX_CPUS.
[ ... ]
arch/arm64/kvm/vgic/vgic.c int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) · line 839
Quoted 7 lines
arch/arm64/kvm/vgic/vgic.c +3
-3+guard (raw_spinlock)(&target->arch.vgic_cpu.ap_list_lock); -raw_spin_lock (&irq->irq_lock); -raw_spin_unlock (&vcpuB->arch.vgic_cpu.ap_list_lock); -raw_spin_unlock (&vcpuA->arch.vgic_cpu.ap_list_lock); +list_for_each_entry_safe (irq, tmp, &culling_list, ap_list) { +guard (raw_spinlock)(&irq->irq_lock);
[Severity: High]
Can scanning the entire remaining culling_list for each set bit in pend_map still perform O(N * V) work with hard IRQs disabled and stall remote vCPUs on ap_list_lock?
An unprivileged guest with vITS can queue up to 57,344 pending LPIs (INTERRUPT_ID_BITS_ITS is 16) on one vCPU, then retarget one IRQ to each of vCPUs 0..V-2 and the rest to vCPU V-1 (up to 512 vCPUs). Nearly all N IRQs stay on culling_list across every lower-ID iteration, causing ~29M raw spinlock acquisitions with hard IRQs disabled and risking host hard lockups or RCU stalls while holding each target's ap_list_lock across unrelated IRQs.
Even with M evenly distributed migrating IRQs, this regresses baseline's O(M) migration cost to O(M * V). In addition, the outer loop has no early list_empty(&culling_list) break, so if earlier iterations cull the remaining IRQs it still looks up and locks ap_list_lock for every remaining stale bit in pend_map.
[ ... ]
Quoted 5 lines
arch/arm64/kvm/vgic/vgic.c +5
-0+/* Catch a potential affinity change */ +if (irq_target != target) { +__set_bit (irq_target->vcpu_id, pend_map); +continue ; + }
[ ... ]
arch/arm64/kvm/vgic/vgic.c int kvm_vgic_set_owner(struct kvm_vcpu *vcpu, unsigned int intid, void *owner) · line 866
Quoted 6 lines
arch/arm64/kvm/vgic/vgic.c +4
-0+__clear_bit (i, pend_map); } +/* Leftovers? Same player shoot again! */ +if (unlikely (!list_empty (&culling_list))) + goto retry;
[Severity: High]
Can a multi-vCPU guest livelock the host CPU here indefinitely with hard IRQs disabled?
Because irq->irq_lock is dropped between target passes and vgic_mmio_write_irouter() only takes irq->irq_lock to update irq->target_vcpu, another vCPU can keep flipping an IRQ's target ahead of the current pass:
CPU0 CPU1kvm_vgic_sync_hwstate()
vgic_prune_ap_list()
target = vcpu0 (pend_map: 0)
vgic_mmio_write_irouter()
lock(&irq->irq_lock)
irq->target_vcpu = vcpu1
unlock(&irq->irq_lock)
lock(&irq->irq_lock)
irq_target != vcpu0
__set_bit(1, pend_map)
unlock(&irq->irq_lock)
__clear_bit(0, pend_map)
target = vcpu1 (pend_map: 1)
vgic_mmio_write_irouter()
lock(&irq->irq_lock)
irq->target_vcpu = vcpu0
unlock(&irq->irq_lock)
lock(&irq->irq_lock)
irq_target != vcpu1
__set_bit(0, pend_map)
unlock(&irq->irq_lock)
__clear_bit(1, pend_map)goto retry; <-- livelocks with IRQs disabled
Since kvm_arch_vcpu_ioctl_run() calls kvm_vgic_sync_hwstate() with hard IRQs disabled and vcpu->mode still IN_GUEST_MODE, this blocks host interrupts and cross-CPU IPIs (such as kvm_flush_remote_tlbs()) and can trigger hard lockups or RCU stalls. Bounding the retries and falling back (for example, returning stubborn IRQs to the source vCPU's ap_list) would prevent the livelock.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4