Commit eed3013f authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: Grab a reference to KVM for VM and vCPU stats file descriptors

Grab a reference to KVM prior to installing VM and vCPU stats file
descriptors to ensure the underlying VM and vCPU objects are not freed
until the last reference to any and all stats fds are dropped.

Note, the stats paths manually invoke fd_install() and so don't need to
grab a reference before creating the file.

Fixes: ce55c049 ("KVM: stats: Support binary stats retrieval for a VCPU")
Fixes: fcfe1bae ("KVM: stats: Support binary stats retrieval for a VM")
Reported-by: default avatarZheng Zhang <zheng.zhang@email.ucr.edu>
Closes: https://lore.kernel.org/all/CAC_GQSr3xzZaeZt85k_RCBd5kfiOve8qXo7a81Cq53LuVQ5r=Q@mail.gmail.com
Cc: stable@vger.kernel.org
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
Reviewed-by: default avatarKees Cook <keescook@chromium.org>
Message-Id: <20230711230131.648752-2-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent 3bcbc209
...@@ -4035,8 +4035,17 @@ static ssize_t kvm_vcpu_stats_read(struct file *file, char __user *user_buffer, ...@@ -4035,8 +4035,17 @@ static ssize_t kvm_vcpu_stats_read(struct file *file, char __user *user_buffer,
sizeof(vcpu->stat), user_buffer, size, offset); sizeof(vcpu->stat), user_buffer, size, offset);
} }
static int kvm_vcpu_stats_release(struct inode *inode, struct file *file)
{
struct kvm_vcpu *vcpu = file->private_data;
kvm_put_kvm(vcpu->kvm);
return 0;
}
static const struct file_operations kvm_vcpu_stats_fops = { static const struct file_operations kvm_vcpu_stats_fops = {
.read = kvm_vcpu_stats_read, .read = kvm_vcpu_stats_read,
.release = kvm_vcpu_stats_release,
.llseek = noop_llseek, .llseek = noop_llseek,
}; };
...@@ -4057,6 +4066,9 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu) ...@@ -4057,6 +4066,9 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)
put_unused_fd(fd); put_unused_fd(fd);
return PTR_ERR(file); return PTR_ERR(file);
} }
kvm_get_kvm(vcpu->kvm);
file->f_mode |= FMODE_PREAD; file->f_mode |= FMODE_PREAD;
fd_install(fd, file); fd_install(fd, file);
...@@ -4701,8 +4713,17 @@ static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer, ...@@ -4701,8 +4713,17 @@ static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer,
sizeof(kvm->stat), user_buffer, size, offset); sizeof(kvm->stat), user_buffer, size, offset);
} }
static int kvm_vm_stats_release(struct inode *inode, struct file *file)
{
struct kvm *kvm = file->private_data;
kvm_put_kvm(kvm);
return 0;
}
static const struct file_operations kvm_vm_stats_fops = { static const struct file_operations kvm_vm_stats_fops = {
.read = kvm_vm_stats_read, .read = kvm_vm_stats_read,
.release = kvm_vm_stats_release,
.llseek = noop_llseek, .llseek = noop_llseek,
}; };
...@@ -4721,6 +4742,9 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm) ...@@ -4721,6 +4742,9 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)
put_unused_fd(fd); put_unused_fd(fd);
return PTR_ERR(file); return PTR_ERR(file);
} }
kvm_get_kvm(kvm);
file->f_mode |= FMODE_PREAD; file->f_mode |= FMODE_PREAD;
fd_install(fd, file); fd_install(fd, file);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment