KVM: arm64: vgic: Serialise CTRL_INIT against vCPU creation
PATCH 1/1 KVM: arm64: vgic: Serialise CTRL_INIT against vCPU creation
vgic_init() compares created_vcpus with online_vcpus to detect a vCPU being created. However, CTRL_INIT only holds config_lock, while admission of new vCPUs and updates to created_vcpus are protected by kvm->lock.
A concurrent KVM_CREATE_VCPU can therefore increment created_vcpus after vgic_init() checks the counters, then block on config_lock before publishing the new vCPU. For a GICv4 VM, vgic_v4_init() consequently sizes its vPE array using a stale online_vcpus value, leaving no vPE entry for the new vCPU once its creation completes. The upcoming GICv5 IRS support would hit the same issue when sizing its doorbell domain.
Hold kvm->lock around CTRL_INIT. The existing counter check detects a vCPU creation already in progress, while the lock prevents a new one from starting until initialisation has completed.
--- arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/kvm/vgic/vgic-kvm-device.c +2 -0
@@ -255,9 +255,11 @@ static int vgic_set_common_attr(struct kvm_device *dev,case KVM_DEV_ARM_VGIC_GRP_CTRL : {switch (attr->attr) {case KVM_DEV_ARM_VGIC_CTRL_INIT : +mutex_lock (&dev->kvm->lock);mutex_lock (&dev->kvm->arch.config_lock); r =vgic_init (dev->kvm);mutex_unlock (&dev->kvm->arch.config_lock); +mutex_unlock (&dev->kvm->lock);return r;case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES :/*
Signature · 1 line
2.34.1
Hi Sascha,
Quoted 13 lines · Sascha Bischoff
vgic_init()comparescreated_vcpuswithonline_vcpusto detect a vCPU being created. However,CTRL_INITonly holdsconfig_lock, while admission of new vCPUs and updates tocreated_vcpusare protected bykvm->lock.A concurrent
KVM_CREATE_VCPUcan therefore incrementcreated_vcpusaftervgic_init()checks the counters, then block onconfig_lockbefore publishing the new vCPU. For a GICv4 VM,vgic_v4_init()consequently sizes its vPE array using a staleonline_vcpusvalue, leaving no vPE entry for the new vCPU once its creation completes. The upcoming GICv5 IRS support would hit the same issue when sizing its doorbell domain.
Quoted 3 lines
Hold
kvm->lockaroundCTRL_INIT. The existing counter check detects a vCPU creation already in progress, while the lock prevents a new one from starting until initialisation has completed.
This also brings CTRL_INIT in line with the other vgic device flows that serialise against vCPU creation: kvm_vgic_create(), the GICv2 register uaccess path, and SAVE_PENDING_TABLES just below it all hold kvm->lock across the config_lock section.
Quoted 4 lines
Fixes f00327731131 ("KVM: arm64: Use config_lock to protect vgic state")Reported-by Sashiko <[email protected]>Signed-off-by Sascha Bischoff <[email protected]>
Given that f00327731131 carried a stable tag, worth a Cc: stable?
Cheers,
/fuad
Quoted 22 lines
--- arch/arm64/kvm/vgic/vgic-kvm-device.c | 2 ++ 1 file changed, 2 insertions(+)arch/arm64/kvm/vgic/vgic-kvm-device.c +2
-1@@ -255,9 +255,11 @@ static int vgic_set_common_attr(struct kvm_device *dev,case KVM_DEV_ARM_VGIC_GRP_CTRL : {switch (attr->attr) {case KVM_DEV_ARM_VGIC_CTRL_INIT : +mutex_lock (&dev->kvm->lock);mutex_lock (&dev->kvm->arch.config_lock); r =vgic_init (dev->kvm);mutex_unlock (&dev->kvm->arch.config_lock); +mutex_unlock (&dev->kvm->lock);return r;case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES :/* -- 2.34.1
Quoted 5 lines · Sascha Bischoff
vgic_init()comparescreated_vcpuswithonline_vcpusto detect a vCPU being created. However,CTRL_INITonly holdsconfig_lock, while admission of new vCPUs and updates tocreated_vcpusare protected bykvm->lock.
Quoted 8 lines
A concurrent
KVM_CREATE_VCPUcan therefore incrementcreated_vcpusaftervgic_init()checks the counters, then block onconfig_lockbefore publishing the new vCPU. For a GICv4 VM,vgic_v4_init()consequently sizes its vPE array using a staleonline_vcpusvalue, leaving no vPE entry for the new vCPU once its creation completes. The upcoming GICv5 IRS support would hit the same issue when sizing its doorbell domain.[...]
Applied to fixes, thanks!
[1/1] KVM: arm64: vgic: Serialise CTRL_INIT against vCPU creation
commit: 5f54835d94db2e896ec27415b9de2e81cb56009a
Cheers,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.