KVM: arm64: Ensure VGIC is fully initialized before entering guest
cover KVM: arm64: Ensure VGIC is fully initialized before entering guest
Fix for the race [*] that I had mentioned last week. The first patch isn't entirely necessary, but it gets rid of a bogus check that could've potentially raced with kvm_vgic_map_resources() as vgic_dist::ready is no longer protected with the config_lock.
Applies to kvmarm/fixes.
Oliver Upton (2):
KVM: arm64: vgic: Don't check for vgic_ready() when setting NR_IRQS
KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration
arch/arm64/kvm/vgic/vgic-init.c | 13 +++++++++++-- arch/arm64/kvm/vgic/vgic-kvm-device.c | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-)
base-commit: df5fd75ee305cb5927e0b1a0b46cc988ad8db2b1
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
PATCH 1/2 KVM: arm64: vgic: Don't check for vgic_ready() when setting NR_IRQS
KVM commits to a particular sizing of SPIs when the vgic is initialized, which is before the point a vgic becomes ready. On top of that, KVM supplies a default amount of SPIs should userspace not explicitly configure this.
As such, the check for vgic_ready() in the handling of KVM_DEV_ARM_VGIC_GRP_NR_IRQS is completely wrong, and testing if nr_spis is nonzero is sufficient for preventing userspace from playing games with us.
--- arch/arm64/kvm/vgic/vgic-kvm-device.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/vgic/vgic-kvm-device.c +6 -1
@@ -236,7 +236,12 @@ static int vgic_set_common_attr(struct kvm_device *dev,mutex_lock (&dev->kvm->arch.config_lock); -if (vgic_ready (dev->kvm) || dev->kvm->arch.vgic.nr_spis) +/* +* Either userspace has already configured NR_IRQS or +* the vgic has already been initialized and vgic_init() +* supplied a default amount of SPIs. +*/ +if (dev->kvm->arch.vgic.nr_spis) ret = -EBUSY ;else dev->kvm->arch.vgic.nr_spis =
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
PATCH 2/2 KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration
In reply to Oliver Uptonkvm_vgic_map_resources() prematurely marks the distributor as 'ready', potentially allowing vCPUs to enter the guest before the distributor's MMIO registration has been made visible.
Plug the race by marking the distributor as ready only after MMIO registration is completed. Rely on the implied ordering of synchronize_srcu() to ensure the MMIO registration is visible before vgic_dist::ready. This also means that writers to vgic_dist::ready are now serialized by the slots_lock, which was effectively the case already as all writers held the slots_lock in addition to the config_lock.
--- arch/arm64/kvm/vgic/vgic-init.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +11 -2
@@ -544,14 +544,23 @@ int kvm_vgic_map_resources(struct kvm *kvm)if (ret) goto out; - dist->ready = true; dist_base = dist->vgic_dist_base;mutex_unlock (&kvm->arch.config_lock); ret =vgic_register_dist_iodev (kvm, dist_base, type); -if (ret) +if (ret) {kvm_err ("Unable to register VGIC dist MMIO regions\n" ); + goto out_slots; + } +/* +* kvm_io_bus_register_dev() guarantees all readers see the new MMIO +* registration before returning through synchronize_srcu(), which also +* implies a full memory barrier. As such, marking the distributor as +* 'ready' here is guaranteed to be ordered after all vCPUs having seen +* a completely configured distributor. +*/ + dist->ready = true; goto out_slots; out:mutex_unlock (&kvm->arch.config_lock);
Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
Quoted 6 lines · Oliver Upton
Fix for the race [*] that I had mentioned last week. The first patch isn't entirely necessary, but it gets rid of a bogus check that could've potentially raced with
kvm_vgic_map_resources()asvgic_dist::readyis no longer protected with theconfig_lock.Applies to kvmarm/fixes.
-ENEEDBEER
Here's the link I meant to add.
[*]: https://lore.kernel.org/kvmarm/[email protected]/
Quoted 13 lines
Oliver Upton (2):
KVM: arm64: vgic: Don't check forvgic_ready()when settingNR_IRQS
KVM: arm64: Ensurevgic_ready()is ordered against MMIO registrationarch/arm64/kvm/vgic/vgic-init.c | 13 +++++++++++-- arch/arm64/kvm/vgic/vgic-kvm-device.c | 7 ++++++- 2 files changed, 17 insertions(+), 3 deletions(-)base-commit:
df5fd75ee305cb5927e0b1a0b46cc988ad8db2b1Signature · 1 line
2.47.0.rc1.288.g06298d1525-goog
Signature · 2 lines
Thanks, Oliver
Marc Zyngier
maintainerRe: [PATCH 2/2] KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration
In reply to Oliver UptonQuoted 38 lines · Oliver Upton
kvm_vgic_map_resources()prematurely marks the distributor as 'ready', potentially allowing vCPUs to enter the guest before the distributor's MMIO registration has been made visible.Plug the race by marking the distributor as ready only after MMIO registration is completed. Rely on the implied ordering of
synchronize_srcu()to ensure the MMIO registration is visible beforevgic_dist::ready. This also means that writers tovgic_dist::readyare now serialized by theslots_lock, which was effectively the case already as all writers held theslots_lockin addition to theconfig_lock.Fixes 59112e9c390b ("KVM: arm64: vgic: Fix a circular locking issue")Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/vgic/vgic-init.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)arch/arm64/kvm/vgic/vgic-init.c +4
-2@@ -544,14 +544,23 @@ int kvm_vgic_map_resources(struct kvm *kvm)if (ret) goto out; - dist->ready = true; dist_base = dist->vgic_dist_base;mutex_unlock (&kvm->arch.config_lock); ret =vgic_register_dist_iodev (kvm, dist_base, type); -if (ret) +if (ret) {kvm_err ("Unable to register VGIC dist MMIO regions\n" ); + goto out_slots; + } +/*
arch/arm64/kvm/vgic/vgic-init.c int kvm_vgic_map_resources(struct kvm *kvm) · line 566
Quoted 10 lines
arch/arm64/kvm/vgic/vgic-init.c +7
-0+* kvm_io_bus_register_dev() guarantees all readers see the new MMIO +* registration before returning through synchronize_srcu(), which also +* implies a full memory barrier. As such, marking the distributor as +* 'ready' here is guaranteed to be ordered after all vCPUs having seen +* a completely configured distributor. +*/ + dist->ready = true; goto out_slots; out:mutex_unlock (&kvm->arch.config_lock);
I thought we would benefit from the removal of the early-out at the beginning of the function, specially given that this is only called on the first run of any vcpu -- and we can probably afford to take the locks on such a rare event. My impression is that we'd gain in robustness and maintainability.
This can come as a later patch if we agree that this is valuable.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 8 lines · Oliver Upton
Fix for the race [*] that I had mentioned last week. The first patch isn't entirely necessary, but it gets rid of a bogus check that could've potentially raced with
kvm_vgic_map_resources()asvgic_dist::readyis no longer protected with theconfig_lock.Applies to kvmarm/fixes.
[...]
Applied to fixes, thanks!
[1/2] KVM: arm64: vgic: Don't check for vgic_ready() when setting NR_IRQS
commit: 5978d4ec7e82ffc472ac2645601dd10b09e61b0f
[2/2] KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration
commit: 78a00555550042ed77b33ace7423aced228b3b4eCheers,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH 2/2] KVM: arm64: Ensure vgic_ready() is ordered against MMIO registration
In reply to Marc ZyngierQuoted 8 lines · Marc Zyngier
On Thu, 17 Oct 2024 01:19:47 +0100, Oliver Upton <[email protected]> wrote:
I thought we would benefit from the removal of the early-out at the beginning of the function, specially given that this is only called on the first run of any vcpu -- and we can probably afford to take the locks on such a rare event. My impression is that we'd gain in robustness and maintainability.This can come as a later patch if we agree that this is valuable.
I should've mentioned my rationale a bit more here, thanks for queuing the patch anyway. Prolonged blackout for live migration is a problem for larger VMs, and I was too chicken to have all vCPUs pile up on slots_lock and config_lock.
Too lazy to prove this is/isn't an issue, so I figured I'd preserve the existing flow as much as possible. If we get this wrong again (which wouldn't be surprising) then let's drop the early out and just take the locks.
Seem reasonable?
Signature · 2 lines
Thanks, Oliver