KVM: hyperv: split lock to protect struct kvm_hv
Subsystems (5)
Paolo Bonzini
maintainerPATCH 1/1 KVM: hyperv: split lock to protect struct kvm_hv
Otherwise, there is an AB-BA deadlock between kvm->lock and vcpu->mutex.
---
Compile-tested only.
Documentation/virtual/kvm/locking.txt | 2 ++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/hyperv.c | 10 +++++----- arch/x86/kvm/x86.c | 1 + 4 files changed, 9 insertions(+), 5 deletions(-)
Documentation/virtual/kvm/locking.txt +2 -0
@@ -16,6 +16,8 @@ The acquisition orders for mutexes are as follows: For spinlocks, kvm_lock is taken outside kvm->mmu_lock. Everything else is a leaf: no other lock is taken inside the critical sections. +In particular, on x86, vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock. + 2: Exception ------------
arch/x86/include/asm/kvm_host.h +1 -0
@@ -704,6 +704,7 @@ struct kvm_apic_map {/* Hyper-V emulation context */ struct kvm_hv { +struct mutex hv_lock;u64 hv_guest_os_id;u64 hv_hypercall;u64 hv_tsc_page;
arch/x86/kvm/hyperv.c +5 -5
@@ -1142,9 +1142,9 @@ int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)if (kvm_hv_msr_partition_wide (msr)) {int r; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.hyperv.hv_lock); r =kvm_hv_set_msr_pw (vcpu, msr, data, host); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.hyperv.hv_lock);return r; }else return kvm_hv_set_msr (vcpu, msr, data, host);
@@ -1155,9 +1155,9 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)if (kvm_hv_msr_partition_wide (msr)) {int r; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.hyperv.hv_lock); r =kvm_hv_get_msr_pw (vcpu, msr, pdata); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.hyperv.hv_lock);return r; }else return kvm_hv_get_msr (vcpu, msr, pdata);
@@ -1165,7 +1165,7 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)bool kvm_hv_hypercall_enabled (struct kvm *kvm) { -return kvm->arch.hyperv.hv_hypercall &HV_X64_MSR_HYPERCALL_ENABLE ; +return READ_ONCE (kvm->arch.hyperv.hv_hypercall) &HV_X64_MSR_HYPERCALL_ENABLE ; }static void kvm_hv_hypercall_set_result (struct kvm_vcpu *vcpu,u64 result)
arch/x86/kvm/x86.c +1 -0
@@ -7861,6 +7861,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)raw_spin_lock_init (&kvm->arch.tsc_write_lock);mutex_init (&kvm->arch.apic_map_lock); +mutex_init (&kvm->arch.hyperv.hv_lock);spin_lock_init (&kvm->arch.pvclock_gtod_sync_lock); kvm->arch.kvmclock_offset = -ktime_get_boot_ns ();
Signature · 1 line
2.9.3
Paolo Bonzini
maintainer----- Original Message -----
Quoted 82 lines
From: "Roman Kagan" <[email protected]>
To: "Paolo Bonzini" <[email protected]>
Cc: [email protected], [email protected], [email protected]
Sent: Monday, December 12, 2016 9:13:57 AM
Subject: Re: [PATCH] KVM: hyperv: split lock to protect structkvm_hvQuoted 75 lines · Paolo Bonzini
Otherwise, there is an AB-BA deadlock between
kvm->lockandvcpu->mutex.Reported-by Dmitry Vyukov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
Compile-tested only.Documentation/virtual/kvm/locking.txt | 2 ++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/hyperv.c | 10 +++++----- arch/x86/kvm/x86.c | 1 + 4 files changed, 9 insertions(+), 5 deletions(-)Documentation/virtual/kvm/locking.txt +1
-0@@ -16,6 +16,8 @@ The acquisition orders for mutexes are as follows: For spinlocks, kvm_lock is taken outside kvm->mmu_lock. Everything else is a leaf: no other lock is taken inside the critical sections. +In particular, on x86, vcpu->mutex is taken outside
kvm->arch.hyperv.hv_lock.Documentation/virtual/kvm/locking.txt +1
-0+ 2: Exception ------------arch/x86/include/asm/kvm_host.h +1
-0@@ -704,6 +704,7 @@ struct kvm_apic_map {/* Hyper-V emulation context */ struct kvm_hv { +struct mutex hv_lock;u64 hv_guest_os_id;u64 hv_hypercall;u64 hv_tsc_page;arch/x86/kvm/hyperv.c +2
-2@@ -1142,9 +1142,9 @@ int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)if (kvm_hv_msr_partition_wide (msr)) {int r; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.hyperv.hv_lock); r =kvm_hv_set_msr_pw (vcpu, msr, data, host); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.hyperv.hv_lock);return r; }else arch/x86/kvm/hyperv.c +2
-2return kvm_hv_set_msr (vcpu, msr, data, host);@@ -1155,9 +1155,9 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)if (kvm_hv_msr_partition_wide (msr)) {int r; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.hyperv.hv_lock); r =kvm_hv_get_msr_pw (vcpu, msr, pdata); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.hyperv.hv_lock);return r; }else arch/x86/kvm/hyperv.c +0
-0return kvm_hv_get_msr (vcpu, msr, pdata);@@ -1165,7 +1165,7 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)
Quoted 12 lines
arch/x86/kvm/hyperv.c int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 1169
Quoted 7 lines
arch/x86/kvm/hyperv.c +1
-1bool kvm_hv_hypercall_enabled (struct kvm *kvm) { -return kvm->arch.hyperv.hv_hypercall &HV_X64_MSR_HYPERCALL_ENABLE ; +return READ_ONCE (kvm->arch.hyperv.hv_hypercall) &HV_X64_MSR_HYPERCALL_ENABLE ; }I'm afraid we have a problem with ->hv_tsc_page which can't be solved with a similar
READ_ONCE()inkvm_hv_setup_tsc_page(). I need to double-check if taking a mutex is ok there; if not we may have to do srcu...
Yes, it can take a mutex.
Paolo
Quoted 68 lines · Paolo Bonzini
Otherwise, there is an AB-BA deadlock between
kvm->lockandvcpu->mutex.Reported-by Dmitry Vyukov <[email protected]>Signed-off-by Paolo Bonzini <[email protected]>---
Compile-tested only.Documentation/virtual/kvm/locking.txt | 2 ++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/hyperv.c | 10 +++++----- arch/x86/kvm/x86.c | 1 + 4 files changed, 9 insertions(+), 5 deletions(-)Documentation/virtual/kvm/locking.txt +2
-0@@ -16,6 +16,8 @@ The acquisition orders for mutexes are as follows: For spinlocks, kvm_lock is taken outside kvm->mmu_lock. Everything else is a leaf: no other lock is taken inside the critical sections. +In particular, on x86, vcpu->mutex is taken outside kvm->arch.hyperv.hv_lock. + 2: Exception ------------arch/x86/include/asm/kvm_host.h +1
-0@@ -704,6 +704,7 @@ struct kvm_apic_map {/* Hyper-V emulation context */ struct kvm_hv { +struct mutex hv_lock;u64 hv_guest_os_id;u64 hv_hypercall;u64 hv_tsc_page;arch/x86/kvm/hyperv.c +4
-4@@ -1142,9 +1142,9 @@ int kvm_hv_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data, bool host)if (kvm_hv_msr_partition_wide (msr)) {int r; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.hyperv.hv_lock); r =kvm_hv_set_msr_pw (vcpu, msr, data, host); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.hyperv.hv_lock);return r; }else return kvm_hv_set_msr (vcpu, msr, data, host);@@ -1155,9 +1155,9 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)if (kvm_hv_msr_partition_wide (msr)) {int r; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.hyperv.hv_lock); r =kvm_hv_get_msr_pw (vcpu, msr, pdata); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.hyperv.hv_lock);return r; }else return kvm_hv_get_msr (vcpu, msr, pdata);
arch/x86/kvm/hyperv.c int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata) · line 1170
Quoted 7 lines
arch/x86/kvm/hyperv.c +1
-1@@ -1165,7 +1165,7 @@ int kvm_hv_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata)bool kvm_hv_hypercall_enabled (struct kvm *kvm) { -return kvm->arch.hyperv.hv_hypercall &HV_X64_MSR_HYPERCALL_ENABLE ; +return READ_ONCE (kvm->arch.hyperv.hv_hypercall) &HV_X64_MSR_HYPERCALL_ENABLE ; }
I'm afraid we have a problem with ->hv_tsc_page which can't be solved with a similar READ_ONCE() in kvm_hv_setup_tsc_page(). I need to double-check if taking a mutex is ok there; if not we may have to do srcu...
Roman.