KVM: PPC: Book3S HV: Don't take kvm->lock around kvm_for_each_vcpu
authorPaul Mackerras <paulus@ozlabs.org>
Thu, 23 May 2019 06:36:32 +0000 (16:36 +1000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 22 Jun 2019 06:16:18 +0000 (08:16 +0200)
commit5390e7fa84b9024efa9d9780ee891363132d0e8c
tree34306e81e677bb53b0ecc78ac09546c49de581d2
parent308a245aeed96dcbb80582875f5e2f5b600a0df5
KVM: PPC: Book3S HV: Don't take kvm->lock around kvm_for_each_vcpu

[ Upstream commit 5a3f49364c3ffa1107bd88f8292406e98c5d206c ]

Currently the HV KVM code takes the kvm->lock around calls to
kvm_for_each_vcpu() and kvm_get_vcpu_by_id() (which can call
kvm_for_each_vcpu() internally).  However, that leads to a lock
order inversion problem, because these are called in contexts where
the vcpu mutex is held, but the vcpu mutexes nest within kvm->lock
according to Documentation/virtual/kvm/locking.txt.  Hence there
is a possibility of deadlock.

To fix this, we simply don't take the kvm->lock mutex around these
calls.  This is safe because the implementations of kvm_for_each_vcpu()
and kvm_get_vcpu_by_id() have been designed to be able to be called
locklessly.

Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
Reviewed-by: Cédric Le Goater <clg@kaod.org>
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/powerpc/kvm/book3s_hv.c