Skip to content

Commit e6353d3

Browse files
author
Maxim Levitsky
committed
KVM: add kvm_lock_all_vcpus and kvm_trylock_all_vcpus
JIRA: https://issues.redhat.com/browse/RHEL-74410 commit e4a454c Author: Maxim Levitsky <mlevitsk@redhat.com> Date: Mon May 12 14:04:04 2025 -0400 KVM: add kvm_lock_all_vcpus and kvm_trylock_all_vcpus In a few cases, usually in the initialization code, KVM locks all vCPUs of a VM to ensure that userspace doesn't do funny things while KVM performs an operation that affects the whole VM. Until now, all these operations were implemented using custom code, and all of them share the same problem: Lockdep can't cope with simultaneous locking of a large number of locks of the same class. However if these locks are taken while another lock is already held, which is luckily the case, it is possible to take advantage of little known _nest_lock feature of lockdep which allows in this case to have an unlimited number of locks of same class to be taken. To implement this, create two functions: kvm_lock_all_vcpus() and kvm_trylock_all_vcpus() Both functions are needed because some code that will be replaced in the subsequent patches, uses mutex_trylock, instead of regular mutex_lock. Suggested-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Acked-by: Marc Zyngier <maz@kernel.org> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Message-ID: <20250512180407.659015-4-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
1 parent ffb8f73 commit e6353d3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

include/linux/kvm_host.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,10 @@ static inline struct kvm_vcpu *kvm_get_vcpu_by_id(struct kvm *kvm, int id)
10141014

10151015
void kvm_destroy_vcpus(struct kvm *kvm);
10161016

1017+
int kvm_trylock_all_vcpus(struct kvm *kvm);
1018+
int kvm_lock_all_vcpus(struct kvm *kvm);
1019+
void kvm_unlock_all_vcpus(struct kvm *kvm);
1020+
10171021
void vcpu_load(struct kvm_vcpu *vcpu);
10181022
void vcpu_put(struct kvm_vcpu *vcpu);
10191023

virt/kvm/kvm_main.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,65 @@ static int kvm_vm_release(struct inode *inode, struct file *filp)
13701370
return 0;
13711371
}
13721372

1373+
int kvm_trylock_all_vcpus(struct kvm *kvm)
1374+
{
1375+
struct kvm_vcpu *vcpu;
1376+
unsigned long i, j;
1377+
1378+
lockdep_assert_held(&kvm->lock);
1379+
1380+
kvm_for_each_vcpu(i, vcpu, kvm)
1381+
if (!mutex_trylock_nest_lock(&vcpu->mutex, &kvm->lock))
1382+
goto out_unlock;
1383+
return 0;
1384+
1385+
out_unlock:
1386+
kvm_for_each_vcpu(j, vcpu, kvm) {
1387+
if (i == j)
1388+
break;
1389+
mutex_unlock(&vcpu->mutex);
1390+
}
1391+
return -EINTR;
1392+
}
1393+
EXPORT_SYMBOL_GPL(kvm_trylock_all_vcpus);
1394+
1395+
int kvm_lock_all_vcpus(struct kvm *kvm)
1396+
{
1397+
struct kvm_vcpu *vcpu;
1398+
unsigned long i, j;
1399+
int r;
1400+
1401+
lockdep_assert_held(&kvm->lock);
1402+
1403+
kvm_for_each_vcpu(i, vcpu, kvm) {
1404+
r = mutex_lock_killable_nest_lock(&vcpu->mutex, &kvm->lock);
1405+
if (r)
1406+
goto out_unlock;
1407+
}
1408+
return 0;
1409+
1410+
out_unlock:
1411+
kvm_for_each_vcpu(j, vcpu, kvm) {
1412+
if (i == j)
1413+
break;
1414+
mutex_unlock(&vcpu->mutex);
1415+
}
1416+
return r;
1417+
}
1418+
EXPORT_SYMBOL_GPL(kvm_lock_all_vcpus);
1419+
1420+
void kvm_unlock_all_vcpus(struct kvm *kvm)
1421+
{
1422+
struct kvm_vcpu *vcpu;
1423+
unsigned long i;
1424+
1425+
lockdep_assert_held(&kvm->lock);
1426+
1427+
kvm_for_each_vcpu(i, vcpu, kvm)
1428+
mutex_unlock(&vcpu->mutex);
1429+
}
1430+
EXPORT_SYMBOL_GPL(kvm_unlock_all_vcpus);
1431+
13731432
/*
13741433
* Allocation size is twice as large as the actual dirty bitmap size.
13751434
* See kvm_vm_ioctl_get_dirty_log() why this is needed.

0 commit comments

Comments
 (0)