@@ -9,7 +9,7 @@ KVM Lock Overview
99
1010The acquisition orders for mutexes are as follows:
1111
12- - cpus_read_lock() is taken outside kvm_lock
12+ - cpus_read_lock() is taken outside kvm_lock and kvm_usage_lock
1313
1414- kvm->lock is taken outside vcpu->mutex
1515
@@ -24,6 +24,13 @@ The acquisition orders for mutexes are as follows:
2424 are taken on the waiting side when modifying memslots, so MMU notifiers
2525 must not take either kvm->slots_lock or kvm->slots_arch_lock.
2626
27+ cpus_read_lock() vs kvm_lock:
28+
29+ - Taking cpus_read_lock() outside of kvm_lock is problematic, despite that
30+ being the official ordering, as it is quite easy to unknowingly trigger
31+ cpus_read_lock() while holding kvm_lock. Use caution when walking vm_list,
32+ e.g. avoid complex operations when possible.
33+
2734For SRCU:
2835
2936- ``synchronize_srcu(&kvm->srcu) `` is called inside critical sections
@@ -227,10 +234,17 @@ time it will be set using the Dirty tracking mechanism described above.
227234:Type: mutex
228235:Arch: any
229236:Protects: - vm_list
230- - kvm_usage_count
237+
238+ ``kvm_usage_lock ``
239+ ^^^^^^^^^^^^^^^^^^
240+
241+ :Type: mutex
242+ :Arch: any
243+ :Protects: - kvm_usage_count
231244 - hardware virtualization enable/disable
232- :Comment: KVM also disables CPU hotplug via cpus_read_lock() during
233- enable/disable.
245+ :Comment: Exists because using kvm_lock leads to deadlock (see earlier comment
246+ on cpus_read_lock() vs kvm_lock). Note, KVM also disables CPU hotplug via
247+ cpus_read_lock() when enabling/disabling virtualization.
234248
235249``kvm->mn_invalidate_lock ``
236250^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -290,11 +304,12 @@ time it will be set using the Dirty tracking mechanism described above.
290304 wakeup.
291305
292306``vendor_module_lock ``
293- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
307+ ^^^^^^^^^^^^^^^^^^^^^^
294308:Type: mutex
295309:Arch: x86
296310:Protects: loading a vendor module (kvm_amd or kvm_intel)
297- :Comment: Exists because using kvm_lock leads to deadlock. cpu_hotplug_lock is
298- taken outside of kvm_lock, e.g. in KVM's CPU online/offline callbacks, and
299- many operations need to take cpu_hotplug_lock when loading a vendor module,
300- e.g. updating static calls.
311+ :Comment: Exists because using kvm_lock leads to deadlock. kvm_lock is taken
312+ in notifiers, e.g. __kvmclock_cpufreq_notifier(), that may be invoked while
313+ cpu_hotplug_lock is held, e.g. from cpufreq_boost_trigger_state(), and many
314+ operations need to take cpu_hotplug_lock when loading a vendor module, e.g.
315+ updating static calls.
0 commit comments