KVM: Introduce kvm_get_kvm_safe()
Introduce this safe version of kvm_get_kvm() so that it can be called even during vm destruction. Use it in kvm_debugfs_open() and remove the verbose comment. Prepare to be used elsewhere. Signed-off-by: Peter Xu <peterx@redhat.com> Message-Id: <20210625153214.43106-3-peterx@redhat.com> [Preserve the comment in kvm_debugfs_open. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
@@ -755,6 +755,7 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
|
|||||||
void kvm_exit(void);
|
void kvm_exit(void);
|
||||||
|
|
||||||
void kvm_get_kvm(struct kvm *kvm);
|
void kvm_get_kvm(struct kvm *kvm);
|
||||||
|
bool kvm_get_kvm_safe(struct kvm *kvm);
|
||||||
void kvm_put_kvm(struct kvm *kvm);
|
void kvm_put_kvm(struct kvm *kvm);
|
||||||
bool file_is_kvm(struct file *file);
|
bool file_is_kvm(struct file *file);
|
||||||
void kvm_put_kvm_no_destroy(struct kvm *kvm);
|
void kvm_put_kvm_no_destroy(struct kvm *kvm);
|
||||||
|
|||||||
@@ -1120,6 +1120,16 @@ void kvm_get_kvm(struct kvm *kvm)
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(kvm_get_kvm);
|
EXPORT_SYMBOL_GPL(kvm_get_kvm);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Make sure the vm is not during destruction, which is a safe version of
|
||||||
|
* kvm_get_kvm(). Return true if kvm referenced successfully, false otherwise.
|
||||||
|
*/
|
||||||
|
bool kvm_get_kvm_safe(struct kvm *kvm)
|
||||||
|
{
|
||||||
|
return refcount_inc_not_zero(&kvm->users_count);
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(kvm_get_kvm_safe);
|
||||||
|
|
||||||
void kvm_put_kvm(struct kvm *kvm)
|
void kvm_put_kvm(struct kvm *kvm)
|
||||||
{
|
{
|
||||||
if (refcount_dec_and_test(&kvm->users_count))
|
if (refcount_dec_and_test(&kvm->users_count))
|
||||||
@@ -4969,12 +4979,12 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
|
|||||||
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
|
struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
|
||||||
inode->i_private;
|
inode->i_private;
|
||||||
|
|
||||||
/* The debugfs files are a reference to the kvm struct which
|
/*
|
||||||
* is still valid when kvm_destroy_vm is called.
|
* The debugfs files are a reference to the kvm struct which
|
||||||
* To avoid the race between open and the removal of the debugfs
|
* is still valid when kvm_destroy_vm is called. kvm_get_kvm_safe
|
||||||
* directory we test against the users count.
|
* avoids the race between open and the removal of the debugfs directory.
|
||||||
*/
|
*/
|
||||||
if (!refcount_inc_not_zero(&stat_data->kvm->users_count))
|
if (!kvm_get_kvm_safe(stat_data->kvm))
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
|
|
||||||
if (simple_attr_open(inode, file, get,
|
if (simple_attr_open(inode, file, get,
|
||||||
|
|||||||
Reference in New Issue
Block a user