public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 0/2] KVM: x86: Strengthen locking rules for kvm_lock
@ 2025-01-24 19:11 Paolo Bonzini
  2025-01-24 19:11 ` [PATCH 1/2] KVM: x86: fix usage of kvm_lock in set_nx_huge_pages() Paolo Bonzini
  2025-01-24 19:11 ` [PATCH 2/2] Documentation: explain issues with taking locks inside kvm_lock Paolo Bonzini
  0 siblings, 2 replies; 12+ messages in thread
From: Paolo Bonzini @ 2025-01-24 19:11 UTC (permalink / raw)
  To: linux-kernel, kvm; +Cc: seanjc

Last June, Sean reported a possible deadlock scenario due to
cpu_hotplug_lock() being taken unknowingly with other KVM locks
taken.

While he solved his immediate needs by inroducing kvm_usage_lock,
the same possibility could exist elsewhere.  The simplest sequence that
I could concoct requires four CPUs and no constant TSC, so this is really
theoretical, but since the fix is easy and can be documented, let's do it.

At the time the suggested solution was to use RCU for vm_list, but that's
not even necessary; it's enough to just keep the critical sections small,
avoiding _any_ mutex_lock while holding kvm_lock.  This is not hard to do
because you can always drop kvm_lock in the middle of the vm_list walk if
you first take a reference to the current struct kvm with kvm_get_kvm();
and anyway most walks of vm_list are already relatively small and only
take spinlocks.

The only case in which concurrent readers could be useful is really
accessing statistics *from debugfs*, but then even an rwsem would do.

RFC because it's compile-tested only.

Paolo

Paolo Bonzini (2):
  KVM: x86: fix usage of kvm_lock in set_nx_huge_pages()
  Documentation: explain issues with taking locks inside kvm_lock

 Documentation/virt/kvm/locking.rst | 27 ++++++++++++++++++++-------
 arch/x86/kvm/mmu/mmu.c             | 13 +++++++------
 2 files changed, 27 insertions(+), 13 deletions(-)

-- 
2.43.5


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2025-01-27 18:17 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-24 19:11 [RFC PATCH 0/2] KVM: x86: Strengthen locking rules for kvm_lock Paolo Bonzini
2025-01-24 19:11 ` [PATCH 1/2] KVM: x86: fix usage of kvm_lock in set_nx_huge_pages() Paolo Bonzini
2025-01-24 20:11   ` Sean Christopherson
2025-01-24 22:19     ` Paolo Bonzini
2025-01-24 23:44       ` Sean Christopherson
2025-01-25  0:08         ` Paolo Bonzini
2025-01-25  0:44           ` Sean Christopherson
2025-01-27 17:27             ` Paolo Bonzini
2025-01-27 17:56               ` Paolo Bonzini
2025-01-27 18:01               ` Sean Christopherson
2025-01-27 18:17                 ` Paolo Bonzini
2025-01-24 19:11 ` [PATCH 2/2] Documentation: explain issues with taking locks inside kvm_lock Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox