Skip to content

Commit 4777225

Browse files
sean-jcgregkh
authored andcommitted
KVM: Use dedicated mutex to protect kvm_usage_count to avoid deadlock
commit 44d1745 upstream. Use a dedicated mutex to guard kvm_usage_count to fix a potential deadlock on x86 due to a chain of locks and SRCU synchronizations. Translating the below lockdep splat, CPU1 #6 will wait on CPU0 #1, CPU0 #8 will wait on CPU2 #3, and CPU2 #7 will wait on CPU1 #4 (if there's a writer, due to the fairness of r/w semaphores). CPU0 CPU1 CPU2 1 lock(&kvm->slots_lock); 2 lock(&vcpu->mutex); 3 lock(&kvm->srcu); 4 lock(cpu_hotplug_lock); 5 lock(kvm_lock); 6 lock(&kvm->slots_lock); 7 lock(cpu_hotplug_lock); 8 sync(&kvm->srcu); Note, there are likely more potential deadlocks in KVM x86, e.g. the same pattern of taking cpu_hotplug_lock outside of kvm_lock likely exists with __kvmclock_cpufreq_notifier(): cpuhp_cpufreq_online() | -> cpufreq_online() | -> cpufreq_gov_performance_limits() | -> __cpufreq_driver_target() | -> __target_index() | -> cpufreq_freq_transition_begin() | -> cpufreq_notify_transition() | -> ... __kvmclock_cpufreq_notifier() But, actually triggering such deadlocks is beyond rare due to the combination of dependencies and timings involved. E.g. the cpufreq notifier is only used on older CPUs without a constant TSC, mucking with the NX hugepage mitigation while VMs are running is very uncommon, and doing so while also onlining/offlining a CPU (necessary to generate contention on cpu_hotplug_lock) would be even more unusual. The most robust solution to the general cpu_hotplug_lock issue is likely to switch vm_list to be an RCU-protected list, e.g. so that x86's cpufreq notifier doesn't to take kvm_lock. For now, settle for fixing the most blatant deadlock, as switching to an RCU-protected list is a much more involved change, but add a comment in locking.rst to call out that care needs to be taken when walking holding kvm_lock and walking vm_list. ====================================================== WARNING: possible circular locking dependency detected 6.10.0-smp--c257535a0c9d-pip #330 Tainted: G S O ------------------------------------------------------ tee/35048 is trying to acquire lock: ff6a80eced71e0a8 (&kvm->slots_lock){+.+.}-{3:3}, at: set_nx_huge_pages+0x179/0x1e0 [kvm] but task is already holding lock: ffffffffc07abb08 (kvm_lock){+.+.}-{3:3}, at: set_nx_huge_pages+0x14a/0x1e0 [kvm] which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #3 (kvm_lock){+.+.}-{3:3}: __mutex_lock+0x6a/0xb40 mutex_lock_nested+0x1f/0x30 kvm_dev_ioctl+0x4fb/0xe50 [kvm] __se_sys_ioctl+0x7b/0xd0 __x64_sys_ioctl+0x21/0x30 x64_sys_call+0x15d0/0x2e60 do_syscall_64+0x83/0x160 entry_SYSCALL_64_after_hwframe+0x76/0x7e -> #2 (cpu_hotplug_lock){++++}-{0:0}: cpus_read_lock+0x2e/0xb0 static_key_slow_inc+0x16/0x30 kvm_lapic_set_base+0x6a/0x1c0 [kvm] kvm_set_apic_base+0x8f/0xe0 [kvm] kvm_set_msr_common+0x9ae/0xf80 [kvm] vmx_set_msr+0xa54/0xbe0 [kvm_intel] __kvm_set_msr+0xb6/0x1a0 [kvm] kvm_arch_vcpu_ioctl+0xeca/0x10c0 [kvm] kvm_vcpu_ioctl+0x485/0x5b0 [kvm] __se_sys_ioctl+0x7b/0xd0 __x64_sys_ioctl+0x21/0x30 x64_sys_call+0x15d0/0x2e60 do_syscall_64+0x83/0x160 entry_SYSCALL_64_after_hwframe+0x76/0x7e -> #1 (&kvm->srcu){.+.+}-{0:0}: __synchronize_srcu+0x44/0x1a0 synchronize_srcu_expedited+0x21/0x30 kvm_swap_active_memslots+0x110/0x1c0 [kvm] kvm_set_memslot+0x360/0x620 [kvm] __kvm_set_memory_region+0x27b/0x300 [kvm] kvm_vm_ioctl_set_memory_region+0x43/0x60 [kvm] kvm_vm_ioctl+0x295/0x650 [kvm] __se_sys_ioctl+0x7b/0xd0 __x64_sys_ioctl+0x21/0x30 x64_sys_call+0x15d0/0x2e60 do_syscall_64+0x83/0x160 entry_SYSCALL_64_after_hwframe+0x76/0x7e -> #0 (&kvm->slots_lock){+.+.}-{3:3}: __lock_acquire+0x15ef/0x2e30 lock_acquire+0xe0/0x260 __mutex_lock+0x6a/0xb40 mutex_lock_nested+0x1f/0x30 set_nx_huge_pages+0x179/0x1e0 [kvm] param_attr_store+0x93/0x100 module_attr_store+0x22/0x40 sysfs_kf_write+0x81/0xb0 kernfs_fop_write_iter+0x133/0x1d0 vfs_write+0x28d/0x380 ksys_write+0x70/0xe0 __x64_sys_write+0x1f/0x30 x64_sys_call+0x281b/0x2e60 do_syscall_64+0x83/0x160 entry_SYSCALL_64_after_hwframe+0x76/0x7e Cc: Chao Gao <[email protected]> Fixes: 0bf5049 ("KVM: Drop kvm_count_lock and instead protect kvm_usage_count with kvm_lock") Cc: [email protected] Reviewed-by: Kai Huang <[email protected]> Acked-by: Kai Huang <[email protected]> Tested-by: Farrah Chen <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent beef335 commit 4777225

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

Documentation/virt/kvm/locking.rst

+23-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ KVM Lock Overview
99

1010
The 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,12 @@ 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+
- Taking cpus_read_lock() outside of kvm_lock is problematic, despite that
29+
being the official ordering, as it is quite easy to unknowingly trigger
30+
cpus_read_lock() while holding kvm_lock. Use caution when walking vm_list,
31+
e.g. avoid complex operations when possible.
32+
2733
For SRCU:
2834

2935
- ``synchronize_srcu(&kvm->srcu)`` is called inside critical sections
@@ -228,10 +234,17 @@ time it will be set using the Dirty tracking mechanism described above.
228234
:Type: mutex
229235
:Arch: any
230236
:Protects: - vm_list
231-
- kvm_usage_count
237+
238+
``kvm_usage_lock``
239+
^^^^^^^^^^^^^^^^^^
240+
241+
:Type: mutex
242+
:Arch: any
243+
:Protects: - kvm_usage_count
232244
- hardware virtualization enable/disable
233-
:Comment: KVM also disables CPU hotplug via cpus_read_lock() during
234-
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.
235248

236249
``kvm->mn_invalidate_lock``
237250
^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -291,11 +304,12 @@ time it will be set using the Dirty tracking mechanism described above.
291304
wakeup.
292305

293306
``vendor_module_lock``
294-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
307+
^^^^^^^^^^^^^^^^^^^^^^
295308
:Type: mutex
296309
:Arch: x86
297310
:Protects: loading a vendor module (kvm_amd or kvm_intel)
298-
:Comment: Exists because using kvm_lock leads to deadlock. cpu_hotplug_lock is
299-
taken outside of kvm_lock, e.g. in KVM's CPU online/offline callbacks, and
300-
many operations need to take cpu_hotplug_lock when loading a vendor module,
301-
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.

virt/kvm/kvm_main.c

+16-15
Original file line numberDiff line numberDiff line change
@@ -5174,6 +5174,7 @@ __visible bool kvm_rebooting;
51745174
EXPORT_SYMBOL_GPL(kvm_rebooting);
51755175

51765176
static DEFINE_PER_CPU(bool, hardware_enabled);
5177+
static DEFINE_MUTEX(kvm_usage_lock);
51775178
static int kvm_usage_count;
51785179

51795180
static int __hardware_enable_nolock(void)
@@ -5206,10 +5207,10 @@ static int kvm_online_cpu(unsigned int cpu)
52065207
* be enabled. Otherwise running VMs would encounter unrecoverable
52075208
* errors when scheduled to this CPU.
52085209
*/
5209-
mutex_lock(&kvm_lock);
5210+
mutex_lock(&kvm_usage_lock);
52105211
if (kvm_usage_count)
52115212
ret = __hardware_enable_nolock();
5212-
mutex_unlock(&kvm_lock);
5213+
mutex_unlock(&kvm_usage_lock);
52135214
return ret;
52145215
}
52155216

@@ -5229,10 +5230,10 @@ static void hardware_disable_nolock(void *junk)
52295230

52305231
static int kvm_offline_cpu(unsigned int cpu)
52315232
{
5232-
mutex_lock(&kvm_lock);
5233+
mutex_lock(&kvm_usage_lock);
52335234
if (kvm_usage_count)
52345235
hardware_disable_nolock(NULL);
5235-
mutex_unlock(&kvm_lock);
5236+
mutex_unlock(&kvm_usage_lock);
52365237
return 0;
52375238
}
52385239

@@ -5248,9 +5249,9 @@ static void hardware_disable_all_nolock(void)
52485249
static void hardware_disable_all(void)
52495250
{
52505251
cpus_read_lock();
5251-
mutex_lock(&kvm_lock);
5252+
mutex_lock(&kvm_usage_lock);
52525253
hardware_disable_all_nolock();
5253-
mutex_unlock(&kvm_lock);
5254+
mutex_unlock(&kvm_usage_lock);
52545255
cpus_read_unlock();
52555256
}
52565257

@@ -5281,7 +5282,7 @@ static int hardware_enable_all(void)
52815282
* enable hardware multiple times.
52825283
*/
52835284
cpus_read_lock();
5284-
mutex_lock(&kvm_lock);
5285+
mutex_lock(&kvm_usage_lock);
52855286

52865287
r = 0;
52875288

@@ -5295,7 +5296,7 @@ static int hardware_enable_all(void)
52955296
}
52965297
}
52975298

5298-
mutex_unlock(&kvm_lock);
5299+
mutex_unlock(&kvm_usage_lock);
52995300
cpus_read_unlock();
53005301

53015302
return r;
@@ -5323,13 +5324,13 @@ static int kvm_suspend(void)
53235324
{
53245325
/*
53255326
* Secondary CPUs and CPU hotplug are disabled across the suspend/resume
5326-
* callbacks, i.e. no need to acquire kvm_lock to ensure the usage count
5327-
* is stable. Assert that kvm_lock is not held to ensure the system
5328-
* isn't suspended while KVM is enabling hardware. Hardware enabling
5329-
* can be preempted, but the task cannot be frozen until it has dropped
5330-
* all locks (userspace tasks are frozen via a fake signal).
5327+
* callbacks, i.e. no need to acquire kvm_usage_lock to ensure the usage
5328+
* count is stable. Assert that kvm_usage_lock is not held to ensure
5329+
* the system isn't suspended while KVM is enabling hardware. Hardware
5330+
* enabling can be preempted, but the task cannot be frozen until it has
5331+
* dropped all locks (userspace tasks are frozen via a fake signal).
53315332
*/
5332-
lockdep_assert_not_held(&kvm_lock);
5333+
lockdep_assert_not_held(&kvm_usage_lock);
53335334
lockdep_assert_irqs_disabled();
53345335

53355336
if (kvm_usage_count)
@@ -5339,7 +5340,7 @@ static int kvm_suspend(void)
53395340

53405341
static void kvm_resume(void)
53415342
{
5342-
lockdep_assert_not_held(&kvm_lock);
5343+
lockdep_assert_not_held(&kvm_usage_lock);
53435344
lockdep_assert_irqs_disabled();
53445345

53455346
if (kvm_usage_count)

0 commit comments

Comments
 (0)