KVM: arm64: vgic fixes for 6.7
Subsystems (4)
Marc Zyngier
maintainercover KVM: arm64: vgic fixes for 6.7
It appears that under some cirumstances, the lifetime of a vcpu doesn't correctly align with that of the structure describing the redistributor associated with that vcpu. That's not great.
Fixing it is, unfortunately, not as trivial as it appears as the required locking gets in the way.
The first two patches in this series amend that locking so that the third patch, which is the actual fix, becomes almost trivial. The last two patches are more cosmetic and only add assertions that helped me debugging the whole thing.
I've earmarked the first 3 patches as stable candidates, and would love to see them in 6.7. Patches on top of -rc4.
Marc Zyngier (5):
KVM: arm64: vgic: Simplify kvm_vgic_destroy()
KVM: arm64: vgic: Add a non-locking primitive for
kvm_vgic_vcpu_destroy()
KVM: arm64: vgic: Force vcpu vgic teardown on vcpu destroy
KVM: arm64: vgic: Ensure that slots_lock is held in
vgic_register_all_redist_iodevs()
KVM: Convert comment into an assertion in kvm_io_bus_register_dev()
arch/arm64/kvm/arm.c | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 47 ++++++++++++++++++------------ arch/arm64/kvm/vgic/vgic-mmio-v3.c | 4 ++- arch/arm64/kvm/vgic/vgic.h | 1 + virt/kvm/kvm_main.c | 3 +- 5 files changed, 36 insertions(+), 21 deletions(-)
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 1/5 KVM: arm64: vgic: Simplify kvm_vgic_destroy()
When destroying a vgic, we have rather cumbersome rules about when slots_lock and config_lock are held, resulting in fun buglets.
The first port of call is to simplify kvm_vgic_map_resources() so that there is only one call to kvm_vgic_destroy() instead of two, with the second only holding half of the locks.
For that, we kill the non-locking primitive and move the call outside of the locking altogether. This doesn't change anything (we re-acquire the locks and teardown the whole vgic), and simplifies the code significantly.
--- arch/arm64/kvm/vgic/vgic-init.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +14 -15
@@ -382,26 +382,24 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) vgic_cpu->rd_iodev.base_addr =VGIC_ADDR_UNDEF ; } -static void __kvm_vgic_destroy (struct kvm *kvm) +void kvm_vgic_destroy (struct kvm *kvm) {struct kvm_vcpu *vcpu;unsigned long i; -lockdep_assert_held (&kvm->arch.config_lock); +mutex_lock (&kvm->slots_lock);vgic_debug_destroy (kvm);kvm_for_each_vcpu (i, vcpu, kvm)kvm_vgic_vcpu_destroy (vcpu); +mutex_lock (&kvm->arch.config_lock); +kvm_vgic_dist_destroy (kvm); -} -void kvm_vgic_destroy (struct kvm *kvm) -{ -mutex_lock (&kvm->arch.config_lock); -__kvm_vgic_destroy (kvm);mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->slots_lock); }/**
@@ -469,25 +467,26 @@ int kvm_vgic_map_resources(struct kvm *kvm) type =VGIC_V3 ; } -if (ret) { -__kvm_vgic_destroy (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" ); -kvm_vgic_destroy (kvm); - } -mutex_unlock (&kvm->slots_lock); -return ret; + goto out_slots; out:mutex_unlock (&kvm->arch.config_lock); +out_slots:mutex_unlock (&kvm->slots_lock); + +if (ret) +kvm_vgic_destroy (kvm); +return ret; }
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 2/5 KVM: arm64: vgic: Add a non-locking primitive for kvm_vgic_vcpu_destroy()
In reply to Marc ZyngierAs we are going to need to call into kvm_vgic_vcpu_destroy() without prior holding of the slots_lock, introduce __kvm_vgic_vcpu_destroy() as a non-locking primitive of kvm_vgic_vcpu_destroy().
--- arch/arm64/kvm/vgic/vgic-init.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-init.c +11 -2
@@ -368,7 +368,7 @@ static void kvm_vgic_dist_destroy(struct kvm *kvm)vgic_v4_teardown (kvm); } -void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu) +static void __kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
@@ -382,6 +382,15 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) vgic_cpu->rd_iodev.base_addr =VGIC_ADDR_UNDEF ; } +void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu) +{ +struct kvm *kvm = vcpu->kvm; + +mutex_lock (&kvm->slots_lock); +__kvm_vgic_vcpu_destroy (vcpu); +mutex_unlock (&kvm->slots_lock); +} +void kvm_vgic_destroy (struct kvm *kvm) {struct kvm_vcpu *vcpu;
@@ -392,7 +401,7 @@ void kvm_vgic_destroy(struct kvm *kvm)vgic_debug_destroy (kvm);kvm_for_each_vcpu (i, vcpu, kvm) -kvm_vgic_vcpu_destroy (vcpu); +__kvm_vgic_vcpu_destroy (vcpu);mutex_lock (&kvm->arch.config_lock);
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 3/5 KVM: arm64: vgic: Force vcpu vgic teardown on vcpu destroy
In reply to Marc ZyngierWhen failing to create a vcpu because (for example) it has a duplicate vcpu_id, we destroy the vcpu. Amusingly, this leaves the redistributor registered with the KVM_MMIO bus.
This is no good, and we should properly clean the mess. Force a teardown of the vgic vcpu interface, including the RD device before returning to the caller.
--- arch/arm64/kvm/arm.c | 2 +- arch/arm64/kvm/vgic/vgic-init.c | 5 ++++- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 2 +- arch/arm64/kvm/vgic/vgic.h | 1 + 4 files changed, 7 insertions(+), 3 deletions(-)
arch/arm64/kvm/arm.c +1 -1
@@ -410,7 +410,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)kvm_mmu_free_memory_cache (&vcpu->arch.mmu_page_cache);kvm_timer_vcpu_terminate (vcpu);kvm_pmu_vcpu_destroy (vcpu); - +kvm_vgic_vcpu_destroy (vcpu);kvm_arm_vcpu_destroy (vcpu); }
arch/arm64/kvm/vgic/vgic-init.c +4 -1
@@ -379,7 +379,10 @@ static void __kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu)vgic_flush_pending_lpis (vcpu);INIT_LIST_HEAD (&vgic_cpu->ap_list_head); - vgic_cpu->rd_iodev.base_addr =VGIC_ADDR_UNDEF ; +if (vcpu->kvm->arch.vgic.vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) { +vgic_unregister_redist_iodev (vcpu); + vgic_cpu->rd_iodev.base_addr =VGIC_ADDR_UNDEF ; + } }void kvm_vgic_vcpu_destroy (struct kvm_vcpu *vcpu)
arch/arm64/kvm/vgic/vgic-mmio-v3.c +1 -1
@@ -820,7 +820,7 @@ int vgic_register_redist_iodev(struct kvm_vcpu *vcpu)return ret; } -static void vgic_unregister_redist_iodev (struct kvm_vcpu *vcpu) +void vgic_unregister_redist_iodev (struct kvm_vcpu *vcpu) {struct vgic_io_device *rd_dev = &vcpu->arch.vgic_cpu.rd_iodev;
arch/arm64/kvm/vgic/vgic.h +1 -0
@@ -241,6 +241,7 @@ 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); +void vgic_unregister_redist_iodev (struct kvm_vcpu *vcpu);bool vgic_v3_check_base (struct kvm *kvm);void vgic_v3_load (struct kvm_vcpu *vcpu);
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 4/5 KVM: arm64: vgic: Ensure that slots_lock is held in vgic_register_all_redist_iodevs()
In reply to Marc ZyngierAlthough we implicitly depend on slots_lock being held when registering IO devices with the IO bus infrastructure, we don't enforce this requirement. Make it explicit.
--- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 2 ++ 1 file changed, 2 insertions(+)
arch/arm64/kvm/vgic/vgic-mmio-v3.c +2 -0
@@ -833,6 +833,8 @@ static int vgic_register_all_redist_iodevs(struct kvm *kvm)unsigned long c;int ret =0 ; +lockdep_assert_held (&kvm->slots_lock); +kvm_for_each_vcpu (c, vcpu, kvm) { ret =vgic_register_redist_iodev (vcpu);if (ret)
Signature · 1 line
2.39.2
Marc Zyngier
maintainerPATCH 5/5 KVM: Convert comment into an assertion in kvm_io_bus_register_dev()
In reply to Marc ZyngierInstead of having a comment indicating the need to hold slots_lock when calling kvm_io_bus_register_dev(), make it explicit with a lockdep assertion.
--- virt/kvm/kvm_main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
virt/kvm/kvm_main.c +2 -1
@@ -5545,7 +5545,6 @@ int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,return r <0 ? r :0 ; } -/* Caller must hold slots_lock. */ int kvm_io_bus_register_dev (struct kvm *kvm,enum kvm_bus bus_idx,gpa_t addr,int len,struct kvm_io_device *dev) {
@@ -5553,6 +5552,8 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,struct kvm_io_bus *new_bus, *bus;struct kvm_io_range range; +lockdep_assert_held (&kvm->slots_lock); + bus =kvm_get_bus (kvm, bus_idx);if (!bus)return -ENOMEM ;
Signature · 1 line
2.39.2
Oliver Upton
AppliedQuoted 8 lines · Marc Zyngier
It appears that under some cirumstances, the lifetime of a vcpu doesn't correctly align with that of the structure describing the redistributor associated with that vcpu. That's not great.
Fixing it is, unfortunately, not as trivial as it appears as the required locking gets in the way.
[...]
Applied to kvmarm/fixes, thanks!
[1/5] KVM: arm64: vgic: Simplify kvm_vgic_destroy()
https://git.kernel.org/kvmarm/kvmarm/c/01ad29d224ff [2/5] KVM: arm64: vgic: Add a non-locking primitive for kvm_vgic_vcpu_destroy() https://git.kernel.org/kvmarm/kvmarm/c/d26b9cb33c2d [3/5] KVM: arm64: vgic: Force vcpu vgic teardown on vcpu destroy https://git.kernel.org/kvmarm/kvmarm/c/02e3858f08fa [4/5] KVM: arm64: vgic: Ensure that slots_lock is held in vgic_register_all_redist_iodevs() https://git.kernel.org/kvmarm/kvmarm/c/6bef365e310a [5/5] KVM: Convert comment into an assertion in kvm_io_bus_register_dev() https://git.kernel.org/kvmarm/kvmarm/c/b1a39a718db4
--
Best,
Oliver